summaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/bpf/progs/test_ctx.c
blob: 7d499550671731f65023d1baf64a66edb395e256 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (c) 2026 Valve Corporation.
 * Author: Changwoo Min <changwoo@igalia.com>
 */

#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
#include "bpf_experimental.h"

char _license[] SEC("license") = "GPL";

extern void bpf_kfunc_trigger_ctx_check(void) __ksym;

int count_hardirq;
int count_softirq;
int count_task;

/* Triggered via bpf_prog_test_run from user-space */
SEC("syscall")
int trigger_all_contexts(void *ctx)
{
	if (bpf_in_task())
		__sync_fetch_and_add(&count_task, 1);

	/* Trigger the firing of a hardirq and softirq for test. */
	bpf_kfunc_trigger_ctx_check();
	return 0;
}

/* Observer for HardIRQ */
SEC("fentry/bpf_testmod_test_hardirq_fn")
int BPF_PROG(on_hardirq)
{
	if (bpf_in_hardirq())
		__sync_fetch_and_add(&count_hardirq, 1);
	return 0;
}

/* Observer for SoftIRQ */
SEC("fentry/bpf_testmod_test_softirq_fn")
int BPF_PROG(on_softirq)
{
	if (bpf_in_serving_softirq())
		__sync_fetch_and_add(&count_softirq, 1);
	return 0;
}