hc
2024-10-09 05e59e5fb0064c97a1c10921ecd549f2d4a58565
kernel/samples/bpf/tracex6_kern.c
....@@ -2,6 +2,8 @@
22 #include <linux/version.h>
33 #include <uapi/linux/bpf.h>
44 #include <bpf/bpf_helpers.h>
5
+#include <bpf/bpf_tracing.h>
6
+#include <bpf/bpf_core_read.h>
57
68 struct {
79 __uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
....@@ -45,13 +47,24 @@
4547 return 0;
4648 }
4749
48
-SEC("kprobe/htab_map_lookup_elem")
49
-int bpf_prog2(struct pt_regs *ctx)
50
+/*
51
+ * Since *_map_lookup_elem can't be expected to trigger bpf programs
52
+ * due to potential deadlocks (bpf_disable_instrumentation), this bpf
53
+ * program will be attached to bpf_map_copy_value (which is called
54
+ * from map_lookup_elem) and will only filter the hashtable type.
55
+ */
56
+SEC("kprobe/bpf_map_copy_value")
57
+int BPF_KPROBE(bpf_prog2, struct bpf_map *map)
5058 {
5159 u32 key = bpf_get_smp_processor_id();
5260 struct bpf_perf_event_value *val, buf;
61
+ enum bpf_map_type type;
5362 int error;
5463
64
+ type = BPF_CORE_READ(map, map_type);
65
+ if (type != BPF_MAP_TYPE_HASH)
66
+ return 0;
67
+
5568 error = bpf_perf_event_read_value(&counters, key, &buf, sizeof(buf));
5669 if (error)
5770 return 0;