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=1796214807; b=UPOHllhUN/yXY0AZsRzs76BGW3gfv798x9R5XcU4F+UG9ePWDPIihrfODiaI7apXIaaL jfh+d9fad2d+Z3+5OAzXWHYED15k9lnYHjIgf9CPXq95KBEsDyJBw2gO6YFk8Ku6N4CVp SpHyzWcWFH9oJZjq25Z5tYncNKe/82gKDUtVXsDsEeR5P1GsNL6m3PGFuZOYo7/Qo5Yvm bgWe0WzqcWGJ1VtM/esva6uykmJV9A7B19IC23f3pRYv9xAxnFbeqIIe0MhSZXVg+iqew DgM+UBYiOYY2JxnJLI90k7/0BoJOIXXz5Zfh/0ep/rT/XQGz35EdwI2rZBZsuhQRclA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796214807; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=93TPDdqRcEV+5etdT1S5rQBZmtnODAzCqjdzoOIMiEc=; b=K/0WEKGfUTGYrWPa7oFi2bqOtfwm4Ov8RSgoqZopRrc6XJ/TZACSDIf0GAkCKDB0Nqcf zBJ9K90gNaqIip+FAat6cDTasDQPq+hfFRd3RJaTvI6HU+wRWZyzEtZtzx/1AicHIWj8f Jfovk8+wJzr+9iKLxvcnhfP+OtBd7EhXSiVCwxp3IzujP/zQ10eEnUIapuYkSn7YxyaNE ZUpKHjmjw07t6JJyefJKlJUZ3QT2WMg9IRLdMuFCzuzz1mm6B0CxYL+zHliIbQNser39J r9YtaIU3qzY+NZIJ+WD0E+1SeKVA7JWvsAzvQROiMahYRnuxvD+G8dOW9LUga5JrgvQ== 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="YC/MY3jG"; 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 9753F606A325; Wed, 2 Dec 2026 12:33:27 +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=YC/MY3jG Received: by smtp.kernel.org (Postfix) with ESMTPSA id 312D91F00897; Wed, 2 Dec 2026 12:33:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796214807; bh=93TPDdqRcEV+5etdT1S5rQBZmtnODAzCqjdzoOIMiEc=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=YC/MY3jGP06qtx7LWqIdz0whqd6BKIebu1vfEMEin3u8GkL1yokEw/MmqjrE3XhV0 /AArc6A68dwYiu5E8OPEM9IzKbF9DKbB3DJsPIHUNJhH3wBmotyn228px2JZ2cQHqO DpRo/sWfpgiroaMBi3jmOPo/oKa+UNx7EzjzsOHCtTOBvTZr5nkfb/5XVUk0mBEhlN opNhg5a+gIJd67rn2/Xm39mYy1U1gBBGKlORy5LlzKk+dPSA1wreGqCtlin3Qa96FD qkI1YCWSfki+Cg7kVUy3DpfVKvIh4AbgdXGaR1c0a7QzaiJXRYf/uLJ9h6T0LhfH0j aVmnVgGTN+xAA== 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 16/31] bpf: Add get_func_arg[_cnt] support for kprobe multi inlined functions Date: Wed, 2 Dec 2026 13:30:20 +0100 Message-ID: <20261202123035.10335-17-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: bpf@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 bpf_get_func_arg and bpf_get_func_arg_cnt helpers support for inlined instances attached with BPF_F_KPROBE_MULTI_INLINE flag. The program attached to inlined instance gets pt_regs of the probed instruction, where the arguments are not in the argument registers. Their locations are described in inline BTF, so store the location prototype of each instance and use it in the helpers. The register value is read with regs_get_dwarf_register function, so the support is at the moment limited to x86_64. The bpf_get_func_arg helper fails with: -EINVAL for argument index out of the number of arguments -ENOENT for argument without location (likely optimized out) -EFAULT if the argument can't be read from its location -EOPNOTSUPP for unsupported location Signed-off-by: Jiri Olsa --- include/uapi/linux/bpf.h | 13 ++-- kernel/trace/bpf_trace.c | 132 ++++++++++++++++++++++++++++++--- tools/include/uapi/linux/bpf.h | 13 ++-- 3 files changed, 137 insertions(+), 21 deletions(-) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 863b911c4bc5..eab777547afd 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -5681,13 +5681,16 @@ union bpf_attr { * returned in **value**. * * For kprobe multi programs get **n**-th argument (zero based) of - * the traced function. + * the traced function or of its inlined instance. * * 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. + * **-ENOENT** if the argument of inlined instance has no location. + * **-EFAULT** if the argument of inlined instance can't be read. + * **-EOPNOTSUPP** if the location of inlined instance argument is + * not supported or for kprobe multi programs executed in return + * probe. * * long bpf_get_func_ret(void *ctx, u64 *value) * Description @@ -5704,12 +5707,12 @@ union bpf_attr { * function arguments are stored in these registers. * * For kprobe multi programs get number of arguments of the traced - * function. + * function or of its inlined instance. * * 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. + * **BPF_F_KPROBE_MULTI_RETURN** flag. * * int bpf_get_retval(void) * Description diff --git a/kernel/trace/bpf_trace.c b/kernel/trace/bpf_trace.c index f9b63cf5f66f..af7d258d7357 100644 --- a/kernel/trace/bpf_trace.c +++ b/kernel/trace/bpf_trace.c @@ -2382,10 +2382,13 @@ struct bpf_kprobe_multi_link; struct inline_site { struct kprobe kp; struct bpf_kprobe_multi_link *link; + const struct btf *btf; + const struct btf_type *loc_proto; u64 cookie; u32 sym_idx; u32 func_id; bool has_entry; + bool is_vmlinux; bool traceable; }; @@ -2733,6 +2736,109 @@ static u64 bpf_kprobe_multi_entry_ip(struct bpf_run_ctx *ctx) return run_ctx->entry_ip; } +/* + * Get @n-th argument of the inlined instance @site from its location + * described in inline BTF, @regs are registers of the probed instruction. + */ +static int inline_site_arg(const struct inline_site *site, struct pt_regs *regs, + u32 n, u64 *value) +{ + const struct btf_loc_param *p; + const struct btf_type *t; + u32 id, flags, vlen; + unsigned long regval; + u64 val, mem = 0; + int err; + + if (n >= btf_type_vlen(site->loc_proto)) + return -EINVAL; + + /* There's no location for the argument, it's likely optimized out. */ + id = btf_loc_proto_params(site->loc_proto)[n]; + if (!id) + return -ENOENT; + + /* + * The location is BTF_KIND_LOC_PARAM type, its size is the size + * of the argument and its flags say how to get the argument value + * from the vlen number of 32-bit values that follow. + */ + t = btf_type_by_id(site->btf, id); + p = btf_loc_param(t); + vlen = btf_type_vlen(t); + flags = p->flags; + + /* The value needs to fit in u64. */ + if (t->size > sizeof(u64)) + return -EOPNOTSUPP; + + /* + * The argument is constant, stored in the first value, 64-bit + * constant is stored in two values in {lo, hi} order. + */ + if (flags & BTF_LOC_PARAM_CONST) { + val = p->values[0]; + if (vlen > 1) + val |= (u64)p->values[1] << 32; + if (flags & BTF_LOC_PARAM_ADDR) { + /* The address is relative to the kernel image start. */ + if (!site->is_vmlinux) + return -EOPNOTSUPP; + val += (unsigned long)_text; + } else if (flags & BTF_LOC_PARAM_SIGNED) { + /* Sign extend the constant from the argument size. */ + val = sign_extend64(val, t->size * 8 - 1); + } + *value = val; + return 0; + } + + /* + * The rest of the locations is based on register, the first value + * is its DWARF number. + */ + + /* Value stored in two registers. */ + if (flags == BTF_LOC_PARAM_REG && vlen > 1) + return -EOPNOTSUPP; + + /* + * Fails for registers we don't have value for, which includes + * frame base register (BTF_LOC_PARAM_FBREG). + */ + err = regs_get_dwarf_register(regs, p->values[0], ®val); + if (err) + return err; + val = regval; + + /* + * The offset follows the register number, it's either signed + * 32-bit value or 64-bit value in {lo, hi} order. + */ + if (flags & BTF_LOC_PARAM_OFFSET) { + if (vlen > 2) + val += (u64)p->values[2] << 32 | p->values[1]; + else + val += (s32)p->values[1]; + } + + /* + * Without dereference the argument is the register value (plus + * offset) itself, otherwise it's address the argument is read from. + */ + if (flags & BTF_LOC_PARAM_DEREF) { + if (copy_from_kernel_nofault(&mem, (void *)(unsigned long)val, t->size)) + return -EFAULT; + /* On big endian the argument was read to the high bytes of mem. */ + if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN)) + mem >>= (sizeof(mem) - t->size) * 8; + val = mem; + } + + *value = val; + return 0; +} + /* * Get number of arguments of the function we got executed for * through its entry probe. @@ -2759,9 +2865,8 @@ BPF_CALL_3(bpf_get_func_arg_kprobe_multi, struct pt_regs *, regs, u32, n, u64 *, 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; + return inline_site_arg(run_ctx->site, regs, n, value); /* The arguments are not available in return probe. */ if (run_ctx->session_ctx.is_return) return -EOPNOTSUPP; @@ -2791,9 +2896,8 @@ BPF_CALL_1(bpf_get_func_arg_cnt_kprobe_multi, struct pt_regs *, regs) 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; + return btf_type_vlen(run_ctx->site->loc_proto); cnt = kprobe_multi_entry_args_cnt(run_ctx); return cnt < 0 ? 0 : cnt; @@ -3057,8 +3161,9 @@ struct inline_lookup { u32 cap; }; -static int inline_lookup_add(struct inline_lookup *lookup, unsigned long addr, - u32 sym_idx, u32 func_id) +static int inline_lookup_add(struct inline_lookup *lookup, const struct btf *btf, + const struct btf_loc *loc, unsigned long addr, + u32 sym_idx, bool is_vmlinux) { struct inline_site *sites; u32 cap; @@ -3077,8 +3182,11 @@ static int inline_lookup_add(struct inline_lookup *lookup, unsigned long addr, lookup->sites[lookup->cnt++] = (struct inline_site) { .kp.addr = (kprobe_opcode_t *)addr, + .btf = btf, + .loc_proto = btf_type_by_id(btf, loc->loc_proto), .sym_idx = sym_idx, - .func_id = func_id, + .func_id = loc->func, + .is_vmlinux = is_vmlinux, }; return 0; } @@ -3088,7 +3196,8 @@ static int inline_lookup_add(struct inline_lookup *lookup, unsigned long addr, * the offsets of its locations are relative to @base address. */ static int inline_lookup_locsec(struct inline_lookup *lookup, const struct btf *btf, - const struct btf_type *locsec, unsigned long base) + const struct btf_type *locsec, unsigned long base, + bool is_vmlinux) { struct btf_loc *loc; int idx, err; @@ -3098,7 +3207,7 @@ static int inline_lookup_locsec(struct inline_lookup *lookup, const struct btf * idx = inline_sym_idx(btf, loc, lookup->syms, lookup->syms_cnt); if (idx < 0) continue; - err = inline_lookup_add(lookup, base + loc->offset, idx, loc->func); + err = inline_lookup_add(lookup, btf, loc, base + loc->offset, idx, is_vmlinux); if (err) return err; } @@ -3131,7 +3240,7 @@ static int inline_lookup_module(struct module *mod, const struct btf *btf, void base = module_sect_addr(mod, name + strlen("inline")); if (!base || !within_module_mem_type(base, mod, MOD_TEXT)) continue; - err = inline_lookup_locsec(lookup, btf, t, base); + err = inline_lookup_locsec(lookup, btf, t, base, false); if (err) return err; } @@ -3266,7 +3375,8 @@ static int inline_lookup_symbols(struct inline_lookup *lookup) if (err) goto error; - err = inline_lookup_locsec(lookup, btf, btf_type_by_id(btf, id), (unsigned long)_stext); + err = inline_lookup_locsec(lookup, btf, btf_type_by_id(btf, id), + (unsigned long)_stext, true); if (err) goto error; diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 863b911c4bc5..eab777547afd 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -5681,13 +5681,16 @@ union bpf_attr { * returned in **value**. * * For kprobe multi programs get **n**-th argument (zero based) of - * the traced function. + * the traced function or of its inlined instance. * * 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. + * **-ENOENT** if the argument of inlined instance has no location. + * **-EFAULT** if the argument of inlined instance can't be read. + * **-EOPNOTSUPP** if the location of inlined instance argument is + * not supported or for kprobe multi programs executed in return + * probe. * * long bpf_get_func_ret(void *ctx, u64 *value) * Description @@ -5704,12 +5707,12 @@ union bpf_attr { * function arguments are stored in these registers. * * For kprobe multi programs get number of arguments of the traced - * function. + * function or of its inlined instance. * * 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. + * **BPF_F_KPROBE_MULTI_RETURN** flag. * * int bpf_get_retval(void) * Description -- 2.56.1