From: Alexei Starovoitov <alexei.starovoitov@gmail.com>
To: Vlastimil Babka <vbabka@suse.cz>
Cc: bpf <bpf@vger.kernel.org>, Andrii Nakryiko <andrii@kernel.org>,
Kumar Kartikeya Dwivedi <memxor@gmail.com>,
Andrew Morton <akpm@linux-foundation.org>,
Peter Zijlstra <peterz@infradead.org>,
Sebastian Sewior <bigeasy@linutronix.de>,
Steven Rostedt <rostedt@goodmis.org>,
Hou Tao <houtao1@huawei.com>,
Johannes Weiner <hannes@cmpxchg.org>,
shakeel.butt@linux.dev, Michal Hocko <mhocko@suse.com>,
Matthew Wilcox <willy@infradead.org>,
Thomas Gleixner <tglx@linutronix.de>, Tejun Heo <tj@kernel.org>,
linux-mm <linux-mm@kvack.org>, Kernel Team <kernel-team@fb.com>
Subject: Re: [PATCH bpf-next v2 1/6] mm, bpf: Introduce __GFP_TRYLOCK for opportunistic page allocation
Date: Tue, 10 Dec 2024 14:42:09 -0800 [thread overview]
Message-ID: <CAADnVQJ4814Bq3bZXdgqSQ9kuJ7EohxaBBdfs+YxsYUqmCQsBg@mail.gmail.com> (raw)
In-Reply-To: <c5e2138c-f1a8-4091-bd60-ac7f704c6d13@suse.cz>
On Tue, Dec 10, 2024 at 10:39 AM Vlastimil Babka <vbabka@suse.cz> wrote:
>
> On 12/10/24 03:39, Alexei Starovoitov wrote:
> > From: Alexei Starovoitov <ast@kernel.org>
> >
> > Tracing BPF programs execute from tracepoints and kprobes where running
> > context is unknown, but they need to request additional memory.
> > The prior workarounds were using pre-allocated memory and BPF specific
> > freelists to satisfy such allocation requests. Instead, introduce
> > __GFP_TRYLOCK flag that makes page allocator accessible from any context.
> > It relies on percpu free list of pages that rmqueue_pcplist() should be
> > able to pop the page from. If it fails (due to IRQ re-entrancy or list
> > being empty) then try_alloc_pages() attempts to spin_trylock zone->lock
> > and refill percpu freelist as normal.
> > BPF program may execute with IRQs disabled and zone->lock is sleeping in RT,
> > so trylock is the only option.
> > In theory we can introduce percpu reentrance counter and increment it
> > every time spin_lock_irqsave(&zone->lock, flags) is used,
> > but we cannot rely on it. Even if this cpu is not in page_alloc path
> > the spin_lock_irqsave() is not safe, since BPF prog might be called
> > from tracepoint where preemption is disabled. So trylock only.
> >
> > Note, free_page and memcg are not taught about __GFP_TRYLOCK yet.
> > The support comes in the next patches.
> >
> > This is a first step towards supporting BPF requirements in SLUB
> > and getting rid of bpf_mem_alloc.
> > That goal was discussed at LSFMM: https://lwn.net/Articles/974138/
> >
> > Signed-off-by: Alexei Starovoitov <ast@kernel.org>
>
> I think there might be more non-try spin_locks reachable from page allocations:
>
> - in reserve_highatomic_pageblock() which I think is reachable unless this
> is limited to order-0
Good point. I missed this bit:
if (order > 0)
alloc_flags |= ALLOC_HIGHATOMIC;
In bpf use case it will be called with order == 0 only,
but it's better to fool proof it.
I will switch to:
__GFP_NOMEMALLOC | __GFP_TRYLOCK | __GFP_NOWARN | __GFP_ZERO | __GFP_ACCOUNT
> - try_to_accept_memory_one()
when I studied the code it looked to me that there should be no
unaccepted_pages.
I think you're saying that there could be unaccepted memory
from the previous allocation and trylock attempt just got unlucky
to reach that path?
What do you think of the following:
- cond_accept_memory(zone, order);
+ cond_accept_memory(zone, order, alloc_flags);
/*
* Detect whether the number of free pages is below high
@@ -7024,7 +7024,8 @@ static inline bool has_unaccepted_memory(void)
return static_branch_unlikely(&zones_with_unaccepted_pages);
}
-static bool cond_accept_memory(struct zone *zone, unsigned int order)
+static bool cond_accept_memory(struct zone *zone, unsigned int order,
+ unsigned int alloc_flags)
{
long to_accept;
bool ret = false;
@@ -7032,6 +7033,9 @@ static bool cond_accept_memory(struct zone
*zone, unsigned int order)
if (!has_unaccepted_memory())
return false;
+ if (unlikely(alloc_flags & ALLOC_TRYLOCK))
+ return false;
+
or is there a better approach?
Reading from current->flags the way Matthew proposed?
> - as part of post_alloc_hook() in set_page_owner(), stack depot might do
> raw_spin_lock_irqsave(), is that one ok?
Well, I looked at the stack depot and was tempted to add trylock
handling there, but it looked to be a bit dodgy in general and
I figured it should be done separately from this set.
Like:
if (unlikely(can_alloc && !READ_ONCE(new_pool))) {
page = alloc_pages(gfp_nested_mask(alloc_flags),
followed by:
if (in_nmi()) {
/* We can never allocate in NMI context. */
WARN_ON_ONCE(can_alloc);
that warn is too late. If we were in_nmi and called alloc_pages
the kernel might be misbehaving already.
>
> hope I didn't miss anything else especially in those other debugging hooks
> (KASAN etc)
I looked through them and could be missing something, of course.
kasan usage in alloc_page path seems fine.
But for slab I found kasan_quarantine logic which needs a special treatment.
Other slab debugging bits pose issues too.
The rough idea is to do kmalloc_nolock() / kfree_nolock() that
don't call into any pre/post hooks (including slab_free_hook,
slab_pre_alloc_hook).
kmalloc_nolock() will pretty much call __slab_alloc_node() directly
and do basic kasan poison stuff that needs no locks.
I will be going over all the paths again, of course.
Thanks for the reviews so far!
next prev parent reply other threads:[~2024-12-10 22:42 UTC|newest]
Thread overview: 50+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-12-10 2:39 [PATCH bpf-next v2 0/6] bpf, mm: Introduce __GFP_TRYLOCK Alexei Starovoitov
2024-12-10 2:39 ` [PATCH bpf-next v2 1/6] mm, bpf: Introduce __GFP_TRYLOCK for opportunistic page allocation Alexei Starovoitov
2024-12-10 5:31 ` Matthew Wilcox
2024-12-10 9:05 ` Michal Hocko
2024-12-10 20:25 ` Shakeel Butt
2024-12-11 10:08 ` Michal Hocko
2024-12-10 22:06 ` Alexei Starovoitov
2024-12-11 10:19 ` Michal Hocko
2024-12-12 15:07 ` Sebastian Sewior
2024-12-12 15:21 ` Michal Hocko
2024-12-12 15:35 ` Sebastian Sewior
2024-12-12 15:48 ` Steven Rostedt
2024-12-12 16:00 ` Sebastian Sewior
2024-12-13 17:44 ` Steven Rostedt
2024-12-13 18:44 ` Alexei Starovoitov
2024-12-13 18:57 ` Alexei Starovoitov
2024-12-13 20:09 ` Steven Rostedt
2024-12-13 21:00 ` Steven Rostedt
2024-12-13 22:02 ` Alexei Starovoitov
2024-12-12 21:57 ` Alexei Starovoitov
2024-12-10 21:42 ` Alexei Starovoitov
2024-12-10 9:01 ` Sebastian Andrzej Siewior
2024-12-10 21:53 ` Alexei Starovoitov
2024-12-11 8:38 ` Vlastimil Babka
2024-12-12 2:14 ` Alexei Starovoitov
2024-12-12 8:54 ` Vlastimil Babka
2024-12-10 18:39 ` Vlastimil Babka
2024-12-10 22:42 ` Alexei Starovoitov [this message]
2024-12-11 8:48 ` Vlastimil Babka
2024-12-10 2:39 ` [PATCH bpf-next v2 2/6] mm, bpf: Introduce free_pages_nolock() Alexei Starovoitov
2024-12-10 8:35 ` Sebastian Andrzej Siewior
2024-12-10 22:49 ` Alexei Starovoitov
2024-12-12 14:44 ` Sebastian Andrzej Siewior
2024-12-12 19:57 ` Alexei Starovoitov
2024-12-11 10:11 ` Vlastimil Babka
2024-12-12 1:43 ` Alexei Starovoitov
2024-12-10 2:39 ` [PATCH bpf-next v2 3/6] locking/local_lock: Introduce local_trylock_irqsave() Alexei Starovoitov
2024-12-11 10:53 ` Vlastimil Babka
2024-12-11 11:55 ` Vlastimil Babka
2024-12-12 2:49 ` Alexei Starovoitov
2024-12-12 9:15 ` Vlastimil Babka
2024-12-13 14:02 ` Vlastimil Babka
2024-12-12 15:15 ` Sebastian Andrzej Siewior
2024-12-12 19:59 ` Alexei Starovoitov
2024-12-10 2:39 ` [PATCH bpf-next v2 4/6] memcg: Add __GFP_TRYLOCK support Alexei Starovoitov
2024-12-11 23:47 ` kernel test robot
2024-12-10 2:39 ` [PATCH bpf-next v2 5/6] mm, bpf: Use __GFP_ACCOUNT in try_alloc_pages() Alexei Starovoitov
2024-12-11 12:05 ` Vlastimil Babka
2024-12-12 2:54 ` Alexei Starovoitov
2024-12-10 2:39 ` [PATCH bpf-next v2 6/6] bpf: Use try_alloc_pages() to allocate pages for bpf needs 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=CAADnVQJ4814Bq3bZXdgqSQ9kuJ7EohxaBBdfs+YxsYUqmCQsBg@mail.gmail.com \
--to=alexei.starovoitov@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=andrii@kernel.org \
--cc=bigeasy@linutronix.de \
--cc=bpf@vger.kernel.org \
--cc=hannes@cmpxchg.org \
--cc=houtao1@huawei.com \
--cc=kernel-team@fb.com \
--cc=linux-mm@kvack.org \
--cc=memxor@gmail.com \
--cc=mhocko@suse.com \
--cc=peterz@infradead.org \
--cc=rostedt@goodmis.org \
--cc=shakeel.butt@linux.dev \
--cc=tglx@linutronix.de \
--cc=tj@kernel.org \
--cc=vbabka@suse.cz \
--cc=willy@infradead.org \
/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