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