summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/sched_ext/cyclic_kick_wait.bpf.c
blob: cb34d3335917ab29ca9a3e331ea8e984a20e132d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/* SPDX-License-Identifier: GPL-2.0 */
/*
 * Stress concurrent SCX_KICK_WAIT calls to reproduce wait-cycle deadlock.
 *
 * Three CPUs are designated from userspace. Every enqueue from one of the
 * three CPUs kicks the next CPU in the ring with SCX_KICK_WAIT, creating a
 * persistent A -> B -> C -> A wait cycle pressure.
 */
#include <scx/common.bpf.h>

char _license[] SEC("license") = "GPL";

const volatile s32 test_cpu_a;
const volatile s32 test_cpu_b;
const volatile s32 test_cpu_c;

u64 nr_enqueues;
u64 nr_wait_kicks;

UEI_DEFINE(uei);

static s32 target_cpu(s32 cpu)
{
	if (cpu == test_cpu_a)
		return test_cpu_b;
	if (cpu == test_cpu_b)
		return test_cpu_c;
	if (cpu == test_cpu_c)
		return test_cpu_a;
	return -1;
}

void BPF_STRUCT_OPS(cyclic_kick_wait_enqueue, struct task_struct *p,
		    u64 enq_flags)
{
	s32 this_cpu = bpf_get_smp_processor_id();
	s32 tgt;

	__sync_fetch_and_add(&nr_enqueues, 1);

	if (p->flags & PF_KTHREAD) {
		scx_bpf_dsq_insert(p, SCX_DSQ_LOCAL, SCX_SLICE_INF,
				   enq_flags | SCX_ENQ_PREEMPT);
		return;
	}

	scx_bpf_dsq_insert(p, SCX_DSQ_GLOBAL, SCX_SLICE_DFL, enq_flags);

	tgt = target_cpu(this_cpu);
	if (tgt < 0 || tgt == this_cpu)
		return;

	__sync_fetch_and_add(&nr_wait_kicks, 1);
	scx_bpf_kick_cpu(tgt, SCX_KICK_WAIT);
}

void BPF_STRUCT_OPS(cyclic_kick_wait_exit, struct scx_exit_info *ei)
{
	UEI_RECORD(uei, ei);
}

SEC(".struct_ops.link")
struct sched_ext_ops cyclic_kick_wait_ops = {
	.enqueue		= cyclic_kick_wait_enqueue,
	.exit			= cyclic_kick_wait_exit,
	.name			= "cyclic_kick_wait",
	.timeout_ms		= 1000U,
};