* [PATCH] mm/vmstat: reject invalid stat_interval values
@ 2026-04-10 11:25 Cao Ruichuang
2026-04-10 12:32 ` Michal Hocko
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Cao Ruichuang @ 2026-04-10 11:25 UTC (permalink / raw)
To: Andrew Morton, linux-mm
Cc: David Hildenbrand, Lorenzo Stoakes, Liam R . Howlett,
Vlastimil Babka, Mike Rapoport, Suren Baghdasaryan, Michal Hocko,
linux-kernel, Cao Ruichuang
vm.stat_interval is exposed in seconds, but proc_dointvec_jiffies()
currently accepts zero and negative values. In the current tree,
writing 0 succeeds and leaves /proc/sys/vm/stat_interval at 0.
vmstat_update() uses the stored jiffy interval directly when it
requeues its delayed work, so a zero interval can drive excessive
kworker CPU usage. Negative values are not meaningful either.
Switch vm.stat_interval to a small custom sysctl handler that keeps
the existing seconds-based userspace ABI while rejecting values below
1 and capping the upper bound at INT_MAX / HZ.
Link: https://bugzilla.kernel.org/show_bug.cgi?id=220226
Signed-off-by: Cao Ruichuang <create0818@163.com>
---
mm/vmstat.c | 23 ++++++++++++++++++++++-
1 file changed, 22 insertions(+), 1 deletion(-)
diff --git a/mm/vmstat.c b/mm/vmstat.c
index 86b14b0f77b..f48d3bdad64 100644
--- a/mm/vmstat.c
+++ b/mm/vmstat.c
@@ -1964,6 +1964,7 @@ static const struct seq_operations vmstat_op = {
#ifdef CONFIG_SMP
static DEFINE_PER_CPU(struct delayed_work, vmstat_work);
static int sysctl_stat_interval __read_mostly = HZ;
+static const int sysctl_stat_interval_max = INT_MAX / HZ;
static int vmstat_late_init_done;
#ifdef CONFIG_PROC_FS
@@ -1972,6 +1973,26 @@ static void refresh_vm_stats(struct work_struct *work)
refresh_cpu_vm_stats(true);
}
+static int vmstat_stat_interval_handler(const struct ctl_table *table, int write,
+ void *buffer, size_t *lenp, loff_t *ppos)
+{
+ int interval = sysctl_stat_interval / HZ;
+ const struct ctl_table tmp = {
+ .data = &interval,
+ .maxlen = sizeof(interval),
+ .mode = table->mode,
+ .extra1 = SYSCTL_ONE,
+ .extra2 = (void *)&sysctl_stat_interval_max,
+ };
+ int ret;
+
+ ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
+ if (!ret && write)
+ sysctl_stat_interval = interval * HZ;
+
+ return ret;
+}
+
static int vmstat_refresh(const struct ctl_table *table, int write,
void *buffer, size_t *lenp, loff_t *ppos)
{
@@ -2236,7 +2257,7 @@ static const struct ctl_table vmstat_table[] = {
.data = &sysctl_stat_interval,
.maxlen = sizeof(sysctl_stat_interval),
.mode = 0644,
- .proc_handler = proc_dointvec_jiffies,
+ .proc_handler = vmstat_stat_interval_handler,
},
{
.procname = "stat_refresh",
--
2.39.5 (Apple Git-154)
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] mm/vmstat: reject invalid stat_interval values
2026-04-10 11:25 [PATCH] mm/vmstat: reject invalid stat_interval values Cao Ruichuang
@ 2026-04-10 12:32 ` Michal Hocko
2026-04-11 10:34 ` kernel test robot
2026-04-11 11:07 ` kernel test robot
2 siblings, 0 replies; 4+ messages in thread
From: Michal Hocko @ 2026-04-10 12:32 UTC (permalink / raw)
To: Cao Ruichuang
Cc: Andrew Morton, linux-mm, David Hildenbrand, Lorenzo Stoakes,
Liam R . Howlett, Vlastimil Babka, Mike Rapoport,
Suren Baghdasaryan, linux-kernel
On Fri 10-04-26 19:25:54, Cao Ruichuang wrote:
> vm.stat_interval is exposed in seconds, but proc_dointvec_jiffies()
> currently accepts zero and negative values. In the current tree,
> writing 0 succeeds and leaves /proc/sys/vm/stat_interval at 0.
>
> vmstat_update() uses the stored jiffy interval directly when it
> requeues its delayed work, so a zero interval can drive excessive
> kworker CPU usage. Negative values are not meaningful either.
>
> Switch vm.stat_interval to a small custom sysctl handler that keeps
> the existing seconds-based userspace ABI while rejecting values below
> 1 and capping the upper bound at INT_MAX / HZ.
This is admin only interface and we usually trust them to know what they
are doing. The same applies here. There are many interface behaving this
way. Is there any specific reason why we should treat this one
differently or are you suggesting to change this approach and sanitize
all of them? Changing just this one in isolation is IMHO not worth it.
>
> Link: https://bugzilla.kernel.org/show_bug.cgi?id=220226
> Signed-off-by: Cao Ruichuang <create0818@163.com>
> ---
> mm/vmstat.c | 23 ++++++++++++++++++++++-
> 1 file changed, 22 insertions(+), 1 deletion(-)
>
> diff --git a/mm/vmstat.c b/mm/vmstat.c
> index 86b14b0f77b..f48d3bdad64 100644
> --- a/mm/vmstat.c
> +++ b/mm/vmstat.c
> @@ -1964,6 +1964,7 @@ static const struct seq_operations vmstat_op = {
> #ifdef CONFIG_SMP
> static DEFINE_PER_CPU(struct delayed_work, vmstat_work);
> static int sysctl_stat_interval __read_mostly = HZ;
> +static const int sysctl_stat_interval_max = INT_MAX / HZ;
> static int vmstat_late_init_done;
>
> #ifdef CONFIG_PROC_FS
> @@ -1972,6 +1973,26 @@ static void refresh_vm_stats(struct work_struct *work)
> refresh_cpu_vm_stats(true);
> }
>
> +static int vmstat_stat_interval_handler(const struct ctl_table *table, int write,
> + void *buffer, size_t *lenp, loff_t *ppos)
> +{
> + int interval = sysctl_stat_interval / HZ;
> + const struct ctl_table tmp = {
> + .data = &interval,
> + .maxlen = sizeof(interval),
> + .mode = table->mode,
> + .extra1 = SYSCTL_ONE,
> + .extra2 = (void *)&sysctl_stat_interval_max,
> + };
> + int ret;
> +
> + ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
> + if (!ret && write)
> + sysctl_stat_interval = interval * HZ;
> +
> + return ret;
> +}
> +
> static int vmstat_refresh(const struct ctl_table *table, int write,
> void *buffer, size_t *lenp, loff_t *ppos)
> {
> @@ -2236,7 +2257,7 @@ static const struct ctl_table vmstat_table[] = {
> .data = &sysctl_stat_interval,
> .maxlen = sizeof(sysctl_stat_interval),
> .mode = 0644,
> - .proc_handler = proc_dointvec_jiffies,
> + .proc_handler = vmstat_stat_interval_handler,
> },
> {
> .procname = "stat_refresh",
> --
> 2.39.5 (Apple Git-154)
--
Michal Hocko
SUSE Labs
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] mm/vmstat: reject invalid stat_interval values
2026-04-10 11:25 [PATCH] mm/vmstat: reject invalid stat_interval values Cao Ruichuang
2026-04-10 12:32 ` Michal Hocko
@ 2026-04-11 10:34 ` kernel test robot
2026-04-11 11:07 ` kernel test robot
2 siblings, 0 replies; 4+ messages in thread
From: kernel test robot @ 2026-04-11 10:34 UTC (permalink / raw)
To: Cao Ruichuang, Andrew Morton
Cc: oe-kbuild-all, Linux Memory Management List, David Hildenbrand,
Lorenzo Stoakes, Liam R . Howlett, Vlastimil Babka,
Mike Rapoport, Suren Baghdasaryan, Michal Hocko, linux-kernel,
Cao Ruichuang
Hi Cao,
kernel test robot noticed the following build warnings:
[auto build test WARNING on akpm-mm/mm-everything]
url: https://github.com/intel-lab-lkp/linux/commits/Cao-Ruichuang/mm-vmstat-reject-invalid-stat_interval-values/20260411-085547
base: https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link: https://lore.kernel.org/r/20260410112554.23165-1-create0818%40163.com
patch subject: [PATCH] mm/vmstat: reject invalid stat_interval values
config: s390-randconfig-001-20260411 (https://download.01.org/0day-ci/archive/20260411/202604111811.THSgZ1ry-lkp@intel.com/config)
compiler: s390-linux-gcc (GCC) 8.5.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260411/202604111811.THSgZ1ry-lkp@intel.com/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202604111811.THSgZ1ry-lkp@intel.com/
All warnings (new ones prefixed by >>):
>> mm/vmstat.c:1968:18: warning: 'sysctl_stat_interval_max' defined but not used [-Wunused-const-variable=]
static const int sysctl_stat_interval_max = INT_MAX / HZ;
^~~~~~~~~~~~~~~~~~~~~~~~
Kconfig warnings: (for reference only)
WARNING: unmet direct dependencies detected for OF_GPIO
Depends on [n]: GPIOLIB [=y] && OF [=y] && HAS_IOMEM [=n]
Selected by [y]:
- REGULATOR_RT5133 [=y] && REGULATOR [=y] && I2C [=y] && GPIOLIB [=y] && OF [=y]
vim +/sysctl_stat_interval_max +1968 mm/vmstat.c
1964
1965 #ifdef CONFIG_SMP
1966 static DEFINE_PER_CPU(struct delayed_work, vmstat_work);
1967 static int sysctl_stat_interval __read_mostly = HZ;
> 1968 static const int sysctl_stat_interval_max = INT_MAX / HZ;
1969 static int vmstat_late_init_done;
1970
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] mm/vmstat: reject invalid stat_interval values
2026-04-10 11:25 [PATCH] mm/vmstat: reject invalid stat_interval values Cao Ruichuang
2026-04-10 12:32 ` Michal Hocko
2026-04-11 10:34 ` kernel test robot
@ 2026-04-11 11:07 ` kernel test robot
2 siblings, 0 replies; 4+ messages in thread
From: kernel test robot @ 2026-04-11 11:07 UTC (permalink / raw)
To: Cao Ruichuang, Andrew Morton
Cc: llvm, oe-kbuild-all, Linux Memory Management List,
David Hildenbrand, Lorenzo Stoakes, Liam R . Howlett,
Vlastimil Babka, Mike Rapoport, Suren Baghdasaryan, Michal Hocko,
linux-kernel, Cao Ruichuang
Hi Cao,
kernel test robot noticed the following build warnings:
[auto build test WARNING on akpm-mm/mm-everything]
url: https://github.com/intel-lab-lkp/linux/commits/Cao-Ruichuang/mm-vmstat-reject-invalid-stat_interval-values/20260411-085547
base: https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link: https://lore.kernel.org/r/20260410112554.23165-1-create0818%40163.com
patch subject: [PATCH] mm/vmstat: reject invalid stat_interval values
config: arm-randconfig-004-20260411 (https://download.01.org/0day-ci/archive/20260411/202604111816.CsqTatIh-lkp@intel.com/config)
compiler: clang version 23.0.0git (https://github.com/llvm/llvm-project ae825cb8cea7f3ac8e5e4096f22713845cf5e501)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260411/202604111816.CsqTatIh-lkp@intel.com/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202604111816.CsqTatIh-lkp@intel.com/
All warnings (new ones prefixed by >>):
>> mm/vmstat.c:1968:18: warning: unused variable 'sysctl_stat_interval_max' [-Wunused-const-variable]
1968 | static const int sysctl_stat_interval_max = INT_MAX / HZ;
| ^~~~~~~~~~~~~~~~~~~~~~~~
1 warning generated.
vim +/sysctl_stat_interval_max +1968 mm/vmstat.c
1964
1965 #ifdef CONFIG_SMP
1966 static DEFINE_PER_CPU(struct delayed_work, vmstat_work);
1967 static int sysctl_stat_interval __read_mostly = HZ;
> 1968 static const int sysctl_stat_interval_max = INT_MAX / HZ;
1969 static int vmstat_late_init_done;
1970
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-04-11 11:08 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-04-10 11:25 [PATCH] mm/vmstat: reject invalid stat_interval values Cao Ruichuang
2026-04-10 12:32 ` Michal Hocko
2026-04-11 10:34 ` kernel test robot
2026-04-11 11:07 ` kernel test robot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox