BPF List
 help / color / mirror / Atom feed
From: Eduard Zingerman <eddyz87@gmail.com>
To: Emil Tsalapatis <emil@etsalapatis.com>
Cc: bpf@vger.kernel.org, ast@kernel.org, daniel@iogearbox.net,
	 john.fastabend@gmail.com, memxor@gmail.com, andrii@kernel.org,
	 yonghong.song@linux.dev
Subject: Re: [PATCH v2 3/4] libbpf: offset global arena data into the arena if possible
Date: Mon, 01 Dec 2025 14:35:23 -0800	[thread overview]
Message-ID: <9a224465dc5cf8642673e633885057bb61b8fd31.camel@gmail.com> (raw)
In-Reply-To: <CABFh=a6sNSrYnpmhSBjBxO9g8oLk3kY4avQ6hwoNzmX4b7aKgA@mail.gmail.com>

On Mon, 2025-12-01 at 13:34 -0500, Emil Tsalapatis wrote:

[...]

> > For (a), is there a way to move an address of first valid mmaped page
> > (from BPF perspective) w/o physically allocating the first page?
> > 
> 
> IIUC what you mean:
> 
> This is what this change combined with bpf_alloc_reserve_pages() amounts to.
> First, we adjust all the symbol addresses into the mapping to move the address
> out of the zero the page. Then, we call the existing bpf_alloc_reserve_pages()
> call to prevent the first page from ever being physically allocated.

libbpf.c:bpf_object__create_maps() allocates arena memory region as
follows:

  map->mmaped = mmap(addr: (void *)(long)map->map_extra, ...);

Where 'map_extra' comes from fill_map_from_def() / parse_btf_map_def().
The latter even provides a flag:

  map_def->parts |= MAP_DEF_MAP_EXTRA;

On the kernel side arena.c:arena_map_alloc() uses this 'map_extra' as
a starting arena address:

  arena->user_vm_start = attr->map_extra;

arena.c:arena_alloc_pages() uses 'user_vm_start' as follows:

  uaddr32 = (u32)(arena->user_vm_start + pgoff * PAGE_SIZE);
  ...
  return clear_lo32(val: arena->user_vm_start) + uaddr32;

Returned value is what BPF program sees as an address, right?
Is there a way to tweak arena_alloc_pages() implementation or arena
creation at libbpf side, such that arena_alloc_pages() never returns
value smaller then 16 * PAGE_SIZE?
E.g. by tweaking the attr->map_extra value in
bpf_object__create_maps() if it was not specified directly by user?

> Alternatively, if you maybe mean transparently add an offset to every
> arena address
>  in xlated/jitted code: That would also adjust the NULL pointers we're
> trying to catch
> to point to the first valid page we can allocate. In general, I don't
> think it's possible
> to do this in the verifier/JIT because this change depends on adjusting only
> the arena globals, and that is best done at load time.

For sure, it does not make sense to do it on verifier side.
It might even be impossible in general case.

  reply	other threads:[~2025-12-01 22:35 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-18  3:00 [PATCH v2 0/4] libbpf: move arena variables out of the zero page Emil Tsalapatis
2025-11-18  3:00 ` [PATCH v2 1/4] selftests/bpf: explicitly account for globals in verifier_arena_large Emil Tsalapatis
2025-11-22  1:44   ` Eduard Zingerman
2025-11-18  3:00 ` [PATCH v2 2/4] libbpf: add stub for offset-related skeleton padding Emil Tsalapatis
2025-11-18  3:00 ` [PATCH v2 3/4] libbpf: offset global arena data into the arena if possible Emil Tsalapatis
2025-11-22  3:17   ` Eduard Zingerman
2025-12-01 18:34     ` Emil Tsalapatis
2025-12-01 22:35       ` Eduard Zingerman [this message]
2025-12-03 16:07         ` Emil Tsalapatis
2025-11-25 22:11   ` Andrii Nakryiko
2025-12-01 20:41     ` Emil Tsalapatis
2025-12-01 22:45       ` Andrii Nakryiko
2025-12-03 16:13         ` Emil Tsalapatis
2025-11-18  3:00 ` [PATCH v2 4/4] selftests/bpf: add tests for the arena offset of globals Emil Tsalapatis

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=9a224465dc5cf8642673e633885057bb61b8fd31.camel@gmail.com \
    --to=eddyz87@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=emil@etsalapatis.com \
    --cc=john.fastabend@gmail.com \
    --cc=memxor@gmail.com \
    --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