Blame view
mm/zswap.c
24.1 KB
2b2811178 zswap: add to mm/ |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
/* * zswap.c - zswap driver file * * zswap is a backend for frontswap that takes pages that are in the process * of being swapped out and attempts to compress and store them in a * RAM-based memory pool. This can result in a significant I/O reduction on * the swap device and, in the case where decompressing from RAM is faster * than reading from the swap device, can also improve workload performance. * * Copyright (C) 2012 Seth Jennings <sjenning@linux.vnet.ibm.com> * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. */ #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt #include <linux/module.h> #include <linux/cpu.h> #include <linux/highmem.h> #include <linux/slab.h> #include <linux/spinlock.h> #include <linux/types.h> #include <linux/atomic.h> #include <linux/frontswap.h> #include <linux/rbtree.h> #include <linux/swap.h> #include <linux/crypto.h> #include <linux/mempool.h> #include <linux/zbud.h> #include <linux/mm_types.h> #include <linux/page-flags.h> #include <linux/swapops.h> #include <linux/writeback.h> #include <linux/pagemap.h> /********************************* * statistics **********************************/ /* Number of memory pages used by the compressed pool */ static u64 zswap_pool_pages; /* The number of compressed pages currently stored in zswap */ static atomic_t zswap_stored_pages = ATOMIC_INIT(0); /* * The statistics below are not protected from concurrent access for * performance reasons so they may not be a 100% accurate. However, * they do provide useful information on roughly how many times a * certain event is occurring. */ /* Pool limit was hit (see zswap_max_pool_percent) */ static u64 zswap_pool_limit_hit; /* Pages written back when pool limit was reached */ static u64 zswap_written_back_pages; /* Store failed due to a reclaim failure after pool limit was reached */ static u64 zswap_reject_reclaim_fail; /* Compressed page was too big for the allocator to (optimally) store */ static u64 zswap_reject_compress_poor; /* Store failed because underlying allocator could not get memory */ static u64 zswap_reject_alloc_fail; /* Store failed because the entry metadata could not be allocated (rare) */ static u64 zswap_reject_kmemcache_fail; /* Duplicate store was encountered (rare) */ static u64 zswap_duplicate_entry; /********************************* * tunables **********************************/ /* Enable/disable zswap (disabled by default, fixed at boot for now) */ static bool zswap_enabled __read_mostly; |
12ab028be mm/zswap.c: chang... |
80 |
module_param_named(enabled, zswap_enabled, bool, 0444); |
2b2811178 zswap: add to mm/ |
81 82 83 84 |
/* Compressor to be used by zswap (fixed at boot for now) */ #define ZSWAP_COMPRESSOR_DEFAULT "lzo" static char *zswap_compressor = ZSWAP_COMPRESSOR_DEFAULT; |
12ab028be mm/zswap.c: chang... |
85 |
module_param_named(compressor, zswap_compressor, charp, 0444); |
2b2811178 zswap: add to mm/ |
86 87 88 89 90 |
/* The maximum percentage of memory that the compressed pool can occupy */ static unsigned int zswap_max_pool_percent = 20; module_param_named(max_pool_percent, zswap_max_pool_percent, uint, 0644); |
60105e124 mm/zswap: support... |
91 92 |
/* zbud_pool is shared by all of zswap backend */ static struct zbud_pool *zswap_pool; |
2b2811178 zswap: add to mm/ |
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
/********************************* * compression functions **********************************/ /* per-cpu compression transforms */ static struct crypto_comp * __percpu *zswap_comp_pcpu_tfms; enum comp_op { ZSWAP_COMPOP_COMPRESS, ZSWAP_COMPOP_DECOMPRESS }; static int zswap_comp_op(enum comp_op op, const u8 *src, unsigned int slen, u8 *dst, unsigned int *dlen) { struct crypto_comp *tfm; int ret; tfm = *per_cpu_ptr(zswap_comp_pcpu_tfms, get_cpu()); switch (op) { case ZSWAP_COMPOP_COMPRESS: ret = crypto_comp_compress(tfm, src, slen, dst, dlen); break; case ZSWAP_COMPOP_DECOMPRESS: ret = crypto_comp_decompress(tfm, src, slen, dst, dlen); break; default: ret = -EINVAL; } put_cpu(); return ret; } static int __init zswap_comp_init(void) { if (!crypto_has_comp(zswap_compressor, 0, 0)) { pr_info("%s compressor not available ", zswap_compressor); /* fall back to default compressor */ zswap_compressor = ZSWAP_COMPRESSOR_DEFAULT; if (!crypto_has_comp(zswap_compressor, 0, 0)) /* can't even load the default compressor */ return -ENODEV; } pr_info("using %s compressor ", zswap_compressor); /* alloc percpu transforms */ zswap_comp_pcpu_tfms = alloc_percpu(struct crypto_comp *); if (!zswap_comp_pcpu_tfms) return -ENOMEM; return 0; } static void zswap_comp_exit(void) { /* free percpu transforms */ if (zswap_comp_pcpu_tfms) free_percpu(zswap_comp_pcpu_tfms); } /********************************* * data structures **********************************/ /* * struct zswap_entry * * This structure contains the metadata for tracking a single compressed * page within zswap. * * rbnode - links the entry into red-black tree for the appropriate swap type * refcount - the number of outstanding reference to the entry. This is needed * to protect against premature freeing of the entry by code |
6b4525164 mm/zswap.c: fix t... |
166 |
* concurrent calls to load, invalidate, and writeback. The lock |
2b2811178 zswap: add to mm/ |
167 168 169 170 |
* for the zswap_tree structure that contains the entry must * be held while changing the refcount. Since the lock must * be held, there is no reason to also make refcount atomic. * offset - the swap offset for the entry. Index into the red-black tree. |
6335b1934 mm/zswap.c: updat... |
171 |
* handle - zbud allocation handle that stores the compressed page data |
2b2811178 zswap: add to mm/ |
172 |
* length - the length in bytes of the compressed page data. Needed during |
6b4525164 mm/zswap.c: fix t... |
173 |
* decompression |
2b2811178 zswap: add to mm/ |
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
*/ struct zswap_entry { struct rb_node rbnode; pgoff_t offset; int refcount; unsigned int length; unsigned long handle; }; struct zswap_header { swp_entry_t swpentry; }; /* * The tree lock in the zswap_tree struct protects a few things: * - the rbtree * - the refcount field of each entry in the tree */ struct zswap_tree { struct rb_root rbroot; spinlock_t lock; |
2b2811178 zswap: add to mm/ |
195 196 197 198 199 200 201 202 203 204 205 206 |
}; static struct zswap_tree *zswap_trees[MAX_SWAPFILES]; /********************************* * zswap entry functions **********************************/ static struct kmem_cache *zswap_entry_cache; static int zswap_entry_cache_create(void) { zswap_entry_cache = KMEM_CACHE(zswap_entry, 0); |
5d2d42de1 mm/zswap.c: remov... |
207 |
return zswap_entry_cache == NULL; |
2b2811178 zswap: add to mm/ |
208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
} static void zswap_entry_cache_destory(void) { kmem_cache_destroy(zswap_entry_cache); } static struct zswap_entry *zswap_entry_cache_alloc(gfp_t gfp) { struct zswap_entry *entry; entry = kmem_cache_alloc(zswap_entry_cache, gfp); if (!entry) return NULL; entry->refcount = 1; |
0ab0abcf5 mm/zswap: refacto... |
222 |
RB_CLEAR_NODE(&entry->rbnode); |
2b2811178 zswap: add to mm/ |
223 224 225 226 227 228 229 |
return entry; } static void zswap_entry_cache_free(struct zswap_entry *entry) { kmem_cache_free(zswap_entry_cache, entry); } |
2b2811178 zswap: add to mm/ |
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
/********************************* * rbtree functions **********************************/ static struct zswap_entry *zswap_rb_search(struct rb_root *root, pgoff_t offset) { struct rb_node *node = root->rb_node; struct zswap_entry *entry; while (node) { entry = rb_entry(node, struct zswap_entry, rbnode); if (entry->offset > offset) node = node->rb_left; else if (entry->offset < offset) node = node->rb_right; else return entry; } return NULL; } /* * In the case that a entry with the same offset is found, a pointer to * the existing entry is stored in dupentry and the function returns -EEXIST */ static int zswap_rb_insert(struct rb_root *root, struct zswap_entry *entry, struct zswap_entry **dupentry) { struct rb_node **link = &root->rb_node, *parent = NULL; struct zswap_entry *myentry; while (*link) { parent = *link; myentry = rb_entry(parent, struct zswap_entry, rbnode); if (myentry->offset > entry->offset) link = &(*link)->rb_left; else if (myentry->offset < entry->offset) link = &(*link)->rb_right; else { *dupentry = myentry; return -EEXIST; } } rb_link_node(&entry->rbnode, parent, link); rb_insert_color(&entry->rbnode, root); return 0; } |
0ab0abcf5 mm/zswap: refacto... |
276 277 278 279 280 281 282 283 284 |
static void zswap_rb_erase(struct rb_root *root, struct zswap_entry *entry) { if (!RB_EMPTY_NODE(&entry->rbnode)) { rb_erase(&entry->rbnode, root); RB_CLEAR_NODE(&entry->rbnode); } } /* |
6335b1934 mm/zswap.c: updat... |
285 |
* Carries out the common pattern of freeing and entry's zbud allocation, |
0ab0abcf5 mm/zswap: refacto... |
286 287 |
* freeing the entry itself, and decrementing the number of stored pages. */ |
60105e124 mm/zswap: support... |
288 |
static void zswap_free_entry(struct zswap_entry *entry) |
0ab0abcf5 mm/zswap: refacto... |
289 |
{ |
60105e124 mm/zswap: support... |
290 |
zbud_free(zswap_pool, entry->handle); |
0ab0abcf5 mm/zswap: refacto... |
291 292 |
zswap_entry_cache_free(entry); atomic_dec(&zswap_stored_pages); |
60105e124 mm/zswap: support... |
293 |
zswap_pool_pages = zbud_get_pool_size(zswap_pool); |
0ab0abcf5 mm/zswap: refacto... |
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
} /* caller must hold the tree lock */ static void zswap_entry_get(struct zswap_entry *entry) { entry->refcount++; } /* caller must hold the tree lock * remove from the tree and free it, if nobody reference the entry */ static void zswap_entry_put(struct zswap_tree *tree, struct zswap_entry *entry) { int refcount = --entry->refcount; BUG_ON(refcount < 0); if (refcount == 0) { zswap_rb_erase(&tree->rbroot, entry); |
60105e124 mm/zswap: support... |
313 |
zswap_free_entry(entry); |
0ab0abcf5 mm/zswap: refacto... |
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 |
} } /* caller must hold the tree lock */ static struct zswap_entry *zswap_entry_find_get(struct rb_root *root, pgoff_t offset) { struct zswap_entry *entry = NULL; entry = zswap_rb_search(root, offset); if (entry) zswap_entry_get(entry); return entry; } |
2b2811178 zswap: add to mm/ |
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 |
/********************************* * per-cpu code **********************************/ static DEFINE_PER_CPU(u8 *, zswap_dstmem); static int __zswap_cpu_notifier(unsigned long action, unsigned long cpu) { struct crypto_comp *tfm; u8 *dst; switch (action) { case CPU_UP_PREPARE: tfm = crypto_alloc_comp(zswap_compressor, 0, 0); if (IS_ERR(tfm)) { pr_err("can't allocate compressor transform "); return NOTIFY_BAD; } *per_cpu_ptr(zswap_comp_pcpu_tfms, cpu) = tfm; dst = kmalloc(PAGE_SIZE * 2, GFP_KERNEL); if (!dst) { pr_err("can't allocate compressor buffer "); crypto_free_comp(tfm); *per_cpu_ptr(zswap_comp_pcpu_tfms, cpu) = NULL; return NOTIFY_BAD; } per_cpu(zswap_dstmem, cpu) = dst; break; case CPU_DEAD: case CPU_UP_CANCELED: tfm = *per_cpu_ptr(zswap_comp_pcpu_tfms, cpu); if (tfm) { crypto_free_comp(tfm); *per_cpu_ptr(zswap_comp_pcpu_tfms, cpu) = NULL; } dst = per_cpu(zswap_dstmem, cpu); kfree(dst); per_cpu(zswap_dstmem, cpu) = NULL; break; default: break; } return NOTIFY_OK; } static int zswap_cpu_notifier(struct notifier_block *nb, unsigned long action, void *pcpu) { unsigned long cpu = (unsigned long)pcpu; return __zswap_cpu_notifier(action, cpu); } static struct notifier_block zswap_cpu_notifier_block = { .notifier_call = zswap_cpu_notifier }; static int zswap_cpu_init(void) { unsigned long cpu; |
576378249 mm, zswap: Fix CP... |
389 |
cpu_notifier_register_begin(); |
2b2811178 zswap: add to mm/ |
390 391 392 |
for_each_online_cpu(cpu) if (__zswap_cpu_notifier(CPU_UP_PREPARE, cpu) != NOTIFY_OK) goto cleanup; |
576378249 mm, zswap: Fix CP... |
393 394 |
__register_cpu_notifier(&zswap_cpu_notifier_block); cpu_notifier_register_done(); |
2b2811178 zswap: add to mm/ |
395 396 397 398 399 |
return 0; cleanup: for_each_online_cpu(cpu) __zswap_cpu_notifier(CPU_UP_CANCELED, cpu); |
576378249 mm, zswap: Fix CP... |
400 |
cpu_notifier_register_done(); |
2b2811178 zswap: add to mm/ |
401 402 403 404 405 406 407 408 |
return -ENOMEM; } /********************************* * helpers **********************************/ static bool zswap_is_full(void) { |
5d2d42de1 mm/zswap.c: remov... |
409 410 |
return totalram_pages * zswap_max_pool_percent / 100 < zswap_pool_pages; |
2b2811178 zswap: add to mm/ |
411 |
} |
2b2811178 zswap: add to mm/ |
412 413 414 415 416 417 418 |
/********************************* * writeback code **********************************/ /* return enum for zswap_get_swap_cache_page */ enum zswap_get_swap_ret { ZSWAP_SWAPCACHE_NEW, ZSWAP_SWAPCACHE_EXIST, |
67d13fe84 mm/zswap: bugfix:... |
419 |
ZSWAP_SWAPCACHE_FAIL, |
2b2811178 zswap: add to mm/ |
420 421 422 423 424 425 426 427 428 429 430 431 432 |
}; /* * zswap_get_swap_cache_page * * This is an adaption of read_swap_cache_async() * * This function tries to find a page with the given swap entry * in the swapper_space address space (the swap cache). If the page * is found, it is returned in retpage. Otherwise, a page is allocated, * added to the swap cache, and returned in retpage. * * If success, the swap cache page is returned in retpage |
67d13fe84 mm/zswap: bugfix:... |
433 434 435 436 |
* Returns ZSWAP_SWAPCACHE_EXIST if page was already in the swap cache * Returns ZSWAP_SWAPCACHE_NEW if the new page needs to be populated, * the new page is added to swapcache and locked * Returns ZSWAP_SWAPCACHE_FAIL on error |
2b2811178 zswap: add to mm/ |
437 438 439 440 441 |
*/ static int zswap_get_swap_cache_page(swp_entry_t entry, struct page **retpage) { struct page *found_page, *new_page = NULL; |
822518dc5 mm/zswap.c: get s... |
442 |
struct address_space *swapper_space = swap_address_space(entry); |
2b2811178 zswap: add to mm/ |
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 |
int err; *retpage = NULL; do { /* * First check the swap cache. Since this is normally * called after lookup_swap_cache() failed, re-calling * that would confuse statistics. */ found_page = find_get_page(swapper_space, entry.val); if (found_page) break; /* * Get a new page to read into from swap. */ if (!new_page) { new_page = alloc_page(GFP_KERNEL); if (!new_page) break; /* Out of memory */ } /* * call radix_tree_preload() while we can wait. */ err = radix_tree_preload(GFP_KERNEL); if (err) break; /* * Swap entry may have been freed since our caller observed it. */ err = swapcache_prepare(entry); if (err == -EEXIST) { /* seems racy */ radix_tree_preload_end(); continue; } if (err) { /* swp entry is obsolete ? */ radix_tree_preload_end(); break; } /* May fail (-ENOMEM) if radix-tree node allocation failed. */ __set_page_locked(new_page); SetPageSwapBacked(new_page); err = __add_to_swap_cache(new_page, entry); if (likely(!err)) { radix_tree_preload_end(); lru_cache_add_anon(new_page); *retpage = new_page; return ZSWAP_SWAPCACHE_NEW; } radix_tree_preload_end(); ClearPageSwapBacked(new_page); __clear_page_locked(new_page); /* * add_to_swap_cache() doesn't return -EEXIST, so we can safely * clear SWAP_HAS_CACHE flag. */ swapcache_free(entry, NULL); } while (err != -ENOMEM); if (new_page) page_cache_release(new_page); if (!found_page) |
67d13fe84 mm/zswap: bugfix:... |
508 |
return ZSWAP_SWAPCACHE_FAIL; |
2b2811178 zswap: add to mm/ |
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 |
*retpage = found_page; return ZSWAP_SWAPCACHE_EXIST; } /* * Attempts to free an entry by adding a page to the swap cache, * decompressing the entry data into the page, and issuing a * bio write to write the page back to the swap device. * * This can be thought of as a "resumed writeback" of the page * to the swap device. We are basically resuming the same swap * writeback path that was intercepted with the frontswap_store() * in the first place. After the page has been decompressed into * the swap cache, the compressed version stored by zswap can be * freed. */ static int zswap_writeback_entry(struct zbud_pool *pool, unsigned long handle) { struct zswap_header *zhdr; swp_entry_t swpentry; struct zswap_tree *tree; pgoff_t offset; struct zswap_entry *entry; struct page *page; u8 *src, *dst; unsigned int dlen; |
0ab0abcf5 mm/zswap: refacto... |
535 |
int ret; |
2b2811178 zswap: add to mm/ |
536 537 538 539 540 541 542 543 544 545 |
struct writeback_control wbc = { .sync_mode = WB_SYNC_NONE, }; /* extract swpentry from data */ zhdr = zbud_map(pool, handle); swpentry = zhdr->swpentry; /* here */ zbud_unmap(pool, handle); tree = zswap_trees[swp_type(swpentry)]; offset = swp_offset(swpentry); |
2b2811178 zswap: add to mm/ |
546 547 548 |
/* find and ref zswap entry */ spin_lock(&tree->lock); |
0ab0abcf5 mm/zswap: refacto... |
549 |
entry = zswap_entry_find_get(&tree->rbroot, offset); |
2b2811178 zswap: add to mm/ |
550 551 552 553 554 |
if (!entry) { /* entry was invalidated */ spin_unlock(&tree->lock); return 0; } |
2b2811178 zswap: add to mm/ |
555 556 557 558 559 |
spin_unlock(&tree->lock); BUG_ON(offset != entry->offset); /* try to allocate swap cache page */ switch (zswap_get_swap_cache_page(swpentry, &page)) { |
67d13fe84 mm/zswap: bugfix:... |
560 |
case ZSWAP_SWAPCACHE_FAIL: /* no memory or invalidate happened */ |
2b2811178 zswap: add to mm/ |
561 562 |
ret = -ENOMEM; goto fail; |
67d13fe84 mm/zswap: bugfix:... |
563 |
case ZSWAP_SWAPCACHE_EXIST: |
2b2811178 zswap: add to mm/ |
564 565 566 567 568 569 570 571 |
/* page is already in the swap cache, ignore for now */ page_cache_release(page); ret = -EEXIST; goto fail; case ZSWAP_SWAPCACHE_NEW: /* page is locked */ /* decompress */ dlen = PAGE_SIZE; |
60105e124 mm/zswap: support... |
572 |
src = (u8 *)zbud_map(zswap_pool, entry->handle) + |
2b2811178 zswap: add to mm/ |
573 574 575 576 577 |
sizeof(struct zswap_header); dst = kmap_atomic(page); ret = zswap_comp_op(ZSWAP_COMPOP_DECOMPRESS, src, entry->length, dst, &dlen); kunmap_atomic(dst); |
60105e124 mm/zswap: support... |
578 |
zbud_unmap(zswap_pool, entry->handle); |
2b2811178 zswap: add to mm/ |
579 580 581 582 583 584 |
BUG_ON(ret); BUG_ON(dlen != PAGE_SIZE); /* page is up to date */ SetPageUptodate(page); } |
b349acc76 mm/zswap: avoid u... |
585 586 |
/* move it to the tail of the inactive list after end_writeback */ SetPageReclaim(page); |
2b2811178 zswap: add to mm/ |
587 588 589 590 591 592 |
/* start writeback */ __swap_writepage(page, &wbc, end_swap_bio_write); page_cache_release(page); zswap_written_back_pages++; spin_lock(&tree->lock); |
2b2811178 zswap: add to mm/ |
593 |
/* drop local reference */ |
0ab0abcf5 mm/zswap: refacto... |
594 |
zswap_entry_put(tree, entry); |
2b2811178 zswap: add to mm/ |
595 596 |
/* |
0ab0abcf5 mm/zswap: refacto... |
597 598 599 600 601 602 603 604 |
* There are two possible situations for entry here: * (1) refcount is 1(normal case), entry is valid and on the tree * (2) refcount is 0, entry is freed and not on the tree * because invalidate happened during writeback * search the tree and free the entry if find entry */ if (entry == zswap_rb_search(&tree->rbroot, offset)) zswap_entry_put(tree, entry); |
2b2811178 zswap: add to mm/ |
605 |
spin_unlock(&tree->lock); |
2b2811178 zswap: add to mm/ |
606 |
|
0ab0abcf5 mm/zswap: refacto... |
607 608 609 610 611 612 613 614 615 |
goto end; /* * if we get here due to ZSWAP_SWAPCACHE_EXIST * a load may happening concurrently * it is safe and okay to not free the entry * if we free the entry in the following put * it it either okay to return !0 */ |
2b2811178 zswap: add to mm/ |
616 617 |
fail: spin_lock(&tree->lock); |
0ab0abcf5 mm/zswap: refacto... |
618 |
zswap_entry_put(tree, entry); |
2b2811178 zswap: add to mm/ |
619 |
spin_unlock(&tree->lock); |
0ab0abcf5 mm/zswap: refacto... |
620 621 |
end: |
2b2811178 zswap: add to mm/ |
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 |
return ret; } /********************************* * frontswap hooks **********************************/ /* attempts to compress and store an single page */ static int zswap_frontswap_store(unsigned type, pgoff_t offset, struct page *page) { struct zswap_tree *tree = zswap_trees[type]; struct zswap_entry *entry, *dupentry; int ret; unsigned int dlen = PAGE_SIZE, len; unsigned long handle; char *buf; u8 *src, *dst; struct zswap_header *zhdr; if (!tree) { ret = -ENODEV; goto reject; } /* reclaim space if needed */ if (zswap_is_full()) { zswap_pool_limit_hit++; |
60105e124 mm/zswap: support... |
649 |
if (zbud_reclaim_page(zswap_pool, 8)) { |
2b2811178 zswap: add to mm/ |
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 |
zswap_reject_reclaim_fail++; ret = -ENOMEM; goto reject; } } /* allocate entry */ entry = zswap_entry_cache_alloc(GFP_KERNEL); if (!entry) { zswap_reject_kmemcache_fail++; ret = -ENOMEM; goto reject; } /* compress */ dst = get_cpu_var(zswap_dstmem); src = kmap_atomic(page); ret = zswap_comp_op(ZSWAP_COMPOP_COMPRESS, src, PAGE_SIZE, dst, &dlen); kunmap_atomic(src); if (ret) { ret = -EINVAL; goto freepage; } /* store */ len = dlen + sizeof(struct zswap_header); |
60105e124 mm/zswap: support... |
676 |
ret = zbud_alloc(zswap_pool, len, __GFP_NORETRY | __GFP_NOWARN, |
2b2811178 zswap: add to mm/ |
677 678 679 680 681 682 683 684 685 |
&handle); if (ret == -ENOSPC) { zswap_reject_compress_poor++; goto freepage; } if (ret) { zswap_reject_alloc_fail++; goto freepage; } |
60105e124 mm/zswap: support... |
686 |
zhdr = zbud_map(zswap_pool, handle); |
2b2811178 zswap: add to mm/ |
687 688 689 |
zhdr->swpentry = swp_entry(type, offset); buf = (u8 *)(zhdr + 1); memcpy(buf, dst, dlen); |
60105e124 mm/zswap: support... |
690 |
zbud_unmap(zswap_pool, handle); |
2b2811178 zswap: add to mm/ |
691 692 693 694 695 696 697 698 699 700 701 702 703 704 |
put_cpu_var(zswap_dstmem); /* populate entry */ entry->offset = offset; entry->handle = handle; entry->length = dlen; /* map */ spin_lock(&tree->lock); do { ret = zswap_rb_insert(&tree->rbroot, entry, &dupentry); if (ret == -EEXIST) { zswap_duplicate_entry++; /* remove from rbtree */ |
0ab0abcf5 mm/zswap: refacto... |
705 706 |
zswap_rb_erase(&tree->rbroot, dupentry); zswap_entry_put(tree, dupentry); |
2b2811178 zswap: add to mm/ |
707 708 709 710 711 712 |
} } while (ret == -EEXIST); spin_unlock(&tree->lock); /* update stats */ atomic_inc(&zswap_stored_pages); |
60105e124 mm/zswap: support... |
713 |
zswap_pool_pages = zbud_get_pool_size(zswap_pool); |
2b2811178 zswap: add to mm/ |
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 |
return 0; freepage: put_cpu_var(zswap_dstmem); zswap_entry_cache_free(entry); reject: return ret; } /* * returns 0 if the page was successfully decompressed * return -1 on entry not found or error */ static int zswap_frontswap_load(unsigned type, pgoff_t offset, struct page *page) { struct zswap_tree *tree = zswap_trees[type]; struct zswap_entry *entry; u8 *src, *dst; unsigned int dlen; |
0ab0abcf5 mm/zswap: refacto... |
735 |
int ret; |
2b2811178 zswap: add to mm/ |
736 737 738 |
/* find */ spin_lock(&tree->lock); |
0ab0abcf5 mm/zswap: refacto... |
739 |
entry = zswap_entry_find_get(&tree->rbroot, offset); |
2b2811178 zswap: add to mm/ |
740 741 742 743 744 |
if (!entry) { /* entry was written back */ spin_unlock(&tree->lock); return -1; } |
2b2811178 zswap: add to mm/ |
745 746 747 748 |
spin_unlock(&tree->lock); /* decompress */ dlen = PAGE_SIZE; |
60105e124 mm/zswap: support... |
749 |
src = (u8 *)zbud_map(zswap_pool, entry->handle) + |
2b2811178 zswap: add to mm/ |
750 751 752 753 754 |
sizeof(struct zswap_header); dst = kmap_atomic(page); ret = zswap_comp_op(ZSWAP_COMPOP_DECOMPRESS, src, entry->length, dst, &dlen); kunmap_atomic(dst); |
60105e124 mm/zswap: support... |
755 |
zbud_unmap(zswap_pool, entry->handle); |
2b2811178 zswap: add to mm/ |
756 757 758 |
BUG_ON(ret); spin_lock(&tree->lock); |
0ab0abcf5 mm/zswap: refacto... |
759 |
zswap_entry_put(tree, entry); |
2b2811178 zswap: add to mm/ |
760 |
spin_unlock(&tree->lock); |
2b2811178 zswap: add to mm/ |
761 762 763 764 765 766 767 768 |
return 0; } /* frees an entry in zswap */ static void zswap_frontswap_invalidate_page(unsigned type, pgoff_t offset) { struct zswap_tree *tree = zswap_trees[type]; struct zswap_entry *entry; |
2b2811178 zswap: add to mm/ |
769 770 771 772 773 774 775 776 777 778 779 |
/* find */ spin_lock(&tree->lock); entry = zswap_rb_search(&tree->rbroot, offset); if (!entry) { /* entry was written back */ spin_unlock(&tree->lock); return; } /* remove from rbtree */ |
0ab0abcf5 mm/zswap: refacto... |
780 |
zswap_rb_erase(&tree->rbroot, entry); |
2b2811178 zswap: add to mm/ |
781 782 |
/* drop the initial reference from entry creation */ |
0ab0abcf5 mm/zswap: refacto... |
783 |
zswap_entry_put(tree, entry); |
2b2811178 zswap: add to mm/ |
784 785 |
spin_unlock(&tree->lock); |
2b2811178 zswap: add to mm/ |
786 787 788 789 790 791 |
} /* frees all zswap entries for the given swap type */ static void zswap_frontswap_invalidate_area(unsigned type) { struct zswap_tree *tree = zswap_trees[type]; |
0bd42136f mm/zswap: use pos... |
792 |
struct zswap_entry *entry, *n; |
2b2811178 zswap: add to mm/ |
793 794 795 796 797 798 |
if (!tree) return; /* walk the tree and free everything */ spin_lock(&tree->lock); |
0ab0abcf5 mm/zswap: refacto... |
799 |
rbtree_postorder_for_each_entry_safe(entry, n, &tree->rbroot, rbnode) |
60105e124 mm/zswap: support... |
800 |
zswap_free_entry(entry); |
2b2811178 zswap: add to mm/ |
801 802 |
tree->rbroot = RB_ROOT; spin_unlock(&tree->lock); |
aa9bca05a mm/zswap: bugfix:... |
803 804 |
kfree(tree); zswap_trees[type] = NULL; |
2b2811178 zswap: add to mm/ |
805 806 807 808 809 810 811 812 813 814 815 |
} static struct zbud_ops zswap_zbud_ops = { .evict = zswap_writeback_entry }; static void zswap_frontswap_init(unsigned type) { struct zswap_tree *tree; tree = kzalloc(sizeof(struct zswap_tree), GFP_KERNEL); |
60105e124 mm/zswap: support... |
816 817 818 819 820 |
if (!tree) { pr_err("alloc failed, zswap disabled for swap type %d ", type); return; } |
2b2811178 zswap: add to mm/ |
821 822 823 |
tree->rbroot = RB_ROOT; spin_lock_init(&tree->lock); zswap_trees[type] = tree; |
2b2811178 zswap: add to mm/ |
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 |
} static struct frontswap_ops zswap_frontswap_ops = { .store = zswap_frontswap_store, .load = zswap_frontswap_load, .invalidate_page = zswap_frontswap_invalidate_page, .invalidate_area = zswap_frontswap_invalidate_area, .init = zswap_frontswap_init }; /********************************* * debugfs functions **********************************/ #ifdef CONFIG_DEBUG_FS #include <linux/debugfs.h> static struct dentry *zswap_debugfs_root; static int __init zswap_debugfs_init(void) { if (!debugfs_initialized()) return -ENODEV; zswap_debugfs_root = debugfs_create_dir("zswap", NULL); if (!zswap_debugfs_root) return -ENOMEM; debugfs_create_u64("pool_limit_hit", S_IRUGO, zswap_debugfs_root, &zswap_pool_limit_hit); debugfs_create_u64("reject_reclaim_fail", S_IRUGO, zswap_debugfs_root, &zswap_reject_reclaim_fail); debugfs_create_u64("reject_alloc_fail", S_IRUGO, zswap_debugfs_root, &zswap_reject_alloc_fail); debugfs_create_u64("reject_kmemcache_fail", S_IRUGO, zswap_debugfs_root, &zswap_reject_kmemcache_fail); debugfs_create_u64("reject_compress_poor", S_IRUGO, zswap_debugfs_root, &zswap_reject_compress_poor); debugfs_create_u64("written_back_pages", S_IRUGO, zswap_debugfs_root, &zswap_written_back_pages); debugfs_create_u64("duplicate_entry", S_IRUGO, zswap_debugfs_root, &zswap_duplicate_entry); debugfs_create_u64("pool_pages", S_IRUGO, zswap_debugfs_root, &zswap_pool_pages); debugfs_create_atomic_t("stored_pages", S_IRUGO, zswap_debugfs_root, &zswap_stored_pages); return 0; } static void __exit zswap_debugfs_exit(void) { debugfs_remove_recursive(zswap_debugfs_root); } #else static int __init zswap_debugfs_init(void) { return 0; } static void __exit zswap_debugfs_exit(void) { } #endif /********************************* * module init and exit **********************************/ static int __init init_zswap(void) { if (!zswap_enabled) return 0; pr_info("loading zswap "); |
60105e124 mm/zswap: support... |
896 897 898 899 900 901 902 |
zswap_pool = zbud_create_pool(GFP_KERNEL, &zswap_zbud_ops); if (!zswap_pool) { pr_err("zbud pool creation failed "); goto error; } |
2b2811178 zswap: add to mm/ |
903 904 905 |
if (zswap_entry_cache_create()) { pr_err("entry cache creation failed "); |
60105e124 mm/zswap: support... |
906 |
goto cachefail; |
2b2811178 zswap: add to mm/ |
907 908 909 910 911 912 913 914 915 916 917 |
} if (zswap_comp_init()) { pr_err("compressor initialization failed "); goto compfail; } if (zswap_cpu_init()) { pr_err("per-cpu initialization failed "); goto pcpufail; } |
60105e124 mm/zswap: support... |
918 |
|
2b2811178 zswap: add to mm/ |
919 920 921 922 923 924 925 926 927 |
frontswap_register_ops(&zswap_frontswap_ops); if (zswap_debugfs_init()) pr_warn("debugfs initialization failed "); return 0; pcpufail: zswap_comp_exit(); compfail: zswap_entry_cache_destory(); |
60105e124 mm/zswap: support... |
928 929 |
cachefail: zbud_destroy_pool(zswap_pool); |
2b2811178 zswap: add to mm/ |
930 931 932 933 934 935 936 937 938 |
error: return -ENOMEM; } /* must be late so crypto has time to come up */ late_initcall(init_zswap); MODULE_LICENSE("GPL"); MODULE_AUTHOR("Seth Jennings <sjenning@linux.vnet.ibm.com>"); MODULE_DESCRIPTION("Compressed cache for swap pages"); |