brintos

brintos / linux-shallow public Read only

0
0
Text · 3.4 KiB · 2806e3e Raw
86 lines · plain
1=======================2Kernel Samepage Merging3=======================4 5KSM is a memory-saving de-duplication feature, enabled by CONFIG_KSM=y,6added to the Linux kernel in 2.6.32.  See ``mm/ksm.c`` for its implementation,7and http://lwn.net/Articles/306704/ and https://lwn.net/Articles/330589/8 9The userspace interface of KSM is described in Documentation/admin-guide/mm/ksm.rst10 11Design12======13 14Overview15--------16 17.. kernel-doc:: mm/ksm.c18   :DOC: Overview19 20Reverse mapping21---------------22KSM maintains reverse mapping information for KSM pages in the stable23tree.24 25If a KSM page is shared between less than ``max_page_sharing`` VMAs,26the node of the stable tree that represents such KSM page points to a27list of struct ksm_rmap_item and the ``page->mapping`` of the28KSM page points to the stable tree node.29 30When the sharing passes this threshold, KSM adds a second dimension to31the stable tree. The tree node becomes a "chain" that links one or32more "dups". Each "dup" keeps reverse mapping information for a KSM33page with ``page->mapping`` pointing to that "dup".34 35Every "chain" and all "dups" linked into a "chain" enforce the36invariant that they represent the same write protected memory content,37even if each "dup" will be pointed by a different KSM page copy of38that content.39 40This way the stable tree lookup computational complexity is unaffected41if compared to an unlimited list of reverse mappings. It is still42enforced that there cannot be KSM page content duplicates in the43stable tree itself.44 45The deduplication limit enforced by ``max_page_sharing`` is required46to avoid the virtual memory rmap lists to grow too large. The rmap47walk has O(N) complexity where N is the number of rmap_items48(i.e. virtual mappings) that are sharing the page, which is in turn49capped by ``max_page_sharing``. So this effectively spreads the linear50O(N) computational complexity from rmap walk context over different51KSM pages. The ksmd walk over the stable_node "chains" is also O(N),52but N is the number of stable_node "dups", not the number of53rmap_items, so it has not a significant impact on ksmd performance. In54practice the best stable_node "dup" candidate will be kept and found55at the head of the "dups" list.56 57High values of ``max_page_sharing`` result in faster memory merging58(because there will be fewer stable_node dups queued into the59stable_node chain->hlist to check for pruning) and higher60deduplication factor at the expense of slower worst case for rmap61walks for any KSM page which can happen during swapping, compaction,62NUMA balancing and page migration.63 64The ``stable_node_dups/stable_node_chains`` ratio is also affected by the65``max_page_sharing`` tunable, and an high ratio may indicate fragmentation66in the stable_node dups, which could be solved by introducing67fragmentation algorithms in ksmd which would refile rmap_items from68one stable_node dup to another stable_node dup, in order to free up69stable_node "dups" with few rmap_items in them, but that may increase70the ksmd CPU usage and possibly slowdown the readonly computations on71the KSM pages of the applications.72 73The whole list of stable_node "dups" linked in the stable_node74"chains" is scanned periodically in order to prune stale stable_nodes.75The frequency of such scans is defined by76``stable_node_chains_prune_millisecs`` sysfs tunable.77 78Reference79---------80.. kernel-doc:: mm/ksm.c81   :functions: mm_slot ksm_scan stable_node rmap_item82 83--84Izik Eidus,85Hugh Dickins, 17 Nov 200986