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