BPF List
 help / color / mirror / Atom feed
* [PATCH bpf-next 0/6] workqueue: introduce BPF iterators for workqueues, worker pools and pending work
@ 2026-07-28  6:55 Imran Khan
  2026-07-28  6:55 ` [PATCH bpf-next 1/6] workqueue: introduce a BPF open-coded iterator for traversing workqueues list Imran Khan
                   ` (6 more replies)
  0 siblings, 7 replies; 10+ messages in thread
From: Imran Khan @ 2026-07-28  6:55 UTC (permalink / raw)
  To: bpf; +Cc: tj

Nearly every subsystem defers work to workqueues, and their state can
already be observed on a live system, just not at a granularity that is
convenient to consume. sysrq dumps every workqueue and pool to the kernel
log in a fixed format, with no way to select or aggregate; the WQ_SYSFS
interface only covers workqueues that ask for it and exposes attributes
rather than runtime state; and the drgn scripts under tools/workqueue/ need
a debuginfo-equipped userspace and read the state from the outside, without
the locks that protect it.

This series adds BPF iterators for workqueues, worker pools and pending
work items, so that this state can be walked from inside the kernel, under
the right locking, with filtering and aggregation done in place and only
the interesting part copied to userspace.

For the workqueue and worker_pool targets both forms are provided: the
open-coded form (bpf_iter_*_{new,next,destroy} kfuncs), usable from any BPF
program and thus combinable with other tracing and with maps, and the
seq_file form, pinnable to bpffs and readable via bpftool iter or read(),
for standalone dump-on-demand tools.

Patches:

 1-2: workqueue iterator. Walks the global workqueues list. The list is
      RCU-protected for reads and workqueue_struct is freed via call_rcu(),
      so the open-coded iterator is KF_RCU_PROTECTED and the caller must
      hold bpf_rcu_read_lock() across new()..destroy(). The seq form
      supplies the RCU section itself, per read chunk, and re-derives the
      position from *pos on each start so nothing is dereferenced across
      the gap between chunks.

 3-4: worker_pool iterator. Walks worker_pool_idr in ascending pool-id
      order, mirroring the pool enumeration done by wq_dump.py. The idr
      walk stays in kernel C (idr_get_next()), so no BPF-side idr support
      is needed. Same RCU rules and same seq scheme as above.

   5: pending work iterator ("workqueue_pending_work"), seq_file only.
      This one is deliberately not open-coded: pool->worklist is protected
      by pool->lock, and struct work_struct has neither a refcount nor
      RCU-freeing, so a pending work item cannot be kept alive for a
      suspended iterator. Instead a bounded snapshot of a pool's pending
      works (pool id, work address, work function) is copied out while
      pool->lock is held, and the BPF program then runs over that stable
      snapshot with no lock held. Pools are visited in worker_pool_idr
      order, which is a stable resume key across read() chunks; a pool with
      more than WQ_PENDING_SNAP_MAX pending works is truncated, best-effort,
      like the kernel's own worklist dump.

   6: selftests covering both forms of the workqueue and worker_pool
      iterators, the pending-work iterator, and a verifier test that the
      RCU-protected open-coded iterators are rejected outside an RCU
      critical section.

All new code sits behind CONFIG_BPF_SYSCALL. Tested with
"test_progs -t wq_iter" and result has been pasted below:
....
+ [ -x /etc/rcS.d/S50-startup ]
+ /etc/rcS.d/S50-startup
./test_progs -t wq_iter
[    1.105718] bpf_testmod: loading out-of-tree module taints kernel.
[    1.106438] bpf_testmod: module verification failed: signature and/or required key missing - tainting kernel
719/1   wq_iter/open_coded:OK
719/2   wq_iter/seq:OK
719/3   wq_iter/workqueue_iter_no_rcu:OK
719/4   wq_iter/worker_pool_iter_no_rcu:OK
719     wq_iter:OK
Summary: 1/4 PASSED, 0 SKIPPED, 0 FAILED
....

A PR for this changeset has been created at [1].

Thanks,
Imran

Imran Khan (6):
  workqueue: introduce a BPF open-coded iterator for traversing
    workqueues list
  workqueue: introduce a seq_file form for the workqueue iterator
  workqueue: introduce open-coded BPF iterator for worker pools
  workqueue: introduce seq_file form of the worker_pool iterator
  workqueue: introduce BPF iterator for pending work items
  selftests/bpf: add tests for the workqueue BPF iterators

 kernel/workqueue.c                            | 556 ++++++++++++++++++
 .../testing/selftests/bpf/bpf_experimental.h  |  10 +
 .../selftests/bpf/prog_tests/wq_iter.c        |  80 +++
 tools/testing/selftests/bpf/progs/wq_iter.c   |  95 +++
 .../selftests/bpf/progs/wq_iter_fail.c        |  37 ++
 5 files changed, 778 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/wq_iter.c
 create mode 100644 tools/testing/selftests/bpf/progs/wq_iter.c
 create mode 100644 tools/testing/selftests/bpf/progs/wq_iter_fail.c


base-commit: 87267b89459813cb50ab5377e076b639c07b4491

[1]: https://github.com/kernel-patches/bpf/pull/12981
-- 
2.43.0


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2026-08-10 19:44 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-28  6:55 [PATCH bpf-next 0/6] workqueue: introduce BPF iterators for workqueues, worker pools and pending work Imran Khan
2026-07-28  6:55 ` [PATCH bpf-next 1/6] workqueue: introduce a BPF open-coded iterator for traversing workqueues list Imran Khan
2026-07-28  6:55 ` [PATCH bpf-next 2/6] workqueue: introduce a seq_file form for the workqueue iterator Imran Khan
2026-07-28  6:55 ` [PATCH bpf-next 3/6] workqueue: introduce open-coded BPF iterator for worker pools Imran Khan
2026-07-28  6:55 ` [PATCH bpf-next 4/6] workqueue: introduce seq_file form of the worker_pool iterator Imran Khan
2026-07-28  6:55 ` [PATCH bpf-next 5/6] workqueue: introduce BPF iterator for pending work items Imran Khan
2026-07-28  6:55 ` [PATCH bpf-next 6/6] selftests/bpf: add tests for the workqueue BPF iterators Imran Khan
2026-08-02  2:59 ` [PATCH bpf-next 0/6] workqueue: introduce BPF iterators for workqueues, worker pools and pending work Tejun Heo
2026-08-06  8:41   ` imran.f.khan
2026-08-10 19:44     ` Tejun Heo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox