Skip to contentSkip to conversation

arm: fix using smp_processor_id() in preemptible context

PATCH gaoyongliang 2 participants 2 messages Jun 3, 2019 Stable branch

lore
Share & tools
lore
#0 lore raw

Hi Marc,

Quoted 37 lines · Marc Zyngier
Quoted 36 lines · Yongliang Gao

harden_branch_predictor() call smp_processor_id() in preemptible context, this would cause a bug messages.

The bug messages is as follows:

BUG: using smp_processor_id() in preemptible [00000000] code: syz-executor0/17992
caller is harden_branch_predictor arch/arm/include/asm/system_misc.h:27 [inline]
caller is __do_user_fault+0x34/0x114 arch/arm/mm/fault.c:200
CPU: 1 PID: 17992 Comm: syz-executor0 Tainted: G O 4.4.176 #1
Hardware name: Hisilicon A9
[<c0114ae4>] (unwind_backtrace) from [<c010e6fc>] (show_stack+0x18/0x1c)
[<c010e6fc>] (show_stack) from [<c0379514>] (dump_stack+0xc8/0x118)
[<c0379514>] (dump_stack) from [<c039b5a0>] (check_preemption_disabled+0xf4/0x138)
[<c039b5a0>] (check_preemption_disabled) from [<c011abe4>] (__do_user_fault+0x34/0x114)
[<c011abe4>] (__do_user_fault) from [<c053b0d0>] (do_page_fault+0x3b4/0x3d8)
[<c053b0d0>] (do_page_fault) from [<c01013dc>] (do_DataAbort+0x58/0xf8)
[<c01013dc>] (do_DataAbort) from [<c053a880>] (__dabt_usr+0x40/0x60)
---
 arch/arm/include/asm/system_misc.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/arm/include/asm/system_misc.h +1 -1
index 66f6a3a..4a55cfb 100644
@@ -22,9 +22,10 @@
 static inline void harden_branch_predictor(void)
 {
 	harden_branch_predictor_fn_t fn = per_cpu(harden_branch_predictor_fn,
-						  smp_processor_id());
+						  get_cpu());
 	if (fn)
 		fn();
Quoted 8 lines

arch/arm/include/asm/system_misc.h line 31

Quoted 5 lines
arch/arm/include/asm/system_misc.h +1 -0
+	put_cpu();
 }
 #else
 #define harden_branch_predictor() do { } while (0)

This doesn't look like the right fix. If we're in a preemptible context, then we could invalidate the branch predictor on the wrong CPU.

Sorry, my bad, thanks a lot for the good catch.

Quoted 3 lines

The right fix would be to move the call to a point where we haven't enabled preemption yet.

I took a look at the code, and find out that the caller of harden_branch_predictor(), __do_user_fault(), is called by do_page_fault() and do_bad_area(), those two function's context are both running with preemption enabled, so I didn't find a good place to move the call, could you please give some suggestion for my next step?

Best Regards
Yongliang

#1 lore raw
Quoted 52 lines · gaoyongliang

Hi Marc,

Quoted 37 lines · Marc Zyngier
Quoted 36 lines · Yongliang Gao

harden_branch_predictor() call smp_processor_id() in preemptible context, this would cause a bug messages.

The bug messages is as follows:

BUG: using smp_processor_id() in preemptible [00000000] code: syz-executor0/17992
caller is harden_branch_predictor arch/arm/include/asm/system_misc.h:27 [inline]
caller is __do_user_fault+0x34/0x114 arch/arm/mm/fault.c:200
CPU: 1 PID: 17992 Comm: syz-executor0 Tainted: G O 4.4.176 #1
Hardware name: Hisilicon A9
[<c0114ae4>] (unwind_backtrace) from [<c010e6fc>] (show_stack+0x18/0x1c)
[<c010e6fc>] (show_stack) from [<c0379514>] (dump_stack+0xc8/0x118)
[<c0379514>] (dump_stack) from [<c039b5a0>] (check_preemption_disabled+0xf4/0x138)
[<c039b5a0>] (check_preemption_disabled) from [<c011abe4>] (__do_user_fault+0x34/0x114)
[<c011abe4>] (__do_user_fault) from [<c053b0d0>] (do_page_fault+0x3b4/0x3d8)
[<c053b0d0>] (do_page_fault) from [<c01013dc>] (do_DataAbort+0x58/0xf8)
[<c01013dc>] (do_DataAbort) from [<c053a880>] (__dabt_usr+0x40/0x60)
---
 arch/arm/include/asm/system_misc.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
arch/arm/include/asm/system_misc.h +1 -1
index 66f6a3a..4a55cfb 100644
@@ -22,9 +22,10 @@
 static inline void harden_branch_predictor(void)
 {
 	harden_branch_predictor_fn_t fn = per_cpu(harden_branch_predictor_fn,
-						  smp_processor_id());
+						  get_cpu());
 	if (fn)
 		fn();
Quoted 8 lines

arch/arm/include/asm/system_misc.h line 31

Quoted 5 lines
arch/arm/include/asm/system_misc.h +1 -0
+	put_cpu();
 }
 #else
 #define harden_branch_predictor() do { } while (0)

This doesn't look like the right fix. If we're in a preemptible context, then we could invalidate the branch predictor on the wrong CPU.

Sorry, my bad, thanks a lot for the good catch.

Quoted 1 line
Quoted 8 lines
Quoted 2 lines

The right fix would be to move the call to a point where we haven't enabled preemption yet.

I took a look at the code, and find out that the caller of harden_branch_predictor(), __do_user_fault(), is called by do_page_fault() and do_bad_area(), those two function's context are both running with preemption enabled, so I didn't find a good place to move the call, could you please give some suggestion for my next step?

Since we land here from do_page_fault, it seems natural to move the invalidation up there, probably before we re-enable interrupts.

Thanks,

	M.
Signature · 1 line
Jazz is not dead. It just smells funny...