From: Magnus Karlsson <magnus.karlsson@gmail.com>
To: magnus.karlsson@intel.com, bjorn@kernel.org, ast@kernel.org,
daniel@iogearbox.net, netdev@vger.kernel.org,
maciej.fijalkowski@intel.com
Cc: jonathan.lemon@gmail.com, bpf@vger.kernel.org
Subject: [PATCH bpf 2/3] selftests/xsk: do not close unused file descriptors
Date: Tue, 15 Nov 2022 09:05:37 +0100 [thread overview]
Message-ID: <20221115080538.18503-3-magnus.karlsson@gmail.com> (raw)
In-Reply-To: <20221115080538.18503-1-magnus.karlsson@gmail.com>
From: Magnus Karlsson <magnus.karlsson@intel.com>
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.
Fixes: f36600634282 ("libbpf: move xsk.{c,h} into selftests/bpf")
Signed-off-by: Magnus Karlsson <magnus.karlsson@intel.com>
---
tools/testing/selftests/bpf/xsk.c | 13 +++++++++++--
1 file changed, 11 insertions(+), 2 deletions(-)
diff --git a/tools/testing/selftests/bpf/xsk.c b/tools/testing/selftests/bpf/xsk.c
index 0b3ff49c740d..bea4cd076cd4 100644
--- a/tools/testing/selftests/bpf/xsk.c
+++ b/tools/testing/selftests/bpf/xsk.c
@@ -34,6 +34,8 @@
#include <bpf/libbpf.h>
#include "xsk.h"
+#define FD_NOT_USED (-1)
+
#ifndef SOL_XDP
#define SOL_XDP 283
#endif
@@ -601,6 +603,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);
}
@@ -959,6 +964,9 @@ static struct xsk_ctx *xsk_create_ctx(struct xsk_socket *xsk,
ctx->umem = umem;
ctx->queue_id = queue_id;
libbpf_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;
@@ -1239,8 +1247,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);
}
--
2.34.1
next prev parent reply other threads:[~2022-11-15 8:06 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-11-15 8:05 [PATCH bpf 0/3] selftests/xsk: three small fixes Magnus Karlsson
2022-11-15 8:05 ` [PATCH bpf 1/3] selftests/xsk: print correct payload for packet dump Magnus Karlsson
2022-11-15 12:29 ` Maciej Fijalkowski
2022-11-15 12:40 ` Magnus Karlsson
2022-11-22 10:42 ` Maciej Fijalkowski
2022-11-15 8:05 ` Magnus Karlsson [this message]
2022-11-15 8:05 ` [PATCH bpf 3/3] selftests/xsk: print correct error codes when exiting Magnus Karlsson
2022-11-15 12:30 ` [PATCH bpf 0/3] selftests/xsk: three small fixes Maciej Fijalkowski
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20221115080538.18503-3-magnus.karlsson@gmail.com \
--to=magnus.karlsson@gmail.com \
--cc=ast@kernel.org \
--cc=bjorn@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=jonathan.lemon@gmail.com \
--cc=maciej.fijalkowski@intel.com \
--cc=magnus.karlsson@intel.com \
--cc=netdev@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox