zsmalloc.c 49 KB

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