From 23b8e18f61de3c1a37a1c2b9c2714821e7a7fc07 Mon Sep 17 00:00:00 2001 From: Liu Shixin Date: Mon, 27 May 2024 12:39:24 +0800 Subject: [PATCH 1/2] mm/vmscan: introduce vm_swap_extension sysctl hulk inclusion category: feature bugzilla: https://gitee.com/openeuler/kernel/issues/I9K8D1 CVE: NA -------------------------------- Introduce a sysctl named vm_swap_extension to add control of swap feature. In global reclaim, set swappiness to 0 doesn't means swapout disabled, which is often misused by user. Add a control to really disable swapout by setting vm_swap_extension = 1. Signed-off-by: Liu Shixin --- Documentation/admin-guide/sysctl/vm.rst | 11 +++++++++ include/linux/swap.h | 3 +++ kernel/sysctl.c | 11 +++++++++ mm/Kconfig | 6 +++++ mm/vmscan.c | 30 +++++++++++++++++++++++++ 5 files changed, 61 insertions(+) diff --git a/Documentation/admin-guide/sysctl/vm.rst b/Documentation/admin-guide/sysctl/vm.rst index b508acfdde2e..3e043b9aa4b4 100644 --- a/Documentation/admin-guide/sysctl/vm.rst +++ b/Documentation/admin-guide/sysctl/vm.rst @@ -70,6 +70,7 @@ Currently, these files are in /proc/sys/vm: - stat_refresh - numa_stat - swappiness +- swap_extension - unprivileged_userfaultfd - user_reserve_kbytes - vfs_cache_pressure @@ -928,6 +929,16 @@ At 0, the kernel will not initiate swap until the amount of free and file-backed pages is less than the high watermark in a zone. +swap_extension +============== + +This control is used for some swap extension feature. The default value is +0. Now support one extension which is to disable global swap which can't +acquired by swappiness. + +To disable global swap: + echo 1 > /proc/sys/vm/swap_extension + unprivileged_userfaultfd ======================== diff --git a/include/linux/swap.h b/include/linux/swap.h index 8e62c870517c..3241ce0a556a 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -390,6 +390,9 @@ extern unsigned long mem_cgroup_shrink_node(struct mem_cgroup *mem, unsigned long *nr_scanned); extern unsigned long shrink_all_memory(unsigned long nr_pages); extern int vm_swappiness; +#ifdef CONFIG_SWAP_EXTENSION +extern int vm_swap_extension; +#endif extern int remove_mapping(struct address_space *mapping, struct page *page); extern unsigned long reclaim_pages(struct list_head *page_list); diff --git a/kernel/sysctl.c b/kernel/sysctl.c index f3f43b2def7f..0eea66af812a 100644 --- a/kernel/sysctl.c +++ b/kernel/sysctl.c @@ -2958,6 +2958,17 @@ static struct ctl_table vm_table[] = { .extra1 = SYSCTL_ZERO, .extra2 = &two_hundred, }, +#ifdef CONFIG_SWAP_EXTENSION + { + .procname = "swap_extension", + .data = &vm_swap_extension, + .maxlen = sizeof(vm_swap_extension), + .mode = 0644, + .proc_handler = proc_dointvec_minmax, + .extra1 = SYSCTL_ZERO, + .extra2 = SYSCTL_ONE, + }, +#endif #ifdef CONFIG_NUMA { .procname = "numa_stat", diff --git a/mm/Kconfig b/mm/Kconfig index 70c85533aada..0f9209cd969b 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -568,6 +568,12 @@ config PAGE_CACHE_LIMIT if unsure, say N to disable the PAGE_CACHE_LIMIT. +config SWAP_EXTENSION + bool "Support some swap extension feature" + depends on MMU && SYSCTL + default n + help + Add a sysctl swap_extension to support some swap extension feature. config CMA bool "Contiguous Memory Allocator" depends on MMU diff --git a/mm/vmscan.c b/mm/vmscan.c index 044bf496885b..0979dad6180b 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -182,6 +182,27 @@ struct scan_control { */ int vm_swappiness = 60; +#ifdef CONFIG_SWAP_EXTENSION + +#define VM_GLOBAL_SWAP_DISABLE 1 + +int vm_swap_extension; + +/* + * In global reclaim, set swappiness to 0 doesn't means swapout + * disabled, which is often misused by user. + */ +static bool should_disable_global_swap(void) +{ + return (vm_swap_extension & VM_GLOBAL_SWAP_DISABLE); +} +#else +static bool should_disable_global_swap(void) +{ + return false; +} +#endif + static void set_task_reclaim_state(struct task_struct *task, struct reclaim_state *rs) { @@ -2490,6 +2511,15 @@ static void get_scan_count(struct lruvec *lruvec, struct scan_control *sc, goto out; } + /* + * In global reclaim, set swappiness to 0 doesn't means swapout + * disabled, which is often misused by user. + */ + if (!cgroup_reclaim(sc) && should_disable_global_swap()) { + scan_balance = SCAN_FILE; + goto out; + } + /* * Do not apply any pressure balancing cleverness when the * system is close to OOM, scan both anon and file equally -- Gitee From 0e155817e38c0ee2f89870ad0de2bdeecaa0c5e8 Mon Sep 17 00:00:00 2001 From: Liu Shixin Date: Mon, 27 May 2024 12:39:25 +0800 Subject: [PATCH 2/2] openeuler_defconfig: enable swap_extension for x86 and arm64 hulk inclusion category: feature bugzilla: https://gitee.com/openeuler/kernel/issues/I9K8D1 CVE: NA ---------------------------------------- Enable CONFIG_SWAP_EXTENSION to support swap_extension. Signed-off-by: Liu Shixin --- arch/arm64/configs/openeuler_defconfig | 1 + arch/x86/configs/openeuler_defconfig | 1 + 2 files changed, 2 insertions(+) diff --git a/arch/arm64/configs/openeuler_defconfig b/arch/arm64/configs/openeuler_defconfig index a8f83a4e2a4f..efab422d9bd3 100644 --- a/arch/arm64/configs/openeuler_defconfig +++ b/arch/arm64/configs/openeuler_defconfig @@ -1126,6 +1126,7 @@ CONFIG_ETMEM_SWAP=m CONFIG_ETMEM=y CONFIG_USERSWAP=y # CONFIG_PAGE_CACHE_LIMIT is not set +CONFIG_SWAP_EXTENSION=y CONFIG_CMA=y # CONFIG_CMA_DEBUG is not set # CONFIG_CMA_DEBUGFS is not set diff --git a/arch/x86/configs/openeuler_defconfig b/arch/x86/configs/openeuler_defconfig index 86a2e4ea72da..46c16c7bfc25 100644 --- a/arch/x86/configs/openeuler_defconfig +++ b/arch/x86/configs/openeuler_defconfig @@ -1059,6 +1059,7 @@ CONFIG_ETMEM_SWAP=m CONFIG_ETMEM=y CONFIG_USERSWAP=y # CONFIG_PAGE_CACHE_LIMIT is not set +CONFIG_SWAP_EXTENSION=y # CONFIG_CMA is not set CONFIG_MEM_SOFT_DIRTY=y CONFIG_ZSWAP=y -- Gitee