Skip to contentSkip to conversation

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

PATCH Applied zerons 3 participants 7 messages Feb 26, 2019 – Feb 27, 2019

Subsystems (4)
lore
Share & tools
lore

zerons

author
#0 lore raw

PATCH 1/1 bpf: decrease usercnt if bpf_map_new_fd() fails in bpf_map_get_fd_by_id()

[ Upstream commit c91951f15978f1a0c6b65f063d30f7ea7bc6fb42 ]

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.

---
 kernel/bpf/syscall.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/syscall.c +1 -1
index cf5040f..db1ed12 100644
@@ -1966,7 +1966,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;
 }

--
2.7.4

maintainer
#1 lore raw
Quoted 1 line · zerons

[ Upstream commit c91951f15978f1a0c6b65f063d30f7ea7bc6fb42 ]

Thanks for the fix! What do you mean by "upstream commit" above in this context?

Quoted 25 lines

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.

---
 kernel/bpf/syscall.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/syscall.c +1 -2
index cf5040f..db1ed12 100644
@@ -1966,7 +1966,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;
 }
--

2.7.4

author
#2 lore raw
Quoted 5 lines · Daniel Borkmann
Quoted 1 line · zerons

[ Upstream commit c91951f15978f1a0c6b65f063d30f7ea7bc6fb42 ]

Thanks for the fix! What do you mean by "upstream commit" above in this context?

This patch is based on that commit, I thought I should mention this. Sorry for the confusion.

maintainer
#3 lore raw
Quoted 8 lines · zerons
Quoted 4 lines · Daniel Borkmann
Quoted 1 line · zerons

[ Upstream commit c91951f15978f1a0c6b65f063d30f7ea7bc6fb42 ]

Thanks for the fix! What do you mean by "upstream commit" above in this context?

This patch is based on that commit, I thought I should mention this. Sorry for the confusion.

Ah, no. Usually that line is placed into stable commits that are cherry-picked to point to the original commit sha in the mainline tree. In any case, I didn't find c91951f15978, hence my question. Anyway, for normal patch submissions in future, please don't add it into the log, we'll remove it this time.

Thanks,
Daniel

author
#4 lore raw
Quoted 5 lines · Daniel Borkmann
Quoted 4 lines · zerons
Quoted 3 lines · Daniel Borkmann
Quoted 1 line · zerons

[ Upstream commit c91951f15978f1a0c6b65f063d30f7ea7bc6fb42 ]

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Thanks for the fix! What do you mean by "upstream commit" above in this context?

This patch is based on that commit, I thought I should mention this. Sorry for the confusion.

Ah, no. Usually that line is placed into stable commits that are cherry-picked to point to the original commit sha in the mainline tree. In any case, I didn't find c91951f15978, hence my question.

That is v4.20.12.
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?h=v4.20.12&id=c91951f15978f1a0c6b65f063d30f7ea7bc6fb42

Quoted 3 lines

Anyway, for normal patch submissions in future, please don't add it into the log, we'll remove it this time.

Thanks for reminding me. I won't do that again.

Acked-by on v1 1/1
#5 lore raw
In reply to zerons
Quoted 5 lines · zerons

[ Upstream commit c91951f15978f1a0c6b65f063d30f7ea7bc6fb42 ]

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.

Good catch! Thanks for the fix.

Fixes bd5f5f4ecb78 ("bpf: Add BPF_MAP_GET_FD_BY_ID")
maintainer Applied
#6 lore raw
Quoted 7 lines · Martin Lau
Quoted 5 lines · zerons

[ Upstream commit c91951f15978f1a0c6b65f063d30f7ea7bc6fb42 ]

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.

Good catch! Thanks for the fix.

+1

Quoted 2 lines
Fixes bd5f5f4ecb78 ("bpf: Add BPF_MAP_GET_FD_BY_ID")

Applied, thanks!