Back to home page

OSCL-LXR

 
 

    


0001 .. _frontswap:
0002 
0003 =========
0004 Frontswap
0005 =========
0006 
0007 Frontswap provides a "transcendent memory" interface for swap pages.
0008 In some environments, dramatic performance savings may be obtained because
0009 swapped pages are saved in RAM (or a RAM-like device) instead of a swap disk.
0010 
0011 .. _Transcendent memory in a nutshell: https://lwn.net/Articles/454795/
0012 
0013 Frontswap is so named because it can be thought of as the opposite of
0014 a "backing" store for a swap device.  The storage is assumed to be
0015 a synchronous concurrency-safe page-oriented "pseudo-RAM device" conforming
0016 to the requirements of transcendent memory (such as Xen's "tmem", or
0017 in-kernel compressed memory, aka "zcache", or future RAM-like devices);
0018 this pseudo-RAM device is not directly accessible or addressable by the
0019 kernel and is of unknown and possibly time-varying size.  The driver
0020 links itself to frontswap by calling frontswap_register_ops to set the
0021 frontswap_ops funcs appropriately and the functions it provides must
0022 conform to certain policies as follows:
0023 
0024 An "init" prepares the device to receive frontswap pages associated
0025 with the specified swap device number (aka "type").  A "store" will
0026 copy the page to transcendent memory and associate it with the type and
0027 offset associated with the page. A "load" will copy the page, if found,
0028 from transcendent memory into kernel memory, but will NOT remove the page
0029 from transcendent memory.  An "invalidate_page" will remove the page
0030 from transcendent memory and an "invalidate_area" will remove ALL pages
0031 associated with the swap type (e.g., like swapoff) and notify the "device"
0032 to refuse further stores with that swap type.
0033 
0034 Once a page is successfully stored, a matching load on the page will normally
0035 succeed.  So when the kernel finds itself in a situation where it needs
0036 to swap out a page, it first attempts to use frontswap.  If the store returns
0037 success, the data has been successfully saved to transcendent memory and
0038 a disk write and, if the data is later read back, a disk read are avoided.
0039 If a store returns failure, transcendent memory has rejected the data, and the
0040 page can be written to swap as usual.
0041 
0042 Note that if a page is stored and the page already exists in transcendent memory
0043 (a "duplicate" store), either the store succeeds and the data is overwritten,
0044 or the store fails AND the page is invalidated.  This ensures stale data may
0045 never be obtained from frontswap.
0046 
0047 If properly configured, monitoring of frontswap is done via debugfs in
0048 the `/sys/kernel/debug/frontswap` directory.  The effectiveness of
0049 frontswap can be measured (across all swap devices) with:
0050 
0051 ``failed_stores``
0052         how many store attempts have failed
0053 
0054 ``loads``
0055         how many loads were attempted (all should succeed)
0056 
0057 ``succ_stores``
0058         how many store attempts have succeeded
0059 
0060 ``invalidates``
0061         how many invalidates were attempted
0062 
0063 A backend implementation may provide additional metrics.
0064 
0065 FAQ
0066 ===
0067 
0068 * Where's the value?
0069 
0070 When a workload starts swapping, performance falls through the floor.
0071 Frontswap significantly increases performance in many such workloads by
0072 providing a clean, dynamic interface to read and write swap pages to
0073 "transcendent memory" that is otherwise not directly addressable to the kernel.
0074 This interface is ideal when data is transformed to a different form
0075 and size (such as with compression) or secretly moved (as might be
0076 useful for write-balancing for some RAM-like devices).  Swap pages (and
0077 evicted page-cache pages) are a great use for this kind of slower-than-RAM-
0078 but-much-faster-than-disk "pseudo-RAM device".
0079 
0080 Frontswap with a fairly small impact on the kernel,
0081 provides a huge amount of flexibility for more dynamic, flexible RAM
0082 utilization in various system configurations:
0083 
0084 In the single kernel case, aka "zcache", pages are compressed and
0085 stored in local memory, thus increasing the total anonymous pages
0086 that can be safely kept in RAM.  Zcache essentially trades off CPU
0087 cycles used in compression/decompression for better memory utilization.
0088 Benchmarks have shown little or no impact when memory pressure is
0089 low while providing a significant performance improvement (25%+)
0090 on some workloads under high memory pressure.
0091 
0092 "RAMster" builds on zcache by adding "peer-to-peer" transcendent memory
0093 support for clustered systems.  Frontswap pages are locally compressed
0094 as in zcache, but then "remotified" to another system's RAM.  This
0095 allows RAM to be dynamically load-balanced back-and-forth as needed,
0096 i.e. when system A is overcommitted, it can swap to system B, and
0097 vice versa.  RAMster can also be configured as a memory server so
0098 many servers in a cluster can swap, dynamically as needed, to a single
0099 server configured with a large amount of RAM... without pre-configuring
0100 how much of the RAM is available for each of the clients!
0101 
0102 In the virtual case, the whole point of virtualization is to statistically
0103 multiplex physical resources across the varying demands of multiple
0104 virtual machines.  This is really hard to do with RAM and efforts to do
0105 it well with no kernel changes have essentially failed (except in some
0106 well-publicized special-case workloads).
0107 Specifically, the Xen Transcendent Memory backend allows otherwise
0108 "fallow" hypervisor-owned RAM to not only be "time-shared" between multiple
0109 virtual machines, but the pages can be compressed and deduplicated to
0110 optimize RAM utilization.  And when guest OS's are induced to surrender
0111 underutilized RAM (e.g. with "selfballooning"), sudden unexpected
0112 memory pressure may result in swapping; frontswap allows those pages
0113 to be swapped to and from hypervisor RAM (if overall host system memory
0114 conditions allow), thus mitigating the potentially awful performance impact
0115 of unplanned swapping.
0116 
0117 A KVM implementation is underway and has been RFC'ed to lkml.  And,
0118 using frontswap, investigation is also underway on the use of NVM as
0119 a memory extension technology.
0120 
0121 * Sure there may be performance advantages in some situations, but
0122   what's the space/time overhead of frontswap?
0123 
0124 If CONFIG_FRONTSWAP is disabled, every frontswap hook compiles into
0125 nothingness and the only overhead is a few extra bytes per swapon'ed
0126 swap device.  If CONFIG_FRONTSWAP is enabled but no frontswap "backend"
0127 registers, there is one extra global variable compared to zero for
0128 every swap page read or written.  If CONFIG_FRONTSWAP is enabled
0129 AND a frontswap backend registers AND the backend fails every "store"
0130 request (i.e. provides no memory despite claiming it might),
0131 CPU overhead is still negligible -- and since every frontswap fail
0132 precedes a swap page write-to-disk, the system is highly likely
0133 to be I/O bound and using a small fraction of a percent of a CPU
0134 will be irrelevant anyway.
0135 
0136 As for space, if CONFIG_FRONTSWAP is enabled AND a frontswap backend
0137 registers, one bit is allocated for every swap page for every swap
0138 device that is swapon'd.  This is added to the EIGHT bits (which
0139 was sixteen until about 2.6.34) that the kernel already allocates
0140 for every swap page for every swap device that is swapon'd.  (Hugh
0141 Dickins has observed that frontswap could probably steal one of
0142 the existing eight bits, but let's worry about that minor optimization
0143 later.)  For very large swap disks (which are rare) on a standard
0144 4K pagesize, this is 1MB per 32GB swap.
0145 
0146 When swap pages are stored in transcendent memory instead of written
0147 out to disk, there is a side effect that this may create more memory
0148 pressure that can potentially outweigh the other advantages.  A
0149 backend, such as zcache, must implement policies to carefully (but
0150 dynamically) manage memory limits to ensure this doesn't happen.
0151 
0152 * OK, how about a quick overview of what this frontswap patch does
0153   in terms that a kernel hacker can grok?
0154 
0155 Let's assume that a frontswap "backend" has registered during
0156 kernel initialization; this registration indicates that this
0157 frontswap backend has access to some "memory" that is not directly
0158 accessible by the kernel.  Exactly how much memory it provides is
0159 entirely dynamic and random.
0160 
0161 Whenever a swap-device is swapon'd frontswap_init() is called,
0162 passing the swap device number (aka "type") as a parameter.
0163 This notifies frontswap to expect attempts to "store" swap pages
0164 associated with that number.
0165 
0166 Whenever the swap subsystem is readying a page to write to a swap
0167 device (c.f swap_writepage()), frontswap_store is called.  Frontswap
0168 consults with the frontswap backend and if the backend says it does NOT
0169 have room, frontswap_store returns -1 and the kernel swaps the page
0170 to the swap device as normal.  Note that the response from the frontswap
0171 backend is unpredictable to the kernel; it may choose to never accept a
0172 page, it could accept every ninth page, or it might accept every
0173 page.  But if the backend does accept a page, the data from the page
0174 has already been copied and associated with the type and offset,
0175 and the backend guarantees the persistence of the data.  In this case,
0176 frontswap sets a bit in the "frontswap_map" for the swap device
0177 corresponding to the page offset on the swap device to which it would
0178 otherwise have written the data.
0179 
0180 When the swap subsystem needs to swap-in a page (swap_readpage()),
0181 it first calls frontswap_load() which checks the frontswap_map to
0182 see if the page was earlier accepted by the frontswap backend.  If
0183 it was, the page of data is filled from the frontswap backend and
0184 the swap-in is complete.  If not, the normal swap-in code is
0185 executed to obtain the page of data from the real swap device.
0186 
0187 So every time the frontswap backend accepts a page, a swap device read
0188 and (potentially) a swap device write are replaced by a "frontswap backend
0189 store" and (possibly) a "frontswap backend loads", which are presumably much
0190 faster.
0191 
0192 * Can't frontswap be configured as a "special" swap device that is
0193   just higher priority than any real swap device (e.g. like zswap,
0194   or maybe swap-over-nbd/NFS)?
0195 
0196 No.  First, the existing swap subsystem doesn't allow for any kind of
0197 swap hierarchy.  Perhaps it could be rewritten to accommodate a hierarchy,
0198 but this would require fairly drastic changes.  Even if it were
0199 rewritten, the existing swap subsystem uses the block I/O layer which
0200 assumes a swap device is fixed size and any page in it is linearly
0201 addressable.  Frontswap barely touches the existing swap subsystem,
0202 and works around the constraints of the block I/O subsystem to provide
0203 a great deal of flexibility and dynamicity.
0204 
0205 For example, the acceptance of any swap page by the frontswap backend is
0206 entirely unpredictable. This is critical to the definition of frontswap
0207 backends because it grants completely dynamic discretion to the
0208 backend.  In zcache, one cannot know a priori how compressible a page is.
0209 "Poorly" compressible pages can be rejected, and "poorly" can itself be
0210 defined dynamically depending on current memory constraints.
0211 
0212 Further, frontswap is entirely synchronous whereas a real swap
0213 device is, by definition, asynchronous and uses block I/O.  The
0214 block I/O layer is not only unnecessary, but may perform "optimizations"
0215 that are inappropriate for a RAM-oriented device including delaying
0216 the write of some pages for a significant amount of time.  Synchrony is
0217 required to ensure the dynamicity of the backend and to avoid thorny race
0218 conditions that would unnecessarily and greatly complicate frontswap
0219 and/or the block I/O subsystem.  That said, only the initial "store"
0220 and "load" operations need be synchronous.  A separate asynchronous thread
0221 is free to manipulate the pages stored by frontswap.  For example,
0222 the "remotification" thread in RAMster uses standard asynchronous
0223 kernel sockets to move compressed frontswap pages to a remote machine.
0224 Similarly, a KVM guest-side implementation could do in-guest compression
0225 and use "batched" hypercalls.
0226 
0227 In a virtualized environment, the dynamicity allows the hypervisor
0228 (or host OS) to do "intelligent overcommit".  For example, it can
0229 choose to accept pages only until host-swapping might be imminent,
0230 then force guests to do their own swapping.
0231 
0232 There is a downside to the transcendent memory specifications for
0233 frontswap:  Since any "store" might fail, there must always be a real
0234 slot on a real swap device to swap the page.  Thus frontswap must be
0235 implemented as a "shadow" to every swapon'd device with the potential
0236 capability of holding every page that the swap device might have held
0237 and the possibility that it might hold no pages at all.  This means
0238 that frontswap cannot contain more pages than the total of swapon'd
0239 swap devices.  For example, if NO swap device is configured on some
0240 installation, frontswap is useless.  Swapless portable devices
0241 can still use frontswap but a backend for such devices must configure
0242 some kind of "ghost" swap device and ensure that it is never used.
0243 
0244 * Why this weird definition about "duplicate stores"?  If a page
0245   has been previously successfully stored, can't it always be
0246   successfully overwritten?
0247 
0248 Nearly always it can, but no, sometimes it cannot.  Consider an example
0249 where data is compressed and the original 4K page has been compressed
0250 to 1K.  Now an attempt is made to overwrite the page with data that
0251 is non-compressible and so would take the entire 4K.  But the backend
0252 has no more space.  In this case, the store must be rejected.  Whenever
0253 frontswap rejects a store that would overwrite, it also must invalidate
0254 the old data and ensure that it is no longer accessible.  Since the
0255 swap subsystem then writes the new data to the read swap device,
0256 this is the correct course of action to ensure coherency.
0257 
0258 * Why does the frontswap patch create the new include file swapfile.h?
0259 
0260 The frontswap code depends on some swap-subsystem-internal data
0261 structures that have, over the years, moved back and forth between
0262 static and global.  This seemed a reasonable compromise:  Define
0263 them as global but declare them in a new include file that isn't
0264 included by the large number of source files that include swap.h.
0265 
0266 Dan Magenheimer, last updated April 9, 2012