bpf: decrease usercnt if bpf_map_new_fd() fails in bpf_map_get_fd_by_id() [Linux 5.0]

bpf: decrease usercnt if bpf_map_new_fd() fails in bpf_map_get_fd_by_id() [Linux 5.0]

This Linux kernel change "bpf: decrease usercnt if bpf_map_new_fd() fails in bpf_map_get_fd_by_id()" is included in the Linux 5.0 release. This change is authored by Peng Sun <sironhide0null [at] gmail.com> on Tue Feb 26 22:15:37 2019 +0800. The commit for this change in Linux stable tree is 781e628 (patch).

bpf: decrease usercnt if bpf_map_new_fd() fails in bpf_map_get_fd_by_id()

In bpf/syscall.c, bpf_map_get_fd_by_id() use bpf_map_inc_not_zero()
to increase the refcount, both map->refcnt and map->usercnt. Then, if
bpf_map_new_fd() fails, should handle map->usercnt too.

Fixes: bd5f5f4ecb78 ("bpf: Add BPF_MAP_GET_FD_BY_ID")
Signed-off-by: Peng Sun <[email protected]>
Acked-by: Martin KaFai Lau <[email protected]>
Signed-off-by: Daniel Borkmann <[email protected]>

There are 2 lines of Linux source code added/deleted in this change. Code changes to Linux kernel are as follows.

 kernel/bpf/syscall.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c
index 8577bb7..f98328a 100644
--- a/kernel/bpf/syscall.c
+++ b/kernel/bpf/syscall.c
@@ -1986,7 +1986,7 @@ static int bpf_map_get_fd_by_id(const union bpf_attr *attr)

    fd = bpf_map_new_fd(map, f_flags);
    if (fd < 0)
-       bpf_map_put(map);
+       bpf_map_put_with_uref(map);

    return fd;
 }

Leave a Reply

Your email address will not be published. Required fields are marked *