Skip to contentSkip to conversation

tracing: events_hist: avoid using excessive stack space

PATCH In review Arnd Bergmann 3 participants 5 messages May 14, 2021 Tracing

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tracing: events_hist: avoid using excessive stack space

From: Arnd Bergmann <[email protected]>

In some configurations, clang produces a warning about an overly large amount of stack space used in hist_trigger_print_key():

kernel/trace/trace_events_hist.c:4594:13: error: stack frame size of 1248 bytes in function 'hist_trigger_print_key' [-Werror,-Wframe-larger-than=] static void hist_trigger_print_key(struct seq_file *m,

Moving the 'str' variable into a more local scope in the two places where it gets used actually reduces the the used stack space here and gets it below the warning limit, because the compiler can now assume that it is safe to use the same stack slot that it has for the stack of any inline function.

---
 kernel/trace/trace_events_hist.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/trace/trace_events_hist.c +2 -1
index c1abd63f1d6c..e3fe84f017a8 100644
@@ -4597,7 +4597,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;
@@ -4617,11 +4616,13 @@ static void hist_trigger_print_key(struct seq_file *m,
 			uval = *(u64 *)(key + key_field->offset);
 			seq_printf(m, "%s: %llx", field_name, uval);
 		} else if (key_field->flags & HIST_FIELD_FL_SYM) {
+			char str[KSYM_SYMBOL_LEN];
 			uval = *(u64 *)(key + key_field->offset);
 			sprint_symbol_no_offset(str, uval);
 			seq_printf(m, "%s: [%llx] %-45s", field_name,
 				   uval, str);
 		} else if (key_field->flags & HIST_FIELD_FL_SYM_OFFSET) {
+			char str[KSYM_SYMBOL_LEN];
 			uval = *(u64 *)(key + key_field->offset);
 			sprint_symbol(str, uval);
 			seq_printf(m, "%s: [%llx] %-55s", field_name,
Signature · 1 line
2.29.2
maintainer
#1 lore raw
Quoted 5 lines · On Fri, 14 May 2021 16:04:25 +0200 Arnd Bergmann <[email protected]> wrote:

From: Arnd Bergmann <[email protected]>

In some configurations, clang produces a warning about an overly large amount of stack space used in hist_trigger_print_key():

Quoted 8 lines

kernel/trace/trace_events_hist.c:4594:13: error: stack frame size of 1248 bytes in function 'hist_trigger_print_key' [-Werror,-Wframe-larger-than=] static void hist_trigger_print_key(struct seq_file *m,

Moving the 'str' variable into a more local scope in the two places where it gets used actually reduces the the used stack space here and gets it below the warning limit, because the compiler can now assume that it is safe to use the same stack slot that it has for the stack of any inline function.

Thanks Arnd for the nice explanation of the rationale for this change.

But I still find it too subtle to my liking that we need to move the declaration like this (and duplicate it twice) for internal behavior of the compiler (where it can't figure out itself by the use cases if it can optimize the stack).

Quoted 11 lines
---
 kernel/trace/trace_events_hist.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/trace/trace_events_hist.c +0 -0
index c1abd63f1d6c..e3fe84f017a8 100644

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

Quoted 4 lines
kernel/trace/trace_events_hist.c +0 -1
@@ -4597,7 +4597,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];

Instead, I think we should just make str static, as this should only be called under the event_mutex. To be sure, we can also add:

	/* To protect the static str variable */
	lockdep_assert_held(&event_mutex);

-- Steve

Quoted 17 lines
kernel/trace/trace_events_hist.c +2 -0
 	bool multiline = false;
 	const char *field_name;
 	unsigned int i;
@@ -4617,11 +4616,13 @@ static void hist_trigger_print_key(struct seq_file *m,
 			uval = *(u64 *)(key + key_field->offset);
 			seq_printf(m, "%s: %llx", field_name, uval);
 		} else if (key_field->flags & HIST_FIELD_FL_SYM) {
+			char str[KSYM_SYMBOL_LEN];
 			uval = *(u64 *)(key + key_field->offset);
 			sprint_symbol_no_offset(str, uval);
 			seq_printf(m, "%s: [%llx] %-45s", field_name,
 				   uval, str);
 		} else if (key_field->flags & HIST_FIELD_FL_SYM_OFFSET) {
+			char str[KSYM_SYMBOL_LEN];
 			uval = *(u64 *)(key + key_field->offset);
 			sprint_symbol(str, uval);
 			seq_printf(m, "%s: [%llx] %-55s", field_name,
#2 lore raw
Quoted 16 lines · Steven Rostedt
Quoted 5 lines · On Fri, 14 May 2021 16:04:25 +0200 Arnd Bergmann <[email protected]> wrote:

From: Arnd Bergmann <[email protected]>

In some configurations, clang produces a warning about an overly large amount of stack space used in hist_trigger_print_key():

Quoted 8 lines

kernel/trace/trace_events_hist.c:4594:13: error: stack frame size of 1248 bytes in function 'hist_trigger_print_key' [-Werror,-Wframe-larger-than=] static void hist_trigger_print_key(struct seq_file *m,

Moving the 'str' variable into a more local scope in the two places where it gets used actually reduces the the used stack space here and gets it below the warning limit, because the compiler can now assume that it is safe to use the same stack slot that it has for the stack of any inline function.

Quoted 6 lines

Thanks Arnd for the nice explanation of the rationale for this change.

But I still find it too subtle to my liking that we need to move the declaration like this (and duplicate it twice) for internal behavior of the compiler (where it can't figure out itself by the use cases if it can optimize the stack).

It's not really internal behavior of the compiler that causes the smaller scope to help here, I'm sure gcc has to do the same thing regarding the placement of this variable on the stack. Clang does have some related cases that it doesn't do as well as gcc, but I don't think this is one of them.

The difference between the compilers or configurations is the way that inlining decisions are made, so if a compiler inlines another function into this one that also uses a lot of stack space, that will trigger the warning, while a compiler that does not inline that would not warn but also not use any less stack space in the code path that uses the nested function.

Quoted 13 lines

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

Quoted 9 lines
kernel/trace/trace_events_hist.c +0 -1
index c1abd63f1d6c..e3fe84f017a8 100644
@@ -4597,7 +4597,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];

Instead, I think we should just make str static, as this should only be called under the event_mutex. To be sure, we can also add:

Quoted 2 lines
        /* To protect the static str variable */
        lockdep_assert_held(&event_mutex);

Ok, I changed my local version to this, will send v2 once it passes some more randconfig builds.

Arnd

#3 lore raw
In reply to Steven Rostedt
Quoted 18 lines · Steven Rostedt
Quoted 5 lines · On Fri, 14 May 2021 16:04:25 +0200 Arnd Bergmann <[email protected]> wrote:

From: Arnd Bergmann <[email protected]>

In some configurations, clang produces a warning about an overly large amount of stack space used in hist_trigger_print_key():

Quoted 8 lines

kernel/trace/trace_events_hist.c:4594:13: error: stack frame size of 1248 bytes in function 'hist_trigger_print_key' [-Werror,-Wframe-larger-than=] static void hist_trigger_print_key(struct seq_file *m,

Moving the 'str' variable into a more local scope in the two places where it gets used actually reduces the the used stack space here and gets it below the warning limit, because the compiler can now assume that it is safe to use the same stack slot that it has for the stack of any inline function.

Quoted 6 lines

Thanks Arnd for the nice explanation of the rationale for this change.

But I still find it too subtle to my liking that we need to move the declaration like this (and duplicate it twice) for internal behavior of the compiler (where it can't figure out itself by the use cases if it can optimize the stack).

Under which configurations this warning was observed wasn't specified, but I'd bet it's one of the sanitizers splitting this in two in order to tell which branch may have overflowed the buffer.

Signature · 2 lines
Thanks,
~Nick Desaulniers
#4 lore raw
Quoted 22 lines · Nick Desaulniers' via Clang Built Linux
Quoted 20 lines · Steven Rostedt
Quoted 5 lines · On Fri, 14 May 2021 16:04:25 +0200 Arnd Bergmann <[email protected]> wrote:

From: Arnd Bergmann <[email protected]>

In some configurations, clang produces a warning about an overly large amount of stack space used in hist_trigger_print_key():

Quoted 8 lines

kernel/trace/trace_events_hist.c:4594:13: error: stack frame size of 1248 bytes in function 'hist_trigger_print_key' [-Werror,-Wframe-larger-than=] static void hist_trigger_print_key(struct seq_file *m,

Moving the 'str' variable into a more local scope in the two places where it gets used actually reduces the the used stack space here and gets it below the warning limit, because the compiler can now assume that it is safe to use the same stack slot that it has for the stack of any inline function.

Thanks Arnd for the nice explanation of the rationale for this change.

Quoted 8 lines
Quoted 4 lines

But I still find it too subtle to my liking that we need to move the declaration like this (and duplicate it twice) for internal behavior of the compiler (where it can't figure out itself by the use cases if it can optimize the stack).

Under which configurations this warning was observed wasn't specified, but I'd bet it's one of the sanitizers splitting this in two in order to tell which branch may have overflowed the buffer.

Right, I did not manage to find the exact conditions that trigger it. I did *not* see it with KASAN enabled and I saw it with UBSAN both on or off.

Arnd