zsmalloc.c 60 KB

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