summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/bpf/progs/cgrp_kfunc_common.h
blob: 73ba32e9a693125a840ea5c9e5de3e6d28ab4274 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/* SPDX-License-Identifier: GPL-2.0 */
/* Copyright (c) 2022 Meta Platforms, Inc. and affiliates. */

#ifndef _CGRP_KFUNC_COMMON_H
#define _CGRP_KFUNC_COMMON_H

#include <errno.h>
#include <vmlinux.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>

struct __cgrps_kfunc_map_value {
	struct cgroup __kptr * cgrp;
};

struct {
	__uint(type, BPF_MAP_TYPE_HASH);
	__type(key, int);
	__type(value, struct __cgrps_kfunc_map_value);
	__uint(max_entries, 1);
} __cgrps_kfunc_map SEC(".maps");

struct cgroup *bpf_cgroup_acquire(struct cgroup *p) __ksym;
void bpf_cgroup_release(struct cgroup *p) __ksym;
struct cgroup *bpf_cgroup_ancestor(struct cgroup *cgrp, int level) __ksym;
struct cgroup *bpf_cgroup_from_id(u64 cgid) __ksym;
void bpf_rcu_read_lock(void) __ksym;
void bpf_rcu_read_unlock(void) __ksym;

static inline struct __cgrps_kfunc_map_value *cgrps_kfunc_map_value_lookup(struct cgroup *cgrp)
{
	s32 id;
	long status;

	status = bpf_probe_read_kernel(&id, sizeof(id), &cgrp->self.id);
	if (status)
		return NULL;

	return bpf_map_lookup_elem(&__cgrps_kfunc_map, &id);
}

static inline int cgrps_kfunc_map_insert(struct cgroup *cgrp)
{
	struct __cgrps_kfunc_map_value local, *v;
	long status;
	struct cgroup *acquired, *old;
	s32 id;

	status = bpf_probe_read_kernel(&id, sizeof(id), &cgrp->self.id);
	if (status)
		return status;

	local.cgrp = NULL;
	status = bpf_map_update_elem(&__cgrps_kfunc_map, &id, &local, BPF_NOEXIST);
	if (status)
		return status;

	v = bpf_map_lookup_elem(&__cgrps_kfunc_map, &id);
	if (!v) {
		bpf_map_delete_elem(&__cgrps_kfunc_map, &id);
		return -ENOENT;
	}

	acquired = bpf_cgroup_acquire(cgrp);
	if (!acquired) {
		bpf_map_delete_elem(&__cgrps_kfunc_map, &id);
		return -ENOENT;
	}

	old = bpf_kptr_xchg(&v->cgrp, acquired);
	if (old) {
		bpf_cgroup_release(old);
		return -EEXIST;
	}

	return 0;
}

#endif /* _CGRP_KFUNC_COMMON_H */