zsmalloc.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956
  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. /* In huge class size, we store the handle into first_page->private */
  1169. if (class->huge) {
  1170. size -= ZS_HANDLE_SIZE;
  1171. class = pool->size_class[get_size_class_index(size)];
  1172. }
  1173. spin_lock(&class->lock);
  1174. first_page = find_get_zspage(class);
  1175. if (!first_page) {
  1176. spin_unlock(&class->lock);
  1177. first_page = alloc_zspage(class, pool->flags);
  1178. if (unlikely(!first_page)) {
  1179. free_handle(pool, handle);
  1180. return 0;
  1181. }
  1182. set_zspage_mapping(first_page, class->index, ZS_EMPTY);
  1183. atomic_long_add(class->pages_per_zspage,
  1184. &pool->pages_allocated);
  1185. spin_lock(&class->lock);
  1186. zs_stat_inc(class, OBJ_ALLOCATED, get_maxobj_per_zspage(
  1187. class->size, class->pages_per_zspage));
  1188. }
  1189. obj = obj_malloc(first_page, class, handle);
  1190. /* Now move the zspage to another fullness group, if required */
  1191. fix_fullness_group(class, first_page);
  1192. record_obj(handle, obj);
  1193. spin_unlock(&class->lock);
  1194. return handle;
  1195. }
  1196. EXPORT_SYMBOL_GPL(zs_malloc);
  1197. static void obj_free(struct zs_pool *pool, struct size_class *class,
  1198. unsigned long obj)
  1199. {
  1200. struct link_free *link;
  1201. struct page *first_page, *f_page;
  1202. unsigned long f_objidx, f_offset;
  1203. void *vaddr;
  1204. int class_idx;
  1205. enum fullness_group fullness;
  1206. BUG_ON(!obj);
  1207. obj &= ~OBJ_ALLOCATED_TAG;
  1208. obj_to_location(obj, &f_page, &f_objidx);
  1209. first_page = get_first_page(f_page);
  1210. get_zspage_mapping(first_page, &class_idx, &fullness);
  1211. f_offset = obj_idx_to_offset(f_page, f_objidx, class->size);
  1212. vaddr = kmap_atomic(f_page);
  1213. /* Insert this object in containing zspage's freelist */
  1214. link = (struct link_free *)(vaddr + f_offset);
  1215. link->next = first_page->freelist;
  1216. if (class->huge)
  1217. set_page_private(first_page, 0);
  1218. kunmap_atomic(vaddr);
  1219. first_page->freelist = (void *)obj;
  1220. first_page->inuse--;
  1221. zs_stat_dec(class, OBJ_USED, 1);
  1222. }
  1223. void zs_free(struct zs_pool *pool, unsigned long handle)
  1224. {
  1225. struct page *first_page, *f_page;
  1226. unsigned long obj, f_objidx;
  1227. int class_idx;
  1228. struct size_class *class;
  1229. enum fullness_group fullness;
  1230. if (unlikely(!handle))
  1231. return;
  1232. pin_tag(handle);
  1233. obj = handle_to_obj(handle);
  1234. obj_to_location(obj, &f_page, &f_objidx);
  1235. first_page = get_first_page(f_page);
  1236. get_zspage_mapping(first_page, &class_idx, &fullness);
  1237. class = pool->size_class[class_idx];
  1238. spin_lock(&class->lock);
  1239. obj_free(pool, class, obj);
  1240. fullness = fix_fullness_group(class, first_page);
  1241. if (fullness == ZS_EMPTY) {
  1242. zs_stat_dec(class, OBJ_ALLOCATED, get_maxobj_per_zspage(
  1243. class->size, class->pages_per_zspage));
  1244. atomic_long_sub(class->pages_per_zspage,
  1245. &pool->pages_allocated);
  1246. free_zspage(first_page);
  1247. }
  1248. spin_unlock(&class->lock);
  1249. unpin_tag(handle);
  1250. free_handle(pool, handle);
  1251. }
  1252. EXPORT_SYMBOL_GPL(zs_free);
  1253. static void zs_object_copy(unsigned long src, unsigned long dst,
  1254. struct size_class *class)
  1255. {
  1256. struct page *s_page, *d_page;
  1257. unsigned long s_objidx, d_objidx;
  1258. unsigned long s_off, d_off;
  1259. void *s_addr, *d_addr;
  1260. int s_size, d_size, size;
  1261. int written = 0;
  1262. s_size = d_size = class->size;
  1263. obj_to_location(src, &s_page, &s_objidx);
  1264. obj_to_location(dst, &d_page, &d_objidx);
  1265. s_off = obj_idx_to_offset(s_page, s_objidx, class->size);
  1266. d_off = obj_idx_to_offset(d_page, d_objidx, class->size);
  1267. if (s_off + class->size > PAGE_SIZE)
  1268. s_size = PAGE_SIZE - s_off;
  1269. if (d_off + class->size > PAGE_SIZE)
  1270. d_size = PAGE_SIZE - d_off;
  1271. s_addr = kmap_atomic(s_page);
  1272. d_addr = kmap_atomic(d_page);
  1273. while (1) {
  1274. size = min(s_size, d_size);
  1275. memcpy(d_addr + d_off, s_addr + s_off, size);
  1276. written += size;
  1277. if (written == class->size)
  1278. break;
  1279. if (s_off + size >= PAGE_SIZE) {
  1280. kunmap_atomic(d_addr);
  1281. kunmap_atomic(s_addr);
  1282. s_page = get_next_page(s_page);
  1283. BUG_ON(!s_page);
  1284. s_addr = kmap_atomic(s_page);
  1285. d_addr = kmap_atomic(d_page);
  1286. s_size = class->size - written;
  1287. s_off = 0;
  1288. } else {
  1289. s_off += size;
  1290. s_size -= size;
  1291. }
  1292. if (d_off + size >= PAGE_SIZE) {
  1293. kunmap_atomic(d_addr);
  1294. d_page = get_next_page(d_page);
  1295. BUG_ON(!d_page);
  1296. d_addr = kmap_atomic(d_page);
  1297. d_size = class->size - written;
  1298. d_off = 0;
  1299. } else {
  1300. d_off += size;
  1301. d_size -= size;
  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. record_obj(handle, free_obj);
  1377. unpin_tag(handle);
  1378. obj_free(pool, class, used_obj);
  1379. nr_migrated++;
  1380. }
  1381. /* Remember last position in this iteration */
  1382. cc->s_page = s_page;
  1383. cc->index = index;
  1384. cc->nr_migrated = nr_migrated;
  1385. return ret;
  1386. }
  1387. static struct page *alloc_target_page(struct size_class *class)
  1388. {
  1389. int i;
  1390. struct page *page;
  1391. for (i = 0; i < _ZS_NR_FULLNESS_GROUPS; i++) {
  1392. page = class->fullness_list[i];
  1393. if (page) {
  1394. remove_zspage(page, class, i);
  1395. break;
  1396. }
  1397. }
  1398. return page;
  1399. }
  1400. static void putback_zspage(struct zs_pool *pool, struct size_class *class,
  1401. struct page *first_page)
  1402. {
  1403. int class_idx;
  1404. enum fullness_group fullness;
  1405. BUG_ON(!is_first_page(first_page));
  1406. get_zspage_mapping(first_page, &class_idx, &fullness);
  1407. insert_zspage(first_page, class, fullness);
  1408. fullness = fix_fullness_group(class, first_page);
  1409. if (fullness == ZS_EMPTY) {
  1410. zs_stat_dec(class, OBJ_ALLOCATED, get_maxobj_per_zspage(
  1411. class->size, class->pages_per_zspage));
  1412. atomic_long_sub(class->pages_per_zspage,
  1413. &pool->pages_allocated);
  1414. free_zspage(first_page);
  1415. }
  1416. }
  1417. static struct page *isolate_source_page(struct size_class *class)
  1418. {
  1419. struct page *page;
  1420. page = class->fullness_list[ZS_ALMOST_EMPTY];
  1421. if (page)
  1422. remove_zspage(page, class, ZS_ALMOST_EMPTY);
  1423. return page;
  1424. }
  1425. static unsigned long __zs_compact(struct zs_pool *pool,
  1426. struct size_class *class)
  1427. {
  1428. int nr_to_migrate;
  1429. struct zs_compact_control cc;
  1430. struct page *src_page;
  1431. struct page *dst_page = NULL;
  1432. unsigned long nr_total_migrated = 0;
  1433. cond_resched();
  1434. spin_lock(&class->lock);
  1435. while ((src_page = isolate_source_page(class))) {
  1436. BUG_ON(!is_first_page(src_page));
  1437. /* The goal is to migrate all live objects in source page */
  1438. nr_to_migrate = src_page->inuse;
  1439. cc.index = 0;
  1440. cc.s_page = src_page;
  1441. while ((dst_page = alloc_target_page(class))) {
  1442. cc.d_page = dst_page;
  1443. /*
  1444. * If there is no more space in dst_page, try to
  1445. * allocate another zspage.
  1446. */
  1447. if (!migrate_zspage(pool, class, &cc))
  1448. break;
  1449. putback_zspage(pool, class, dst_page);
  1450. nr_total_migrated += cc.nr_migrated;
  1451. nr_to_migrate -= cc.nr_migrated;
  1452. }
  1453. /* Stop if we couldn't find slot */
  1454. if (dst_page == NULL)
  1455. break;
  1456. putback_zspage(pool, class, dst_page);
  1457. putback_zspage(pool, class, src_page);
  1458. spin_unlock(&class->lock);
  1459. nr_total_migrated += cc.nr_migrated;
  1460. cond_resched();
  1461. spin_lock(&class->lock);
  1462. }
  1463. if (src_page)
  1464. putback_zspage(pool, class, src_page);
  1465. spin_unlock(&class->lock);
  1466. return nr_total_migrated;
  1467. }
  1468. unsigned long zs_compact(struct zs_pool *pool)
  1469. {
  1470. int i;
  1471. unsigned long nr_migrated = 0;
  1472. struct size_class *class;
  1473. for (i = zs_size_classes - 1; i >= 0; i--) {
  1474. class = pool->size_class[i];
  1475. if (!class)
  1476. continue;
  1477. if (class->index != i)
  1478. continue;
  1479. nr_migrated += __zs_compact(pool, class);
  1480. }
  1481. synchronize_rcu();
  1482. return nr_migrated;
  1483. }
  1484. EXPORT_SYMBOL_GPL(zs_compact);
  1485. /**
  1486. * zs_create_pool - Creates an allocation pool to work from.
  1487. * @flags: allocation flags used to allocate pool metadata
  1488. *
  1489. * This function must be called before anything when using
  1490. * the zsmalloc allocator.
  1491. *
  1492. * On success, a pointer to the newly created pool is returned,
  1493. * otherwise NULL.
  1494. */
  1495. struct zs_pool *zs_create_pool(char *name, gfp_t flags)
  1496. {
  1497. int i;
  1498. struct zs_pool *pool;
  1499. struct size_class *prev_class = NULL;
  1500. pool = kzalloc(sizeof(*pool), GFP_KERNEL);
  1501. if (!pool)
  1502. return NULL;
  1503. pool->size_class = kcalloc(zs_size_classes, sizeof(struct size_class *),
  1504. GFP_KERNEL);
  1505. if (!pool->size_class) {
  1506. kfree(pool);
  1507. return NULL;
  1508. }
  1509. pool->name = kstrdup(name, GFP_KERNEL);
  1510. if (!pool->name)
  1511. goto err;
  1512. if (create_handle_cache(pool))
  1513. goto err;
  1514. /*
  1515. * Iterate reversly, because, size of size_class that we want to use
  1516. * for merging should be larger or equal to current size.
  1517. */
  1518. for (i = zs_size_classes - 1; i >= 0; i--) {
  1519. int size;
  1520. int pages_per_zspage;
  1521. struct size_class *class;
  1522. size = ZS_MIN_ALLOC_SIZE + i * ZS_SIZE_CLASS_DELTA;
  1523. if (size > ZS_MAX_ALLOC_SIZE)
  1524. size = ZS_MAX_ALLOC_SIZE;
  1525. pages_per_zspage = get_pages_per_zspage(size);
  1526. /*
  1527. * size_class is used for normal zsmalloc operation such
  1528. * as alloc/free for that size. Although it is natural that we
  1529. * have one size_class for each size, there is a chance that we
  1530. * can get more memory utilization if we use one size_class for
  1531. * many different sizes whose size_class have same
  1532. * characteristics. So, we makes size_class point to
  1533. * previous size_class if possible.
  1534. */
  1535. if (prev_class) {
  1536. if (can_merge(prev_class, size, pages_per_zspage)) {
  1537. pool->size_class[i] = prev_class;
  1538. continue;
  1539. }
  1540. }
  1541. class = kzalloc(sizeof(struct size_class), GFP_KERNEL);
  1542. if (!class)
  1543. goto err;
  1544. class->size = size;
  1545. class->index = i;
  1546. class->pages_per_zspage = pages_per_zspage;
  1547. if (pages_per_zspage == 1 &&
  1548. get_maxobj_per_zspage(size, pages_per_zspage) == 1)
  1549. class->huge = true;
  1550. spin_lock_init(&class->lock);
  1551. pool->size_class[i] = class;
  1552. prev_class = class;
  1553. }
  1554. pool->flags = flags;
  1555. if (zs_pool_stat_create(name, pool))
  1556. goto err;
  1557. return pool;
  1558. err:
  1559. zs_destroy_pool(pool);
  1560. return NULL;
  1561. }
  1562. EXPORT_SYMBOL_GPL(zs_create_pool);
  1563. void zs_destroy_pool(struct zs_pool *pool)
  1564. {
  1565. int i;
  1566. zs_pool_stat_destroy(pool);
  1567. for (i = 0; i < zs_size_classes; i++) {
  1568. int fg;
  1569. struct size_class *class = pool->size_class[i];
  1570. if (!class)
  1571. continue;
  1572. if (class->index != i)
  1573. continue;
  1574. for (fg = 0; fg < _ZS_NR_FULLNESS_GROUPS; fg++) {
  1575. if (class->fullness_list[fg]) {
  1576. pr_info("Freeing non-empty class with size %db, fullness group %d\n",
  1577. class->size, fg);
  1578. }
  1579. }
  1580. kfree(class);
  1581. }
  1582. destroy_handle_cache(pool);
  1583. kfree(pool->size_class);
  1584. kfree(pool->name);
  1585. kfree(pool);
  1586. }
  1587. EXPORT_SYMBOL_GPL(zs_destroy_pool);
  1588. static int __init zs_init(void)
  1589. {
  1590. int ret = zs_register_cpu_notifier();
  1591. if (ret)
  1592. goto notifier_fail;
  1593. init_zs_size_classes();
  1594. #ifdef CONFIG_ZPOOL
  1595. zpool_register_driver(&zs_zpool_driver);
  1596. #endif
  1597. ret = zs_stat_init();
  1598. if (ret) {
  1599. pr_err("zs stat initialization failed\n");
  1600. goto stat_fail;
  1601. }
  1602. return 0;
  1603. stat_fail:
  1604. #ifdef CONFIG_ZPOOL
  1605. zpool_unregister_driver(&zs_zpool_driver);
  1606. #endif
  1607. notifier_fail:
  1608. zs_unregister_cpu_notifier();
  1609. return ret;
  1610. }
  1611. static void __exit zs_exit(void)
  1612. {
  1613. #ifdef CONFIG_ZPOOL
  1614. zpool_unregister_driver(&zs_zpool_driver);
  1615. #endif
  1616. zs_unregister_cpu_notifier();
  1617. zs_stat_exit();
  1618. }
  1619. module_init(zs_init);
  1620. module_exit(zs_exit);
  1621. MODULE_LICENSE("Dual BSD/GPL");
  1622. MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");