xref: /openbmc/linux/mm/z3fold.c (revision ec2da07c)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * z3fold.c
4  *
5  * Author: Vitaly Wool <vitaly.wool@konsulko.com>
6  * Copyright (C) 2016, Sony Mobile Communications Inc.
7  *
8  * This implementation is based on zbud written by Seth Jennings.
9  *
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.
17  *
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.
20  *
21  * z3fold doesn't export any API and is meant to be used via zpool API.
22  */
23 
24 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25 
26 #include <linux/atomic.h>
27 #include <linux/sched.h>
28 #include <linux/cpumask.h>
29 #include <linux/list.h>
30 #include <linux/mm.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>
39 #include <linux/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/wait.h>
45 #include <linux/zpool.h>
46 #include <linux/magic.h>
47 
48 /*
49  * NCHUNKS_ORDER determines the internal allocation granularity, effectively
50  * adjusting internal fragmentation.  It also determines the number of
51  * freelists maintained in each pool. NCHUNKS_ORDER of 6 means that the
52  * allocation granularity will be in chunks of size PAGE_SIZE/64. Some chunks
53  * in the beginning of an allocated page are occupied by z3fold header, so
54  * NCHUNKS will be calculated to 63 (or 62 in case CONFIG_DEBUG_SPINLOCK=y),
55  * which shows the max number of free chunks in z3fold page, also there will
56  * be 63, or 62, respectively, freelists per pool.
57  */
58 #define NCHUNKS_ORDER	6
59 
60 #define CHUNK_SHIFT	(PAGE_SHIFT - NCHUNKS_ORDER)
61 #define CHUNK_SIZE	(1 << CHUNK_SHIFT)
62 #define ZHDR_SIZE_ALIGNED round_up(sizeof(struct z3fold_header), CHUNK_SIZE)
63 #define ZHDR_CHUNKS	(ZHDR_SIZE_ALIGNED >> CHUNK_SHIFT)
64 #define TOTAL_CHUNKS	(PAGE_SIZE >> CHUNK_SHIFT)
65 #define NCHUNKS		((PAGE_SIZE - ZHDR_SIZE_ALIGNED) >> CHUNK_SHIFT)
66 
67 #define BUDDY_MASK	(0x3)
68 #define BUDDY_SHIFT	2
69 #define SLOTS_ALIGN	(0x40)
70 
71 /*****************
72  * Structures
73 *****************/
74 struct z3fold_pool;
75 struct z3fold_ops {
76 	int (*evict)(struct z3fold_pool *pool, unsigned long handle);
77 };
78 
79 enum buddy {
80 	HEADLESS = 0,
81 	FIRST,
82 	MIDDLE,
83 	LAST,
84 	BUDDIES_MAX = LAST
85 };
86 
87 struct z3fold_buddy_slots {
88 	/*
89 	 * we are using BUDDY_MASK in handle_to_buddy etc. so there should
90 	 * be enough slots to hold all possible variants
91 	 */
92 	unsigned long slot[BUDDY_MASK + 1];
93 	unsigned long pool; /* back link + flags */
94 };
95 #define HANDLE_FLAG_MASK	(0x03)
96 
97 /*
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
101  *			pool
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
113  */
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;
121 	short cpu;
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 };
129 
130 /**
131  * struct z3fold_pool - stores metadata for each z3fold pool
132  * @name:	pool name
133  * @lock:	protects pool unbuddied/lru lists
134  * @stale_lock:	protects pool stale page list
135  * @unbuddied:	per-cpu array of lists tracking z3fold pages that contain 2-
136  *		buddies; the list each z3fold page is added to depends on
137  *		the size of its free region.
138  * @lru:	list tracking the z3fold pages in LRU order by most recently
139  *		added buddy.
140  * @stale:	list of pages marked for freeing
141  * @pages_nr:	number of z3fold pages in the pool.
142  * @c_handle:	cache for z3fold_buddy_slots allocation
143  * @ops:	pointer to a structure of user defined operations specified at
144  *		pool creation time.
145  * @compact_wq:	workqueue for page layout background optimization
146  * @release_wq:	workqueue for safe page release
147  * @work:	work_struct for safe page release
148  * @inode:	inode for z3fold pseudo filesystem
149  * @destroying: bool to stop migration once we start destruction
150  * @isolated: int to count the number of pages currently in isolation
151  *
152  * This structure is allocated at pool creation time and maintains metadata
153  * pertaining to a particular z3fold pool.
154  */
155 struct z3fold_pool {
156 	const char *name;
157 	spinlock_t lock;
158 	spinlock_t stale_lock;
159 	struct list_head *unbuddied;
160 	struct list_head lru;
161 	struct list_head stale;
162 	atomic64_t pages_nr;
163 	struct kmem_cache *c_handle;
164 	const struct z3fold_ops *ops;
165 	struct zpool *zpool;
166 	const struct zpool_ops *zpool_ops;
167 	struct workqueue_struct *compact_wq;
168 	struct workqueue_struct *release_wq;
169 	struct wait_queue_head isolate_wait;
170 	struct work_struct work;
171 	struct inode *inode;
172 	bool destroying;
173 	int isolated;
174 };
175 
176 /*
177  * Internal z3fold page flags
178  */
179 enum z3fold_page_flags {
180 	PAGE_HEADLESS = 0,
181 	MIDDLE_CHUNK_MAPPED,
182 	NEEDS_COMPACTING,
183 	PAGE_STALE,
184 	PAGE_CLAIMED, /* by either reclaim or free */
185 };
186 
187 /*****************
188  * Helpers
189 *****************/
190 
191 /* Converts an allocation size in bytes to size in z3fold chunks */
192 static int size_to_chunks(size_t size)
193 {
194 	return (size + CHUNK_SIZE - 1) >> CHUNK_SHIFT;
195 }
196 
197 #define for_each_unbuddied_list(_iter, _begin) \
198 	for ((_iter) = (_begin); (_iter) < NCHUNKS; (_iter)++)
199 
200 static void compact_page_work(struct work_struct *w);
201 
202 static inline struct z3fold_buddy_slots *alloc_slots(struct z3fold_pool *pool,
203 							gfp_t gfp)
204 {
205 	struct z3fold_buddy_slots *slots;
206 
207 	slots = kmem_cache_alloc(pool->c_handle,
208 				 (gfp & ~(__GFP_HIGHMEM | __GFP_MOVABLE)));
209 
210 	if (slots) {
211 		memset(slots->slot, 0, sizeof(slots->slot));
212 		slots->pool = (unsigned long)pool;
213 	}
214 
215 	return slots;
216 }
217 
218 static inline struct z3fold_pool *slots_to_pool(struct z3fold_buddy_slots *s)
219 {
220 	return (struct z3fold_pool *)(s->pool & ~HANDLE_FLAG_MASK);
221 }
222 
223 static inline struct z3fold_buddy_slots *handle_to_slots(unsigned long handle)
224 {
225 	return (struct z3fold_buddy_slots *)(handle & ~(SLOTS_ALIGN - 1));
226 }
227 
228 static inline void free_handle(unsigned long handle)
229 {
230 	struct z3fold_buddy_slots *slots;
231 	int i;
232 	bool is_free;
233 
234 	if (handle & (1 << PAGE_HEADLESS))
235 		return;
236 
237 	WARN_ON(*(unsigned long *)handle == 0);
238 	*(unsigned long *)handle = 0;
239 	slots = handle_to_slots(handle);
240 	is_free = true;
241 	for (i = 0; i <= BUDDY_MASK; i++) {
242 		if (slots->slot[i]) {
243 			is_free = false;
244 			break;
245 		}
246 	}
247 
248 	if (is_free) {
249 		struct z3fold_pool *pool = slots_to_pool(slots);
250 
251 		kmem_cache_free(pool->c_handle, slots);
252 	}
253 }
254 
255 static int z3fold_init_fs_context(struct fs_context *fc)
256 {
257 	return init_pseudo(fc, Z3FOLD_MAGIC) ? 0 : -ENOMEM;
258 }
259 
260 static struct file_system_type z3fold_fs = {
261 	.name		= "z3fold",
262 	.init_fs_context = z3fold_init_fs_context,
263 	.kill_sb	= kill_anon_super,
264 };
265 
266 static struct vfsmount *z3fold_mnt;
267 static int z3fold_mount(void)
268 {
269 	int ret = 0;
270 
271 	z3fold_mnt = kern_mount(&z3fold_fs);
272 	if (IS_ERR(z3fold_mnt))
273 		ret = PTR_ERR(z3fold_mnt);
274 
275 	return ret;
276 }
277 
278 static void z3fold_unmount(void)
279 {
280 	kern_unmount(z3fold_mnt);
281 }
282 
283 static const struct address_space_operations z3fold_aops;
284 static int z3fold_register_migration(struct z3fold_pool *pool)
285 {
286 	pool->inode = alloc_anon_inode(z3fold_mnt->mnt_sb);
287 	if (IS_ERR(pool->inode)) {
288 		pool->inode = NULL;
289 		return 1;
290 	}
291 
292 	pool->inode->i_mapping->private_data = pool;
293 	pool->inode->i_mapping->a_ops = &z3fold_aops;
294 	return 0;
295 }
296 
297 static void z3fold_unregister_migration(struct z3fold_pool *pool)
298 {
299 	if (pool->inode)
300 		iput(pool->inode);
301  }
302 
303 /* Initializes the z3fold header of a newly allocated z3fold page */
304 static struct z3fold_header *init_z3fold_page(struct page *page,
305 					struct z3fold_pool *pool, gfp_t gfp)
306 {
307 	struct z3fold_header *zhdr = page_address(page);
308 	struct z3fold_buddy_slots *slots = alloc_slots(pool, gfp);
309 
310 	if (!slots)
311 		return NULL;
312 
313 	INIT_LIST_HEAD(&page->lru);
314 	clear_bit(PAGE_HEADLESS, &page->private);
315 	clear_bit(MIDDLE_CHUNK_MAPPED, &page->private);
316 	clear_bit(NEEDS_COMPACTING, &page->private);
317 	clear_bit(PAGE_STALE, &page->private);
318 	clear_bit(PAGE_CLAIMED, &page->private);
319 
320 	spin_lock_init(&zhdr->page_lock);
321 	kref_init(&zhdr->refcount);
322 	zhdr->first_chunks = 0;
323 	zhdr->middle_chunks = 0;
324 	zhdr->last_chunks = 0;
325 	zhdr->first_num = 0;
326 	zhdr->start_middle = 0;
327 	zhdr->cpu = -1;
328 	zhdr->slots = slots;
329 	zhdr->pool = pool;
330 	INIT_LIST_HEAD(&zhdr->buddy);
331 	INIT_WORK(&zhdr->work, compact_page_work);
332 	return zhdr;
333 }
334 
335 /* Resets the struct page fields and frees the page */
336 static void free_z3fold_page(struct page *page, bool headless)
337 {
338 	if (!headless) {
339 		lock_page(page);
340 		__ClearPageMovable(page);
341 		unlock_page(page);
342 	}
343 	ClearPagePrivate(page);
344 	__free_page(page);
345 }
346 
347 /* Lock a z3fold page */
348 static inline void z3fold_page_lock(struct z3fold_header *zhdr)
349 {
350 	spin_lock(&zhdr->page_lock);
351 }
352 
353 /* Try to lock a z3fold page */
354 static inline int z3fold_page_trylock(struct z3fold_header *zhdr)
355 {
356 	return spin_trylock(&zhdr->page_lock);
357 }
358 
359 /* Unlock a z3fold page */
360 static inline void z3fold_page_unlock(struct z3fold_header *zhdr)
361 {
362 	spin_unlock(&zhdr->page_lock);
363 }
364 
365 /* Helper function to build the index */
366 static inline int __idx(struct z3fold_header *zhdr, enum buddy bud)
367 {
368 	return (bud + zhdr->first_num) & BUDDY_MASK;
369 }
370 
371 /*
372  * Encodes the handle of a particular buddy within a z3fold page
373  * Pool lock should be held as this function accesses first_num
374  */
375 static unsigned long encode_handle(struct z3fold_header *zhdr, enum buddy bud)
376 {
377 	struct z3fold_buddy_slots *slots;
378 	unsigned long h = (unsigned long)zhdr;
379 	int idx = 0;
380 
381 	/*
382 	 * For a headless page, its handle is its pointer with the extra
383 	 * PAGE_HEADLESS bit set
384 	 */
385 	if (bud == HEADLESS)
386 		return h | (1 << PAGE_HEADLESS);
387 
388 	/* otherwise, return pointer to encoded handle */
389 	idx = __idx(zhdr, bud);
390 	h += idx;
391 	if (bud == LAST)
392 		h |= (zhdr->last_chunks << BUDDY_SHIFT);
393 
394 	slots = zhdr->slots;
395 	slots->slot[idx] = h;
396 	return (unsigned long)&slots->slot[idx];
397 }
398 
399 /* Returns the z3fold page where a given handle is stored */
400 static inline struct z3fold_header *handle_to_z3fold_header(unsigned long h)
401 {
402 	unsigned long addr = h;
403 
404 	if (!(addr & (1 << PAGE_HEADLESS)))
405 		addr = *(unsigned long *)h;
406 
407 	return (struct z3fold_header *)(addr & PAGE_MASK);
408 }
409 
410 /* only for LAST bud, returns zero otherwise */
411 static unsigned short handle_to_chunks(unsigned long handle)
412 {
413 	unsigned long addr = *(unsigned long *)handle;
414 
415 	return (addr & ~PAGE_MASK) >> BUDDY_SHIFT;
416 }
417 
418 /*
419  * (handle & BUDDY_MASK) < zhdr->first_num is possible in encode_handle
420  *  but that doesn't matter. because the masking will result in the
421  *  correct buddy number.
422  */
423 static enum buddy handle_to_buddy(unsigned long handle)
424 {
425 	struct z3fold_header *zhdr;
426 	unsigned long addr;
427 
428 	WARN_ON(handle & (1 << PAGE_HEADLESS));
429 	addr = *(unsigned long *)handle;
430 	zhdr = (struct z3fold_header *)(addr & PAGE_MASK);
431 	return (addr - zhdr->first_num) & BUDDY_MASK;
432 }
433 
434 static inline struct z3fold_pool *zhdr_to_pool(struct z3fold_header *zhdr)
435 {
436 	return zhdr->pool;
437 }
438 
439 static void __release_z3fold_page(struct z3fold_header *zhdr, bool locked)
440 {
441 	struct page *page = virt_to_page(zhdr);
442 	struct z3fold_pool *pool = zhdr_to_pool(zhdr);
443 
444 	WARN_ON(!list_empty(&zhdr->buddy));
445 	set_bit(PAGE_STALE, &page->private);
446 	clear_bit(NEEDS_COMPACTING, &page->private);
447 	spin_lock(&pool->lock);
448 	if (!list_empty(&page->lru))
449 		list_del_init(&page->lru);
450 	spin_unlock(&pool->lock);
451 	if (locked)
452 		z3fold_page_unlock(zhdr);
453 	spin_lock(&pool->stale_lock);
454 	list_add(&zhdr->buddy, &pool->stale);
455 	queue_work(pool->release_wq, &pool->work);
456 	spin_unlock(&pool->stale_lock);
457 }
458 
459 static void __attribute__((__unused__))
460 			release_z3fold_page(struct kref *ref)
461 {
462 	struct z3fold_header *zhdr = container_of(ref, struct z3fold_header,
463 						refcount);
464 	__release_z3fold_page(zhdr, false);
465 }
466 
467 static void release_z3fold_page_locked(struct kref *ref)
468 {
469 	struct z3fold_header *zhdr = container_of(ref, struct z3fold_header,
470 						refcount);
471 	WARN_ON(z3fold_page_trylock(zhdr));
472 	__release_z3fold_page(zhdr, true);
473 }
474 
475 static void release_z3fold_page_locked_list(struct kref *ref)
476 {
477 	struct z3fold_header *zhdr = container_of(ref, struct z3fold_header,
478 					       refcount);
479 	struct z3fold_pool *pool = zhdr_to_pool(zhdr);
480 	spin_lock(&pool->lock);
481 	list_del_init(&zhdr->buddy);
482 	spin_unlock(&pool->lock);
483 
484 	WARN_ON(z3fold_page_trylock(zhdr));
485 	__release_z3fold_page(zhdr, true);
486 }
487 
488 static void free_pages_work(struct work_struct *w)
489 {
490 	struct z3fold_pool *pool = container_of(w, struct z3fold_pool, work);
491 
492 	spin_lock(&pool->stale_lock);
493 	while (!list_empty(&pool->stale)) {
494 		struct z3fold_header *zhdr = list_first_entry(&pool->stale,
495 						struct z3fold_header, buddy);
496 		struct page *page = virt_to_page(zhdr);
497 
498 		list_del(&zhdr->buddy);
499 		if (WARN_ON(!test_bit(PAGE_STALE, &page->private)))
500 			continue;
501 		spin_unlock(&pool->stale_lock);
502 		cancel_work_sync(&zhdr->work);
503 		free_z3fold_page(page, false);
504 		cond_resched();
505 		spin_lock(&pool->stale_lock);
506 	}
507 	spin_unlock(&pool->stale_lock);
508 }
509 
510 /*
511  * Returns the number of free chunks in a z3fold page.
512  * NB: can't be used with HEADLESS pages.
513  */
514 static int num_free_chunks(struct z3fold_header *zhdr)
515 {
516 	int nfree;
517 	/*
518 	 * If there is a middle object, pick up the bigger free space
519 	 * either before or after it. Otherwise just subtract the number
520 	 * of chunks occupied by the first and the last objects.
521 	 */
522 	if (zhdr->middle_chunks != 0) {
523 		int nfree_before = zhdr->first_chunks ?
524 			0 : zhdr->start_middle - ZHDR_CHUNKS;
525 		int nfree_after = zhdr->last_chunks ?
526 			0 : TOTAL_CHUNKS -
527 				(zhdr->start_middle + zhdr->middle_chunks);
528 		nfree = max(nfree_before, nfree_after);
529 	} else
530 		nfree = NCHUNKS - zhdr->first_chunks - zhdr->last_chunks;
531 	return nfree;
532 }
533 
534 /* Add to the appropriate unbuddied list */
535 static inline void add_to_unbuddied(struct z3fold_pool *pool,
536 				struct z3fold_header *zhdr)
537 {
538 	if (zhdr->first_chunks == 0 || zhdr->last_chunks == 0 ||
539 			zhdr->middle_chunks == 0) {
540 		struct list_head *unbuddied = get_cpu_ptr(pool->unbuddied);
541 
542 		int freechunks = num_free_chunks(zhdr);
543 		spin_lock(&pool->lock);
544 		list_add(&zhdr->buddy, &unbuddied[freechunks]);
545 		spin_unlock(&pool->lock);
546 		zhdr->cpu = smp_processor_id();
547 		put_cpu_ptr(pool->unbuddied);
548 	}
549 }
550 
551 static inline void *mchunk_memmove(struct z3fold_header *zhdr,
552 				unsigned short dst_chunk)
553 {
554 	void *beg = zhdr;
555 	return memmove(beg + (dst_chunk << CHUNK_SHIFT),
556 		       beg + (zhdr->start_middle << CHUNK_SHIFT),
557 		       zhdr->middle_chunks << CHUNK_SHIFT);
558 }
559 
560 #define BIG_CHUNK_GAP	3
561 /* Has to be called with lock held */
562 static int z3fold_compact_page(struct z3fold_header *zhdr)
563 {
564 	struct page *page = virt_to_page(zhdr);
565 
566 	if (test_bit(MIDDLE_CHUNK_MAPPED, &page->private))
567 		return 0; /* can't move middle chunk, it's used */
568 
569 	if (unlikely(PageIsolated(page)))
570 		return 0;
571 
572 	if (zhdr->middle_chunks == 0)
573 		return 0; /* nothing to compact */
574 
575 	if (zhdr->first_chunks == 0 && zhdr->last_chunks == 0) {
576 		/* move to the beginning */
577 		mchunk_memmove(zhdr, ZHDR_CHUNKS);
578 		zhdr->first_chunks = zhdr->middle_chunks;
579 		zhdr->middle_chunks = 0;
580 		zhdr->start_middle = 0;
581 		zhdr->first_num++;
582 		return 1;
583 	}
584 
585 	/*
586 	 * moving data is expensive, so let's only do that if
587 	 * there's substantial gain (at least BIG_CHUNK_GAP chunks)
588 	 */
589 	if (zhdr->first_chunks != 0 && zhdr->last_chunks == 0 &&
590 	    zhdr->start_middle - (zhdr->first_chunks + ZHDR_CHUNKS) >=
591 			BIG_CHUNK_GAP) {
592 		mchunk_memmove(zhdr, zhdr->first_chunks + ZHDR_CHUNKS);
593 		zhdr->start_middle = zhdr->first_chunks + ZHDR_CHUNKS;
594 		return 1;
595 	} else if (zhdr->last_chunks != 0 && zhdr->first_chunks == 0 &&
596 		   TOTAL_CHUNKS - (zhdr->last_chunks + zhdr->start_middle
597 					+ zhdr->middle_chunks) >=
598 			BIG_CHUNK_GAP) {
599 		unsigned short new_start = TOTAL_CHUNKS - zhdr->last_chunks -
600 			zhdr->middle_chunks;
601 		mchunk_memmove(zhdr, new_start);
602 		zhdr->start_middle = new_start;
603 		return 1;
604 	}
605 
606 	return 0;
607 }
608 
609 static void do_compact_page(struct z3fold_header *zhdr, bool locked)
610 {
611 	struct z3fold_pool *pool = zhdr_to_pool(zhdr);
612 	struct page *page;
613 
614 	page = virt_to_page(zhdr);
615 	if (locked)
616 		WARN_ON(z3fold_page_trylock(zhdr));
617 	else
618 		z3fold_page_lock(zhdr);
619 	if (WARN_ON(!test_and_clear_bit(NEEDS_COMPACTING, &page->private))) {
620 		z3fold_page_unlock(zhdr);
621 		return;
622 	}
623 	spin_lock(&pool->lock);
624 	list_del_init(&zhdr->buddy);
625 	spin_unlock(&pool->lock);
626 
627 	if (kref_put(&zhdr->refcount, release_z3fold_page_locked)) {
628 		atomic64_dec(&pool->pages_nr);
629 		return;
630 	}
631 
632 	if (unlikely(PageIsolated(page) ||
633 		     test_bit(PAGE_STALE, &page->private))) {
634 		z3fold_page_unlock(zhdr);
635 		return;
636 	}
637 
638 	z3fold_compact_page(zhdr);
639 	add_to_unbuddied(pool, zhdr);
640 	z3fold_page_unlock(zhdr);
641 }
642 
643 static void compact_page_work(struct work_struct *w)
644 {
645 	struct z3fold_header *zhdr = container_of(w, struct z3fold_header,
646 						work);
647 
648 	do_compact_page(zhdr, false);
649 }
650 
651 /* returns _locked_ z3fold page header or NULL */
652 static inline struct z3fold_header *__z3fold_alloc(struct z3fold_pool *pool,
653 						size_t size, bool can_sleep)
654 {
655 	struct z3fold_header *zhdr = NULL;
656 	struct page *page;
657 	struct list_head *unbuddied;
658 	int chunks = size_to_chunks(size), i;
659 
660 lookup:
661 	/* First, try to find an unbuddied z3fold page. */
662 	unbuddied = get_cpu_ptr(pool->unbuddied);
663 	for_each_unbuddied_list(i, chunks) {
664 		struct list_head *l = &unbuddied[i];
665 
666 		zhdr = list_first_entry_or_null(READ_ONCE(l),
667 					struct z3fold_header, buddy);
668 
669 		if (!zhdr)
670 			continue;
671 
672 		/* Re-check under lock. */
673 		spin_lock(&pool->lock);
674 		l = &unbuddied[i];
675 		if (unlikely(zhdr != list_first_entry(READ_ONCE(l),
676 						struct z3fold_header, buddy)) ||
677 		    !z3fold_page_trylock(zhdr)) {
678 			spin_unlock(&pool->lock);
679 			zhdr = NULL;
680 			put_cpu_ptr(pool->unbuddied);
681 			if (can_sleep)
682 				cond_resched();
683 			goto lookup;
684 		}
685 		list_del_init(&zhdr->buddy);
686 		zhdr->cpu = -1;
687 		spin_unlock(&pool->lock);
688 
689 		page = virt_to_page(zhdr);
690 		if (test_bit(NEEDS_COMPACTING, &page->private)) {
691 			z3fold_page_unlock(zhdr);
692 			zhdr = NULL;
693 			put_cpu_ptr(pool->unbuddied);
694 			if (can_sleep)
695 				cond_resched();
696 			goto lookup;
697 		}
698 
699 		/*
700 		 * this page could not be removed from its unbuddied
701 		 * list while pool lock was held, and then we've taken
702 		 * page lock so kref_put could not be called before
703 		 * we got here, so it's safe to just call kref_get()
704 		 */
705 		kref_get(&zhdr->refcount);
706 		break;
707 	}
708 	put_cpu_ptr(pool->unbuddied);
709 
710 	if (!zhdr) {
711 		int cpu;
712 
713 		/* look for _exact_ match on other cpus' lists */
714 		for_each_online_cpu(cpu) {
715 			struct list_head *l;
716 
717 			unbuddied = per_cpu_ptr(pool->unbuddied, cpu);
718 			spin_lock(&pool->lock);
719 			l = &unbuddied[chunks];
720 
721 			zhdr = list_first_entry_or_null(READ_ONCE(l),
722 						struct z3fold_header, buddy);
723 
724 			if (!zhdr || !z3fold_page_trylock(zhdr)) {
725 				spin_unlock(&pool->lock);
726 				zhdr = NULL;
727 				continue;
728 			}
729 			list_del_init(&zhdr->buddy);
730 			zhdr->cpu = -1;
731 			spin_unlock(&pool->lock);
732 
733 			page = virt_to_page(zhdr);
734 			if (test_bit(NEEDS_COMPACTING, &page->private)) {
735 				z3fold_page_unlock(zhdr);
736 				zhdr = NULL;
737 				if (can_sleep)
738 					cond_resched();
739 				continue;
740 			}
741 			kref_get(&zhdr->refcount);
742 			break;
743 		}
744 	}
745 
746 	return zhdr;
747 }
748 
749 /*
750  * API Functions
751  */
752 
753 /**
754  * z3fold_create_pool() - create a new z3fold pool
755  * @name:	pool name
756  * @gfp:	gfp flags when allocating the z3fold pool structure
757  * @ops:	user-defined operations for the z3fold pool
758  *
759  * Return: pointer to the new z3fold pool or NULL if the metadata allocation
760  * failed.
761  */
762 static struct z3fold_pool *z3fold_create_pool(const char *name, gfp_t gfp,
763 		const struct z3fold_ops *ops)
764 {
765 	struct z3fold_pool *pool = NULL;
766 	int i, cpu;
767 
768 	pool = kzalloc(sizeof(struct z3fold_pool), gfp);
769 	if (!pool)
770 		goto out;
771 	pool->c_handle = kmem_cache_create("z3fold_handle",
772 				sizeof(struct z3fold_buddy_slots),
773 				SLOTS_ALIGN, 0, NULL);
774 	if (!pool->c_handle)
775 		goto out_c;
776 	spin_lock_init(&pool->lock);
777 	spin_lock_init(&pool->stale_lock);
778 	init_waitqueue_head(&pool->isolate_wait);
779 	pool->unbuddied = __alloc_percpu(sizeof(struct list_head)*NCHUNKS, 2);
780 	if (!pool->unbuddied)
781 		goto out_pool;
782 	for_each_possible_cpu(cpu) {
783 		struct list_head *unbuddied =
784 				per_cpu_ptr(pool->unbuddied, cpu);
785 		for_each_unbuddied_list(i, 0)
786 			INIT_LIST_HEAD(&unbuddied[i]);
787 	}
788 	INIT_LIST_HEAD(&pool->lru);
789 	INIT_LIST_HEAD(&pool->stale);
790 	atomic64_set(&pool->pages_nr, 0);
791 	pool->name = name;
792 	pool->compact_wq = create_singlethread_workqueue(pool->name);
793 	if (!pool->compact_wq)
794 		goto out_unbuddied;
795 	pool->release_wq = create_singlethread_workqueue(pool->name);
796 	if (!pool->release_wq)
797 		goto out_wq;
798 	if (z3fold_register_migration(pool))
799 		goto out_rwq;
800 	INIT_WORK(&pool->work, free_pages_work);
801 	pool->ops = ops;
802 	return pool;
803 
804 out_rwq:
805 	destroy_workqueue(pool->release_wq);
806 out_wq:
807 	destroy_workqueue(pool->compact_wq);
808 out_unbuddied:
809 	free_percpu(pool->unbuddied);
810 out_pool:
811 	kmem_cache_destroy(pool->c_handle);
812 out_c:
813 	kfree(pool);
814 out:
815 	return NULL;
816 }
817 
818 static bool pool_isolated_are_drained(struct z3fold_pool *pool)
819 {
820 	bool ret;
821 
822 	spin_lock(&pool->lock);
823 	ret = pool->isolated == 0;
824 	spin_unlock(&pool->lock);
825 	return ret;
826 }
827 /**
828  * z3fold_destroy_pool() - destroys an existing z3fold pool
829  * @pool:	the z3fold pool to be destroyed
830  *
831  * The pool should be emptied before this function is called.
832  */
833 static void z3fold_destroy_pool(struct z3fold_pool *pool)
834 {
835 	kmem_cache_destroy(pool->c_handle);
836 	/*
837 	 * We set pool-> destroying under lock to ensure that
838 	 * z3fold_page_isolate() sees any changes to destroying. This way we
839 	 * avoid the need for any memory barriers.
840 	 */
841 
842 	spin_lock(&pool->lock);
843 	pool->destroying = true;
844 	spin_unlock(&pool->lock);
845 
846 	/*
847 	 * We need to ensure that no pages are being migrated while we destroy
848 	 * these workqueues, as migration can queue work on either of the
849 	 * workqueues.
850 	 */
851 	wait_event(pool->isolate_wait, !pool_isolated_are_drained(pool));
852 
853 	/*
854 	 * We need to destroy pool->compact_wq before pool->release_wq,
855 	 * as any pending work on pool->compact_wq will call
856 	 * queue_work(pool->release_wq, &pool->work).
857 	 *
858 	 * There are still outstanding pages until both workqueues are drained,
859 	 * so we cannot unregister migration until then.
860 	 */
861 
862 	destroy_workqueue(pool->compact_wq);
863 	destroy_workqueue(pool->release_wq);
864 	z3fold_unregister_migration(pool);
865 	kfree(pool);
866 }
867 
868 /**
869  * z3fold_alloc() - allocates a region of a given size
870  * @pool:	z3fold pool from which to allocate
871  * @size:	size in bytes of the desired allocation
872  * @gfp:	gfp flags used if the pool needs to grow
873  * @handle:	handle of the new allocation
874  *
875  * This function will attempt to find a free region in the pool large enough to
876  * satisfy the allocation request.  A search of the unbuddied lists is
877  * performed first. If no suitable free region is found, then a new page is
878  * allocated and added to the pool to satisfy the request.
879  *
880  * gfp should not set __GFP_HIGHMEM as highmem pages cannot be used
881  * as z3fold pool pages.
882  *
883  * Return: 0 if success and handle is set, otherwise -EINVAL if the size or
884  * gfp arguments are invalid or -ENOMEM if the pool was unable to allocate
885  * a new page.
886  */
887 static int z3fold_alloc(struct z3fold_pool *pool, size_t size, gfp_t gfp,
888 			unsigned long *handle)
889 {
890 	int chunks = size_to_chunks(size);
891 	struct z3fold_header *zhdr = NULL;
892 	struct page *page = NULL;
893 	enum buddy bud;
894 	bool can_sleep = gfpflags_allow_blocking(gfp);
895 
896 	if (!size)
897 		return -EINVAL;
898 
899 	if (size > PAGE_SIZE)
900 		return -ENOSPC;
901 
902 	if (size > PAGE_SIZE - ZHDR_SIZE_ALIGNED - CHUNK_SIZE)
903 		bud = HEADLESS;
904 	else {
905 retry:
906 		zhdr = __z3fold_alloc(pool, size, can_sleep);
907 		if (zhdr) {
908 			if (zhdr->first_chunks == 0) {
909 				if (zhdr->middle_chunks != 0 &&
910 				    chunks >= zhdr->start_middle)
911 					bud = LAST;
912 				else
913 					bud = FIRST;
914 			} else if (zhdr->last_chunks == 0)
915 				bud = LAST;
916 			else if (zhdr->middle_chunks == 0)
917 				bud = MIDDLE;
918 			else {
919 				if (kref_put(&zhdr->refcount,
920 					     release_z3fold_page_locked))
921 					atomic64_dec(&pool->pages_nr);
922 				else
923 					z3fold_page_unlock(zhdr);
924 				pr_err("No free chunks in unbuddied\n");
925 				WARN_ON(1);
926 				goto retry;
927 			}
928 			page = virt_to_page(zhdr);
929 			goto found;
930 		}
931 		bud = FIRST;
932 	}
933 
934 	page = NULL;
935 	if (can_sleep) {
936 		spin_lock(&pool->stale_lock);
937 		zhdr = list_first_entry_or_null(&pool->stale,
938 						struct z3fold_header, buddy);
939 		/*
940 		 * Before allocating a page, let's see if we can take one from
941 		 * the stale pages list. cancel_work_sync() can sleep so we
942 		 * limit this case to the contexts where we can sleep
943 		 */
944 		if (zhdr) {
945 			list_del(&zhdr->buddy);
946 			spin_unlock(&pool->stale_lock);
947 			cancel_work_sync(&zhdr->work);
948 			page = virt_to_page(zhdr);
949 		} else {
950 			spin_unlock(&pool->stale_lock);
951 		}
952 	}
953 	if (!page)
954 		page = alloc_page(gfp);
955 
956 	if (!page)
957 		return -ENOMEM;
958 
959 	zhdr = init_z3fold_page(page, pool, gfp);
960 	if (!zhdr) {
961 		__free_page(page);
962 		return -ENOMEM;
963 	}
964 	atomic64_inc(&pool->pages_nr);
965 
966 	if (bud == HEADLESS) {
967 		set_bit(PAGE_HEADLESS, &page->private);
968 		goto headless;
969 	}
970 	if (can_sleep) {
971 		lock_page(page);
972 		__SetPageMovable(page, pool->inode->i_mapping);
973 		unlock_page(page);
974 	} else {
975 		if (trylock_page(page)) {
976 			__SetPageMovable(page, pool->inode->i_mapping);
977 			unlock_page(page);
978 		}
979 	}
980 	z3fold_page_lock(zhdr);
981 
982 found:
983 	if (bud == FIRST)
984 		zhdr->first_chunks = chunks;
985 	else if (bud == LAST)
986 		zhdr->last_chunks = chunks;
987 	else {
988 		zhdr->middle_chunks = chunks;
989 		zhdr->start_middle = zhdr->first_chunks + ZHDR_CHUNKS;
990 	}
991 	add_to_unbuddied(pool, zhdr);
992 
993 headless:
994 	spin_lock(&pool->lock);
995 	/* Add/move z3fold page to beginning of LRU */
996 	if (!list_empty(&page->lru))
997 		list_del(&page->lru);
998 
999 	list_add(&page->lru, &pool->lru);
1000 
1001 	*handle = encode_handle(zhdr, bud);
1002 	spin_unlock(&pool->lock);
1003 	if (bud != HEADLESS)
1004 		z3fold_page_unlock(zhdr);
1005 
1006 	return 0;
1007 }
1008 
1009 /**
1010  * z3fold_free() - frees the allocation associated with the given handle
1011  * @pool:	pool in which the allocation resided
1012  * @handle:	handle associated with the allocation returned by z3fold_alloc()
1013  *
1014  * In the case that the z3fold page in which the allocation resides is under
1015  * reclaim, as indicated by the PG_reclaim flag being set, this function
1016  * only sets the first|last_chunks to 0.  The page is actually freed
1017  * once both buddies are evicted (see z3fold_reclaim_page() below).
1018  */
1019 static void z3fold_free(struct z3fold_pool *pool, unsigned long handle)
1020 {
1021 	struct z3fold_header *zhdr;
1022 	struct page *page;
1023 	enum buddy bud;
1024 
1025 	zhdr = handle_to_z3fold_header(handle);
1026 	page = virt_to_page(zhdr);
1027 
1028 	if (test_bit(PAGE_HEADLESS, &page->private)) {
1029 		/* if a headless page is under reclaim, just leave.
1030 		 * NB: we use test_and_set_bit for a reason: if the bit
1031 		 * has not been set before, we release this page
1032 		 * immediately so we don't care about its value any more.
1033 		 */
1034 		if (!test_and_set_bit(PAGE_CLAIMED, &page->private)) {
1035 			spin_lock(&pool->lock);
1036 			list_del(&page->lru);
1037 			spin_unlock(&pool->lock);
1038 			free_z3fold_page(page, true);
1039 			atomic64_dec(&pool->pages_nr);
1040 		}
1041 		return;
1042 	}
1043 
1044 	/* Non-headless case */
1045 	z3fold_page_lock(zhdr);
1046 	bud = handle_to_buddy(handle);
1047 
1048 	switch (bud) {
1049 	case FIRST:
1050 		zhdr->first_chunks = 0;
1051 		break;
1052 	case MIDDLE:
1053 		zhdr->middle_chunks = 0;
1054 		break;
1055 	case LAST:
1056 		zhdr->last_chunks = 0;
1057 		break;
1058 	default:
1059 		pr_err("%s: unknown bud %d\n", __func__, bud);
1060 		WARN_ON(1);
1061 		z3fold_page_unlock(zhdr);
1062 		return;
1063 	}
1064 
1065 	free_handle(handle);
1066 	if (kref_put(&zhdr->refcount, release_z3fold_page_locked_list)) {
1067 		atomic64_dec(&pool->pages_nr);
1068 		return;
1069 	}
1070 	if (test_bit(PAGE_CLAIMED, &page->private)) {
1071 		z3fold_page_unlock(zhdr);
1072 		return;
1073 	}
1074 	if (unlikely(PageIsolated(page)) ||
1075 	    test_and_set_bit(NEEDS_COMPACTING, &page->private)) {
1076 		z3fold_page_unlock(zhdr);
1077 		return;
1078 	}
1079 	if (zhdr->cpu < 0 || !cpu_online(zhdr->cpu)) {
1080 		spin_lock(&pool->lock);
1081 		list_del_init(&zhdr->buddy);
1082 		spin_unlock(&pool->lock);
1083 		zhdr->cpu = -1;
1084 		kref_get(&zhdr->refcount);
1085 		do_compact_page(zhdr, true);
1086 		return;
1087 	}
1088 	kref_get(&zhdr->refcount);
1089 	queue_work_on(zhdr->cpu, pool->compact_wq, &zhdr->work);
1090 	z3fold_page_unlock(zhdr);
1091 }
1092 
1093 /**
1094  * z3fold_reclaim_page() - evicts allocations from a pool page and frees it
1095  * @pool:	pool from which a page will attempt to be evicted
1096  * @retries:	number of pages on the LRU list for which eviction will
1097  *		be attempted before failing
1098  *
1099  * z3fold reclaim is different from normal system reclaim in that it is done
1100  * from the bottom, up. This is because only the bottom layer, z3fold, has
1101  * information on how the allocations are organized within each z3fold page.
1102  * This has the potential to create interesting locking situations between
1103  * z3fold and the user, however.
1104  *
1105  * To avoid these, this is how z3fold_reclaim_page() should be called:
1106  *
1107  * The user detects a page should be reclaimed and calls z3fold_reclaim_page().
1108  * z3fold_reclaim_page() will remove a z3fold page from the pool LRU list and
1109  * call the user-defined eviction handler with the pool and handle as
1110  * arguments.
1111  *
1112  * If the handle can not be evicted, the eviction handler should return
1113  * non-zero. z3fold_reclaim_page() will add the z3fold page back to the
1114  * appropriate list and try the next z3fold page on the LRU up to
1115  * a user defined number of retries.
1116  *
1117  * If the handle is successfully evicted, the eviction handler should
1118  * return 0 _and_ should have called z3fold_free() on the handle. z3fold_free()
1119  * contains logic to delay freeing the page if the page is under reclaim,
1120  * as indicated by the setting of the PG_reclaim flag on the underlying page.
1121  *
1122  * If all buddies in the z3fold page are successfully evicted, then the
1123  * z3fold page can be freed.
1124  *
1125  * Returns: 0 if page is successfully freed, otherwise -EINVAL if there are
1126  * no pages to evict or an eviction handler is not registered, -EAGAIN if
1127  * the retry limit was hit.
1128  */
1129 static int z3fold_reclaim_page(struct z3fold_pool *pool, unsigned int retries)
1130 {
1131 	int i, ret = 0;
1132 	struct z3fold_header *zhdr = NULL;
1133 	struct page *page = NULL;
1134 	struct list_head *pos;
1135 	unsigned long first_handle = 0, middle_handle = 0, last_handle = 0;
1136 
1137 	spin_lock(&pool->lock);
1138 	if (!pool->ops || !pool->ops->evict || retries == 0) {
1139 		spin_unlock(&pool->lock);
1140 		return -EINVAL;
1141 	}
1142 	for (i = 0; i < retries; i++) {
1143 		if (list_empty(&pool->lru)) {
1144 			spin_unlock(&pool->lock);
1145 			return -EINVAL;
1146 		}
1147 		list_for_each_prev(pos, &pool->lru) {
1148 			page = list_entry(pos, struct page, lru);
1149 
1150 			/* this bit could have been set by free, in which case
1151 			 * we pass over to the next page in the pool.
1152 			 */
1153 			if (test_and_set_bit(PAGE_CLAIMED, &page->private))
1154 				continue;
1155 
1156 			if (unlikely(PageIsolated(page)))
1157 				continue;
1158 			if (test_bit(PAGE_HEADLESS, &page->private))
1159 				break;
1160 
1161 			zhdr = page_address(page);
1162 			if (!z3fold_page_trylock(zhdr)) {
1163 				zhdr = NULL;
1164 				continue; /* can't evict at this point */
1165 			}
1166 			kref_get(&zhdr->refcount);
1167 			list_del_init(&zhdr->buddy);
1168 			zhdr->cpu = -1;
1169 			break;
1170 		}
1171 
1172 		if (!zhdr)
1173 			break;
1174 
1175 		list_del_init(&page->lru);
1176 		spin_unlock(&pool->lock);
1177 
1178 		if (!test_bit(PAGE_HEADLESS, &page->private)) {
1179 			/*
1180 			 * We need encode the handles before unlocking, since
1181 			 * we can race with free that will set
1182 			 * (first|last)_chunks to 0
1183 			 */
1184 			first_handle = 0;
1185 			last_handle = 0;
1186 			middle_handle = 0;
1187 			if (zhdr->first_chunks)
1188 				first_handle = encode_handle(zhdr, FIRST);
1189 			if (zhdr->middle_chunks)
1190 				middle_handle = encode_handle(zhdr, MIDDLE);
1191 			if (zhdr->last_chunks)
1192 				last_handle = encode_handle(zhdr, LAST);
1193 			/*
1194 			 * it's safe to unlock here because we hold a
1195 			 * reference to this page
1196 			 */
1197 			z3fold_page_unlock(zhdr);
1198 		} else {
1199 			first_handle = encode_handle(zhdr, HEADLESS);
1200 			last_handle = middle_handle = 0;
1201 		}
1202 
1203 		/* Issue the eviction callback(s) */
1204 		if (middle_handle) {
1205 			ret = pool->ops->evict(pool, middle_handle);
1206 			if (ret)
1207 				goto next;
1208 		}
1209 		if (first_handle) {
1210 			ret = pool->ops->evict(pool, first_handle);
1211 			if (ret)
1212 				goto next;
1213 		}
1214 		if (last_handle) {
1215 			ret = pool->ops->evict(pool, last_handle);
1216 			if (ret)
1217 				goto next;
1218 		}
1219 next:
1220 		if (test_bit(PAGE_HEADLESS, &page->private)) {
1221 			if (ret == 0) {
1222 				free_z3fold_page(page, true);
1223 				atomic64_dec(&pool->pages_nr);
1224 				return 0;
1225 			}
1226 			spin_lock(&pool->lock);
1227 			list_add(&page->lru, &pool->lru);
1228 			spin_unlock(&pool->lock);
1229 		} else {
1230 			z3fold_page_lock(zhdr);
1231 			clear_bit(PAGE_CLAIMED, &page->private);
1232 			if (kref_put(&zhdr->refcount,
1233 					release_z3fold_page_locked)) {
1234 				atomic64_dec(&pool->pages_nr);
1235 				return 0;
1236 			}
1237 			/*
1238 			 * if we are here, the page is still not completely
1239 			 * free. Take the global pool lock then to be able
1240 			 * to add it back to the lru list
1241 			 */
1242 			spin_lock(&pool->lock);
1243 			list_add(&page->lru, &pool->lru);
1244 			spin_unlock(&pool->lock);
1245 			z3fold_page_unlock(zhdr);
1246 		}
1247 
1248 		/* We started off locked to we need to lock the pool back */
1249 		spin_lock(&pool->lock);
1250 	}
1251 	spin_unlock(&pool->lock);
1252 	return -EAGAIN;
1253 }
1254 
1255 /**
1256  * z3fold_map() - maps the allocation associated with the given handle
1257  * @pool:	pool in which the allocation resides
1258  * @handle:	handle associated with the allocation to be mapped
1259  *
1260  * Extracts the buddy number from handle and constructs the pointer to the
1261  * correct starting chunk within the page.
1262  *
1263  * Returns: a pointer to the mapped allocation
1264  */
1265 static void *z3fold_map(struct z3fold_pool *pool, unsigned long handle)
1266 {
1267 	struct z3fold_header *zhdr;
1268 	struct page *page;
1269 	void *addr;
1270 	enum buddy buddy;
1271 
1272 	zhdr = handle_to_z3fold_header(handle);
1273 	addr = zhdr;
1274 	page = virt_to_page(zhdr);
1275 
1276 	if (test_bit(PAGE_HEADLESS, &page->private))
1277 		goto out;
1278 
1279 	z3fold_page_lock(zhdr);
1280 	buddy = handle_to_buddy(handle);
1281 	switch (buddy) {
1282 	case FIRST:
1283 		addr += ZHDR_SIZE_ALIGNED;
1284 		break;
1285 	case MIDDLE:
1286 		addr += zhdr->start_middle << CHUNK_SHIFT;
1287 		set_bit(MIDDLE_CHUNK_MAPPED, &page->private);
1288 		break;
1289 	case LAST:
1290 		addr += PAGE_SIZE - (handle_to_chunks(handle) << CHUNK_SHIFT);
1291 		break;
1292 	default:
1293 		pr_err("unknown buddy id %d\n", buddy);
1294 		WARN_ON(1);
1295 		addr = NULL;
1296 		break;
1297 	}
1298 
1299 	if (addr)
1300 		zhdr->mapped_count++;
1301 	z3fold_page_unlock(zhdr);
1302 out:
1303 	return addr;
1304 }
1305 
1306 /**
1307  * z3fold_unmap() - unmaps the allocation associated with the given handle
1308  * @pool:	pool in which the allocation resides
1309  * @handle:	handle associated with the allocation to be unmapped
1310  */
1311 static void z3fold_unmap(struct z3fold_pool *pool, unsigned long handle)
1312 {
1313 	struct z3fold_header *zhdr;
1314 	struct page *page;
1315 	enum buddy buddy;
1316 
1317 	zhdr = handle_to_z3fold_header(handle);
1318 	page = virt_to_page(zhdr);
1319 
1320 	if (test_bit(PAGE_HEADLESS, &page->private))
1321 		return;
1322 
1323 	z3fold_page_lock(zhdr);
1324 	buddy = handle_to_buddy(handle);
1325 	if (buddy == MIDDLE)
1326 		clear_bit(MIDDLE_CHUNK_MAPPED, &page->private);
1327 	zhdr->mapped_count--;
1328 	z3fold_page_unlock(zhdr);
1329 }
1330 
1331 /**
1332  * z3fold_get_pool_size() - gets the z3fold pool size in pages
1333  * @pool:	pool whose size is being queried
1334  *
1335  * Returns: size in pages of the given pool.
1336  */
1337 static u64 z3fold_get_pool_size(struct z3fold_pool *pool)
1338 {
1339 	return atomic64_read(&pool->pages_nr);
1340 }
1341 
1342 /*
1343  * z3fold_dec_isolated() expects to be called while pool->lock is held.
1344  */
1345 static void z3fold_dec_isolated(struct z3fold_pool *pool)
1346 {
1347 	assert_spin_locked(&pool->lock);
1348 	VM_BUG_ON(pool->isolated <= 0);
1349 	pool->isolated--;
1350 
1351 	/*
1352 	 * If we have no more isolated pages, we have to see if
1353 	 * z3fold_destroy_pool() is waiting for a signal.
1354 	 */
1355 	if (pool->isolated == 0 && waitqueue_active(&pool->isolate_wait))
1356 		wake_up_all(&pool->isolate_wait);
1357 }
1358 
1359 static void z3fold_inc_isolated(struct z3fold_pool *pool)
1360 {
1361 	pool->isolated++;
1362 }
1363 
1364 static bool z3fold_page_isolate(struct page *page, isolate_mode_t mode)
1365 {
1366 	struct z3fold_header *zhdr;
1367 	struct z3fold_pool *pool;
1368 
1369 	VM_BUG_ON_PAGE(!PageMovable(page), page);
1370 	VM_BUG_ON_PAGE(PageIsolated(page), page);
1371 
1372 	if (test_bit(PAGE_HEADLESS, &page->private))
1373 		return false;
1374 
1375 	zhdr = page_address(page);
1376 	z3fold_page_lock(zhdr);
1377 	if (test_bit(NEEDS_COMPACTING, &page->private) ||
1378 	    test_bit(PAGE_STALE, &page->private))
1379 		goto out;
1380 
1381 	pool = zhdr_to_pool(zhdr);
1382 
1383 	if (zhdr->mapped_count == 0) {
1384 		kref_get(&zhdr->refcount);
1385 		if (!list_empty(&zhdr->buddy))
1386 			list_del_init(&zhdr->buddy);
1387 		spin_lock(&pool->lock);
1388 		if (!list_empty(&page->lru))
1389 			list_del(&page->lru);
1390 		/*
1391 		 * We need to check for destruction while holding pool->lock, as
1392 		 * otherwise destruction could see 0 isolated pages, and
1393 		 * proceed.
1394 		 */
1395 		if (unlikely(pool->destroying)) {
1396 			spin_unlock(&pool->lock);
1397 			/*
1398 			 * If this page isn't stale, somebody else holds a
1399 			 * reference to it. Let't drop our refcount so that they
1400 			 * can call the release logic.
1401 			 */
1402 			if (unlikely(kref_put(&zhdr->refcount,
1403 					      release_z3fold_page_locked))) {
1404 				/*
1405 				 * If we get here we have kref problems, so we
1406 				 * should freak out.
1407 				 */
1408 				WARN(1, "Z3fold is experiencing kref problems\n");
1409 				return false;
1410 			}
1411 			z3fold_page_unlock(zhdr);
1412 			return false;
1413 		}
1414 
1415 
1416 		z3fold_inc_isolated(pool);
1417 		spin_unlock(&pool->lock);
1418 		z3fold_page_unlock(zhdr);
1419 		return true;
1420 	}
1421 out:
1422 	z3fold_page_unlock(zhdr);
1423 	return false;
1424 }
1425 
1426 static int z3fold_page_migrate(struct address_space *mapping, struct page *newpage,
1427 			       struct page *page, enum migrate_mode mode)
1428 {
1429 	struct z3fold_header *zhdr, *new_zhdr;
1430 	struct z3fold_pool *pool;
1431 	struct address_space *new_mapping;
1432 
1433 	VM_BUG_ON_PAGE(!PageMovable(page), page);
1434 	VM_BUG_ON_PAGE(!PageIsolated(page), page);
1435 	VM_BUG_ON_PAGE(!PageLocked(newpage), newpage);
1436 
1437 	zhdr = page_address(page);
1438 	pool = zhdr_to_pool(zhdr);
1439 
1440 	if (!z3fold_page_trylock(zhdr)) {
1441 		return -EAGAIN;
1442 	}
1443 	if (zhdr->mapped_count != 0) {
1444 		z3fold_page_unlock(zhdr);
1445 		return -EBUSY;
1446 	}
1447 	if (work_pending(&zhdr->work)) {
1448 		z3fold_page_unlock(zhdr);
1449 		return -EAGAIN;
1450 	}
1451 	new_zhdr = page_address(newpage);
1452 	memcpy(new_zhdr, zhdr, PAGE_SIZE);
1453 	newpage->private = page->private;
1454 	page->private = 0;
1455 	z3fold_page_unlock(zhdr);
1456 	spin_lock_init(&new_zhdr->page_lock);
1457 	INIT_WORK(&new_zhdr->work, compact_page_work);
1458 	/*
1459 	 * z3fold_page_isolate() ensures that new_zhdr->buddy is empty,
1460 	 * so we only have to reinitialize it.
1461 	 */
1462 	INIT_LIST_HEAD(&new_zhdr->buddy);
1463 	new_mapping = page_mapping(page);
1464 	__ClearPageMovable(page);
1465 	ClearPagePrivate(page);
1466 
1467 	get_page(newpage);
1468 	z3fold_page_lock(new_zhdr);
1469 	if (new_zhdr->first_chunks)
1470 		encode_handle(new_zhdr, FIRST);
1471 	if (new_zhdr->last_chunks)
1472 		encode_handle(new_zhdr, LAST);
1473 	if (new_zhdr->middle_chunks)
1474 		encode_handle(new_zhdr, MIDDLE);
1475 	set_bit(NEEDS_COMPACTING, &newpage->private);
1476 	new_zhdr->cpu = smp_processor_id();
1477 	spin_lock(&pool->lock);
1478 	list_add(&newpage->lru, &pool->lru);
1479 	spin_unlock(&pool->lock);
1480 	__SetPageMovable(newpage, new_mapping);
1481 	z3fold_page_unlock(new_zhdr);
1482 
1483 	queue_work_on(new_zhdr->cpu, pool->compact_wq, &new_zhdr->work);
1484 
1485 	spin_lock(&pool->lock);
1486 	z3fold_dec_isolated(pool);
1487 	spin_unlock(&pool->lock);
1488 
1489 	page_mapcount_reset(page);
1490 	put_page(page);
1491 	return 0;
1492 }
1493 
1494 static void z3fold_page_putback(struct page *page)
1495 {
1496 	struct z3fold_header *zhdr;
1497 	struct z3fold_pool *pool;
1498 
1499 	zhdr = page_address(page);
1500 	pool = zhdr_to_pool(zhdr);
1501 
1502 	z3fold_page_lock(zhdr);
1503 	if (!list_empty(&zhdr->buddy))
1504 		list_del_init(&zhdr->buddy);
1505 	INIT_LIST_HEAD(&page->lru);
1506 	if (kref_put(&zhdr->refcount, release_z3fold_page_locked)) {
1507 		atomic64_dec(&pool->pages_nr);
1508 		spin_lock(&pool->lock);
1509 		z3fold_dec_isolated(pool);
1510 		spin_unlock(&pool->lock);
1511 		return;
1512 	}
1513 	spin_lock(&pool->lock);
1514 	list_add(&page->lru, &pool->lru);
1515 	z3fold_dec_isolated(pool);
1516 	spin_unlock(&pool->lock);
1517 	z3fold_page_unlock(zhdr);
1518 }
1519 
1520 static const struct address_space_operations z3fold_aops = {
1521 	.isolate_page = z3fold_page_isolate,
1522 	.migratepage = z3fold_page_migrate,
1523 	.putback_page = z3fold_page_putback,
1524 };
1525 
1526 /*****************
1527  * zpool
1528  ****************/
1529 
1530 static int z3fold_zpool_evict(struct z3fold_pool *pool, unsigned long handle)
1531 {
1532 	if (pool->zpool && pool->zpool_ops && pool->zpool_ops->evict)
1533 		return pool->zpool_ops->evict(pool->zpool, handle);
1534 	else
1535 		return -ENOENT;
1536 }
1537 
1538 static const struct z3fold_ops z3fold_zpool_ops = {
1539 	.evict =	z3fold_zpool_evict
1540 };
1541 
1542 static void *z3fold_zpool_create(const char *name, gfp_t gfp,
1543 			       const struct zpool_ops *zpool_ops,
1544 			       struct zpool *zpool)
1545 {
1546 	struct z3fold_pool *pool;
1547 
1548 	pool = z3fold_create_pool(name, gfp,
1549 				zpool_ops ? &z3fold_zpool_ops : NULL);
1550 	if (pool) {
1551 		pool->zpool = zpool;
1552 		pool->zpool_ops = zpool_ops;
1553 	}
1554 	return pool;
1555 }
1556 
1557 static void z3fold_zpool_destroy(void *pool)
1558 {
1559 	z3fold_destroy_pool(pool);
1560 }
1561 
1562 static int z3fold_zpool_malloc(void *pool, size_t size, gfp_t gfp,
1563 			unsigned long *handle)
1564 {
1565 	return z3fold_alloc(pool, size, gfp, handle);
1566 }
1567 static void z3fold_zpool_free(void *pool, unsigned long handle)
1568 {
1569 	z3fold_free(pool, handle);
1570 }
1571 
1572 static int z3fold_zpool_shrink(void *pool, unsigned int pages,
1573 			unsigned int *reclaimed)
1574 {
1575 	unsigned int total = 0;
1576 	int ret = -EINVAL;
1577 
1578 	while (total < pages) {
1579 		ret = z3fold_reclaim_page(pool, 8);
1580 		if (ret < 0)
1581 			break;
1582 		total++;
1583 	}
1584 
1585 	if (reclaimed)
1586 		*reclaimed = total;
1587 
1588 	return ret;
1589 }
1590 
1591 static void *z3fold_zpool_map(void *pool, unsigned long handle,
1592 			enum zpool_mapmode mm)
1593 {
1594 	return z3fold_map(pool, handle);
1595 }
1596 static void z3fold_zpool_unmap(void *pool, unsigned long handle)
1597 {
1598 	z3fold_unmap(pool, handle);
1599 }
1600 
1601 static u64 z3fold_zpool_total_size(void *pool)
1602 {
1603 	return z3fold_get_pool_size(pool) * PAGE_SIZE;
1604 }
1605 
1606 static struct zpool_driver z3fold_zpool_driver = {
1607 	.type =		"z3fold",
1608 	.owner =	THIS_MODULE,
1609 	.create =	z3fold_zpool_create,
1610 	.destroy =	z3fold_zpool_destroy,
1611 	.malloc =	z3fold_zpool_malloc,
1612 	.free =		z3fold_zpool_free,
1613 	.shrink =	z3fold_zpool_shrink,
1614 	.map =		z3fold_zpool_map,
1615 	.unmap =	z3fold_zpool_unmap,
1616 	.total_size =	z3fold_zpool_total_size,
1617 };
1618 
1619 MODULE_ALIAS("zpool-z3fold");
1620 
1621 static int __init init_z3fold(void)
1622 {
1623 	int ret;
1624 
1625 	/* Make sure the z3fold header is not larger than the page size */
1626 	BUILD_BUG_ON(ZHDR_SIZE_ALIGNED > PAGE_SIZE);
1627 	ret = z3fold_mount();
1628 	if (ret)
1629 		return ret;
1630 
1631 	zpool_register_driver(&z3fold_zpool_driver);
1632 
1633 	return 0;
1634 }
1635 
1636 static void __exit exit_z3fold(void)
1637 {
1638 	z3fold_unmount();
1639 	zpool_unregister_driver(&z3fold_zpool_driver);
1640 }
1641 
1642 module_init(init_z3fold);
1643 module_exit(exit_z3fold);
1644 
1645 MODULE_LICENSE("GPL");
1646 MODULE_AUTHOR("Vitaly Wool <vitalywool@gmail.com>");
1647 MODULE_DESCRIPTION("3-Fold Allocator for Compressed Pages");
1648