From: Anton Protopopov <a.s.protopopov@gmail.com>
To: bot+bpf-ci@kernel.org
Cc: bpf@vger.kernel.org, ast@kernel.org, andrii@kernel.org,
aspsk@isovalent.com, daniel@iogearbox.net, eddyz87@gmail.com,
qmo@kernel.org, yonghong.song@linux.dev, martin.lau@kernel.org,
clm@meta.com, ihor.solodrai@linux.dev
Subject: Re: [PATCH v9 bpf-next 01/11] bpf, x86: add new map type: instructions array
Date: Sat, 1 Nov 2025 16:32:46 +0000 [thread overview]
Message-ID: <aQY2LvmzFfzNxm1Q@mail.gmail.com> (raw)
In-Reply-To: <d383a192daaee1666c5a3dec3d07020dfcb4b4caa2434e73e3f911f629e35f45@mail.kernel.org>
On 25/11/01 11:29AM, bot+bpf-ci@kernel.org wrote:
>
> ```
> commit 77dc1e7091f8bbd42ffa1730c0339642333528c6
> Author: Anton Protopopov <a.s.protopopov@gmail.com>
>
> bpf, x86: add new map type: instructions array
>
> This patch adds BPF_MAP_TYPE_INSN_ARRAY to track instruction offset
> mappings (original -> xlated -> jitted) during BPF program verification
> and JIT compilation.
>
> > diff --git a/kernel/bpf/bpf_insn_array.c b/kernel/bpf/bpf_insn_array.c
> > new file mode 100644
> > index 000000000..d29f38721
> > --- /dev/null
> > +++ b/kernel/bpf/bpf_insn_array.c
>
> [ ... ]
>
> > @@ -0,0 +1,286 @@
> > +// SPDX-License-Identifier: GPL-2.0-only
> > +/* Copyright (c) 2025 Isovalent */
> > +
> > +#include <linux/bpf.h>
> > +
> > +struct bpf_insn_array {
> > + struct bpf_map map;
> > + atomic_t used;
> > + long *ips;
> > + DECLARE_FLEX_ARRAY(struct bpf_insn_array_value, values);
> > +};
> > +
> > +#define cast_insn_array(MAP_PTR) \
> > + container_of((MAP_PTR), struct bpf_insn_array, map)
> > +
> > +#define INSN_DELETED ((u32)-1)
> > +
> > +static inline u32 insn_array_alloc_size(u32 max_entries)
> > +{
> > + const u32 base_size = sizeof(struct bpf_insn_array);
> > + const u32 entry_size = sizeof(struct bpf_insn_array_value);
> > +
> > + return base_size + max_entries * (entry_size + sizeof(long));
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> > +}
>
> Can this overflow u32? With entry_size=16 and sizeof(long)=8 on 64-bit,
> max_entries around 200 million would cause: 200M * 24 = 4.8B which wraps
> to around 504MB when cast to u32. The subsequent bpf_map_area_alloc()
> would then allocate only 504MB, but later accesses to values[150000000]
> would be out of bounds. Should this function return u64 instead, or
> should insn_array_alloc_check() validate an upper bound on max_entries?
>
> [ ... ]
Yes, it can now, thanks AI. (The latest verion removed the limit check
for the max_entires, set previously to 256.) I will convert types to
u64 in insn_array_alloc_size as all the call sites use u64 in any case.
> > +static int insn_array_alloc_check(union bpf_attr *attr)
> > +{
> > + u32 value_size = sizeof(struct bpf_insn_array_value);
> > +
> > + if (attr->max_entries == 0 || attr->key_size != 4 ||
> > + attr->value_size != value_size || attr->map_flags != 0)
> > + return -EINVAL;
> > +
> > + return 0;
> > +}
>
> [ ... ]
>
> > +void bpf_prog_update_insn_ptrs(struct bpf_prog *prog, u32 *offsets, void *image)
> > +{
> > + struct bpf_insn_array *insn_array;
> > + struct bpf_map *map;
> > + u32 xlated_off;
> > + int i, j;
> > +
> > + if (!offsets || !image)
> > + return;
> > +
> > + for (i = 0; i < prog->aux->used_map_cnt; i++) {
> > + map = prog->aux->used_maps[i];
> > + if (!is_insn_array(map))
> > + continue;
> > +
> > + insn_array = cast_insn_array(map);
> > + for (j = 0; j < map->max_entries; j++) {
> > + xlated_off = insn_array->values[j].xlated_off;
> > + if (xlated_off == INSN_DELETED)
> > + continue;
> > + if (xlated_off < prog->aux->subprog_start)
> > + continue;
> > + xlated_off -= prog->aux->subprog_start;
> > + if (xlated_off >= prog->len)
> > + continue;
> > +
> > + insn_array->values[j].jitted_off = offsets[xlated_off];
> > + insn_array->ips[j] = (long)(image + offsets[xlated_off]);
> > + }
> > + }
> > +}
>
> Does this handle programs with function calls correctly? When
> jit_subprogs() in the verifier splits a program with function calls, it
> creates separate bpf_prog structures for the main program and each
> subprogram. During JIT compilation, bpf_int_jit_compile() calls
> bpf_prog_update_insn_ptrs() for each subprogram separately.
>
> However, subprograms don't have used_maps copied to their aux structure
They have. However, this change is introduced in a later commit.
One thing the robot is right about is that it should have been done
as part of this patch. Will move these chunks here.
> (see jit_subprogs() in verifier.c). So when bpf_prog_update_insn_ptrs()
> is called on a subprogram, prog->aux->used_map_cnt is 0 and the loop
> doesn't execute.
>
> When called on the main program (func[0]), prog->len only covers the
> main program's instructions (not subprograms), so any tracked instruction
> with xlated_off >= prog->len (i.e., in a subprogram) hits the continue
> statement and doesn't get its ips[] field set.
>
> Then bpf_insn_array_ready() checks if ips[i] is NULL and returns -EFAULT,
> causing program load to fail. Should this function iterate through all
> func[] entries and handle each subprogram, or should the main program's
> used_maps be copied to subprograms so this works as-is?
> > diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
>
> [ ... ]
>
> > @@ -2853,6 +2854,23 @@ static int bpf_prog_verify_signature(struct bpf_prog *prog, union bpf_attr *attr
> > return err;
> > }
> >
> > +static int bpf_prog_mark_insn_arrays_ready(struct bpf_prog *prog)
> > +{
> > + int err;
> > + int i;
> > +
> > + for (i = 0; i < prog->aux->used_map_cnt; i++) {
> > + if (prog->aux->used_maps[i]->map_type != BPF_MAP_TYPE_INSN_ARRAY)
> > + continue;
> > +
> > + err = bpf_insn_array_ready(prog->aux->used_maps[i]);
> > + if (err)
> > + return err;
> > + }
> > +
> > + return 0;
> > +}
>
> [ ... ]
>
>
> ```
>
> ---
> 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
>
> In-Reply-To-Subject: `bpf, x86: add new map type: instructions array`
> CI run summary: https://github.com/kernel-patches/bpf/actions/runs/18995945884
next prev parent reply other threads:[~2025-11-01 16:26 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-01 11:07 [PATCH v9 bpf-next 00/11] BPF indirect jumps Anton Protopopov
2025-11-01 11:07 ` [PATCH v9 bpf-next 01/11] bpf, x86: add new map type: instructions array Anton Protopopov
2025-11-01 11:29 ` bot+bpf-ci
2025-11-01 16:32 ` Anton Protopopov [this message]
2025-11-01 11:07 ` [PATCH v9 bpf-next 02/11] selftests/bpf: add selftests for new insn_array map Anton Protopopov
2025-11-01 11:07 ` [PATCH v9 bpf-next 03/11] bpf: support instructions arrays with constants blinding Anton Protopopov
2025-11-01 11:07 ` [PATCH v9 bpf-next 04/11] selftests/bpf: test instructions arrays with blinding Anton Protopopov
2025-11-01 11:07 ` [PATCH v9 bpf-next 05/11] bpf, x86: allow indirect jumps to r8...r15 Anton Protopopov
2025-11-01 11:07 ` [PATCH v9 bpf-next 06/11] bpf, x86: add support for indirect jumps Anton Protopopov
2025-11-01 11:30 ` bot+bpf-ci
2025-11-01 17:27 ` Anton Protopopov
2025-11-01 11:07 ` [PATCH v9 bpf-next 07/11] bpf: disasm: add support for BPF_JMP|BPF_JA|BPF_X Anton Protopopov
2025-11-01 11:07 ` [PATCH v9 bpf-next 08/11] libbpf: support llvm-generated indirect jumps Anton Protopopov
2025-11-01 11:07 ` [PATCH v9 bpf-next 09/11] bpftool: Recognize insn_array map type Anton Protopopov
2025-11-01 11:07 ` [PATCH v9 bpf-next 10/11] selftests/bpf: add new verifier_gotox test Anton Protopopov
2025-11-01 11:07 ` [PATCH v9 bpf-next 11/11] selftests/bpf: add C-level selftests for indirect jumps Anton Protopopov
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=aQY2LvmzFfzNxm1Q@mail.gmail.com \
--to=a.s.protopopov@gmail.com \
--cc=andrii@kernel.org \
--cc=aspsk@isovalent.com \
--cc=ast@kernel.org \
--cc=bot+bpf-ci@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=clm@meta.com \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=ihor.solodrai@linux.dev \
--cc=martin.lau@kernel.org \
--cc=qmo@kernel.org \
--cc=yonghong.song@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.