1 // SPDX-License-Identifier: GPL-2.0-only
5 * Author: Vitaly Wool <vitaly.wool@konsulko.com>
6 * Copyright (C) 2016, Sony Mobile Communications Inc.
8 * This implementation is based on zbud written by Seth Jennings.
10 * z3fold is an special purpose allocator for storing compressed pages. It
11 * can store up to three compressed pages per page which improves the
12 * compression ratio of zbud while retaining its main concepts (e. g. always
13 * storing an integral number of objects per page) and simplicity.
14 * It still has simple and deterministic reclaim properties that make it
15 * preferable to a higher density approach (with no requirement on integral
16 * number of object per page) when reclaim is used.
18 * As in zbud, pages are divided into "chunks". The size of the chunks is
19 * fixed at compile time and is determined by NCHUNKS_ORDER below.
21 * z3fold doesn't export any API and is meant to be used via zpool API.
24 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26 #include <linux/atomic.h>
27 #include <linux/sched.h>
28 #include <linux/cpumask.h>
29 #include <linux/list.h>
31 #include <linux/module.h>
32 #include <linux/page-flags.h>
33 #include <linux/migrate.h>
34 #include <linux/node.h>
35 #include <linux/compaction.h>
36 #include <linux/percpu.h>
37 #include <linux/mount.h>
38 #include <linux/pseudo_fs.h>
40 #include <linux/preempt.h>
41 #include <linux/workqueue.h>
42 #include <linux/slab.h>
43 #include <linux/spinlock.h>
44 #include <linux/zpool.h>
45 #include <linux/magic.h>
48 * NCHUNKS_ORDER determines the internal allocation granularity, effectively
49 * adjusting internal fragmentation. It also determines the number of
50 * freelists maintained in each pool. NCHUNKS_ORDER of 6 means that the
51 * allocation granularity will be in chunks of size PAGE_SIZE/64. Some chunks
52 * in the beginning of an allocated page are occupied by z3fold header, so
53 * NCHUNKS will be calculated to 63 (or 62 in case CONFIG_DEBUG_SPINLOCK=y),
54 * which shows the max number of free chunks in z3fold page, also there will
55 * be 63, or 62, respectively, freelists per pool.
57 #define NCHUNKS_ORDER 6
59 #define CHUNK_SHIFT (PAGE_SHIFT - NCHUNKS_ORDER)
60 #define CHUNK_SIZE (1 << CHUNK_SHIFT)
61 #define ZHDR_SIZE_ALIGNED round_up(sizeof(struct z3fold_header), CHUNK_SIZE)
62 #define ZHDR_CHUNKS (ZHDR_SIZE_ALIGNED >> CHUNK_SHIFT)
63 #define TOTAL_CHUNKS (PAGE_SIZE >> CHUNK_SHIFT)
64 #define NCHUNKS ((PAGE_SIZE - ZHDR_SIZE_ALIGNED) >> CHUNK_SHIFT)
66 #define BUDDY_MASK (0x3)
68 #define SLOTS_ALIGN (0x40)
75 int (*evict
)(struct z3fold_pool
*pool
, unsigned long handle
);
86 struct z3fold_buddy_slots
{
88 * we are using BUDDY_MASK in handle_to_buddy etc. so there should
89 * be enough slots to hold all possible variants
91 unsigned long slot
[BUDDY_MASK
+ 1];
92 unsigned long pool
; /* back link + flags */
95 #define HANDLE_FLAG_MASK (0x03)
98 * struct z3fold_header - z3fold page metadata occupying first chunks of each
99 * z3fold page, except for HEADLESS pages
100 * @buddy: links the z3fold page into the relevant list in the
102 * @page_lock: per-page lock
103 * @refcount: reference count for the z3fold page
104 * @work: work_struct for page layout optimization
105 * @slots: pointer to the structure holding buddy slots
106 * @pool: pointer to the containing pool
107 * @cpu: CPU which this page "belongs" to
108 * @first_chunks: the size of the first buddy in chunks, 0 if free
109 * @middle_chunks: the size of the middle buddy in chunks, 0 if free
110 * @last_chunks: the size of the last buddy in chunks, 0 if free
111 * @first_num: the starting number (for the first handle)
112 * @mapped_count: the number of objects currently mapped
114 struct z3fold_header
{
115 struct list_head buddy
;
116 spinlock_t page_lock
;
117 struct kref refcount
;
118 struct work_struct work
;
119 struct z3fold_buddy_slots
*slots
;
120 struct z3fold_pool
*pool
;
122 unsigned short first_chunks
;
123 unsigned short middle_chunks
;
124 unsigned short last_chunks
;
125 unsigned short start_middle
;
126 unsigned short first_num
:2;
127 unsigned short mapped_count
:2;
128 unsigned short foreign_handles
:2;
132 * struct z3fold_pool - stores metadata for each z3fold pool
134 * @lock: protects pool unbuddied/lru lists
135 * @stale_lock: protects pool stale page list
136 * @unbuddied: per-cpu array of lists tracking z3fold pages that contain 2-
137 * buddies; the list each z3fold page is added to depends on
138 * the size of its free region.
139 * @lru: list tracking the z3fold pages in LRU order by most recently
141 * @stale: list of pages marked for freeing
142 * @pages_nr: number of z3fold pages in the pool.
143 * @c_handle: cache for z3fold_buddy_slots allocation
144 * @ops: pointer to a structure of user defined operations specified at
145 * pool creation time.
146 * @compact_wq: workqueue for page layout background optimization
147 * @release_wq: workqueue for safe page release
148 * @work: work_struct for safe page release
149 * @inode: inode for z3fold pseudo filesystem
151 * This structure is allocated at pool creation time and maintains metadata
152 * pertaining to a particular z3fold pool.
157 spinlock_t stale_lock
;
158 struct list_head
*unbuddied
;
159 struct list_head lru
;
160 struct list_head stale
;
162 struct kmem_cache
*c_handle
;
163 const struct z3fold_ops
*ops
;
165 const struct zpool_ops
*zpool_ops
;
166 struct workqueue_struct
*compact_wq
;
167 struct workqueue_struct
*release_wq
;
168 struct work_struct work
;
173 * Internal z3fold page flags
175 enum z3fold_page_flags
{
180 PAGE_CLAIMED
, /* by either reclaim or free */
184 * handle flags, go under HANDLE_FLAG_MASK
186 enum z3fold_handle_flags
{
187 HANDLES_ORPHANED
= 0,
191 * Forward declarations
193 static struct z3fold_header
*__z3fold_alloc(struct z3fold_pool
*, size_t, bool);
194 static void compact_page_work(struct work_struct
*w
);
200 /* Converts an allocation size in bytes to size in z3fold chunks */
201 static int size_to_chunks(size_t size
)
203 return (size
+ CHUNK_SIZE
- 1) >> CHUNK_SHIFT
;
206 #define for_each_unbuddied_list(_iter, _begin) \
207 for ((_iter) = (_begin); (_iter) < NCHUNKS; (_iter)++)
209 static inline struct z3fold_buddy_slots
*alloc_slots(struct z3fold_pool
*pool
,
212 struct z3fold_buddy_slots
*slots
;
214 slots
= kmem_cache_alloc(pool
->c_handle
,
215 (gfp
& ~(__GFP_HIGHMEM
| __GFP_MOVABLE
)));
218 memset(slots
->slot
, 0, sizeof(slots
->slot
));
219 slots
->pool
= (unsigned long)pool
;
220 rwlock_init(&slots
->lock
);
226 static inline struct z3fold_pool
*slots_to_pool(struct z3fold_buddy_slots
*s
)
228 return (struct z3fold_pool
*)(s
->pool
& ~HANDLE_FLAG_MASK
);
231 static inline struct z3fold_buddy_slots
*handle_to_slots(unsigned long handle
)
233 return (struct z3fold_buddy_slots
*)(handle
& ~(SLOTS_ALIGN
- 1));
236 /* Lock a z3fold page */
237 static inline void z3fold_page_lock(struct z3fold_header
*zhdr
)
239 spin_lock(&zhdr
->page_lock
);
242 /* Try to lock a z3fold page */
243 static inline int z3fold_page_trylock(struct z3fold_header
*zhdr
)
245 return spin_trylock(&zhdr
->page_lock
);
248 /* Unlock a z3fold page */
249 static inline void z3fold_page_unlock(struct z3fold_header
*zhdr
)
251 spin_unlock(&zhdr
->page_lock
);
255 static inline struct z3fold_header
*__get_z3fold_header(unsigned long handle
,
258 struct z3fold_buddy_slots
*slots
;
259 struct z3fold_header
*zhdr
;
262 if (!(handle
& (1 << PAGE_HEADLESS
))) {
263 slots
= handle_to_slots(handle
);
267 read_lock(&slots
->lock
);
268 addr
= *(unsigned long *)handle
;
269 zhdr
= (struct z3fold_header
*)(addr
& PAGE_MASK
);
271 locked
= z3fold_page_trylock(zhdr
);
272 read_unlock(&slots
->lock
);
278 zhdr
= (struct z3fold_header
*)(handle
& PAGE_MASK
);
284 /* Returns the z3fold page where a given handle is stored */
285 static inline struct z3fold_header
*handle_to_z3fold_header(unsigned long h
)
287 return __get_z3fold_header(h
, false);
290 /* return locked z3fold page if it's not headless */
291 static inline struct z3fold_header
*get_z3fold_header(unsigned long h
)
293 return __get_z3fold_header(h
, true);
296 static inline void put_z3fold_header(struct z3fold_header
*zhdr
)
298 struct page
*page
= virt_to_page(zhdr
);
300 if (!test_bit(PAGE_HEADLESS
, &page
->private))
301 z3fold_page_unlock(zhdr
);
304 static inline void free_handle(unsigned long handle
)
306 struct z3fold_buddy_slots
*slots
;
307 struct z3fold_header
*zhdr
;
311 if (handle
& (1 << PAGE_HEADLESS
))
314 if (WARN_ON(*(unsigned long *)handle
== 0))
317 zhdr
= handle_to_z3fold_header(handle
);
318 slots
= handle_to_slots(handle
);
319 write_lock(&slots
->lock
);
320 *(unsigned long *)handle
= 0;
321 write_unlock(&slots
->lock
);
322 if (zhdr
->slots
== slots
)
323 return; /* simple case, nothing else to do */
325 /* we are freeing a foreign handle if we are here */
326 zhdr
->foreign_handles
--;
328 read_lock(&slots
->lock
);
329 if (!test_bit(HANDLES_ORPHANED
, &slots
->pool
)) {
330 read_unlock(&slots
->lock
);
333 for (i
= 0; i
<= BUDDY_MASK
; i
++) {
334 if (slots
->slot
[i
]) {
339 read_unlock(&slots
->lock
);
342 struct z3fold_pool
*pool
= slots_to_pool(slots
);
344 kmem_cache_free(pool
->c_handle
, slots
);
348 static int z3fold_init_fs_context(struct fs_context
*fc
)
350 return init_pseudo(fc
, Z3FOLD_MAGIC
) ? 0 : -ENOMEM
;
353 static struct file_system_type z3fold_fs
= {
355 .init_fs_context
= z3fold_init_fs_context
,
356 .kill_sb
= kill_anon_super
,
359 static struct vfsmount
*z3fold_mnt
;
360 static int z3fold_mount(void)
364 z3fold_mnt
= kern_mount(&z3fold_fs
);
365 if (IS_ERR(z3fold_mnt
))
366 ret
= PTR_ERR(z3fold_mnt
);
371 static void z3fold_unmount(void)
373 kern_unmount(z3fold_mnt
);
376 static const struct address_space_operations z3fold_aops
;
377 static int z3fold_register_migration(struct z3fold_pool
*pool
)
379 pool
->inode
= alloc_anon_inode(z3fold_mnt
->mnt_sb
);
380 if (IS_ERR(pool
->inode
)) {
385 pool
->inode
->i_mapping
->private_data
= pool
;
386 pool
->inode
->i_mapping
->a_ops
= &z3fold_aops
;
390 static void z3fold_unregister_migration(struct z3fold_pool
*pool
)
396 /* Initializes the z3fold header of a newly allocated z3fold page */
397 static struct z3fold_header
*init_z3fold_page(struct page
*page
, bool headless
,
398 struct z3fold_pool
*pool
, gfp_t gfp
)
400 struct z3fold_header
*zhdr
= page_address(page
);
401 struct z3fold_buddy_slots
*slots
;
403 INIT_LIST_HEAD(&page
->lru
);
404 clear_bit(PAGE_HEADLESS
, &page
->private);
405 clear_bit(MIDDLE_CHUNK_MAPPED
, &page
->private);
406 clear_bit(NEEDS_COMPACTING
, &page
->private);
407 clear_bit(PAGE_STALE
, &page
->private);
408 clear_bit(PAGE_CLAIMED
, &page
->private);
412 slots
= alloc_slots(pool
, gfp
);
416 spin_lock_init(&zhdr
->page_lock
);
417 kref_init(&zhdr
->refcount
);
418 zhdr
->first_chunks
= 0;
419 zhdr
->middle_chunks
= 0;
420 zhdr
->last_chunks
= 0;
422 zhdr
->start_middle
= 0;
424 zhdr
->foreign_handles
= 0;
427 INIT_LIST_HEAD(&zhdr
->buddy
);
428 INIT_WORK(&zhdr
->work
, compact_page_work
);
432 /* Resets the struct page fields and frees the page */
433 static void free_z3fold_page(struct page
*page
, bool headless
)
437 __ClearPageMovable(page
);
440 ClearPagePrivate(page
);
444 /* Helper function to build the index */
445 static inline int __idx(struct z3fold_header
*zhdr
, enum buddy bud
)
447 return (bud
+ zhdr
->first_num
) & BUDDY_MASK
;
451 * Encodes the handle of a particular buddy within a z3fold page
452 * Pool lock should be held as this function accesses first_num
454 static unsigned long __encode_handle(struct z3fold_header
*zhdr
,
455 struct z3fold_buddy_slots
*slots
,
458 unsigned long h
= (unsigned long)zhdr
;
462 * For a headless page, its handle is its pointer with the extra
463 * PAGE_HEADLESS bit set
466 return h
| (1 << PAGE_HEADLESS
);
468 /* otherwise, return pointer to encoded handle */
469 idx
= __idx(zhdr
, bud
);
472 h
|= (zhdr
->last_chunks
<< BUDDY_SHIFT
);
474 write_lock(&slots
->lock
);
475 slots
->slot
[idx
] = h
;
476 write_unlock(&slots
->lock
);
477 return (unsigned long)&slots
->slot
[idx
];
480 static unsigned long encode_handle(struct z3fold_header
*zhdr
, enum buddy bud
)
482 return __encode_handle(zhdr
, zhdr
->slots
, bud
);
485 /* only for LAST bud, returns zero otherwise */
486 static unsigned short handle_to_chunks(unsigned long handle
)
488 struct z3fold_buddy_slots
*slots
= handle_to_slots(handle
);
491 read_lock(&slots
->lock
);
492 addr
= *(unsigned long *)handle
;
493 read_unlock(&slots
->lock
);
494 return (addr
& ~PAGE_MASK
) >> BUDDY_SHIFT
;
498 * (handle & BUDDY_MASK) < zhdr->first_num is possible in encode_handle
499 * but that doesn't matter. because the masking will result in the
500 * correct buddy number.
502 static enum buddy
handle_to_buddy(unsigned long handle
)
504 struct z3fold_header
*zhdr
;
505 struct z3fold_buddy_slots
*slots
= handle_to_slots(handle
);
508 read_lock(&slots
->lock
);
509 WARN_ON(handle
& (1 << PAGE_HEADLESS
));
510 addr
= *(unsigned long *)handle
;
511 read_unlock(&slots
->lock
);
512 zhdr
= (struct z3fold_header
*)(addr
& PAGE_MASK
);
513 return (addr
- zhdr
->first_num
) & BUDDY_MASK
;
516 static inline struct z3fold_pool
*zhdr_to_pool(struct z3fold_header
*zhdr
)
521 static void __release_z3fold_page(struct z3fold_header
*zhdr
, bool locked
)
523 struct page
*page
= virt_to_page(zhdr
);
524 struct z3fold_pool
*pool
= zhdr_to_pool(zhdr
);
528 WARN_ON(!list_empty(&zhdr
->buddy
));
529 set_bit(PAGE_STALE
, &page
->private);
530 clear_bit(NEEDS_COMPACTING
, &page
->private);
531 spin_lock(&pool
->lock
);
532 if (!list_empty(&page
->lru
))
533 list_del_init(&page
->lru
);
534 spin_unlock(&pool
->lock
);
536 /* If there are no foreign handles, free the handles array */
537 read_lock(&zhdr
->slots
->lock
);
538 for (i
= 0; i
<= BUDDY_MASK
; i
++) {
539 if (zhdr
->slots
->slot
[i
]) {
545 set_bit(HANDLES_ORPHANED
, &zhdr
->slots
->pool
);
546 read_unlock(&zhdr
->slots
->lock
);
549 kmem_cache_free(pool
->c_handle
, zhdr
->slots
);
552 z3fold_page_unlock(zhdr
);
554 spin_lock(&pool
->stale_lock
);
555 list_add(&zhdr
->buddy
, &pool
->stale
);
556 queue_work(pool
->release_wq
, &pool
->work
);
557 spin_unlock(&pool
->stale_lock
);
560 static void __attribute__((__unused__
))
561 release_z3fold_page(struct kref
*ref
)
563 struct z3fold_header
*zhdr
= container_of(ref
, struct z3fold_header
,
565 __release_z3fold_page(zhdr
, false);
568 static void release_z3fold_page_locked(struct kref
*ref
)
570 struct z3fold_header
*zhdr
= container_of(ref
, struct z3fold_header
,
572 WARN_ON(z3fold_page_trylock(zhdr
));
573 __release_z3fold_page(zhdr
, true);
576 static void release_z3fold_page_locked_list(struct kref
*ref
)
578 struct z3fold_header
*zhdr
= container_of(ref
, struct z3fold_header
,
580 struct z3fold_pool
*pool
= zhdr_to_pool(zhdr
);
582 spin_lock(&pool
->lock
);
583 list_del_init(&zhdr
->buddy
);
584 spin_unlock(&pool
->lock
);
586 WARN_ON(z3fold_page_trylock(zhdr
));
587 __release_z3fold_page(zhdr
, true);
590 static void free_pages_work(struct work_struct
*w
)
592 struct z3fold_pool
*pool
= container_of(w
, struct z3fold_pool
, work
);
594 spin_lock(&pool
->stale_lock
);
595 while (!list_empty(&pool
->stale
)) {
596 struct z3fold_header
*zhdr
= list_first_entry(&pool
->stale
,
597 struct z3fold_header
, buddy
);
598 struct page
*page
= virt_to_page(zhdr
);
600 list_del(&zhdr
->buddy
);
601 if (WARN_ON(!test_bit(PAGE_STALE
, &page
->private)))
603 spin_unlock(&pool
->stale_lock
);
604 cancel_work_sync(&zhdr
->work
);
605 free_z3fold_page(page
, false);
607 spin_lock(&pool
->stale_lock
);
609 spin_unlock(&pool
->stale_lock
);
613 * Returns the number of free chunks in a z3fold page.
614 * NB: can't be used with HEADLESS pages.
616 static int num_free_chunks(struct z3fold_header
*zhdr
)
620 * If there is a middle object, pick up the bigger free space
621 * either before or after it. Otherwise just subtract the number
622 * of chunks occupied by the first and the last objects.
624 if (zhdr
->middle_chunks
!= 0) {
625 int nfree_before
= zhdr
->first_chunks
?
626 0 : zhdr
->start_middle
- ZHDR_CHUNKS
;
627 int nfree_after
= zhdr
->last_chunks
?
629 (zhdr
->start_middle
+ zhdr
->middle_chunks
);
630 nfree
= max(nfree_before
, nfree_after
);
632 nfree
= NCHUNKS
- zhdr
->first_chunks
- zhdr
->last_chunks
;
636 /* Add to the appropriate unbuddied list */
637 static inline void add_to_unbuddied(struct z3fold_pool
*pool
,
638 struct z3fold_header
*zhdr
)
640 if (zhdr
->first_chunks
== 0 || zhdr
->last_chunks
== 0 ||
641 zhdr
->middle_chunks
== 0) {
642 struct list_head
*unbuddied
= get_cpu_ptr(pool
->unbuddied
);
644 int freechunks
= num_free_chunks(zhdr
);
645 spin_lock(&pool
->lock
);
646 list_add(&zhdr
->buddy
, &unbuddied
[freechunks
]);
647 spin_unlock(&pool
->lock
);
648 zhdr
->cpu
= smp_processor_id();
649 put_cpu_ptr(pool
->unbuddied
);
653 static inline void *mchunk_memmove(struct z3fold_header
*zhdr
,
654 unsigned short dst_chunk
)
657 return memmove(beg
+ (dst_chunk
<< CHUNK_SHIFT
),
658 beg
+ (zhdr
->start_middle
<< CHUNK_SHIFT
),
659 zhdr
->middle_chunks
<< CHUNK_SHIFT
);
662 static inline bool buddy_single(struct z3fold_header
*zhdr
)
664 return !((zhdr
->first_chunks
&& zhdr
->middle_chunks
) ||
665 (zhdr
->first_chunks
&& zhdr
->last_chunks
) ||
666 (zhdr
->middle_chunks
&& zhdr
->last_chunks
));
669 static struct z3fold_header
*compact_single_buddy(struct z3fold_header
*zhdr
)
671 struct z3fold_pool
*pool
= zhdr_to_pool(zhdr
);
673 unsigned long old_handle
= 0;
675 struct z3fold_header
*new_zhdr
= NULL
;
676 int first_idx
= __idx(zhdr
, FIRST
);
677 int middle_idx
= __idx(zhdr
, MIDDLE
);
678 int last_idx
= __idx(zhdr
, LAST
);
679 unsigned short *moved_chunks
= NULL
;
682 * No need to protect slots here -- all the slots are "local" and
683 * the page lock is already taken
685 if (zhdr
->first_chunks
&& zhdr
->slots
->slot
[first_idx
]) {
686 p
+= ZHDR_SIZE_ALIGNED
;
687 sz
= zhdr
->first_chunks
<< CHUNK_SHIFT
;
688 old_handle
= (unsigned long)&zhdr
->slots
->slot
[first_idx
];
689 moved_chunks
= &zhdr
->first_chunks
;
690 } else if (zhdr
->middle_chunks
&& zhdr
->slots
->slot
[middle_idx
]) {
691 p
+= zhdr
->start_middle
<< CHUNK_SHIFT
;
692 sz
= zhdr
->middle_chunks
<< CHUNK_SHIFT
;
693 old_handle
= (unsigned long)&zhdr
->slots
->slot
[middle_idx
];
694 moved_chunks
= &zhdr
->middle_chunks
;
695 } else if (zhdr
->last_chunks
&& zhdr
->slots
->slot
[last_idx
]) {
696 p
+= PAGE_SIZE
- (zhdr
->last_chunks
<< CHUNK_SHIFT
);
697 sz
= zhdr
->last_chunks
<< CHUNK_SHIFT
;
698 old_handle
= (unsigned long)&zhdr
->slots
->slot
[last_idx
];
699 moved_chunks
= &zhdr
->last_chunks
;
703 enum buddy new_bud
= HEADLESS
;
704 short chunks
= size_to_chunks(sz
);
707 new_zhdr
= __z3fold_alloc(pool
, sz
, false);
711 if (WARN_ON(new_zhdr
== zhdr
))
714 if (new_zhdr
->first_chunks
== 0) {
715 if (new_zhdr
->middle_chunks
!= 0 &&
716 chunks
>= new_zhdr
->start_middle
) {
721 } else if (new_zhdr
->last_chunks
== 0) {
723 } else if (new_zhdr
->middle_chunks
== 0) {
729 new_zhdr
->first_chunks
= chunks
;
730 q
+= ZHDR_SIZE_ALIGNED
;
733 new_zhdr
->middle_chunks
= chunks
;
734 new_zhdr
->start_middle
=
735 new_zhdr
->first_chunks
+ ZHDR_CHUNKS
;
736 q
+= new_zhdr
->start_middle
<< CHUNK_SHIFT
;
739 new_zhdr
->last_chunks
= chunks
;
740 q
+= PAGE_SIZE
- (new_zhdr
->last_chunks
<< CHUNK_SHIFT
);
745 new_zhdr
->foreign_handles
++;
747 write_lock(&zhdr
->slots
->lock
);
748 *(unsigned long *)old_handle
= (unsigned long)new_zhdr
+
749 __idx(new_zhdr
, new_bud
);
751 *(unsigned long *)old_handle
|=
752 (new_zhdr
->last_chunks
<< BUDDY_SHIFT
);
753 write_unlock(&zhdr
->slots
->lock
);
754 add_to_unbuddied(pool
, new_zhdr
);
755 z3fold_page_unlock(new_zhdr
);
764 if (kref_put(&new_zhdr
->refcount
, release_z3fold_page_locked
))
765 atomic64_dec(&pool
->pages_nr
);
767 add_to_unbuddied(pool
, new_zhdr
);
768 z3fold_page_unlock(new_zhdr
);
775 #define BIG_CHUNK_GAP 3
776 /* Has to be called with lock held */
777 static int z3fold_compact_page(struct z3fold_header
*zhdr
)
779 struct page
*page
= virt_to_page(zhdr
);
781 if (test_bit(MIDDLE_CHUNK_MAPPED
, &page
->private))
782 return 0; /* can't move middle chunk, it's used */
784 if (unlikely(PageIsolated(page
)))
787 if (zhdr
->middle_chunks
== 0)
788 return 0; /* nothing to compact */
790 if (zhdr
->first_chunks
== 0 && zhdr
->last_chunks
== 0) {
791 /* move to the beginning */
792 mchunk_memmove(zhdr
, ZHDR_CHUNKS
);
793 zhdr
->first_chunks
= zhdr
->middle_chunks
;
794 zhdr
->middle_chunks
= 0;
795 zhdr
->start_middle
= 0;
801 * moving data is expensive, so let's only do that if
802 * there's substantial gain (at least BIG_CHUNK_GAP chunks)
804 if (zhdr
->first_chunks
!= 0 && zhdr
->last_chunks
== 0 &&
805 zhdr
->start_middle
- (zhdr
->first_chunks
+ ZHDR_CHUNKS
) >=
807 mchunk_memmove(zhdr
, zhdr
->first_chunks
+ ZHDR_CHUNKS
);
808 zhdr
->start_middle
= zhdr
->first_chunks
+ ZHDR_CHUNKS
;
810 } else if (zhdr
->last_chunks
!= 0 && zhdr
->first_chunks
== 0 &&
811 TOTAL_CHUNKS
- (zhdr
->last_chunks
+ zhdr
->start_middle
812 + zhdr
->middle_chunks
) >=
814 unsigned short new_start
= TOTAL_CHUNKS
- zhdr
->last_chunks
-
816 mchunk_memmove(zhdr
, new_start
);
817 zhdr
->start_middle
= new_start
;
824 static void do_compact_page(struct z3fold_header
*zhdr
, bool locked
)
826 struct z3fold_pool
*pool
= zhdr_to_pool(zhdr
);
829 page
= virt_to_page(zhdr
);
831 WARN_ON(z3fold_page_trylock(zhdr
));
833 z3fold_page_lock(zhdr
);
834 if (WARN_ON(!test_and_clear_bit(NEEDS_COMPACTING
, &page
->private))) {
835 z3fold_page_unlock(zhdr
);
838 spin_lock(&pool
->lock
);
839 list_del_init(&zhdr
->buddy
);
840 spin_unlock(&pool
->lock
);
842 if (kref_put(&zhdr
->refcount
, release_z3fold_page_locked
)) {
843 atomic64_dec(&pool
->pages_nr
);
847 if (unlikely(PageIsolated(page
) ||
848 test_bit(PAGE_CLAIMED
, &page
->private) ||
849 test_bit(PAGE_STALE
, &page
->private))) {
850 z3fold_page_unlock(zhdr
);
854 if (!zhdr
->foreign_handles
&& buddy_single(zhdr
) &&
855 zhdr
->mapped_count
== 0 && compact_single_buddy(zhdr
)) {
856 if (kref_put(&zhdr
->refcount
, release_z3fold_page_locked
))
857 atomic64_dec(&pool
->pages_nr
);
859 z3fold_page_unlock(zhdr
);
863 z3fold_compact_page(zhdr
);
864 add_to_unbuddied(pool
, zhdr
);
865 z3fold_page_unlock(zhdr
);
868 static void compact_page_work(struct work_struct
*w
)
870 struct z3fold_header
*zhdr
= container_of(w
, struct z3fold_header
,
873 do_compact_page(zhdr
, false);
876 /* returns _locked_ z3fold page header or NULL */
877 static inline struct z3fold_header
*__z3fold_alloc(struct z3fold_pool
*pool
,
878 size_t size
, bool can_sleep
)
880 struct z3fold_header
*zhdr
= NULL
;
882 struct list_head
*unbuddied
;
883 int chunks
= size_to_chunks(size
), i
;
886 /* First, try to find an unbuddied z3fold page. */
887 unbuddied
= get_cpu_ptr(pool
->unbuddied
);
888 for_each_unbuddied_list(i
, chunks
) {
889 struct list_head
*l
= &unbuddied
[i
];
891 zhdr
= list_first_entry_or_null(READ_ONCE(l
),
892 struct z3fold_header
, buddy
);
897 /* Re-check under lock. */
898 spin_lock(&pool
->lock
);
900 if (unlikely(zhdr
!= list_first_entry(READ_ONCE(l
),
901 struct z3fold_header
, buddy
)) ||
902 !z3fold_page_trylock(zhdr
)) {
903 spin_unlock(&pool
->lock
);
905 put_cpu_ptr(pool
->unbuddied
);
910 list_del_init(&zhdr
->buddy
);
912 spin_unlock(&pool
->lock
);
914 page
= virt_to_page(zhdr
);
915 if (test_bit(NEEDS_COMPACTING
, &page
->private) ||
916 test_bit(PAGE_CLAIMED
, &page
->private)) {
917 z3fold_page_unlock(zhdr
);
919 put_cpu_ptr(pool
->unbuddied
);
926 * this page could not be removed from its unbuddied
927 * list while pool lock was held, and then we've taken
928 * page lock so kref_put could not be called before
929 * we got here, so it's safe to just call kref_get()
931 kref_get(&zhdr
->refcount
);
934 put_cpu_ptr(pool
->unbuddied
);
939 /* look for _exact_ match on other cpus' lists */
940 for_each_online_cpu(cpu
) {
943 unbuddied
= per_cpu_ptr(pool
->unbuddied
, cpu
);
944 spin_lock(&pool
->lock
);
945 l
= &unbuddied
[chunks
];
947 zhdr
= list_first_entry_or_null(READ_ONCE(l
),
948 struct z3fold_header
, buddy
);
950 if (!zhdr
|| !z3fold_page_trylock(zhdr
)) {
951 spin_unlock(&pool
->lock
);
955 list_del_init(&zhdr
->buddy
);
957 spin_unlock(&pool
->lock
);
959 page
= virt_to_page(zhdr
);
960 if (test_bit(NEEDS_COMPACTING
, &page
->private) ||
961 test_bit(PAGE_CLAIMED
, &page
->private)) {
962 z3fold_page_unlock(zhdr
);
968 kref_get(&zhdr
->refcount
);
981 * z3fold_create_pool() - create a new z3fold pool
983 * @gfp: gfp flags when allocating the z3fold pool structure
984 * @ops: user-defined operations for the z3fold pool
986 * Return: pointer to the new z3fold pool or NULL if the metadata allocation
989 static struct z3fold_pool
*z3fold_create_pool(const char *name
, gfp_t gfp
,
990 const struct z3fold_ops
*ops
)
992 struct z3fold_pool
*pool
= NULL
;
995 pool
= kzalloc(sizeof(struct z3fold_pool
), gfp
);
998 pool
->c_handle
= kmem_cache_create("z3fold_handle",
999 sizeof(struct z3fold_buddy_slots
),
1000 SLOTS_ALIGN
, 0, NULL
);
1001 if (!pool
->c_handle
)
1003 spin_lock_init(&pool
->lock
);
1004 spin_lock_init(&pool
->stale_lock
);
1005 pool
->unbuddied
= __alloc_percpu(sizeof(struct list_head
)*NCHUNKS
, 2);
1006 if (!pool
->unbuddied
)
1008 for_each_possible_cpu(cpu
) {
1009 struct list_head
*unbuddied
=
1010 per_cpu_ptr(pool
->unbuddied
, cpu
);
1011 for_each_unbuddied_list(i
, 0)
1012 INIT_LIST_HEAD(&unbuddied
[i
]);
1014 INIT_LIST_HEAD(&pool
->lru
);
1015 INIT_LIST_HEAD(&pool
->stale
);
1016 atomic64_set(&pool
->pages_nr
, 0);
1018 pool
->compact_wq
= create_singlethread_workqueue(pool
->name
);
1019 if (!pool
->compact_wq
)
1021 pool
->release_wq
= create_singlethread_workqueue(pool
->name
);
1022 if (!pool
->release_wq
)
1024 if (z3fold_register_migration(pool
))
1026 INIT_WORK(&pool
->work
, free_pages_work
);
1031 destroy_workqueue(pool
->release_wq
);
1033 destroy_workqueue(pool
->compact_wq
);
1035 free_percpu(pool
->unbuddied
);
1037 kmem_cache_destroy(pool
->c_handle
);
1045 * z3fold_destroy_pool() - destroys an existing z3fold pool
1046 * @pool: the z3fold pool to be destroyed
1048 * The pool should be emptied before this function is called.
1050 static void z3fold_destroy_pool(struct z3fold_pool
*pool
)
1052 kmem_cache_destroy(pool
->c_handle
);
1055 * We need to destroy pool->compact_wq before pool->release_wq,
1056 * as any pending work on pool->compact_wq will call
1057 * queue_work(pool->release_wq, &pool->work).
1059 * There are still outstanding pages until both workqueues are drained,
1060 * so we cannot unregister migration until then.
1063 destroy_workqueue(pool
->compact_wq
);
1064 destroy_workqueue(pool
->release_wq
);
1065 z3fold_unregister_migration(pool
);
1070 * z3fold_alloc() - allocates a region of a given size
1071 * @pool: z3fold pool from which to allocate
1072 * @size: size in bytes of the desired allocation
1073 * @gfp: gfp flags used if the pool needs to grow
1074 * @handle: handle of the new allocation
1076 * This function will attempt to find a free region in the pool large enough to
1077 * satisfy the allocation request. A search of the unbuddied lists is
1078 * performed first. If no suitable free region is found, then a new page is
1079 * allocated and added to the pool to satisfy the request.
1081 * gfp should not set __GFP_HIGHMEM as highmem pages cannot be used
1082 * as z3fold pool pages.
1084 * Return: 0 if success and handle is set, otherwise -EINVAL if the size or
1085 * gfp arguments are invalid or -ENOMEM if the pool was unable to allocate
1088 static int z3fold_alloc(struct z3fold_pool
*pool
, size_t size
, gfp_t gfp
,
1089 unsigned long *handle
)
1091 int chunks
= size_to_chunks(size
);
1092 struct z3fold_header
*zhdr
= NULL
;
1093 struct page
*page
= NULL
;
1095 bool can_sleep
= gfpflags_allow_blocking(gfp
);
1100 if (size
> PAGE_SIZE
)
1103 if (size
> PAGE_SIZE
- ZHDR_SIZE_ALIGNED
- CHUNK_SIZE
)
1107 zhdr
= __z3fold_alloc(pool
, size
, can_sleep
);
1109 if (zhdr
->first_chunks
== 0) {
1110 if (zhdr
->middle_chunks
!= 0 &&
1111 chunks
>= zhdr
->start_middle
)
1115 } else if (zhdr
->last_chunks
== 0)
1117 else if (zhdr
->middle_chunks
== 0)
1120 if (kref_put(&zhdr
->refcount
,
1121 release_z3fold_page_locked
))
1122 atomic64_dec(&pool
->pages_nr
);
1124 z3fold_page_unlock(zhdr
);
1125 pr_err("No free chunks in unbuddied\n");
1129 page
= virt_to_page(zhdr
);
1137 spin_lock(&pool
->stale_lock
);
1138 zhdr
= list_first_entry_or_null(&pool
->stale
,
1139 struct z3fold_header
, buddy
);
1141 * Before allocating a page, let's see if we can take one from
1142 * the stale pages list. cancel_work_sync() can sleep so we
1143 * limit this case to the contexts where we can sleep
1146 list_del(&zhdr
->buddy
);
1147 spin_unlock(&pool
->stale_lock
);
1148 cancel_work_sync(&zhdr
->work
);
1149 page
= virt_to_page(zhdr
);
1151 spin_unlock(&pool
->stale_lock
);
1155 page
= alloc_page(gfp
);
1160 zhdr
= init_z3fold_page(page
, bud
== HEADLESS
, pool
, gfp
);
1165 atomic64_inc(&pool
->pages_nr
);
1167 if (bud
== HEADLESS
) {
1168 set_bit(PAGE_HEADLESS
, &page
->private);
1173 __SetPageMovable(page
, pool
->inode
->i_mapping
);
1176 if (trylock_page(page
)) {
1177 __SetPageMovable(page
, pool
->inode
->i_mapping
);
1181 z3fold_page_lock(zhdr
);
1185 zhdr
->first_chunks
= chunks
;
1186 else if (bud
== LAST
)
1187 zhdr
->last_chunks
= chunks
;
1189 zhdr
->middle_chunks
= chunks
;
1190 zhdr
->start_middle
= zhdr
->first_chunks
+ ZHDR_CHUNKS
;
1192 add_to_unbuddied(pool
, zhdr
);
1195 spin_lock(&pool
->lock
);
1196 /* Add/move z3fold page to beginning of LRU */
1197 if (!list_empty(&page
->lru
))
1198 list_del(&page
->lru
);
1200 list_add(&page
->lru
, &pool
->lru
);
1202 *handle
= encode_handle(zhdr
, bud
);
1203 spin_unlock(&pool
->lock
);
1204 if (bud
!= HEADLESS
)
1205 z3fold_page_unlock(zhdr
);
1211 * z3fold_free() - frees the allocation associated with the given handle
1212 * @pool: pool in which the allocation resided
1213 * @handle: handle associated with the allocation returned by z3fold_alloc()
1215 * In the case that the z3fold page in which the allocation resides is under
1216 * reclaim, as indicated by the PG_reclaim flag being set, this function
1217 * only sets the first|last_chunks to 0. The page is actually freed
1218 * once both buddies are evicted (see z3fold_reclaim_page() below).
1220 static void z3fold_free(struct z3fold_pool
*pool
, unsigned long handle
)
1222 struct z3fold_header
*zhdr
;
1227 zhdr
= get_z3fold_header(handle
);
1228 page
= virt_to_page(zhdr
);
1229 page_claimed
= test_and_set_bit(PAGE_CLAIMED
, &page
->private);
1231 if (test_bit(PAGE_HEADLESS
, &page
->private)) {
1232 /* if a headless page is under reclaim, just leave.
1233 * NB: we use test_and_set_bit for a reason: if the bit
1234 * has not been set before, we release this page
1235 * immediately so we don't care about its value any more.
1237 if (!page_claimed
) {
1238 spin_lock(&pool
->lock
);
1239 list_del(&page
->lru
);
1240 spin_unlock(&pool
->lock
);
1241 put_z3fold_header(zhdr
);
1242 free_z3fold_page(page
, true);
1243 atomic64_dec(&pool
->pages_nr
);
1248 /* Non-headless case */
1249 bud
= handle_to_buddy(handle
);
1253 zhdr
->first_chunks
= 0;
1256 zhdr
->middle_chunks
= 0;
1259 zhdr
->last_chunks
= 0;
1262 pr_err("%s: unknown bud %d\n", __func__
, bud
);
1264 put_z3fold_header(zhdr
);
1265 clear_bit(PAGE_CLAIMED
, &page
->private);
1270 free_handle(handle
);
1271 if (kref_put(&zhdr
->refcount
, release_z3fold_page_locked_list
)) {
1272 atomic64_dec(&pool
->pages_nr
);
1276 /* the page has not been claimed by us */
1277 z3fold_page_unlock(zhdr
);
1280 if (unlikely(PageIsolated(page
)) ||
1281 test_and_set_bit(NEEDS_COMPACTING
, &page
->private)) {
1282 put_z3fold_header(zhdr
);
1283 clear_bit(PAGE_CLAIMED
, &page
->private);
1286 if (zhdr
->cpu
< 0 || !cpu_online(zhdr
->cpu
)) {
1287 spin_lock(&pool
->lock
);
1288 list_del_init(&zhdr
->buddy
);
1289 spin_unlock(&pool
->lock
);
1291 kref_get(&zhdr
->refcount
);
1292 clear_bit(PAGE_CLAIMED
, &page
->private);
1293 do_compact_page(zhdr
, true);
1296 kref_get(&zhdr
->refcount
);
1297 clear_bit(PAGE_CLAIMED
, &page
->private);
1298 queue_work_on(zhdr
->cpu
, pool
->compact_wq
, &zhdr
->work
);
1299 put_z3fold_header(zhdr
);
1303 * z3fold_reclaim_page() - evicts allocations from a pool page and frees it
1304 * @pool: pool from which a page will attempt to be evicted
1305 * @retries: number of pages on the LRU list for which eviction will
1306 * be attempted before failing
1308 * z3fold reclaim is different from normal system reclaim in that it is done
1309 * from the bottom, up. This is because only the bottom layer, z3fold, has
1310 * information on how the allocations are organized within each z3fold page.
1311 * This has the potential to create interesting locking situations between
1312 * z3fold and the user, however.
1314 * To avoid these, this is how z3fold_reclaim_page() should be called:
1316 * The user detects a page should be reclaimed and calls z3fold_reclaim_page().
1317 * z3fold_reclaim_page() will remove a z3fold page from the pool LRU list and
1318 * call the user-defined eviction handler with the pool and handle as
1321 * If the handle can not be evicted, the eviction handler should return
1322 * non-zero. z3fold_reclaim_page() will add the z3fold page back to the
1323 * appropriate list and try the next z3fold page on the LRU up to
1324 * a user defined number of retries.
1326 * If the handle is successfully evicted, the eviction handler should
1327 * return 0 _and_ should have called z3fold_free() on the handle. z3fold_free()
1328 * contains logic to delay freeing the page if the page is under reclaim,
1329 * as indicated by the setting of the PG_reclaim flag on the underlying page.
1331 * If all buddies in the z3fold page are successfully evicted, then the
1332 * z3fold page can be freed.
1334 * Returns: 0 if page is successfully freed, otherwise -EINVAL if there are
1335 * no pages to evict or an eviction handler is not registered, -EAGAIN if
1336 * the retry limit was hit.
1338 static int z3fold_reclaim_page(struct z3fold_pool
*pool
, unsigned int retries
)
1341 struct z3fold_header
*zhdr
= NULL
;
1342 struct page
*page
= NULL
;
1343 struct list_head
*pos
;
1344 unsigned long first_handle
= 0, middle_handle
= 0, last_handle
= 0;
1346 spin_lock(&pool
->lock
);
1347 if (!pool
->ops
|| !pool
->ops
->evict
|| retries
== 0) {
1348 spin_unlock(&pool
->lock
);
1351 for (i
= 0; i
< retries
; i
++) {
1352 if (list_empty(&pool
->lru
)) {
1353 spin_unlock(&pool
->lock
);
1356 list_for_each_prev(pos
, &pool
->lru
) {
1357 page
= list_entry(pos
, struct page
, lru
);
1359 /* this bit could have been set by free, in which case
1360 * we pass over to the next page in the pool.
1362 if (test_and_set_bit(PAGE_CLAIMED
, &page
->private)) {
1367 if (unlikely(PageIsolated(page
))) {
1368 clear_bit(PAGE_CLAIMED
, &page
->private);
1372 zhdr
= page_address(page
);
1373 if (test_bit(PAGE_HEADLESS
, &page
->private))
1376 if (!z3fold_page_trylock(zhdr
)) {
1377 clear_bit(PAGE_CLAIMED
, &page
->private);
1379 continue; /* can't evict at this point */
1381 if (zhdr
->foreign_handles
) {
1382 clear_bit(PAGE_CLAIMED
, &page
->private);
1383 z3fold_page_unlock(zhdr
);
1385 continue; /* can't evict such page */
1387 kref_get(&zhdr
->refcount
);
1388 list_del_init(&zhdr
->buddy
);
1396 list_del_init(&page
->lru
);
1397 spin_unlock(&pool
->lock
);
1399 if (!test_bit(PAGE_HEADLESS
, &page
->private)) {
1401 * We need encode the handles before unlocking, and
1402 * use our local slots structure because z3fold_free
1403 * can zero out zhdr->slots and we can't do much
1409 if (zhdr
->first_chunks
)
1410 first_handle
= encode_handle(zhdr
, FIRST
);
1411 if (zhdr
->middle_chunks
)
1412 middle_handle
= encode_handle(zhdr
, MIDDLE
);
1413 if (zhdr
->last_chunks
)
1414 last_handle
= encode_handle(zhdr
, LAST
);
1416 * it's safe to unlock here because we hold a
1417 * reference to this page
1419 z3fold_page_unlock(zhdr
);
1421 first_handle
= encode_handle(zhdr
, HEADLESS
);
1422 last_handle
= middle_handle
= 0;
1424 /* Issue the eviction callback(s) */
1425 if (middle_handle
) {
1426 ret
= pool
->ops
->evict(pool
, middle_handle
);
1429 free_handle(middle_handle
);
1432 ret
= pool
->ops
->evict(pool
, first_handle
);
1435 free_handle(first_handle
);
1438 ret
= pool
->ops
->evict(pool
, last_handle
);
1441 free_handle(last_handle
);
1444 if (test_bit(PAGE_HEADLESS
, &page
->private)) {
1446 free_z3fold_page(page
, true);
1447 atomic64_dec(&pool
->pages_nr
);
1450 spin_lock(&pool
->lock
);
1451 list_add(&page
->lru
, &pool
->lru
);
1452 spin_unlock(&pool
->lock
);
1453 clear_bit(PAGE_CLAIMED
, &page
->private);
1455 z3fold_page_lock(zhdr
);
1456 if (kref_put(&zhdr
->refcount
,
1457 release_z3fold_page_locked
)) {
1458 atomic64_dec(&pool
->pages_nr
);
1462 * if we are here, the page is still not completely
1463 * free. Take the global pool lock then to be able
1464 * to add it back to the lru list
1466 spin_lock(&pool
->lock
);
1467 list_add(&page
->lru
, &pool
->lru
);
1468 spin_unlock(&pool
->lock
);
1469 z3fold_page_unlock(zhdr
);
1470 clear_bit(PAGE_CLAIMED
, &page
->private);
1473 /* We started off locked to we need to lock the pool back */
1474 spin_lock(&pool
->lock
);
1476 spin_unlock(&pool
->lock
);
1481 * z3fold_map() - maps the allocation associated with the given handle
1482 * @pool: pool in which the allocation resides
1483 * @handle: handle associated with the allocation to be mapped
1485 * Extracts the buddy number from handle and constructs the pointer to the
1486 * correct starting chunk within the page.
1488 * Returns: a pointer to the mapped allocation
1490 static void *z3fold_map(struct z3fold_pool
*pool
, unsigned long handle
)
1492 struct z3fold_header
*zhdr
;
1497 zhdr
= get_z3fold_header(handle
);
1499 page
= virt_to_page(zhdr
);
1501 if (test_bit(PAGE_HEADLESS
, &page
->private))
1504 buddy
= handle_to_buddy(handle
);
1507 addr
+= ZHDR_SIZE_ALIGNED
;
1510 addr
+= zhdr
->start_middle
<< CHUNK_SHIFT
;
1511 set_bit(MIDDLE_CHUNK_MAPPED
, &page
->private);
1514 addr
+= PAGE_SIZE
- (handle_to_chunks(handle
) << CHUNK_SHIFT
);
1517 pr_err("unknown buddy id %d\n", buddy
);
1524 zhdr
->mapped_count
++;
1526 put_z3fold_header(zhdr
);
1531 * z3fold_unmap() - unmaps the allocation associated with the given handle
1532 * @pool: pool in which the allocation resides
1533 * @handle: handle associated with the allocation to be unmapped
1535 static void z3fold_unmap(struct z3fold_pool
*pool
, unsigned long handle
)
1537 struct z3fold_header
*zhdr
;
1541 zhdr
= get_z3fold_header(handle
);
1542 page
= virt_to_page(zhdr
);
1544 if (test_bit(PAGE_HEADLESS
, &page
->private))
1547 buddy
= handle_to_buddy(handle
);
1548 if (buddy
== MIDDLE
)
1549 clear_bit(MIDDLE_CHUNK_MAPPED
, &page
->private);
1550 zhdr
->mapped_count
--;
1551 put_z3fold_header(zhdr
);
1555 * z3fold_get_pool_size() - gets the z3fold pool size in pages
1556 * @pool: pool whose size is being queried
1558 * Returns: size in pages of the given pool.
1560 static u64
z3fold_get_pool_size(struct z3fold_pool
*pool
)
1562 return atomic64_read(&pool
->pages_nr
);
1565 static bool z3fold_page_isolate(struct page
*page
, isolate_mode_t mode
)
1567 struct z3fold_header
*zhdr
;
1568 struct z3fold_pool
*pool
;
1570 VM_BUG_ON_PAGE(!PageMovable(page
), page
);
1571 VM_BUG_ON_PAGE(PageIsolated(page
), page
);
1573 if (test_bit(PAGE_HEADLESS
, &page
->private) ||
1574 test_bit(PAGE_CLAIMED
, &page
->private))
1577 zhdr
= page_address(page
);
1578 z3fold_page_lock(zhdr
);
1579 if (test_bit(NEEDS_COMPACTING
, &page
->private) ||
1580 test_bit(PAGE_STALE
, &page
->private))
1583 if (zhdr
->mapped_count
!= 0 || zhdr
->foreign_handles
!= 0)
1586 pool
= zhdr_to_pool(zhdr
);
1587 spin_lock(&pool
->lock
);
1588 if (!list_empty(&zhdr
->buddy
))
1589 list_del_init(&zhdr
->buddy
);
1590 if (!list_empty(&page
->lru
))
1591 list_del_init(&page
->lru
);
1592 spin_unlock(&pool
->lock
);
1594 kref_get(&zhdr
->refcount
);
1595 z3fold_page_unlock(zhdr
);
1599 z3fold_page_unlock(zhdr
);
1603 static int z3fold_page_migrate(struct address_space
*mapping
, struct page
*newpage
,
1604 struct page
*page
, enum migrate_mode mode
)
1606 struct z3fold_header
*zhdr
, *new_zhdr
;
1607 struct z3fold_pool
*pool
;
1608 struct address_space
*new_mapping
;
1610 VM_BUG_ON_PAGE(!PageMovable(page
), page
);
1611 VM_BUG_ON_PAGE(!PageIsolated(page
), page
);
1612 VM_BUG_ON_PAGE(!PageLocked(newpage
), newpage
);
1614 zhdr
= page_address(page
);
1615 pool
= zhdr_to_pool(zhdr
);
1617 if (!z3fold_page_trylock(zhdr
)) {
1620 if (zhdr
->mapped_count
!= 0 || zhdr
->foreign_handles
!= 0) {
1621 z3fold_page_unlock(zhdr
);
1624 if (work_pending(&zhdr
->work
)) {
1625 z3fold_page_unlock(zhdr
);
1628 new_zhdr
= page_address(newpage
);
1629 memcpy(new_zhdr
, zhdr
, PAGE_SIZE
);
1630 newpage
->private = page
->private;
1632 z3fold_page_unlock(zhdr
);
1633 spin_lock_init(&new_zhdr
->page_lock
);
1634 INIT_WORK(&new_zhdr
->work
, compact_page_work
);
1636 * z3fold_page_isolate() ensures that new_zhdr->buddy is empty,
1637 * so we only have to reinitialize it.
1639 INIT_LIST_HEAD(&new_zhdr
->buddy
);
1640 new_mapping
= page_mapping(page
);
1641 __ClearPageMovable(page
);
1642 ClearPagePrivate(page
);
1645 z3fold_page_lock(new_zhdr
);
1646 if (new_zhdr
->first_chunks
)
1647 encode_handle(new_zhdr
, FIRST
);
1648 if (new_zhdr
->last_chunks
)
1649 encode_handle(new_zhdr
, LAST
);
1650 if (new_zhdr
->middle_chunks
)
1651 encode_handle(new_zhdr
, MIDDLE
);
1652 set_bit(NEEDS_COMPACTING
, &newpage
->private);
1653 new_zhdr
->cpu
= smp_processor_id();
1654 spin_lock(&pool
->lock
);
1655 list_add(&newpage
->lru
, &pool
->lru
);
1656 spin_unlock(&pool
->lock
);
1657 __SetPageMovable(newpage
, new_mapping
);
1658 z3fold_page_unlock(new_zhdr
);
1660 queue_work_on(new_zhdr
->cpu
, pool
->compact_wq
, &new_zhdr
->work
);
1662 page_mapcount_reset(page
);
1667 static void z3fold_page_putback(struct page
*page
)
1669 struct z3fold_header
*zhdr
;
1670 struct z3fold_pool
*pool
;
1672 zhdr
= page_address(page
);
1673 pool
= zhdr_to_pool(zhdr
);
1675 z3fold_page_lock(zhdr
);
1676 if (!list_empty(&zhdr
->buddy
))
1677 list_del_init(&zhdr
->buddy
);
1678 INIT_LIST_HEAD(&page
->lru
);
1679 if (kref_put(&zhdr
->refcount
, release_z3fold_page_locked
)) {
1680 atomic64_dec(&pool
->pages_nr
);
1683 spin_lock(&pool
->lock
);
1684 list_add(&page
->lru
, &pool
->lru
);
1685 spin_unlock(&pool
->lock
);
1686 z3fold_page_unlock(zhdr
);
1689 static const struct address_space_operations z3fold_aops
= {
1690 .isolate_page
= z3fold_page_isolate
,
1691 .migratepage
= z3fold_page_migrate
,
1692 .putback_page
= z3fold_page_putback
,
1699 static int z3fold_zpool_evict(struct z3fold_pool
*pool
, unsigned long handle
)
1701 if (pool
->zpool
&& pool
->zpool_ops
&& pool
->zpool_ops
->evict
)
1702 return pool
->zpool_ops
->evict(pool
->zpool
, handle
);
1707 static const struct z3fold_ops z3fold_zpool_ops
= {
1708 .evict
= z3fold_zpool_evict
1711 static void *z3fold_zpool_create(const char *name
, gfp_t gfp
,
1712 const struct zpool_ops
*zpool_ops
,
1713 struct zpool
*zpool
)
1715 struct z3fold_pool
*pool
;
1717 pool
= z3fold_create_pool(name
, gfp
,
1718 zpool_ops
? &z3fold_zpool_ops
: NULL
);
1720 pool
->zpool
= zpool
;
1721 pool
->zpool_ops
= zpool_ops
;
1726 static void z3fold_zpool_destroy(void *pool
)
1728 z3fold_destroy_pool(pool
);
1731 static int z3fold_zpool_malloc(void *pool
, size_t size
, gfp_t gfp
,
1732 unsigned long *handle
)
1734 return z3fold_alloc(pool
, size
, gfp
, handle
);
1736 static void z3fold_zpool_free(void *pool
, unsigned long handle
)
1738 z3fold_free(pool
, handle
);
1741 static int z3fold_zpool_shrink(void *pool
, unsigned int pages
,
1742 unsigned int *reclaimed
)
1744 unsigned int total
= 0;
1747 while (total
< pages
) {
1748 ret
= z3fold_reclaim_page(pool
, 8);
1760 static void *z3fold_zpool_map(void *pool
, unsigned long handle
,
1761 enum zpool_mapmode mm
)
1763 return z3fold_map(pool
, handle
);
1765 static void z3fold_zpool_unmap(void *pool
, unsigned long handle
)
1767 z3fold_unmap(pool
, handle
);
1770 static u64
z3fold_zpool_total_size(void *pool
)
1772 return z3fold_get_pool_size(pool
) * PAGE_SIZE
;
1775 static struct zpool_driver z3fold_zpool_driver
= {
1777 .owner
= THIS_MODULE
,
1778 .create
= z3fold_zpool_create
,
1779 .destroy
= z3fold_zpool_destroy
,
1780 .malloc
= z3fold_zpool_malloc
,
1781 .free
= z3fold_zpool_free
,
1782 .shrink
= z3fold_zpool_shrink
,
1783 .map
= z3fold_zpool_map
,
1784 .unmap
= z3fold_zpool_unmap
,
1785 .total_size
= z3fold_zpool_total_size
,
1788 MODULE_ALIAS("zpool-z3fold");
1790 static int __init
init_z3fold(void)
1794 /* Make sure the z3fold header is not larger than the page size */
1795 BUILD_BUG_ON(ZHDR_SIZE_ALIGNED
> PAGE_SIZE
);
1796 ret
= z3fold_mount();
1800 zpool_register_driver(&z3fold_zpool_driver
);
1805 static void __exit
exit_z3fold(void)
1808 zpool_unregister_driver(&z3fold_zpool_driver
);
1811 module_init(init_z3fold
);
1812 module_exit(exit_z3fold
);
1814 MODULE_LICENSE("GPL");
1815 MODULE_AUTHOR("Vitaly Wool <vitalywool@gmail.com>");
1816 MODULE_DESCRIPTION("3-Fold Allocator for Compressed Pages");