Skip to contentSkip to conversation

locking/lockdep: Replace snprintf with strscpy in seq_stats

PATCH RESEND Applied Thorsten Blum 4 participants 7 messages Mar 18 – Jun 9 Locking primitives

lore
Share & tools
lore
#0 lore raw

PATCH RESEND 1/1 locking/lockdep: Replace snprintf with strscpy in seq_stats

Replace snprintf("%s", ...) with the faster and more direct strscpy().

---
 kernel/locking/lockdep_proc.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
kernel/locking/lockdep_proc.c +3 -2
index 1916db9aa46b..e458fa258d05 100644
@@ -19,6 +19,7 @@
 #include <linux/debug_locks.h>
 #include <linux/vmalloc.h>
 #include <linux/sort.h>
+#include <linux/string.h>
 #include <linux/uaccess.h>
 #include <asm/div64.h>
 
@@ -488,9 +489,9 @@ static void seq_stats(struct seq_file *m, struct lock_stat_data *data)
 		const char *key_name;
 
 		key_name = __get_key_name(ckey, str);
-		snprintf(name, namelen, "%s", key_name);
+		strscpy(name, key_name, namelen);
 	} else {
-		snprintf(name, namelen, "%s", cname);
+		strscpy(name, cname, namelen);
 	}
 	rcu_read_unlock_sched();
 
reviewer Acked-by on v1 1/1
#1 lore raw
Quoted 23 lines · Thorsten Blum

Replace snprintf("%s", ...) with the faster and more direct strscpy().

---
  kernel/locking/lockdep_proc.c | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
kernel/locking/lockdep_proc.c +1 -0
index 1916db9aa46b..e458fa258d05 100644
@@ -19,6 +19,7 @@
  #include <linux/debug_locks.h>
  #include <linux/vmalloc.h>
  #include <linux/sort.h>
+#include <linux/string.h>
  #include <linux/uaccess.h>
  #include <asm/div64.h>
 
@@ -488,9 +489,9 @@ static void seq_stats(struct seq_file *m, struct lock_stat_data *data)
  		const char *key_name;

kernel/locking/lockdep_proc.c static void seq_stats(struct seq_file *m, struct lock_stat_data *data) · line 497

Quoted 10 lines
kernel/locking/lockdep_proc.c +2 -2
 
  		key_name = __get_key_name(ckey, str);
-		snprintf(name, namelen, "%s", key_name);
+		strscpy(name, key_name, namelen);
  	} else {
-		snprintf(name, namelen, "%s", cname);
+		strscpy(name, cname, namelen);
  	}
  	rcu_read_unlock_sched();
 
maintainer Applied
#2 lore raw
Quoted 25 lines · Waiman Long
Quoted 23 lines · Thorsten Blum

Replace snprintf("%s", ...) with the faster and more direct strscpy().

---
  kernel/locking/lockdep_proc.c | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
kernel/locking/lockdep_proc.c +1 -1
index 1916db9aa46b..e458fa258d05 100644
@@ -19,6 +19,7 @@
  #include <linux/debug_locks.h>
  #include <linux/vmalloc.h>
  #include <linux/sort.h>
+#include <linux/string.h>
  #include <linux/uaccess.h>
  #include <asm/div64.h>
@@ -488,9 +489,9 @@ static void seq_stats(struct seq_file *m, struct lock_stat_data *data)
  		const char *key_name;
  		key_name = __get_key_name(ckey, str);
-		snprintf(name, namelen, "%s", key_name);
Quoted 9 lines

kernel/locking/lockdep_proc.c static void seq_stats(struct seq_file *m, struct lock_stat_data *data) · line 497

Quoted 7 lines
kernel/locking/lockdep_proc.c +2 -1
+		strscpy(name, key_name, namelen);
  	} else {
-		snprintf(name, namelen, "%s", cname);
+		strscpy(name, cname, namelen);
  	}
  	rcu_read_unlock_sched();
 

Queued, thank you both!

Regards,
Boqun

maintainer
#5 lore raw
In reply to Thorsten Blum
Quoted 21 lines · Thorsten Blum

Replace snprintf("%s", ...) with the faster and more direct strscpy().

---
 kernel/locking/lockdep_proc.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
kernel/locking/lockdep_proc.c +1 -0
index 1916db9aa46b..e458fa258d05 100644
@@ -19,6 +19,7 @@
 #include <linux/debug_locks.h>
 #include <linux/vmalloc.h>
 #include <linux/sort.h>
+#include <linux/string.h>
 #include <linux/uaccess.h>
 #include <asm/div64.h>
 

kernel/locking/lockdep_proc.c static void seq_stats(struct seq_file *m, struct lock_stat_data *data) · line 496

Quoted 10 lines
kernel/locking/lockdep_proc.c +2 -2
@@ -488,9 +489,9 @@ static void seq_stats(struct seq_file *m, struct lock_stat_data *data)
 		const char *key_name;
 
 		key_name = __get_key_name(ckey, str);
-		snprintf(name, namelen, "%s", key_name);
+		strscpy(name, key_name, namelen);
 	} else {
-		snprintf(name, namelen, "%s", cname);
+		strscpy(name, cname, namelen);
 	}
 	rcu_read_unlock_sched();

Why though? I suppose it doesn't matter, but this hardly seems worth the electrons is was sent with :/

#6 lore raw
Quoted 24 lines · Peter Zijlstra
Quoted 23 lines · Thorsten Blum

Replace snprintf("%s", ...) with the faster and more direct strscpy().

---
 kernel/locking/lockdep_proc.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
kernel/locking/lockdep_proc.c +1 -0
index 1916db9aa46b..e458fa258d05 100644
@@ -19,6 +19,7 @@
 #include <linux/debug_locks.h>
 #include <linux/vmalloc.h>
 #include <linux/sort.h>
+#include <linux/string.h>
 #include <linux/uaccess.h>
 #include <asm/div64.h>
 
@@ -488,9 +489,9 @@ static void seq_stats(struct seq_file *m, struct lock_stat_data *data)
 		const char *key_name;
 
Quoted 11 lines

kernel/locking/lockdep_proc.c static void seq_stats(struct seq_file *m, struct lock_stat_data *data) · line 496

Quoted 8 lines
kernel/locking/lockdep_proc.c +2 -2
 		key_name = __get_key_name(ckey, str);
-		snprintf(name, namelen, "%s", key_name);
+		strscpy(name, key_name, namelen);
 	} else {
-		snprintf(name, namelen, "%s", cname);
+		strscpy(name, cname, namelen);
 	}
 	rcu_read_unlock_sched();

Why though? I suppose it doesn't matter, but this hardly seems worth the electrons is was sent with :/

Is "faster and more direct" not a reason? There's nothing to format so the format specifier is unnecessary and can be removed.

maintainer
#3 lore raw
In reply to Boqun Feng
Quoted 36 lines · Boqun Feng
Quoted 25 lines · Waiman Long
Quoted 23 lines · Thorsten Blum

Replace snprintf("%s", ...) with the faster and more direct strscpy().

---
  kernel/locking/lockdep_proc.c | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
kernel/locking/lockdep_proc.c +1 -1
index 1916db9aa46b..e458fa258d05 100644
@@ -19,6 +19,7 @@
  #include <linux/debug_locks.h>
  #include <linux/vmalloc.h>
  #include <linux/sort.h>
+#include <linux/string.h>
  #include <linux/uaccess.h>
  #include <asm/div64.h>
@@ -488,9 +489,9 @@ static void seq_stats(struct seq_file *m, struct lock_stat_data *data)
  		const char *key_name;
  		key_name = __get_key_name(ckey, str);
-		snprintf(name, namelen, "%s", key_name);
Quoted 9 lines

kernel/locking/lockdep_proc.c static void seq_stats(struct seq_file *m, struct lock_stat_data *data) · line 497

Quoted 7 lines
kernel/locking/lockdep_proc.c +2 -1
+		strscpy(name, key_name, namelen);
  	} else {
-		snprintf(name, namelen, "%s", cname);
+		strscpy(name, cname, namelen);
  	}
  	rcu_read_unlock_sched();
 
Quoted 2 lines

Queued, thank you both!

I was trying to merge it for v7.2 but sashiko found it out this:

	https://sashiko.dev/#/patchset/20260605052331.1628-1-boqun%40kernel.org

Thorsten, could you take a look?

Regards,
Boqun

Quoted 2 lines

Regards,
Boqun

#4 lore raw
Quoted 37 lines · Boqun Feng
Quoted 36 lines · Boqun Feng
Quoted 34 lines · Waiman Long
Quoted 20 lines · Thorsten Blum

Replace snprintf("%s", ...) with the faster and more direct strscpy().

---
  kernel/locking/lockdep_proc.c | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
kernel/locking/lockdep_proc.c +1 -0
index 1916db9aa46b..e458fa258d05 100644
@@ -19,6 +19,7 @@
  #include <linux/debug_locks.h>
  #include <linux/vmalloc.h>
  #include <linux/sort.h>
+#include <linux/string.h>
  #include <linux/uaccess.h>
  #include <asm/div64.h>

kernel/locking/lockdep_proc.c static void seq_stats(struct seq_file *m, struct lock_stat_data *data) · line 496

Quoted 10 lines
kernel/locking/lockdep_proc.c +2 -2
@@ -488,9 +489,9 @@ static void seq_stats(struct seq_file *m, struct lock_stat_data *data)
  		const char *key_name;
  		key_name = __get_key_name(ckey, str);
-		snprintf(name, namelen, "%s", key_name);
+		strscpy(name, key_name, namelen);
  	} else {
-		snprintf(name, namelen, "%s", cname);
+		strscpy(name, cname, namelen);
  	}
  	rcu_read_unlock_sched();
 
Quoted 8 lines
Quoted 2 lines

Queued, thank you both!

I was trying to merge it for v7.2 but sashiko found it out this:

	https://sashiko.dev/#/patchset/20260605052331.1628-1-boqun%40kernel.org

Thorsten, could you take a look?

This appears valid, so we should probably drop this patch. I incorrectly assumed snprintf() also requires the source to be non-NULL for %s.

Handling a failed lookup explicitly could improve this code, but that requires more work and testing because it would change /proc/lock_stat output from "(null)" to no output.

Thanks for catching this,
Thorsten