frontswap.rst 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. .. _frontswap:
  2. =========
  3. Frontswap
  4. =========
  5. Frontswap provides a "transcendent memory" interface for swap pages.
  6. In some environments, dramatic performance savings may be obtained because
  7. swapped pages are saved in RAM (or a RAM-like device) instead of a swap disk.
  8. (Note, frontswap -- and :ref:`cleancache` (merged at 3.0) -- are the "frontends"
  9. and the only necessary changes to the core kernel for transcendent memory;
  10. all other supporting code -- the "backends" -- is implemented as drivers.
  11. See the LWN.net article `Transcendent memory in a nutshell`_
  12. for a detailed overview of frontswap and related kernel parts)
  13. .. _Transcendent memory in a nutshell: https://lwn.net/Articles/454795/
  14. Frontswap is so named because it can be thought of as the opposite of
  15. a "backing" store for a swap device. The storage is assumed to be
  16. a synchronous concurrency-safe page-oriented "pseudo-RAM device" conforming
  17. to the requirements of transcendent memory (such as Xen's "tmem", or
  18. in-kernel compressed memory, aka "zcache", or future RAM-like devices);
  19. this pseudo-RAM device is not directly accessible or addressable by the
  20. kernel and is of unknown and possibly time-varying size. The driver
  21. links itself to frontswap by calling frontswap_register_ops to set the
  22. frontswap_ops funcs appropriately and the functions it provides must
  23. conform to certain policies as follows:
  24. An "init" prepares the device to receive frontswap pages associated
  25. with the specified swap device number (aka "type"). A "store" will
  26. copy the page to transcendent memory and associate it with the type and
  27. offset associated with the page. A "load" will copy the page, if found,
  28. from transcendent memory into kernel memory, but will NOT remove the page
  29. from transcendent memory. An "invalidate_page" will remove the page
  30. from transcendent memory and an "invalidate_area" will remove ALL pages
  31. associated with the swap type (e.g., like swapoff) and notify the "device"
  32. to refuse further stores with that swap type.
  33. Once a page is successfully stored, a matching load on the page will normally
  34. succeed. So when the kernel finds itself in a situation where it needs
  35. to swap out a page, it first attempts to use frontswap. If the store returns
  36. success, the data has been successfully saved to transcendent memory and
  37. a disk write and, if the data is later read back, a disk read are avoided.
  38. If a store returns failure, transcendent memory has rejected the data, and the
  39. page can be written to swap as usual.
  40. Note that if a page is stored and the page already exists in transcendent memory
  41. (a "duplicate" store), either the store succeeds and the data is overwritten,
  42. or the store fails AND the page is invalidated. This ensures stale data may
  43. never be obtained from frontswap.
  44. If properly configured, monitoring of frontswap is done via debugfs in
  45. the `/sys/kernel/debug/frontswap` directory. The effectiveness of
  46. frontswap can be measured (across all swap devices) with:
  47. ``failed_stores``
  48. how many store attempts have failed
  49. ``loads``
  50. how many loads were attempted (all should succeed)
  51. ``succ_stores``
  52. how many store attempts have succeeded
  53. ``invalidates``
  54. how many invalidates were attempted
  55. A backend implementation may provide additional metrics.
  56. FAQ
  57. ===
  58. * Where's the value?
  59. When a workload starts swapping, performance falls through the floor.
  60. Frontswap significantly increases performance in many such workloads by
  61. providing a clean, dynamic interface to read and write swap pages to
  62. "transcendent memory" that is otherwise not directly addressable to the kernel.
  63. This interface is ideal when data is transformed to a different form
  64. and size (such as with compression) or secretly moved (as might be
  65. useful for write-balancing for some RAM-like devices). Swap pages (and
  66. evicted page-cache pages) are a great use for this kind of slower-than-RAM-
  67. but-much-faster-than-disk "pseudo-RAM device" and the frontswap (and
  68. cleancache) interface to transcendent memory provides a nice way to read
  69. and write -- and indirectly "name" -- the pages.
  70. Frontswap -- and cleancache -- with a fairly small impact on the kernel,
  71. provides a huge amount of flexibility for more dynamic, flexible RAM
  72. utilization in various system configurations:
  73. In the single kernel case, aka "zcache", pages are compressed and
  74. stored in local memory, thus increasing the total anonymous pages
  75. that can be safely kept in RAM. Zcache essentially trades off CPU
  76. cycles used in compression/decompression for better memory utilization.
  77. Benchmarks have shown little or no impact when memory pressure is
  78. low while providing a significant performance improvement (25%+)
  79. on some workloads under high memory pressure.
  80. "RAMster" builds on zcache by adding "peer-to-peer" transcendent memory
  81. support for clustered systems. Frontswap pages are locally compressed
  82. as in zcache, but then "remotified" to another system's RAM. This
  83. allows RAM to be dynamically load-balanced back-and-forth as needed,
  84. i.e. when system A is overcommitted, it can swap to system B, and
  85. vice versa. RAMster can also be configured as a memory server so
  86. many servers in a cluster can swap, dynamically as needed, to a single
  87. server configured with a large amount of RAM... without pre-configuring
  88. how much of the RAM is available for each of the clients!
  89. In the virtual case, the whole point of virtualization is to statistically
  90. multiplex physical resources across the varying demands of multiple
  91. virtual machines. This is really hard to do with RAM and efforts to do
  92. it well with no kernel changes have essentially failed (except in some
  93. well-publicized special-case workloads).
  94. Specifically, the Xen Transcendent Memory backend allows otherwise
  95. "fallow" hypervisor-owned RAM to not only be "time-shared" between multiple
  96. virtual machines, but the pages can be compressed and deduplicated to
  97. optimize RAM utilization. And when guest OS's are induced to surrender
  98. underutilized RAM (e.g. with "selfballooning"), sudden unexpected
  99. memory pressure may result in swapping; frontswap allows those pages
  100. to be swapped to and from hypervisor RAM (if overall host system memory
  101. conditions allow), thus mitigating the potentially awful performance impact
  102. of unplanned swapping.
  103. A KVM implementation is underway and has been RFC'ed to lkml. And,
  104. using frontswap, investigation is also underway on the use of NVM as
  105. a memory extension technology.
  106. * Sure there may be performance advantages in some situations, but
  107. what's the space/time overhead of frontswap?
  108. If CONFIG_FRONTSWAP is disabled, every frontswap hook compiles into
  109. nothingness and the only overhead is a few extra bytes per swapon'ed
  110. swap device. If CONFIG_FRONTSWAP is enabled but no frontswap "backend"
  111. registers, there is one extra global variable compared to zero for
  112. every swap page read or written. If CONFIG_FRONTSWAP is enabled
  113. AND a frontswap backend registers AND the backend fails every "store"
  114. request (i.e. provides no memory despite claiming it might),
  115. CPU overhead is still negligible -- and since every frontswap fail
  116. precedes a swap page write-to-disk, the system is highly likely
  117. to be I/O bound and using a small fraction of a percent of a CPU
  118. will be irrelevant anyway.
  119. As for space, if CONFIG_FRONTSWAP is enabled AND a frontswap backend
  120. registers, one bit is allocated for every swap page for every swap
  121. device that is swapon'd. This is added to the EIGHT bits (which
  122. was sixteen until about 2.6.34) that the kernel already allocates
  123. for every swap page for every swap device that is swapon'd. (Hugh
  124. Dickins has observed that frontswap could probably steal one of
  125. the existing eight bits, but let's worry about that minor optimization
  126. later.) For very large swap disks (which are rare) on a standard
  127. 4K pagesize, this is 1MB per 32GB swap.
  128. When swap pages are stored in transcendent memory instead of written
  129. out to disk, there is a side effect that this may create more memory
  130. pressure that can potentially outweigh the other advantages. A
  131. backend, such as zcache, must implement policies to carefully (but
  132. dynamically) manage memory limits to ensure this doesn't happen.
  133. * OK, how about a quick overview of what this frontswap patch does
  134. in terms that a kernel hacker can grok?
  135. Let's assume that a frontswap "backend" has registered during
  136. kernel initialization; this registration indicates that this
  137. frontswap backend has access to some "memory" that is not directly
  138. accessible by the kernel. Exactly how much memory it provides is
  139. entirely dynamic and random.
  140. Whenever a swap-device is swapon'd frontswap_init() is called,
  141. passing the swap device number (aka "type") as a parameter.
  142. This notifies frontswap to expect attempts to "store" swap pages
  143. associated with that number.
  144. Whenever the swap subsystem is readying a page to write to a swap
  145. device (c.f swap_writepage()), frontswap_store is called. Frontswap
  146. consults with the frontswap backend and if the backend says it does NOT
  147. have room, frontswap_store returns -1 and the kernel swaps the page
  148. to the swap device as normal. Note that the response from the frontswap
  149. backend is unpredictable to the kernel; it may choose to never accept a
  150. page, it could accept every ninth page, or it might accept every
  151. page. But if the backend does accept a page, the data from the page
  152. has already been copied and associated with the type and offset,
  153. and the backend guarantees the persistence of the data. In this case,
  154. frontswap sets a bit in the "frontswap_map" for the swap device
  155. corresponding to the page offset on the swap device to which it would
  156. otherwise have written the data.
  157. When the swap subsystem needs to swap-in a page (swap_readpage()),
  158. it first calls frontswap_load() which checks the frontswap_map to
  159. see if the page was earlier accepted by the frontswap backend. If
  160. it was, the page of data is filled from the frontswap backend and
  161. the swap-in is complete. If not, the normal swap-in code is
  162. executed to obtain the page of data from the real swap device.
  163. So every time the frontswap backend accepts a page, a swap device read
  164. and (potentially) a swap device write are replaced by a "frontswap backend
  165. store" and (possibly) a "frontswap backend loads", which are presumably much
  166. faster.
  167. * Can't frontswap be configured as a "special" swap device that is
  168. just higher priority than any real swap device (e.g. like zswap,
  169. or maybe swap-over-nbd/NFS)?
  170. No. First, the existing swap subsystem doesn't allow for any kind of
  171. swap hierarchy. Perhaps it could be rewritten to accommodate a hierarchy,
  172. but this would require fairly drastic changes. Even if it were
  173. rewritten, the existing swap subsystem uses the block I/O layer which
  174. assumes a swap device is fixed size and any page in it is linearly
  175. addressable. Frontswap barely touches the existing swap subsystem,
  176. and works around the constraints of the block I/O subsystem to provide
  177. a great deal of flexibility and dynamicity.
  178. For example, the acceptance of any swap page by the frontswap backend is
  179. entirely unpredictable. This is critical to the definition of frontswap
  180. backends because it grants completely dynamic discretion to the
  181. backend. In zcache, one cannot know a priori how compressible a page is.
  182. "Poorly" compressible pages can be rejected, and "poorly" can itself be
  183. defined dynamically depending on current memory constraints.
  184. Further, frontswap is entirely synchronous whereas a real swap
  185. device is, by definition, asynchronous and uses block I/O. The
  186. block I/O layer is not only unnecessary, but may perform "optimizations"
  187. that are inappropriate for a RAM-oriented device including delaying
  188. the write of some pages for a significant amount of time. Synchrony is
  189. required to ensure the dynamicity of the backend and to avoid thorny race
  190. conditions that would unnecessarily and greatly complicate frontswap
  191. and/or the block I/O subsystem. That said, only the initial "store"
  192. and "load" operations need be synchronous. A separate asynchronous thread
  193. is free to manipulate the pages stored by frontswap. For example,
  194. the "remotification" thread in RAMster uses standard asynchronous
  195. kernel sockets to move compressed frontswap pages to a remote machine.
  196. Similarly, a KVM guest-side implementation could do in-guest compression
  197. and use "batched" hypercalls.
  198. In a virtualized environment, the dynamicity allows the hypervisor
  199. (or host OS) to do "intelligent overcommit". For example, it can
  200. choose to accept pages only until host-swapping might be imminent,
  201. then force guests to do their own swapping.
  202. There is a downside to the transcendent memory specifications for
  203. frontswap: Since any "store" might fail, there must always be a real
  204. slot on a real swap device to swap the page. Thus frontswap must be
  205. implemented as a "shadow" to every swapon'd device with the potential
  206. capability of holding every page that the swap device might have held
  207. and the possibility that it might hold no pages at all. This means
  208. that frontswap cannot contain more pages than the total of swapon'd
  209. swap devices. For example, if NO swap device is configured on some
  210. installation, frontswap is useless. Swapless portable devices
  211. can still use frontswap but a backend for such devices must configure
  212. some kind of "ghost" swap device and ensure that it is never used.
  213. * Why this weird definition about "duplicate stores"? If a page
  214. has been previously successfully stored, can't it always be
  215. successfully overwritten?
  216. Nearly always it can, but no, sometimes it cannot. Consider an example
  217. where data is compressed and the original 4K page has been compressed
  218. to 1K. Now an attempt is made to overwrite the page with data that
  219. is non-compressible and so would take the entire 4K. But the backend
  220. has no more space. In this case, the store must be rejected. Whenever
  221. frontswap rejects a store that would overwrite, it also must invalidate
  222. the old data and ensure that it is no longer accessible. Since the
  223. swap subsystem then writes the new data to the read swap device,
  224. this is the correct course of action to ensure coherency.
  225. * Why does the frontswap patch create the new include file swapfile.h?
  226. The frontswap code depends on some swap-subsystem-internal data
  227. structures that have, over the years, moved back and forth between
  228. static and global. This seemed a reasonable compromise: Define
  229. them as global but declare them in a new include file that isn't
  230. included by the large number of source files that include swap.h.
  231. Dan Magenheimer, last updated April 9, 2012