xref: /linux/tools/sched_ext/scx_cpu0.bpf.c (revision b2128290c29902315e632ea59e0504d6bc9e9b42)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * A CPU0 scheduler.
4  *
5  * This scheduler queues all tasks to a shared DSQ and only dispatches them on
6  * CPU0 in FIFO order. This is useful for testing bypass behavior when many
7  * tasks are concentrated on a single CPU. If the load balancer doesn't work,
8  * bypass mode can trigger task hangs or RCU stalls as the queue is long and
9  * there's only one CPU working on it.
10  *
11  * - Statistics tracking how many tasks are queued to local and CPU0 DSQs.
12  * - Termination notification for userspace.
13  *
14  * Copyright (c) 2025 Meta Platforms, Inc. and affiliates.
15  * Copyright (c) 2025 Tejun Heo <tj@kernel.org>
16  */
17 #include <scx/common.bpf.h>
18 
19 char _license[] SEC("license") = "GPL";
20 
21 UEI_DEFINE(uei);
22 
23 /*
24  * We create a custom DSQ with ID 0 that we dispatch to and consume from on
25  * CPU0.
26  */
27 #define DSQ_CPU0 0
28 
29 struct {
30 	__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
31 	__uint(key_size, sizeof(u32));
32 	__uint(value_size, sizeof(u64));
33 	__uint(max_entries, 2);			/* [local, cpu0] */
34 } stats SEC(".maps");
35 
36 static void stat_inc(u32 idx)
37 {
38 	u64 *cnt_p = bpf_map_lookup_elem(&stats, &idx);
39 	if (cnt_p)
40 		(*cnt_p)++;
41 }
42 
43 s32 BPF_STRUCT_OPS(cpu0_select_cpu, struct task_struct *p, s32 prev_cpu, u64 wake_flags)
44 {
45 	return 0;
46 }
47 
48 void BPF_STRUCT_OPS(cpu0_enqueue, struct task_struct *p, u64 enq_flags)
49 {
50 	/*
51 	 * select_cpu() always picks CPU0. If @p is not on CPU0, it can't run on
52 	 * CPU 0. Queue on whichever CPU it's currently only.
53 	 */
54 	if (scx_bpf_task_cpu(p) != 0) {
55 		stat_inc(0);	/* count local queueing */
56 		scx_bpf_dsq_insert(p, SCX_DSQ_LOCAL, SCX_SLICE_DFL, 0);
57 		return;
58 	}
59 
60 	stat_inc(1);	/* count cpu0 queueing */
61 	scx_bpf_dsq_insert(p, DSQ_CPU0, SCX_SLICE_DFL, enq_flags);
62 }
63 
64 void BPF_STRUCT_OPS(cpu0_dispatch, s32 cpu, struct task_struct *prev)
65 {
66 	if (cpu == 0)
67 		scx_bpf_dsq_move_to_local(DSQ_CPU0, 0);
68 }
69 
70 s32 BPF_STRUCT_OPS_SLEEPABLE(cpu0_init)
71 {
72 	int ret;
73 
74 	ret = scx_bpf_create_dsq(DSQ_CPU0, -1);
75 	if (ret) {
76 		scx_bpf_error("failed to create DSQ %d (%d)", DSQ_CPU0, ret);
77 		return ret;
78 	}
79 
80 	return 0;
81 }
82 
83 void BPF_STRUCT_OPS(cpu0_exit, struct scx_exit_info *ei)
84 {
85 	UEI_RECORD(uei, ei);
86 }
87 
88 SCX_OPS_DEFINE(cpu0_ops,
89 	       .select_cpu		= (void *)cpu0_select_cpu,
90 	       .enqueue			= (void *)cpu0_enqueue,
91 	       .dispatch		= (void *)cpu0_dispatch,
92 	       .init			= (void *)cpu0_init,
93 	       .exit			= (void *)cpu0_exit,
94 	       .name			= "cpu0");
95