[PATCH RFC 6/9] bpf: add a helper to find map by id

Yafang Shao
2022-03-08T13:11:27Z

A new helper bpf_map_idr_find() is introduced for later use.

Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
---
kernel/bpf/syscall.c | 24 ++++++++++++++++--------
1 file changed, 16 insertions(+), 8 deletions(-)

diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index 3b50fcb..68fea3b 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -3474,6 +3474,21 @@ static int bpf_prog_get_fd_by_id(const union bpf_attr *attr)
return fd;
}

+static struct bpf_map *bpf_map_idr_find(unsigned long id)
+{
+ void *map;
+
+ spin_lock_bh(&map_idr_lock);
+ map = idr_find(&map_idr, id);
+ if (map)
+ map = __bpf_map_inc_not_zero(map, true);
+ else
+ map = ERR_PTR(-ENOENT);
+ spin_unlock_bh(&map_idr_lock);
+
+ return map;
+}
+
#define BPF_MAP_GET_FD_BY_ID_LAST_FIELD open_flags

static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
@@ -3494,14 +3509,7 @@ static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
if (f_flags < 0)
return f_flags;

- spin_lock_bh(&map_idr_lock);
- map = idr_find(&map_idr, id);
- if (map)
- map = __bpf_map_inc_not_zero(map, true);
- else
- map = ERR_PTR(-ENOENT);
- spin_unlock_bh(&map_idr_lock);
-
+ map = bpf_map_idr_find(id);
if (IS_ERR(map))
return PTR_ERR(map);

--
1.8.3.1