]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
bpf: Add cookie object to bpf maps
authorDaniel Borkmann <daniel@iogearbox.net>
Wed, 30 Jul 2025 23:47:30 +0000 (01:47 +0200)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Tue, 9 Sep 2025 16:56:18 +0000 (18:56 +0200)
[ Upstream commit 12df58ad294253ac1d8df0c9bb9cf726397a671d ]

Add a cookie to BPF maps to uniquely identify BPF maps for the timespan
when the node is up. This is different to comparing a pointer or BPF map
id which could get rolled over and reused.

Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/r/20250730234733.530041-1-daniel@iogearbox.net
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
include/linux/bpf.h
kernel/bpf/syscall.c

index 17de12a98f858a95ba722a434e874ce05e038f37..0a097087f0a7cfa7b212b7927e767e6c9b4e4622 100644 (file)
@@ -300,6 +300,7 @@ struct bpf_map {
        bool free_after_rcu_gp;
        atomic64_t sleepable_refcnt;
        s64 __percpu *elem_count;
+       u64 cookie; /* write-once */
 };
 
 static inline const char *btf_field_type_name(enum btf_field_type type)
index b66349f892f25ea9096c8694c3390d0824e5ac3f..1d6bd012de9e66ac41effa0564ce17212e16c603 100644 (file)
@@ -35,6 +35,7 @@
 #include <linux/rcupdate_trace.h>
 #include <linux/memcontrol.h>
 #include <linux/trace_events.h>
+#include <linux/cookie.h>
 #include <net/netfilter/nf_bpf_link.h>
 
 #include <net/tcx.h>
@@ -50,6 +51,7 @@
 #define BPF_OBJ_FLAG_MASK   (BPF_F_RDONLY | BPF_F_WRONLY)
 
 DEFINE_PER_CPU(int, bpf_prog_active);
+DEFINE_COOKIE(bpf_map_cookie);
 static DEFINE_IDR(prog_idr);
 static DEFINE_SPINLOCK(prog_idr_lock);
 static DEFINE_IDR(map_idr);
@@ -1253,6 +1255,10 @@ static int map_create(union bpf_attr *attr)
        if (err < 0)
                goto free_map;
 
+       preempt_disable();
+       map->cookie = gen_cookie_next(&bpf_map_cookie);
+       preempt_enable();
+
        atomic64_set(&map->refcnt, 1);
        atomic64_set(&map->usercnt, 1);
        mutex_init(&map->freeze_mutex);