zsmalloc.c 57 KB

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