xref: /linux/samples/bpf/tracex6.bpf.c (revision d93a7cf6)
14a0ee788SDaniel T. Lee #include "vmlinux.h"
24a0ee788SDaniel T. Lee #include <linux/version.h>
34a0ee788SDaniel T. Lee #include <bpf/bpf_helpers.h>
4*d93a7cf6SDaniel T. Lee #include <bpf/bpf_tracing.h>
5*d93a7cf6SDaniel T. Lee #include <bpf/bpf_core_read.h>
64a0ee788SDaniel T. Lee 
74a0ee788SDaniel T. Lee struct {
84a0ee788SDaniel T. Lee 	__uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
94a0ee788SDaniel T. Lee 	__uint(key_size, sizeof(int));
104a0ee788SDaniel T. Lee 	__uint(value_size, sizeof(u32));
114a0ee788SDaniel T. Lee 	__uint(max_entries, 64);
124a0ee788SDaniel T. Lee } counters SEC(".maps");
134a0ee788SDaniel T. Lee 
144a0ee788SDaniel T. Lee struct {
154a0ee788SDaniel T. Lee 	__uint(type, BPF_MAP_TYPE_HASH);
164a0ee788SDaniel T. Lee 	__type(key, int);
174a0ee788SDaniel T. Lee 	__type(value, u64);
184a0ee788SDaniel T. Lee 	__uint(max_entries, 64);
194a0ee788SDaniel T. Lee } values SEC(".maps");
204a0ee788SDaniel T. Lee 
214a0ee788SDaniel T. Lee struct {
224a0ee788SDaniel T. Lee 	__uint(type, BPF_MAP_TYPE_HASH);
234a0ee788SDaniel T. Lee 	__type(key, int);
244a0ee788SDaniel T. Lee 	__type(value, struct bpf_perf_event_value);
254a0ee788SDaniel T. Lee 	__uint(max_entries, 64);
264a0ee788SDaniel T. Lee } values2 SEC(".maps");
274a0ee788SDaniel T. Lee 
284a0ee788SDaniel T. Lee SEC("kprobe/htab_map_get_next_key")
bpf_prog1(struct pt_regs * ctx)294a0ee788SDaniel T. Lee int bpf_prog1(struct pt_regs *ctx)
304a0ee788SDaniel T. Lee {
314a0ee788SDaniel T. Lee 	u32 key = bpf_get_smp_processor_id();
324a0ee788SDaniel T. Lee 	u64 count, *val;
334a0ee788SDaniel T. Lee 	s64 error;
344a0ee788SDaniel T. Lee 
354a0ee788SDaniel T. Lee 	count = bpf_perf_event_read(&counters, key);
364a0ee788SDaniel T. Lee 	error = (s64)count;
374a0ee788SDaniel T. Lee 	if (error <= -2 && error >= -22)
384a0ee788SDaniel T. Lee 		return 0;
394a0ee788SDaniel T. Lee 
404a0ee788SDaniel T. Lee 	val = bpf_map_lookup_elem(&values, &key);
414a0ee788SDaniel T. Lee 	if (val)
424a0ee788SDaniel T. Lee 		*val = count;
434a0ee788SDaniel T. Lee 	else
444a0ee788SDaniel T. Lee 		bpf_map_update_elem(&values, &key, &count, BPF_NOEXIST);
454a0ee788SDaniel T. Lee 
464a0ee788SDaniel T. Lee 	return 0;
474a0ee788SDaniel T. Lee }
484a0ee788SDaniel T. Lee 
49*d93a7cf6SDaniel T. Lee /*
50*d93a7cf6SDaniel T. Lee  * Since *_map_lookup_elem can't be expected to trigger bpf programs
51*d93a7cf6SDaniel T. Lee  * due to potential deadlocks (bpf_disable_instrumentation), this bpf
52*d93a7cf6SDaniel T. Lee  * program will be attached to bpf_map_copy_value (which is called
53*d93a7cf6SDaniel T. Lee  * from map_lookup_elem) and will only filter the hashtable type.
54*d93a7cf6SDaniel T. Lee  */
55*d93a7cf6SDaniel T. Lee SEC("kprobe/bpf_map_copy_value")
BPF_KPROBE(bpf_prog2,struct bpf_map * map)56*d93a7cf6SDaniel T. Lee int BPF_KPROBE(bpf_prog2, struct bpf_map *map)
574a0ee788SDaniel T. Lee {
584a0ee788SDaniel T. Lee 	u32 key = bpf_get_smp_processor_id();
594a0ee788SDaniel T. Lee 	struct bpf_perf_event_value *val, buf;
60*d93a7cf6SDaniel T. Lee 	enum bpf_map_type type;
614a0ee788SDaniel T. Lee 	int error;
624a0ee788SDaniel T. Lee 
63*d93a7cf6SDaniel T. Lee 	type = BPF_CORE_READ(map, map_type);
64*d93a7cf6SDaniel T. Lee 	if (type != BPF_MAP_TYPE_HASH)
65*d93a7cf6SDaniel T. Lee 		return 0;
66*d93a7cf6SDaniel T. Lee 
674a0ee788SDaniel T. Lee 	error = bpf_perf_event_read_value(&counters, key, &buf, sizeof(buf));
684a0ee788SDaniel T. Lee 	if (error)
694a0ee788SDaniel T. Lee 		return 0;
704a0ee788SDaniel T. Lee 
714a0ee788SDaniel T. Lee 	val = bpf_map_lookup_elem(&values2, &key);
724a0ee788SDaniel T. Lee 	if (val)
734a0ee788SDaniel T. Lee 		*val = buf;
744a0ee788SDaniel T. Lee 	else
754a0ee788SDaniel T. Lee 		bpf_map_update_elem(&values2, &key, &buf, BPF_NOEXIST);
764a0ee788SDaniel T. Lee 
774a0ee788SDaniel T. Lee 	return 0;
784a0ee788SDaniel T. Lee }
794a0ee788SDaniel T. Lee 
804a0ee788SDaniel T. Lee char _license[] SEC("license") = "GPL";
814a0ee788SDaniel T. Lee u32 _version SEC("version") = LINUX_VERSION_CODE;
82