11410620cSUdip Pant // SPDX-License-Identifier: GPL-2.0
21410620cSUdip Pant // Copyright (c) 2020 Facebook
31410620cSUdip Pant 
41410620cSUdip Pant #include <linux/ptrace.h>
51410620cSUdip Pant #include <linux/bpf.h>
61410620cSUdip Pant #include <bpf/bpf_helpers.h>
71410620cSUdip Pant #include <bpf/bpf_tracing.h>
81410620cSUdip Pant 
91410620cSUdip Pant #define VAR_NUM 2
101410620cSUdip Pant 
111410620cSUdip Pant struct hmap_elem {
121410620cSUdip Pant 	struct bpf_spin_lock lock;
131410620cSUdip Pant 	int var[VAR_NUM];
141410620cSUdip Pant };
151410620cSUdip Pant 
161410620cSUdip Pant struct {
171410620cSUdip Pant 	__uint(type, BPF_MAP_TYPE_HASH);
181410620cSUdip Pant 	__uint(max_entries, 1);
191410620cSUdip Pant 	__type(key, __u32);
201410620cSUdip Pant 	__type(value, struct hmap_elem);
211410620cSUdip Pant } hash_map SEC(".maps");
221410620cSUdip Pant 
231410620cSUdip Pant SEC("freplace/handle_kprobe")
new_handle_kprobe(struct pt_regs * ctx)241410620cSUdip Pant int new_handle_kprobe(struct pt_regs *ctx)
251410620cSUdip Pant {
26*c8ed6685SAndrii Nakryiko 	struct hmap_elem *val;
271410620cSUdip Pant 	int key = 0;
281410620cSUdip Pant 
291410620cSUdip Pant 	val = bpf_map_lookup_elem(&hash_map, &key);
301410620cSUdip Pant 	if (!val)
311410620cSUdip Pant 		return 1;
321410620cSUdip Pant 	/* spin_lock in hash map */
331410620cSUdip Pant 	bpf_spin_lock(&val->lock);
341410620cSUdip Pant 	val->var[0] = 99;
351410620cSUdip Pant 	bpf_spin_unlock(&val->lock);
361410620cSUdip Pant 
371410620cSUdip Pant 	return 0;
381410620cSUdip Pant }
391410620cSUdip Pant 
401410620cSUdip Pant char _license[] SEC("license") = "GPL";
41