xref: /linux/tools/testing/selftests/sched_ext/exit.bpf.c (revision daa9f66fe194f672d2c94d879b6dad7035e03ebe)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (c) 2024 Meta Platforms, Inc. and affiliates.
4  * Copyright (c) 2024 David Vernet <dvernet@meta.com>
5  */
6 
7 #include <scx/common.bpf.h>
8 
9 char _license[] SEC("license") = "GPL";
10 
11 #include "exit_test.h"
12 
13 const volatile int exit_point;
14 UEI_DEFINE(uei);
15 
16 #define EXIT_CLEANLY() scx_bpf_exit(exit_point, "%d", exit_point)
17 
18 #define DSQ_ID 0
19 
BPF_STRUCT_OPS(exit_select_cpu,struct task_struct * p,s32 prev_cpu,u64 wake_flags)20 s32 BPF_STRUCT_OPS(exit_select_cpu, struct task_struct *p,
21 		   s32 prev_cpu, u64 wake_flags)
22 {
23 	bool found;
24 
25 	if (exit_point == EXIT_SELECT_CPU)
26 		EXIT_CLEANLY();
27 
28 	return scx_bpf_select_cpu_dfl(p, prev_cpu, wake_flags, &found);
29 }
30 
BPF_STRUCT_OPS(exit_enqueue,struct task_struct * p,u64 enq_flags)31 void BPF_STRUCT_OPS(exit_enqueue, struct task_struct *p, u64 enq_flags)
32 {
33 	if (exit_point == EXIT_ENQUEUE)
34 		EXIT_CLEANLY();
35 
36 	scx_bpf_dispatch(p, DSQ_ID, SCX_SLICE_DFL, enq_flags);
37 }
38 
BPF_STRUCT_OPS(exit_dispatch,s32 cpu,struct task_struct * p)39 void BPF_STRUCT_OPS(exit_dispatch, s32 cpu, struct task_struct *p)
40 {
41 	if (exit_point == EXIT_DISPATCH)
42 		EXIT_CLEANLY();
43 
44 	scx_bpf_consume(DSQ_ID);
45 }
46 
BPF_STRUCT_OPS(exit_enable,struct task_struct * p)47 void BPF_STRUCT_OPS(exit_enable, struct task_struct *p)
48 {
49 	if (exit_point == EXIT_ENABLE)
50 		EXIT_CLEANLY();
51 }
52 
BPF_STRUCT_OPS(exit_init_task,struct task_struct * p,struct scx_init_task_args * args)53 s32 BPF_STRUCT_OPS(exit_init_task, struct task_struct *p,
54 		    struct scx_init_task_args *args)
55 {
56 	if (exit_point == EXIT_INIT_TASK)
57 		EXIT_CLEANLY();
58 
59 	return 0;
60 }
61 
BPF_STRUCT_OPS(exit_exit,struct scx_exit_info * ei)62 void BPF_STRUCT_OPS(exit_exit, struct scx_exit_info *ei)
63 {
64 	UEI_RECORD(uei, ei);
65 }
66 
BPF_STRUCT_OPS_SLEEPABLE(exit_init)67 s32 BPF_STRUCT_OPS_SLEEPABLE(exit_init)
68 {
69 	if (exit_point == EXIT_INIT)
70 		EXIT_CLEANLY();
71 
72 	return scx_bpf_create_dsq(DSQ_ID, -1);
73 }
74 
75 SEC(".struct_ops.link")
76 struct sched_ext_ops exit_ops = {
77 	.select_cpu		= (void *) exit_select_cpu,
78 	.enqueue		= (void *) exit_enqueue,
79 	.dispatch		= (void *) exit_dispatch,
80 	.init_task		= (void *) exit_init_task,
81 	.enable			= (void *) exit_enable,
82 	.exit			= (void *) exit_exit,
83 	.init			= (void *) exit_init,
84 	.name			= "exit",
85 	.timeout_ms		= 1000U,
86 };
87