blob: 6b0ca7feb13573b7a40a9f3c7c145304cf158979 [file] [log] [blame]
Hugh Dickins7701c9c2009-09-21 17:02:24 -07001How to use the Kernel Samepage Merging feature
2----------------------------------------------
3
4KSM is a memory-saving de-duplication feature, enabled by CONFIG_KSM=y,
5added to the Linux kernel in 2.6.32. See mm/ksm.c for its implementation,
6and http://lwn.net/Articles/306704/ and http://lwn.net/Articles/330589/
7
8The KSM daemon ksmd periodically scans those areas of user memory which
9have been registered with it, looking for pages of identical content which
10can be replaced by a single write-protected page (which is automatically
11copied if a process later wants to update its content).
12
13KSM was originally developed for use with KVM (where it was known as
14Kernel Shared Memory), to fit more virtual machines into physical memory,
15by sharing the data common between them. But it can be useful to any
16application which generates many instances of the same data.
17
18KSM only merges anonymous (private) pages, never pagecache (file) pages.
Hugh Dickinsd0f209f2009-12-14 17:59:34 -080019KSM's merged pages were originally locked into kernel memory, but can now
20be swapped out just like other user pages (but sharing is broken when they
21are swapped back in: ksmd must rediscover their identity and merge again).
Hugh Dickins7701c9c2009-09-21 17:02:24 -070022
23KSM only operates on those areas of address space which an application
24has advised to be likely candidates for merging, by using the madvise(2)
25system call: int madvise(addr, length, MADV_MERGEABLE).
26
27The app may call int madvise(addr, length, MADV_UNMERGEABLE) to cancel
28that advice and restore unshared pages: whereupon KSM unmerges whatever
29it merged in that range. Note: this unmerging call may suddenly require
30more memory than is available - possibly failing with EAGAIN, but more
31probably arousing the Out-Of-Memory killer.
32
33If KSM is not configured into the running kernel, madvise MADV_MERGEABLE
34and MADV_UNMERGEABLE simply fail with EINVAL. If the running kernel was
35built with CONFIG_KSM=y, those calls will normally succeed: even if the
36the KSM daemon is not currently running, MADV_MERGEABLE still registers
37the range for whenever the KSM daemon is started; even if the range
38cannot contain any pages which KSM could actually merge; even if
39MADV_UNMERGEABLE is applied to a range which was never MADV_MERGEABLE.
40
David Rientjesdef5efe2017-02-24 14:58:47 -080041If a region of memory must be split into at least one new MADV_MERGEABLE
42or MADV_UNMERGEABLE region, the madvise may return ENOMEM if the process
43will exceed vm.max_map_count (see Documentation/sysctl/vm.txt).
44
Hugh Dickins7701c9c2009-09-21 17:02:24 -070045Like other madvise calls, they are intended for use on mapped areas of
46the user address space: they will report ENOMEM if the specified range
47includes unmapped gaps (though working on the intervening mapped areas),
48and might fail with EAGAIN if not enough memory for internal structures.
49
50Applications should be considerate in their use of MADV_MERGEABLE,
Hugh Dickinsd0f209f2009-12-14 17:59:34 -080051restricting its use to areas likely to benefit. KSM's scans may use a lot
52of processing power: some installations will disable KSM for that reason.
Hugh Dickins7701c9c2009-09-21 17:02:24 -070053
54The KSM daemon is controlled by sysfs files in /sys/kernel/mm/ksm/,
55readable by all but writable only by root:
56
Hugh Dickins7701c9c2009-09-21 17:02:24 -070057pages_to_scan - how many present pages to scan before ksmd goes to sleep
Hugh Dickinsc73602a2009-10-07 16:32:22 -070058 e.g. "echo 100 > /sys/kernel/mm/ksm/pages_to_scan"
59 Default: 100 (chosen for demonstration purposes)
Hugh Dickins7701c9c2009-09-21 17:02:24 -070060
61sleep_millisecs - how many milliseconds ksmd should sleep before next scan
62 e.g. "echo 20 > /sys/kernel/mm/ksm/sleep_millisecs"
63 Default: 20 (chosen for demonstration purposes)
64
Petr Holasek90bd6fd2013-02-22 16:35:00 -080065merge_across_nodes - specifies if pages from different numa nodes can be merged.
66 When set to 0, ksm merges only pages which physically
Hugh Dickins8fdb3db2013-02-22 16:36:03 -080067 reside in the memory area of same NUMA node. That brings
68 lower latency to access of shared pages. Systems with more
69 nodes, at significant NUMA distances, are likely to benefit
70 from the lower latency of setting 0. Smaller systems, which
71 need to minimize memory usage, are likely to benefit from
72 the greater sharing of setting 1 (default). You may wish to
73 compare how your system performs under each setting, before
74 deciding on which to use. merge_across_nodes setting can be
75 changed only when there are no ksm shared pages in system:
76 set run 2 to unmerge pages first, then to 1 after changing
77 merge_across_nodes, to remerge according to the new setting.
78 Default: 1 (merging across nodes as in earlier releases)
Petr Holasek90bd6fd2013-02-22 16:35:00 -080079
Hugh Dickins7701c9c2009-09-21 17:02:24 -070080run - set 0 to stop ksmd from running but keep merged pages,
81 set 1 to run ksmd e.g. "echo 1 > /sys/kernel/mm/ksm/run",
82 set 2 to stop ksmd and unmerge all pages currently merged,
83 but leave mergeable areas registered for next run
Hugh Dickinsc73602a2009-10-07 16:32:22 -070084 Default: 0 (must be changed to 1 to activate KSM,
85 except if CONFIG_SYSFS is disabled)
Hugh Dickins7701c9c2009-09-21 17:02:24 -070086
Claudio Imbrendae86c59b2017-02-24 14:55:39 -080087use_zero_pages - specifies whether empty pages (i.e. allocated pages
88 that only contain zeroes) should be treated specially.
89 When set to 1, empty pages are merged with the kernel
90 zero page(s) instead of with each other as it would
91 happen normally. This can improve the performance on
92 architectures with coloured zero pages, depending on
93 the workload. Care should be taken when enabling this
94 setting, as it can potentially degrade the performance
95 of KSM for some workloads, for example if the checksums
96 of pages candidate for merging match the checksum of
97 an empty page. This setting can be changed at any time,
98 it is only effective for pages merged after the change.
99 Default: 0 (normal KSM behaviour as in earlier releases)
100
Hugh Dickins7701c9c2009-09-21 17:02:24 -0700101The effectiveness of KSM and MADV_MERGEABLE is shown in /sys/kernel/mm/ksm/:
102
Hugh Dickinsd0f209f2009-12-14 17:59:34 -0800103pages_shared - how many shared pages are being used
Hugh Dickins7701c9c2009-09-21 17:02:24 -0700104pages_sharing - how many more sites are sharing them i.e. how much saved
105pages_unshared - how many pages unique but repeatedly checked for merging
106pages_volatile - how many pages changing too fast to be placed in a tree
107full_scans - how many times all mergeable areas have been scanned
108
109A high ratio of pages_sharing to pages_shared indicates good sharing, but
110a high ratio of pages_unshared to pages_sharing indicates wasted effort.
111pages_volatile embraces several different kinds of activity, but a high
112proportion there would also indicate poor use of madvise MADV_MERGEABLE.
113
114Izik Eidus,
Hugh Dickinsd0f209f2009-12-14 17:59:34 -0800115Hugh Dickins, 17 Nov 2009