Skip to contentSkip to conversation

sysctl: drop the pointer dance in proc_put_char()

PATCH In review Bradley Morgan 3 participants 4 messages Aug 22 – Sep 25

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sysctl: drop the pointer dance in proc_put_char()

proc_put_char() still drags around a char **buffer alias, writing the char through it, advancing it, then copying it back into the slot it was loaded from. That only made sense when the buffer was __user and the char went through put_user() (which could fail).

Since commit 32927393dc1c ("sysctl: pass kernel pointers to ->proc_handler") the buffer is just a kernel pointer, so the alias is dead weight. proc_put_long() and the skip helpers already advance *buf directly, so do the same here. No functional change.

---
 kernel/sysctl.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
kernel/sysctl.c +2 -5
index f7b7598..2b92b30 100644
@@ -350,12 +350,9 @@ static void proc_put_long(void **buf, size_t *size, unsigned long val, bool neg)
 static void proc_put_char(void **buf, size_t *size, char c)
 {
 	if (*size) {
-		char **buffer = (char **)buf;
-		**buffer = c;
-
+		*(char *)*buf = c;
 		(*size)--;
-		(*buffer)++;
-		*buf = *buffer;
+		(*buf)++;
 	}
 }
 
Signature · 1 line
2.47.3
maintainer
#1 lore raw
Quoted 37 lines · Bradley Morgan

proc_put_char() still drags around a char **buffer alias, writing the char through it, advancing it, then copying it back into the slot it was loaded from. That only made sense when the buffer was __user and the char went through put_user() (which could fail).

Since commit 32927393dc1c ("sysctl: pass kernel pointers to ->proc_handler") the buffer is just a kernel pointer, so the alias is dead weight. proc_put_long() and the skip helpers already advance *buf directly, so do the same here. No functional change.

---
 kernel/sysctl.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
kernel/sysctl.c +2 -5
index f7b7598..2b92b30 100644
@@ -350,12 +350,9 @@ static void proc_put_long(void **buf, size_t *size, unsigned long val, bool neg)
 static void proc_put_char(void **buf, size_t *size, char c)
 {
 	if (*size) {
-		char **buffer = (char **)buf;
-		**buffer = c;
-
+		*(char *)*buf = c;
 		(*size)--;
-		(*buffer)++;
-		*buf = *buffer;
+		(*buf)++;
 	}
 }
 
Signature · 1 line
2.47.3

I was thinking more along these lines https://lore.kernel.org/all/[email protected]/

Best

#2 lore raw
Quoted 35 lines · On Wed, 16 Sep 2026 13:46:40 +0200 Joel Granados <[email protected]>…
Quoted 34 lines · Bradley Morgan

proc_put_char() still drags around a char **buffer alias, writing the char through it, advancing it, then copying it back into the slot it was loaded from. That only made sense when the buffer was __user and the char went through put_user() (which could fail).

Since commit 32927393dc1c ("sysctl: pass kernel pointers to ->proc_handler") the buffer is just a kernel pointer, so the alias is dead weight. proc_put_long() and the skip helpers already advance *buf directly, so do the same here. No functional change.

---
 kernel/sysctl.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
kernel/sysctl.c +2 -5
index f7b7598..2b92b30 100644
@@ -350,12 +350,9 @@ static void proc_put_long(void **buf, size_t *size, unsigned long val, bool neg)
 static void proc_put_char(void **buf, size_t *size, char c)
 {
 	if (*size) {
-		char **buffer = (char **)buf;
-		**buffer = c;
-
+		*(char *)*buf = c;
 		(*size)--;
-		(*buffer)++;
-		*buf = *buffer;
+		(*buf)++;
 	}
 }
 
Quoted 5 lines
Quoted 3 lines
Signature · 1 line
2.47.3

I was thinking more along these lines https://lore.kernel.org/all/[email protected]/

I think I'd decrement *size before writing to buf.
Should ensure the compiler never reloads *size.
Could also be worth an __always_inline - likely to make the code smaller.

David

Quoted 2 lines

Best

maintainer
#3 lore raw
Quoted 44 lines · David Laight
Quoted 35 lines · On Wed, 16 Sep 2026 13:46:40 +0200 Joel Granados <[email protected]>…
Quoted 34 lines · Bradley Morgan

proc_put_char() still drags around a char **buffer alias, writing the char through it, advancing it, then copying it back into the slot it was loaded from. That only made sense when the buffer was __user and the char went through put_user() (which could fail).

Since commit 32927393dc1c ("sysctl: pass kernel pointers to ->proc_handler") the buffer is just a kernel pointer, so the alias is dead weight. proc_put_long() and the skip helpers already advance *buf directly, so do the same here. No functional change.

---
 kernel/sysctl.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
kernel/sysctl.c +2 -5
index f7b7598..2b92b30 100644
@@ -350,12 +350,9 @@ static void proc_put_long(void **buf, size_t *size, unsigned long val, bool neg)
 static void proc_put_char(void **buf, size_t *size, char c)
 {
 	if (*size) {
-		char **buffer = (char **)buf;
-		**buffer = c;
-
+		*(char *)*buf = c;
 		(*size)--;
-		(*buffer)++;
-		*buf = *buffer;
+		(*buf)++;
 	}
 }
 
Quoted 5 lines
Quoted 3 lines
Signature · 1 line
2.47.3

I was thinking more along these lines https://lore.kernel.org/all/[email protected]/

Quoted 3 lines

I think I'd decrement *size before writing to buf.
Should ensure the compiler never reloads *size.
Could also be worth an __always_inline - likely to make the code smaller.

This seems sensible. I'll update my the one in [1]

[1] https://lore.kernel.org/all/[email protected]/