Skip to content

workqueue: introduce BPF iterators for workqueues, worker pools and pending work

Imran Khan bpf 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 Latest6 patches 4 repliesIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/6workqueue: introduce a BPF open-coded iterator for traversing workqueues listkernel/workqueue.c+103 -00 replies
2/6workqueue: introduce a seq_file form for the workqueue iteratorkernel/workqueue.c+117 -20 replies
3/6workqueue: introduce open-coded BPF iterator for worker poolskernel/workqueue.c+60 -00 replies
4/6workqueue: introduce seq_file form of the worker_pool iteratorkernel/workqueue.c+107 -10 replies
5/6workqueue: introduce BPF iterator for pending work itemskernel/workqueue.c+173 -10 replies
6/6selftests/bpf: add tests for the workqueue BPF iteratorstools/testing/selftests/bpf/bpf_experimental.h tools/testing/selftests/bpf/prog_tests/wq_iter.c tools/testing/selftests/bpf/progs/wq_iter.c +1 more+222 -00 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

Cover letter [PATCH bpf-next 0/6] workqueue: introduce BPF iterators for workqueues, worker pools and pending work

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

Signature · 1 line
2.43.0