Skip to content

Commit 12df58a

Browse files
borkmannAlexei Starovoitov
authored andcommitted
bpf: Add cookie object to bpf maps
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 <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent d6084bb commit 12df58a

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
@@ -310,6 +310,7 @@ struct bpf_map {
310310
bool free_after_rcu_gp;
311311
atomic64_t sleepable_refcnt;
312312
s64 __percpu *elem_count;
313+
u64 cookie; /* write-once */
313314
};
314315

315316
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
@@ -37,6 +37,7 @@
3737
#include <linux/trace_events.h>
3838
#include <linux/tracepoint.h>
3939
#include <linux/overflow.h>
40+
#include <linux/cookie.h>
4041

4142
#include <net/netfilter/nf_bpf_link.h>
4243
#include <net/netkit.h>
@@ -53,6 +54,7 @@
5354
#define BPF_OBJ_FLAG_MASK (BPF_F_RDONLY | BPF_F_WRONLY)
5455

5556
DEFINE_PER_CPU(int, bpf_prog_active);
57+
DEFINE_COOKIE(bpf_map_cookie);
5658
static DEFINE_IDR(prog_idr);
5759
static DEFINE_SPINLOCK(prog_idr_lock);
5860
static DEFINE_IDR(map_idr);
@@ -1487,6 +1489,10 @@ static int map_create(union bpf_attr *attr, bool kernel)
14871489
if (err < 0)
14881490
goto free_map;
14891491

1492+
preempt_disable();
1493+
map->cookie = gen_cookie_next(&bpf_map_cookie);
1494+
preempt_enable();
1495+
14901496
atomic64_set(&map->refcnt, 1);
14911497
atomic64_set(&map->usercnt, 1);
14921498
mutex_init(&map->freeze_mutex);

0 commit comments

Comments
 (0)