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