forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
selftests/bpf: Add selftests for cpumask iter
Add selftests for the newly added cpumask iter. - cpumask_iter_success - The number of CPUs should be expected when iterating over the cpumask - percpu data extracted from the percpu struct should be expected - It can work in both non-sleepable and sleepable prog - RCU lock is only required by bpf_iter_cpumask_new() - It is fine without calling bpf_iter_cpumask_next() - cpumask_iter_failure - RCU lock is required in sleepable prog - The cpumask to be iterated over can't be NULL - bpf_iter_cpumask_destroy() is required after calling bpf_iter_cpumask_new() - bpf_iter_cpumask_destroy() can only destroy an initilialized iter - bpf_iter_cpumask_next() must use an initilialized iter The result as follows, torvalds#64/37 cpumask/test_cpumask_iter:OK torvalds#64/38 cpumask/test_cpumask_iter_sleepable:OK torvalds#64/39 cpumask/test_cpumask_iter_sleepable:OK torvalds#64/40 cpumask/test_cpumask_iter_next_no_rcu:OK torvalds#64/41 cpumask/test_cpumask_iter_no_next:OK torvalds#64/42 cpumask/test_cpumask_iter:OK torvalds#64/43 cpumask/test_cpumask_iter_no_rcu:OK torvalds#64/44 cpumask/test_cpumask_iter_no_destroy:OK torvalds#64/45 cpumask/test_cpumask_iter_null_pointer:OK torvalds#64/46 cpumask/test_cpumask_iter_next_uninit:OK torvalds#64/47 cpumask/test_cpumask_iter_destroy_uninit:OK torvalds#64 cpumask:OK Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
- Loading branch information
1 parent
92f80f2
commit 0c6c6b2
Showing
5 changed files
with
381 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// SPDX-License-Identifier: GPL-2.0-only | ||
/* Copyright (c) 2024 Yafang Shao <laoar.shao@gmail.com> */ | ||
|
||
#include "vmlinux.h" | ||
#include <bpf/bpf_helpers.h> | ||
#include <bpf/bpf_tracing.h> | ||
|
||
#include "bpf_misc.h" | ||
#include "task_kfunc_common.h" | ||
#include "cpumask_common.h" | ||
|
||
char _license[] SEC("license") = "GPL"; | ||
|
||
SEC("iter.s/cgroup") | ||
__failure __msg("R2 must be a rcu pointer") | ||
int BPF_PROG(test_cpumask_iter_no_rcu, struct bpf_iter_meta *meta, struct cgroup *cgrp) | ||
{ | ||
struct task_struct *p; | ||
int *cpu; | ||
|
||
p = bpf_task_from_pid(1); | ||
if (!p) | ||
return 1; | ||
|
||
bpf_for_each(cpumask, cpu, p->cpus_ptr) { | ||
} | ||
bpf_task_release(p); | ||
return 0; | ||
} | ||
|
||
SEC("iter/cgroup") | ||
__failure __msg("Possibly NULL pointer passed to trusted arg1") | ||
int BPF_PROG(test_cpumask_iter_null_pointer, struct bpf_iter_meta *meta, struct cgroup *cgrp) | ||
{ | ||
struct cpumask *mask = NULL; | ||
int *cpu; | ||
|
||
bpf_for_each(cpumask, cpu, mask) { | ||
} | ||
return 0; | ||
} | ||
|
||
SEC("iter.s/cgroup") | ||
__failure __msg("Unreleased reference id=3 alloc_insn=10") | ||
int BPF_PROG(test_cpumask_iter_no_destroy, struct bpf_iter_meta *meta, struct cgroup *cgrp) | ||
{ | ||
struct bpf_iter_cpumask it; | ||
struct task_struct *p; | ||
|
||
p = bpf_task_from_pid(1); | ||
if (!p) | ||
return 1; | ||
|
||
bpf_rcu_read_lock(); | ||
bpf_iter_cpumask_new(&it, p->cpus_ptr); | ||
bpf_rcu_read_unlock(); | ||
|
||
bpf_iter_cpumask_next(&it); | ||
bpf_task_release(p); | ||
return 0; | ||
} | ||
|
||
SEC("iter/cgroup") | ||
__failure __msg("expected an initialized iter_cpumask as arg #1") | ||
int BPF_PROG(test_cpumask_iter_next_uninit, struct bpf_iter_meta *meta, struct cgroup *cgrp) | ||
{ | ||
struct bpf_iter_cpumask *it = NULL; | ||
|
||
bpf_iter_cpumask_next(it); | ||
return 0; | ||
} | ||
|
||
SEC("iter/cgroup") | ||
__failure __msg("expected an initialized iter_cpumask as arg #1") | ||
int BPF_PROG(test_cpumask_iter_next_uninit2, struct bpf_iter_meta *meta, struct cgroup *cgrp) | ||
{ | ||
struct bpf_iter_cpumask it = {}; | ||
|
||
bpf_iter_cpumask_next(&it); | ||
return 0; | ||
} | ||
|
||
SEC("iter/cgroup") | ||
__failure __msg("expected an initialized iter_cpumask as arg #1") | ||
int BPF_PROG(test_cpumask_iter_destroy_uninit, struct bpf_iter_meta *meta, struct cgroup *cgrp) | ||
{ | ||
struct bpf_iter_cpumask_kern it = {.cpu = -1}; | ||
struct bpf_cpumask *mask; | ||
|
||
mask = bpf_cpumask_create(); | ||
if (!mask) | ||
return 1; | ||
|
||
bpf_cpumask_setall(mask); | ||
it.mask = &mask->cpumask; | ||
bpf_iter_cpumask_destroy((struct bpf_iter_cpumask *)&it); | ||
bpf_cpumask_release(mask); | ||
return 0; | ||
} |
Oops, something went wrong.