Skip to content

Commit fa99a34

Browse files
rnavgregkh
authored andcommitted
powerpc/kprobes: Fix call trace due to incorrect preempt count
commit e6e133c upstream. Michael Ellerman reported the following call trace when running ftracetest: BUG: using __this_cpu_write() in preemptible [00000000] code: ftracetest/6178 caller is opt_pre_handler+0xc4/0x110 CPU: 1 PID: 6178 Comm: ftracetest Not tainted 4.15.0-rc7-gcc6x-gb2cd1df #1 Call Trace: [c0000000f9ec39c0] [c000000000ac4304] dump_stack+0xb4/0x100 (unreliable) [c0000000f9ec3a00] [c00000000061159c] check_preemption_disabled+0x15c/0x170 [c0000000f9ec3a90] [c000000000217e84] opt_pre_handler+0xc4/0x110 [c0000000f9ec3af0] [c00000000004cf68] optimized_callback+0x148/0x170 [c0000000f9ec3b40] [c00000000004d954] optinsn_slot+0xec/0x10000 [c0000000f9ec3e30] [c00000000004bae0] kretprobe_trampoline+0x0/0x10 This is showing up since OPTPROBES is now enabled with CONFIG_PREEMPT. trampoline_probe_handler() considers itself to be a special kprobe handler for kretprobes. In doing so, it expects to be called from kprobe_handler() on a trap, and re-enables preemption before returning a non-zero return value so as to suppress any subsequent processing of the trap by the kprobe_handler(). However, with optprobes, we don't deal with special handlers (we ignore the return code) and just try to re-enable preemption causing the above trace. To address this, modify trampoline_probe_handler() to not be special. The only additional processing done in kprobe_handler() is to emulate the instruction (in this case, a 'nop'). We adjust the value of regs->nip for the purpose and delegate the job of re-enabling preemption and resetting current kprobe to the probe handlers (kprobe_handler() or optimized_callback()). Fixes: 8a2d71a ("powerpc/kprobes: Disable preemption before invoking probe handler for optprobes") Cc: [email protected] # v4.15+ Reported-by: Michael Ellerman <[email protected]> Signed-off-by: Naveen N. Rao <[email protected]> Acked-by: Ananth N Mavinakayanahalli <[email protected]> Signed-off-by: Michael Ellerman <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 3df05fc commit fa99a34

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

arch/powerpc/kernel/kprobes.c

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -457,29 +457,33 @@ static int trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
457457
}
458458

459459
kretprobe_assert(ri, orig_ret_address, trampoline_address);
460-
regs->nip = orig_ret_address;
460+
461461
/*
462-
* Make LR point to the orig_ret_address.
463-
* When the 'nop' inside the kretprobe_trampoline
464-
* is optimized, we can do a 'blr' after executing the
465-
* detour buffer code.
462+
* We get here through one of two paths:
463+
* 1. by taking a trap -> kprobe_handler() -> here
464+
* 2. by optprobe branch -> optimized_callback() -> opt_pre_handler() -> here
465+
*
466+
* When going back through (1), we need regs->nip to be setup properly
467+
* as it is used to determine the return address from the trap.
468+
* For (2), since nip is not honoured with optprobes, we instead setup
469+
* the link register properly so that the subsequent 'blr' in
470+
* kretprobe_trampoline jumps back to the right instruction.
471+
*
472+
* For nip, we should set the address to the previous instruction since
473+
* we end up emulating it in kprobe_handler(), which increments the nip
474+
* again.
466475
*/
476+
regs->nip = orig_ret_address - 4;
467477
regs->link = orig_ret_address;
468478

469-
reset_current_kprobe();
470479
kretprobe_hash_unlock(current, &flags);
471-
preempt_enable_no_resched();
472480

473481
hlist_for_each_entry_safe(ri, tmp, &empty_rp, hlist) {
474482
hlist_del(&ri->hlist);
475483
kfree(ri);
476484
}
477-
/*
478-
* By returning a non-zero value, we are telling
479-
* kprobe_handler() that we don't want the post_handler
480-
* to run (and have re-enabled preemption)
481-
*/
482-
return 1;
485+
486+
return 0;
483487
}
484488
NOKPROBE_SYMBOL(trampoline_probe_handler);
485489

0 commit comments

Comments
 (0)