4 * Author: Vitaly Wool <vitaly.wool@konsulko.com>
5 * Copyright (C) 2016, Sony Mobile Communications Inc.
7 * This implementation is based on zbud written by Seth Jennings.
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.
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.
20 * z3fold doesn't export any API and is meant to be used via zpool API.
23 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25 #include <linux/atomic.h>
26 #include <linux/list.h>
28 #include <linux/module.h>
29 #include <linux/preempt.h>
30 #include <linux/slab.h>
31 #include <linux/spinlock.h>
32 #include <linux/zpool.h>
39 int (*evict)(struct z3fold_pool *pool, unsigned long handle);
51 * struct z3fold_header - z3fold page metadata occupying the first chunk of each
52 * z3fold page, except for HEADLESS pages
53 * @buddy: links the z3fold page into the relevant list in the pool
54 * @first_chunks: the size of the first buddy in chunks, 0 if free
55 * @middle_chunks: the size of the middle buddy in chunks, 0 if free
56 * @last_chunks: the size of the last buddy in chunks, 0 if free
57 * @first_num: the starting number (for the first handle)
59 struct z3fold_header {
60 struct list_head buddy;
61 unsigned short first_chunks;
62 unsigned short middle_chunks;
63 unsigned short last_chunks;
64 unsigned short start_middle;
65 unsigned short first_num:2;
69 * NCHUNKS_ORDER determines the internal allocation granularity, effectively
70 * adjusting internal fragmentation. It also determines the number of
71 * freelists maintained in each pool. NCHUNKS_ORDER of 6 means that the
72 * allocation granularity will be in chunks of size PAGE_SIZE/64. Some chunks
73 * in the beginning of an allocated page are occupied by z3fold header, so
74 * NCHUNKS will be calculated to 63 (or 62 in case CONFIG_DEBUG_SPINLOCK=y),
75 * which shows the max number of free chunks in z3fold page, also there will
76 * be 63, or 62, respectively, freelists per pool.
78 #define NCHUNKS_ORDER 6
80 #define CHUNK_SHIFT (PAGE_SHIFT - NCHUNKS_ORDER)
81 #define CHUNK_SIZE (1 << CHUNK_SHIFT)
82 #define ZHDR_SIZE_ALIGNED round_up(sizeof(struct z3fold_header), CHUNK_SIZE)
83 #define ZHDR_CHUNKS (ZHDR_SIZE_ALIGNED >> CHUNK_SHIFT)
84 #define TOTAL_CHUNKS (PAGE_SIZE >> CHUNK_SHIFT)
85 #define NCHUNKS ((PAGE_SIZE - ZHDR_SIZE_ALIGNED) >> CHUNK_SHIFT)
87 #define BUDDY_MASK (0x3)
90 * struct z3fold_pool - stores metadata for each z3fold pool
91 * @lock: protects all pool fields and first|last_chunk fields of any
92 * z3fold page in the pool
93 * @unbuddied: array of lists tracking z3fold pages that contain 2- buddies;
94 * the lists each z3fold page is added to depends on the size of
96 * @buddied: list tracking the z3fold pages that contain 3 buddies;
97 * these z3fold pages are full
98 * @lru: list tracking the z3fold pages in LRU order by most recently
100 * @pages_nr: number of z3fold pages in the pool.
101 * @ops: pointer to a structure of user defined operations specified at
102 * pool creation time.
104 * This structure is allocated at pool creation time and maintains metadata
105 * pertaining to a particular z3fold pool.
109 struct list_head unbuddied[NCHUNKS];
110 struct list_head buddied;
111 struct list_head lru;
113 const struct z3fold_ops *ops;
115 const struct zpool_ops *zpool_ops;
119 * Internal z3fold page flags
121 enum z3fold_page_flags {
132 /* Converts an allocation size in bytes to size in z3fold chunks */
133 static int size_to_chunks(size_t size)
135 return (size + CHUNK_SIZE - 1) >> CHUNK_SHIFT;
138 #define for_each_unbuddied_list(_iter, _begin) \
139 for ((_iter) = (_begin); (_iter) < NCHUNKS; (_iter)++)
141 /* Initializes the z3fold header of a newly allocated z3fold page */
142 static struct z3fold_header *init_z3fold_page(struct page *page)
144 struct z3fold_header *zhdr = page_address(page);
146 INIT_LIST_HEAD(&page->lru);
147 clear_bit(UNDER_RECLAIM, &page->private);
148 clear_bit(PAGE_HEADLESS, &page->private);
149 clear_bit(MIDDLE_CHUNK_MAPPED, &page->private);
151 zhdr->first_chunks = 0;
152 zhdr->middle_chunks = 0;
153 zhdr->last_chunks = 0;
155 zhdr->start_middle = 0;
156 INIT_LIST_HEAD(&zhdr->buddy);
160 /* Resets the struct page fields and frees the page */
161 static void free_z3fold_page(struct z3fold_header *zhdr)
163 __free_page(virt_to_page(zhdr));
167 * Encodes the handle of a particular buddy within a z3fold page
168 * Pool lock should be held as this function accesses first_num
170 static unsigned long encode_handle(struct z3fold_header *zhdr, enum buddy bud)
172 unsigned long handle;
174 handle = (unsigned long)zhdr;
176 handle += (bud + zhdr->first_num) & BUDDY_MASK;
180 /* Returns the z3fold page where a given handle is stored */
181 static struct z3fold_header *handle_to_z3fold_header(unsigned long handle)
183 return (struct z3fold_header *)(handle & PAGE_MASK);
187 * (handle & BUDDY_MASK) < zhdr->first_num is possible in encode_handle
188 * but that doesn't matter. because the masking will result in the
189 * correct buddy number.
191 static enum buddy handle_to_buddy(unsigned long handle)
193 struct z3fold_header *zhdr = handle_to_z3fold_header(handle);
194 return (handle - zhdr->first_num) & BUDDY_MASK;
198 * Returns the number of free chunks in a z3fold page.
199 * NB: can't be used with HEADLESS pages.
201 static int num_free_chunks(struct z3fold_header *zhdr)
205 * If there is a middle object, pick up the bigger free space
206 * either before or after it. Otherwise just subtract the number
207 * of chunks occupied by the first and the last objects.
209 if (zhdr->middle_chunks != 0) {
210 int nfree_before = zhdr->first_chunks ?
211 0 : zhdr->start_middle - ZHDR_CHUNKS;
212 int nfree_after = zhdr->last_chunks ?
214 (zhdr->start_middle + zhdr->middle_chunks);
215 nfree = max(nfree_before, nfree_after);
217 nfree = NCHUNKS - zhdr->first_chunks - zhdr->last_chunks;
225 * z3fold_create_pool() - create a new z3fold pool
226 * @gfp: gfp flags when allocating the z3fold pool structure
227 * @ops: user-defined operations for the z3fold pool
229 * Return: pointer to the new z3fold pool or NULL if the metadata allocation
232 static struct z3fold_pool *z3fold_create_pool(gfp_t gfp,
233 const struct z3fold_ops *ops)
235 struct z3fold_pool *pool;
238 pool = kzalloc(sizeof(struct z3fold_pool), gfp);
241 spin_lock_init(&pool->lock);
242 for_each_unbuddied_list(i, 0)
243 INIT_LIST_HEAD(&pool->unbuddied[i]);
244 INIT_LIST_HEAD(&pool->buddied);
245 INIT_LIST_HEAD(&pool->lru);
246 atomic64_set(&pool->pages_nr, 0);
252 * z3fold_destroy_pool() - destroys an existing z3fold pool
253 * @pool: the z3fold pool to be destroyed
255 * The pool should be emptied before this function is called.
257 static void z3fold_destroy_pool(struct z3fold_pool *pool)
262 static inline void *mchunk_memmove(struct z3fold_header *zhdr,
263 unsigned short dst_chunk)
266 return memmove(beg + (dst_chunk << CHUNK_SHIFT),
267 beg + (zhdr->start_middle << CHUNK_SHIFT),
268 zhdr->middle_chunks << CHUNK_SHIFT);
271 /* Has to be called with lock held */
272 static int z3fold_compact_page(struct z3fold_header *zhdr)
274 struct page *page = virt_to_page(zhdr);
276 if (test_bit(MIDDLE_CHUNK_MAPPED, &page->private))
277 return 0; /* can't move middle chunk, it's used */
279 if (zhdr->middle_chunks == 0)
280 return 0; /* nothing to compact */
282 if (zhdr->first_chunks == 0 && zhdr->last_chunks == 0) {
283 /* move to the beginning */
284 mchunk_memmove(zhdr, ZHDR_CHUNKS);
285 zhdr->first_chunks = zhdr->middle_chunks;
286 zhdr->middle_chunks = 0;
287 zhdr->start_middle = 0;
294 * z3fold_alloc() - allocates a region of a given size
295 * @pool: z3fold pool from which to allocate
296 * @size: size in bytes of the desired allocation
297 * @gfp: gfp flags used if the pool needs to grow
298 * @handle: handle of the new allocation
300 * This function will attempt to find a free region in the pool large enough to
301 * satisfy the allocation request. A search of the unbuddied lists is
302 * performed first. If no suitable free region is found, then a new page is
303 * allocated and added to the pool to satisfy the request.
305 * gfp should not set __GFP_HIGHMEM as highmem pages cannot be used
306 * as z3fold pool pages.
308 * Return: 0 if success and handle is set, otherwise -EINVAL if the size or
309 * gfp arguments are invalid or -ENOMEM if the pool was unable to allocate
312 static int z3fold_alloc(struct z3fold_pool *pool, size_t size, gfp_t gfp,
313 unsigned long *handle)
315 int chunks = 0, i, freechunks;
316 struct z3fold_header *zhdr = NULL;
320 if (!size || (gfp & __GFP_HIGHMEM))
323 if (size > PAGE_SIZE)
326 if (size > PAGE_SIZE - ZHDR_SIZE_ALIGNED - CHUNK_SIZE)
329 chunks = size_to_chunks(size);
330 spin_lock(&pool->lock);
332 /* First, try to find an unbuddied z3fold page. */
334 for_each_unbuddied_list(i, chunks) {
335 if (!list_empty(&pool->unbuddied[i])) {
336 zhdr = list_first_entry(&pool->unbuddied[i],
337 struct z3fold_header, buddy);
338 page = virt_to_page(zhdr);
339 if (zhdr->first_chunks == 0) {
340 if (zhdr->middle_chunks != 0 &&
341 chunks >= zhdr->start_middle)
345 } else if (zhdr->last_chunks == 0)
347 else if (zhdr->middle_chunks == 0)
350 pr_err("No free chunks in unbuddied\n");
354 list_del(&zhdr->buddy);
359 spin_unlock(&pool->lock);
362 /* Couldn't find unbuddied z3fold page, create new one */
363 page = alloc_page(gfp);
366 spin_lock(&pool->lock);
367 atomic64_inc(&pool->pages_nr);
368 zhdr = init_z3fold_page(page);
370 if (bud == HEADLESS) {
371 set_bit(PAGE_HEADLESS, &page->private);
377 zhdr->first_chunks = chunks;
378 else if (bud == LAST)
379 zhdr->last_chunks = chunks;
381 zhdr->middle_chunks = chunks;
382 zhdr->start_middle = zhdr->first_chunks + ZHDR_CHUNKS;
385 if (zhdr->first_chunks == 0 || zhdr->last_chunks == 0 ||
386 zhdr->middle_chunks == 0) {
387 /* Add to unbuddied list */
388 freechunks = num_free_chunks(zhdr);
389 list_add(&zhdr->buddy, &pool->unbuddied[freechunks]);
391 /* Add to buddied list */
392 list_add(&zhdr->buddy, &pool->buddied);
396 /* Add/move z3fold page to beginning of LRU */
397 if (!list_empty(&page->lru))
398 list_del(&page->lru);
400 list_add(&page->lru, &pool->lru);
402 *handle = encode_handle(zhdr, bud);
403 spin_unlock(&pool->lock);
409 * z3fold_free() - frees the allocation associated with the given handle
410 * @pool: pool in which the allocation resided
411 * @handle: handle associated with the allocation returned by z3fold_alloc()
413 * In the case that the z3fold page in which the allocation resides is under
414 * reclaim, as indicated by the PG_reclaim flag being set, this function
415 * only sets the first|last_chunks to 0. The page is actually freed
416 * once both buddies are evicted (see z3fold_reclaim_page() below).
418 static void z3fold_free(struct z3fold_pool *pool, unsigned long handle)
420 struct z3fold_header *zhdr;
425 spin_lock(&pool->lock);
426 zhdr = handle_to_z3fold_header(handle);
427 page = virt_to_page(zhdr);
429 if (test_bit(PAGE_HEADLESS, &page->private)) {
430 /* HEADLESS page stored */
433 bud = handle_to_buddy(handle);
437 zhdr->first_chunks = 0;
440 zhdr->middle_chunks = 0;
441 zhdr->start_middle = 0;
444 zhdr->last_chunks = 0;
447 pr_err("%s: unknown bud %d\n", __func__, bud);
449 spin_unlock(&pool->lock);
454 if (test_bit(UNDER_RECLAIM, &page->private)) {
455 /* z3fold page is under reclaim, reclaim will free */
456 spin_unlock(&pool->lock);
460 /* Remove from existing buddy list */
462 list_del(&zhdr->buddy);
464 if (bud == HEADLESS ||
465 (zhdr->first_chunks == 0 && zhdr->middle_chunks == 0 &&
466 zhdr->last_chunks == 0)) {
467 /* z3fold page is empty, free */
468 list_del(&page->lru);
469 clear_bit(PAGE_HEADLESS, &page->private);
470 free_z3fold_page(zhdr);
471 atomic64_dec(&pool->pages_nr);
473 z3fold_compact_page(zhdr);
474 /* Add to the unbuddied list */
475 freechunks = num_free_chunks(zhdr);
476 list_add(&zhdr->buddy, &pool->unbuddied[freechunks]);
479 spin_unlock(&pool->lock);
483 * z3fold_reclaim_page() - evicts allocations from a pool page and frees it
484 * @pool: pool from which a page will attempt to be evicted
485 * @retires: number of pages on the LRU list for which eviction will
486 * be attempted before failing
488 * z3fold reclaim is different from normal system reclaim in that it is done
489 * from the bottom, up. This is because only the bottom layer, z3fold, has
490 * information on how the allocations are organized within each z3fold page.
491 * This has the potential to create interesting locking situations between
492 * z3fold and the user, however.
494 * To avoid these, this is how z3fold_reclaim_page() should be called:
496 * The user detects a page should be reclaimed and calls z3fold_reclaim_page().
497 * z3fold_reclaim_page() will remove a z3fold page from the pool LRU list and
498 * call the user-defined eviction handler with the pool and handle as
501 * If the handle can not be evicted, the eviction handler should return
502 * non-zero. z3fold_reclaim_page() will add the z3fold page back to the
503 * appropriate list and try the next z3fold page on the LRU up to
504 * a user defined number of retries.
506 * If the handle is successfully evicted, the eviction handler should
507 * return 0 _and_ should have called z3fold_free() on the handle. z3fold_free()
508 * contains logic to delay freeing the page if the page is under reclaim,
509 * as indicated by the setting of the PG_reclaim flag on the underlying page.
511 * If all buddies in the z3fold page are successfully evicted, then the
512 * z3fold page can be freed.
514 * Returns: 0 if page is successfully freed, otherwise -EINVAL if there are
515 * no pages to evict or an eviction handler is not registered, -EAGAIN if
516 * the retry limit was hit.
518 static int z3fold_reclaim_page(struct z3fold_pool *pool, unsigned int retries)
520 int i, ret = 0, freechunks;
521 struct z3fold_header *zhdr;
523 unsigned long first_handle = 0, middle_handle = 0, last_handle = 0;
525 spin_lock(&pool->lock);
526 if (!pool->ops || !pool->ops->evict || list_empty(&pool->lru) ||
528 spin_unlock(&pool->lock);
531 for (i = 0; i < retries; i++) {
532 page = list_last_entry(&pool->lru, struct page, lru);
533 list_del(&page->lru);
535 /* Protect z3fold page against free */
536 set_bit(UNDER_RECLAIM, &page->private);
537 zhdr = page_address(page);
538 if (!test_bit(PAGE_HEADLESS, &page->private)) {
539 list_del(&zhdr->buddy);
541 * We need encode the handles before unlocking, since
542 * we can race with free that will set
543 * (first|last)_chunks to 0
548 if (zhdr->first_chunks)
549 first_handle = encode_handle(zhdr, FIRST);
550 if (zhdr->middle_chunks)
551 middle_handle = encode_handle(zhdr, MIDDLE);
552 if (zhdr->last_chunks)
553 last_handle = encode_handle(zhdr, LAST);
555 first_handle = encode_handle(zhdr, HEADLESS);
556 last_handle = middle_handle = 0;
559 spin_unlock(&pool->lock);
561 /* Issue the eviction callback(s) */
563 ret = pool->ops->evict(pool, middle_handle);
568 ret = pool->ops->evict(pool, first_handle);
573 ret = pool->ops->evict(pool, last_handle);
578 spin_lock(&pool->lock);
579 clear_bit(UNDER_RECLAIM, &page->private);
580 if ((test_bit(PAGE_HEADLESS, &page->private) && ret == 0) ||
581 (zhdr->first_chunks == 0 && zhdr->last_chunks == 0 &&
582 zhdr->middle_chunks == 0)) {
584 * All buddies are now free, free the z3fold page and
587 clear_bit(PAGE_HEADLESS, &page->private);
588 free_z3fold_page(zhdr);
589 atomic64_dec(&pool->pages_nr);
590 spin_unlock(&pool->lock);
592 } else if (!test_bit(PAGE_HEADLESS, &page->private)) {
593 if (zhdr->first_chunks != 0 &&
594 zhdr->last_chunks != 0 &&
595 zhdr->middle_chunks != 0) {
596 /* Full, add to buddied list */
597 list_add(&zhdr->buddy, &pool->buddied);
599 z3fold_compact_page(zhdr);
600 /* add to unbuddied list */
601 freechunks = num_free_chunks(zhdr);
602 list_add(&zhdr->buddy,
603 &pool->unbuddied[freechunks]);
607 /* add to beginning of LRU */
608 list_add(&page->lru, &pool->lru);
610 spin_unlock(&pool->lock);
615 * z3fold_map() - maps the allocation associated with the given handle
616 * @pool: pool in which the allocation resides
617 * @handle: handle associated with the allocation to be mapped
619 * Extracts the buddy number from handle and constructs the pointer to the
620 * correct starting chunk within the page.
622 * Returns: a pointer to the mapped allocation
624 static void *z3fold_map(struct z3fold_pool *pool, unsigned long handle)
626 struct z3fold_header *zhdr;
631 spin_lock(&pool->lock);
632 zhdr = handle_to_z3fold_header(handle);
634 page = virt_to_page(zhdr);
636 if (test_bit(PAGE_HEADLESS, &page->private))
639 buddy = handle_to_buddy(handle);
642 addr += ZHDR_SIZE_ALIGNED;
645 addr += zhdr->start_middle << CHUNK_SHIFT;
646 set_bit(MIDDLE_CHUNK_MAPPED, &page->private);
649 addr += PAGE_SIZE - (zhdr->last_chunks << CHUNK_SHIFT);
652 pr_err("unknown buddy id %d\n", buddy);
658 spin_unlock(&pool->lock);
663 * z3fold_unmap() - unmaps the allocation associated with the given handle
664 * @pool: pool in which the allocation resides
665 * @handle: handle associated with the allocation to be unmapped
667 static void z3fold_unmap(struct z3fold_pool *pool, unsigned long handle)
669 struct z3fold_header *zhdr;
673 spin_lock(&pool->lock);
674 zhdr = handle_to_z3fold_header(handle);
675 page = virt_to_page(zhdr);
677 if (test_bit(PAGE_HEADLESS, &page->private)) {
678 spin_unlock(&pool->lock);
682 buddy = handle_to_buddy(handle);
684 clear_bit(MIDDLE_CHUNK_MAPPED, &page->private);
685 spin_unlock(&pool->lock);
689 * z3fold_get_pool_size() - gets the z3fold pool size in pages
690 * @pool: pool whose size is being queried
692 * Returns: size in pages of the given pool.
694 static u64 z3fold_get_pool_size(struct z3fold_pool *pool)
696 return atomic64_read(&pool->pages_nr);
703 static int z3fold_zpool_evict(struct z3fold_pool *pool, unsigned long handle)
705 if (pool->zpool && pool->zpool_ops && pool->zpool_ops->evict)
706 return pool->zpool_ops->evict(pool->zpool, handle);
711 static const struct z3fold_ops z3fold_zpool_ops = {
712 .evict = z3fold_zpool_evict
715 static void *z3fold_zpool_create(const char *name, gfp_t gfp,
716 const struct zpool_ops *zpool_ops,
719 struct z3fold_pool *pool;
721 pool = z3fold_create_pool(gfp, zpool_ops ? &z3fold_zpool_ops : NULL);
724 pool->zpool_ops = zpool_ops;
729 static void z3fold_zpool_destroy(void *pool)
731 z3fold_destroy_pool(pool);
734 static int z3fold_zpool_malloc(void *pool, size_t size, gfp_t gfp,
735 unsigned long *handle)
737 return z3fold_alloc(pool, size, gfp, handle);
739 static void z3fold_zpool_free(void *pool, unsigned long handle)
741 z3fold_free(pool, handle);
744 static int z3fold_zpool_shrink(void *pool, unsigned int pages,
745 unsigned int *reclaimed)
747 unsigned int total = 0;
750 while (total < pages) {
751 ret = z3fold_reclaim_page(pool, 8);
763 static void *z3fold_zpool_map(void *pool, unsigned long handle,
764 enum zpool_mapmode mm)
766 return z3fold_map(pool, handle);
768 static void z3fold_zpool_unmap(void *pool, unsigned long handle)
770 z3fold_unmap(pool, handle);
773 static u64 z3fold_zpool_total_size(void *pool)
775 return z3fold_get_pool_size(pool) * PAGE_SIZE;
778 static struct zpool_driver z3fold_zpool_driver = {
780 .owner = THIS_MODULE,
781 .create = z3fold_zpool_create,
782 .destroy = z3fold_zpool_destroy,
783 .malloc = z3fold_zpool_malloc,
784 .free = z3fold_zpool_free,
785 .shrink = z3fold_zpool_shrink,
786 .map = z3fold_zpool_map,
787 .unmap = z3fold_zpool_unmap,
788 .total_size = z3fold_zpool_total_size,
791 MODULE_ALIAS("zpool-z3fold");
793 static int __init init_z3fold(void)
795 /* Make sure the z3fold header is not larger than the page size */
796 BUILD_BUG_ON(ZHDR_SIZE_ALIGNED > PAGE_SIZE);
797 zpool_register_driver(&z3fold_zpool_driver);
802 static void __exit exit_z3fold(void)
804 zpool_unregister_driver(&z3fold_zpool_driver);
807 module_init(init_z3fold);
808 module_exit(exit_z3fold);
810 MODULE_LICENSE("GPL");
811 MODULE_AUTHOR("Vitaly Wool <vitalywool@gmail.com>");
812 MODULE_DESCRIPTION("3-Fold Allocator for Compressed Pages");