zsmalloc.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  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 zcache and zram. Thus, the
  15. * allocator is supposed to work well under low memory conditions. In
  16. * particular, it never attempts higher order page allocation which is
  17. * very likely to fail under memory pressure. On the other hand, if we
  18. * just use single (0-order) pages, it would suffer from very high
  19. * fragmentation -- any object of size PAGE_SIZE/2 or larger would occupy
  20. * an entire page. This was one of the major issues with its predecessor
  21. * (xvmalloc).
  22. *
  23. * To overcome these issues, zsmalloc allocates a bunch of 0-order pages
  24. * and links them together using various 'struct page' fields. These linked
  25. * pages act as a single higher-order page i.e. an object can span 0-order
  26. * page boundaries. The code refers to these linked pages as a single entity
  27. * called zspage.
  28. *
  29. * Following is how we use various fields and flags of underlying
  30. * struct page(s) to form a zspage.
  31. *
  32. * Usage of struct page fields:
  33. * page->first_page: points to the first component (0-order) page
  34. * page->index (union with page->freelist): offset of the first object
  35. * starting in this page. For the first page, this is
  36. * always 0, so we use this field (aka freelist) to point
  37. * to the first free object in zspage.
  38. * page->lru: links together all component pages (except the first page)
  39. * of a zspage
  40. *
  41. * For _first_ page only:
  42. *
  43. * page->private (union with page->first_page): refers to the
  44. * component page after the first page
  45. * page->freelist: points to the first free object in zspage.
  46. * Free objects are linked together using in-place
  47. * metadata.
  48. * page->objects: maximum number of objects we can store in this
  49. * zspage (class->zspage_order * PAGE_SIZE / class->size)
  50. * page->lru: links together first pages of various zspages.
  51. * Basically forming list of zspages in a fullness group.
  52. * page->mapping: class index and fullness group of the zspage
  53. *
  54. * Usage of struct page flags:
  55. * PG_private: identifies the first component page
  56. * PG_private2: identifies the last component page
  57. *
  58. */
  59. #ifdef CONFIG_ZSMALLOC_DEBUG
  60. #define DEBUG
  61. #endif
  62. #include <linux/module.h>
  63. #include <linux/kernel.h>
  64. #include <linux/bitops.h>
  65. #include <linux/errno.h>
  66. #include <linux/highmem.h>
  67. #include <linux/init.h>
  68. #include <linux/string.h>
  69. #include <linux/slab.h>
  70. #include <asm/tlbflush.h>
  71. #include <asm/pgtable.h>
  72. #include <linux/cpumask.h>
  73. #include <linux/cpu.h>
  74. #include <linux/vmalloc.h>
  75. #include <linux/hardirq.h>
  76. #include <linux/spinlock.h>
  77. #include <linux/types.h>
  78. #include <linux/zsmalloc.h>
  79. /*
  80. * This must be power of 2 and greater than of equal to sizeof(link_free).
  81. * These two conditions ensure that any 'struct link_free' itself doesn't
  82. * span more than 1 page which avoids complex case of mapping 2 pages simply
  83. * to restore link_free pointer values.
  84. */
  85. #define ZS_ALIGN 8
  86. /*
  87. * A single 'zspage' is composed of up to 2^N discontiguous 0-order (single)
  88. * pages. ZS_MAX_ZSPAGE_ORDER defines upper limit on N.
  89. */
  90. #define ZS_MAX_ZSPAGE_ORDER 2
  91. #define ZS_MAX_PAGES_PER_ZSPAGE (_AC(1, UL) << ZS_MAX_ZSPAGE_ORDER)
  92. /*
  93. * Object location (<PFN>, <obj_idx>) is encoded as
  94. * as single (void *) handle value.
  95. *
  96. * Note that object index <obj_idx> is relative to system
  97. * page <PFN> it is stored in, so for each sub-page belonging
  98. * to a zspage, obj_idx starts with 0.
  99. *
  100. * This is made more complicated by various memory models and PAE.
  101. */
  102. #ifndef MAX_PHYSMEM_BITS
  103. #ifdef CONFIG_HIGHMEM64G
  104. #define MAX_PHYSMEM_BITS 36
  105. #else /* !CONFIG_HIGHMEM64G */
  106. /*
  107. * If this definition of MAX_PHYSMEM_BITS is used, OBJ_INDEX_BITS will just
  108. * be PAGE_SHIFT
  109. */
  110. #define MAX_PHYSMEM_BITS BITS_PER_LONG
  111. #endif
  112. #endif
  113. #define _PFN_BITS (MAX_PHYSMEM_BITS - PAGE_SHIFT)
  114. #define OBJ_INDEX_BITS (BITS_PER_LONG - _PFN_BITS)
  115. #define OBJ_INDEX_MASK ((_AC(1, UL) << OBJ_INDEX_BITS) - 1)
  116. #define MAX(a, b) ((a) >= (b) ? (a) : (b))
  117. /* ZS_MIN_ALLOC_SIZE must be multiple of ZS_ALIGN */
  118. #define ZS_MIN_ALLOC_SIZE \
  119. MAX(32, (ZS_MAX_PAGES_PER_ZSPAGE << PAGE_SHIFT >> OBJ_INDEX_BITS))
  120. #define ZS_MAX_ALLOC_SIZE PAGE_SIZE
  121. /*
  122. * On systems with 4K page size, this gives 254 size classes! There is a
  123. * trader-off here:
  124. * - Large number of size classes is potentially wasteful as free page are
  125. * spread across these classes
  126. * - Small number of size classes causes large internal fragmentation
  127. * - Probably its better to use specific size classes (empirically
  128. * determined). NOTE: all those class sizes must be set as multiple of
  129. * ZS_ALIGN to make sure link_free itself never has to span 2 pages.
  130. *
  131. * ZS_MIN_ALLOC_SIZE and ZS_SIZE_CLASS_DELTA must be multiple of ZS_ALIGN
  132. * (reason above)
  133. */
  134. #define ZS_SIZE_CLASS_DELTA (PAGE_SIZE >> 8)
  135. #define ZS_SIZE_CLASSES ((ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE) / \
  136. ZS_SIZE_CLASS_DELTA + 1)
  137. /*
  138. * We do not maintain any list for completely empty or full pages
  139. */
  140. enum fullness_group {
  141. ZS_ALMOST_FULL,
  142. ZS_ALMOST_EMPTY,
  143. _ZS_NR_FULLNESS_GROUPS,
  144. ZS_EMPTY,
  145. ZS_FULL
  146. };
  147. /*
  148. * We assign a page to ZS_ALMOST_EMPTY fullness group when:
  149. * n <= N / f, where
  150. * n = number of allocated objects
  151. * N = total number of objects zspage can store
  152. * f = 1/fullness_threshold_frac
  153. *
  154. * Similarly, we assign zspage to:
  155. * ZS_ALMOST_FULL when n > N / f
  156. * ZS_EMPTY when n == 0
  157. * ZS_FULL when n == N
  158. *
  159. * (see: fix_fullness_group())
  160. */
  161. static const int fullness_threshold_frac = 4;
  162. struct size_class {
  163. /*
  164. * Size of objects stored in this class. Must be multiple
  165. * of ZS_ALIGN.
  166. */
  167. int size;
  168. unsigned int index;
  169. /* Number of PAGE_SIZE sized pages to combine to form a 'zspage' */
  170. int pages_per_zspage;
  171. spinlock_t lock;
  172. /* stats */
  173. u64 pages_allocated;
  174. struct page *fullness_list[_ZS_NR_FULLNESS_GROUPS];
  175. };
  176. /*
  177. * Placed within free objects to form a singly linked list.
  178. * For every zspage, first_page->freelist gives head of this list.
  179. *
  180. * This must be power of 2 and less than or equal to ZS_ALIGN
  181. */
  182. struct link_free {
  183. /* Handle of next free chunk (encodes <PFN, obj_idx>) */
  184. void *next;
  185. };
  186. struct zs_pool {
  187. struct size_class size_class[ZS_SIZE_CLASSES];
  188. gfp_t flags; /* allocation flags used when growing pool */
  189. };
  190. /*
  191. * A zspage's class index and fullness group
  192. * are encoded in its (first)page->mapping
  193. */
  194. #define CLASS_IDX_BITS 28
  195. #define FULLNESS_BITS 4
  196. #define CLASS_IDX_MASK ((1 << CLASS_IDX_BITS) - 1)
  197. #define FULLNESS_MASK ((1 << FULLNESS_BITS) - 1)
  198. /*
  199. * By default, zsmalloc uses a copy-based object mapping method to access
  200. * allocations that span two pages. However, if a particular architecture
  201. * performs VM mapping faster than copying, then it should be added here
  202. * so that USE_PGTABLE_MAPPING is defined. This causes zsmalloc to use
  203. * page table mapping rather than copying for object mapping.
  204. */
  205. #if defined(CONFIG_ARM) && !defined(MODULE)
  206. #define USE_PGTABLE_MAPPING
  207. #endif
  208. struct mapping_area {
  209. #ifdef USE_PGTABLE_MAPPING
  210. struct vm_struct *vm; /* vm area for mapping object that span pages */
  211. #else
  212. char *vm_buf; /* copy buffer for objects that span pages */
  213. #endif
  214. char *vm_addr; /* address of kmap_atomic()'ed pages */
  215. enum zs_mapmode vm_mm; /* mapping mode */
  216. };
  217. /* per-cpu VM mapping areas for zspage accesses that cross page boundaries */
  218. static DEFINE_PER_CPU(struct mapping_area, zs_map_area);
  219. static int is_first_page(struct page *page)
  220. {
  221. return PagePrivate(page);
  222. }
  223. static int is_last_page(struct page *page)
  224. {
  225. return PagePrivate2(page);
  226. }
  227. static void get_zspage_mapping(struct page *page, unsigned int *class_idx,
  228. enum fullness_group *fullness)
  229. {
  230. unsigned long m;
  231. BUG_ON(!is_first_page(page));
  232. m = (unsigned long)page->mapping;
  233. *fullness = m & FULLNESS_MASK;
  234. *class_idx = (m >> FULLNESS_BITS) & CLASS_IDX_MASK;
  235. }
  236. static void set_zspage_mapping(struct page *page, unsigned int class_idx,
  237. enum fullness_group fullness)
  238. {
  239. unsigned long m;
  240. BUG_ON(!is_first_page(page));
  241. m = ((class_idx & CLASS_IDX_MASK) << FULLNESS_BITS) |
  242. (fullness & FULLNESS_MASK);
  243. page->mapping = (struct address_space *)m;
  244. }
  245. static int get_size_class_index(int size)
  246. {
  247. int idx = 0;
  248. if (likely(size > ZS_MIN_ALLOC_SIZE))
  249. idx = DIV_ROUND_UP(size - ZS_MIN_ALLOC_SIZE,
  250. ZS_SIZE_CLASS_DELTA);
  251. return idx;
  252. }
  253. static enum fullness_group get_fullness_group(struct page *page)
  254. {
  255. int inuse, max_objects;
  256. enum fullness_group fg;
  257. BUG_ON(!is_first_page(page));
  258. inuse = page->inuse;
  259. max_objects = page->objects;
  260. if (inuse == 0)
  261. fg = ZS_EMPTY;
  262. else if (inuse == max_objects)
  263. fg = ZS_FULL;
  264. else if (inuse <= max_objects / fullness_threshold_frac)
  265. fg = ZS_ALMOST_EMPTY;
  266. else
  267. fg = ZS_ALMOST_FULL;
  268. return fg;
  269. }
  270. static void insert_zspage(struct page *page, struct size_class *class,
  271. enum fullness_group fullness)
  272. {
  273. struct page **head;
  274. BUG_ON(!is_first_page(page));
  275. if (fullness >= _ZS_NR_FULLNESS_GROUPS)
  276. return;
  277. head = &class->fullness_list[fullness];
  278. if (*head)
  279. list_add_tail(&page->lru, &(*head)->lru);
  280. *head = page;
  281. }
  282. static void remove_zspage(struct page *page, struct size_class *class,
  283. enum fullness_group fullness)
  284. {
  285. struct page **head;
  286. BUG_ON(!is_first_page(page));
  287. if (fullness >= _ZS_NR_FULLNESS_GROUPS)
  288. return;
  289. head = &class->fullness_list[fullness];
  290. BUG_ON(!*head);
  291. if (list_empty(&(*head)->lru))
  292. *head = NULL;
  293. else if (*head == page)
  294. *head = (struct page *)list_entry((*head)->lru.next,
  295. struct page, lru);
  296. list_del_init(&page->lru);
  297. }
  298. static enum fullness_group fix_fullness_group(struct zs_pool *pool,
  299. struct page *page)
  300. {
  301. int class_idx;
  302. struct size_class *class;
  303. enum fullness_group currfg, newfg;
  304. BUG_ON(!is_first_page(page));
  305. get_zspage_mapping(page, &class_idx, &currfg);
  306. newfg = get_fullness_group(page);
  307. if (newfg == currfg)
  308. goto out;
  309. class = &pool->size_class[class_idx];
  310. remove_zspage(page, class, currfg);
  311. insert_zspage(page, class, newfg);
  312. set_zspage_mapping(page, class_idx, newfg);
  313. out:
  314. return newfg;
  315. }
  316. /*
  317. * We have to decide on how many pages to link together
  318. * to form a zspage for each size class. This is important
  319. * to reduce wastage due to unusable space left at end of
  320. * each zspage which is given as:
  321. * wastage = Zp - Zp % size_class
  322. * where Zp = zspage size = k * PAGE_SIZE where k = 1, 2, ...
  323. *
  324. * For example, for size class of 3/8 * PAGE_SIZE, we should
  325. * link together 3 PAGE_SIZE sized pages to form a zspage
  326. * since then we can perfectly fit in 8 such objects.
  327. */
  328. static int get_pages_per_zspage(int class_size)
  329. {
  330. int i, max_usedpc = 0;
  331. /* zspage order which gives maximum used size per KB */
  332. int max_usedpc_order = 1;
  333. for (i = 1; i <= ZS_MAX_PAGES_PER_ZSPAGE; i++) {
  334. int zspage_size;
  335. int waste, usedpc;
  336. zspage_size = i * PAGE_SIZE;
  337. waste = zspage_size % class_size;
  338. usedpc = (zspage_size - waste) * 100 / zspage_size;
  339. if (usedpc > max_usedpc) {
  340. max_usedpc = usedpc;
  341. max_usedpc_order = i;
  342. }
  343. }
  344. return max_usedpc_order;
  345. }
  346. /*
  347. * A single 'zspage' is composed of many system pages which are
  348. * linked together using fields in struct page. This function finds
  349. * the first/head page, given any component page of a zspage.
  350. */
  351. static struct page *get_first_page(struct page *page)
  352. {
  353. if (is_first_page(page))
  354. return page;
  355. else
  356. return page->first_page;
  357. }
  358. static struct page *get_next_page(struct page *page)
  359. {
  360. struct page *next;
  361. if (is_last_page(page))
  362. next = NULL;
  363. else if (is_first_page(page))
  364. next = (struct page *)page_private(page);
  365. else
  366. next = list_entry(page->lru.next, struct page, lru);
  367. return next;
  368. }
  369. /*
  370. * Encode <page, obj_idx> as a single handle value.
  371. * On hardware platforms with physical memory starting at 0x0 the pfn
  372. * could be 0 so we ensure that the handle will never be 0 by adjusting the
  373. * encoded obj_idx value before encoding.
  374. */
  375. static void *obj_location_to_handle(struct page *page, unsigned long obj_idx)
  376. {
  377. unsigned long handle;
  378. if (!page) {
  379. BUG_ON(obj_idx);
  380. return NULL;
  381. }
  382. handle = page_to_pfn(page) << OBJ_INDEX_BITS;
  383. handle |= ((obj_idx + 1) & OBJ_INDEX_MASK);
  384. return (void *)handle;
  385. }
  386. /*
  387. * Decode <page, obj_idx> pair from the given object handle. We adjust the
  388. * decoded obj_idx back to its original value since it was adjusted in
  389. * obj_location_to_handle().
  390. */
  391. static void obj_handle_to_location(unsigned long handle, struct page **page,
  392. unsigned long *obj_idx)
  393. {
  394. *page = pfn_to_page(handle >> OBJ_INDEX_BITS);
  395. *obj_idx = (handle & OBJ_INDEX_MASK) - 1;
  396. }
  397. static unsigned long obj_idx_to_offset(struct page *page,
  398. unsigned long obj_idx, int class_size)
  399. {
  400. unsigned long off = 0;
  401. if (!is_first_page(page))
  402. off = page->index;
  403. return off + obj_idx * class_size;
  404. }
  405. static void reset_page(struct page *page)
  406. {
  407. clear_bit(PG_private, &page->flags);
  408. clear_bit(PG_private_2, &page->flags);
  409. set_page_private(page, 0);
  410. page->mapping = NULL;
  411. page->freelist = NULL;
  412. reset_page_mapcount(page);
  413. }
  414. static void free_zspage(struct page *first_page)
  415. {
  416. struct page *nextp, *tmp, *head_extra;
  417. BUG_ON(!is_first_page(first_page));
  418. BUG_ON(first_page->inuse);
  419. head_extra = (struct page *)page_private(first_page);
  420. reset_page(first_page);
  421. __free_page(first_page);
  422. /* zspage with only 1 system page */
  423. if (!head_extra)
  424. return;
  425. list_for_each_entry_safe(nextp, tmp, &head_extra->lru, lru) {
  426. list_del(&nextp->lru);
  427. reset_page(nextp);
  428. __free_page(nextp);
  429. }
  430. reset_page(head_extra);
  431. __free_page(head_extra);
  432. }
  433. /* Initialize a newly allocated zspage */
  434. static void init_zspage(struct page *first_page, struct size_class *class)
  435. {
  436. unsigned long off = 0;
  437. struct page *page = first_page;
  438. BUG_ON(!is_first_page(first_page));
  439. while (page) {
  440. struct page *next_page;
  441. struct link_free *link;
  442. unsigned int i, objs_on_page;
  443. /*
  444. * page->index stores offset of first object starting
  445. * in the page. For the first page, this is always 0,
  446. * so we use first_page->index (aka ->freelist) to store
  447. * head of corresponding zspage's freelist.
  448. */
  449. if (page != first_page)
  450. page->index = off;
  451. link = (struct link_free *)kmap_atomic(page) +
  452. off / sizeof(*link);
  453. objs_on_page = (PAGE_SIZE - off) / class->size;
  454. for (i = 1; i <= objs_on_page; i++) {
  455. off += class->size;
  456. if (off < PAGE_SIZE) {
  457. link->next = obj_location_to_handle(page, i);
  458. link += class->size / sizeof(*link);
  459. }
  460. }
  461. /*
  462. * We now come to the last (full or partial) object on this
  463. * page, which must point to the first object on the next
  464. * page (if present)
  465. */
  466. next_page = get_next_page(page);
  467. link->next = obj_location_to_handle(next_page, 0);
  468. kunmap_atomic(link);
  469. page = next_page;
  470. off = (off + class->size) % PAGE_SIZE;
  471. }
  472. }
  473. /*
  474. * Allocate a zspage for the given size class
  475. */
  476. static struct page *alloc_zspage(struct size_class *class, gfp_t flags)
  477. {
  478. int i, error;
  479. struct page *first_page = NULL, *uninitialized_var(prev_page);
  480. /*
  481. * Allocate individual pages and link them together as:
  482. * 1. first page->private = first sub-page
  483. * 2. all sub-pages are linked together using page->lru
  484. * 3. each sub-page is linked to the first page using page->first_page
  485. *
  486. * For each size class, First/Head pages are linked together using
  487. * page->lru. Also, we set PG_private to identify the first page
  488. * (i.e. no other sub-page has this flag set) and PG_private_2 to
  489. * identify the last page.
  490. */
  491. error = -ENOMEM;
  492. for (i = 0; i < class->pages_per_zspage; i++) {
  493. struct page *page;
  494. page = alloc_page(flags);
  495. if (!page)
  496. goto cleanup;
  497. INIT_LIST_HEAD(&page->lru);
  498. if (i == 0) { /* first page */
  499. SetPagePrivate(page);
  500. set_page_private(page, 0);
  501. first_page = page;
  502. first_page->inuse = 0;
  503. }
  504. if (i == 1)
  505. set_page_private(first_page, (unsigned long)page);
  506. if (i >= 1)
  507. page->first_page = first_page;
  508. if (i >= 2)
  509. list_add(&page->lru, &prev_page->lru);
  510. if (i == class->pages_per_zspage - 1) /* last page */
  511. SetPagePrivate2(page);
  512. prev_page = page;
  513. }
  514. init_zspage(first_page, class);
  515. first_page->freelist = obj_location_to_handle(first_page, 0);
  516. /* Maximum number of objects we can store in this zspage */
  517. first_page->objects = class->pages_per_zspage * PAGE_SIZE / class->size;
  518. error = 0; /* Success */
  519. cleanup:
  520. if (unlikely(error) && first_page) {
  521. free_zspage(first_page);
  522. first_page = NULL;
  523. }
  524. return first_page;
  525. }
  526. static struct page *find_get_zspage(struct size_class *class)
  527. {
  528. int i;
  529. struct page *page;
  530. for (i = 0; i < _ZS_NR_FULLNESS_GROUPS; i++) {
  531. page = class->fullness_list[i];
  532. if (page)
  533. break;
  534. }
  535. return page;
  536. }
  537. #ifdef USE_PGTABLE_MAPPING
  538. static inline int __zs_cpu_up(struct mapping_area *area)
  539. {
  540. /*
  541. * Make sure we don't leak memory if a cpu UP notification
  542. * and zs_init() race and both call zs_cpu_up() on the same cpu
  543. */
  544. if (area->vm)
  545. return 0;
  546. area->vm = alloc_vm_area(PAGE_SIZE * 2, NULL);
  547. if (!area->vm)
  548. return -ENOMEM;
  549. return 0;
  550. }
  551. static inline void __zs_cpu_down(struct mapping_area *area)
  552. {
  553. if (area->vm)
  554. free_vm_area(area->vm);
  555. area->vm = NULL;
  556. }
  557. static inline void *__zs_map_object(struct mapping_area *area,
  558. struct page *pages[2], int off, int size)
  559. {
  560. BUG_ON(map_vm_area(area->vm, PAGE_KERNEL, &pages));
  561. area->vm_addr = area->vm->addr;
  562. return area->vm_addr + off;
  563. }
  564. static inline void __zs_unmap_object(struct mapping_area *area,
  565. struct page *pages[2], int off, int size)
  566. {
  567. unsigned long addr = (unsigned long)area->vm_addr;
  568. unmap_kernel_range(addr, PAGE_SIZE * 2);
  569. }
  570. #else /* USE_PGTABLE_MAPPING */
  571. static inline int __zs_cpu_up(struct mapping_area *area)
  572. {
  573. /*
  574. * Make sure we don't leak memory if a cpu UP notification
  575. * and zs_init() race and both call zs_cpu_up() on the same cpu
  576. */
  577. if (area->vm_buf)
  578. return 0;
  579. area->vm_buf = (char *)__get_free_page(GFP_KERNEL);
  580. if (!area->vm_buf)
  581. return -ENOMEM;
  582. return 0;
  583. }
  584. static inline void __zs_cpu_down(struct mapping_area *area)
  585. {
  586. if (area->vm_buf)
  587. free_page((unsigned long)area->vm_buf);
  588. area->vm_buf = NULL;
  589. }
  590. static void *__zs_map_object(struct mapping_area *area,
  591. struct page *pages[2], int off, int size)
  592. {
  593. int sizes[2];
  594. void *addr;
  595. char *buf = area->vm_buf;
  596. /* disable page faults to match kmap_atomic() return conditions */
  597. pagefault_disable();
  598. /* no read fastpath */
  599. if (area->vm_mm == ZS_MM_WO)
  600. goto out;
  601. sizes[0] = PAGE_SIZE - off;
  602. sizes[1] = size - sizes[0];
  603. /* copy object to per-cpu buffer */
  604. addr = kmap_atomic(pages[0]);
  605. memcpy(buf, addr + off, sizes[0]);
  606. kunmap_atomic(addr);
  607. addr = kmap_atomic(pages[1]);
  608. memcpy(buf + sizes[0], addr, sizes[1]);
  609. kunmap_atomic(addr);
  610. out:
  611. return area->vm_buf;
  612. }
  613. static void __zs_unmap_object(struct mapping_area *area,
  614. struct page *pages[2], int off, int size)
  615. {
  616. int sizes[2];
  617. void *addr;
  618. char *buf = area->vm_buf;
  619. /* no write fastpath */
  620. if (area->vm_mm == ZS_MM_RO)
  621. goto out;
  622. sizes[0] = PAGE_SIZE - off;
  623. sizes[1] = size - sizes[0];
  624. /* copy per-cpu buffer to object */
  625. addr = kmap_atomic(pages[0]);
  626. memcpy(addr + off, buf, sizes[0]);
  627. kunmap_atomic(addr);
  628. addr = kmap_atomic(pages[1]);
  629. memcpy(addr, buf + sizes[0], sizes[1]);
  630. kunmap_atomic(addr);
  631. out:
  632. /* enable page faults to match kunmap_atomic() return conditions */
  633. pagefault_enable();
  634. }
  635. #endif /* USE_PGTABLE_MAPPING */
  636. static int zs_cpu_notifier(struct notifier_block *nb, unsigned long action,
  637. void *pcpu)
  638. {
  639. int ret, cpu = (long)pcpu;
  640. struct mapping_area *area;
  641. switch (action) {
  642. case CPU_UP_PREPARE:
  643. area = &per_cpu(zs_map_area, cpu);
  644. ret = __zs_cpu_up(area);
  645. if (ret)
  646. return notifier_from_errno(ret);
  647. break;
  648. case CPU_DEAD:
  649. case CPU_UP_CANCELED:
  650. area = &per_cpu(zs_map_area, cpu);
  651. __zs_cpu_down(area);
  652. break;
  653. }
  654. return NOTIFY_OK;
  655. }
  656. static struct notifier_block zs_cpu_nb = {
  657. .notifier_call = zs_cpu_notifier
  658. };
  659. static void zs_exit(void)
  660. {
  661. int cpu;
  662. cpu_notifier_register_begin();
  663. for_each_online_cpu(cpu)
  664. zs_cpu_notifier(NULL, CPU_DEAD, (void *)(long)cpu);
  665. __unregister_cpu_notifier(&zs_cpu_nb);
  666. cpu_notifier_register_done();
  667. }
  668. static int zs_init(void)
  669. {
  670. int cpu, ret;
  671. cpu_notifier_register_begin();
  672. __register_cpu_notifier(&zs_cpu_nb);
  673. for_each_online_cpu(cpu) {
  674. ret = zs_cpu_notifier(NULL, CPU_UP_PREPARE, (void *)(long)cpu);
  675. if (notifier_to_errno(ret)) {
  676. cpu_notifier_register_done();
  677. goto fail;
  678. }
  679. }
  680. cpu_notifier_register_done();
  681. return 0;
  682. fail:
  683. zs_exit();
  684. return notifier_to_errno(ret);
  685. }
  686. /**
  687. * zs_create_pool - Creates an allocation pool to work from.
  688. * @flags: allocation flags used to allocate pool metadata
  689. *
  690. * This function must be called before anything when using
  691. * the zsmalloc allocator.
  692. *
  693. * On success, a pointer to the newly created pool is returned,
  694. * otherwise NULL.
  695. */
  696. struct zs_pool *zs_create_pool(gfp_t flags)
  697. {
  698. int i, ovhd_size;
  699. struct zs_pool *pool;
  700. ovhd_size = roundup(sizeof(*pool), PAGE_SIZE);
  701. pool = kzalloc(ovhd_size, GFP_KERNEL);
  702. if (!pool)
  703. return NULL;
  704. for (i = 0; i < ZS_SIZE_CLASSES; i++) {
  705. int size;
  706. struct size_class *class;
  707. size = ZS_MIN_ALLOC_SIZE + i * ZS_SIZE_CLASS_DELTA;
  708. if (size > ZS_MAX_ALLOC_SIZE)
  709. size = ZS_MAX_ALLOC_SIZE;
  710. class = &pool->size_class[i];
  711. class->size = size;
  712. class->index = i;
  713. spin_lock_init(&class->lock);
  714. class->pages_per_zspage = get_pages_per_zspage(size);
  715. }
  716. pool->flags = flags;
  717. return pool;
  718. }
  719. EXPORT_SYMBOL_GPL(zs_create_pool);
  720. void zs_destroy_pool(struct zs_pool *pool)
  721. {
  722. int i;
  723. for (i = 0; i < ZS_SIZE_CLASSES; i++) {
  724. int fg;
  725. struct size_class *class = &pool->size_class[i];
  726. for (fg = 0; fg < _ZS_NR_FULLNESS_GROUPS; fg++) {
  727. if (class->fullness_list[fg]) {
  728. pr_info("Freeing non-empty class with size %db, fullness group %d\n",
  729. class->size, fg);
  730. }
  731. }
  732. }
  733. kfree(pool);
  734. }
  735. EXPORT_SYMBOL_GPL(zs_destroy_pool);
  736. /**
  737. * zs_malloc - Allocate block of given size from pool.
  738. * @pool: pool to allocate from
  739. * @size: size of block to allocate
  740. *
  741. * On success, handle to the allocated object is returned,
  742. * otherwise 0.
  743. * Allocation requests with size > ZS_MAX_ALLOC_SIZE will fail.
  744. */
  745. unsigned long zs_malloc(struct zs_pool *pool, size_t size)
  746. {
  747. unsigned long obj;
  748. struct link_free *link;
  749. int class_idx;
  750. struct size_class *class;
  751. struct page *first_page, *m_page;
  752. unsigned long m_objidx, m_offset;
  753. if (unlikely(!size || size > ZS_MAX_ALLOC_SIZE))
  754. return 0;
  755. class_idx = get_size_class_index(size);
  756. class = &pool->size_class[class_idx];
  757. BUG_ON(class_idx != class->index);
  758. spin_lock(&class->lock);
  759. first_page = find_get_zspage(class);
  760. if (!first_page) {
  761. spin_unlock(&class->lock);
  762. first_page = alloc_zspage(class, pool->flags);
  763. if (unlikely(!first_page))
  764. return 0;
  765. set_zspage_mapping(first_page, class->index, ZS_EMPTY);
  766. spin_lock(&class->lock);
  767. class->pages_allocated += class->pages_per_zspage;
  768. }
  769. obj = (unsigned long)first_page->freelist;
  770. obj_handle_to_location(obj, &m_page, &m_objidx);
  771. m_offset = obj_idx_to_offset(m_page, m_objidx, class->size);
  772. link = (struct link_free *)kmap_atomic(m_page) +
  773. m_offset / sizeof(*link);
  774. first_page->freelist = link->next;
  775. memset(link, POISON_INUSE, sizeof(*link));
  776. kunmap_atomic(link);
  777. first_page->inuse++;
  778. /* Now move the zspage to another fullness group, if required */
  779. fix_fullness_group(pool, first_page);
  780. spin_unlock(&class->lock);
  781. return obj;
  782. }
  783. EXPORT_SYMBOL_GPL(zs_malloc);
  784. void zs_free(struct zs_pool *pool, unsigned long obj)
  785. {
  786. struct link_free *link;
  787. struct page *first_page, *f_page;
  788. unsigned long f_objidx, f_offset;
  789. int class_idx;
  790. struct size_class *class;
  791. enum fullness_group fullness;
  792. if (unlikely(!obj))
  793. return;
  794. obj_handle_to_location(obj, &f_page, &f_objidx);
  795. first_page = get_first_page(f_page);
  796. get_zspage_mapping(first_page, &class_idx, &fullness);
  797. class = &pool->size_class[class_idx];
  798. f_offset = obj_idx_to_offset(f_page, f_objidx, class->size);
  799. spin_lock(&class->lock);
  800. /* Insert this object in containing zspage's freelist */
  801. link = (struct link_free *)((unsigned char *)kmap_atomic(f_page)
  802. + f_offset);
  803. link->next = first_page->freelist;
  804. kunmap_atomic(link);
  805. first_page->freelist = (void *)obj;
  806. first_page->inuse--;
  807. fullness = fix_fullness_group(pool, first_page);
  808. if (fullness == ZS_EMPTY)
  809. class->pages_allocated -= class->pages_per_zspage;
  810. spin_unlock(&class->lock);
  811. if (fullness == ZS_EMPTY)
  812. free_zspage(first_page);
  813. }
  814. EXPORT_SYMBOL_GPL(zs_free);
  815. /**
  816. * zs_map_object - get address of allocated object from handle.
  817. * @pool: pool from which the object was allocated
  818. * @handle: handle returned from zs_malloc
  819. *
  820. * Before using an object allocated from zs_malloc, it must be mapped using
  821. * this function. When done with the object, it must be unmapped using
  822. * zs_unmap_object.
  823. *
  824. * Only one object can be mapped per cpu at a time. There is no protection
  825. * against nested mappings.
  826. *
  827. * This function returns with preemption and page faults disabled.
  828. */
  829. void *zs_map_object(struct zs_pool *pool, unsigned long handle,
  830. enum zs_mapmode mm)
  831. {
  832. struct page *page;
  833. unsigned long obj_idx, off;
  834. unsigned int class_idx;
  835. enum fullness_group fg;
  836. struct size_class *class;
  837. struct mapping_area *area;
  838. struct page *pages[2];
  839. BUG_ON(!handle);
  840. /*
  841. * Because we use per-cpu mapping areas shared among the
  842. * pools/users, we can't allow mapping in interrupt context
  843. * because it can corrupt another users mappings.
  844. */
  845. BUG_ON(in_interrupt());
  846. obj_handle_to_location(handle, &page, &obj_idx);
  847. get_zspage_mapping(get_first_page(page), &class_idx, &fg);
  848. class = &pool->size_class[class_idx];
  849. off = obj_idx_to_offset(page, obj_idx, class->size);
  850. area = &get_cpu_var(zs_map_area);
  851. area->vm_mm = mm;
  852. if (off + class->size <= PAGE_SIZE) {
  853. /* this object is contained entirely within a page */
  854. area->vm_addr = kmap_atomic(page);
  855. return area->vm_addr + off;
  856. }
  857. /* this object spans two pages */
  858. pages[0] = page;
  859. pages[1] = get_next_page(page);
  860. BUG_ON(!pages[1]);
  861. return __zs_map_object(area, pages, off, class->size);
  862. }
  863. EXPORT_SYMBOL_GPL(zs_map_object);
  864. void zs_unmap_object(struct zs_pool *pool, unsigned long handle)
  865. {
  866. struct page *page;
  867. unsigned long obj_idx, off;
  868. unsigned int class_idx;
  869. enum fullness_group fg;
  870. struct size_class *class;
  871. struct mapping_area *area;
  872. BUG_ON(!handle);
  873. obj_handle_to_location(handle, &page, &obj_idx);
  874. get_zspage_mapping(get_first_page(page), &class_idx, &fg);
  875. class = &pool->size_class[class_idx];
  876. off = obj_idx_to_offset(page, obj_idx, class->size);
  877. area = &__get_cpu_var(zs_map_area);
  878. if (off + class->size <= PAGE_SIZE)
  879. kunmap_atomic(area->vm_addr);
  880. else {
  881. struct page *pages[2];
  882. pages[0] = page;
  883. pages[1] = get_next_page(page);
  884. BUG_ON(!pages[1]);
  885. __zs_unmap_object(area, pages, off, class->size);
  886. }
  887. put_cpu_var(zs_map_area);
  888. }
  889. EXPORT_SYMBOL_GPL(zs_unmap_object);
  890. u64 zs_get_total_size_bytes(struct zs_pool *pool)
  891. {
  892. int i;
  893. u64 npages = 0;
  894. for (i = 0; i < ZS_SIZE_CLASSES; i++)
  895. npages += pool->size_class[i].pages_allocated;
  896. return npages << PAGE_SHIFT;
  897. }
  898. EXPORT_SYMBOL_GPL(zs_get_total_size_bytes);
  899. module_init(zs_init);
  900. module_exit(zs_exit);
  901. MODULE_LICENSE("Dual BSD/GPL");
  902. MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");