zsmalloc.c 46 KB

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