All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Alan Maguire" <almagui@kernel.org>
Cc: bpf@vger.kernel.org
Subject: Re: [PATCH bpf-next 1/7] libbpf: Support CO-RE relocation of tracing program arguments
Date: Mon, 23 Nov 2026 22:53:11 +0000	[thread overview]
Message-ID: <sashiko-outbox-230162@kernel.org> (raw)
In-Reply-To: <20261123223227.745345-2-almagui@kernel.org>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] libbpf: wrong big-endian relocation for signed sub-8-byte
  tp_btf args
- [Medium] libbpf: pointer argument size and offset mismatch on 32-bit
  target BTF
- [Low] libbpf: multi-line comment style and misplaced comment in
  relo_core.c
--

commit 966987a90447045d3ff50e58c43a513399cdbc27
Author: Alan Maguire <almagui@kernel.org>

libbpf: Support CO-RE relocation of tracing program arguments

This commit adds CO-RE relocation support for tracing program arguments
described by structs tagged with __bpf_func_args and introduces the
BPF_PROG_CORE() convenience macro.

> diff --git a/tools/lib/bpf/relo_core.c b/tools/lib/bpf/relo_core.c
> index 2672623a41989..2f6627952220b 100644
> --- a/tools/lib/bpf/relo_core.c
> +++ b/tools/lib/bpf/relo_core.c
[ ... ]
> +	/* sub-8-byte arguments live in the low-order bytes of their slot,
> +	 * including structs and unions passed by value, which, e.g., s390
> +	 * passes in registers like integers of the same size
> +	 */
> +	sz = btf__resolve_size(targ_btf, type_id);
> +	if (btf_is_big_endian(targ_btf) && sz > 0 && sz < 8)
> +		slot_off += 8 - sz;
[ ... ]
> @@ -734,10 +1092,17 @@ static int bpf_core_calc_field_relo(const char *prog_name,
>  		return 0;
>  	}
>  
> -	m = btf_members(t) + acc->idx;
> -	mt = skip_mods_and_typedefs(spec->btf, m->type, &field_type_id);
> +	if (btf_kind(t) == BTF_KIND_FUNC) {
> +		/* function argument (or return value), never a bitfield */
> +		mt = skip_mods_and_typedefs(spec->btf, func_arg_type(spec->btf, t, acc->idx),
> +					    &field_type_id);
> +		bit_sz = 0;
> +	} else {
> +		m = btf_members(t) + acc->idx;
> +		mt = skip_mods_and_typedefs(spec->btf, m->type, &field_type_id);
> +		bit_sz = btf_member_bitfield_size(t, acc->idx);
> +	}

[Severity: Medium]
Does relocating a pointer argument against 32-bit target BTF downsize the
context load to 4 bytes and misalign the offset on 32-bit big-endian
targets?

Unlike func_arg_slot_sz(), which checks btf_is_ptr(t) and returns 8,
bpf_core_calc_field_relo() falls through to btf__resolve_size() for
BTF_KIND_FUNC arguments.  On 32-bit target BTF, btf__resolve_size()
returns 4 for BTF_KIND_PTR, so bpf_core_patch_insn() shrinks the 8-byte
context load to BPF_W, and bpf_core_match_func_arg() adds 4 to slot_off on
big-endian targets.  However, context slots are always u64 and
btf_ctx_access() requires pointer loads from the context array to be
8-byte aligned with size == sizeof(u64), so the verifier will reject the
program.

[ ... ]

> @@ -893,6 +1258,38 @@ static int bpf_core_calc_enumval_relo(const struct bpf_core_relo *relo,
>   * with each other. Otherwise, libbpf will refuse to proceed due to ambiguity.
>   * If instruction has to be poisoned, *poison will be set to true.
>   */
> +/* Whether a target spec refers to an integer or enum function argument (or
> + * return value), rather than to a member of a struct passed by value, that is
> + * smaller than 8 bytes on a big-endian target. Such arguments are in the
> + * low-order bytes of their context slot, where the verifier doesn't allow
> + * loads, but the calling conventions of big-endian architectures with BPF
> + * trampolines (s390x, powerpc64) extend them to 64 bits.
> + */

[Severity: Low]
This isn't a bug, but bpf_core_is_be_small_func_arg() is inserted between
the function comment for bpf_core_calc_relo() and bpf_core_calc_relo()
itself.  Also, the new multi-line comments in tools/lib/bpf/libbpf.c,
tools/lib/bpf/relo_core.c, and tools/lib/bpf/relo_core.h place text on the
opening /* line rather than putting /* on its own line per BPF comment
style.

> +static bool bpf_core_is_be_small_func_arg(const struct bpf_core_spec *spec,
> +					  bool *is_signed)
> +{
> +	const struct bpf_core_accessor *acc;
> +	const struct btf_type *t;
> +	__u32 id;
> +	__s64 sz;
> +
> +	if (spec->len != 2 || !btf_is_big_endian(spec->btf))
> +		return false;
> +	acc = &spec->spec[1];
> +	t = btf_type_by_id(spec->btf, acc->type_id);
> +	if (!t || btf_kind(t) != BTF_KIND_FUNC)
> +		return false;
> +	t = skip_mods_and_typedefs(spec->btf, func_arg_type(spec->btf, t, acc->idx), &id);
> +	if (!t || (!btf_is_int(t) && !btf_is_any_enum(t)))
> +		return false;
> +	sz = btf__resolve_size(spec->btf, id);
> +	if (sz <= 0 || sz >= 8)
> +		return false;
> +	*is_signed = (btf_is_any_enum(t) && BTF_INFO_KFLAG(t->info)) ||
> +		     (btf_is_int(t) && (btf_int_encoding(t) & BTF_INT_SIGNED));
> +	return true;
> +}
[ ... ]
> @@ -1100,6 +1506,38 @@ int bpf_core_patch_insn(const char *prog_name, struct bpf_insn *insn,
>  			return bpf_core_poison_insn(prog_name, relo_idx, insn, insn_idx);
>  		}
>  
> +		if (res->widen_func_arg && class == BPF_LDX) {
> +			__u8 mode = BPF_MODE(insn->code);
> +
> +			if (insn_bpf_size_to_bytes(insn) != res->orig_sz) {
> +				pr_warn("prog '%s': relo #%d: insn #%d (LDX) unexpected mem size: got %d, exp %u\n",
> +					prog_name, relo_idx, insn_idx,
> +					insn_bpf_size_to_bytes(insn), res->orig_sz);
> +				return -EINVAL;
> +			}
> +			/* An 8-byte load of the whole slot yields the argument
> +			 * as extended to 64 bits by the calling convention,
> +			 * which is what the original load yields only if it
> +			 * extends it the same way.
> +			 */
> +			if (!(mode == BPF_MEM && !res->func_arg_signed) &&
> +			    !(mode == BPF_MEMSX && res->func_arg_signed &&
> +			      res->orig_sz == res->new_sz)) {
> +				pr_warn("prog '%s': relo #%d: insn #%d (LDX) can't load function argument smaller than 8 bytes on big-endian target. "
> +					"Make sure to use zero-extending loads of unsigned integers or sign-extending loads of signed ones.\n",
> +					prog_name, relo_idx, insn_idx);
> +				return bpf_core_poison_insn(prog_name, relo_idx, insn, insn_idx);
> +			}
> +
> +			orig_val = insn->off;
> +			insn->off = res->func_arg_slot_off;
> +			insn->code = BPF_LDX | BPF_MEM | BPF_DW;

[Severity: Medium]
For tp_btf programs on big-endian targets, does widening a BPF_MEMSX load
of a signed sub-8-byte argument to BPF_DW corrupt negative values?

Unlike BPF trampolines, raw tracepoint probes populate context slots via
__CAST_TO_U64() in include/trace/bpf_probe.h:

include/trace/bpf_probe.h:__CAST_TO_U64() {
#define __CAST_TO_U64(x) ({ \
	typeof(x) __src = (x); \
	UINTTYPE(sizeof(x)) __dst; \
	memcpy(&__dst, &__src, sizeof(__dst)); \
	(u64)__dst; })
}

Because __CAST_TO_U64() copies the argument into an unsigned integer of
the same width before casting to u64, sub-8-byte signed tracepoint
arguments are always zero-extended in their 64-bit context slot rather
than sign-extended.  Treating res->func_arg_signed as true for tp_btf
candidates causes bpf_core_patch_insn() to rewrite BPF_MEMSX loads into
BPF_DW loads that leave zeros in the upper 32/48/56 bits of negative
values, while poisoning BPF_MEM loads that would have read the
zero-extended slot value as expected.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261123223227.745345-1-almagui@kernel.org?part=1

  reply	other threads:[~2026-11-23 22:53 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23 22:32 [PATCH bpf-next 0/7] libbpf, bpf: Utilize CO-RE for BPF program parameter presence, ordering Alan Maguire
2026-11-23 22:32 ` [PATCH bpf-next 1/7] libbpf: Support CO-RE relocation of tracing program arguments Alan Maguire
2026-11-23 22:53   ` sashiko-bot [this message]
2026-11-23 23:23   ` bot+bpf-ci
2026-11-24 20:17     ` Andrey Grodzovsky
2026-11-23 22:32 ` [PATCH bpf-next 2/7] bpf: " Alan Maguire
2026-11-23 22:52   ` sashiko-bot
2026-11-23 22:32 ` [PATCH bpf-next 3/7] selftests/bpf: Test " Alan Maguire
2026-11-23 22:43   ` sashiko-bot
2026-11-23 22:32 ` [PATCH bpf-next 4/7] selftests/bpf: Test CO-RE relocation of BPF LSM " Alan Maguire
2026-11-23 22:41   ` sashiko-bot
2026-11-23 22:32 ` [PATCH bpf-next 5/7] libbpf: Report function argument relocation support via type existence Alan Maguire
2026-11-23 22:38   ` sashiko-bot
2026-11-23 22:32 ` [PATCH bpf-next 6/7] selftests/bpf: Test detection of function argument relocation support Alan Maguire
2026-11-23 22:36   ` sashiko-bot
2026-11-23 22:32 ` [PATCH bpf-next 7/7] Documentation/bpf: Describe CO-RE relocation of function arguments Alan Maguire
2026-11-24  0:50 ` [PATCH bpf-next 0/7] libbpf, bpf: Utilize CO-RE for BPF program parameter presence, ordering Alexei Starovoitov
  -- strict thread matches above, loose matches on Subject: below --
2026-11-27 16:11 [PATCH bpf-next 1/7] libbpf: Support CO-RE relocation of tracing program arguments kernel test robot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-230162@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=almagui@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.