]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - mm/z3fold.c
z3fold: fix reclaim lock-ups
[mirror_ubuntu-bionic-kernel.git] / mm / z3fold.c
CommitLineData
9a001fc1
VW
1/*
2 * z3fold.c
3 *
4 * Author: Vitaly Wool <vitaly.wool@konsulko.com>
5 * Copyright (C) 2016, Sony Mobile Communications Inc.
6 *
7 * This implementation is based on zbud written by Seth Jennings.
8 *
9 * z3fold is an special purpose allocator for storing compressed pages. It
10 * can store up to three compressed pages per page which improves the
11 * compression ratio of zbud while retaining its main concepts (e. g. always
12 * storing an integral number of objects per page) and simplicity.
13 * It still has simple and deterministic reclaim properties that make it
14 * preferable to a higher density approach (with no requirement on integral
15 * number of object per page) when reclaim is used.
16 *
17 * As in zbud, pages are divided into "chunks". The size of the chunks is
18 * fixed at compile time and is determined by NCHUNKS_ORDER below.
19 *
20 * z3fold doesn't export any API and is meant to be used via zpool API.
21 */
22
23#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
25#include <linux/atomic.h>
d30561c5 26#include <linux/sched.h>
9a001fc1
VW
27#include <linux/list.h>
28#include <linux/mm.h>
29#include <linux/module.h>
d30561c5 30#include <linux/percpu.h>
9a001fc1 31#include <linux/preempt.h>
d30561c5 32#include <linux/workqueue.h>
9a001fc1
VW
33#include <linux/slab.h>
34#include <linux/spinlock.h>
35#include <linux/zpool.h>
36
37/*****************
38 * Structures
39*****************/
ede93213
VW
40struct z3fold_pool;
41struct z3fold_ops {
42 int (*evict)(struct z3fold_pool *pool, unsigned long handle);
43};
44
45enum buddy {
46 HEADLESS = 0,
47 FIRST,
48 MIDDLE,
49 LAST,
50 BUDDIES_MAX
51};
52
53/*
d30561c5 54 * struct z3fold_header - z3fold page metadata occupying first chunks of each
ede93213 55 * z3fold page, except for HEADLESS pages
d30561c5
VW
56 * @buddy: links the z3fold page into the relevant list in the
57 * pool
2f1e5e4d 58 * @page_lock: per-page lock
d30561c5
VW
59 * @refcount: reference count for the z3fold page
60 * @work: work_struct for page layout optimization
61 * @pool: pointer to the pool which this page belongs to
62 * @cpu: CPU which this page "belongs" to
ede93213
VW
63 * @first_chunks: the size of the first buddy in chunks, 0 if free
64 * @middle_chunks: the size of the middle buddy in chunks, 0 if free
65 * @last_chunks: the size of the last buddy in chunks, 0 if free
66 * @first_num: the starting number (for the first handle)
67 */
68struct z3fold_header {
69 struct list_head buddy;
2f1e5e4d 70 spinlock_t page_lock;
5a27aa82 71 struct kref refcount;
d30561c5
VW
72 struct work_struct work;
73 struct z3fold_pool *pool;
74 short cpu;
ede93213
VW
75 unsigned short first_chunks;
76 unsigned short middle_chunks;
77 unsigned short last_chunks;
78 unsigned short start_middle;
79 unsigned short first_num:2;
80};
81
9a001fc1
VW
82/*
83 * NCHUNKS_ORDER determines the internal allocation granularity, effectively
84 * adjusting internal fragmentation. It also determines the number of
85 * freelists maintained in each pool. NCHUNKS_ORDER of 6 means that the
ede93213
VW
86 * allocation granularity will be in chunks of size PAGE_SIZE/64. Some chunks
87 * in the beginning of an allocated page are occupied by z3fold header, so
88 * NCHUNKS will be calculated to 63 (or 62 in case CONFIG_DEBUG_SPINLOCK=y),
89 * which shows the max number of free chunks in z3fold page, also there will
90 * be 63, or 62, respectively, freelists per pool.
9a001fc1
VW
91 */
92#define NCHUNKS_ORDER 6
93
94#define CHUNK_SHIFT (PAGE_SHIFT - NCHUNKS_ORDER)
95#define CHUNK_SIZE (1 << CHUNK_SHIFT)
ede93213
VW
96#define ZHDR_SIZE_ALIGNED round_up(sizeof(struct z3fold_header), CHUNK_SIZE)
97#define ZHDR_CHUNKS (ZHDR_SIZE_ALIGNED >> CHUNK_SHIFT)
98#define TOTAL_CHUNKS (PAGE_SIZE >> CHUNK_SHIFT)
9a001fc1
VW
99#define NCHUNKS ((PAGE_SIZE - ZHDR_SIZE_ALIGNED) >> CHUNK_SHIFT)
100
f201ebd8 101#define BUDDY_MASK (0x3)
9a001fc1 102
9a001fc1
VW
103/**
104 * struct z3fold_pool - stores metadata for each z3fold pool
d30561c5
VW
105 * @name: pool name
106 * @lock: protects pool unbuddied/lru lists
107 * @stale_lock: protects pool stale page list
108 * @unbuddied: per-cpu array of lists tracking z3fold pages that contain 2-
109 * buddies; the list each z3fold page is added to depends on
110 * the size of its free region.
9a001fc1
VW
111 * @lru: list tracking the z3fold pages in LRU order by most recently
112 * added buddy.
d30561c5 113 * @stale: list of pages marked for freeing
9a001fc1
VW
114 * @pages_nr: number of z3fold pages in the pool.
115 * @ops: pointer to a structure of user defined operations specified at
116 * pool creation time.
d30561c5
VW
117 * @compact_wq: workqueue for page layout background optimization
118 * @release_wq: workqueue for safe page release
119 * @work: work_struct for safe page release
9a001fc1
VW
120 *
121 * This structure is allocated at pool creation time and maintains metadata
122 * pertaining to a particular z3fold pool.
123 */
124struct z3fold_pool {
d30561c5 125 const char *name;
9a001fc1 126 spinlock_t lock;
d30561c5
VW
127 spinlock_t stale_lock;
128 struct list_head *unbuddied;
9a001fc1 129 struct list_head lru;
d30561c5 130 struct list_head stale;
12d59ae6 131 atomic64_t pages_nr;
9a001fc1
VW
132 const struct z3fold_ops *ops;
133 struct zpool *zpool;
134 const struct zpool_ops *zpool_ops;
d30561c5
VW
135 struct workqueue_struct *compact_wq;
136 struct workqueue_struct *release_wq;
137 struct work_struct work;
9a001fc1
VW
138};
139
9a001fc1
VW
140/*
141 * Internal z3fold page flags
142 */
143enum z3fold_page_flags {
5a27aa82 144 PAGE_HEADLESS = 0,
9a001fc1 145 MIDDLE_CHUNK_MAPPED,
d30561c5 146 NEEDS_COMPACTING,
9fc5adb0
VW
147 PAGE_STALE,
148 UNDER_RECLAIM
9a001fc1
VW
149};
150
151/*****************
152 * Helpers
153*****************/
154
155/* Converts an allocation size in bytes to size in z3fold chunks */
156static int size_to_chunks(size_t size)
157{
158 return (size + CHUNK_SIZE - 1) >> CHUNK_SHIFT;
159}
160
161#define for_each_unbuddied_list(_iter, _begin) \
162 for ((_iter) = (_begin); (_iter) < NCHUNKS; (_iter)++)
163
d30561c5
VW
164static void compact_page_work(struct work_struct *w);
165
9a001fc1 166/* Initializes the z3fold header of a newly allocated z3fold page */
d30561c5
VW
167static struct z3fold_header *init_z3fold_page(struct page *page,
168 struct z3fold_pool *pool)
9a001fc1
VW
169{
170 struct z3fold_header *zhdr = page_address(page);
171
172 INIT_LIST_HEAD(&page->lru);
9a001fc1
VW
173 clear_bit(PAGE_HEADLESS, &page->private);
174 clear_bit(MIDDLE_CHUNK_MAPPED, &page->private);
d30561c5
VW
175 clear_bit(NEEDS_COMPACTING, &page->private);
176 clear_bit(PAGE_STALE, &page->private);
9fc5adb0 177 clear_bit(UNDER_RECLAIM, &page->private);
9a001fc1 178
2f1e5e4d 179 spin_lock_init(&zhdr->page_lock);
5a27aa82 180 kref_init(&zhdr->refcount);
9a001fc1
VW
181 zhdr->first_chunks = 0;
182 zhdr->middle_chunks = 0;
183 zhdr->last_chunks = 0;
184 zhdr->first_num = 0;
185 zhdr->start_middle = 0;
d30561c5
VW
186 zhdr->cpu = -1;
187 zhdr->pool = pool;
9a001fc1 188 INIT_LIST_HEAD(&zhdr->buddy);
d30561c5 189 INIT_WORK(&zhdr->work, compact_page_work);
9a001fc1
VW
190 return zhdr;
191}
192
193/* Resets the struct page fields and frees the page */
5a27aa82 194static void free_z3fold_page(struct page *page)
9a001fc1 195{
5a27aa82
VW
196 __free_page(page);
197}
198
2f1e5e4d
VW
199/* Lock a z3fold page */
200static inline void z3fold_page_lock(struct z3fold_header *zhdr)
201{
202 spin_lock(&zhdr->page_lock);
203}
204
76e32a2a
VW
205/* Try to lock a z3fold page */
206static inline int z3fold_page_trylock(struct z3fold_header *zhdr)
207{
208 return spin_trylock(&zhdr->page_lock);
209}
210
2f1e5e4d
VW
211/* Unlock a z3fold page */
212static inline void z3fold_page_unlock(struct z3fold_header *zhdr)
213{
214 spin_unlock(&zhdr->page_lock);
215}
216
9a001fc1
VW
217/*
218 * Encodes the handle of a particular buddy within a z3fold page
219 * Pool lock should be held as this function accesses first_num
220 */
221static unsigned long encode_handle(struct z3fold_header *zhdr, enum buddy bud)
222{
223 unsigned long handle;
224
225 handle = (unsigned long)zhdr;
226 if (bud != HEADLESS)
227 handle += (bud + zhdr->first_num) & BUDDY_MASK;
228 return handle;
229}
230
231/* Returns the z3fold page where a given handle is stored */
232static struct z3fold_header *handle_to_z3fold_header(unsigned long handle)
233{
234 return (struct z3fold_header *)(handle & PAGE_MASK);
235}
236
f201ebd8 237/*
238 * (handle & BUDDY_MASK) < zhdr->first_num is possible in encode_handle
239 * but that doesn't matter. because the masking will result in the
240 * correct buddy number.
241 */
9a001fc1
VW
242static enum buddy handle_to_buddy(unsigned long handle)
243{
244 struct z3fold_header *zhdr = handle_to_z3fold_header(handle);
245 return (handle - zhdr->first_num) & BUDDY_MASK;
246}
247
d30561c5
VW
248static void __release_z3fold_page(struct z3fold_header *zhdr, bool locked)
249{
250 struct page *page = virt_to_page(zhdr);
251 struct z3fold_pool *pool = zhdr->pool;
252
253 WARN_ON(!list_empty(&zhdr->buddy));
254 set_bit(PAGE_STALE, &page->private);
35529357 255 clear_bit(NEEDS_COMPACTING, &page->private);
d30561c5
VW
256 spin_lock(&pool->lock);
257 if (!list_empty(&page->lru))
258 list_del(&page->lru);
259 spin_unlock(&pool->lock);
260 if (locked)
261 z3fold_page_unlock(zhdr);
262 spin_lock(&pool->stale_lock);
263 list_add(&zhdr->buddy, &pool->stale);
264 queue_work(pool->release_wq, &pool->work);
265 spin_unlock(&pool->stale_lock);
266}
267
268static void __attribute__((__unused__))
269 release_z3fold_page(struct kref *ref)
270{
271 struct z3fold_header *zhdr = container_of(ref, struct z3fold_header,
272 refcount);
273 __release_z3fold_page(zhdr, false);
274}
275
276static void release_z3fold_page_locked(struct kref *ref)
277{
278 struct z3fold_header *zhdr = container_of(ref, struct z3fold_header,
279 refcount);
280 WARN_ON(z3fold_page_trylock(zhdr));
281 __release_z3fold_page(zhdr, true);
282}
283
284static void release_z3fold_page_locked_list(struct kref *ref)
285{
286 struct z3fold_header *zhdr = container_of(ref, struct z3fold_header,
287 refcount);
288 spin_lock(&zhdr->pool->lock);
289 list_del_init(&zhdr->buddy);
290 spin_unlock(&zhdr->pool->lock);
291
292 WARN_ON(z3fold_page_trylock(zhdr));
293 __release_z3fold_page(zhdr, true);
294}
295
296static void free_pages_work(struct work_struct *w)
297{
298 struct z3fold_pool *pool = container_of(w, struct z3fold_pool, work);
299
300 spin_lock(&pool->stale_lock);
301 while (!list_empty(&pool->stale)) {
302 struct z3fold_header *zhdr = list_first_entry(&pool->stale,
303 struct z3fold_header, buddy);
304 struct page *page = virt_to_page(zhdr);
305
306 list_del(&zhdr->buddy);
307 if (WARN_ON(!test_bit(PAGE_STALE, &page->private)))
308 continue;
d30561c5
VW
309 spin_unlock(&pool->stale_lock);
310 cancel_work_sync(&zhdr->work);
311 free_z3fold_page(page);
312 cond_resched();
313 spin_lock(&pool->stale_lock);
314 }
315 spin_unlock(&pool->stale_lock);
316}
317
9a001fc1
VW
318/*
319 * Returns the number of free chunks in a z3fold page.
320 * NB: can't be used with HEADLESS pages.
321 */
322static int num_free_chunks(struct z3fold_header *zhdr)
323{
324 int nfree;
325 /*
326 * If there is a middle object, pick up the bigger free space
327 * either before or after it. Otherwise just subtract the number
328 * of chunks occupied by the first and the last objects.
329 */
330 if (zhdr->middle_chunks != 0) {
331 int nfree_before = zhdr->first_chunks ?
ede93213 332 0 : zhdr->start_middle - ZHDR_CHUNKS;
9a001fc1 333 int nfree_after = zhdr->last_chunks ?
ede93213
VW
334 0 : TOTAL_CHUNKS -
335 (zhdr->start_middle + zhdr->middle_chunks);
9a001fc1
VW
336 nfree = max(nfree_before, nfree_after);
337 } else
338 nfree = NCHUNKS - zhdr->first_chunks - zhdr->last_chunks;
339 return nfree;
340}
341
ede93213
VW
342static inline void *mchunk_memmove(struct z3fold_header *zhdr,
343 unsigned short dst_chunk)
344{
345 void *beg = zhdr;
346 return memmove(beg + (dst_chunk << CHUNK_SHIFT),
347 beg + (zhdr->start_middle << CHUNK_SHIFT),
348 zhdr->middle_chunks << CHUNK_SHIFT);
349}
350
1b096e5a 351#define BIG_CHUNK_GAP 3
9a001fc1
VW
352/* Has to be called with lock held */
353static int z3fold_compact_page(struct z3fold_header *zhdr)
354{
355 struct page *page = virt_to_page(zhdr);
9a001fc1 356
ede93213
VW
357 if (test_bit(MIDDLE_CHUNK_MAPPED, &page->private))
358 return 0; /* can't move middle chunk, it's used */
9a001fc1 359
ede93213
VW
360 if (zhdr->middle_chunks == 0)
361 return 0; /* nothing to compact */
362
363 if (zhdr->first_chunks == 0 && zhdr->last_chunks == 0) {
364 /* move to the beginning */
365 mchunk_memmove(zhdr, ZHDR_CHUNKS);
9a001fc1
VW
366 zhdr->first_chunks = zhdr->middle_chunks;
367 zhdr->middle_chunks = 0;
368 zhdr->start_middle = 0;
369 zhdr->first_num++;
1b096e5a 370 return 1;
9a001fc1 371 }
1b096e5a
VW
372
373 /*
374 * moving data is expensive, so let's only do that if
375 * there's substantial gain (at least BIG_CHUNK_GAP chunks)
376 */
377 if (zhdr->first_chunks != 0 && zhdr->last_chunks == 0 &&
378 zhdr->start_middle - (zhdr->first_chunks + ZHDR_CHUNKS) >=
379 BIG_CHUNK_GAP) {
380 mchunk_memmove(zhdr, zhdr->first_chunks + ZHDR_CHUNKS);
381 zhdr->start_middle = zhdr->first_chunks + ZHDR_CHUNKS;
382 return 1;
383 } else if (zhdr->last_chunks != 0 && zhdr->first_chunks == 0 &&
384 TOTAL_CHUNKS - (zhdr->last_chunks + zhdr->start_middle
385 + zhdr->middle_chunks) >=
386 BIG_CHUNK_GAP) {
387 unsigned short new_start = TOTAL_CHUNKS - zhdr->last_chunks -
388 zhdr->middle_chunks;
389 mchunk_memmove(zhdr, new_start);
390 zhdr->start_middle = new_start;
391 return 1;
392 }
393
394 return 0;
9a001fc1
VW
395}
396
d30561c5
VW
397static void do_compact_page(struct z3fold_header *zhdr, bool locked)
398{
399 struct z3fold_pool *pool = zhdr->pool;
400 struct page *page;
401 struct list_head *unbuddied;
402 int fchunks;
403
404 page = virt_to_page(zhdr);
405 if (locked)
406 WARN_ON(z3fold_page_trylock(zhdr));
407 else
408 z3fold_page_lock(zhdr);
5d03a661 409 if (WARN_ON(!test_and_clear_bit(NEEDS_COMPACTING, &page->private))) {
d30561c5
VW
410 z3fold_page_unlock(zhdr);
411 return;
412 }
413 spin_lock(&pool->lock);
414 list_del_init(&zhdr->buddy);
415 spin_unlock(&pool->lock);
416
5d03a661
VW
417 if (kref_put(&zhdr->refcount, release_z3fold_page_locked)) {
418 atomic64_dec(&pool->pages_nr);
419 return;
420 }
421
d30561c5
VW
422 z3fold_compact_page(zhdr);
423 unbuddied = get_cpu_ptr(pool->unbuddied);
424 fchunks = num_free_chunks(zhdr);
425 if (fchunks < NCHUNKS &&
426 (!zhdr->first_chunks || !zhdr->middle_chunks ||
427 !zhdr->last_chunks)) {
428 /* the page's not completely free and it's unbuddied */
429 spin_lock(&pool->lock);
430 list_add(&zhdr->buddy, &unbuddied[fchunks]);
431 spin_unlock(&pool->lock);
432 zhdr->cpu = smp_processor_id();
433 }
434 put_cpu_ptr(pool->unbuddied);
435 z3fold_page_unlock(zhdr);
436}
437
438static void compact_page_work(struct work_struct *w)
439{
440 struct z3fold_header *zhdr = container_of(w, struct z3fold_header,
441 work);
442
443 do_compact_page(zhdr, false);
444}
445
446
447/*
448 * API Functions
449 */
450
451/**
452 * z3fold_create_pool() - create a new z3fold pool
453 * @name: pool name
454 * @gfp: gfp flags when allocating the z3fold pool structure
455 * @ops: user-defined operations for the z3fold pool
456 *
457 * Return: pointer to the new z3fold pool or NULL if the metadata allocation
458 * failed.
459 */
460static struct z3fold_pool *z3fold_create_pool(const char *name, gfp_t gfp,
461 const struct z3fold_ops *ops)
462{
463 struct z3fold_pool *pool = NULL;
464 int i, cpu;
465
466 pool = kzalloc(sizeof(struct z3fold_pool), gfp);
467 if (!pool)
468 goto out;
469 spin_lock_init(&pool->lock);
470 spin_lock_init(&pool->stale_lock);
471 pool->unbuddied = __alloc_percpu(sizeof(struct list_head)*NCHUNKS, 2);
472 for_each_possible_cpu(cpu) {
473 struct list_head *unbuddied =
474 per_cpu_ptr(pool->unbuddied, cpu);
475 for_each_unbuddied_list(i, 0)
476 INIT_LIST_HEAD(&unbuddied[i]);
477 }
478 INIT_LIST_HEAD(&pool->lru);
479 INIT_LIST_HEAD(&pool->stale);
480 atomic64_set(&pool->pages_nr, 0);
481 pool->name = name;
482 pool->compact_wq = create_singlethread_workqueue(pool->name);
483 if (!pool->compact_wq)
484 goto out;
485 pool->release_wq = create_singlethread_workqueue(pool->name);
486 if (!pool->release_wq)
487 goto out_wq;
488 INIT_WORK(&pool->work, free_pages_work);
489 pool->ops = ops;
490 return pool;
491
492out_wq:
493 destroy_workqueue(pool->compact_wq);
494out:
495 kfree(pool);
496 return NULL;
497}
498
499/**
500 * z3fold_destroy_pool() - destroys an existing z3fold pool
501 * @pool: the z3fold pool to be destroyed
502 *
503 * The pool should be emptied before this function is called.
504 */
505static void z3fold_destroy_pool(struct z3fold_pool *pool)
506{
507 destroy_workqueue(pool->release_wq);
508 destroy_workqueue(pool->compact_wq);
509 kfree(pool);
510}
511
9a001fc1
VW
512/**
513 * z3fold_alloc() - allocates a region of a given size
514 * @pool: z3fold pool from which to allocate
515 * @size: size in bytes of the desired allocation
516 * @gfp: gfp flags used if the pool needs to grow
517 * @handle: handle of the new allocation
518 *
519 * This function will attempt to find a free region in the pool large enough to
520 * satisfy the allocation request. A search of the unbuddied lists is
521 * performed first. If no suitable free region is found, then a new page is
522 * allocated and added to the pool to satisfy the request.
523 *
524 * gfp should not set __GFP_HIGHMEM as highmem pages cannot be used
525 * as z3fold pool pages.
526 *
527 * Return: 0 if success and handle is set, otherwise -EINVAL if the size or
528 * gfp arguments are invalid or -ENOMEM if the pool was unable to allocate
529 * a new page.
530 */
531static int z3fold_alloc(struct z3fold_pool *pool, size_t size, gfp_t gfp,
532 unsigned long *handle)
533{
534 int chunks = 0, i, freechunks;
535 struct z3fold_header *zhdr = NULL;
d30561c5 536 struct page *page = NULL;
9a001fc1 537 enum buddy bud;
d30561c5 538 bool can_sleep = (gfp & __GFP_RECLAIM) == __GFP_RECLAIM;
9a001fc1
VW
539
540 if (!size || (gfp & __GFP_HIGHMEM))
541 return -EINVAL;
542
543 if (size > PAGE_SIZE)
544 return -ENOSPC;
545
546 if (size > PAGE_SIZE - ZHDR_SIZE_ALIGNED - CHUNK_SIZE)
547 bud = HEADLESS;
548 else {
d30561c5 549 struct list_head *unbuddied;
9a001fc1 550 chunks = size_to_chunks(size);
9a001fc1 551
d30561c5 552lookup:
9a001fc1 553 /* First, try to find an unbuddied z3fold page. */
d30561c5 554 unbuddied = get_cpu_ptr(pool->unbuddied);
9a001fc1 555 for_each_unbuddied_list(i, chunks) {
d30561c5
VW
556 struct list_head *l = &unbuddied[i];
557
558 zhdr = list_first_entry_or_null(READ_ONCE(l),
9a001fc1 559 struct z3fold_header, buddy);
d30561c5
VW
560
561 if (!zhdr)
2f1e5e4d 562 continue;
d30561c5
VW
563
564 /* Re-check under lock. */
565 spin_lock(&pool->lock);
566 l = &unbuddied[i];
567 if (unlikely(zhdr != list_first_entry(READ_ONCE(l),
568 struct z3fold_header, buddy)) ||
569 !z3fold_page_trylock(zhdr)) {
570 spin_unlock(&pool->lock);
571 put_cpu_ptr(pool->unbuddied);
572 goto lookup;
2f1e5e4d
VW
573 }
574 list_del_init(&zhdr->buddy);
d30561c5 575 zhdr->cpu = -1;
2f1e5e4d
VW
576 spin_unlock(&pool->lock);
577
578 page = virt_to_page(zhdr);
d30561c5
VW
579 if (test_bit(NEEDS_COMPACTING, &page->private)) {
580 z3fold_page_unlock(zhdr);
581 zhdr = NULL;
582 put_cpu_ptr(pool->unbuddied);
583 if (can_sleep)
584 cond_resched();
585 goto lookup;
586 }
587
588 /*
589 * this page could not be removed from its unbuddied
590 * list while pool lock was held, and then we've taken
591 * page lock so kref_put could not be called before
592 * we got here, so it's safe to just call kref_get()
593 */
594 kref_get(&zhdr->refcount);
595 break;
596 }
597 put_cpu_ptr(pool->unbuddied);
598
599 if (zhdr) {
2f1e5e4d
VW
600 if (zhdr->first_chunks == 0) {
601 if (zhdr->middle_chunks != 0 &&
602 chunks >= zhdr->start_middle)
9a001fc1 603 bud = LAST;
2f1e5e4d
VW
604 else
605 bud = FIRST;
606 } else if (zhdr->last_chunks == 0)
607 bud = LAST;
608 else if (zhdr->middle_chunks == 0)
609 bud = MIDDLE;
610 else {
5a27aa82 611 if (kref_put(&zhdr->refcount,
d30561c5 612 release_z3fold_page_locked))
5a27aa82 613 atomic64_dec(&pool->pages_nr);
d30561c5
VW
614 else
615 z3fold_page_unlock(zhdr);
2f1e5e4d
VW
616 pr_err("No free chunks in unbuddied\n");
617 WARN_ON(1);
d30561c5 618 goto lookup;
9a001fc1 619 }
2f1e5e4d 620 goto found;
9a001fc1
VW
621 }
622 bud = FIRST;
9a001fc1
VW
623 }
624
d30561c5
VW
625 spin_lock(&pool->stale_lock);
626 zhdr = list_first_entry_or_null(&pool->stale,
627 struct z3fold_header, buddy);
628 /*
629 * Before allocating a page, let's see if we can take one from the
630 * stale pages list. cancel_work_sync() can sleep so we must make
631 * sure it won't be called in case we're in atomic context.
632 */
35529357 633 if (zhdr && (can_sleep || !work_pending(&zhdr->work))) {
d30561c5 634 list_del(&zhdr->buddy);
d30561c5
VW
635 spin_unlock(&pool->stale_lock);
636 if (can_sleep)
637 cancel_work_sync(&zhdr->work);
638 page = virt_to_page(zhdr);
639 } else {
640 spin_unlock(&pool->stale_lock);
641 page = alloc_page(gfp);
642 }
643
9a001fc1
VW
644 if (!page)
645 return -ENOMEM;
2f1e5e4d 646
12d59ae6 647 atomic64_inc(&pool->pages_nr);
d30561c5 648 zhdr = init_z3fold_page(page, pool);
9a001fc1
VW
649
650 if (bud == HEADLESS) {
651 set_bit(PAGE_HEADLESS, &page->private);
652 goto headless;
653 }
2f1e5e4d 654 z3fold_page_lock(zhdr);
9a001fc1
VW
655
656found:
657 if (bud == FIRST)
658 zhdr->first_chunks = chunks;
659 else if (bud == LAST)
660 zhdr->last_chunks = chunks;
661 else {
662 zhdr->middle_chunks = chunks;
ede93213 663 zhdr->start_middle = zhdr->first_chunks + ZHDR_CHUNKS;
9a001fc1
VW
664 }
665
666 if (zhdr->first_chunks == 0 || zhdr->last_chunks == 0 ||
667 zhdr->middle_chunks == 0) {
d30561c5
VW
668 struct list_head *unbuddied = get_cpu_ptr(pool->unbuddied);
669
9a001fc1
VW
670 /* Add to unbuddied list */
671 freechunks = num_free_chunks(zhdr);
d30561c5
VW
672 spin_lock(&pool->lock);
673 list_add(&zhdr->buddy, &unbuddied[freechunks]);
674 spin_unlock(&pool->lock);
675 zhdr->cpu = smp_processor_id();
676 put_cpu_ptr(pool->unbuddied);
9a001fc1
VW
677 }
678
679headless:
d30561c5 680 spin_lock(&pool->lock);
9a001fc1
VW
681 /* Add/move z3fold page to beginning of LRU */
682 if (!list_empty(&page->lru))
683 list_del(&page->lru);
684
685 list_add(&page->lru, &pool->lru);
686
687 *handle = encode_handle(zhdr, bud);
688 spin_unlock(&pool->lock);
2f1e5e4d
VW
689 if (bud != HEADLESS)
690 z3fold_page_unlock(zhdr);
9a001fc1
VW
691
692 return 0;
693}
694
695/**
696 * z3fold_free() - frees the allocation associated with the given handle
697 * @pool: pool in which the allocation resided
698 * @handle: handle associated with the allocation returned by z3fold_alloc()
699 *
700 * In the case that the z3fold page in which the allocation resides is under
701 * reclaim, as indicated by the PG_reclaim flag being set, this function
702 * only sets the first|last_chunks to 0. The page is actually freed
703 * once both buddies are evicted (see z3fold_reclaim_page() below).
704 */
705static void z3fold_free(struct z3fold_pool *pool, unsigned long handle)
706{
707 struct z3fold_header *zhdr;
9a001fc1
VW
708 struct page *page;
709 enum buddy bud;
710
9a001fc1
VW
711 zhdr = handle_to_z3fold_header(handle);
712 page = virt_to_page(zhdr);
713
714 if (test_bit(PAGE_HEADLESS, &page->private)) {
715 /* HEADLESS page stored */
716 bud = HEADLESS;
717 } else {
2f1e5e4d 718 z3fold_page_lock(zhdr);
43afc194 719 bud = handle_to_buddy(handle);
9a001fc1
VW
720
721 switch (bud) {
722 case FIRST:
723 zhdr->first_chunks = 0;
724 break;
725 case MIDDLE:
726 zhdr->middle_chunks = 0;
727 zhdr->start_middle = 0;
728 break;
729 case LAST:
730 zhdr->last_chunks = 0;
731 break;
732 default:
733 pr_err("%s: unknown bud %d\n", __func__, bud);
734 WARN_ON(1);
2f1e5e4d 735 z3fold_page_unlock(zhdr);
9a001fc1
VW
736 return;
737 }
738 }
739
5a27aa82 740 if (bud == HEADLESS) {
2f1e5e4d 741 spin_lock(&pool->lock);
9a001fc1 742 list_del(&page->lru);
2f1e5e4d 743 spin_unlock(&pool->lock);
5a27aa82 744 free_z3fold_page(page);
12d59ae6 745 atomic64_dec(&pool->pages_nr);
d30561c5
VW
746 return;
747 }
748
749 if (kref_put(&zhdr->refcount, release_z3fold_page_locked_list)) {
750 atomic64_dec(&pool->pages_nr);
751 return;
752 }
9fc5adb0
VW
753 if (test_bit(UNDER_RECLAIM, &page->private)) {
754 z3fold_page_unlock(zhdr);
755 return;
756 }
d30561c5 757 if (test_and_set_bit(NEEDS_COMPACTING, &page->private)) {
5a27aa82 758 z3fold_page_unlock(zhdr);
d30561c5
VW
759 return;
760 }
761 if (zhdr->cpu < 0 || !cpu_online(zhdr->cpu)) {
2f1e5e4d 762 spin_lock(&pool->lock);
d30561c5 763 list_del_init(&zhdr->buddy);
2f1e5e4d 764 spin_unlock(&pool->lock);
d30561c5 765 zhdr->cpu = -1;
5d03a661 766 kref_get(&zhdr->refcount);
d30561c5
VW
767 do_compact_page(zhdr, true);
768 return;
9a001fc1 769 }
5d03a661 770 kref_get(&zhdr->refcount);
d30561c5
VW
771 queue_work_on(zhdr->cpu, pool->compact_wq, &zhdr->work);
772 z3fold_page_unlock(zhdr);
9a001fc1
VW
773}
774
775/**
776 * z3fold_reclaim_page() - evicts allocations from a pool page and frees it
777 * @pool: pool from which a page will attempt to be evicted
778 * @retires: number of pages on the LRU list for which eviction will
779 * be attempted before failing
780 *
781 * z3fold reclaim is different from normal system reclaim in that it is done
782 * from the bottom, up. This is because only the bottom layer, z3fold, has
783 * information on how the allocations are organized within each z3fold page.
784 * This has the potential to create interesting locking situations between
785 * z3fold and the user, however.
786 *
787 * To avoid these, this is how z3fold_reclaim_page() should be called:
788
789 * The user detects a page should be reclaimed and calls z3fold_reclaim_page().
790 * z3fold_reclaim_page() will remove a z3fold page from the pool LRU list and
791 * call the user-defined eviction handler with the pool and handle as
792 * arguments.
793 *
794 * If the handle can not be evicted, the eviction handler should return
795 * non-zero. z3fold_reclaim_page() will add the z3fold page back to the
796 * appropriate list and try the next z3fold page on the LRU up to
797 * a user defined number of retries.
798 *
799 * If the handle is successfully evicted, the eviction handler should
800 * return 0 _and_ should have called z3fold_free() on the handle. z3fold_free()
801 * contains logic to delay freeing the page if the page is under reclaim,
802 * as indicated by the setting of the PG_reclaim flag on the underlying page.
803 *
804 * If all buddies in the z3fold page are successfully evicted, then the
805 * z3fold page can be freed.
806 *
807 * Returns: 0 if page is successfully freed, otherwise -EINVAL if there are
808 * no pages to evict or an eviction handler is not registered, -EAGAIN if
809 * the retry limit was hit.
810 */
811static int z3fold_reclaim_page(struct z3fold_pool *pool, unsigned int retries)
812{
d30561c5
VW
813 int i, ret = 0;
814 struct z3fold_header *zhdr = NULL;
815 struct page *page = NULL;
816 struct list_head *pos;
9a001fc1
VW
817 unsigned long first_handle = 0, middle_handle = 0, last_handle = 0;
818
819 spin_lock(&pool->lock);
2f1e5e4d 820 if (!pool->ops || !pool->ops->evict || retries == 0) {
9a001fc1
VW
821 spin_unlock(&pool->lock);
822 return -EINVAL;
823 }
824 for (i = 0; i < retries; i++) {
2f1e5e4d
VW
825 if (list_empty(&pool->lru)) {
826 spin_unlock(&pool->lock);
827 return -EINVAL;
828 }
d30561c5
VW
829 list_for_each_prev(pos, &pool->lru) {
830 page = list_entry(pos, struct page, lru);
831 if (test_bit(PAGE_HEADLESS, &page->private))
832 /* candidate found */
833 break;
834
835 zhdr = page_address(page);
836 if (!z3fold_page_trylock(zhdr))
837 continue; /* can't evict at this point */
838 kref_get(&zhdr->refcount);
839 list_del_init(&zhdr->buddy);
840 zhdr->cpu = -1;
9fc5adb0
VW
841 set_bit(UNDER_RECLAIM, &page->private);
842 break;
d30561c5
VW
843 }
844
5a27aa82 845 list_del_init(&page->lru);
d30561c5 846 spin_unlock(&pool->lock);
9a001fc1 847
9a001fc1 848 if (!test_bit(PAGE_HEADLESS, &page->private)) {
9a001fc1
VW
849 /*
850 * We need encode the handles before unlocking, since
851 * we can race with free that will set
852 * (first|last)_chunks to 0
853 */
854 first_handle = 0;
855 last_handle = 0;
856 middle_handle = 0;
857 if (zhdr->first_chunks)
858 first_handle = encode_handle(zhdr, FIRST);
859 if (zhdr->middle_chunks)
860 middle_handle = encode_handle(zhdr, MIDDLE);
861 if (zhdr->last_chunks)
862 last_handle = encode_handle(zhdr, LAST);
d30561c5
VW
863 /*
864 * it's safe to unlock here because we hold a
865 * reference to this page
866 */
2f1e5e4d 867 z3fold_page_unlock(zhdr);
9a001fc1
VW
868 } else {
869 first_handle = encode_handle(zhdr, HEADLESS);
870 last_handle = middle_handle = 0;
871 }
872
9a001fc1
VW
873 /* Issue the eviction callback(s) */
874 if (middle_handle) {
875 ret = pool->ops->evict(pool, middle_handle);
876 if (ret)
877 goto next;
878 }
879 if (first_handle) {
880 ret = pool->ops->evict(pool, first_handle);
881 if (ret)
882 goto next;
883 }
884 if (last_handle) {
885 ret = pool->ops->evict(pool, last_handle);
886 if (ret)
887 goto next;
888 }
889next:
5a27aa82
VW
890 if (test_bit(PAGE_HEADLESS, &page->private)) {
891 if (ret == 0) {
892 free_z3fold_page(page);
893 return 0;
5a27aa82 894 }
9fc5adb0
VW
895 spin_lock(&pool->lock);
896 list_add(&page->lru, &pool->lru);
897 spin_unlock(&pool->lock);
898 } else {
899 z3fold_page_lock(zhdr);
900 clear_bit(UNDER_RECLAIM, &page->private);
901 if (kref_put(&zhdr->refcount,
902 release_z3fold_page_locked)) {
903 atomic64_dec(&pool->pages_nr);
904 return 0;
905 }
906 /*
907 * if we are here, the page is still not completely
908 * free. Take the global pool lock then to be able
909 * to add it back to the lru list
910 */
911 spin_lock(&pool->lock);
912 list_add(&page->lru, &pool->lru);
d5567c9d 913 spin_unlock(&pool->lock);
9fc5adb0 914 z3fold_page_unlock(zhdr);
5a27aa82 915 }
2f1e5e4d 916
9fc5adb0
VW
917 /* We started off locked to we need to lock the pool back */
918 spin_lock(&pool->lock);
9a001fc1
VW
919 }
920 spin_unlock(&pool->lock);
921 return -EAGAIN;
922}
923
924/**
925 * z3fold_map() - maps the allocation associated with the given handle
926 * @pool: pool in which the allocation resides
927 * @handle: handle associated with the allocation to be mapped
928 *
929 * Extracts the buddy number from handle and constructs the pointer to the
930 * correct starting chunk within the page.
931 *
932 * Returns: a pointer to the mapped allocation
933 */
934static void *z3fold_map(struct z3fold_pool *pool, unsigned long handle)
935{
936 struct z3fold_header *zhdr;
937 struct page *page;
938 void *addr;
939 enum buddy buddy;
940
9a001fc1
VW
941 zhdr = handle_to_z3fold_header(handle);
942 addr = zhdr;
943 page = virt_to_page(zhdr);
944
945 if (test_bit(PAGE_HEADLESS, &page->private))
946 goto out;
947
2f1e5e4d 948 z3fold_page_lock(zhdr);
9a001fc1
VW
949 buddy = handle_to_buddy(handle);
950 switch (buddy) {
951 case FIRST:
952 addr += ZHDR_SIZE_ALIGNED;
953 break;
954 case MIDDLE:
955 addr += zhdr->start_middle << CHUNK_SHIFT;
956 set_bit(MIDDLE_CHUNK_MAPPED, &page->private);
957 break;
958 case LAST:
959 addr += PAGE_SIZE - (zhdr->last_chunks << CHUNK_SHIFT);
960 break;
961 default:
962 pr_err("unknown buddy id %d\n", buddy);
963 WARN_ON(1);
964 addr = NULL;
965 break;
966 }
2f1e5e4d
VW
967
968 z3fold_page_unlock(zhdr);
9a001fc1 969out:
9a001fc1
VW
970 return addr;
971}
972
973/**
974 * z3fold_unmap() - unmaps the allocation associated with the given handle
975 * @pool: pool in which the allocation resides
976 * @handle: handle associated with the allocation to be unmapped
977 */
978static void z3fold_unmap(struct z3fold_pool *pool, unsigned long handle)
979{
980 struct z3fold_header *zhdr;
981 struct page *page;
982 enum buddy buddy;
983
9a001fc1
VW
984 zhdr = handle_to_z3fold_header(handle);
985 page = virt_to_page(zhdr);
986
2f1e5e4d 987 if (test_bit(PAGE_HEADLESS, &page->private))
9a001fc1 988 return;
9a001fc1 989
2f1e5e4d 990 z3fold_page_lock(zhdr);
9a001fc1
VW
991 buddy = handle_to_buddy(handle);
992 if (buddy == MIDDLE)
993 clear_bit(MIDDLE_CHUNK_MAPPED, &page->private);
2f1e5e4d 994 z3fold_page_unlock(zhdr);
9a001fc1
VW
995}
996
997/**
998 * z3fold_get_pool_size() - gets the z3fold pool size in pages
999 * @pool: pool whose size is being queried
1000 *
12d59ae6 1001 * Returns: size in pages of the given pool.
9a001fc1
VW
1002 */
1003static u64 z3fold_get_pool_size(struct z3fold_pool *pool)
1004{
12d59ae6 1005 return atomic64_read(&pool->pages_nr);
9a001fc1
VW
1006}
1007
1008/*****************
1009 * zpool
1010 ****************/
1011
1012static int z3fold_zpool_evict(struct z3fold_pool *pool, unsigned long handle)
1013{
1014 if (pool->zpool && pool->zpool_ops && pool->zpool_ops->evict)
1015 return pool->zpool_ops->evict(pool->zpool, handle);
1016 else
1017 return -ENOENT;
1018}
1019
1020static const struct z3fold_ops z3fold_zpool_ops = {
1021 .evict = z3fold_zpool_evict
1022};
1023
1024static void *z3fold_zpool_create(const char *name, gfp_t gfp,
1025 const struct zpool_ops *zpool_ops,
1026 struct zpool *zpool)
1027{
1028 struct z3fold_pool *pool;
1029
d30561c5
VW
1030 pool = z3fold_create_pool(name, gfp,
1031 zpool_ops ? &z3fold_zpool_ops : NULL);
9a001fc1
VW
1032 if (pool) {
1033 pool->zpool = zpool;
1034 pool->zpool_ops = zpool_ops;
1035 }
1036 return pool;
1037}
1038
1039static void z3fold_zpool_destroy(void *pool)
1040{
1041 z3fold_destroy_pool(pool);
1042}
1043
1044static int z3fold_zpool_malloc(void *pool, size_t size, gfp_t gfp,
1045 unsigned long *handle)
1046{
1047 return z3fold_alloc(pool, size, gfp, handle);
1048}
1049static void z3fold_zpool_free(void *pool, unsigned long handle)
1050{
1051 z3fold_free(pool, handle);
1052}
1053
1054static int z3fold_zpool_shrink(void *pool, unsigned int pages,
1055 unsigned int *reclaimed)
1056{
1057 unsigned int total = 0;
1058 int ret = -EINVAL;
1059
1060 while (total < pages) {
1061 ret = z3fold_reclaim_page(pool, 8);
1062 if (ret < 0)
1063 break;
1064 total++;
1065 }
1066
1067 if (reclaimed)
1068 *reclaimed = total;
1069
1070 return ret;
1071}
1072
1073static void *z3fold_zpool_map(void *pool, unsigned long handle,
1074 enum zpool_mapmode mm)
1075{
1076 return z3fold_map(pool, handle);
1077}
1078static void z3fold_zpool_unmap(void *pool, unsigned long handle)
1079{
1080 z3fold_unmap(pool, handle);
1081}
1082
1083static u64 z3fold_zpool_total_size(void *pool)
1084{
1085 return z3fold_get_pool_size(pool) * PAGE_SIZE;
1086}
1087
1088static struct zpool_driver z3fold_zpool_driver = {
1089 .type = "z3fold",
1090 .owner = THIS_MODULE,
1091 .create = z3fold_zpool_create,
1092 .destroy = z3fold_zpool_destroy,
1093 .malloc = z3fold_zpool_malloc,
1094 .free = z3fold_zpool_free,
1095 .shrink = z3fold_zpool_shrink,
1096 .map = z3fold_zpool_map,
1097 .unmap = z3fold_zpool_unmap,
1098 .total_size = z3fold_zpool_total_size,
1099};
1100
1101MODULE_ALIAS("zpool-z3fold");
1102
1103static int __init init_z3fold(void)
1104{
ede93213
VW
1105 /* Make sure the z3fold header is not larger than the page size */
1106 BUILD_BUG_ON(ZHDR_SIZE_ALIGNED > PAGE_SIZE);
9a001fc1
VW
1107 zpool_register_driver(&z3fold_zpool_driver);
1108
1109 return 0;
1110}
1111
1112static void __exit exit_z3fold(void)
1113{
1114 zpool_unregister_driver(&z3fold_zpool_driver);
1115}
1116
1117module_init(init_z3fold);
1118module_exit(exit_z3fold);
1119
1120MODULE_LICENSE("GPL");
1121MODULE_AUTHOR("Vitaly Wool <vitalywool@gmail.com>");
1122MODULE_DESCRIPTION("3-Fold Allocator for Compressed Pages");