133965Sjdp// SPDX-License-Identifier: GPL-2.0
238889Sjdp/* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */
333965Sjdp
433965Sjdp#include <vmlinux.h>
533965Sjdp#include <bpf/bpf_tracing.h>
633965Sjdp#include <bpf/bpf_helpers.h>
733965Sjdp
833965Sjdp#include "bpf_misc.h"
933965Sjdp#include "cgrp_kfunc_common.h"
1033965Sjdp#include "cpumask_common.h"
1133965Sjdp#include "task_kfunc_common.h"
1233965Sjdp
1333965Sjdpchar _license[] SEC("license") = "GPL";
1433965Sjdp
1533965Sjdp/***************
1633965Sjdp * Task kfuncs *
1733965Sjdp ***************/
1833965Sjdp
1933965Sjdpstatic void task_kfunc_load_test(void)
2033965Sjdp{
2133965Sjdp	struct task_struct *current, *ref_1, *ref_2;
2233965Sjdp
2333965Sjdp	current = bpf_get_current_task_btf();
2433965Sjdp	ref_1 = bpf_task_from_pid(current->pid);
2533965Sjdp	if (!ref_1)
2633965Sjdp		return;
2733965Sjdp
2833965Sjdp	ref_2 = bpf_task_acquire(ref_1);
2933965Sjdp	if (ref_2)
3033965Sjdp		bpf_task_release(ref_2);
3133965Sjdp	bpf_task_release(ref_1);
3233965Sjdp}
3333965Sjdp
3433965SjdpSEC("raw_tp")
3533965Sjdp__failure __msg("calling kernel function")
3633965Sjdpint BPF_PROG(task_kfunc_raw_tp)
3733965Sjdp{
3833965Sjdp	task_kfunc_load_test();
3933965Sjdp	return 0;
4033965Sjdp}
4133965Sjdp
4233965SjdpSEC("syscall")
4333965Sjdp__success
4433965Sjdpint BPF_PROG(task_kfunc_syscall)
4533965Sjdp{
4633965Sjdp	task_kfunc_load_test();
4733965Sjdp	return 0;
4833965Sjdp}
4933965Sjdp
5033965Sjdp/*****************
5133965Sjdp * cgroup kfuncs *
5233965Sjdp *****************/
5333965Sjdp
5433965Sjdpstatic void cgrp_kfunc_load_test(void)
5533965Sjdp{
5633965Sjdp	struct cgroup *cgrp, *ref;
5733965Sjdp
5833965Sjdp	cgrp = bpf_cgroup_from_id(0);
5933965Sjdp	if (!cgrp)
6033965Sjdp		return;
6133965Sjdp
6233965Sjdp	ref = bpf_cgroup_acquire(cgrp);
6333965Sjdp	if (!ref) {
6433965Sjdp		bpf_cgroup_release(cgrp);
6533965Sjdp		return;
6633965Sjdp	}
6733965Sjdp
6833965Sjdp	bpf_cgroup_release(ref);
6933965Sjdp	bpf_cgroup_release(cgrp);
7033965Sjdp}
7133965Sjdp
7233965SjdpSEC("raw_tp")
7333965Sjdp__failure __msg("calling kernel function")
7433965Sjdpint BPF_PROG(cgrp_kfunc_raw_tp)
7533965Sjdp{
7633965Sjdp	cgrp_kfunc_load_test();
7733965Sjdp	return 0;
7833965Sjdp}
7933965Sjdp
8033965SjdpSEC("syscall")
8133965Sjdp__success
8233965Sjdpint BPF_PROG(cgrp_kfunc_syscall)
8333965Sjdp{
8433965Sjdp	cgrp_kfunc_load_test();
8533965Sjdp	return 0;
8633965Sjdp}
8733965Sjdp
8833965Sjdp/******************
8933965Sjdp * cpumask kfuncs *
9033965Sjdp ******************/
9133965Sjdp
9233965Sjdpstatic void cpumask_kfunc_load_test(void)
9333965Sjdp{
9433965Sjdp	struct bpf_cpumask *alloc, *ref;
9533965Sjdp
9633965Sjdp	alloc = bpf_cpumask_create();
9733965Sjdp	if (!alloc)
9833965Sjdp		return;
9933965Sjdp
10033965Sjdp	ref = bpf_cpumask_acquire(alloc);
10133965Sjdp	bpf_cpumask_set_cpu(0, alloc);
10233965Sjdp	bpf_cpumask_test_cpu(0, (const struct cpumask *)ref);
10333965Sjdp
10433965Sjdp	bpf_cpumask_release(ref);
10533965Sjdp	bpf_cpumask_release(alloc);
10633965Sjdp}
10733965Sjdp
10833965SjdpSEC("raw_tp")
10933965Sjdp__failure __msg("calling kernel function")
11033965Sjdpint BPF_PROG(cpumask_kfunc_raw_tp)
11133965Sjdp{
11233965Sjdp	cpumask_kfunc_load_test();
11333965Sjdp	return 0;
11433965Sjdp}
11533965Sjdp
11633965SjdpSEC("syscall")
11733965Sjdp__success
11833965Sjdpint BPF_PROG(cpumask_kfunc_syscall)
11933965Sjdp{
12033965Sjdp	cpumask_kfunc_load_test();
12133965Sjdp	return 0;
12233965Sjdp}
12333965Sjdp