xref: /openbmc/linux/Documentation/admin-guide/mm/ksm.rst (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
1c9161088SMike Rapoport=======================
2c9161088SMike RapoportKernel Samepage Merging
3c9161088SMike Rapoport=======================
4c9161088SMike Rapoport
5c9161088SMike RapoportOverview
6c9161088SMike Rapoport========
7c9161088SMike Rapoport
8c9161088SMike RapoportKSM is a memory-saving de-duplication feature, enabled by CONFIG_KSM=y,
9c9161088SMike Rapoportadded to the Linux kernel in 2.6.32.  See ``mm/ksm.c`` for its implementation,
106b2484e1SAlexander A. Klimovand http://lwn.net/Articles/306704/ and https://lwn.net/Articles/330589/
11c9161088SMike Rapoport
12c9161088SMike RapoportKSM was originally developed for use with KVM (where it was known as
13c9161088SMike RapoportKernel Shared Memory), to fit more virtual machines into physical memory,
14c9161088SMike Rapoportby sharing the data common between them.  But it can be useful to any
15c9161088SMike Rapoportapplication which generates many instances of the same data.
16c9161088SMike Rapoport
17c9161088SMike RapoportThe KSM daemon ksmd periodically scans those areas of user memory
18c9161088SMike Rapoportwhich have been registered with it, looking for pages of identical
19c9161088SMike Rapoportcontent which can be replaced by a single write-protected page (which
20c9161088SMike Rapoportis automatically copied if a process later wants to update its
21c9161088SMike Rapoportcontent). The amount of pages that KSM daemon scans in a single pass
22c9161088SMike Rapoportand the time between the passes are configured using :ref:`sysfs
230b656310SDonald Hunterinterface <ksm_sysfs>`
24c9161088SMike Rapoport
25c9161088SMike RapoportKSM only merges anonymous (private) pages, never pagecache (file) pages.
26c9161088SMike RapoportKSM's merged pages were originally locked into kernel memory, but can now
27c9161088SMike Rapoportbe swapped out just like other user pages (but sharing is broken when they
28c9161088SMike Rapoportare swapped back in: ksmd must rediscover their identity and merge again).
29c9161088SMike Rapoport
30c9161088SMike RapoportControlling KSM with madvise
31c9161088SMike Rapoport============================
32c9161088SMike Rapoport
33c9161088SMike RapoportKSM only operates on those areas of address space which an application
34c9161088SMike Rapoporthas advised to be likely candidates for merging, by using the madvise(2)
35c9161088SMike Rapoportsystem call::
36c9161088SMike Rapoport
37c9161088SMike Rapoport	int madvise(addr, length, MADV_MERGEABLE)
38c9161088SMike Rapoport
39c9161088SMike RapoportThe app may call
40c9161088SMike Rapoport
41c9161088SMike Rapoport::
42c9161088SMike Rapoport
43c9161088SMike Rapoport	int madvise(addr, length, MADV_UNMERGEABLE)
44c9161088SMike Rapoport
45c9161088SMike Rapoportto cancel that advice and restore unshared pages: whereupon KSM
46c9161088SMike Rapoportunmerges whatever it merged in that range.  Note: this unmerging call
47c9161088SMike Rapoportmay suddenly require more memory than is available - possibly failing
48c9161088SMike Rapoportwith EAGAIN, but more probably arousing the Out-Of-Memory killer.
49c9161088SMike Rapoport
50c9161088SMike RapoportIf KSM is not configured into the running kernel, madvise MADV_MERGEABLE
51c9161088SMike Rapoportand MADV_UNMERGEABLE simply fail with EINVAL.  If the running kernel was
52c9161088SMike Rapoportbuilt with CONFIG_KSM=y, those calls will normally succeed: even if the
53b32dae55SRandy DunlapKSM daemon is not currently running, MADV_MERGEABLE still registers
54c9161088SMike Rapoportthe range for whenever the KSM daemon is started; even if the range
55c9161088SMike Rapoportcannot contain any pages which KSM could actually merge; even if
56c9161088SMike RapoportMADV_UNMERGEABLE is applied to a range which was never MADV_MERGEABLE.
57c9161088SMike Rapoport
58c9161088SMike RapoportIf a region of memory must be split into at least one new MADV_MERGEABLE
59c9161088SMike Rapoportor MADV_UNMERGEABLE region, the madvise may return ENOMEM if the process
6057043247SMauro Carvalho Chehabwill exceed ``vm.max_map_count`` (see Documentation/admin-guide/sysctl/vm.rst).
61c9161088SMike Rapoport
62c9161088SMike RapoportLike other madvise calls, they are intended for use on mapped areas of
63c9161088SMike Rapoportthe user address space: they will report ENOMEM if the specified range
64c9161088SMike Rapoportincludes unmapped gaps (though working on the intervening mapped areas),
65c9161088SMike Rapoportand might fail with EAGAIN if not enough memory for internal structures.
66c9161088SMike Rapoport
67c9161088SMike RapoportApplications should be considerate in their use of MADV_MERGEABLE,
68c9161088SMike Rapoportrestricting its use to areas likely to benefit.  KSM's scans may use a lot
69c9161088SMike Rapoportof processing power: some installations will disable KSM for that reason.
70c9161088SMike Rapoport
71c9161088SMike Rapoport.. _ksm_sysfs:
72c9161088SMike Rapoport
73c9161088SMike RapoportKSM daemon sysfs interface
74c9161088SMike Rapoport==========================
75c9161088SMike Rapoport
76c9161088SMike RapoportThe KSM daemon is controlled by sysfs files in ``/sys/kernel/mm/ksm/``,
77c9161088SMike Rapoportreadable by all but writable only by root:
78c9161088SMike Rapoport
79c9161088SMike Rapoportpages_to_scan
80c9161088SMike Rapoport        how many pages to scan before ksmd goes to sleep
81c9161088SMike Rapoport        e.g. ``echo 100 > /sys/kernel/mm/ksm/pages_to_scan``.
82c9161088SMike Rapoport
83c9161088SMike Rapoport        Default: 100 (chosen for demonstration purposes)
84c9161088SMike Rapoport
85c9161088SMike Rapoportsleep_millisecs
86c9161088SMike Rapoport        how many milliseconds ksmd should sleep before next scan
87c9161088SMike Rapoport        e.g. ``echo 20 > /sys/kernel/mm/ksm/sleep_millisecs``
88c9161088SMike Rapoport
89c9161088SMike Rapoport        Default: 20 (chosen for demonstration purposes)
90c9161088SMike Rapoport
91c9161088SMike Rapoportmerge_across_nodes
92c9161088SMike Rapoport        specifies if pages from different NUMA nodes can be merged.
93c9161088SMike Rapoport        When set to 0, ksm merges only pages which physically reside
94c9161088SMike Rapoport        in the memory area of same NUMA node. That brings lower
95c9161088SMike Rapoport        latency to access of shared pages. Systems with more nodes, at
96c9161088SMike Rapoport        significant NUMA distances, are likely to benefit from the
97c9161088SMike Rapoport        lower latency of setting 0. Smaller systems, which need to
98c9161088SMike Rapoport        minimize memory usage, are likely to benefit from the greater
99c9161088SMike Rapoport        sharing of setting 1 (default). You may wish to compare how
100c9161088SMike Rapoport        your system performs under each setting, before deciding on
101c9161088SMike Rapoport        which to use. ``merge_across_nodes`` setting can be changed only
102c9161088SMike Rapoport        when there are no ksm shared pages in the system: set run 2 to
103c9161088SMike Rapoport        unmerge pages first, then to 1 after changing
104c9161088SMike Rapoport        ``merge_across_nodes``, to remerge according to the new setting.
105c9161088SMike Rapoport
106c9161088SMike Rapoport        Default: 1 (merging across nodes as in earlier releases)
107c9161088SMike Rapoport
108c9161088SMike Rapoportrun
109c9161088SMike Rapoport        * set to 0 to stop ksmd from running but keep merged pages,
110c9161088SMike Rapoport        * set to 1 to run ksmd e.g. ``echo 1 > /sys/kernel/mm/ksm/run``,
111c9161088SMike Rapoport        * set to 2 to stop ksmd and unmerge all pages currently merged, but
112c9161088SMike Rapoport	  leave mergeable areas registered for next run.
113c9161088SMike Rapoport
114c9161088SMike Rapoport        Default: 0 (must be changed to 1 to activate KSM, except if
115c9161088SMike Rapoport        CONFIG_SYSFS is disabled)
116c9161088SMike Rapoport
117c9161088SMike Rapoportuse_zero_pages
118c9161088SMike Rapoport        specifies whether empty pages (i.e. allocated pages that only
119c9161088SMike Rapoport        contain zeroes) should be treated specially.  When set to 1,
120c9161088SMike Rapoport        empty pages are merged with the kernel zero page(s) instead of
121c9161088SMike Rapoport        with each other as it would happen normally. This can improve
122c9161088SMike Rapoport        the performance on architectures with coloured zero pages,
123c9161088SMike Rapoport        depending on the workload. Care should be taken when enabling
124c9161088SMike Rapoport        this setting, as it can potentially degrade the performance of
125c9161088SMike Rapoport        KSM for some workloads, for example if the checksums of pages
126c9161088SMike Rapoport        candidate for merging match the checksum of an empty
127c9161088SMike Rapoport        page. This setting can be changed at any time, it is only
128c9161088SMike Rapoport        effective for pages merged after the change.
129c9161088SMike Rapoport
130c9161088SMike Rapoport        Default: 0 (normal KSM behaviour as in earlier releases)
131c9161088SMike Rapoport
132c9161088SMike Rapoportmax_page_sharing
133c9161088SMike Rapoport        Maximum sharing allowed for each KSM page. This enforces a
134c9161088SMike Rapoport        deduplication limit to avoid high latency for virtual memory
135c9161088SMike Rapoport        operations that involve traversal of the virtual mappings that
136c9161088SMike Rapoport        share the KSM page. The minimum value is 2 as a newly created
137c9161088SMike Rapoport        KSM page will have at least two sharers. The higher this value
138c9161088SMike Rapoport        the faster KSM will merge the memory and the higher the
139c9161088SMike Rapoport        deduplication factor will be, but the slower the worst case
140c9161088SMike Rapoport        virtual mappings traversal could be for any given KSM
141c9161088SMike Rapoport        page. Slowing down this traversal means there will be higher
142c9161088SMike Rapoport        latency for certain virtual memory operations happening during
143c9161088SMike Rapoport        swapping, compaction, NUMA balancing and page migration, in
144c9161088SMike Rapoport        turn decreasing responsiveness for the caller of those virtual
145c9161088SMike Rapoport        memory operations. The scheduler latency of other tasks not
146c9161088SMike Rapoport        involved with the VM operations doing the virtual mappings
147c9161088SMike Rapoport        traversal is not affected by this parameter as these
148c9161088SMike Rapoport        traversals are always schedule friendly themselves.
149c9161088SMike Rapoport
150c9161088SMike Rapoportstable_node_chains_prune_millisecs
151c9161088SMike Rapoport        specifies how frequently KSM checks the metadata of the pages
152c9161088SMike Rapoport        that hit the deduplication limit for stale information.
153c9161088SMike Rapoport        Smaller milllisecs values will free up the KSM metadata with
154c9161088SMike Rapoport        lower latency, but they will make ksmd use more CPU during the
155c9161088SMike Rapoport        scan. It's a noop if not a single KSM page hit the
156c9161088SMike Rapoport        ``max_page_sharing`` yet.
157c9161088SMike Rapoport
158c9161088SMike RapoportThe effectiveness of KSM and MADV_MERGEABLE is shown in ``/sys/kernel/mm/ksm/``:
159c9161088SMike Rapoport
160d21077fbSStefan Roeschgeneral_profit
161d21077fbSStefan Roesch        how effective is KSM. The calculation is explained below.
162*b348b5feSStefan Roeschpages_scanned
163*b348b5feSStefan Roesch        how many pages are being scanned for ksm
164c9161088SMike Rapoportpages_shared
165c9161088SMike Rapoport        how many shared pages are being used
166c9161088SMike Rapoportpages_sharing
167c9161088SMike Rapoport        how many more sites are sharing them i.e. how much saved
168c9161088SMike Rapoportpages_unshared
169c9161088SMike Rapoport        how many pages unique but repeatedly checked for merging
170c9161088SMike Rapoportpages_volatile
171c9161088SMike Rapoport        how many pages changing too fast to be placed in a tree
172c9161088SMike Rapoportfull_scans
173c9161088SMike Rapoport        how many times all mergeable areas have been scanned
174c9161088SMike Rapoportstable_node_chains
175c9161088SMike Rapoport        the number of KSM pages that hit the ``max_page_sharing`` limit
176c9161088SMike Rapoportstable_node_dups
177c9161088SMike Rapoport        number of duplicated KSM pages
178e2942062Sxu xinksm_zero_pages
179e2942062Sxu xin        how many zero pages that are still mapped into processes were mapped by
180e2942062Sxu xin        KSM when deduplicating.
181e2942062Sxu xin
182e2942062Sxu xinWhen ``use_zero_pages`` is/was enabled, the sum of ``pages_sharing`` +
183e2942062Sxu xin``ksm_zero_pages`` represents the actual number of pages saved by KSM.
184e2942062Sxu xinif ``use_zero_pages`` has never been enabled, ``ksm_zero_pages`` is 0.
185c9161088SMike Rapoport
186c9161088SMike RapoportA high ratio of ``pages_sharing`` to ``pages_shared`` indicates good
187c9161088SMike Rapoportsharing, but a high ratio of ``pages_unshared`` to ``pages_sharing``
188c9161088SMike Rapoportindicates wasted effort.  ``pages_volatile`` embraces several
189c9161088SMike Rapoportdifferent kinds of activity, but a high proportion there would also
190c9161088SMike Rapoportindicate poor use of madvise MADV_MERGEABLE.
191c9161088SMike Rapoport
192c9161088SMike RapoportThe maximum possible ``pages_sharing/pages_shared`` ratio is limited by the
193c9161088SMike Rapoport``max_page_sharing`` tunable. To increase the ratio ``max_page_sharing`` must
194c9161088SMike Rapoportbe increased accordingly.
195c9161088SMike Rapoport
19621b7bdb5Sxu xinMonitoring KSM profit
19721b7bdb5Sxu xin=====================
19821b7bdb5Sxu xin
19921b7bdb5Sxu xinKSM can save memory by merging identical pages, but also can consume
20021b7bdb5Sxu xinadditional memory, because it needs to generate a number of rmap_items to
20121b7bdb5Sxu xinsave each scanned page's brief rmap information. Some of these pages may
20221b7bdb5Sxu xinbe merged, but some may not be abled to be merged after being checked
20321b7bdb5Sxu xinseveral times, which are unprofitable memory consumed.
20421b7bdb5Sxu xin
20521b7bdb5Sxu xin1) How to determine whether KSM save memory or consume memory in system-wide
20621b7bdb5Sxu xin   range? Here is a simple approximate calculation for reference::
20721b7bdb5Sxu xin
2081a8e8430Sxu xin	general_profit =~ ksm_saved_pages * sizeof(page) - (all_rmap_items) *
20921b7bdb5Sxu xin			  sizeof(rmap_item);
21021b7bdb5Sxu xin
2111a8e8430Sxu xin   where ksm_saved_pages equals to the sum of ``pages_sharing`` +
2121a8e8430Sxu xin   ``ksm_zero_pages`` of the system, and all_rmap_items can be easily
2131a8e8430Sxu xin   obtained by summing ``pages_sharing``, ``pages_shared``, ``pages_unshared``
2141a8e8430Sxu xin   and ``pages_volatile``.
21521b7bdb5Sxu xin
21621b7bdb5Sxu xin2) The KSM profit inner a single process can be similarly obtained by the
21721b7bdb5Sxu xin   following approximate calculation::
21821b7bdb5Sxu xin
2191a8e8430Sxu xin	process_profit =~ ksm_saved_pages * sizeof(page) -
22021b7bdb5Sxu xin			  ksm_rmap_items * sizeof(rmap_item).
22121b7bdb5Sxu xin
2221a8e8430Sxu xin   where ksm_saved_pages equals to the sum of ``ksm_merging_pages`` and
2231a8e8430Sxu xin   ``ksm_zero_pages``, both of which are shown under the directory
2241a8e8430Sxu xin   ``/proc/<pid>/ksm_stat``, and ksm_rmap_items is also shown in
2251a8e8430Sxu xin   ``/proc/<pid>/ksm_stat``. The process profit is also shown in
2261a8e8430Sxu xin   ``/proc/<pid>/ksm_stat`` as ksm_process_profit.
22721b7bdb5Sxu xin
22821b7bdb5Sxu xinFrom the perspective of application, a high ratio of ``ksm_rmap_items`` to
22921b7bdb5Sxu xin``ksm_merging_pages`` means a bad madvise-applied policy, so developers or
23021b7bdb5Sxu xinadministrators have to rethink how to change madvise policy. Giving an example
23121b7bdb5Sxu xinfor reference, a page's size is usually 4K, and the rmap_item's size is
23221b7bdb5Sxu xinseparately 32B on 32-bit CPU architecture and 64B on 64-bit CPU architecture.
23321b7bdb5Sxu xinso if the ``ksm_rmap_items/ksm_merging_pages`` ratio exceeds 64 on 64-bit CPU
23421b7bdb5Sxu xinor exceeds 128 on 32-bit CPU, then the app's madvise policy should be dropped,
23521b7bdb5Sxu xinbecause the ksm profit is approximately zero or negative.
23621b7bdb5Sxu xin
23794bfe85bSYang YangMonitoring KSM events
23894bfe85bSYang Yang=====================
23994bfe85bSYang Yang
24094bfe85bSYang YangThere are some counters in /proc/vmstat that may be used to monitor KSM events.
24194bfe85bSYang YangKSM might help save memory, it's a tradeoff by may suffering delay on KSM COW
24294bfe85bSYang Yangor on swapping in copy. Those events could help users evaluate whether or how
24394bfe85bSYang Yangto use KSM. For example, if cow_ksm increases too fast, user may decrease the
24494bfe85bSYang Yangrange of madvise(, , MADV_MERGEABLE).
24594bfe85bSYang Yang
24694bfe85bSYang Yangcow_ksm
24794bfe85bSYang Yang	is incremented every time a KSM page triggers copy on write (COW)
24894bfe85bSYang Yang	when users try to write to a KSM page, we have to make a copy.
24994bfe85bSYang Yang
25094bfe85bSYang Yangksm_swpin_copy
25194bfe85bSYang Yang	is incremented every time a KSM page is copied when swapping in
25294bfe85bSYang Yang	note that KSM page might be copied when swapping in because do_swap_page()
25394bfe85bSYang Yang	cannot do all the locking needed to reconstitute a cross-anon_vma KSM page.
25494bfe85bSYang Yang
255c9161088SMike Rapoport--
256c9161088SMike RapoportIzik Eidus,
257c9161088SMike RapoportHugh Dickins, 17 Nov 2009
258