xref: /linux/tools/testing/selftests/bpf/progs/task_ls_uptr.c (revision 60675d4ca1ef0857e44eba5849b74a3a998d0c0f)
1*4579b4a4SKui-Feng Lee // SPDX-License-Identifier: GPL-2.0
2*4579b4a4SKui-Feng Lee /* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */
3*4579b4a4SKui-Feng Lee 
4*4579b4a4SKui-Feng Lee #include <vmlinux.h>
5*4579b4a4SKui-Feng Lee #include <bpf/bpf_helpers.h>
6*4579b4a4SKui-Feng Lee #include "uptr_test_common.h"
7*4579b4a4SKui-Feng Lee 
8*4579b4a4SKui-Feng Lee struct task_struct *bpf_task_from_pid(s32 pid) __ksym;
9*4579b4a4SKui-Feng Lee void bpf_task_release(struct task_struct *p) __ksym;
10*4579b4a4SKui-Feng Lee void bpf_cgroup_release(struct cgroup *cgrp) __ksym;
11*4579b4a4SKui-Feng Lee 
12*4579b4a4SKui-Feng Lee struct {
13*4579b4a4SKui-Feng Lee 	__uint(type, BPF_MAP_TYPE_TASK_STORAGE);
14*4579b4a4SKui-Feng Lee 	__uint(map_flags, BPF_F_NO_PREALLOC);
15*4579b4a4SKui-Feng Lee 	__type(key, int);
16*4579b4a4SKui-Feng Lee 	__type(value, struct value_type);
17*4579b4a4SKui-Feng Lee } datamap SEC(".maps");
18*4579b4a4SKui-Feng Lee 
19*4579b4a4SKui-Feng Lee pid_t target_pid = 0;
20*4579b4a4SKui-Feng Lee pid_t parent_pid = 0;
21*4579b4a4SKui-Feng Lee 
22*4579b4a4SKui-Feng Lee SEC("tp_btf/sys_enter")
23*4579b4a4SKui-Feng Lee int on_enter(__u64 *ctx)
24*4579b4a4SKui-Feng Lee {
25*4579b4a4SKui-Feng Lee 	struct task_struct *task, *data_task;
26*4579b4a4SKui-Feng Lee 	struct value_type *ptr;
27*4579b4a4SKui-Feng Lee 	struct user_data *udata;
28*4579b4a4SKui-Feng Lee 	struct cgroup *cgrp;
29*4579b4a4SKui-Feng Lee 
30*4579b4a4SKui-Feng Lee 	task = bpf_get_current_task_btf();
31*4579b4a4SKui-Feng Lee 	if (task->pid != target_pid)
32*4579b4a4SKui-Feng Lee 		return 0;
33*4579b4a4SKui-Feng Lee 
34*4579b4a4SKui-Feng Lee 	data_task = bpf_task_from_pid(parent_pid);
35*4579b4a4SKui-Feng Lee 	if (!data_task)
36*4579b4a4SKui-Feng Lee 		return 0;
37*4579b4a4SKui-Feng Lee 
38*4579b4a4SKui-Feng Lee 	ptr = bpf_task_storage_get(&datamap, data_task, 0, 0);
39*4579b4a4SKui-Feng Lee 	bpf_task_release(data_task);
40*4579b4a4SKui-Feng Lee 	if (!ptr)
41*4579b4a4SKui-Feng Lee 		return 0;
42*4579b4a4SKui-Feng Lee 
43*4579b4a4SKui-Feng Lee 	cgrp = bpf_kptr_xchg(&ptr->cgrp, NULL);
44*4579b4a4SKui-Feng Lee 	if (cgrp) {
45*4579b4a4SKui-Feng Lee 		int lvl = cgrp->level;
46*4579b4a4SKui-Feng Lee 
47*4579b4a4SKui-Feng Lee 		bpf_cgroup_release(cgrp);
48*4579b4a4SKui-Feng Lee 		return lvl;
49*4579b4a4SKui-Feng Lee 	}
50*4579b4a4SKui-Feng Lee 
51*4579b4a4SKui-Feng Lee 	udata = ptr->udata;
52*4579b4a4SKui-Feng Lee 	if (!udata || udata->result)
53*4579b4a4SKui-Feng Lee 		return 0;
54*4579b4a4SKui-Feng Lee 	udata->result = MAGIC_VALUE + udata->a + udata->b;
55*4579b4a4SKui-Feng Lee 
56*4579b4a4SKui-Feng Lee 	udata = ptr->nested.udata;
57*4579b4a4SKui-Feng Lee 	if (udata && !udata->nested_result)
58*4579b4a4SKui-Feng Lee 		udata->nested_result = udata->result;
59*4579b4a4SKui-Feng Lee 
60*4579b4a4SKui-Feng Lee 	return 0;
61*4579b4a4SKui-Feng Lee }
62*4579b4a4SKui-Feng Lee 
63*4579b4a4SKui-Feng Lee char _license[] SEC("license") = "GPL";
64