zsmalloc.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053
  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 the first component (0-order) page
  19. * page->index (union with page->freelist): offset of the first object
  20. * starting in this page. For the first page, this is
  21. * always 0, so we use this field (aka freelist) to point
  22. * to the first free object in zspage.
  23. * page->lru: links together all component pages (except the first page)
  24. * of a zspage
  25. *
  26. * For _first_ page only:
  27. *
  28. * page->private: refers to the component page after the first page
  29. * If the page is first_page for huge object, it stores handle.
  30. * Look at size_class->huge.
  31. * page->freelist: points to the first free object in zspage.
  32. * Free objects are linked together using in-place
  33. * metadata.
  34. * page->objects: maximum number of objects we can store in this
  35. * zspage (class->zspage_order * PAGE_SIZE / class->size)
  36. * page->lru: links together first pages of various zspages.
  37. * Basically forming list of zspages in a fullness group.
  38. * page->mapping: class index and fullness group of the zspage
  39. * page->inuse: the number of objects that are used in this zspage
  40. *
  41. * Usage of struct page flags:
  42. * PG_private: identifies the first component page
  43. * PG_private2: identifies the last component page
  44. *
  45. */
  46. #include <linux/module.h>
  47. #include <linux/kernel.h>
  48. #include <linux/sched.h>
  49. #include <linux/bitops.h>
  50. #include <linux/errno.h>
  51. #include <linux/highmem.h>
  52. #include <linux/string.h>
  53. #include <linux/slab.h>
  54. #include <asm/tlbflush.h>
  55. #include <asm/pgtable.h>
  56. #include <linux/cpumask.h>
  57. #include <linux/cpu.h>
  58. #include <linux/vmalloc.h>
  59. #include <linux/preempt.h>
  60. #include <linux/spinlock.h>
  61. #include <linux/types.h>
  62. #include <linux/debugfs.h>
  63. #include <linux/zsmalloc.h>
  64. #include <linux/zpool.h>
  65. /*
  66. * This must be power of 2 and greater than of equal to sizeof(link_free).
  67. * These two conditions ensure that any 'struct link_free' itself doesn't
  68. * span more than 1 page which avoids complex case of mapping 2 pages simply
  69. * to restore link_free pointer values.
  70. */
  71. #define ZS_ALIGN 8
  72. /*
  73. * A single 'zspage' is composed of up to 2^N discontiguous 0-order (single)
  74. * pages. ZS_MAX_ZSPAGE_ORDER defines upper limit on N.
  75. */
  76. #define ZS_MAX_ZSPAGE_ORDER 2
  77. #define ZS_MAX_PAGES_PER_ZSPAGE (_AC(1, UL) << ZS_MAX_ZSPAGE_ORDER)
  78. #define ZS_HANDLE_SIZE (sizeof(unsigned long))
  79. /*
  80. * Object location (<PFN>, <obj_idx>) is encoded as
  81. * as single (unsigned long) handle value.
  82. *
  83. * Note that object index <obj_idx> is relative to system
  84. * page <PFN> it is stored in, so for each sub-page belonging
  85. * to a zspage, obj_idx starts with 0.
  86. *
  87. * This is made more complicated by various memory models and PAE.
  88. */
  89. #ifndef MAX_PHYSMEM_BITS
  90. #ifdef CONFIG_HIGHMEM64G
  91. #define MAX_PHYSMEM_BITS 36
  92. #else /* !CONFIG_HIGHMEM64G */
  93. /*
  94. * If this definition of MAX_PHYSMEM_BITS is used, OBJ_INDEX_BITS will just
  95. * be PAGE_SHIFT
  96. */
  97. #define MAX_PHYSMEM_BITS BITS_PER_LONG
  98. #endif
  99. #endif
  100. #define _PFN_BITS (MAX_PHYSMEM_BITS - PAGE_SHIFT)
  101. /*
  102. * Memory for allocating for handle keeps object position by
  103. * encoding <page, obj_idx> and the encoded value has a room
  104. * in least bit(ie, look at obj_to_location).
  105. * We use the bit to synchronize between object access by
  106. * user and migration.
  107. */
  108. #define HANDLE_PIN_BIT 0
  109. /*
  110. * Head in allocated object should have OBJ_ALLOCATED_TAG
  111. * to identify the object was allocated or not.
  112. * It's okay to add the status bit in the least bit because
  113. * header keeps handle which is 4byte-aligned address so we
  114. * have room for two bit at least.
  115. */
  116. #define OBJ_ALLOCATED_TAG 1
  117. #define OBJ_TAG_BITS 1
  118. #define OBJ_INDEX_BITS (BITS_PER_LONG - _PFN_BITS - OBJ_TAG_BITS)
  119. #define OBJ_INDEX_MASK ((_AC(1, UL) << OBJ_INDEX_BITS) - 1)
  120. #define MAX(a, b) ((a) >= (b) ? (a) : (b))
  121. /* ZS_MIN_ALLOC_SIZE must be multiple of ZS_ALIGN */
  122. #define ZS_MIN_ALLOC_SIZE \
  123. MAX(32, (ZS_MAX_PAGES_PER_ZSPAGE << PAGE_SHIFT >> OBJ_INDEX_BITS))
  124. /* each chunk includes extra space to keep handle */
  125. #define ZS_MAX_ALLOC_SIZE PAGE_SIZE
  126. /*
  127. * On systems with 4K page size, this gives 255 size classes! There is a
  128. * trader-off here:
  129. * - Large number of size classes is potentially wasteful as free page are
  130. * spread across these classes
  131. * - Small number of size classes causes large internal fragmentation
  132. * - Probably its better to use specific size classes (empirically
  133. * determined). NOTE: all those class sizes must be set as multiple of
  134. * ZS_ALIGN to make sure link_free itself never has to span 2 pages.
  135. *
  136. * ZS_MIN_ALLOC_SIZE and ZS_SIZE_CLASS_DELTA must be multiple of ZS_ALIGN
  137. * (reason above)
  138. */
  139. #define ZS_SIZE_CLASS_DELTA (PAGE_SIZE >> 8)
  140. /*
  141. * We do not maintain any list for completely empty or full pages
  142. */
  143. enum fullness_group {
  144. ZS_ALMOST_FULL,
  145. ZS_ALMOST_EMPTY,
  146. _ZS_NR_FULLNESS_GROUPS,
  147. ZS_EMPTY,
  148. ZS_FULL
  149. };
  150. enum zs_stat_type {
  151. OBJ_ALLOCATED,
  152. OBJ_USED,
  153. CLASS_ALMOST_FULL,
  154. CLASS_ALMOST_EMPTY,
  155. };
  156. #ifdef CONFIG_ZSMALLOC_STAT
  157. #define NR_ZS_STAT_TYPE (CLASS_ALMOST_EMPTY + 1)
  158. #else
  159. #define NR_ZS_STAT_TYPE (OBJ_USED + 1)
  160. #endif
  161. struct zs_size_stat {
  162. unsigned long objs[NR_ZS_STAT_TYPE];
  163. };
  164. #ifdef CONFIG_ZSMALLOC_STAT
  165. static struct dentry *zs_stat_root;
  166. #endif
  167. /*
  168. * number of size_classes
  169. */
  170. static int zs_size_classes;
  171. /*
  172. * We assign a page to ZS_ALMOST_EMPTY fullness group when:
  173. * n <= N / f, where
  174. * n = number of allocated objects
  175. * N = total number of objects zspage can store
  176. * f = fullness_threshold_frac
  177. *
  178. * Similarly, we assign zspage to:
  179. * ZS_ALMOST_FULL when n > N / f
  180. * ZS_EMPTY when n == 0
  181. * ZS_FULL when n == N
  182. *
  183. * (see: fix_fullness_group())
  184. */
  185. static const int fullness_threshold_frac = 4;
  186. struct size_class {
  187. spinlock_t lock;
  188. struct page *fullness_list[_ZS_NR_FULLNESS_GROUPS];
  189. /*
  190. * Size of objects stored in this class. Must be multiple
  191. * of ZS_ALIGN.
  192. */
  193. int size;
  194. unsigned int index;
  195. /* Number of PAGE_SIZE sized pages to combine to form a 'zspage' */
  196. int pages_per_zspage;
  197. struct zs_size_stat stats;
  198. /* huge object: pages_per_zspage == 1 && maxobj_per_zspage == 1 */
  199. bool huge;
  200. };
  201. /*
  202. * Placed within free objects to form a singly linked list.
  203. * For every zspage, first_page->freelist gives head of this list.
  204. *
  205. * This must be power of 2 and less than or equal to ZS_ALIGN
  206. */
  207. struct link_free {
  208. union {
  209. /*
  210. * Position of next free chunk (encodes <PFN, obj_idx>)
  211. * It's valid for non-allocated object
  212. */
  213. void *next;
  214. /*
  215. * Handle of allocated object.
  216. */
  217. unsigned long handle;
  218. };
  219. };
  220. struct zs_pool {
  221. const char *name;
  222. struct size_class **size_class;
  223. struct kmem_cache *handle_cachep;
  224. gfp_t flags; /* allocation flags used when growing pool */
  225. atomic_long_t pages_allocated;
  226. struct zs_pool_stats stats;
  227. /* Compact classes */
  228. struct shrinker shrinker;
  229. /*
  230. * To signify that register_shrinker() was successful
  231. * and unregister_shrinker() will not Oops.
  232. */
  233. bool shrinker_enabled;
  234. #ifdef CONFIG_ZSMALLOC_STAT
  235. struct dentry *stat_dentry;
  236. #endif
  237. };
  238. /*
  239. * A zspage's class index and fullness group
  240. * are encoded in its (first)page->mapping
  241. */
  242. #define CLASS_IDX_BITS 28
  243. #define FULLNESS_BITS 4
  244. #define CLASS_IDX_MASK ((1 << CLASS_IDX_BITS) - 1)
  245. #define FULLNESS_MASK ((1 << FULLNESS_BITS) - 1)
  246. struct mapping_area {
  247. #ifdef CONFIG_PGTABLE_MAPPING
  248. struct vm_struct *vm; /* vm area for mapping object that span pages */
  249. #else
  250. char *vm_buf; /* copy buffer for objects that span pages */
  251. #endif
  252. char *vm_addr; /* address of kmap_atomic()'ed pages */
  253. enum zs_mapmode vm_mm; /* mapping mode */
  254. bool huge;
  255. };
  256. static int create_handle_cache(struct zs_pool *pool)
  257. {
  258. pool->handle_cachep = kmem_cache_create("zs_handle", ZS_HANDLE_SIZE,
  259. 0, 0, NULL);
  260. return pool->handle_cachep ? 0 : 1;
  261. }
  262. static void destroy_handle_cache(struct zs_pool *pool)
  263. {
  264. kmem_cache_destroy(pool->handle_cachep);
  265. }
  266. static unsigned long alloc_handle(struct zs_pool *pool)
  267. {
  268. return (unsigned long)kmem_cache_alloc(pool->handle_cachep,
  269. pool->flags & ~__GFP_HIGHMEM);
  270. }
  271. static void free_handle(struct zs_pool *pool, unsigned long handle)
  272. {
  273. kmem_cache_free(pool->handle_cachep, (void *)handle);
  274. }
  275. static void record_obj(unsigned long handle, unsigned long obj)
  276. {
  277. /*
  278. * lsb of @obj represents handle lock while other bits
  279. * represent object value the handle is pointing so
  280. * updating shouldn't do store tearing.
  281. */
  282. WRITE_ONCE(*(unsigned long *)handle, obj);
  283. }
  284. /* zpool driver */
  285. #ifdef CONFIG_ZPOOL
  286. static void *zs_zpool_create(const char *name, gfp_t gfp,
  287. const struct zpool_ops *zpool_ops,
  288. struct zpool *zpool)
  289. {
  290. return zs_create_pool(name, gfp);
  291. }
  292. static void zs_zpool_destroy(void *pool)
  293. {
  294. zs_destroy_pool(pool);
  295. }
  296. static int zs_zpool_malloc(void *pool, size_t size, gfp_t gfp,
  297. unsigned long *handle)
  298. {
  299. *handle = zs_malloc(pool, size);
  300. return *handle ? 0 : -1;
  301. }
  302. static void zs_zpool_free(void *pool, unsigned long handle)
  303. {
  304. zs_free(pool, handle);
  305. }
  306. static int zs_zpool_shrink(void *pool, unsigned int pages,
  307. unsigned int *reclaimed)
  308. {
  309. return -EINVAL;
  310. }
  311. static void *zs_zpool_map(void *pool, unsigned long handle,
  312. enum zpool_mapmode mm)
  313. {
  314. enum zs_mapmode zs_mm;
  315. switch (mm) {
  316. case ZPOOL_MM_RO:
  317. zs_mm = ZS_MM_RO;
  318. break;
  319. case ZPOOL_MM_WO:
  320. zs_mm = ZS_MM_WO;
  321. break;
  322. case ZPOOL_MM_RW: /* fallthru */
  323. default:
  324. zs_mm = ZS_MM_RW;
  325. break;
  326. }
  327. return zs_map_object(pool, handle, zs_mm);
  328. }
  329. static void zs_zpool_unmap(void *pool, unsigned long handle)
  330. {
  331. zs_unmap_object(pool, handle);
  332. }
  333. static u64 zs_zpool_total_size(void *pool)
  334. {
  335. return zs_get_total_pages(pool) << PAGE_SHIFT;
  336. }
  337. static struct zpool_driver zs_zpool_driver = {
  338. .type = "zsmalloc",
  339. .owner = THIS_MODULE,
  340. .create = zs_zpool_create,
  341. .destroy = zs_zpool_destroy,
  342. .malloc = zs_zpool_malloc,
  343. .free = zs_zpool_free,
  344. .shrink = zs_zpool_shrink,
  345. .map = zs_zpool_map,
  346. .unmap = zs_zpool_unmap,
  347. .total_size = zs_zpool_total_size,
  348. };
  349. MODULE_ALIAS("zpool-zsmalloc");
  350. #endif /* CONFIG_ZPOOL */
  351. static unsigned int get_maxobj_per_zspage(int size, int pages_per_zspage)
  352. {
  353. return pages_per_zspage * PAGE_SIZE / size;
  354. }
  355. /* per-cpu VM mapping areas for zspage accesses that cross page boundaries */
  356. static DEFINE_PER_CPU(struct mapping_area, zs_map_area);
  357. static int is_first_page(struct page *page)
  358. {
  359. return PagePrivate(page);
  360. }
  361. static int is_last_page(struct page *page)
  362. {
  363. return PagePrivate2(page);
  364. }
  365. static void get_zspage_mapping(struct page *page, unsigned int *class_idx,
  366. enum fullness_group *fullness)
  367. {
  368. unsigned long m;
  369. BUG_ON(!is_first_page(page));
  370. m = (unsigned long)page->mapping;
  371. *fullness = m & FULLNESS_MASK;
  372. *class_idx = (m >> FULLNESS_BITS) & CLASS_IDX_MASK;
  373. }
  374. static void set_zspage_mapping(struct page *page, unsigned int class_idx,
  375. enum fullness_group fullness)
  376. {
  377. unsigned long m;
  378. BUG_ON(!is_first_page(page));
  379. m = ((class_idx & CLASS_IDX_MASK) << FULLNESS_BITS) |
  380. (fullness & FULLNESS_MASK);
  381. page->mapping = (struct address_space *)m;
  382. }
  383. /*
  384. * zsmalloc divides the pool into various size classes where each
  385. * class maintains a list of zspages where each zspage is divided
  386. * into equal sized chunks. Each allocation falls into one of these
  387. * classes depending on its size. This function returns index of the
  388. * size class which has chunk size big enough to hold the give size.
  389. */
  390. static int get_size_class_index(int size)
  391. {
  392. int idx = 0;
  393. if (likely(size > ZS_MIN_ALLOC_SIZE))
  394. idx = DIV_ROUND_UP(size - ZS_MIN_ALLOC_SIZE,
  395. ZS_SIZE_CLASS_DELTA);
  396. return min(zs_size_classes - 1, idx);
  397. }
  398. static inline void zs_stat_inc(struct size_class *class,
  399. enum zs_stat_type type, unsigned long cnt)
  400. {
  401. if (type < NR_ZS_STAT_TYPE)
  402. class->stats.objs[type] += cnt;
  403. }
  404. static inline void zs_stat_dec(struct size_class *class,
  405. enum zs_stat_type type, unsigned long cnt)
  406. {
  407. if (type < NR_ZS_STAT_TYPE)
  408. class->stats.objs[type] -= cnt;
  409. }
  410. static inline unsigned long zs_stat_get(struct size_class *class,
  411. enum zs_stat_type type)
  412. {
  413. if (type < NR_ZS_STAT_TYPE)
  414. return class->stats.objs[type];
  415. return 0;
  416. }
  417. #ifdef CONFIG_ZSMALLOC_STAT
  418. static int __init zs_stat_init(void)
  419. {
  420. if (!debugfs_initialized())
  421. return -ENODEV;
  422. zs_stat_root = debugfs_create_dir("zsmalloc", NULL);
  423. if (!zs_stat_root)
  424. return -ENOMEM;
  425. return 0;
  426. }
  427. static void __exit zs_stat_exit(void)
  428. {
  429. debugfs_remove_recursive(zs_stat_root);
  430. }
  431. static int zs_stats_size_show(struct seq_file *s, void *v)
  432. {
  433. int i;
  434. struct zs_pool *pool = s->private;
  435. struct size_class *class;
  436. int objs_per_zspage;
  437. unsigned long class_almost_full, class_almost_empty;
  438. unsigned long obj_allocated, obj_used, pages_used;
  439. unsigned long total_class_almost_full = 0, total_class_almost_empty = 0;
  440. unsigned long total_objs = 0, total_used_objs = 0, total_pages = 0;
  441. seq_printf(s, " %5s %5s %11s %12s %13s %10s %10s %16s\n",
  442. "class", "size", "almost_full", "almost_empty",
  443. "obj_allocated", "obj_used", "pages_used",
  444. "pages_per_zspage");
  445. for (i = 0; i < zs_size_classes; i++) {
  446. class = pool->size_class[i];
  447. if (class->index != i)
  448. continue;
  449. spin_lock(&class->lock);
  450. class_almost_full = zs_stat_get(class, CLASS_ALMOST_FULL);
  451. class_almost_empty = zs_stat_get(class, CLASS_ALMOST_EMPTY);
  452. obj_allocated = zs_stat_get(class, OBJ_ALLOCATED);
  453. obj_used = zs_stat_get(class, OBJ_USED);
  454. spin_unlock(&class->lock);
  455. objs_per_zspage = get_maxobj_per_zspage(class->size,
  456. class->pages_per_zspage);
  457. pages_used = obj_allocated / objs_per_zspage *
  458. class->pages_per_zspage;
  459. seq_printf(s, " %5u %5u %11lu %12lu %13lu %10lu %10lu %16d\n",
  460. i, class->size, class_almost_full, class_almost_empty,
  461. obj_allocated, obj_used, pages_used,
  462. class->pages_per_zspage);
  463. total_class_almost_full += class_almost_full;
  464. total_class_almost_empty += class_almost_empty;
  465. total_objs += obj_allocated;
  466. total_used_objs += obj_used;
  467. total_pages += pages_used;
  468. }
  469. seq_puts(s, "\n");
  470. seq_printf(s, " %5s %5s %11lu %12lu %13lu %10lu %10lu\n",
  471. "Total", "", total_class_almost_full,
  472. total_class_almost_empty, total_objs,
  473. total_used_objs, total_pages);
  474. return 0;
  475. }
  476. static int zs_stats_size_open(struct inode *inode, struct file *file)
  477. {
  478. return single_open(file, zs_stats_size_show, inode->i_private);
  479. }
  480. static const struct file_operations zs_stat_size_ops = {
  481. .open = zs_stats_size_open,
  482. .read = seq_read,
  483. .llseek = seq_lseek,
  484. .release = single_release,
  485. };
  486. static int zs_pool_stat_create(const char *name, struct zs_pool *pool)
  487. {
  488. struct dentry *entry;
  489. if (!zs_stat_root)
  490. return -ENODEV;
  491. entry = debugfs_create_dir(name, zs_stat_root);
  492. if (!entry) {
  493. pr_warn("debugfs dir <%s> creation failed\n", name);
  494. return -ENOMEM;
  495. }
  496. pool->stat_dentry = entry;
  497. entry = debugfs_create_file("classes", S_IFREG | S_IRUGO,
  498. pool->stat_dentry, pool, &zs_stat_size_ops);
  499. if (!entry) {
  500. pr_warn("%s: debugfs file entry <%s> creation failed\n",
  501. name, "classes");
  502. return -ENOMEM;
  503. }
  504. return 0;
  505. }
  506. static void zs_pool_stat_destroy(struct zs_pool *pool)
  507. {
  508. debugfs_remove_recursive(pool->stat_dentry);
  509. }
  510. #else /* CONFIG_ZSMALLOC_STAT */
  511. static int __init zs_stat_init(void)
  512. {
  513. return 0;
  514. }
  515. static void __exit zs_stat_exit(void)
  516. {
  517. }
  518. static inline int zs_pool_stat_create(const char *name, struct zs_pool *pool)
  519. {
  520. return 0;
  521. }
  522. static inline void zs_pool_stat_destroy(struct zs_pool *pool)
  523. {
  524. }
  525. #endif
  526. /*
  527. * For each size class, zspages are divided into different groups
  528. * depending on how "full" they are. This was done so that we could
  529. * easily find empty or nearly empty zspages when we try to shrink
  530. * the pool (not yet implemented). This function returns fullness
  531. * status of the given page.
  532. */
  533. static enum fullness_group get_fullness_group(struct page *page)
  534. {
  535. int inuse, max_objects;
  536. enum fullness_group fg;
  537. BUG_ON(!is_first_page(page));
  538. inuse = page->inuse;
  539. max_objects = page->objects;
  540. if (inuse == 0)
  541. fg = ZS_EMPTY;
  542. else if (inuse == max_objects)
  543. fg = ZS_FULL;
  544. else if (inuse <= 3 * max_objects / fullness_threshold_frac)
  545. fg = ZS_ALMOST_EMPTY;
  546. else
  547. fg = ZS_ALMOST_FULL;
  548. return fg;
  549. }
  550. /*
  551. * Each size class maintains various freelists and zspages are assigned
  552. * to one of these freelists based on the number of live objects they
  553. * have. This functions inserts the given zspage into the freelist
  554. * identified by <class, fullness_group>.
  555. */
  556. static void insert_zspage(struct page *page, struct size_class *class,
  557. enum fullness_group fullness)
  558. {
  559. struct page **head;
  560. BUG_ON(!is_first_page(page));
  561. if (fullness >= _ZS_NR_FULLNESS_GROUPS)
  562. return;
  563. zs_stat_inc(class, fullness == ZS_ALMOST_EMPTY ?
  564. CLASS_ALMOST_EMPTY : CLASS_ALMOST_FULL, 1);
  565. head = &class->fullness_list[fullness];
  566. if (!*head) {
  567. *head = page;
  568. return;
  569. }
  570. /*
  571. * We want to see more ZS_FULL pages and less almost
  572. * empty/full. Put pages with higher ->inuse first.
  573. */
  574. list_add_tail(&page->lru, &(*head)->lru);
  575. if (page->inuse >= (*head)->inuse)
  576. *head = page;
  577. }
  578. /*
  579. * This function removes the given zspage from the freelist identified
  580. * by <class, fullness_group>.
  581. */
  582. static void remove_zspage(struct page *page, struct size_class *class,
  583. enum fullness_group fullness)
  584. {
  585. struct page **head;
  586. BUG_ON(!is_first_page(page));
  587. if (fullness >= _ZS_NR_FULLNESS_GROUPS)
  588. return;
  589. head = &class->fullness_list[fullness];
  590. BUG_ON(!*head);
  591. if (list_empty(&(*head)->lru))
  592. *head = NULL;
  593. else if (*head == page)
  594. *head = (struct page *)list_entry((*head)->lru.next,
  595. struct page, lru);
  596. list_del_init(&page->lru);
  597. zs_stat_dec(class, fullness == ZS_ALMOST_EMPTY ?
  598. CLASS_ALMOST_EMPTY : CLASS_ALMOST_FULL, 1);
  599. }
  600. /*
  601. * Each size class maintains zspages in different fullness groups depending
  602. * on the number of live objects they contain. When allocating or freeing
  603. * objects, the fullness status of the page can change, say, from ALMOST_FULL
  604. * to ALMOST_EMPTY when freeing an object. This function checks if such
  605. * a status change has occurred for the given page and accordingly moves the
  606. * page from the freelist of the old fullness group to that of the new
  607. * fullness group.
  608. */
  609. static enum fullness_group fix_fullness_group(struct size_class *class,
  610. struct page *page)
  611. {
  612. int class_idx;
  613. enum fullness_group currfg, newfg;
  614. BUG_ON(!is_first_page(page));
  615. get_zspage_mapping(page, &class_idx, &currfg);
  616. newfg = get_fullness_group(page);
  617. if (newfg == currfg)
  618. goto out;
  619. remove_zspage(page, class, currfg);
  620. insert_zspage(page, class, newfg);
  621. set_zspage_mapping(page, class_idx, newfg);
  622. out:
  623. return newfg;
  624. }
  625. /*
  626. * We have to decide on how many pages to link together
  627. * to form a zspage for each size class. This is important
  628. * to reduce wastage due to unusable space left at end of
  629. * each zspage which is given as:
  630. * wastage = Zp % class_size
  631. * usage = Zp - wastage
  632. * where Zp = zspage size = k * PAGE_SIZE where k = 1, 2, ...
  633. *
  634. * For example, for size class of 3/8 * PAGE_SIZE, we should
  635. * link together 3 PAGE_SIZE sized pages to form a zspage
  636. * since then we can perfectly fit in 8 such objects.
  637. */
  638. static int get_pages_per_zspage(int class_size)
  639. {
  640. int i, max_usedpc = 0;
  641. /* zspage order which gives maximum used size per KB */
  642. int max_usedpc_order = 1;
  643. for (i = 1; i <= ZS_MAX_PAGES_PER_ZSPAGE; i++) {
  644. int zspage_size;
  645. int waste, usedpc;
  646. zspage_size = i * PAGE_SIZE;
  647. waste = zspage_size % class_size;
  648. usedpc = (zspage_size - waste) * 100 / zspage_size;
  649. if (usedpc > max_usedpc) {
  650. max_usedpc = usedpc;
  651. max_usedpc_order = i;
  652. }
  653. }
  654. return max_usedpc_order;
  655. }
  656. /*
  657. * A single 'zspage' is composed of many system pages which are
  658. * linked together using fields in struct page. This function finds
  659. * the first/head page, given any component page of a zspage.
  660. */
  661. static struct page *get_first_page(struct page *page)
  662. {
  663. if (is_first_page(page))
  664. return page;
  665. else
  666. return (struct page *)page_private(page);
  667. }
  668. static struct page *get_next_page(struct page *page)
  669. {
  670. struct page *next;
  671. if (is_last_page(page))
  672. next = NULL;
  673. else if (is_first_page(page))
  674. next = (struct page *)page_private(page);
  675. else
  676. next = list_entry(page->lru.next, struct page, lru);
  677. return next;
  678. }
  679. /*
  680. * Encode <page, obj_idx> as a single handle value.
  681. * We use the least bit of handle for tagging.
  682. */
  683. static void *location_to_obj(struct page *page, unsigned long obj_idx)
  684. {
  685. unsigned long obj;
  686. if (!page) {
  687. BUG_ON(obj_idx);
  688. return NULL;
  689. }
  690. obj = page_to_pfn(page) << OBJ_INDEX_BITS;
  691. obj |= ((obj_idx) & OBJ_INDEX_MASK);
  692. obj <<= OBJ_TAG_BITS;
  693. return (void *)obj;
  694. }
  695. /*
  696. * Decode <page, obj_idx> pair from the given object handle. We adjust the
  697. * decoded obj_idx back to its original value since it was adjusted in
  698. * location_to_obj().
  699. */
  700. static void obj_to_location(unsigned long obj, struct page **page,
  701. unsigned long *obj_idx)
  702. {
  703. obj >>= OBJ_TAG_BITS;
  704. *page = pfn_to_page(obj >> OBJ_INDEX_BITS);
  705. *obj_idx = (obj & OBJ_INDEX_MASK);
  706. }
  707. static unsigned long handle_to_obj(unsigned long handle)
  708. {
  709. return *(unsigned long *)handle;
  710. }
  711. static unsigned long obj_to_head(struct size_class *class, struct page *page,
  712. void *obj)
  713. {
  714. if (class->huge) {
  715. VM_BUG_ON(!is_first_page(page));
  716. return page_private(page);
  717. } else
  718. return *(unsigned long *)obj;
  719. }
  720. static unsigned long obj_idx_to_offset(struct page *page,
  721. unsigned long obj_idx, int class_size)
  722. {
  723. unsigned long off = 0;
  724. if (!is_first_page(page))
  725. off = page->index;
  726. return off + obj_idx * class_size;
  727. }
  728. static inline int trypin_tag(unsigned long handle)
  729. {
  730. unsigned long *ptr = (unsigned long *)handle;
  731. return !test_and_set_bit_lock(HANDLE_PIN_BIT, ptr);
  732. }
  733. static void pin_tag(unsigned long handle)
  734. {
  735. while (!trypin_tag(handle));
  736. }
  737. static void unpin_tag(unsigned long handle)
  738. {
  739. unsigned long *ptr = (unsigned long *)handle;
  740. clear_bit_unlock(HANDLE_PIN_BIT, ptr);
  741. }
  742. static void reset_page(struct page *page)
  743. {
  744. clear_bit(PG_private, &page->flags);
  745. clear_bit(PG_private_2, &page->flags);
  746. set_page_private(page, 0);
  747. page->mapping = NULL;
  748. page->freelist = NULL;
  749. page_mapcount_reset(page);
  750. }
  751. static void free_zspage(struct page *first_page)
  752. {
  753. struct page *nextp, *tmp, *head_extra;
  754. BUG_ON(!is_first_page(first_page));
  755. BUG_ON(first_page->inuse);
  756. head_extra = (struct page *)page_private(first_page);
  757. reset_page(first_page);
  758. __free_page(first_page);
  759. /* zspage with only 1 system page */
  760. if (!head_extra)
  761. return;
  762. list_for_each_entry_safe(nextp, tmp, &head_extra->lru, lru) {
  763. list_del(&nextp->lru);
  764. reset_page(nextp);
  765. __free_page(nextp);
  766. }
  767. reset_page(head_extra);
  768. __free_page(head_extra);
  769. }
  770. /* Initialize a newly allocated zspage */
  771. static void init_zspage(struct page *first_page, struct size_class *class)
  772. {
  773. unsigned long off = 0;
  774. struct page *page = first_page;
  775. BUG_ON(!is_first_page(first_page));
  776. while (page) {
  777. struct page *next_page;
  778. struct link_free *link;
  779. unsigned int i = 1;
  780. void *vaddr;
  781. /*
  782. * page->index stores offset of first object starting
  783. * in the page. For the first page, this is always 0,
  784. * so we use first_page->index (aka ->freelist) to store
  785. * head of corresponding zspage's freelist.
  786. */
  787. if (page != first_page)
  788. page->index = off;
  789. vaddr = kmap_atomic(page);
  790. link = (struct link_free *)vaddr + off / sizeof(*link);
  791. while ((off += class->size) < PAGE_SIZE) {
  792. link->next = location_to_obj(page, i++);
  793. link += class->size / sizeof(*link);
  794. }
  795. /*
  796. * We now come to the last (full or partial) object on this
  797. * page, which must point to the first object on the next
  798. * page (if present)
  799. */
  800. next_page = get_next_page(page);
  801. link->next = location_to_obj(next_page, 0);
  802. kunmap_atomic(vaddr);
  803. page = next_page;
  804. off %= PAGE_SIZE;
  805. }
  806. }
  807. /*
  808. * Allocate a zspage for the given size class
  809. */
  810. static struct page *alloc_zspage(struct size_class *class, gfp_t flags)
  811. {
  812. int i, error;
  813. struct page *first_page = NULL, *uninitialized_var(prev_page);
  814. /*
  815. * Allocate individual pages and link them together as:
  816. * 1. first page->private = first sub-page
  817. * 2. all sub-pages are linked together using page->lru
  818. * 3. each sub-page is linked to the first page using page->private
  819. *
  820. * For each size class, First/Head pages are linked together using
  821. * page->lru. Also, we set PG_private to identify the first page
  822. * (i.e. no other sub-page has this flag set) and PG_private_2 to
  823. * identify the last page.
  824. */
  825. error = -ENOMEM;
  826. for (i = 0; i < class->pages_per_zspage; i++) {
  827. struct page *page;
  828. page = alloc_page(flags);
  829. if (!page)
  830. goto cleanup;
  831. INIT_LIST_HEAD(&page->lru);
  832. if (i == 0) { /* first page */
  833. SetPagePrivate(page);
  834. set_page_private(page, 0);
  835. first_page = page;
  836. first_page->inuse = 0;
  837. }
  838. if (i == 1)
  839. set_page_private(first_page, (unsigned long)page);
  840. if (i >= 1)
  841. set_page_private(page, (unsigned long)first_page);
  842. if (i >= 2)
  843. list_add(&page->lru, &prev_page->lru);
  844. if (i == class->pages_per_zspage - 1) /* last page */
  845. SetPagePrivate2(page);
  846. prev_page = page;
  847. }
  848. init_zspage(first_page, class);
  849. first_page->freelist = location_to_obj(first_page, 0);
  850. /* Maximum number of objects we can store in this zspage */
  851. first_page->objects = class->pages_per_zspage * PAGE_SIZE / class->size;
  852. error = 0; /* Success */
  853. cleanup:
  854. if (unlikely(error) && first_page) {
  855. free_zspage(first_page);
  856. first_page = NULL;
  857. }
  858. return first_page;
  859. }
  860. static struct page *find_get_zspage(struct size_class *class)
  861. {
  862. int i;
  863. struct page *page;
  864. for (i = 0; i < _ZS_NR_FULLNESS_GROUPS; i++) {
  865. page = class->fullness_list[i];
  866. if (page)
  867. break;
  868. }
  869. return page;
  870. }
  871. #ifdef CONFIG_PGTABLE_MAPPING
  872. static inline int __zs_cpu_up(struct mapping_area *area)
  873. {
  874. /*
  875. * Make sure we don't leak memory if a cpu UP notification
  876. * and zs_init() race and both call zs_cpu_up() on the same cpu
  877. */
  878. if (area->vm)
  879. return 0;
  880. area->vm = alloc_vm_area(PAGE_SIZE * 2, NULL);
  881. if (!area->vm)
  882. return -ENOMEM;
  883. return 0;
  884. }
  885. static inline void __zs_cpu_down(struct mapping_area *area)
  886. {
  887. if (area->vm)
  888. free_vm_area(area->vm);
  889. area->vm = NULL;
  890. }
  891. static inline void *__zs_map_object(struct mapping_area *area,
  892. struct page *pages[2], int off, int size)
  893. {
  894. BUG_ON(map_vm_area(area->vm, PAGE_KERNEL, pages));
  895. area->vm_addr = area->vm->addr;
  896. return area->vm_addr + off;
  897. }
  898. static inline void __zs_unmap_object(struct mapping_area *area,
  899. struct page *pages[2], int off, int size)
  900. {
  901. unsigned long addr = (unsigned long)area->vm_addr;
  902. unmap_kernel_range(addr, PAGE_SIZE * 2);
  903. }
  904. #else /* CONFIG_PGTABLE_MAPPING */
  905. static inline int __zs_cpu_up(struct mapping_area *area)
  906. {
  907. /*
  908. * Make sure we don't leak memory if a cpu UP notification
  909. * and zs_init() race and both call zs_cpu_up() on the same cpu
  910. */
  911. if (area->vm_buf)
  912. return 0;
  913. area->vm_buf = kmalloc(ZS_MAX_ALLOC_SIZE, GFP_KERNEL);
  914. if (!area->vm_buf)
  915. return -ENOMEM;
  916. return 0;
  917. }
  918. static inline void __zs_cpu_down(struct mapping_area *area)
  919. {
  920. kfree(area->vm_buf);
  921. area->vm_buf = NULL;
  922. }
  923. static void *__zs_map_object(struct mapping_area *area,
  924. struct page *pages[2], int off, int size)
  925. {
  926. int sizes[2];
  927. void *addr;
  928. char *buf = area->vm_buf;
  929. /* disable page faults to match kmap_atomic() return conditions */
  930. pagefault_disable();
  931. /* no read fastpath */
  932. if (area->vm_mm == ZS_MM_WO)
  933. goto out;
  934. sizes[0] = PAGE_SIZE - off;
  935. sizes[1] = size - sizes[0];
  936. /* copy object to per-cpu buffer */
  937. addr = kmap_atomic(pages[0]);
  938. memcpy(buf, addr + off, sizes[0]);
  939. kunmap_atomic(addr);
  940. addr = kmap_atomic(pages[1]);
  941. memcpy(buf + sizes[0], addr, sizes[1]);
  942. kunmap_atomic(addr);
  943. out:
  944. return area->vm_buf;
  945. }
  946. static void __zs_unmap_object(struct mapping_area *area,
  947. struct page *pages[2], int off, int size)
  948. {
  949. int sizes[2];
  950. void *addr;
  951. char *buf;
  952. /* no write fastpath */
  953. if (area->vm_mm == ZS_MM_RO)
  954. goto out;
  955. buf = area->vm_buf;
  956. if (!area->huge) {
  957. buf = buf + ZS_HANDLE_SIZE;
  958. size -= ZS_HANDLE_SIZE;
  959. off += ZS_HANDLE_SIZE;
  960. }
  961. sizes[0] = PAGE_SIZE - off;
  962. sizes[1] = size - sizes[0];
  963. /* copy per-cpu buffer to object */
  964. addr = kmap_atomic(pages[0]);
  965. memcpy(addr + off, buf, sizes[0]);
  966. kunmap_atomic(addr);
  967. addr = kmap_atomic(pages[1]);
  968. memcpy(addr, buf + sizes[0], sizes[1]);
  969. kunmap_atomic(addr);
  970. out:
  971. /* enable page faults to match kunmap_atomic() return conditions */
  972. pagefault_enable();
  973. }
  974. #endif /* CONFIG_PGTABLE_MAPPING */
  975. static int zs_cpu_notifier(struct notifier_block *nb, unsigned long action,
  976. void *pcpu)
  977. {
  978. int ret, cpu = (long)pcpu;
  979. struct mapping_area *area;
  980. switch (action) {
  981. case CPU_UP_PREPARE:
  982. area = &per_cpu(zs_map_area, cpu);
  983. ret = __zs_cpu_up(area);
  984. if (ret)
  985. return notifier_from_errno(ret);
  986. break;
  987. case CPU_DEAD:
  988. case CPU_UP_CANCELED:
  989. area = &per_cpu(zs_map_area, cpu);
  990. __zs_cpu_down(area);
  991. break;
  992. }
  993. return NOTIFY_OK;
  994. }
  995. static struct notifier_block zs_cpu_nb = {
  996. .notifier_call = zs_cpu_notifier
  997. };
  998. static int zs_register_cpu_notifier(void)
  999. {
  1000. int cpu, uninitialized_var(ret);
  1001. cpu_notifier_register_begin();
  1002. __register_cpu_notifier(&zs_cpu_nb);
  1003. for_each_online_cpu(cpu) {
  1004. ret = zs_cpu_notifier(NULL, CPU_UP_PREPARE, (void *)(long)cpu);
  1005. if (notifier_to_errno(ret))
  1006. break;
  1007. }
  1008. cpu_notifier_register_done();
  1009. return notifier_to_errno(ret);
  1010. }
  1011. static void zs_unregister_cpu_notifier(void)
  1012. {
  1013. int cpu;
  1014. cpu_notifier_register_begin();
  1015. for_each_online_cpu(cpu)
  1016. zs_cpu_notifier(NULL, CPU_DEAD, (void *)(long)cpu);
  1017. __unregister_cpu_notifier(&zs_cpu_nb);
  1018. cpu_notifier_register_done();
  1019. }
  1020. static void init_zs_size_classes(void)
  1021. {
  1022. int nr;
  1023. nr = (ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE) / ZS_SIZE_CLASS_DELTA + 1;
  1024. if ((ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE) % ZS_SIZE_CLASS_DELTA)
  1025. nr += 1;
  1026. zs_size_classes = nr;
  1027. }
  1028. static bool can_merge(struct size_class *prev, int size, int pages_per_zspage)
  1029. {
  1030. if (prev->pages_per_zspage != pages_per_zspage)
  1031. return false;
  1032. if (get_maxobj_per_zspage(prev->size, prev->pages_per_zspage)
  1033. != get_maxobj_per_zspage(size, pages_per_zspage))
  1034. return false;
  1035. return true;
  1036. }
  1037. static bool zspage_full(struct page *page)
  1038. {
  1039. BUG_ON(!is_first_page(page));
  1040. return page->inuse == page->objects;
  1041. }
  1042. unsigned long zs_get_total_pages(struct zs_pool *pool)
  1043. {
  1044. return atomic_long_read(&pool->pages_allocated);
  1045. }
  1046. EXPORT_SYMBOL_GPL(zs_get_total_pages);
  1047. /**
  1048. * zs_map_object - get address of allocated object from handle.
  1049. * @pool: pool from which the object was allocated
  1050. * @handle: handle returned from zs_malloc
  1051. *
  1052. * Before using an object allocated from zs_malloc, it must be mapped using
  1053. * this function. When done with the object, it must be unmapped using
  1054. * zs_unmap_object.
  1055. *
  1056. * Only one object can be mapped per cpu at a time. There is no protection
  1057. * against nested mappings.
  1058. *
  1059. * This function returns with preemption and page faults disabled.
  1060. */
  1061. void *zs_map_object(struct zs_pool *pool, unsigned long handle,
  1062. enum zs_mapmode mm)
  1063. {
  1064. struct page *page;
  1065. unsigned long obj, obj_idx, off;
  1066. unsigned int class_idx;
  1067. enum fullness_group fg;
  1068. struct size_class *class;
  1069. struct mapping_area *area;
  1070. struct page *pages[2];
  1071. void *ret;
  1072. BUG_ON(!handle);
  1073. /*
  1074. * Because we use per-cpu mapping areas shared among the
  1075. * pools/users, we can't allow mapping in interrupt context
  1076. * because it can corrupt another users mappings.
  1077. */
  1078. BUG_ON(in_interrupt());
  1079. /* From now on, migration cannot move the object */
  1080. pin_tag(handle);
  1081. obj = handle_to_obj(handle);
  1082. obj_to_location(obj, &page, &obj_idx);
  1083. get_zspage_mapping(get_first_page(page), &class_idx, &fg);
  1084. class = pool->size_class[class_idx];
  1085. off = obj_idx_to_offset(page, obj_idx, class->size);
  1086. area = &get_cpu_var(zs_map_area);
  1087. area->vm_mm = mm;
  1088. if (off + class->size <= PAGE_SIZE) {
  1089. /* this object is contained entirely within a page */
  1090. area->vm_addr = kmap_atomic(page);
  1091. ret = area->vm_addr + off;
  1092. goto out;
  1093. }
  1094. /* this object spans two pages */
  1095. pages[0] = page;
  1096. pages[1] = get_next_page(page);
  1097. BUG_ON(!pages[1]);
  1098. ret = __zs_map_object(area, pages, off, class->size);
  1099. out:
  1100. if (!class->huge)
  1101. ret += ZS_HANDLE_SIZE;
  1102. return ret;
  1103. }
  1104. EXPORT_SYMBOL_GPL(zs_map_object);
  1105. void zs_unmap_object(struct zs_pool *pool, unsigned long handle)
  1106. {
  1107. struct page *page;
  1108. unsigned long obj, obj_idx, off;
  1109. unsigned int class_idx;
  1110. enum fullness_group fg;
  1111. struct size_class *class;
  1112. struct mapping_area *area;
  1113. BUG_ON(!handle);
  1114. obj = handle_to_obj(handle);
  1115. obj_to_location(obj, &page, &obj_idx);
  1116. get_zspage_mapping(get_first_page(page), &class_idx, &fg);
  1117. class = pool->size_class[class_idx];
  1118. off = obj_idx_to_offset(page, obj_idx, class->size);
  1119. area = this_cpu_ptr(&zs_map_area);
  1120. if (off + class->size <= PAGE_SIZE)
  1121. kunmap_atomic(area->vm_addr);
  1122. else {
  1123. struct page *pages[2];
  1124. pages[0] = page;
  1125. pages[1] = get_next_page(page);
  1126. BUG_ON(!pages[1]);
  1127. __zs_unmap_object(area, pages, off, class->size);
  1128. }
  1129. put_cpu_var(zs_map_area);
  1130. unpin_tag(handle);
  1131. }
  1132. EXPORT_SYMBOL_GPL(zs_unmap_object);
  1133. static unsigned long obj_malloc(struct page *first_page,
  1134. struct size_class *class, unsigned long handle)
  1135. {
  1136. unsigned long obj;
  1137. struct link_free *link;
  1138. struct page *m_page;
  1139. unsigned long m_objidx, m_offset;
  1140. void *vaddr;
  1141. handle |= OBJ_ALLOCATED_TAG;
  1142. obj = (unsigned long)first_page->freelist;
  1143. obj_to_location(obj, &m_page, &m_objidx);
  1144. m_offset = obj_idx_to_offset(m_page, m_objidx, class->size);
  1145. vaddr = kmap_atomic(m_page);
  1146. link = (struct link_free *)vaddr + m_offset / sizeof(*link);
  1147. first_page->freelist = link->next;
  1148. if (!class->huge)
  1149. /* record handle in the header of allocated chunk */
  1150. link->handle = handle;
  1151. else
  1152. /* record handle in first_page->private */
  1153. set_page_private(first_page, handle);
  1154. kunmap_atomic(vaddr);
  1155. first_page->inuse++;
  1156. zs_stat_inc(class, OBJ_USED, 1);
  1157. return obj;
  1158. }
  1159. /**
  1160. * zs_malloc - Allocate block of given size from pool.
  1161. * @pool: pool to allocate from
  1162. * @size: size of block to allocate
  1163. *
  1164. * On success, handle to the allocated object is returned,
  1165. * otherwise 0.
  1166. * Allocation requests with size > ZS_MAX_ALLOC_SIZE will fail.
  1167. */
  1168. unsigned long zs_malloc(struct zs_pool *pool, size_t size)
  1169. {
  1170. unsigned long handle, obj;
  1171. struct size_class *class;
  1172. struct page *first_page;
  1173. if (unlikely(!size || size > ZS_MAX_ALLOC_SIZE))
  1174. return 0;
  1175. handle = alloc_handle(pool);
  1176. if (!handle)
  1177. return 0;
  1178. /* extra space in chunk to keep the handle */
  1179. size += ZS_HANDLE_SIZE;
  1180. class = pool->size_class[get_size_class_index(size)];
  1181. spin_lock(&class->lock);
  1182. first_page = find_get_zspage(class);
  1183. if (!first_page) {
  1184. spin_unlock(&class->lock);
  1185. first_page = alloc_zspage(class, pool->flags);
  1186. if (unlikely(!first_page)) {
  1187. free_handle(pool, handle);
  1188. return 0;
  1189. }
  1190. set_zspage_mapping(first_page, class->index, ZS_EMPTY);
  1191. atomic_long_add(class->pages_per_zspage,
  1192. &pool->pages_allocated);
  1193. spin_lock(&class->lock);
  1194. zs_stat_inc(class, OBJ_ALLOCATED, get_maxobj_per_zspage(
  1195. class->size, class->pages_per_zspage));
  1196. }
  1197. obj = obj_malloc(first_page, class, handle);
  1198. /* Now move the zspage to another fullness group, if required */
  1199. fix_fullness_group(class, first_page);
  1200. record_obj(handle, obj);
  1201. spin_unlock(&class->lock);
  1202. return handle;
  1203. }
  1204. EXPORT_SYMBOL_GPL(zs_malloc);
  1205. static void obj_free(struct zs_pool *pool, struct size_class *class,
  1206. unsigned long obj)
  1207. {
  1208. struct link_free *link;
  1209. struct page *first_page, *f_page;
  1210. unsigned long f_objidx, f_offset;
  1211. void *vaddr;
  1212. BUG_ON(!obj);
  1213. obj &= ~OBJ_ALLOCATED_TAG;
  1214. obj_to_location(obj, &f_page, &f_objidx);
  1215. first_page = get_first_page(f_page);
  1216. f_offset = obj_idx_to_offset(f_page, f_objidx, class->size);
  1217. vaddr = kmap_atomic(f_page);
  1218. /* Insert this object in containing zspage's freelist */
  1219. link = (struct link_free *)(vaddr + f_offset);
  1220. link->next = first_page->freelist;
  1221. if (class->huge)
  1222. set_page_private(first_page, 0);
  1223. kunmap_atomic(vaddr);
  1224. first_page->freelist = (void *)obj;
  1225. first_page->inuse--;
  1226. zs_stat_dec(class, OBJ_USED, 1);
  1227. }
  1228. void zs_free(struct zs_pool *pool, unsigned long handle)
  1229. {
  1230. struct page *first_page, *f_page;
  1231. unsigned long obj, f_objidx;
  1232. int class_idx;
  1233. struct size_class *class;
  1234. enum fullness_group fullness;
  1235. if (unlikely(!handle))
  1236. return;
  1237. pin_tag(handle);
  1238. obj = handle_to_obj(handle);
  1239. obj_to_location(obj, &f_page, &f_objidx);
  1240. first_page = get_first_page(f_page);
  1241. get_zspage_mapping(first_page, &class_idx, &fullness);
  1242. class = pool->size_class[class_idx];
  1243. spin_lock(&class->lock);
  1244. obj_free(pool, class, obj);
  1245. fullness = fix_fullness_group(class, first_page);
  1246. if (fullness == ZS_EMPTY) {
  1247. zs_stat_dec(class, OBJ_ALLOCATED, get_maxobj_per_zspage(
  1248. class->size, class->pages_per_zspage));
  1249. atomic_long_sub(class->pages_per_zspage,
  1250. &pool->pages_allocated);
  1251. free_zspage(first_page);
  1252. }
  1253. spin_unlock(&class->lock);
  1254. unpin_tag(handle);
  1255. free_handle(pool, handle);
  1256. }
  1257. EXPORT_SYMBOL_GPL(zs_free);
  1258. static void zs_object_copy(unsigned long dst, unsigned long src,
  1259. struct size_class *class)
  1260. {
  1261. struct page *s_page, *d_page;
  1262. unsigned long s_objidx, d_objidx;
  1263. unsigned long s_off, d_off;
  1264. void *s_addr, *d_addr;
  1265. int s_size, d_size, size;
  1266. int written = 0;
  1267. s_size = d_size = class->size;
  1268. obj_to_location(src, &s_page, &s_objidx);
  1269. obj_to_location(dst, &d_page, &d_objidx);
  1270. s_off = obj_idx_to_offset(s_page, s_objidx, class->size);
  1271. d_off = obj_idx_to_offset(d_page, d_objidx, class->size);
  1272. if (s_off + class->size > PAGE_SIZE)
  1273. s_size = PAGE_SIZE - s_off;
  1274. if (d_off + class->size > PAGE_SIZE)
  1275. d_size = PAGE_SIZE - d_off;
  1276. s_addr = kmap_atomic(s_page);
  1277. d_addr = kmap_atomic(d_page);
  1278. while (1) {
  1279. size = min(s_size, d_size);
  1280. memcpy(d_addr + d_off, s_addr + s_off, size);
  1281. written += size;
  1282. if (written == class->size)
  1283. break;
  1284. s_off += size;
  1285. s_size -= size;
  1286. d_off += size;
  1287. d_size -= size;
  1288. if (s_off >= PAGE_SIZE) {
  1289. kunmap_atomic(d_addr);
  1290. kunmap_atomic(s_addr);
  1291. s_page = get_next_page(s_page);
  1292. BUG_ON(!s_page);
  1293. s_addr = kmap_atomic(s_page);
  1294. d_addr = kmap_atomic(d_page);
  1295. s_size = class->size - written;
  1296. s_off = 0;
  1297. }
  1298. if (d_off >= PAGE_SIZE) {
  1299. kunmap_atomic(d_addr);
  1300. d_page = get_next_page(d_page);
  1301. BUG_ON(!d_page);
  1302. d_addr = kmap_atomic(d_page);
  1303. d_size = class->size - written;
  1304. d_off = 0;
  1305. }
  1306. }
  1307. kunmap_atomic(d_addr);
  1308. kunmap_atomic(s_addr);
  1309. }
  1310. /*
  1311. * Find alloced object in zspage from index object and
  1312. * return handle.
  1313. */
  1314. static unsigned long find_alloced_obj(struct page *page, int index,
  1315. struct size_class *class)
  1316. {
  1317. unsigned long head;
  1318. int offset = 0;
  1319. unsigned long handle = 0;
  1320. void *addr = kmap_atomic(page);
  1321. if (!is_first_page(page))
  1322. offset = page->index;
  1323. offset += class->size * index;
  1324. while (offset < PAGE_SIZE) {
  1325. head = obj_to_head(class, page, addr + offset);
  1326. if (head & OBJ_ALLOCATED_TAG) {
  1327. handle = head & ~OBJ_ALLOCATED_TAG;
  1328. if (trypin_tag(handle))
  1329. break;
  1330. handle = 0;
  1331. }
  1332. offset += class->size;
  1333. index++;
  1334. }
  1335. kunmap_atomic(addr);
  1336. return handle;
  1337. }
  1338. struct zs_compact_control {
  1339. /* Source page for migration which could be a subpage of zspage. */
  1340. struct page *s_page;
  1341. /* Destination page for migration which should be a first page
  1342. * of zspage. */
  1343. struct page *d_page;
  1344. /* Starting object index within @s_page which used for live object
  1345. * in the subpage. */
  1346. int index;
  1347. };
  1348. static int migrate_zspage(struct zs_pool *pool, struct size_class *class,
  1349. struct zs_compact_control *cc)
  1350. {
  1351. unsigned long used_obj, free_obj;
  1352. unsigned long handle;
  1353. struct page *s_page = cc->s_page;
  1354. struct page *d_page = cc->d_page;
  1355. unsigned long index = cc->index;
  1356. int ret = 0;
  1357. while (1) {
  1358. handle = find_alloced_obj(s_page, index, class);
  1359. if (!handle) {
  1360. s_page = get_next_page(s_page);
  1361. if (!s_page)
  1362. break;
  1363. index = 0;
  1364. continue;
  1365. }
  1366. /* Stop if there is no more space */
  1367. if (zspage_full(d_page)) {
  1368. unpin_tag(handle);
  1369. ret = -ENOMEM;
  1370. break;
  1371. }
  1372. used_obj = handle_to_obj(handle);
  1373. free_obj = obj_malloc(d_page, class, handle);
  1374. zs_object_copy(free_obj, used_obj, class);
  1375. index++;
  1376. /*
  1377. * record_obj updates handle's value to free_obj and it will
  1378. * invalidate lock bit(ie, HANDLE_PIN_BIT) of handle, which
  1379. * breaks synchronization using pin_tag(e,g, zs_free) so
  1380. * let's keep the lock bit.
  1381. */
  1382. free_obj |= BIT(HANDLE_PIN_BIT);
  1383. record_obj(handle, free_obj);
  1384. unpin_tag(handle);
  1385. obj_free(pool, class, used_obj);
  1386. }
  1387. /* Remember last position in this iteration */
  1388. cc->s_page = s_page;
  1389. cc->index = index;
  1390. return ret;
  1391. }
  1392. static struct page *isolate_target_page(struct size_class *class)
  1393. {
  1394. int i;
  1395. struct page *page;
  1396. for (i = 0; i < _ZS_NR_FULLNESS_GROUPS; i++) {
  1397. page = class->fullness_list[i];
  1398. if (page) {
  1399. remove_zspage(page, class, i);
  1400. break;
  1401. }
  1402. }
  1403. return page;
  1404. }
  1405. /*
  1406. * putback_zspage - add @first_page into right class's fullness list
  1407. * @pool: target pool
  1408. * @class: destination class
  1409. * @first_page: target page
  1410. *
  1411. * Return @fist_page's fullness_group
  1412. */
  1413. static enum fullness_group putback_zspage(struct zs_pool *pool,
  1414. struct size_class *class,
  1415. struct page *first_page)
  1416. {
  1417. enum fullness_group fullness;
  1418. BUG_ON(!is_first_page(first_page));
  1419. fullness = get_fullness_group(first_page);
  1420. insert_zspage(first_page, class, fullness);
  1421. set_zspage_mapping(first_page, class->index, fullness);
  1422. if (fullness == ZS_EMPTY) {
  1423. zs_stat_dec(class, OBJ_ALLOCATED, get_maxobj_per_zspage(
  1424. class->size, class->pages_per_zspage));
  1425. atomic_long_sub(class->pages_per_zspage,
  1426. &pool->pages_allocated);
  1427. free_zspage(first_page);
  1428. }
  1429. return fullness;
  1430. }
  1431. static struct page *isolate_source_page(struct size_class *class)
  1432. {
  1433. int i;
  1434. struct page *page = NULL;
  1435. for (i = ZS_ALMOST_EMPTY; i >= ZS_ALMOST_FULL; i--) {
  1436. page = class->fullness_list[i];
  1437. if (!page)
  1438. continue;
  1439. remove_zspage(page, class, i);
  1440. break;
  1441. }
  1442. return page;
  1443. }
  1444. /*
  1445. *
  1446. * Based on the number of unused allocated objects calculate
  1447. * and return the number of pages that we can free.
  1448. */
  1449. static unsigned long zs_can_compact(struct size_class *class)
  1450. {
  1451. unsigned long obj_wasted;
  1452. unsigned long obj_allocated = zs_stat_get(class, OBJ_ALLOCATED);
  1453. unsigned long obj_used = zs_stat_get(class, OBJ_USED);
  1454. if (obj_allocated <= obj_used)
  1455. return 0;
  1456. obj_wasted = obj_allocated - obj_used;
  1457. obj_wasted /= get_maxobj_per_zspage(class->size,
  1458. class->pages_per_zspage);
  1459. return obj_wasted * class->pages_per_zspage;
  1460. }
  1461. static void __zs_compact(struct zs_pool *pool, struct size_class *class)
  1462. {
  1463. struct zs_compact_control cc;
  1464. struct page *src_page;
  1465. struct page *dst_page = NULL;
  1466. spin_lock(&class->lock);
  1467. while ((src_page = isolate_source_page(class))) {
  1468. BUG_ON(!is_first_page(src_page));
  1469. if (!zs_can_compact(class))
  1470. break;
  1471. cc.index = 0;
  1472. cc.s_page = src_page;
  1473. while ((dst_page = isolate_target_page(class))) {
  1474. cc.d_page = dst_page;
  1475. /*
  1476. * If there is no more space in dst_page, resched
  1477. * and see if anyone had allocated another zspage.
  1478. */
  1479. if (!migrate_zspage(pool, class, &cc))
  1480. break;
  1481. putback_zspage(pool, class, dst_page);
  1482. }
  1483. /* Stop if we couldn't find slot */
  1484. if (dst_page == NULL)
  1485. break;
  1486. putback_zspage(pool, class, dst_page);
  1487. if (putback_zspage(pool, class, src_page) == ZS_EMPTY)
  1488. pool->stats.pages_compacted += class->pages_per_zspage;
  1489. spin_unlock(&class->lock);
  1490. cond_resched();
  1491. spin_lock(&class->lock);
  1492. }
  1493. if (src_page)
  1494. putback_zspage(pool, class, src_page);
  1495. spin_unlock(&class->lock);
  1496. }
  1497. unsigned long zs_compact(struct zs_pool *pool)
  1498. {
  1499. int i;
  1500. struct size_class *class;
  1501. for (i = zs_size_classes - 1; i >= 0; i--) {
  1502. class = pool->size_class[i];
  1503. if (!class)
  1504. continue;
  1505. if (class->index != i)
  1506. continue;
  1507. __zs_compact(pool, class);
  1508. }
  1509. return pool->stats.pages_compacted;
  1510. }
  1511. EXPORT_SYMBOL_GPL(zs_compact);
  1512. void zs_pool_stats(struct zs_pool *pool, struct zs_pool_stats *stats)
  1513. {
  1514. memcpy(stats, &pool->stats, sizeof(struct zs_pool_stats));
  1515. }
  1516. EXPORT_SYMBOL_GPL(zs_pool_stats);
  1517. static unsigned long zs_shrinker_scan(struct shrinker *shrinker,
  1518. struct shrink_control *sc)
  1519. {
  1520. unsigned long pages_freed;
  1521. struct zs_pool *pool = container_of(shrinker, struct zs_pool,
  1522. shrinker);
  1523. pages_freed = pool->stats.pages_compacted;
  1524. /*
  1525. * Compact classes and calculate compaction delta.
  1526. * Can run concurrently with a manually triggered
  1527. * (by user) compaction.
  1528. */
  1529. pages_freed = zs_compact(pool) - pages_freed;
  1530. return pages_freed ? pages_freed : SHRINK_STOP;
  1531. }
  1532. static unsigned long zs_shrinker_count(struct shrinker *shrinker,
  1533. struct shrink_control *sc)
  1534. {
  1535. int i;
  1536. struct size_class *class;
  1537. unsigned long pages_to_free = 0;
  1538. struct zs_pool *pool = container_of(shrinker, struct zs_pool,
  1539. shrinker);
  1540. for (i = zs_size_classes - 1; i >= 0; i--) {
  1541. class = pool->size_class[i];
  1542. if (!class)
  1543. continue;
  1544. if (class->index != i)
  1545. continue;
  1546. pages_to_free += zs_can_compact(class);
  1547. }
  1548. return pages_to_free;
  1549. }
  1550. static void zs_unregister_shrinker(struct zs_pool *pool)
  1551. {
  1552. if (pool->shrinker_enabled) {
  1553. unregister_shrinker(&pool->shrinker);
  1554. pool->shrinker_enabled = false;
  1555. }
  1556. }
  1557. static int zs_register_shrinker(struct zs_pool *pool)
  1558. {
  1559. pool->shrinker.scan_objects = zs_shrinker_scan;
  1560. pool->shrinker.count_objects = zs_shrinker_count;
  1561. pool->shrinker.batch = 0;
  1562. pool->shrinker.seeks = DEFAULT_SEEKS;
  1563. return register_shrinker(&pool->shrinker);
  1564. }
  1565. /**
  1566. * zs_create_pool - Creates an allocation pool to work from.
  1567. * @flags: allocation flags used to allocate pool metadata
  1568. *
  1569. * This function must be called before anything when using
  1570. * the zsmalloc allocator.
  1571. *
  1572. * On success, a pointer to the newly created pool is returned,
  1573. * otherwise NULL.
  1574. */
  1575. struct zs_pool *zs_create_pool(const char *name, gfp_t flags)
  1576. {
  1577. int i;
  1578. struct zs_pool *pool;
  1579. struct size_class *prev_class = NULL;
  1580. pool = kzalloc(sizeof(*pool), GFP_KERNEL);
  1581. if (!pool)
  1582. return NULL;
  1583. pool->size_class = kcalloc(zs_size_classes, sizeof(struct size_class *),
  1584. GFP_KERNEL);
  1585. if (!pool->size_class) {
  1586. kfree(pool);
  1587. return NULL;
  1588. }
  1589. pool->name = kstrdup(name, GFP_KERNEL);
  1590. if (!pool->name)
  1591. goto err;
  1592. if (create_handle_cache(pool))
  1593. goto err;
  1594. /*
  1595. * Iterate reversly, because, size of size_class that we want to use
  1596. * for merging should be larger or equal to current size.
  1597. */
  1598. for (i = zs_size_classes - 1; i >= 0; i--) {
  1599. int size;
  1600. int pages_per_zspage;
  1601. struct size_class *class;
  1602. size = ZS_MIN_ALLOC_SIZE + i * ZS_SIZE_CLASS_DELTA;
  1603. if (size > ZS_MAX_ALLOC_SIZE)
  1604. size = ZS_MAX_ALLOC_SIZE;
  1605. pages_per_zspage = get_pages_per_zspage(size);
  1606. /*
  1607. * size_class is used for normal zsmalloc operation such
  1608. * as alloc/free for that size. Although it is natural that we
  1609. * have one size_class for each size, there is a chance that we
  1610. * can get more memory utilization if we use one size_class for
  1611. * many different sizes whose size_class have same
  1612. * characteristics. So, we makes size_class point to
  1613. * previous size_class if possible.
  1614. */
  1615. if (prev_class) {
  1616. if (can_merge(prev_class, size, pages_per_zspage)) {
  1617. pool->size_class[i] = prev_class;
  1618. continue;
  1619. }
  1620. }
  1621. class = kzalloc(sizeof(struct size_class), GFP_KERNEL);
  1622. if (!class)
  1623. goto err;
  1624. class->size = size;
  1625. class->index = i;
  1626. class->pages_per_zspage = pages_per_zspage;
  1627. if (pages_per_zspage == 1 &&
  1628. get_maxobj_per_zspage(size, pages_per_zspage) == 1)
  1629. class->huge = true;
  1630. spin_lock_init(&class->lock);
  1631. pool->size_class[i] = class;
  1632. prev_class = class;
  1633. }
  1634. pool->flags = flags;
  1635. if (zs_pool_stat_create(name, pool))
  1636. goto err;
  1637. /*
  1638. * Not critical, we still can use the pool
  1639. * and user can trigger compaction manually.
  1640. */
  1641. if (zs_register_shrinker(pool) == 0)
  1642. pool->shrinker_enabled = true;
  1643. return pool;
  1644. err:
  1645. zs_destroy_pool(pool);
  1646. return NULL;
  1647. }
  1648. EXPORT_SYMBOL_GPL(zs_create_pool);
  1649. void zs_destroy_pool(struct zs_pool *pool)
  1650. {
  1651. int i;
  1652. zs_unregister_shrinker(pool);
  1653. zs_pool_stat_destroy(pool);
  1654. for (i = 0; i < zs_size_classes; i++) {
  1655. int fg;
  1656. struct size_class *class = pool->size_class[i];
  1657. if (!class)
  1658. continue;
  1659. if (class->index != i)
  1660. continue;
  1661. for (fg = 0; fg < _ZS_NR_FULLNESS_GROUPS; fg++) {
  1662. if (class->fullness_list[fg]) {
  1663. pr_info("Freeing non-empty class with size %db, fullness group %d\n",
  1664. class->size, fg);
  1665. }
  1666. }
  1667. kfree(class);
  1668. }
  1669. destroy_handle_cache(pool);
  1670. kfree(pool->size_class);
  1671. kfree(pool->name);
  1672. kfree(pool);
  1673. }
  1674. EXPORT_SYMBOL_GPL(zs_destroy_pool);
  1675. static int __init zs_init(void)
  1676. {
  1677. int ret = zs_register_cpu_notifier();
  1678. if (ret)
  1679. goto notifier_fail;
  1680. init_zs_size_classes();
  1681. #ifdef CONFIG_ZPOOL
  1682. zpool_register_driver(&zs_zpool_driver);
  1683. #endif
  1684. ret = zs_stat_init();
  1685. if (ret) {
  1686. pr_err("zs stat initialization failed\n");
  1687. goto stat_fail;
  1688. }
  1689. return 0;
  1690. stat_fail:
  1691. #ifdef CONFIG_ZPOOL
  1692. zpool_unregister_driver(&zs_zpool_driver);
  1693. #endif
  1694. notifier_fail:
  1695. zs_unregister_cpu_notifier();
  1696. return ret;
  1697. }
  1698. static void __exit zs_exit(void)
  1699. {
  1700. #ifdef CONFIG_ZPOOL
  1701. zpool_unregister_driver(&zs_zpool_driver);
  1702. #endif
  1703. zs_unregister_cpu_notifier();
  1704. zs_stat_exit();
  1705. }
  1706. module_init(zs_init);
  1707. module_exit(zs_exit);
  1708. MODULE_LICENSE("Dual BSD/GPL");
  1709. MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");