ksm.rst 3.4 KB

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