summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/bpf/progs/lru_lock_nmi.c
blob: c0692cd5423795af35d5f943d6e7614183d33ad4 (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
// SPDX-License-Identifier: GPL-2.0
#include <vmlinux.h>
#include <bpf/bpf_helpers.h>

struct {
	__uint(type, BPF_MAP_TYPE_LRU_HASH);
	__uint(max_entries, 64);
	__type(key, __u32);
	__type(value, __u64);
} lru_map SEC(".maps");

int hits;

SEC("perf_event")
int oncpu(void *ctx)
{
	/*
	 * Key range deliberately wider than max_entries to force LRU
	 * eviction on every other update.
	 */
	__u32 key = bpf_get_prandom_u32() % 128;
	bool do_update = bpf_get_prandom_u32() & 1;
	__u64 val = 1;

	if (do_update)
		bpf_map_update_elem(&lru_map, &key, &val, BPF_ANY);
	else
		bpf_map_delete_elem(&lru_map, &key);
	__sync_fetch_and_add(&hits, 1);
	return 0;
}

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