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