ksm.rst 3.4 KB

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