Skip to contentSkip to conversation

tracing: use %ps format string to print symbols

PATCH Superseded Arnd Bergmann 2 participants 3 messages Oct 18, 2021 – Oct 19, 2021 Tracing

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 tracing: use %ps format string to print symbols

From: Arnd Bergmann <[email protected]>

clang started warning about excessive stack usage in hist_trigger_print_key()

kernel/trace/trace_events_hist.c:4723:13: error: stack frame size (1336) exceeds limit (1024) in function 'hist_trigger_print_key' [-Werror,-Wframe-larger-than]

The problem is that there are two 512-byte arrays on the stack if hist_trigger_stacktrace_print() gets inlined. I don't think this has changed in the past five years, but something probably changed the inlining decisions made by the compiler, so the problem is now made more obvious.

Rather than printing the symbol names into separate buffers, it seems we can simply use the special %ps format string modifier to print the pointers symbolically and get rid of both buffers.

Marking hist_trigger_stacktrace_print() would be a simpler way of avoiding the warning, but that would not address the excessive stack usage.

Fixes 69a0200c2e25 ("tracing: Add hist trigger support for stacktraces as keys")

---
Changes in v2:
- Use %pS instead of %ps to include offset in third string
- add (void*) cast to avoid compile-time warnings

This is still only compile-tested to ensure that the warning goes away, I have not validated what it does to the formatting.

---
 kernel/trace/trace_events_hist.c | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)
kernel/trace/trace_events_hist.c +5 -10
index a6061a69aa84..d6d4362c273c 100644
@@ -4706,7 +4706,6 @@ static void hist_trigger_stacktrace_print(struct seq_file *m,
 					  unsigned long *stacktrace_entries,
 					  unsigned int max_entries)
 {
-	char str[KSYM_SYMBOL_LEN];
 	unsigned int spaces = 8;
 	unsigned int i;
 
@@ -4715,8 +4714,7 @@ static void hist_trigger_stacktrace_print(struct seq_file *m,
 			return;
 
 		seq_printf(m, "%*c", 1 + spaces, ' ');
-		sprint_symbol(str, stacktrace_entries[i]);
-		seq_printf(m, "%s\n", str);
+		seq_printf(m, "%pS\n", (void*)stacktrace_entries[i]);
 	}
 }
 
@@ -4726,7 +4724,6 @@ static void hist_trigger_print_key(struct seq_file *m,
 				   struct tracing_map_elt *elt)
 {
 	struct hist_field *key_field;
-	char str[KSYM_SYMBOL_LEN];
 	bool multiline = false;
 	const char *field_name;
 	unsigned int i;
@@ -4747,14 +4744,12 @@ static void hist_trigger_print_key(struct seq_file *m,
 			seq_printf(m, "%s: %llx", field_name, uval);
 		} else if (key_field->flags & HIST_FIELD_FL_SYM) {
 			uval = *(u64 *)(key + key_field->offset);
-			sprint_symbol_no_offset(str, uval);
-			seq_printf(m, "%s: [%llx] %-45s", field_name,
-				   uval, str);
+			seq_printf(m, "%s: [%llx] %-45ps", field_name,
+				   uval, (void *)uval);
 		} else if (key_field->flags & HIST_FIELD_FL_SYM_OFFSET) {
 			uval = *(u64 *)(key + key_field->offset);
-			sprint_symbol(str, uval);
-			seq_printf(m, "%s: [%llx] %-55s", field_name,
-				   uval, str);
+			seq_printf(m, "%s: [%llx] %-55pS", field_name,
+				   uval, (void *)uval);
 		} else if (key_field->flags & HIST_FIELD_FL_EXECNAME) {
 			struct hist_elt_data *elt_data = elt->private_data;
 			char *comm;
Signature · 1 line
2.29.2
Reviewed-by Tested-by on v2 1/1
#1 lore raw

Hi Arnd,

Quoted 27 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

clang started warning about excessive stack usage in hist_trigger_print_key()

kernel/trace/trace_events_hist.c:4723:13: error: stack frame size (1336) exceeds limit (1024) in function 'hist_trigger_print_key' [- Werror,-Wframe-larger-than]

The problem is that there are two 512-byte arrays on the stack if hist_trigger_stacktrace_print() gets inlined. I don't think this has changed in the past five years, but something probably changed the inlining decisions made by the compiler, so the problem is now made more obvious.

Rather than printing the symbol names into separate buffers, it seems we can simply use the special %ps format string modifier to print the pointers symbolically and get rid of both buffers.

Marking hist_trigger_stacktrace_print() would be a simpler way of avoiding the warning, but that would not address the excessive stack usage.

Fixes 69a0200c2e25 ("tracing: Add hist trigger support for

stacktraces as keys")
Link:
https://lore.kernel.org/all/[email protected]/

Quoted 8 lines

---
Changes in v2:
- Use %pS instead of %ps to include offset in third string
- add (void*) cast to avoid compile-time warnings

This is still only compile-tested to ensure that the warning goes away, I have not validated what it does to the formatting.

This looks fine to me, thanks for the patch!

Quoted 61 lines
---
 kernel/trace/trace_events_hist.c | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)
kernel/trace/trace_events_hist.c +0 -1
index a6061a69aa84..d6d4362c273c 100644
@@ -4706,7 +4706,6 @@ static void
 hist_trigger_stacktrace_print(struct seq_file *m,
 					  unsigned long
 *stacktrace_entries,
 					  unsigned int max_entries)
 {
-	char str[KSYM_SYMBOL_LEN];
 	unsigned int spaces = 8;
kernel/trace/trace_events_hist.c +1 -3
 	unsigned int i;
@@ -4715,8 +4714,7 @@ static void
 hist_trigger_stacktrace_print(struct seq_file *m,
 			return;
 
 		seq_printf(m, "%*c", 1 + spaces, ' ');
-		sprint_symbol(str, stacktrace_entries[i]);
-		seq_printf(m, "%s\n", str);
+		seq_printf(m, "%pS\n", (void*)stacktrace_entries[i]);
 	}
 }
@@ -4726,7 +4724,6 @@ static void hist_trigger_print_key(struct
 seq_file *m,
 				   struct tracing_map_elt *elt)
 {
 	struct hist_field *key_field;
-	char str[KSYM_SYMBOL_LEN];
 	bool multiline = false;
 	const char *field_name;
kernel/trace/trace_events_hist.c +4 -6
 	unsigned int i;
@@ -4747,14 +4744,12 @@ static void hist_trigger_print_key(struct
 seq_file *m,
 			seq_printf(m, "%s: %llx", field_name, uval);
 		} else if (key_field->flags & HIST_FIELD_FL_SYM) {
 			uval = *(u64 *)(key + key_field->offset);
-			sprint_symbol_no_offset(str, uval);
-			seq_printf(m, "%s: [%llx] %-45s", field_name,
-				   uval, str);
+			seq_printf(m, "%s: [%llx] %-45ps", field_name,
+				   uval, (void *)uval);
 		} else if (key_field->flags & HIST_FIELD_FL_SYM_OFFSET)
 {
 			uval = *(u64 *)(key + key_field->offset);
-			sprint_symbol(str, uval);
-			seq_printf(m, "%s: [%llx] %-55s", field_name,
-				   uval, str);
+			seq_printf(m, "%s: [%llx] %-55pS", field_name,
+				   uval, (void *)uval);
 		} else if (key_field->flags & HIST_FIELD_FL_EXECNAME) {

struct hist_elt_data *elt_data = elt-

Quoted 1 line

private_data;

char *comm;

#2 lore raw
Quoted 6 lines · Tom Zanussi

On Mon, 2021-10-18 at 15:25 +0200, Arnd Bergmann wrote:

This looks fine to me, thanks for the patch!

Thanks for testing!

Quoted 4 lines

kernel/trace/trace_events_hist.c static void hist_trigger_print_key(struct seq_file *m, · line 4748

Quoted 4 lines
kernel/trace/trace_events_hist.c +2 -2
-                     seq_printf(m, "%s: [%llx] %-45s", field_name,
-                                uval, str);
+                     seq_printf(m, "%s: [%llx] %-45ps", field_name,
+                                uval, (void *)uval);

This turns out to be still wrong on 32-bit, as 'uval' is a u64 and I can't cast that to a pointer without a uintptr_t cast first.

I was testing randconfig builds with clang here, and for some reason that didn't catch the bug, but I found it after going back to gcc testing.

Sending v3 now.

Arnd