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