mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: bot+bpf-ci@kernel.org
To: fadouse@pm.me,ast@kernel.org,daniel@iogearbox.net,andrii@kernel.org,eddyz87@gmail.com,memxor@gmail.com
Cc: martin.lau@linux.dev,song@kernel.org,yonghong.song@linux.dev,jolsa@kernel.org,emil@etsalapatis.com,ihor.solodrai@linux.dev,bpf@vger.kernel.org,linux-kernel@vger.kernel.org,stable@vger.kernel.org,ast@kernel.org,andrii@kernel.org,daniel@iogearbox.net,martin.lau@kernel.org,eddyz87@gmail.com,yonghong.song@linux.dev,mason@kernel.org,ihor.solodrai@linux.dev
Subject: Re: [PATCH bpf v2] bpf: bound arena allocations to the user VMA
Date: Sun, 22 Nov 2026 05:26:38 +0000 (UTC)	[thread overview]
Message-ID: <86a108a3441c8160bcf73fe92b8ff79dd6562d3313c80e65489607f3d737d50b@mail.kernel.org> (raw)
In-Reply-To: <20261122-b4-03-bpf-arena-vmalloc-oob-v2-5f6209c1a20e-v2-1-b2a9906e9676@pm.me>

[-- Attachment #1: Type: text/plain, Size: 4005 bytes --]

> bpf: bound arena allocations to the user VMA

TL;DR: Commit message page counts don't add up and its "can never be mapped
without wrapping" claim is questionable; the new -ERANGE check in
arena_adjust_tree() looks unreachable and its comment seems inaccurate.

> A program that first allocates 287 pages and
> then asks for 288 more receives pgoff = 287, uaddr32 = 0xfffff000,
> and arena_alloc_pages() maps 288 pages at kern_vm_start + 0xfffff000:
> one page inside the VMA, then the arena's upper guard pages, and then
> 258 pages into the vmalloc area past the end of the arena's own
> vmap area - installing PTEs into address space the arena does not own
> and returning a "success" pointer to the caller.
[ ... ]
> Fix this on both sides.  On the first mmap() of a map created without
> map_extra, mark [vma_pages, max_entries) as allocated in the range
> tree, because nothing outside the VMA can ever be freed or mapped
> without wrapping;

The page counts do not seem to add up here.  GUARD_SZ is
round_up(1 << 16, PAGE_SIZE << 1), so kern_vm_start is offset by 32 KiB
into the area and the upper guard above kern_vm_start + 4 GiB is 32 KiB,
which is 8 pages with 4 KiB pages.

Mapping 288 pages at kern_vm_start + 0xfffff000 would then be 1 page in
the 4 GiB band and 8 upper guard pages, leaving 279 pages past the end of
the arena's vm area, and 1 + 8 + 258 is 267, not 288.  Could the numbers
be corrected?

Also, is "nothing outside the VMA can ever be freed or mapped without
wrapping" accurate?  Pages outside the VMA can never be freed because
arena_free_pages() clips to user_vm_end, but they only wrap when the VMA
ends close to a 4 GiB boundary.  For a 4 GiB aligned user_vm_start,
uaddr32 = pgoff * PAGE_SIZE stays inside the arena's own kernel area.

> diff --git a/kernel/bpf/arena.c b/kernel/bpf/arena.c
> --- a/kernel/bpf/arena.c
> +++ b/kernel/bpf/arena.c

[ ... ]

> @@ -758,8 +787,19 @@ static int arena_adjust_tree(struct bpf_arena *arena, long uaddr, long page_cnt,
>  	ret = range_tree_find(&arena->rt, page_cnt);
>  	if (ret < 0)
>  		return ret;
>
> +	/*
> +	 * The range tree spans max_entries pages, but when map_extra was
> +	 * not given at arena creation time the user VMA, and hence the
> +	 * range whose lower 32 bits do not wrap past the 32-bit boundary,
> +	 * can be smaller.  A pgoff past the VMA would wrap uaddr32 past
> +	 * the 32-bit boundary, be mapped past the end of the arena's
> +	 * kernel vm area, and could never be freed, so reject it.
> +	 */
> +	if (ret > page_cnt_max - page_cnt)
> +		return -ERANGE;
> +
>  	*pgoff = ret;

This isn't a bug, but does this comment match the code after this patch?

The comment says "The range tree spans max_entries pages", but
arena_map_mmap() now clips the tree on the first mmap():

    ret = range_tree_clear(&arena->rt, vma_pages(vma),
                           map->max_entries - vma_pages(vma));

Since page_cnt_max is 0 until user_vm_start/user_vm_end are stored, and
arena_alloc_pages() returns early when page_cnt > page_cnt_max, the tree
already spans at most page_cnt_max pages whenever arena_adjust_tree()
runs.  That would make the -ERANGE branch unreachable today, with the
clip in arena_map_mmap() being what enforces the bound.

The claim that "a pgoff past the VMA would wrap uaddr32" also only holds
when the VMA ends near a 4 GiB boundary.  For a VMA starting at a 4 GiB
aligned address, uaddr32 = pgoff * PAGE_SIZE < 4 GiB for every pgoff <
max_entries, so those pages land inside the arena's own kernel area.

Could the comment say that the mmap-side clip enforces the bound and that
this check is only a backstop, and drop the universal "would wrap" claim?
Alternatively, could the check be removed if the clip alone is the
intended invariant?


---
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/44987264909

      reply	other threads:[~2026-11-22  5:26 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-22  4:46 YanXin Li via B4 Relay
2026-11-22  5:26 ` bot+bpf-ci [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=86a108a3441c8160bcf73fe92b8ff79dd6562d3313c80e65489607f3d737d50b@mail.kernel.org \
    --to=bot+bpf-ci@kernel.org \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=emil@etsalapatis.com \
    --cc=fadouse@pm.me \
    --cc=ihor.solodrai@linux.dev \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=martin.lau@kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=mason@kernel.org \
    --cc=memxor@gmail.com \
    --cc=song@kernel.org \
    --cc=stable@vger.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

all inboxes | Powered by JetHome®