zsmalloc.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503
  1. /*
  2. * zsmalloc memory allocator
  3. *
  4. * Copyright (C) 2011 Nitin Gupta
  5. * Copyright (C) 2012, 2013 Minchan Kim
  6. *
  7. * This code is released using a dual license strategy: BSD/GPL
  8. * You can choose the license that better fits your requirements.
  9. *
  10. * Released under the terms of 3-clause BSD License
  11. * Released under the terms of GNU General Public License Version 2.0
  12. */
  13. /*
  14. * Following is how we use various fields and flags of underlying
  15. * struct page(s) to form a zspage.
  16. *
  17. * Usage of struct page fields:
  18. * page->private: points to zspage
  19. * page->freelist(index): links together all component pages of a zspage
  20. * For the huge page, this is always 0, so we use this field
  21. * to store handle.
  22. * page->units: first object offset in a subpage of zspage
  23. *
  24. * Usage of struct page flags:
  25. * PG_private: identifies the first component page
  26. * PG_owner_priv_1: identifies the huge component page
  27. *
  28. */
  29. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  30. #include <linux/module.h>
  31. #include <linux/kernel.h>
  32. #include <linux/sched.h>
  33. #include <linux/magic.h>
  34. #include <linux/bitops.h>
  35. #include <linux/errno.h>
  36. #include <linux/highmem.h>
  37. #include <linux/string.h>
  38. #include <linux/slab.h>
  39. #include <asm/tlbflush.h>
  40. #include <asm/pgtable.h>
  41. #include <linux/cpumask.h>
  42. #include <linux/cpu.h>
  43. #include <linux/vmalloc.h>
  44. #include <linux/preempt.h>
  45. #include <linux/spinlock.h>
  46. #include <linux/types.h>
  47. #include <linux/debugfs.h>
  48. #include <linux/zsmalloc.h>
  49. #include <linux/zpool.h>
  50. #include <linux/mount.h>
  51. #include <linux/migrate.h>
  52. #include <linux/pagemap.h>
  53. #define ZSPAGE_MAGIC 0x58
  54. /*
  55. * This must be power of 2 and greater than of equal to sizeof(link_free).
  56. * These two conditions ensure that any 'struct link_free' itself doesn't
  57. * span more than 1 page which avoids complex case of mapping 2 pages simply
  58. * to restore link_free pointer values.
  59. */
  60. #define ZS_ALIGN 8
  61. /*
  62. * A single 'zspage' is composed of up to 2^N discontiguous 0-order (single)
  63. * pages. ZS_MAX_ZSPAGE_ORDER defines upper limit on N.
  64. */
  65. #define ZS_MAX_ZSPAGE_ORDER 2
  66. #define ZS_MAX_PAGES_PER_ZSPAGE (_AC(1, UL) << ZS_MAX_ZSPAGE_ORDER)
  67. #define ZS_HANDLE_SIZE (sizeof(unsigned long))
  68. /*
  69. * Object location (<PFN>, <obj_idx>) is encoded as
  70. * as single (unsigned long) handle value.
  71. *
  72. * Note that object index <obj_idx> starts from 0.
  73. *
  74. * This is made more complicated by various memory models and PAE.
  75. */
  76. #ifndef MAX_PHYSMEM_BITS
  77. #ifdef CONFIG_HIGHMEM64G
  78. #define MAX_PHYSMEM_BITS 36
  79. #else /* !CONFIG_HIGHMEM64G */
  80. /*
  81. * If this definition of MAX_PHYSMEM_BITS is used, OBJ_INDEX_BITS will just
  82. * be PAGE_SHIFT
  83. */
  84. #define MAX_PHYSMEM_BITS BITS_PER_LONG
  85. #endif
  86. #endif
  87. #define _PFN_BITS (MAX_PHYSMEM_BITS - PAGE_SHIFT)
  88. /*
  89. * Memory for allocating for handle keeps object position by
  90. * encoding <page, obj_idx> and the encoded value has a room
  91. * in least bit(ie, look at obj_to_location).
  92. * We use the bit to synchronize between object access by
  93. * user and migration.
  94. */
  95. #define HANDLE_PIN_BIT 0
  96. /*
  97. * Head in allocated object should have OBJ_ALLOCATED_TAG
  98. * to identify the object was allocated or not.
  99. * It's okay to add the status bit in the least bit because
  100. * header keeps handle which is 4byte-aligned address so we
  101. * have room for two bit at least.
  102. */
  103. #define OBJ_ALLOCATED_TAG 1
  104. #define OBJ_TAG_BITS 1
  105. #define OBJ_INDEX_BITS (BITS_PER_LONG - _PFN_BITS - OBJ_TAG_BITS)
  106. #define OBJ_INDEX_MASK ((_AC(1, UL) << OBJ_INDEX_BITS) - 1)
  107. #define FULLNESS_BITS 2
  108. #define CLASS_BITS 8
  109. #define ISOLATED_BITS 3
  110. #define MAGIC_VAL_BITS 8
  111. #define MAX(a, b) ((a) >= (b) ? (a) : (b))
  112. /* ZS_MIN_ALLOC_SIZE must be multiple of ZS_ALIGN */
  113. #define ZS_MIN_ALLOC_SIZE \
  114. MAX(32, (ZS_MAX_PAGES_PER_ZSPAGE << PAGE_SHIFT >> OBJ_INDEX_BITS))
  115. /* each chunk includes extra space to keep handle */
  116. #define ZS_MAX_ALLOC_SIZE PAGE_SIZE
  117. /*
  118. * On systems with 4K page size, this gives 255 size classes! There is a
  119. * trader-off here:
  120. * - Large number of size classes is potentially wasteful as free page are
  121. * spread across these classes
  122. * - Small number of size classes causes large internal fragmentation
  123. * - Probably its better to use specific size classes (empirically
  124. * determined). NOTE: all those class sizes must be set as multiple of
  125. * ZS_ALIGN to make sure link_free itself never has to span 2 pages.
  126. *
  127. * ZS_MIN_ALLOC_SIZE and ZS_SIZE_CLASS_DELTA must be multiple of ZS_ALIGN
  128. * (reason above)
  129. */
  130. #define ZS_SIZE_CLASS_DELTA (PAGE_SIZE >> CLASS_BITS)
  131. #define ZS_SIZE_CLASSES (DIV_ROUND_UP(ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE, \
  132. ZS_SIZE_CLASS_DELTA) + 1)
  133. enum fullness_group {
  134. ZS_EMPTY,
  135. ZS_ALMOST_EMPTY,
  136. ZS_ALMOST_FULL,
  137. ZS_FULL,
  138. NR_ZS_FULLNESS,
  139. };
  140. enum zs_stat_type {
  141. CLASS_EMPTY,
  142. CLASS_ALMOST_EMPTY,
  143. CLASS_ALMOST_FULL,
  144. CLASS_FULL,
  145. OBJ_ALLOCATED,
  146. OBJ_USED,
  147. NR_ZS_STAT_TYPE,
  148. };
  149. struct zs_size_stat {
  150. unsigned long objs[NR_ZS_STAT_TYPE];
  151. };
  152. #ifdef CONFIG_ZSMALLOC_STAT
  153. static struct dentry *zs_stat_root;
  154. #endif
  155. #ifdef CONFIG_COMPACTION
  156. static struct vfsmount *zsmalloc_mnt;
  157. #endif
  158. /*
  159. * We assign a page to ZS_ALMOST_EMPTY fullness group when:
  160. * n <= N / f, where
  161. * n = number of allocated objects
  162. * N = total number of objects zspage can store
  163. * f = fullness_threshold_frac
  164. *
  165. * Similarly, we assign zspage to:
  166. * ZS_ALMOST_FULL when n > N / f
  167. * ZS_EMPTY when n == 0
  168. * ZS_FULL when n == N
  169. *
  170. * (see: fix_fullness_group())
  171. */
  172. static const int fullness_threshold_frac = 4;
  173. struct size_class {
  174. spinlock_t lock;
  175. struct list_head fullness_list[NR_ZS_FULLNESS];
  176. /*
  177. * Size of objects stored in this class. Must be multiple
  178. * of ZS_ALIGN.
  179. */
  180. int size;
  181. int objs_per_zspage;
  182. /* Number of PAGE_SIZE sized pages to combine to form a 'zspage' */
  183. int pages_per_zspage;
  184. unsigned int index;
  185. struct zs_size_stat stats;
  186. };
  187. /* huge object: pages_per_zspage == 1 && maxobj_per_zspage == 1 */
  188. static void SetPageHugeObject(struct page *page)
  189. {
  190. SetPageOwnerPriv1(page);
  191. }
  192. static void ClearPageHugeObject(struct page *page)
  193. {
  194. ClearPageOwnerPriv1(page);
  195. }
  196. static int PageHugeObject(struct page *page)
  197. {
  198. return PageOwnerPriv1(page);
  199. }
  200. /*
  201. * Placed within free objects to form a singly linked list.
  202. * For every zspage, zspage->freeobj gives head of this list.
  203. *
  204. * This must be power of 2 and less than or equal to ZS_ALIGN
  205. */
  206. struct link_free {
  207. union {
  208. /*
  209. * Free object index;
  210. * It's valid for non-allocated object
  211. */
  212. unsigned long next;
  213. /*
  214. * Handle of allocated object.
  215. */
  216. unsigned long handle;
  217. };
  218. };
  219. struct zs_pool {
  220. const char *name;
  221. struct size_class *size_class[ZS_SIZE_CLASSES];
  222. struct kmem_cache *handle_cachep;
  223. struct kmem_cache *zspage_cachep;
  224. atomic_long_t pages_allocated;
  225. struct zs_pool_stats stats;
  226. /* Compact classes */
  227. struct shrinker shrinker;
  228. /*
  229. * To signify that register_shrinker() was successful
  230. * and unregister_shrinker() will not Oops.
  231. */
  232. bool shrinker_enabled;
  233. #ifdef CONFIG_ZSMALLOC_STAT
  234. struct dentry *stat_dentry;
  235. #endif
  236. #ifdef CONFIG_COMPACTION
  237. struct inode *inode;
  238. struct work_struct free_work;
  239. #endif
  240. };
  241. struct zspage {
  242. struct {
  243. unsigned int fullness:FULLNESS_BITS;
  244. unsigned int class:CLASS_BITS + 1;
  245. unsigned int isolated:ISOLATED_BITS;
  246. unsigned int magic:MAGIC_VAL_BITS;
  247. };
  248. unsigned int inuse;
  249. unsigned int freeobj;
  250. struct page *first_page;
  251. struct list_head list; /* fullness list */
  252. #ifdef CONFIG_COMPACTION
  253. rwlock_t lock;
  254. #endif
  255. };
  256. struct mapping_area {
  257. #ifdef CONFIG_PGTABLE_MAPPING
  258. struct vm_struct *vm; /* vm area for mapping object that span pages */
  259. #else
  260. char *vm_buf; /* copy buffer for objects that span pages */
  261. #endif
  262. char *vm_addr; /* address of kmap_atomic()'ed pages */
  263. enum zs_mapmode vm_mm; /* mapping mode */
  264. };
  265. #ifdef CONFIG_COMPACTION
  266. static int zs_register_migration(struct zs_pool *pool);
  267. static void zs_unregister_migration(struct zs_pool *pool);
  268. static void migrate_lock_init(struct zspage *zspage);
  269. static void migrate_read_lock(struct zspage *zspage);
  270. static void migrate_read_unlock(struct zspage *zspage);
  271. static void kick_deferred_free(struct zs_pool *pool);
  272. static void init_deferred_free(struct zs_pool *pool);
  273. static void SetZsPageMovable(struct zs_pool *pool, struct zspage *zspage);
  274. #else
  275. static int zsmalloc_mount(void) { return 0; }
  276. static void zsmalloc_unmount(void) {}
  277. static int zs_register_migration(struct zs_pool *pool) { return 0; }
  278. static void zs_unregister_migration(struct zs_pool *pool) {}
  279. static void migrate_lock_init(struct zspage *zspage) {}
  280. static void migrate_read_lock(struct zspage *zspage) {}
  281. static void migrate_read_unlock(struct zspage *zspage) {}
  282. static void kick_deferred_free(struct zs_pool *pool) {}
  283. static void init_deferred_free(struct zs_pool *pool) {}
  284. static void SetZsPageMovable(struct zs_pool *pool, struct zspage *zspage) {}
  285. #endif
  286. static int create_cache(struct zs_pool *pool)
  287. {
  288. pool->handle_cachep = kmem_cache_create("zs_handle", ZS_HANDLE_SIZE,
  289. 0, 0, NULL);
  290. if (!pool->handle_cachep)
  291. return 1;
  292. pool->zspage_cachep = kmem_cache_create("zspage", sizeof(struct zspage),
  293. 0, 0, NULL);
  294. if (!pool->zspage_cachep) {
  295. kmem_cache_destroy(pool->handle_cachep);
  296. pool->handle_cachep = NULL;
  297. return 1;
  298. }
  299. return 0;
  300. }
  301. static void destroy_cache(struct zs_pool *pool)
  302. {
  303. kmem_cache_destroy(pool->handle_cachep);
  304. kmem_cache_destroy(pool->zspage_cachep);
  305. }
  306. static unsigned long cache_alloc_handle(struct zs_pool *pool, gfp_t gfp)
  307. {
  308. return (unsigned long)kmem_cache_alloc(pool->handle_cachep,
  309. gfp & ~(__GFP_HIGHMEM|__GFP_MOVABLE));
  310. }
  311. static void cache_free_handle(struct zs_pool *pool, unsigned long handle)
  312. {
  313. kmem_cache_free(pool->handle_cachep, (void *)handle);
  314. }
  315. static struct zspage *cache_alloc_zspage(struct zs_pool *pool, gfp_t flags)
  316. {
  317. return kmem_cache_alloc(pool->zspage_cachep,
  318. flags & ~(__GFP_HIGHMEM|__GFP_MOVABLE));
  319. }
  320. static void cache_free_zspage(struct zs_pool *pool, struct zspage *zspage)
  321. {
  322. kmem_cache_free(pool->zspage_cachep, zspage);
  323. }
  324. static void record_obj(unsigned long handle, unsigned long obj)
  325. {
  326. /*
  327. * lsb of @obj represents handle lock while other bits
  328. * represent object value the handle is pointing so
  329. * updating shouldn't do store tearing.
  330. */
  331. WRITE_ONCE(*(unsigned long *)handle, obj);
  332. }
  333. /* zpool driver */
  334. #ifdef CONFIG_ZPOOL
  335. static void *zs_zpool_create(const char *name, gfp_t gfp,
  336. const struct zpool_ops *zpool_ops,
  337. struct zpool *zpool)
  338. {
  339. /*
  340. * Ignore global gfp flags: zs_malloc() may be invoked from
  341. * different contexts and its caller must provide a valid
  342. * gfp mask.
  343. */
  344. return zs_create_pool(name);
  345. }
  346. static void zs_zpool_destroy(void *pool)
  347. {
  348. zs_destroy_pool(pool);
  349. }
  350. static int zs_zpool_malloc(void *pool, size_t size, gfp_t gfp,
  351. unsigned long *handle)
  352. {
  353. *handle = zs_malloc(pool, size, gfp);
  354. return *handle ? 0 : -1;
  355. }
  356. static void zs_zpool_free(void *pool, unsigned long handle)
  357. {
  358. zs_free(pool, handle);
  359. }
  360. static int zs_zpool_shrink(void *pool, unsigned int pages,
  361. unsigned int *reclaimed)
  362. {
  363. return -EINVAL;
  364. }
  365. static void *zs_zpool_map(void *pool, unsigned long handle,
  366. enum zpool_mapmode mm)
  367. {
  368. enum zs_mapmode zs_mm;
  369. switch (mm) {
  370. case ZPOOL_MM_RO:
  371. zs_mm = ZS_MM_RO;
  372. break;
  373. case ZPOOL_MM_WO:
  374. zs_mm = ZS_MM_WO;
  375. break;
  376. case ZPOOL_MM_RW: /* fallthru */
  377. default:
  378. zs_mm = ZS_MM_RW;
  379. break;
  380. }
  381. return zs_map_object(pool, handle, zs_mm);
  382. }
  383. static void zs_zpool_unmap(void *pool, unsigned long handle)
  384. {
  385. zs_unmap_object(pool, handle);
  386. }
  387. static u64 zs_zpool_total_size(void *pool)
  388. {
  389. return zs_get_total_pages(pool) << PAGE_SHIFT;
  390. }
  391. static struct zpool_driver zs_zpool_driver = {
  392. .type = "zsmalloc",
  393. .owner = THIS_MODULE,
  394. .create = zs_zpool_create,
  395. .destroy = zs_zpool_destroy,
  396. .malloc = zs_zpool_malloc,
  397. .free = zs_zpool_free,
  398. .shrink = zs_zpool_shrink,
  399. .map = zs_zpool_map,
  400. .unmap = zs_zpool_unmap,
  401. .total_size = zs_zpool_total_size,
  402. };
  403. MODULE_ALIAS("zpool-zsmalloc");
  404. #endif /* CONFIG_ZPOOL */
  405. /* per-cpu VM mapping areas for zspage accesses that cross page boundaries */
  406. static DEFINE_PER_CPU(struct mapping_area, zs_map_area);
  407. static bool is_zspage_isolated(struct zspage *zspage)
  408. {
  409. return zspage->isolated;
  410. }
  411. static __maybe_unused int is_first_page(struct page *page)
  412. {
  413. return PagePrivate(page);
  414. }
  415. /* Protected by class->lock */
  416. static inline int get_zspage_inuse(struct zspage *zspage)
  417. {
  418. return zspage->inuse;
  419. }
  420. static inline void set_zspage_inuse(struct zspage *zspage, int val)
  421. {
  422. zspage->inuse = val;
  423. }
  424. static inline void mod_zspage_inuse(struct zspage *zspage, int val)
  425. {
  426. zspage->inuse += val;
  427. }
  428. static inline struct page *get_first_page(struct zspage *zspage)
  429. {
  430. struct page *first_page = zspage->first_page;
  431. VM_BUG_ON_PAGE(!is_first_page(first_page), first_page);
  432. return first_page;
  433. }
  434. static inline int get_first_obj_offset(struct page *page)
  435. {
  436. return page->units;
  437. }
  438. static inline void set_first_obj_offset(struct page *page, int offset)
  439. {
  440. page->units = offset;
  441. }
  442. static inline unsigned int get_freeobj(struct zspage *zspage)
  443. {
  444. return zspage->freeobj;
  445. }
  446. static inline void set_freeobj(struct zspage *zspage, unsigned int obj)
  447. {
  448. zspage->freeobj = obj;
  449. }
  450. static void get_zspage_mapping(struct zspage *zspage,
  451. unsigned int *class_idx,
  452. enum fullness_group *fullness)
  453. {
  454. BUG_ON(zspage->magic != ZSPAGE_MAGIC);
  455. *fullness = zspage->fullness;
  456. *class_idx = zspage->class;
  457. }
  458. static void set_zspage_mapping(struct zspage *zspage,
  459. unsigned int class_idx,
  460. enum fullness_group fullness)
  461. {
  462. zspage->class = class_idx;
  463. zspage->fullness = fullness;
  464. }
  465. /*
  466. * zsmalloc divides the pool into various size classes where each
  467. * class maintains a list of zspages where each zspage is divided
  468. * into equal sized chunks. Each allocation falls into one of these
  469. * classes depending on its size. This function returns index of the
  470. * size class which has chunk size big enough to hold the give size.
  471. */
  472. static int get_size_class_index(int size)
  473. {
  474. int idx = 0;
  475. if (likely(size > ZS_MIN_ALLOC_SIZE))
  476. idx = DIV_ROUND_UP(size - ZS_MIN_ALLOC_SIZE,
  477. ZS_SIZE_CLASS_DELTA);
  478. return min_t(int, ZS_SIZE_CLASSES - 1, idx);
  479. }
  480. static inline void zs_stat_inc(struct size_class *class,
  481. enum zs_stat_type type, unsigned long cnt)
  482. {
  483. class->stats.objs[type] += cnt;
  484. }
  485. static inline void zs_stat_dec(struct size_class *class,
  486. enum zs_stat_type type, unsigned long cnt)
  487. {
  488. class->stats.objs[type] -= cnt;
  489. }
  490. static inline unsigned long zs_stat_get(struct size_class *class,
  491. enum zs_stat_type type)
  492. {
  493. return class->stats.objs[type];
  494. }
  495. #ifdef CONFIG_ZSMALLOC_STAT
  496. static void __init zs_stat_init(void)
  497. {
  498. if (!debugfs_initialized()) {
  499. pr_warn("debugfs not available, stat dir not created\n");
  500. return;
  501. }
  502. zs_stat_root = debugfs_create_dir("zsmalloc", NULL);
  503. if (!zs_stat_root)
  504. pr_warn("debugfs 'zsmalloc' stat dir creation failed\n");
  505. }
  506. static void __exit zs_stat_exit(void)
  507. {
  508. debugfs_remove_recursive(zs_stat_root);
  509. }
  510. static unsigned long zs_can_compact(struct size_class *class);
  511. static int zs_stats_size_show(struct seq_file *s, void *v)
  512. {
  513. int i;
  514. struct zs_pool *pool = s->private;
  515. struct size_class *class;
  516. int objs_per_zspage;
  517. unsigned long class_almost_full, class_almost_empty;
  518. unsigned long obj_allocated, obj_used, pages_used, freeable;
  519. unsigned long total_class_almost_full = 0, total_class_almost_empty = 0;
  520. unsigned long total_objs = 0, total_used_objs = 0, total_pages = 0;
  521. unsigned long total_freeable = 0;
  522. seq_printf(s, " %5s %5s %11s %12s %13s %10s %10s %16s %8s\n",
  523. "class", "size", "almost_full", "almost_empty",
  524. "obj_allocated", "obj_used", "pages_used",
  525. "pages_per_zspage", "freeable");
  526. for (i = 0; i < ZS_SIZE_CLASSES; i++) {
  527. class = pool->size_class[i];
  528. if (class->index != i)
  529. continue;
  530. spin_lock(&class->lock);
  531. class_almost_full = zs_stat_get(class, CLASS_ALMOST_FULL);
  532. class_almost_empty = zs_stat_get(class, CLASS_ALMOST_EMPTY);
  533. obj_allocated = zs_stat_get(class, OBJ_ALLOCATED);
  534. obj_used = zs_stat_get(class, OBJ_USED);
  535. freeable = zs_can_compact(class);
  536. spin_unlock(&class->lock);
  537. objs_per_zspage = class->objs_per_zspage;
  538. pages_used = obj_allocated / objs_per_zspage *
  539. class->pages_per_zspage;
  540. seq_printf(s, " %5u %5u %11lu %12lu %13lu"
  541. " %10lu %10lu %16d %8lu\n",
  542. i, class->size, class_almost_full, class_almost_empty,
  543. obj_allocated, obj_used, pages_used,
  544. class->pages_per_zspage, freeable);
  545. total_class_almost_full += class_almost_full;
  546. total_class_almost_empty += class_almost_empty;
  547. total_objs += obj_allocated;
  548. total_used_objs += obj_used;
  549. total_pages += pages_used;
  550. total_freeable += freeable;
  551. }
  552. seq_puts(s, "\n");
  553. seq_printf(s, " %5s %5s %11lu %12lu %13lu %10lu %10lu %16s %8lu\n",
  554. "Total", "", total_class_almost_full,
  555. total_class_almost_empty, total_objs,
  556. total_used_objs, total_pages, "", total_freeable);
  557. return 0;
  558. }
  559. static int zs_stats_size_open(struct inode *inode, struct file *file)
  560. {
  561. return single_open(file, zs_stats_size_show, inode->i_private);
  562. }
  563. static const struct file_operations zs_stat_size_ops = {
  564. .open = zs_stats_size_open,
  565. .read = seq_read,
  566. .llseek = seq_lseek,
  567. .release = single_release,
  568. };
  569. static void zs_pool_stat_create(struct zs_pool *pool, const char *name)
  570. {
  571. struct dentry *entry;
  572. if (!zs_stat_root) {
  573. pr_warn("no root stat dir, not creating <%s> stat dir\n", name);
  574. return;
  575. }
  576. entry = debugfs_create_dir(name, zs_stat_root);
  577. if (!entry) {
  578. pr_warn("debugfs dir <%s> creation failed\n", name);
  579. return;
  580. }
  581. pool->stat_dentry = entry;
  582. entry = debugfs_create_file("classes", S_IFREG | S_IRUGO,
  583. pool->stat_dentry, pool, &zs_stat_size_ops);
  584. if (!entry) {
  585. pr_warn("%s: debugfs file entry <%s> creation failed\n",
  586. name, "classes");
  587. debugfs_remove_recursive(pool->stat_dentry);
  588. pool->stat_dentry = NULL;
  589. }
  590. }
  591. static void zs_pool_stat_destroy(struct zs_pool *pool)
  592. {
  593. debugfs_remove_recursive(pool->stat_dentry);
  594. }
  595. #else /* CONFIG_ZSMALLOC_STAT */
  596. static void __init zs_stat_init(void)
  597. {
  598. }
  599. static void __exit zs_stat_exit(void)
  600. {
  601. }
  602. static inline void zs_pool_stat_create(struct zs_pool *pool, const char *name)
  603. {
  604. }
  605. static inline void zs_pool_stat_destroy(struct zs_pool *pool)
  606. {
  607. }
  608. #endif
  609. /*
  610. * For each size class, zspages are divided into different groups
  611. * depending on how "full" they are. This was done so that we could
  612. * easily find empty or nearly empty zspages when we try to shrink
  613. * the pool (not yet implemented). This function returns fullness
  614. * status of the given page.
  615. */
  616. static enum fullness_group get_fullness_group(struct size_class *class,
  617. struct zspage *zspage)
  618. {
  619. int inuse, objs_per_zspage;
  620. enum fullness_group fg;
  621. inuse = get_zspage_inuse(zspage);
  622. objs_per_zspage = class->objs_per_zspage;
  623. if (inuse == 0)
  624. fg = ZS_EMPTY;
  625. else if (inuse == objs_per_zspage)
  626. fg = ZS_FULL;
  627. else if (inuse <= 3 * objs_per_zspage / fullness_threshold_frac)
  628. fg = ZS_ALMOST_EMPTY;
  629. else
  630. fg = ZS_ALMOST_FULL;
  631. return fg;
  632. }
  633. /*
  634. * Each size class maintains various freelists and zspages are assigned
  635. * to one of these freelists based on the number of live objects they
  636. * have. This functions inserts the given zspage into the freelist
  637. * identified by <class, fullness_group>.
  638. */
  639. static void insert_zspage(struct size_class *class,
  640. struct zspage *zspage,
  641. enum fullness_group fullness)
  642. {
  643. struct zspage *head;
  644. zs_stat_inc(class, fullness, 1);
  645. head = list_first_entry_or_null(&class->fullness_list[fullness],
  646. struct zspage, list);
  647. /*
  648. * We want to see more ZS_FULL pages and less almost empty/full.
  649. * Put pages with higher ->inuse first.
  650. */
  651. if (head) {
  652. if (get_zspage_inuse(zspage) < get_zspage_inuse(head)) {
  653. list_add(&zspage->list, &head->list);
  654. return;
  655. }
  656. }
  657. list_add(&zspage->list, &class->fullness_list[fullness]);
  658. }
  659. /*
  660. * This function removes the given zspage from the freelist identified
  661. * by <class, fullness_group>.
  662. */
  663. static void remove_zspage(struct size_class *class,
  664. struct zspage *zspage,
  665. enum fullness_group fullness)
  666. {
  667. VM_BUG_ON(list_empty(&class->fullness_list[fullness]));
  668. VM_BUG_ON(is_zspage_isolated(zspage));
  669. list_del_init(&zspage->list);
  670. zs_stat_dec(class, fullness, 1);
  671. }
  672. /*
  673. * Each size class maintains zspages in different fullness groups depending
  674. * on the number of live objects they contain. When allocating or freeing
  675. * objects, the fullness status of the page can change, say, from ALMOST_FULL
  676. * to ALMOST_EMPTY when freeing an object. This function checks if such
  677. * a status change has occurred for the given page and accordingly moves the
  678. * page from the freelist of the old fullness group to that of the new
  679. * fullness group.
  680. */
  681. static enum fullness_group fix_fullness_group(struct size_class *class,
  682. struct zspage *zspage)
  683. {
  684. int class_idx;
  685. enum fullness_group currfg, newfg;
  686. get_zspage_mapping(zspage, &class_idx, &currfg);
  687. newfg = get_fullness_group(class, zspage);
  688. if (newfg == currfg)
  689. goto out;
  690. if (!is_zspage_isolated(zspage)) {
  691. remove_zspage(class, zspage, currfg);
  692. insert_zspage(class, zspage, newfg);
  693. }
  694. set_zspage_mapping(zspage, class_idx, newfg);
  695. out:
  696. return newfg;
  697. }
  698. /*
  699. * We have to decide on how many pages to link together
  700. * to form a zspage for each size class. This is important
  701. * to reduce wastage due to unusable space left at end of
  702. * each zspage which is given as:
  703. * wastage = Zp % class_size
  704. * usage = Zp - wastage
  705. * where Zp = zspage size = k * PAGE_SIZE where k = 1, 2, ...
  706. *
  707. * For example, for size class of 3/8 * PAGE_SIZE, we should
  708. * link together 3 PAGE_SIZE sized pages to form a zspage
  709. * since then we can perfectly fit in 8 such objects.
  710. */
  711. static int get_pages_per_zspage(int class_size)
  712. {
  713. int i, max_usedpc = 0;
  714. /* zspage order which gives maximum used size per KB */
  715. int max_usedpc_order = 1;
  716. for (i = 1; i <= ZS_MAX_PAGES_PER_ZSPAGE; i++) {
  717. int zspage_size;
  718. int waste, usedpc;
  719. zspage_size = i * PAGE_SIZE;
  720. waste = zspage_size % class_size;
  721. usedpc = (zspage_size - waste) * 100 / zspage_size;
  722. if (usedpc > max_usedpc) {
  723. max_usedpc = usedpc;
  724. max_usedpc_order = i;
  725. }
  726. }
  727. return max_usedpc_order;
  728. }
  729. static struct zspage *get_zspage(struct page *page)
  730. {
  731. struct zspage *zspage = (struct zspage *)page->private;
  732. BUG_ON(zspage->magic != ZSPAGE_MAGIC);
  733. return zspage;
  734. }
  735. static struct page *get_next_page(struct page *page)
  736. {
  737. if (unlikely(PageHugeObject(page)))
  738. return NULL;
  739. return page->freelist;
  740. }
  741. /**
  742. * obj_to_location - get (<page>, <obj_idx>) from encoded object value
  743. * @page: page object resides in zspage
  744. * @obj_idx: object index
  745. */
  746. static void obj_to_location(unsigned long obj, struct page **page,
  747. unsigned int *obj_idx)
  748. {
  749. obj >>= OBJ_TAG_BITS;
  750. *page = pfn_to_page(obj >> OBJ_INDEX_BITS);
  751. *obj_idx = (obj & OBJ_INDEX_MASK);
  752. }
  753. /**
  754. * location_to_obj - get obj value encoded from (<page>, <obj_idx>)
  755. * @page: page object resides in zspage
  756. * @obj_idx: object index
  757. */
  758. static unsigned long location_to_obj(struct page *page, unsigned int obj_idx)
  759. {
  760. unsigned long obj;
  761. obj = page_to_pfn(page) << OBJ_INDEX_BITS;
  762. obj |= obj_idx & OBJ_INDEX_MASK;
  763. obj <<= OBJ_TAG_BITS;
  764. return obj;
  765. }
  766. static unsigned long handle_to_obj(unsigned long handle)
  767. {
  768. return *(unsigned long *)handle;
  769. }
  770. static unsigned long obj_to_head(struct page *page, void *obj)
  771. {
  772. if (unlikely(PageHugeObject(page))) {
  773. VM_BUG_ON_PAGE(!is_first_page(page), page);
  774. return page->index;
  775. } else
  776. return *(unsigned long *)obj;
  777. }
  778. static inline int testpin_tag(unsigned long handle)
  779. {
  780. return bit_spin_is_locked(HANDLE_PIN_BIT, (unsigned long *)handle);
  781. }
  782. static inline int trypin_tag(unsigned long handle)
  783. {
  784. return bit_spin_trylock(HANDLE_PIN_BIT, (unsigned long *)handle);
  785. }
  786. static void pin_tag(unsigned long handle)
  787. {
  788. bit_spin_lock(HANDLE_PIN_BIT, (unsigned long *)handle);
  789. }
  790. static void unpin_tag(unsigned long handle)
  791. {
  792. bit_spin_unlock(HANDLE_PIN_BIT, (unsigned long *)handle);
  793. }
  794. static void reset_page(struct page *page)
  795. {
  796. __ClearPageMovable(page);
  797. ClearPagePrivate(page);
  798. set_page_private(page, 0);
  799. page_mapcount_reset(page);
  800. ClearPageHugeObject(page);
  801. page->freelist = NULL;
  802. }
  803. /*
  804. * To prevent zspage destroy during migration, zspage freeing should
  805. * hold locks of all pages in the zspage.
  806. */
  807. void lock_zspage(struct zspage *zspage)
  808. {
  809. struct page *page = get_first_page(zspage);
  810. do {
  811. lock_page(page);
  812. } while ((page = get_next_page(page)) != NULL);
  813. }
  814. int trylock_zspage(struct zspage *zspage)
  815. {
  816. struct page *cursor, *fail;
  817. for (cursor = get_first_page(zspage); cursor != NULL; cursor =
  818. get_next_page(cursor)) {
  819. if (!trylock_page(cursor)) {
  820. fail = cursor;
  821. goto unlock;
  822. }
  823. }
  824. return 1;
  825. unlock:
  826. for (cursor = get_first_page(zspage); cursor != fail; cursor =
  827. get_next_page(cursor))
  828. unlock_page(cursor);
  829. return 0;
  830. }
  831. static void __free_zspage(struct zs_pool *pool, struct size_class *class,
  832. struct zspage *zspage)
  833. {
  834. struct page *page, *next;
  835. enum fullness_group fg;
  836. unsigned int class_idx;
  837. get_zspage_mapping(zspage, &class_idx, &fg);
  838. assert_spin_locked(&class->lock);
  839. VM_BUG_ON(get_zspage_inuse(zspage));
  840. VM_BUG_ON(fg != ZS_EMPTY);
  841. next = page = get_first_page(zspage);
  842. do {
  843. VM_BUG_ON_PAGE(!PageLocked(page), page);
  844. next = get_next_page(page);
  845. reset_page(page);
  846. unlock_page(page);
  847. dec_zone_page_state(page, NR_ZSPAGES);
  848. put_page(page);
  849. page = next;
  850. } while (page != NULL);
  851. cache_free_zspage(pool, zspage);
  852. zs_stat_dec(class, OBJ_ALLOCATED, class->objs_per_zspage);
  853. atomic_long_sub(class->pages_per_zspage,
  854. &pool->pages_allocated);
  855. }
  856. static void free_zspage(struct zs_pool *pool, struct size_class *class,
  857. struct zspage *zspage)
  858. {
  859. VM_BUG_ON(get_zspage_inuse(zspage));
  860. VM_BUG_ON(list_empty(&zspage->list));
  861. if (!trylock_zspage(zspage)) {
  862. kick_deferred_free(pool);
  863. return;
  864. }
  865. remove_zspage(class, zspage, ZS_EMPTY);
  866. __free_zspage(pool, class, zspage);
  867. }
  868. /* Initialize a newly allocated zspage */
  869. static void init_zspage(struct size_class *class, struct zspage *zspage)
  870. {
  871. unsigned int freeobj = 1;
  872. unsigned long off = 0;
  873. struct page *page = get_first_page(zspage);
  874. while (page) {
  875. struct page *next_page;
  876. struct link_free *link;
  877. void *vaddr;
  878. set_first_obj_offset(page, off);
  879. vaddr = kmap_atomic(page);
  880. link = (struct link_free *)vaddr + off / sizeof(*link);
  881. while ((off += class->size) < PAGE_SIZE) {
  882. link->next = freeobj++ << OBJ_TAG_BITS;
  883. link += class->size / sizeof(*link);
  884. }
  885. /*
  886. * We now come to the last (full or partial) object on this
  887. * page, which must point to the first object on the next
  888. * page (if present)
  889. */
  890. next_page = get_next_page(page);
  891. if (next_page) {
  892. link->next = freeobj++ << OBJ_TAG_BITS;
  893. } else {
  894. /*
  895. * Reset OBJ_TAG_BITS bit to last link to tell
  896. * whether it's allocated object or not.
  897. */
  898. link->next = -1 << OBJ_TAG_BITS;
  899. }
  900. kunmap_atomic(vaddr);
  901. page = next_page;
  902. off %= PAGE_SIZE;
  903. }
  904. set_freeobj(zspage, 0);
  905. }
  906. static void create_page_chain(struct size_class *class, struct zspage *zspage,
  907. struct page *pages[])
  908. {
  909. int i;
  910. struct page *page;
  911. struct page *prev_page = NULL;
  912. int nr_pages = class->pages_per_zspage;
  913. /*
  914. * Allocate individual pages and link them together as:
  915. * 1. all pages are linked together using page->freelist
  916. * 2. each sub-page point to zspage using page->private
  917. *
  918. * we set PG_private to identify the first page (i.e. no other sub-page
  919. * has this flag set).
  920. */
  921. for (i = 0; i < nr_pages; i++) {
  922. page = pages[i];
  923. set_page_private(page, (unsigned long)zspage);
  924. page->freelist = NULL;
  925. if (i == 0) {
  926. zspage->first_page = page;
  927. SetPagePrivate(page);
  928. if (unlikely(class->objs_per_zspage == 1 &&
  929. class->pages_per_zspage == 1))
  930. SetPageHugeObject(page);
  931. } else {
  932. prev_page->freelist = page;
  933. }
  934. prev_page = page;
  935. }
  936. }
  937. /*
  938. * Allocate a zspage for the given size class
  939. */
  940. static struct zspage *alloc_zspage(struct zs_pool *pool,
  941. struct size_class *class,
  942. gfp_t gfp)
  943. {
  944. int i;
  945. struct page *pages[ZS_MAX_PAGES_PER_ZSPAGE];
  946. struct zspage *zspage = cache_alloc_zspage(pool, gfp);
  947. if (!zspage)
  948. return NULL;
  949. memset(zspage, 0, sizeof(struct zspage));
  950. zspage->magic = ZSPAGE_MAGIC;
  951. migrate_lock_init(zspage);
  952. for (i = 0; i < class->pages_per_zspage; i++) {
  953. struct page *page;
  954. page = alloc_page(gfp);
  955. if (!page) {
  956. while (--i >= 0) {
  957. dec_zone_page_state(pages[i], NR_ZSPAGES);
  958. __free_page(pages[i]);
  959. }
  960. cache_free_zspage(pool, zspage);
  961. return NULL;
  962. }
  963. inc_zone_page_state(page, NR_ZSPAGES);
  964. pages[i] = page;
  965. }
  966. create_page_chain(class, zspage, pages);
  967. init_zspage(class, zspage);
  968. return zspage;
  969. }
  970. static struct zspage *find_get_zspage(struct size_class *class)
  971. {
  972. int i;
  973. struct zspage *zspage;
  974. for (i = ZS_ALMOST_FULL; i >= ZS_EMPTY; i--) {
  975. zspage = list_first_entry_or_null(&class->fullness_list[i],
  976. struct zspage, list);
  977. if (zspage)
  978. break;
  979. }
  980. return zspage;
  981. }
  982. #ifdef CONFIG_PGTABLE_MAPPING
  983. static inline int __zs_cpu_up(struct mapping_area *area)
  984. {
  985. /*
  986. * Make sure we don't leak memory if a cpu UP notification
  987. * and zs_init() race and both call zs_cpu_up() on the same cpu
  988. */
  989. if (area->vm)
  990. return 0;
  991. area->vm = alloc_vm_area(PAGE_SIZE * 2, NULL);
  992. if (!area->vm)
  993. return -ENOMEM;
  994. return 0;
  995. }
  996. static inline void __zs_cpu_down(struct mapping_area *area)
  997. {
  998. if (area->vm)
  999. free_vm_area(area->vm);
  1000. area->vm = NULL;
  1001. }
  1002. static inline void *__zs_map_object(struct mapping_area *area,
  1003. struct page *pages[2], int off, int size)
  1004. {
  1005. BUG_ON(map_vm_area(area->vm, PAGE_KERNEL, pages));
  1006. area->vm_addr = area->vm->addr;
  1007. return area->vm_addr + off;
  1008. }
  1009. static inline void __zs_unmap_object(struct mapping_area *area,
  1010. struct page *pages[2], int off, int size)
  1011. {
  1012. unsigned long addr = (unsigned long)area->vm_addr;
  1013. unmap_kernel_range(addr, PAGE_SIZE * 2);
  1014. }
  1015. #else /* CONFIG_PGTABLE_MAPPING */
  1016. static inline int __zs_cpu_up(struct mapping_area *area)
  1017. {
  1018. /*
  1019. * Make sure we don't leak memory if a cpu UP notification
  1020. * and zs_init() race and both call zs_cpu_up() on the same cpu
  1021. */
  1022. if (area->vm_buf)
  1023. return 0;
  1024. area->vm_buf = kmalloc(ZS_MAX_ALLOC_SIZE, GFP_KERNEL);
  1025. if (!area->vm_buf)
  1026. return -ENOMEM;
  1027. return 0;
  1028. }
  1029. static inline void __zs_cpu_down(struct mapping_area *area)
  1030. {
  1031. kfree(area->vm_buf);
  1032. area->vm_buf = NULL;
  1033. }
  1034. static void *__zs_map_object(struct mapping_area *area,
  1035. struct page *pages[2], int off, int size)
  1036. {
  1037. int sizes[2];
  1038. void *addr;
  1039. char *buf = area->vm_buf;
  1040. /* disable page faults to match kmap_atomic() return conditions */
  1041. pagefault_disable();
  1042. /* no read fastpath */
  1043. if (area->vm_mm == ZS_MM_WO)
  1044. goto out;
  1045. sizes[0] = PAGE_SIZE - off;
  1046. sizes[1] = size - sizes[0];
  1047. /* copy object to per-cpu buffer */
  1048. addr = kmap_atomic(pages[0]);
  1049. memcpy(buf, addr + off, sizes[0]);
  1050. kunmap_atomic(addr);
  1051. addr = kmap_atomic(pages[1]);
  1052. memcpy(buf + sizes[0], addr, sizes[1]);
  1053. kunmap_atomic(addr);
  1054. out:
  1055. return area->vm_buf;
  1056. }
  1057. static void __zs_unmap_object(struct mapping_area *area,
  1058. struct page *pages[2], int off, int size)
  1059. {
  1060. int sizes[2];
  1061. void *addr;
  1062. char *buf;
  1063. /* no write fastpath */
  1064. if (area->vm_mm == ZS_MM_RO)
  1065. goto out;
  1066. buf = area->vm_buf;
  1067. buf = buf + ZS_HANDLE_SIZE;
  1068. size -= ZS_HANDLE_SIZE;
  1069. off += ZS_HANDLE_SIZE;
  1070. sizes[0] = PAGE_SIZE - off;
  1071. sizes[1] = size - sizes[0];
  1072. /* copy per-cpu buffer to object */
  1073. addr = kmap_atomic(pages[0]);
  1074. memcpy(addr + off, buf, sizes[0]);
  1075. kunmap_atomic(addr);
  1076. addr = kmap_atomic(pages[1]);
  1077. memcpy(addr, buf + sizes[0], sizes[1]);
  1078. kunmap_atomic(addr);
  1079. out:
  1080. /* enable page faults to match kunmap_atomic() return conditions */
  1081. pagefault_enable();
  1082. }
  1083. #endif /* CONFIG_PGTABLE_MAPPING */
  1084. static int zs_cpu_prepare(unsigned int cpu)
  1085. {
  1086. struct mapping_area *area;
  1087. area = &per_cpu(zs_map_area, cpu);
  1088. return __zs_cpu_up(area);
  1089. }
  1090. static int zs_cpu_dead(unsigned int cpu)
  1091. {
  1092. struct mapping_area *area;
  1093. area = &per_cpu(zs_map_area, cpu);
  1094. __zs_cpu_down(area);
  1095. return 0;
  1096. }
  1097. static bool can_merge(struct size_class *prev, int pages_per_zspage,
  1098. int objs_per_zspage)
  1099. {
  1100. if (prev->pages_per_zspage == pages_per_zspage &&
  1101. prev->objs_per_zspage == objs_per_zspage)
  1102. return true;
  1103. return false;
  1104. }
  1105. static bool zspage_full(struct size_class *class, struct zspage *zspage)
  1106. {
  1107. return get_zspage_inuse(zspage) == class->objs_per_zspage;
  1108. }
  1109. unsigned long zs_get_total_pages(struct zs_pool *pool)
  1110. {
  1111. return atomic_long_read(&pool->pages_allocated);
  1112. }
  1113. EXPORT_SYMBOL_GPL(zs_get_total_pages);
  1114. /**
  1115. * zs_map_object - get address of allocated object from handle.
  1116. * @pool: pool from which the object was allocated
  1117. * @handle: handle returned from zs_malloc
  1118. *
  1119. * Before using an object allocated from zs_malloc, it must be mapped using
  1120. * this function. When done with the object, it must be unmapped using
  1121. * zs_unmap_object.
  1122. *
  1123. * Only one object can be mapped per cpu at a time. There is no protection
  1124. * against nested mappings.
  1125. *
  1126. * This function returns with preemption and page faults disabled.
  1127. */
  1128. void *zs_map_object(struct zs_pool *pool, unsigned long handle,
  1129. enum zs_mapmode mm)
  1130. {
  1131. struct zspage *zspage;
  1132. struct page *page;
  1133. unsigned long obj, off;
  1134. unsigned int obj_idx;
  1135. unsigned int class_idx;
  1136. enum fullness_group fg;
  1137. struct size_class *class;
  1138. struct mapping_area *area;
  1139. struct page *pages[2];
  1140. void *ret;
  1141. /*
  1142. * Because we use per-cpu mapping areas shared among the
  1143. * pools/users, we can't allow mapping in interrupt context
  1144. * because it can corrupt another users mappings.
  1145. */
  1146. WARN_ON_ONCE(in_interrupt());
  1147. /* From now on, migration cannot move the object */
  1148. pin_tag(handle);
  1149. obj = handle_to_obj(handle);
  1150. obj_to_location(obj, &page, &obj_idx);
  1151. zspage = get_zspage(page);
  1152. /* migration cannot move any subpage in this zspage */
  1153. migrate_read_lock(zspage);
  1154. get_zspage_mapping(zspage, &class_idx, &fg);
  1155. class = pool->size_class[class_idx];
  1156. off = (class->size * obj_idx) & ~PAGE_MASK;
  1157. area = &get_cpu_var(zs_map_area);
  1158. area->vm_mm = mm;
  1159. if (off + class->size <= PAGE_SIZE) {
  1160. /* this object is contained entirely within a page */
  1161. area->vm_addr = kmap_atomic(page);
  1162. ret = area->vm_addr + off;
  1163. goto out;
  1164. }
  1165. /* this object spans two pages */
  1166. pages[0] = page;
  1167. pages[1] = get_next_page(page);
  1168. BUG_ON(!pages[1]);
  1169. ret = __zs_map_object(area, pages, off, class->size);
  1170. out:
  1171. if (likely(!PageHugeObject(page)))
  1172. ret += ZS_HANDLE_SIZE;
  1173. return ret;
  1174. }
  1175. EXPORT_SYMBOL_GPL(zs_map_object);
  1176. void zs_unmap_object(struct zs_pool *pool, unsigned long handle)
  1177. {
  1178. struct zspage *zspage;
  1179. struct page *page;
  1180. unsigned long obj, off;
  1181. unsigned int obj_idx;
  1182. unsigned int class_idx;
  1183. enum fullness_group fg;
  1184. struct size_class *class;
  1185. struct mapping_area *area;
  1186. obj = handle_to_obj(handle);
  1187. obj_to_location(obj, &page, &obj_idx);
  1188. zspage = get_zspage(page);
  1189. get_zspage_mapping(zspage, &class_idx, &fg);
  1190. class = pool->size_class[class_idx];
  1191. off = (class->size * obj_idx) & ~PAGE_MASK;
  1192. area = this_cpu_ptr(&zs_map_area);
  1193. if (off + class->size <= PAGE_SIZE)
  1194. kunmap_atomic(area->vm_addr);
  1195. else {
  1196. struct page *pages[2];
  1197. pages[0] = page;
  1198. pages[1] = get_next_page(page);
  1199. BUG_ON(!pages[1]);
  1200. __zs_unmap_object(area, pages, off, class->size);
  1201. }
  1202. put_cpu_var(zs_map_area);
  1203. migrate_read_unlock(zspage);
  1204. unpin_tag(handle);
  1205. }
  1206. EXPORT_SYMBOL_GPL(zs_unmap_object);
  1207. static unsigned long obj_malloc(struct size_class *class,
  1208. struct zspage *zspage, unsigned long handle)
  1209. {
  1210. int i, nr_page, offset;
  1211. unsigned long obj;
  1212. struct link_free *link;
  1213. struct page *m_page;
  1214. unsigned long m_offset;
  1215. void *vaddr;
  1216. handle |= OBJ_ALLOCATED_TAG;
  1217. obj = get_freeobj(zspage);
  1218. offset = obj * class->size;
  1219. nr_page = offset >> PAGE_SHIFT;
  1220. m_offset = offset & ~PAGE_MASK;
  1221. m_page = get_first_page(zspage);
  1222. for (i = 0; i < nr_page; i++)
  1223. m_page = get_next_page(m_page);
  1224. vaddr = kmap_atomic(m_page);
  1225. link = (struct link_free *)vaddr + m_offset / sizeof(*link);
  1226. set_freeobj(zspage, link->next >> OBJ_TAG_BITS);
  1227. if (likely(!PageHugeObject(m_page)))
  1228. /* record handle in the header of allocated chunk */
  1229. link->handle = handle;
  1230. else
  1231. /* record handle to page->index */
  1232. zspage->first_page->index = handle;
  1233. kunmap_atomic(vaddr);
  1234. mod_zspage_inuse(zspage, 1);
  1235. zs_stat_inc(class, OBJ_USED, 1);
  1236. obj = location_to_obj(m_page, obj);
  1237. return obj;
  1238. }
  1239. /**
  1240. * zs_malloc - Allocate block of given size from pool.
  1241. * @pool: pool to allocate from
  1242. * @size: size of block to allocate
  1243. * @gfp: gfp flags when allocating object
  1244. *
  1245. * On success, handle to the allocated object is returned,
  1246. * otherwise 0.
  1247. * Allocation requests with size > ZS_MAX_ALLOC_SIZE will fail.
  1248. */
  1249. unsigned long zs_malloc(struct zs_pool *pool, size_t size, gfp_t gfp)
  1250. {
  1251. unsigned long handle, obj;
  1252. struct size_class *class;
  1253. enum fullness_group newfg;
  1254. struct zspage *zspage;
  1255. if (unlikely(!size || size > ZS_MAX_ALLOC_SIZE))
  1256. return 0;
  1257. handle = cache_alloc_handle(pool, gfp);
  1258. if (!handle)
  1259. return 0;
  1260. /* extra space in chunk to keep the handle */
  1261. size += ZS_HANDLE_SIZE;
  1262. class = pool->size_class[get_size_class_index(size)];
  1263. spin_lock(&class->lock);
  1264. zspage = find_get_zspage(class);
  1265. if (likely(zspage)) {
  1266. obj = obj_malloc(class, zspage, handle);
  1267. /* Now move the zspage to another fullness group, if required */
  1268. fix_fullness_group(class, zspage);
  1269. record_obj(handle, obj);
  1270. spin_unlock(&class->lock);
  1271. return handle;
  1272. }
  1273. spin_unlock(&class->lock);
  1274. zspage = alloc_zspage(pool, class, gfp);
  1275. if (!zspage) {
  1276. cache_free_handle(pool, handle);
  1277. return 0;
  1278. }
  1279. spin_lock(&class->lock);
  1280. obj = obj_malloc(class, zspage, handle);
  1281. newfg = get_fullness_group(class, zspage);
  1282. insert_zspage(class, zspage, newfg);
  1283. set_zspage_mapping(zspage, class->index, newfg);
  1284. record_obj(handle, obj);
  1285. atomic_long_add(class->pages_per_zspage,
  1286. &pool->pages_allocated);
  1287. zs_stat_inc(class, OBJ_ALLOCATED, class->objs_per_zspage);
  1288. /* We completely set up zspage so mark them as movable */
  1289. SetZsPageMovable(pool, zspage);
  1290. spin_unlock(&class->lock);
  1291. return handle;
  1292. }
  1293. EXPORT_SYMBOL_GPL(zs_malloc);
  1294. static void obj_free(struct size_class *class, unsigned long obj)
  1295. {
  1296. struct link_free *link;
  1297. struct zspage *zspage;
  1298. struct page *f_page;
  1299. unsigned long f_offset;
  1300. unsigned int f_objidx;
  1301. void *vaddr;
  1302. obj &= ~OBJ_ALLOCATED_TAG;
  1303. obj_to_location(obj, &f_page, &f_objidx);
  1304. f_offset = (class->size * f_objidx) & ~PAGE_MASK;
  1305. zspage = get_zspage(f_page);
  1306. vaddr = kmap_atomic(f_page);
  1307. /* Insert this object in containing zspage's freelist */
  1308. link = (struct link_free *)(vaddr + f_offset);
  1309. link->next = get_freeobj(zspage) << OBJ_TAG_BITS;
  1310. kunmap_atomic(vaddr);
  1311. set_freeobj(zspage, f_objidx);
  1312. mod_zspage_inuse(zspage, -1);
  1313. zs_stat_dec(class, OBJ_USED, 1);
  1314. }
  1315. void zs_free(struct zs_pool *pool, unsigned long handle)
  1316. {
  1317. struct zspage *zspage;
  1318. struct page *f_page;
  1319. unsigned long obj;
  1320. unsigned int f_objidx;
  1321. int class_idx;
  1322. struct size_class *class;
  1323. enum fullness_group fullness;
  1324. bool isolated;
  1325. if (unlikely(!handle))
  1326. return;
  1327. pin_tag(handle);
  1328. obj = handle_to_obj(handle);
  1329. obj_to_location(obj, &f_page, &f_objidx);
  1330. zspage = get_zspage(f_page);
  1331. migrate_read_lock(zspage);
  1332. get_zspage_mapping(zspage, &class_idx, &fullness);
  1333. class = pool->size_class[class_idx];
  1334. spin_lock(&class->lock);
  1335. obj_free(class, obj);
  1336. fullness = fix_fullness_group(class, zspage);
  1337. if (fullness != ZS_EMPTY) {
  1338. migrate_read_unlock(zspage);
  1339. goto out;
  1340. }
  1341. isolated = is_zspage_isolated(zspage);
  1342. migrate_read_unlock(zspage);
  1343. /* If zspage is isolated, zs_page_putback will free the zspage */
  1344. if (likely(!isolated))
  1345. free_zspage(pool, class, zspage);
  1346. out:
  1347. spin_unlock(&class->lock);
  1348. unpin_tag(handle);
  1349. cache_free_handle(pool, handle);
  1350. }
  1351. EXPORT_SYMBOL_GPL(zs_free);
  1352. static void zs_object_copy(struct size_class *class, unsigned long dst,
  1353. unsigned long src)
  1354. {
  1355. struct page *s_page, *d_page;
  1356. unsigned int s_objidx, d_objidx;
  1357. unsigned long s_off, d_off;
  1358. void *s_addr, *d_addr;
  1359. int s_size, d_size, size;
  1360. int written = 0;
  1361. s_size = d_size = class->size;
  1362. obj_to_location(src, &s_page, &s_objidx);
  1363. obj_to_location(dst, &d_page, &d_objidx);
  1364. s_off = (class->size * s_objidx) & ~PAGE_MASK;
  1365. d_off = (class->size * d_objidx) & ~PAGE_MASK;
  1366. if (s_off + class->size > PAGE_SIZE)
  1367. s_size = PAGE_SIZE - s_off;
  1368. if (d_off + class->size > PAGE_SIZE)
  1369. d_size = PAGE_SIZE - d_off;
  1370. s_addr = kmap_atomic(s_page);
  1371. d_addr = kmap_atomic(d_page);
  1372. while (1) {
  1373. size = min(s_size, d_size);
  1374. memcpy(d_addr + d_off, s_addr + s_off, size);
  1375. written += size;
  1376. if (written == class->size)
  1377. break;
  1378. s_off += size;
  1379. s_size -= size;
  1380. d_off += size;
  1381. d_size -= size;
  1382. if (s_off >= PAGE_SIZE) {
  1383. kunmap_atomic(d_addr);
  1384. kunmap_atomic(s_addr);
  1385. s_page = get_next_page(s_page);
  1386. s_addr = kmap_atomic(s_page);
  1387. d_addr = kmap_atomic(d_page);
  1388. s_size = class->size - written;
  1389. s_off = 0;
  1390. }
  1391. if (d_off >= PAGE_SIZE) {
  1392. kunmap_atomic(d_addr);
  1393. d_page = get_next_page(d_page);
  1394. d_addr = kmap_atomic(d_page);
  1395. d_size = class->size - written;
  1396. d_off = 0;
  1397. }
  1398. }
  1399. kunmap_atomic(d_addr);
  1400. kunmap_atomic(s_addr);
  1401. }
  1402. /*
  1403. * Find alloced object in zspage from index object and
  1404. * return handle.
  1405. */
  1406. static unsigned long find_alloced_obj(struct size_class *class,
  1407. struct page *page, int *obj_idx)
  1408. {
  1409. unsigned long head;
  1410. int offset = 0;
  1411. int index = *obj_idx;
  1412. unsigned long handle = 0;
  1413. void *addr = kmap_atomic(page);
  1414. offset = get_first_obj_offset(page);
  1415. offset += class->size * index;
  1416. while (offset < PAGE_SIZE) {
  1417. head = obj_to_head(page, addr + offset);
  1418. if (head & OBJ_ALLOCATED_TAG) {
  1419. handle = head & ~OBJ_ALLOCATED_TAG;
  1420. if (trypin_tag(handle))
  1421. break;
  1422. handle = 0;
  1423. }
  1424. offset += class->size;
  1425. index++;
  1426. }
  1427. kunmap_atomic(addr);
  1428. *obj_idx = index;
  1429. return handle;
  1430. }
  1431. struct zs_compact_control {
  1432. /* Source spage for migration which could be a subpage of zspage */
  1433. struct page *s_page;
  1434. /* Destination page for migration which should be a first page
  1435. * of zspage. */
  1436. struct page *d_page;
  1437. /* Starting object index within @s_page which used for live object
  1438. * in the subpage. */
  1439. int obj_idx;
  1440. };
  1441. static int migrate_zspage(struct zs_pool *pool, struct size_class *class,
  1442. struct zs_compact_control *cc)
  1443. {
  1444. unsigned long used_obj, free_obj;
  1445. unsigned long handle;
  1446. struct page *s_page = cc->s_page;
  1447. struct page *d_page = cc->d_page;
  1448. int obj_idx = cc->obj_idx;
  1449. int ret = 0;
  1450. while (1) {
  1451. handle = find_alloced_obj(class, s_page, &obj_idx);
  1452. if (!handle) {
  1453. s_page = get_next_page(s_page);
  1454. if (!s_page)
  1455. break;
  1456. obj_idx = 0;
  1457. continue;
  1458. }
  1459. /* Stop if there is no more space */
  1460. if (zspage_full(class, get_zspage(d_page))) {
  1461. unpin_tag(handle);
  1462. ret = -ENOMEM;
  1463. break;
  1464. }
  1465. used_obj = handle_to_obj(handle);
  1466. free_obj = obj_malloc(class, get_zspage(d_page), handle);
  1467. zs_object_copy(class, free_obj, used_obj);
  1468. obj_idx++;
  1469. /*
  1470. * record_obj updates handle's value to free_obj and it will
  1471. * invalidate lock bit(ie, HANDLE_PIN_BIT) of handle, which
  1472. * breaks synchronization using pin_tag(e,g, zs_free) so
  1473. * let's keep the lock bit.
  1474. */
  1475. free_obj |= BIT(HANDLE_PIN_BIT);
  1476. record_obj(handle, free_obj);
  1477. unpin_tag(handle);
  1478. obj_free(class, used_obj);
  1479. }
  1480. /* Remember last position in this iteration */
  1481. cc->s_page = s_page;
  1482. cc->obj_idx = obj_idx;
  1483. return ret;
  1484. }
  1485. static struct zspage *isolate_zspage(struct size_class *class, bool source)
  1486. {
  1487. int i;
  1488. struct zspage *zspage;
  1489. enum fullness_group fg[2] = {ZS_ALMOST_EMPTY, ZS_ALMOST_FULL};
  1490. if (!source) {
  1491. fg[0] = ZS_ALMOST_FULL;
  1492. fg[1] = ZS_ALMOST_EMPTY;
  1493. }
  1494. for (i = 0; i < 2; i++) {
  1495. zspage = list_first_entry_or_null(&class->fullness_list[fg[i]],
  1496. struct zspage, list);
  1497. if (zspage) {
  1498. VM_BUG_ON(is_zspage_isolated(zspage));
  1499. remove_zspage(class, zspage, fg[i]);
  1500. return zspage;
  1501. }
  1502. }
  1503. return zspage;
  1504. }
  1505. /*
  1506. * putback_zspage - add @zspage into right class's fullness list
  1507. * @class: destination class
  1508. * @zspage: target page
  1509. *
  1510. * Return @zspage's fullness_group
  1511. */
  1512. static enum fullness_group putback_zspage(struct size_class *class,
  1513. struct zspage *zspage)
  1514. {
  1515. enum fullness_group fullness;
  1516. VM_BUG_ON(is_zspage_isolated(zspage));
  1517. fullness = get_fullness_group(class, zspage);
  1518. insert_zspage(class, zspage, fullness);
  1519. set_zspage_mapping(zspage, class->index, fullness);
  1520. return fullness;
  1521. }
  1522. #ifdef CONFIG_COMPACTION
  1523. static struct dentry *zs_mount(struct file_system_type *fs_type,
  1524. int flags, const char *dev_name, void *data)
  1525. {
  1526. static const struct dentry_operations ops = {
  1527. .d_dname = simple_dname,
  1528. };
  1529. return mount_pseudo(fs_type, "zsmalloc:", NULL, &ops, ZSMALLOC_MAGIC);
  1530. }
  1531. static struct file_system_type zsmalloc_fs = {
  1532. .name = "zsmalloc",
  1533. .mount = zs_mount,
  1534. .kill_sb = kill_anon_super,
  1535. };
  1536. static int zsmalloc_mount(void)
  1537. {
  1538. int ret = 0;
  1539. zsmalloc_mnt = kern_mount(&zsmalloc_fs);
  1540. if (IS_ERR(zsmalloc_mnt))
  1541. ret = PTR_ERR(zsmalloc_mnt);
  1542. return ret;
  1543. }
  1544. static void zsmalloc_unmount(void)
  1545. {
  1546. kern_unmount(zsmalloc_mnt);
  1547. }
  1548. static void migrate_lock_init(struct zspage *zspage)
  1549. {
  1550. rwlock_init(&zspage->lock);
  1551. }
  1552. static void migrate_read_lock(struct zspage *zspage)
  1553. {
  1554. read_lock(&zspage->lock);
  1555. }
  1556. static void migrate_read_unlock(struct zspage *zspage)
  1557. {
  1558. read_unlock(&zspage->lock);
  1559. }
  1560. static void migrate_write_lock(struct zspage *zspage)
  1561. {
  1562. write_lock(&zspage->lock);
  1563. }
  1564. static void migrate_write_unlock(struct zspage *zspage)
  1565. {
  1566. write_unlock(&zspage->lock);
  1567. }
  1568. /* Number of isolated subpage for *page migration* in this zspage */
  1569. static void inc_zspage_isolation(struct zspage *zspage)
  1570. {
  1571. zspage->isolated++;
  1572. }
  1573. static void dec_zspage_isolation(struct zspage *zspage)
  1574. {
  1575. zspage->isolated--;
  1576. }
  1577. static void replace_sub_page(struct size_class *class, struct zspage *zspage,
  1578. struct page *newpage, struct page *oldpage)
  1579. {
  1580. struct page *page;
  1581. struct page *pages[ZS_MAX_PAGES_PER_ZSPAGE] = {NULL, };
  1582. int idx = 0;
  1583. page = get_first_page(zspage);
  1584. do {
  1585. if (page == oldpage)
  1586. pages[idx] = newpage;
  1587. else
  1588. pages[idx] = page;
  1589. idx++;
  1590. } while ((page = get_next_page(page)) != NULL);
  1591. create_page_chain(class, zspage, pages);
  1592. set_first_obj_offset(newpage, get_first_obj_offset(oldpage));
  1593. if (unlikely(PageHugeObject(oldpage)))
  1594. newpage->index = oldpage->index;
  1595. __SetPageMovable(newpage, page_mapping(oldpage));
  1596. }
  1597. bool zs_page_isolate(struct page *page, isolate_mode_t mode)
  1598. {
  1599. struct zs_pool *pool;
  1600. struct size_class *class;
  1601. int class_idx;
  1602. enum fullness_group fullness;
  1603. struct zspage *zspage;
  1604. struct address_space *mapping;
  1605. /*
  1606. * Page is locked so zspage couldn't be destroyed. For detail, look at
  1607. * lock_zspage in free_zspage.
  1608. */
  1609. VM_BUG_ON_PAGE(!PageMovable(page), page);
  1610. VM_BUG_ON_PAGE(PageIsolated(page), page);
  1611. zspage = get_zspage(page);
  1612. /*
  1613. * Without class lock, fullness could be stale while class_idx is okay
  1614. * because class_idx is constant unless page is freed so we should get
  1615. * fullness again under class lock.
  1616. */
  1617. get_zspage_mapping(zspage, &class_idx, &fullness);
  1618. mapping = page_mapping(page);
  1619. pool = mapping->private_data;
  1620. class = pool->size_class[class_idx];
  1621. spin_lock(&class->lock);
  1622. if (get_zspage_inuse(zspage) == 0) {
  1623. spin_unlock(&class->lock);
  1624. return false;
  1625. }
  1626. /* zspage is isolated for object migration */
  1627. if (list_empty(&zspage->list) && !is_zspage_isolated(zspage)) {
  1628. spin_unlock(&class->lock);
  1629. return false;
  1630. }
  1631. /*
  1632. * If this is first time isolation for the zspage, isolate zspage from
  1633. * size_class to prevent further object allocation from the zspage.
  1634. */
  1635. if (!list_empty(&zspage->list) && !is_zspage_isolated(zspage)) {
  1636. get_zspage_mapping(zspage, &class_idx, &fullness);
  1637. remove_zspage(class, zspage, fullness);
  1638. }
  1639. inc_zspage_isolation(zspage);
  1640. spin_unlock(&class->lock);
  1641. return true;
  1642. }
  1643. int zs_page_migrate(struct address_space *mapping, struct page *newpage,
  1644. struct page *page, enum migrate_mode mode)
  1645. {
  1646. struct zs_pool *pool;
  1647. struct size_class *class;
  1648. int class_idx;
  1649. enum fullness_group fullness;
  1650. struct zspage *zspage;
  1651. struct page *dummy;
  1652. void *s_addr, *d_addr, *addr;
  1653. int offset, pos;
  1654. unsigned long handle, head;
  1655. unsigned long old_obj, new_obj;
  1656. unsigned int obj_idx;
  1657. int ret = -EAGAIN;
  1658. VM_BUG_ON_PAGE(!PageMovable(page), page);
  1659. VM_BUG_ON_PAGE(!PageIsolated(page), page);
  1660. zspage = get_zspage(page);
  1661. /* Concurrent compactor cannot migrate any subpage in zspage */
  1662. migrate_write_lock(zspage);
  1663. get_zspage_mapping(zspage, &class_idx, &fullness);
  1664. pool = mapping->private_data;
  1665. class = pool->size_class[class_idx];
  1666. offset = get_first_obj_offset(page);
  1667. spin_lock(&class->lock);
  1668. if (!get_zspage_inuse(zspage)) {
  1669. ret = -EBUSY;
  1670. goto unlock_class;
  1671. }
  1672. pos = offset;
  1673. s_addr = kmap_atomic(page);
  1674. while (pos < PAGE_SIZE) {
  1675. head = obj_to_head(page, s_addr + pos);
  1676. if (head & OBJ_ALLOCATED_TAG) {
  1677. handle = head & ~OBJ_ALLOCATED_TAG;
  1678. if (!trypin_tag(handle))
  1679. goto unpin_objects;
  1680. }
  1681. pos += class->size;
  1682. }
  1683. /*
  1684. * Here, any user cannot access all objects in the zspage so let's move.
  1685. */
  1686. d_addr = kmap_atomic(newpage);
  1687. memcpy(d_addr, s_addr, PAGE_SIZE);
  1688. kunmap_atomic(d_addr);
  1689. for (addr = s_addr + offset; addr < s_addr + pos;
  1690. addr += class->size) {
  1691. head = obj_to_head(page, addr);
  1692. if (head & OBJ_ALLOCATED_TAG) {
  1693. handle = head & ~OBJ_ALLOCATED_TAG;
  1694. if (!testpin_tag(handle))
  1695. BUG();
  1696. old_obj = handle_to_obj(handle);
  1697. obj_to_location(old_obj, &dummy, &obj_idx);
  1698. new_obj = (unsigned long)location_to_obj(newpage,
  1699. obj_idx);
  1700. new_obj |= BIT(HANDLE_PIN_BIT);
  1701. record_obj(handle, new_obj);
  1702. }
  1703. }
  1704. replace_sub_page(class, zspage, newpage, page);
  1705. get_page(newpage);
  1706. dec_zspage_isolation(zspage);
  1707. /*
  1708. * Page migration is done so let's putback isolated zspage to
  1709. * the list if @page is final isolated subpage in the zspage.
  1710. */
  1711. if (!is_zspage_isolated(zspage))
  1712. putback_zspage(class, zspage);
  1713. reset_page(page);
  1714. put_page(page);
  1715. page = newpage;
  1716. ret = MIGRATEPAGE_SUCCESS;
  1717. unpin_objects:
  1718. for (addr = s_addr + offset; addr < s_addr + pos;
  1719. addr += class->size) {
  1720. head = obj_to_head(page, addr);
  1721. if (head & OBJ_ALLOCATED_TAG) {
  1722. handle = head & ~OBJ_ALLOCATED_TAG;
  1723. if (!testpin_tag(handle))
  1724. BUG();
  1725. unpin_tag(handle);
  1726. }
  1727. }
  1728. kunmap_atomic(s_addr);
  1729. unlock_class:
  1730. spin_unlock(&class->lock);
  1731. migrate_write_unlock(zspage);
  1732. return ret;
  1733. }
  1734. void zs_page_putback(struct page *page)
  1735. {
  1736. struct zs_pool *pool;
  1737. struct size_class *class;
  1738. int class_idx;
  1739. enum fullness_group fg;
  1740. struct address_space *mapping;
  1741. struct zspage *zspage;
  1742. VM_BUG_ON_PAGE(!PageMovable(page), page);
  1743. VM_BUG_ON_PAGE(!PageIsolated(page), page);
  1744. zspage = get_zspage(page);
  1745. get_zspage_mapping(zspage, &class_idx, &fg);
  1746. mapping = page_mapping(page);
  1747. pool = mapping->private_data;
  1748. class = pool->size_class[class_idx];
  1749. spin_lock(&class->lock);
  1750. dec_zspage_isolation(zspage);
  1751. if (!is_zspage_isolated(zspage)) {
  1752. fg = putback_zspage(class, zspage);
  1753. /*
  1754. * Due to page_lock, we cannot free zspage immediately
  1755. * so let's defer.
  1756. */
  1757. if (fg == ZS_EMPTY)
  1758. schedule_work(&pool->free_work);
  1759. }
  1760. spin_unlock(&class->lock);
  1761. }
  1762. const struct address_space_operations zsmalloc_aops = {
  1763. .isolate_page = zs_page_isolate,
  1764. .migratepage = zs_page_migrate,
  1765. .putback_page = zs_page_putback,
  1766. };
  1767. static int zs_register_migration(struct zs_pool *pool)
  1768. {
  1769. pool->inode = alloc_anon_inode(zsmalloc_mnt->mnt_sb);
  1770. if (IS_ERR(pool->inode)) {
  1771. pool->inode = NULL;
  1772. return 1;
  1773. }
  1774. pool->inode->i_mapping->private_data = pool;
  1775. pool->inode->i_mapping->a_ops = &zsmalloc_aops;
  1776. return 0;
  1777. }
  1778. static void zs_unregister_migration(struct zs_pool *pool)
  1779. {
  1780. flush_work(&pool->free_work);
  1781. iput(pool->inode);
  1782. }
  1783. /*
  1784. * Caller should hold page_lock of all pages in the zspage
  1785. * In here, we cannot use zspage meta data.
  1786. */
  1787. static void async_free_zspage(struct work_struct *work)
  1788. {
  1789. int i;
  1790. struct size_class *class;
  1791. unsigned int class_idx;
  1792. enum fullness_group fullness;
  1793. struct zspage *zspage, *tmp;
  1794. LIST_HEAD(free_pages);
  1795. struct zs_pool *pool = container_of(work, struct zs_pool,
  1796. free_work);
  1797. for (i = 0; i < ZS_SIZE_CLASSES; i++) {
  1798. class = pool->size_class[i];
  1799. if (class->index != i)
  1800. continue;
  1801. spin_lock(&class->lock);
  1802. list_splice_init(&class->fullness_list[ZS_EMPTY], &free_pages);
  1803. spin_unlock(&class->lock);
  1804. }
  1805. list_for_each_entry_safe(zspage, tmp, &free_pages, list) {
  1806. list_del(&zspage->list);
  1807. lock_zspage(zspage);
  1808. get_zspage_mapping(zspage, &class_idx, &fullness);
  1809. VM_BUG_ON(fullness != ZS_EMPTY);
  1810. class = pool->size_class[class_idx];
  1811. spin_lock(&class->lock);
  1812. __free_zspage(pool, pool->size_class[class_idx], zspage);
  1813. spin_unlock(&class->lock);
  1814. }
  1815. };
  1816. static void kick_deferred_free(struct zs_pool *pool)
  1817. {
  1818. schedule_work(&pool->free_work);
  1819. }
  1820. static void init_deferred_free(struct zs_pool *pool)
  1821. {
  1822. INIT_WORK(&pool->free_work, async_free_zspage);
  1823. }
  1824. static void SetZsPageMovable(struct zs_pool *pool, struct zspage *zspage)
  1825. {
  1826. struct page *page = get_first_page(zspage);
  1827. do {
  1828. WARN_ON(!trylock_page(page));
  1829. __SetPageMovable(page, pool->inode->i_mapping);
  1830. unlock_page(page);
  1831. } while ((page = get_next_page(page)) != NULL);
  1832. }
  1833. #endif
  1834. /*
  1835. *
  1836. * Based on the number of unused allocated objects calculate
  1837. * and return the number of pages that we can free.
  1838. */
  1839. static unsigned long zs_can_compact(struct size_class *class)
  1840. {
  1841. unsigned long obj_wasted;
  1842. unsigned long obj_allocated = zs_stat_get(class, OBJ_ALLOCATED);
  1843. unsigned long obj_used = zs_stat_get(class, OBJ_USED);
  1844. if (obj_allocated <= obj_used)
  1845. return 0;
  1846. obj_wasted = obj_allocated - obj_used;
  1847. obj_wasted /= class->objs_per_zspage;
  1848. return obj_wasted * class->pages_per_zspage;
  1849. }
  1850. static void __zs_compact(struct zs_pool *pool, struct size_class *class)
  1851. {
  1852. struct zs_compact_control cc;
  1853. struct zspage *src_zspage;
  1854. struct zspage *dst_zspage = NULL;
  1855. spin_lock(&class->lock);
  1856. while ((src_zspage = isolate_zspage(class, true))) {
  1857. if (!zs_can_compact(class))
  1858. break;
  1859. cc.obj_idx = 0;
  1860. cc.s_page = get_first_page(src_zspage);
  1861. while ((dst_zspage = isolate_zspage(class, false))) {
  1862. cc.d_page = get_first_page(dst_zspage);
  1863. /*
  1864. * If there is no more space in dst_page, resched
  1865. * and see if anyone had allocated another zspage.
  1866. */
  1867. if (!migrate_zspage(pool, class, &cc))
  1868. break;
  1869. putback_zspage(class, dst_zspage);
  1870. }
  1871. /* Stop if we couldn't find slot */
  1872. if (dst_zspage == NULL)
  1873. break;
  1874. putback_zspage(class, dst_zspage);
  1875. if (putback_zspage(class, src_zspage) == ZS_EMPTY) {
  1876. free_zspage(pool, class, src_zspage);
  1877. pool->stats.pages_compacted += class->pages_per_zspage;
  1878. }
  1879. spin_unlock(&class->lock);
  1880. cond_resched();
  1881. spin_lock(&class->lock);
  1882. }
  1883. if (src_zspage)
  1884. putback_zspage(class, src_zspage);
  1885. spin_unlock(&class->lock);
  1886. }
  1887. unsigned long zs_compact(struct zs_pool *pool)
  1888. {
  1889. int i;
  1890. struct size_class *class;
  1891. for (i = ZS_SIZE_CLASSES - 1; i >= 0; i--) {
  1892. class = pool->size_class[i];
  1893. if (!class)
  1894. continue;
  1895. if (class->index != i)
  1896. continue;
  1897. __zs_compact(pool, class);
  1898. }
  1899. return pool->stats.pages_compacted;
  1900. }
  1901. EXPORT_SYMBOL_GPL(zs_compact);
  1902. void zs_pool_stats(struct zs_pool *pool, struct zs_pool_stats *stats)
  1903. {
  1904. memcpy(stats, &pool->stats, sizeof(struct zs_pool_stats));
  1905. }
  1906. EXPORT_SYMBOL_GPL(zs_pool_stats);
  1907. static unsigned long zs_shrinker_scan(struct shrinker *shrinker,
  1908. struct shrink_control *sc)
  1909. {
  1910. unsigned long pages_freed;
  1911. struct zs_pool *pool = container_of(shrinker, struct zs_pool,
  1912. shrinker);
  1913. pages_freed = pool->stats.pages_compacted;
  1914. /*
  1915. * Compact classes and calculate compaction delta.
  1916. * Can run concurrently with a manually triggered
  1917. * (by user) compaction.
  1918. */
  1919. pages_freed = zs_compact(pool) - pages_freed;
  1920. return pages_freed ? pages_freed : SHRINK_STOP;
  1921. }
  1922. static unsigned long zs_shrinker_count(struct shrinker *shrinker,
  1923. struct shrink_control *sc)
  1924. {
  1925. int i;
  1926. struct size_class *class;
  1927. unsigned long pages_to_free = 0;
  1928. struct zs_pool *pool = container_of(shrinker, struct zs_pool,
  1929. shrinker);
  1930. for (i = ZS_SIZE_CLASSES - 1; i >= 0; i--) {
  1931. class = pool->size_class[i];
  1932. if (!class)
  1933. continue;
  1934. if (class->index != i)
  1935. continue;
  1936. pages_to_free += zs_can_compact(class);
  1937. }
  1938. return pages_to_free;
  1939. }
  1940. static void zs_unregister_shrinker(struct zs_pool *pool)
  1941. {
  1942. if (pool->shrinker_enabled) {
  1943. unregister_shrinker(&pool->shrinker);
  1944. pool->shrinker_enabled = false;
  1945. }
  1946. }
  1947. static int zs_register_shrinker(struct zs_pool *pool)
  1948. {
  1949. pool->shrinker.scan_objects = zs_shrinker_scan;
  1950. pool->shrinker.count_objects = zs_shrinker_count;
  1951. pool->shrinker.batch = 0;
  1952. pool->shrinker.seeks = DEFAULT_SEEKS;
  1953. return register_shrinker(&pool->shrinker);
  1954. }
  1955. /**
  1956. * zs_create_pool - Creates an allocation pool to work from.
  1957. * @name: pool name to be created
  1958. *
  1959. * This function must be called before anything when using
  1960. * the zsmalloc allocator.
  1961. *
  1962. * On success, a pointer to the newly created pool is returned,
  1963. * otherwise NULL.
  1964. */
  1965. struct zs_pool *zs_create_pool(const char *name)
  1966. {
  1967. int i;
  1968. struct zs_pool *pool;
  1969. struct size_class *prev_class = NULL;
  1970. pool = kzalloc(sizeof(*pool), GFP_KERNEL);
  1971. if (!pool)
  1972. return NULL;
  1973. init_deferred_free(pool);
  1974. pool->name = kstrdup(name, GFP_KERNEL);
  1975. if (!pool->name)
  1976. goto err;
  1977. if (create_cache(pool))
  1978. goto err;
  1979. /*
  1980. * Iterate reversely, because, size of size_class that we want to use
  1981. * for merging should be larger or equal to current size.
  1982. */
  1983. for (i = ZS_SIZE_CLASSES - 1; i >= 0; i--) {
  1984. int size;
  1985. int pages_per_zspage;
  1986. int objs_per_zspage;
  1987. struct size_class *class;
  1988. int fullness = 0;
  1989. size = ZS_MIN_ALLOC_SIZE + i * ZS_SIZE_CLASS_DELTA;
  1990. if (size > ZS_MAX_ALLOC_SIZE)
  1991. size = ZS_MAX_ALLOC_SIZE;
  1992. pages_per_zspage = get_pages_per_zspage(size);
  1993. objs_per_zspage = pages_per_zspage * PAGE_SIZE / size;
  1994. /*
  1995. * size_class is used for normal zsmalloc operation such
  1996. * as alloc/free for that size. Although it is natural that we
  1997. * have one size_class for each size, there is a chance that we
  1998. * can get more memory utilization if we use one size_class for
  1999. * many different sizes whose size_class have same
  2000. * characteristics. So, we makes size_class point to
  2001. * previous size_class if possible.
  2002. */
  2003. if (prev_class) {
  2004. if (can_merge(prev_class, pages_per_zspage, objs_per_zspage)) {
  2005. pool->size_class[i] = prev_class;
  2006. continue;
  2007. }
  2008. }
  2009. class = kzalloc(sizeof(struct size_class), GFP_KERNEL);
  2010. if (!class)
  2011. goto err;
  2012. class->size = size;
  2013. class->index = i;
  2014. class->pages_per_zspage = pages_per_zspage;
  2015. class->objs_per_zspage = objs_per_zspage;
  2016. spin_lock_init(&class->lock);
  2017. pool->size_class[i] = class;
  2018. for (fullness = ZS_EMPTY; fullness < NR_ZS_FULLNESS;
  2019. fullness++)
  2020. INIT_LIST_HEAD(&class->fullness_list[fullness]);
  2021. prev_class = class;
  2022. }
  2023. /* debug only, don't abort if it fails */
  2024. zs_pool_stat_create(pool, name);
  2025. if (zs_register_migration(pool))
  2026. goto err;
  2027. /*
  2028. * Not critical, we still can use the pool
  2029. * and user can trigger compaction manually.
  2030. */
  2031. if (zs_register_shrinker(pool) == 0)
  2032. pool->shrinker_enabled = true;
  2033. return pool;
  2034. err:
  2035. zs_destroy_pool(pool);
  2036. return NULL;
  2037. }
  2038. EXPORT_SYMBOL_GPL(zs_create_pool);
  2039. void zs_destroy_pool(struct zs_pool *pool)
  2040. {
  2041. int i;
  2042. zs_unregister_shrinker(pool);
  2043. zs_unregister_migration(pool);
  2044. zs_pool_stat_destroy(pool);
  2045. for (i = 0; i < ZS_SIZE_CLASSES; i++) {
  2046. int fg;
  2047. struct size_class *class = pool->size_class[i];
  2048. if (!class)
  2049. continue;
  2050. if (class->index != i)
  2051. continue;
  2052. for (fg = ZS_EMPTY; fg < NR_ZS_FULLNESS; fg++) {
  2053. if (!list_empty(&class->fullness_list[fg])) {
  2054. pr_info("Freeing non-empty class with size %db, fullness group %d\n",
  2055. class->size, fg);
  2056. }
  2057. }
  2058. kfree(class);
  2059. }
  2060. destroy_cache(pool);
  2061. kfree(pool->name);
  2062. kfree(pool);
  2063. }
  2064. EXPORT_SYMBOL_GPL(zs_destroy_pool);
  2065. static int __init zs_init(void)
  2066. {
  2067. int ret;
  2068. ret = zsmalloc_mount();
  2069. if (ret)
  2070. goto out;
  2071. ret = cpuhp_setup_state(CPUHP_MM_ZS_PREPARE, "mm/zsmalloc:prepare",
  2072. zs_cpu_prepare, zs_cpu_dead);
  2073. if (ret)
  2074. goto hp_setup_fail;
  2075. #ifdef CONFIG_ZPOOL
  2076. zpool_register_driver(&zs_zpool_driver);
  2077. #endif
  2078. zs_stat_init();
  2079. return 0;
  2080. hp_setup_fail:
  2081. zsmalloc_unmount();
  2082. out:
  2083. return ret;
  2084. }
  2085. static void __exit zs_exit(void)
  2086. {
  2087. #ifdef CONFIG_ZPOOL
  2088. zpool_unregister_driver(&zs_zpool_driver);
  2089. #endif
  2090. zsmalloc_unmount();
  2091. cpuhp_remove_state(CPUHP_MM_ZS_PREPARE);
  2092. zs_stat_exit();
  2093. }
  2094. module_init(zs_init);
  2095. module_exit(zs_exit);
  2096. MODULE_LICENSE("Dual BSD/GPL");
  2097. MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");