zsmalloc.c 60 KB

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