zsmalloc.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503
  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/bitops.h>
  79. #include <linux/errno.h>
  80. #include <linux/highmem.h>
  81. #include <linux/string.h>
  82. #include <linux/slab.h>
  83. #include <asm/tlbflush.h>
  84. #include <asm/pgtable.h>
  85. #include <linux/cpumask.h>
  86. #include <linux/cpu.h>
  87. #include <linux/vmalloc.h>
  88. #include <linux/hardirq.h>
  89. #include <linux/spinlock.h>
  90. #include <linux/types.h>
  91. #include <linux/debugfs.h>
  92. #include <linux/zsmalloc.h>
  93. #include <linux/zpool.h>
  94. /*
  95. * This must be power of 2 and greater than of equal to sizeof(link_free).
  96. * These two conditions ensure that any 'struct link_free' itself doesn't
  97. * span more than 1 page which avoids complex case of mapping 2 pages simply
  98. * to restore link_free pointer values.
  99. */
  100. #define ZS_ALIGN 8
  101. /*
  102. * A single 'zspage' is composed of up to 2^N discontiguous 0-order (single)
  103. * pages. ZS_MAX_ZSPAGE_ORDER defines upper limit on N.
  104. */
  105. #define ZS_MAX_ZSPAGE_ORDER 2
  106. #define ZS_MAX_PAGES_PER_ZSPAGE (_AC(1, UL) << ZS_MAX_ZSPAGE_ORDER)
  107. /*
  108. * Object location (<PFN>, <obj_idx>) is encoded as
  109. * as single (unsigned long) handle value.
  110. *
  111. * Note that object index <obj_idx> is relative to system
  112. * page <PFN> it is stored in, so for each sub-page belonging
  113. * to a zspage, obj_idx starts with 0.
  114. *
  115. * This is made more complicated by various memory models and PAE.
  116. */
  117. #ifndef MAX_PHYSMEM_BITS
  118. #ifdef CONFIG_HIGHMEM64G
  119. #define MAX_PHYSMEM_BITS 36
  120. #else /* !CONFIG_HIGHMEM64G */
  121. /*
  122. * If this definition of MAX_PHYSMEM_BITS is used, OBJ_INDEX_BITS will just
  123. * be PAGE_SHIFT
  124. */
  125. #define MAX_PHYSMEM_BITS BITS_PER_LONG
  126. #endif
  127. #endif
  128. #define _PFN_BITS (MAX_PHYSMEM_BITS - PAGE_SHIFT)
  129. #define OBJ_INDEX_BITS (BITS_PER_LONG - _PFN_BITS)
  130. #define OBJ_INDEX_MASK ((_AC(1, UL) << OBJ_INDEX_BITS) - 1)
  131. #define MAX(a, b) ((a) >= (b) ? (a) : (b))
  132. /* ZS_MIN_ALLOC_SIZE must be multiple of ZS_ALIGN */
  133. #define ZS_MIN_ALLOC_SIZE \
  134. MAX(32, (ZS_MAX_PAGES_PER_ZSPAGE << PAGE_SHIFT >> OBJ_INDEX_BITS))
  135. #define ZS_MAX_ALLOC_SIZE PAGE_SIZE
  136. /*
  137. * On systems with 4K page size, this gives 255 size classes! There is a
  138. * trader-off here:
  139. * - Large number of size classes is potentially wasteful as free page are
  140. * spread across these classes
  141. * - Small number of size classes causes large internal fragmentation
  142. * - Probably its better to use specific size classes (empirically
  143. * determined). NOTE: all those class sizes must be set as multiple of
  144. * ZS_ALIGN to make sure link_free itself never has to span 2 pages.
  145. *
  146. * ZS_MIN_ALLOC_SIZE and ZS_SIZE_CLASS_DELTA must be multiple of ZS_ALIGN
  147. * (reason above)
  148. */
  149. #define ZS_SIZE_CLASS_DELTA (PAGE_SIZE >> 8)
  150. /*
  151. * We do not maintain any list for completely empty or full pages
  152. */
  153. enum fullness_group {
  154. ZS_ALMOST_FULL,
  155. ZS_ALMOST_EMPTY,
  156. _ZS_NR_FULLNESS_GROUPS,
  157. ZS_EMPTY,
  158. ZS_FULL
  159. };
  160. enum zs_stat_type {
  161. OBJ_ALLOCATED,
  162. OBJ_USED,
  163. NR_ZS_STAT_TYPE,
  164. };
  165. #ifdef CONFIG_ZSMALLOC_STAT
  166. static struct dentry *zs_stat_root;
  167. struct zs_size_stat {
  168. unsigned long objs[NR_ZS_STAT_TYPE];
  169. };
  170. #endif
  171. /*
  172. * number of size_classes
  173. */
  174. static int zs_size_classes;
  175. /*
  176. * We assign a page to ZS_ALMOST_EMPTY fullness group when:
  177. * n <= N / f, where
  178. * n = number of allocated objects
  179. * N = total number of objects zspage can store
  180. * f = fullness_threshold_frac
  181. *
  182. * Similarly, we assign zspage to:
  183. * ZS_ALMOST_FULL when n > N / f
  184. * ZS_EMPTY when n == 0
  185. * ZS_FULL when n == N
  186. *
  187. * (see: fix_fullness_group())
  188. */
  189. static const int fullness_threshold_frac = 4;
  190. struct size_class {
  191. /*
  192. * Size of objects stored in this class. Must be multiple
  193. * of ZS_ALIGN.
  194. */
  195. int size;
  196. unsigned int index;
  197. /* Number of PAGE_SIZE sized pages to combine to form a 'zspage' */
  198. int pages_per_zspage;
  199. #ifdef CONFIG_ZSMALLOC_STAT
  200. struct zs_size_stat stats;
  201. #endif
  202. spinlock_t lock;
  203. struct page *fullness_list[_ZS_NR_FULLNESS_GROUPS];
  204. };
  205. /*
  206. * Placed within free objects to form a singly linked list.
  207. * For every zspage, first_page->freelist gives head of this list.
  208. *
  209. * This must be power of 2 and less than or equal to ZS_ALIGN
  210. */
  211. struct link_free {
  212. /* Handle of next free chunk (encodes <PFN, obj_idx>) */
  213. void *next;
  214. };
  215. struct zs_pool {
  216. char *name;
  217. struct size_class **size_class;
  218. gfp_t flags; /* allocation flags used when growing pool */
  219. atomic_long_t pages_allocated;
  220. #ifdef CONFIG_ZSMALLOC_STAT
  221. struct dentry *stat_dentry;
  222. #endif
  223. };
  224. /*
  225. * A zspage's class index and fullness group
  226. * are encoded in its (first)page->mapping
  227. */
  228. #define CLASS_IDX_BITS 28
  229. #define FULLNESS_BITS 4
  230. #define CLASS_IDX_MASK ((1 << CLASS_IDX_BITS) - 1)
  231. #define FULLNESS_MASK ((1 << FULLNESS_BITS) - 1)
  232. struct mapping_area {
  233. #ifdef CONFIG_PGTABLE_MAPPING
  234. struct vm_struct *vm; /* vm area for mapping object that span pages */
  235. #else
  236. char *vm_buf; /* copy buffer for objects that span pages */
  237. #endif
  238. char *vm_addr; /* address of kmap_atomic()'ed pages */
  239. enum zs_mapmode vm_mm; /* mapping mode */
  240. };
  241. /* zpool driver */
  242. #ifdef CONFIG_ZPOOL
  243. static void *zs_zpool_create(char *name, gfp_t gfp, struct zpool_ops *zpool_ops)
  244. {
  245. return zs_create_pool(name, gfp);
  246. }
  247. static void zs_zpool_destroy(void *pool)
  248. {
  249. zs_destroy_pool(pool);
  250. }
  251. static int zs_zpool_malloc(void *pool, size_t size, gfp_t gfp,
  252. unsigned long *handle)
  253. {
  254. *handle = zs_malloc(pool, size);
  255. return *handle ? 0 : -1;
  256. }
  257. static void zs_zpool_free(void *pool, unsigned long handle)
  258. {
  259. zs_free(pool, handle);
  260. }
  261. static int zs_zpool_shrink(void *pool, unsigned int pages,
  262. unsigned int *reclaimed)
  263. {
  264. return -EINVAL;
  265. }
  266. static void *zs_zpool_map(void *pool, unsigned long handle,
  267. enum zpool_mapmode mm)
  268. {
  269. enum zs_mapmode zs_mm;
  270. switch (mm) {
  271. case ZPOOL_MM_RO:
  272. zs_mm = ZS_MM_RO;
  273. break;
  274. case ZPOOL_MM_WO:
  275. zs_mm = ZS_MM_WO;
  276. break;
  277. case ZPOOL_MM_RW: /* fallthru */
  278. default:
  279. zs_mm = ZS_MM_RW;
  280. break;
  281. }
  282. return zs_map_object(pool, handle, zs_mm);
  283. }
  284. static void zs_zpool_unmap(void *pool, unsigned long handle)
  285. {
  286. zs_unmap_object(pool, handle);
  287. }
  288. static u64 zs_zpool_total_size(void *pool)
  289. {
  290. return zs_get_total_pages(pool) << PAGE_SHIFT;
  291. }
  292. static struct zpool_driver zs_zpool_driver = {
  293. .type = "zsmalloc",
  294. .owner = THIS_MODULE,
  295. .create = zs_zpool_create,
  296. .destroy = zs_zpool_destroy,
  297. .malloc = zs_zpool_malloc,
  298. .free = zs_zpool_free,
  299. .shrink = zs_zpool_shrink,
  300. .map = zs_zpool_map,
  301. .unmap = zs_zpool_unmap,
  302. .total_size = zs_zpool_total_size,
  303. };
  304. MODULE_ALIAS("zpool-zsmalloc");
  305. #endif /* CONFIG_ZPOOL */
  306. /* per-cpu VM mapping areas for zspage accesses that cross page boundaries */
  307. static DEFINE_PER_CPU(struct mapping_area, zs_map_area);
  308. static int is_first_page(struct page *page)
  309. {
  310. return PagePrivate(page);
  311. }
  312. static int is_last_page(struct page *page)
  313. {
  314. return PagePrivate2(page);
  315. }
  316. static void get_zspage_mapping(struct page *page, unsigned int *class_idx,
  317. enum fullness_group *fullness)
  318. {
  319. unsigned long m;
  320. BUG_ON(!is_first_page(page));
  321. m = (unsigned long)page->mapping;
  322. *fullness = m & FULLNESS_MASK;
  323. *class_idx = (m >> FULLNESS_BITS) & CLASS_IDX_MASK;
  324. }
  325. static void set_zspage_mapping(struct page *page, unsigned int class_idx,
  326. enum fullness_group fullness)
  327. {
  328. unsigned long m;
  329. BUG_ON(!is_first_page(page));
  330. m = ((class_idx & CLASS_IDX_MASK) << FULLNESS_BITS) |
  331. (fullness & FULLNESS_MASK);
  332. page->mapping = (struct address_space *)m;
  333. }
  334. /*
  335. * zsmalloc divides the pool into various size classes where each
  336. * class maintains a list of zspages where each zspage is divided
  337. * into equal sized chunks. Each allocation falls into one of these
  338. * classes depending on its size. This function returns index of the
  339. * size class which has chunk size big enough to hold the give size.
  340. */
  341. static int get_size_class_index(int size)
  342. {
  343. int idx = 0;
  344. if (likely(size > ZS_MIN_ALLOC_SIZE))
  345. idx = DIV_ROUND_UP(size - ZS_MIN_ALLOC_SIZE,
  346. ZS_SIZE_CLASS_DELTA);
  347. return idx;
  348. }
  349. /*
  350. * For each size class, zspages are divided into different groups
  351. * depending on how "full" they are. This was done so that we could
  352. * easily find empty or nearly empty zspages when we try to shrink
  353. * the pool (not yet implemented). This function returns fullness
  354. * status of the given page.
  355. */
  356. static enum fullness_group get_fullness_group(struct page *page)
  357. {
  358. int inuse, max_objects;
  359. enum fullness_group fg;
  360. BUG_ON(!is_first_page(page));
  361. inuse = page->inuse;
  362. max_objects = page->objects;
  363. if (inuse == 0)
  364. fg = ZS_EMPTY;
  365. else if (inuse == max_objects)
  366. fg = ZS_FULL;
  367. else if (inuse <= max_objects / fullness_threshold_frac)
  368. fg = ZS_ALMOST_EMPTY;
  369. else
  370. fg = ZS_ALMOST_FULL;
  371. return fg;
  372. }
  373. /*
  374. * Each size class maintains various freelists and zspages are assigned
  375. * to one of these freelists based on the number of live objects they
  376. * have. This functions inserts the given zspage into the freelist
  377. * identified by <class, fullness_group>.
  378. */
  379. static void insert_zspage(struct page *page, struct size_class *class,
  380. enum fullness_group fullness)
  381. {
  382. struct page **head;
  383. BUG_ON(!is_first_page(page));
  384. if (fullness >= _ZS_NR_FULLNESS_GROUPS)
  385. return;
  386. head = &class->fullness_list[fullness];
  387. if (*head)
  388. list_add_tail(&page->lru, &(*head)->lru);
  389. *head = page;
  390. }
  391. /*
  392. * This function removes the given zspage from the freelist identified
  393. * by <class, fullness_group>.
  394. */
  395. static void remove_zspage(struct page *page, struct size_class *class,
  396. enum fullness_group fullness)
  397. {
  398. struct page **head;
  399. BUG_ON(!is_first_page(page));
  400. if (fullness >= _ZS_NR_FULLNESS_GROUPS)
  401. return;
  402. head = &class->fullness_list[fullness];
  403. BUG_ON(!*head);
  404. if (list_empty(&(*head)->lru))
  405. *head = NULL;
  406. else if (*head == page)
  407. *head = (struct page *)list_entry((*head)->lru.next,
  408. struct page, lru);
  409. list_del_init(&page->lru);
  410. }
  411. /*
  412. * Each size class maintains zspages in different fullness groups depending
  413. * on the number of live objects they contain. When allocating or freeing
  414. * objects, the fullness status of the page can change, say, from ALMOST_FULL
  415. * to ALMOST_EMPTY when freeing an object. This function checks if such
  416. * a status change has occurred for the given page and accordingly moves the
  417. * page from the freelist of the old fullness group to that of the new
  418. * fullness group.
  419. */
  420. static enum fullness_group fix_fullness_group(struct zs_pool *pool,
  421. struct page *page)
  422. {
  423. int class_idx;
  424. struct size_class *class;
  425. enum fullness_group currfg, newfg;
  426. BUG_ON(!is_first_page(page));
  427. get_zspage_mapping(page, &class_idx, &currfg);
  428. newfg = get_fullness_group(page);
  429. if (newfg == currfg)
  430. goto out;
  431. class = pool->size_class[class_idx];
  432. remove_zspage(page, class, currfg);
  433. insert_zspage(page, class, newfg);
  434. set_zspage_mapping(page, class_idx, newfg);
  435. out:
  436. return newfg;
  437. }
  438. /*
  439. * We have to decide on how many pages to link together
  440. * to form a zspage for each size class. This is important
  441. * to reduce wastage due to unusable space left at end of
  442. * each zspage which is given as:
  443. * wastage = Zp - Zp % size_class
  444. * where Zp = zspage size = k * PAGE_SIZE where k = 1, 2, ...
  445. *
  446. * For example, for size class of 3/8 * PAGE_SIZE, we should
  447. * link together 3 PAGE_SIZE sized pages to form a zspage
  448. * since then we can perfectly fit in 8 such objects.
  449. */
  450. static int get_pages_per_zspage(int class_size)
  451. {
  452. int i, max_usedpc = 0;
  453. /* zspage order which gives maximum used size per KB */
  454. int max_usedpc_order = 1;
  455. for (i = 1; i <= ZS_MAX_PAGES_PER_ZSPAGE; i++) {
  456. int zspage_size;
  457. int waste, usedpc;
  458. zspage_size = i * PAGE_SIZE;
  459. waste = zspage_size % class_size;
  460. usedpc = (zspage_size - waste) * 100 / zspage_size;
  461. if (usedpc > max_usedpc) {
  462. max_usedpc = usedpc;
  463. max_usedpc_order = i;
  464. }
  465. }
  466. return max_usedpc_order;
  467. }
  468. /*
  469. * A single 'zspage' is composed of many system pages which are
  470. * linked together using fields in struct page. This function finds
  471. * the first/head page, given any component page of a zspage.
  472. */
  473. static struct page *get_first_page(struct page *page)
  474. {
  475. if (is_first_page(page))
  476. return page;
  477. else
  478. return page->first_page;
  479. }
  480. static struct page *get_next_page(struct page *page)
  481. {
  482. struct page *next;
  483. if (is_last_page(page))
  484. next = NULL;
  485. else if (is_first_page(page))
  486. next = (struct page *)page_private(page);
  487. else
  488. next = list_entry(page->lru.next, struct page, lru);
  489. return next;
  490. }
  491. /*
  492. * Encode <page, obj_idx> as a single handle value.
  493. * On hardware platforms with physical memory starting at 0x0 the pfn
  494. * could be 0 so we ensure that the handle will never be 0 by adjusting the
  495. * encoded obj_idx value before encoding.
  496. */
  497. static void *obj_location_to_handle(struct page *page, unsigned long obj_idx)
  498. {
  499. unsigned long handle;
  500. if (!page) {
  501. BUG_ON(obj_idx);
  502. return NULL;
  503. }
  504. handle = page_to_pfn(page) << OBJ_INDEX_BITS;
  505. handle |= ((obj_idx + 1) & OBJ_INDEX_MASK);
  506. return (void *)handle;
  507. }
  508. /*
  509. * Decode <page, obj_idx> pair from the given object handle. We adjust the
  510. * decoded obj_idx back to its original value since it was adjusted in
  511. * obj_location_to_handle().
  512. */
  513. static void obj_handle_to_location(unsigned long handle, struct page **page,
  514. unsigned long *obj_idx)
  515. {
  516. *page = pfn_to_page(handle >> OBJ_INDEX_BITS);
  517. *obj_idx = (handle & OBJ_INDEX_MASK) - 1;
  518. }
  519. static unsigned long obj_idx_to_offset(struct page *page,
  520. unsigned long obj_idx, int class_size)
  521. {
  522. unsigned long off = 0;
  523. if (!is_first_page(page))
  524. off = page->index;
  525. return off + obj_idx * class_size;
  526. }
  527. static void reset_page(struct page *page)
  528. {
  529. clear_bit(PG_private, &page->flags);
  530. clear_bit(PG_private_2, &page->flags);
  531. set_page_private(page, 0);
  532. page->mapping = NULL;
  533. page->freelist = NULL;
  534. page_mapcount_reset(page);
  535. }
  536. static void free_zspage(struct page *first_page)
  537. {
  538. struct page *nextp, *tmp, *head_extra;
  539. BUG_ON(!is_first_page(first_page));
  540. BUG_ON(first_page->inuse);
  541. head_extra = (struct page *)page_private(first_page);
  542. reset_page(first_page);
  543. __free_page(first_page);
  544. /* zspage with only 1 system page */
  545. if (!head_extra)
  546. return;
  547. list_for_each_entry_safe(nextp, tmp, &head_extra->lru, lru) {
  548. list_del(&nextp->lru);
  549. reset_page(nextp);
  550. __free_page(nextp);
  551. }
  552. reset_page(head_extra);
  553. __free_page(head_extra);
  554. }
  555. /* Initialize a newly allocated zspage */
  556. static void init_zspage(struct page *first_page, struct size_class *class)
  557. {
  558. unsigned long off = 0;
  559. struct page *page = first_page;
  560. BUG_ON(!is_first_page(first_page));
  561. while (page) {
  562. struct page *next_page;
  563. struct link_free *link;
  564. unsigned int i = 1;
  565. void *vaddr;
  566. /*
  567. * page->index stores offset of first object starting
  568. * in the page. For the first page, this is always 0,
  569. * so we use first_page->index (aka ->freelist) to store
  570. * head of corresponding zspage's freelist.
  571. */
  572. if (page != first_page)
  573. page->index = off;
  574. vaddr = kmap_atomic(page);
  575. link = (struct link_free *)vaddr + off / sizeof(*link);
  576. while ((off += class->size) < PAGE_SIZE) {
  577. link->next = obj_location_to_handle(page, i++);
  578. link += class->size / sizeof(*link);
  579. }
  580. /*
  581. * We now come to the last (full or partial) object on this
  582. * page, which must point to the first object on the next
  583. * page (if present)
  584. */
  585. next_page = get_next_page(page);
  586. link->next = obj_location_to_handle(next_page, 0);
  587. kunmap_atomic(vaddr);
  588. page = next_page;
  589. off %= PAGE_SIZE;
  590. }
  591. }
  592. /*
  593. * Allocate a zspage for the given size class
  594. */
  595. static struct page *alloc_zspage(struct size_class *class, gfp_t flags)
  596. {
  597. int i, error;
  598. struct page *first_page = NULL, *uninitialized_var(prev_page);
  599. /*
  600. * Allocate individual pages and link them together as:
  601. * 1. first page->private = first sub-page
  602. * 2. all sub-pages are linked together using page->lru
  603. * 3. each sub-page is linked to the first page using page->first_page
  604. *
  605. * For each size class, First/Head pages are linked together using
  606. * page->lru. Also, we set PG_private to identify the first page
  607. * (i.e. no other sub-page has this flag set) and PG_private_2 to
  608. * identify the last page.
  609. */
  610. error = -ENOMEM;
  611. for (i = 0; i < class->pages_per_zspage; i++) {
  612. struct page *page;
  613. page = alloc_page(flags);
  614. if (!page)
  615. goto cleanup;
  616. INIT_LIST_HEAD(&page->lru);
  617. if (i == 0) { /* first page */
  618. SetPagePrivate(page);
  619. set_page_private(page, 0);
  620. first_page = page;
  621. first_page->inuse = 0;
  622. }
  623. if (i == 1)
  624. set_page_private(first_page, (unsigned long)page);
  625. if (i >= 1)
  626. page->first_page = first_page;
  627. if (i >= 2)
  628. list_add(&page->lru, &prev_page->lru);
  629. if (i == class->pages_per_zspage - 1) /* last page */
  630. SetPagePrivate2(page);
  631. prev_page = page;
  632. }
  633. init_zspage(first_page, class);
  634. first_page->freelist = obj_location_to_handle(first_page, 0);
  635. /* Maximum number of objects we can store in this zspage */
  636. first_page->objects = class->pages_per_zspage * PAGE_SIZE / class->size;
  637. error = 0; /* Success */
  638. cleanup:
  639. if (unlikely(error) && first_page) {
  640. free_zspage(first_page);
  641. first_page = NULL;
  642. }
  643. return first_page;
  644. }
  645. static struct page *find_get_zspage(struct size_class *class)
  646. {
  647. int i;
  648. struct page *page;
  649. for (i = 0; i < _ZS_NR_FULLNESS_GROUPS; i++) {
  650. page = class->fullness_list[i];
  651. if (page)
  652. break;
  653. }
  654. return page;
  655. }
  656. #ifdef CONFIG_PGTABLE_MAPPING
  657. static inline int __zs_cpu_up(struct mapping_area *area)
  658. {
  659. /*
  660. * Make sure we don't leak memory if a cpu UP notification
  661. * and zs_init() race and both call zs_cpu_up() on the same cpu
  662. */
  663. if (area->vm)
  664. return 0;
  665. area->vm = alloc_vm_area(PAGE_SIZE * 2, NULL);
  666. if (!area->vm)
  667. return -ENOMEM;
  668. return 0;
  669. }
  670. static inline void __zs_cpu_down(struct mapping_area *area)
  671. {
  672. if (area->vm)
  673. free_vm_area(area->vm);
  674. area->vm = NULL;
  675. }
  676. static inline void *__zs_map_object(struct mapping_area *area,
  677. struct page *pages[2], int off, int size)
  678. {
  679. BUG_ON(map_vm_area(area->vm, PAGE_KERNEL, pages));
  680. area->vm_addr = area->vm->addr;
  681. return area->vm_addr + off;
  682. }
  683. static inline void __zs_unmap_object(struct mapping_area *area,
  684. struct page *pages[2], int off, int size)
  685. {
  686. unsigned long addr = (unsigned long)area->vm_addr;
  687. unmap_kernel_range(addr, PAGE_SIZE * 2);
  688. }
  689. #else /* CONFIG_PGTABLE_MAPPING */
  690. static inline int __zs_cpu_up(struct mapping_area *area)
  691. {
  692. /*
  693. * Make sure we don't leak memory if a cpu UP notification
  694. * and zs_init() race and both call zs_cpu_up() on the same cpu
  695. */
  696. if (area->vm_buf)
  697. return 0;
  698. area->vm_buf = kmalloc(ZS_MAX_ALLOC_SIZE, GFP_KERNEL);
  699. if (!area->vm_buf)
  700. return -ENOMEM;
  701. return 0;
  702. }
  703. static inline void __zs_cpu_down(struct mapping_area *area)
  704. {
  705. kfree(area->vm_buf);
  706. area->vm_buf = NULL;
  707. }
  708. static void *__zs_map_object(struct mapping_area *area,
  709. struct page *pages[2], int off, int size)
  710. {
  711. int sizes[2];
  712. void *addr;
  713. char *buf = area->vm_buf;
  714. /* disable page faults to match kmap_atomic() return conditions */
  715. pagefault_disable();
  716. /* no read fastpath */
  717. if (area->vm_mm == ZS_MM_WO)
  718. goto out;
  719. sizes[0] = PAGE_SIZE - off;
  720. sizes[1] = size - sizes[0];
  721. /* copy object to per-cpu buffer */
  722. addr = kmap_atomic(pages[0]);
  723. memcpy(buf, addr + off, sizes[0]);
  724. kunmap_atomic(addr);
  725. addr = kmap_atomic(pages[1]);
  726. memcpy(buf + sizes[0], addr, sizes[1]);
  727. kunmap_atomic(addr);
  728. out:
  729. return area->vm_buf;
  730. }
  731. static void __zs_unmap_object(struct mapping_area *area,
  732. struct page *pages[2], int off, int size)
  733. {
  734. int sizes[2];
  735. void *addr;
  736. char *buf = area->vm_buf;
  737. /* no write fastpath */
  738. if (area->vm_mm == ZS_MM_RO)
  739. goto out;
  740. sizes[0] = PAGE_SIZE - off;
  741. sizes[1] = size - sizes[0];
  742. /* copy per-cpu buffer to object */
  743. addr = kmap_atomic(pages[0]);
  744. memcpy(addr + off, buf, sizes[0]);
  745. kunmap_atomic(addr);
  746. addr = kmap_atomic(pages[1]);
  747. memcpy(addr, buf + sizes[0], sizes[1]);
  748. kunmap_atomic(addr);
  749. out:
  750. /* enable page faults to match kunmap_atomic() return conditions */
  751. pagefault_enable();
  752. }
  753. #endif /* CONFIG_PGTABLE_MAPPING */
  754. static int zs_cpu_notifier(struct notifier_block *nb, unsigned long action,
  755. void *pcpu)
  756. {
  757. int ret, cpu = (long)pcpu;
  758. struct mapping_area *area;
  759. switch (action) {
  760. case CPU_UP_PREPARE:
  761. area = &per_cpu(zs_map_area, cpu);
  762. ret = __zs_cpu_up(area);
  763. if (ret)
  764. return notifier_from_errno(ret);
  765. break;
  766. case CPU_DEAD:
  767. case CPU_UP_CANCELED:
  768. area = &per_cpu(zs_map_area, cpu);
  769. __zs_cpu_down(area);
  770. break;
  771. }
  772. return NOTIFY_OK;
  773. }
  774. static struct notifier_block zs_cpu_nb = {
  775. .notifier_call = zs_cpu_notifier
  776. };
  777. static int zs_register_cpu_notifier(void)
  778. {
  779. int cpu, uninitialized_var(ret);
  780. cpu_notifier_register_begin();
  781. __register_cpu_notifier(&zs_cpu_nb);
  782. for_each_online_cpu(cpu) {
  783. ret = zs_cpu_notifier(NULL, CPU_UP_PREPARE, (void *)(long)cpu);
  784. if (notifier_to_errno(ret))
  785. break;
  786. }
  787. cpu_notifier_register_done();
  788. return notifier_to_errno(ret);
  789. }
  790. static void zs_unregister_cpu_notifier(void)
  791. {
  792. int cpu;
  793. cpu_notifier_register_begin();
  794. for_each_online_cpu(cpu)
  795. zs_cpu_notifier(NULL, CPU_DEAD, (void *)(long)cpu);
  796. __unregister_cpu_notifier(&zs_cpu_nb);
  797. cpu_notifier_register_done();
  798. }
  799. static void init_zs_size_classes(void)
  800. {
  801. int nr;
  802. nr = (ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE) / ZS_SIZE_CLASS_DELTA + 1;
  803. if ((ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE) % ZS_SIZE_CLASS_DELTA)
  804. nr += 1;
  805. zs_size_classes = nr;
  806. }
  807. static unsigned int get_maxobj_per_zspage(int size, int pages_per_zspage)
  808. {
  809. return pages_per_zspage * PAGE_SIZE / size;
  810. }
  811. static bool can_merge(struct size_class *prev, int size, int pages_per_zspage)
  812. {
  813. if (prev->pages_per_zspage != pages_per_zspage)
  814. return false;
  815. if (get_maxobj_per_zspage(prev->size, prev->pages_per_zspage)
  816. != get_maxobj_per_zspage(size, pages_per_zspage))
  817. return false;
  818. return true;
  819. }
  820. #ifdef CONFIG_ZSMALLOC_STAT
  821. static inline void zs_stat_inc(struct size_class *class,
  822. enum zs_stat_type type, unsigned long cnt)
  823. {
  824. class->stats.objs[type] += cnt;
  825. }
  826. static inline void zs_stat_dec(struct size_class *class,
  827. enum zs_stat_type type, unsigned long cnt)
  828. {
  829. class->stats.objs[type] -= cnt;
  830. }
  831. static inline unsigned long zs_stat_get(struct size_class *class,
  832. enum zs_stat_type type)
  833. {
  834. return class->stats.objs[type];
  835. }
  836. static int __init zs_stat_init(void)
  837. {
  838. if (!debugfs_initialized())
  839. return -ENODEV;
  840. zs_stat_root = debugfs_create_dir("zsmalloc", NULL);
  841. if (!zs_stat_root)
  842. return -ENOMEM;
  843. return 0;
  844. }
  845. static void __exit zs_stat_exit(void)
  846. {
  847. debugfs_remove_recursive(zs_stat_root);
  848. }
  849. static int zs_stats_size_show(struct seq_file *s, void *v)
  850. {
  851. int i;
  852. struct zs_pool *pool = s->private;
  853. struct size_class *class;
  854. int objs_per_zspage;
  855. unsigned long obj_allocated, obj_used, pages_used;
  856. unsigned long total_objs = 0, total_used_objs = 0, total_pages = 0;
  857. seq_printf(s, " %5s %5s %13s %10s %10s\n", "class", "size",
  858. "obj_allocated", "obj_used", "pages_used");
  859. for (i = 0; i < zs_size_classes; i++) {
  860. class = pool->size_class[i];
  861. if (class->index != i)
  862. continue;
  863. spin_lock(&class->lock);
  864. obj_allocated = zs_stat_get(class, OBJ_ALLOCATED);
  865. obj_used = zs_stat_get(class, OBJ_USED);
  866. spin_unlock(&class->lock);
  867. objs_per_zspage = get_maxobj_per_zspage(class->size,
  868. class->pages_per_zspage);
  869. pages_used = obj_allocated / objs_per_zspage *
  870. class->pages_per_zspage;
  871. seq_printf(s, " %5u %5u %10lu %10lu %10lu\n", i,
  872. class->size, obj_allocated, obj_used, pages_used);
  873. total_objs += obj_allocated;
  874. total_used_objs += obj_used;
  875. total_pages += pages_used;
  876. }
  877. seq_puts(s, "\n");
  878. seq_printf(s, " %5s %5s %10lu %10lu %10lu\n", "Total", "",
  879. total_objs, total_used_objs, total_pages);
  880. return 0;
  881. }
  882. static int zs_stats_size_open(struct inode *inode, struct file *file)
  883. {
  884. return single_open(file, zs_stats_size_show, inode->i_private);
  885. }
  886. static const struct file_operations zs_stat_size_ops = {
  887. .open = zs_stats_size_open,
  888. .read = seq_read,
  889. .llseek = seq_lseek,
  890. .release = single_release,
  891. };
  892. static int zs_pool_stat_create(char *name, struct zs_pool *pool)
  893. {
  894. struct dentry *entry;
  895. if (!zs_stat_root)
  896. return -ENODEV;
  897. entry = debugfs_create_dir(name, zs_stat_root);
  898. if (!entry) {
  899. pr_warn("debugfs dir <%s> creation failed\n", name);
  900. return -ENOMEM;
  901. }
  902. pool->stat_dentry = entry;
  903. entry = debugfs_create_file("obj_in_classes", S_IFREG | S_IRUGO,
  904. pool->stat_dentry, pool, &zs_stat_size_ops);
  905. if (!entry) {
  906. pr_warn("%s: debugfs file entry <%s> creation failed\n",
  907. name, "obj_in_classes");
  908. return -ENOMEM;
  909. }
  910. return 0;
  911. }
  912. static void zs_pool_stat_destroy(struct zs_pool *pool)
  913. {
  914. debugfs_remove_recursive(pool->stat_dentry);
  915. }
  916. #else /* CONFIG_ZSMALLOC_STAT */
  917. static inline void zs_stat_inc(struct size_class *class,
  918. enum zs_stat_type type, unsigned long cnt)
  919. {
  920. }
  921. static inline void zs_stat_dec(struct size_class *class,
  922. enum zs_stat_type type, unsigned long cnt)
  923. {
  924. }
  925. static inline unsigned long zs_stat_get(struct size_class *class,
  926. enum zs_stat_type type)
  927. {
  928. return 0;
  929. }
  930. static int __init zs_stat_init(void)
  931. {
  932. return 0;
  933. }
  934. static void __exit zs_stat_exit(void)
  935. {
  936. }
  937. static inline int zs_pool_stat_create(char *name, struct zs_pool *pool)
  938. {
  939. return 0;
  940. }
  941. static inline void zs_pool_stat_destroy(struct zs_pool *pool)
  942. {
  943. }
  944. #endif
  945. unsigned long zs_get_total_pages(struct zs_pool *pool)
  946. {
  947. return atomic_long_read(&pool->pages_allocated);
  948. }
  949. EXPORT_SYMBOL_GPL(zs_get_total_pages);
  950. /**
  951. * zs_map_object - get address of allocated object from handle.
  952. * @pool: pool from which the object was allocated
  953. * @handle: handle returned from zs_malloc
  954. *
  955. * Before using an object allocated from zs_malloc, it must be mapped using
  956. * this function. When done with the object, it must be unmapped using
  957. * zs_unmap_object.
  958. *
  959. * Only one object can be mapped per cpu at a time. There is no protection
  960. * against nested mappings.
  961. *
  962. * This function returns with preemption and page faults disabled.
  963. */
  964. void *zs_map_object(struct zs_pool *pool, unsigned long handle,
  965. enum zs_mapmode mm)
  966. {
  967. struct page *page;
  968. unsigned long obj_idx, off;
  969. unsigned int class_idx;
  970. enum fullness_group fg;
  971. struct size_class *class;
  972. struct mapping_area *area;
  973. struct page *pages[2];
  974. BUG_ON(!handle);
  975. /*
  976. * Because we use per-cpu mapping areas shared among the
  977. * pools/users, we can't allow mapping in interrupt context
  978. * because it can corrupt another users mappings.
  979. */
  980. BUG_ON(in_interrupt());
  981. obj_handle_to_location(handle, &page, &obj_idx);
  982. get_zspage_mapping(get_first_page(page), &class_idx, &fg);
  983. class = pool->size_class[class_idx];
  984. off = obj_idx_to_offset(page, obj_idx, class->size);
  985. area = &get_cpu_var(zs_map_area);
  986. area->vm_mm = mm;
  987. if (off + class->size <= PAGE_SIZE) {
  988. /* this object is contained entirely within a page */
  989. area->vm_addr = kmap_atomic(page);
  990. return area->vm_addr + off;
  991. }
  992. /* this object spans two pages */
  993. pages[0] = page;
  994. pages[1] = get_next_page(page);
  995. BUG_ON(!pages[1]);
  996. return __zs_map_object(area, pages, off, class->size);
  997. }
  998. EXPORT_SYMBOL_GPL(zs_map_object);
  999. void zs_unmap_object(struct zs_pool *pool, unsigned long handle)
  1000. {
  1001. struct page *page;
  1002. unsigned long obj_idx, off;
  1003. unsigned int class_idx;
  1004. enum fullness_group fg;
  1005. struct size_class *class;
  1006. struct mapping_area *area;
  1007. BUG_ON(!handle);
  1008. obj_handle_to_location(handle, &page, &obj_idx);
  1009. get_zspage_mapping(get_first_page(page), &class_idx, &fg);
  1010. class = pool->size_class[class_idx];
  1011. off = obj_idx_to_offset(page, obj_idx, class->size);
  1012. area = this_cpu_ptr(&zs_map_area);
  1013. if (off + class->size <= PAGE_SIZE)
  1014. kunmap_atomic(area->vm_addr);
  1015. else {
  1016. struct page *pages[2];
  1017. pages[0] = page;
  1018. pages[1] = get_next_page(page);
  1019. BUG_ON(!pages[1]);
  1020. __zs_unmap_object(area, pages, off, class->size);
  1021. }
  1022. put_cpu_var(zs_map_area);
  1023. }
  1024. EXPORT_SYMBOL_GPL(zs_unmap_object);
  1025. /**
  1026. * zs_malloc - Allocate block of given size from pool.
  1027. * @pool: pool to allocate from
  1028. * @size: size of block to allocate
  1029. *
  1030. * On success, handle to the allocated object is returned,
  1031. * otherwise 0.
  1032. * Allocation requests with size > ZS_MAX_ALLOC_SIZE will fail.
  1033. */
  1034. unsigned long zs_malloc(struct zs_pool *pool, size_t size)
  1035. {
  1036. unsigned long obj;
  1037. struct link_free *link;
  1038. struct size_class *class;
  1039. void *vaddr;
  1040. struct page *first_page, *m_page;
  1041. unsigned long m_objidx, m_offset;
  1042. if (unlikely(!size || size > ZS_MAX_ALLOC_SIZE))
  1043. return 0;
  1044. class = pool->size_class[get_size_class_index(size)];
  1045. spin_lock(&class->lock);
  1046. first_page = find_get_zspage(class);
  1047. if (!first_page) {
  1048. spin_unlock(&class->lock);
  1049. first_page = alloc_zspage(class, pool->flags);
  1050. if (unlikely(!first_page))
  1051. return 0;
  1052. set_zspage_mapping(first_page, class->index, ZS_EMPTY);
  1053. atomic_long_add(class->pages_per_zspage,
  1054. &pool->pages_allocated);
  1055. spin_lock(&class->lock);
  1056. zs_stat_inc(class, OBJ_ALLOCATED, get_maxobj_per_zspage(
  1057. class->size, class->pages_per_zspage));
  1058. }
  1059. obj = (unsigned long)first_page->freelist;
  1060. obj_handle_to_location(obj, &m_page, &m_objidx);
  1061. m_offset = obj_idx_to_offset(m_page, m_objidx, class->size);
  1062. vaddr = kmap_atomic(m_page);
  1063. link = (struct link_free *)vaddr + m_offset / sizeof(*link);
  1064. first_page->freelist = link->next;
  1065. memset(link, POISON_INUSE, sizeof(*link));
  1066. kunmap_atomic(vaddr);
  1067. first_page->inuse++;
  1068. zs_stat_inc(class, OBJ_USED, 1);
  1069. /* Now move the zspage to another fullness group, if required */
  1070. fix_fullness_group(pool, first_page);
  1071. spin_unlock(&class->lock);
  1072. return obj;
  1073. }
  1074. EXPORT_SYMBOL_GPL(zs_malloc);
  1075. void zs_free(struct zs_pool *pool, unsigned long obj)
  1076. {
  1077. struct link_free *link;
  1078. struct page *first_page, *f_page;
  1079. unsigned long f_objidx, f_offset;
  1080. void *vaddr;
  1081. int class_idx;
  1082. struct size_class *class;
  1083. enum fullness_group fullness;
  1084. if (unlikely(!obj))
  1085. return;
  1086. obj_handle_to_location(obj, &f_page, &f_objidx);
  1087. first_page = get_first_page(f_page);
  1088. get_zspage_mapping(first_page, &class_idx, &fullness);
  1089. class = pool->size_class[class_idx];
  1090. f_offset = obj_idx_to_offset(f_page, f_objidx, class->size);
  1091. spin_lock(&class->lock);
  1092. /* Insert this object in containing zspage's freelist */
  1093. vaddr = kmap_atomic(f_page);
  1094. link = (struct link_free *)(vaddr + f_offset);
  1095. link->next = first_page->freelist;
  1096. kunmap_atomic(vaddr);
  1097. first_page->freelist = (void *)obj;
  1098. first_page->inuse--;
  1099. fullness = fix_fullness_group(pool, first_page);
  1100. zs_stat_dec(class, OBJ_USED, 1);
  1101. if (fullness == ZS_EMPTY)
  1102. zs_stat_dec(class, OBJ_ALLOCATED, get_maxobj_per_zspage(
  1103. class->size, class->pages_per_zspage));
  1104. spin_unlock(&class->lock);
  1105. if (fullness == ZS_EMPTY) {
  1106. atomic_long_sub(class->pages_per_zspage,
  1107. &pool->pages_allocated);
  1108. free_zspage(first_page);
  1109. }
  1110. }
  1111. EXPORT_SYMBOL_GPL(zs_free);
  1112. /**
  1113. * zs_create_pool - Creates an allocation pool to work from.
  1114. * @flags: allocation flags used to allocate pool metadata
  1115. *
  1116. * This function must be called before anything when using
  1117. * the zsmalloc allocator.
  1118. *
  1119. * On success, a pointer to the newly created pool is returned,
  1120. * otherwise NULL.
  1121. */
  1122. struct zs_pool *zs_create_pool(char *name, gfp_t flags)
  1123. {
  1124. int i;
  1125. struct zs_pool *pool;
  1126. struct size_class *prev_class = NULL;
  1127. pool = kzalloc(sizeof(*pool), GFP_KERNEL);
  1128. if (!pool)
  1129. return NULL;
  1130. pool->name = kstrdup(name, GFP_KERNEL);
  1131. if (!pool->name) {
  1132. kfree(pool);
  1133. return NULL;
  1134. }
  1135. pool->size_class = kcalloc(zs_size_classes, sizeof(struct size_class *),
  1136. GFP_KERNEL);
  1137. if (!pool->size_class) {
  1138. kfree(pool->name);
  1139. kfree(pool);
  1140. return NULL;
  1141. }
  1142. /*
  1143. * Iterate reversly, because, size of size_class that we want to use
  1144. * for merging should be larger or equal to current size.
  1145. */
  1146. for (i = zs_size_classes - 1; i >= 0; i--) {
  1147. int size;
  1148. int pages_per_zspage;
  1149. struct size_class *class;
  1150. size = ZS_MIN_ALLOC_SIZE + i * ZS_SIZE_CLASS_DELTA;
  1151. if (size > ZS_MAX_ALLOC_SIZE)
  1152. size = ZS_MAX_ALLOC_SIZE;
  1153. pages_per_zspage = get_pages_per_zspage(size);
  1154. /*
  1155. * size_class is used for normal zsmalloc operation such
  1156. * as alloc/free for that size. Although it is natural that we
  1157. * have one size_class for each size, there is a chance that we
  1158. * can get more memory utilization if we use one size_class for
  1159. * many different sizes whose size_class have same
  1160. * characteristics. So, we makes size_class point to
  1161. * previous size_class if possible.
  1162. */
  1163. if (prev_class) {
  1164. if (can_merge(prev_class, size, pages_per_zspage)) {
  1165. pool->size_class[i] = prev_class;
  1166. continue;
  1167. }
  1168. }
  1169. class = kzalloc(sizeof(struct size_class), GFP_KERNEL);
  1170. if (!class)
  1171. goto err;
  1172. class->size = size;
  1173. class->index = i;
  1174. class->pages_per_zspage = pages_per_zspage;
  1175. spin_lock_init(&class->lock);
  1176. pool->size_class[i] = class;
  1177. prev_class = class;
  1178. }
  1179. pool->flags = flags;
  1180. if (zs_pool_stat_create(name, pool))
  1181. goto err;
  1182. return pool;
  1183. err:
  1184. zs_destroy_pool(pool);
  1185. return NULL;
  1186. }
  1187. EXPORT_SYMBOL_GPL(zs_create_pool);
  1188. void zs_destroy_pool(struct zs_pool *pool)
  1189. {
  1190. int i;
  1191. zs_pool_stat_destroy(pool);
  1192. for (i = 0; i < zs_size_classes; i++) {
  1193. int fg;
  1194. struct size_class *class = pool->size_class[i];
  1195. if (!class)
  1196. continue;
  1197. if (class->index != i)
  1198. continue;
  1199. for (fg = 0; fg < _ZS_NR_FULLNESS_GROUPS; fg++) {
  1200. if (class->fullness_list[fg]) {
  1201. pr_info("Freeing non-empty class with size %db, fullness group %d\n",
  1202. class->size, fg);
  1203. }
  1204. }
  1205. kfree(class);
  1206. }
  1207. kfree(pool->size_class);
  1208. kfree(pool->name);
  1209. kfree(pool);
  1210. }
  1211. EXPORT_SYMBOL_GPL(zs_destroy_pool);
  1212. static int __init zs_init(void)
  1213. {
  1214. int ret = zs_register_cpu_notifier();
  1215. if (ret)
  1216. goto notifier_fail;
  1217. init_zs_size_classes();
  1218. #ifdef CONFIG_ZPOOL
  1219. zpool_register_driver(&zs_zpool_driver);
  1220. #endif
  1221. ret = zs_stat_init();
  1222. if (ret) {
  1223. pr_err("zs stat initialization failed\n");
  1224. goto stat_fail;
  1225. }
  1226. return 0;
  1227. stat_fail:
  1228. #ifdef CONFIG_ZPOOL
  1229. zpool_unregister_driver(&zs_zpool_driver);
  1230. #endif
  1231. notifier_fail:
  1232. zs_unregister_cpu_notifier();
  1233. return ret;
  1234. }
  1235. static void __exit zs_exit(void)
  1236. {
  1237. #ifdef CONFIG_ZPOOL
  1238. zpool_unregister_driver(&zs_zpool_driver);
  1239. #endif
  1240. zs_unregister_cpu_notifier();
  1241. zs_stat_exit();
  1242. }
  1243. module_init(zs_init);
  1244. module_exit(zs_exit);
  1245. MODULE_LICENSE("Dual BSD/GPL");
  1246. MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");