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