Back to home page

OSCL-LXR

 
 

    


0001 .. _ksm:
0002 
0003 =======================
0004 Kernel Samepage Merging
0005 =======================
0006 
0007 KSM is a memory-saving de-duplication feature, enabled by CONFIG_KSM=y,
0008 added to the Linux kernel in 2.6.32.  See ``mm/ksm.c`` for its implementation,
0009 and http://lwn.net/Articles/306704/ and https://lwn.net/Articles/330589/
0010 
0011 The userspace interface of KSM is described in :ref:`Documentation/admin-guide/mm/ksm.rst <admin_guide_ksm>`
0012 
0013 Design
0014 ======
0015 
0016 Overview
0017 --------
0018 
0019 .. kernel-doc:: mm/ksm.c
0020    :DOC: Overview
0021 
0022 Reverse mapping
0023 ---------------
0024 KSM maintains reverse mapping information for KSM pages in the stable
0025 tree.
0026 
0027 If a KSM page is shared between less than ``max_page_sharing`` VMAs,
0028 the node of the stable tree that represents such KSM page points to a
0029 list of struct rmap_item and the ``page->mapping`` of the
0030 KSM page points to the stable tree node.
0031 
0032 When the sharing passes this threshold, KSM adds a second dimension to
0033 the stable tree. The tree node becomes a "chain" that links one or
0034 more "dups". Each "dup" keeps reverse mapping information for a KSM
0035 page with ``page->mapping`` pointing to that "dup".
0036 
0037 Every "chain" and all "dups" linked into a "chain" enforce the
0038 invariant that they represent the same write protected memory content,
0039 even if each "dup" will be pointed by a different KSM page copy of
0040 that content.
0041 
0042 This way the stable tree lookup computational complexity is unaffected
0043 if compared to an unlimited list of reverse mappings. It is still
0044 enforced that there cannot be KSM page content duplicates in the
0045 stable tree itself.
0046 
0047 The deduplication limit enforced by ``max_page_sharing`` is required
0048 to avoid the virtual memory rmap lists to grow too large. The rmap
0049 walk has O(N) complexity where N is the number of rmap_items
0050 (i.e. virtual mappings) that are sharing the page, which is in turn
0051 capped by ``max_page_sharing``. So this effectively spreads the linear
0052 O(N) computational complexity from rmap walk context over different
0053 KSM pages. The ksmd walk over the stable_node "chains" is also O(N),
0054 but N is the number of stable_node "dups", not the number of
0055 rmap_items, so it has not a significant impact on ksmd performance. In
0056 practice the best stable_node "dup" candidate will be kept and found
0057 at the head of the "dups" list.
0058 
0059 High values of ``max_page_sharing`` result in faster memory merging
0060 (because there will be fewer stable_node dups queued into the
0061 stable_node chain->hlist to check for pruning) and higher
0062 deduplication factor at the expense of slower worst case for rmap
0063 walks for any KSM page which can happen during swapping, compaction,
0064 NUMA balancing and page migration.
0065 
0066 The ``stable_node_dups/stable_node_chains`` ratio is also affected by the
0067 ``max_page_sharing`` tunable, and an high ratio may indicate fragmentation
0068 in the stable_node dups, which could be solved by introducing
0069 fragmentation algorithms in ksmd which would refile rmap_items from
0070 one stable_node dup to another stable_node dup, in order to free up
0071 stable_node "dups" with few rmap_items in them, but that may increase
0072 the ksmd CPU usage and possibly slowdown the readonly computations on
0073 the KSM pages of the applications.
0074 
0075 The whole list of stable_node "dups" linked in the stable_node
0076 "chains" is scanned periodically in order to prune stale stable_nodes.
0077 The frequency of such scans is defined by
0078 ``stable_node_chains_prune_millisecs`` sysfs tunable.
0079 
0080 Reference
0081 ---------
0082 .. kernel-doc:: mm/ksm.c
0083    :functions: mm_slot ksm_scan stable_node rmap_item
0084 
0085 --
0086 Izik Eidus,
0087 Hugh Dickins, 17 Nov 2009