bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Anton Protopopov <a.s.protopopov@gmail.com>
To: Eduard Zingerman <eddyz87@gmail.com>
Cc: bpf@vger.kernel.org, Alexei Starovoitov <ast@kernel.org>,
	Andrii Nakryiko <andrii@kernel.org>,
	Anton Protopopov <aspsk@isovalent.com>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Quentin Monnet <qmo@kernel.org>,
	Yonghong Song <yonghong.song@linux.dev>
Subject: Re: [PATCH v7 bpf-next 01/12] bpf, x86: add new map type: instructions array
Date: Tue, 28 Oct 2025 10:10:44 +0000	[thread overview]
Message-ID: <aQCWpEY6lF7x1aYp@mail.gmail.com> (raw)
In-Reply-To: <d2c4c93fcc4d7d6c8faef63e918ba4e625ae7b03.camel@gmail.com>

On 25/10/27 02:44PM, Eduard Zingerman wrote:
> On Sun, 2025-10-26 at 19:26 +0000, Anton Protopopov wrote:
> 
> [...]
> 
> > diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c
> > index d4c93d9e73e4..d8ee0c4d9af8 100644
> > --- a/arch/x86/net/bpf_jit_comp.c
> > +++ b/arch/x86/net/bpf_jit_comp.c
> > @@ -3648,6 +3648,22 @@ struct x64_jit_data {
> >  	struct jit_context ctx;
> >  };
> >  
> > +struct insn_ptrs_data {
> > +	int *addrs;
> > +	u8 *image;
> > +};
> > +
> > +static void update_insn_ptr(void *jit_priv, u32 xlated_off, u32 *jitted_off, long *ip)
> > +{
> > +	struct insn_ptrs_data *data = jit_priv;
> > +
> > +	if (!data->addrs || !data->image || !jitted_off || !ip)
> > +		return;
> > +
> > +	*jitted_off = data->addrs[xlated_off];
> > +	*ip = (long)(data->image + *jitted_off);
> > +}
> > +
> >  #define MAX_PASSES 20
> >  #define PADDING_PASSES (MAX_PASSES - 5)
> >  
> > @@ -3658,6 +3674,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
> >  	struct bpf_prog *tmp, *orig_prog = prog;
> >  	void __percpu *priv_stack_ptr = NULL;
> >  	struct x64_jit_data *jit_data;
> > +	struct insn_ptrs_data insn_ptrs_data;
> >  	int priv_stack_alloc_sz;
> >  	int proglen, oldproglen = 0;
> >  	struct jit_context ctx = {};
> > @@ -3827,6 +3844,12 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
> >  			jit_data->header = header;
> >  			jit_data->rw_header = rw_header;
> >  		}
> > +
> > +		/* jit_data may not contain proper info, copy the required fields */
> > +		insn_ptrs_data.addrs = addrs;
> > +		insn_ptrs_data.image = image;
> > +		bpf_prog_update_insn_ptrs(prog, &insn_ptrs_data, update_insn_ptr);
> > +
> >  		/*
> >  		 * ctx.prog_offset is used when CFI preambles put code *before*
> >  		 * the function. See emit_cfi(). For FineIBT specifically this code
> 
> [...]
> 
> > diff --git a/kernel/bpf/bpf_insn_array.c b/kernel/bpf/bpf_insn_array.c
> > new file mode 100644
> > index 000000000000..f9f875ee2027
> > +++ b/kernel/bpf/bpf_insn_array.c
> 
> [...]
> 
> > +void bpf_prog_update_insn_ptrs(struct bpf_prog *prog, void *jit_priv,
> > +			       update_insn_ptr_func_t update_insn_ptr)
> > +{
> 
> Nit: I think the control flow becomes a bit convoluted with this
>      function pointer. Wdyt about changing the signature to:
> 
>        void bpf_prog_update_insn_ptrs(struct bpf_prog *prog,
>                                       u32 *offsets /* maps xlated_off to offset in image */,
>                                       void *image)
> 
>      x86 jit provides this info, it looks like arm64 and riscv jits do too
>      (arch/arm64/net/bpf_jit_comp.c:jit_ctx->offset field,
>       arch/riscv/net/bpf_jit.h:rv_jit_context->offset).
>      So, seem to be a reasonable assumption.
> 
>      Wdyt?

I did it a bit more abstract variant right away because in future
(read "static keys") there will be more data passed around.  I will
switch to your variant, and then, once I follow up with static keys,
it can be generalized, if needed.

> > +	struct bpf_insn_array *insn_array;
> > +	struct bpf_map *map;
> > +	u32 xlated_off;
> > +	int i, j;
> > +
> > +	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;
> > +
> > +			update_insn_ptr(jit_priv, xlated_off,
> > +					&insn_array->values[j].jitted_off,
> > +					&insn_array->ips[j]);
> > +		}
> > +	}
> > +}
> 
> [...]

  reply	other threads:[~2025-10-28 10:04 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-26 19:26 [PATCH v7 bpf-next 00/12] BPF indirect jumps Anton Protopopov
2025-10-26 19:26 ` [PATCH v7 bpf-next 01/12] bpf, x86: add new map type: instructions array Anton Protopopov
2025-10-26 20:12   ` Anton Protopopov
2025-10-26 22:34   ` kernel test robot
2025-10-26 22:59   ` kernel test robot
2025-10-27 21:44   ` Eduard Zingerman
2025-10-28 10:10     ` Anton Protopopov [this message]
2025-10-29 21:54   ` Eduard Zingerman
2025-10-26 19:26 ` [PATCH v7 bpf-next 02/12] selftests/bpf: add selftests for new insn_array map Anton Protopopov
2025-10-26 19:27 ` [PATCH v7 bpf-next 03/12] bpf: support instructions arrays with constants blinding Anton Protopopov
2025-10-26 19:27 ` [PATCH v7 bpf-next 04/12] selftests/bpf: test instructions arrays with blinding Anton Protopopov
2025-10-26 19:27 ` [PATCH v7 bpf-next 05/12] bpf, x86: allow indirect jumps to r8...r15 Anton Protopopov
2025-10-26 19:27 ` [PATCH v7 bpf-next 06/12] bpf, x86: add support for indirect jumps Anton Protopopov
2025-10-26 20:41   ` Anton Protopopov
2025-10-26 19:27 ` [PATCH v7 bpf-next 07/12] bpf: disasm: add support for BPF_JMP|BPF_JA|BPF_X Anton Protopopov
2025-10-26 19:27 ` [PATCH v7 bpf-next 08/12] bpf, docs: do not state that indirect jumps are not supported Anton Protopopov
2025-10-27  6:30   ` Anton Protopopov
2025-10-26 19:27 ` [PATCH v7 bpf-next 09/12] libbpf: support llvm-generated indirect jumps Anton Protopopov
2025-10-26 20:15   ` Anton Protopopov
2025-10-27 22:09   ` Eduard Zingerman
2025-10-27 22:38   ` Eduard Zingerman
2025-10-27 22:59     ` Eduard Zingerman
2025-10-28 11:36       ` Anton Protopopov
2025-10-28 11:42       ` Anton Protopopov
2025-10-26 19:27 ` [PATCH v7 bpf-next 10/12] bpftool: Recognize insn_array map type Anton Protopopov
2025-10-26 19:27 ` [PATCH v7 bpf-next 11/12] selftests/bpf: add new verifier_gotox test Anton Protopopov
2025-10-26 19:27 ` [PATCH v7 bpf-next 12/12] selftests/bpf: add C-level selftests for indirect jumps Anton Protopopov
2025-10-27 23:25   ` Eduard Zingerman
2025-10-28 10:59     ` 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=aQCWpEY6lF7x1aYp@mail.gmail.com \
    --to=a.s.protopopov@gmail.com \
    --cc=andrii@kernel.org \
    --cc=aspsk@isovalent.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).