From: Song Liu <song@kernel.org>
To: Mike Rapoport <rppt@kernel.org>
Cc: Alexei Starovoitov <ast@kernel.org>,
Andrii Nakryiko <andrii@kernel.org>,
Andy Lutomirski <luto@kernel.org>,
Borislav Petkov <bp@alien8.de>,
Daniel Borkmann <daniel@iogearbox.net>,
Dave Hansen <dave.hansen@linux.intel.com>,
Eduard Zingerman <eddyz87@gmail.com>,
Ingo Molnar <mingo@redhat.com>,
Kumar Kartikeya Dwivedi <memxor@gmail.com>,
Peter Zijlstra <peterz@infradead.org>,
Thomas Gleixner <tglx@kernel.org>,
Emil Tsalapatis <emil@etsalapatis.com>,
Jiri Olsa <jolsa@kernel.org>,
John Fastabend <john.fastabend@gmail.com>,
Martin KaFai Lau <martin.lau@linux.dev>,
"H. Peter Anvin" <hpa@zytor.com>,
Yonghong Song <yonghong.song@linux.dev>,
bpf@vger.kernel.org, linux-kernel@vger.kernel.org,
x86@kernel.org
Subject: Re: [PATCH bpf-next v3 0/5] bpf, x86: enable EXECMEM_ROX_CACHE for BPF allocations
Date: Fri, 17 Jul 2026 10:50:09 -0700 [thread overview]
Message-ID: <CAPhsuW4dt6YdNnce4wfJkVuuP339sh=0wCGwBG2_Nc2BYu4CSQ@mail.gmail.com> (raw)
In-Reply-To: <aln1_kGygUqdtlAt@kernel.org>
On Fri, Jul 17, 2026 at 2:29 AM Mike Rapoport <rppt@kernel.org> wrote:
[...]
> > >
> > > "It works, don't touch"? ;-)
> > >
> > > We can add another layer for sub-page allocations to execmem.
> >
> > Sub-page allocation is not a hard requirement here. Using 4kB for
> > each small BPF program isn't too bad. We added bpf_prog_pack to
> > avoid fragmentation of direct map page table entry (caused by W^X
> > requirement). If execmem allocator reserves large enough ROX
> > memory (with PMD page table entries) and reuses them properly,
> > we shouldn't see page table fragmentation getting worse over time.
> > Then, we can use 4kB granularity allocation for BPF programs. (I am
> > not sure about 64kB pages..).
>
> execmem uses PMD_SIZE pages for ROX cache. Since right now it's only
> supported on x86, the issue with oversized large page with 64k base pages
> didn't come up yet.
With 64kB page size, PMD_SIZE is probably too big for execmem.
> > > Since BPF is the only user the easiest would be just to move prog_pack
> > > logic from BPF to execmem and call it a day.
> >
> > If we move to bigger page sizes, say 64kB, there will be other
> > users that would benefit from sub page allocation, right?
>
> Maybe modules could, don't know TBH.
> There is another caveat with sub-page allocations: they must preserve ROX
> and no subsystem except BPF can deal with ROX-only allocations and writable
> copies.
I think ftrace and kprobe could be good users?
> > > Another option is to add a slab-like layer for sub-page allocations to
> > > execmem. This is more complex but it would allow to get rid of the rigid
> > > BPF_PROG_CHUNK_SIZE.
> > >
> > > Maybe it would be also possible to teach SLUB to use execmem_alloc()
> > > instead of alloc_pages() but that's surely the most far fetched one :)
> >
> > I was thinking some rb-tree algorithm might be useful here,
> > something similar to vmap.
>
> Could be that or maple_tree especially considering there's an active
> discussion about converting vmalloc to maple_tree as well:
>
> https://lore.kernel.org/linux-mm/20260613-vmalloc_maple-v1-0-0aa740bb944b@oss.qualcomm.com/
>
> But the choice of allocation algorithm is anyway secondary to the decision
> whether execmem should have the sub-page allocator.
>
> If you say that a page per BPF program is fine, than maybe we can just rip
> off bpf_prog_pack_alloc().
> If BPF still needs to maintain the ability to allocate in smaller chunks,
> then maybe the best way is to keep bpf_prog_pack_alloc() as is because I
> don't foresee other users for small page allocations any time soon.
>
> > > And since we are talking about bpf_prog_pack_alloc(), why
> > > BPF_PROG_PACK_SIZE accounts for num_possible_nodes():
> > >
> > > #define BPF_PROG_PACK_SIZE (SZ_2M * num_possible_nodes())
> > >
> > > Is it an elaborate choice or it was picked to work around older
> > > vmalloc_huge() limitations?
> >
> > It is a bit complicated. The goal is to get PMDs for prog_pack.
> > We can adjust this if vmalloc_huge() changes after that.
>
> Since commit c82be0be9576 ("mm: vmalloc: don't account for number of nodes
> for HUGE_VMAP allocations") vmalloc_huge() is fine with PMD_SIZE regardless
> of number of nodes.
Before execmem can handle sub page allocations, how about we
send allocations that are bigger than page size directly to execmem,
and let bpf_prog_pack handle sub page allocations. Then,
BPF_PROG_PACK_SIZE will be PAGE_SIZE. This should be a net
win for x86_64. Other archs will be the same. WDYT?
Thanks,
Song
prev parent reply other threads:[~2026-07-17 17:50 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-16 7:51 Mike Rapoport (Microsoft)
2026-07-16 7:51 ` [PATCH bpf-next v3 1/5] bpf: dispatcher: allocate bpf_dispatcher->rw_image with vzalloc() Mike Rapoport (Microsoft)
2026-07-16 8:52 ` bot+bpf-ci
2026-07-16 9:27 ` Mike Rapoport
2026-07-16 9:49 ` Kumar Kartikeya Dwivedi
2026-07-16 7:51 ` [PATCH bpf-next v3 2/5] bpf: drop __weak from bpf_jit_alloc_exec() and bpf_jit_free_exec() Mike Rapoport (Microsoft)
2026-07-16 7:51 ` [PATCH bpf-next v3 3/5] bpf: alloc_prog_pack(): skip ROX management for already ROX memory Mike Rapoport (Microsoft)
2026-07-16 7:51 ` [PATCH bpf-next v3 4/5] bpf, x86: make sure allocation in arch_bpf_trampoline_size() is writable Mike Rapoport (Microsoft)
2026-07-16 7:51 ` [PATCH bpf-next v3 5/5] x86/bpf: enable EXECMEM_ROX_CACHE for BPF allocations Mike Rapoport (Microsoft)
2026-07-17 0:00 ` [PATCH bpf-next v3 0/5] bpf, x86: " Song Liu
2026-07-17 6:41 ` Mike Rapoport
2026-07-17 7:27 ` Song Liu
2026-07-17 9:29 ` Mike Rapoport
2026-07-17 17:50 ` Song Liu [this message]
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='CAPhsuW4dt6YdNnce4wfJkVuuP339sh=0wCGwBG2_Nc2BYu4CSQ@mail.gmail.com' \
--to=song@kernel.org \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bp@alien8.de \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=dave.hansen@linux.intel.com \
--cc=eddyz87@gmail.com \
--cc=emil@etsalapatis.com \
--cc=hpa@zytor.com \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=luto@kernel.org \
--cc=martin.lau@linux.dev \
--cc=memxor@gmail.com \
--cc=mingo@redhat.com \
--cc=peterz@infradead.org \
--cc=rppt@kernel.org \
--cc=tglx@kernel.org \
--cc=x86@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