xref: /linux/tools/testing/selftests/bpf/progs/normal_map_btf.c (revision 45d8b572fac3aa8b49d53c946b3685eaf78a2824)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2023. Huawei Technologies Co., Ltd */
3 #include <vmlinux.h>
4 #include <bpf/bpf_tracing.h>
5 #include <bpf/bpf_helpers.h>
6 
7 #include "bpf_misc.h"
8 #include "bpf_experimental.h"
9 
10 struct node_data {
11 	__u64 data;
12 	struct bpf_list_node node;
13 };
14 
15 struct map_value {
16 	struct bpf_list_head head __contains(node_data, node);
17 	struct bpf_spin_lock lock;
18 };
19 
20 struct {
21 	__uint(type, BPF_MAP_TYPE_ARRAY);
22 	__type(key, int);
23 	__type(value, struct map_value);
24 	__uint(max_entries, 1);
25 } array SEC(".maps");
26 
27 char _license[] SEC("license") = "GPL";
28 
29 int pid = 0;
30 bool done = false;
31 
32 SEC("fentry/" SYS_PREFIX "sys_nanosleep")
33 int add_to_list_in_array(void *ctx)
34 {
35 	struct map_value *value;
36 	struct node_data *new;
37 	int zero = 0;
38 
39 	if (done || (int)bpf_get_current_pid_tgid() != pid)
40 		return 0;
41 
42 	value = bpf_map_lookup_elem(&array, &zero);
43 	if (!value)
44 		return 0;
45 
46 	new = bpf_obj_new(typeof(*new));
47 	if (!new)
48 		return 0;
49 
50 	bpf_spin_lock(&value->lock);
51 	bpf_list_push_back(&value->head, &new->node);
52 	bpf_spin_unlock(&value->lock);
53 	done = true;
54 
55 	return 0;
56 }
57