Skip to contentSkip to conversation

powerpc/ftrace: Don't restore r13 during ftrace_regs_caller

PATCH Superseded Shrikanth Hegde 3 participants 4 messages Sep 18 – Sep 21

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Shrikanth Hegde

author reviewer
#0 lore raw

PATCH 1/1 powerpc/ftrace: Don't restore r13 during ftrace_regs_caller

Michal reported a stack-protector failure and subsequent panic when running kernel builds. This was observed with full/lazy preemption. Initially it was suspected as KVM, but later turned out to be due to a bcc tool running in parallel.

Issue was recreated using a bcc tool.
For example, running below in parallel leads to crash. ./funccount sched* -d 100 and make -j 64

The same crash was observed when running kprobe for schedule() function, while simpler function tracer for schedule() didn't cause the crash. This helped to narrow it down to ftrace backed kprobes area.

The crash occurs as follows:

ftrace_regs_caller entry on CPU A

    |
    +-> save r13 = CPU A PACA into pt_regs
    |
    +-> call kprobe_ftrace_handler()
            |
            +-> ftrace_test_recursion_unlock()
                    |
                    +-> preempt_enable
                    +-> task can schedule and migrate to CPU B
                    +-> task resumes with live r13 = CPU B PACA
    |
    +-> REST_GPRS(2, 31)
            |
            +-> restore saved r13 = CPU A PACA
    |
    |-> The task then continues running on CPU B with r13 pointing
    |   to CPU A's PACA.

The stack-protector canary is accessed through the PACA. After the task migrates, CPU A may run a different task and update its PACA with that task's canary. Restoring the saved r13 then causes the migrated task's saved stack canary to be compared against the canary in CPU A's PACA, resulting in a stack-protector failure.

Similarly, current is resolved through the PACA. With a stale r13, preempt_count() can access the state of the task referenced by CPU A's PACA instead of the task running on CPU B. This results in corrupted preempt-count warnings and scheduling-while-atomic failures.

This path for example is called when using kprobes and parallel kernel builds can cause preemptions during ftrace_test_recursion_unlock.

Do not restore r13 from the saved register frame. If the task did not migrate, the live r13 already has the saved value. If it migrated, the live r13 contains the correct PACA pointer for the CPU on which the task resumed.

Fixes 153086644fd1 ("powerpc/ftrace: Add support for -mprofile-kernel ftrace ABI")

---
PS:
Fixes is the initial commit that introduced this restore regs almost 10 years ago, all commit afterwords are code refactors changing the code layout. Also backporting all the way maybe tricky. Backport can easily happen till aebd1fb45c622.

 arch/powerpc/kernel/trace/ftrace_entry.S | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/powerpc/kernel/trace/ftrace_entry.S +3 -1
index 6599fe3c6234..54c8727b48cd 100644
@@ -220,7 +220,9 @@
 
 	/* Restore gprs */
 	.if \allregs == 1
-	REST_GPRS(2, 31, r1)
+	REST_GPRS(2, 12, r1)
+	/* Do not restore a stale PACA pointer if the task migrated */
+	REST_GPRS(14, 31, r1)
 	.else
 	REST_GPRS(3, 10, r1)
 #if defined(CONFIG_LIVEPATCH_64) || defined(CONFIG_PPC_FTRACE_OUT_OF_LINE)
Signature · 1 line
2.52.0
reviewer Reviewed-by on v1 1/1
#1 lore raw

Le 18/09/2026 à 17:08, Shrikanth Hegde a écrit :

Quoted 53 lines

Michal reported a stack-protector failure and subsequent panic when running kernel builds. This was observed with full/lazy preemption. Initially it was suspected as KVM, but later turned out to be due to a bcc tool running in parallel.

Issue was recreated using a bcc tool.
For example, running below in parallel leads to crash. ./funccount sched* -d 100 and make -j 64

The same crash was observed when running kprobe for schedule() function, while simpler function tracer for schedule() didn't cause the crash. This helped to narrow it down to ftrace backed kprobes area.

The crash occurs as follows:

ftrace_regs_caller entry on CPU A

     |
     +-> save r13 = CPU A PACA into pt_regs
     |
     +-> call kprobe_ftrace_handler()
             |
             +-> ftrace_test_recursion_unlock()
                     |
                     +-> preempt_enable
                     +-> task can schedule and migrate to CPU B
                     +-> task resumes with live r13 = CPU B PACA
     |
     +-> REST_GPRS(2, 31)
             |
             +-> restore saved r13 = CPU A PACA
     |
     |-> The task then continues running on CPU B with r13 pointing
     |   to CPU A's PACA.

The stack-protector canary is accessed through the PACA. After the task migrates, CPU A may run a different task and update its PACA with that task's canary. Restoring the saved r13 then causes the migrated task's saved stack canary to be compared against the canary in CPU A's PACA, resulting in a stack-protector failure.

Similarly, current is resolved through the PACA. With a stale r13, preempt_count() can access the state of the task referenced by CPU A's PACA instead of the task running on CPU B. This results in corrupted preempt-count warnings and scheduling-while-atomic failures.

This path for example is called when using kprobes and parallel kernel builds can cause preemptions during ftrace_test_recursion_unlock.

Do not restore r13 from the saved register frame. If the task did not migrate, the live r13 already has the saved value. If it migrated, the live r13 contains the correct PACA pointer for the CPU on which the task resumed.

Quoted 4 lines
Quoted 6 lines

---
PS:
Fixes is the initial commit that introduced this restore regs almost 10 years ago, all commit afterwords are code refactors changing the code layout. Also backporting all the way maybe tricky. Backport can easily happen till aebd1fb45c622.

Probably not a big issue. That commit is in 5.17. Stable branches 5.10 and 5.15 will be EOL by Decembre 2026.

Quoted 19 lines
  arch/powerpc/kernel/trace/ftrace_entry.S | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
arch/powerpc/kernel/trace/ftrace_entry.S +3 -1
index 6599fe3c6234..54c8727b48cd 100644
@@ -220,7 +220,9 @@
 
  	/* Restore gprs */
  	.if \allregs == 1
-	REST_GPRS(2, 31, r1)
+	REST_GPRS(2, 12, r1)
+	/* Do not restore a stale PACA pointer if the task migrated */
+	REST_GPRS(14, 31, r1)
  	.else
  	REST_GPRS(3, 10, r1)
  #if defined(CONFIG_LIVEPATCH_64) || defined(CONFIG_PPC_FTRACE_OUT_OF_LINE)
Reviewed-by on v1 1/1
#2 lore raw
In reply to Shrikanth Hegde
Quoted 45 lines · Shrikanth Hegde

Michal reported a stack-protector failure and subsequent panic when running kernel builds. This was observed with full/lazy preemption. Initially it was suspected as KVM, but later turned out to be due to a bcc tool running in parallel.

Issue was recreated using a bcc tool.
For example, running below in parallel leads to crash. ./funccount sched* -d 100 and make -j 64

The same crash was observed when running kprobe for schedule() function, while simpler function tracer for schedule() didn't cause the crash. This helped to narrow it down to ftrace backed kprobes area.

The crash occurs as follows:

ftrace_regs_caller entry on CPU A

     |
     +-> save r13 = CPU A PACA into pt_regs
     |
     +-> call kprobe_ftrace_handler()
             |
             +-> ftrace_test_recursion_unlock()
                     |
                     +-> preempt_enable
                     +-> task can schedule and migrate to CPU B
                     +-> task resumes with live r13 = CPU B PACA
     |
     +-> REST_GPRS(2, 31)
             |
             +-> restore saved r13 = CPU A PACA
     |
     |-> The task then continues running on CPU B with r13 pointing
     |   to CPU A's PACA.

The stack-protector canary is accessed through the PACA. After the task migrates, CPU A may run a different task and update its PACA with that task's canary. Restoring the saved r13 then causes the migrated task's saved stack canary to be compared against the canary in CPU A's PACA, resulting in a stack-protector failure.

Similarly, current is resolved through the PACA. With a stale r13, preempt_count() can access the state of the task referenced by CPU A's PACA instead of the task running on CPU B. This results in corrupted preempt-count warnings and scheduling-while-atomic failures.

Quoted 8 lines

This path for example is called when using kprobes and parallel kernel builds can cause preemptions during ftrace_test_recursion_unlock.

Do not restore r13 from the saved register frame. If the task did not migrate, the live r13 already has the saved value. If it migrated, the live r13 contains the correct PACA pointer for the CPU on which the task resumed.

Looks good to me except for a minor nit below.

Quoted 19 lines
Fixes 153086644fd1 ("powerpc/ftrace: Add support for -mprofile-kernel ftrace ABI")

---
PS:
Fixes is the initial commit that introduced this restore regs almost 10 years ago, all commit afterwords are code refactors changing the code layout. Also backporting all the way maybe tricky. Backport can easily happen till aebd1fb45c622.

  arch/powerpc/kernel/trace/ftrace_entry.S | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
arch/powerpc/kernel/trace/ftrace_entry.S +0 -0
index 6599fe3c6234..54c8727b48cd 100644

arch/powerpc/kernel/trace/ftrace_entry.S line 226

Quoted 8 lines
arch/powerpc/kernel/trace/ftrace_entry.S +3 -1
@@ -220,7 +220,9 @@
 
  	/* Restore gprs */
  	.if \allregs == 1
 
-	REST_GPRS(2, 31, r1)
+	REST_GPRS(2, 12, r1)
+	/* Do not restore a stale PACA pointer if the task migrated */
+	REST_GPRS(14, 31, r1)

Given that r13 is not paca on ppc32, shouldn't the above change only apply to PPC64?

Quoted 3 lines
arch/powerpc/kernel/trace/ftrace_entry.S +0 -0
  	.else
  	REST_GPRS(3, 10, r1)
  #if defined(CONFIG_LIVEPATCH_64) || defined(CONFIG_PPC_FTRACE_OUT_OF_LINE)

- Hari

author reviewer
#3 lore raw

Hi Hari,

Quoted 83 lines · Hari Bathini
Quoted 45 lines · Shrikanth Hegde

Michal reported a stack-protector failure and subsequent panic when running kernel builds. This was observed with full/lazy preemption. Initially it was suspected as KVM, but later turned out to be due to a bcc tool running in parallel.

Issue was recreated using a bcc tool.
For example, running below in parallel leads to crash. ./funccount sched* -d 100 and make -j 64

The same crash was observed when running kprobe for schedule() function, while simpler function tracer for schedule() didn't cause the crash. This helped to narrow it down to ftrace backed kprobes area.

The crash occurs as follows:

ftrace_regs_caller entry on CPU A
     |

     +-> save r13 = CPU A PACA into pt_regs
     |
     +-> call kprobe_ftrace_handler()
             |
             +-> ftrace_test_recursion_unlock()
                     |
                     +-> preempt_enable
                     +-> task can schedule and migrate to CPU B
                     +-> task resumes with live r13 = CPU B PACA
     |
     +-> REST_GPRS(2, 31)
             |
             +-> restore saved r13 = CPU A PACA
     |
     |-> The task then continues running on CPU B with r13 pointing
     |   to CPU A's PACA.

The stack-protector canary is accessed through the PACA. After the task migrates, CPU A may run a different task and update its PACA with that task's canary. Restoring the saved r13 then causes the migrated task's saved stack canary to be compared against the canary in CPU A's PACA, resulting in a stack-protector failure.

Similarly, current is resolved through the PACA. With a stale r13, preempt_count() can access the state of the task referenced by CPU A's PACA instead of the task running on CPU B. This results in corrupted preempt-count warnings and scheduling-while-atomic failures.

Quoted 8 lines

This path for example is called when using kprobes and parallel kernel builds can cause preemptions during ftrace_test_recursion_unlock.

Do not restore r13 from the saved register frame. If the task did not migrate, the live r13 already has the saved value. If it migrated, the live r13 contains the correct PACA pointer for the CPU on which the task resumed.

Looks good to me except for a minor nit below.

Quoted 21 lines
Fixes 153086644fd1 ("powerpc/ftrace: Add support for -mprofile-kernel ftrace ABI")

---
PS:
Fixes is the initial commit that introduced this restore regs almost 10 years ago, all commit afterwords are code refactors changing the code layout. Also backporting all the way maybe tricky. Backport can easily happen till aebd1fb45c622.

  arch/powerpc/kernel/trace/ftrace_entry.S | 4 +++-

  1 file changed, 3 insertions(+), 1 deletion(-)
arch/powerpc/kernel/trace/ftrace_entry.S +0 -0
index 6599fe3c6234..54c8727b48cd 100644
@@ -220,7 +220,9 @@
       /* Restore gprs */

      .if \allregs == 1

Quoted 7 lines
Quoted 4 lines

-    REST_GPRS(2, 31, r1)
+    REST_GPRS(2, 12, r1)
+    /* Do not restore a stale PACA pointer if the task migrated */ +    REST_GPRS(14, 31, r1)

Given that r13 is not paca on ppc32, shouldn't the above change only apply to PPC64?

Yes. Good catch. Will make the above change only for PPC64.

Thanks for reviewing.

Quoted 6 lines
Quoted 3 lines

      .else
      REST_GPRS(3, 10, r1)
  #if defined(CONFIG_LIVEPATCH_64) || defined(CONFIG_PPC_FTRACE_OUT_OF_LINE)

- Hari