zsmalloc.c 47 KB

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