zsmalloc.c 63 KB

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