public inbox for bpf@vger.kernel.org
 help / color / mirror / Atom feed
From: Mykyta Yatsenko <mykyta.yatsenko5@gmail.com>
To: Kumar Kartikeya Dwivedi <memxor@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, Mykyta Yatsenko <yatsenko@meta.com>
Subject: Re: [PATCH 2/2] bpf: Migrate dynptr file to kmalloc_nolock
Date: Fri, 27 Mar 2026 14:46:57 +0000	[thread overview]
Message-ID: <87cy0pib32.fsf@gmail.com> (raw)
In-Reply-To: <CAP01T74SDG+yAp5y3J7u_3=TtBporsykcnUCDnFsxKBVGTXeBQ@mail.gmail.com>

Kumar Kartikeya Dwivedi <memxor@gmail.com> writes:

> On Wed, 25 Mar 2026 at 22:12, 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_dynptr_file_impl, continuing the migration away from bpf_mem_alloc
>> now that kmalloc can be used from NMI context.
>>
>> freader_cleanup() runs before kfree_rcu() while the dynptr still holds
>> exclusive access. kfree_rcu() then defers the actual free until after
>> a grace period.
>>
>> Add struct rcu_head to bpf_dynptr_file_impl for kfree_rcu().
>> ---
>>  kernel/bpf/helpers.c | 9 ++++++---
>>  1 file changed, 6 insertions(+), 3 deletions(-)
>>
>> diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
>> index b197b6978f1a..b349c8a34e50 100644
>> --- a/kernel/bpf/helpers.c
>> +++ b/kernel/bpf/helpers.c
>> @@ -1736,7 +1736,10 @@ static const struct bpf_func_proto bpf_kptr_xchg_proto = {
>>  };
>>
>>  struct bpf_dynptr_file_impl {
>> -       struct freader freader;
>> +       union {
>> +               struct freader freader;
>> +               struct rcu_head rcu;
>> +       };
>
> Sorry, this is confusing to me. Why do we need RCU gp wait before freeing here?
> bpf_mem_free() didn't do any RCU gp before.
>
Double checked, I think you are right, there's no point to do
kfree_rcu() just kfree() should do, as concurrent access to dynptr
should not be possible and after discard() nothing should be able to
access it anyway. Thanks.
>>         /* 64 bit offset and size overriding 32 bit ones in bpf_dynptr_kern */
>>         u64 offset;
>>         u64 size;
>> @@ -4427,7 +4430,7 @@ static int make_file_dynptr(struct file *file, u32 flags, bool may_sleep,
>>                 return -EINVAL;
>>         }
>>
>> -       state = bpf_mem_alloc(&bpf_global_ma, sizeof(struct bpf_dynptr_file_impl));
>> +       state = kmalloc_nolock(sizeof(*state), 0, NUMA_NO_NODE);
>>         if (!state) {
>>                 bpf_dynptr_set_null(ptr);
>>                 return -ENOMEM;
>> @@ -4459,7 +4462,7 @@ __bpf_kfunc int bpf_dynptr_file_discard(struct bpf_dynptr *dynptr)
>>                 return 0;
>>
>>         freader_cleanup(&df->freader);
>> -       bpf_mem_free(&bpf_global_ma, df);
>> +       kfree_rcu(df, rcu);
>>         bpf_dynptr_set_null(ptr);
>>         return 0;
>>  }
>>
>> --
>> 2.52.0
>>

      reply	other threads:[~2026-03-27 14:46 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-25 21:11 [PATCH 0/2] bpf: Migrate bpf_task_work and file dynptr to kmalloc_nolock Mykyta Yatsenko
2026-03-25 21:11 ` [PATCH 1/2] bpf: Migrate bpf_task_work " Mykyta Yatsenko
2026-03-27  3:41   ` Kumar Kartikeya Dwivedi
2026-03-27 14:36     ` Mykyta Yatsenko
2026-03-25 21:11 ` [PATCH 2/2] bpf: Migrate dynptr file " Mykyta Yatsenko
2026-03-27  3:37   ` Kumar Kartikeya Dwivedi
2026-03-27 14:46     ` Mykyta Yatsenko [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=87cy0pib32.fsf@gmail.com \
    --to=mykyta.yatsenko5@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