1*29c11aa8SHou Tao // SPDX-License-Identifier: GPL-2.0
2*29c11aa8SHou Tao /* Copyright (C) 2023. Huawei Technologies Co., Ltd */
3*29c11aa8SHou Tao #include <vmlinux.h>
4*29c11aa8SHou Tao #include <bpf/bpf_tracing.h>
5*29c11aa8SHou Tao #include <bpf/bpf_helpers.h>
6*29c11aa8SHou Tao
7*29c11aa8SHou Tao #include "bpf_experimental.h"
8*29c11aa8SHou Tao
9*29c11aa8SHou Tao struct bin_data {
10*29c11aa8SHou Tao char data[256];
11*29c11aa8SHou Tao struct bpf_spin_lock lock;
12*29c11aa8SHou Tao };
13*29c11aa8SHou Tao
14*29c11aa8SHou Tao struct map_value {
15*29c11aa8SHou Tao struct bin_data __kptr * data;
16*29c11aa8SHou Tao };
17*29c11aa8SHou Tao
18*29c11aa8SHou Tao struct {
19*29c11aa8SHou Tao __uint(type, BPF_MAP_TYPE_ARRAY);
20*29c11aa8SHou Tao __type(key, int);
21*29c11aa8SHou Tao __type(value, struct map_value);
22*29c11aa8SHou Tao __uint(max_entries, 2048);
23*29c11aa8SHou Tao } array SEC(".maps");
24*29c11aa8SHou Tao
25*29c11aa8SHou Tao char _license[] SEC("license") = "GPL";
26*29c11aa8SHou Tao
27*29c11aa8SHou Tao bool nomem_err = false;
28*29c11aa8SHou Tao
del_array(unsigned int i,int * from)29*29c11aa8SHou Tao static int del_array(unsigned int i, int *from)
30*29c11aa8SHou Tao {
31*29c11aa8SHou Tao struct map_value *value;
32*29c11aa8SHou Tao struct bin_data *old;
33*29c11aa8SHou Tao
34*29c11aa8SHou Tao value = bpf_map_lookup_elem(&array, from);
35*29c11aa8SHou Tao if (!value)
36*29c11aa8SHou Tao return 1;
37*29c11aa8SHou Tao
38*29c11aa8SHou Tao old = bpf_kptr_xchg(&value->data, NULL);
39*29c11aa8SHou Tao if (old)
40*29c11aa8SHou Tao bpf_obj_drop(old);
41*29c11aa8SHou Tao
42*29c11aa8SHou Tao (*from)++;
43*29c11aa8SHou Tao return 0;
44*29c11aa8SHou Tao }
45*29c11aa8SHou Tao
add_array(unsigned int i,int * from)46*29c11aa8SHou Tao static int add_array(unsigned int i, int *from)
47*29c11aa8SHou Tao {
48*29c11aa8SHou Tao struct bin_data *old, *new;
49*29c11aa8SHou Tao struct map_value *value;
50*29c11aa8SHou Tao
51*29c11aa8SHou Tao value = bpf_map_lookup_elem(&array, from);
52*29c11aa8SHou Tao if (!value)
53*29c11aa8SHou Tao return 1;
54*29c11aa8SHou Tao
55*29c11aa8SHou Tao new = bpf_obj_new(typeof(*new));
56*29c11aa8SHou Tao if (!new) {
57*29c11aa8SHou Tao nomem_err = true;
58*29c11aa8SHou Tao return 1;
59*29c11aa8SHou Tao }
60*29c11aa8SHou Tao
61*29c11aa8SHou Tao old = bpf_kptr_xchg(&value->data, new);
62*29c11aa8SHou Tao if (old)
63*29c11aa8SHou Tao bpf_obj_drop(old);
64*29c11aa8SHou Tao
65*29c11aa8SHou Tao (*from)++;
66*29c11aa8SHou Tao return 0;
67*29c11aa8SHou Tao }
68*29c11aa8SHou Tao
del_then_add_array(int from)69*29c11aa8SHou Tao static void del_then_add_array(int from)
70*29c11aa8SHou Tao {
71*29c11aa8SHou Tao int i;
72*29c11aa8SHou Tao
73*29c11aa8SHou Tao i = from;
74*29c11aa8SHou Tao bpf_loop(512, del_array, &i, 0);
75*29c11aa8SHou Tao
76*29c11aa8SHou Tao i = from;
77*29c11aa8SHou Tao bpf_loop(512, add_array, &i, 0);
78*29c11aa8SHou Tao }
79*29c11aa8SHou Tao
80*29c11aa8SHou Tao SEC("fentry/bpf_fentry_test1")
BPF_PROG2(test0,int,a)81*29c11aa8SHou Tao int BPF_PROG2(test0, int, a)
82*29c11aa8SHou Tao {
83*29c11aa8SHou Tao del_then_add_array(0);
84*29c11aa8SHou Tao return 0;
85*29c11aa8SHou Tao }
86*29c11aa8SHou Tao
87*29c11aa8SHou Tao SEC("fentry/bpf_fentry_test2")
BPF_PROG2(test1,int,a,u64,b)88*29c11aa8SHou Tao int BPF_PROG2(test1, int, a, u64, b)
89*29c11aa8SHou Tao {
90*29c11aa8SHou Tao del_then_add_array(512);
91*29c11aa8SHou Tao return 0;
92*29c11aa8SHou Tao }
93*29c11aa8SHou Tao
94*29c11aa8SHou Tao SEC("fentry/bpf_fentry_test3")
BPF_PROG2(test2,char,a,int,b,u64,c)95*29c11aa8SHou Tao int BPF_PROG2(test2, char, a, int, b, u64, c)
96*29c11aa8SHou Tao {
97*29c11aa8SHou Tao del_then_add_array(1024);
98*29c11aa8SHou Tao return 0;
99*29c11aa8SHou Tao }
100*29c11aa8SHou Tao
101*29c11aa8SHou Tao SEC("fentry/bpf_fentry_test4")
BPF_PROG2(test3,void *,a,char,b,int,c,u64,d)102*29c11aa8SHou Tao int BPF_PROG2(test3, void *, a, char, b, int, c, u64, d)
103*29c11aa8SHou Tao {
104*29c11aa8SHou Tao del_then_add_array(1536);
105*29c11aa8SHou Tao return 0;
106*29c11aa8SHou Tao }
107