All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Emil Tsalapatis" <emil@etsalapatis.com>
To: "Alexei Starovoitov" <alexei.starovoitov@gmail.com>,
	"Emil Tsalapatis" <emil@etsalapatis.com>, <bpf@vger.kernel.org>
Cc: <andrii@kernel.org>, <eddyz87@gmail.com>, <memxor@gmail.com>,
	<daniel@iogearbox.net>,
	"Mykola Lysenko" <nickolay.lysenko@gmail.com>
Subject: Re: [PATCH bpf-next v4 6/8] bpf: Fix arena race between page free and alloc leading to incoherency
Date: Fri, 16 Oct 2026 19:19:42 +0000	[thread overview]
Message-ID: <DM6I7R3QQ1QO.269JGZBC9VTPW@etsalapatis.com> (raw)
In-Reply-To: <DM5W5UNBV54W.LJ1K9VUN2S3S@gmail.com>

On Fri Oct 16, 2026 at 2:02 AM UTC, Alexei Starovoitov wrote:
> On Wed, Oct 14, 2026 at 11:34 PM Emil Tsalapatis <emil@etsalapatis.com> wrote:
>> @@ -1063,23 +1147,51 @@ static void arena_free_worker(struct work_struct *work)
> [...]
>> +		unavail_node = range_tree_set_unavail(&arena->rt, pgoff, page_cnt);
>> +		if (!IS_ERR(unavail_node)) {
>> +			s->flags |= ARENA_FREE_SPAN_RESERVED;
>> +			goto clear_ptes;
>> +		}
>> +
>> +		ret = PTR_ERR(unavail_node);
>> +		/* Retry without starting teardown. */
>> +		if (ret == -EAGAIN || ret == -ENOMEM)
>> +			continue;
>
> v3 dropped the span here. v4 puts it back into free_spans and
> the worker does schedule_work() on itself until kmalloc_nolock()
> succeeds. It's __GFP_ACCOUNT and it cannot reclaim.
> At memory.max the arena cannot free its pages, because it cannot
> allocate a range node, and those pages are charged to the same memcg.
> The kworker burns cpu in the meantime.
> In arena_free_pages() -ENOMEM goes to defer: where kmalloc_nolock()
> of the span fails for the same reason and the pages stay mapped until
> map free. Same as v3.
> The free path shouldn't allocate before the ptes are cleared.

The next version drops the unavailable state, so the free no longer
allocates a range node before it clears the ptes. range_tree_clear()
only allocates when the whole range is free, and then there is nothing
to free, so the free is done. The worker no longer requeues itself
either. It waits for arena->lock and for a contended mmap_lock instead.

The one allocation left before clearing is the span for a deferred
free, when a sleepable free fails to trylock arena->lock or one of the
mmap locks (see my reply to patch 7 for the mmap locks). It uses
GFP_KERNEL_ACCOUNT | __GFP_RETRY_MAYFAIL, so it can reclaim. If it
still fails, the free has not touched anything yet. The pages stay
mapped for both BPF and user space and are freed with the map, as when
kmalloc_nolock() fails in the non-sleepable path today. Allocating
after clearing would be worse. The pages could not be freed then,
because user ptes may still map them and the page table walk in
arena_map_free() no longer finds them.

  reply	other threads:[~2026-10-16 19:19 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-14 23:34 [PATCH bpf-next v4 0/8] bpf: Fix arena memory incoherence Emil Tsalapatis
2026-10-14 23:34 ` [PATCH bpf-next v4 1/8] bpf: Use mapping tree to zap arena VMAs Emil Tsalapatis
2026-10-15  0:33   ` bot+bpf-ci
2026-10-15 15:02     ` Emil Tsalapatis
2026-10-16 15:07     ` David Windsor
2026-10-16  2:02   ` Alexei Starovoitov
2026-10-14 23:34 ` [PATCH bpf-next v4 2/8] bpf: Associate arena pages with their mapping Emil Tsalapatis
2026-10-15  0:01   ` sashiko-bot
2026-10-15 15:15     ` Emil Tsalapatis
2026-10-16  2:02   ` Alexei Starovoitov
2026-10-16 19:17     ` Emil Tsalapatis
2026-10-14 23:34 ` [PATCH bpf-next v4 3/8] bpf: arena: Stop deferred frees before map teardown Emil Tsalapatis
2026-10-14 23:34 ` [PATCH bpf-next v4 4/8] bpf: Update is_range_tree_set to work for consecutive ranges Emil Tsalapatis
2026-10-14 23:34 ` [PATCH bpf-next v4 5/8] bpf: Track availability information for ranges in range tree Emil Tsalapatis
2026-10-14 23:34 ` [PATCH bpf-next v4 6/8] bpf: Fix arena race between page free and alloc leading to incoherency Emil Tsalapatis
2026-10-16  2:02   ` Alexei Starovoitov
2026-10-16 19:19     ` Emil Tsalapatis [this message]
2026-10-14 23:34 ` [PATCH bpf-next v4 7/8] bpf: Synchronize arena page faults with page freeing Emil Tsalapatis
2026-10-15  0:33   ` bot+bpf-ci
2026-10-15 15:18     ` Emil Tsalapatis
2026-10-16  2:02   ` Alexei Starovoitov
2026-10-16 19:34     ` Emil Tsalapatis
2026-10-14 23:34 ` [PATCH bpf-next v4 8/8] selftests/bpf: Add arena allocation race tests Emil Tsalapatis
2026-10-14 23:52   ` sashiko-bot
2026-10-16  0:03 ` [PATCH bpf-next v4 0/8] bpf: Fix arena memory incoherence Emil Tsalapatis
2026-10-16  0:04   ` Emil Tsalapatis
2026-10-16  2:03 ` Alexei Starovoitov

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=DM6I7R3QQ1QO.269JGZBC9VTPW@etsalapatis.com \
    --to=emil@etsalapatis.com \
    --cc=alexei.starovoitov@gmail.com \
    --cc=andrii@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=memxor@gmail.com \
    --cc=nickolay.lysenko@gmail.com \
    /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.