tracing/kprobes: Check whether get_kretprobe() returns NULL in kretprobe_dispatcher()

JIRA: https://issues.redhat.com/browse/RHEL-101938

commit cc72b72073ac982a954d3b43519ca1c28f03c27c
Author: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Date:   Sat May 28 00:55:39 2022 +0900

    tracing/kprobes: Check whether get_kretprobe() returns NULL in kretprobe_dispatcher()

    There is a small chance that get_kretprobe(ri) returns NULL in
    kretprobe_dispatcher() when another CPU unregisters the kretprobe
    right after __kretprobe_trampoline_handler().

    To avoid this issue, kretprobe_dispatcher() checks the get_kretprobe()
    return value again. And if it is NULL, it returns soon because that
    kretprobe is under unregistering process.

    This issue has been introduced when the kretprobe is decoupled
    from the struct kretprobe_instance by commit d741bf41d7
    ("kprobes: Remove kretprobe hash"). Before that commit, the
    struct kretprob_instance::rp directly points the kretprobe
    and it is never be NULL.

    Link: https://lkml.kernel.org/r/165366693881.797669.16926184644089588731.stgit@devnote2

    Reported-by: Yonghong Song <yhs@fb.com>
    Fixes: d741bf41d7 ("kprobes: Remove kretprobe hash")
    Cc: Peter Zijlstra <peterz@infradead.org>
    Cc: Ingo Molnar <mingo@kernel.org>
    Cc: bpf <bpf@vger.kernel.org>
    Cc: Kernel Team <kernel-team@fb.com>
    Cc: stable@vger.kernel.org
    Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
    Acked-by: Jiri Olsa <jolsa@kernel.org>
    Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>

Signed-off-by: Jerome Marchand <jmarchan@redhat.com>
This commit is contained in:
Jerome Marchand 2025-07-21 09:54:46 +02:00
parent 7dc962dd11
commit f5ebcab25b
1 changed files with 10 additions and 1 deletions

View File

@ -1745,8 +1745,17 @@ static int
kretprobe_dispatcher(struct kretprobe_instance *ri, struct pt_regs *regs)
{
struct kretprobe *rp = get_kretprobe(ri);
struct trace_kprobe *tk = container_of(rp, struct trace_kprobe, rp);
struct trace_kprobe *tk;
/*
* There is a small chance that get_kretprobe(ri) returns NULL when
* the kretprobe is unregister on another CPU between kretprobe's
* trampoline_handler and this function.
*/
if (unlikely(!rp))
return 0;
tk = container_of(rp, struct trace_kprobe, rp);
raw_cpu_inc(*tk->nhit);
if (trace_probe_test_flag(&tk->tp, TP_FLAG_TRACE))