Skip to contentSkip to conversation

fs: move and constify sysctl_nr_open_min, sysctl_nr_open_max

PATCH In review Alexey Dobriyan 2 participants 5 messages Oct 11, 2023 – Oct 15, 2023 VFS

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fs: move and constify sysctl_nr_open_min, sysctl_nr_open_max

sysctl_nr_open_min, sysctl_nr_open_max variables are really hard limits on fs.nr_open sysctl, they aren't interesting to the rest of the code and are constants (sysctl_nr_open is not constant obviously).

---

 fs/file.c            |    5 -----
 fs/file_table.c      |   10 ++++++++--
 include/linux/file.h |    2 --
 3 files changed, 8 insertions(+), 9 deletions(-)
fs/file.c +0 -5
@@ -25,11 +25,6 @@
 #include "internal.h"
 
 unsigned int sysctl_nr_open __read_mostly = 1024*1024;
-unsigned int sysctl_nr_open_min = BITS_PER_LONG;
-/* our min() is unusable in constant expressions ;-/ */
-#define __const_min(x, y) ((x) < (y) ? (x) : (y))
-unsigned int sysctl_nr_open_max =
-	__const_min(INT_MAX, ~(size_t)0/sizeof(void *)) & -BITS_PER_LONG;
 
 static void __free_fdtable(struct fdtable *fdt)
 {
fs/file_table.c +8 -2
@@ -111,6 +111,12 @@ static int proc_nr_files(struct ctl_table *table, int write, void *buffer,
 	return proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
 }
 
+static const unsigned int sysctl_nr_open_min = BITS_PER_LONG;
+/* our min() is unusable in constant expressions ;-/ */
+#define __const_min(x, y) ((x) < (y) ? (x) : (y))
+static const unsigned int sysctl_nr_open_max =
+	__const_min(INT_MAX, ~(size_t)0/sizeof(void *)) & -BITS_PER_LONG;
+
 static struct ctl_table fs_stat_sysctls[] = {
 	{
 		.procname	= "file-nr",
@@ -134,8 +140,8 @@ static struct ctl_table fs_stat_sysctls[] = {
 		.maxlen		= sizeof(unsigned int),
 		.mode		= 0644,
 		.proc_handler	= proc_dointvec_minmax,
-		.extra1		= &sysctl_nr_open_min,
-		.extra2		= &sysctl_nr_open_max,
+		.extra1		= (void *)&sysctl_nr_open_min,
+		.extra2		= (void *)&sysctl_nr_open_max,
 	},
 	{ }
 };
include/linux/file.h +0 -2
@@ -113,6 +113,4 @@ int receive_fd_replace(int new_fd, struct file *file, unsigned int o_flags);
 extern void flush_delayed_fput(void);
 extern void __fput_sync(struct file *);
 
-extern unsigned int sysctl_nr_open_min, sysctl_nr_open_max;
-
 #endif /* __LINUX_FILE_H */
#1 lore raw

From: Alexey Dobriyan

Quoted 31 lines

Sent: 11 October 2023 19:24

sysctl_nr_open_min, sysctl_nr_open_max variables are really hard limits on fs.nr_open sysctl, they aren't interesting to the rest of the code and are constants (sysctl_nr_open is not constant obviously).

---

 fs/file.c            |    5 -----
 fs/file_table.c      |   10 ++++++++--
 include/linux/file.h |    2 --
 3 files changed, 8 insertions(+), 9 deletions(-)
fs/file.c +0 -5
@@ -25,11 +25,6 @@
 #include "internal.h"
 
 unsigned int sysctl_nr_open __read_mostly = 1024*1024;
-unsigned int sysctl_nr_open_min = BITS_PER_LONG;
-/* our min() is unusable in constant expressions ;-/ */
-#define __const_min(x, y) ((x) < (y) ? (x) : (y))
-unsigned int sysctl_nr_open_max =
-	__const_min(INT_MAX, ~(size_t)0/sizeof(void *)) & -BITS_PER_LONG;
 
 static void __free_fdtable(struct fdtable *fdt)
 {

fs/file_table.c static int proc_nr_files(struct ctl_table *table, int write, void *buffer, · line 118

Quoted 8 lines
fs/file_table.c +5 -0
@@ -111,6 +111,12 @@ static int proc_nr_files(struct ctl_table *table, int write, void *buffer,
 	return proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
 }
 
+static const unsigned int sysctl_nr_open_min = BITS_PER_LONG;
+/* our min() is unusable in constant expressions ;-/ */
+#define __const_min(x, y) ((x) < (y) ? (x) : (y))
+static const unsigned int sysctl_nr_open_max =
+	__const_min(INT_MAX, ~(size_t)0/sizeof(void *)) & -BITS_PER_LONG;

I think you'll find that min() is fine.

Although those upper limits look suspect to me.
Looks like ~0.5G on 32bit and ~2G on 64bit based on the size of a bitmap? But the kernel will run out of address space much earlier - esp on 32bit.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

#2 lore raw
Quoted 9 lines · David Laight

From: Alexey Dobriyan

fs/file_table.c static int proc_nr_files(struct ctl_table *table, int write, void *buffer, · line 118

Quoted 6 lines

Sent: 11 October 2023 19:24

fs/file_table.c +4 -0
+/* our min() is unusable in constant expressions ;-/ */
+#define __const_min(x, y) ((x) < (y) ? (x) : (y))
+static const unsigned int sysctl_nr_open_max =
+	__const_min(INT_MAX, ~(size_t)0/sizeof(void *)) & -BITS_PER_LONG;

I think you'll find that min() is fine.

Mainline min() still can't do

	static const x = min(1, 2);
#3 lore raw

From: Alexey Dobriyan

Quoted 13 lines

Sent: 14 October 2023 10:23

Quoted 9 lines · David Laight

From: Alexey Dobriyan

fs/file_table.c static int proc_nr_files(struct ctl_table *table, int write, void *buffer, · line 118

Quoted 6 lines

Sent: 11 October 2023 19:24

fs/file_table.c +4 -0
+/* our min() is unusable in constant expressions ;-/ */
+#define __const_min(x, y) ((x) < (y) ? (x) : (y))
+static const unsigned int sysctl_nr_open_max =
+	__const_min(INT_MAX, ~(size_t)0/sizeof(void *)) & -BITS_PER_LONG;

I think you'll find that min() is fine.

Quoted 3 lines

Mainline min() still can't do

	static const x = min(1, 2);

There has been a whole load of stuff that is supposed to do that for ages - my changes don't affect it.

I presume they work - because they make it horribly complicated.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

#4 lore raw
In reply to Alexey Dobriyan

From: Alexey Dobriyan

Quoted 13 lines

Sent: 14 October 2023 10:23

Quoted 9 lines · David Laight

From: Alexey Dobriyan

fs/file_table.c static int proc_nr_files(struct ctl_table *table, int write, void *buffer, · line 118

Quoted 6 lines

Sent: 11 October 2023 19:24

fs/file_table.c +4 -0
+/* our min() is unusable in constant expressions ;-/ */
+#define __const_min(x, y) ((x) < (y) ? (x) : (y))
+static const unsigned int sysctl_nr_open_max =
+	__const_min(INT_MAX, ~(size_t)0/sizeof(void *)) & -BITS_PER_LONG;

I think you'll find that min() is fine.

Quoted 3 lines

Mainline min() still can't do

	static const x = min(1, 2);

(second reply having realised what actually fails.)

That is ok.

The code actually falls foul of the type check.
The LHS will be 'signed int' and the RHS 'size_t' which IIRC is 'unsigned int' on 32 bit and 'unsigned long' on 64 bit.

So the comment is explaining the wrong error. Changing to (size_t)INT_MAX should make min() work.

My new version should let that through, it allows unsigned against non-negative signed constants.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)