mm: Introduce uKSM for user-controlled KSM
Subsystems (6)
RFC 1/1 mm: Introduce uKSM for user-controlled KSM
uKSM (user space control Kernel SamePage Merging) is an extension to the existing Kernel SamePage Merging (KSM) facility. This enhancement addresses the flexibility limitations of the current KSM implementation, allowing for user-space control over page merging activities. With uKSM, user-space applications can now interact directly with KSM functionality via proc interfaces, providing them with the capability to inform kernel decisions with more context-specific information.
Current limitations of KSM include the kernel's sole responsibility for identifying and merging identical pages, which restricts adaptability to diverse user requirements. For instance, virtual machine clean file caches are prime candidates for KSM, but the user had no control over managing these pages until now. Additionally, KSM can cause the splitting of transparent huge pages (THPs), which may not always be desirable depending on the task. Users also need finer control over the prioritization and frequency of page scans, as well as grouping tasks for page merging within specific groups only.
To overcome these challenges, uKSM introduces several proc interfaces for
managing page merging:
- /proc/uksm/merge enables the merging of two pages given their process IDs and addresses.
- /proc/uksm/unmerge allows unmerging a previously merged KSM page.
- /proc/uksm/cmp provides a lightweight mechanism to check page content equivalence before invoking a merge operation.
- /proc/uksm/lru_add_drain_all facilitates efficient merging of newly added LRU pages without the overhead of calling lru_add_drain_all on every merge operation.
- /proc/task_id/uksm_pagemap delivers extended page information useful for identifying mergeable pages with additional uKSM-specific metadata.
This feature is governed by the CONFIG_UKSM compile-time option, which depends on CONFIG_KSM being enabled. The bulk of the functionality is implemented in mm/uksm.c.
A sample application demonstrating the use of uKSM interfaces for tracking and merging pages of specified tasks is available at:
https://github.com/teawater/uksmd
--- fs/proc/base.c | 6 + fs/proc/internal.h | 3 + fs/proc/task_mmu.c | 147 +++++++++- include/linux/errno.h | 2 + include/linux/ksm.h | 38 ++- mm/Kconfig | 12 + mm/Makefile | 1 + mm/ksm.c | 64 ++++- mm/swap.c | 5 + mm/uksm.c | 643 ++++++++++++++++++++++++++++++++++++++++++ mm/uksm.h | 76 +++++ 11 files changed, 974 insertions(+), 23 deletions(-) create mode 100644 mm/uksm.c create mode 100644 mm/uksm.h
fs/proc/base.c +6 -0
@@ -3359,6 +3359,9 @@ static const struct pid_entry tgid_base_stuff[] = { ONE("ksm_merging_pages", S_IRUSR, proc_pid_ksm_merging_pages), ONE("ksm_stat", S_IRUSR, proc_pid_ksm_stat),#endif +#ifdef CONFIG_UKSM + REG("uksm_pagemap", S_IRUSR, proc_uksm_pagemap_operations), +#endif };static int proc_tgid_base_readdir (struct file *file,struct dir_context *ctx)
@@ -3698,6 +3701,9 @@ static const struct pid_entry tid_base_stuff[] = { ONE("ksm_merging_pages", S_IRUSR, proc_pid_ksm_merging_pages), ONE("ksm_stat", S_IRUSR, proc_pid_ksm_stat),#endif +#ifdef CONFIG_UKSM + REG("uksm_pagemap", S_IRUSR, proc_uksm_pagemap_operations), +#endif };static int proc_tid_base_readdir (struct file *file,struct dir_context *ctx)
fs/proc/internal.h +3 -0
@@ -303,6 +303,9 @@ extern const struct file_operations proc_pid_smaps_operations;extern const struct file_operations proc_pid_smaps_rollup_operations;extern const struct file_operations proc_clear_refs_operations;extern const struct file_operations proc_pagemap_operations; +#ifdef CONFIG_UKSM +extern const struct file_operations proc_uksm_pagemap_operations; +#endif extern unsigned long task_vsize (struct mm_struct *); extern unsigned long task_statm(struct mm_struct *,
fs/proc/task_mmu.c +135 -12
271 lines not on this page · raw message
include/linux/errno.h +2 -0
@@ -33,4 +33,6 @@#define ERECALLCONFLICT 530/* conflict with recalled state */ #define ENOGRACE 531/* NFS file lock reclaim refused */ +#define EPAGESNOTSAME 541/* KSM two pages are not the same. */ +#endif
include/linux/ksm.h +36 -2
@@ -16,6 +16,8 @@#include <linux/sched/coredump.h> #ifdef CONFIG_KSM +#ifndef CONFIG_UKSM +int ksm_madvise (struct vm_area_struct *vma,unsigned long start,unsigned long end,int advice,unsigned long *vm_flags);
@@ -26,6 +28,14 @@ int ksm_disable(struct mm_struct *mm);int __ksm_enter (struct mm_struct *mm);void __ksm_exit (struct mm_struct *mm); + +#else /* CONFIG_UKSM */ + +void uksm_folio_clear (struct folio *folio); +u32 ksm_calc_checksum (struct page *page); + +#endif /* CONFIG_UKSM */ +/* * To identify zeropages that were mapped by KSM, we reuse the dirty bit * in the PTE. If the PTE is dirty, the zeropage was mapped by KSM when
@@ -38,11 +48,14 @@ extern unsigned long ksm_zero_pages;static inline void ksm_might_unmap_zero_page (struct mm_struct *mm,pte_t pte) {if (is_ksm_zero_pte (pte)) { +#ifndef CONFIG_UKSM ksm_zero_pages--; +#endif mm->ksm_zero_pages--; } } +#ifndef CONFIG_UKSM static inline int ksm_fork (struct mm_struct *mm,struct mm_struct *oldmm) {int ret;
@@ -65,6 +78,8 @@ static inline void ksm_exit(struct mm_struct *mm) __ksm_exit(mm); } +#endif /* !CONFIG_UKSM */ +/* * When do_swap_page() first faults in from swap what used to be a KSM page, * no problem, it will be assigned to this vma's anon_vma; but thereafter,
@@ -91,8 +106,9 @@ void collect_procs_ksm(struct page *page, struct list_head *to_kill,long ksm_process_profit (struct mm_struct *);#endif /* CONFIG_PROC_FS */ -#else /* !CONFIG_KSM */ +#endif /* CONFIG_KSM */ +#if !defined(CONFIG_KSM ) || defined(CONFIG_UKSM )static inline void ksm_add_vma (struct vm_area_struct *vma) { }
@@ -111,6 +127,7 @@ static inline void ksm_exit(struct mm_struct *mm) { } +#ifndef CONFIG_KSM static inline void ksm_might_unmap_zero_page (struct mm_struct *mm,pte_t pte) { }
@@ -121,6 +138,7 @@ static inline void collect_procs_ksm(struct page *page, { }#endif +#endif /* !CONFIG_KSM */ #ifdef CONFIG_MMU static inline int ksm_madvise(struct vm_area_struct *vma, unsigned long start,
@@ -129,6 +147,7 @@ static inline int ksm_madvise(struct vm_area_struct *vma, unsigned long start,return 0 ; } +#ifndef CONFIG_KSM static inline struct folio *ksm_might_need_to_copy (struct folio *folio,struct vm_area_struct *vma,unsigned long addr) {
@@ -143,7 +162,22 @@ static inline void rmap_walk_ksm(struct folio *folio,static inline void folio_migrate_ksm (struct folio *newfolio,struct folio *old) { } -#endif /* CONFIG_MMU */ #endif /* !CONFIG_KSM */ +#endif /* CONFIG_MMU */ +#endif /* !CONFIG_KSM || CONFIG_UKSM */ + +#ifdef CONFIG_UKSM + +static inline int ksm_enable_merge_any (struct mm_struct *mm) +{ +return 0 ; +} + +static inline int ksm_disable_merge_any (struct mm_struct *mm) +{ +return 0 ; +} + +#endif /* CONFIG_UKSM */ #endif /* __LINUX_KSM_H */
mm/Kconfig +12 -0
@@ -706,6 +706,18 @@ config KSM until a program has madvised that an area is MADV_MERGEABLE, and root has set /sys/kernel/mm/ksm/run to 1 (if CONFIG_SYSFS is set). +config UKSM + bool "Enable user space control KSM" + depends on KSM + help + uKSM (user space control Kernel SamePage Merging) is an extension to the + existing Kernel SamePage Merging (KSM) facility. This enhancement + addresses the flexibility limitations of the current KSM implementation, + allowing for user-space control over page merging activities. With uKSM, + user-space applications can now interact directly with KSM functionality + via proc interfaces, providing them with the capability to inform kernel + decisions with more context-specific information. + config DEFAULT_MMAP_MIN_ADDR int "Low address space to protect from user allocation" depends on MMU
mm/Makefile +1 -0
@@ -79,6 +79,7 @@ obj-$(CONFIG_SPARSEMEM) += sparse.o obj-$(CONFIG_SPARSEMEM_VMEMMAP) += sparse-vmemmap.o obj-$(CONFIG_MMU_NOTIFIER) += mmu_notifier.o obj-$(CONFIG_KSM) += ksm.o +obj-$(CONFIG_UKSM) += uksm.o obj-$(CONFIG_PAGE_POISONING) += page_poison.o obj-$(CONFIG_KASAN) += kasan/ obj-$(CONFIG_KFENCE) += kfence/
mm/ksm.c +55 -9
215 lines not on this page · raw message
mm/swap.c +5 -0
@@ -37,6 +37,7 @@#include <linux/page_idle.h> #include <linux/local_lock.h> #include <linux/buffer_head.h> +#include <linux/ksm.h> #include "internal.h"
@@ -96,6 +97,10 @@ static void __page_cache_release(struct folio *folio, struct lruvec **lruvecp,zone_stat_mod_folio (folio,NR_MLOCK , -nr_pages);count_vm_events (UNEVICTABLE_PGCLEARED , nr_pages); } +#ifdef CONFIG_UKSM +if (unlikely (folio_test_ksm (folio))) +uksm_folio_clear (folio); +#endif }/*
mm/uksm.c new file +643 -0
643 lines not on this page · raw message
mm/uksm.h new file +76 -0
76 lines not on this page · raw message
Signature · 1 line
2.19.1.6.gb485710b