zsmalloc.c 63 KB

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