zsmalloc.c 55 KB

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