]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - mm/z3fold.c
x86/speculation: Rework speculative_store_bypass_update()
[mirror_ubuntu-artful-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>
26#include <linux/list.h>
27#include <linux/mm.h>
28#include <linux/module.h>
29#include <linux/preempt.h>
30#include <linux/slab.h>
31#include <linux/spinlock.h>
32#include <linux/zpool.h>
33
34/*****************
35 * Structures
36*****************/
ede93213
VW
37struct z3fold_pool;
38struct z3fold_ops {
39 int (*evict)(struct z3fold_pool *pool, unsigned long handle);
40};
41
42enum buddy {
43 HEADLESS = 0,
44 FIRST,
45 MIDDLE,
46 LAST,
47 BUDDIES_MAX
48};
49
50/*
51 * struct z3fold_header - z3fold page metadata occupying the first chunk of each
52 * z3fold page, except for HEADLESS pages
53 * @buddy: links the z3fold page into the relevant list in the pool
2f1e5e4d 54 * @page_lock: per-page lock
5a27aa82 55 * @refcount: reference cound for the z3fold page
ede93213
VW
56 * @first_chunks: the size of the first buddy in chunks, 0 if free
57 * @middle_chunks: the size of the middle buddy in chunks, 0 if free
58 * @last_chunks: the size of the last buddy in chunks, 0 if free
59 * @first_num: the starting number (for the first handle)
60 */
61struct z3fold_header {
62 struct list_head buddy;
2f1e5e4d 63 spinlock_t page_lock;
5a27aa82 64 struct kref refcount;
ede93213
VW
65 unsigned short first_chunks;
66 unsigned short middle_chunks;
67 unsigned short last_chunks;
68 unsigned short start_middle;
69 unsigned short first_num:2;
70};
71
9a001fc1
VW
72/*
73 * NCHUNKS_ORDER determines the internal allocation granularity, effectively
74 * adjusting internal fragmentation. It also determines the number of
75 * freelists maintained in each pool. NCHUNKS_ORDER of 6 means that the
ede93213
VW
76 * allocation granularity will be in chunks of size PAGE_SIZE/64. Some chunks
77 * in the beginning of an allocated page are occupied by z3fold header, so
78 * NCHUNKS will be calculated to 63 (or 62 in case CONFIG_DEBUG_SPINLOCK=y),
79 * which shows the max number of free chunks in z3fold page, also there will
80 * be 63, or 62, respectively, freelists per pool.
9a001fc1
VW
81 */
82#define NCHUNKS_ORDER 6
83
84#define CHUNK_SHIFT (PAGE_SHIFT - NCHUNKS_ORDER)
85#define CHUNK_SIZE (1 << CHUNK_SHIFT)
ede93213
VW
86#define ZHDR_SIZE_ALIGNED round_up(sizeof(struct z3fold_header), CHUNK_SIZE)
87#define ZHDR_CHUNKS (ZHDR_SIZE_ALIGNED >> CHUNK_SHIFT)
88#define TOTAL_CHUNKS (PAGE_SIZE >> CHUNK_SHIFT)
9a001fc1
VW
89#define NCHUNKS ((PAGE_SIZE - ZHDR_SIZE_ALIGNED) >> CHUNK_SHIFT)
90
f201ebd8 91#define BUDDY_MASK (0x3)
9a001fc1 92
9a001fc1
VW
93/**
94 * struct z3fold_pool - stores metadata for each z3fold pool
95 * @lock: protects all pool fields and first|last_chunk fields of any
96 * z3fold page in the pool
97 * @unbuddied: array of lists tracking z3fold pages that contain 2- buddies;
98 * the lists each z3fold page is added to depends on the size of
99 * its free region.
9a001fc1
VW
100 * @lru: list tracking the z3fold pages in LRU order by most recently
101 * added buddy.
102 * @pages_nr: number of z3fold pages in the pool.
103 * @ops: pointer to a structure of user defined operations specified at
104 * pool creation time.
105 *
106 * This structure is allocated at pool creation time and maintains metadata
107 * pertaining to a particular z3fold pool.
108 */
109struct z3fold_pool {
110 spinlock_t lock;
111 struct list_head unbuddied[NCHUNKS];
9a001fc1 112 struct list_head lru;
12d59ae6 113 atomic64_t pages_nr;
9a001fc1
VW
114 const struct z3fold_ops *ops;
115 struct zpool *zpool;
116 const struct zpool_ops *zpool_ops;
117};
118
9a001fc1
VW
119/*
120 * Internal z3fold page flags
121 */
122enum z3fold_page_flags {
5a27aa82 123 PAGE_HEADLESS = 0,
9a001fc1
VW
124 MIDDLE_CHUNK_MAPPED,
125};
126
ede93213 127
9a001fc1
VW
128/*****************
129 * Helpers
130*****************/
131
132/* Converts an allocation size in bytes to size in z3fold chunks */
133static int size_to_chunks(size_t size)
134{
135 return (size + CHUNK_SIZE - 1) >> CHUNK_SHIFT;
136}
137
138#define for_each_unbuddied_list(_iter, _begin) \
139 for ((_iter) = (_begin); (_iter) < NCHUNKS; (_iter)++)
140
141/* Initializes the z3fold header of a newly allocated z3fold page */
142static struct z3fold_header *init_z3fold_page(struct page *page)
143{
144 struct z3fold_header *zhdr = page_address(page);
145
146 INIT_LIST_HEAD(&page->lru);
9a001fc1
VW
147 clear_bit(PAGE_HEADLESS, &page->private);
148 clear_bit(MIDDLE_CHUNK_MAPPED, &page->private);
149
2f1e5e4d 150 spin_lock_init(&zhdr->page_lock);
5a27aa82 151 kref_init(&zhdr->refcount);
9a001fc1
VW
152 zhdr->first_chunks = 0;
153 zhdr->middle_chunks = 0;
154 zhdr->last_chunks = 0;
155 zhdr->first_num = 0;
156 zhdr->start_middle = 0;
157 INIT_LIST_HEAD(&zhdr->buddy);
158 return zhdr;
159}
160
161/* Resets the struct page fields and frees the page */
5a27aa82 162static void free_z3fold_page(struct page *page)
9a001fc1 163{
5a27aa82
VW
164 __free_page(page);
165}
166
167static void release_z3fold_page(struct kref *ref)
168{
169 struct z3fold_header *zhdr;
170 struct page *page;
171
172 zhdr = container_of(ref, struct z3fold_header, refcount);
173 page = virt_to_page(zhdr);
174
175 if (!list_empty(&zhdr->buddy))
176 list_del(&zhdr->buddy);
177 if (!list_empty(&page->lru))
178 list_del(&page->lru);
179 free_z3fold_page(page);
9a001fc1
VW
180}
181
2f1e5e4d
VW
182/* Lock a z3fold page */
183static inline void z3fold_page_lock(struct z3fold_header *zhdr)
184{
185 spin_lock(&zhdr->page_lock);
186}
187
76e32a2a
VW
188/* Try to lock a z3fold page */
189static inline int z3fold_page_trylock(struct z3fold_header *zhdr)
190{
191 return spin_trylock(&zhdr->page_lock);
192}
193
2f1e5e4d
VW
194/* Unlock a z3fold page */
195static inline void z3fold_page_unlock(struct z3fold_header *zhdr)
196{
197 spin_unlock(&zhdr->page_lock);
198}
199
9a001fc1
VW
200/*
201 * Encodes the handle of a particular buddy within a z3fold page
202 * Pool lock should be held as this function accesses first_num
203 */
204static unsigned long encode_handle(struct z3fold_header *zhdr, enum buddy bud)
205{
206 unsigned long handle;
207
208 handle = (unsigned long)zhdr;
209 if (bud != HEADLESS)
210 handle += (bud + zhdr->first_num) & BUDDY_MASK;
211 return handle;
212}
213
214/* Returns the z3fold page where a given handle is stored */
215static struct z3fold_header *handle_to_z3fold_header(unsigned long handle)
216{
217 return (struct z3fold_header *)(handle & PAGE_MASK);
218}
219
f201ebd8 220/*
221 * (handle & BUDDY_MASK) < zhdr->first_num is possible in encode_handle
222 * but that doesn't matter. because the masking will result in the
223 * correct buddy number.
224 */
9a001fc1
VW
225static enum buddy handle_to_buddy(unsigned long handle)
226{
227 struct z3fold_header *zhdr = handle_to_z3fold_header(handle);
228 return (handle - zhdr->first_num) & BUDDY_MASK;
229}
230
231/*
232 * Returns the number of free chunks in a z3fold page.
233 * NB: can't be used with HEADLESS pages.
234 */
235static int num_free_chunks(struct z3fold_header *zhdr)
236{
237 int nfree;
238 /*
239 * If there is a middle object, pick up the bigger free space
240 * either before or after it. Otherwise just subtract the number
241 * of chunks occupied by the first and the last objects.
242 */
243 if (zhdr->middle_chunks != 0) {
244 int nfree_before = zhdr->first_chunks ?
ede93213 245 0 : zhdr->start_middle - ZHDR_CHUNKS;
9a001fc1 246 int nfree_after = zhdr->last_chunks ?
ede93213
VW
247 0 : TOTAL_CHUNKS -
248 (zhdr->start_middle + zhdr->middle_chunks);
9a001fc1
VW
249 nfree = max(nfree_before, nfree_after);
250 } else
251 nfree = NCHUNKS - zhdr->first_chunks - zhdr->last_chunks;
252 return nfree;
253}
254
255/*****************
256 * API Functions
257*****************/
258/**
259 * z3fold_create_pool() - create a new z3fold pool
260 * @gfp: gfp flags when allocating the z3fold pool structure
261 * @ops: user-defined operations for the z3fold pool
262 *
263 * Return: pointer to the new z3fold pool or NULL if the metadata allocation
264 * failed.
265 */
266static struct z3fold_pool *z3fold_create_pool(gfp_t gfp,
267 const struct z3fold_ops *ops)
268{
269 struct z3fold_pool *pool;
270 int i;
271
272 pool = kzalloc(sizeof(struct z3fold_pool), gfp);
273 if (!pool)
274 return NULL;
275 spin_lock_init(&pool->lock);
276 for_each_unbuddied_list(i, 0)
277 INIT_LIST_HEAD(&pool->unbuddied[i]);
9a001fc1 278 INIT_LIST_HEAD(&pool->lru);
12d59ae6 279 atomic64_set(&pool->pages_nr, 0);
9a001fc1
VW
280 pool->ops = ops;
281 return pool;
282}
283
284/**
285 * z3fold_destroy_pool() - destroys an existing z3fold pool
286 * @pool: the z3fold pool to be destroyed
287 *
288 * The pool should be emptied before this function is called.
289 */
290static void z3fold_destroy_pool(struct z3fold_pool *pool)
291{
292 kfree(pool);
293}
294
ede93213
VW
295static inline void *mchunk_memmove(struct z3fold_header *zhdr,
296 unsigned short dst_chunk)
297{
298 void *beg = zhdr;
299 return memmove(beg + (dst_chunk << CHUNK_SHIFT),
300 beg + (zhdr->start_middle << CHUNK_SHIFT),
301 zhdr->middle_chunks << CHUNK_SHIFT);
302}
303
1b096e5a 304#define BIG_CHUNK_GAP 3
9a001fc1
VW
305/* Has to be called with lock held */
306static int z3fold_compact_page(struct z3fold_header *zhdr)
307{
308 struct page *page = virt_to_page(zhdr);
9a001fc1 309
ede93213
VW
310 if (test_bit(MIDDLE_CHUNK_MAPPED, &page->private))
311 return 0; /* can't move middle chunk, it's used */
9a001fc1 312
ede93213
VW
313 if (zhdr->middle_chunks == 0)
314 return 0; /* nothing to compact */
315
316 if (zhdr->first_chunks == 0 && zhdr->last_chunks == 0) {
317 /* move to the beginning */
318 mchunk_memmove(zhdr, ZHDR_CHUNKS);
9a001fc1
VW
319 zhdr->first_chunks = zhdr->middle_chunks;
320 zhdr->middle_chunks = 0;
321 zhdr->start_middle = 0;
322 zhdr->first_num++;
1b096e5a 323 return 1;
9a001fc1 324 }
1b096e5a
VW
325
326 /*
327 * moving data is expensive, so let's only do that if
328 * there's substantial gain (at least BIG_CHUNK_GAP chunks)
329 */
330 if (zhdr->first_chunks != 0 && zhdr->last_chunks == 0 &&
331 zhdr->start_middle - (zhdr->first_chunks + ZHDR_CHUNKS) >=
332 BIG_CHUNK_GAP) {
333 mchunk_memmove(zhdr, zhdr->first_chunks + ZHDR_CHUNKS);
334 zhdr->start_middle = zhdr->first_chunks + ZHDR_CHUNKS;
335 return 1;
336 } else if (zhdr->last_chunks != 0 && zhdr->first_chunks == 0 &&
337 TOTAL_CHUNKS - (zhdr->last_chunks + zhdr->start_middle
338 + zhdr->middle_chunks) >=
339 BIG_CHUNK_GAP) {
340 unsigned short new_start = TOTAL_CHUNKS - zhdr->last_chunks -
341 zhdr->middle_chunks;
342 mchunk_memmove(zhdr, new_start);
343 zhdr->start_middle = new_start;
344 return 1;
345 }
346
347 return 0;
9a001fc1
VW
348}
349
350/**
351 * z3fold_alloc() - allocates a region of a given size
352 * @pool: z3fold pool from which to allocate
353 * @size: size in bytes of the desired allocation
354 * @gfp: gfp flags used if the pool needs to grow
355 * @handle: handle of the new allocation
356 *
357 * This function will attempt to find a free region in the pool large enough to
358 * satisfy the allocation request. A search of the unbuddied lists is
359 * performed first. If no suitable free region is found, then a new page is
360 * allocated and added to the pool to satisfy the request.
361 *
362 * gfp should not set __GFP_HIGHMEM as highmem pages cannot be used
363 * as z3fold pool pages.
364 *
365 * Return: 0 if success and handle is set, otherwise -EINVAL if the size or
366 * gfp arguments are invalid or -ENOMEM if the pool was unable to allocate
367 * a new page.
368 */
369static int z3fold_alloc(struct z3fold_pool *pool, size_t size, gfp_t gfp,
370 unsigned long *handle)
371{
372 int chunks = 0, i, freechunks;
373 struct z3fold_header *zhdr = NULL;
374 enum buddy bud;
375 struct page *page;
376
377 if (!size || (gfp & __GFP_HIGHMEM))
378 return -EINVAL;
379
380 if (size > PAGE_SIZE)
381 return -ENOSPC;
382
383 if (size > PAGE_SIZE - ZHDR_SIZE_ALIGNED - CHUNK_SIZE)
384 bud = HEADLESS;
385 else {
386 chunks = size_to_chunks(size);
9a001fc1
VW
387
388 /* First, try to find an unbuddied z3fold page. */
389 zhdr = NULL;
390 for_each_unbuddied_list(i, chunks) {
2f1e5e4d
VW
391 spin_lock(&pool->lock);
392 zhdr = list_first_entry_or_null(&pool->unbuddied[i],
9a001fc1 393 struct z3fold_header, buddy);
76e32a2a 394 if (!zhdr || !z3fold_page_trylock(zhdr)) {
2f1e5e4d
VW
395 spin_unlock(&pool->lock);
396 continue;
397 }
5a27aa82 398 kref_get(&zhdr->refcount);
2f1e5e4d
VW
399 list_del_init(&zhdr->buddy);
400 spin_unlock(&pool->lock);
401
402 page = virt_to_page(zhdr);
2f1e5e4d
VW
403 if (zhdr->first_chunks == 0) {
404 if (zhdr->middle_chunks != 0 &&
405 chunks >= zhdr->start_middle)
9a001fc1 406 bud = LAST;
2f1e5e4d
VW
407 else
408 bud = FIRST;
409 } else if (zhdr->last_chunks == 0)
410 bud = LAST;
411 else if (zhdr->middle_chunks == 0)
412 bud = MIDDLE;
413 else {
5a27aa82 414 z3fold_page_unlock(zhdr);
2f1e5e4d 415 spin_lock(&pool->lock);
5a27aa82
VW
416 if (kref_put(&zhdr->refcount,
417 release_z3fold_page))
418 atomic64_dec(&pool->pages_nr);
2f1e5e4d 419 spin_unlock(&pool->lock);
2f1e5e4d
VW
420 pr_err("No free chunks in unbuddied\n");
421 WARN_ON(1);
422 continue;
9a001fc1 423 }
2f1e5e4d 424 goto found;
9a001fc1
VW
425 }
426 bud = FIRST;
9a001fc1
VW
427 }
428
429 /* Couldn't find unbuddied z3fold page, create new one */
430 page = alloc_page(gfp);
431 if (!page)
432 return -ENOMEM;
2f1e5e4d 433
12d59ae6 434 atomic64_inc(&pool->pages_nr);
9a001fc1
VW
435 zhdr = init_z3fold_page(page);
436
437 if (bud == HEADLESS) {
438 set_bit(PAGE_HEADLESS, &page->private);
2f1e5e4d 439 spin_lock(&pool->lock);
9a001fc1
VW
440 goto headless;
441 }
2f1e5e4d 442 z3fold_page_lock(zhdr);
9a001fc1
VW
443
444found:
445 if (bud == FIRST)
446 zhdr->first_chunks = chunks;
447 else if (bud == LAST)
448 zhdr->last_chunks = chunks;
449 else {
450 zhdr->middle_chunks = chunks;
ede93213 451 zhdr->start_middle = zhdr->first_chunks + ZHDR_CHUNKS;
9a001fc1
VW
452 }
453
2f1e5e4d 454 spin_lock(&pool->lock);
9a001fc1
VW
455 if (zhdr->first_chunks == 0 || zhdr->last_chunks == 0 ||
456 zhdr->middle_chunks == 0) {
457 /* Add to unbuddied list */
458 freechunks = num_free_chunks(zhdr);
459 list_add(&zhdr->buddy, &pool->unbuddied[freechunks]);
9a001fc1
VW
460 }
461
462headless:
463 /* Add/move z3fold page to beginning of LRU */
464 if (!list_empty(&page->lru))
465 list_del(&page->lru);
466
467 list_add(&page->lru, &pool->lru);
468
469 *handle = encode_handle(zhdr, bud);
470 spin_unlock(&pool->lock);
2f1e5e4d
VW
471 if (bud != HEADLESS)
472 z3fold_page_unlock(zhdr);
9a001fc1
VW
473
474 return 0;
475}
476
477/**
478 * z3fold_free() - frees the allocation associated with the given handle
479 * @pool: pool in which the allocation resided
480 * @handle: handle associated with the allocation returned by z3fold_alloc()
481 *
482 * In the case that the z3fold page in which the allocation resides is under
483 * reclaim, as indicated by the PG_reclaim flag being set, this function
484 * only sets the first|last_chunks to 0. The page is actually freed
485 * once both buddies are evicted (see z3fold_reclaim_page() below).
486 */
487static void z3fold_free(struct z3fold_pool *pool, unsigned long handle)
488{
489 struct z3fold_header *zhdr;
490 int freechunks;
491 struct page *page;
492 enum buddy bud;
493
9a001fc1
VW
494 zhdr = handle_to_z3fold_header(handle);
495 page = virt_to_page(zhdr);
496
497 if (test_bit(PAGE_HEADLESS, &page->private)) {
498 /* HEADLESS page stored */
499 bud = HEADLESS;
500 } else {
2f1e5e4d 501 z3fold_page_lock(zhdr);
43afc194 502 bud = handle_to_buddy(handle);
9a001fc1
VW
503
504 switch (bud) {
505 case FIRST:
506 zhdr->first_chunks = 0;
507 break;
508 case MIDDLE:
509 zhdr->middle_chunks = 0;
510 zhdr->start_middle = 0;
511 break;
512 case LAST:
513 zhdr->last_chunks = 0;
514 break;
515 default:
516 pr_err("%s: unknown bud %d\n", __func__, bud);
517 WARN_ON(1);
2f1e5e4d 518 z3fold_page_unlock(zhdr);
9a001fc1
VW
519 return;
520 }
521 }
522
5a27aa82 523 if (bud == HEADLESS) {
2f1e5e4d 524 spin_lock(&pool->lock);
9a001fc1 525 list_del(&page->lru);
2f1e5e4d 526 spin_unlock(&pool->lock);
5a27aa82 527 free_z3fold_page(page);
12d59ae6 528 atomic64_dec(&pool->pages_nr);
9a001fc1 529 } else {
5a27aa82
VW
530 if (zhdr->first_chunks != 0 || zhdr->middle_chunks != 0 ||
531 zhdr->last_chunks != 0) {
532 z3fold_compact_page(zhdr);
533 /* Add to the unbuddied list */
534 spin_lock(&pool->lock);
535 if (!list_empty(&zhdr->buddy))
536 list_del(&zhdr->buddy);
537 freechunks = num_free_chunks(zhdr);
538 list_add(&zhdr->buddy, &pool->unbuddied[freechunks]);
539 spin_unlock(&pool->lock);
540 }
541 z3fold_page_unlock(zhdr);
2f1e5e4d 542 spin_lock(&pool->lock);
5a27aa82
VW
543 if (kref_put(&zhdr->refcount, release_z3fold_page))
544 atomic64_dec(&pool->pages_nr);
2f1e5e4d 545 spin_unlock(&pool->lock);
9a001fc1
VW
546 }
547
9a001fc1
VW
548}
549
550/**
551 * z3fold_reclaim_page() - evicts allocations from a pool page and frees it
552 * @pool: pool from which a page will attempt to be evicted
553 * @retires: number of pages on the LRU list for which eviction will
554 * be attempted before failing
555 *
556 * z3fold reclaim is different from normal system reclaim in that it is done
557 * from the bottom, up. This is because only the bottom layer, z3fold, has
558 * information on how the allocations are organized within each z3fold page.
559 * This has the potential to create interesting locking situations between
560 * z3fold and the user, however.
561 *
562 * To avoid these, this is how z3fold_reclaim_page() should be called:
563
564 * The user detects a page should be reclaimed and calls z3fold_reclaim_page().
565 * z3fold_reclaim_page() will remove a z3fold page from the pool LRU list and
566 * call the user-defined eviction handler with the pool and handle as
567 * arguments.
568 *
569 * If the handle can not be evicted, the eviction handler should return
570 * non-zero. z3fold_reclaim_page() will add the z3fold page back to the
571 * appropriate list and try the next z3fold page on the LRU up to
572 * a user defined number of retries.
573 *
574 * If the handle is successfully evicted, the eviction handler should
575 * return 0 _and_ should have called z3fold_free() on the handle. z3fold_free()
576 * contains logic to delay freeing the page if the page is under reclaim,
577 * as indicated by the setting of the PG_reclaim flag on the underlying page.
578 *
579 * If all buddies in the z3fold page are successfully evicted, then the
580 * z3fold page can be freed.
581 *
582 * Returns: 0 if page is successfully freed, otherwise -EINVAL if there are
583 * no pages to evict or an eviction handler is not registered, -EAGAIN if
584 * the retry limit was hit.
585 */
586static int z3fold_reclaim_page(struct z3fold_pool *pool, unsigned int retries)
587{
588 int i, ret = 0, freechunks;
589 struct z3fold_header *zhdr;
590 struct page *page;
591 unsigned long first_handle = 0, middle_handle = 0, last_handle = 0;
592
593 spin_lock(&pool->lock);
2f1e5e4d 594 if (!pool->ops || !pool->ops->evict || retries == 0) {
9a001fc1
VW
595 spin_unlock(&pool->lock);
596 return -EINVAL;
597 }
598 for (i = 0; i < retries; i++) {
2f1e5e4d
VW
599 if (list_empty(&pool->lru)) {
600 spin_unlock(&pool->lock);
601 return -EINVAL;
602 }
9a001fc1 603 page = list_last_entry(&pool->lru, struct page, lru);
5a27aa82 604 list_del_init(&page->lru);
9a001fc1 605
9a001fc1
VW
606 zhdr = page_address(page);
607 if (!test_bit(PAGE_HEADLESS, &page->private)) {
5a27aa82
VW
608 if (!list_empty(&zhdr->buddy))
609 list_del_init(&zhdr->buddy);
610 kref_get(&zhdr->refcount);
2f1e5e4d
VW
611 spin_unlock(&pool->lock);
612 z3fold_page_lock(zhdr);
9a001fc1
VW
613 /*
614 * We need encode the handles before unlocking, since
615 * we can race with free that will set
616 * (first|last)_chunks to 0
617 */
618 first_handle = 0;
619 last_handle = 0;
620 middle_handle = 0;
621 if (zhdr->first_chunks)
622 first_handle = encode_handle(zhdr, FIRST);
623 if (zhdr->middle_chunks)
624 middle_handle = encode_handle(zhdr, MIDDLE);
625 if (zhdr->last_chunks)
626 last_handle = encode_handle(zhdr, LAST);
2f1e5e4d 627 z3fold_page_unlock(zhdr);
9a001fc1
VW
628 } else {
629 first_handle = encode_handle(zhdr, HEADLESS);
630 last_handle = middle_handle = 0;
2f1e5e4d 631 spin_unlock(&pool->lock);
9a001fc1
VW
632 }
633
9a001fc1
VW
634 /* Issue the eviction callback(s) */
635 if (middle_handle) {
636 ret = pool->ops->evict(pool, middle_handle);
637 if (ret)
638 goto next;
639 }
640 if (first_handle) {
641 ret = pool->ops->evict(pool, first_handle);
642 if (ret)
643 goto next;
644 }
645 if (last_handle) {
646 ret = pool->ops->evict(pool, last_handle);
647 if (ret)
648 goto next;
649 }
650next:
5a27aa82
VW
651 if (test_bit(PAGE_HEADLESS, &page->private)) {
652 if (ret == 0) {
653 free_z3fold_page(page);
654 return 0;
43afc194 655 } else {
5a27aa82
VW
656 spin_lock(&pool->lock);
657 }
658 } else {
659 z3fold_page_lock(zhdr);
660 if ((zhdr->first_chunks || zhdr->last_chunks ||
661 zhdr->middle_chunks) &&
662 !(zhdr->first_chunks && zhdr->last_chunks &&
663 zhdr->middle_chunks)) {
43afc194
VW
664 z3fold_compact_page(zhdr);
665 /* add to unbuddied list */
2f1e5e4d 666 spin_lock(&pool->lock);
43afc194
VW
667 freechunks = num_free_chunks(zhdr);
668 list_add(&zhdr->buddy,
669 &pool->unbuddied[freechunks]);
2f1e5e4d 670 spin_unlock(&pool->lock);
43afc194 671 }
2f1e5e4d 672 z3fold_page_unlock(zhdr);
5a27aa82
VW
673 spin_lock(&pool->lock);
674 if (kref_put(&zhdr->refcount, release_z3fold_page)) {
271df90e 675 spin_unlock(&pool->lock);
5a27aa82
VW
676 atomic64_dec(&pool->pages_nr);
677 return 0;
678 }
679 }
2f1e5e4d 680
5a27aa82
VW
681 /*
682 * Add to the beginning of LRU.
683 * Pool lock has to be kept here to ensure the page has
684 * not already been released
685 */
9a001fc1
VW
686 list_add(&page->lru, &pool->lru);
687 }
688 spin_unlock(&pool->lock);
689 return -EAGAIN;
690}
691
692/**
693 * z3fold_map() - maps the allocation associated with the given handle
694 * @pool: pool in which the allocation resides
695 * @handle: handle associated with the allocation to be mapped
696 *
697 * Extracts the buddy number from handle and constructs the pointer to the
698 * correct starting chunk within the page.
699 *
700 * Returns: a pointer to the mapped allocation
701 */
702static void *z3fold_map(struct z3fold_pool *pool, unsigned long handle)
703{
704 struct z3fold_header *zhdr;
705 struct page *page;
706 void *addr;
707 enum buddy buddy;
708
9a001fc1
VW
709 zhdr = handle_to_z3fold_header(handle);
710 addr = zhdr;
711 page = virt_to_page(zhdr);
712
713 if (test_bit(PAGE_HEADLESS, &page->private))
714 goto out;
715
2f1e5e4d 716 z3fold_page_lock(zhdr);
9a001fc1
VW
717 buddy = handle_to_buddy(handle);
718 switch (buddy) {
719 case FIRST:
720 addr += ZHDR_SIZE_ALIGNED;
721 break;
722 case MIDDLE:
723 addr += zhdr->start_middle << CHUNK_SHIFT;
724 set_bit(MIDDLE_CHUNK_MAPPED, &page->private);
725 break;
726 case LAST:
727 addr += PAGE_SIZE - (zhdr->last_chunks << CHUNK_SHIFT);
728 break;
729 default:
730 pr_err("unknown buddy id %d\n", buddy);
731 WARN_ON(1);
732 addr = NULL;
733 break;
734 }
2f1e5e4d
VW
735
736 z3fold_page_unlock(zhdr);
9a001fc1 737out:
9a001fc1
VW
738 return addr;
739}
740
741/**
742 * z3fold_unmap() - unmaps the allocation associated with the given handle
743 * @pool: pool in which the allocation resides
744 * @handle: handle associated with the allocation to be unmapped
745 */
746static void z3fold_unmap(struct z3fold_pool *pool, unsigned long handle)
747{
748 struct z3fold_header *zhdr;
749 struct page *page;
750 enum buddy buddy;
751
9a001fc1
VW
752 zhdr = handle_to_z3fold_header(handle);
753 page = virt_to_page(zhdr);
754
2f1e5e4d 755 if (test_bit(PAGE_HEADLESS, &page->private))
9a001fc1 756 return;
9a001fc1 757
2f1e5e4d 758 z3fold_page_lock(zhdr);
9a001fc1
VW
759 buddy = handle_to_buddy(handle);
760 if (buddy == MIDDLE)
761 clear_bit(MIDDLE_CHUNK_MAPPED, &page->private);
2f1e5e4d 762 z3fold_page_unlock(zhdr);
9a001fc1
VW
763}
764
765/**
766 * z3fold_get_pool_size() - gets the z3fold pool size in pages
767 * @pool: pool whose size is being queried
768 *
12d59ae6 769 * Returns: size in pages of the given pool.
9a001fc1
VW
770 */
771static u64 z3fold_get_pool_size(struct z3fold_pool *pool)
772{
12d59ae6 773 return atomic64_read(&pool->pages_nr);
9a001fc1
VW
774}
775
776/*****************
777 * zpool
778 ****************/
779
780static int z3fold_zpool_evict(struct z3fold_pool *pool, unsigned long handle)
781{
782 if (pool->zpool && pool->zpool_ops && pool->zpool_ops->evict)
783 return pool->zpool_ops->evict(pool->zpool, handle);
784 else
785 return -ENOENT;
786}
787
788static const struct z3fold_ops z3fold_zpool_ops = {
789 .evict = z3fold_zpool_evict
790};
791
792static void *z3fold_zpool_create(const char *name, gfp_t gfp,
793 const struct zpool_ops *zpool_ops,
794 struct zpool *zpool)
795{
796 struct z3fold_pool *pool;
797
798 pool = z3fold_create_pool(gfp, zpool_ops ? &z3fold_zpool_ops : NULL);
799 if (pool) {
800 pool->zpool = zpool;
801 pool->zpool_ops = zpool_ops;
802 }
803 return pool;
804}
805
806static void z3fold_zpool_destroy(void *pool)
807{
808 z3fold_destroy_pool(pool);
809}
810
811static int z3fold_zpool_malloc(void *pool, size_t size, gfp_t gfp,
812 unsigned long *handle)
813{
814 return z3fold_alloc(pool, size, gfp, handle);
815}
816static void z3fold_zpool_free(void *pool, unsigned long handle)
817{
818 z3fold_free(pool, handle);
819}
820
821static int z3fold_zpool_shrink(void *pool, unsigned int pages,
822 unsigned int *reclaimed)
823{
824 unsigned int total = 0;
825 int ret = -EINVAL;
826
827 while (total < pages) {
828 ret = z3fold_reclaim_page(pool, 8);
829 if (ret < 0)
830 break;
831 total++;
832 }
833
834 if (reclaimed)
835 *reclaimed = total;
836
837 return ret;
838}
839
840static void *z3fold_zpool_map(void *pool, unsigned long handle,
841 enum zpool_mapmode mm)
842{
843 return z3fold_map(pool, handle);
844}
845static void z3fold_zpool_unmap(void *pool, unsigned long handle)
846{
847 z3fold_unmap(pool, handle);
848}
849
850static u64 z3fold_zpool_total_size(void *pool)
851{
852 return z3fold_get_pool_size(pool) * PAGE_SIZE;
853}
854
855static struct zpool_driver z3fold_zpool_driver = {
856 .type = "z3fold",
857 .owner = THIS_MODULE,
858 .create = z3fold_zpool_create,
859 .destroy = z3fold_zpool_destroy,
860 .malloc = z3fold_zpool_malloc,
861 .free = z3fold_zpool_free,
862 .shrink = z3fold_zpool_shrink,
863 .map = z3fold_zpool_map,
864 .unmap = z3fold_zpool_unmap,
865 .total_size = z3fold_zpool_total_size,
866};
867
868MODULE_ALIAS("zpool-z3fold");
869
870static int __init init_z3fold(void)
871{
ede93213
VW
872 /* Make sure the z3fold header is not larger than the page size */
873 BUILD_BUG_ON(ZHDR_SIZE_ALIGNED > PAGE_SIZE);
9a001fc1
VW
874 zpool_register_driver(&z3fold_zpool_driver);
875
876 return 0;
877}
878
879static void __exit exit_z3fold(void)
880{
881 zpool_unregister_driver(&z3fold_zpool_driver);
882}
883
884module_init(init_z3fold);
885module_exit(exit_z3fold);
886
887MODULE_LICENSE("GPL");
888MODULE_AUTHOR("Vitaly Wool <vitalywool@gmail.com>");
889MODULE_DESCRIPTION("3-Fold Allocator for Compressed Pages");