Skip to contentSkip to conversation

Kill off show_stack() NULL-implies-current idiom

PATCH In review Mark Rutland 2 participants 7 messages Sep 26, 2016 – Sep 27, 2016

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

cover Kill off show_stack() NULL-implies-current idiom

Today, show_stack() accepts a NULL task parameter, which it takes to mean the current task. However, as noted in tip/x86/asm commit:

81539169f283329f ("x86/dumpstack: Remove NULL task pointer convention")

... having a NULL task parameter imply current leads to subtle bugs in stack walking code (so far seen on both 86 and arm64), makes callsites harder to read, and is unnecessary as all callers have access to current.

As a step towards removing the problematic NULL-implies-current idiom entirely, these patches ensure that generic code explictly passes current to show_stack(), rather than relying on arch code to handle NULL.

Thanks,
Mark.

Mark Rutland (3):
sched: document that show_stack() should not be passed a NULL task drivers/tty: Explicitly pass current to show_stack
lib: dump_stack: explicitly pass current to show_stack

 drivers/tty/sysrq.c   | 2 +-
 include/linux/sched.h | 3 +++
 lib/dump_stack.c      | 2 +-
 3 files changed, 5 insertions(+), 2 deletions(-)
Signature · 1 line
2.7.4
#1 lore raw

PATCH 1/3 sched: document that show_stack() should not be passed a NULL task

As noted in commit:

81539169f283329f ("x86/dumpstack: Remove NULL task pointer convention")

... having a NULL task parameter imply current leads to subtle bugs in stack walking code (so far seen on both 86 and arm64), makes callsites harder to read, and is unnecessary as all callers have access to current.

As a step towards removing the problematic NULL-implies-current idiom entirely, document that new code should pass current explicitly.

Cc Ingo Molnar <[email protected]>
Cc Josh Poimboeuf <[email protected]>
Cc Peter Zijlstra <[email protected]>
---
 include/linux/sched.h | 3 +++
 1 file changed, 3 insertions(+)
include/linux/sched.h +3 -0
index abb795a..4bc5571 100644
@@ -367,6 +367,9 @@ extern void show_regs(struct pt_regs *);
  * TASK is a pointer to the task whose backtrace we want to see (or NULL for current
  * task), SP is the stack pointer of the first frame that should be shown in the back
  * trace (or NULL if the entire call-chain of the task should be shown).
+ *
+ * Note: passing a NULL task is deprecated, and new code should pass current
+ * explicitly.
  */
 extern void show_stack(struct task_struct *task, unsigned long *sp);
 
Signature · 1 line
2.7.4
#2 lore raw

PATCH 2/3 drivers/tty: Explicitly pass current to show_stack

In reply to Mark Rutland

As noted in commit:

81539169f283329f ("x86/dumpstack: Remove NULL task pointer convention")

... having a NULL task parameter imply current leads to subtle bugs in stack walking code (so far seen on both 86 and arm64), makes callsites harder to read, and is unnecessary as all callers have access to current.

As a step towards removing the problematic NULL-implies-current idiom entirely, have the sysrq code explicitly pass current to show_stack.

Cc Greg Kroah-Hartman <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Jiri Slaby <[email protected]>
Cc Josh Poimboeuf <[email protected]>
---
 drivers/tty/sysrq.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/tty/sysrq.c +1 -1
index 52bbd27..5b1e0ed 100644
@@ -224,7 +224,7 @@ static void showacpu(void *dummy)
 
 	spin_lock_irqsave(&show_lock, flags);
 	pr_info("CPU%d:\n", smp_processor_id());
-	show_stack(NULL, NULL);
+	show_stack(current, NULL);
 	spin_unlock_irqrestore(&show_lock, flags);
 }
 
Signature · 1 line
2.7.4
#3 lore raw

PATCH 3/3 lib: dump_stack: explicitly pass current to show_stack

In reply to Mark Rutland

As noted in commit:

81539169f283329f ("x86/dumpstack: Remove NULL task pointer convention")

... having a NULL task parameter imply current leads to subtle bugs in stack walking code (so far seen on both 86 and arm64), makes callsites harder to read, and is unnecessary as all callers have access to current.

As a step towards removing the problematic NULL-implies-current idiom entirely, have the dump_stack code explicitly pass current to show_stack.

Cc Andrew Morton <[email protected]>
Cc Eric Dumazet <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Josh Poimboeuf <[email protected]>
---
 lib/dump_stack.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/dump_stack.c +1 -1
index c30d07e..7296b63 100644
@@ -12,7 +12,7 @@
 static void __dump_stack(void)
 {
 	dump_stack_print_info(KERN_DEFAULT);
-	show_stack(NULL, NULL);
+	show_stack(current, NULL);
 }
 
 /**
Signature · 1 line
2.7.4
Reviewed-by on the v1 series
#4 lore raw
In reply to Mark Rutland
Quoted 5 lines · Mark Rutland

Today, show_stack() accepts a NULL task parameter, which it takes to mean the current task. However, as noted in tip/x86/asm commit:

81539169f283329f ("x86/dumpstack: Remove NULL task pointer convention")

Quoted 7 lines

... having a NULL task parameter imply current leads to subtle bugs in stack walking code (so far seen on both 86 and arm64), makes callsites harder to read, and is unnecessary as all callers have access to current.

As a step towards removing the problematic NULL-implies-current idiom entirely, these patches ensure that generic code explictly passes current to show_stack(), rather than relying on arch code to handle NULL.

This is a good step, though it would be really nice to fix this tree-wide. Do you have any plans to do so?

Regardless, for the series:

Signature · 1 line
Josh
#5 lore raw
Quoted 10 lines · Josh Poimboeuf
Quoted 9 lines · Mark Rutland

Today, show_stack() accepts a NULL task parameter, which it takes to mean the current task. However, as noted in tip/x86/asm commit:

81539169f283329f ("x86/dumpstack: Remove NULL task pointer convention")

... having a NULL task parameter imply current leads to subtle bugs in stack walking code (so far seen on both 86 and arm64), makes callsites harder to read, and is unnecessary as all callers have access to current.

Quoted 6 lines
Quoted 3 lines

As a step towards removing the problematic NULL-implies-current idiom entirely, these patches ensure that generic code explictly passes current to show_stack(), rather than relying on arch code to handle NULL.

This is a good step, though it would be really nice to fix this tree-wide. Do you have any plans to do so?

FWIW, I completely agree, though I have no plans to do so currently.

I'd meant to Cc linux-arch for this series in order to make people aware, though evidently I did not. I can Cc subsequent arm64 NULL-implies-current cleanup patches there, if that's helpful?

Quoted 3 lines

Regardless, for the series:

Cheers!

Thanks,
Mark.

#6 lore raw
In reply to Josh Poimboeuf
Quoted 10 lines · Josh Poimboeuf
Quoted 9 lines · Mark Rutland

Today, show_stack() accepts a NULL task parameter, which it takes to mean the current task. However, as noted in tip/x86/asm commit:

81539169f283329f ("x86/dumpstack: Remove NULL task pointer convention")

... having a NULL task parameter imply current leads to subtle bugs in stack walking code (so far seen on both 86 and arm64), makes callsites harder to read, and is unnecessary as all callers have access to current.

Quoted 6 lines
Quoted 3 lines

As a step towards removing the problematic NULL-implies-current idiom entirely, these patches ensure that generic code explictly passes current to show_stack(), rather than relying on arch code to handle NULL.

This is a good step, though it would be really nice to fix this tree-wide. Do you have any plans to do so?

I started having a go, and in doing so I've realised that as things stand, these patches are broken, as some architectures don't handle current explicitly.

Please disregard all patches in this series.

Apologies,
Mark.