xref: /linux/tools/testing/selftests/sched_ext/cyclic_kick_wait.bpf.c (revision 9147566d801602c9e7fc7f85e989735735bf38ba)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Stress concurrent SCX_KICK_WAIT calls to reproduce wait-cycle deadlock.
4  *
5  * Three CPUs are designated from userspace. Every enqueue from one of the
6  * three CPUs kicks the next CPU in the ring with SCX_KICK_WAIT, creating a
7  * persistent A -> B -> C -> A wait cycle pressure.
8  */
9 #include <scx/common.bpf.h>
10 
11 char _license[] SEC("license") = "GPL";
12 
13 const volatile s32 test_cpu_a;
14 const volatile s32 test_cpu_b;
15 const volatile s32 test_cpu_c;
16 
17 u64 nr_enqueues;
18 u64 nr_wait_kicks;
19 
20 UEI_DEFINE(uei);
21 
target_cpu(s32 cpu)22 static s32 target_cpu(s32 cpu)
23 {
24 	if (cpu == test_cpu_a)
25 		return test_cpu_b;
26 	if (cpu == test_cpu_b)
27 		return test_cpu_c;
28 	if (cpu == test_cpu_c)
29 		return test_cpu_a;
30 	return -1;
31 }
32 
BPF_STRUCT_OPS(cyclic_kick_wait_enqueue,struct task_struct * p,u64 enq_flags)33 void BPF_STRUCT_OPS(cyclic_kick_wait_enqueue, struct task_struct *p,
34 		    u64 enq_flags)
35 {
36 	s32 this_cpu = bpf_get_smp_processor_id();
37 	s32 tgt;
38 
39 	__sync_fetch_and_add(&nr_enqueues, 1);
40 
41 	if (p->flags & PF_KTHREAD) {
42 		scx_bpf_dsq_insert(p, SCX_DSQ_LOCAL, SCX_SLICE_INF,
43 				   enq_flags | SCX_ENQ_PREEMPT);
44 		return;
45 	}
46 
47 	scx_bpf_dsq_insert(p, SCX_DSQ_GLOBAL, SCX_SLICE_DFL, enq_flags);
48 
49 	tgt = target_cpu(this_cpu);
50 	if (tgt < 0 || tgt == this_cpu)
51 		return;
52 
53 	__sync_fetch_and_add(&nr_wait_kicks, 1);
54 	scx_bpf_kick_cpu(tgt, SCX_KICK_WAIT);
55 }
56 
BPF_STRUCT_OPS(cyclic_kick_wait_exit,struct scx_exit_info * ei)57 void BPF_STRUCT_OPS(cyclic_kick_wait_exit, struct scx_exit_info *ei)
58 {
59 	UEI_RECORD(uei, ei);
60 }
61 
62 SEC(".struct_ops.link")
63 struct sched_ext_ops cyclic_kick_wait_ops = {
64 	.enqueue		= cyclic_kick_wait_enqueue,
65 	.exit			= cyclic_kick_wait_exit,
66 	.name			= "cyclic_kick_wait",
67 	.timeout_ms		= 1000U,
68 };
69