BPF List
 help / color / mirror / Atom feed
From: Alexei Starovoitov <alexei.starovoitov@gmail.com>
To: bpf@vger.kernel.org
Cc: daniel@iogearbox.net, andrii@kernel.org, martin.lau@kernel.org,
	memxor@gmail.com, mykyta.yatsenko5@gmail.com, kernel-team@fb.com
Subject: [PATCH v9 bpf-next 0/9] bpf: Avoid locks in bpf_timer and bpf_wq
Date: Sat, 31 Jan 2026 18:53:54 -0800	[thread overview]
Message-ID: <20260201025403.66625-1-alexei.starovoitov@gmail.com> (raw)

From: Alexei Starovoitov <ast@kernel.org>

This series reworks implementation of BPF timer and workqueue APIs to
make them usable from any context.

Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com>

Changes in v9:
- Different approach for patches 1 and 3:
- s/EBUSY/ENOENT/ when refcnt==0 to match existing
- drop latch, use refcnt and kmalloc_nolock() instead
- address race between timer/wq_start and delete_elem, add a test
- Link to v8: https://lore.kernel.org/bpf/20260127-timer_nolock-v8-0-5a29a9571059@meta.com/

Changes in v8:
- Return -EBUSY in bpf_async_read_op() if last_seq is failed to be set
- In bpf_async_cancel_and_free() drop bpf_async_cb ref after calling bpf_async_process()
- Link to v7: https://lore.kernel.org/r/20260122-timer_nolock-v7-0-04a45c55c2e2@meta.com

Changes in v7:
- Addressed Andrii's review points from the previous version - nothing
  very significang.
- Added NMI stress tests for bpf_timer - hit few verifier failing checks
  and removed them.
- Address sparse warning in the bpf_async_update_prog_callback()
- Link to v6: https://lore.kernel.org/r/20260120-timer_nolock-v6-0-670ffdd787b4@meta.com

Changes in v6:
- Reworked destruction and refcnt use:
  - On cancel_and_free() set last_seq to BPF_ASYNC_DESTROY value, drop
    map's reference
  - In irq work callback, atomically switch DESTROY to DESTROYED, cancel
    timer/wq
  - Free bpf_async_cb on refcnt going to 0.
- Link to v5: https://lore.kernel.org/r/20260115-timer_nolock-v5-0-15e3aef2703d@meta.com

Changes in v5:
- Extracted lock-free algorithm for updating cb->prog and
cb->callback_fn into a function bpf_async_update_prog_callback(),
added a new commit and introduces this function and uses it in
__bpf_async_set_callback(), bpf_timer_cancel() and
bpf_async_cancel_and_free().
This allows to move the change into the separate commit without breaking
correctness.
- Handle NULL prog in bpf_async_update_prog_callback().
- Link to v4: https://lore.kernel.org/r/20260114-timer_nolock-v4-0-fa6355f51fa7@meta.com

Changes in v4:
- Handle irq_work_queue failures in both schedule and cancel_and_free
paths: introduced bpf_async_refcnt_dec_cleanup() that decrements refcnt
and makes sure if last reference is put, there is at least one irq_work
scheduled to execute final cleanup.
- Additional refcnt inc/dec in set_callback() + rcu lock to make sure
cleanup is not running at the same time as set_callback().
- Added READ_ONCE where it was needed.
- Squash 'bpf: Refactor __bpf_async_set_callback()' commit into 'bpf:
Add lock-free cell for NMI-safe
async operations'
- Removed mpmc_cell, use seqcount_latch_t instead.
- Link to v3: https://lore.kernel.org/r/20260107-timer_nolock-v3-0-740d3ec3e5f9@meta.com

Changes in v3:
- Major rework
- Introduce mpmc_cell, allowing concurrent writes and reads
- Implement irq_work deferring
- Adding selftests
- Introduces bpf_timer_cancel_async kfunc
- Link to v2: https://lore.kernel.org/r/20251105-timer_nolock-v2-0-32698db08bfa@meta.com

Changes in v2:
- Move refcnt initialization and put (from cancel_and_free())
from patch 5 into the patch 4, so that patch 4 has more clear and full
implementation and use of refcnt
- Link to v1: https://lore.kernel.org/r/20251031-timer_nolock-v1-0-b064ae403bfb@meta.com

Alexei Starovoitov (3):
  bpf: Enable bpf_timer and bpf_wq in any context
  bpf: Introduce bpf_timer_cancel_async() kfunc
  selftests/bpf: Add a test to stress bpf_timer_start and map_delete
    race

Mykyta Yatsenko (6):
  bpf: Add verifier support for bpf_timer argument in kfuncs
  selftests/bpf: Refactor timer selftests
  selftests/bpf: Add stress test for timer async cancel
  selftests/bpf: Verify bpf_timer_cancel_async works
  selftests/bpf: Add timer stress test in NMI context
  selftests/bpf: Removed obsolete tests

 kernel/bpf/helpers.c                          | 456 +++++++++++-------
 kernel/bpf/verifier.c                         |  55 ++-
 .../testing/selftests/bpf/prog_tests/timer.c  | 250 +++++++++-
 .../bpf/prog_tests/timer_start_delete_race.c  | 137 ++++++
 tools/testing/selftests/bpf/progs/timer.c     | 118 ++++-
 .../bpf/progs/timer_start_delete_race.c       |  66 +++
 .../bpf/progs/verifier_helper_restricted.c    | 111 -----
 7 files changed, 851 insertions(+), 342 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/timer_start_delete_race.c
 create mode 100644 tools/testing/selftests/bpf/progs/timer_start_delete_race.c

-- 
2.47.3


             reply	other threads:[~2026-02-01  2:54 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-01  2:53 Alexei Starovoitov [this message]
2026-02-01  2:53 ` [PATCH v9 bpf-next 1/9] bpf: Enable bpf_timer and bpf_wq in any context Alexei Starovoitov
2026-02-02 13:36   ` Mykyta Yatsenko
2026-02-02 17:29     ` Alexei Starovoitov
2026-02-03 22:14   ` Kumar Kartikeya Dwivedi
2026-02-03 23:53   ` Andrii Nakryiko
2026-02-04  0:32     ` Alexei Starovoitov
2026-02-04  0:53       ` Andrii Nakryiko
2026-02-04  0:56         ` Andrii Nakryiko
2026-02-01  2:53 ` [PATCH v9 bpf-next 2/9] bpf: Add verifier support for bpf_timer argument in kfuncs Alexei Starovoitov
2026-02-01  3:15   ` bot+bpf-ci
2026-02-01  2:53 ` [PATCH v9 bpf-next 3/9] bpf: Introduce bpf_timer_cancel_async() kfunc Alexei Starovoitov
2026-02-01  2:53 ` [PATCH v9 bpf-next 4/9] selftests/bpf: Refactor timer selftests Alexei Starovoitov
2026-02-01  2:53 ` [PATCH v9 bpf-next 5/9] selftests/bpf: Add stress test for timer async cancel Alexei Starovoitov
2026-02-01  2:54 ` [PATCH v9 bpf-next 6/9] selftests/bpf: Verify bpf_timer_cancel_async works Alexei Starovoitov
2026-02-01  2:54 ` [PATCH v9 bpf-next 7/9] selftests/bpf: Add timer stress test in NMI context Alexei Starovoitov
2026-02-01  2:54 ` [PATCH v9 bpf-next 8/9] selftests/bpf: Removed obsolete tests Alexei Starovoitov
2026-02-01  2:54 ` [PATCH v9 bpf-next 9/9] selftests/bpf: Add a test to stress bpf_timer_start and map_delete race Alexei Starovoitov
2026-02-01  3:15   ` bot+bpf-ci
2026-02-01  3:30     ` Alexei Starovoitov
2026-02-04  1:10 ` [PATCH v9 bpf-next 0/9] bpf: Avoid locks in bpf_timer and bpf_wq 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=20260201025403.66625-1-alexei.starovoitov@gmail.com \
    --to=alexei.starovoitov@gmail.com \
    --cc=andrii@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=kernel-team@fb.com \
    --cc=martin.lau@kernel.org \
    --cc=memxor@gmail.com \
    --cc=mykyta.yatsenko5@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox