All of lore.kernel.org
 help / color / mirror / Atom feed
From: Yonghong Song <yonghong.song@linux.dev>
To: bot+bpf-ci@kernel.org, bpf@vger.kernel.org
Cc: ast@kernel.org, andrii@kernel.org, daniel@iogearbox.net,
	eddyz87@gmail.com, kernel-team@fb.com, martin.lau@kernel.org,
	mason@kernel.org, ihor.solodrai@linux.dev
Subject: Re: [PATCH bpf-next v6 15/21] libbpf: Collect .bpf_cleanup records and pass them to the kernel
Date: Sun, 27 Sep 2026 20:28:03 -0700	[thread overview]
Message-ID: <0b83c451-1704-4630-9086-1f4e7a652619@linux.dev> (raw)
In-Reply-To: <3b553c5ab29803b0aa25087a5a5b0e1d15bfce01166bfa8e97aad8e704cce09f@mail.kernel.org>



On 9/27/26 1:39 PM, bot+bpf-ci@kernel.org wrote:
>> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
>> index 7eb15a013a83..4e9584fff264 100644
>> --- a/tools/lib/bpf/libbpf.c
>> +++ b/tools/lib/bpf/libbpf.c
> [ ... ]
>
>> @@ -4915,6 +4953,249 @@ static struct bpf_program *find_prog_by_sec_insn(const struct bpf_object *obj,
>>   	return NULL;
>>   }
>>
>> +static int bpf_object__init_cleanup_info(struct bpf_object *obj)
>> +{
>> +	Elf_Data *data = obj->efile.cleanup_data;
>> +	Elf_Data *relo = NULL;
>> +	size_t i, nrels, nslots, nrecs;
>> +	struct cleanup_raw_rec *recs;
>> +	int *slot_sec, ret = 0;
>> +	size_t *slot_val;
>> +	const __u32 *vals;
>> +	Elf64_Shdr *sh;
>> +	bool native;
>> +
>> +	if (!data || obj->efile.cleanup_shndx < 0 || !data->d_size)
>> +		return 0;
>> +
>> +	native = is_native_endianness(obj);
>> +
>> +	for (i = 0; i < obj->efile.sec_cnt; i++) {
>> +		struct elf_sec_desc *sd = &obj->efile.secs[i];
>> +
>> +		if (sd->sec_type == SEC_RELO && sd->shdr &&
>> +		    sd->shdr->sh_info == (Elf64_Word)obj->efile.cleanup_shndx) {
>> +			relo = sd->data;
>> +			break;
>> +		}
>> +	}
>> +	if (!relo) {
>> +		pr_warn("%s present without relocations\n", CLEANUP_SEC);
>> +		return -LIBBPF_ERRNO__FORMAT;
>> +	}
>> +	/*
>> +	 * LLVM leaves sh_entsize unset, so this only bites a producer that
>> +	 * declares a record size -- which is the one able to tell us it means
>> +	 * something other than three 4-byte fields.
>> +	 */
>> +	sh = elf_sec_hdr(obj, elf_sec_by_idx(obj, obj->efile.cleanup_shndx));
>> +	if (sh && sh->sh_entsize &&
>> +	    sh->sh_entsize != sizeof(struct bpf_cleanup_info)) {
>> +		pr_warn("%s record size %zu is not the expected %zu\n", CLEANUP_SEC,
>> +			(size_t)sh->sh_entsize, sizeof(struct bpf_cleanup_info));
>> +		return -LIBBPF_ERRNO__FORMAT;
>> +	}
> Should sh_entsize == 0 be explicitly treated as 12, so that wider
> records (if necessary in the future) end up being easier to reject?
>
> This design question was raised by Kumar Kartikeya Dwivedi in v5:
> https://lore.kernel.org/bpf/DLMGMYOHSGFW.24JML4GSMTV2K@gmail.com/
>
> The current code accepts sh_entsize == 0 (LLVM's default) silently, but
> treating it explicitly as 12 would allow future tools to cleanly reject
> wider records by setting sh_entsize to a different value.

Okay. This is not a bug, but rather to have better code to have
explicit 'entsize' size and then check against size of bpf_cleanup_info.

>
>> +	if (data->d_size % sizeof(struct bpf_cleanup_info)) {
>> +		pr_warn("%s size %zu is not a multiple of the record size %zu\n",
>> +			CLEANUP_SEC, data->d_size, sizeof(struct bpf_cleanup_info));
>> +		return -LIBBPF_ERRNO__FORMAT;
>> +	}
>> +
>> +	vals = data->d_buf;
>> +	nslots = data->d_size / sizeof(__u32);
>> +	nrecs = data->d_size / sizeof(struct bpf_cleanup_info);
>> +
>> +	slot_sec = calloc(nslots, sizeof(*slot_sec));
>> +	slot_val = calloc(nslots, sizeof(*slot_val));
>> +	recs = calloc(nrecs ?: 1, sizeof(*recs));
> This isn't a bug, but could the relocation loop fill recs directly
> (with sec_idx initialised to -1) so the slot_sec/slot_val scratch
> arrays and the copy loop go away?

Good point. This will make things easier.

>
> The relocation loop could write straight into recs, using sec_idx = -1
> as the 'no relocation' sentinel and checking alignment while the byte
> offset is at hand, the way bpf_object__collect_rodata_relos() checks
> (sym->st_value + addend) % BPF_INSN_SZ inline. That would leave one
> allocation instead of three and drop the copy loop.
>
> Also, is the 'nrecs ?: 1' needed? d_size has already been checked to be
> a non-zero multiple of the record size.

Will do.

>
>> +	if (!slot_sec || !slot_val || !recs) {
>> +		ret = -ENOMEM;
>> +		goto out;
>> +	}
>> +	for (i = 0; i < nslots; i++)
>> +		slot_sec[i] = -1;
>> +
>> +	/* One relocation per 4-byte field, naming the section it points into. */
>> +	nrels = relo->d_size / sizeof(Elf64_Rel);
>> +	for (i = 0; i < nrels; i++) {
> [ ... ]
>
>> +	for (i = 0; i < nslots; i++) {
>> +		struct cleanup_raw_rec *rec = &recs[i / CLEANUP_REC_FIELDS];
>> +		size_t field = i % CLEANUP_REC_FIELDS;
>> +
>> +		if (slot_sec[i] < 0) {
>> +			pr_warn("%s: field %zu has no relocation\n", CLEANUP_SEC, i);
>> +			ret = -LIBBPF_ERRNO__FORMAT;
>> +			goto out;
>> +		}
>> +		if (slot_val[i] % BPF_INSN_SZ) {
>> +			pr_warn("%s: field %zu offset %zu is not instruction aligned\n",
>> +				CLEANUP_SEC, i, slot_val[i]);
>> +			ret = -LIBBPF_ERRNO__FORMAT;
>> +			goto out;
>> +		}
>> +		rec->sec_idx[field] = slot_sec[i];
>> +		rec->insn_idx[field] = slot_val[i] / BPF_INSN_SZ;
>> +	}
>> +
>> +	obj->cleanup_recs = recs;
>> +	obj->cleanup_rec_cnt = nrecs;
>> +	recs = NULL;
>> +out:
>> +	free(recs);
>> +	free(slot_val);
>> +	free(slot_sec);
>> +	return ret;
>> +}
> [ ... ]
>
>
> ---
> AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
> See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
>
> CI run summary: https://github.com/kernel-patches/bpf/actions/runs/36346422430


  reply	other threads:[~2026-09-28  3:28 UTC|newest]

Thread overview: 56+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-26  5:00 [PATCH bpf-next v6 00/21] bpf: Run exception cleanup landing pads when bpf_unwind() unwinds Yonghong Song
2026-09-26  5:00 ` [PATCH bpf-next v6 01/21] bpf: Pack bpf_insn_aux_data flags into bit fields Yonghong Song
2026-09-26  5:00 ` [PATCH bpf-next v6 02/21] bpf: Accept the compiler's exception cleanup table at program load Yonghong Song
2026-09-26  5:00 ` [PATCH bpf-next v6 03/21] bpf: Add the bpf_unwind() and bpf_unwind_resume() kfuncs Yonghong Song
2026-09-26  5:00 ` [PATCH bpf-next v6 04/21] bpf: Add lookups for exception cleanup resumes and landing pads Yonghong Song
2026-09-26  5:00 ` [PATCH bpf-next v6 05/21] bpf: Prepare for an exception cleanup table before the CFG walk Yonghong Song
2026-09-26  5:16   ` sashiko-bot
2026-09-26 23:54     ` Yonghong Song
2026-09-27 20:39   ` bot+bpf-ci
2026-09-28  0:01     ` Yonghong Song
2026-09-26  5:00 ` [PATCH bpf-next v6 06/21] bpf: Make exception landing pads reachable in the CFG Yonghong Song
2026-09-26  5:21   ` sashiko-bot
2026-09-27  0:02     ` Yonghong Song
2026-09-27 20:40   ` bot+bpf-ci
2026-09-28  0:12     ` Yonghong Song
2026-09-26  5:00 ` [PATCH bpf-next v6 07/21] bpf: Resume a covered call at its landing pad Yonghong Song
2026-09-26  5:15   ` sashiko-bot
2026-09-26  8:21     ` Alexei Starovoitov
2026-09-27  0:04       ` Yonghong Song
2026-09-27  0:41     ` Yonghong Song
2026-09-27 20:40   ` bot+bpf-ci
2026-09-28  0:17     ` Yonghong Song
2026-09-26  5:00 ` [PATCH bpf-next v6 08/21] bpf: Refuse a landing pad that does not resume Yonghong Song
2026-09-26  5:17   ` sashiko-bot
2026-09-27  3:06     ` Yonghong Song
2026-09-27 20:40   ` bot+bpf-ci
2026-09-28  0:29     ` Yonghong Song
2026-09-26  5:00 ` [PATCH bpf-next v6 09/21] bpf: Refuse a private stack for a program with an exception cleanup table Yonghong Song
2026-09-26  5:00 ` [PATCH bpf-next v6 10/21] bpf: Dispatch cleanup pads by rewriting return addresses Yonghong Song
2026-09-27 20:40   ` bot+bpf-ci
2026-09-28  1:08     ` Yonghong Song
2026-09-26  5:01 ` [PATCH bpf-next v6 11/21] bpf, x86: Dispatch exception cleanup pads at run time Yonghong Song
2026-09-26  5:15   ` sashiko-bot
2026-09-27  4:35     ` Yonghong Song
2026-09-27 20:39   ` bot+bpf-ci
2026-09-28  3:10     ` Yonghong Song
2026-09-26  5:01 ` [PATCH bpf-next v6 12/21] bpf, arm64: " Yonghong Song
2026-09-26  5:14   ` sashiko-bot
2026-09-27 20:40   ` bot+bpf-ci
2026-09-26  5:01 ` [PATCH bpf-next v6 13/21] libbpf: Resolve the compiler's _Unwind_Resume to the kernel's kfunc Yonghong Song
2026-09-26  5:01 ` [PATCH bpf-next v6 14/21] libbpf: Add cleanup_info to bpf_prog_load_opts Yonghong Song
2026-09-26  5:01 ` [PATCH bpf-next v6 15/21] libbpf: Collect .bpf_cleanup records and pass them to the kernel Yonghong Song
2026-09-27 20:39   ` bot+bpf-ci
2026-09-28  3:28     ` Yonghong Song [this message]
2026-09-26  5:01 ` [PATCH bpf-next v6 16/21] libbpf: Carry the exception cleanup table through the light skeleton Yonghong Song
2026-09-26  5:01 ` [PATCH bpf-next v6 17/21] libbpf: Let the static linker carry .bpf_cleanup relocations Yonghong Song
2026-09-26  5:01 ` [PATCH bpf-next v6 18/21] selftests/bpf: Add an end-to-end .bpf_cleanup exception test Yonghong Song
2026-09-26  5:01 ` [PATCH bpf-next v6 19/21] selftests/bpf: Add __set_global() and __ret_global() test tags Yonghong Song
2026-09-26  5:18   ` sashiko-bot
2026-09-27  4:58     ` Yonghong Song
2026-09-27 20:24   ` bot+bpf-ci
2026-09-28  3:36     ` Yonghong Song
2026-09-26  5:01 ` [PATCH bpf-next v6 20/21] selftests/bpf: Cover the exception cleanup shapes the chain does not reach Yonghong Song
2026-09-27 20:40   ` bot+bpf-ci
2026-09-28  3:49     ` Yonghong Song
2026-09-26  5:01 ` [PATCH bpf-next v6 21/21] selftests/bpf: Load an exception cleanup program from a light skeleton Yonghong Song

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=0b83c451-1704-4630-9086-1f4e7a652619@linux.dev \
    --to=yonghong.song@linux.dev \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bot+bpf-ci@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=ihor.solodrai@linux.dev \
    --cc=kernel-team@fb.com \
    --cc=martin.lau@kernel.org \
    --cc=mason@kernel.org \
    /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.