cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

cache.rst (13262B)


      1=====
      2Cache
      3=====
      4
      5Introduction
      6============
      7
      8dm-cache is a device mapper target written by Joe Thornber, Heinz
      9Mauelshagen, and Mike Snitzer.
     10
     11It aims to improve performance of a block device (eg, a spindle) by
     12dynamically migrating some of its data to a faster, smaller device
     13(eg, an SSD).
     14
     15This device-mapper solution allows us to insert this caching at
     16different levels of the dm stack, for instance above the data device for
     17a thin-provisioning pool.  Caching solutions that are integrated more
     18closely with the virtual memory system should give better performance.
     19
     20The target reuses the metadata library used in the thin-provisioning
     21library.
     22
     23The decision as to what data to migrate and when is left to a plug-in
     24policy module.  Several of these have been written as we experiment,
     25and we hope other people will contribute others for specific io
     26scenarios (eg. a vm image server).
     27
     28Glossary
     29========
     30
     31  Migration
     32	       Movement of the primary copy of a logical block from one
     33	       device to the other.
     34  Promotion
     35	       Migration from slow device to fast device.
     36  Demotion
     37	       Migration from fast device to slow device.
     38
     39The origin device always contains a copy of the logical block, which
     40may be out of date or kept in sync with the copy on the cache device
     41(depending on policy).
     42
     43Design
     44======
     45
     46Sub-devices
     47-----------
     48
     49The target is constructed by passing three devices to it (along with
     50other parameters detailed later):
     51
     521. An origin device - the big, slow one.
     53
     542. A cache device - the small, fast one.
     55
     563. A small metadata device - records which blocks are in the cache,
     57   which are dirty, and extra hints for use by the policy object.
     58   This information could be put on the cache device, but having it
     59   separate allows the volume manager to configure it differently,
     60   e.g. as a mirror for extra robustness.  This metadata device may only
     61   be used by a single cache device.
     62
     63Fixed block size
     64----------------
     65
     66The origin is divided up into blocks of a fixed size.  This block size
     67is configurable when you first create the cache.  Typically we've been
     68using block sizes of 256KB - 1024KB.  The block size must be between 64
     69sectors (32KB) and 2097152 sectors (1GB) and a multiple of 64 sectors (32KB).
     70
     71Having a fixed block size simplifies the target a lot.  But it is
     72something of a compromise.  For instance, a small part of a block may be
     73getting hit a lot, yet the whole block will be promoted to the cache.
     74So large block sizes are bad because they waste cache space.  And small
     75block sizes are bad because they increase the amount of metadata (both
     76in core and on disk).
     77
     78Cache operating modes
     79---------------------
     80
     81The cache has three operating modes: writeback, writethrough and
     82passthrough.
     83
     84If writeback, the default, is selected then a write to a block that is
     85cached will go only to the cache and the block will be marked dirty in
     86the metadata.
     87
     88If writethrough is selected then a write to a cached block will not
     89complete until it has hit both the origin and cache devices.  Clean
     90blocks should remain clean.
     91
     92If passthrough is selected, useful when the cache contents are not known
     93to be coherent with the origin device, then all reads are served from
     94the origin device (all reads miss the cache) and all writes are
     95forwarded to the origin device; additionally, write hits cause cache
     96block invalidates.  To enable passthrough mode the cache must be clean.
     97Passthrough mode allows a cache device to be activated without having to
     98worry about coherency.  Coherency that exists is maintained, although
     99the cache will gradually cool as writes take place.  If the coherency of
    100the cache can later be verified, or established through use of the
    101"invalidate_cblocks" message, the cache device can be transitioned to
    102writethrough or writeback mode while still warm.  Otherwise, the cache
    103contents can be discarded prior to transitioning to the desired
    104operating mode.
    105
    106A simple cleaner policy is provided, which will clean (write back) all
    107dirty blocks in a cache.  Useful for decommissioning a cache or when
    108shrinking a cache.  Shrinking the cache's fast device requires all cache
    109blocks, in the area of the cache being removed, to be clean.  If the
    110area being removed from the cache still contains dirty blocks the resize
    111will fail.  Care must be taken to never reduce the volume used for the
    112cache's fast device until the cache is clean.  This is of particular
    113importance if writeback mode is used.  Writethrough and passthrough
    114modes already maintain a clean cache.  Future support to partially clean
    115the cache, above a specified threshold, will allow for keeping the cache
    116warm and in writeback mode during resize.
    117
    118Migration throttling
    119--------------------
    120
    121Migrating data between the origin and cache device uses bandwidth.
    122The user can set a throttle to prevent more than a certain amount of
    123migration occurring at any one time.  Currently we're not taking any
    124account of normal io traffic going to the devices.  More work needs
    125doing here to avoid migrating during those peak io moments.
    126
    127For the time being, a message "migration_threshold <#sectors>"
    128can be used to set the maximum number of sectors being migrated,
    129the default being 2048 sectors (1MB).
    130
    131Updating on-disk metadata
    132-------------------------
    133
    134On-disk metadata is committed every time a FLUSH or FUA bio is written.
    135If no such requests are made then commits will occur every second.  This
    136means the cache behaves like a physical disk that has a volatile write
    137cache.  If power is lost you may lose some recent writes.  The metadata
    138should always be consistent in spite of any crash.
    139
    140The 'dirty' state for a cache block changes far too frequently for us
    141to keep updating it on the fly.  So we treat it as a hint.  In normal
    142operation it will be written when the dm device is suspended.  If the
    143system crashes all cache blocks will be assumed dirty when restarted.
    144
    145Per-block policy hints
    146----------------------
    147
    148Policy plug-ins can store a chunk of data per cache block.  It's up to
    149the policy how big this chunk is, but it should be kept small.  Like the
    150dirty flags this data is lost if there's a crash so a safe fallback
    151value should always be possible.
    152
    153Policy hints affect performance, not correctness.
    154
    155Policy messaging
    156----------------
    157
    158Policies will have different tunables, specific to each one, so we
    159need a generic way of getting and setting these.  Device-mapper
    160messages are used.  Refer to cache-policies.txt.
    161
    162Discard bitset resolution
    163-------------------------
    164
    165We can avoid copying data during migration if we know the block has
    166been discarded.  A prime example of this is when mkfs discards the
    167whole block device.  We store a bitset tracking the discard state of
    168blocks.  However, we allow this bitset to have a different block size
    169from the cache blocks.  This is because we need to track the discard
    170state for all of the origin device (compare with the dirty bitset
    171which is just for the smaller cache device).
    172
    173Target interface
    174================
    175
    176Constructor
    177-----------
    178
    179  ::
    180
    181   cache <metadata dev> <cache dev> <origin dev> <block size>
    182         <#feature args> [<feature arg>]*
    183         <policy> <#policy args> [policy args]*
    184
    185 ================ =======================================================
    186 metadata dev     fast device holding the persistent metadata
    187 cache dev	  fast device holding cached data blocks
    188 origin dev	  slow device holding original data blocks
    189 block size       cache unit size in sectors
    190
    191 #feature args    number of feature arguments passed
    192 feature args     writethrough or passthrough (The default is writeback.)
    193
    194 policy           the replacement policy to use
    195 #policy args     an even number of arguments corresponding to
    196                  key/value pairs passed to the policy
    197 policy args      key/value pairs passed to the policy
    198		  E.g. 'sequential_threshold 1024'
    199		  See cache-policies.txt for details.
    200 ================ =======================================================
    201
    202Optional feature arguments are:
    203
    204
    205   ==================== ========================================================
    206   writethrough		write through caching that prohibits cache block
    207			content from being different from origin block content.
    208			Without this argument, the default behaviour is to write
    209			back cache block contents later for performance reasons,
    210			so they may differ from the corresponding origin blocks.
    211
    212   passthrough		a degraded mode useful for various cache coherency
    213			situations (e.g., rolling back snapshots of
    214			underlying storage).	 Reads and writes always go to
    215			the origin.	If a write goes to a cached origin
    216			block, then the cache block is invalidated.
    217			To enable passthrough mode the cache must be clean.
    218
    219   metadata2		use version 2 of the metadata.  This stores the dirty
    220			bits in a separate btree, which improves speed of
    221			shutting down the cache.
    222
    223   no_discard_passdown	disable passing down discards from the cache
    224			to the origin's data device.
    225   ==================== ========================================================
    226
    227A policy called 'default' is always registered.  This is an alias for
    228the policy we currently think is giving best all round performance.
    229
    230As the default policy could vary between kernels, if you are relying on
    231the characteristics of a specific policy, always request it by name.
    232
    233Status
    234------
    235
    236::
    237
    238  <metadata block size> <#used metadata blocks>/<#total metadata blocks>
    239  <cache block size> <#used cache blocks>/<#total cache blocks>
    240  <#read hits> <#read misses> <#write hits> <#write misses>
    241  <#demotions> <#promotions> <#dirty> <#features> <features>*
    242  <#core args> <core args>* <policy name> <#policy args> <policy args>*
    243  <cache metadata mode>
    244
    245
    246========================= =====================================================
    247metadata block size	  Fixed block size for each metadata block in
    248			  sectors
    249#used metadata blocks	  Number of metadata blocks used
    250#total metadata blocks	  Total number of metadata blocks
    251cache block size	  Configurable block size for the cache device
    252			  in sectors
    253#used cache blocks	  Number of blocks resident in the cache
    254#total cache blocks	  Total number of cache blocks
    255#read hits		  Number of times a READ bio has been mapped
    256			  to the cache
    257#read misses		  Number of times a READ bio has been mapped
    258			  to the origin
    259#write hits		  Number of times a WRITE bio has been mapped
    260			  to the cache
    261#write misses		  Number of times a WRITE bio has been
    262			  mapped to the origin
    263#demotions		  Number of times a block has been removed
    264			  from the cache
    265#promotions		  Number of times a block has been moved to
    266			  the cache
    267#dirty			  Number of blocks in the cache that differ
    268			  from the origin
    269#feature args		  Number of feature args to follow
    270feature args		  'writethrough' (optional)
    271#core args		  Number of core arguments (must be even)
    272core args		  Key/value pairs for tuning the core
    273			  e.g. migration_threshold
    274policy name		  Name of the policy
    275#policy args		  Number of policy arguments to follow (must be even)
    276policy args		  Key/value pairs e.g. sequential_threshold
    277cache metadata mode       ro if read-only, rw if read-write
    278
    279			  In serious cases where even a read-only mode is
    280			  deemed unsafe no further I/O will be permitted and
    281			  the status will just contain the string 'Fail'.
    282			  The userspace recovery tools should then be used.
    283needs_check		  'needs_check' if set, '-' if not set
    284			  A metadata operation has failed, resulting in the
    285			  needs_check flag being set in the metadata's
    286			  superblock.  The metadata device must be
    287			  deactivated and checked/repaired before the
    288			  cache can be made fully operational again.
    289			  '-' indicates	needs_check is not set.
    290========================= =====================================================
    291
    292Messages
    293--------
    294
    295Policies will have different tunables, specific to each one, so we
    296need a generic way of getting and setting these.  Device-mapper
    297messages are used.  (A sysfs interface would also be possible.)
    298
    299The message format is::
    300
    301   <key> <value>
    302
    303E.g.::
    304
    305   dmsetup message my_cache 0 sequential_threshold 1024
    306
    307
    308Invalidation is removing an entry from the cache without writing it
    309back.  Cache blocks can be invalidated via the invalidate_cblocks
    310message, which takes an arbitrary number of cblock ranges.  Each cblock
    311range's end value is "one past the end", meaning 5-10 expresses a range
    312of values from 5 to 9.  Each cblock must be expressed as a decimal
    313value, in the future a variant message that takes cblock ranges
    314expressed in hexadecimal may be needed to better support efficient
    315invalidation of larger caches.  The cache must be in passthrough mode
    316when invalidate_cblocks is used::
    317
    318   invalidate_cblocks [<cblock>|<cblock begin>-<cblock end>]*
    319
    320E.g.::
    321
    322   dmsetup message my_cache 0 invalidate_cblocks 2345 3456-4567 5678-6789
    323
    324Examples
    325========
    326
    327The test suite can be found here:
    328
    329https://github.com/jthornber/device-mapper-test-suite
    330
    331::
    332
    333  dmsetup create my_cache --table '0 41943040 cache /dev/mapper/metadata \
    334	  /dev/mapper/ssd /dev/mapper/origin 512 1 writeback default 0'
    335  dmsetup create my_cache --table '0 41943040 cache /dev/mapper/metadata \
    336	  /dev/mapper/ssd /dev/mapper/origin 1024 1 writeback \
    337	  mq 4 sequential_threshold 1024 random_threshold 8'