Skip to content

Commit 963e79f

Browse files
borkmanngregkh
authored andcommitted
bpf: Add cookie object to bpf maps
[ Upstream commit 12df58a ] 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>
1 parent b0c51e9 commit 963e79f

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

include/linux/bpf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ struct bpf_map {
306306
bool free_after_rcu_gp;
307307
atomic64_t sleepable_refcnt;
308308
s64 __percpu *elem_count;
309+
u64 cookie; /* write-once */
309310
};
310311

311312
static inline const char *btf_field_type_name(enum btf_field_type type)

kernel/bpf/syscall.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include <linux/rcupdate_trace.h>
3636
#include <linux/memcontrol.h>
3737
#include <linux/trace_events.h>
38+
#include <linux/cookie.h>
3839

3940
#include <net/netfilter/nf_bpf_link.h>
4041
#include <net/netkit.h>
@@ -51,6 +52,7 @@
5152
#define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY)
5253

5354
DEFINE_PER_CPU(int, bpf_prog_active);
55+
DEFINE_COOKIE(bpf_map_cookie);
5456
static DEFINE_IDR(prog_idr);
5557
static DEFINE_SPINLOCK(prog_idr_lock);
5658
static DEFINE_IDR(map_idr);
@@ -1360,6 +1362,10 @@ static int map_create(union bpf_attr *attr)
13601362
if (err < 0)
13611363
goto free_map;
13621364

1365+
preempt_disable();
1366+
map->cookie = gen_cookie_next(&bpf_map_cookie);
1367+
preempt_enable();
1368+
13631369
atomic64_set(&map->refcnt, 1);
13641370
atomic64_set(&map->usercnt, 1);
13651371
mutex_init(&map->freeze_mutex);

0 commit comments

Comments
 (0)