xref: /linux/tools/testing/selftests/bpf/progs/test_ctx.c (revision 221b5e76c1c6e8ad4fa7c95a689e44ff45daab1c)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2026 Valve Corporation.
4  * Author: Changwoo Min <changwoo@igalia.com>
5  */
6 
7 #include "vmlinux.h"
8 #include <bpf/bpf_helpers.h>
9 #include <bpf/bpf_tracing.h>
10 #include "bpf_experimental.h"
11 
12 char _license[] SEC("license") = "GPL";
13 
14 extern void bpf_kfunc_trigger_ctx_check(void) __ksym;
15 
16 int count_hardirq;
17 int count_softirq;
18 int count_task;
19 
20 /* Triggered via bpf_prog_test_run from user-space */
21 SEC("syscall")
22 int trigger_all_contexts(void *ctx)
23 {
24 	if (bpf_in_task())
25 		__sync_fetch_and_add(&count_task, 1);
26 
27 	/* Trigger the firing of a hardirq and softirq for test. */
28 	bpf_kfunc_trigger_ctx_check();
29 	return 0;
30 }
31 
32 /* Observer for HardIRQ */
33 SEC("fentry/bpf_testmod_test_hardirq_fn")
34 int BPF_PROG(on_hardirq)
35 {
36 	if (bpf_in_hardirq())
37 		__sync_fetch_and_add(&count_hardirq, 1);
38 	return 0;
39 }
40 
41 /* Observer for SoftIRQ */
42 SEC("fentry/bpf_testmod_test_softirq_fn")
43 int BPF_PROG(on_softirq)
44 {
45 	if (bpf_in_serving_softirq())
46 		__sync_fetch_and_add(&count_softirq, 1);
47 	return 0;
48 }
49