Commit 43f1bc1e authored by Ciara Loftus's avatar Ciara Loftus Committed by Alexei Starovoitov

libbpf: Restore umem state after socket create failure

If the call to xsk_socket__create fails, the user may want to retry the
socket creation using the same umem. Ensure that the umem is in the
same state on exit if the call fails by:
1. ensuring the umem _save pointers are unmodified.
2. not unmapping the set of umem rings that were set up with the umem
during xsk_umem__create, since those maps existed before the call to
xsk_socket__create and should remain in tact even in the event of
failure.

Fixes: 2f6324a3 ("libbpf: Support shared umems between queues and devices")
Signed-off-by: default avatarCiara Loftus <ciara.loftus@intel.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20210331061218.1647-3-ciara.loftus@intel.com
parent df662016
...@@ -743,26 +743,30 @@ static struct xsk_ctx *xsk_get_ctx(struct xsk_umem *umem, int ifindex, ...@@ -743,26 +743,30 @@ static struct xsk_ctx *xsk_get_ctx(struct xsk_umem *umem, int ifindex,
return NULL; return NULL;
} }
static void xsk_put_ctx(struct xsk_ctx *ctx) static void xsk_put_ctx(struct xsk_ctx *ctx, bool unmap)
{ {
struct xsk_umem *umem = ctx->umem; struct xsk_umem *umem = ctx->umem;
struct xdp_mmap_offsets off; struct xdp_mmap_offsets off;
int err; int err;
if (--ctx->refcount == 0) { if (--ctx->refcount)
return;
if (!unmap)
goto out_free;
err = xsk_get_mmap_offsets(umem->fd, &off); err = xsk_get_mmap_offsets(umem->fd, &off);
if (!err) { if (err)
munmap(ctx->fill->ring - off.fr.desc, goto out_free;
off.fr.desc + umem->config.fill_size *
munmap(ctx->fill->ring - off.fr.desc, off.fr.desc + umem->config.fill_size *
sizeof(__u64)); sizeof(__u64));
munmap(ctx->comp->ring - off.cr.desc, munmap(ctx->comp->ring - off.cr.desc, off.cr.desc + umem->config.comp_size *
off.cr.desc + umem->config.comp_size *
sizeof(__u64)); sizeof(__u64));
}
out_free:
list_del(&ctx->list); list_del(&ctx->list);
free(ctx); free(ctx);
}
} }
static struct xsk_ctx *xsk_create_ctx(struct xsk_socket *xsk, static struct xsk_ctx *xsk_create_ctx(struct xsk_socket *xsk,
...@@ -797,8 +801,6 @@ static struct xsk_ctx *xsk_create_ctx(struct xsk_socket *xsk, ...@@ -797,8 +801,6 @@ static struct xsk_ctx *xsk_create_ctx(struct xsk_socket *xsk,
memcpy(ctx->ifname, ifname, IFNAMSIZ - 1); memcpy(ctx->ifname, ifname, IFNAMSIZ - 1);
ctx->ifname[IFNAMSIZ - 1] = '\0'; ctx->ifname[IFNAMSIZ - 1] = '\0';
umem->fill_save = NULL;
umem->comp_save = NULL;
ctx->fill = fill; ctx->fill = fill;
ctx->comp = comp; ctx->comp = comp;
list_add(&ctx->list, &umem->ctx_list); list_add(&ctx->list, &umem->ctx_list);
...@@ -854,6 +856,7 @@ int xsk_socket__create_shared(struct xsk_socket **xsk_ptr, ...@@ -854,6 +856,7 @@ int xsk_socket__create_shared(struct xsk_socket **xsk_ptr,
struct xsk_socket *xsk; struct xsk_socket *xsk;
struct xsk_ctx *ctx; struct xsk_ctx *ctx;
int err, ifindex; int err, ifindex;
bool unmap = umem->fill_save != fill;
if (!umem || !xsk_ptr || !(rx || tx)) if (!umem || !xsk_ptr || !(rx || tx))
return -EFAULT; return -EFAULT;
...@@ -994,6 +997,8 @@ int xsk_socket__create_shared(struct xsk_socket **xsk_ptr, ...@@ -994,6 +997,8 @@ int xsk_socket__create_shared(struct xsk_socket **xsk_ptr,
} }
*xsk_ptr = xsk; *xsk_ptr = xsk;
umem->fill_save = NULL;
umem->comp_save = NULL;
return 0; return 0;
out_mmap_tx: out_mmap_tx:
...@@ -1005,7 +1010,7 @@ int xsk_socket__create_shared(struct xsk_socket **xsk_ptr, ...@@ -1005,7 +1010,7 @@ int xsk_socket__create_shared(struct xsk_socket **xsk_ptr,
munmap(rx_map, off.rx.desc + munmap(rx_map, off.rx.desc +
xsk->config.rx_size * sizeof(struct xdp_desc)); xsk->config.rx_size * sizeof(struct xdp_desc));
out_put_ctx: out_put_ctx:
xsk_put_ctx(ctx); xsk_put_ctx(ctx, unmap);
out_socket: out_socket:
if (--umem->refcount) if (--umem->refcount)
close(xsk->fd); close(xsk->fd);
...@@ -1071,7 +1076,7 @@ void xsk_socket__delete(struct xsk_socket *xsk) ...@@ -1071,7 +1076,7 @@ void xsk_socket__delete(struct xsk_socket *xsk)
} }
} }
xsk_put_ctx(ctx); xsk_put_ctx(ctx, true);
umem->refcount--; umem->refcount--;
/* Do not close an fd that also has an associated umem connected /* Do not close an fd that also has an associated umem connected
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment