Skip to content

Commit 32f275f

Browse files
imran-knKernel Patches Daemon
authored andcommitted
selftests/bpf: add tests for the workqueue BPF iterators
Cover both the open-coded and seq_file forms of the workqueue and worker_pool iterators, plus the pending-work seq iterator: - count_workqueues / count_worker_pools drive the open-coded iterators (inside bpf_rcu_read_lock()); userspace checks the counts are non-zero. - dump_workqueues / dump_worker_pools attach the seq forms and drain them, exercising the RCU-per-chunk seq path that reuses the open-coded next(). - dump_pending attaches the workqueue_pending_work seq iterator and drains it, exercising the per-pool snapshot path. - wq_iter_fail.c checks the verifier rejects the RCU-protected open-coded iterators when used outside an RCU critical section. Also declare the open-coded iterator kfuncs in bpf_experimental.h. Signed-off-by: Imran Khan <imran.f.khan@oracle.com>
1 parent 52f03a3 commit 32f275f

4 files changed

Lines changed: 222 additions & 0 deletions

File tree

tools/testing/selftests/bpf/bpf_experimental.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,16 @@ extern int bpf_iter_dmabuf_new(struct bpf_iter_dmabuf *it) __weak __ksym;
361361
extern struct dma_buf *bpf_iter_dmabuf_next(struct bpf_iter_dmabuf *it) __weak __ksym;
362362
extern void bpf_iter_dmabuf_destroy(struct bpf_iter_dmabuf *it) __weak __ksym;
363363

364+
struct bpf_iter_workqueue;
365+
extern int bpf_iter_workqueue_new(struct bpf_iter_workqueue *it) __weak __ksym;
366+
extern struct workqueue_struct *bpf_iter_workqueue_next(struct bpf_iter_workqueue *it) __weak __ksym;
367+
extern void bpf_iter_workqueue_destroy(struct bpf_iter_workqueue *it) __weak __ksym;
368+
369+
struct bpf_iter_worker_pool;
370+
extern int bpf_iter_worker_pool_new(struct bpf_iter_worker_pool *it) __weak __ksym;
371+
extern struct worker_pool *bpf_iter_worker_pool_next(struct bpf_iter_worker_pool *it) __weak __ksym;
372+
extern void bpf_iter_worker_pool_destroy(struct bpf_iter_worker_pool *it) __weak __ksym;
373+
364374
extern int bpf_cgroup_read_xattr(struct cgroup *cgroup, const char *name__str,
365375
struct bpf_dynptr *value_p) __weak __ksym;
366376

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include <test_progs.h>
3+
#include "wq_iter.skel.h"
4+
#include "wq_iter_fail.skel.h"
5+
6+
static void subtest_open_coded(struct wq_iter *skel)
7+
{
8+
LIBBPF_OPTS(bpf_test_run_opts, opts);
9+
int err;
10+
11+
err = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.count_workqueues),
12+
&opts);
13+
if (!ASSERT_OK(err, "run count_workqueues"))
14+
return;
15+
/* There are always several system workqueues (events, ...). */
16+
ASSERT_GT(skel->bss->nr_workqueues, 0, "nr_workqueues");
17+
18+
err = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.count_worker_pools),
19+
&opts);
20+
if (!ASSERT_OK(err, "run count_worker_pools"))
21+
return;
22+
/* At least the per-CPU normal/highpri pools exist. */
23+
ASSERT_GT(skel->bss->nr_worker_pools, 0, "nr_worker_pools");
24+
ASSERT_GT(skel->bss->nr_percpu_pools, 0, "nr_percpu_pools");
25+
}
26+
27+
static void drain_iter(struct bpf_program *prog, const char *name)
28+
{
29+
struct bpf_link *link;
30+
char buf[512];
31+
int iter_fd;
32+
ssize_t n;
33+
34+
link = bpf_program__attach_iter(prog, NULL);
35+
if (!ASSERT_OK_PTR(link, name))
36+
return;
37+
iter_fd = bpf_iter_create(bpf_link__fd(link));
38+
if (!ASSERT_GE(iter_fd, 0, "iter_create"))
39+
goto out;
40+
while ((n = read(iter_fd, buf, sizeof(buf))) > 0)
41+
;
42+
ASSERT_GE(n, 0, "read iter");
43+
close(iter_fd);
44+
out:
45+
bpf_link__destroy(link);
46+
}
47+
48+
static void subtest_seq(struct wq_iter *skel)
49+
{
50+
/* seq forms are backed by the same open-coded next(). */
51+
drain_iter(skel->progs.dump_workqueues, "attach workqueue iter");
52+
ASSERT_GT(skel->bss->nr_wq_seq, 0, "nr_wq_seq");
53+
54+
drain_iter(skel->progs.dump_worker_pools, "attach worker_pool iter");
55+
ASSERT_GT(skel->bss->nr_pool_seq, 0, "nr_pool_seq");
56+
57+
/*
58+
* worklists are usually empty on an idle system; this drives the whole
59+
* per-pool snapshot path and verifies it drains cleanly.
60+
*/
61+
drain_iter(skel->progs.dump_pending, "attach pending_work iter");
62+
}
63+
64+
void test_wq_iter(void)
65+
{
66+
struct wq_iter *skel;
67+
68+
skel = wq_iter__open_and_load();
69+
if (!ASSERT_OK_PTR(skel, "wq_iter__open_and_load"))
70+
return;
71+
72+
if (test__start_subtest("open_coded"))
73+
subtest_open_coded(skel);
74+
if (test__start_subtest("seq"))
75+
subtest_seq(skel);
76+
77+
wq_iter__destroy(skel);
78+
79+
RUN_TESTS(wq_iter_fail);
80+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include "vmlinux.h"
3+
#include <bpf/bpf_helpers.h>
4+
#include <bpf/bpf_tracing.h>
5+
#include "bpf_experimental.h"
6+
7+
char _license[] SEC("license") = "GPL";
8+
9+
void bpf_rcu_read_lock(void) __ksym;
10+
void bpf_rcu_read_unlock(void) __ksym;
11+
12+
/* Results, checked by userspace. */
13+
int nr_workqueues;
14+
int nr_worker_pools;
15+
int nr_percpu_pools;
16+
int nr_wq_seq;
17+
int nr_pool_seq;
18+
int nr_pending;
19+
20+
/* --- open-coded iterators (KF_RCU_PROTECTED) --- */
21+
22+
SEC("syscall")
23+
int count_workqueues(const void *ctx)
24+
{
25+
struct workqueue_struct *wq;
26+
int n = 0;
27+
28+
bpf_rcu_read_lock();
29+
bpf_for_each(workqueue, wq)
30+
n++;
31+
bpf_rcu_read_unlock();
32+
33+
nr_workqueues = n;
34+
return 0;
35+
}
36+
37+
SEC("syscall")
38+
int count_worker_pools(const void *ctx)
39+
{
40+
struct worker_pool *pool;
41+
int n = 0, percpu = 0;
42+
43+
bpf_rcu_read_lock();
44+
bpf_for_each(worker_pool, pool) {
45+
n++;
46+
if (pool->cpu >= 0) /* per-CPU pool */
47+
percpu++;
48+
}
49+
bpf_rcu_read_unlock();
50+
51+
nr_worker_pools = n;
52+
nr_percpu_pools = percpu;
53+
return 0;
54+
}
55+
56+
/* --- seq_file iterators --- */
57+
58+
SEC("iter/workqueue")
59+
int dump_workqueues(struct bpf_iter__workqueue *ctx)
60+
{
61+
struct seq_file *seq = ctx->meta->seq;
62+
struct workqueue_struct *wq = ctx->wq;
63+
64+
if (!wq)
65+
return 0;
66+
nr_wq_seq++;
67+
BPF_SEQ_PRINTF(seq, "%s\n", wq->name);
68+
return 0;
69+
}
70+
71+
SEC("iter/worker_pool")
72+
int dump_worker_pools(struct bpf_iter__worker_pool *ctx)
73+
{
74+
struct worker_pool *pool = ctx->pool;
75+
76+
if (!pool)
77+
return 0;
78+
nr_pool_seq++;
79+
return 0;
80+
}
81+
82+
SEC("iter/workqueue_pending_work")
83+
int dump_pending(struct bpf_iter__workqueue_pending_work *ctx)
84+
{
85+
struct seq_file *seq = ctx->meta->seq;
86+
struct wq_pending_work_info *info = ctx->info;
87+
88+
if (!info) /* final call */
89+
return 0;
90+
91+
nr_pending++;
92+
BPF_SEQ_PRINTF(seq, "pool %llu work 0x%llx func 0x%llx\n",
93+
info->pool_id, info->work, info->func);
94+
return 0;
95+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include "vmlinux.h"
3+
#include <bpf/bpf_helpers.h>
4+
#include <bpf/bpf_tracing.h>
5+
#include "bpf_misc.h"
6+
#include "bpf_experimental.h"
7+
8+
char _license[] SEC("license") = "GPL";
9+
10+
/*
11+
* The workqueue and worker_pool open-coded iterators are KF_RCU_PROTECTED:
12+
* using them outside a bpf_rcu_read_lock() region must be rejected.
13+
*/
14+
15+
SEC("?syscall")
16+
__failure __msg("kernel func bpf_iter_workqueue_new requires RCU critical section protection")
17+
int workqueue_iter_no_rcu(const void *ctx)
18+
{
19+
struct workqueue_struct *wq;
20+
int n = 0;
21+
22+
bpf_for_each(workqueue, wq)
23+
n++;
24+
return n;
25+
}
26+
27+
SEC("?syscall")
28+
__failure __msg("kernel func bpf_iter_worker_pool_new requires RCU critical section protection")
29+
int worker_pool_iter_no_rcu(const void *ctx)
30+
{
31+
struct worker_pool *pool;
32+
int n = 0;
33+
34+
bpf_for_each(worker_pool, pool)
35+
n++;
36+
return n;
37+
}

0 commit comments

Comments
 (0)