zsmalloc.c 45 KB

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