BPF List
 help / color / mirror / Atom feed
From: Mykyta Yatsenko <mykyta.yatsenko5@gmail.com>
To: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Cc: Kumar Kartikeya Dwivedi <memxor@gmail.com>,
	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 RFC v3 04/10] bpf: Add lock-free cell for NMI-safe async operations
Date: Fri, 9 Jan 2026 18:22:53 +0000	[thread overview]
Message-ID: <c5269858-7285-4e44-a5ef-72e69e9c00a2@gmail.com> (raw)
In-Reply-To: <CAEf4BzYY0s6yF8JACTENANzJXd6abZctiB1iP+jYARq_xPDm0A@mail.gmail.com>

On 1/9/26 01:18, Andrii Nakryiko wrote:
> On Wed, Jan 7, 2026 at 11:05 AM Mykyta Yatsenko
> <mykyta.yatsenko5@gmail.com> wrote:
>> On 1/7/26 18:30, Kumar Kartikeya Dwivedi wrote:
>>> On Wed, 7 Jan 2026 at 18:49, Mykyta Yatsenko <mykyta.yatsenko5@gmail.com> wrote:
>>>> From: Mykyta Yatsenko <yatsenko@meta.com>
>>>>
>>>> Introduce mpmc_cell, a lock-free cell primitive designed to support
>>>> concurrent writes to struct in NMI context (only one writer advances),
>>>> allowing readers to consume consistent snapshot.
>>>>
>>>> Implementation details:
>>>>    Double buffering allows writers run concurrently with readers (read
>>>>    from one cell, write to another)
>>>>
>>>>    The implementation uses a sequence-number-based protocol to enable
>>>>    exclusive writes.
>>>>     * Bit 0 of seq indicates an active writer
>>>>     * Bits 1+ form a generation counter
>>>>     * (seq & 2) >> 1 selects the read cell, write cell is opposite
>>>>     * Writers atomically set bit 0, write to the inactive cell, then
>>>>       increment seq to publish
>>>>     * Readers snapshot seq, read from the active cell, then validate
>>>>       that seq hasn't changed
>>>>
>>>> mpmc_cell expects users to pre-allocate double buffers.
>>>>
>>>> Key properties:
>>>>    * Writers never block (fail if lost the race to another writer)
>>>>    * Readers never block writers (double buffering), but may require
>>>>    retries if write updates the snapshot concurrently.
>>>>
>>>> This will be used by BPF timer and workqueue helpers to defer NMI-unsafe
>>>> operations (like hrtimer_start()) to irq_work effectively allowing BPF
>>>> programs to initiate timers and workqueues from NMI context.
>>>>
>>>> Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com>
>>>> ---
>>> We already have a dual-versioned concurrency control primitive in the
>>> kernel (seqcount_latch_t). I would just use that instead of
>>> reinventing it here. I don't see much of a difference except writer
>>> serialization, which can be done on top of it. If it was already
>>> considered and discarded for some reason, please add that reason to
>>> the commit message.
>> yes, multiple concurrent  writers support would is the main difference
>> between seqcount_latch_t and my implementation. I'll switch to
>> seqcount_latch_t and external synchronization for writers.
> One advantage of custom primitive is that we don't need a second
> atomic counter for writers. Here we combine the reader latch counter
> (it's just scaled 2x for custom implementation) and "writer is active"
> bit (even/odd counter).
>
> With potentially millions of timer activations per second for some
> extreme cases, would performance be enough reason to have custom
> "seqlock latch"? (I'm not sure myself, trying to get opinions)
>
Actually seqcount_latch_t variant may be faster (correct me if I'm wrong),
because mpmc_cell requires 2 lock prefixed instructions to enter the write
critical section and seqcount_latch_t just 1.

mpmc_cell:

if (1&atomic_fetch_or_acquire(1, &ctl->seq)) // first lock prefixed insn
return;
...
       atomic_fetch_add_release(1, &ctl->seq);     // second lock 
prefixed insn

seqcount_latch_t based:

     if (atomic_cmpxchg(&ctl->lock, 0, 1))        // first lock prefixed 
insn
         return;
write_seqcount_latch_begin(&ctl->seq);       // inc with barriers
     ...
     write_seqcount_latch(&ctl->seq);            // inc with barriers
     atomic_set(&ctl->lock, 0);            // plain mov on x86_64

Does it look right?
>>>>    [...]
>>>>


  reply	other threads:[~2026-01-09 18:22 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-07 17:49 [PATCH RFC v3 00/10] bpf: Avoid locks in bpf_timer and bpf_wq Mykyta Yatsenko
2026-01-07 17:49 ` [PATCH RFC v3 01/10] bpf: Refactor __bpf_async_set_callback() Mykyta Yatsenko
2026-01-07 18:22   ` bot+bpf-ci
2026-01-09 22:18     ` Andrii Nakryiko
2026-01-13 13:58       ` Mykyta Yatsenko
2026-01-13 16:14       ` Mykyta Yatsenko
2026-01-09 22:18   ` Andrii Nakryiko
2026-01-12  6:47   ` Kumar Kartikeya Dwivedi
2026-01-12  8:12     ` Kumar Kartikeya Dwivedi
2026-01-07 17:49 ` [PATCH RFC v3 02/10] bpf: Factor out timer deletion helper Mykyta Yatsenko
2026-01-12  6:51   ` Kumar Kartikeya Dwivedi
2026-01-07 17:49 ` [PATCH RFC v3 03/10] bpf: Simplify bpf_timer_cancel() Mykyta Yatsenko
2026-01-07 18:22   ` bot+bpf-ci
2026-01-09 22:19     ` Andrii Nakryiko
2026-01-12  7:29       ` Kumar Kartikeya Dwivedi
2026-01-07 17:49 ` [PATCH RFC v3 04/10] bpf: Add lock-free cell for NMI-safe async operations Mykyta Yatsenko
2026-01-07 18:08   ` bot+bpf-ci
2026-01-07 18:30   ` Kumar Kartikeya Dwivedi
2026-01-07 19:05     ` Mykyta Yatsenko
2026-01-09  1:18       ` Andrii Nakryiko
2026-01-09 18:22         ` Mykyta Yatsenko [this message]
2026-01-09 18:47           ` Andrii Nakryiko
2026-01-09 23:51             ` Alexei Starovoitov
2026-01-10  0:03               ` Andrii Nakryiko
2026-01-09 22:19   ` Andrii Nakryiko
2026-01-07 17:49 ` [PATCH RFC v3 05/10] bpf: Enable bpf timer and workqueue use in NMI Mykyta Yatsenko
2026-01-07 18:22   ` bot+bpf-ci
2026-01-09 22:19   ` Andrii Nakryiko
2026-01-14 14:53     ` Mykyta Yatsenko
2026-01-15 18:39       ` Andrii Nakryiko
2026-01-15 18:52         ` Mykyta Yatsenko
2026-01-12  8:10   ` Kumar Kartikeya Dwivedi
2026-01-07 17:49 ` [PATCH RFC v3 06/10] bpf: Add verifier support for bpf_timer argument in kfuncs Mykyta Yatsenko
2026-01-09 22:19   ` Andrii Nakryiko
2026-01-07 17:49 ` [PATCH RFC v3 07/10] bpf: Introduce bpf_timer_cancel_async() kfunc Mykyta Yatsenko
2026-01-09 22:19   ` Andrii Nakryiko
2026-01-07 17:49 ` [PATCH RFC v3 08/10] selftests/bpf: Refactor timer selftests Mykyta Yatsenko
2026-01-07 17:49 ` [PATCH RFC v3 09/10] selftests/bpf: Add stress test for timer async cancel Mykyta Yatsenko
2026-01-07 17:49 ` [PATCH RFC v3 10/10] selftests/bpf: Verify bpf_timer_cancel_async works Mykyta Yatsenko
2026-01-09 22:19   ` Andrii Nakryiko

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=c5269858-7285-4e44-a5ef-72e69e9c00a2@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