Skip to contentSkip to conversation

sysfs: Replace %p with %pK in the warning of sysfs_emit*

PATCH In review Kyle Tso 2 participants 3 messages Jun 23, 2023 Driver core

lore
Share & tools
lore

Kyle Tso

author
#0 lore raw

PATCH 1/1 sysfs: Replace %p with %pK in the warning of sysfs_emit*

According to Documentation/core-api/printk-formats.rst, kernel pointers need to be printed with %pK format specifier to respect kptr_restrict in sysctl.

Also replace the function names in the strings with %s and __func__ as checkpatch.pl suggested.

---
 fs/sysfs/file.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/sysfs/file.c +2 -2
index a12ac0356c69..56712f0886ef 100644
@@ -732,7 +732,7 @@ int sysfs_emit(char *buf, const char *fmt, ...)
 	int len;
 
 	if (WARN(!buf || offset_in_page(buf),
-		 "invalid sysfs_emit: buf:%p\n", buf))
+		 "invalid %s: buf:%pK\n", __func__, buf))
 		return 0;
 
 	va_start(args, fmt);
@@ -760,7 +760,7 @@ int sysfs_emit_at(char *buf, int at, const char *fmt, ...)
 	int len;
 
 	if (WARN(!buf || offset_in_page(buf) || at < 0 || at >= PAGE_SIZE,
-		 "invalid sysfs_emit_at: buf:%p at:%d\n", buf, at))
+		 "invalid %s: buf:%pK at:%d\n", __func__, buf, at))
 		return 0;
 
 	va_start(args, fmt);
Signature · 1 line
2.41.0.162.gfafddb0af9-goog
maintainer
#1 lore raw
Quoted 17 lines · Kyle Tso

According to Documentation/core-api/printk-formats.rst, kernel pointers need to be printed with %pK format specifier to respect kptr_restrict in sysctl.

Also replace the function names in the strings with %s and __func__ as checkpatch.pl suggested.

---
 fs/sysfs/file.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/sysfs/file.c +0 -0
index a12ac0356c69..56712f0886ef 100644

fs/sysfs/file.c int sysfs_emit(char *buf, const char *fmt, ...) · line 735

Quoted 5 lines
fs/sysfs/file.c +1 -1
@@ -732,7 +732,7 @@ int sysfs_emit(char *buf, const char *fmt, ...)
 	int len;
 
 	if (WARN(!buf || offset_in_page(buf),
-		 "invalid sysfs_emit: buf:%p\n", buf))
+		 "invalid %s: buf:%pK\n", __func__, buf))

Wait, no, these are not going out in sysfs files, as they are still going to dmesg, right?

So I think this line in the documentation matters:
This modifier is *only* intended when producing content of a file read by userspace from e.g. procfs or sysfs, not for dmesg. Please refer to the section about %p above for discussion about how to manage hashing pointers in printk().

so %p should still be ok here, the hashed value will end up in the kernerl log, which is what we want, and the WARN() traceback will show the problem properly, right?

fs/sysfs/file.c int sysfs_emit_at(char *buf, int at, const char *fmt, ...) · line 763

Quoted 9 lines
fs/sysfs/file.c +1 -1
 		return 0;
 
 	va_start(args, fmt);
@@ -760,7 +760,7 @@ int sysfs_emit_at(char *buf, int at, const char *fmt, ...)
 	int len;
 
 	if (WARN(!buf || offset_in_page(buf) || at < 0 || at >= PAGE_SIZE,
-		 "invalid sysfs_emit_at: buf:%p at:%d\n", buf, at))
+		 "invalid %s: buf:%pK at:%d\n", __func__, buf, at))

Same here, %p should be correct.

Sorry for missing this before.

greg k-h

author
#2 lore raw
Quoted 51 lines · Greg KH
Quoted 17 lines · Kyle Tso

According to Documentation/core-api/printk-formats.rst, kernel pointers need to be printed with %pK format specifier to respect kptr_restrict in sysctl.

Also replace the function names in the strings with %s and __func__ as checkpatch.pl suggested.

---
 fs/sysfs/file.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/sysfs/file.c +0 -0
index a12ac0356c69..56712f0886ef 100644

fs/sysfs/file.c int sysfs_emit(char *buf, const char *fmt, ...) · line 735

Quoted 5 lines
fs/sysfs/file.c +1 -1
@@ -732,7 +732,7 @@ int sysfs_emit(char *buf, const char *fmt, ...)
      int len;
 
      if (WARN(!buf || offset_in_page(buf),
-              "invalid sysfs_emit: buf:%p\n", buf))
+              "invalid %s: buf:%pK\n", __func__, buf))

Wait, no, these are not going out in sysfs files, as they are still going to dmesg, right?

So I think this line in the documentation matters:
This modifier is *only* intended when producing content of a file read by userspace from e.g. procfs or sysfs, not for dmesg. Please refer to the section about %p above for discussion about how to manage hashing pointers in printk().

so %p should still be ok here, the hashed value will end up in the kernerl log, which is what we want, and the WARN() traceback will show the problem properly, right?

fs/sysfs/file.c int sysfs_emit_at(char *buf, int at, const char *fmt, ...) · line 763

Quoted 9 lines
fs/sysfs/file.c +1 -1
              return 0;
 
      va_start(args, fmt);
@@ -760,7 +760,7 @@ int sysfs_emit_at(char *buf, int at, const char *fmt, ...)
      int len;
 
      if (WARN(!buf || offset_in_page(buf) || at < 0 || at >= PAGE_SIZE,
-              "invalid sysfs_emit_at: buf:%p at:%d\n", buf, at))
+              "invalid %s: buf:%pK at:%d\n", __func__, buf, at))

Same here, %p should be correct.

Quoted 3 lines

Sorry for missing this before.

greg k-h

Yes, you are right. Sorry for the noise.

Kyle