From 169d050bc82a519fdc28f83bb685d86804383f0b Mon Sep 17 00:00:00 2001 From: Greg Inozemtsev Date: Tue, 27 Oct 2020 11:24:30 -0700 Subject: [PATCH] Fix cmd_fd leak in mlx5_alloc_context MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The alloc_context function is supposed to take ownership of cmd_fd and close it if it fails and returns NULL. This was not done in early exit error paths from mlx5_init_context (called from mlx5_alloc_context). Fixes: cb3be404fadc (“mlx5: Refactor mlx5_alloc_context()”) Signed-off-by: Greg Inozemtsev --- providers/mlx5/mlx5.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/providers/mlx5/mlx5.c b/providers/mlx5/mlx5.c index 0a091f5b6b49..551560d561fe 100644 --- a/providers/mlx5/mlx5.c +++ b/providers/mlx5/mlx5.c @@ -1334,25 +1334,14 @@ static void mlx5_uninit_context(struct mlx5_context *context) } static struct mlx5_context *mlx5_init_context(struct ibv_device *ibdev, - int cmd_fd, - void *private_data) + int cmd_fd) { - struct mlx5dv_context_attr *ctx_attr = private_data; struct mlx5_device *mdev = to_mdev(ibdev); struct mlx5_context *context; int low_lat_uuars; int tot_uuars; int ret; - if (ctx_attr && ctx_attr->comp_mask) { - errno = EINVAL; - return NULL; - } - - ret = get_uar_info(mdev, &tot_uuars, &low_lat_uuars); - if (ret) - return NULL; - context = verbs_init_and_alloc_context(ibdev, cmd_fd, context, ibv_ctx, RDMA_DRIVER_MLX5); if (!context) @@ -1365,6 +1354,12 @@ static struct mlx5_context *mlx5_init_context(struct ibv_device *ibdev, strcpy(context->hostname, "host_unknown"); mlx5_single_threaded = single_threaded_app(); + + ret = get_uar_info(mdev, &tot_uuars, &low_lat_uuars); + if (ret) { + mlx5_uninit_context(context); + return NULL; + } context->tot_uuars = tot_uuars; context->low_lat_uuars = low_lat_uuars; @@ -1569,10 +1564,15 @@ static struct verbs_context *mlx5_alloc_context(struct ibv_device *ibdev, bool always_devx = false; int ret; - context = mlx5_init_context(ibdev, cmd_fd, NULL); + context = mlx5_init_context(ibdev, cmd_fd); if (!context) return NULL; + if (ctx_attr && ctx_attr->comp_mask) { + errno = EINVAL; + goto err; + } + req.total_num_bfregs = context->tot_uuars; req.num_low_latency_bfregs = context->low_lat_uuars; req.max_cqe_version = MLX5_CQE_VERSION_V1; @@ -1627,7 +1627,7 @@ static struct verbs_context *mlx5_import_context(struct ibv_device *ibdev, struct mlx5_context *mctx; int ret; - mctx = mlx5_init_context(ibdev, cmd_fd, NULL); + mctx = mlx5_init_context(ibdev, cmd_fd); if (!mctx) return NULL; -- 2.25.4