z3fold.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  1. /*
  2. * z3fold.c
  3. *
  4. * Author: Vitaly Wool <vitaly.wool@konsulko.com>
  5. * Copyright (C) 2016, Sony Mobile Communications Inc.
  6. *
  7. * This implementation is based on zbud written by Seth Jennings.
  8. *
  9. * z3fold is an special purpose allocator for storing compressed pages. It
  10. * can store up to three compressed pages per page which improves the
  11. * compression ratio of zbud while retaining its main concepts (e. g. always
  12. * storing an integral number of objects per page) and simplicity.
  13. * It still has simple and deterministic reclaim properties that make it
  14. * preferable to a higher density approach (with no requirement on integral
  15. * number of object per page) when reclaim is used.
  16. *
  17. * As in zbud, pages are divided into "chunks". The size of the chunks is
  18. * fixed at compile time and is determined by NCHUNKS_ORDER below.
  19. *
  20. * z3fold doesn't export any API and is meant to be used via zpool API.
  21. */
  22. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  23. #include <linux/atomic.h>
  24. #include <linux/list.h>
  25. #include <linux/mm.h>
  26. #include <linux/module.h>
  27. #include <linux/preempt.h>
  28. #include <linux/slab.h>
  29. #include <linux/spinlock.h>
  30. #include <linux/zpool.h>
  31. /*****************
  32. * Structures
  33. *****************/
  34. struct z3fold_pool;
  35. struct z3fold_ops {
  36. int (*evict)(struct z3fold_pool *pool, unsigned long handle);
  37. };
  38. enum buddy {
  39. HEADLESS = 0,
  40. FIRST,
  41. MIDDLE,
  42. LAST,
  43. BUDDIES_MAX
  44. };
  45. /*
  46. * struct z3fold_header - z3fold page metadata occupying the first chunk of each
  47. * z3fold page, except for HEADLESS pages
  48. * @buddy: links the z3fold page into the relevant list in the pool
  49. * @page_lock: per-page lock
  50. * @refcount: reference cound for the z3fold page
  51. * @first_chunks: the size of the first buddy in chunks, 0 if free
  52. * @middle_chunks: the size of the middle buddy in chunks, 0 if free
  53. * @last_chunks: the size of the last buddy in chunks, 0 if free
  54. * @first_num: the starting number (for the first handle)
  55. */
  56. struct z3fold_header {
  57. struct list_head buddy;
  58. spinlock_t page_lock;
  59. struct kref refcount;
  60. unsigned short first_chunks;
  61. unsigned short middle_chunks;
  62. unsigned short last_chunks;
  63. unsigned short start_middle;
  64. unsigned short first_num:2;
  65. };
  66. /*
  67. * NCHUNKS_ORDER determines the internal allocation granularity, effectively
  68. * adjusting internal fragmentation. It also determines the number of
  69. * freelists maintained in each pool. NCHUNKS_ORDER of 6 means that the
  70. * allocation granularity will be in chunks of size PAGE_SIZE/64. Some chunks
  71. * in the beginning of an allocated page are occupied by z3fold header, so
  72. * NCHUNKS will be calculated to 63 (or 62 in case CONFIG_DEBUG_SPINLOCK=y),
  73. * which shows the max number of free chunks in z3fold page, also there will
  74. * be 63, or 62, respectively, freelists per pool.
  75. */
  76. #define NCHUNKS_ORDER 6
  77. #define CHUNK_SHIFT (PAGE_SHIFT - NCHUNKS_ORDER)
  78. #define CHUNK_SIZE (1 << CHUNK_SHIFT)
  79. #define ZHDR_SIZE_ALIGNED round_up(sizeof(struct z3fold_header), CHUNK_SIZE)
  80. #define ZHDR_CHUNKS (ZHDR_SIZE_ALIGNED >> CHUNK_SHIFT)
  81. #define TOTAL_CHUNKS (PAGE_SIZE >> CHUNK_SHIFT)
  82. #define NCHUNKS ((PAGE_SIZE - ZHDR_SIZE_ALIGNED) >> CHUNK_SHIFT)
  83. #define BUDDY_MASK (0x3)
  84. /**
  85. * struct z3fold_pool - stores metadata for each z3fold pool
  86. * @lock: protects all pool fields and first|last_chunk fields of any
  87. * z3fold page in the pool
  88. * @unbuddied: array of lists tracking z3fold pages that contain 2- buddies;
  89. * the lists each z3fold page is added to depends on the size of
  90. * its free region.
  91. * @lru: list tracking the z3fold pages in LRU order by most recently
  92. * added buddy.
  93. * @pages_nr: number of z3fold pages in the pool.
  94. * @ops: pointer to a structure of user defined operations specified at
  95. * pool creation time.
  96. *
  97. * This structure is allocated at pool creation time and maintains metadata
  98. * pertaining to a particular z3fold pool.
  99. */
  100. struct z3fold_pool {
  101. spinlock_t lock;
  102. struct list_head unbuddied[NCHUNKS];
  103. struct list_head lru;
  104. atomic64_t pages_nr;
  105. const struct z3fold_ops *ops;
  106. struct zpool *zpool;
  107. const struct zpool_ops *zpool_ops;
  108. };
  109. /*
  110. * Internal z3fold page flags
  111. */
  112. enum z3fold_page_flags {
  113. PAGE_HEADLESS = 0,
  114. MIDDLE_CHUNK_MAPPED,
  115. };
  116. /*****************
  117. * Helpers
  118. *****************/
  119. /* Converts an allocation size in bytes to size in z3fold chunks */
  120. static int size_to_chunks(size_t size)
  121. {
  122. return (size + CHUNK_SIZE - 1) >> CHUNK_SHIFT;
  123. }
  124. #define for_each_unbuddied_list(_iter, _begin) \
  125. for ((_iter) = (_begin); (_iter) < NCHUNKS; (_iter)++)
  126. /* Initializes the z3fold header of a newly allocated z3fold page */
  127. static struct z3fold_header *init_z3fold_page(struct page *page)
  128. {
  129. struct z3fold_header *zhdr = page_address(page);
  130. INIT_LIST_HEAD(&page->lru);
  131. clear_bit(PAGE_HEADLESS, &page->private);
  132. clear_bit(MIDDLE_CHUNK_MAPPED, &page->private);
  133. spin_lock_init(&zhdr->page_lock);
  134. kref_init(&zhdr->refcount);
  135. zhdr->first_chunks = 0;
  136. zhdr->middle_chunks = 0;
  137. zhdr->last_chunks = 0;
  138. zhdr->first_num = 0;
  139. zhdr->start_middle = 0;
  140. INIT_LIST_HEAD(&zhdr->buddy);
  141. return zhdr;
  142. }
  143. /* Resets the struct page fields and frees the page */
  144. static void free_z3fold_page(struct page *page)
  145. {
  146. __free_page(page);
  147. }
  148. static void release_z3fold_page(struct kref *ref)
  149. {
  150. struct z3fold_header *zhdr;
  151. struct page *page;
  152. zhdr = container_of(ref, struct z3fold_header, refcount);
  153. page = virt_to_page(zhdr);
  154. if (!list_empty(&zhdr->buddy))
  155. list_del(&zhdr->buddy);
  156. if (!list_empty(&page->lru))
  157. list_del(&page->lru);
  158. free_z3fold_page(page);
  159. }
  160. /* Lock a z3fold page */
  161. static inline void z3fold_page_lock(struct z3fold_header *zhdr)
  162. {
  163. spin_lock(&zhdr->page_lock);
  164. }
  165. /* Try to lock a z3fold page */
  166. static inline int z3fold_page_trylock(struct z3fold_header *zhdr)
  167. {
  168. return spin_trylock(&zhdr->page_lock);
  169. }
  170. /* Unlock a z3fold page */
  171. static inline void z3fold_page_unlock(struct z3fold_header *zhdr)
  172. {
  173. spin_unlock(&zhdr->page_lock);
  174. }
  175. /*
  176. * Encodes the handle of a particular buddy within a z3fold page
  177. * Pool lock should be held as this function accesses first_num
  178. */
  179. static unsigned long encode_handle(struct z3fold_header *zhdr, enum buddy bud)
  180. {
  181. unsigned long handle;
  182. handle = (unsigned long)zhdr;
  183. if (bud != HEADLESS)
  184. handle += (bud + zhdr->first_num) & BUDDY_MASK;
  185. return handle;
  186. }
  187. /* Returns the z3fold page where a given handle is stored */
  188. static struct z3fold_header *handle_to_z3fold_header(unsigned long handle)
  189. {
  190. return (struct z3fold_header *)(handle & PAGE_MASK);
  191. }
  192. /*
  193. * (handle & BUDDY_MASK) < zhdr->first_num is possible in encode_handle
  194. * but that doesn't matter. because the masking will result in the
  195. * correct buddy number.
  196. */
  197. static enum buddy handle_to_buddy(unsigned long handle)
  198. {
  199. struct z3fold_header *zhdr = handle_to_z3fold_header(handle);
  200. return (handle - zhdr->first_num) & BUDDY_MASK;
  201. }
  202. /*
  203. * Returns the number of free chunks in a z3fold page.
  204. * NB: can't be used with HEADLESS pages.
  205. */
  206. static int num_free_chunks(struct z3fold_header *zhdr)
  207. {
  208. int nfree;
  209. /*
  210. * If there is a middle object, pick up the bigger free space
  211. * either before or after it. Otherwise just subtract the number
  212. * of chunks occupied by the first and the last objects.
  213. */
  214. if (zhdr->middle_chunks != 0) {
  215. int nfree_before = zhdr->first_chunks ?
  216. 0 : zhdr->start_middle - ZHDR_CHUNKS;
  217. int nfree_after = zhdr->last_chunks ?
  218. 0 : TOTAL_CHUNKS -
  219. (zhdr->start_middle + zhdr->middle_chunks);
  220. nfree = max(nfree_before, nfree_after);
  221. } else
  222. nfree = NCHUNKS - zhdr->first_chunks - zhdr->last_chunks;
  223. return nfree;
  224. }
  225. /*****************
  226. * API Functions
  227. *****************/
  228. /**
  229. * z3fold_create_pool() - create a new z3fold pool
  230. * @gfp: gfp flags when allocating the z3fold pool structure
  231. * @ops: user-defined operations for the z3fold pool
  232. *
  233. * Return: pointer to the new z3fold pool or NULL if the metadata allocation
  234. * failed.
  235. */
  236. static struct z3fold_pool *z3fold_create_pool(gfp_t gfp,
  237. const struct z3fold_ops *ops)
  238. {
  239. struct z3fold_pool *pool;
  240. int i;
  241. pool = kzalloc(sizeof(struct z3fold_pool), gfp);
  242. if (!pool)
  243. return NULL;
  244. spin_lock_init(&pool->lock);
  245. for_each_unbuddied_list(i, 0)
  246. INIT_LIST_HEAD(&pool->unbuddied[i]);
  247. INIT_LIST_HEAD(&pool->lru);
  248. atomic64_set(&pool->pages_nr, 0);
  249. pool->ops = ops;
  250. return pool;
  251. }
  252. /**
  253. * z3fold_destroy_pool() - destroys an existing z3fold pool
  254. * @pool: the z3fold pool to be destroyed
  255. *
  256. * The pool should be emptied before this function is called.
  257. */
  258. static void z3fold_destroy_pool(struct z3fold_pool *pool)
  259. {
  260. kfree(pool);
  261. }
  262. static inline void *mchunk_memmove(struct z3fold_header *zhdr,
  263. unsigned short dst_chunk)
  264. {
  265. void *beg = zhdr;
  266. return memmove(beg + (dst_chunk << CHUNK_SHIFT),
  267. beg + (zhdr->start_middle << CHUNK_SHIFT),
  268. zhdr->middle_chunks << CHUNK_SHIFT);
  269. }
  270. #define BIG_CHUNK_GAP 3
  271. /* Has to be called with lock held */
  272. static int z3fold_compact_page(struct z3fold_header *zhdr)
  273. {
  274. struct page *page = virt_to_page(zhdr);
  275. if (test_bit(MIDDLE_CHUNK_MAPPED, &page->private))
  276. return 0; /* can't move middle chunk, it's used */
  277. if (zhdr->middle_chunks == 0)
  278. return 0; /* nothing to compact */
  279. if (zhdr->first_chunks == 0 && zhdr->last_chunks == 0) {
  280. /* move to the beginning */
  281. mchunk_memmove(zhdr, ZHDR_CHUNKS);
  282. zhdr->first_chunks = zhdr->middle_chunks;
  283. zhdr->middle_chunks = 0;
  284. zhdr->start_middle = 0;
  285. zhdr->first_num++;
  286. return 1;
  287. }
  288. /*
  289. * moving data is expensive, so let's only do that if
  290. * there's substantial gain (at least BIG_CHUNK_GAP chunks)
  291. */
  292. if (zhdr->first_chunks != 0 && zhdr->last_chunks == 0 &&
  293. zhdr->start_middle - (zhdr->first_chunks + ZHDR_CHUNKS) >=
  294. BIG_CHUNK_GAP) {
  295. mchunk_memmove(zhdr, zhdr->first_chunks + ZHDR_CHUNKS);
  296. zhdr->start_middle = zhdr->first_chunks + ZHDR_CHUNKS;
  297. return 1;
  298. } else if (zhdr->last_chunks != 0 && zhdr->first_chunks == 0 &&
  299. TOTAL_CHUNKS - (zhdr->last_chunks + zhdr->start_middle
  300. + zhdr->middle_chunks) >=
  301. BIG_CHUNK_GAP) {
  302. unsigned short new_start = TOTAL_CHUNKS - zhdr->last_chunks -
  303. zhdr->middle_chunks;
  304. mchunk_memmove(zhdr, new_start);
  305. zhdr->start_middle = new_start;
  306. return 1;
  307. }
  308. return 0;
  309. }
  310. /**
  311. * z3fold_alloc() - allocates a region of a given size
  312. * @pool: z3fold pool from which to allocate
  313. * @size: size in bytes of the desired allocation
  314. * @gfp: gfp flags used if the pool needs to grow
  315. * @handle: handle of the new allocation
  316. *
  317. * This function will attempt to find a free region in the pool large enough to
  318. * satisfy the allocation request. A search of the unbuddied lists is
  319. * performed first. If no suitable free region is found, then a new page is
  320. * allocated and added to the pool to satisfy the request.
  321. *
  322. * gfp should not set __GFP_HIGHMEM as highmem pages cannot be used
  323. * as z3fold pool pages.
  324. *
  325. * Return: 0 if success and handle is set, otherwise -EINVAL if the size or
  326. * gfp arguments are invalid or -ENOMEM if the pool was unable to allocate
  327. * a new page.
  328. */
  329. static int z3fold_alloc(struct z3fold_pool *pool, size_t size, gfp_t gfp,
  330. unsigned long *handle)
  331. {
  332. int chunks = 0, i, freechunks;
  333. struct z3fold_header *zhdr = NULL;
  334. enum buddy bud;
  335. struct page *page;
  336. if (!size || (gfp & __GFP_HIGHMEM))
  337. return -EINVAL;
  338. if (size > PAGE_SIZE)
  339. return -ENOSPC;
  340. if (size > PAGE_SIZE - ZHDR_SIZE_ALIGNED - CHUNK_SIZE)
  341. bud = HEADLESS;
  342. else {
  343. chunks = size_to_chunks(size);
  344. /* First, try to find an unbuddied z3fold page. */
  345. zhdr = NULL;
  346. for_each_unbuddied_list(i, chunks) {
  347. spin_lock(&pool->lock);
  348. zhdr = list_first_entry_or_null(&pool->unbuddied[i],
  349. struct z3fold_header, buddy);
  350. if (!zhdr || !z3fold_page_trylock(zhdr)) {
  351. spin_unlock(&pool->lock);
  352. continue;
  353. }
  354. kref_get(&zhdr->refcount);
  355. list_del_init(&zhdr->buddy);
  356. spin_unlock(&pool->lock);
  357. page = virt_to_page(zhdr);
  358. if (zhdr->first_chunks == 0) {
  359. if (zhdr->middle_chunks != 0 &&
  360. chunks >= zhdr->start_middle)
  361. bud = LAST;
  362. else
  363. bud = FIRST;
  364. } else if (zhdr->last_chunks == 0)
  365. bud = LAST;
  366. else if (zhdr->middle_chunks == 0)
  367. bud = MIDDLE;
  368. else {
  369. z3fold_page_unlock(zhdr);
  370. spin_lock(&pool->lock);
  371. if (kref_put(&zhdr->refcount,
  372. release_z3fold_page))
  373. atomic64_dec(&pool->pages_nr);
  374. spin_unlock(&pool->lock);
  375. pr_err("No free chunks in unbuddied\n");
  376. WARN_ON(1);
  377. continue;
  378. }
  379. goto found;
  380. }
  381. bud = FIRST;
  382. }
  383. /* Couldn't find unbuddied z3fold page, create new one */
  384. page = alloc_page(gfp);
  385. if (!page)
  386. return -ENOMEM;
  387. atomic64_inc(&pool->pages_nr);
  388. zhdr = init_z3fold_page(page);
  389. if (bud == HEADLESS) {
  390. set_bit(PAGE_HEADLESS, &page->private);
  391. spin_lock(&pool->lock);
  392. goto headless;
  393. }
  394. z3fold_page_lock(zhdr);
  395. found:
  396. if (bud == FIRST)
  397. zhdr->first_chunks = chunks;
  398. else if (bud == LAST)
  399. zhdr->last_chunks = chunks;
  400. else {
  401. zhdr->middle_chunks = chunks;
  402. zhdr->start_middle = zhdr->first_chunks + ZHDR_CHUNKS;
  403. }
  404. spin_lock(&pool->lock);
  405. if (zhdr->first_chunks == 0 || zhdr->last_chunks == 0 ||
  406. zhdr->middle_chunks == 0) {
  407. /* Add to unbuddied list */
  408. freechunks = num_free_chunks(zhdr);
  409. list_add(&zhdr->buddy, &pool->unbuddied[freechunks]);
  410. }
  411. headless:
  412. /* Add/move z3fold page to beginning of LRU */
  413. if (!list_empty(&page->lru))
  414. list_del(&page->lru);
  415. list_add(&page->lru, &pool->lru);
  416. *handle = encode_handle(zhdr, bud);
  417. spin_unlock(&pool->lock);
  418. if (bud != HEADLESS)
  419. z3fold_page_unlock(zhdr);
  420. return 0;
  421. }
  422. /**
  423. * z3fold_free() - frees the allocation associated with the given handle
  424. * @pool: pool in which the allocation resided
  425. * @handle: handle associated with the allocation returned by z3fold_alloc()
  426. *
  427. * In the case that the z3fold page in which the allocation resides is under
  428. * reclaim, as indicated by the PG_reclaim flag being set, this function
  429. * only sets the first|last_chunks to 0. The page is actually freed
  430. * once both buddies are evicted (see z3fold_reclaim_page() below).
  431. */
  432. static void z3fold_free(struct z3fold_pool *pool, unsigned long handle)
  433. {
  434. struct z3fold_header *zhdr;
  435. int freechunks;
  436. struct page *page;
  437. enum buddy bud;
  438. zhdr = handle_to_z3fold_header(handle);
  439. page = virt_to_page(zhdr);
  440. if (test_bit(PAGE_HEADLESS, &page->private)) {
  441. /* HEADLESS page stored */
  442. bud = HEADLESS;
  443. } else {
  444. z3fold_page_lock(zhdr);
  445. bud = handle_to_buddy(handle);
  446. switch (bud) {
  447. case FIRST:
  448. zhdr->first_chunks = 0;
  449. break;
  450. case MIDDLE:
  451. zhdr->middle_chunks = 0;
  452. zhdr->start_middle = 0;
  453. break;
  454. case LAST:
  455. zhdr->last_chunks = 0;
  456. break;
  457. default:
  458. pr_err("%s: unknown bud %d\n", __func__, bud);
  459. WARN_ON(1);
  460. z3fold_page_unlock(zhdr);
  461. return;
  462. }
  463. }
  464. if (bud == HEADLESS) {
  465. spin_lock(&pool->lock);
  466. list_del(&page->lru);
  467. spin_unlock(&pool->lock);
  468. free_z3fold_page(page);
  469. atomic64_dec(&pool->pages_nr);
  470. } else {
  471. if (zhdr->first_chunks != 0 || zhdr->middle_chunks != 0 ||
  472. zhdr->last_chunks != 0) {
  473. z3fold_compact_page(zhdr);
  474. /* Add to the unbuddied list */
  475. spin_lock(&pool->lock);
  476. if (!list_empty(&zhdr->buddy))
  477. list_del(&zhdr->buddy);
  478. freechunks = num_free_chunks(zhdr);
  479. list_add(&zhdr->buddy, &pool->unbuddied[freechunks]);
  480. spin_unlock(&pool->lock);
  481. }
  482. z3fold_page_unlock(zhdr);
  483. spin_lock(&pool->lock);
  484. if (kref_put(&zhdr->refcount, release_z3fold_page))
  485. atomic64_dec(&pool->pages_nr);
  486. spin_unlock(&pool->lock);
  487. }
  488. }
  489. /**
  490. * z3fold_reclaim_page() - evicts allocations from a pool page and frees it
  491. * @pool: pool from which a page will attempt to be evicted
  492. * @retires: number of pages on the LRU list for which eviction will
  493. * be attempted before failing
  494. *
  495. * z3fold reclaim is different from normal system reclaim in that it is done
  496. * from the bottom, up. This is because only the bottom layer, z3fold, has
  497. * information on how the allocations are organized within each z3fold page.
  498. * This has the potential to create interesting locking situations between
  499. * z3fold and the user, however.
  500. *
  501. * To avoid these, this is how z3fold_reclaim_page() should be called:
  502. * The user detects a page should be reclaimed and calls z3fold_reclaim_page().
  503. * z3fold_reclaim_page() will remove a z3fold page from the pool LRU list and
  504. * call the user-defined eviction handler with the pool and handle as
  505. * arguments.
  506. *
  507. * If the handle can not be evicted, the eviction handler should return
  508. * non-zero. z3fold_reclaim_page() will add the z3fold page back to the
  509. * appropriate list and try the next z3fold page on the LRU up to
  510. * a user defined number of retries.
  511. *
  512. * If the handle is successfully evicted, the eviction handler should
  513. * return 0 _and_ should have called z3fold_free() on the handle. z3fold_free()
  514. * contains logic to delay freeing the page if the page is under reclaim,
  515. * as indicated by the setting of the PG_reclaim flag on the underlying page.
  516. *
  517. * If all buddies in the z3fold page are successfully evicted, then the
  518. * z3fold page can be freed.
  519. *
  520. * Returns: 0 if page is successfully freed, otherwise -EINVAL if there are
  521. * no pages to evict or an eviction handler is not registered, -EAGAIN if
  522. * the retry limit was hit.
  523. */
  524. static int z3fold_reclaim_page(struct z3fold_pool *pool, unsigned int retries)
  525. {
  526. int i, ret = 0, freechunks;
  527. struct z3fold_header *zhdr;
  528. struct page *page;
  529. unsigned long first_handle = 0, middle_handle = 0, last_handle = 0;
  530. spin_lock(&pool->lock);
  531. if (!pool->ops || !pool->ops->evict || retries == 0) {
  532. spin_unlock(&pool->lock);
  533. return -EINVAL;
  534. }
  535. for (i = 0; i < retries; i++) {
  536. if (list_empty(&pool->lru)) {
  537. spin_unlock(&pool->lock);
  538. return -EINVAL;
  539. }
  540. page = list_last_entry(&pool->lru, struct page, lru);
  541. list_del_init(&page->lru);
  542. zhdr = page_address(page);
  543. if (!test_bit(PAGE_HEADLESS, &page->private)) {
  544. if (!list_empty(&zhdr->buddy))
  545. list_del_init(&zhdr->buddy);
  546. kref_get(&zhdr->refcount);
  547. spin_unlock(&pool->lock);
  548. z3fold_page_lock(zhdr);
  549. /*
  550. * We need encode the handles before unlocking, since
  551. * we can race with free that will set
  552. * (first|last)_chunks to 0
  553. */
  554. first_handle = 0;
  555. last_handle = 0;
  556. middle_handle = 0;
  557. if (zhdr->first_chunks)
  558. first_handle = encode_handle(zhdr, FIRST);
  559. if (zhdr->middle_chunks)
  560. middle_handle = encode_handle(zhdr, MIDDLE);
  561. if (zhdr->last_chunks)
  562. last_handle = encode_handle(zhdr, LAST);
  563. z3fold_page_unlock(zhdr);
  564. } else {
  565. first_handle = encode_handle(zhdr, HEADLESS);
  566. last_handle = middle_handle = 0;
  567. spin_unlock(&pool->lock);
  568. }
  569. /* Issue the eviction callback(s) */
  570. if (middle_handle) {
  571. ret = pool->ops->evict(pool, middle_handle);
  572. if (ret)
  573. goto next;
  574. }
  575. if (first_handle) {
  576. ret = pool->ops->evict(pool, first_handle);
  577. if (ret)
  578. goto next;
  579. }
  580. if (last_handle) {
  581. ret = pool->ops->evict(pool, last_handle);
  582. if (ret)
  583. goto next;
  584. }
  585. next:
  586. if (test_bit(PAGE_HEADLESS, &page->private)) {
  587. if (ret == 0) {
  588. free_z3fold_page(page);
  589. return 0;
  590. } else {
  591. spin_lock(&pool->lock);
  592. }
  593. } else {
  594. z3fold_page_lock(zhdr);
  595. if ((zhdr->first_chunks || zhdr->last_chunks ||
  596. zhdr->middle_chunks) &&
  597. !(zhdr->first_chunks && zhdr->last_chunks &&
  598. zhdr->middle_chunks)) {
  599. z3fold_compact_page(zhdr);
  600. /* add to unbuddied list */
  601. spin_lock(&pool->lock);
  602. freechunks = num_free_chunks(zhdr);
  603. list_add(&zhdr->buddy,
  604. &pool->unbuddied[freechunks]);
  605. spin_unlock(&pool->lock);
  606. }
  607. z3fold_page_unlock(zhdr);
  608. spin_lock(&pool->lock);
  609. if (kref_put(&zhdr->refcount, release_z3fold_page)) {
  610. spin_unlock(&pool->lock);
  611. atomic64_dec(&pool->pages_nr);
  612. return 0;
  613. }
  614. }
  615. /*
  616. * Add to the beginning of LRU.
  617. * Pool lock has to be kept here to ensure the page has
  618. * not already been released
  619. */
  620. list_add(&page->lru, &pool->lru);
  621. }
  622. spin_unlock(&pool->lock);
  623. return -EAGAIN;
  624. }
  625. /**
  626. * z3fold_map() - maps the allocation associated with the given handle
  627. * @pool: pool in which the allocation resides
  628. * @handle: handle associated with the allocation to be mapped
  629. *
  630. * Extracts the buddy number from handle and constructs the pointer to the
  631. * correct starting chunk within the page.
  632. *
  633. * Returns: a pointer to the mapped allocation
  634. */
  635. static void *z3fold_map(struct z3fold_pool *pool, unsigned long handle)
  636. {
  637. struct z3fold_header *zhdr;
  638. struct page *page;
  639. void *addr;
  640. enum buddy buddy;
  641. zhdr = handle_to_z3fold_header(handle);
  642. addr = zhdr;
  643. page = virt_to_page(zhdr);
  644. if (test_bit(PAGE_HEADLESS, &page->private))
  645. goto out;
  646. z3fold_page_lock(zhdr);
  647. buddy = handle_to_buddy(handle);
  648. switch (buddy) {
  649. case FIRST:
  650. addr += ZHDR_SIZE_ALIGNED;
  651. break;
  652. case MIDDLE:
  653. addr += zhdr->start_middle << CHUNK_SHIFT;
  654. set_bit(MIDDLE_CHUNK_MAPPED, &page->private);
  655. break;
  656. case LAST:
  657. addr += PAGE_SIZE - (zhdr->last_chunks << CHUNK_SHIFT);
  658. break;
  659. default:
  660. pr_err("unknown buddy id %d\n", buddy);
  661. WARN_ON(1);
  662. addr = NULL;
  663. break;
  664. }
  665. z3fold_page_unlock(zhdr);
  666. out:
  667. return addr;
  668. }
  669. /**
  670. * z3fold_unmap() - unmaps the allocation associated with the given handle
  671. * @pool: pool in which the allocation resides
  672. * @handle: handle associated with the allocation to be unmapped
  673. */
  674. static void z3fold_unmap(struct z3fold_pool *pool, unsigned long handle)
  675. {
  676. struct z3fold_header *zhdr;
  677. struct page *page;
  678. enum buddy buddy;
  679. zhdr = handle_to_z3fold_header(handle);
  680. page = virt_to_page(zhdr);
  681. if (test_bit(PAGE_HEADLESS, &page->private))
  682. return;
  683. z3fold_page_lock(zhdr);
  684. buddy = handle_to_buddy(handle);
  685. if (buddy == MIDDLE)
  686. clear_bit(MIDDLE_CHUNK_MAPPED, &page->private);
  687. z3fold_page_unlock(zhdr);
  688. }
  689. /**
  690. * z3fold_get_pool_size() - gets the z3fold pool size in pages
  691. * @pool: pool whose size is being queried
  692. *
  693. * Returns: size in pages of the given pool.
  694. */
  695. static u64 z3fold_get_pool_size(struct z3fold_pool *pool)
  696. {
  697. return atomic64_read(&pool->pages_nr);
  698. }
  699. /*****************
  700. * zpool
  701. ****************/
  702. static int z3fold_zpool_evict(struct z3fold_pool *pool, unsigned long handle)
  703. {
  704. if (pool->zpool && pool->zpool_ops && pool->zpool_ops->evict)
  705. return pool->zpool_ops->evict(pool->zpool, handle);
  706. else
  707. return -ENOENT;
  708. }
  709. static const struct z3fold_ops z3fold_zpool_ops = {
  710. .evict = z3fold_zpool_evict
  711. };
  712. static void *z3fold_zpool_create(const char *name, gfp_t gfp,
  713. const struct zpool_ops *zpool_ops,
  714. struct zpool *zpool)
  715. {
  716. struct z3fold_pool *pool;
  717. pool = z3fold_create_pool(gfp, zpool_ops ? &z3fold_zpool_ops : NULL);
  718. if (pool) {
  719. pool->zpool = zpool;
  720. pool->zpool_ops = zpool_ops;
  721. }
  722. return pool;
  723. }
  724. static void z3fold_zpool_destroy(void *pool)
  725. {
  726. z3fold_destroy_pool(pool);
  727. }
  728. static int z3fold_zpool_malloc(void *pool, size_t size, gfp_t gfp,
  729. unsigned long *handle)
  730. {
  731. return z3fold_alloc(pool, size, gfp, handle);
  732. }
  733. static void z3fold_zpool_free(void *pool, unsigned long handle)
  734. {
  735. z3fold_free(pool, handle);
  736. }
  737. static int z3fold_zpool_shrink(void *pool, unsigned int pages,
  738. unsigned int *reclaimed)
  739. {
  740. unsigned int total = 0;
  741. int ret = -EINVAL;
  742. while (total < pages) {
  743. ret = z3fold_reclaim_page(pool, 8);
  744. if (ret < 0)
  745. break;
  746. total++;
  747. }
  748. if (reclaimed)
  749. *reclaimed = total;
  750. return ret;
  751. }
  752. static void *z3fold_zpool_map(void *pool, unsigned long handle,
  753. enum zpool_mapmode mm)
  754. {
  755. return z3fold_map(pool, handle);
  756. }
  757. static void z3fold_zpool_unmap(void *pool, unsigned long handle)
  758. {
  759. z3fold_unmap(pool, handle);
  760. }
  761. static u64 z3fold_zpool_total_size(void *pool)
  762. {
  763. return z3fold_get_pool_size(pool) * PAGE_SIZE;
  764. }
  765. static struct zpool_driver z3fold_zpool_driver = {
  766. .type = "z3fold",
  767. .owner = THIS_MODULE,
  768. .create = z3fold_zpool_create,
  769. .destroy = z3fold_zpool_destroy,
  770. .malloc = z3fold_zpool_malloc,
  771. .free = z3fold_zpool_free,
  772. .shrink = z3fold_zpool_shrink,
  773. .map = z3fold_zpool_map,
  774. .unmap = z3fold_zpool_unmap,
  775. .total_size = z3fold_zpool_total_size,
  776. };
  777. MODULE_ALIAS("zpool-z3fold");
  778. static int __init init_z3fold(void)
  779. {
  780. /* Make sure the z3fold header is not larger than the page size */
  781. BUILD_BUG_ON(ZHDR_SIZE_ALIGNED > PAGE_SIZE);
  782. zpool_register_driver(&z3fold_zpool_driver);
  783. return 0;
  784. }
  785. static void __exit exit_z3fold(void)
  786. {
  787. zpool_unregister_driver(&z3fold_zpool_driver);
  788. }
  789. module_init(init_z3fold);
  790. module_exit(exit_z3fold);
  791. MODULE_LICENSE("GPL");
  792. MODULE_AUTHOR("Vitaly Wool <vitalywool@gmail.com>");
  793. MODULE_DESCRIPTION("3-Fold Allocator for Compressed Pages");