cleancache.txt 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. MOTIVATION
  2. Cleancache is a new optional feature provided by the VFS layer that
  3. potentially dramatically increases page cache effectiveness for
  4. many workloads in many environments at a negligible cost.
  5. Cleancache can be thought of as a page-granularity victim cache for clean
  6. pages that the kernel's pageframe replacement algorithm (PFRA) would like
  7. to keep around, but can't since there isn't enough memory. So when the
  8. PFRA "evicts" a page, it first attempts to use cleancache code to
  9. put the data contained in that page into "transcendent memory", memory
  10. that is not directly accessible or addressable by the kernel and is
  11. of unknown and possibly time-varying size.
  12. Later, when a cleancache-enabled filesystem wishes to access a page
  13. in a file on disk, it first checks cleancache to see if it already
  14. contains it; if it does, the page of data is copied into the kernel
  15. and a disk access is avoided.
  16. Transcendent memory "drivers" for cleancache are currently implemented
  17. in Xen (using hypervisor memory) and zcache (using in-kernel compressed
  18. memory) and other implementations are in development.
  19. FAQs are included below.
  20. IMPLEMENTATION OVERVIEW
  21. A cleancache "backend" that provides transcendent memory registers itself
  22. to the kernel's cleancache "frontend" by calling cleancache_register_ops,
  23. passing a pointer to a cleancache_ops structure with funcs set appropriately.
  24. Note that cleancache_register_ops returns the previous settings so that
  25. chaining can be performed if desired. The functions provided must conform to
  26. certain semantics as follows:
  27. Most important, cleancache is "ephemeral". Pages which are copied into
  28. cleancache have an indefinite lifetime which is completely unknowable
  29. by the kernel and so may or may not still be in cleancache at any later time.
  30. Thus, as its name implies, cleancache is not suitable for dirty pages.
  31. Cleancache has complete discretion over what pages to preserve and what
  32. pages to discard and when.
  33. Mounting a cleancache-enabled filesystem should call "init_fs" to obtain a
  34. pool id which, if positive, must be saved in the filesystem's superblock;
  35. a negative return value indicates failure. A "put_page" will copy a
  36. (presumably about-to-be-evicted) page into cleancache and associate it with
  37. the pool id, a file key, and a page index into the file. (The combination
  38. of a pool id, a file key, and an index is sometimes called a "handle".)
  39. A "get_page" will copy the page, if found, from cleancache into kernel memory.
  40. A "flush_page" will ensure the page no longer is present in cleancache;
  41. a "flush_inode" will flush all pages associated with the specified file;
  42. and, when a filesystem is unmounted, a "flush_fs" will flush all pages in
  43. all files specified by the given pool id and also surrender the pool id.
  44. An "init_shared_fs", like init_fs, obtains a pool id but tells cleancache
  45. to treat the pool as shared using a 128-bit UUID as a key. On systems
  46. that may run multiple kernels (such as hard partitioned or virtualized
  47. systems) that may share a clustered filesystem, and where cleancache
  48. may be shared among those kernels, calls to init_shared_fs that specify the
  49. same UUID will receive the same pool id, thus allowing the pages to
  50. be shared. Note that any security requirements must be imposed outside
  51. of the kernel (e.g. by "tools" that control cleancache). Or a
  52. cleancache implementation can simply disable shared_init by always
  53. returning a negative value.
  54. If a get_page is successful on a non-shared pool, the page is flushed (thus
  55. making cleancache an "exclusive" cache). On a shared pool, the page
  56. is NOT flushed on a successful get_page so that it remains accessible to
  57. other sharers. The kernel is responsible for ensuring coherency between
  58. cleancache (shared or not), the page cache, and the filesystem, using
  59. cleancache flush operations as required.
  60. Note that cleancache must enforce put-put-get coherency and get-get
  61. coherency. For the former, if two puts are made to the same handle but
  62. with different data, say AAA by the first put and BBB by the second, a
  63. subsequent get can never return the stale data (AAA). For get-get coherency,
  64. if a get for a given handle fails, subsequent gets for that handle will
  65. never succeed unless preceded by a successful put with that handle.
  66. Last, cleancache provides no SMP serialization guarantees; if two
  67. different Linux threads are simultaneously putting and flushing a page
  68. with the same handle, the results are indeterminate. Callers must
  69. lock the page to ensure serial behavior.
  70. CLEANCACHE PERFORMANCE METRICS
  71. Cleancache monitoring is done by sysfs files in the
  72. /sys/kernel/mm/cleancache directory. The effectiveness of cleancache
  73. can be measured (across all filesystems) with:
  74. succ_gets - number of gets that were successful
  75. failed_gets - number of gets that failed
  76. puts - number of puts attempted (all "succeed")
  77. flushes - number of flushes attempted
  78. A backend implementatation may provide additional metrics.
  79. FAQ
  80. 1) Where's the value? (Andrew Morton)
  81. Cleancache provides a significant performance benefit to many workloads
  82. in many environments with negligible overhead by improving the
  83. effectiveness of the pagecache. Clean pagecache pages are
  84. saved in transcendent memory (RAM that is otherwise not directly
  85. addressable to the kernel); fetching those pages later avoids "refaults"
  86. and thus disk reads.
  87. Cleancache (and its sister code "frontswap") provide interfaces for
  88. this transcendent memory (aka "tmem"), which conceptually lies between
  89. fast kernel-directly-addressable RAM and slower DMA/asynchronous devices.
  90. Disallowing direct kernel or userland reads/writes to tmem
  91. is ideal when data is transformed to a different form and size (such
  92. as with compression) or secretly moved (as might be useful for write-
  93. balancing for some RAM-like devices). Evicted page-cache pages (and
  94. swap pages) are a great use for this kind of slower-than-RAM-but-much-
  95. faster-than-disk transcendent memory, and the cleancache (and frontswap)
  96. "page-object-oriented" specification provides a nice way to read and
  97. write -- and indirectly "name" -- the pages.
  98. In the virtual case, the whole point of virtualization is to statistically
  99. multiplex physical resources across the varying demands of multiple
  100. virtual machines. This is really hard to do with RAM and efforts to
  101. do it well with no kernel change have essentially failed (except in some
  102. well-publicized special-case workloads). Cleancache -- and frontswap --
  103. with a fairly small impact on the kernel, provide a huge amount
  104. of flexibility for more dynamic, flexible RAM multiplexing.
  105. Specifically, the Xen Transcendent Memory backend allows otherwise
  106. "fallow" hypervisor-owned RAM to not only be "time-shared" between multiple
  107. virtual machines, but the pages can be compressed and deduplicated to
  108. optimize RAM utilization. And when guest OS's are induced to surrender
  109. underutilized RAM (e.g. with "self-ballooning"), page cache pages
  110. are the first to go, and cleancache allows those pages to be
  111. saved and reclaimed if overall host system memory conditions allow.
  112. And the identical interface used for cleancache can be used in
  113. physical systems as well. The zcache driver acts as a memory-hungry
  114. device that stores pages of data in a compressed state. And
  115. the proposed "RAMster" driver shares RAM across multiple physical
  116. systems.
  117. 2) Why does cleancache have its sticky fingers so deep inside the
  118. filesystems and VFS? (Andrew Morton and Christoph Hellwig)
  119. The core hooks for cleancache in VFS are in most cases a single line
  120. and the minimum set are placed precisely where needed to maintain
  121. coherency (via cleancache_flush operations) between cleancache,
  122. the page cache, and disk. All hooks compile into nothingness if
  123. cleancache is config'ed off and turn into a function-pointer-
  124. compare-to-NULL if config'ed on but no backend claims the ops
  125. functions, or to a compare-struct-element-to-negative if a
  126. backend claims the ops functions but a filesystem doesn't enable
  127. cleancache.
  128. Some filesystems are built entirely on top of VFS and the hooks
  129. in VFS are sufficient, so don't require an "init_fs" hook; the
  130. initial implementation of cleancache didn't provide this hook.
  131. But for some filesystems (such as btrfs), the VFS hooks are
  132. incomplete and one or more hooks in fs-specific code are required.
  133. And for some other filesystems, such as tmpfs, cleancache may
  134. be counterproductive. So it seemed prudent to require a filesystem
  135. to "opt in" to use cleancache, which requires adding a hook in
  136. each filesystem. Not all filesystems are supported by cleancache
  137. only because they haven't been tested. The existing set should
  138. be sufficient to validate the concept, the opt-in approach means
  139. that untested filesystems are not affected, and the hooks in the
  140. existing filesystems should make it very easy to add more
  141. filesystems in the future.
  142. The total impact of the hooks to existing fs and mm files is only
  143. about 40 lines added (not counting comments and blank lines).
  144. 3) Why not make cleancache asynchronous and batched so it can
  145. more easily interface with real devices with DMA instead
  146. of copying each individual page? (Minchan Kim)
  147. The one-page-at-a-time copy semantics simplifies the implementation
  148. on both the frontend and backend and also allows the backend to
  149. do fancy things on-the-fly like page compression and
  150. page deduplication. And since the data is "gone" (copied into/out
  151. of the pageframe) before the cleancache get/put call returns,
  152. a great deal of race conditions and potential coherency issues
  153. are avoided. While the interface seems odd for a "real device"
  154. or for real kernel-addressable RAM, it makes perfect sense for
  155. transcendent memory.
  156. 4) Why is non-shared cleancache "exclusive"? And where is the
  157. page "flushed" after a "get"? (Minchan Kim)
  158. The main reason is to free up space in transcendent memory and
  159. to avoid unnecessary cleancache_flush calls. If you want inclusive,
  160. the page can be "put" immediately following the "get". If
  161. put-after-get for inclusive becomes common, the interface could
  162. be easily extended to add a "get_no_flush" call.
  163. The flush is done by the cleancache backend implementation.
  164. 5) What's the performance impact?
  165. Performance analysis has been presented at OLS'09 and LCA'10.
  166. Briefly, performance gains can be significant on most workloads,
  167. especially when memory pressure is high (e.g. when RAM is
  168. overcommitted in a virtual workload); and because the hooks are
  169. invoked primarily in place of or in addition to a disk read/write,
  170. overhead is negligible even in worst case workloads. Basically
  171. cleancache replaces I/O with memory-copy-CPU-overhead; on older
  172. single-core systems with slow memory-copy speeds, cleancache
  173. has little value, but in newer multicore machines, especially
  174. consolidated/virtualized machines, it has great value.
  175. 6) How do I add cleancache support for filesystem X? (Boaz Harrash)
  176. Filesystems that are well-behaved and conform to certain
  177. restrictions can utilize cleancache simply by making a call to
  178. cleancache_init_fs at mount time. Unusual, misbehaving, or
  179. poorly layered filesystems must either add additional hooks
  180. and/or undergo extensive additional testing... or should just
  181. not enable the optional cleancache.
  182. Some points for a filesystem to consider:
  183. - The FS should be block-device-based (e.g. a ram-based FS such
  184. as tmpfs should not enable cleancache)
  185. - To ensure coherency/correctness, the FS must ensure that all
  186. file removal or truncation operations either go through VFS or
  187. add hooks to do the equivalent cleancache "flush" operations
  188. - To ensure coherency/correctness, either inode numbers must
  189. be unique across the lifetime of the on-disk file OR the
  190. FS must provide an "encode_fh" function.
  191. - The FS must call the VFS superblock alloc and deactivate routines
  192. or add hooks to do the equivalent cleancache calls done there.
  193. - To maximize performance, all pages fetched from the FS should
  194. go through the do_mpag_readpage routine or the FS should add
  195. hooks to do the equivalent (cf. btrfs)
  196. - Currently, the FS blocksize must be the same as PAGESIZE. This
  197. is not an architectural restriction, but no backends currently
  198. support anything different.
  199. - A clustered FS should invoke the "shared_init_fs" cleancache
  200. hook to get best performance for some backends.
  201. 7) Why not use the KVA of the inode as the key? (Christoph Hellwig)
  202. If cleancache would use the inode virtual address instead of
  203. inode/filehandle, the pool id could be eliminated. But, this
  204. won't work because cleancache retains pagecache data pages
  205. persistently even when the inode has been pruned from the
  206. inode unused list, and only flushes the data page if the file
  207. gets removed/truncated. So if cleancache used the inode kva,
  208. there would be potential coherency issues if/when the inode
  209. kva is reused for a different file. Alternately, if cleancache
  210. flushed the pages when the inode kva was freed, much of the value
  211. of cleancache would be lost because the cache of pages in cleanache
  212. is potentially much larger than the kernel pagecache and is most
  213. useful if the pages survive inode cache removal.
  214. 8) Why is a global variable required?
  215. The cleancache_enabled flag is checked in all of the frequently-used
  216. cleancache hooks. The alternative is a function call to check a static
  217. variable. Since cleancache is enabled dynamically at runtime, systems
  218. that don't enable cleancache would suffer thousands (possibly
  219. tens-of-thousands) of unnecessary function calls per second. So the
  220. global variable allows cleancache to be enabled by default at compile
  221. time, but have insignificant performance impact when cleancache remains
  222. disabled at runtime.
  223. 9) Does cleanache work with KVM?
  224. The memory model of KVM is sufficiently different that a cleancache
  225. backend may have less value for KVM. This remains to be tested,
  226. especially in an overcommitted system.
  227. 10) Does cleancache work in userspace? It sounds useful for
  228. memory hungry caches like web browsers. (Jamie Lokier)
  229. No plans yet, though we agree it sounds useful, at least for
  230. apps that bypass the page cache (e.g. O_DIRECT).
  231. Last updated: Dan Magenheimer, April 13 2011