~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/Documentation/mm/ksm.rst

Version: ~ [ linux-6.11-rc3 ] ~ [ linux-6.10.4 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.45 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.104 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.164 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.223 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.281 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.319 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 =======================
  2 Kernel Samepage Merging
  3 =======================
  4 
  5 KSM is a memory-saving de-duplication feature, enabled by CONFIG_KSM=y,
  6 added to the Linux kernel in 2.6.32.  See ``mm/ksm.c`` for its implementation,
  7 and http://lwn.net/Articles/306704/ and https://lwn.net/Articles/330589/
  8 
  9 The userspace interface of KSM is described in Documentation/admin-guide/mm/ksm.rst
 10 
 11 Design
 12 ======
 13 
 14 Overview
 15 --------
 16 
 17 .. kernel-doc:: mm/ksm.c
 18    :DOC: Overview
 19 
 20 Reverse mapping
 21 ---------------
 22 KSM maintains reverse mapping information for KSM pages in the stable
 23 tree.
 24 
 25 If a KSM page is shared between less than ``max_page_sharing`` VMAs,
 26 the node of the stable tree that represents such KSM page points to a
 27 list of struct ksm_rmap_item and the ``page->mapping`` of the
 28 KSM page points to the stable tree node.
 29 
 30 When the sharing passes this threshold, KSM adds a second dimension to
 31 the stable tree. The tree node becomes a "chain" that links one or
 32 more "dups". Each "dup" keeps reverse mapping information for a KSM
 33 page with ``page->mapping`` pointing to that "dup".
 34 
 35 Every "chain" and all "dups" linked into a "chain" enforce the
 36 invariant that they represent the same write protected memory content,
 37 even if each "dup" will be pointed by a different KSM page copy of
 38 that content.
 39 
 40 This way the stable tree lookup computational complexity is unaffected
 41 if compared to an unlimited list of reverse mappings. It is still
 42 enforced that there cannot be KSM page content duplicates in the
 43 stable tree itself.
 44 
 45 The deduplication limit enforced by ``max_page_sharing`` is required
 46 to avoid the virtual memory rmap lists to grow too large. The rmap
 47 walk has O(N) complexity where N is the number of rmap_items
 48 (i.e. virtual mappings) that are sharing the page, which is in turn
 49 capped by ``max_page_sharing``. So this effectively spreads the linear
 50 O(N) computational complexity from rmap walk context over different
 51 KSM pages. The ksmd walk over the stable_node "chains" is also O(N),
 52 but N is the number of stable_node "dups", not the number of
 53 rmap_items, so it has not a significant impact on ksmd performance. In
 54 practice the best stable_node "dup" candidate will be kept and found
 55 at the head of the "dups" list.
 56 
 57 High values of ``max_page_sharing`` result in faster memory merging
 58 (because there will be fewer stable_node dups queued into the
 59 stable_node chain->hlist to check for pruning) and higher
 60 deduplication factor at the expense of slower worst case for rmap
 61 walks for any KSM page which can happen during swapping, compaction,
 62 NUMA balancing and page migration.
 63 
 64 The ``stable_node_dups/stable_node_chains`` ratio is also affected by the
 65 ``max_page_sharing`` tunable, and an high ratio may indicate fragmentation
 66 in the stable_node dups, which could be solved by introducing
 67 fragmentation algorithms in ksmd which would refile rmap_items from
 68 one stable_node dup to another stable_node dup, in order to free up
 69 stable_node "dups" with few rmap_items in them, but that may increase
 70 the ksmd CPU usage and possibly slowdown the readonly computations on
 71 the KSM pages of the applications.
 72 
 73 The whole list of stable_node "dups" linked in the stable_node
 74 "chains" is scanned periodically in order to prune stale stable_nodes.
 75 The frequency of such scans is defined by
 76 ``stable_node_chains_prune_millisecs`` sysfs tunable.
 77 
 78 Reference
 79 ---------
 80 .. kernel-doc:: mm/ksm.c
 81    :functions: mm_slot ksm_scan stable_node rmap_item
 82 
 83 --
 84 Izik Eidus,
 85 Hugh Dickins, 17 Nov 2009

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php