zswap.txt 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. Overview:
  2. Zswap is a lightweight compressed cache for swap pages. It takes
  3. pages that are in the process of being swapped out and attempts to
  4. compress them into a dynamically allocated RAM-based memory pool.
  5. If this process is successful, the writeback to the swap device is
  6. deferred and, in many cases, avoided completely.  This results in
  7. a significant I/O reduction and performance gains for systems that
  8. are swapping.
  9. Zswap provides compressed swap caching that basically trades CPU cycles
  10. for reduced swap I/O.  This trade-off can result in a significant
  11. performance improvement as reads to/writes from to the compressed
  12. cache almost always faster that reading from a swap device
  13. which incurs the latency of an asynchronous block I/O read.
  14. Some potential benefits:
  15. * Desktop/laptop users with limited RAM capacities can mitigate the
  16.     performance impact of swapping.
  17. * Overcommitted guests that share a common I/O resource can
  18.     dramatically reduce their swap I/O pressure, avoiding heavy
  19.     handed I/O throttling by the hypervisor.  This allows more work
  20.     to get done with less impact to the guest workload and guests
  21.     sharing the I/O subsystem
  22. * Users with SSDs as swap devices can extend the life of the device by
  23.     drastically reducing life-shortening writes.
  24. Zswap evicts pages from compressed cache on an LRU basis to the backing
  25. swap device when the compress pool reaches it size limit or the pool is
  26. unable to obtain additional pages from the buddy allocator.  This
  27. requirement had been identified in prior community discussions.
  28. To enabled zswap, the "enabled" attribute must be set to 1 at boot time.
  29. e.g. zswap.enabled=1
  30. Design:
  31. Zswap receives pages for compression through the Frontswap API and
  32. is able to evict pages from its own compressed pool on an LRU basis
  33. and write them back to the backing swap device in the case that the
  34. compressed pool is full or unable to secure additional pages from
  35. the buddy allocator.
  36. Zswap makes use of zsmalloc for the managing the compressed memory
  37. pool. This is because zsmalloc is specifically designed to minimize
  38. fragmentation on large (> PAGE_SIZE/2) allocation sizes. Each
  39. allocation in zsmalloc is not directly accessible by address.
  40. Rather, a handle is return by the allocation routine and that handle
  41. must be mapped before being accessed. The compressed memory pool grows
  42. on demand and shrinks as compressed pages are freed. The pool is
  43. not preallocated.
  44. When a swap page is passed from frontswap to zswap, zswap maintains
  45. a mapping of the swap entry, a combination of the swap type and swap
  46. offset, to the zsmalloc handle that references that compressed swap
  47. page. This mapping is achieved with a red-black tree per swap type.
  48. The swap offset is the search key for the tree nodes.
  49. During a page fault on a PTE that is a swap entry, frontswap calls
  50. the zswap load function to decompress the page into the page
  51. allocated by the page fault handler.
  52. Once there are no PTEs referencing a swap page stored in zswap
  53. (i.e. the count in the swap_map goes to 0) the swap code calls
  54. the zswap invalidate function, via frontswap, to free the compressed
  55. entry.
  56. Zswap seeks to be simple in its policies. Sysfs attributes allow for
  57. two user controlled policies:
  58. * max_compression_ratio - Maximum compression ratio, as as percentage,
  59. for an acceptable compressed page. Any page that does not compress
  60. by at least this ratio will be rejected.
  61. * max_pool_percent - The maximum percentage of memory that the compressed
  62. pool can occupy.
  63. Zswap allows the compressor to be selected at kernel boot time by
  64. setting the “compressor” attribute. The default compressor is lzo.
  65. e.g. zswap.compressor=deflate
  66. A debugfs interface is provided for various statistic about pool size,
  67. number of pages stored, and various counters for the reasons pages
  68. are rejected.