zsmalloc.c 48 KB

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