zsmalloc.c 45 KB

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