zsmalloc.c 45 KB

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