zsmalloc.c 64 KB

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