diff mbox series

[4/6] sysctl: ensure files_stat.max_files is not less than sysctl_nr_open

Message ID 20241123181256.184004-1-alexjlzheng@tencent.com
State New
Headers show
Series Maintain the relative size of fs.file-max and fs.nr_open | expand

Commit Message

Jinliang Zheng Nov. 23, 2024, 6:12 p.m. UTC
Introduce proc_doulongvec_maxfiles_minmax(), ensure the value of
files_stat.max_files is not less than sysctl_nr_open.

Signed-off-by: Jinliang Zheng <alexjlzheng@tencent.com>
---
 fs/file_table.c        |  2 +-
 include/linux/sysctl.h |  2 ++
 kernel/sysctl.c        | 17 +++++++++++++++++
 3 files changed, 20 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/fs/file_table.c b/fs/file_table.c
index db3d3a9cb421..01faa9c2869e 100644
--- a/fs/file_table.c
+++ b/fs/file_table.c
@@ -119,7 +119,7 @@  static struct ctl_table fs_stat_sysctls[] = {
 		.data		= &files_stat.max_files,
 		.maxlen		= sizeof(files_stat.max_files),
 		.mode		= 0644,
-		.proc_handler	= proc_doulongvec_minmax,
+		.proc_handler	= proc_doulongvec_maxfiles_minmax,
 		.extra1		= SYSCTL_LONG_ZERO,
 		.extra2		= SYSCTL_LONG_MAX,
 	},
diff --git a/include/linux/sysctl.h b/include/linux/sysctl.h
index aa4c6d44aaa0..4ecf945de956 100644
--- a/include/linux/sysctl.h
+++ b/include/linux/sysctl.h
@@ -82,6 +82,8 @@  int proc_dointvec_userhz_jiffies(const struct ctl_table *, int, void *, size_t *
 int proc_dointvec_ms_jiffies(const struct ctl_table *, int, void *, size_t *,
 		loff_t *);
 int proc_doulongvec_minmax(const struct ctl_table *, int, void *, size_t *, loff_t *);
+int proc_doulongvec_maxfiles_minmax(const struct ctl_table *, int, void *,
+		size_t *, loff_t *);
 int proc_doulongvec_ms_jiffies_minmax(const struct ctl_table *table, int, void *,
 		size_t *, loff_t *);
 int proc_do_large_bitmap(const struct ctl_table *, int, void *, size_t *, loff_t *);
diff --git a/kernel/sysctl.c b/kernel/sysctl.c
index 05b48b204ed4..5ee2bfc7fcbe 100644
--- a/kernel/sysctl.c
+++ b/kernel/sysctl.c
@@ -1122,6 +1122,23 @@  int proc_doulongvec_minmax(const struct ctl_table *table, int write,
     return do_proc_doulongvec_minmax(table, write, buffer, lenp, ppos, 1l, 1l);
 }
 
+/*
+ * Used for 'sysctl -w fs.file-max', ensuring its value will not be less
+ * than sysctl_nr_open.
+ */
+int proc_doulongvec_maxfiles_minmax(const struct ctl_table *table, int write,
+		void *buffer, size_t *lenp, loff_t *ppos)
+{
+	unsigned long *min = table->extra1;
+	unsigned long *max = table->extra2;
+	unsigned long nr_open = sysctl_nr_open;
+
+	if (write)
+		min = &nr_open;
+	return __do_proc_doulongvec_minmax(table->data, table, write,
+			buffer, lenp, ppos, 1l, 1l, min, max);
+}
+
 /**
  * proc_doulongvec_ms_jiffies_minmax - read a vector of millisecond values with min/max values
  * @table: the sysctl table