zsmalloc.c 46 KB

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