Skip to content

Commit d93a7cf

Browse files
DanielTimLeeAlexei Starovoitov
authored andcommitted
samples/bpf: fix broken map lookup probe
In the commit 7c4cd05 ("bpf: Fix syscall's stackmap lookup potential deadlock"), a potential deadlock issue was addressed, which resulted in *_map_lookup_elem not triggering BPF programs. (prior to lookup, bpf_disable_instrumentation() is used) To resolve the broken map lookup probe using "htab_map_lookup_elem", this commit introduces an alternative approach. Instead, it utilize "bpf_map_copy_value" and apply a filter specifically for the hash table with map_type. Signed-off-by: Daniel T. Lee <[email protected]> Fixes: 7c4cd05 ("bpf: Fix syscall's stackmap lookup potential deadlock") Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 9263211 commit d93a7cf

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

samples/bpf/tracex6.bpf.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "vmlinux.h"
22
#include <linux/version.h>
33
#include <bpf/bpf_helpers.h>
4+
#include <bpf/bpf_tracing.h>
5+
#include <bpf/bpf_core_read.h>
46

57
struct {
68
__uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
@@ -44,13 +46,24 @@ int bpf_prog1(struct pt_regs *ctx)
4446
return 0;
4547
}
4648

47-
SEC("kprobe/htab_map_lookup_elem")
48-
int bpf_prog2(struct pt_regs *ctx)
49+
/*
50+
* Since *_map_lookup_elem can't be expected to trigger bpf programs
51+
* due to potential deadlocks (bpf_disable_instrumentation), this bpf
52+
* program will be attached to bpf_map_copy_value (which is called
53+
* from map_lookup_elem) and will only filter the hashtable type.
54+
*/
55+
SEC("kprobe/bpf_map_copy_value")
56+
int BPF_KPROBE(bpf_prog2, struct bpf_map *map)
4957
{
5058
u32 key = bpf_get_smp_processor_id();
5159
struct bpf_perf_event_value *val, buf;
60+
enum bpf_map_type type;
5261
int error;
5362

63+
type = BPF_CORE_READ(map, map_type);
64+
if (type != BPF_MAP_TYPE_HASH)
65+
return 0;
66+
5467
error = bpf_perf_event_read_value(&counters, key, &buf, sizeof(buf));
5568
if (error)
5669
return 0;

0 commit comments

Comments
 (0)