summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/bpf
diff options
context:
space:
mode:
authorMagnus Karlsson <magnus.karlsson@intel.com>2023-01-11 12:35:13 +0300
committerAlexei Starovoitov <ast@kernel.org>2023-01-12 05:16:51 +0300
commit5adaf52776a4178434c0670a8e05e529a2b4aafa (patch)
treeca1be1ddf478b367d6face1103ea985a94005b23 /tools/testing/selftests/bpf
parent2d0b2ae2871ae6d42a9f0a4280e0fb5bff8d38b8 (diff)
downloadlinux-5adaf52776a4178434c0670a8e05e529a2b4aafa.tar.xz
selftests/xsk: do not close unused file descriptors
Do not close descriptors that have never been used. File descriptor fields that are not in use are erroneously marked with the number 0, which is a valid fd. Mark unused fds with -1 instead and do not close these when deleting the socket. Signed-off-by: Magnus Karlsson <magnus.karlsson@intel.com> Acked-by: Maciej Fijalkowski <maciej.fijalkowski@intel.com> Link: https://lore.kernel.org/r/20230111093526.11682-3-magnus.karlsson@gmail.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Diffstat (limited to 'tools/testing/selftests/bpf')
-rw-r--r--tools/testing/selftests/bpf/xsk.c13
1 files changed, 11 insertions, 2 deletions
diff --git a/tools/testing/selftests/bpf/xsk.c b/tools/testing/selftests/bpf/xsk.c
index 39d349509ba4..5e4a6552ed37 100644
--- a/tools/testing/selftests/bpf/xsk.c
+++ b/tools/testing/selftests/bpf/xsk.c
@@ -35,6 +35,8 @@
#include "xsk.h"
#include "bpf_util.h"
+#define FD_NOT_USED (-1)
+
#ifndef SOL_XDP
#define SOL_XDP 283
#endif
@@ -583,6 +585,9 @@ static void xsk_delete_bpf_maps(struct xsk_socket *xsk)
{
struct xsk_ctx *ctx = xsk->ctx;
+ if (ctx->xsks_map_fd == FD_NOT_USED)
+ return;
+
bpf_map_delete_elem(ctx->xsks_map_fd, &ctx->queue_id);
close(ctx->xsks_map_fd);
}
@@ -941,6 +946,9 @@ static struct xsk_ctx *xsk_create_ctx(struct xsk_socket *xsk,
ctx->umem = umem;
ctx->queue_id = queue_id;
bpf_strlcpy(ctx->ifname, ifname, IFNAMSIZ);
+ ctx->prog_fd = FD_NOT_USED;
+ ctx->link_fd = FD_NOT_USED;
+ ctx->xsks_map_fd = FD_NOT_USED;
ctx->fill = fill;
ctx->comp = comp;
@@ -1221,8 +1229,9 @@ void xsk_socket__delete(struct xsk_socket *xsk)
if (ctx->refcount == 1) {
xsk_delete_bpf_maps(xsk);
- close(ctx->prog_fd);
- if (ctx->has_bpf_link)
+ if (ctx->prog_fd != FD_NOT_USED)
+ close(ctx->prog_fd);
+ if (ctx->has_bpf_link && ctx->link_fd != FD_NOT_USED)
close(ctx->link_fd);
}