diff mbox series

[6/6] fs: synchronize the access of fs.file-max and fs.nr_open

Message ID 20241123181323.184391-1-alexjlzheng@tencent.com (mailing list archive)
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:13 p.m. UTC
Signed-off-by: Jinliang Zheng <alexjlzheng@tencent.com>
---
 fs/file_table.c    |  9 ++++++++-
 include/linux/fs.h |  1 +
 kernel/sysctl.c    | 18 ++++++++++++++----
 3 files changed, 23 insertions(+), 5 deletions(-)
diff mbox series

Patch

diff --git a/fs/file_table.c b/fs/file_table.c
index 43838354ce6d..4c0113912d9b 100644
--- a/fs/file_table.c
+++ b/fs/file_table.c
@@ -37,6 +37,8 @@  struct files_stat_struct files_stat = {
 	.max_files = NR_FILE
 };
 
+DECLARE_RWSEM(file_number_sem);
+
 /* SLAB cache for file structures */
 static struct kmem_cache *filp_cachep __ro_after_init;
 static struct kmem_cache *bfilp_cachep __ro_after_init;
@@ -102,8 +104,13 @@  EXPORT_SYMBOL_GPL(get_max_files);
 static int proc_nr_files(const struct ctl_table *table, int write, void *buffer,
 			 size_t *lenp, loff_t *ppos)
 {
+	int ret;
+
+	down_read(&file_number_sem);
 	files_stat.nr_files = get_nr_files();
-	return proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
+	ret = proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
+	up_read(&file_number_sem);
+	return ret;
 }
 
 static struct ctl_table fs_stat_sysctls[] = {
diff --git a/include/linux/fs.h b/include/linux/fs.h
index 931076faadde..f8f983e5dde6 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -90,6 +90,7 @@  extern void __init files_maxfiles_init(void);
 extern unsigned long get_max_files(void);
 extern unsigned int sysctl_nr_open;
 extern struct files_stat_struct files_stat;
+extern struct rw_semaphore file_number_sem;
 
 typedef __kernel_rwf_t rwf_t;
 
diff --git a/kernel/sysctl.c b/kernel/sysctl.c
index d8ce18368ab3..cf860d0e2c8b 100644
--- a/kernel/sysctl.c
+++ b/kernel/sysctl.c
@@ -951,18 +951,22 @@  int proc_douintvec_minmax(const struct ctl_table *table, int write,
 int proc_douintvec_nropen_minmax(const struct ctl_table *table, int write,
 		void *buffer, size_t *lenp, loff_t *ppos)
 {
+	int ret;
 	unsigned int file_max;
 	struct do_proc_douintvec_minmax_conv_param param = {
 		.min = (unsigned int *) table->extra1,
 		.max = (unsigned int *) table->extra2,
 	};
 
+	down_write(&file_number_sem);
 	file_max = min_t(unsigned int, files_stat.max_files,
 			*(unsigned int *)table->extra2);
 	if (write)
 		param.max = &file_max;
-	return do_proc_douintvec(table, write, buffer, lenp, ppos,
+	ret = do_proc_douintvec(table, write, buffer, lenp, ppos,
 				 do_proc_douintvec_minmax_conv, &param);
+	up_write(&file_number_sem);
+	return ret;
 }
 
 /**
@@ -1150,14 +1154,20 @@  int proc_doulongvec_minmax(const struct ctl_table *table, int write,
 int proc_doulongvec_maxfiles_minmax(const struct ctl_table *table, int write,
 		void *buffer, size_t *lenp, loff_t *ppos)
 {
+	int ret;
 	unsigned long *min = table->extra1;
 	unsigned long *max = table->extra2;
-	unsigned long nr_open = sysctl_nr_open;
+	unsigned long nr_open;
 
-	if (write)
+	down_write(&file_number_sem);
+	if (write) {
+		nr_open = sysctl_nr_open;
 		min = &nr_open;
-	return __do_proc_doulongvec_minmax(table->data, table, write,
+	}
+	ret = __do_proc_doulongvec_minmax(table->data, table, write,
 			buffer, lenp, ppos, 1l, 1l, min, max);
+	up_write(&file_number_sem);
+	return ret;
 }
 
 /**