From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796214797; b=ANHSJnBFXpQP48iAuRF9LWmZmPq7qZrA8m+eAdRxxrP0m3L+1xsryVtPp4ari5V734+X liHRCXb4DZBPYcxhb0bbf0idEHofm6LhHTlySThmLGDUpLc1tm4njPDLl7Hxson5mTOwH gYzmi7VrgJ1h8eetiLRE+dneo9lK108l6ckjgPuKvNUGnvSHemj2iZ2IFGJko1UMId3OF xknVp1BESmFkbXB6gLOFK5tep32rPL4fOV2yETMvK97hqNCSZsH7A4pTr7nrdVb75sAqF zJSqO0U1oT8X3Fg1A4d5kL5UhPAaFs1pyho5Nkr1BzdlsCiK4mSkcOwwULIwvDeI5/A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796214797; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=BzNQQI1D5+/prt3FZViz8cN9KDKEaL//RI8MZEdpgUM=; b=urvHrvvxjSl7Xn+lT8g1RfcmRZbq7QA2EKUmhzB7SojybuLb41jvhg1afo0nuqT8Qooa Xks9LUR3mDe40PgWH+YbM4ntEQbSCaFiD8MA/eHBJAYK6U0FtCUlTIQ0Lhk4zVn2YdDu7 EGPpoirn2HbOR2LjtAOVHY8oi3eBSjEB8OB7fw/m4Zvj3m6wqvpXdJzYBToN+jrQdj61j NwA2QAzRycwy2mS0N4Cq0QFGaO7pIItCeBLSrWK0hR1ru68baRLGeiEgcxUYua/iq58IG CbBqdpaCE6MkKBZZYEWQ2Q7+ZxtJNP5FF1qzW7oXuAxYpsDaaMBqRBeB80x4Wu3ZTmg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=kfkjKi+2; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4EC06606A32A; Wed, 2 Dec 2026 12:33:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=kfkjKi+2 Received: by smtp.kernel.org (Postfix) with ESMTPSA id B941A1F00893; Wed, 2 Dec 2026 12:33:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796214796; bh=BzNQQI1D5+/prt3FZViz8cN9KDKEaL//RI8MZEdpgUM=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=kfkjKi+2vzlVH8gio5nLHymzx2ivkSL2SkLA9kXwMeVHdu8hgJZsbNY56FObRsN5m 7F3uzattRC4IHQ8ro+seieTnC+brb7dKPs1o6qtuKF3IYTektbVAvaPZFuC0yw/IGd h3gvAfJmH4I9GGgu+aaelw/fW3R/k+MDPKIWKYBwIT9wvfjxx/LBLfKc7bGxlLtv71 X4hgN7k+bNiVtn29QcJG+tBvi+0IZOd2X3cbXHSzgi40KTql/or9ZB2B71E6uJ7/j/ 0IYXJNJpxzlI7q5xtFnwJXgPZlJCvguiFUc7U+b46crGLhPgyQRVq4fWP8DNn5yG/O eNiTzkoDSpFig== From: Jiri Olsa To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Masami Hiramatsu Cc: bpf@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-kernel@vger.kernel.org, x86@kernel.org, Song Liu , Yonghong Song , Quentin Monnet , Alan Maguire Subject: [PATCH RFC bpf-next 15/31] bpf: Add get_func_arg[_cnt] support for kprobe multi functions Date: Wed, 2 Dec 2026 13:30:19 +0100 Message-ID: <20261202123035.10335-16-jolsa@kernel.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261202123035.10335-1-jolsa@kernel.org> References: <20261202123035.10335-1-jolsa@kernel.org> X-Mailing-List: linux-trace-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Adding support for bpf_get_func_arg and bpf_get_func_arg_cnt helpers in kprobe multi programs. The number of arguments is read from function's BTF during the attach and the argument value is read from ftrace_regs. The BTF lookup is done only for programs that call the helpers. The kprobe multi link does not need kernel BTF so far and that stays the same for programs that do not call the helpers. For programs that do, the number of arguments is zero for functions without BTF, the attach does not fail. The arguments are not available in return probe and for inlined instances, the support for those is added in following change. Signed-off-by: Jiri Olsa --- include/linux/bpf.h | 1 + include/uapi/linux/bpf.h | 10 ++ kernel/bpf/verifier.c | 3 + kernel/trace/bpf_trace.c | 179 +++++++++++++++++++++++++++++++-- tools/include/uapi/linux/bpf.h | 10 ++ 5 files changed, 192 insertions(+), 11 deletions(-) diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 94560bd9dccc..0c700a08bb0c 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -2073,6 +2073,7 @@ struct bpf_prog { enforce_expected_attach_type:1, /* Enforce expected_attach_type checking at attach time */ call_get_stack:1, /* Do we call bpf_get_stack() or bpf_get_stackid() */ call_get_func_ip:1, /* Do we call get_func_ip() */ + call_get_func_arg:1, /* Do we call get_func_arg[_cnt]() */ call_session_cookie:1, /* Do we call bpf_session_cookie() */ tstamp_type_access:1, /* Accessed __sk_buff->tstamp_type */ sleepable:1; /* BPF program is sleepable */ diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 3b97904851f4..863b911c4bc5 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -5680,9 +5680,14 @@ union bpf_attr { * Get **n**-th argument register (zero based) of the traced function (for tracing programs) * returned in **value**. * + * For kprobe multi programs get **n**-th argument (zero based) of + * the traced function. + * * Return * 0 on success. * **-EINVAL** if n >= argument register count of traced function. + * **-EOPNOTSUPP** for kprobe multi programs executed in return + * probe or for inlined instances. * * long bpf_get_func_ret(void *ctx, u64 *value) * Description @@ -5698,8 +5703,13 @@ union bpf_attr { * Get number of registers of the traced function (for tracing programs) where * function arguments are stored in these registers. * + * For kprobe multi programs get number of arguments of the traced + * function. + * * Return * The number of argument registers of the traced function. + * 0 for kprobe multi programs attached with + * **BPF_F_KPROBE_MULTI_RETURN** flag or for inlined instances. * * int bpf_get_retval(void) * Description diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 18c59ae784eb..a2a8dffd44b6 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -13479,6 +13479,9 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn env->prog->call_get_func_ip = true; } + if (func_id == BPF_FUNC_get_func_arg || func_id == BPF_FUNC_get_func_arg_cnt) + env->prog->call_get_func_arg = true; + /* a call that only a speculative state reaches is dead code */ if (func_id == BPF_FUNC_override_return && !env->cur_state->speculative) env->prog->kprobe_override = 1; diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index 552a7420fb8c..f9b63cf5f66f 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -1358,6 +1358,11 @@ static inline bool is_trace_fsession(const struct bpf_prog *prog) prog->expected_attach_type == BPF_TRACE_FSESSION_MULTI); } +#ifdef CONFIG_FPROBE +static const struct bpf_func_proto bpf_get_func_arg_proto_kprobe_multi; +static const struct bpf_func_proto bpf_get_func_arg_cnt_proto_kprobe_multi; +#endif + static const struct bpf_func_proto * kprobe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) { @@ -1384,6 +1389,12 @@ kprobe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) if (is_uprobe_multi(prog)) return &bpf_get_attach_cookie_proto_umulti; return &bpf_get_attach_cookie_proto_trace; +#ifdef CONFIG_FPROBE + case BPF_FUNC_get_func_arg: + return is_kprobe_multi(prog) ? &bpf_get_func_arg_proto_kprobe_multi : NULL; + case BPF_FUNC_get_func_arg_cnt: + return is_kprobe_multi(prog) ? &bpf_get_func_arg_cnt_proto_kprobe_multi : NULL; +#endif default: return bpf_tracing_func_proto(func_id, prog); } @@ -2394,6 +2405,7 @@ struct bpf_kprobe_multi_link { struct fprobe fp; unsigned long *addrs; u64 *cookies; + u8 *args_cnt; u32 cnt; u32 mods_cnt; struct module **mods; @@ -2405,6 +2417,7 @@ struct bpf_kprobe_multi_run_ctx { struct bpf_kprobe_multi_link *link; unsigned long entry_ip; struct inline_site *site; + struct ftrace_regs *fregs; }; struct user_syms { @@ -2558,6 +2571,7 @@ static void bpf_kprobe_multi_link_dealloc(struct bpf_link *link) kvfree(kmulti_link->inlines.kps); kvfree(kmulti_link->addrs); kvfree(kmulti_link->cookies); + kvfree(kmulti_link->args_cnt); kfree(kmulti_link->mods); kfree(kmulti_link); } @@ -2660,14 +2674,15 @@ static void bpf_kprobe_multi_cookie_swap(void *a, void *b, int size, const void { const struct bpf_kprobe_multi_link *link = priv; unsigned long *addr_a = a, *addr_b = b; - u64 *cookie_a, *cookie_b; - - cookie_a = link->cookies + (addr_a - link->addrs); - cookie_b = link->cookies + (addr_b - link->addrs); + u32 idx_a = addr_a - link->addrs; + u32 idx_b = addr_b - link->addrs; - /* swap addr_a/addr_b and cookie_a/cookie_b values */ + /* swap addr_a/addr_b and related cookie and args_cnt values */ swap(*addr_a, *addr_b); - swap(*cookie_a, *cookie_b); + if (link->cookies) + swap(link->cookies[idx_a], link->cookies[idx_b]); + if (link->args_cnt) + swap(link->args_cnt[idx_a], link->args_cnt[idx_b]); } static int bpf_kprobe_multi_addrs_cmp(const void *a, const void *b) @@ -2718,6 +2733,78 @@ static u64 bpf_kprobe_multi_entry_ip(struct bpf_run_ctx *ctx) return run_ctx->entry_ip; } +/* + * Get number of arguments of the function we got executed for + * through its entry probe. + */ +static int kprobe_multi_entry_args_cnt(struct bpf_kprobe_multi_run_ctx *run_ctx) +{ + struct bpf_kprobe_multi_link *link = run_ctx->link; + unsigned long entry_ip = run_ctx->entry_ip; + unsigned long *addr; + + if (!link->args_cnt) + return -EOPNOTSUPP; + addr = bsearch(&entry_ip, link->addrs, link->cnt, sizeof(entry_ip), + bpf_kprobe_multi_addrs_cmp); + if (!addr) + return -EOPNOTSUPP; + return link->args_cnt[addr - link->addrs]; +} + +BPF_CALL_3(bpf_get_func_arg_kprobe_multi, struct pt_regs *, regs, u32, n, u64 *, value) +{ + struct bpf_kprobe_multi_run_ctx *run_ctx; + int cnt; + + run_ctx = container_of(current->bpf_ctx, struct bpf_kprobe_multi_run_ctx, + session_ctx.run_ctx); + /* Arguments of inlined instances are not supported. */ + if (run_ctx->site) + return -EOPNOTSUPP; + /* The arguments are not available in return probe. */ + if (run_ctx->session_ctx.is_return) + return -EOPNOTSUPP; + + cnt = kprobe_multi_entry_args_cnt(run_ctx); + if (cnt < 0) + return cnt; + if (n >= cnt) + return -EINVAL; + *value = ftrace_regs_get_argument(run_ctx->fregs, n); + return 0; +} + +static const struct bpf_func_proto bpf_get_func_arg_proto_kprobe_multi = { + .func = bpf_get_func_arg_kprobe_multi, + .ret_type = RET_INTEGER, + .arg1_type = ARG_PTR_TO_CTX, + .arg2_type = ARG_ANYTHING, + .arg3_type = ARG_PTR_TO_FIXED_SIZE_MEM | MEM_UNINIT | MEM_WRITE | MEM_ALIGNED, + .arg3_size = sizeof(u64), +}; + +BPF_CALL_1(bpf_get_func_arg_cnt_kprobe_multi, struct pt_regs *, regs) +{ + struct bpf_kprobe_multi_run_ctx *run_ctx; + int cnt; + + run_ctx = container_of(current->bpf_ctx, struct bpf_kprobe_multi_run_ctx, + session_ctx.run_ctx); + /* Arguments of inlined instances are not supported. */ + if (run_ctx->site) + return 0; + + cnt = kprobe_multi_entry_args_cnt(run_ctx); + return cnt < 0 ? 0 : cnt; +} + +static const struct bpf_func_proto bpf_get_func_arg_cnt_proto_kprobe_multi = { + .func = bpf_get_func_arg_cnt_kprobe_multi, + .ret_type = RET_INTEGER, + .arg1_type = ARG_PTR_TO_CTX, +}; + static __always_inline int kprobe_multi_link_prog_run(struct bpf_kprobe_multi_link *link, struct inline_site *site, @@ -2732,6 +2819,7 @@ kprobe_multi_link_prog_run(struct bpf_kprobe_multi_link *link, .link = link, .entry_ip = entry_ip, .site = site, + .fregs = fregs, }; struct bpf_run_ctx *old_run_ctx; int err; @@ -2955,6 +3043,7 @@ struct inline_lookup { const char **syms; unsigned long *addrs; u64 *cookies; + u8 *args_cnt; u32 syms_cnt; /* Output */ @@ -3068,6 +3157,49 @@ static int inline_lookup_modules(struct inline_lookup *lookup) } #endif +/* Get number of arguments of the function from its BTF, zero if there's none. */ +static u8 func_args_cnt(const char *name) +{ + const struct btf_type *t; + struct btf *btf; + u32 cnt = 0; + s32 id; + + id = bpf_find_btf_id(name, BTF_KIND_FUNC, &btf); + if (id < 0) + return 0; + t = btf_type_by_id(btf, btf_type_by_id(btf, id)->type); + if (btf_type_is_func_proto(t)) + cnt = btf_type_vlen(t); + /* Do not count variable arguments. */ + if (cnt && !btf_params(t)[cnt - 1].type) + cnt--; + btf_put(btf); + return min(cnt, U8_MAX); +} + +/* + * Get number of arguments for @cnt functions in @addrs, the function + * name is taken from @syms if it's provided, otherwise it's resolved + * from the function address. + */ +static void kprobe_multi_args_cnt(u8 *args_cnt, const char **syms, + unsigned long *addrs, u32 cnt) +{ + char buf[KSYM_NAME_LEN]; + const char *name; + u32 i; + + for (i = 0; i < cnt; i++) { + /* Function that has only inlined instances. */ + if (!addrs[i]) + continue; + name = syms ? syms[i] : kallsyms_lookup(addrs[i], NULL, NULL, NULL, buf); + if (name) + args_cnt[i] = func_args_cnt(name); + } +} + /* * Sort instances by symbol name and address, so instances of each * symbol are next to each other, and get rid of duplicates. @@ -3169,6 +3301,8 @@ static int inline_lookup_symbols(struct inline_lookup *lookup) addrs[j] = addrs[i]; if (cookies) cookies[j] = cookies[i]; + if (lookup->args_cnt) + lookup->args_cnt[j] = lookup->args_cnt[i]; j++; } @@ -3257,6 +3391,7 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr u32 flags, cnt, size; void __user *uaddrs; u64 *cookies = NULL; + u8 *args_cnt = NULL; void __user *usyms; bool strict; int err; @@ -3335,11 +3470,26 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr } } + /* + * The number of arguments is needed only for programs that call + * bpf_get_func_arg[_cnt] helpers, the arguments are not available + * in return probe. + */ + if (prog->call_get_func_arg && !(flags & BPF_F_KPROBE_MULTI_RETURN)) { + args_cnt = kvcalloc(cnt, sizeof(*args_cnt), GFP_KERNEL); + if (!args_cnt) { + err = -ENOMEM; + goto error; + } + } + if (uaddrs) { if (copy_from_user(addrs, uaddrs, size)) { err = -EFAULT; goto error; } + if (args_cnt) + kprobe_multi_args_cnt(args_cnt, NULL, addrs, cnt); } else { struct multi_symbols_sort data = { .cookies = cookies, @@ -3358,6 +3508,9 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr err = ftrace_lookup_symbols(us.syms, cnt, addrs); + if (args_cnt && (!err || err == -ESRCH)) + kprobe_multi_args_cnt(args_cnt, us.syms, addrs, cnt); + /* * With inline flag it's ok if the function is not found, * as long as it has inlined instance. Unresolved functions @@ -3371,6 +3524,7 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr .syms_cnt = cnt, .addrs = addrs, .cookies = cookies, + .args_cnt = args_cnt, }; err = inline_lookup_symbols(&lookup); @@ -3411,16 +3565,18 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr link->addrs = addrs; link->cookies = cookies; + link->args_cnt = args_cnt; link->cnt = cnt; link->link.flags = flags; link->inlines = inlines; - if (cookies) { + if (cookies || args_cnt) { /* - * Sorting addresses will trigger sorting cookies as well - * (check bpf_kprobe_multi_cookie_swap). This way we can - * find cookie based on the address in bpf_get_attach_cookie - * helper. + * Sorting addresses will trigger sorting cookies and + * args_cnt as well (check bpf_kprobe_multi_cookie_swap). + * This way we can find cookie based on the address in + * bpf_get_attach_cookie helper and number of arguments + * in bpf_get_func_arg[_cnt] helpers. */ sort_r(addrs, cnt, sizeof(*addrs), bpf_kprobe_multi_cookie_cmp, @@ -3463,6 +3619,7 @@ int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *pr kfree(link); kvfree(addrs); kvfree(cookies); + kvfree(args_cnt); kvfree(inlines.sites); return err; } diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 3b97904851f4..863b911c4bc5 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -5680,9 +5680,14 @@ union bpf_attr { * Get **n**-th argument register (zero based) of the traced function (for tracing programs) * returned in **value**. * + * For kprobe multi programs get **n**-th argument (zero based) of + * the traced function. + * * Return * 0 on success. * **-EINVAL** if n >= argument register count of traced function. + * **-EOPNOTSUPP** for kprobe multi programs executed in return + * probe or for inlined instances. * * long bpf_get_func_ret(void *ctx, u64 *value) * Description @@ -5698,8 +5703,13 @@ union bpf_attr { * Get number of registers of the traced function (for tracing programs) where * function arguments are stored in these registers. * + * For kprobe multi programs get number of arguments of the traced + * function. + * * Return * The number of argument registers of the traced function. + * 0 for kprobe multi programs attached with + * **BPF_F_KPROBE_MULTI_RETURN** flag or for inlined instances. * * int bpf_get_retval(void) * Description -- 2.56.1