blob: 42f31c4b53ad43e4204874fb20d3d901d9ef921a [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Vitaly Wool9a001fc2016-05-20 16:58:30 -07002/*
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>
Vitaly Woold30561c2017-09-06 16:24:47 -070027#include <linux/sched.h>
Vitaly Wool1f862982019-05-13 17:22:52 -070028#include <linux/cpumask.h>
Vitaly Wool9a001fc2016-05-20 16:58:30 -070029#include <linux/list.h>
30#include <linux/mm.h>
31#include <linux/module.h>
Vitaly Wool1f862982019-05-13 17:22:52 -070032#include <linux/page-flags.h>
33#include <linux/migrate.h>
34#include <linux/node.h>
35#include <linux/compaction.h>
Vitaly Woold30561c2017-09-06 16:24:47 -070036#include <linux/percpu.h>
Vitaly Wool1f862982019-05-13 17:22:52 -070037#include <linux/mount.h>
David Howellsea8157a2019-05-21 07:55:45 +010038#include <linux/pseudo_fs.h>
Vitaly Wool1f862982019-05-13 17:22:52 -070039#include <linux/fs.h>
Vitaly Wool9a001fc2016-05-20 16:58:30 -070040#include <linux/preempt.h>
Vitaly Woold30561c2017-09-06 16:24:47 -070041#include <linux/workqueue.h>
Vitaly Wool9a001fc2016-05-20 16:58:30 -070042#include <linux/slab.h>
43#include <linux/spinlock.h>
44#include <linux/zpool.h>
David Howellsea8157a2019-05-21 07:55:45 +010045#include <linux/magic.h>
Vitaly Wool9a001fc2016-05-20 16:58:30 -070046
Vitaly Wool9a001fc2016-05-20 16:58:30 -070047/*
48 * NCHUNKS_ORDER determines the internal allocation granularity, effectively
49 * adjusting internal fragmentation. It also determines the number of
50 * freelists maintained in each pool. NCHUNKS_ORDER of 6 means that the
Vitaly Woolede93212017-02-24 14:57:17 -080051 * allocation granularity will be in chunks of size PAGE_SIZE/64. Some chunks
52 * in the beginning of an allocated page are occupied by z3fold header, so
53 * NCHUNKS will be calculated to 63 (or 62 in case CONFIG_DEBUG_SPINLOCK=y),
54 * which shows the max number of free chunks in z3fold page, also there will
55 * be 63, or 62, respectively, freelists per pool.
Vitaly Wool9a001fc2016-05-20 16:58:30 -070056 */
57#define NCHUNKS_ORDER 6
58
59#define CHUNK_SHIFT (PAGE_SHIFT - NCHUNKS_ORDER)
60#define CHUNK_SIZE (1 << CHUNK_SHIFT)
Vitaly Woolede93212017-02-24 14:57:17 -080061#define ZHDR_SIZE_ALIGNED round_up(sizeof(struct z3fold_header), CHUNK_SIZE)
62#define ZHDR_CHUNKS (ZHDR_SIZE_ALIGNED >> CHUNK_SHIFT)
63#define TOTAL_CHUNKS (PAGE_SIZE >> CHUNK_SHIFT)
Vitaly Wool9a001fc2016-05-20 16:58:30 -070064#define NCHUNKS ((PAGE_SIZE - ZHDR_SIZE_ALIGNED) >> CHUNK_SHIFT)
65
zhong jiangf201ebd2017-02-22 15:46:51 -080066#define BUDDY_MASK (0x3)
Vitaly Woolca0246b2018-11-16 15:07:56 -080067#define BUDDY_SHIFT 2
Vitaly Wool7c2b8ba2019-05-13 17:22:49 -070068#define SLOTS_ALIGN (0x40)
69
70/*****************
71 * Structures
72*****************/
73struct z3fold_pool;
74struct z3fold_ops {
75 int (*evict)(struct z3fold_pool *pool, unsigned long handle);
76};
77
78enum buddy {
79 HEADLESS = 0,
80 FIRST,
81 MIDDLE,
82 LAST,
83 BUDDIES_MAX = LAST
84};
85
86struct z3fold_buddy_slots {
87 /*
88 * we are using BUDDY_MASK in handle_to_buddy etc. so there should
89 * be enough slots to hold all possible variants
90 */
91 unsigned long slot[BUDDY_MASK + 1];
92 unsigned long pool; /* back link + flags */
Vitaly Wool4a3ac932019-11-30 17:56:11 -080093 rwlock_t lock;
Vitaly Wool7c2b8ba2019-05-13 17:22:49 -070094};
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
Vitaly Woolbb9a3742019-07-16 16:25:48 -0700106 * @pool: pointer to the containing pool
Vitaly Wool7c2b8ba2019-05-13 17:22:49 -0700107 * @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)
Vitaly Wool1f862982019-05-13 17:22:52 -0700112 * @mapped_count: the number of objects currently mapped
Vitaly Wool7c2b8ba2019-05-13 17:22:49 -0700113 */
114struct 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;
Vitaly Woolbb9a3742019-07-16 16:25:48 -0700120 struct z3fold_pool *pool;
Vitaly Wool7c2b8ba2019-05-13 17:22:49 -0700121 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;
Vitaly Wool1f862982019-05-13 17:22:52 -0700127 unsigned short mapped_count:2;
Vitaly Wool4a3ac932019-11-30 17:56:11 -0800128 unsigned short foreign_handles:2;
Vitaly Wool7c2b8ba2019-05-13 17:22:49 -0700129};
Vitaly Wool9a001fc2016-05-20 16:58:30 -0700130
Vitaly Wool9a001fc2016-05-20 16:58:30 -0700131/**
132 * struct z3fold_pool - stores metadata for each z3fold pool
Vitaly Woold30561c2017-09-06 16:24:47 -0700133 * @name: pool name
134 * @lock: protects pool unbuddied/lru lists
135 * @stale_lock: protects pool stale page list
136 * @unbuddied: per-cpu array of lists tracking z3fold pages that contain 2-
137 * buddies; the list each z3fold page is added to depends on
138 * the size of its free region.
Vitaly Wool9a001fc2016-05-20 16:58:30 -0700139 * @lru: list tracking the z3fold pages in LRU order by most recently
140 * added buddy.
Vitaly Woold30561c2017-09-06 16:24:47 -0700141 * @stale: list of pages marked for freeing
Vitaly Wool9a001fc2016-05-20 16:58:30 -0700142 * @pages_nr: number of z3fold pages in the pool.
Vitaly Wool7c2b8ba2019-05-13 17:22:49 -0700143 * @c_handle: cache for z3fold_buddy_slots allocation
Vitaly Wool9a001fc2016-05-20 16:58:30 -0700144 * @ops: pointer to a structure of user defined operations specified at
145 * pool creation time.
Vitaly Woold30561c2017-09-06 16:24:47 -0700146 * @compact_wq: workqueue for page layout background optimization
147 * @release_wq: workqueue for safe page release
148 * @work: work_struct for safe page release
Vitaly Wool1f862982019-05-13 17:22:52 -0700149 * @inode: inode for z3fold pseudo filesystem
Vitaly Wool9a001fc2016-05-20 16:58:30 -0700150 *
151 * This structure is allocated at pool creation time and maintains metadata
152 * pertaining to a particular z3fold pool.
153 */
154struct z3fold_pool {
Vitaly Woold30561c2017-09-06 16:24:47 -0700155 const char *name;
Vitaly Wool9a001fc2016-05-20 16:58:30 -0700156 spinlock_t lock;
Vitaly Woold30561c2017-09-06 16:24:47 -0700157 spinlock_t stale_lock;
158 struct list_head *unbuddied;
Vitaly Wool9a001fc2016-05-20 16:58:30 -0700159 struct list_head lru;
Vitaly Woold30561c2017-09-06 16:24:47 -0700160 struct list_head stale;
Vitaly Wool12d59ae2017-02-24 14:57:15 -0800161 atomic64_t pages_nr;
Vitaly Wool7c2b8ba2019-05-13 17:22:49 -0700162 struct kmem_cache *c_handle;
Vitaly Wool9a001fc2016-05-20 16:58:30 -0700163 const struct z3fold_ops *ops;
164 struct zpool *zpool;
165 const struct zpool_ops *zpool_ops;
Vitaly Woold30561c2017-09-06 16:24:47 -0700166 struct workqueue_struct *compact_wq;
167 struct workqueue_struct *release_wq;
168 struct work_struct work;
Vitaly Wool1f862982019-05-13 17:22:52 -0700169 struct inode *inode;
Vitaly Wool9a001fc2016-05-20 16:58:30 -0700170};
171
Vitaly Wool9a001fc2016-05-20 16:58:30 -0700172/*
173 * Internal z3fold page flags
174 */
175enum z3fold_page_flags {
Vitaly Wool5a27aa82017-02-24 14:57:26 -0800176 PAGE_HEADLESS = 0,
Vitaly Wool9a001fc2016-05-20 16:58:30 -0700177 MIDDLE_CHUNK_MAPPED,
Vitaly Woold30561c2017-09-06 16:24:47 -0700178 NEEDS_COMPACTING,
Vitaly Wool6098d7e2018-05-11 16:01:46 -0700179 PAGE_STALE,
Vitaly Woolca0246b2018-11-16 15:07:56 -0800180 PAGE_CLAIMED, /* by either reclaim or free */
Vitaly Wool9a001fc2016-05-20 16:58:30 -0700181};
182
Vitaly Wool4a3ac932019-11-30 17:56:11 -0800183/*
184 * handle flags, go under HANDLE_FLAG_MASK
185 */
186enum z3fold_handle_flags {
187 HANDLES_ORPHANED = 0,
188};
189
190/*
191 * Forward declarations
192 */
193static struct z3fold_header *__z3fold_alloc(struct z3fold_pool *, size_t, bool);
194static void compact_page_work(struct work_struct *w);
195
Vitaly Wool9a001fc2016-05-20 16:58:30 -0700196/*****************
197 * Helpers
198*****************/
199
200/* Converts an allocation size in bytes to size in z3fold chunks */
201static int size_to_chunks(size_t size)
202{
203 return (size + CHUNK_SIZE - 1) >> CHUNK_SHIFT;
204}
205
206#define for_each_unbuddied_list(_iter, _begin) \
207 for ((_iter) = (_begin); (_iter) < NCHUNKS; (_iter)++)
208
Vitaly Woolbb9f6f62019-05-31 22:30:39 -0700209static inline struct z3fold_buddy_slots *alloc_slots(struct z3fold_pool *pool,
210 gfp_t gfp)
Vitaly Wool7c2b8ba2019-05-13 17:22:49 -0700211{
Henry Burnsf1549cb2019-07-16 16:26:03 -0700212 struct z3fold_buddy_slots *slots;
213
214 slots = kmem_cache_alloc(pool->c_handle,
215 (gfp & ~(__GFP_HIGHMEM | __GFP_MOVABLE)));
Vitaly Wool7c2b8ba2019-05-13 17:22:49 -0700216
217 if (slots) {
218 memset(slots->slot, 0, sizeof(slots->slot));
219 slots->pool = (unsigned long)pool;
Vitaly Wool4a3ac932019-11-30 17:56:11 -0800220 rwlock_init(&slots->lock);
Vitaly Wool7c2b8ba2019-05-13 17:22:49 -0700221 }
222
223 return slots;
224}
225
226static inline struct z3fold_pool *slots_to_pool(struct z3fold_buddy_slots *s)
227{
228 return (struct z3fold_pool *)(s->pool & ~HANDLE_FLAG_MASK);
229}
230
231static inline struct z3fold_buddy_slots *handle_to_slots(unsigned long handle)
232{
233 return (struct z3fold_buddy_slots *)(handle & ~(SLOTS_ALIGN - 1));
234}
235
Vitaly Wool4a3ac932019-11-30 17:56:11 -0800236/* Lock a z3fold page */
237static inline void z3fold_page_lock(struct z3fold_header *zhdr)
238{
239 spin_lock(&zhdr->page_lock);
240}
241
242/* Try to lock a z3fold page */
243static inline int z3fold_page_trylock(struct z3fold_header *zhdr)
244{
245 return spin_trylock(&zhdr->page_lock);
246}
247
248/* Unlock a z3fold page */
249static inline void z3fold_page_unlock(struct z3fold_header *zhdr)
250{
251 spin_unlock(&zhdr->page_lock);
252}
253
254
255static inline struct z3fold_header *__get_z3fold_header(unsigned long handle,
256 bool lock)
257{
258 struct z3fold_buddy_slots *slots;
259 struct z3fold_header *zhdr;
260 int locked = 0;
261
262 if (!(handle & (1 << PAGE_HEADLESS))) {
263 slots = handle_to_slots(handle);
264 do {
265 unsigned long addr;
266
267 read_lock(&slots->lock);
268 addr = *(unsigned long *)handle;
269 zhdr = (struct z3fold_header *)(addr & PAGE_MASK);
270 if (lock)
271 locked = z3fold_page_trylock(zhdr);
272 read_unlock(&slots->lock);
273 if (locked)
274 break;
275 cpu_relax();
276 } while (lock);
277 } else {
278 zhdr = (struct z3fold_header *)(handle & PAGE_MASK);
279 }
280
281 return zhdr;
282}
283
284/* Returns the z3fold page where a given handle is stored */
285static inline struct z3fold_header *handle_to_z3fold_header(unsigned long h)
286{
287 return __get_z3fold_header(h, false);
288}
289
290/* return locked z3fold page if it's not headless */
291static inline struct z3fold_header *get_z3fold_header(unsigned long h)
292{
293 return __get_z3fold_header(h, true);
294}
295
296static inline void put_z3fold_header(struct z3fold_header *zhdr)
297{
298 struct page *page = virt_to_page(zhdr);
299
300 if (!test_bit(PAGE_HEADLESS, &page->private))
301 z3fold_page_unlock(zhdr);
302}
303
Vitaly Wool7c2b8ba2019-05-13 17:22:49 -0700304static inline void free_handle(unsigned long handle)
305{
306 struct z3fold_buddy_slots *slots;
Vitaly Wool4a3ac932019-11-30 17:56:11 -0800307 struct z3fold_header *zhdr;
Vitaly Wool7c2b8ba2019-05-13 17:22:49 -0700308 int i;
309 bool is_free;
310
311 if (handle & (1 << PAGE_HEADLESS))
312 return;
313
Vitaly Wool4a3ac932019-11-30 17:56:11 -0800314 if (WARN_ON(*(unsigned long *)handle == 0))
315 return;
316
317 zhdr = handle_to_z3fold_header(handle);
Vitaly Wool7c2b8ba2019-05-13 17:22:49 -0700318 slots = handle_to_slots(handle);
Vitaly Wool4a3ac932019-11-30 17:56:11 -0800319 write_lock(&slots->lock);
320 *(unsigned long *)handle = 0;
321 write_unlock(&slots->lock);
322 if (zhdr->slots == slots)
323 return; /* simple case, nothing else to do */
324
325 /* we are freeing a foreign handle if we are here */
326 zhdr->foreign_handles--;
Vitaly Wool7c2b8ba2019-05-13 17:22:49 -0700327 is_free = true;
Vitaly Wool4a3ac932019-11-30 17:56:11 -0800328 read_lock(&slots->lock);
329 if (!test_bit(HANDLES_ORPHANED, &slots->pool)) {
330 read_unlock(&slots->lock);
331 return;
332 }
Vitaly Wool7c2b8ba2019-05-13 17:22:49 -0700333 for (i = 0; i <= BUDDY_MASK; i++) {
334 if (slots->slot[i]) {
335 is_free = false;
336 break;
337 }
338 }
Vitaly Wool4a3ac932019-11-30 17:56:11 -0800339 read_unlock(&slots->lock);
Vitaly Wool7c2b8ba2019-05-13 17:22:49 -0700340
341 if (is_free) {
342 struct z3fold_pool *pool = slots_to_pool(slots);
343
344 kmem_cache_free(pool->c_handle, slots);
345 }
346}
347
David Howellsea8157a2019-05-21 07:55:45 +0100348static int z3fold_init_fs_context(struct fs_context *fc)
Vitaly Wool1f862982019-05-13 17:22:52 -0700349{
David Howellsea8157a2019-05-21 07:55:45 +0100350 return init_pseudo(fc, Z3FOLD_MAGIC) ? 0 : -ENOMEM;
Vitaly Wool1f862982019-05-13 17:22:52 -0700351}
352
353static struct file_system_type z3fold_fs = {
354 .name = "z3fold",
David Howellsea8157a2019-05-21 07:55:45 +0100355 .init_fs_context = z3fold_init_fs_context,
Vitaly Wool1f862982019-05-13 17:22:52 -0700356 .kill_sb = kill_anon_super,
357};
358
359static struct vfsmount *z3fold_mnt;
360static int z3fold_mount(void)
361{
362 int ret = 0;
363
364 z3fold_mnt = kern_mount(&z3fold_fs);
365 if (IS_ERR(z3fold_mnt))
366 ret = PTR_ERR(z3fold_mnt);
367
368 return ret;
369}
370
371static void z3fold_unmount(void)
372{
373 kern_unmount(z3fold_mnt);
374}
375
376static const struct address_space_operations z3fold_aops;
377static int z3fold_register_migration(struct z3fold_pool *pool)
378{
379 pool->inode = alloc_anon_inode(z3fold_mnt->mnt_sb);
380 if (IS_ERR(pool->inode)) {
381 pool->inode = NULL;
382 return 1;
383 }
384
385 pool->inode->i_mapping->private_data = pool;
386 pool->inode->i_mapping->a_ops = &z3fold_aops;
387 return 0;
388}
389
390static void z3fold_unregister_migration(struct z3fold_pool *pool)
391{
392 if (pool->inode)
393 iput(pool->inode);
394 }
395
Vitaly Wool9a001fc2016-05-20 16:58:30 -0700396/* Initializes the z3fold header of a newly allocated z3fold page */
Vitaly Wool63398412019-09-23 15:36:51 -0700397static struct z3fold_header *init_z3fold_page(struct page *page, bool headless,
Vitaly Woolbb9f6f62019-05-31 22:30:39 -0700398 struct z3fold_pool *pool, gfp_t gfp)
Vitaly Wool9a001fc2016-05-20 16:58:30 -0700399{
400 struct z3fold_header *zhdr = page_address(page);
Vitaly Wool63398412019-09-23 15:36:51 -0700401 struct z3fold_buddy_slots *slots;
Vitaly Wool9a001fc2016-05-20 16:58:30 -0700402
403 INIT_LIST_HEAD(&page->lru);
Vitaly Wool9a001fc2016-05-20 16:58:30 -0700404 clear_bit(PAGE_HEADLESS, &page->private);
405 clear_bit(MIDDLE_CHUNK_MAPPED, &page->private);
Vitaly Woold30561c2017-09-06 16:24:47 -0700406 clear_bit(NEEDS_COMPACTING, &page->private);
407 clear_bit(PAGE_STALE, &page->private);
Vitaly Woolca0246b2018-11-16 15:07:56 -0800408 clear_bit(PAGE_CLAIMED, &page->private);
Vitaly Wool63398412019-09-23 15:36:51 -0700409 if (headless)
410 return zhdr;
411
412 slots = alloc_slots(pool, gfp);
413 if (!slots)
414 return NULL;
Vitaly Wool9a001fc2016-05-20 16:58:30 -0700415
Vitaly Wool2f1e5e42017-02-24 14:57:23 -0800416 spin_lock_init(&zhdr->page_lock);
Vitaly Wool5a27aa82017-02-24 14:57:26 -0800417 kref_init(&zhdr->refcount);
Vitaly Wool9a001fc2016-05-20 16:58:30 -0700418 zhdr->first_chunks = 0;
419 zhdr->middle_chunks = 0;
420 zhdr->last_chunks = 0;
421 zhdr->first_num = 0;
422 zhdr->start_middle = 0;
Vitaly Woold30561c2017-09-06 16:24:47 -0700423 zhdr->cpu = -1;
Vitaly Wool4a3ac932019-11-30 17:56:11 -0800424 zhdr->foreign_handles = 0;
Vitaly Wool7c2b8ba2019-05-13 17:22:49 -0700425 zhdr->slots = slots;
Vitaly Woolbb9a3742019-07-16 16:25:48 -0700426 zhdr->pool = pool;
Vitaly Wool9a001fc2016-05-20 16:58:30 -0700427 INIT_LIST_HEAD(&zhdr->buddy);
Vitaly Woold30561c2017-09-06 16:24:47 -0700428 INIT_WORK(&zhdr->work, compact_page_work);
Vitaly Wool9a001fc2016-05-20 16:58:30 -0700429 return zhdr;
430}
431
432/* Resets the struct page fields and frees the page */
Vitaly Wool1f862982019-05-13 17:22:52 -0700433static void free_z3fold_page(struct page *page, bool headless)
Vitaly Wool9a001fc2016-05-20 16:58:30 -0700434{
Vitaly Wool1f862982019-05-13 17:22:52 -0700435 if (!headless) {
436 lock_page(page);
437 __ClearPageMovable(page);
438 unlock_page(page);
439 }
440 ClearPagePrivate(page);
Vitaly Wool5a27aa82017-02-24 14:57:26 -0800441 __free_page(page);
442}
443
Vitaly Wool7c2b8ba2019-05-13 17:22:49 -0700444/* Helper function to build the index */
445static inline int __idx(struct z3fold_header *zhdr, enum buddy bud)
446{
447 return (bud + zhdr->first_num) & BUDDY_MASK;
448}
449
Vitaly Wool9a001fc2016-05-20 16:58:30 -0700450/*
451 * Encodes the handle of a particular buddy within a z3fold page
452 * Pool lock should be held as this function accesses first_num
453 */
Vitaly Wool3f9d2b52019-09-23 15:33:02 -0700454static unsigned long __encode_handle(struct z3fold_header *zhdr,
455 struct z3fold_buddy_slots *slots,
456 enum buddy bud)
Vitaly Wool9a001fc2016-05-20 16:58:30 -0700457{
Vitaly Wool7c2b8ba2019-05-13 17:22:49 -0700458 unsigned long h = (unsigned long)zhdr;
459 int idx = 0;
Vitaly Wool9a001fc2016-05-20 16:58:30 -0700460
Vitaly Wool7c2b8ba2019-05-13 17:22:49 -0700461 /*
462 * For a headless page, its handle is its pointer with the extra
463 * PAGE_HEADLESS bit set
464 */
465 if (bud == HEADLESS)
466 return h | (1 << PAGE_HEADLESS);
467
468 /* otherwise, return pointer to encoded handle */
469 idx = __idx(zhdr, bud);
470 h += idx;
471 if (bud == LAST)
472 h |= (zhdr->last_chunks << BUDDY_SHIFT);
473
Vitaly Wool4a3ac932019-11-30 17:56:11 -0800474 write_lock(&slots->lock);
Vitaly Wool7c2b8ba2019-05-13 17:22:49 -0700475 slots->slot[idx] = h;
Vitaly Wool4a3ac932019-11-30 17:56:11 -0800476 write_unlock(&slots->lock);
Vitaly Wool7c2b8ba2019-05-13 17:22:49 -0700477 return (unsigned long)&slots->slot[idx];
Vitaly Wool9a001fc2016-05-20 16:58:30 -0700478}
479
Vitaly Wool3f9d2b52019-09-23 15:33:02 -0700480static unsigned long encode_handle(struct z3fold_header *zhdr, enum buddy bud)
481{
482 return __encode_handle(zhdr, zhdr->slots, bud);
483}
484
Vitaly Woolca0246b2018-11-16 15:07:56 -0800485/* only for LAST bud, returns zero otherwise */
486static unsigned short handle_to_chunks(unsigned long handle)
487{
Vitaly Wool4a3ac932019-11-30 17:56:11 -0800488 struct z3fold_buddy_slots *slots = handle_to_slots(handle);
489 unsigned long addr;
Vitaly Wool7c2b8ba2019-05-13 17:22:49 -0700490
Vitaly Wool4a3ac932019-11-30 17:56:11 -0800491 read_lock(&slots->lock);
492 addr = *(unsigned long *)handle;
493 read_unlock(&slots->lock);
Vitaly Wool7c2b8ba2019-05-13 17:22:49 -0700494 return (addr & ~PAGE_MASK) >> BUDDY_SHIFT;
Vitaly Woolca0246b2018-11-16 15:07:56 -0800495}
496
zhong jiangf201ebd2017-02-22 15:46:51 -0800497/*
498 * (handle & BUDDY_MASK) < zhdr->first_num is possible in encode_handle
499 * but that doesn't matter. because the masking will result in the
500 * correct buddy number.
501 */
Vitaly Wool9a001fc2016-05-20 16:58:30 -0700502static enum buddy handle_to_buddy(unsigned long handle)
503{
Vitaly Wool7c2b8ba2019-05-13 17:22:49 -0700504 struct z3fold_header *zhdr;
Vitaly Wool4a3ac932019-11-30 17:56:11 -0800505 struct z3fold_buddy_slots *slots = handle_to_slots(handle);
Vitaly Wool7c2b8ba2019-05-13 17:22:49 -0700506 unsigned long addr;
507
Vitaly Wool4a3ac932019-11-30 17:56:11 -0800508 read_lock(&slots->lock);
Vitaly Wool7c2b8ba2019-05-13 17:22:49 -0700509 WARN_ON(handle & (1 << PAGE_HEADLESS));
510 addr = *(unsigned long *)handle;
Vitaly Wool4a3ac932019-11-30 17:56:11 -0800511 read_unlock(&slots->lock);
Vitaly Wool7c2b8ba2019-05-13 17:22:49 -0700512 zhdr = (struct z3fold_header *)(addr & PAGE_MASK);
513 return (addr - zhdr->first_num) & BUDDY_MASK;
Vitaly Wool9a001fc2016-05-20 16:58:30 -0700514}
515
Vitaly Wool9050cce2019-05-13 17:22:43 -0700516static inline struct z3fold_pool *zhdr_to_pool(struct z3fold_header *zhdr)
517{
Vitaly Woolbb9a3742019-07-16 16:25:48 -0700518 return zhdr->pool;
Vitaly Wool9050cce2019-05-13 17:22:43 -0700519}
520
Vitaly Woold30561c2017-09-06 16:24:47 -0700521static void __release_z3fold_page(struct z3fold_header *zhdr, bool locked)
522{
523 struct page *page = virt_to_page(zhdr);
Vitaly Wool9050cce2019-05-13 17:22:43 -0700524 struct z3fold_pool *pool = zhdr_to_pool(zhdr);
Vitaly Wool4a3ac932019-11-30 17:56:11 -0800525 bool is_free = true;
526 int i;
Vitaly Woold30561c2017-09-06 16:24:47 -0700527
528 WARN_ON(!list_empty(&zhdr->buddy));
529 set_bit(PAGE_STALE, &page->private);
Vitaly Wool35529352017-10-03 16:15:06 -0700530 clear_bit(NEEDS_COMPACTING, &page->private);
Vitaly Woold30561c2017-09-06 16:24:47 -0700531 spin_lock(&pool->lock);
532 if (!list_empty(&page->lru))
Vitaly Wool1f862982019-05-13 17:22:52 -0700533 list_del_init(&page->lru);
Vitaly Woold30561c2017-09-06 16:24:47 -0700534 spin_unlock(&pool->lock);
Vitaly Wool4a3ac932019-11-30 17:56:11 -0800535
536 /* If there are no foreign handles, free the handles array */
537 read_lock(&zhdr->slots->lock);
538 for (i = 0; i <= BUDDY_MASK; i++) {
539 if (zhdr->slots->slot[i]) {
540 is_free = false;
541 break;
542 }
543 }
544 if (!is_free)
545 set_bit(HANDLES_ORPHANED, &zhdr->slots->pool);
546 read_unlock(&zhdr->slots->lock);
547
548 if (is_free)
549 kmem_cache_free(pool->c_handle, zhdr->slots);
550
Vitaly Woold30561c2017-09-06 16:24:47 -0700551 if (locked)
552 z3fold_page_unlock(zhdr);
Vitaly Wool4a3ac932019-11-30 17:56:11 -0800553
Vitaly Woold30561c2017-09-06 16:24:47 -0700554 spin_lock(&pool->stale_lock);
555 list_add(&zhdr->buddy, &pool->stale);
556 queue_work(pool->release_wq, &pool->work);
557 spin_unlock(&pool->stale_lock);
558}
559
560static void __attribute__((__unused__))
561 release_z3fold_page(struct kref *ref)
562{
563 struct z3fold_header *zhdr = container_of(ref, struct z3fold_header,
564 refcount);
565 __release_z3fold_page(zhdr, false);
566}
567
568static void release_z3fold_page_locked(struct kref *ref)
569{
570 struct z3fold_header *zhdr = container_of(ref, struct z3fold_header,
571 refcount);
572 WARN_ON(z3fold_page_trylock(zhdr));
573 __release_z3fold_page(zhdr, true);
574}
575
576static void release_z3fold_page_locked_list(struct kref *ref)
577{
578 struct z3fold_header *zhdr = container_of(ref, struct z3fold_header,
579 refcount);
Vitaly Wool9050cce2019-05-13 17:22:43 -0700580 struct z3fold_pool *pool = zhdr_to_pool(zhdr);
Vitaly Wool4a3ac932019-11-30 17:56:11 -0800581
Vitaly Wool9050cce2019-05-13 17:22:43 -0700582 spin_lock(&pool->lock);
Vitaly Woold30561c2017-09-06 16:24:47 -0700583 list_del_init(&zhdr->buddy);
Vitaly Wool9050cce2019-05-13 17:22:43 -0700584 spin_unlock(&pool->lock);
Vitaly Woold30561c2017-09-06 16:24:47 -0700585
586 WARN_ON(z3fold_page_trylock(zhdr));
587 __release_z3fold_page(zhdr, true);
588}
589
590static void free_pages_work(struct work_struct *w)
591{
592 struct z3fold_pool *pool = container_of(w, struct z3fold_pool, work);
593
594 spin_lock(&pool->stale_lock);
595 while (!list_empty(&pool->stale)) {
596 struct z3fold_header *zhdr = list_first_entry(&pool->stale,
597 struct z3fold_header, buddy);
598 struct page *page = virt_to_page(zhdr);
599
600 list_del(&zhdr->buddy);
601 if (WARN_ON(!test_bit(PAGE_STALE, &page->private)))
602 continue;
Vitaly Woold30561c2017-09-06 16:24:47 -0700603 spin_unlock(&pool->stale_lock);
604 cancel_work_sync(&zhdr->work);
Vitaly Wool1f862982019-05-13 17:22:52 -0700605 free_z3fold_page(page, false);
Vitaly Woold30561c2017-09-06 16:24:47 -0700606 cond_resched();
607 spin_lock(&pool->stale_lock);
608 }
609 spin_unlock(&pool->stale_lock);
610}
611
Vitaly Wool9a001fc2016-05-20 16:58:30 -0700612/*
613 * Returns the number of free chunks in a z3fold page.
614 * NB: can't be used with HEADLESS pages.
615 */
616static int num_free_chunks(struct z3fold_header *zhdr)
617{
618 int nfree;
619 /*
620 * If there is a middle object, pick up the bigger free space
621 * either before or after it. Otherwise just subtract the number
622 * of chunks occupied by the first and the last objects.
623 */
624 if (zhdr->middle_chunks != 0) {
625 int nfree_before = zhdr->first_chunks ?
Vitaly Woolede93212017-02-24 14:57:17 -0800626 0 : zhdr->start_middle - ZHDR_CHUNKS;
Vitaly Wool9a001fc2016-05-20 16:58:30 -0700627 int nfree_after = zhdr->last_chunks ?
Vitaly Woolede93212017-02-24 14:57:17 -0800628 0 : TOTAL_CHUNKS -
629 (zhdr->start_middle + zhdr->middle_chunks);
Vitaly Wool9a001fc2016-05-20 16:58:30 -0700630 nfree = max(nfree_before, nfree_after);
631 } else
632 nfree = NCHUNKS - zhdr->first_chunks - zhdr->last_chunks;
633 return nfree;
634}
635
Vitaly Wool9050cce2019-05-13 17:22:43 -0700636/* Add to the appropriate unbuddied list */
637static inline void add_to_unbuddied(struct z3fold_pool *pool,
638 struct z3fold_header *zhdr)
639{
640 if (zhdr->first_chunks == 0 || zhdr->last_chunks == 0 ||
641 zhdr->middle_chunks == 0) {
642 struct list_head *unbuddied = get_cpu_ptr(pool->unbuddied);
643
644 int freechunks = num_free_chunks(zhdr);
645 spin_lock(&pool->lock);
646 list_add(&zhdr->buddy, &unbuddied[freechunks]);
647 spin_unlock(&pool->lock);
648 zhdr->cpu = smp_processor_id();
649 put_cpu_ptr(pool->unbuddied);
650 }
651}
652
Vitaly Woolede93212017-02-24 14:57:17 -0800653static inline void *mchunk_memmove(struct z3fold_header *zhdr,
654 unsigned short dst_chunk)
655{
656 void *beg = zhdr;
657 return memmove(beg + (dst_chunk << CHUNK_SHIFT),
658 beg + (zhdr->start_middle << CHUNK_SHIFT),
659 zhdr->middle_chunks << CHUNK_SHIFT);
660}
661
Vitaly Wool4a3ac932019-11-30 17:56:11 -0800662static inline bool buddy_single(struct z3fold_header *zhdr)
663{
664 return !((zhdr->first_chunks && zhdr->middle_chunks) ||
665 (zhdr->first_chunks && zhdr->last_chunks) ||
666 (zhdr->middle_chunks && zhdr->last_chunks));
667}
668
669static struct z3fold_header *compact_single_buddy(struct z3fold_header *zhdr)
670{
671 struct z3fold_pool *pool = zhdr_to_pool(zhdr);
672 void *p = zhdr;
673 unsigned long old_handle = 0;
674 size_t sz = 0;
675 struct z3fold_header *new_zhdr = NULL;
676 int first_idx = __idx(zhdr, FIRST);
677 int middle_idx = __idx(zhdr, MIDDLE);
678 int last_idx = __idx(zhdr, LAST);
679 unsigned short *moved_chunks = NULL;
680
681 /*
682 * No need to protect slots here -- all the slots are "local" and
683 * the page lock is already taken
684 */
685 if (zhdr->first_chunks && zhdr->slots->slot[first_idx]) {
686 p += ZHDR_SIZE_ALIGNED;
687 sz = zhdr->first_chunks << CHUNK_SHIFT;
688 old_handle = (unsigned long)&zhdr->slots->slot[first_idx];
689 moved_chunks = &zhdr->first_chunks;
690 } else if (zhdr->middle_chunks && zhdr->slots->slot[middle_idx]) {
691 p += zhdr->start_middle << CHUNK_SHIFT;
692 sz = zhdr->middle_chunks << CHUNK_SHIFT;
693 old_handle = (unsigned long)&zhdr->slots->slot[middle_idx];
694 moved_chunks = &zhdr->middle_chunks;
695 } else if (zhdr->last_chunks && zhdr->slots->slot[last_idx]) {
696 p += PAGE_SIZE - (zhdr->last_chunks << CHUNK_SHIFT);
697 sz = zhdr->last_chunks << CHUNK_SHIFT;
698 old_handle = (unsigned long)&zhdr->slots->slot[last_idx];
699 moved_chunks = &zhdr->last_chunks;
700 }
701
702 if (sz > 0) {
703 enum buddy new_bud = HEADLESS;
704 short chunks = size_to_chunks(sz);
705 void *q;
706
707 new_zhdr = __z3fold_alloc(pool, sz, false);
708 if (!new_zhdr)
709 return NULL;
710
711 if (WARN_ON(new_zhdr == zhdr))
712 goto out_fail;
713
714 if (new_zhdr->first_chunks == 0) {
715 if (new_zhdr->middle_chunks != 0 &&
716 chunks >= new_zhdr->start_middle) {
717 new_bud = LAST;
718 } else {
719 new_bud = FIRST;
720 }
721 } else if (new_zhdr->last_chunks == 0) {
722 new_bud = LAST;
723 } else if (new_zhdr->middle_chunks == 0) {
724 new_bud = MIDDLE;
725 }
726 q = new_zhdr;
727 switch (new_bud) {
728 case FIRST:
729 new_zhdr->first_chunks = chunks;
730 q += ZHDR_SIZE_ALIGNED;
731 break;
732 case MIDDLE:
733 new_zhdr->middle_chunks = chunks;
734 new_zhdr->start_middle =
735 new_zhdr->first_chunks + ZHDR_CHUNKS;
736 q += new_zhdr->start_middle << CHUNK_SHIFT;
737 break;
738 case LAST:
739 new_zhdr->last_chunks = chunks;
740 q += PAGE_SIZE - (new_zhdr->last_chunks << CHUNK_SHIFT);
741 break;
742 default:
743 goto out_fail;
744 }
745 new_zhdr->foreign_handles++;
746 memcpy(q, p, sz);
747 write_lock(&zhdr->slots->lock);
748 *(unsigned long *)old_handle = (unsigned long)new_zhdr +
749 __idx(new_zhdr, new_bud);
750 if (new_bud == LAST)
751 *(unsigned long *)old_handle |=
752 (new_zhdr->last_chunks << BUDDY_SHIFT);
753 write_unlock(&zhdr->slots->lock);
754 add_to_unbuddied(pool, new_zhdr);
755 z3fold_page_unlock(new_zhdr);
756
757 *moved_chunks = 0;
758 }
759
760 return new_zhdr;
761
762out_fail:
763 if (new_zhdr) {
764 if (kref_put(&new_zhdr->refcount, release_z3fold_page_locked))
765 atomic64_dec(&pool->pages_nr);
766 else {
767 add_to_unbuddied(pool, new_zhdr);
768 z3fold_page_unlock(new_zhdr);
769 }
770 }
771 return NULL;
772
773}
774
Vitaly Wool1b096e52017-02-24 14:57:20 -0800775#define BIG_CHUNK_GAP 3
Vitaly Wool9a001fc2016-05-20 16:58:30 -0700776/* Has to be called with lock held */
777static int z3fold_compact_page(struct z3fold_header *zhdr)
778{
779 struct page *page = virt_to_page(zhdr);
Vitaly Wool9a001fc2016-05-20 16:58:30 -0700780
Vitaly Woolede93212017-02-24 14:57:17 -0800781 if (test_bit(MIDDLE_CHUNK_MAPPED, &page->private))
782 return 0; /* can't move middle chunk, it's used */
Vitaly Wool9a001fc2016-05-20 16:58:30 -0700783
Vitaly Wool1f862982019-05-13 17:22:52 -0700784 if (unlikely(PageIsolated(page)))
785 return 0;
786
Vitaly Woolede93212017-02-24 14:57:17 -0800787 if (zhdr->middle_chunks == 0)
788 return 0; /* nothing to compact */
789
790 if (zhdr->first_chunks == 0 && zhdr->last_chunks == 0) {
791 /* move to the beginning */
792 mchunk_memmove(zhdr, ZHDR_CHUNKS);
Vitaly Wool9a001fc2016-05-20 16:58:30 -0700793 zhdr->first_chunks = zhdr->middle_chunks;
794 zhdr->middle_chunks = 0;
795 zhdr->start_middle = 0;
796 zhdr->first_num++;
Vitaly Wool1b096e52017-02-24 14:57:20 -0800797 return 1;
Vitaly Wool9a001fc2016-05-20 16:58:30 -0700798 }
Vitaly Wool1b096e52017-02-24 14:57:20 -0800799
800 /*
801 * moving data is expensive, so let's only do that if
802 * there's substantial gain (at least BIG_CHUNK_GAP chunks)
803 */
804 if (zhdr->first_chunks != 0 && zhdr->last_chunks == 0 &&
805 zhdr->start_middle - (zhdr->first_chunks + ZHDR_CHUNKS) >=
806 BIG_CHUNK_GAP) {
807 mchunk_memmove(zhdr, zhdr->first_chunks + ZHDR_CHUNKS);
808 zhdr->start_middle = zhdr->first_chunks + ZHDR_CHUNKS;
809 return 1;
810 } else if (zhdr->last_chunks != 0 && zhdr->first_chunks == 0 &&
811 TOTAL_CHUNKS - (zhdr->last_chunks + zhdr->start_middle
812 + zhdr->middle_chunks) >=
813 BIG_CHUNK_GAP) {
814 unsigned short new_start = TOTAL_CHUNKS - zhdr->last_chunks -
815 zhdr->middle_chunks;
816 mchunk_memmove(zhdr, new_start);
817 zhdr->start_middle = new_start;
818 return 1;
819 }
820
821 return 0;
Vitaly Wool9a001fc2016-05-20 16:58:30 -0700822}
823
Vitaly Woold30561c2017-09-06 16:24:47 -0700824static void do_compact_page(struct z3fold_header *zhdr, bool locked)
825{
Vitaly Wool9050cce2019-05-13 17:22:43 -0700826 struct z3fold_pool *pool = zhdr_to_pool(zhdr);
Vitaly Woold30561c2017-09-06 16:24:47 -0700827 struct page *page;
Vitaly Woold30561c2017-09-06 16:24:47 -0700828
829 page = virt_to_page(zhdr);
830 if (locked)
831 WARN_ON(z3fold_page_trylock(zhdr));
832 else
833 z3fold_page_lock(zhdr);
Vitaly Wool5d03a662017-11-17 15:26:16 -0800834 if (WARN_ON(!test_and_clear_bit(NEEDS_COMPACTING, &page->private))) {
Vitaly Woold30561c2017-09-06 16:24:47 -0700835 z3fold_page_unlock(zhdr);
836 return;
837 }
838 spin_lock(&pool->lock);
839 list_del_init(&zhdr->buddy);
840 spin_unlock(&pool->lock);
841
Vitaly Wool5d03a662017-11-17 15:26:16 -0800842 if (kref_put(&zhdr->refcount, release_z3fold_page_locked)) {
843 atomic64_dec(&pool->pages_nr);
844 return;
845 }
846
Vitaly Wool1f862982019-05-13 17:22:52 -0700847 if (unlikely(PageIsolated(page) ||
Vitaly Wool3f9d2b52019-09-23 15:33:02 -0700848 test_bit(PAGE_CLAIMED, &page->private) ||
Vitaly Wool1f862982019-05-13 17:22:52 -0700849 test_bit(PAGE_STALE, &page->private))) {
850 z3fold_page_unlock(zhdr);
851 return;
852 }
853
Vitaly Wool4a3ac932019-11-30 17:56:11 -0800854 if (!zhdr->foreign_handles && buddy_single(zhdr) &&
855 zhdr->mapped_count == 0 && compact_single_buddy(zhdr)) {
856 if (kref_put(&zhdr->refcount, release_z3fold_page_locked))
857 atomic64_dec(&pool->pages_nr);
858 else
859 z3fold_page_unlock(zhdr);
860 return;
861 }
862
Vitaly Woold30561c2017-09-06 16:24:47 -0700863 z3fold_compact_page(zhdr);
Vitaly Wool9050cce2019-05-13 17:22:43 -0700864 add_to_unbuddied(pool, zhdr);
Vitaly Woold30561c2017-09-06 16:24:47 -0700865 z3fold_page_unlock(zhdr);
866}
867
868static void compact_page_work(struct work_struct *w)
869{
870 struct z3fold_header *zhdr = container_of(w, struct z3fold_header,
871 work);
872
873 do_compact_page(zhdr, false);
874}
875
Vitaly Wool9050cce2019-05-13 17:22:43 -0700876/* returns _locked_ z3fold page header or NULL */
877static inline struct z3fold_header *__z3fold_alloc(struct z3fold_pool *pool,
878 size_t size, bool can_sleep)
879{
880 struct z3fold_header *zhdr = NULL;
881 struct page *page;
882 struct list_head *unbuddied;
883 int chunks = size_to_chunks(size), i;
884
885lookup:
886 /* First, try to find an unbuddied z3fold page. */
887 unbuddied = get_cpu_ptr(pool->unbuddied);
888 for_each_unbuddied_list(i, chunks) {
889 struct list_head *l = &unbuddied[i];
890
891 zhdr = list_first_entry_or_null(READ_ONCE(l),
892 struct z3fold_header, buddy);
893
894 if (!zhdr)
895 continue;
896
897 /* Re-check under lock. */
898 spin_lock(&pool->lock);
899 l = &unbuddied[i];
900 if (unlikely(zhdr != list_first_entry(READ_ONCE(l),
901 struct z3fold_header, buddy)) ||
902 !z3fold_page_trylock(zhdr)) {
903 spin_unlock(&pool->lock);
904 zhdr = NULL;
905 put_cpu_ptr(pool->unbuddied);
906 if (can_sleep)
907 cond_resched();
908 goto lookup;
909 }
910 list_del_init(&zhdr->buddy);
911 zhdr->cpu = -1;
912 spin_unlock(&pool->lock);
913
914 page = virt_to_page(zhdr);
Vitaly Wool4a3ac932019-11-30 17:56:11 -0800915 if (test_bit(NEEDS_COMPACTING, &page->private) ||
916 test_bit(PAGE_CLAIMED, &page->private)) {
Vitaly Wool9050cce2019-05-13 17:22:43 -0700917 z3fold_page_unlock(zhdr);
918 zhdr = NULL;
919 put_cpu_ptr(pool->unbuddied);
920 if (can_sleep)
921 cond_resched();
922 goto lookup;
923 }
924
925 /*
926 * this page could not be removed from its unbuddied
927 * list while pool lock was held, and then we've taken
928 * page lock so kref_put could not be called before
929 * we got here, so it's safe to just call kref_get()
930 */
931 kref_get(&zhdr->refcount);
932 break;
933 }
934 put_cpu_ptr(pool->unbuddied);
935
Vitaly Wool351618b2019-05-13 17:22:46 -0700936 if (!zhdr) {
937 int cpu;
938
939 /* look for _exact_ match on other cpus' lists */
940 for_each_online_cpu(cpu) {
941 struct list_head *l;
942
943 unbuddied = per_cpu_ptr(pool->unbuddied, cpu);
944 spin_lock(&pool->lock);
945 l = &unbuddied[chunks];
946
947 zhdr = list_first_entry_or_null(READ_ONCE(l),
948 struct z3fold_header, buddy);
949
950 if (!zhdr || !z3fold_page_trylock(zhdr)) {
951 spin_unlock(&pool->lock);
952 zhdr = NULL;
953 continue;
954 }
955 list_del_init(&zhdr->buddy);
956 zhdr->cpu = -1;
957 spin_unlock(&pool->lock);
958
959 page = virt_to_page(zhdr);
Vitaly Wool4a3ac932019-11-30 17:56:11 -0800960 if (test_bit(NEEDS_COMPACTING, &page->private) ||
961 test_bit(PAGE_CLAIMED, &page->private)) {
Vitaly Wool351618b2019-05-13 17:22:46 -0700962 z3fold_page_unlock(zhdr);
963 zhdr = NULL;
964 if (can_sleep)
965 cond_resched();
966 continue;
967 }
968 kref_get(&zhdr->refcount);
969 break;
970 }
971 }
972
Vitaly Wool9050cce2019-05-13 17:22:43 -0700973 return zhdr;
974}
Vitaly Woold30561c2017-09-06 16:24:47 -0700975
976/*
977 * API Functions
978 */
979
980/**
981 * z3fold_create_pool() - create a new z3fold pool
982 * @name: pool name
983 * @gfp: gfp flags when allocating the z3fold pool structure
984 * @ops: user-defined operations for the z3fold pool
985 *
986 * Return: pointer to the new z3fold pool or NULL if the metadata allocation
987 * failed.
988 */
989static struct z3fold_pool *z3fold_create_pool(const char *name, gfp_t gfp,
990 const struct z3fold_ops *ops)
991{
992 struct z3fold_pool *pool = NULL;
993 int i, cpu;
994
995 pool = kzalloc(sizeof(struct z3fold_pool), gfp);
996 if (!pool)
997 goto out;
Vitaly Wool7c2b8ba2019-05-13 17:22:49 -0700998 pool->c_handle = kmem_cache_create("z3fold_handle",
999 sizeof(struct z3fold_buddy_slots),
1000 SLOTS_ALIGN, 0, NULL);
1001 if (!pool->c_handle)
1002 goto out_c;
Vitaly Woold30561c2017-09-06 16:24:47 -07001003 spin_lock_init(&pool->lock);
1004 spin_lock_init(&pool->stale_lock);
1005 pool->unbuddied = __alloc_percpu(sizeof(struct list_head)*NCHUNKS, 2);
Xidong Wang1ec69952018-04-10 16:29:34 -07001006 if (!pool->unbuddied)
1007 goto out_pool;
Vitaly Woold30561c2017-09-06 16:24:47 -07001008 for_each_possible_cpu(cpu) {
1009 struct list_head *unbuddied =
1010 per_cpu_ptr(pool->unbuddied, cpu);
1011 for_each_unbuddied_list(i, 0)
1012 INIT_LIST_HEAD(&unbuddied[i]);
1013 }
1014 INIT_LIST_HEAD(&pool->lru);
1015 INIT_LIST_HEAD(&pool->stale);
1016 atomic64_set(&pool->pages_nr, 0);
1017 pool->name = name;
1018 pool->compact_wq = create_singlethread_workqueue(pool->name);
1019 if (!pool->compact_wq)
Xidong Wang1ec69952018-04-10 16:29:34 -07001020 goto out_unbuddied;
Vitaly Woold30561c2017-09-06 16:24:47 -07001021 pool->release_wq = create_singlethread_workqueue(pool->name);
1022 if (!pool->release_wq)
1023 goto out_wq;
Vitaly Wool1f862982019-05-13 17:22:52 -07001024 if (z3fold_register_migration(pool))
1025 goto out_rwq;
Vitaly Woold30561c2017-09-06 16:24:47 -07001026 INIT_WORK(&pool->work, free_pages_work);
1027 pool->ops = ops;
1028 return pool;
1029
Vitaly Wool1f862982019-05-13 17:22:52 -07001030out_rwq:
1031 destroy_workqueue(pool->release_wq);
Vitaly Woold30561c2017-09-06 16:24:47 -07001032out_wq:
1033 destroy_workqueue(pool->compact_wq);
Xidong Wang1ec69952018-04-10 16:29:34 -07001034out_unbuddied:
1035 free_percpu(pool->unbuddied);
1036out_pool:
Vitaly Wool7c2b8ba2019-05-13 17:22:49 -07001037 kmem_cache_destroy(pool->c_handle);
1038out_c:
Vitaly Woold30561c2017-09-06 16:24:47 -07001039 kfree(pool);
Xidong Wang1ec69952018-04-10 16:29:34 -07001040out:
Vitaly Woold30561c2017-09-06 16:24:47 -07001041 return NULL;
1042}
1043
1044/**
1045 * z3fold_destroy_pool() - destroys an existing z3fold pool
1046 * @pool: the z3fold pool to be destroyed
1047 *
1048 * The pool should be emptied before this function is called.
1049 */
1050static void z3fold_destroy_pool(struct z3fold_pool *pool)
1051{
Vitaly Wool7c2b8ba2019-05-13 17:22:49 -07001052 kmem_cache_destroy(pool->c_handle);
Henry Burns6051d3b2019-08-13 15:37:21 -07001053
1054 /*
1055 * We need to destroy pool->compact_wq before pool->release_wq,
1056 * as any pending work on pool->compact_wq will call
1057 * queue_work(pool->release_wq, &pool->work).
Henry Burnsb9970522019-08-13 15:37:25 -07001058 *
1059 * There are still outstanding pages until both workqueues are drained,
1060 * so we cannot unregister migration until then.
Henry Burns6051d3b2019-08-13 15:37:21 -07001061 */
1062
Vitaly Woold30561c2017-09-06 16:24:47 -07001063 destroy_workqueue(pool->compact_wq);
Henry Burns6051d3b2019-08-13 15:37:21 -07001064 destroy_workqueue(pool->release_wq);
Henry Burnsb9970522019-08-13 15:37:25 -07001065 z3fold_unregister_migration(pool);
Vitaly Woold30561c2017-09-06 16:24:47 -07001066 kfree(pool);
1067}
1068
Vitaly Wool9a001fc2016-05-20 16:58:30 -07001069/**
1070 * z3fold_alloc() - allocates a region of a given size
1071 * @pool: z3fold pool from which to allocate
1072 * @size: size in bytes of the desired allocation
1073 * @gfp: gfp flags used if the pool needs to grow
1074 * @handle: handle of the new allocation
1075 *
1076 * This function will attempt to find a free region in the pool large enough to
1077 * satisfy the allocation request. A search of the unbuddied lists is
1078 * performed first. If no suitable free region is found, then a new page is
1079 * allocated and added to the pool to satisfy the request.
1080 *
1081 * gfp should not set __GFP_HIGHMEM as highmem pages cannot be used
1082 * as z3fold pool pages.
1083 *
1084 * Return: 0 if success and handle is set, otherwise -EINVAL if the size or
1085 * gfp arguments are invalid or -ENOMEM if the pool was unable to allocate
1086 * a new page.
1087 */
1088static int z3fold_alloc(struct z3fold_pool *pool, size_t size, gfp_t gfp,
1089 unsigned long *handle)
1090{
Vitaly Wool9050cce2019-05-13 17:22:43 -07001091 int chunks = size_to_chunks(size);
Vitaly Wool9a001fc2016-05-20 16:58:30 -07001092 struct z3fold_header *zhdr = NULL;
Vitaly Woold30561c2017-09-06 16:24:47 -07001093 struct page *page = NULL;
Vitaly Wool9a001fc2016-05-20 16:58:30 -07001094 enum buddy bud;
Matthew Wilcox8a97ea542018-04-10 16:29:37 -07001095 bool can_sleep = gfpflags_allow_blocking(gfp);
Vitaly Wool9a001fc2016-05-20 16:58:30 -07001096
Henry Burnsf1549cb2019-07-16 16:26:03 -07001097 if (!size)
Vitaly Wool9a001fc2016-05-20 16:58:30 -07001098 return -EINVAL;
1099
1100 if (size > PAGE_SIZE)
1101 return -ENOSPC;
1102
1103 if (size > PAGE_SIZE - ZHDR_SIZE_ALIGNED - CHUNK_SIZE)
1104 bud = HEADLESS;
1105 else {
Vitaly Wool9050cce2019-05-13 17:22:43 -07001106retry:
1107 zhdr = __z3fold_alloc(pool, size, can_sleep);
Vitaly Woold30561c2017-09-06 16:24:47 -07001108 if (zhdr) {
Vitaly Wool2f1e5e42017-02-24 14:57:23 -08001109 if (zhdr->first_chunks == 0) {
1110 if (zhdr->middle_chunks != 0 &&
1111 chunks >= zhdr->start_middle)
1112 bud = LAST;
1113 else
1114 bud = FIRST;
1115 } else if (zhdr->last_chunks == 0)
1116 bud = LAST;
1117 else if (zhdr->middle_chunks == 0)
1118 bud = MIDDLE;
1119 else {
Vitaly Wool5a27aa82017-02-24 14:57:26 -08001120 if (kref_put(&zhdr->refcount,
Vitaly Woold30561c2017-09-06 16:24:47 -07001121 release_z3fold_page_locked))
Vitaly Wool5a27aa82017-02-24 14:57:26 -08001122 atomic64_dec(&pool->pages_nr);
Vitaly Woold30561c2017-09-06 16:24:47 -07001123 else
1124 z3fold_page_unlock(zhdr);
Vitaly Wool2f1e5e42017-02-24 14:57:23 -08001125 pr_err("No free chunks in unbuddied\n");
1126 WARN_ON(1);
Vitaly Wool9050cce2019-05-13 17:22:43 -07001127 goto retry;
Vitaly Wool2f1e5e42017-02-24 14:57:23 -08001128 }
Vitaly Wool9050cce2019-05-13 17:22:43 -07001129 page = virt_to_page(zhdr);
Vitaly Wool2f1e5e42017-02-24 14:57:23 -08001130 goto found;
Vitaly Wool9a001fc2016-05-20 16:58:30 -07001131 }
1132 bud = FIRST;
Vitaly Wool9a001fc2016-05-20 16:58:30 -07001133 }
1134
Vitaly Wool5c9bab52018-04-05 16:23:32 -07001135 page = NULL;
1136 if (can_sleep) {
1137 spin_lock(&pool->stale_lock);
1138 zhdr = list_first_entry_or_null(&pool->stale,
1139 struct z3fold_header, buddy);
1140 /*
1141 * Before allocating a page, let's see if we can take one from
1142 * the stale pages list. cancel_work_sync() can sleep so we
1143 * limit this case to the contexts where we can sleep
1144 */
1145 if (zhdr) {
1146 list_del(&zhdr->buddy);
1147 spin_unlock(&pool->stale_lock);
Vitaly Woold30561c2017-09-06 16:24:47 -07001148 cancel_work_sync(&zhdr->work);
Vitaly Wool5c9bab52018-04-05 16:23:32 -07001149 page = virt_to_page(zhdr);
1150 } else {
1151 spin_unlock(&pool->stale_lock);
1152 }
Vitaly Woold30561c2017-09-06 16:24:47 -07001153 }
Vitaly Wool5c9bab52018-04-05 16:23:32 -07001154 if (!page)
1155 page = alloc_page(gfp);
Vitaly Woold30561c2017-09-06 16:24:47 -07001156
Vitaly Wool9a001fc2016-05-20 16:58:30 -07001157 if (!page)
1158 return -ENOMEM;
Vitaly Wool2f1e5e42017-02-24 14:57:23 -08001159
Vitaly Wool63398412019-09-23 15:36:51 -07001160 zhdr = init_z3fold_page(page, bud == HEADLESS, pool, gfp);
Vitaly Wool9050cce2019-05-13 17:22:43 -07001161 if (!zhdr) {
1162 __free_page(page);
1163 return -ENOMEM;
1164 }
1165 atomic64_inc(&pool->pages_nr);
Vitaly Wool9a001fc2016-05-20 16:58:30 -07001166
1167 if (bud == HEADLESS) {
1168 set_bit(PAGE_HEADLESS, &page->private);
1169 goto headless;
1170 }
Henry Burns810481a2019-07-11 20:52:14 -07001171 if (can_sleep) {
1172 lock_page(page);
1173 __SetPageMovable(page, pool->inode->i_mapping);
1174 unlock_page(page);
1175 } else {
1176 if (trylock_page(page)) {
1177 __SetPageMovable(page, pool->inode->i_mapping);
1178 unlock_page(page);
1179 }
1180 }
Vitaly Wool2f1e5e42017-02-24 14:57:23 -08001181 z3fold_page_lock(zhdr);
Vitaly Wool9a001fc2016-05-20 16:58:30 -07001182
1183found:
1184 if (bud == FIRST)
1185 zhdr->first_chunks = chunks;
1186 else if (bud == LAST)
1187 zhdr->last_chunks = chunks;
1188 else {
1189 zhdr->middle_chunks = chunks;
Vitaly Woolede93212017-02-24 14:57:17 -08001190 zhdr->start_middle = zhdr->first_chunks + ZHDR_CHUNKS;
Vitaly Wool9a001fc2016-05-20 16:58:30 -07001191 }
Vitaly Wool9050cce2019-05-13 17:22:43 -07001192 add_to_unbuddied(pool, zhdr);
Vitaly Wool9a001fc2016-05-20 16:58:30 -07001193
1194headless:
Vitaly Woold30561c2017-09-06 16:24:47 -07001195 spin_lock(&pool->lock);
Vitaly Wool9a001fc2016-05-20 16:58:30 -07001196 /* Add/move z3fold page to beginning of LRU */
1197 if (!list_empty(&page->lru))
1198 list_del(&page->lru);
1199
1200 list_add(&page->lru, &pool->lru);
1201
1202 *handle = encode_handle(zhdr, bud);
1203 spin_unlock(&pool->lock);
Vitaly Wool2f1e5e42017-02-24 14:57:23 -08001204 if (bud != HEADLESS)
1205 z3fold_page_unlock(zhdr);
Vitaly Wool9a001fc2016-05-20 16:58:30 -07001206
1207 return 0;
1208}
1209
1210/**
1211 * z3fold_free() - frees the allocation associated with the given handle
1212 * @pool: pool in which the allocation resided
1213 * @handle: handle associated with the allocation returned by z3fold_alloc()
1214 *
1215 * In the case that the z3fold page in which the allocation resides is under
1216 * reclaim, as indicated by the PG_reclaim flag being set, this function
1217 * only sets the first|last_chunks to 0. The page is actually freed
1218 * once both buddies are evicted (see z3fold_reclaim_page() below).
1219 */
1220static void z3fold_free(struct z3fold_pool *pool, unsigned long handle)
1221{
1222 struct z3fold_header *zhdr;
Vitaly Wool9a001fc2016-05-20 16:58:30 -07001223 struct page *page;
1224 enum buddy bud;
Vitaly Wool5b6807d2019-10-06 17:58:22 -07001225 bool page_claimed;
Vitaly Wool9a001fc2016-05-20 16:58:30 -07001226
Vitaly Wool4a3ac932019-11-30 17:56:11 -08001227 zhdr = get_z3fold_header(handle);
Vitaly Wool9a001fc2016-05-20 16:58:30 -07001228 page = virt_to_page(zhdr);
Vitaly Wool5b6807d2019-10-06 17:58:22 -07001229 page_claimed = test_and_set_bit(PAGE_CLAIMED, &page->private);
Vitaly Wool9a001fc2016-05-20 16:58:30 -07001230
1231 if (test_bit(PAGE_HEADLESS, &page->private)) {
Vitaly Woolca0246b2018-11-16 15:07:56 -08001232 /* if a headless page is under reclaim, just leave.
1233 * NB: we use test_and_set_bit for a reason: if the bit
1234 * has not been set before, we release this page
1235 * immediately so we don't care about its value any more.
1236 */
Vitaly Wool5b6807d2019-10-06 17:58:22 -07001237 if (!page_claimed) {
Vitaly Woolca0246b2018-11-16 15:07:56 -08001238 spin_lock(&pool->lock);
1239 list_del(&page->lru);
1240 spin_unlock(&pool->lock);
Vitaly Wool4a3ac932019-11-30 17:56:11 -08001241 put_z3fold_header(zhdr);
Vitaly Wool1f862982019-05-13 17:22:52 -07001242 free_z3fold_page(page, true);
Vitaly Woolca0246b2018-11-16 15:07:56 -08001243 atomic64_dec(&pool->pages_nr);
Vitaly Wool9a001fc2016-05-20 16:58:30 -07001244 }
Vitaly Woolca0246b2018-11-16 15:07:56 -08001245 return;
Vitaly Wool9a001fc2016-05-20 16:58:30 -07001246 }
1247
Vitaly Woolca0246b2018-11-16 15:07:56 -08001248 /* Non-headless case */
Vitaly Woolca0246b2018-11-16 15:07:56 -08001249 bud = handle_to_buddy(handle);
1250
1251 switch (bud) {
1252 case FIRST:
1253 zhdr->first_chunks = 0;
1254 break;
1255 case MIDDLE:
1256 zhdr->middle_chunks = 0;
1257 break;
1258 case LAST:
1259 zhdr->last_chunks = 0;
1260 break;
1261 default:
1262 pr_err("%s: unknown bud %d\n", __func__, bud);
1263 WARN_ON(1);
Vitaly Wool4a3ac932019-11-30 17:56:11 -08001264 put_z3fold_header(zhdr);
1265 clear_bit(PAGE_CLAIMED, &page->private);
Vitaly Woold30561c2017-09-06 16:24:47 -07001266 return;
Vitaly Wool9a001fc2016-05-20 16:58:30 -07001267 }
1268
Vitaly Wool4a3ac932019-11-30 17:56:11 -08001269 if (!page_claimed)
1270 free_handle(handle);
Vitaly Woold30561c2017-09-06 16:24:47 -07001271 if (kref_put(&zhdr->refcount, release_z3fold_page_locked_list)) {
1272 atomic64_dec(&pool->pages_nr);
1273 return;
1274 }
Vitaly Wool5b6807d2019-10-06 17:58:22 -07001275 if (page_claimed) {
1276 /* the page has not been claimed by us */
Vitaly Wool6098d7e2018-05-11 16:01:46 -07001277 z3fold_page_unlock(zhdr);
1278 return;
1279 }
Vitaly Wool1f862982019-05-13 17:22:52 -07001280 if (unlikely(PageIsolated(page)) ||
1281 test_and_set_bit(NEEDS_COMPACTING, &page->private)) {
Vitaly Wool4a3ac932019-11-30 17:56:11 -08001282 put_z3fold_header(zhdr);
Vitaly Wool5b6807d2019-10-06 17:58:22 -07001283 clear_bit(PAGE_CLAIMED, &page->private);
Vitaly Woold30561c2017-09-06 16:24:47 -07001284 return;
1285 }
1286 if (zhdr->cpu < 0 || !cpu_online(zhdr->cpu)) {
1287 spin_lock(&pool->lock);
1288 list_del_init(&zhdr->buddy);
1289 spin_unlock(&pool->lock);
1290 zhdr->cpu = -1;
Vitaly Wool5d03a662017-11-17 15:26:16 -08001291 kref_get(&zhdr->refcount);
Vitaly Wool5b6807d2019-10-06 17:58:22 -07001292 clear_bit(PAGE_CLAIMED, &page->private);
Vitaly Wool4a3ac932019-11-30 17:56:11 -08001293 do_compact_page(zhdr, true);
Vitaly Woold30561c2017-09-06 16:24:47 -07001294 return;
1295 }
Vitaly Wool5d03a662017-11-17 15:26:16 -08001296 kref_get(&zhdr->refcount);
Vitaly Wool5b6807d2019-10-06 17:58:22 -07001297 clear_bit(PAGE_CLAIMED, &page->private);
Vitaly Wool4a3ac932019-11-30 17:56:11 -08001298 queue_work_on(zhdr->cpu, pool->compact_wq, &zhdr->work);
1299 put_z3fold_header(zhdr);
Vitaly Wool9a001fc2016-05-20 16:58:30 -07001300}
1301
1302/**
1303 * z3fold_reclaim_page() - evicts allocations from a pool page and frees it
1304 * @pool: pool from which a page will attempt to be evicted
Mike Rapoportf144c392018-02-06 15:42:16 -08001305 * @retries: number of pages on the LRU list for which eviction will
Vitaly Wool9a001fc2016-05-20 16:58:30 -07001306 * be attempted before failing
1307 *
1308 * z3fold reclaim is different from normal system reclaim in that it is done
1309 * from the bottom, up. This is because only the bottom layer, z3fold, has
1310 * information on how the allocations are organized within each z3fold page.
1311 * This has the potential to create interesting locking situations between
1312 * z3fold and the user, however.
1313 *
1314 * To avoid these, this is how z3fold_reclaim_page() should be called:
Mike Rapoportf144c392018-02-06 15:42:16 -08001315 *
Vitaly Wool9a001fc2016-05-20 16:58:30 -07001316 * The user detects a page should be reclaimed and calls z3fold_reclaim_page().
1317 * z3fold_reclaim_page() will remove a z3fold page from the pool LRU list and
1318 * call the user-defined eviction handler with the pool and handle as
1319 * arguments.
1320 *
1321 * If the handle can not be evicted, the eviction handler should return
1322 * non-zero. z3fold_reclaim_page() will add the z3fold page back to the
1323 * appropriate list and try the next z3fold page on the LRU up to
1324 * a user defined number of retries.
1325 *
1326 * If the handle is successfully evicted, the eviction handler should
1327 * return 0 _and_ should have called z3fold_free() on the handle. z3fold_free()
1328 * contains logic to delay freeing the page if the page is under reclaim,
1329 * as indicated by the setting of the PG_reclaim flag on the underlying page.
1330 *
1331 * If all buddies in the z3fold page are successfully evicted, then the
1332 * z3fold page can be freed.
1333 *
1334 * Returns: 0 if page is successfully freed, otherwise -EINVAL if there are
1335 * no pages to evict or an eviction handler is not registered, -EAGAIN if
1336 * the retry limit was hit.
1337 */
1338static int z3fold_reclaim_page(struct z3fold_pool *pool, unsigned int retries)
1339{
Vitaly Wool4a3ac932019-11-30 17:56:11 -08001340 int i, ret = -1;
Vitaly Woold30561c2017-09-06 16:24:47 -07001341 struct z3fold_header *zhdr = NULL;
1342 struct page *page = NULL;
1343 struct list_head *pos;
Vitaly Wool9a001fc2016-05-20 16:58:30 -07001344 unsigned long first_handle = 0, middle_handle = 0, last_handle = 0;
1345
1346 spin_lock(&pool->lock);
Vitaly Wool2f1e5e42017-02-24 14:57:23 -08001347 if (!pool->ops || !pool->ops->evict || retries == 0) {
Vitaly Wool9a001fc2016-05-20 16:58:30 -07001348 spin_unlock(&pool->lock);
1349 return -EINVAL;
1350 }
1351 for (i = 0; i < retries; i++) {
Vitaly Wool2f1e5e42017-02-24 14:57:23 -08001352 if (list_empty(&pool->lru)) {
1353 spin_unlock(&pool->lock);
1354 return -EINVAL;
1355 }
Vitaly Woold30561c2017-09-06 16:24:47 -07001356 list_for_each_prev(pos, &pool->lru) {
1357 page = list_entry(pos, struct page, lru);
Vitaly Woolca0246b2018-11-16 15:07:56 -08001358
1359 /* this bit could have been set by free, in which case
1360 * we pass over to the next page in the pool.
1361 */
Vitaly Wool3f9d2b52019-09-23 15:33:02 -07001362 if (test_and_set_bit(PAGE_CLAIMED, &page->private)) {
1363 page = NULL;
Vitaly Woolca0246b2018-11-16 15:07:56 -08001364 continue;
Vitaly Wool3f9d2b52019-09-23 15:33:02 -07001365 }
Vitaly Wool9a001fc2016-05-20 16:58:30 -07001366
Vitaly Wool3f9d2b52019-09-23 15:33:02 -07001367 if (unlikely(PageIsolated(page))) {
1368 clear_bit(PAGE_CLAIMED, &page->private);
1369 page = NULL;
Vitaly Wool1f862982019-05-13 17:22:52 -07001370 continue;
Vitaly Wool3f9d2b52019-09-23 15:33:02 -07001371 }
1372 zhdr = page_address(page);
Vitaly Woolca0246b2018-11-16 15:07:56 -08001373 if (test_bit(PAGE_HEADLESS, &page->private))
1374 break;
1375
1376 if (!z3fold_page_trylock(zhdr)) {
Vitaly Wool3f9d2b52019-09-23 15:33:02 -07001377 clear_bit(PAGE_CLAIMED, &page->private);
Vitaly Woolca0246b2018-11-16 15:07:56 -08001378 zhdr = NULL;
Vitaly Woold30561c2017-09-06 16:24:47 -07001379 continue; /* can't evict at this point */
Vitaly Woolca0246b2018-11-16 15:07:56 -08001380 }
Vitaly Wool4a3ac932019-11-30 17:56:11 -08001381 if (zhdr->foreign_handles) {
1382 clear_bit(PAGE_CLAIMED, &page->private);
1383 z3fold_page_unlock(zhdr);
1384 zhdr = NULL;
1385 continue; /* can't evict such page */
1386 }
Vitaly Wool5a27aa82017-02-24 14:57:26 -08001387 kref_get(&zhdr->refcount);
Vitaly Woold30561c2017-09-06 16:24:47 -07001388 list_del_init(&zhdr->buddy);
1389 zhdr->cpu = -1;
Vitaly Wool6098d7e2018-05-11 16:01:46 -07001390 break;
Vitaly Woold30561c2017-09-06 16:24:47 -07001391 }
1392
Vitaly Woolca0246b2018-11-16 15:07:56 -08001393 if (!zhdr)
1394 break;
1395
Vitaly Woold30561c2017-09-06 16:24:47 -07001396 list_del_init(&page->lru);
1397 spin_unlock(&pool->lock);
1398
1399 if (!test_bit(PAGE_HEADLESS, &page->private)) {
Vitaly Wool9a001fc2016-05-20 16:58:30 -07001400 /*
Vitaly Wool3f9d2b52019-09-23 15:33:02 -07001401 * We need encode the handles before unlocking, and
1402 * use our local slots structure because z3fold_free
1403 * can zero out zhdr->slots and we can't do much
1404 * about that
Vitaly Wool9a001fc2016-05-20 16:58:30 -07001405 */
1406 first_handle = 0;
1407 last_handle = 0;
1408 middle_handle = 0;
1409 if (zhdr->first_chunks)
Vitaly Wool4a3ac932019-11-30 17:56:11 -08001410 first_handle = encode_handle(zhdr, FIRST);
Vitaly Wool9a001fc2016-05-20 16:58:30 -07001411 if (zhdr->middle_chunks)
Vitaly Wool4a3ac932019-11-30 17:56:11 -08001412 middle_handle = encode_handle(zhdr, MIDDLE);
Vitaly Wool9a001fc2016-05-20 16:58:30 -07001413 if (zhdr->last_chunks)
Vitaly Wool4a3ac932019-11-30 17:56:11 -08001414 last_handle = encode_handle(zhdr, LAST);
Vitaly Woold30561c2017-09-06 16:24:47 -07001415 /*
1416 * it's safe to unlock here because we hold a
1417 * reference to this page
1418 */
Vitaly Wool2f1e5e42017-02-24 14:57:23 -08001419 z3fold_page_unlock(zhdr);
Vitaly Wool9a001fc2016-05-20 16:58:30 -07001420 } else {
Vitaly Wool4a3ac932019-11-30 17:56:11 -08001421 first_handle = encode_handle(zhdr, HEADLESS);
Vitaly Wool9a001fc2016-05-20 16:58:30 -07001422 last_handle = middle_handle = 0;
1423 }
Vitaly Wool9a001fc2016-05-20 16:58:30 -07001424 /* Issue the eviction callback(s) */
1425 if (middle_handle) {
1426 ret = pool->ops->evict(pool, middle_handle);
1427 if (ret)
1428 goto next;
Vitaly Wool4a3ac932019-11-30 17:56:11 -08001429 free_handle(middle_handle);
Vitaly Wool9a001fc2016-05-20 16:58:30 -07001430 }
1431 if (first_handle) {
1432 ret = pool->ops->evict(pool, first_handle);
1433 if (ret)
1434 goto next;
Vitaly Wool4a3ac932019-11-30 17:56:11 -08001435 free_handle(first_handle);
Vitaly Wool9a001fc2016-05-20 16:58:30 -07001436 }
1437 if (last_handle) {
1438 ret = pool->ops->evict(pool, last_handle);
1439 if (ret)
1440 goto next;
Vitaly Wool4a3ac932019-11-30 17:56:11 -08001441 free_handle(last_handle);
Vitaly Wool9a001fc2016-05-20 16:58:30 -07001442 }
1443next:
Vitaly Wool5a27aa82017-02-24 14:57:26 -08001444 if (test_bit(PAGE_HEADLESS, &page->private)) {
1445 if (ret == 0) {
Vitaly Wool1f862982019-05-13 17:22:52 -07001446 free_z3fold_page(page, true);
Vitaly Woolca0246b2018-11-16 15:07:56 -08001447 atomic64_dec(&pool->pages_nr);
Vitaly Wool5a27aa82017-02-24 14:57:26 -08001448 return 0;
Vitaly Wool5a27aa82017-02-24 14:57:26 -08001449 }
Vitaly Wool6098d7e2018-05-11 16:01:46 -07001450 spin_lock(&pool->lock);
1451 list_add(&page->lru, &pool->lru);
Vitaly Woold5567c92017-10-03 16:14:47 -07001452 spin_unlock(&pool->lock);
Vitaly Wool3f9d2b52019-09-23 15:33:02 -07001453 clear_bit(PAGE_CLAIMED, &page->private);
Vitaly Wool6098d7e2018-05-11 16:01:46 -07001454 } else {
1455 z3fold_page_lock(zhdr);
Vitaly Wool6098d7e2018-05-11 16:01:46 -07001456 if (kref_put(&zhdr->refcount,
1457 release_z3fold_page_locked)) {
1458 atomic64_dec(&pool->pages_nr);
1459 return 0;
1460 }
1461 /*
1462 * if we are here, the page is still not completely
1463 * free. Take the global pool lock then to be able
1464 * to add it back to the lru list
1465 */
1466 spin_lock(&pool->lock);
1467 list_add(&page->lru, &pool->lru);
1468 spin_unlock(&pool->lock);
1469 z3fold_page_unlock(zhdr);
Vitaly Wool3f9d2b52019-09-23 15:33:02 -07001470 clear_bit(PAGE_CLAIMED, &page->private);
Vitaly Wool9a001fc2016-05-20 16:58:30 -07001471 }
1472
Vitaly Wool6098d7e2018-05-11 16:01:46 -07001473 /* We started off locked to we need to lock the pool back */
1474 spin_lock(&pool->lock);
Vitaly Wool9a001fc2016-05-20 16:58:30 -07001475 }
1476 spin_unlock(&pool->lock);
1477 return -EAGAIN;
1478}
1479
1480/**
1481 * z3fold_map() - maps the allocation associated with the given handle
1482 * @pool: pool in which the allocation resides
1483 * @handle: handle associated with the allocation to be mapped
1484 *
1485 * Extracts the buddy number from handle and constructs the pointer to the
1486 * correct starting chunk within the page.
1487 *
1488 * Returns: a pointer to the mapped allocation
1489 */
1490static void *z3fold_map(struct z3fold_pool *pool, unsigned long handle)
1491{
1492 struct z3fold_header *zhdr;
1493 struct page *page;
1494 void *addr;
1495 enum buddy buddy;
1496
Vitaly Wool4a3ac932019-11-30 17:56:11 -08001497 zhdr = get_z3fold_header(handle);
Vitaly Wool9a001fc2016-05-20 16:58:30 -07001498 addr = zhdr;
1499 page = virt_to_page(zhdr);
1500
1501 if (test_bit(PAGE_HEADLESS, &page->private))
1502 goto out;
1503
1504 buddy = handle_to_buddy(handle);
1505 switch (buddy) {
1506 case FIRST:
1507 addr += ZHDR_SIZE_ALIGNED;
1508 break;
1509 case MIDDLE:
1510 addr += zhdr->start_middle << CHUNK_SHIFT;
1511 set_bit(MIDDLE_CHUNK_MAPPED, &page->private);
1512 break;
1513 case LAST:
Vitaly Woolca0246b2018-11-16 15:07:56 -08001514 addr += PAGE_SIZE - (handle_to_chunks(handle) << CHUNK_SHIFT);
Vitaly Wool9a001fc2016-05-20 16:58:30 -07001515 break;
1516 default:
1517 pr_err("unknown buddy id %d\n", buddy);
1518 WARN_ON(1);
1519 addr = NULL;
1520 break;
1521 }
Vitaly Wool2f1e5e42017-02-24 14:57:23 -08001522
Vitaly Wool1f862982019-05-13 17:22:52 -07001523 if (addr)
1524 zhdr->mapped_count++;
Vitaly Wool9a001fc2016-05-20 16:58:30 -07001525out:
Vitaly Wool4a3ac932019-11-30 17:56:11 -08001526 put_z3fold_header(zhdr);
Vitaly Wool9a001fc2016-05-20 16:58:30 -07001527 return addr;
1528}
1529
1530/**
1531 * z3fold_unmap() - unmaps the allocation associated with the given handle
1532 * @pool: pool in which the allocation resides
1533 * @handle: handle associated with the allocation to be unmapped
1534 */
1535static void z3fold_unmap(struct z3fold_pool *pool, unsigned long handle)
1536{
1537 struct z3fold_header *zhdr;
1538 struct page *page;
1539 enum buddy buddy;
1540
Vitaly Wool4a3ac932019-11-30 17:56:11 -08001541 zhdr = get_z3fold_header(handle);
Vitaly Wool9a001fc2016-05-20 16:58:30 -07001542 page = virt_to_page(zhdr);
1543
Vitaly Wool2f1e5e42017-02-24 14:57:23 -08001544 if (test_bit(PAGE_HEADLESS, &page->private))
Vitaly Wool9a001fc2016-05-20 16:58:30 -07001545 return;
Vitaly Wool9a001fc2016-05-20 16:58:30 -07001546
1547 buddy = handle_to_buddy(handle);
1548 if (buddy == MIDDLE)
1549 clear_bit(MIDDLE_CHUNK_MAPPED, &page->private);
Vitaly Wool1f862982019-05-13 17:22:52 -07001550 zhdr->mapped_count--;
Vitaly Wool4a3ac932019-11-30 17:56:11 -08001551 put_z3fold_header(zhdr);
Vitaly Wool9a001fc2016-05-20 16:58:30 -07001552}
1553
1554/**
1555 * z3fold_get_pool_size() - gets the z3fold pool size in pages
1556 * @pool: pool whose size is being queried
1557 *
Vitaly Wool12d59ae2017-02-24 14:57:15 -08001558 * Returns: size in pages of the given pool.
Vitaly Wool9a001fc2016-05-20 16:58:30 -07001559 */
1560static u64 z3fold_get_pool_size(struct z3fold_pool *pool)
1561{
Vitaly Wool12d59ae2017-02-24 14:57:15 -08001562 return atomic64_read(&pool->pages_nr);
Vitaly Wool9a001fc2016-05-20 16:58:30 -07001563}
1564
Vitaly Wool1f862982019-05-13 17:22:52 -07001565static bool z3fold_page_isolate(struct page *page, isolate_mode_t mode)
1566{
1567 struct z3fold_header *zhdr;
1568 struct z3fold_pool *pool;
1569
1570 VM_BUG_ON_PAGE(!PageMovable(page), page);
1571 VM_BUG_ON_PAGE(PageIsolated(page), page);
1572
Vitaly Wool3f9d2b52019-09-23 15:33:02 -07001573 if (test_bit(PAGE_HEADLESS, &page->private) ||
1574 test_bit(PAGE_CLAIMED, &page->private))
Vitaly Wool1f862982019-05-13 17:22:52 -07001575 return false;
1576
1577 zhdr = page_address(page);
1578 z3fold_page_lock(zhdr);
1579 if (test_bit(NEEDS_COMPACTING, &page->private) ||
1580 test_bit(PAGE_STALE, &page->private))
1581 goto out;
1582
Vitaly Wool4a3ac932019-11-30 17:56:11 -08001583 if (zhdr->mapped_count != 0 || zhdr->foreign_handles != 0)
1584 goto out;
Vitaly Wool1f862982019-05-13 17:22:52 -07001585
Vitaly Wool4a3ac932019-11-30 17:56:11 -08001586 pool = zhdr_to_pool(zhdr);
1587 spin_lock(&pool->lock);
1588 if (!list_empty(&zhdr->buddy))
1589 list_del_init(&zhdr->buddy);
1590 if (!list_empty(&page->lru))
1591 list_del_init(&page->lru);
1592 spin_unlock(&pool->lock);
1593
1594 kref_get(&zhdr->refcount);
1595 z3fold_page_unlock(zhdr);
1596 return true;
1597
Vitaly Wool1f862982019-05-13 17:22:52 -07001598out:
1599 z3fold_page_unlock(zhdr);
1600 return false;
1601}
1602
1603static int z3fold_page_migrate(struct address_space *mapping, struct page *newpage,
1604 struct page *page, enum migrate_mode mode)
1605{
1606 struct z3fold_header *zhdr, *new_zhdr;
1607 struct z3fold_pool *pool;
1608 struct address_space *new_mapping;
1609
1610 VM_BUG_ON_PAGE(!PageMovable(page), page);
1611 VM_BUG_ON_PAGE(!PageIsolated(page), page);
Henry Burns810481a2019-07-11 20:52:14 -07001612 VM_BUG_ON_PAGE(!PageLocked(newpage), newpage);
Vitaly Wool1f862982019-05-13 17:22:52 -07001613
1614 zhdr = page_address(page);
1615 pool = zhdr_to_pool(zhdr);
1616
Vitaly Wool1f862982019-05-13 17:22:52 -07001617 if (!z3fold_page_trylock(zhdr)) {
Vitaly Wool1f862982019-05-13 17:22:52 -07001618 return -EAGAIN;
1619 }
Vitaly Wool4a3ac932019-11-30 17:56:11 -08001620 if (zhdr->mapped_count != 0 || zhdr->foreign_handles != 0) {
Vitaly Wool1f862982019-05-13 17:22:52 -07001621 z3fold_page_unlock(zhdr);
Vitaly Wool1f862982019-05-13 17:22:52 -07001622 return -EBUSY;
1623 }
Henry Burnsc92d2f32019-07-16 16:26:21 -07001624 if (work_pending(&zhdr->work)) {
1625 z3fold_page_unlock(zhdr);
1626 return -EAGAIN;
1627 }
Vitaly Wool1f862982019-05-13 17:22:52 -07001628 new_zhdr = page_address(newpage);
1629 memcpy(new_zhdr, zhdr, PAGE_SIZE);
1630 newpage->private = page->private;
1631 page->private = 0;
1632 z3fold_page_unlock(zhdr);
1633 spin_lock_init(&new_zhdr->page_lock);
Henry Burnsc92d2f32019-07-16 16:26:21 -07001634 INIT_WORK(&new_zhdr->work, compact_page_work);
1635 /*
1636 * z3fold_page_isolate() ensures that new_zhdr->buddy is empty,
1637 * so we only have to reinitialize it.
1638 */
1639 INIT_LIST_HEAD(&new_zhdr->buddy);
Vitaly Wool1f862982019-05-13 17:22:52 -07001640 new_mapping = page_mapping(page);
1641 __ClearPageMovable(page);
1642 ClearPagePrivate(page);
1643
1644 get_page(newpage);
1645 z3fold_page_lock(new_zhdr);
1646 if (new_zhdr->first_chunks)
1647 encode_handle(new_zhdr, FIRST);
1648 if (new_zhdr->last_chunks)
1649 encode_handle(new_zhdr, LAST);
1650 if (new_zhdr->middle_chunks)
1651 encode_handle(new_zhdr, MIDDLE);
1652 set_bit(NEEDS_COMPACTING, &newpage->private);
1653 new_zhdr->cpu = smp_processor_id();
1654 spin_lock(&pool->lock);
1655 list_add(&newpage->lru, &pool->lru);
1656 spin_unlock(&pool->lock);
1657 __SetPageMovable(newpage, new_mapping);
1658 z3fold_page_unlock(new_zhdr);
1659
1660 queue_work_on(new_zhdr->cpu, pool->compact_wq, &new_zhdr->work);
1661
1662 page_mapcount_reset(page);
Vitaly Wool1f862982019-05-13 17:22:52 -07001663 put_page(page);
1664 return 0;
1665}
1666
1667static void z3fold_page_putback(struct page *page)
1668{
1669 struct z3fold_header *zhdr;
1670 struct z3fold_pool *pool;
1671
1672 zhdr = page_address(page);
1673 pool = zhdr_to_pool(zhdr);
1674
1675 z3fold_page_lock(zhdr);
1676 if (!list_empty(&zhdr->buddy))
1677 list_del_init(&zhdr->buddy);
1678 INIT_LIST_HEAD(&page->lru);
1679 if (kref_put(&zhdr->refcount, release_z3fold_page_locked)) {
1680 atomic64_dec(&pool->pages_nr);
1681 return;
1682 }
1683 spin_lock(&pool->lock);
1684 list_add(&page->lru, &pool->lru);
1685 spin_unlock(&pool->lock);
1686 z3fold_page_unlock(zhdr);
1687}
1688
1689static const struct address_space_operations z3fold_aops = {
1690 .isolate_page = z3fold_page_isolate,
1691 .migratepage = z3fold_page_migrate,
1692 .putback_page = z3fold_page_putback,
1693};
1694
Vitaly Wool9a001fc2016-05-20 16:58:30 -07001695/*****************
1696 * zpool
1697 ****************/
1698
1699static int z3fold_zpool_evict(struct z3fold_pool *pool, unsigned long handle)
1700{
1701 if (pool->zpool && pool->zpool_ops && pool->zpool_ops->evict)
1702 return pool->zpool_ops->evict(pool->zpool, handle);
1703 else
1704 return -ENOENT;
1705}
1706
1707static const struct z3fold_ops z3fold_zpool_ops = {
1708 .evict = z3fold_zpool_evict
1709};
1710
1711static void *z3fold_zpool_create(const char *name, gfp_t gfp,
1712 const struct zpool_ops *zpool_ops,
1713 struct zpool *zpool)
1714{
1715 struct z3fold_pool *pool;
1716
Vitaly Woold30561c2017-09-06 16:24:47 -07001717 pool = z3fold_create_pool(name, gfp,
1718 zpool_ops ? &z3fold_zpool_ops : NULL);
Vitaly Wool9a001fc2016-05-20 16:58:30 -07001719 if (pool) {
1720 pool->zpool = zpool;
1721 pool->zpool_ops = zpool_ops;
1722 }
1723 return pool;
1724}
1725
1726static void z3fold_zpool_destroy(void *pool)
1727{
1728 z3fold_destroy_pool(pool);
1729}
1730
1731static int z3fold_zpool_malloc(void *pool, size_t size, gfp_t gfp,
1732 unsigned long *handle)
1733{
1734 return z3fold_alloc(pool, size, gfp, handle);
1735}
1736static void z3fold_zpool_free(void *pool, unsigned long handle)
1737{
1738 z3fold_free(pool, handle);
1739}
1740
1741static int z3fold_zpool_shrink(void *pool, unsigned int pages,
1742 unsigned int *reclaimed)
1743{
1744 unsigned int total = 0;
1745 int ret = -EINVAL;
1746
1747 while (total < pages) {
1748 ret = z3fold_reclaim_page(pool, 8);
1749 if (ret < 0)
1750 break;
1751 total++;
1752 }
1753
1754 if (reclaimed)
1755 *reclaimed = total;
1756
1757 return ret;
1758}
1759
1760static void *z3fold_zpool_map(void *pool, unsigned long handle,
1761 enum zpool_mapmode mm)
1762{
1763 return z3fold_map(pool, handle);
1764}
1765static void z3fold_zpool_unmap(void *pool, unsigned long handle)
1766{
1767 z3fold_unmap(pool, handle);
1768}
1769
1770static u64 z3fold_zpool_total_size(void *pool)
1771{
1772 return z3fold_get_pool_size(pool) * PAGE_SIZE;
1773}
1774
1775static struct zpool_driver z3fold_zpool_driver = {
1776 .type = "z3fold",
1777 .owner = THIS_MODULE,
1778 .create = z3fold_zpool_create,
1779 .destroy = z3fold_zpool_destroy,
1780 .malloc = z3fold_zpool_malloc,
1781 .free = z3fold_zpool_free,
1782 .shrink = z3fold_zpool_shrink,
1783 .map = z3fold_zpool_map,
1784 .unmap = z3fold_zpool_unmap,
1785 .total_size = z3fold_zpool_total_size,
1786};
1787
1788MODULE_ALIAS("zpool-z3fold");
1789
1790static int __init init_z3fold(void)
1791{
Vitaly Wool1f862982019-05-13 17:22:52 -07001792 int ret;
1793
Vitaly Woolede93212017-02-24 14:57:17 -08001794 /* Make sure the z3fold header is not larger than the page size */
1795 BUILD_BUG_ON(ZHDR_SIZE_ALIGNED > PAGE_SIZE);
Vitaly Wool1f862982019-05-13 17:22:52 -07001796 ret = z3fold_mount();
1797 if (ret)
1798 return ret;
1799
Vitaly Wool9a001fc2016-05-20 16:58:30 -07001800 zpool_register_driver(&z3fold_zpool_driver);
1801
1802 return 0;
1803}
1804
1805static void __exit exit_z3fold(void)
1806{
Vitaly Wool1f862982019-05-13 17:22:52 -07001807 z3fold_unmount();
Vitaly Wool9a001fc2016-05-20 16:58:30 -07001808 zpool_unregister_driver(&z3fold_zpool_driver);
1809}
1810
1811module_init(init_z3fold);
1812module_exit(exit_z3fold);
1813
1814MODULE_LICENSE("GPL");
1815MODULE_AUTHOR("Vitaly Wool <vitalywool@gmail.com>");
1816MODULE_DESCRIPTION("3-Fold Allocator for Compressed Pages");