xref: /linux/tools/testing/selftests/sched_ext/select_cpu_dispatch.bpf.c (revision daa9f66fe194f672d2c94d879b6dad7035e03ebe)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * A scheduler that validates the behavior of direct dispatching with a default
4  * select_cpu implementation.
5  *
6  * Copyright (c) 2023 Meta Platforms, Inc. and affiliates.
7  * Copyright (c) 2023 David Vernet <dvernet@meta.com>
8  * Copyright (c) 2023 Tejun Heo <tj@kernel.org>
9  */
10 
11 #include <scx/common.bpf.h>
12 
13 char _license[] SEC("license") = "GPL";
14 
BPF_STRUCT_OPS(select_cpu_dispatch_select_cpu,struct task_struct * p,s32 prev_cpu,u64 wake_flags)15 s32 BPF_STRUCT_OPS(select_cpu_dispatch_select_cpu, struct task_struct *p,
16 		   s32 prev_cpu, u64 wake_flags)
17 {
18 	u64 dsq_id = SCX_DSQ_LOCAL;
19 	s32 cpu = prev_cpu;
20 
21 	if (scx_bpf_test_and_clear_cpu_idle(cpu))
22 		goto dispatch;
23 
24 	cpu = scx_bpf_pick_idle_cpu(p->cpus_ptr, 0);
25 	if (cpu >= 0)
26 		goto dispatch;
27 
28 	dsq_id = SCX_DSQ_GLOBAL;
29 	cpu = prev_cpu;
30 
31 dispatch:
32 	scx_bpf_dispatch(p, dsq_id, SCX_SLICE_DFL, 0);
33 	return cpu;
34 }
35 
36 SEC(".struct_ops.link")
37 struct sched_ext_ops select_cpu_dispatch_ops = {
38 	.select_cpu		= (void *) select_cpu_dispatch_select_cpu,
39 	.name			= "select_cpu_dispatch",
40 	.timeout_ms		= 1000U,
41 };
42