Skip to contentSkip to conversation

powerpc/kdump: fix KASAN sanitization flag for core_$(BITS).o

PATCH Superseded Sourabh Jain 3 participants 9 messages Apr 3 – Apr 7 PowerPC

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 powerpc/kdump: fix KASAN sanitization flag for core_$(BITS).o

KASAN instrumentation is intended to be disabled for the kexec core code, but the existing Makefile entry misses the object suffix. As a result, the flag is not applied correctly to core_$(BITS).o.

So when KASAN is enabled, kexec_copy_flush and copy_segments in kexec/core_64.c are instrumented, which can result in accesses to shadow memory via normal address translation paths. Since these run with the MMU disabled, such accesses may trigger page faults (bad_page_fault) that cannot be handled in the kdump path, ultimately causing a hang and preventing the kdump kernel from booting. The same is true for kexec as well, since the same functions are used there.

Update the entry to include the “.o” suffix so that KASAN instrumentation is properly disabled for this object file.

Fixes 2ab2d5794f14 ("powerpc/kasan: Disable address sanitization in kexec paths")
Cc Aboorva Devarajan <[email protected]>
Cc Aditya Gupta <[email protected]>
Cc Daniel Axtens <[email protected]>
Cc Hari Bathini <[email protected]>
Cc Madhavan Srinivasan <[email protected]>
Cc Michael Ellerman <[email protected]>
Cc Shivang Upadhyay <[email protected]>

---
Changelog:

v2:
- Add Reviewed-by, Acked-by and Tested-by tags
- No functional changes

v1:
https://lore.kernel.org/all/[email protected]/

---
 arch/powerpc/kexec/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/kexec/Makefile +1 -1
index 470eb0453e17..ec7a0eed75dc 100644
@@ -16,4 +16,4 @@ GCOV_PROFILE_core_$(BITS).o := n
 KCOV_INSTRUMENT_core_$(BITS).o := n
 UBSAN_SANITIZE_core_$(BITS).o := n
 KASAN_SANITIZE_core.o := n
-KASAN_SANITIZE_core_$(BITS) := n
+KASAN_SANITIZE_core_$(BITS).o := n
Signature · 1 line
2.52.0
#1 lore raw

PATCH v2 2/2 powerpc/vmx: avoid KASAN instrumentation in enter_vmx_ops() for kexec

The kexec sequence invokes enter_vmx_ops() via copy_page() with the MMU disabled. In this context, code must not rely on normal virtual address translations or trigger page faults.

With KASAN enabled, functions get instrumented and may access shadow memory using regular address translation. When executed with the MMU off, this can lead to page faults (bad_page_fault) from which the kernel cannot recover in the kexec path, resulting in a hang.

The kexec path sets preempt_count to HARDIRQ_OFFSET before entering the MMU-off copy sequence.

current_thread_info()->preempt_count = HARDIRQ_OFFSET
kexec_sequence(..., copy_with_mmu_off = 1)

    -> kexec_copy_flush(image)
         copy_segments()
           -> copy_page(dest, addr)
	         bl enter_vmx_ops()
                   if (in_interrupt())
                     return 0
	         beq .Lnonvmx_copy

Since kexec sets preempt_count to HARDIRQ_OFFSET, in_interrupt() evaluates to true and enter_vmx_ops() returns early.

As in_interrupt() (and preempt_count()) are always inlined, mark enter_vmx_ops() with __no_sanitize_address to avoid KASAN instrumentation and shadow memory access with MMU disabled, helping kexec boot fine with KASAN enabled.

Cc Aditya Gupta <[email protected]>
Cc Daniel Axtens <[email protected]>
Cc Hari Bathini <[email protected]>
Cc Madhavan Srinivasan <[email protected]>
Cc Mahesh Salgaonkar <[email protected]>
Cc Michael Ellerman <[email protected]>
Cc Ritesh Harjani (IBM) <[email protected]>
Cc Shivang Upadhyay <[email protected]>
Cc Venkat Rao Bagalkote <[email protected]>

---
Changelog:

v2:
- Remove __no_sanitize_address from exit_vmx_ops
- Add a comment explaining that marking only enter_vmx_ops with __no_sanitize_address is sufficient for kexec to function properly with KASAN enabled

v1:
https://lore.kernel.org/all/[email protected]/

---
 arch/powerpc/lib/vmx-helper.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
arch/powerpc/lib/vmx-helper.c +8 -1
index 554b248002b4..57e897b60db8 100644
@@ -52,7 +52,14 @@ int exit_vmx_usercopy(void)
 }
 EXPORT_SYMBOL(exit_vmx_usercopy);
 
-int enter_vmx_ops(void)
+/*
+ * Can be called from kexec copy_page() path with MMU off. The kexec
+ * code sets preempt_count to HARDIRQ_OFFSET so we return early here.
+ * Since in_interrupt() is always inline, __no_sanitize_address on this
+ * function is sufficient to avoid KASAN shadow memory accesses in real
+ * mode.
+ */
+int __no_sanitize_address enter_vmx_ops(void)
 {
 	if (in_interrupt())
 		return 0;
Signature · 1 line
2.52.0
reviewer Reviewed-by on v2 2/2
#2 lore raw

Re: [PATCH v2 2/2] powerpc/vmx: avoid KASAN instrumentation in enter_vmx_ops() for kexec

Quoted 22 lines · Sourabh Jain

The kexec sequence invokes enter_vmx_ops() via copy_page() with the MMU disabled. In this context, code must not rely on normal virtual address translations or trigger page faults.

With KASAN enabled, functions get instrumented and may access shadow memory using regular address translation. When executed with the MMU off, this can lead to page faults (bad_page_fault) from which the kernel cannot recover in the kexec path, resulting in a hang.

The kexec path sets preempt_count to HARDIRQ_OFFSET before entering the MMU-off copy sequence.

current_thread_info()->preempt_count = HARDIRQ_OFFSET
kexec_sequence(..., copy_with_mmu_off = 1)

    -> kexec_copy_flush(image)
         copy_segments()
           -> copy_page(dest, addr)
	         bl enter_vmx_ops()
                   if (in_interrupt())
                     return 0
	         beq .Lnonvmx_copy
Quoted 8 lines

Since kexec sets preempt_count to HARDIRQ_OFFSET, in_interrupt() evaluates to true and enter_vmx_ops() returns early.

As in_interrupt() (and preempt_count()) are always inlined, mark enter_vmx_ops() with __no_sanitize_address to avoid KASAN instrumentation and shadow memory access with MMU disabled, helping kexec boot fine with KASAN enabled.

Nice! LGTM. Please feel free to add:

Quoted 48 lines
Cc Aditya Gupta <[email protected]>
Cc Daniel Axtens <[email protected]>
Cc Hari Bathini <[email protected]>
Cc Madhavan Srinivasan <[email protected]>
Cc Mahesh Salgaonkar <[email protected]>
Cc Michael Ellerman <[email protected]>
Cc Ritesh Harjani (IBM) <[email protected]>
Cc Shivang Upadhyay <[email protected]>
Cc Venkat Rao Bagalkote <[email protected]>

---
Changelog:

v2:
- Remove __no_sanitize_address from exit_vmx_ops
- Add a comment explaining that marking only enter_vmx_ops with __no_sanitize_address is sufficient for kexec to function properly with KASAN enabled

v1:
https://lore.kernel.org/all/[email protected]/

---
 arch/powerpc/lib/vmx-helper.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
arch/powerpc/lib/vmx-helper.c +8 -1
index 554b248002b4..57e897b60db8 100644
@@ -52,7 +52,14 @@ int exit_vmx_usercopy(void)
 }
 EXPORT_SYMBOL(exit_vmx_usercopy);
 
-int enter_vmx_ops(void)
+/*
+ * Can be called from kexec copy_page() path with MMU off. The kexec
+ * code sets preempt_count to HARDIRQ_OFFSET so we return early here.
+ * Since in_interrupt() is always inline, __no_sanitize_address on this
+ * function is sufficient to avoid KASAN shadow memory accesses in real
+ * mode.
+ */
+int __no_sanitize_address enter_vmx_ops(void)
 {
 	if (in_interrupt())
 		return 0;
Signature · 1 line
2.52.0
#3 lore raw

Re: [PATCH v2 2/2] powerpc/vmx: avoid KASAN instrumentation in enter_vmx_ops() for kexec

Quoted 27 lines · Ritesh Harjani (IBM)
Quoted 25 lines · Sourabh Jain

The kexec sequence invokes enter_vmx_ops() via copy_page() with the MMU disabled. In this context, code must not rely on normal virtual address translations or trigger page faults.

With KASAN enabled, functions get instrumented and may access shadow memory using regular address translation. When executed with the MMU off, this can lead to page faults (bad_page_fault) from which the kernel cannot recover in the kexec path, resulting in a hang.

The kexec path sets preempt_count to HARDIRQ_OFFSET before entering the MMU-off copy sequence.

current_thread_info()->preempt_count = HARDIRQ_OFFSET
kexec_sequence(..., copy_with_mmu_off = 1)

     -> kexec_copy_flush(image)
          copy_segments()
            -> copy_page(dest, addr)
	         bl enter_vmx_ops()
                    if (in_interrupt())
                      return 0
	         beq .Lnonvmx_copy

Since kexec sets preempt_count to HARDIRQ_OFFSET, in_interrupt() evaluates to true and enter_vmx_ops() returns early.

Quoted 7 lines
Quoted 5 lines

As in_interrupt() (and preempt_count()) are always inlined, mark enter_vmx_ops() with __no_sanitize_address to avoid KASAN instrumentation and shadow memory access with MMU disabled, helping kexec boot fine with KASAN enabled.

Nice! LGTM. Please feel free to add:

Thanks, Ritesh!

Will add your Reviewed-by tag in the next version.

- Sourabh Jain

Reviewed-by Tested-by on v2 2/2
#4 lore raw

Re: [PATCH v2 2/2] powerpc/vmx: avoid KASAN instrumentation in enter_vmx_ops() for kexec

In reply to Sourabh Jain
Quoted 73 lines · Sourabh Jain

The kexec sequence invokes enter_vmx_ops() via copy_page() with the MMU disabled. In this context, code must not rely on normal virtual address translations or trigger page faults.

With KASAN enabled, functions get instrumented and may access shadow memory using regular address translation. When executed with the MMU off, this can lead to page faults (bad_page_fault) from which the kernel cannot recover in the kexec path, resulting in a hang.

The kexec path sets preempt_count to HARDIRQ_OFFSET before entering the MMU-off copy sequence.

current_thread_info()->preempt_count = HARDIRQ_OFFSET kexec_sequence(..., copy_with_mmu_off = 1) -> kexec_copy_flush(image) copy_segments()
           -> copy_page(dest, addr)
         bl enter_vmx_ops()
                   if (in_interrupt())
                     return 0
         beq .Lnonvmx_copy

Since kexec sets preempt_count to HARDIRQ_OFFSET, in_interrupt() evaluates to true and enter_vmx_ops() returns early.

As in_interrupt() (and preempt_count()) are always inlined, mark enter_vmx_ops() with __no_sanitize_address to avoid KASAN instrumentation and shadow memory access with MMU disabled, helping kexec boot fine with KASAN enabled.

Cc Aditya Gupta <[email protected]>
Cc Daniel Axtens <[email protected]>
Cc Hari Bathini <[email protected]>
Cc Madhavan Srinivasan <[email protected]>
Cc Mahesh Salgaonkar <[email protected]>
Cc Michael Ellerman <[email protected]>
Cc Ritesh Harjani (IBM) <[email protected]>
Cc Shivang Upadhyay <[email protected]>
Cc Venkat Rao Bagalkote <[email protected]>

---
Changelog:

v2:
- Remove __no_sanitize_address from exit_vmx_ops
- Add a comment explaining that marking only enter_vmx_ops with __no_sanitize_address is sufficient for kexec to function properly with KASAN enabled

v1:
https://lore.kernel.org/all/[email protected]/
---
 arch/powerpc/lib/vmx-helper.c | 9 ++++++++-

 1 file changed, 8 insertions(+), 1 deletion(-)
arch/powerpc/lib/vmx-helper.c +0 -0
index 554b248002b4..57e897b60db8 100644
@@ -52,7 +52,14 @@ int exit_vmx_usercopy(void)
  }
  EXPORT_SYMBOL(exit_vmx_usercopy);

arch/powerpc/lib/vmx-helper.c +8 -1
-int enter_vmx_ops(void)
+/*
+ * Can be called from kexec copy_page() path with MMU off. The kexec
+ * code sets preempt_count to HARDIRQ_OFFSET so we return early here.
+ * Since in_interrupt() is always inline, __no_sanitize_address on this
+ * function is sufficient to avoid KASAN shadow memory accesses in real
+ * mode.
+ */
+int __no_sanitize_address enter_vmx_ops(void)
Quoted 3 lines

 {
  if (in_interrupt())
  return 0;

Without these patches, when KASAN is enabled, I observe a hang during kexec boot on pseries (PowerVM):

[ 3459.012617][ T4209] kexec_core: Starting new kernel
[ 3459.012814][ T4209] kexec: waiting for cpu 1 (physical 1) to enter 2 state
[ 3459.016236][ T4209] kexec: waiting for cpu 11 (physical 11) to enter 2 state
[ 3459.016287][ T4209] kexec: waiting for cpu 12 (physical 12) to enter 2 state
[ 3459.016380][ T4209] kexec: waiting for cpu 13 (physical 13) to enter 2 state
[ 3459.016418][ T4209] kexec: waiting for cpu 14 (physical 14) to enter 2 state
[ 3459.016444][ T4209] kexec: waiting for cpu 15 (physical 15) to enter 2 state
[ 3459.016462][ T4209] kexec: waiting for cpu 18 (physical 18) to enter 2 state
[ 3459.271929][ T4209] kexec: Starting switchover sequence.

[system hangs here and no further progress]

==============

With both the patches applied, kexec completes successfully with KASAN enabled.

Thanks,
Aboorva

#5 lore raw

Re: [PATCH v2 2/2] powerpc/vmx: avoid KASAN instrumentation in enter_vmx_ops() for kexec

Quoted 90 lines · Aboorva Devarajan
Quoted 66 lines · Sourabh Jain

The kexec sequence invokes enter_vmx_ops() via copy_page() with the MMU disabled. In this context, code must not rely on normal virtual address translations or trigger page faults.

With KASAN enabled, functions get instrumented and may access shadow memory using regular address translation. When executed with the MMU off, this can lead to page faults (bad_page_fault) from which the kernel cannot recover in the kexec path, resulting in a hang.

The kexec path sets preempt_count to HARDIRQ_OFFSET before entering the MMU-off copy sequence.

current_thread_info()->preempt_count = HARDIRQ_OFFSET
  kexec_sequence(..., copy_with_mmu_off = 1) -> kexec_copy_flush(image) copy_segments()
           -> copy_page(dest, addr)
         bl enter_vmx_ops()
                   if (in_interrupt()) return 0
         beq .Lnonvmx_copy

Since kexec sets preempt_count to HARDIRQ_OFFSET, in_interrupt() evaluates to true and enter_vmx_ops() returns early.

As in_interrupt() (and preempt_count()) are always inlined, mark enter_vmx_ops() with __no_sanitize_address to avoid KASAN instrumentation and shadow memory access with MMU disabled, helping kexec boot fine with KASAN enabled.

Cc Aditya Gupta <[email protected]>
Cc Daniel Axtens <[email protected]>
Cc Hari Bathini <[email protected]>
Cc Madhavan Srinivasan <[email protected]>
Cc Mahesh Salgaonkar <[email protected]>
Cc Michael Ellerman <[email protected]>
Cc Ritesh Harjani (IBM) <[email protected]>
Cc Shivang Upadhyay <[email protected]>
Cc Venkat Rao Bagalkote <[email protected]>

---
Changelog:

v2:
- Remove __no_sanitize_address from exit_vmx_ops
- Add a comment explaining that marking only enter_vmx_ops with __no_sanitize_address is sufficient for kexec to function properly with KASAN enabled

v1:
https://lore.kernel.org/all/[email protected]/

---
  arch/powerpc/lib/vmx-helper.c | 9 ++++++++-
  1 file changed, 8 insertions(+), 1 deletion(-)
arch/powerpc/lib/vmx-helper.c +1 -1
index 554b248002b4..57e897b60db8 100644
@@ -52,7 +52,14 @@ int exit_vmx_usercopy(void)
  }
  EXPORT_SYMBOL(exit_vmx_usercopy);
 
-int enter_vmx_ops(void)
+/*

arch/powerpc/lib/vmx-helper.c int exit_vmx_usercopy(void) · line 65

Quoted 10 lines
arch/powerpc/lib/vmx-helper.c +7 -0
+ * Can be called from kexec copy_page() path with MMU off. The kexec
+ * code sets preempt_count to HARDIRQ_OFFSET so we return early here.
+ * Since in_interrupt() is always inline, __no_sanitize_address on this
+ * function is sufficient to avoid KASAN shadow memory accesses in real
+ * mode.
+ */
+int __no_sanitize_address enter_vmx_ops(void)
  {
  	if (in_interrupt())
  		return 0;

Without these patches, when KASAN is enabled, I observe a hang during kexec boot on pseries (PowerVM):

[ 3459.012617][ T4209] kexec_core: Starting new kernel
[ 3459.012814][ T4209] kexec: waiting for cpu 1 (physical 1) to enter 2 state
[ 3459.016236][ T4209] kexec: waiting for cpu 11 (physical 11) to enter 2 state
[ 3459.016287][ T4209] kexec: waiting for cpu 12 (physical 12) to enter 2 state
[ 3459.016380][ T4209] kexec: waiting for cpu 13 (physical 13) to enter 2 state
[ 3459.016418][ T4209] kexec: waiting for cpu 14 (physical 14) to enter 2 state
[ 3459.016444][ T4209] kexec: waiting for cpu 15 (physical 15) to enter 2 state
[ 3459.016462][ T4209] kexec: waiting for cpu 18 (physical 18) to enter 2 state
[ 3459.271929][ T4209] kexec: Starting switchover sequence.
Quoted 8 lines

[system hangs here and no further progress]

==============

With both the patches applied, kexec completes successfully with KASAN enabled.

Thanks for testing and the review.

- Sourabh Jain

#6 lore raw
In reply to Sourabh Jain
Quoted 15 lines · Sourabh Jain

KASAN instrumentation is intended to be disabled for the kexec core code, but the existing Makefile entry misses the object suffix. As a result, the flag is not applied correctly to core_$(BITS).o.

So when KASAN is enabled, kexec_copy_flush and copy_segments in kexec/core_64.c are instrumented, which can result in accesses to shadow memory via normal address translation paths. Since these run with the MMU disabled, such accesses may trigger page faults (bad_page_fault) that cannot be handled in the kdump path, ultimately causing a hang and preventing the kdump kernel from booting. The same is true for kexec as well, since the same functions are used there.

Update the entry to include the “.o” suffix so that KASAN instrumentation is properly disabled for this object file.

Quoted 10 lines
Fixes 2ab2d5794f14 ("powerpc/kasan: Disable address sanitization in kexec paths")
Cc Aboorva Devarajan <[email protected]>
Cc Aditya Gupta <[email protected]>
Cc Daniel Axtens <[email protected]>
Cc Hari Bathini <[email protected]>
Cc Madhavan Srinivasan <[email protected]>
Cc Michael Ellerman <[email protected]>
Cc Shivang Upadhyay <[email protected]>

I guess you missed adding:
Cc: [email protected]

Quoted 29 lines

---
Changelog:

v2:
- Add Reviewed-by, Acked-by and Tested-by tags
- No functional changes

v1:
https://lore.kernel.org/all/[email protected]/

---
 arch/powerpc/kexec/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/kexec/Makefile +1 -1
index 470eb0453e17..ec7a0eed75dc 100644
@@ -16,4 +16,4 @@ GCOV_PROFILE_core_$(BITS).o := n
 KCOV_INSTRUMENT_core_$(BITS).o := n
 UBSAN_SANITIZE_core_$(BITS).o := n
 KASAN_SANITIZE_core.o := n
-KASAN_SANITIZE_core_$(BITS) := n
+KASAN_SANITIZE_core_$(BITS).o := n
Signature · 1 line
2.52.0
#7 lore raw
Quoted 27 lines · Ritesh Harjani (IBM)
Quoted 15 lines · Sourabh Jain

KASAN instrumentation is intended to be disabled for the kexec core code, but the existing Makefile entry misses the object suffix. As a result, the flag is not applied correctly to core_$(BITS).o.

So when KASAN is enabled, kexec_copy_flush and copy_segments in kexec/core_64.c are instrumented, which can result in accesses to shadow memory via normal address translation paths. Since these run with the MMU disabled, such accesses may trigger page faults (bad_page_fault) that cannot be handled in the kdump path, ultimately causing a hang and preventing the kdump kernel from booting. The same is true for kexec as well, since the same functions are used there.

Update the entry to include the “.o” suffix so that KASAN instrumentation is properly disabled for this object file.

Quoted 10 lines
Fixes 2ab2d5794f14 ("powerpc/kasan: Disable address sanitization in kexec paths")
Cc Aboorva Devarajan <[email protected]>
Cc Aditya Gupta <[email protected]>
Cc Daniel Axtens <[email protected]>
Cc Hari Bathini <[email protected]>
Cc Madhavan Srinivasan <[email protected]>
Cc Michael Ellerman <[email protected]>
Cc Shivang Upadhyay <[email protected]>
Quoted 2 lines

I guess you missed adding:
Cc: [email protected]

You’re right, I missed it.
I’ll include it in the next revision.

Thanks,
Sourabh Jain

Reviewed-by Tested-by on v2 1/2
#8 lore raw
In reply to Sourabh Jain
Quoted 55 lines · Sourabh Jain

KASAN instrumentation is intended to be disabled for the kexec core code, but the existing Makefile entry misses the object suffix. As a result, the flag is not applied correctly to core_$(BITS).o.

So when KASAN is enabled, kexec_copy_flush and copy_segments in kexec/core_64.c are instrumented, which can result in accesses to shadow memory via normal address translation paths. Since these run with the MMU disabled, such accesses may trigger page faults (bad_page_fault) that cannot be handled in the kdump path, ultimately causing a hang and preventing the kdump kernel from booting. The same is true for kexec as well, since the same functions are used there.

Update the entry to include the “.o” suffix so that KASAN instrumentation is properly disabled for this object file.

Fixes 2ab2d5794f14 ("powerpc/kasan: Disable address sanitization in

kexec paths")

Closes:
https://lore.kernel.org/all/[email protected]/

Cc Aboorva Devarajan <[email protected]>
Cc Aditya Gupta <[email protected]>
Cc Daniel Axtens <[email protected]>
Cc Hari Bathini <[email protected]>
Cc Madhavan Srinivasan <[email protected]>
Cc Michael Ellerman <[email protected]>
Cc Shivang Upadhyay <[email protected]>

---
Changelog:

v2:
- Add Reviewed-by, Acked-by and Tested-by tags
- No functional changes

 v1:

https://lore.kernel.org/all/20260321053121.614022-1-sourabhjain@linux.
ibm.com/
---
 arch/powerpc/kexec/Makefile | 2 +-

 1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/kexec/Makefile +0 -0
index 470eb0453e17..ec7a0eed75dc 100644
@@ -16,4 +16,4 @@ GCOV_PROFILE_core_$(BITS).o := n

 KCOV_INSTRUMENT_core_$(BITS).o := n
 UBSAN_SANITIZE_core_$(BITS).o := n
 KASAN_SANITIZE_core.o := n

arch/powerpc/kexec/Makefile GCOV_PROFILE_core_$(BITS).o := n · line 19

Quoted 2 lines
arch/powerpc/kexec/Makefile +1 -1
-KASAN_SANITIZE_core_$(BITS) := n
+KASAN_SANITIZE_core_$(BITS).o := n

With both the patches applied, kexec completes successfully with KASAN enabled.

Thanks,
Aboorva