Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

bpf: export bpf_map_inc_not_zero

Rename existing bpf_map_inc_not_zero to __bpf_map_inc_not_zero to
indicate that it's caller's responsibility to do proper locking.
Create and export bpf_map_inc_not_zero wrapper that properly
locks map_idr_lock. Will be used in the next commit to
hold a map while cloning a socket.

Cc: Martin KaFai Lau <kafai@fb.com>
Cc: Yonghong Song <yhs@fb.com>
Acked-by: Martin KaFai Lau <kafai@fb.com>
Acked-by: Yonghong Song <yhs@fb.com>
Signed-off-by: Stanislav Fomichev <sdf@google.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>

authored by

Stanislav Fomichev and committed by
Daniel Borkmann
b0e4701c fae55527

+15 -3
+2
include/linux/bpf.h
··· 647 647 struct bpf_map *bpf_map_get_with_uref(u32 ufd); 648 648 struct bpf_map *__bpf_map_get(struct fd f); 649 649 struct bpf_map * __must_check bpf_map_inc(struct bpf_map *map, bool uref); 650 + struct bpf_map * __must_check bpf_map_inc_not_zero(struct bpf_map *map, 651 + bool uref); 650 652 void bpf_map_put_with_uref(struct bpf_map *map); 651 653 void bpf_map_put(struct bpf_map *map); 652 654 int bpf_map_charge_memlock(struct bpf_map *map, u32 pages);
+13 -3
kernel/bpf/syscall.c
··· 683 683 } 684 684 685 685 /* map_idr_lock should have been held */ 686 - static struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map, 687 - bool uref) 686 + static struct bpf_map *__bpf_map_inc_not_zero(struct bpf_map *map, 687 + bool uref) 688 688 { 689 689 int refold; 690 690 ··· 703 703 704 704 return map; 705 705 } 706 + 707 + struct bpf_map *bpf_map_inc_not_zero(struct bpf_map *map, bool uref) 708 + { 709 + spin_lock_bh(&map_idr_lock); 710 + map = __bpf_map_inc_not_zero(map, uref); 711 + spin_unlock_bh(&map_idr_lock); 712 + 713 + return map; 714 + } 715 + EXPORT_SYMBOL_GPL(bpf_map_inc_not_zero); 706 716 707 717 int __weak bpf_stackmap_copy(struct bpf_map *map, void *key, void *value) 708 718 { ··· 2187 2177 spin_lock_bh(&map_idr_lock); 2188 2178 map = idr_find(&map_idr, id); 2189 2179 if (map) 2190 - map = bpf_map_inc_not_zero(map, true); 2180 + map = __bpf_map_inc_not_zero(map, true); 2191 2181 else 2192 2182 map = ERR_PTR(-ENOENT); 2193 2183 spin_unlock_bh(&map_idr_lock);