xref: /linux/tools/testing/selftests/sched_ext/init_enable_count.bpf.c (revision 566ab427f827b0256d3e8ce0235d088e6a9c28bd)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * A scheduler that verifies that we do proper counting of init, enable, etc
4  * callbacks.
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 
15 u64 init_task_cnt, exit_task_cnt, enable_cnt, disable_cnt;
16 u64 init_fork_cnt, init_transition_cnt;
17 
18 s32 BPF_STRUCT_OPS_SLEEPABLE(cnt_init_task, struct task_struct *p,
19 			     struct scx_init_task_args *args)
20 {
21 	__sync_fetch_and_add(&init_task_cnt, 1);
22 
23 	if (args->fork)
24 		__sync_fetch_and_add(&init_fork_cnt, 1);
25 	else
26 		__sync_fetch_and_add(&init_transition_cnt, 1);
27 
28 	return 0;
29 }
30 
31 void BPF_STRUCT_OPS(cnt_exit_task, struct task_struct *p)
32 {
33 	__sync_fetch_and_add(&exit_task_cnt, 1);
34 }
35 
36 void BPF_STRUCT_OPS(cnt_enable, struct task_struct *p)
37 {
38 	__sync_fetch_and_add(&enable_cnt, 1);
39 }
40 
41 void BPF_STRUCT_OPS(cnt_disable, struct task_struct *p)
42 {
43 	__sync_fetch_and_add(&disable_cnt, 1);
44 }
45 
46 SEC(".struct_ops.link")
47 struct sched_ext_ops init_enable_count_ops = {
48 	.init_task	= cnt_init_task,
49 	.exit_task	= cnt_exit_task,
50 	.enable		= cnt_enable,
51 	.disable	= cnt_disable,
52 	.name		= "init_enable_count",
53 };
54