1c9161088SMike Rapoport.. _admin_guide_ksm: 2c9161088SMike Rapoport 3c9161088SMike Rapoport======================= 4c9161088SMike RapoportKernel Samepage Merging 5c9161088SMike Rapoport======================= 6c9161088SMike Rapoport 7c9161088SMike RapoportOverview 8c9161088SMike Rapoport======== 9c9161088SMike Rapoport 10c9161088SMike RapoportKSM is a memory-saving de-duplication feature, enabled by CONFIG_KSM=y, 11c9161088SMike Rapoportadded to the Linux kernel in 2.6.32. See ``mm/ksm.c`` for its implementation, 12*6b2484e1SAlexander A. Klimovand http://lwn.net/Articles/306704/ and https://lwn.net/Articles/330589/ 13c9161088SMike Rapoport 14c9161088SMike RapoportKSM was originally developed for use with KVM (where it was known as 15c9161088SMike RapoportKernel Shared Memory), to fit more virtual machines into physical memory, 16c9161088SMike Rapoportby sharing the data common between them. But it can be useful to any 17c9161088SMike Rapoportapplication which generates many instances of the same data. 18c9161088SMike Rapoport 19c9161088SMike RapoportThe KSM daemon ksmd periodically scans those areas of user memory 20c9161088SMike Rapoportwhich have been registered with it, looking for pages of identical 21c9161088SMike Rapoportcontent which can be replaced by a single write-protected page (which 22c9161088SMike Rapoportis automatically copied if a process later wants to update its 23c9161088SMike Rapoportcontent). The amount of pages that KSM daemon scans in a single pass 24c9161088SMike Rapoportand the time between the passes are configured using :ref:`sysfs 25c9161088SMike Rapoportintraface <ksm_sysfs>` 26c9161088SMike Rapoport 27c9161088SMike RapoportKSM only merges anonymous (private) pages, never pagecache (file) pages. 28c9161088SMike RapoportKSM's merged pages were originally locked into kernel memory, but can now 29c9161088SMike Rapoportbe swapped out just like other user pages (but sharing is broken when they 30c9161088SMike Rapoportare swapped back in: ksmd must rediscover their identity and merge again). 31c9161088SMike Rapoport 32c9161088SMike RapoportControlling KSM with madvise 33c9161088SMike Rapoport============================ 34c9161088SMike Rapoport 35c9161088SMike RapoportKSM only operates on those areas of address space which an application 36c9161088SMike Rapoporthas advised to be likely candidates for merging, by using the madvise(2) 37c9161088SMike Rapoportsystem call:: 38c9161088SMike Rapoport 39c9161088SMike Rapoport int madvise(addr, length, MADV_MERGEABLE) 40c9161088SMike Rapoport 41c9161088SMike RapoportThe app may call 42c9161088SMike Rapoport 43c9161088SMike Rapoport:: 44c9161088SMike Rapoport 45c9161088SMike Rapoport int madvise(addr, length, MADV_UNMERGEABLE) 46c9161088SMike Rapoport 47c9161088SMike Rapoportto cancel that advice and restore unshared pages: whereupon KSM 48c9161088SMike Rapoportunmerges whatever it merged in that range. Note: this unmerging call 49c9161088SMike Rapoportmay suddenly require more memory than is available - possibly failing 50c9161088SMike Rapoportwith EAGAIN, but more probably arousing the Out-Of-Memory killer. 51c9161088SMike Rapoport 52c9161088SMike RapoportIf KSM is not configured into the running kernel, madvise MADV_MERGEABLE 53c9161088SMike Rapoportand MADV_UNMERGEABLE simply fail with EINVAL. If the running kernel was 54c9161088SMike Rapoportbuilt with CONFIG_KSM=y, those calls will normally succeed: even if the 55b32dae55SRandy DunlapKSM daemon is not currently running, MADV_MERGEABLE still registers 56c9161088SMike Rapoportthe range for whenever the KSM daemon is started; even if the range 57c9161088SMike Rapoportcannot contain any pages which KSM could actually merge; even if 58c9161088SMike RapoportMADV_UNMERGEABLE is applied to a range which was never MADV_MERGEABLE. 59c9161088SMike Rapoport 60c9161088SMike RapoportIf a region of memory must be split into at least one new MADV_MERGEABLE 61c9161088SMike Rapoportor MADV_UNMERGEABLE region, the madvise may return ENOMEM if the process 6257043247SMauro Carvalho Chehabwill exceed ``vm.max_map_count`` (see Documentation/admin-guide/sysctl/vm.rst). 63c9161088SMike Rapoport 64c9161088SMike RapoportLike other madvise calls, they are intended for use on mapped areas of 65c9161088SMike Rapoportthe user address space: they will report ENOMEM if the specified range 66c9161088SMike Rapoportincludes unmapped gaps (though working on the intervening mapped areas), 67c9161088SMike Rapoportand might fail with EAGAIN if not enough memory for internal structures. 68c9161088SMike Rapoport 69c9161088SMike RapoportApplications should be considerate in their use of MADV_MERGEABLE, 70c9161088SMike Rapoportrestricting its use to areas likely to benefit. KSM's scans may use a lot 71c9161088SMike Rapoportof processing power: some installations will disable KSM for that reason. 72c9161088SMike Rapoport 73c9161088SMike Rapoport.. _ksm_sysfs: 74c9161088SMike Rapoport 75c9161088SMike RapoportKSM daemon sysfs interface 76c9161088SMike Rapoport========================== 77c9161088SMike Rapoport 78c9161088SMike RapoportThe KSM daemon is controlled by sysfs files in ``/sys/kernel/mm/ksm/``, 79c9161088SMike Rapoportreadable by all but writable only by root: 80c9161088SMike Rapoport 81c9161088SMike Rapoportpages_to_scan 82c9161088SMike Rapoport how many pages to scan before ksmd goes to sleep 83c9161088SMike Rapoport e.g. ``echo 100 > /sys/kernel/mm/ksm/pages_to_scan``. 84c9161088SMike Rapoport 85c9161088SMike Rapoport Default: 100 (chosen for demonstration purposes) 86c9161088SMike Rapoport 87c9161088SMike Rapoportsleep_millisecs 88c9161088SMike Rapoport how many milliseconds ksmd should sleep before next scan 89c9161088SMike Rapoport e.g. ``echo 20 > /sys/kernel/mm/ksm/sleep_millisecs`` 90c9161088SMike Rapoport 91c9161088SMike Rapoport Default: 20 (chosen for demonstration purposes) 92c9161088SMike Rapoport 93c9161088SMike Rapoportmerge_across_nodes 94c9161088SMike Rapoport specifies if pages from different NUMA nodes can be merged. 95c9161088SMike Rapoport When set to 0, ksm merges only pages which physically reside 96c9161088SMike Rapoport in the memory area of same NUMA node. That brings lower 97c9161088SMike Rapoport latency to access of shared pages. Systems with more nodes, at 98c9161088SMike Rapoport significant NUMA distances, are likely to benefit from the 99c9161088SMike Rapoport lower latency of setting 0. Smaller systems, which need to 100c9161088SMike Rapoport minimize memory usage, are likely to benefit from the greater 101c9161088SMike Rapoport sharing of setting 1 (default). You may wish to compare how 102c9161088SMike Rapoport your system performs under each setting, before deciding on 103c9161088SMike Rapoport which to use. ``merge_across_nodes`` setting can be changed only 104c9161088SMike Rapoport when there are no ksm shared pages in the system: set run 2 to 105c9161088SMike Rapoport unmerge pages first, then to 1 after changing 106c9161088SMike Rapoport ``merge_across_nodes``, to remerge according to the new setting. 107c9161088SMike Rapoport 108c9161088SMike Rapoport Default: 1 (merging across nodes as in earlier releases) 109c9161088SMike Rapoport 110c9161088SMike Rapoportrun 111c9161088SMike Rapoport * set to 0 to stop ksmd from running but keep merged pages, 112c9161088SMike Rapoport * set to 1 to run ksmd e.g. ``echo 1 > /sys/kernel/mm/ksm/run``, 113c9161088SMike Rapoport * set to 2 to stop ksmd and unmerge all pages currently merged, but 114c9161088SMike Rapoport leave mergeable areas registered for next run. 115c9161088SMike Rapoport 116c9161088SMike Rapoport Default: 0 (must be changed to 1 to activate KSM, except if 117c9161088SMike Rapoport CONFIG_SYSFS is disabled) 118c9161088SMike Rapoport 119c9161088SMike Rapoportuse_zero_pages 120c9161088SMike Rapoport specifies whether empty pages (i.e. allocated pages that only 121c9161088SMike Rapoport contain zeroes) should be treated specially. When set to 1, 122c9161088SMike Rapoport empty pages are merged with the kernel zero page(s) instead of 123c9161088SMike Rapoport with each other as it would happen normally. This can improve 124c9161088SMike Rapoport the performance on architectures with coloured zero pages, 125c9161088SMike Rapoport depending on the workload. Care should be taken when enabling 126c9161088SMike Rapoport this setting, as it can potentially degrade the performance of 127c9161088SMike Rapoport KSM for some workloads, for example if the checksums of pages 128c9161088SMike Rapoport candidate for merging match the checksum of an empty 129c9161088SMike Rapoport page. This setting can be changed at any time, it is only 130c9161088SMike Rapoport effective for pages merged after the change. 131c9161088SMike Rapoport 132c9161088SMike Rapoport Default: 0 (normal KSM behaviour as in earlier releases) 133c9161088SMike Rapoport 134c9161088SMike Rapoportmax_page_sharing 135c9161088SMike Rapoport Maximum sharing allowed for each KSM page. This enforces a 136c9161088SMike Rapoport deduplication limit to avoid high latency for virtual memory 137c9161088SMike Rapoport operations that involve traversal of the virtual mappings that 138c9161088SMike Rapoport share the KSM page. The minimum value is 2 as a newly created 139c9161088SMike Rapoport KSM page will have at least two sharers. The higher this value 140c9161088SMike Rapoport the faster KSM will merge the memory and the higher the 141c9161088SMike Rapoport deduplication factor will be, but the slower the worst case 142c9161088SMike Rapoport virtual mappings traversal could be for any given KSM 143c9161088SMike Rapoport page. Slowing down this traversal means there will be higher 144c9161088SMike Rapoport latency for certain virtual memory operations happening during 145c9161088SMike Rapoport swapping, compaction, NUMA balancing and page migration, in 146c9161088SMike Rapoport turn decreasing responsiveness for the caller of those virtual 147c9161088SMike Rapoport memory operations. The scheduler latency of other tasks not 148c9161088SMike Rapoport involved with the VM operations doing the virtual mappings 149c9161088SMike Rapoport traversal is not affected by this parameter as these 150c9161088SMike Rapoport traversals are always schedule friendly themselves. 151c9161088SMike Rapoport 152c9161088SMike Rapoportstable_node_chains_prune_millisecs 153c9161088SMike Rapoport specifies how frequently KSM checks the metadata of the pages 154c9161088SMike Rapoport that hit the deduplication limit for stale information. 155c9161088SMike Rapoport Smaller milllisecs values will free up the KSM metadata with 156c9161088SMike Rapoport lower latency, but they will make ksmd use more CPU during the 157c9161088SMike Rapoport scan. It's a noop if not a single KSM page hit the 158c9161088SMike Rapoport ``max_page_sharing`` yet. 159c9161088SMike Rapoport 160c9161088SMike RapoportThe effectiveness of KSM and MADV_MERGEABLE is shown in ``/sys/kernel/mm/ksm/``: 161c9161088SMike Rapoport 162c9161088SMike Rapoportpages_shared 163c9161088SMike Rapoport how many shared pages are being used 164c9161088SMike Rapoportpages_sharing 165c9161088SMike Rapoport how many more sites are sharing them i.e. how much saved 166c9161088SMike Rapoportpages_unshared 167c9161088SMike Rapoport how many pages unique but repeatedly checked for merging 168c9161088SMike Rapoportpages_volatile 169c9161088SMike Rapoport how many pages changing too fast to be placed in a tree 170c9161088SMike Rapoportfull_scans 171c9161088SMike Rapoport how many times all mergeable areas have been scanned 172c9161088SMike Rapoportstable_node_chains 173c9161088SMike Rapoport the number of KSM pages that hit the ``max_page_sharing`` limit 174c9161088SMike Rapoportstable_node_dups 175c9161088SMike Rapoport number of duplicated KSM pages 176c9161088SMike Rapoport 177c9161088SMike RapoportA high ratio of ``pages_sharing`` to ``pages_shared`` indicates good 178c9161088SMike Rapoportsharing, but a high ratio of ``pages_unshared`` to ``pages_sharing`` 179c9161088SMike Rapoportindicates wasted effort. ``pages_volatile`` embraces several 180c9161088SMike Rapoportdifferent kinds of activity, but a high proportion there would also 181c9161088SMike Rapoportindicate poor use of madvise MADV_MERGEABLE. 182c9161088SMike Rapoport 183c9161088SMike RapoportThe maximum possible ``pages_sharing/pages_shared`` ratio is limited by the 184c9161088SMike Rapoport``max_page_sharing`` tunable. To increase the ratio ``max_page_sharing`` must 185c9161088SMike Rapoportbe increased accordingly. 186c9161088SMike Rapoport 187c9161088SMike Rapoport-- 188c9161088SMike RapoportIzik Eidus, 189c9161088SMike RapoportHugh Dickins, 17 Nov 2009 190