xref: /linux/tools/testing/selftests/bpf/progs/struct_ops_assoc.c (revision c17ee635fd3a482b2ad2bf5e269755c2eae5f25e)
133a165f9SAmery Hung // SPDX-License-Identifier: GPL-2.0
233a165f9SAmery Hung 
333a165f9SAmery Hung #include <vmlinux.h>
433a165f9SAmery Hung #include <bpf/bpf_tracing.h>
533a165f9SAmery Hung #include "bpf_misc.h"
633a165f9SAmery Hung #include "../test_kmods/bpf_testmod.h"
733a165f9SAmery Hung #include "../test_kmods/bpf_testmod_kfunc.h"
833a165f9SAmery Hung 
933a165f9SAmery Hung char _license[] SEC("license") = "GPL";
1033a165f9SAmery Hung 
1133a165f9SAmery Hung int test_pid;
1233a165f9SAmery Hung 
1333a165f9SAmery Hung /* Programs associated with st_ops_map_a */
1433a165f9SAmery Hung 
1533a165f9SAmery Hung #define MAP_A_MAGIC 1234
1633a165f9SAmery Hung int test_err_a;
1733a165f9SAmery Hung 
1833a165f9SAmery Hung SEC("struct_ops")
1933a165f9SAmery Hung int BPF_PROG(test_1_a, struct st_ops_args *args)
2033a165f9SAmery Hung {
2133a165f9SAmery Hung 	return MAP_A_MAGIC;
2233a165f9SAmery Hung }
2333a165f9SAmery Hung 
2433a165f9SAmery Hung SEC("tp_btf/sys_enter")
2533a165f9SAmery Hung int BPF_PROG(sys_enter_prog_a, struct pt_regs *regs, long id)
2633a165f9SAmery Hung {
2733a165f9SAmery Hung 	struct st_ops_args args = {};
2833a165f9SAmery Hung 	struct task_struct *task;
2933a165f9SAmery Hung 	int ret;
3033a165f9SAmery Hung 
3133a165f9SAmery Hung 	task = bpf_get_current_task_btf();
3233a165f9SAmery Hung 	if (!test_pid || task->pid != test_pid)
3333a165f9SAmery Hung 		return 0;
3433a165f9SAmery Hung 
35*bd06b977SIhor Solodrai 	ret = bpf_kfunc_multi_st_ops_test_1_assoc(&args);
3633a165f9SAmery Hung 	if (ret != MAP_A_MAGIC)
3733a165f9SAmery Hung 		test_err_a++;
3833a165f9SAmery Hung 
3933a165f9SAmery Hung 	return 0;
4033a165f9SAmery Hung }
4133a165f9SAmery Hung 
4233a165f9SAmery Hung SEC("syscall")
4333a165f9SAmery Hung int syscall_prog_a(void *ctx)
4433a165f9SAmery Hung {
4533a165f9SAmery Hung 	struct st_ops_args args = {};
4633a165f9SAmery Hung 	int ret;
4733a165f9SAmery Hung 
48*bd06b977SIhor Solodrai 	ret = bpf_kfunc_multi_st_ops_test_1_assoc(&args);
4933a165f9SAmery Hung 	if (ret != MAP_A_MAGIC)
5033a165f9SAmery Hung 		test_err_a++;
5133a165f9SAmery Hung 
5233a165f9SAmery Hung 	return 0;
5333a165f9SAmery Hung }
5433a165f9SAmery Hung 
5533a165f9SAmery Hung SEC(".struct_ops.link")
5633a165f9SAmery Hung struct bpf_testmod_multi_st_ops st_ops_map_a = {
5733a165f9SAmery Hung 	.test_1 = (void *)test_1_a,
5833a165f9SAmery Hung };
5933a165f9SAmery Hung 
6033a165f9SAmery Hung /* Programs associated with st_ops_map_b */
6133a165f9SAmery Hung 
6233a165f9SAmery Hung #define MAP_B_MAGIC 5678
6333a165f9SAmery Hung int test_err_b;
6433a165f9SAmery Hung 
6533a165f9SAmery Hung SEC("struct_ops")
6633a165f9SAmery Hung int BPF_PROG(test_1_b, struct st_ops_args *args)
6733a165f9SAmery Hung {
6833a165f9SAmery Hung 	return MAP_B_MAGIC;
6933a165f9SAmery Hung }
7033a165f9SAmery Hung 
7133a165f9SAmery Hung SEC("tp_btf/sys_enter")
7233a165f9SAmery Hung int BPF_PROG(sys_enter_prog_b, struct pt_regs *regs, long id)
7333a165f9SAmery Hung {
7433a165f9SAmery Hung 	struct st_ops_args args = {};
7533a165f9SAmery Hung 	struct task_struct *task;
7633a165f9SAmery Hung 	int ret;
7733a165f9SAmery Hung 
7833a165f9SAmery Hung 	task = bpf_get_current_task_btf();
7933a165f9SAmery Hung 	if (!test_pid || task->pid != test_pid)
8033a165f9SAmery Hung 		return 0;
8133a165f9SAmery Hung 
82*bd06b977SIhor Solodrai 	ret = bpf_kfunc_multi_st_ops_test_1_assoc(&args);
8333a165f9SAmery Hung 	if (ret != MAP_B_MAGIC)
8433a165f9SAmery Hung 		test_err_b++;
8533a165f9SAmery Hung 
8633a165f9SAmery Hung 	return 0;
8733a165f9SAmery Hung }
8833a165f9SAmery Hung 
8933a165f9SAmery Hung SEC("syscall")
9033a165f9SAmery Hung int syscall_prog_b(void *ctx)
9133a165f9SAmery Hung {
9233a165f9SAmery Hung 	struct st_ops_args args = {};
9333a165f9SAmery Hung 	int ret;
9433a165f9SAmery Hung 
95*bd06b977SIhor Solodrai 	ret = bpf_kfunc_multi_st_ops_test_1_assoc(&args);
9633a165f9SAmery Hung 	if (ret != MAP_B_MAGIC)
9733a165f9SAmery Hung 		test_err_b++;
9833a165f9SAmery Hung 
9933a165f9SAmery Hung 	return 0;
10033a165f9SAmery Hung }
10133a165f9SAmery Hung 
10233a165f9SAmery Hung SEC(".struct_ops.link")
10333a165f9SAmery Hung struct bpf_testmod_multi_st_ops st_ops_map_b = {
10433a165f9SAmery Hung 	.test_1 = (void *)test_1_b,
10533a165f9SAmery Hung };
106