aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/tools/testing/selftests/bpf/progs/preempted_bpf_ma_op.c
blob: 55907ef961bff75966798a706e0f75c0a01115d6 (plain) (blame)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// 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_experimental.h"

struct bin_data {
	char data[256];
	struct bpf_spin_lock lock;
};

struct map_value {
	struct bin_data __kptr * data;
};

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

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

bool nomem_err = false;

static int del_array(unsigned int i, int *from)
{
	struct map_value *value;
	struct bin_data *old;

	value = bpf_map_lookup_elem(&array, from);
	if (!value)
		return 1;

	old = bpf_kptr_xchg(&value->data, NULL);
	if (old)
		bpf_obj_drop(old);

	(*from)++;
	return 0;
}

static int add_array(unsigned int i, int *from)
{
	struct bin_data *old, *new;
	struct map_value *value;

	value = bpf_map_lookup_elem(&array, from);
	if (!value)
		return 1;

	new = bpf_obj_new(typeof(*new));
	if (!new) {
		nomem_err = true;
		return 1;
	}

	old = bpf_kptr_xchg(&value->data, new);
	if (old)
		bpf_obj_drop(old);

	(*from)++;
	return 0;
}

static void del_then_add_array(int from)
{
	int i;

	i = from;
	bpf_loop(512, del_array, &i, 0);

	i = from;
	bpf_loop(512, add_array, &i, 0);
}

SEC("fentry/bpf_fentry_test1")
int BPF_PROG2(test0, int, a)
{
	del_then_add_array(0);
	return 0;
}

SEC("fentry/bpf_fentry_test2")
int BPF_PROG2(test1, int, a, u64, b)
{
	del_then_add_array(512);
	return 0;
}

SEC("fentry/bpf_fentry_test3")
int BPF_PROG2(test2, char, a, int, b, u64, c)
{
	del_then_add_array(1024);
	return 0;
}

SEC("fentry/bpf_fentry_test4")
int BPF_PROG2(test3, void *, a, char, b, int, c, u64, d)
{
	del_then_add_array(1536);
	return 0;
}