xref: /linux/tools/testing/selftests/bpf/progs/lru_lock_nmi.c (revision b2128290c29902315e632ea59e0504d6bc9e9b42)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <vmlinux.h>
3 #include <bpf/bpf_helpers.h>
4 
5 struct {
6 	__uint(type, BPF_MAP_TYPE_LRU_HASH);
7 	__uint(max_entries, 64);
8 	__type(key, __u32);
9 	__type(value, __u64);
10 } lru_map SEC(".maps");
11 
12 int hits;
13 
14 SEC("perf_event")
15 int oncpu(void *ctx)
16 {
17 	/*
18 	 * Key range deliberately wider than max_entries to force LRU
19 	 * eviction on every other update.
20 	 */
21 	__u32 key = bpf_get_prandom_u32() % 128;
22 	bool do_update = bpf_get_prandom_u32() & 1;
23 	__u64 val = 1;
24 
25 	if (do_update)
26 		bpf_map_update_elem(&lru_map, &key, &val, BPF_ANY);
27 	else
28 		bpf_map_delete_elem(&lru_map, &key);
29 	__sync_fetch_and_add(&hits, 1);
30 	return 0;
31 }
32 
33 char _license[] SEC("license") = "GPL";
34