zsmalloc.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035
  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->first_page: points to the first component (0-order) page
  19. * page->index (union with page->freelist): offset of the first object
  20. * starting in this page. For the first page, this is
  21. * always 0, so we use this field (aka freelist) to point
  22. * to the first free object in zspage.
  23. * page->lru: links together all component pages (except the first page)
  24. * of a zspage
  25. *
  26. * For _first_ page only:
  27. *
  28. * page->private (union with page->first_page): refers to the
  29. * component page after the first page
  30. * If the page is first_page for huge object, it stores handle.
  31. * Look at size_class->huge.
  32. * page->freelist: points to the first free object in zspage.
  33. * Free objects are linked together using in-place
  34. * metadata.
  35. * page->objects: maximum number of objects we can store in this
  36. * zspage (class->zspage_order * PAGE_SIZE / class->size)
  37. * page->lru: links together first pages of various zspages.
  38. * Basically forming list of zspages in a fullness group.
  39. * page->mapping: class index and fullness group of the zspage
  40. *
  41. * Usage of struct page flags:
  42. * PG_private: identifies the first component page
  43. * PG_private2: identifies the last component page
  44. *
  45. */
  46. #include <linux/module.h>
  47. #include <linux/kernel.h>
  48. #include <linux/sched.h>
  49. #include <linux/bitops.h>
  50. #include <linux/errno.h>
  51. #include <linux/highmem.h>
  52. #include <linux/string.h>
  53. #include <linux/slab.h>
  54. #include <asm/tlbflush.h>
  55. #include <asm/pgtable.h>
  56. #include <linux/cpumask.h>
  57. #include <linux/cpu.h>
  58. #include <linux/vmalloc.h>
  59. #include <linux/hardirq.h>
  60. #include <linux/spinlock.h>
  61. #include <linux/types.h>
  62. #include <linux/debugfs.h>
  63. #include <linux/zsmalloc.h>
  64. #include <linux/zpool.h>
  65. /*
  66. * This must be power of 2 and greater than of 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. /*
  73. * A single 'zspage' is composed of up to 2^N discontiguous 0-order (single)
  74. * pages. ZS_MAX_ZSPAGE_ORDER defines upper limit on N.
  75. */
  76. #define ZS_MAX_ZSPAGE_ORDER 2
  77. #define ZS_MAX_PAGES_PER_ZSPAGE (_AC(1, UL) << ZS_MAX_ZSPAGE_ORDER)
  78. #define ZS_HANDLE_SIZE (sizeof(unsigned long))
  79. /*
  80. * Object location (<PFN>, <obj_idx>) is encoded as
  81. * as single (unsigned long) handle value.
  82. *
  83. * Note that object index <obj_idx> is relative to system
  84. * page <PFN> it is stored in, so for each sub-page belonging
  85. * to a zspage, obj_idx starts with 0.
  86. *
  87. * This is made more complicated by various memory models and PAE.
  88. */
  89. #ifndef MAX_PHYSMEM_BITS
  90. #ifdef CONFIG_HIGHMEM64G
  91. #define MAX_PHYSMEM_BITS 36
  92. #else /* !CONFIG_HIGHMEM64G */
  93. /*
  94. * If this definition of MAX_PHYSMEM_BITS is used, OBJ_INDEX_BITS will just
  95. * be PAGE_SHIFT
  96. */
  97. #define MAX_PHYSMEM_BITS BITS_PER_LONG
  98. #endif
  99. #endif
  100. #define _PFN_BITS (MAX_PHYSMEM_BITS - PAGE_SHIFT)
  101. /*
  102. * Memory for allocating for handle keeps object position by
  103. * encoding <page, obj_idx> and the encoded value has a room
  104. * in least bit(ie, look at obj_to_location).
  105. * We use the bit to synchronize between object access by
  106. * user and migration.
  107. */
  108. #define HANDLE_PIN_BIT 0
  109. /*
  110. * Head in allocated object should have OBJ_ALLOCATED_TAG
  111. * to identify the object was allocated or not.
  112. * It's okay to add the status bit in the least bit because
  113. * header keeps handle which is 4byte-aligned address so we
  114. * have room for two bit at least.
  115. */
  116. #define OBJ_ALLOCATED_TAG 1
  117. #define OBJ_TAG_BITS 1
  118. #define OBJ_INDEX_BITS (BITS_PER_LONG - _PFN_BITS - OBJ_TAG_BITS)
  119. #define OBJ_INDEX_MASK ((_AC(1, UL) << OBJ_INDEX_BITS) - 1)
  120. #define MAX(a, b) ((a) >= (b) ? (a) : (b))
  121. /* ZS_MIN_ALLOC_SIZE must be multiple of ZS_ALIGN */
  122. #define ZS_MIN_ALLOC_SIZE \
  123. MAX(32, (ZS_MAX_PAGES_PER_ZSPAGE << PAGE_SHIFT >> OBJ_INDEX_BITS))
  124. /* each chunk includes extra space to keep handle */
  125. #define ZS_MAX_ALLOC_SIZE PAGE_SIZE
  126. /*
  127. * On systems with 4K page size, this gives 255 size classes! There is a
  128. * trader-off here:
  129. * - Large number of size classes is potentially wasteful as free page are
  130. * spread across these classes
  131. * - Small number of size classes causes large internal fragmentation
  132. * - Probably its better to use specific size classes (empirically
  133. * determined). NOTE: all those class sizes must be set as multiple of
  134. * ZS_ALIGN to make sure link_free itself never has to span 2 pages.
  135. *
  136. * ZS_MIN_ALLOC_SIZE and ZS_SIZE_CLASS_DELTA must be multiple of ZS_ALIGN
  137. * (reason above)
  138. */
  139. #define ZS_SIZE_CLASS_DELTA (PAGE_SIZE >> 8)
  140. /*
  141. * We do not maintain any list for completely empty or full pages
  142. */
  143. enum fullness_group {
  144. ZS_ALMOST_FULL,
  145. ZS_ALMOST_EMPTY,
  146. _ZS_NR_FULLNESS_GROUPS,
  147. ZS_EMPTY,
  148. ZS_FULL
  149. };
  150. enum zs_stat_type {
  151. OBJ_ALLOCATED,
  152. OBJ_USED,
  153. CLASS_ALMOST_FULL,
  154. CLASS_ALMOST_EMPTY,
  155. NR_ZS_STAT_TYPE,
  156. };
  157. struct zs_size_stat {
  158. unsigned long objs[NR_ZS_STAT_TYPE];
  159. };
  160. #ifdef CONFIG_ZSMALLOC_STAT
  161. static struct dentry *zs_stat_root;
  162. #endif
  163. /*
  164. * number of size_classes
  165. */
  166. static int zs_size_classes;
  167. /*
  168. * We assign a page to ZS_ALMOST_EMPTY fullness group when:
  169. * n <= N / f, where
  170. * n = number of allocated objects
  171. * N = total number of objects zspage can store
  172. * f = fullness_threshold_frac
  173. *
  174. * Similarly, we assign zspage to:
  175. * ZS_ALMOST_FULL when n > N / f
  176. * ZS_EMPTY when n == 0
  177. * ZS_FULL when n == N
  178. *
  179. * (see: fix_fullness_group())
  180. */
  181. static const int fullness_threshold_frac = 4;
  182. struct size_class {
  183. spinlock_t lock;
  184. struct page *fullness_list[_ZS_NR_FULLNESS_GROUPS];
  185. /*
  186. * Size of objects stored in this class. Must be multiple
  187. * of ZS_ALIGN.
  188. */
  189. int size;
  190. unsigned int index;
  191. /* Number of PAGE_SIZE sized pages to combine to form a 'zspage' */
  192. int pages_per_zspage;
  193. struct zs_size_stat stats;
  194. /* huge object: pages_per_zspage == 1 && maxobj_per_zspage == 1 */
  195. bool huge;
  196. };
  197. /*
  198. * Placed within free objects to form a singly linked list.
  199. * For every zspage, first_page->freelist gives head of this list.
  200. *
  201. * This must be power of 2 and less than or equal to ZS_ALIGN
  202. */
  203. struct link_free {
  204. union {
  205. /*
  206. * Position of next free chunk (encodes <PFN, obj_idx>)
  207. * It's valid for non-allocated object
  208. */
  209. void *next;
  210. /*
  211. * Handle of allocated object.
  212. */
  213. unsigned long handle;
  214. };
  215. };
  216. struct zs_pool {
  217. char *name;
  218. struct size_class **size_class;
  219. struct kmem_cache *handle_cachep;
  220. gfp_t flags; /* allocation flags used when growing pool */
  221. atomic_long_t pages_allocated;
  222. struct zs_pool_stats stats;
  223. /* Compact classes */
  224. struct shrinker shrinker;
  225. /*
  226. * To signify that register_shrinker() was successful
  227. * and unregister_shrinker() will not Oops.
  228. */
  229. bool shrinker_enabled;
  230. #ifdef CONFIG_ZSMALLOC_STAT
  231. struct dentry *stat_dentry;
  232. #endif
  233. };
  234. /*
  235. * A zspage's class index and fullness group
  236. * are encoded in its (first)page->mapping
  237. */
  238. #define CLASS_IDX_BITS 28
  239. #define FULLNESS_BITS 4
  240. #define CLASS_IDX_MASK ((1 << CLASS_IDX_BITS) - 1)
  241. #define FULLNESS_MASK ((1 << FULLNESS_BITS) - 1)
  242. struct mapping_area {
  243. #ifdef CONFIG_PGTABLE_MAPPING
  244. struct vm_struct *vm; /* vm area for mapping object that span pages */
  245. #else
  246. char *vm_buf; /* copy buffer for objects that span pages */
  247. #endif
  248. char *vm_addr; /* address of kmap_atomic()'ed pages */
  249. enum zs_mapmode vm_mm; /* mapping mode */
  250. bool huge;
  251. };
  252. static int create_handle_cache(struct zs_pool *pool)
  253. {
  254. pool->handle_cachep = kmem_cache_create("zs_handle", ZS_HANDLE_SIZE,
  255. 0, 0, NULL);
  256. return pool->handle_cachep ? 0 : 1;
  257. }
  258. static void destroy_handle_cache(struct zs_pool *pool)
  259. {
  260. kmem_cache_destroy(pool->handle_cachep);
  261. }
  262. static unsigned long alloc_handle(struct zs_pool *pool)
  263. {
  264. return (unsigned long)kmem_cache_alloc(pool->handle_cachep,
  265. pool->flags & ~__GFP_HIGHMEM);
  266. }
  267. static void free_handle(struct zs_pool *pool, unsigned long handle)
  268. {
  269. kmem_cache_free(pool->handle_cachep, (void *)handle);
  270. }
  271. static void record_obj(unsigned long handle, unsigned long obj)
  272. {
  273. *(unsigned long *)handle = obj;
  274. }
  275. /* zpool driver */
  276. #ifdef CONFIG_ZPOOL
  277. static void *zs_zpool_create(char *name, gfp_t gfp,
  278. const struct zpool_ops *zpool_ops,
  279. struct zpool *zpool)
  280. {
  281. return zs_create_pool(name, gfp);
  282. }
  283. static void zs_zpool_destroy(void *pool)
  284. {
  285. zs_destroy_pool(pool);
  286. }
  287. static int zs_zpool_malloc(void *pool, size_t size, gfp_t gfp,
  288. unsigned long *handle)
  289. {
  290. *handle = zs_malloc(pool, size);
  291. return *handle ? 0 : -1;
  292. }
  293. static void zs_zpool_free(void *pool, unsigned long handle)
  294. {
  295. zs_free(pool, handle);
  296. }
  297. static int zs_zpool_shrink(void *pool, unsigned int pages,
  298. unsigned int *reclaimed)
  299. {
  300. return -EINVAL;
  301. }
  302. static void *zs_zpool_map(void *pool, unsigned long handle,
  303. enum zpool_mapmode mm)
  304. {
  305. enum zs_mapmode zs_mm;
  306. switch (mm) {
  307. case ZPOOL_MM_RO:
  308. zs_mm = ZS_MM_RO;
  309. break;
  310. case ZPOOL_MM_WO:
  311. zs_mm = ZS_MM_WO;
  312. break;
  313. case ZPOOL_MM_RW: /* fallthru */
  314. default:
  315. zs_mm = ZS_MM_RW;
  316. break;
  317. }
  318. return zs_map_object(pool, handle, zs_mm);
  319. }
  320. static void zs_zpool_unmap(void *pool, unsigned long handle)
  321. {
  322. zs_unmap_object(pool, handle);
  323. }
  324. static u64 zs_zpool_total_size(void *pool)
  325. {
  326. return zs_get_total_pages(pool) << PAGE_SHIFT;
  327. }
  328. static struct zpool_driver zs_zpool_driver = {
  329. .type = "zsmalloc",
  330. .owner = THIS_MODULE,
  331. .create = zs_zpool_create,
  332. .destroy = zs_zpool_destroy,
  333. .malloc = zs_zpool_malloc,
  334. .free = zs_zpool_free,
  335. .shrink = zs_zpool_shrink,
  336. .map = zs_zpool_map,
  337. .unmap = zs_zpool_unmap,
  338. .total_size = zs_zpool_total_size,
  339. };
  340. MODULE_ALIAS("zpool-zsmalloc");
  341. #endif /* CONFIG_ZPOOL */
  342. static unsigned int get_maxobj_per_zspage(int size, int pages_per_zspage)
  343. {
  344. return pages_per_zspage * PAGE_SIZE / size;
  345. }
  346. /* per-cpu VM mapping areas for zspage accesses that cross page boundaries */
  347. static DEFINE_PER_CPU(struct mapping_area, zs_map_area);
  348. static int is_first_page(struct page *page)
  349. {
  350. return PagePrivate(page);
  351. }
  352. static int is_last_page(struct page *page)
  353. {
  354. return PagePrivate2(page);
  355. }
  356. static void get_zspage_mapping(struct page *page, unsigned int *class_idx,
  357. enum fullness_group *fullness)
  358. {
  359. unsigned long m;
  360. BUG_ON(!is_first_page(page));
  361. m = (unsigned long)page->mapping;
  362. *fullness = m & FULLNESS_MASK;
  363. *class_idx = (m >> FULLNESS_BITS) & CLASS_IDX_MASK;
  364. }
  365. static void set_zspage_mapping(struct page *page, unsigned int class_idx,
  366. enum fullness_group fullness)
  367. {
  368. unsigned long m;
  369. BUG_ON(!is_first_page(page));
  370. m = ((class_idx & CLASS_IDX_MASK) << FULLNESS_BITS) |
  371. (fullness & FULLNESS_MASK);
  372. page->mapping = (struct address_space *)m;
  373. }
  374. /*
  375. * zsmalloc divides the pool into various size classes where each
  376. * class maintains a list of zspages where each zspage is divided
  377. * into equal sized chunks. Each allocation falls into one of these
  378. * classes depending on its size. This function returns index of the
  379. * size class which has chunk size big enough to hold the give size.
  380. */
  381. static int get_size_class_index(int size)
  382. {
  383. int idx = 0;
  384. if (likely(size > ZS_MIN_ALLOC_SIZE))
  385. idx = DIV_ROUND_UP(size - ZS_MIN_ALLOC_SIZE,
  386. ZS_SIZE_CLASS_DELTA);
  387. return min(zs_size_classes - 1, idx);
  388. }
  389. static inline void zs_stat_inc(struct size_class *class,
  390. enum zs_stat_type type, unsigned long cnt)
  391. {
  392. class->stats.objs[type] += cnt;
  393. }
  394. static inline void zs_stat_dec(struct size_class *class,
  395. enum zs_stat_type type, unsigned long cnt)
  396. {
  397. class->stats.objs[type] -= cnt;
  398. }
  399. static inline unsigned long zs_stat_get(struct size_class *class,
  400. enum zs_stat_type type)
  401. {
  402. return class->stats.objs[type];
  403. }
  404. #ifdef CONFIG_ZSMALLOC_STAT
  405. static int __init zs_stat_init(void)
  406. {
  407. if (!debugfs_initialized())
  408. return -ENODEV;
  409. zs_stat_root = debugfs_create_dir("zsmalloc", NULL);
  410. if (!zs_stat_root)
  411. return -ENOMEM;
  412. return 0;
  413. }
  414. static void __exit zs_stat_exit(void)
  415. {
  416. debugfs_remove_recursive(zs_stat_root);
  417. }
  418. static int zs_stats_size_show(struct seq_file *s, void *v)
  419. {
  420. int i;
  421. struct zs_pool *pool = s->private;
  422. struct size_class *class;
  423. int objs_per_zspage;
  424. unsigned long class_almost_full, class_almost_empty;
  425. unsigned long obj_allocated, obj_used, pages_used;
  426. unsigned long total_class_almost_full = 0, total_class_almost_empty = 0;
  427. unsigned long total_objs = 0, total_used_objs = 0, total_pages = 0;
  428. seq_printf(s, " %5s %5s %11s %12s %13s %10s %10s %16s\n",
  429. "class", "size", "almost_full", "almost_empty",
  430. "obj_allocated", "obj_used", "pages_used",
  431. "pages_per_zspage");
  432. for (i = 0; i < zs_size_classes; i++) {
  433. class = pool->size_class[i];
  434. if (class->index != i)
  435. continue;
  436. spin_lock(&class->lock);
  437. class_almost_full = zs_stat_get(class, CLASS_ALMOST_FULL);
  438. class_almost_empty = zs_stat_get(class, CLASS_ALMOST_EMPTY);
  439. obj_allocated = zs_stat_get(class, OBJ_ALLOCATED);
  440. obj_used = zs_stat_get(class, OBJ_USED);
  441. spin_unlock(&class->lock);
  442. objs_per_zspage = get_maxobj_per_zspage(class->size,
  443. class->pages_per_zspage);
  444. pages_used = obj_allocated / objs_per_zspage *
  445. class->pages_per_zspage;
  446. seq_printf(s, " %5u %5u %11lu %12lu %13lu %10lu %10lu %16d\n",
  447. i, class->size, class_almost_full, class_almost_empty,
  448. obj_allocated, obj_used, pages_used,
  449. class->pages_per_zspage);
  450. total_class_almost_full += class_almost_full;
  451. total_class_almost_empty += class_almost_empty;
  452. total_objs += obj_allocated;
  453. total_used_objs += obj_used;
  454. total_pages += pages_used;
  455. }
  456. seq_puts(s, "\n");
  457. seq_printf(s, " %5s %5s %11lu %12lu %13lu %10lu %10lu\n",
  458. "Total", "", total_class_almost_full,
  459. total_class_almost_empty, total_objs,
  460. total_used_objs, total_pages);
  461. return 0;
  462. }
  463. static int zs_stats_size_open(struct inode *inode, struct file *file)
  464. {
  465. return single_open(file, zs_stats_size_show, inode->i_private);
  466. }
  467. static const struct file_operations zs_stat_size_ops = {
  468. .open = zs_stats_size_open,
  469. .read = seq_read,
  470. .llseek = seq_lseek,
  471. .release = single_release,
  472. };
  473. static int zs_pool_stat_create(char *name, struct zs_pool *pool)
  474. {
  475. struct dentry *entry;
  476. if (!zs_stat_root)
  477. return -ENODEV;
  478. entry = debugfs_create_dir(name, zs_stat_root);
  479. if (!entry) {
  480. pr_warn("debugfs dir <%s> creation failed\n", name);
  481. return -ENOMEM;
  482. }
  483. pool->stat_dentry = entry;
  484. entry = debugfs_create_file("classes", S_IFREG | S_IRUGO,
  485. pool->stat_dentry, pool, &zs_stat_size_ops);
  486. if (!entry) {
  487. pr_warn("%s: debugfs file entry <%s> creation failed\n",
  488. name, "classes");
  489. return -ENOMEM;
  490. }
  491. return 0;
  492. }
  493. static void zs_pool_stat_destroy(struct zs_pool *pool)
  494. {
  495. debugfs_remove_recursive(pool->stat_dentry);
  496. }
  497. #else /* CONFIG_ZSMALLOC_STAT */
  498. static int __init zs_stat_init(void)
  499. {
  500. return 0;
  501. }
  502. static void __exit zs_stat_exit(void)
  503. {
  504. }
  505. static inline int zs_pool_stat_create(char *name, struct zs_pool *pool)
  506. {
  507. return 0;
  508. }
  509. static inline void zs_pool_stat_destroy(struct zs_pool *pool)
  510. {
  511. }
  512. #endif
  513. /*
  514. * For each size class, zspages are divided into different groups
  515. * depending on how "full" they are. This was done so that we could
  516. * easily find empty or nearly empty zspages when we try to shrink
  517. * the pool (not yet implemented). This function returns fullness
  518. * status of the given page.
  519. */
  520. static enum fullness_group get_fullness_group(struct page *page)
  521. {
  522. int inuse, max_objects;
  523. enum fullness_group fg;
  524. BUG_ON(!is_first_page(page));
  525. inuse = page->inuse;
  526. max_objects = page->objects;
  527. if (inuse == 0)
  528. fg = ZS_EMPTY;
  529. else if (inuse == max_objects)
  530. fg = ZS_FULL;
  531. else if (inuse <= 3 * max_objects / fullness_threshold_frac)
  532. fg = ZS_ALMOST_EMPTY;
  533. else
  534. fg = ZS_ALMOST_FULL;
  535. return fg;
  536. }
  537. /*
  538. * Each size class maintains various freelists and zspages are assigned
  539. * to one of these freelists based on the number of live objects they
  540. * have. This functions inserts the given zspage into the freelist
  541. * identified by <class, fullness_group>.
  542. */
  543. static void insert_zspage(struct page *page, struct size_class *class,
  544. enum fullness_group fullness)
  545. {
  546. struct page **head;
  547. BUG_ON(!is_first_page(page));
  548. if (fullness >= _ZS_NR_FULLNESS_GROUPS)
  549. return;
  550. zs_stat_inc(class, fullness == ZS_ALMOST_EMPTY ?
  551. CLASS_ALMOST_EMPTY : CLASS_ALMOST_FULL, 1);
  552. head = &class->fullness_list[fullness];
  553. if (!*head) {
  554. *head = page;
  555. return;
  556. }
  557. /*
  558. * We want to see more ZS_FULL pages and less almost
  559. * empty/full. Put pages with higher ->inuse first.
  560. */
  561. list_add_tail(&page->lru, &(*head)->lru);
  562. if (page->inuse >= (*head)->inuse)
  563. *head = page;
  564. }
  565. /*
  566. * This function removes the given zspage from the freelist identified
  567. * by <class, fullness_group>.
  568. */
  569. static void remove_zspage(struct page *page, struct size_class *class,
  570. enum fullness_group fullness)
  571. {
  572. struct page **head;
  573. BUG_ON(!is_first_page(page));
  574. if (fullness >= _ZS_NR_FULLNESS_GROUPS)
  575. return;
  576. head = &class->fullness_list[fullness];
  577. BUG_ON(!*head);
  578. if (list_empty(&(*head)->lru))
  579. *head = NULL;
  580. else if (*head == page)
  581. *head = (struct page *)list_entry((*head)->lru.next,
  582. struct page, lru);
  583. list_del_init(&page->lru);
  584. zs_stat_dec(class, fullness == ZS_ALMOST_EMPTY ?
  585. CLASS_ALMOST_EMPTY : CLASS_ALMOST_FULL, 1);
  586. }
  587. /*
  588. * Each size class maintains zspages in different fullness groups depending
  589. * on the number of live objects they contain. When allocating or freeing
  590. * objects, the fullness status of the page can change, say, from ALMOST_FULL
  591. * to ALMOST_EMPTY when freeing an object. This function checks if such
  592. * a status change has occurred for the given page and accordingly moves the
  593. * page from the freelist of the old fullness group to that of the new
  594. * fullness group.
  595. */
  596. static enum fullness_group fix_fullness_group(struct size_class *class,
  597. struct page *page)
  598. {
  599. int class_idx;
  600. enum fullness_group currfg, newfg;
  601. BUG_ON(!is_first_page(page));
  602. get_zspage_mapping(page, &class_idx, &currfg);
  603. newfg = get_fullness_group(page);
  604. if (newfg == currfg)
  605. goto out;
  606. remove_zspage(page, class, currfg);
  607. insert_zspage(page, class, newfg);
  608. set_zspage_mapping(page, class_idx, newfg);
  609. out:
  610. return newfg;
  611. }
  612. /*
  613. * We have to decide on how many pages to link together
  614. * to form a zspage for each size class. This is important
  615. * to reduce wastage due to unusable space left at end of
  616. * each zspage which is given as:
  617. * wastage = Zp % class_size
  618. * usage = Zp - wastage
  619. * where Zp = zspage size = k * PAGE_SIZE where k = 1, 2, ...
  620. *
  621. * For example, for size class of 3/8 * PAGE_SIZE, we should
  622. * link together 3 PAGE_SIZE sized pages to form a zspage
  623. * since then we can perfectly fit in 8 such objects.
  624. */
  625. static int get_pages_per_zspage(int class_size)
  626. {
  627. int i, max_usedpc = 0;
  628. /* zspage order which gives maximum used size per KB */
  629. int max_usedpc_order = 1;
  630. for (i = 1; i <= ZS_MAX_PAGES_PER_ZSPAGE; i++) {
  631. int zspage_size;
  632. int waste, usedpc;
  633. zspage_size = i * PAGE_SIZE;
  634. waste = zspage_size % class_size;
  635. usedpc = (zspage_size - waste) * 100 / zspage_size;
  636. if (usedpc > max_usedpc) {
  637. max_usedpc = usedpc;
  638. max_usedpc_order = i;
  639. }
  640. }
  641. return max_usedpc_order;
  642. }
  643. /*
  644. * A single 'zspage' is composed of many system pages which are
  645. * linked together using fields in struct page. This function finds
  646. * the first/head page, given any component page of a zspage.
  647. */
  648. static struct page *get_first_page(struct page *page)
  649. {
  650. if (is_first_page(page))
  651. return page;
  652. else
  653. return page->first_page;
  654. }
  655. static struct page *get_next_page(struct page *page)
  656. {
  657. struct page *next;
  658. if (is_last_page(page))
  659. next = NULL;
  660. else if (is_first_page(page))
  661. next = (struct page *)page_private(page);
  662. else
  663. next = list_entry(page->lru.next, struct page, lru);
  664. return next;
  665. }
  666. /*
  667. * Encode <page, obj_idx> as a single handle value.
  668. * We use the least bit of handle for tagging.
  669. */
  670. static void *location_to_obj(struct page *page, unsigned long obj_idx)
  671. {
  672. unsigned long obj;
  673. if (!page) {
  674. BUG_ON(obj_idx);
  675. return NULL;
  676. }
  677. obj = page_to_pfn(page) << OBJ_INDEX_BITS;
  678. obj |= ((obj_idx) & OBJ_INDEX_MASK);
  679. obj <<= OBJ_TAG_BITS;
  680. return (void *)obj;
  681. }
  682. /*
  683. * Decode <page, obj_idx> pair from the given object handle. We adjust the
  684. * decoded obj_idx back to its original value since it was adjusted in
  685. * location_to_obj().
  686. */
  687. static void obj_to_location(unsigned long obj, struct page **page,
  688. unsigned long *obj_idx)
  689. {
  690. obj >>= OBJ_TAG_BITS;
  691. *page = pfn_to_page(obj >> OBJ_INDEX_BITS);
  692. *obj_idx = (obj & OBJ_INDEX_MASK);
  693. }
  694. static unsigned long handle_to_obj(unsigned long handle)
  695. {
  696. return *(unsigned long *)handle;
  697. }
  698. static unsigned long obj_to_head(struct size_class *class, struct page *page,
  699. void *obj)
  700. {
  701. if (class->huge) {
  702. VM_BUG_ON(!is_first_page(page));
  703. return *(unsigned long *)page_private(page);
  704. } else
  705. return *(unsigned long *)obj;
  706. }
  707. static unsigned long obj_idx_to_offset(struct page *page,
  708. unsigned long obj_idx, int class_size)
  709. {
  710. unsigned long off = 0;
  711. if (!is_first_page(page))
  712. off = page->index;
  713. return off + obj_idx * class_size;
  714. }
  715. static inline int trypin_tag(unsigned long handle)
  716. {
  717. unsigned long *ptr = (unsigned long *)handle;
  718. return !test_and_set_bit_lock(HANDLE_PIN_BIT, ptr);
  719. }
  720. static void pin_tag(unsigned long handle)
  721. {
  722. while (!trypin_tag(handle));
  723. }
  724. static void unpin_tag(unsigned long handle)
  725. {
  726. unsigned long *ptr = (unsigned long *)handle;
  727. clear_bit_unlock(HANDLE_PIN_BIT, ptr);
  728. }
  729. static void reset_page(struct page *page)
  730. {
  731. clear_bit(PG_private, &page->flags);
  732. clear_bit(PG_private_2, &page->flags);
  733. set_page_private(page, 0);
  734. page->mapping = NULL;
  735. page->freelist = NULL;
  736. page_mapcount_reset(page);
  737. }
  738. static void free_zspage(struct page *first_page)
  739. {
  740. struct page *nextp, *tmp, *head_extra;
  741. BUG_ON(!is_first_page(first_page));
  742. BUG_ON(first_page->inuse);
  743. head_extra = (struct page *)page_private(first_page);
  744. reset_page(first_page);
  745. __free_page(first_page);
  746. /* zspage with only 1 system page */
  747. if (!head_extra)
  748. return;
  749. list_for_each_entry_safe(nextp, tmp, &head_extra->lru, lru) {
  750. list_del(&nextp->lru);
  751. reset_page(nextp);
  752. __free_page(nextp);
  753. }
  754. reset_page(head_extra);
  755. __free_page(head_extra);
  756. }
  757. /* Initialize a newly allocated zspage */
  758. static void init_zspage(struct page *first_page, struct size_class *class)
  759. {
  760. unsigned long off = 0;
  761. struct page *page = first_page;
  762. BUG_ON(!is_first_page(first_page));
  763. while (page) {
  764. struct page *next_page;
  765. struct link_free *link;
  766. unsigned int i = 1;
  767. void *vaddr;
  768. /*
  769. * page->index stores offset of first object starting
  770. * in the page. For the first page, this is always 0,
  771. * so we use first_page->index (aka ->freelist) to store
  772. * head of corresponding zspage's freelist.
  773. */
  774. if (page != first_page)
  775. page->index = off;
  776. vaddr = kmap_atomic(page);
  777. link = (struct link_free *)vaddr + off / sizeof(*link);
  778. while ((off += class->size) < PAGE_SIZE) {
  779. link->next = location_to_obj(page, i++);
  780. link += class->size / sizeof(*link);
  781. }
  782. /*
  783. * We now come to the last (full or partial) object on this
  784. * page, which must point to the first object on the next
  785. * page (if present)
  786. */
  787. next_page = get_next_page(page);
  788. link->next = location_to_obj(next_page, 0);
  789. kunmap_atomic(vaddr);
  790. page = next_page;
  791. off %= PAGE_SIZE;
  792. }
  793. }
  794. /*
  795. * Allocate a zspage for the given size class
  796. */
  797. static struct page *alloc_zspage(struct size_class *class, gfp_t flags)
  798. {
  799. int i, error;
  800. struct page *first_page = NULL, *uninitialized_var(prev_page);
  801. /*
  802. * Allocate individual pages and link them together as:
  803. * 1. first page->private = first sub-page
  804. * 2. all sub-pages are linked together using page->lru
  805. * 3. each sub-page is linked to the first page using page->first_page
  806. *
  807. * For each size class, First/Head pages are linked together using
  808. * page->lru. Also, we set PG_private to identify the first page
  809. * (i.e. no other sub-page has this flag set) and PG_private_2 to
  810. * identify the last page.
  811. */
  812. error = -ENOMEM;
  813. for (i = 0; i < class->pages_per_zspage; i++) {
  814. struct page *page;
  815. page = alloc_page(flags);
  816. if (!page)
  817. goto cleanup;
  818. INIT_LIST_HEAD(&page->lru);
  819. if (i == 0) { /* first page */
  820. SetPagePrivate(page);
  821. set_page_private(page, 0);
  822. first_page = page;
  823. first_page->inuse = 0;
  824. }
  825. if (i == 1)
  826. set_page_private(first_page, (unsigned long)page);
  827. if (i >= 1)
  828. page->first_page = first_page;
  829. if (i >= 2)
  830. list_add(&page->lru, &prev_page->lru);
  831. if (i == class->pages_per_zspage - 1) /* last page */
  832. SetPagePrivate2(page);
  833. prev_page = page;
  834. }
  835. init_zspage(first_page, class);
  836. first_page->freelist = location_to_obj(first_page, 0);
  837. /* Maximum number of objects we can store in this zspage */
  838. first_page->objects = class->pages_per_zspage * PAGE_SIZE / class->size;
  839. error = 0; /* Success */
  840. cleanup:
  841. if (unlikely(error) && first_page) {
  842. free_zspage(first_page);
  843. first_page = NULL;
  844. }
  845. return first_page;
  846. }
  847. static struct page *find_get_zspage(struct size_class *class)
  848. {
  849. int i;
  850. struct page *page;
  851. for (i = 0; i < _ZS_NR_FULLNESS_GROUPS; i++) {
  852. page = class->fullness_list[i];
  853. if (page)
  854. break;
  855. }
  856. return page;
  857. }
  858. #ifdef CONFIG_PGTABLE_MAPPING
  859. static inline int __zs_cpu_up(struct mapping_area *area)
  860. {
  861. /*
  862. * Make sure we don't leak memory if a cpu UP notification
  863. * and zs_init() race and both call zs_cpu_up() on the same cpu
  864. */
  865. if (area->vm)
  866. return 0;
  867. area->vm = alloc_vm_area(PAGE_SIZE * 2, NULL);
  868. if (!area->vm)
  869. return -ENOMEM;
  870. return 0;
  871. }
  872. static inline void __zs_cpu_down(struct mapping_area *area)
  873. {
  874. if (area->vm)
  875. free_vm_area(area->vm);
  876. area->vm = NULL;
  877. }
  878. static inline void *__zs_map_object(struct mapping_area *area,
  879. struct page *pages[2], int off, int size)
  880. {
  881. BUG_ON(map_vm_area(area->vm, PAGE_KERNEL, pages));
  882. area->vm_addr = area->vm->addr;
  883. return area->vm_addr + off;
  884. }
  885. static inline void __zs_unmap_object(struct mapping_area *area,
  886. struct page *pages[2], int off, int size)
  887. {
  888. unsigned long addr = (unsigned long)area->vm_addr;
  889. unmap_kernel_range(addr, PAGE_SIZE * 2);
  890. }
  891. #else /* CONFIG_PGTABLE_MAPPING */
  892. static inline int __zs_cpu_up(struct mapping_area *area)
  893. {
  894. /*
  895. * Make sure we don't leak memory if a cpu UP notification
  896. * and zs_init() race and both call zs_cpu_up() on the same cpu
  897. */
  898. if (area->vm_buf)
  899. return 0;
  900. area->vm_buf = kmalloc(ZS_MAX_ALLOC_SIZE, GFP_KERNEL);
  901. if (!area->vm_buf)
  902. return -ENOMEM;
  903. return 0;
  904. }
  905. static inline void __zs_cpu_down(struct mapping_area *area)
  906. {
  907. kfree(area->vm_buf);
  908. area->vm_buf = NULL;
  909. }
  910. static void *__zs_map_object(struct mapping_area *area,
  911. struct page *pages[2], int off, int size)
  912. {
  913. int sizes[2];
  914. void *addr;
  915. char *buf = area->vm_buf;
  916. /* disable page faults to match kmap_atomic() return conditions */
  917. pagefault_disable();
  918. /* no read fastpath */
  919. if (area->vm_mm == ZS_MM_WO)
  920. goto out;
  921. sizes[0] = PAGE_SIZE - off;
  922. sizes[1] = size - sizes[0];
  923. /* copy object to per-cpu buffer */
  924. addr = kmap_atomic(pages[0]);
  925. memcpy(buf, addr + off, sizes[0]);
  926. kunmap_atomic(addr);
  927. addr = kmap_atomic(pages[1]);
  928. memcpy(buf + sizes[0], addr, sizes[1]);
  929. kunmap_atomic(addr);
  930. out:
  931. return area->vm_buf;
  932. }
  933. static void __zs_unmap_object(struct mapping_area *area,
  934. struct page *pages[2], int off, int size)
  935. {
  936. int sizes[2];
  937. void *addr;
  938. char *buf;
  939. /* no write fastpath */
  940. if (area->vm_mm == ZS_MM_RO)
  941. goto out;
  942. buf = area->vm_buf;
  943. if (!area->huge) {
  944. buf = buf + ZS_HANDLE_SIZE;
  945. size -= ZS_HANDLE_SIZE;
  946. off += ZS_HANDLE_SIZE;
  947. }
  948. sizes[0] = PAGE_SIZE - off;
  949. sizes[1] = size - sizes[0];
  950. /* copy per-cpu buffer to object */
  951. addr = kmap_atomic(pages[0]);
  952. memcpy(addr + off, buf, sizes[0]);
  953. kunmap_atomic(addr);
  954. addr = kmap_atomic(pages[1]);
  955. memcpy(addr, buf + sizes[0], sizes[1]);
  956. kunmap_atomic(addr);
  957. out:
  958. /* enable page faults to match kunmap_atomic() return conditions */
  959. pagefault_enable();
  960. }
  961. #endif /* CONFIG_PGTABLE_MAPPING */
  962. static int zs_cpu_notifier(struct notifier_block *nb, unsigned long action,
  963. void *pcpu)
  964. {
  965. int ret, cpu = (long)pcpu;
  966. struct mapping_area *area;
  967. switch (action) {
  968. case CPU_UP_PREPARE:
  969. area = &per_cpu(zs_map_area, cpu);
  970. ret = __zs_cpu_up(area);
  971. if (ret)
  972. return notifier_from_errno(ret);
  973. break;
  974. case CPU_DEAD:
  975. case CPU_UP_CANCELED:
  976. area = &per_cpu(zs_map_area, cpu);
  977. __zs_cpu_down(area);
  978. break;
  979. }
  980. return NOTIFY_OK;
  981. }
  982. static struct notifier_block zs_cpu_nb = {
  983. .notifier_call = zs_cpu_notifier
  984. };
  985. static int zs_register_cpu_notifier(void)
  986. {
  987. int cpu, uninitialized_var(ret);
  988. cpu_notifier_register_begin();
  989. __register_cpu_notifier(&zs_cpu_nb);
  990. for_each_online_cpu(cpu) {
  991. ret = zs_cpu_notifier(NULL, CPU_UP_PREPARE, (void *)(long)cpu);
  992. if (notifier_to_errno(ret))
  993. break;
  994. }
  995. cpu_notifier_register_done();
  996. return notifier_to_errno(ret);
  997. }
  998. static void zs_unregister_cpu_notifier(void)
  999. {
  1000. int cpu;
  1001. cpu_notifier_register_begin();
  1002. for_each_online_cpu(cpu)
  1003. zs_cpu_notifier(NULL, CPU_DEAD, (void *)(long)cpu);
  1004. __unregister_cpu_notifier(&zs_cpu_nb);
  1005. cpu_notifier_register_done();
  1006. }
  1007. static void init_zs_size_classes(void)
  1008. {
  1009. int nr;
  1010. nr = (ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE) / ZS_SIZE_CLASS_DELTA + 1;
  1011. if ((ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE) % ZS_SIZE_CLASS_DELTA)
  1012. nr += 1;
  1013. zs_size_classes = nr;
  1014. }
  1015. static bool can_merge(struct size_class *prev, int size, int pages_per_zspage)
  1016. {
  1017. if (prev->pages_per_zspage != pages_per_zspage)
  1018. return false;
  1019. if (get_maxobj_per_zspage(prev->size, prev->pages_per_zspage)
  1020. != get_maxobj_per_zspage(size, pages_per_zspage))
  1021. return false;
  1022. return true;
  1023. }
  1024. static bool zspage_full(struct page *page)
  1025. {
  1026. BUG_ON(!is_first_page(page));
  1027. return page->inuse == page->objects;
  1028. }
  1029. unsigned long zs_get_total_pages(struct zs_pool *pool)
  1030. {
  1031. return atomic_long_read(&pool->pages_allocated);
  1032. }
  1033. EXPORT_SYMBOL_GPL(zs_get_total_pages);
  1034. /**
  1035. * zs_map_object - get address of allocated object from handle.
  1036. * @pool: pool from which the object was allocated
  1037. * @handle: handle returned from zs_malloc
  1038. *
  1039. * Before using an object allocated from zs_malloc, it must be mapped using
  1040. * this function. When done with the object, it must be unmapped using
  1041. * zs_unmap_object.
  1042. *
  1043. * Only one object can be mapped per cpu at a time. There is no protection
  1044. * against nested mappings.
  1045. *
  1046. * This function returns with preemption and page faults disabled.
  1047. */
  1048. void *zs_map_object(struct zs_pool *pool, unsigned long handle,
  1049. enum zs_mapmode mm)
  1050. {
  1051. struct page *page;
  1052. unsigned long obj, obj_idx, off;
  1053. unsigned int class_idx;
  1054. enum fullness_group fg;
  1055. struct size_class *class;
  1056. struct mapping_area *area;
  1057. struct page *pages[2];
  1058. void *ret;
  1059. BUG_ON(!handle);
  1060. /*
  1061. * Because we use per-cpu mapping areas shared among the
  1062. * pools/users, we can't allow mapping in interrupt context
  1063. * because it can corrupt another users mappings.
  1064. */
  1065. BUG_ON(in_interrupt());
  1066. /* From now on, migration cannot move the object */
  1067. pin_tag(handle);
  1068. obj = handle_to_obj(handle);
  1069. obj_to_location(obj, &page, &obj_idx);
  1070. get_zspage_mapping(get_first_page(page), &class_idx, &fg);
  1071. class = pool->size_class[class_idx];
  1072. off = obj_idx_to_offset(page, obj_idx, class->size);
  1073. area = &get_cpu_var(zs_map_area);
  1074. area->vm_mm = mm;
  1075. if (off + class->size <= PAGE_SIZE) {
  1076. /* this object is contained entirely within a page */
  1077. area->vm_addr = kmap_atomic(page);
  1078. ret = area->vm_addr + off;
  1079. goto out;
  1080. }
  1081. /* this object spans two pages */
  1082. pages[0] = page;
  1083. pages[1] = get_next_page(page);
  1084. BUG_ON(!pages[1]);
  1085. ret = __zs_map_object(area, pages, off, class->size);
  1086. out:
  1087. if (!class->huge)
  1088. ret += ZS_HANDLE_SIZE;
  1089. return ret;
  1090. }
  1091. EXPORT_SYMBOL_GPL(zs_map_object);
  1092. void zs_unmap_object(struct zs_pool *pool, unsigned long handle)
  1093. {
  1094. struct page *page;
  1095. unsigned long obj, obj_idx, off;
  1096. unsigned int class_idx;
  1097. enum fullness_group fg;
  1098. struct size_class *class;
  1099. struct mapping_area *area;
  1100. BUG_ON(!handle);
  1101. obj = handle_to_obj(handle);
  1102. obj_to_location(obj, &page, &obj_idx);
  1103. get_zspage_mapping(get_first_page(page), &class_idx, &fg);
  1104. class = pool->size_class[class_idx];
  1105. off = obj_idx_to_offset(page, obj_idx, class->size);
  1106. area = this_cpu_ptr(&zs_map_area);
  1107. if (off + class->size <= PAGE_SIZE)
  1108. kunmap_atomic(area->vm_addr);
  1109. else {
  1110. struct page *pages[2];
  1111. pages[0] = page;
  1112. pages[1] = get_next_page(page);
  1113. BUG_ON(!pages[1]);
  1114. __zs_unmap_object(area, pages, off, class->size);
  1115. }
  1116. put_cpu_var(zs_map_area);
  1117. unpin_tag(handle);
  1118. }
  1119. EXPORT_SYMBOL_GPL(zs_unmap_object);
  1120. static unsigned long obj_malloc(struct page *first_page,
  1121. struct size_class *class, unsigned long handle)
  1122. {
  1123. unsigned long obj;
  1124. struct link_free *link;
  1125. struct page *m_page;
  1126. unsigned long m_objidx, m_offset;
  1127. void *vaddr;
  1128. handle |= OBJ_ALLOCATED_TAG;
  1129. obj = (unsigned long)first_page->freelist;
  1130. obj_to_location(obj, &m_page, &m_objidx);
  1131. m_offset = obj_idx_to_offset(m_page, m_objidx, class->size);
  1132. vaddr = kmap_atomic(m_page);
  1133. link = (struct link_free *)vaddr + m_offset / sizeof(*link);
  1134. first_page->freelist = link->next;
  1135. if (!class->huge)
  1136. /* record handle in the header of allocated chunk */
  1137. link->handle = handle;
  1138. else
  1139. /* record handle in first_page->private */
  1140. set_page_private(first_page, handle);
  1141. kunmap_atomic(vaddr);
  1142. first_page->inuse++;
  1143. zs_stat_inc(class, OBJ_USED, 1);
  1144. return obj;
  1145. }
  1146. /**
  1147. * zs_malloc - Allocate block of given size from pool.
  1148. * @pool: pool to allocate from
  1149. * @size: size of block to allocate
  1150. *
  1151. * On success, handle to the allocated object is returned,
  1152. * otherwise 0.
  1153. * Allocation requests with size > ZS_MAX_ALLOC_SIZE will fail.
  1154. */
  1155. unsigned long zs_malloc(struct zs_pool *pool, size_t size)
  1156. {
  1157. unsigned long handle, obj;
  1158. struct size_class *class;
  1159. struct page *first_page;
  1160. if (unlikely(!size || size > ZS_MAX_ALLOC_SIZE))
  1161. return 0;
  1162. handle = alloc_handle(pool);
  1163. if (!handle)
  1164. return 0;
  1165. /* extra space in chunk to keep the handle */
  1166. size += ZS_HANDLE_SIZE;
  1167. class = pool->size_class[get_size_class_index(size)];
  1168. spin_lock(&class->lock);
  1169. first_page = find_get_zspage(class);
  1170. if (!first_page) {
  1171. spin_unlock(&class->lock);
  1172. first_page = alloc_zspage(class, pool->flags);
  1173. if (unlikely(!first_page)) {
  1174. free_handle(pool, handle);
  1175. return 0;
  1176. }
  1177. set_zspage_mapping(first_page, class->index, ZS_EMPTY);
  1178. atomic_long_add(class->pages_per_zspage,
  1179. &pool->pages_allocated);
  1180. spin_lock(&class->lock);
  1181. zs_stat_inc(class, OBJ_ALLOCATED, get_maxobj_per_zspage(
  1182. class->size, class->pages_per_zspage));
  1183. }
  1184. obj = obj_malloc(first_page, class, handle);
  1185. /* Now move the zspage to another fullness group, if required */
  1186. fix_fullness_group(class, first_page);
  1187. record_obj(handle, obj);
  1188. spin_unlock(&class->lock);
  1189. return handle;
  1190. }
  1191. EXPORT_SYMBOL_GPL(zs_malloc);
  1192. static void obj_free(struct zs_pool *pool, struct size_class *class,
  1193. unsigned long obj)
  1194. {
  1195. struct link_free *link;
  1196. struct page *first_page, *f_page;
  1197. unsigned long f_objidx, f_offset;
  1198. void *vaddr;
  1199. int class_idx;
  1200. enum fullness_group fullness;
  1201. BUG_ON(!obj);
  1202. obj &= ~OBJ_ALLOCATED_TAG;
  1203. obj_to_location(obj, &f_page, &f_objidx);
  1204. first_page = get_first_page(f_page);
  1205. get_zspage_mapping(first_page, &class_idx, &fullness);
  1206. f_offset = obj_idx_to_offset(f_page, f_objidx, class->size);
  1207. vaddr = kmap_atomic(f_page);
  1208. /* Insert this object in containing zspage's freelist */
  1209. link = (struct link_free *)(vaddr + f_offset);
  1210. link->next = first_page->freelist;
  1211. if (class->huge)
  1212. set_page_private(first_page, 0);
  1213. kunmap_atomic(vaddr);
  1214. first_page->freelist = (void *)obj;
  1215. first_page->inuse--;
  1216. zs_stat_dec(class, OBJ_USED, 1);
  1217. }
  1218. void zs_free(struct zs_pool *pool, unsigned long handle)
  1219. {
  1220. struct page *first_page, *f_page;
  1221. unsigned long obj, f_objidx;
  1222. int class_idx;
  1223. struct size_class *class;
  1224. enum fullness_group fullness;
  1225. if (unlikely(!handle))
  1226. return;
  1227. pin_tag(handle);
  1228. obj = handle_to_obj(handle);
  1229. obj_to_location(obj, &f_page, &f_objidx);
  1230. first_page = get_first_page(f_page);
  1231. get_zspage_mapping(first_page, &class_idx, &fullness);
  1232. class = pool->size_class[class_idx];
  1233. spin_lock(&class->lock);
  1234. obj_free(pool, class, obj);
  1235. fullness = fix_fullness_group(class, first_page);
  1236. if (fullness == ZS_EMPTY) {
  1237. zs_stat_dec(class, OBJ_ALLOCATED, get_maxobj_per_zspage(
  1238. class->size, class->pages_per_zspage));
  1239. atomic_long_sub(class->pages_per_zspage,
  1240. &pool->pages_allocated);
  1241. free_zspage(first_page);
  1242. }
  1243. spin_unlock(&class->lock);
  1244. unpin_tag(handle);
  1245. free_handle(pool, handle);
  1246. }
  1247. EXPORT_SYMBOL_GPL(zs_free);
  1248. static void zs_object_copy(unsigned long dst, unsigned long src,
  1249. struct size_class *class)
  1250. {
  1251. struct page *s_page, *d_page;
  1252. unsigned long s_objidx, d_objidx;
  1253. unsigned long s_off, d_off;
  1254. void *s_addr, *d_addr;
  1255. int s_size, d_size, size;
  1256. int written = 0;
  1257. s_size = d_size = class->size;
  1258. obj_to_location(src, &s_page, &s_objidx);
  1259. obj_to_location(dst, &d_page, &d_objidx);
  1260. s_off = obj_idx_to_offset(s_page, s_objidx, class->size);
  1261. d_off = obj_idx_to_offset(d_page, d_objidx, class->size);
  1262. if (s_off + class->size > PAGE_SIZE)
  1263. s_size = PAGE_SIZE - s_off;
  1264. if (d_off + class->size > PAGE_SIZE)
  1265. d_size = PAGE_SIZE - d_off;
  1266. s_addr = kmap_atomic(s_page);
  1267. d_addr = kmap_atomic(d_page);
  1268. while (1) {
  1269. size = min(s_size, d_size);
  1270. memcpy(d_addr + d_off, s_addr + s_off, size);
  1271. written += size;
  1272. if (written == class->size)
  1273. break;
  1274. s_off += size;
  1275. s_size -= size;
  1276. d_off += size;
  1277. d_size -= size;
  1278. if (s_off >= PAGE_SIZE) {
  1279. kunmap_atomic(d_addr);
  1280. kunmap_atomic(s_addr);
  1281. s_page = get_next_page(s_page);
  1282. BUG_ON(!s_page);
  1283. s_addr = kmap_atomic(s_page);
  1284. d_addr = kmap_atomic(d_page);
  1285. s_size = class->size - written;
  1286. s_off = 0;
  1287. }
  1288. if (d_off >= PAGE_SIZE) {
  1289. kunmap_atomic(d_addr);
  1290. d_page = get_next_page(d_page);
  1291. BUG_ON(!d_page);
  1292. d_addr = kmap_atomic(d_page);
  1293. d_size = class->size - written;
  1294. d_off = 0;
  1295. }
  1296. }
  1297. kunmap_atomic(d_addr);
  1298. kunmap_atomic(s_addr);
  1299. }
  1300. /*
  1301. * Find alloced object in zspage from index object and
  1302. * return handle.
  1303. */
  1304. static unsigned long find_alloced_obj(struct page *page, int index,
  1305. struct size_class *class)
  1306. {
  1307. unsigned long head;
  1308. int offset = 0;
  1309. unsigned long handle = 0;
  1310. void *addr = kmap_atomic(page);
  1311. if (!is_first_page(page))
  1312. offset = page->index;
  1313. offset += class->size * index;
  1314. while (offset < PAGE_SIZE) {
  1315. head = obj_to_head(class, page, addr + offset);
  1316. if (head & OBJ_ALLOCATED_TAG) {
  1317. handle = head & ~OBJ_ALLOCATED_TAG;
  1318. if (trypin_tag(handle))
  1319. break;
  1320. handle = 0;
  1321. }
  1322. offset += class->size;
  1323. index++;
  1324. }
  1325. kunmap_atomic(addr);
  1326. return handle;
  1327. }
  1328. struct zs_compact_control {
  1329. /* Source page for migration which could be a subpage of zspage. */
  1330. struct page *s_page;
  1331. /* Destination page for migration which should be a first page
  1332. * of zspage. */
  1333. struct page *d_page;
  1334. /* Starting object index within @s_page which used for live object
  1335. * in the subpage. */
  1336. int index;
  1337. };
  1338. static int migrate_zspage(struct zs_pool *pool, struct size_class *class,
  1339. struct zs_compact_control *cc)
  1340. {
  1341. unsigned long used_obj, free_obj;
  1342. unsigned long handle;
  1343. struct page *s_page = cc->s_page;
  1344. struct page *d_page = cc->d_page;
  1345. unsigned long index = cc->index;
  1346. int ret = 0;
  1347. while (1) {
  1348. handle = find_alloced_obj(s_page, index, class);
  1349. if (!handle) {
  1350. s_page = get_next_page(s_page);
  1351. if (!s_page)
  1352. break;
  1353. index = 0;
  1354. continue;
  1355. }
  1356. /* Stop if there is no more space */
  1357. if (zspage_full(d_page)) {
  1358. unpin_tag(handle);
  1359. ret = -ENOMEM;
  1360. break;
  1361. }
  1362. used_obj = handle_to_obj(handle);
  1363. free_obj = obj_malloc(d_page, class, handle);
  1364. zs_object_copy(free_obj, used_obj, class);
  1365. index++;
  1366. record_obj(handle, free_obj);
  1367. unpin_tag(handle);
  1368. obj_free(pool, class, used_obj);
  1369. }
  1370. /* Remember last position in this iteration */
  1371. cc->s_page = s_page;
  1372. cc->index = index;
  1373. return ret;
  1374. }
  1375. static struct page *isolate_target_page(struct size_class *class)
  1376. {
  1377. int i;
  1378. struct page *page;
  1379. for (i = 0; i < _ZS_NR_FULLNESS_GROUPS; i++) {
  1380. page = class->fullness_list[i];
  1381. if (page) {
  1382. remove_zspage(page, class, i);
  1383. break;
  1384. }
  1385. }
  1386. return page;
  1387. }
  1388. /*
  1389. * putback_zspage - add @first_page into right class's fullness list
  1390. * @pool: target pool
  1391. * @class: destination class
  1392. * @first_page: target page
  1393. *
  1394. * Return @fist_page's fullness_group
  1395. */
  1396. static enum fullness_group putback_zspage(struct zs_pool *pool,
  1397. struct size_class *class,
  1398. struct page *first_page)
  1399. {
  1400. enum fullness_group fullness;
  1401. BUG_ON(!is_first_page(first_page));
  1402. fullness = get_fullness_group(first_page);
  1403. insert_zspage(first_page, class, fullness);
  1404. set_zspage_mapping(first_page, class->index, fullness);
  1405. if (fullness == ZS_EMPTY) {
  1406. zs_stat_dec(class, OBJ_ALLOCATED, get_maxobj_per_zspage(
  1407. class->size, class->pages_per_zspage));
  1408. atomic_long_sub(class->pages_per_zspage,
  1409. &pool->pages_allocated);
  1410. free_zspage(first_page);
  1411. }
  1412. return fullness;
  1413. }
  1414. static struct page *isolate_source_page(struct size_class *class)
  1415. {
  1416. int i;
  1417. struct page *page = NULL;
  1418. for (i = ZS_ALMOST_EMPTY; i >= ZS_ALMOST_FULL; i--) {
  1419. page = class->fullness_list[i];
  1420. if (!page)
  1421. continue;
  1422. remove_zspage(page, class, i);
  1423. break;
  1424. }
  1425. return page;
  1426. }
  1427. /*
  1428. *
  1429. * Based on the number of unused allocated objects calculate
  1430. * and return the number of pages that we can free.
  1431. */
  1432. static unsigned long zs_can_compact(struct size_class *class)
  1433. {
  1434. unsigned long obj_wasted;
  1435. obj_wasted = zs_stat_get(class, OBJ_ALLOCATED) -
  1436. zs_stat_get(class, OBJ_USED);
  1437. obj_wasted /= get_maxobj_per_zspage(class->size,
  1438. class->pages_per_zspage);
  1439. return obj_wasted * class->pages_per_zspage;
  1440. }
  1441. static void __zs_compact(struct zs_pool *pool, struct size_class *class)
  1442. {
  1443. struct zs_compact_control cc;
  1444. struct page *src_page;
  1445. struct page *dst_page = NULL;
  1446. spin_lock(&class->lock);
  1447. while ((src_page = isolate_source_page(class))) {
  1448. BUG_ON(!is_first_page(src_page));
  1449. if (!zs_can_compact(class))
  1450. break;
  1451. cc.index = 0;
  1452. cc.s_page = src_page;
  1453. while ((dst_page = isolate_target_page(class))) {
  1454. cc.d_page = dst_page;
  1455. /*
  1456. * If there is no more space in dst_page, resched
  1457. * and see if anyone had allocated another zspage.
  1458. */
  1459. if (!migrate_zspage(pool, class, &cc))
  1460. break;
  1461. putback_zspage(pool, class, dst_page);
  1462. }
  1463. /* Stop if we couldn't find slot */
  1464. if (dst_page == NULL)
  1465. break;
  1466. putback_zspage(pool, class, dst_page);
  1467. if (putback_zspage(pool, class, src_page) == ZS_EMPTY)
  1468. pool->stats.pages_compacted += class->pages_per_zspage;
  1469. spin_unlock(&class->lock);
  1470. cond_resched();
  1471. spin_lock(&class->lock);
  1472. }
  1473. if (src_page)
  1474. putback_zspage(pool, class, src_page);
  1475. spin_unlock(&class->lock);
  1476. }
  1477. unsigned long zs_compact(struct zs_pool *pool)
  1478. {
  1479. int i;
  1480. struct size_class *class;
  1481. for (i = zs_size_classes - 1; i >= 0; i--) {
  1482. class = pool->size_class[i];
  1483. if (!class)
  1484. continue;
  1485. if (class->index != i)
  1486. continue;
  1487. __zs_compact(pool, class);
  1488. }
  1489. return pool->stats.pages_compacted;
  1490. }
  1491. EXPORT_SYMBOL_GPL(zs_compact);
  1492. void zs_pool_stats(struct zs_pool *pool, struct zs_pool_stats *stats)
  1493. {
  1494. memcpy(stats, &pool->stats, sizeof(struct zs_pool_stats));
  1495. }
  1496. EXPORT_SYMBOL_GPL(zs_pool_stats);
  1497. static unsigned long zs_shrinker_scan(struct shrinker *shrinker,
  1498. struct shrink_control *sc)
  1499. {
  1500. unsigned long pages_freed;
  1501. struct zs_pool *pool = container_of(shrinker, struct zs_pool,
  1502. shrinker);
  1503. pages_freed = pool->stats.pages_compacted;
  1504. /*
  1505. * Compact classes and calculate compaction delta.
  1506. * Can run concurrently with a manually triggered
  1507. * (by user) compaction.
  1508. */
  1509. pages_freed = zs_compact(pool) - pages_freed;
  1510. return pages_freed ? pages_freed : SHRINK_STOP;
  1511. }
  1512. static unsigned long zs_shrinker_count(struct shrinker *shrinker,
  1513. struct shrink_control *sc)
  1514. {
  1515. int i;
  1516. struct size_class *class;
  1517. unsigned long pages_to_free = 0;
  1518. struct zs_pool *pool = container_of(shrinker, struct zs_pool,
  1519. shrinker);
  1520. if (!pool->shrinker_enabled)
  1521. return 0;
  1522. for (i = zs_size_classes - 1; i >= 0; i--) {
  1523. class = pool->size_class[i];
  1524. if (!class)
  1525. continue;
  1526. if (class->index != i)
  1527. continue;
  1528. pages_to_free += zs_can_compact(class);
  1529. }
  1530. return pages_to_free;
  1531. }
  1532. static void zs_unregister_shrinker(struct zs_pool *pool)
  1533. {
  1534. if (pool->shrinker_enabled) {
  1535. unregister_shrinker(&pool->shrinker);
  1536. pool->shrinker_enabled = false;
  1537. }
  1538. }
  1539. static int zs_register_shrinker(struct zs_pool *pool)
  1540. {
  1541. pool->shrinker.scan_objects = zs_shrinker_scan;
  1542. pool->shrinker.count_objects = zs_shrinker_count;
  1543. pool->shrinker.batch = 0;
  1544. pool->shrinker.seeks = DEFAULT_SEEKS;
  1545. return register_shrinker(&pool->shrinker);
  1546. }
  1547. /**
  1548. * zs_create_pool - Creates an allocation pool to work from.
  1549. * @flags: allocation flags used to allocate pool metadata
  1550. *
  1551. * This function must be called before anything when using
  1552. * the zsmalloc allocator.
  1553. *
  1554. * On success, a pointer to the newly created pool is returned,
  1555. * otherwise NULL.
  1556. */
  1557. struct zs_pool *zs_create_pool(char *name, gfp_t flags)
  1558. {
  1559. int i;
  1560. struct zs_pool *pool;
  1561. struct size_class *prev_class = NULL;
  1562. pool = kzalloc(sizeof(*pool), GFP_KERNEL);
  1563. if (!pool)
  1564. return NULL;
  1565. pool->size_class = kcalloc(zs_size_classes, sizeof(struct size_class *),
  1566. GFP_KERNEL);
  1567. if (!pool->size_class) {
  1568. kfree(pool);
  1569. return NULL;
  1570. }
  1571. pool->name = kstrdup(name, GFP_KERNEL);
  1572. if (!pool->name)
  1573. goto err;
  1574. if (create_handle_cache(pool))
  1575. goto err;
  1576. /*
  1577. * Iterate reversly, because, size of size_class that we want to use
  1578. * for merging should be larger or equal to current size.
  1579. */
  1580. for (i = zs_size_classes - 1; i >= 0; i--) {
  1581. int size;
  1582. int pages_per_zspage;
  1583. struct size_class *class;
  1584. size = ZS_MIN_ALLOC_SIZE + i * ZS_SIZE_CLASS_DELTA;
  1585. if (size > ZS_MAX_ALLOC_SIZE)
  1586. size = ZS_MAX_ALLOC_SIZE;
  1587. pages_per_zspage = get_pages_per_zspage(size);
  1588. /*
  1589. * size_class is used for normal zsmalloc operation such
  1590. * as alloc/free for that size. Although it is natural that we
  1591. * have one size_class for each size, there is a chance that we
  1592. * can get more memory utilization if we use one size_class for
  1593. * many different sizes whose size_class have same
  1594. * characteristics. So, we makes size_class point to
  1595. * previous size_class if possible.
  1596. */
  1597. if (prev_class) {
  1598. if (can_merge(prev_class, size, pages_per_zspage)) {
  1599. pool->size_class[i] = prev_class;
  1600. continue;
  1601. }
  1602. }
  1603. class = kzalloc(sizeof(struct size_class), GFP_KERNEL);
  1604. if (!class)
  1605. goto err;
  1606. class->size = size;
  1607. class->index = i;
  1608. class->pages_per_zspage = pages_per_zspage;
  1609. if (pages_per_zspage == 1 &&
  1610. get_maxobj_per_zspage(size, pages_per_zspage) == 1)
  1611. class->huge = true;
  1612. spin_lock_init(&class->lock);
  1613. pool->size_class[i] = class;
  1614. prev_class = class;
  1615. }
  1616. pool->flags = flags;
  1617. if (zs_pool_stat_create(name, pool))
  1618. goto err;
  1619. /*
  1620. * Not critical, we still can use the pool
  1621. * and user can trigger compaction manually.
  1622. */
  1623. if (zs_register_shrinker(pool) == 0)
  1624. pool->shrinker_enabled = true;
  1625. return pool;
  1626. err:
  1627. zs_destroy_pool(pool);
  1628. return NULL;
  1629. }
  1630. EXPORT_SYMBOL_GPL(zs_create_pool);
  1631. void zs_destroy_pool(struct zs_pool *pool)
  1632. {
  1633. int i;
  1634. zs_unregister_shrinker(pool);
  1635. zs_pool_stat_destroy(pool);
  1636. for (i = 0; i < zs_size_classes; i++) {
  1637. int fg;
  1638. struct size_class *class = pool->size_class[i];
  1639. if (!class)
  1640. continue;
  1641. if (class->index != i)
  1642. continue;
  1643. for (fg = 0; fg < _ZS_NR_FULLNESS_GROUPS; fg++) {
  1644. if (class->fullness_list[fg]) {
  1645. pr_info("Freeing non-empty class with size %db, fullness group %d\n",
  1646. class->size, fg);
  1647. }
  1648. }
  1649. kfree(class);
  1650. }
  1651. destroy_handle_cache(pool);
  1652. kfree(pool->size_class);
  1653. kfree(pool->name);
  1654. kfree(pool);
  1655. }
  1656. EXPORT_SYMBOL_GPL(zs_destroy_pool);
  1657. static int __init zs_init(void)
  1658. {
  1659. int ret = zs_register_cpu_notifier();
  1660. if (ret)
  1661. goto notifier_fail;
  1662. init_zs_size_classes();
  1663. #ifdef CONFIG_ZPOOL
  1664. zpool_register_driver(&zs_zpool_driver);
  1665. #endif
  1666. ret = zs_stat_init();
  1667. if (ret) {
  1668. pr_err("zs stat initialization failed\n");
  1669. goto stat_fail;
  1670. }
  1671. return 0;
  1672. stat_fail:
  1673. #ifdef CONFIG_ZPOOL
  1674. zpool_unregister_driver(&zs_zpool_driver);
  1675. #endif
  1676. notifier_fail:
  1677. zs_unregister_cpu_notifier();
  1678. return ret;
  1679. }
  1680. static void __exit zs_exit(void)
  1681. {
  1682. #ifdef CONFIG_ZPOOL
  1683. zpool_unregister_driver(&zs_zpool_driver);
  1684. #endif
  1685. zs_unregister_cpu_notifier();
  1686. zs_stat_exit();
  1687. }
  1688. module_init(zs_init);
  1689. module_exit(zs_exit);
  1690. MODULE_LICENSE("Dual BSD/GPL");
  1691. MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");