summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/bpf/progs/map_in_map_btf.c
blob: 7a1336d7b16a63f688d3529b0bb41ac043f25a4a (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
// SPDX-License-Identifier: GPL-2.0
/* Copyright (C) 2023. Huawei Technologies Co., Ltd */
#include <vmlinux.h>
#include <bpf/bpf_tracing.h>
#include <bpf/bpf_helpers.h>

#include "bpf_misc.h"
#include "bpf_experimental.h"

struct node_data {
	__u64 data;
	struct bpf_list_node node;
};

struct map_value {
	struct bpf_list_head head __contains(node_data, node);
	struct bpf_spin_lock lock;
};

struct inner_array_type {
	__uint(type, BPF_MAP_TYPE_ARRAY);
	__type(key, int);
	__type(value, struct map_value);
	__uint(max_entries, 1);
} inner_array SEC(".maps");

struct {
	__uint(type, BPF_MAP_TYPE_ARRAY_OF_MAPS);
	__uint(key_size, 4);
	__uint(value_size, 4);
	__uint(max_entries, 1);
	__array(values, struct inner_array_type);
} outer_array SEC(".maps") = {
	.values = {
		[0] = &inner_array,
	},
};

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

int pid = 0;
bool done = false;

SEC("fentry/" SYS_PREFIX "sys_nanosleep")
int add_to_list_in_inner_array(void *ctx)
{
	struct map_value *value;
	struct node_data *new;
	struct bpf_map *map;
	int zero = 0;

	if (done || (u32)bpf_get_current_pid_tgid() != pid)
		return 0;

	map = bpf_map_lookup_elem(&outer_array, &zero);
	if (!map)
		return 0;

	value = bpf_map_lookup_elem(map, &zero);
	if (!value)
		return 0;

	new = bpf_obj_new(typeof(*new));
	if (!new)
		return 0;

	bpf_spin_lock(&value->lock);
	bpf_list_push_back(&value->head, &new->node);
	bpf_spin_unlock(&value->lock);
	done = true;

	return 0;
}