Concurrency Testing
Subsystems (5)
Kumar Kartikeya Dwivedi
maintainercover Concurrency Testing
This set introduces basic tooling to generate more exhaustive test cases for task<->NMI reentrancy and parallel invocation of various operations associated with rqspinlock, timer, wq, and task_work. For now, the approach is simple: test cases are generated up to a maximum specified level of parallelism for the operations to be invoked vertically against each other (on the same CPU, in different contexts), and horizontally (in parallel). To cut down test time, similar cases are pruned out after generation (e.g. start vs cancel and cancel vs start in parallel on two CPUs are similar, thus only one combination should be tested).
More details are in the commit log.
Kumar Kartikeya Dwivedi (6):
bpf: Support repeat, duration fields for syscall prog runs
bpf: Allow timing functions in lock critical sections
bpf: Enable rqspinlock in tracing progs selftests/bpf: Introduce concurrency testing tool selftests/bpf: Generate various conctest permutations selftests/bpf: Extend conctest to wq and task_work
kernel/bpf/verifier.c | 9 +- net/bpf/test_run.c | 92 +- tools/testing/selftests/bpf/Makefile | 6 + tools/testing/selftests/bpf/conctest.c | 1110 ++++++++++++++++++ tools/testing/selftests/bpf/progs/conctest.c | 758 ++++++++++++ 5 files changed, 1955 insertions(+), 20 deletions(-) create mode 100644 tools/testing/selftests/bpf/conctest.c create mode 100644 tools/testing/selftests/bpf/progs/conctest.c
base-commit: dc855b77719fe452d670cae2cf64da1eb51f16cc
Signature · 1 line
2.47.3
Kumar Kartikeya Dwivedi
maintainerRFC PATCH 1/6 bpf: Support repeat, duration fields for syscall prog runs
Currently, BPF syscall programs do not support specifying the repeat field to repeat the test inside the kernel multiple times. Use the test_timer infra for other prog run tests and make it with RCU tasks trace to allow usage for syscall programs. Also make the duration field available for use.
--- net/bpf/test_run.c | 92 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 74 insertions(+), 18 deletions(-)
net/bpf/test_run.c +74 -18
@@ -33,23 +33,27 @@ struct bpf_test_timer {u64 time_start, time_spent; }; -static void bpf_test_timer_enter (struct bpf_test_timer *t) -__acquires (rcu) +static void __bpf_test_timer_enter (struct bpf_test_timer *t,bool trace) { -rcu_read_lock_dont_migrate (); +if (trace) +rcu_read_lock_trace (); +else +rcu_read_lock_dont_migrate (); t->time_start =ktime_get_ns (); } -static void bpf_test_timer_leave (struct bpf_test_timer *t) -__releases (rcu) +static void __bpf_test_timer_leave (struct bpf_test_timer *t,bool trace) { t->time_start =0 ; -rcu_read_unlock_migrate (); +if (trace) +rcu_read_unlock_trace (); +else +rcu_read_unlock_migrate (); } -static bool bpf_test_timer_continue (struct bpf_test_timer *t,int iterations, -u32 repeat,int *err,u32 *duration) -__must_hold (rcu) +static bool __bpf_test_timer_continue (struct bpf_test_timer *t, +int iterations,u32 repeat, +int *err,u32 *duration,bool trace) { t->i += iterations;if (t->i >= repeat) {
@@ -70,9 +74,9 @@ static bool bpf_test_timer_continue(struct bpf_test_timer *t, int iterations,if (need_resched ()) {/* During iteration: we need to reschedule between runs. */ t->time_spent +=ktime_get_ns () - t->time_start; -bpf_test_timer_leave (t); +__bpf_test_timer_leave (t, trace);cond_resched (); -bpf_test_timer_enter (t); +__bpf_test_timer_enter (t, trace); }/* Do another round. */
@@ -83,6 +87,45 @@ static bool bpf_test_timer_continue(struct bpf_test_timer *t, int iterations,return false; } +static void bpf_test_timer_enter (struct bpf_test_timer *t) +__acquires (rcu) +{ +__bpf_test_timer_enter (t, false); +} + +static void bpf_test_timer_leave (struct bpf_test_timer *t) +__releases (rcu) +{ +__bpf_test_timer_leave (t, false); +} + +static bool bpf_test_timer_continue (struct bpf_test_timer *t,int iterations, +u32 repeat,int *err,u32 *duration) +__must_hold (rcu) +{ +return __bpf_test_timer_continue (t, iterations, repeat, err, duration, false); +} + +static void bpf_test_timer_enter_trace (struct bpf_test_timer *t) +__acquires (rcu_trace) +{ +__bpf_test_timer_enter (t, true); +} + +static void bpf_test_timer_leave_trace (struct bpf_test_timer *t) +__releases (rcu_trace) +{ +__bpf_test_timer_leave (t, true); +} + +static bool bpf_test_timer_continue_trace (struct bpf_test_timer *t, +int iterations,u32 repeat, +int *err,u32 *duration) +__must_hold (rcu_trace) +{ +return __bpf_test_timer_continue (t, iterations, repeat, err, duration, true); +} +/* We put this struct at the head of each page with a context and frame * initialised when the page is allocated, so we don't have to do this on each * repetition of the test run.
@@ -1615,14 +1658,14 @@ int bpf_prog_test_run_syscall(struct bpf_prog *prog, {void __user *ctx_in =u64_to_user_ptr (kattr->test.ctx_in);__u32 ctx_size_in = kattr->test.ctx_size_in; +struct bpf_test_timer t = {}; +u32 retval, duration, repeat;void *ctx =NULL ; -u32 retval;int err =0 ; -/* doesn't support data_in/out, ctx_out, duration, or repeat or flags */ +/* doesn't support data_in/out, ctx_out, or flags */ if (kattr->test.data_in || kattr->test.data_out || - kattr->test.ctx_out || kattr->test.duration || - kattr->test.repeat || kattr->test.flags || + kattr->test.ctx_out || kattr->test.flags || kattr->test.batch_size)return -EINVAL ;
@@ -1636,14 +1679,27 @@ int bpf_prog_test_run_syscall(struct bpf_prog *prog,return PTR_ERR (ctx); } -rcu_read_lock_trace (); - retval =bpf_prog_run_pin_on_cpu (prog, ctx); -rcu_read_unlock_trace (); + repeat = kattr->test.repeat; +if (!repeat) + repeat =1 ; + +bpf_test_timer_enter_trace (&t); +do { + retval =bpf_prog_run_pin_on_cpu (prog, ctx); + }while (bpf_test_timer_continue_trace (&t,1 , repeat, &err, &duration)); +bpf_test_timer_leave_trace (&t); + +if (err) + goto out;if (copy_to_user (&uattr->test.retval, &retval,sizeof (u32))) { err = -EFAULT ; goto out; } +if (copy_to_user (&uattr->test.duration, &duration,sizeof (duration))) { + err = -EFAULT ; + goto out; + }if (ctx_size_in)if (copy_to_user (ctx_in, ctx, ctx_size_in)) err = -EFAULT ;
Signature · 1 line
2.47.3
Re: [RFC PATCH bpf-next v1 1/6] bpf: Support repeat, duration fields for syscall prog runs
Quoted 83 lines · Kumar Kartikeya Dwivedi
Currently, BPF syscall programs do not support specifying the repeat field to repeat the test inside the kernel multiple times. Use the
test_timerinfra for other prog run tests and make it with RCU tasks trace to allow usage for syscall programs. Also make the duration field available for use.Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>--- net/bpf/test_run.c | 92 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 74 insertions(+), 18 deletions(-)net/bpf/test_run.c +29
-11@@ -33,23 +33,27 @@ struct bpf_test_timer {u64 time_start, time_spent; }; -static void bpf_test_timer_enter (struct bpf_test_timer *t) -__acquires (rcu) +static void __bpf_test_timer_enter (struct bpf_test_timer *t,bool trace) { -rcu_read_lock_dont_migrate (); +if (trace) +rcu_read_lock_trace (); +else +rcu_read_lock_dont_migrate (); t->time_start =ktime_get_ns (); } -static void bpf_test_timer_leave (struct bpf_test_timer *t) -__releases (rcu) +static void __bpf_test_timer_leave (struct bpf_test_timer *t,bool trace) { t->time_start =0 ; -rcu_read_unlock_migrate (); +if (trace) +rcu_read_unlock_trace (); +else +rcu_read_unlock_migrate (); } -static bool bpf_test_timer_continue (struct bpf_test_timer *t,int iterations, -u32 repeat,int *err,u32 *duration) -__must_hold (rcu) +static bool __bpf_test_timer_continue (struct bpf_test_timer *t, +int iterations,u32 repeat, +int *err,u32 *duration,bool trace) { t->i += iterations;if (t->i >= repeat) {@@ -70,9 +74,9 @@ static bool bpf_test_timer_continue(struct bpf_test_timer *t, int iterations,if (need_resched ()) {/* During iteration: we need to reschedule between runs. */ t->time_spent +=ktime_get_ns () - t->time_start; -bpf_test_timer_leave (t); +__bpf_test_timer_leave (t, trace);cond_resched (); -bpf_test_timer_enter (t); +__bpf_test_timer_enter (t, trace); }/* Do another round. */ @@ -83,6 +87,45 @@ static bool bpf_test_timer_continue(struct bpf_test_timer *t, int iterations,return false; } +static void bpf_test_timer_enter (struct bpf_test_timer *t) +__acquires (rcu) +{ +__bpf_test_timer_enter (t, false); +} + +static void bpf_test_timer_leave (struct bpf_test_timer *t) +__releases (rcu) +{ +__bpf_test_timer_leave (t, false); +} + +static bool bpf_test_timer_continue (struct bpf_test_timer *t,int iterations, +u32 repeat,int *err,u32 *duration)
net/bpf/test_run.c static bool bpf_test_timer_continue(struct bpf_test_timer *t, int iterations, · line 113
Quoted 10 lines
net/bpf/test_run.c +10
-0+__must_hold (rcu) +{ +return __bpf_test_timer_continue (t, iterations, repeat, err, duration, false); +} + +static void bpf_test_timer_enter_trace (struct bpf_test_timer *t) +__acquires (rcu_trace) +{ +__bpf_test_timer_enter (t, true); +}
rcu_task_trace doesn't exist any more. We should move away
from that name as well.
Especially here '_trace' is misleading. What you meant is task_trace and that it's sleepable.
So let's use either "_sleepable" suffix and replace 'bool trace' to 'bool sleepable'?
or use "_srcu" if you want to describe the logic inside rcu_read_lock_trace(). At least "s" in srcu also means sleepable.
Kumar Kartikeya Dwivedi
maintainerRFC PATCH 2/6 bpf: Allow timing functions in lock critical sections
In reply to Kumar Kartikeya DwivediAllow timing related functions in critical sections, which is useful to measure the time spent acquiring a lock inside BPF programs.
--- kernel/bpf/verifier.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
kernel/bpf/verifier.c +5 -1
@@ -21044,7 +21044,11 @@ static int do_check_insn(struct bpf_verifier_env *env, bool *do_print_state)if (env->cur_state->active_locks) {if ((insn->src_reg ==BPF_REG_0 && - insn->imm != BPF_FUNC_spin_unlock) || + insn->imm != BPF_FUNC_spin_unlock && + insn->imm != BPF_FUNC_ktime_get_ns && + insn->imm != BPF_FUNC_ktime_get_boot_ns && + insn->imm != BPF_FUNC_ktime_get_coarse_ns && + insn->imm != BPF_FUNC_ktime_get_tai_ns) || (insn->src_reg ==BPF_PSEUDO_KFUNC_CALL && (insn->off !=0 || !kfunc_spin_allowed (insn->imm)))) { verbose(env,
Signature · 1 line
2.47.3
Kumar Kartikeya Dwivedi
maintainerRFC PATCH 3/6 bpf: Enable rqspinlock in tracing progs
In reply to Kumar Kartikeya DwivediAllow using rqspinlock in tracing programs, since it is safe to acquire from any context. This permits its stress testing from any context using BPF programs.
--- kernel/bpf/verifier.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/bpf/verifier.c +2 -1
@@ -21542,7 +21542,8 @@ static int check_map_prog_compatibility(struct bpf_verifier_env *env,return -EINVAL ; } -if (is_tracing_prog_type (prog_type)) { +if (is_tracing_prog_type (prog_type) && +btf_record_has_field (map->record,BPF_SPIN_LOCK )) {verbose (env,"tracing progs cannot use bpf_spin_lock yet\n" );return -EINVAL ; }
Signature · 1 line
2.47.3
Re: [RFC PATCH bpf-next v1 3/6] bpf: Enable rqspinlock in tracing progs
Quoted 2 lines · Kumar Kartikeya Dwivedi
Allow using rqspinlock in tracing programs, since it is safe to acquire
...
kernel/bpf/verifier.c static int check_map_prog_compatibility(struct bpf_verifier_env *env, · line 21546
Quoted 3 lines
kernel/bpf/verifier.c +2
-1-if (is_tracing_prog_type (prog_type)) { +if (is_tracing_prog_type (prog_type) && +btf_record_has_field (map->record,BPF_SPIN_LOCK )) {
hmm. Did you mean BPF_RES_SPIN_LOCK ?
Kumar Kartikeya Dwivedi
maintainerRFC PATCH 4/6 selftests/bpf: Introduce concurrency testing tool
In reply to Kumar Kartikeya DwivediIntroduce a scaffolding for concurrency testing tool with rqspinlock as the first example. The main idea is to set up various permutations of context reentrancy and parallel invocations to allow more exhaustive testing of various concurrency-related primitives. Each operation also supports specification of expected values (e.g. ABBA should always -EDEADLK, never -ETIMEDOUT) and threshold values on maximum expected delay. In case violations occur, the tests will fail and a histogram with timing distribution will be printed. The timing information is useful in general to observe various statistics of operation latency from different contexts to notice potential anomalies.
--- tools/testing/selftests/bpf/Makefile | 6 + tools/testing/selftests/bpf/conctest.c | 889 +++++++++++++++++++ tools/testing/selftests/bpf/progs/conctest.c | 289 ++++++ 3 files changed, 1184 insertions(+) create mode 100644 tools/testing/selftests/bpf/conctest.c create mode 100644 tools/testing/selftests/bpf/progs/conctest.c
tools/testing/selftests/bpf/Makefile +6 -0
@@ -123,6 +123,7 @@ TEST_KMOD_TARGETS = $(addprefix $(OUTPUT)/,$(TEST_KMODS)) # Compile but not part of 'make run_tests' TEST_GEN_PROGS_EXTENDED = \ bench \ + conctest \ flow_dissector_load \ test_cpp \ test_lirc_mode2_user \
@@ -504,6 +505,7 @@ linked_maps.skel.h-deps := linked_maps1.bpf.o linked_maps2.bpf.o test_subskeleton.skel.h-deps := test_subskeleton_lib2.bpf.o test_subskeleton_lib.bpf.o test_subskeleton.bpf.o test_subskeleton_lib.skel.h-deps := test_subskeleton_lib2.bpf.o test_subskeleton_lib.bpf.o test_usdt.skel.h-deps := test_usdt.bpf.o test_usdt_multispec.bpf.o +conctest.skel.h-deps := conctest.bpf.o xsk_xdp_progs.skel.h-deps := xsk_xdp_progs.bpf.o xdp_hw_metadata.skel.h-deps := xdp_hw_metadata.bpf.o xdp_features.skel.h-deps := xdp_features.bpf.o
@@ -823,6 +825,10 @@ $(OUTPUT)/xdp_features: xdp_features.c $(OUTPUT)/network_helpers.o $(OUTPUT)/xdp $(call msg,BINARY,,$@) $(Q)$(CC) $(CFLAGS) $(filter %.a %.o %.c,$^) $(LDLIBS) -o $@ +$(OUTPUT)/conctest: conctest.c $(OUTPUT)/conctest.skel.h $(BPFOBJ) | $(OUTPUT) + $(call msg,BINARY,,$@) + $(Q)$(CC) $(CFLAGS) $(filter %.a %.o %.c,$^) $(LDLIBS) -lm -o $@ + # Make sure we are able to include and link libbpf against c++. CXXFLAGS += $(CFLAGS) CXXFLAGS := $(subst -D_GNU_SOURCE=,,$(CXXFLAGS))
tools/testing/selftests/bpf/conctest.c new file +889 -0
889 lines not on this page · raw message
tools/testing/selftests/bpf/progs/conctest.c new file +289 -0
289 lines not on this page · raw message
Signature · 1 line
2.47.3
Re: [RFC PATCH bpf-next v1 4/6] selftests/bpf: Introduce concurrency testing tool
Quoted 15 lines · Kumar Kartikeya Dwivedi
tools/testing/selftests/bpf/progs/conctest.c new file +14
-0+ +struct lock_val { +struct bpf_res_spin_lock lock; +}; + +struct { + __uint(type, BPF_MAP_TYPE_ARRAY); + __uint(max_entries, 3); + __type(key, int); + __type(value, struct lock_val); +} lock_map SEC(".maps"); + +SEC("?syscall") +int conctest_rqspinlock_task (void *ctx)
tools/testing/selftests/bpf/progs/conctest.c line 243
Quoted 10 lines
tools/testing/selftests/bpf/progs/conctest.c new file +10
-0+{ +struct lock_val *e; +__u32 key; +int ret; + + key =bpf_get_smp_processor_id (); + key %= (__u32 )nr_cpus; + e =bpf_map_lookup_elem (&lock_map, &key); +if (!e) +return 0 ;
Pls remove all of the 'if (!e)' checks when doing lookup from an array with a constant.
I'm not sure in the above the verifier is smart enough,
but I think we improved bpf_mod logic recently, so should be.
If not, improve the verifier :) and let's get rid of this legacy 'if' checks.
Kumar Kartikeya Dwivedi
maintainerRFC PATCH 5/6 selftests/bpf: Generate various conctest permutations
In reply to Kumar Kartikeya DwivediIntroduce support for testing various permutations given a list of a operations for an async primitive. This unburdens the user from setting up various cases and thinking through all possible interleavings. The allowed context for each operation and the number of operations are specified and the various permutations in both directions (vertical, i.e. on the same CPU, and horizontal, i.e. across CPUs in a flat context) are generated automatically. Further, symmetric operations that won't improve coverage (e.g. flat AvsB and BvsA cases) are pruned to reduce test time.
Add support for timer operations testing in this commit to show its usefulness. Unlike rqspinlock, the number of operations extend to 6 (start, cancel, cancel_async, init, delete, set_callback) which must be tested in various combinations to approach completeness. To bound the number of generated cases bound the maximum parallelism to 4 for timer and 3 for rqspinlock.
--- tools/testing/selftests/bpf/conctest.c | 319 ++++++++++++++----- tools/testing/selftests/bpf/progs/conctest.c | 230 ++++++++++++- 2 files changed, 476 insertions(+), 73 deletions(-)
tools/testing/selftests/bpf/conctest.c +247 -72
@@ -300,92 +300,237 @@ struct conctest_cfg {for (struct conctest_cfg *cfg = (arr); cfg->type !=CT_INVALID ; cfg++) struct conctest_test { -const char *name; -struct conctest_cfg *cfgs; -void (*init)(struct conctest *skel); +char name[128 ]; +struct conctest_cfg cfgs[4 ]; +void (*init)(struct conctest *skel,struct conctest_test *test); +const char **extra_progs;int nr_cpus; +bool vertical; }; -struct conctest_cfg rqspinlock[] = { - { - .type =CT_TASK_PROG , - .task = {"conctest_rqspinlock_task" }, - }, - { - .type =CT_NMI_PROG , - .nmi = {"conctest_rqspinlock_nmi" }, - }, - {} +static int find_prog_fd (struct conctest *skel,const char *prog_name); + +#define CTX_TASK_OK (1 << 0) +#define CTX_NMI_OK (1 << 1) + +struct conctest_op { +const char *name; +const char *task_prog; +const char *nmi_prog; +unsigned int ctx_mask; }; -struct conctest_cfg rqspinlock_shift[] = { - { - .type =CT_TASK_PROG , - .task = {"conctest_rqspinlock_task" }, - }, - { - .type =CT_NMI_PROG , - .nmi = {"conctest_rqspinlock_nmi_shift" }, - }, - {} +struct conctest_suite { +const char *name; +struct conctest_op *ops; +int nr_ops; +void (*init)(struct conctest *skel,struct conctest_test *test); +const char **extra_progs; +int max_cpus; }; -static void __rqspinlock_init (struct conctest *skel,int err,int delay) +static struct conctest_op rqspinlock_ops[] = { + {"lock" ,"conctest_rqspinlock_task" ,"conctest_rqspinlock_nmi" ,CTX_TASK_OK |CTX_NMI_OK }, + {"lock_shift" ,"conctest_rqspinlock_task" ,"conctest_rqspinlock_nmi_shift" ,CTX_TASK_OK |CTX_NMI_OK }, + {}, +}; + +static void rqspinlock_init (struct conctest *skel,struct conctest_test *test) {struct conctest_op_state state; -int key; +bool same_lock, is_vert; +int key, nmi_err; + + is_vert = test->vertical; + +/* +* Horizontal: don't set expectations. +* Vertical, same lock: always -EDEADLK. +* Vertical, different locks: -EDEADLK for <3 CPUs, -ETIMEDOUT for >=3. +*/ +if (!is_vert) { + key =CONCTEST_STAT_SYSCALL ; +memset (&state,0 ,sizeof (state)); + state.delay_thresh_ns =10000000 ; +bpf_map__update_elem (skel->maps.state_map, &key,sizeof (key), + &state,sizeof (state),0 ); +return ; + } + + same_lock = (test->cfgs[0 ].task.prog_name == test->cfgs[1 ].nmi.prog_name) || + (strcmp (test->cfgs[0 ].task.prog_name,"conctest_rqspinlock_task" ) ==0 && +strcmp (test->cfgs[1 ].nmi.prog_name,"conctest_rqspinlock_nmi" ) ==0 ); + +if (same_lock || test->nr_cpus <3 ) + nmi_err = -EDEADLK ; +else + nmi_err = -ETIMEDOUT ; key =CONCTEST_STAT_SYSCALL ;memset (&state,0 ,sizeof (state)); state.expect_ret =0 ; - state.delay_thresh_ns =1000000 * delay; -bpf_map__update_elem (skel->maps.state_map, &key,sizeof (key), &state, -sizeof (state),0 ); + state.delay_thresh_ns = (test->nr_cpus >=3 ) ?300000000ULL :10000000ULL ; +bpf_map__update_elem (skel->maps.state_map, &key,sizeof (key), + &state,sizeof (state),0 ); key =CONCTEST_STAT_NMI ;memset (&state,0 ,sizeof (state)); - state.expect_ret = err; - state.delay_thresh_ns =1000000 * delay; -bpf_map__update_elem (skel->maps.state_map, &key,sizeof (key), &state, -sizeof (state),0 ); + state.expect_ret = nmi_err; + state.delay_thresh_ns = (test->nr_cpus >=3 ) ?300000000ULL :10000000ULL ; +bpf_map__update_elem (skel->maps.state_map, &key,sizeof (key), + &state,sizeof (state),0 ); } -static void rqspinlock_init (struct conctest *skel) +static struct conctest_suite rqspinlock_suite = { + .name ="rqspinlock" , + .ops = rqspinlock_ops, + .nr_ops =2 , + .init = rqspinlock_init, + .max_cpus =3 , +}; + +static void timer_init (struct conctest *skel,struct conctest_test *test) { -return __rqspinlock_init (skel, -EDEADLK ,2 ); +LIBBPF_OPTS (bpf_test_run_opts, opts); +int fd; + + fd =find_prog_fd (skel,"conctest_timer_init" ); +if (fd <0 ) +return ; +bpf_prog_test_run_opts (fd, &opts); } -static void rqspinlock_init_delay (struct conctest *skel) +static struct conctest_op timer_ops[] = { + {"init" ,"conctest_timer_task_reinit" ,"conctest_timer_nmi_reinit" ,CTX_TASK_OK |CTX_NMI_OK }, + {"start" ,"conctest_timer_task_start" ,"conctest_timer_nmi_start" ,CTX_TASK_OK |CTX_NMI_OK }, + {"cancel" ,"conctest_timer_task_cancel" ,NULL ,CTX_TASK_OK }, + {"cancel_async" ,"conctest_timer_task_cancel_async" ,"conctest_timer_nmi_cancel_async" ,CTX_TASK_OK |CTX_NMI_OK }, + {"set_cb" ,"conctest_timer_task_set_cb" ,"conctest_timer_nmi_set_cb" ,CTX_TASK_OK |CTX_NMI_OK }, + {"delete" ,"conctest_timer_task_delete" ,"conctest_timer_nmi_delete" ,CTX_TASK_OK |CTX_NMI_OK }, + {}, +}; + +static const char *timer_extra_progs[] = {"conctest_timer_init" ,NULL }; + +static struct conctest_suite timer_suite = { + .name ="timer" , + .ops = timer_ops, + .nr_ops =6 , + .init = timer_init, + .extra_progs = timer_extra_progs, + .max_cpus =4 , +}; + +static struct conctest_suite *suites[] = { + &rqspinlock_suite, + &timer_suite, +NULL , +}; + +#define MAX_GENERATED_TESTS 1024 + +static struct conctest_test generated_tests[MAX_GENERATED_TESTS ]; +static int nr_generated; + +static void add_test (const char *suite_name,const char *a_name,const char *b_name, +const char *task_prog,const char *nmi_prog, +const char *task_prog_b, +void (*init)(struct conctest *skel,struct conctest_test *test), +const char **extra_progs,int nr_cpus) { -return __rqspinlock_init (skel, -EDEADLK ,10 ); +struct conctest_test *t; + +if (nr_generated >=MAX_GENERATED_TESTS ) +return ; + + t = &generated_tests[nr_generated++]; +if (nmi_prog) +snprintf (t->name,sizeof (t->name),"%s:%d:vert:%s_vs_%s" , + suite_name, nr_cpus, a_name, b_name); +else +snprintf (t->name,sizeof (t->name),"%s:%d:flat:%s_vs_%s" , + suite_name, nr_cpus, a_name, b_name); + + t->init = init; + t->extra_progs = extra_progs; + t->nr_cpus = nr_cpus; + t->vertical = (nmi_prog !=NULL ); + + t->cfgs[0 ].type =CT_TASK_PROG ; + t->cfgs[0 ].task.prog_name = task_prog; + +if (nmi_prog) { + t->cfgs[1 ].type =CT_NMI_PROG ; + t->cfgs[1 ].nmi.prog_name = nmi_prog; + t->cfgs[2 ].type =CT_INVALID ; + }else if (task_prog_b) { + t->cfgs[1 ].type =CT_TASK_PROG ; + t->cfgs[1 ].task.prog_name = task_prog_b; + t->cfgs[2 ].type =CT_INVALID ; + }else { + t->cfgs[1 ].type =CT_INVALID ; + } } -static void rqspinlock_init_timeout (struct conctest *skel) +static void generate_suite_tests (struct conctest_suite *suite,int max_cpus) { -return __rqspinlock_init (skel, -ETIMEDOUT ,300 ); +int i, j, cpus; + +if (suite->max_cpus > max_cpus) + suite->max_cpus = max_cpus; + +for (cpus =1 ; cpus <= suite->max_cpus; cpus++) { +for (i =0 ; i < suite->nr_ops; i++) { +struct conctest_op *a = &suite->ops[i]; + +if (!(a->ctx_mask &CTX_TASK_OK )) +continue ; + +for (j =0 ; j < suite->nr_ops; j++) { +struct conctest_op *b = &suite->ops[j]; + +if (!(b->ctx_mask &CTX_NMI_OK ) || !b->nmi_prog) +continue ; + +add_test (suite->name, a->name, b->name, + a->task_prog, b->nmi_prog,NULL , + suite->init, suite->extra_progs, cpus); + } + } + +/* Horizontal: task(a) vs task(b), needs at least 2 CPUs */ +if (cpus <2 ) +continue ; + +for (i =0 ; i < suite->nr_ops; i++) { +struct conctest_op *a = &suite->ops[i]; + +if (!(a->ctx_mask &CTX_TASK_OK )) +continue ; + +/* j = i to skip symmetric pairs (A vs B == B vs A) */ +for (j = i; j < suite->nr_ops; j++) { +struct conctest_op *b = &suite->ops[j]; + +if (!(b->ctx_mask &CTX_TASK_OK ) || !b->task_prog) +continue ; + +add_test (suite->name, a->name, b->name, + a->task_prog,NULL , b->task_prog, + suite->init, suite->extra_progs, cpus); + } + } + } } -static struct conctest_test all_tests[] = { - { - .name ="rqspinlock:AA" , - .cfgs = rqspinlock, - .init = rqspinlock_init, - .nr_cpus =1 - }, - { - .name ="rqspinlock:ABBA" , - .cfgs = rqspinlock_shift, - .init = rqspinlock_init_delay, - .nr_cpus =2 - }, - { - .name ="rqspinlock:ABBCBA" , - .cfgs = rqspinlock_shift, - .init = rqspinlock_init_timeout, - .nr_cpus =3 - }, - {}, -}; +static void generate_all_tests (int max_cpus) +{ +int i; + + nr_generated =0 ; +for (i =0 ; suites[i]; i++) +generate_suite_tests (suites[i], max_cpus); +}struct task_ctx {int prog_fd;
@@ -466,11 +611,6 @@ static struct conctest_ctx *alloc_ctx(int nr_cpus, struct conctest_cfg *cfgs) } } -if (nr_task >1 ) { -fprintf (stderr,"Only one CT_TASK_PROG entry allowed\n" ); -return NULL ; - } -if (nr_nmi >1 ) {fprintf (stderr,"Only one CT_NMI_PROG entry allowed\n" );return NULL ;
@@ -603,7 +743,9 @@ static int run_test(struct conctest_test *test, int nr_cpus, int duration)if (!ctx)return -ENOMEM ; -printf ("Running test '%s' on %d CPU(s) for %d seconds\n" , test->name, nr_cpus, duration); +if (verbose) +printf ("Running test '%s' on %d CPU(s) for %d seconds\n" , test->name, nr_cpus, + duration); skel =conctest__open ();if (!skel) {
@@ -639,6 +781,20 @@ static int run_test(struct conctest_test *test, int nr_cpus, int duration) } } +if (test->extra_progs) { +struct bpf_program *ep_prog; +const char **ep; + +for (ep = test->extra_progs; *ep; ep++) { + ep_prog =bpf_object__find_program_by_name (skel->obj, *ep); +if (!ep_prog) { +fprintf (stderr,"Extra program '%s' not found\n" , *ep); + goto out; + } +bpf_program__set_autoload (ep_prog, true); + } + } + err =conctest__load (skel);if (err) {fprintf (stderr,"Failed to load BPF skeleton: %d\n" , err);
@@ -648,17 +804,30 @@ static int run_test(struct conctest_test *test, int nr_cpus, int duration) stop =0 ;if (test->init) - test->init (skel); + test->init (skel, test);/* Pass the test nr_cpus to rotate between objects, but use passed in nr_cpus otherwise. */ skel->bss->nr_cpus = test->nr_cpus; skel->bss->delay_seed = delay_us;for_each_conctest_cfg (cfg, test->cfgs) { -int cpu; +int cpu, nr_task_cfgs =0 , task_idx =0 ; +int cpu_start, cpu_end; + +for_each_conctest_cfg (c, test->cfgs) +if (c->type ==CT_TASK_PROG ) + nr_task_cfgs++;switch (cfg->type) {case CT_TASK_PROG : -for (cpu =0 ; cpu < nr_cpus; cpu++) { +for_each_conctest_cfg (c, test->cfgs) { +if (c == cfg) +break ; +if (c->type ==CT_TASK_PROG ) + task_idx++; + } + cpu_start = task_idx * nr_cpus / nr_task_cfgs; + cpu_end = (task_idx +1 ) * nr_cpus / nr_task_cfgs; +for (cpu = cpu_start; cpu < cpu_end; cpu++) {struct task_ctx *tc;int prog_fd;
@@ -747,7 +916,8 @@ static int run_test(struct conctest_test *test, int nr_cpus, int duration) }sleep (duration); -printf ("Test '%s' completed\n" , test->name); +if (verbose) +printf ("Test '%s' completed\n" , test->name); err =0 ; out: stop =1 ;
@@ -814,7 +984,7 @@ int main(int argc, char **argv)int max_cpus =libbpf_num_possible_cpus ();int duration =DEFAULT_DURATION ;int nr_cpus =DEFAULT_NR_CPUS ; -int opt, ran =0 , failed =0 ; +int opt, ran =0 , failed =0 , i;struct conctest_test *test;if (max_cpus <0 ) {
@@ -870,14 +1040,19 @@ int main(int argc, char **argv) delay_us = delay_seed % (delay_max_us +1 );printf ("Seed: %u, delay: %u us (max: %u us)\n" , delay_seed, delay_us, delay_max_us); -for (test = all_tests; test->name; test++) { +generate_all_tests (max_cpus); +printf ("Generated %d tests\n\n" , nr_generated); + +for (i =0 ; i < nr_generated; i++) { + test = &generated_tests[i];if (!test_selected (test->name))continue ; ran++;if (run_test (test, nr_cpus, duration)) failed++; -fprintf (stderr,"\n\n\n" ); +if (verbose) +printf ("\n" ); }if (!ran)
tools/testing/selftests/bpf/progs/conctest.c +229 -1
246 lines not on this page · raw message
Signature · 1 line
2.47.3
Re: [RFC PATCH bpf-next v1 5/6] selftests/bpf: Generate various conctest permutations
tools/testing/selftests/bpf/progs/conctest.c int conctest_rqspinlock_nmi_shift(void *ctx) · line 339
Quoted 10 lines · Kumar Kartikeya Dwivedi
tools/testing/selftests/bpf/progs/conctest.c +9
-0+SEC("?syscall") +int conctest_timer_task_reinit (void *ctx) +{ +struct timer_elem *elem; +int key =0 , ret; + + elem =bpf_map_lookup_elem (&timer_map, &key); +if (!elem) +return 0 ;
here it definitely can be removed.
Kumar Kartikeya Dwivedi
maintainerRFC PATCH 6/6 selftests/bpf: Extend conctest to wq and task_work
In reply to Kumar Kartikeya DwivediExtend coverage to various operations for wq and task_work.
--- tools/testing/selftests/bpf/conctest.c | 46 ++++ tools/testing/selftests/bpf/progs/conctest.c | 241 +++++++++++++++++++ 2 files changed, 287 insertions(+)
tools/testing/selftests/bpf/conctest.c +46 -0
@@ -421,9 +421,55 @@ static struct conctest_suite timer_suite = { .max_cpus = 4, }; +static void wq_init (struct conctest *skel,struct conctest_test *test) +{ +LIBBPF_OPTS (bpf_test_run_opts, opts); +int fd; + + fd =find_prog_fd (skel,"conctest_wq_init" ); +if (fd <0 ) +return ; +bpf_prog_test_run_opts (fd, &opts); +} + +static struct conctest_op wq_ops[] = { + {"start" ,"conctest_wq_task_start" ,"conctest_wq_nmi_start" ,CTX_TASK_OK |CTX_NMI_OK }, + {"set_cb" ,"conctest_wq_task_set_cb" ,"conctest_wq_nmi_set_cb" ,CTX_TASK_OK |CTX_NMI_OK }, + {"delete" ,"conctest_wq_task_delete" ,"conctest_wq_nmi_delete" ,CTX_TASK_OK |CTX_NMI_OK }, + {}, +}; + +static const char *wq_extra_progs[] = {"conctest_wq_init" ,NULL }; + +static struct conctest_suite wq_suite = { + .name ="wq" , + .ops = wq_ops, + .nr_ops =3 , + .init = wq_init, + .extra_progs = wq_extra_progs, + .max_cpus =4 , +}; + +static struct conctest_op tw_ops[] = { + {"signal" ,"conctest_tw_task_signal" ,"conctest_tw_nmi_signal" ,CTX_TASK_OK |CTX_NMI_OK }, + {"resume" ,"conctest_tw_task_resume" ,"conctest_tw_nmi_resume" ,CTX_TASK_OK |CTX_NMI_OK }, + {"delete" ,"conctest_tw_task_delete" ,"conctest_tw_nmi_delete" ,CTX_TASK_OK |CTX_NMI_OK }, + {}, +}; + +static struct conctest_suite tw_suite = { + .name ="task_work" , + .ops = tw_ops, + .nr_ops =3 , + .init =NULL , + .max_cpus =4 , +}; +static struct conctest_suite *suites[] = { &rqspinlock_suite, &timer_suite, + &wq_suite, + &tw_suite,NULL , };
tools/testing/selftests/bpf/progs/conctest.c +241 -0
245 lines not on this page · raw message
Signature · 1 line
2.47.3