zsmalloc.c 46 KB

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