zsmalloc.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310
  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. if (!IS_ENABLED(CONFIG_COMPACTION))
  821. gfp &= ~__GFP_MOVABLE;
  822. zspage->magic = ZSPAGE_MAGIC;
  823. migrate_lock_init(zspage);
  824. for (i = 0; i < class->pages_per_zspage; i++) {
  825. struct page *page;
  826. page = alloc_page(gfp);
  827. if (!page) {
  828. while (--i >= 0) {
  829. dec_zone_page_state(pages[i], NR_ZSPAGES);
  830. __ClearPageZsmalloc(pages[i]);
  831. __free_page(pages[i]);
  832. }
  833. cache_free_zspage(pool, zspage);
  834. return NULL;
  835. }
  836. __SetPageZsmalloc(page);
  837. inc_zone_page_state(page, NR_ZSPAGES);
  838. pages[i] = page;
  839. }
  840. create_page_chain(class, zspage, pages);
  841. init_zspage(class, zspage);
  842. zspage->pool = pool;
  843. zspage->class = class->index;
  844. return zspage;
  845. }
  846. static struct zspage *find_get_zspage(struct size_class *class)
  847. {
  848. int i;
  849. struct zspage *zspage;
  850. for (i = ZS_INUSE_RATIO_99; i >= ZS_INUSE_RATIO_0; i--) {
  851. zspage = list_first_entry_or_null(&class->fullness_list[i],
  852. struct zspage, list);
  853. if (zspage)
  854. break;
  855. }
  856. return zspage;
  857. }
  858. static inline int __zs_cpu_up(struct mapping_area *area)
  859. {
  860. /*
  861. * Make sure we don't leak memory if a cpu UP notification
  862. * and zs_init() race and both call zs_cpu_up() on the same cpu
  863. */
  864. if (area->vm_buf)
  865. return 0;
  866. area->vm_buf = kmalloc(ZS_MAX_ALLOC_SIZE, GFP_KERNEL);
  867. if (!area->vm_buf)
  868. return -ENOMEM;
  869. return 0;
  870. }
  871. static inline void __zs_cpu_down(struct mapping_area *area)
  872. {
  873. kfree(area->vm_buf);
  874. area->vm_buf = NULL;
  875. }
  876. static void *__zs_map_object(struct mapping_area *area,
  877. struct page *pages[2], int off, int size)
  878. {
  879. int sizes[2];
  880. void *addr;
  881. char *buf = area->vm_buf;
  882. /* disable page faults to match kmap_atomic() return conditions */
  883. pagefault_disable();
  884. /* no read fastpath */
  885. if (area->vm_mm == ZS_MM_WO)
  886. goto out;
  887. sizes[0] = PAGE_SIZE - off;
  888. sizes[1] = size - sizes[0];
  889. /* copy object to per-cpu buffer */
  890. addr = kmap_atomic(pages[0]);
  891. memcpy(buf, addr + off, sizes[0]);
  892. kunmap_atomic(addr);
  893. addr = kmap_atomic(pages[1]);
  894. memcpy(buf + sizes[0], addr, sizes[1]);
  895. kunmap_atomic(addr);
  896. out:
  897. return area->vm_buf;
  898. }
  899. static void __zs_unmap_object(struct mapping_area *area,
  900. struct page *pages[2], int off, int size)
  901. {
  902. int sizes[2];
  903. void *addr;
  904. char *buf;
  905. /* no write fastpath */
  906. if (area->vm_mm == ZS_MM_RO)
  907. goto out;
  908. buf = area->vm_buf;
  909. buf = buf + ZS_HANDLE_SIZE;
  910. size -= ZS_HANDLE_SIZE;
  911. off += ZS_HANDLE_SIZE;
  912. sizes[0] = PAGE_SIZE - off;
  913. sizes[1] = size - sizes[0];
  914. /* copy per-cpu buffer to object */
  915. addr = kmap_atomic(pages[0]);
  916. memcpy(addr + off, buf, sizes[0]);
  917. kunmap_atomic(addr);
  918. addr = kmap_atomic(pages[1]);
  919. memcpy(addr, buf + sizes[0], sizes[1]);
  920. kunmap_atomic(addr);
  921. out:
  922. /* enable page faults to match kunmap_atomic() return conditions */
  923. pagefault_enable();
  924. }
  925. static int zs_cpu_prepare(unsigned int cpu)
  926. {
  927. struct mapping_area *area;
  928. area = &per_cpu(zs_map_area, cpu);
  929. return __zs_cpu_up(area);
  930. }
  931. static int zs_cpu_dead(unsigned int cpu)
  932. {
  933. struct mapping_area *area;
  934. area = &per_cpu(zs_map_area, cpu);
  935. __zs_cpu_down(area);
  936. return 0;
  937. }
  938. static bool can_merge(struct size_class *prev, int pages_per_zspage,
  939. int objs_per_zspage)
  940. {
  941. if (prev->pages_per_zspage == pages_per_zspage &&
  942. prev->objs_per_zspage == objs_per_zspage)
  943. return true;
  944. return false;
  945. }
  946. static bool zspage_full(struct size_class *class, struct zspage *zspage)
  947. {
  948. return get_zspage_inuse(zspage) == class->objs_per_zspage;
  949. }
  950. static bool zspage_empty(struct zspage *zspage)
  951. {
  952. return get_zspage_inuse(zspage) == 0;
  953. }
  954. /**
  955. * zs_lookup_class_index() - Returns index of the zsmalloc &size_class
  956. * that hold objects of the provided size.
  957. * @pool: zsmalloc pool to use
  958. * @size: object size
  959. *
  960. * Context: Any context.
  961. *
  962. * Return: the index of the zsmalloc &size_class that hold objects of the
  963. * provided size.
  964. */
  965. unsigned int zs_lookup_class_index(struct zs_pool *pool, unsigned int size)
  966. {
  967. struct size_class *class;
  968. class = pool->size_class[get_size_class_index(size)];
  969. return class->index;
  970. }
  971. EXPORT_SYMBOL_GPL(zs_lookup_class_index);
  972. unsigned long zs_get_total_pages(struct zs_pool *pool)
  973. {
  974. return atomic_long_read(&pool->pages_allocated);
  975. }
  976. EXPORT_SYMBOL_GPL(zs_get_total_pages);
  977. /**
  978. * zs_map_object - get address of allocated object from handle.
  979. * @pool: pool from which the object was allocated
  980. * @handle: handle returned from zs_malloc
  981. * @mm: mapping mode to use
  982. *
  983. * Before using an object allocated from zs_malloc, it must be mapped using
  984. * this function. When done with the object, it must be unmapped using
  985. * zs_unmap_object.
  986. *
  987. * Only one object can be mapped per cpu at a time. There is no protection
  988. * against nested mappings.
  989. *
  990. * This function returns with preemption and page faults disabled.
  991. */
  992. void *zs_map_object(struct zs_pool *pool, unsigned long handle,
  993. enum zs_mapmode mm)
  994. {
  995. struct zspage *zspage;
  996. struct page *page;
  997. unsigned long obj, off;
  998. unsigned int obj_idx;
  999. struct size_class *class;
  1000. struct mapping_area *area;
  1001. struct page *pages[2];
  1002. void *ret;
  1003. /*
  1004. * Because we use per-cpu mapping areas shared among the
  1005. * pools/users, we can't allow mapping in interrupt context
  1006. * because it can corrupt another users mappings.
  1007. */
  1008. BUG_ON(in_interrupt());
  1009. /* It guarantees it can get zspage from handle safely */
  1010. read_lock(&pool->migrate_lock);
  1011. obj = handle_to_obj(handle);
  1012. obj_to_location(obj, &page, &obj_idx);
  1013. zspage = get_zspage(page);
  1014. /*
  1015. * migration cannot move any zpages in this zspage. Here, class->lock
  1016. * is too heavy since callers would take some time until they calls
  1017. * zs_unmap_object API so delegate the locking from class to zspage
  1018. * which is smaller granularity.
  1019. */
  1020. migrate_read_lock(zspage);
  1021. read_unlock(&pool->migrate_lock);
  1022. class = zspage_class(pool, zspage);
  1023. off = offset_in_page(class->size * obj_idx);
  1024. local_lock(&zs_map_area.lock);
  1025. area = this_cpu_ptr(&zs_map_area);
  1026. area->vm_mm = mm;
  1027. if (off + class->size <= PAGE_SIZE) {
  1028. /* this object is contained entirely within a page */
  1029. area->vm_addr = kmap_atomic(page);
  1030. ret = area->vm_addr + off;
  1031. goto out;
  1032. }
  1033. /* this object spans two pages */
  1034. pages[0] = page;
  1035. pages[1] = get_next_page(page);
  1036. BUG_ON(!pages[1]);
  1037. ret = __zs_map_object(area, pages, off, class->size);
  1038. out:
  1039. if (likely(!ZsHugePage(zspage)))
  1040. ret += ZS_HANDLE_SIZE;
  1041. return ret;
  1042. }
  1043. EXPORT_SYMBOL_GPL(zs_map_object);
  1044. void zs_unmap_object(struct zs_pool *pool, unsigned long handle)
  1045. {
  1046. struct zspage *zspage;
  1047. struct page *page;
  1048. unsigned long obj, off;
  1049. unsigned int obj_idx;
  1050. struct size_class *class;
  1051. struct mapping_area *area;
  1052. obj = handle_to_obj(handle);
  1053. obj_to_location(obj, &page, &obj_idx);
  1054. zspage = get_zspage(page);
  1055. class = zspage_class(pool, zspage);
  1056. off = offset_in_page(class->size * obj_idx);
  1057. area = this_cpu_ptr(&zs_map_area);
  1058. if (off + class->size <= PAGE_SIZE)
  1059. kunmap_atomic(area->vm_addr);
  1060. else {
  1061. struct page *pages[2];
  1062. pages[0] = page;
  1063. pages[1] = get_next_page(page);
  1064. BUG_ON(!pages[1]);
  1065. __zs_unmap_object(area, pages, off, class->size);
  1066. }
  1067. local_unlock(&zs_map_area.lock);
  1068. migrate_read_unlock(zspage);
  1069. }
  1070. EXPORT_SYMBOL_GPL(zs_unmap_object);
  1071. /**
  1072. * zs_huge_class_size() - Returns the size (in bytes) of the first huge
  1073. * zsmalloc &size_class.
  1074. * @pool: zsmalloc pool to use
  1075. *
  1076. * The function returns the size of the first huge class - any object of equal
  1077. * or bigger size will be stored in zspage consisting of a single physical
  1078. * page.
  1079. *
  1080. * Context: Any context.
  1081. *
  1082. * Return: the size (in bytes) of the first huge zsmalloc &size_class.
  1083. */
  1084. size_t zs_huge_class_size(struct zs_pool *pool)
  1085. {
  1086. return huge_class_size;
  1087. }
  1088. EXPORT_SYMBOL_GPL(zs_huge_class_size);
  1089. static unsigned long obj_malloc(struct zs_pool *pool,
  1090. struct zspage *zspage, unsigned long handle)
  1091. {
  1092. int i, nr_page, offset;
  1093. unsigned long obj;
  1094. struct link_free *link;
  1095. struct size_class *class;
  1096. struct page *m_page;
  1097. unsigned long m_offset;
  1098. void *vaddr;
  1099. class = pool->size_class[zspage->class];
  1100. obj = get_freeobj(zspage);
  1101. offset = obj * class->size;
  1102. nr_page = offset >> PAGE_SHIFT;
  1103. m_offset = offset_in_page(offset);
  1104. m_page = get_first_page(zspage);
  1105. for (i = 0; i < nr_page; i++)
  1106. m_page = get_next_page(m_page);
  1107. vaddr = kmap_atomic(m_page);
  1108. link = (struct link_free *)vaddr + m_offset / sizeof(*link);
  1109. set_freeobj(zspage, link->next >> OBJ_TAG_BITS);
  1110. if (likely(!ZsHugePage(zspage)))
  1111. /* record handle in the header of allocated chunk */
  1112. link->handle = handle | OBJ_ALLOCATED_TAG;
  1113. else
  1114. /* record handle to page->index */
  1115. zspage->first_page->index = handle | OBJ_ALLOCATED_TAG;
  1116. kunmap_atomic(vaddr);
  1117. mod_zspage_inuse(zspage, 1);
  1118. obj = location_to_obj(m_page, obj);
  1119. record_obj(handle, obj);
  1120. return obj;
  1121. }
  1122. /**
  1123. * zs_malloc - Allocate block of given size from pool.
  1124. * @pool: pool to allocate from
  1125. * @size: size of block to allocate
  1126. * @gfp: gfp flags when allocating object
  1127. *
  1128. * On success, handle to the allocated object is returned,
  1129. * otherwise an ERR_PTR().
  1130. * Allocation requests with size > ZS_MAX_ALLOC_SIZE will fail.
  1131. */
  1132. unsigned long zs_malloc(struct zs_pool *pool, size_t size, gfp_t gfp)
  1133. {
  1134. unsigned long handle;
  1135. struct size_class *class;
  1136. int newfg;
  1137. struct zspage *zspage;
  1138. if (unlikely(!size))
  1139. return (unsigned long)ERR_PTR(-EINVAL);
  1140. if (unlikely(size > ZS_MAX_ALLOC_SIZE))
  1141. return (unsigned long)ERR_PTR(-ENOSPC);
  1142. handle = cache_alloc_handle(pool, gfp);
  1143. if (!handle)
  1144. return (unsigned long)ERR_PTR(-ENOMEM);
  1145. /* extra space in chunk to keep the handle */
  1146. size += ZS_HANDLE_SIZE;
  1147. class = pool->size_class[get_size_class_index(size)];
  1148. /* class->lock effectively protects the zpage migration */
  1149. spin_lock(&class->lock);
  1150. zspage = find_get_zspage(class);
  1151. if (likely(zspage)) {
  1152. obj_malloc(pool, zspage, handle);
  1153. /* Now move the zspage to another fullness group, if required */
  1154. fix_fullness_group(class, zspage);
  1155. class_stat_add(class, ZS_OBJS_INUSE, 1);
  1156. goto out;
  1157. }
  1158. spin_unlock(&class->lock);
  1159. zspage = alloc_zspage(pool, class, gfp);
  1160. if (!zspage) {
  1161. cache_free_handle(pool, handle);
  1162. return (unsigned long)ERR_PTR(-ENOMEM);
  1163. }
  1164. spin_lock(&class->lock);
  1165. obj_malloc(pool, zspage, handle);
  1166. newfg = get_fullness_group(class, zspage);
  1167. insert_zspage(class, zspage, newfg);
  1168. atomic_long_add(class->pages_per_zspage, &pool->pages_allocated);
  1169. class_stat_add(class, ZS_OBJS_ALLOCATED, class->objs_per_zspage);
  1170. class_stat_add(class, ZS_OBJS_INUSE, 1);
  1171. /* We completely set up zspage so mark them as movable */
  1172. SetZsPageMovable(pool, zspage);
  1173. out:
  1174. spin_unlock(&class->lock);
  1175. return handle;
  1176. }
  1177. EXPORT_SYMBOL_GPL(zs_malloc);
  1178. static void obj_free(int class_size, unsigned long obj)
  1179. {
  1180. struct link_free *link;
  1181. struct zspage *zspage;
  1182. struct page *f_page;
  1183. unsigned long f_offset;
  1184. unsigned int f_objidx;
  1185. void *vaddr;
  1186. obj_to_location(obj, &f_page, &f_objidx);
  1187. f_offset = offset_in_page(class_size * f_objidx);
  1188. zspage = get_zspage(f_page);
  1189. vaddr = kmap_atomic(f_page);
  1190. link = (struct link_free *)(vaddr + f_offset);
  1191. /* Insert this object in containing zspage's freelist */
  1192. if (likely(!ZsHugePage(zspage)))
  1193. link->next = get_freeobj(zspage) << OBJ_TAG_BITS;
  1194. else
  1195. f_page->index = 0;
  1196. set_freeobj(zspage, f_objidx);
  1197. kunmap_atomic(vaddr);
  1198. mod_zspage_inuse(zspage, -1);
  1199. }
  1200. void zs_free(struct zs_pool *pool, unsigned long handle)
  1201. {
  1202. struct zspage *zspage;
  1203. struct page *f_page;
  1204. unsigned long obj;
  1205. struct size_class *class;
  1206. int fullness;
  1207. if (IS_ERR_OR_NULL((void *)handle))
  1208. return;
  1209. /*
  1210. * The pool->migrate_lock protects the race with zpage's migration
  1211. * so it's safe to get the page from handle.
  1212. */
  1213. read_lock(&pool->migrate_lock);
  1214. obj = handle_to_obj(handle);
  1215. obj_to_page(obj, &f_page);
  1216. zspage = get_zspage(f_page);
  1217. class = zspage_class(pool, zspage);
  1218. spin_lock(&class->lock);
  1219. read_unlock(&pool->migrate_lock);
  1220. class_stat_sub(class, ZS_OBJS_INUSE, 1);
  1221. obj_free(class->size, obj);
  1222. fullness = fix_fullness_group(class, zspage);
  1223. if (fullness == ZS_INUSE_RATIO_0)
  1224. free_zspage(pool, class, zspage);
  1225. spin_unlock(&class->lock);
  1226. cache_free_handle(pool, handle);
  1227. }
  1228. EXPORT_SYMBOL_GPL(zs_free);
  1229. static void zs_object_copy(struct size_class *class, unsigned long dst,
  1230. unsigned long src)
  1231. {
  1232. struct page *s_page, *d_page;
  1233. unsigned int s_objidx, d_objidx;
  1234. unsigned long s_off, d_off;
  1235. void *s_addr, *d_addr;
  1236. int s_size, d_size, size;
  1237. int written = 0;
  1238. s_size = d_size = class->size;
  1239. obj_to_location(src, &s_page, &s_objidx);
  1240. obj_to_location(dst, &d_page, &d_objidx);
  1241. s_off = offset_in_page(class->size * s_objidx);
  1242. d_off = offset_in_page(class->size * d_objidx);
  1243. if (s_off + class->size > PAGE_SIZE)
  1244. s_size = PAGE_SIZE - s_off;
  1245. if (d_off + class->size > PAGE_SIZE)
  1246. d_size = PAGE_SIZE - d_off;
  1247. s_addr = kmap_atomic(s_page);
  1248. d_addr = kmap_atomic(d_page);
  1249. while (1) {
  1250. size = min(s_size, d_size);
  1251. memcpy(d_addr + d_off, s_addr + s_off, size);
  1252. written += size;
  1253. if (written == class->size)
  1254. break;
  1255. s_off += size;
  1256. s_size -= size;
  1257. d_off += size;
  1258. d_size -= size;
  1259. /*
  1260. * Calling kunmap_atomic(d_addr) is necessary. kunmap_atomic()
  1261. * calls must occurs in reverse order of calls to kmap_atomic().
  1262. * So, to call kunmap_atomic(s_addr) we should first call
  1263. * kunmap_atomic(d_addr). For more details see
  1264. * Documentation/mm/highmem.rst.
  1265. */
  1266. if (s_off >= PAGE_SIZE) {
  1267. kunmap_atomic(d_addr);
  1268. kunmap_atomic(s_addr);
  1269. s_page = get_next_page(s_page);
  1270. s_addr = kmap_atomic(s_page);
  1271. d_addr = kmap_atomic(d_page);
  1272. s_size = class->size - written;
  1273. s_off = 0;
  1274. }
  1275. if (d_off >= PAGE_SIZE) {
  1276. kunmap_atomic(d_addr);
  1277. d_page = get_next_page(d_page);
  1278. d_addr = kmap_atomic(d_page);
  1279. d_size = class->size - written;
  1280. d_off = 0;
  1281. }
  1282. }
  1283. kunmap_atomic(d_addr);
  1284. kunmap_atomic(s_addr);
  1285. }
  1286. /*
  1287. * Find alloced object in zspage from index object and
  1288. * return handle.
  1289. */
  1290. static unsigned long find_alloced_obj(struct size_class *class,
  1291. struct page *page, int *obj_idx)
  1292. {
  1293. unsigned int offset;
  1294. int index = *obj_idx;
  1295. unsigned long handle = 0;
  1296. void *addr = kmap_atomic(page);
  1297. offset = get_first_obj_offset(page);
  1298. offset += class->size * index;
  1299. while (offset < PAGE_SIZE) {
  1300. if (obj_allocated(page, addr + offset, &handle))
  1301. break;
  1302. offset += class->size;
  1303. index++;
  1304. }
  1305. kunmap_atomic(addr);
  1306. *obj_idx = index;
  1307. return handle;
  1308. }
  1309. static void migrate_zspage(struct zs_pool *pool, struct zspage *src_zspage,
  1310. struct zspage *dst_zspage)
  1311. {
  1312. unsigned long used_obj, free_obj;
  1313. unsigned long handle;
  1314. int obj_idx = 0;
  1315. struct page *s_page = get_first_page(src_zspage);
  1316. struct size_class *class = pool->size_class[src_zspage->class];
  1317. while (1) {
  1318. handle = find_alloced_obj(class, s_page, &obj_idx);
  1319. if (!handle) {
  1320. s_page = get_next_page(s_page);
  1321. if (!s_page)
  1322. break;
  1323. obj_idx = 0;
  1324. continue;
  1325. }
  1326. used_obj = handle_to_obj(handle);
  1327. free_obj = obj_malloc(pool, dst_zspage, handle);
  1328. zs_object_copy(class, free_obj, used_obj);
  1329. obj_idx++;
  1330. obj_free(class->size, used_obj);
  1331. /* Stop if there is no more space */
  1332. if (zspage_full(class, dst_zspage))
  1333. break;
  1334. /* Stop if there are no more objects to migrate */
  1335. if (zspage_empty(src_zspage))
  1336. break;
  1337. }
  1338. }
  1339. static struct zspage *isolate_src_zspage(struct size_class *class)
  1340. {
  1341. struct zspage *zspage;
  1342. int fg;
  1343. for (fg = ZS_INUSE_RATIO_10; fg <= ZS_INUSE_RATIO_99; fg++) {
  1344. zspage = list_first_entry_or_null(&class->fullness_list[fg],
  1345. struct zspage, list);
  1346. if (zspage) {
  1347. remove_zspage(class, zspage);
  1348. return zspage;
  1349. }
  1350. }
  1351. return zspage;
  1352. }
  1353. static struct zspage *isolate_dst_zspage(struct size_class *class)
  1354. {
  1355. struct zspage *zspage;
  1356. int fg;
  1357. for (fg = ZS_INUSE_RATIO_99; fg >= ZS_INUSE_RATIO_10; fg--) {
  1358. zspage = list_first_entry_or_null(&class->fullness_list[fg],
  1359. struct zspage, list);
  1360. if (zspage) {
  1361. remove_zspage(class, zspage);
  1362. return zspage;
  1363. }
  1364. }
  1365. return zspage;
  1366. }
  1367. /*
  1368. * putback_zspage - add @zspage into right class's fullness list
  1369. * @class: destination class
  1370. * @zspage: target page
  1371. *
  1372. * Return @zspage's fullness status
  1373. */
  1374. static int putback_zspage(struct size_class *class, struct zspage *zspage)
  1375. {
  1376. int fullness;
  1377. fullness = get_fullness_group(class, zspage);
  1378. insert_zspage(class, zspage, fullness);
  1379. return fullness;
  1380. }
  1381. #ifdef CONFIG_COMPACTION
  1382. /*
  1383. * To prevent zspage destroy during migration, zspage freeing should
  1384. * hold locks of all pages in the zspage.
  1385. */
  1386. static void lock_zspage(struct zspage *zspage)
  1387. {
  1388. struct page *curr_page, *page;
  1389. /*
  1390. * Pages we haven't locked yet can be migrated off the list while we're
  1391. * trying to lock them, so we need to be careful and only attempt to
  1392. * lock each page under migrate_read_lock(). Otherwise, the page we lock
  1393. * may no longer belong to the zspage. This means that we may wait for
  1394. * the wrong page to unlock, so we must take a reference to the page
  1395. * prior to waiting for it to unlock outside migrate_read_lock().
  1396. */
  1397. while (1) {
  1398. migrate_read_lock(zspage);
  1399. page = get_first_page(zspage);
  1400. if (trylock_page(page))
  1401. break;
  1402. get_page(page);
  1403. migrate_read_unlock(zspage);
  1404. wait_on_page_locked(page);
  1405. put_page(page);
  1406. }
  1407. curr_page = page;
  1408. while ((page = get_next_page(curr_page))) {
  1409. if (trylock_page(page)) {
  1410. curr_page = page;
  1411. } else {
  1412. get_page(page);
  1413. migrate_read_unlock(zspage);
  1414. wait_on_page_locked(page);
  1415. put_page(page);
  1416. migrate_read_lock(zspage);
  1417. }
  1418. }
  1419. migrate_read_unlock(zspage);
  1420. }
  1421. #endif /* CONFIG_COMPACTION */
  1422. static void migrate_lock_init(struct zspage *zspage)
  1423. {
  1424. rwlock_init(&zspage->lock);
  1425. }
  1426. static void migrate_read_lock(struct zspage *zspage) __acquires(&zspage->lock)
  1427. {
  1428. read_lock(&zspage->lock);
  1429. }
  1430. static void migrate_read_unlock(struct zspage *zspage) __releases(&zspage->lock)
  1431. {
  1432. read_unlock(&zspage->lock);
  1433. }
  1434. static void migrate_write_lock(struct zspage *zspage)
  1435. {
  1436. write_lock(&zspage->lock);
  1437. }
  1438. static void migrate_write_unlock(struct zspage *zspage)
  1439. {
  1440. write_unlock(&zspage->lock);
  1441. }
  1442. #ifdef CONFIG_COMPACTION
  1443. static const struct movable_operations zsmalloc_mops;
  1444. static void replace_sub_page(struct size_class *class, struct zspage *zspage,
  1445. struct page *newpage, struct page *oldpage)
  1446. {
  1447. struct page *page;
  1448. struct page *pages[ZS_MAX_PAGES_PER_ZSPAGE] = {NULL, };
  1449. int idx = 0;
  1450. page = get_first_page(zspage);
  1451. do {
  1452. if (page == oldpage)
  1453. pages[idx] = newpage;
  1454. else
  1455. pages[idx] = page;
  1456. idx++;
  1457. } while ((page = get_next_page(page)) != NULL);
  1458. create_page_chain(class, zspage, pages);
  1459. set_first_obj_offset(newpage, get_first_obj_offset(oldpage));
  1460. if (unlikely(ZsHugePage(zspage)))
  1461. newpage->index = oldpage->index;
  1462. __SetPageMovable(newpage, &zsmalloc_mops);
  1463. }
  1464. static bool zs_page_isolate(struct page *page, isolate_mode_t mode)
  1465. {
  1466. /*
  1467. * Page is locked so zspage couldn't be destroyed. For detail, look at
  1468. * lock_zspage in free_zspage.
  1469. */
  1470. VM_BUG_ON_PAGE(PageIsolated(page), page);
  1471. return true;
  1472. }
  1473. static int zs_page_migrate(struct page *newpage, struct page *page,
  1474. enum migrate_mode mode)
  1475. {
  1476. struct zs_pool *pool;
  1477. struct size_class *class;
  1478. struct zspage *zspage;
  1479. struct page *dummy;
  1480. void *s_addr, *d_addr, *addr;
  1481. unsigned int offset;
  1482. unsigned long handle;
  1483. unsigned long old_obj, new_obj;
  1484. unsigned int obj_idx;
  1485. VM_BUG_ON_PAGE(!PageIsolated(page), page);
  1486. /* We're committed, tell the world that this is a Zsmalloc page. */
  1487. __SetPageZsmalloc(newpage);
  1488. /* The page is locked, so this pointer must remain valid */
  1489. zspage = get_zspage(page);
  1490. pool = zspage->pool;
  1491. /*
  1492. * The pool migrate_lock protects the race between zpage migration
  1493. * and zs_free.
  1494. */
  1495. write_lock(&pool->migrate_lock);
  1496. class = zspage_class(pool, zspage);
  1497. /*
  1498. * the class lock protects zpage alloc/free in the zspage.
  1499. */
  1500. spin_lock(&class->lock);
  1501. /* the migrate_write_lock protects zpage access via zs_map_object */
  1502. migrate_write_lock(zspage);
  1503. offset = get_first_obj_offset(page);
  1504. s_addr = kmap_atomic(page);
  1505. /*
  1506. * Here, any user cannot access all objects in the zspage so let's move.
  1507. */
  1508. d_addr = kmap_atomic(newpage);
  1509. copy_page(d_addr, s_addr);
  1510. kunmap_atomic(d_addr);
  1511. for (addr = s_addr + offset; addr < s_addr + PAGE_SIZE;
  1512. addr += class->size) {
  1513. if (obj_allocated(page, addr, &handle)) {
  1514. old_obj = handle_to_obj(handle);
  1515. obj_to_location(old_obj, &dummy, &obj_idx);
  1516. new_obj = (unsigned long)location_to_obj(newpage,
  1517. obj_idx);
  1518. record_obj(handle, new_obj);
  1519. }
  1520. }
  1521. kunmap_atomic(s_addr);
  1522. replace_sub_page(class, zspage, newpage, page);
  1523. /*
  1524. * Since we complete the data copy and set up new zspage structure,
  1525. * it's okay to release migration_lock.
  1526. */
  1527. write_unlock(&pool->migrate_lock);
  1528. spin_unlock(&class->lock);
  1529. migrate_write_unlock(zspage);
  1530. get_page(newpage);
  1531. if (page_zone(newpage) != page_zone(page)) {
  1532. dec_zone_page_state(page, NR_ZSPAGES);
  1533. inc_zone_page_state(newpage, NR_ZSPAGES);
  1534. }
  1535. reset_page(page);
  1536. put_page(page);
  1537. return MIGRATEPAGE_SUCCESS;
  1538. }
  1539. static void zs_page_putback(struct page *page)
  1540. {
  1541. VM_BUG_ON_PAGE(!PageIsolated(page), page);
  1542. }
  1543. static const struct movable_operations zsmalloc_mops = {
  1544. .isolate_page = zs_page_isolate,
  1545. .migrate_page = zs_page_migrate,
  1546. .putback_page = zs_page_putback,
  1547. };
  1548. /*
  1549. * Caller should hold page_lock of all pages in the zspage
  1550. * In here, we cannot use zspage meta data.
  1551. */
  1552. static void async_free_zspage(struct work_struct *work)
  1553. {
  1554. int i;
  1555. struct size_class *class;
  1556. struct zspage *zspage, *tmp;
  1557. LIST_HEAD(free_pages);
  1558. struct zs_pool *pool = container_of(work, struct zs_pool,
  1559. free_work);
  1560. for (i = 0; i < ZS_SIZE_CLASSES; i++) {
  1561. class = pool->size_class[i];
  1562. if (class->index != i)
  1563. continue;
  1564. spin_lock(&class->lock);
  1565. list_splice_init(&class->fullness_list[ZS_INUSE_RATIO_0],
  1566. &free_pages);
  1567. spin_unlock(&class->lock);
  1568. }
  1569. list_for_each_entry_safe(zspage, tmp, &free_pages, list) {
  1570. list_del(&zspage->list);
  1571. lock_zspage(zspage);
  1572. class = zspage_class(pool, zspage);
  1573. spin_lock(&class->lock);
  1574. class_stat_sub(class, ZS_INUSE_RATIO_0, 1);
  1575. __free_zspage(pool, class, zspage);
  1576. spin_unlock(&class->lock);
  1577. }
  1578. };
  1579. static void kick_deferred_free(struct zs_pool *pool)
  1580. {
  1581. schedule_work(&pool->free_work);
  1582. }
  1583. static void zs_flush_migration(struct zs_pool *pool)
  1584. {
  1585. flush_work(&pool->free_work);
  1586. }
  1587. static void init_deferred_free(struct zs_pool *pool)
  1588. {
  1589. INIT_WORK(&pool->free_work, async_free_zspage);
  1590. }
  1591. static void SetZsPageMovable(struct zs_pool *pool, struct zspage *zspage)
  1592. {
  1593. struct page *page = get_first_page(zspage);
  1594. do {
  1595. WARN_ON(!trylock_page(page));
  1596. __SetPageMovable(page, &zsmalloc_mops);
  1597. unlock_page(page);
  1598. } while ((page = get_next_page(page)) != NULL);
  1599. }
  1600. #else
  1601. static inline void zs_flush_migration(struct zs_pool *pool) { }
  1602. #endif
  1603. /*
  1604. *
  1605. * Based on the number of unused allocated objects calculate
  1606. * and return the number of pages that we can free.
  1607. */
  1608. static unsigned long zs_can_compact(struct size_class *class)
  1609. {
  1610. unsigned long obj_wasted;
  1611. unsigned long obj_allocated = class_stat_read(class, ZS_OBJS_ALLOCATED);
  1612. unsigned long obj_used = class_stat_read(class, ZS_OBJS_INUSE);
  1613. if (obj_allocated <= obj_used)
  1614. return 0;
  1615. obj_wasted = obj_allocated - obj_used;
  1616. obj_wasted /= class->objs_per_zspage;
  1617. return obj_wasted * class->pages_per_zspage;
  1618. }
  1619. static unsigned long __zs_compact(struct zs_pool *pool,
  1620. struct size_class *class)
  1621. {
  1622. struct zspage *src_zspage = NULL;
  1623. struct zspage *dst_zspage = NULL;
  1624. unsigned long pages_freed = 0;
  1625. /*
  1626. * protect the race between zpage migration and zs_free
  1627. * as well as zpage allocation/free
  1628. */
  1629. write_lock(&pool->migrate_lock);
  1630. spin_lock(&class->lock);
  1631. while (zs_can_compact(class)) {
  1632. int fg;
  1633. if (!dst_zspage) {
  1634. dst_zspage = isolate_dst_zspage(class);
  1635. if (!dst_zspage)
  1636. break;
  1637. }
  1638. src_zspage = isolate_src_zspage(class);
  1639. if (!src_zspage)
  1640. break;
  1641. migrate_write_lock(src_zspage);
  1642. migrate_zspage(pool, src_zspage, dst_zspage);
  1643. migrate_write_unlock(src_zspage);
  1644. fg = putback_zspage(class, src_zspage);
  1645. if (fg == ZS_INUSE_RATIO_0) {
  1646. free_zspage(pool, class, src_zspage);
  1647. pages_freed += class->pages_per_zspage;
  1648. }
  1649. src_zspage = NULL;
  1650. if (get_fullness_group(class, dst_zspage) == ZS_INUSE_RATIO_100
  1651. || rwlock_is_contended(&pool->migrate_lock)) {
  1652. putback_zspage(class, dst_zspage);
  1653. dst_zspage = NULL;
  1654. spin_unlock(&class->lock);
  1655. write_unlock(&pool->migrate_lock);
  1656. cond_resched();
  1657. write_lock(&pool->migrate_lock);
  1658. spin_lock(&class->lock);
  1659. }
  1660. }
  1661. if (src_zspage)
  1662. putback_zspage(class, src_zspage);
  1663. if (dst_zspage)
  1664. putback_zspage(class, dst_zspage);
  1665. spin_unlock(&class->lock);
  1666. write_unlock(&pool->migrate_lock);
  1667. return pages_freed;
  1668. }
  1669. unsigned long zs_compact(struct zs_pool *pool)
  1670. {
  1671. int i;
  1672. struct size_class *class;
  1673. unsigned long pages_freed = 0;
  1674. /*
  1675. * Pool compaction is performed under pool->migrate_lock so it is basically
  1676. * single-threaded. Having more than one thread in __zs_compact()
  1677. * will increase pool->migrate_lock contention, which will impact other
  1678. * zsmalloc operations that need pool->migrate_lock.
  1679. */
  1680. if (atomic_xchg(&pool->compaction_in_progress, 1))
  1681. return 0;
  1682. for (i = ZS_SIZE_CLASSES - 1; i >= 0; i--) {
  1683. class = pool->size_class[i];
  1684. if (class->index != i)
  1685. continue;
  1686. pages_freed += __zs_compact(pool, class);
  1687. }
  1688. atomic_long_add(pages_freed, &pool->stats.pages_compacted);
  1689. atomic_set(&pool->compaction_in_progress, 0);
  1690. return pages_freed;
  1691. }
  1692. EXPORT_SYMBOL_GPL(zs_compact);
  1693. void zs_pool_stats(struct zs_pool *pool, struct zs_pool_stats *stats)
  1694. {
  1695. memcpy(stats, &pool->stats, sizeof(struct zs_pool_stats));
  1696. }
  1697. EXPORT_SYMBOL_GPL(zs_pool_stats);
  1698. static unsigned long zs_shrinker_scan(struct shrinker *shrinker,
  1699. struct shrink_control *sc)
  1700. {
  1701. unsigned long pages_freed;
  1702. struct zs_pool *pool = shrinker->private_data;
  1703. /*
  1704. * Compact classes and calculate compaction delta.
  1705. * Can run concurrently with a manually triggered
  1706. * (by user) compaction.
  1707. */
  1708. pages_freed = zs_compact(pool);
  1709. return pages_freed ? pages_freed : SHRINK_STOP;
  1710. }
  1711. static unsigned long zs_shrinker_count(struct shrinker *shrinker,
  1712. struct shrink_control *sc)
  1713. {
  1714. int i;
  1715. struct size_class *class;
  1716. unsigned long pages_to_free = 0;
  1717. struct zs_pool *pool = shrinker->private_data;
  1718. for (i = ZS_SIZE_CLASSES - 1; i >= 0; i--) {
  1719. class = pool->size_class[i];
  1720. if (class->index != i)
  1721. continue;
  1722. pages_to_free += zs_can_compact(class);
  1723. }
  1724. return pages_to_free;
  1725. }
  1726. static void zs_unregister_shrinker(struct zs_pool *pool)
  1727. {
  1728. shrinker_free(pool->shrinker);
  1729. }
  1730. static int zs_register_shrinker(struct zs_pool *pool)
  1731. {
  1732. pool->shrinker = shrinker_alloc(0, "mm-zspool:%s", pool->name);
  1733. if (!pool->shrinker)
  1734. return -ENOMEM;
  1735. pool->shrinker->scan_objects = zs_shrinker_scan;
  1736. pool->shrinker->count_objects = zs_shrinker_count;
  1737. pool->shrinker->batch = 0;
  1738. pool->shrinker->private_data = pool;
  1739. shrinker_register(pool->shrinker);
  1740. return 0;
  1741. }
  1742. static int calculate_zspage_chain_size(int class_size)
  1743. {
  1744. int i, min_waste = INT_MAX;
  1745. int chain_size = 1;
  1746. if (is_power_of_2(class_size))
  1747. return chain_size;
  1748. for (i = 1; i <= ZS_MAX_PAGES_PER_ZSPAGE; i++) {
  1749. int waste;
  1750. waste = (i * PAGE_SIZE) % class_size;
  1751. if (waste < min_waste) {
  1752. min_waste = waste;
  1753. chain_size = i;
  1754. }
  1755. }
  1756. return chain_size;
  1757. }
  1758. /**
  1759. * zs_create_pool - Creates an allocation pool to work from.
  1760. * @name: pool name to be created
  1761. *
  1762. * This function must be called before anything when using
  1763. * the zsmalloc allocator.
  1764. *
  1765. * On success, a pointer to the newly created pool is returned,
  1766. * otherwise NULL.
  1767. */
  1768. struct zs_pool *zs_create_pool(const char *name)
  1769. {
  1770. int i;
  1771. struct zs_pool *pool;
  1772. struct size_class *prev_class = NULL;
  1773. pool = kzalloc(sizeof(*pool), GFP_KERNEL);
  1774. if (!pool)
  1775. return NULL;
  1776. init_deferred_free(pool);
  1777. rwlock_init(&pool->migrate_lock);
  1778. atomic_set(&pool->compaction_in_progress, 0);
  1779. pool->name = kstrdup(name, GFP_KERNEL);
  1780. if (!pool->name)
  1781. goto err;
  1782. if (create_cache(pool))
  1783. goto err;
  1784. /*
  1785. * Iterate reversely, because, size of size_class that we want to use
  1786. * for merging should be larger or equal to current size.
  1787. */
  1788. for (i = ZS_SIZE_CLASSES - 1; i >= 0; i--) {
  1789. int size;
  1790. int pages_per_zspage;
  1791. int objs_per_zspage;
  1792. struct size_class *class;
  1793. int fullness;
  1794. size = ZS_MIN_ALLOC_SIZE + i * ZS_SIZE_CLASS_DELTA;
  1795. if (size > ZS_MAX_ALLOC_SIZE)
  1796. size = ZS_MAX_ALLOC_SIZE;
  1797. pages_per_zspage = calculate_zspage_chain_size(size);
  1798. objs_per_zspage = pages_per_zspage * PAGE_SIZE / size;
  1799. /*
  1800. * We iterate from biggest down to smallest classes,
  1801. * so huge_class_size holds the size of the first huge
  1802. * class. Any object bigger than or equal to that will
  1803. * endup in the huge class.
  1804. */
  1805. if (pages_per_zspage != 1 && objs_per_zspage != 1 &&
  1806. !huge_class_size) {
  1807. huge_class_size = size;
  1808. /*
  1809. * The object uses ZS_HANDLE_SIZE bytes to store the
  1810. * handle. We need to subtract it, because zs_malloc()
  1811. * unconditionally adds handle size before it performs
  1812. * size class search - so object may be smaller than
  1813. * huge class size, yet it still can end up in the huge
  1814. * class because it grows by ZS_HANDLE_SIZE extra bytes
  1815. * right before class lookup.
  1816. */
  1817. huge_class_size -= (ZS_HANDLE_SIZE - 1);
  1818. }
  1819. /*
  1820. * size_class is used for normal zsmalloc operation such
  1821. * as alloc/free for that size. Although it is natural that we
  1822. * have one size_class for each size, there is a chance that we
  1823. * can get more memory utilization if we use one size_class for
  1824. * many different sizes whose size_class have same
  1825. * characteristics. So, we makes size_class point to
  1826. * previous size_class if possible.
  1827. */
  1828. if (prev_class) {
  1829. if (can_merge(prev_class, pages_per_zspage, objs_per_zspage)) {
  1830. pool->size_class[i] = prev_class;
  1831. continue;
  1832. }
  1833. }
  1834. class = kzalloc(sizeof(struct size_class), GFP_KERNEL);
  1835. if (!class)
  1836. goto err;
  1837. class->size = size;
  1838. class->index = i;
  1839. class->pages_per_zspage = pages_per_zspage;
  1840. class->objs_per_zspage = objs_per_zspage;
  1841. spin_lock_init(&class->lock);
  1842. pool->size_class[i] = class;
  1843. fullness = ZS_INUSE_RATIO_0;
  1844. while (fullness < NR_FULLNESS_GROUPS) {
  1845. INIT_LIST_HEAD(&class->fullness_list[fullness]);
  1846. fullness++;
  1847. }
  1848. prev_class = class;
  1849. }
  1850. /* debug only, don't abort if it fails */
  1851. zs_pool_stat_create(pool, name);
  1852. /*
  1853. * Not critical since shrinker is only used to trigger internal
  1854. * defragmentation of the pool which is pretty optional thing. If
  1855. * registration fails we still can use the pool normally and user can
  1856. * trigger compaction manually. Thus, ignore return code.
  1857. */
  1858. zs_register_shrinker(pool);
  1859. return pool;
  1860. err:
  1861. zs_destroy_pool(pool);
  1862. return NULL;
  1863. }
  1864. EXPORT_SYMBOL_GPL(zs_create_pool);
  1865. void zs_destroy_pool(struct zs_pool *pool)
  1866. {
  1867. int i;
  1868. zs_unregister_shrinker(pool);
  1869. zs_flush_migration(pool);
  1870. zs_pool_stat_destroy(pool);
  1871. for (i = 0; i < ZS_SIZE_CLASSES; i++) {
  1872. int fg;
  1873. struct size_class *class = pool->size_class[i];
  1874. if (!class)
  1875. continue;
  1876. if (class->index != i)
  1877. continue;
  1878. for (fg = ZS_INUSE_RATIO_0; fg < NR_FULLNESS_GROUPS; fg++) {
  1879. if (list_empty(&class->fullness_list[fg]))
  1880. continue;
  1881. pr_err("Class-%d fullness group %d is not empty\n",
  1882. class->size, fg);
  1883. }
  1884. kfree(class);
  1885. }
  1886. destroy_cache(pool);
  1887. kfree(pool->name);
  1888. kfree(pool);
  1889. }
  1890. EXPORT_SYMBOL_GPL(zs_destroy_pool);
  1891. static int __init zs_init(void)
  1892. {
  1893. int ret;
  1894. ret = cpuhp_setup_state(CPUHP_MM_ZS_PREPARE, "mm/zsmalloc:prepare",
  1895. zs_cpu_prepare, zs_cpu_dead);
  1896. if (ret)
  1897. goto out;
  1898. #ifdef CONFIG_ZPOOL
  1899. zpool_register_driver(&zs_zpool_driver);
  1900. #endif
  1901. zs_stat_init();
  1902. return 0;
  1903. out:
  1904. return ret;
  1905. }
  1906. static void __exit zs_exit(void)
  1907. {
  1908. #ifdef CONFIG_ZPOOL
  1909. zpool_unregister_driver(&zs_zpool_driver);
  1910. #endif
  1911. cpuhp_remove_state(CPUHP_MM_ZS_PREPARE);
  1912. zs_stat_exit();
  1913. }
  1914. module_init(zs_init);
  1915. module_exit(zs_exit);
  1916. MODULE_LICENSE("Dual BSD/GPL");
  1917. MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");
  1918. MODULE_DESCRIPTION("zsmalloc memory allocator");