]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - drivers/staging/zcache/zcache.c
Merge branch 'for-linus' of git://oss.sgi.com/xfs/xfs
[mirror_ubuntu-zesty-kernel.git] / drivers / staging / zcache / zcache.c
1 /*
2 * zcache.c
3 *
4 * Copyright (c) 2010,2011, Dan Magenheimer, Oracle Corp.
5 * Copyright (c) 2010,2011, Nitin Gupta
6 *
7 * Zcache provides an in-kernel "host implementation" for transcendent memory
8 * and, thus indirectly, for cleancache and frontswap. Zcache includes two
9 * page-accessible memory [1] interfaces, both utilizing lzo1x compression:
10 * 1) "compression buddies" ("zbud") is used for ephemeral pages
11 * 2) xvmalloc is used for persistent pages.
12 * Xvmalloc (based on the TLSF allocator) has very low fragmentation
13 * so maximizes space efficiency, while zbud allows pairs (and potentially,
14 * in the future, more than a pair of) compressed pages to be closely linked
15 * so that reclaiming can be done via the kernel's physical-page-oriented
16 * "shrinker" interface.
17 *
18 * [1] For a definition of page-accessible memory (aka PAM), see:
19 * http://marc.info/?l=linux-mm&m=127811271605009
20 */
21
22 #include <linux/cpu.h>
23 #include <linux/highmem.h>
24 #include <linux/list.h>
25 #include <linux/lzo.h>
26 #include <linux/slab.h>
27 #include <linux/spinlock.h>
28 #include <linux/types.h>
29 #include <linux/atomic.h>
30 #include "tmem.h"
31
32 #include "../zram/xvmalloc.h" /* if built in drivers/staging */
33
34 #if (!defined(CONFIG_CLEANCACHE) && !defined(CONFIG_FRONTSWAP))
35 #error "zcache is useless without CONFIG_CLEANCACHE or CONFIG_FRONTSWAP"
36 #endif
37 #ifdef CONFIG_CLEANCACHE
38 #include <linux/cleancache.h>
39 #endif
40 #ifdef CONFIG_FRONTSWAP
41 #include <linux/frontswap.h>
42 #endif
43
44 #if 0
45 /* this is more aggressive but may cause other problems? */
46 #define ZCACHE_GFP_MASK (GFP_ATOMIC | __GFP_NORETRY | __GFP_NOWARN)
47 #else
48 #define ZCACHE_GFP_MASK \
49 (__GFP_FS | __GFP_NORETRY | __GFP_NOWARN | __GFP_NOMEMALLOC)
50 #endif
51
52 /**********
53 * Compression buddies ("zbud") provides for packing two (or, possibly
54 * in the future, more) compressed ephemeral pages into a single "raw"
55 * (physical) page and tracking them with data structures so that
56 * the raw pages can be easily reclaimed.
57 *
58 * A zbud page ("zbpg") is an aligned page containing a list_head,
59 * a lock, and two "zbud headers". The remainder of the physical
60 * page is divided up into aligned 64-byte "chunks" which contain
61 * the compressed data for zero, one, or two zbuds. Each zbpg
62 * resides on: (1) an "unused list" if it has no zbuds; (2) a
63 * "buddied" list if it is fully populated with two zbuds; or
64 * (3) one of PAGE_SIZE/64 "unbuddied" lists indexed by how many chunks
65 * the one unbuddied zbud uses. The data inside a zbpg cannot be
66 * read or written unless the zbpg's lock is held.
67 */
68
69 #define ZBH_SENTINEL 0x43214321
70 #define ZBPG_SENTINEL 0xdeadbeef
71
72 #define ZBUD_MAX_BUDS 2
73
74 struct zbud_hdr {
75 uint32_t pool_id;
76 struct tmem_oid oid;
77 uint32_t index;
78 uint16_t size; /* compressed size in bytes, zero means unused */
79 DECL_SENTINEL
80 };
81
82 struct zbud_page {
83 struct list_head bud_list;
84 spinlock_t lock;
85 struct zbud_hdr buddy[ZBUD_MAX_BUDS];
86 DECL_SENTINEL
87 /* followed by NUM_CHUNK aligned CHUNK_SIZE-byte chunks */
88 };
89
90 #define CHUNK_SHIFT 6
91 #define CHUNK_SIZE (1 << CHUNK_SHIFT)
92 #define CHUNK_MASK (~(CHUNK_SIZE-1))
93 #define NCHUNKS (((PAGE_SIZE - sizeof(struct zbud_page)) & \
94 CHUNK_MASK) >> CHUNK_SHIFT)
95 #define MAX_CHUNK (NCHUNKS-1)
96
97 static struct {
98 struct list_head list;
99 unsigned count;
100 } zbud_unbuddied[NCHUNKS];
101 /* list N contains pages with N chunks USED and NCHUNKS-N unused */
102 /* element 0 is never used but optimizing that isn't worth it */
103 static unsigned long zbud_cumul_chunk_counts[NCHUNKS];
104
105 struct list_head zbud_buddied_list;
106 static unsigned long zcache_zbud_buddied_count;
107
108 /* protects the buddied list and all unbuddied lists */
109 static DEFINE_SPINLOCK(zbud_budlists_spinlock);
110
111 static LIST_HEAD(zbpg_unused_list);
112 static unsigned long zcache_zbpg_unused_list_count;
113
114 /* protects the unused page list */
115 static DEFINE_SPINLOCK(zbpg_unused_list_spinlock);
116
117 static atomic_t zcache_zbud_curr_raw_pages;
118 static atomic_t zcache_zbud_curr_zpages;
119 static unsigned long zcache_zbud_curr_zbytes;
120 static unsigned long zcache_zbud_cumul_zpages;
121 static unsigned long zcache_zbud_cumul_zbytes;
122 static unsigned long zcache_compress_poor;
123
124 /* forward references */
125 static void *zcache_get_free_page(void);
126 static void zcache_free_page(void *p);
127
128 /*
129 * zbud helper functions
130 */
131
132 static inline unsigned zbud_max_buddy_size(void)
133 {
134 return MAX_CHUNK << CHUNK_SHIFT;
135 }
136
137 static inline unsigned zbud_size_to_chunks(unsigned size)
138 {
139 BUG_ON(size == 0 || size > zbud_max_buddy_size());
140 return (size + CHUNK_SIZE - 1) >> CHUNK_SHIFT;
141 }
142
143 static inline int zbud_budnum(struct zbud_hdr *zh)
144 {
145 unsigned offset = (unsigned long)zh & (PAGE_SIZE - 1);
146 struct zbud_page *zbpg = NULL;
147 unsigned budnum = -1U;
148 int i;
149
150 for (i = 0; i < ZBUD_MAX_BUDS; i++)
151 if (offset == offsetof(typeof(*zbpg), buddy[i])) {
152 budnum = i;
153 break;
154 }
155 BUG_ON(budnum == -1U);
156 return budnum;
157 }
158
159 static char *zbud_data(struct zbud_hdr *zh, unsigned size)
160 {
161 struct zbud_page *zbpg;
162 char *p;
163 unsigned budnum;
164
165 ASSERT_SENTINEL(zh, ZBH);
166 budnum = zbud_budnum(zh);
167 BUG_ON(size == 0 || size > zbud_max_buddy_size());
168 zbpg = container_of(zh, struct zbud_page, buddy[budnum]);
169 ASSERT_SPINLOCK(&zbpg->lock);
170 p = (char *)zbpg;
171 if (budnum == 0)
172 p += ((sizeof(struct zbud_page) + CHUNK_SIZE - 1) &
173 CHUNK_MASK);
174 else if (budnum == 1)
175 p += PAGE_SIZE - ((size + CHUNK_SIZE - 1) & CHUNK_MASK);
176 return p;
177 }
178
179 /*
180 * zbud raw page management
181 */
182
183 static struct zbud_page *zbud_alloc_raw_page(void)
184 {
185 struct zbud_page *zbpg = NULL;
186 struct zbud_hdr *zh0, *zh1;
187 bool recycled = 0;
188
189 /* if any pages on the zbpg list, use one */
190 spin_lock(&zbpg_unused_list_spinlock);
191 if (!list_empty(&zbpg_unused_list)) {
192 zbpg = list_first_entry(&zbpg_unused_list,
193 struct zbud_page, bud_list);
194 list_del_init(&zbpg->bud_list);
195 zcache_zbpg_unused_list_count--;
196 recycled = 1;
197 }
198 spin_unlock(&zbpg_unused_list_spinlock);
199 if (zbpg == NULL)
200 /* none on zbpg list, try to get a kernel page */
201 zbpg = zcache_get_free_page();
202 if (likely(zbpg != NULL)) {
203 INIT_LIST_HEAD(&zbpg->bud_list);
204 zh0 = &zbpg->buddy[0]; zh1 = &zbpg->buddy[1];
205 spin_lock_init(&zbpg->lock);
206 if (recycled) {
207 ASSERT_INVERTED_SENTINEL(zbpg, ZBPG);
208 SET_SENTINEL(zbpg, ZBPG);
209 BUG_ON(zh0->size != 0 || tmem_oid_valid(&zh0->oid));
210 BUG_ON(zh1->size != 0 || tmem_oid_valid(&zh1->oid));
211 } else {
212 atomic_inc(&zcache_zbud_curr_raw_pages);
213 INIT_LIST_HEAD(&zbpg->bud_list);
214 SET_SENTINEL(zbpg, ZBPG);
215 zh0->size = 0; zh1->size = 0;
216 tmem_oid_set_invalid(&zh0->oid);
217 tmem_oid_set_invalid(&zh1->oid);
218 }
219 }
220 return zbpg;
221 }
222
223 static void zbud_free_raw_page(struct zbud_page *zbpg)
224 {
225 struct zbud_hdr *zh0 = &zbpg->buddy[0], *zh1 = &zbpg->buddy[1];
226
227 ASSERT_SENTINEL(zbpg, ZBPG);
228 BUG_ON(!list_empty(&zbpg->bud_list));
229 ASSERT_SPINLOCK(&zbpg->lock);
230 BUG_ON(zh0->size != 0 || tmem_oid_valid(&zh0->oid));
231 BUG_ON(zh1->size != 0 || tmem_oid_valid(&zh1->oid));
232 INVERT_SENTINEL(zbpg, ZBPG);
233 spin_unlock(&zbpg->lock);
234 spin_lock(&zbpg_unused_list_spinlock);
235 list_add(&zbpg->bud_list, &zbpg_unused_list);
236 zcache_zbpg_unused_list_count++;
237 spin_unlock(&zbpg_unused_list_spinlock);
238 }
239
240 /*
241 * core zbud handling routines
242 */
243
244 static unsigned zbud_free(struct zbud_hdr *zh)
245 {
246 unsigned size;
247
248 ASSERT_SENTINEL(zh, ZBH);
249 BUG_ON(!tmem_oid_valid(&zh->oid));
250 size = zh->size;
251 BUG_ON(zh->size == 0 || zh->size > zbud_max_buddy_size());
252 zh->size = 0;
253 tmem_oid_set_invalid(&zh->oid);
254 INVERT_SENTINEL(zh, ZBH);
255 zcache_zbud_curr_zbytes -= size;
256 atomic_dec(&zcache_zbud_curr_zpages);
257 return size;
258 }
259
260 static void zbud_free_and_delist(struct zbud_hdr *zh)
261 {
262 unsigned chunks;
263 struct zbud_hdr *zh_other;
264 unsigned budnum = zbud_budnum(zh), size;
265 struct zbud_page *zbpg =
266 container_of(zh, struct zbud_page, buddy[budnum]);
267
268 spin_lock(&zbpg->lock);
269 if (list_empty(&zbpg->bud_list)) {
270 /* ignore zombie page... see zbud_evict_pages() */
271 spin_unlock(&zbpg->lock);
272 return;
273 }
274 size = zbud_free(zh);
275 ASSERT_SPINLOCK(&zbpg->lock);
276 zh_other = &zbpg->buddy[(budnum == 0) ? 1 : 0];
277 if (zh_other->size == 0) { /* was unbuddied: unlist and free */
278 chunks = zbud_size_to_chunks(size) ;
279 spin_lock(&zbud_budlists_spinlock);
280 BUG_ON(list_empty(&zbud_unbuddied[chunks].list));
281 list_del_init(&zbpg->bud_list);
282 zbud_unbuddied[chunks].count--;
283 spin_unlock(&zbud_budlists_spinlock);
284 zbud_free_raw_page(zbpg);
285 } else { /* was buddied: move remaining buddy to unbuddied list */
286 chunks = zbud_size_to_chunks(zh_other->size) ;
287 spin_lock(&zbud_budlists_spinlock);
288 list_del_init(&zbpg->bud_list);
289 zcache_zbud_buddied_count--;
290 list_add_tail(&zbpg->bud_list, &zbud_unbuddied[chunks].list);
291 zbud_unbuddied[chunks].count++;
292 spin_unlock(&zbud_budlists_spinlock);
293 spin_unlock(&zbpg->lock);
294 }
295 }
296
297 static struct zbud_hdr *zbud_create(uint32_t pool_id, struct tmem_oid *oid,
298 uint32_t index, struct page *page,
299 void *cdata, unsigned size)
300 {
301 struct zbud_hdr *zh0, *zh1, *zh = NULL;
302 struct zbud_page *zbpg = NULL, *ztmp;
303 unsigned nchunks;
304 char *to;
305 int i, found_good_buddy = 0;
306
307 nchunks = zbud_size_to_chunks(size) ;
308 for (i = MAX_CHUNK - nchunks + 1; i > 0; i--) {
309 spin_lock(&zbud_budlists_spinlock);
310 if (!list_empty(&zbud_unbuddied[i].list)) {
311 list_for_each_entry_safe(zbpg, ztmp,
312 &zbud_unbuddied[i].list, bud_list) {
313 if (spin_trylock(&zbpg->lock)) {
314 found_good_buddy = i;
315 goto found_unbuddied;
316 }
317 }
318 }
319 spin_unlock(&zbud_budlists_spinlock);
320 }
321 /* didn't find a good buddy, try allocating a new page */
322 zbpg = zbud_alloc_raw_page();
323 if (unlikely(zbpg == NULL))
324 goto out;
325 /* ok, have a page, now compress the data before taking locks */
326 spin_lock(&zbpg->lock);
327 spin_lock(&zbud_budlists_spinlock);
328 list_add_tail(&zbpg->bud_list, &zbud_unbuddied[nchunks].list);
329 zbud_unbuddied[nchunks].count++;
330 zh = &zbpg->buddy[0];
331 goto init_zh;
332
333 found_unbuddied:
334 ASSERT_SPINLOCK(&zbpg->lock);
335 zh0 = &zbpg->buddy[0]; zh1 = &zbpg->buddy[1];
336 BUG_ON(!((zh0->size == 0) ^ (zh1->size == 0)));
337 if (zh0->size != 0) { /* buddy0 in use, buddy1 is vacant */
338 ASSERT_SENTINEL(zh0, ZBH);
339 zh = zh1;
340 } else if (zh1->size != 0) { /* buddy1 in use, buddy0 is vacant */
341 ASSERT_SENTINEL(zh1, ZBH);
342 zh = zh0;
343 } else
344 BUG();
345 list_del_init(&zbpg->bud_list);
346 zbud_unbuddied[found_good_buddy].count--;
347 list_add_tail(&zbpg->bud_list, &zbud_buddied_list);
348 zcache_zbud_buddied_count++;
349
350 init_zh:
351 SET_SENTINEL(zh, ZBH);
352 zh->size = size;
353 zh->index = index;
354 zh->oid = *oid;
355 zh->pool_id = pool_id;
356 /* can wait to copy the data until the list locks are dropped */
357 spin_unlock(&zbud_budlists_spinlock);
358
359 to = zbud_data(zh, size);
360 memcpy(to, cdata, size);
361 spin_unlock(&zbpg->lock);
362 zbud_cumul_chunk_counts[nchunks]++;
363 atomic_inc(&zcache_zbud_curr_zpages);
364 zcache_zbud_cumul_zpages++;
365 zcache_zbud_curr_zbytes += size;
366 zcache_zbud_cumul_zbytes += size;
367 out:
368 return zh;
369 }
370
371 static int zbud_decompress(struct page *page, struct zbud_hdr *zh)
372 {
373 struct zbud_page *zbpg;
374 unsigned budnum = zbud_budnum(zh);
375 size_t out_len = PAGE_SIZE;
376 char *to_va, *from_va;
377 unsigned size;
378 int ret = 0;
379
380 zbpg = container_of(zh, struct zbud_page, buddy[budnum]);
381 spin_lock(&zbpg->lock);
382 if (list_empty(&zbpg->bud_list)) {
383 /* ignore zombie page... see zbud_evict_pages() */
384 ret = -EINVAL;
385 goto out;
386 }
387 ASSERT_SENTINEL(zh, ZBH);
388 BUG_ON(zh->size == 0 || zh->size > zbud_max_buddy_size());
389 to_va = kmap_atomic(page, KM_USER0);
390 size = zh->size;
391 from_va = zbud_data(zh, size);
392 ret = lzo1x_decompress_safe(from_va, size, to_va, &out_len);
393 BUG_ON(ret != LZO_E_OK);
394 BUG_ON(out_len != PAGE_SIZE);
395 kunmap_atomic(to_va, KM_USER0);
396 out:
397 spin_unlock(&zbpg->lock);
398 return ret;
399 }
400
401 /*
402 * The following routines handle shrinking of ephemeral pages by evicting
403 * pages "least valuable" first.
404 */
405
406 static unsigned long zcache_evicted_raw_pages;
407 static unsigned long zcache_evicted_buddied_pages;
408 static unsigned long zcache_evicted_unbuddied_pages;
409
410 static struct tmem_pool *zcache_get_pool_by_id(uint32_t poolid);
411 static void zcache_put_pool(struct tmem_pool *pool);
412
413 /*
414 * Flush and free all zbuds in a zbpg, then free the pageframe
415 */
416 static void zbud_evict_zbpg(struct zbud_page *zbpg)
417 {
418 struct zbud_hdr *zh;
419 int i, j;
420 uint32_t pool_id[ZBUD_MAX_BUDS], index[ZBUD_MAX_BUDS];
421 struct tmem_oid oid[ZBUD_MAX_BUDS];
422 struct tmem_pool *pool;
423
424 ASSERT_SPINLOCK(&zbpg->lock);
425 BUG_ON(!list_empty(&zbpg->bud_list));
426 for (i = 0, j = 0; i < ZBUD_MAX_BUDS; i++) {
427 zh = &zbpg->buddy[i];
428 if (zh->size) {
429 pool_id[j] = zh->pool_id;
430 oid[j] = zh->oid;
431 index[j] = zh->index;
432 j++;
433 zbud_free(zh);
434 }
435 }
436 spin_unlock(&zbpg->lock);
437 for (i = 0; i < j; i++) {
438 pool = zcache_get_pool_by_id(pool_id[i]);
439 if (pool != NULL) {
440 tmem_flush_page(pool, &oid[i], index[i]);
441 zcache_put_pool(pool);
442 }
443 }
444 ASSERT_SENTINEL(zbpg, ZBPG);
445 spin_lock(&zbpg->lock);
446 zbud_free_raw_page(zbpg);
447 }
448
449 /*
450 * Free nr pages. This code is funky because we want to hold the locks
451 * protecting various lists for as short a time as possible, and in some
452 * circumstances the list may change asynchronously when the list lock is
453 * not held. In some cases we also trylock not only to avoid waiting on a
454 * page in use by another cpu, but also to avoid potential deadlock due to
455 * lock inversion.
456 */
457 static void zbud_evict_pages(int nr)
458 {
459 struct zbud_page *zbpg;
460 int i;
461
462 /* first try freeing any pages on unused list */
463 retry_unused_list:
464 spin_lock_bh(&zbpg_unused_list_spinlock);
465 if (!list_empty(&zbpg_unused_list)) {
466 /* can't walk list here, since it may change when unlocked */
467 zbpg = list_first_entry(&zbpg_unused_list,
468 struct zbud_page, bud_list);
469 list_del_init(&zbpg->bud_list);
470 zcache_zbpg_unused_list_count--;
471 atomic_dec(&zcache_zbud_curr_raw_pages);
472 spin_unlock_bh(&zbpg_unused_list_spinlock);
473 zcache_free_page(zbpg);
474 zcache_evicted_raw_pages++;
475 if (--nr <= 0)
476 goto out;
477 goto retry_unused_list;
478 }
479 spin_unlock_bh(&zbpg_unused_list_spinlock);
480
481 /* now try freeing unbuddied pages, starting with least space avail */
482 for (i = 0; i < MAX_CHUNK; i++) {
483 retry_unbud_list_i:
484 spin_lock_bh(&zbud_budlists_spinlock);
485 if (list_empty(&zbud_unbuddied[i].list)) {
486 spin_unlock_bh(&zbud_budlists_spinlock);
487 continue;
488 }
489 list_for_each_entry(zbpg, &zbud_unbuddied[i].list, bud_list) {
490 if (unlikely(!spin_trylock(&zbpg->lock)))
491 continue;
492 list_del_init(&zbpg->bud_list);
493 zbud_unbuddied[i].count--;
494 spin_unlock(&zbud_budlists_spinlock);
495 zcache_evicted_unbuddied_pages++;
496 /* want budlists unlocked when doing zbpg eviction */
497 zbud_evict_zbpg(zbpg);
498 local_bh_enable();
499 if (--nr <= 0)
500 goto out;
501 goto retry_unbud_list_i;
502 }
503 spin_unlock_bh(&zbud_budlists_spinlock);
504 }
505
506 /* as a last resort, free buddied pages */
507 retry_bud_list:
508 spin_lock_bh(&zbud_budlists_spinlock);
509 if (list_empty(&zbud_buddied_list)) {
510 spin_unlock_bh(&zbud_budlists_spinlock);
511 goto out;
512 }
513 list_for_each_entry(zbpg, &zbud_buddied_list, bud_list) {
514 if (unlikely(!spin_trylock(&zbpg->lock)))
515 continue;
516 list_del_init(&zbpg->bud_list);
517 zcache_zbud_buddied_count--;
518 spin_unlock(&zbud_budlists_spinlock);
519 zcache_evicted_buddied_pages++;
520 /* want budlists unlocked when doing zbpg eviction */
521 zbud_evict_zbpg(zbpg);
522 local_bh_enable();
523 if (--nr <= 0)
524 goto out;
525 goto retry_bud_list;
526 }
527 spin_unlock_bh(&zbud_budlists_spinlock);
528 out:
529 return;
530 }
531
532 static void zbud_init(void)
533 {
534 int i;
535
536 INIT_LIST_HEAD(&zbud_buddied_list);
537 zcache_zbud_buddied_count = 0;
538 for (i = 0; i < NCHUNKS; i++) {
539 INIT_LIST_HEAD(&zbud_unbuddied[i].list);
540 zbud_unbuddied[i].count = 0;
541 }
542 }
543
544 #ifdef CONFIG_SYSFS
545 /*
546 * These sysfs routines show a nice distribution of how many zbpg's are
547 * currently (and have ever been placed) in each unbuddied list. It's fun
548 * to watch but can probably go away before final merge.
549 */
550 static int zbud_show_unbuddied_list_counts(char *buf)
551 {
552 int i;
553 char *p = buf;
554
555 for (i = 0; i < NCHUNKS - 1; i++)
556 p += sprintf(p, "%u ", zbud_unbuddied[i].count);
557 p += sprintf(p, "%d\n", zbud_unbuddied[i].count);
558 return p - buf;
559 }
560
561 static int zbud_show_cumul_chunk_counts(char *buf)
562 {
563 unsigned long i, chunks = 0, total_chunks = 0, sum_total_chunks = 0;
564 unsigned long total_chunks_lte_21 = 0, total_chunks_lte_32 = 0;
565 unsigned long total_chunks_lte_42 = 0;
566 char *p = buf;
567
568 for (i = 0; i < NCHUNKS; i++) {
569 p += sprintf(p, "%lu ", zbud_cumul_chunk_counts[i]);
570 chunks += zbud_cumul_chunk_counts[i];
571 total_chunks += zbud_cumul_chunk_counts[i];
572 sum_total_chunks += i * zbud_cumul_chunk_counts[i];
573 if (i == 21)
574 total_chunks_lte_21 = total_chunks;
575 if (i == 32)
576 total_chunks_lte_32 = total_chunks;
577 if (i == 42)
578 total_chunks_lte_42 = total_chunks;
579 }
580 p += sprintf(p, "<=21:%lu <=32:%lu <=42:%lu, mean:%lu\n",
581 total_chunks_lte_21, total_chunks_lte_32, total_chunks_lte_42,
582 chunks == 0 ? 0 : sum_total_chunks / chunks);
583 return p - buf;
584 }
585 #endif
586
587 /**********
588 * This "zv" PAM implementation combines the TLSF-based xvMalloc
589 * with lzo1x compression to maximize the amount of data that can
590 * be packed into a physical page.
591 *
592 * Zv represents a PAM page with the index and object (plus a "size" value
593 * necessary for decompression) immediately preceding the compressed data.
594 */
595
596 #define ZVH_SENTINEL 0x43214321
597
598 struct zv_hdr {
599 uint32_t pool_id;
600 struct tmem_oid oid;
601 uint32_t index;
602 DECL_SENTINEL
603 };
604
605 static const int zv_max_page_size = (PAGE_SIZE / 8) * 7;
606
607 static struct zv_hdr *zv_create(struct xv_pool *xvpool, uint32_t pool_id,
608 struct tmem_oid *oid, uint32_t index,
609 void *cdata, unsigned clen)
610 {
611 struct page *page;
612 struct zv_hdr *zv = NULL;
613 uint32_t offset;
614 int ret;
615
616 BUG_ON(!irqs_disabled());
617 ret = xv_malloc(xvpool, clen + sizeof(struct zv_hdr),
618 &page, &offset, ZCACHE_GFP_MASK);
619 if (unlikely(ret))
620 goto out;
621 zv = kmap_atomic(page, KM_USER0) + offset;
622 zv->index = index;
623 zv->oid = *oid;
624 zv->pool_id = pool_id;
625 SET_SENTINEL(zv, ZVH);
626 memcpy((char *)zv + sizeof(struct zv_hdr), cdata, clen);
627 kunmap_atomic(zv, KM_USER0);
628 out:
629 return zv;
630 }
631
632 static void zv_free(struct xv_pool *xvpool, struct zv_hdr *zv)
633 {
634 unsigned long flags;
635 struct page *page;
636 uint32_t offset;
637 uint16_t size;
638
639 ASSERT_SENTINEL(zv, ZVH);
640 size = xv_get_object_size(zv) - sizeof(*zv);
641 BUG_ON(size == 0 || size > zv_max_page_size);
642 INVERT_SENTINEL(zv, ZVH);
643 page = virt_to_page(zv);
644 offset = (unsigned long)zv & ~PAGE_MASK;
645 local_irq_save(flags);
646 xv_free(xvpool, page, offset);
647 local_irq_restore(flags);
648 }
649
650 static void zv_decompress(struct page *page, struct zv_hdr *zv)
651 {
652 size_t clen = PAGE_SIZE;
653 char *to_va;
654 unsigned size;
655 int ret;
656
657 ASSERT_SENTINEL(zv, ZVH);
658 size = xv_get_object_size(zv) - sizeof(*zv);
659 BUG_ON(size == 0 || size > zv_max_page_size);
660 to_va = kmap_atomic(page, KM_USER0);
661 ret = lzo1x_decompress_safe((char *)zv + sizeof(*zv),
662 size, to_va, &clen);
663 kunmap_atomic(to_va, KM_USER0);
664 BUG_ON(ret != LZO_E_OK);
665 BUG_ON(clen != PAGE_SIZE);
666 }
667
668 /*
669 * zcache core code starts here
670 */
671
672 /* useful stats not collected by cleancache or frontswap */
673 static unsigned long zcache_flush_total;
674 static unsigned long zcache_flush_found;
675 static unsigned long zcache_flobj_total;
676 static unsigned long zcache_flobj_found;
677 static unsigned long zcache_failed_eph_puts;
678 static unsigned long zcache_failed_pers_puts;
679
680 #define MAX_POOLS_PER_CLIENT 16
681
682 static struct {
683 struct tmem_pool *tmem_pools[MAX_POOLS_PER_CLIENT];
684 struct xv_pool *xvpool;
685 } zcache_client;
686
687 /*
688 * Tmem operations assume the poolid implies the invoking client.
689 * Zcache only has one client (the kernel itself), so translate
690 * the poolid into the tmem_pool allocated for it. A KVM version
691 * of zcache would have one client per guest and each client might
692 * have a poolid==N.
693 */
694 static struct tmem_pool *zcache_get_pool_by_id(uint32_t poolid)
695 {
696 struct tmem_pool *pool = NULL;
697
698 if (poolid >= 0) {
699 pool = zcache_client.tmem_pools[poolid];
700 if (pool != NULL)
701 atomic_inc(&pool->refcount);
702 }
703 return pool;
704 }
705
706 static void zcache_put_pool(struct tmem_pool *pool)
707 {
708 if (pool != NULL)
709 atomic_dec(&pool->refcount);
710 }
711
712 /* counters for debugging */
713 static unsigned long zcache_failed_get_free_pages;
714 static unsigned long zcache_failed_alloc;
715 static unsigned long zcache_put_to_flush;
716 static unsigned long zcache_aborted_preload;
717 static unsigned long zcache_aborted_shrink;
718
719 /*
720 * Ensure that memory allocation requests in zcache don't result
721 * in direct reclaim requests via the shrinker, which would cause
722 * an infinite loop. Maybe a GFP flag would be better?
723 */
724 static DEFINE_SPINLOCK(zcache_direct_reclaim_lock);
725
726 /*
727 * for now, used named slabs so can easily track usage; later can
728 * either just use kmalloc, or perhaps add a slab-like allocator
729 * to more carefully manage total memory utilization
730 */
731 static struct kmem_cache *zcache_objnode_cache;
732 static struct kmem_cache *zcache_obj_cache;
733 static atomic_t zcache_curr_obj_count = ATOMIC_INIT(0);
734 static unsigned long zcache_curr_obj_count_max;
735 static atomic_t zcache_curr_objnode_count = ATOMIC_INIT(0);
736 static unsigned long zcache_curr_objnode_count_max;
737
738 /*
739 * to avoid memory allocation recursion (e.g. due to direct reclaim), we
740 * preload all necessary data structures so the hostops callbacks never
741 * actually do a malloc
742 */
743 struct zcache_preload {
744 void *page;
745 struct tmem_obj *obj;
746 int nr;
747 struct tmem_objnode *objnodes[OBJNODE_TREE_MAX_PATH];
748 };
749 static DEFINE_PER_CPU(struct zcache_preload, zcache_preloads) = { 0, };
750
751 static int zcache_do_preload(struct tmem_pool *pool)
752 {
753 struct zcache_preload *kp;
754 struct tmem_objnode *objnode;
755 struct tmem_obj *obj;
756 void *page;
757 int ret = -ENOMEM;
758
759 if (unlikely(zcache_objnode_cache == NULL))
760 goto out;
761 if (unlikely(zcache_obj_cache == NULL))
762 goto out;
763 if (!spin_trylock(&zcache_direct_reclaim_lock)) {
764 zcache_aborted_preload++;
765 goto out;
766 }
767 preempt_disable();
768 kp = &__get_cpu_var(zcache_preloads);
769 while (kp->nr < ARRAY_SIZE(kp->objnodes)) {
770 preempt_enable_no_resched();
771 objnode = kmem_cache_alloc(zcache_objnode_cache,
772 ZCACHE_GFP_MASK);
773 if (unlikely(objnode == NULL)) {
774 zcache_failed_alloc++;
775 goto unlock_out;
776 }
777 preempt_disable();
778 kp = &__get_cpu_var(zcache_preloads);
779 if (kp->nr < ARRAY_SIZE(kp->objnodes))
780 kp->objnodes[kp->nr++] = objnode;
781 else
782 kmem_cache_free(zcache_objnode_cache, objnode);
783 }
784 preempt_enable_no_resched();
785 obj = kmem_cache_alloc(zcache_obj_cache, ZCACHE_GFP_MASK);
786 if (unlikely(obj == NULL)) {
787 zcache_failed_alloc++;
788 goto unlock_out;
789 }
790 page = (void *)__get_free_page(ZCACHE_GFP_MASK);
791 if (unlikely(page == NULL)) {
792 zcache_failed_get_free_pages++;
793 kmem_cache_free(zcache_obj_cache, obj);
794 goto unlock_out;
795 }
796 preempt_disable();
797 kp = &__get_cpu_var(zcache_preloads);
798 if (kp->obj == NULL)
799 kp->obj = obj;
800 else
801 kmem_cache_free(zcache_obj_cache, obj);
802 if (kp->page == NULL)
803 kp->page = page;
804 else
805 free_page((unsigned long)page);
806 ret = 0;
807 unlock_out:
808 spin_unlock(&zcache_direct_reclaim_lock);
809 out:
810 return ret;
811 }
812
813 static void *zcache_get_free_page(void)
814 {
815 struct zcache_preload *kp;
816 void *page;
817
818 kp = &__get_cpu_var(zcache_preloads);
819 page = kp->page;
820 BUG_ON(page == NULL);
821 kp->page = NULL;
822 return page;
823 }
824
825 static void zcache_free_page(void *p)
826 {
827 free_page((unsigned long)p);
828 }
829
830 /*
831 * zcache implementation for tmem host ops
832 */
833
834 static struct tmem_objnode *zcache_objnode_alloc(struct tmem_pool *pool)
835 {
836 struct tmem_objnode *objnode = NULL;
837 unsigned long count;
838 struct zcache_preload *kp;
839
840 kp = &__get_cpu_var(zcache_preloads);
841 if (kp->nr <= 0)
842 goto out;
843 objnode = kp->objnodes[kp->nr - 1];
844 BUG_ON(objnode == NULL);
845 kp->objnodes[kp->nr - 1] = NULL;
846 kp->nr--;
847 count = atomic_inc_return(&zcache_curr_objnode_count);
848 if (count > zcache_curr_objnode_count_max)
849 zcache_curr_objnode_count_max = count;
850 out:
851 return objnode;
852 }
853
854 static void zcache_objnode_free(struct tmem_objnode *objnode,
855 struct tmem_pool *pool)
856 {
857 atomic_dec(&zcache_curr_objnode_count);
858 BUG_ON(atomic_read(&zcache_curr_objnode_count) < 0);
859 kmem_cache_free(zcache_objnode_cache, objnode);
860 }
861
862 static struct tmem_obj *zcache_obj_alloc(struct tmem_pool *pool)
863 {
864 struct tmem_obj *obj = NULL;
865 unsigned long count;
866 struct zcache_preload *kp;
867
868 kp = &__get_cpu_var(zcache_preloads);
869 obj = kp->obj;
870 BUG_ON(obj == NULL);
871 kp->obj = NULL;
872 count = atomic_inc_return(&zcache_curr_obj_count);
873 if (count > zcache_curr_obj_count_max)
874 zcache_curr_obj_count_max = count;
875 return obj;
876 }
877
878 static void zcache_obj_free(struct tmem_obj *obj, struct tmem_pool *pool)
879 {
880 atomic_dec(&zcache_curr_obj_count);
881 BUG_ON(atomic_read(&zcache_curr_obj_count) < 0);
882 kmem_cache_free(zcache_obj_cache, obj);
883 }
884
885 static struct tmem_hostops zcache_hostops = {
886 .obj_alloc = zcache_obj_alloc,
887 .obj_free = zcache_obj_free,
888 .objnode_alloc = zcache_objnode_alloc,
889 .objnode_free = zcache_objnode_free,
890 };
891
892 /*
893 * zcache implementations for PAM page descriptor ops
894 */
895
896 static atomic_t zcache_curr_eph_pampd_count = ATOMIC_INIT(0);
897 static unsigned long zcache_curr_eph_pampd_count_max;
898 static atomic_t zcache_curr_pers_pampd_count = ATOMIC_INIT(0);
899 static unsigned long zcache_curr_pers_pampd_count_max;
900
901 /* forward reference */
902 static int zcache_compress(struct page *from, void **out_va, size_t *out_len);
903
904 static void *zcache_pampd_create(struct tmem_pool *pool, struct tmem_oid *oid,
905 uint32_t index, struct page *page)
906 {
907 void *pampd = NULL, *cdata;
908 size_t clen;
909 int ret;
910 bool ephemeral = is_ephemeral(pool);
911 unsigned long count;
912
913 if (ephemeral) {
914 ret = zcache_compress(page, &cdata, &clen);
915 if (ret == 0)
916
917 goto out;
918 if (clen == 0 || clen > zbud_max_buddy_size()) {
919 zcache_compress_poor++;
920 goto out;
921 }
922 pampd = (void *)zbud_create(pool->pool_id, oid, index,
923 page, cdata, clen);
924 if (pampd != NULL) {
925 count = atomic_inc_return(&zcache_curr_eph_pampd_count);
926 if (count > zcache_curr_eph_pampd_count_max)
927 zcache_curr_eph_pampd_count_max = count;
928 }
929 } else {
930 /*
931 * FIXME: This is all the "policy" there is for now.
932 * 3/4 totpages should allow ~37% of RAM to be filled with
933 * compressed frontswap pages
934 */
935 if (atomic_read(&zcache_curr_pers_pampd_count) >
936 3 * totalram_pages / 4)
937 goto out;
938 ret = zcache_compress(page, &cdata, &clen);
939 if (ret == 0)
940 goto out;
941 if (clen > zv_max_page_size) {
942 zcache_compress_poor++;
943 goto out;
944 }
945 pampd = (void *)zv_create(zcache_client.xvpool, pool->pool_id,
946 oid, index, cdata, clen);
947 if (pampd == NULL)
948 goto out;
949 count = atomic_inc_return(&zcache_curr_pers_pampd_count);
950 if (count > zcache_curr_pers_pampd_count_max)
951 zcache_curr_pers_pampd_count_max = count;
952 }
953 out:
954 return pampd;
955 }
956
957 /*
958 * fill the pageframe corresponding to the struct page with the data
959 * from the passed pampd
960 */
961 static int zcache_pampd_get_data(struct page *page, void *pampd,
962 struct tmem_pool *pool)
963 {
964 int ret = 0;
965
966 if (is_ephemeral(pool))
967 ret = zbud_decompress(page, pampd);
968 else
969 zv_decompress(page, pampd);
970 return ret;
971 }
972
973 /*
974 * free the pampd and remove it from any zcache lists
975 * pampd must no longer be pointed to from any tmem data structures!
976 */
977 static void zcache_pampd_free(void *pampd, struct tmem_pool *pool)
978 {
979 if (is_ephemeral(pool)) {
980 zbud_free_and_delist((struct zbud_hdr *)pampd);
981 atomic_dec(&zcache_curr_eph_pampd_count);
982 BUG_ON(atomic_read(&zcache_curr_eph_pampd_count) < 0);
983 } else {
984 zv_free(zcache_client.xvpool, (struct zv_hdr *)pampd);
985 atomic_dec(&zcache_curr_pers_pampd_count);
986 BUG_ON(atomic_read(&zcache_curr_pers_pampd_count) < 0);
987 }
988 }
989
990 static struct tmem_pamops zcache_pamops = {
991 .create = zcache_pampd_create,
992 .get_data = zcache_pampd_get_data,
993 .free = zcache_pampd_free,
994 };
995
996 /*
997 * zcache compression/decompression and related per-cpu stuff
998 */
999
1000 #define LZO_WORKMEM_BYTES LZO1X_1_MEM_COMPRESS
1001 #define LZO_DSTMEM_PAGE_ORDER 1
1002 static DEFINE_PER_CPU(unsigned char *, zcache_workmem);
1003 static DEFINE_PER_CPU(unsigned char *, zcache_dstmem);
1004
1005 static int zcache_compress(struct page *from, void **out_va, size_t *out_len)
1006 {
1007 int ret = 0;
1008 unsigned char *dmem = __get_cpu_var(zcache_dstmem);
1009 unsigned char *wmem = __get_cpu_var(zcache_workmem);
1010 char *from_va;
1011
1012 BUG_ON(!irqs_disabled());
1013 if (unlikely(dmem == NULL || wmem == NULL))
1014 goto out; /* no buffer, so can't compress */
1015 from_va = kmap_atomic(from, KM_USER0);
1016 mb();
1017 ret = lzo1x_1_compress(from_va, PAGE_SIZE, dmem, out_len, wmem);
1018 BUG_ON(ret != LZO_E_OK);
1019 *out_va = dmem;
1020 kunmap_atomic(from_va, KM_USER0);
1021 ret = 1;
1022 out:
1023 return ret;
1024 }
1025
1026
1027 static int zcache_cpu_notifier(struct notifier_block *nb,
1028 unsigned long action, void *pcpu)
1029 {
1030 int cpu = (long)pcpu;
1031 struct zcache_preload *kp;
1032
1033 switch (action) {
1034 case CPU_UP_PREPARE:
1035 per_cpu(zcache_dstmem, cpu) = (void *)__get_free_pages(
1036 GFP_KERNEL | __GFP_REPEAT,
1037 LZO_DSTMEM_PAGE_ORDER),
1038 per_cpu(zcache_workmem, cpu) =
1039 kzalloc(LZO1X_MEM_COMPRESS,
1040 GFP_KERNEL | __GFP_REPEAT);
1041 break;
1042 case CPU_DEAD:
1043 case CPU_UP_CANCELED:
1044 free_pages((unsigned long)per_cpu(zcache_dstmem, cpu),
1045 LZO_DSTMEM_PAGE_ORDER);
1046 per_cpu(zcache_dstmem, cpu) = NULL;
1047 kfree(per_cpu(zcache_workmem, cpu));
1048 per_cpu(zcache_workmem, cpu) = NULL;
1049 kp = &per_cpu(zcache_preloads, cpu);
1050 while (kp->nr) {
1051 kmem_cache_free(zcache_objnode_cache,
1052 kp->objnodes[kp->nr - 1]);
1053 kp->objnodes[kp->nr - 1] = NULL;
1054 kp->nr--;
1055 }
1056 kmem_cache_free(zcache_obj_cache, kp->obj);
1057 free_page((unsigned long)kp->page);
1058 break;
1059 default:
1060 break;
1061 }
1062 return NOTIFY_OK;
1063 }
1064
1065 static struct notifier_block zcache_cpu_notifier_block = {
1066 .notifier_call = zcache_cpu_notifier
1067 };
1068
1069 #ifdef CONFIG_SYSFS
1070 #define ZCACHE_SYSFS_RO(_name) \
1071 static ssize_t zcache_##_name##_show(struct kobject *kobj, \
1072 struct kobj_attribute *attr, char *buf) \
1073 { \
1074 return sprintf(buf, "%lu\n", zcache_##_name); \
1075 } \
1076 static struct kobj_attribute zcache_##_name##_attr = { \
1077 .attr = { .name = __stringify(_name), .mode = 0444 }, \
1078 .show = zcache_##_name##_show, \
1079 }
1080
1081 #define ZCACHE_SYSFS_RO_ATOMIC(_name) \
1082 static ssize_t zcache_##_name##_show(struct kobject *kobj, \
1083 struct kobj_attribute *attr, char *buf) \
1084 { \
1085 return sprintf(buf, "%d\n", atomic_read(&zcache_##_name)); \
1086 } \
1087 static struct kobj_attribute zcache_##_name##_attr = { \
1088 .attr = { .name = __stringify(_name), .mode = 0444 }, \
1089 .show = zcache_##_name##_show, \
1090 }
1091
1092 #define ZCACHE_SYSFS_RO_CUSTOM(_name, _func) \
1093 static ssize_t zcache_##_name##_show(struct kobject *kobj, \
1094 struct kobj_attribute *attr, char *buf) \
1095 { \
1096 return _func(buf); \
1097 } \
1098 static struct kobj_attribute zcache_##_name##_attr = { \
1099 .attr = { .name = __stringify(_name), .mode = 0444 }, \
1100 .show = zcache_##_name##_show, \
1101 }
1102
1103 ZCACHE_SYSFS_RO(curr_obj_count_max);
1104 ZCACHE_SYSFS_RO(curr_objnode_count_max);
1105 ZCACHE_SYSFS_RO(flush_total);
1106 ZCACHE_SYSFS_RO(flush_found);
1107 ZCACHE_SYSFS_RO(flobj_total);
1108 ZCACHE_SYSFS_RO(flobj_found);
1109 ZCACHE_SYSFS_RO(failed_eph_puts);
1110 ZCACHE_SYSFS_RO(failed_pers_puts);
1111 ZCACHE_SYSFS_RO(zbud_curr_zbytes);
1112 ZCACHE_SYSFS_RO(zbud_cumul_zpages);
1113 ZCACHE_SYSFS_RO(zbud_cumul_zbytes);
1114 ZCACHE_SYSFS_RO(zbud_buddied_count);
1115 ZCACHE_SYSFS_RO(zbpg_unused_list_count);
1116 ZCACHE_SYSFS_RO(evicted_raw_pages);
1117 ZCACHE_SYSFS_RO(evicted_unbuddied_pages);
1118 ZCACHE_SYSFS_RO(evicted_buddied_pages);
1119 ZCACHE_SYSFS_RO(failed_get_free_pages);
1120 ZCACHE_SYSFS_RO(failed_alloc);
1121 ZCACHE_SYSFS_RO(put_to_flush);
1122 ZCACHE_SYSFS_RO(aborted_preload);
1123 ZCACHE_SYSFS_RO(aborted_shrink);
1124 ZCACHE_SYSFS_RO(compress_poor);
1125 ZCACHE_SYSFS_RO_ATOMIC(zbud_curr_raw_pages);
1126 ZCACHE_SYSFS_RO_ATOMIC(zbud_curr_zpages);
1127 ZCACHE_SYSFS_RO_ATOMIC(curr_obj_count);
1128 ZCACHE_SYSFS_RO_ATOMIC(curr_objnode_count);
1129 ZCACHE_SYSFS_RO_CUSTOM(zbud_unbuddied_list_counts,
1130 zbud_show_unbuddied_list_counts);
1131 ZCACHE_SYSFS_RO_CUSTOM(zbud_cumul_chunk_counts,
1132 zbud_show_cumul_chunk_counts);
1133
1134 static struct attribute *zcache_attrs[] = {
1135 &zcache_curr_obj_count_attr.attr,
1136 &zcache_curr_obj_count_max_attr.attr,
1137 &zcache_curr_objnode_count_attr.attr,
1138 &zcache_curr_objnode_count_max_attr.attr,
1139 &zcache_flush_total_attr.attr,
1140 &zcache_flobj_total_attr.attr,
1141 &zcache_flush_found_attr.attr,
1142 &zcache_flobj_found_attr.attr,
1143 &zcache_failed_eph_puts_attr.attr,
1144 &zcache_failed_pers_puts_attr.attr,
1145 &zcache_compress_poor_attr.attr,
1146 &zcache_zbud_curr_raw_pages_attr.attr,
1147 &zcache_zbud_curr_zpages_attr.attr,
1148 &zcache_zbud_curr_zbytes_attr.attr,
1149 &zcache_zbud_cumul_zpages_attr.attr,
1150 &zcache_zbud_cumul_zbytes_attr.attr,
1151 &zcache_zbud_buddied_count_attr.attr,
1152 &zcache_zbpg_unused_list_count_attr.attr,
1153 &zcache_evicted_raw_pages_attr.attr,
1154 &zcache_evicted_unbuddied_pages_attr.attr,
1155 &zcache_evicted_buddied_pages_attr.attr,
1156 &zcache_failed_get_free_pages_attr.attr,
1157 &zcache_failed_alloc_attr.attr,
1158 &zcache_put_to_flush_attr.attr,
1159 &zcache_aborted_preload_attr.attr,
1160 &zcache_aborted_shrink_attr.attr,
1161 &zcache_zbud_unbuddied_list_counts_attr.attr,
1162 &zcache_zbud_cumul_chunk_counts_attr.attr,
1163 NULL,
1164 };
1165
1166 static struct attribute_group zcache_attr_group = {
1167 .attrs = zcache_attrs,
1168 .name = "zcache",
1169 };
1170
1171 #endif /* CONFIG_SYSFS */
1172 /*
1173 * When zcache is disabled ("frozen"), pools can be created and destroyed,
1174 * but all puts (and thus all other operations that require memory allocation)
1175 * must fail. If zcache is unfrozen, accepts puts, then frozen again,
1176 * data consistency requires all puts while frozen to be converted into
1177 * flushes.
1178 */
1179 static bool zcache_freeze;
1180
1181 /*
1182 * zcache shrinker interface (only useful for ephemeral pages, so zbud only)
1183 */
1184 static int shrink_zcache_memory(struct shrinker *shrink, int nr, gfp_t gfp_mask)
1185 {
1186 int ret = -1;
1187
1188 if (nr >= 0) {
1189 if (!(gfp_mask & __GFP_FS))
1190 /* does this case really need to be skipped? */
1191 goto out;
1192 if (spin_trylock(&zcache_direct_reclaim_lock)) {
1193 zbud_evict_pages(nr);
1194 spin_unlock(&zcache_direct_reclaim_lock);
1195 } else
1196 zcache_aborted_shrink++;
1197 }
1198 ret = (int)atomic_read(&zcache_zbud_curr_raw_pages);
1199 out:
1200 return ret;
1201 }
1202
1203 static struct shrinker zcache_shrinker = {
1204 .shrink = shrink_zcache_memory,
1205 .seeks = DEFAULT_SEEKS,
1206 };
1207
1208 /*
1209 * zcache shims between cleancache/frontswap ops and tmem
1210 */
1211
1212 static int zcache_put_page(int pool_id, struct tmem_oid *oidp,
1213 uint32_t index, struct page *page)
1214 {
1215 struct tmem_pool *pool;
1216 int ret = -1;
1217
1218 BUG_ON(!irqs_disabled());
1219 pool = zcache_get_pool_by_id(pool_id);
1220 if (unlikely(pool == NULL))
1221 goto out;
1222 if (!zcache_freeze && zcache_do_preload(pool) == 0) {
1223 /* preload does preempt_disable on success */
1224 ret = tmem_put(pool, oidp, index, page);
1225 if (ret < 0) {
1226 if (is_ephemeral(pool))
1227 zcache_failed_eph_puts++;
1228 else
1229 zcache_failed_pers_puts++;
1230 }
1231 zcache_put_pool(pool);
1232 preempt_enable_no_resched();
1233 } else {
1234 zcache_put_to_flush++;
1235 if (atomic_read(&pool->obj_count) > 0)
1236 /* the put fails whether the flush succeeds or not */
1237 (void)tmem_flush_page(pool, oidp, index);
1238 zcache_put_pool(pool);
1239 }
1240 out:
1241 return ret;
1242 }
1243
1244 static int zcache_get_page(int pool_id, struct tmem_oid *oidp,
1245 uint32_t index, struct page *page)
1246 {
1247 struct tmem_pool *pool;
1248 int ret = -1;
1249 unsigned long flags;
1250
1251 local_irq_save(flags);
1252 pool = zcache_get_pool_by_id(pool_id);
1253 if (likely(pool != NULL)) {
1254 if (atomic_read(&pool->obj_count) > 0)
1255 ret = tmem_get(pool, oidp, index, page);
1256 zcache_put_pool(pool);
1257 }
1258 local_irq_restore(flags);
1259 return ret;
1260 }
1261
1262 static int zcache_flush_page(int pool_id, struct tmem_oid *oidp, uint32_t index)
1263 {
1264 struct tmem_pool *pool;
1265 int ret = -1;
1266 unsigned long flags;
1267
1268 local_irq_save(flags);
1269 zcache_flush_total++;
1270 pool = zcache_get_pool_by_id(pool_id);
1271 if (likely(pool != NULL)) {
1272 if (atomic_read(&pool->obj_count) > 0)
1273 ret = tmem_flush_page(pool, oidp, index);
1274 zcache_put_pool(pool);
1275 }
1276 if (ret >= 0)
1277 zcache_flush_found++;
1278 local_irq_restore(flags);
1279 return ret;
1280 }
1281
1282 static int zcache_flush_object(int pool_id, struct tmem_oid *oidp)
1283 {
1284 struct tmem_pool *pool;
1285 int ret = -1;
1286 unsigned long flags;
1287
1288 local_irq_save(flags);
1289 zcache_flobj_total++;
1290 pool = zcache_get_pool_by_id(pool_id);
1291 if (likely(pool != NULL)) {
1292 if (atomic_read(&pool->obj_count) > 0)
1293 ret = tmem_flush_object(pool, oidp);
1294 zcache_put_pool(pool);
1295 }
1296 if (ret >= 0)
1297 zcache_flobj_found++;
1298 local_irq_restore(flags);
1299 return ret;
1300 }
1301
1302 static int zcache_destroy_pool(int pool_id)
1303 {
1304 struct tmem_pool *pool = NULL;
1305 int ret = -1;
1306
1307 if (pool_id < 0)
1308 goto out;
1309 pool = zcache_client.tmem_pools[pool_id];
1310 if (pool == NULL)
1311 goto out;
1312 zcache_client.tmem_pools[pool_id] = NULL;
1313 /* wait for pool activity on other cpus to quiesce */
1314 while (atomic_read(&pool->refcount) != 0)
1315 ;
1316 local_bh_disable();
1317 ret = tmem_destroy_pool(pool);
1318 local_bh_enable();
1319 kfree(pool);
1320 pr_info("zcache: destroyed pool id=%d\n", pool_id);
1321 out:
1322 return ret;
1323 }
1324
1325 static int zcache_new_pool(uint32_t flags)
1326 {
1327 int poolid = -1;
1328 struct tmem_pool *pool;
1329
1330 pool = kmalloc(sizeof(struct tmem_pool), GFP_KERNEL);
1331 if (pool == NULL) {
1332 pr_info("zcache: pool creation failed: out of memory\n");
1333 goto out;
1334 }
1335
1336 for (poolid = 0; poolid < MAX_POOLS_PER_CLIENT; poolid++)
1337 if (zcache_client.tmem_pools[poolid] == NULL)
1338 break;
1339 if (poolid >= MAX_POOLS_PER_CLIENT) {
1340 pr_info("zcache: pool creation failed: max exceeded\n");
1341 kfree(pool);
1342 poolid = -1;
1343 goto out;
1344 }
1345 atomic_set(&pool->refcount, 0);
1346 pool->client = &zcache_client;
1347 pool->pool_id = poolid;
1348 tmem_new_pool(pool, flags);
1349 zcache_client.tmem_pools[poolid] = pool;
1350 pr_info("zcache: created %s tmem pool, id=%d\n",
1351 flags & TMEM_POOL_PERSIST ? "persistent" : "ephemeral",
1352 poolid);
1353 out:
1354 return poolid;
1355 }
1356
1357 /**********
1358 * Two kernel functionalities currently can be layered on top of tmem.
1359 * These are "cleancache" which is used as a second-chance cache for clean
1360 * page cache pages; and "frontswap" which is used for swap pages
1361 * to avoid writes to disk. A generic "shim" is provided here for each
1362 * to translate in-kernel semantics to zcache semantics.
1363 */
1364
1365 #ifdef CONFIG_CLEANCACHE
1366 static void zcache_cleancache_put_page(int pool_id,
1367 struct cleancache_filekey key,
1368 pgoff_t index, struct page *page)
1369 {
1370 u32 ind = (u32) index;
1371 struct tmem_oid oid = *(struct tmem_oid *)&key;
1372
1373 if (likely(ind == index))
1374 (void)zcache_put_page(pool_id, &oid, index, page);
1375 }
1376
1377 static int zcache_cleancache_get_page(int pool_id,
1378 struct cleancache_filekey key,
1379 pgoff_t index, struct page *page)
1380 {
1381 u32 ind = (u32) index;
1382 struct tmem_oid oid = *(struct tmem_oid *)&key;
1383 int ret = -1;
1384
1385 if (likely(ind == index))
1386 ret = zcache_get_page(pool_id, &oid, index, page);
1387 return ret;
1388 }
1389
1390 static void zcache_cleancache_flush_page(int pool_id,
1391 struct cleancache_filekey key,
1392 pgoff_t index)
1393 {
1394 u32 ind = (u32) index;
1395 struct tmem_oid oid = *(struct tmem_oid *)&key;
1396
1397 if (likely(ind == index))
1398 (void)zcache_flush_page(pool_id, &oid, ind);
1399 }
1400
1401 static void zcache_cleancache_flush_inode(int pool_id,
1402 struct cleancache_filekey key)
1403 {
1404 struct tmem_oid oid = *(struct tmem_oid *)&key;
1405
1406 (void)zcache_flush_object(pool_id, &oid);
1407 }
1408
1409 static void zcache_cleancache_flush_fs(int pool_id)
1410 {
1411 if (pool_id >= 0)
1412 (void)zcache_destroy_pool(pool_id);
1413 }
1414
1415 static int zcache_cleancache_init_fs(size_t pagesize)
1416 {
1417 BUG_ON(sizeof(struct cleancache_filekey) !=
1418 sizeof(struct tmem_oid));
1419 BUG_ON(pagesize != PAGE_SIZE);
1420 return zcache_new_pool(0);
1421 }
1422
1423 static int zcache_cleancache_init_shared_fs(char *uuid, size_t pagesize)
1424 {
1425 /* shared pools are unsupported and map to private */
1426 BUG_ON(sizeof(struct cleancache_filekey) !=
1427 sizeof(struct tmem_oid));
1428 BUG_ON(pagesize != PAGE_SIZE);
1429 return zcache_new_pool(0);
1430 }
1431
1432 static struct cleancache_ops zcache_cleancache_ops = {
1433 .put_page = zcache_cleancache_put_page,
1434 .get_page = zcache_cleancache_get_page,
1435 .flush_page = zcache_cleancache_flush_page,
1436 .flush_inode = zcache_cleancache_flush_inode,
1437 .flush_fs = zcache_cleancache_flush_fs,
1438 .init_shared_fs = zcache_cleancache_init_shared_fs,
1439 .init_fs = zcache_cleancache_init_fs
1440 };
1441
1442 struct cleancache_ops zcache_cleancache_register_ops(void)
1443 {
1444 struct cleancache_ops old_ops =
1445 cleancache_register_ops(&zcache_cleancache_ops);
1446
1447 return old_ops;
1448 }
1449 #endif
1450
1451 #ifdef CONFIG_FRONTSWAP
1452 /* a single tmem poolid is used for all frontswap "types" (swapfiles) */
1453 static int zcache_frontswap_poolid = -1;
1454
1455 /*
1456 * Swizzling increases objects per swaptype, increasing tmem concurrency
1457 * for heavy swaploads. Later, larger nr_cpus -> larger SWIZ_BITS
1458 */
1459 #define SWIZ_BITS 4
1460 #define SWIZ_MASK ((1 << SWIZ_BITS) - 1)
1461 #define _oswiz(_type, _ind) ((_type << SWIZ_BITS) | (_ind & SWIZ_MASK))
1462 #define iswiz(_ind) (_ind >> SWIZ_BITS)
1463
1464 static inline struct tmem_oid oswiz(unsigned type, u32 ind)
1465 {
1466 struct tmem_oid oid = { .oid = { 0 } };
1467 oid.oid[0] = _oswiz(type, ind);
1468 return oid;
1469 }
1470
1471 static int zcache_frontswap_put_page(unsigned type, pgoff_t offset,
1472 struct page *page)
1473 {
1474 u64 ind64 = (u64)offset;
1475 u32 ind = (u32)offset;
1476 struct tmem_oid oid = oswiz(type, ind);
1477 int ret = -1;
1478 unsigned long flags;
1479
1480 BUG_ON(!PageLocked(page));
1481 if (likely(ind64 == ind)) {
1482 local_irq_save(flags);
1483 ret = zcache_put_page(zcache_frontswap_poolid, &oid,
1484 iswiz(ind), page);
1485 local_irq_restore(flags);
1486 }
1487 return ret;
1488 }
1489
1490 /* returns 0 if the page was successfully gotten from frontswap, -1 if
1491 * was not present (should never happen!) */
1492 static int zcache_frontswap_get_page(unsigned type, pgoff_t offset,
1493 struct page *page)
1494 {
1495 u64 ind64 = (u64)offset;
1496 u32 ind = (u32)offset;
1497 struct tmem_oid oid = oswiz(type, ind);
1498 int ret = -1;
1499
1500 BUG_ON(!PageLocked(page));
1501 if (likely(ind64 == ind))
1502 ret = zcache_get_page(zcache_frontswap_poolid, &oid,
1503 iswiz(ind), page);
1504 return ret;
1505 }
1506
1507 /* flush a single page from frontswap */
1508 static void zcache_frontswap_flush_page(unsigned type, pgoff_t offset)
1509 {
1510 u64 ind64 = (u64)offset;
1511 u32 ind = (u32)offset;
1512 struct tmem_oid oid = oswiz(type, ind);
1513
1514 if (likely(ind64 == ind))
1515 (void)zcache_flush_page(zcache_frontswap_poolid, &oid,
1516 iswiz(ind));
1517 }
1518
1519 /* flush all pages from the passed swaptype */
1520 static void zcache_frontswap_flush_area(unsigned type)
1521 {
1522 struct tmem_oid oid;
1523 int ind;
1524
1525 for (ind = SWIZ_MASK; ind >= 0; ind--) {
1526 oid = oswiz(type, ind);
1527 (void)zcache_flush_object(zcache_frontswap_poolid, &oid);
1528 }
1529 }
1530
1531 static void zcache_frontswap_init(unsigned ignored)
1532 {
1533 /* a single tmem poolid is used for all frontswap "types" (swapfiles) */
1534 if (zcache_frontswap_poolid < 0)
1535 zcache_frontswap_poolid = zcache_new_pool(TMEM_POOL_PERSIST);
1536 }
1537
1538 static struct frontswap_ops zcache_frontswap_ops = {
1539 .put_page = zcache_frontswap_put_page,
1540 .get_page = zcache_frontswap_get_page,
1541 .flush_page = zcache_frontswap_flush_page,
1542 .flush_area = zcache_frontswap_flush_area,
1543 .init = zcache_frontswap_init
1544 };
1545
1546 struct frontswap_ops zcache_frontswap_register_ops(void)
1547 {
1548 struct frontswap_ops old_ops =
1549 frontswap_register_ops(&zcache_frontswap_ops);
1550
1551 return old_ops;
1552 }
1553 #endif
1554
1555 /*
1556 * zcache initialization
1557 * NOTE FOR NOW zcache MUST BE PROVIDED AS A KERNEL BOOT PARAMETER OR
1558 * NOTHING HAPPENS!
1559 */
1560
1561 static int zcache_enabled;
1562
1563 static int __init enable_zcache(char *s)
1564 {
1565 zcache_enabled = 1;
1566 return 1;
1567 }
1568 __setup("zcache", enable_zcache);
1569
1570 /* allow independent dynamic disabling of cleancache and frontswap */
1571
1572 static int use_cleancache = 1;
1573
1574 static int __init no_cleancache(char *s)
1575 {
1576 use_cleancache = 0;
1577 return 1;
1578 }
1579
1580 __setup("nocleancache", no_cleancache);
1581
1582 static int use_frontswap = 1;
1583
1584 static int __init no_frontswap(char *s)
1585 {
1586 use_frontswap = 0;
1587 return 1;
1588 }
1589
1590 __setup("nofrontswap", no_frontswap);
1591
1592 static int __init zcache_init(void)
1593 {
1594 #ifdef CONFIG_SYSFS
1595 int ret = 0;
1596
1597 ret = sysfs_create_group(mm_kobj, &zcache_attr_group);
1598 if (ret) {
1599 pr_err("zcache: can't create sysfs\n");
1600 goto out;
1601 }
1602 #endif /* CONFIG_SYSFS */
1603 #if defined(CONFIG_CLEANCACHE) || defined(CONFIG_FRONTSWAP)
1604 if (zcache_enabled) {
1605 unsigned int cpu;
1606
1607 tmem_register_hostops(&zcache_hostops);
1608 tmem_register_pamops(&zcache_pamops);
1609 ret = register_cpu_notifier(&zcache_cpu_notifier_block);
1610 if (ret) {
1611 pr_err("zcache: can't register cpu notifier\n");
1612 goto out;
1613 }
1614 for_each_online_cpu(cpu) {
1615 void *pcpu = (void *)(long)cpu;
1616 zcache_cpu_notifier(&zcache_cpu_notifier_block,
1617 CPU_UP_PREPARE, pcpu);
1618 }
1619 }
1620 zcache_objnode_cache = kmem_cache_create("zcache_objnode",
1621 sizeof(struct tmem_objnode), 0, 0, NULL);
1622 zcache_obj_cache = kmem_cache_create("zcache_obj",
1623 sizeof(struct tmem_obj), 0, 0, NULL);
1624 #endif
1625 #ifdef CONFIG_CLEANCACHE
1626 if (zcache_enabled && use_cleancache) {
1627 struct cleancache_ops old_ops;
1628
1629 zbud_init();
1630 register_shrinker(&zcache_shrinker);
1631 old_ops = zcache_cleancache_register_ops();
1632 pr_info("zcache: cleancache enabled using kernel "
1633 "transcendent memory and compression buddies\n");
1634 if (old_ops.init_fs != NULL)
1635 pr_warning("zcache: cleancache_ops overridden");
1636 }
1637 #endif
1638 #ifdef CONFIG_FRONTSWAP
1639 if (zcache_enabled && use_frontswap) {
1640 struct frontswap_ops old_ops;
1641
1642 zcache_client.xvpool = xv_create_pool();
1643 if (zcache_client.xvpool == NULL) {
1644 pr_err("zcache: can't create xvpool\n");
1645 goto out;
1646 }
1647 old_ops = zcache_frontswap_register_ops();
1648 pr_info("zcache: frontswap enabled using kernel "
1649 "transcendent memory and xvmalloc\n");
1650 if (old_ops.init != NULL)
1651 pr_warning("ktmem: frontswap_ops overridden");
1652 }
1653 #endif
1654 out:
1655 return ret;
1656 }
1657
1658 module_init(zcache_init)