From: Mykyta Yatsenko <mykyta.yatsenko5@gmail.com>
To: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Cc: bpf@vger.kernel.org, ast@kernel.org, andrii@kernel.org,
daniel@iogearbox.net, kafai@meta.com, kernel-team@meta.com,
eddyz87@gmail.com, memxor@gmail.com,
Mykyta Yatsenko <yatsenko@meta.com>
Subject: Re: [PATCH bpf-next v2 1/2] bpf: Migrate bpf_task_work to kmalloc_nolock
Date: Tue, 31 Mar 2026 11:29:09 +0100 [thread overview]
Message-ID: <cb4c2d10-8131-4166-92aa-882a182160d8@gmail.com> (raw)
In-Reply-To: <CAEf4BzahW744tL+0DvvB_4nhstRsgMui0xEX4+obQ4kYo77YyQ@mail.gmail.com>
On 3/31/26 1:00 AM, Andrii Nakryiko wrote:
> On Mon, Mar 30, 2026 at 3:28 PM Mykyta Yatsenko
> <mykyta.yatsenko5@gmail.com> wrote:
>>
>> From: Mykyta Yatsenko <yatsenko@meta.com>
>>
>> Replace bpf_mem_alloc/bpf_mem_free with
>> kmalloc_nolock/kfree_rcu for bpf_task_work_ctx.
>>
>> Replace guard(rcu_tasks_trace)() with guard(rcu)() in
>> bpf_task_work_irq(). The function only accesses ctx struct members
>> (not map values), so tasks trace protection is not needed - regular
>> RCU is sufficient since ctx is freed via kfree_rcu. The guard in
>> bpf_task_work_callback() remains as tasks trace since it accesses map
>> values from process context.
>
> I didn't quite get if this change was necessary for correctness or
> it's just an optimization?
>
Correctness - ctx is freed via kfree_rcu(), so we need to hold rcu read
lock when we pass refcnt to the task_work_add() callback. It worked
before on tasks trace rcu because bpf_mem_alloc() used it (with normal
rcu chaining) before freeing the ctx.
>>
>> Sleepable BPF programs hold rcu_read_lock_trace but not
>> regular rcu_read_lock. Since kfree_rcu
>> waits for a regular RCU grace period, the ctx memory can be freed
>> while a sleepable program is still running. Add scoped_guard(rcu)
>> around the pointer read and refcount tryget in
>> bpf_task_work_acquire_ctx to close this race window.
>>
>> Since kfree_rcu uses call_rcu internally which is not safe from
>> NMI context, defer destruction via irq_work when irqs are disabled.
>>
>> For the lost-cmpxchg path the ctx was never published, so
>> kfree_nolock is safe.
>>
>> Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com>
>> ---
>> kernel/bpf/helpers.c | 56 ++++++++++++++++++++++++++++++++++------------------
>> 1 file changed, 37 insertions(+), 19 deletions(-)
>>
>
> [...]
next prev parent reply other threads:[~2026-03-31 10:29 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-30 22:27 [PATCH bpf-next v2 0/2] bpf: Migrate bpf_task_work and file dynptr to kmalloc_nolock Mykyta Yatsenko
2026-03-30 22:27 ` [PATCH bpf-next v2 1/2] bpf: Migrate bpf_task_work " Mykyta Yatsenko
2026-03-31 0:00 ` Andrii Nakryiko
2026-03-31 10:29 ` Mykyta Yatsenko [this message]
2026-03-31 0:58 ` Kumar Kartikeya Dwivedi
2026-03-30 22:27 ` [PATCH bpf-next v2 2/2] bpf: Migrate dynptr file " Mykyta Yatsenko
2026-03-31 0:01 ` Andrii Nakryiko
2026-03-31 0:58 ` Kumar Kartikeya Dwivedi
2026-04-02 16:40 ` [PATCH bpf-next v2 0/2] bpf: Migrate bpf_task_work and file dynptr " patchwork-bot+netdevbpf
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=cb4c2d10-8131-4166-92aa-882a182160d8@gmail.com \
--to=mykyta.yatsenko5@gmail.com \
--cc=andrii.nakryiko@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eddyz87@gmail.com \
--cc=kafai@meta.com \
--cc=kernel-team@meta.com \
--cc=memxor@gmail.com \
--cc=yatsenko@meta.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox