[PATCH v2] RDMA/mlx5: Preserve DEVX objects after failed output copy

From: Jaeyeong Lee

Date: Sun Nov 22 2026 - 05:37:33 EST


A DEVX create can successfully create and publish a hardware object
before uverbs_copy_to() returns an error. The error path currently
destroys the object directly and frees struct devx_obj without checking
whether destruction succeeded.

This is unsafe for DCTs. mlx5_core_destroy_dct() returns immediately if
the DRAIN_DCT command fails, leaving core_dct in qp_table.dct_xa. A later
DCT_DRAINED event retrieves the stale entry and completes the embedded
completion after devx_obj was freed.

The failure was reproduced by forcing DRAIN_DCT to fail and then
delivering a DCT_DRAINED event:

BUG: KASAN: slab-use-after-free in complete+0x1a7/0x1b0
Write of size 4 at addr ffff88800b112ca8 by task insmod/77
Call Trace:
complete+0x1a7/0x1b0
rsc_event_notifier+0x1fc/0x410
init_module+0x454/0xff0 [mlx5_dct_repro]

Initialize the object ID and indirect mkey bookkeeping before marking
the uobject as holding a valid hardware object. Mark it valid immediately
before copying the output. On a copy failure, the uverbs abort path now
invokes devx_obj_cleanup(). If destruction fails,
rdma_alloc_abort_uobject() commits the uobject, keeping its wrapper alive
for another cleanup attempt at file close.

Keep the direct unwind path for indirect mkey setup failures because
those occur before the uobject is marked valid. This avoids running
generic cleanup against partially initialized mkey bookkeeping.

The issue was found by NSCV static analysis.

Fixes: c5ae1954c47d3 ("IB/mlx5: Use mlx5 core to create/destroy a DEVX DCT")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Finder16 <nakamurajames123@xxxxxxxxx>
Signed-off-by: Jaeyeong Lee <lee@xxxxxxxxxxx>
---
Changes in v2:
- Include the KASAN reproducer trace and describe the failed DCT destroy
path.
- Initialize obj_id and indirect mkey state before finalizing the uobject.
- Keep the direct unwind path for failures before uobject finalization.

drivers/infiniband/hw/mlx5/devx.c | 12 ++++++++----
1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/drivers/infiniband/hw/mlx5/devx.c b/drivers/infiniband/hw/mlx5/devx.c
index 5ba783bc7f9b7..fd41104fe42fe 100644
--- a/drivers/infiniband/hw/mlx5/devx.c
+++ b/drivers/infiniband/hw/mlx5/devx.c
@@ -1614,10 +1614,6 @@ static int UVERBS_HANDLER(MLX5_IB_METHOD_DEVX_OBJ_CREATE)(
&obj_id);
WARN_ON(obj->dinlen > MLX5_MAX_DESTROY_INBOX_SIZE_DW * sizeof(u32));

- err = uverbs_copy_to(attrs, MLX5_IB_ATTR_DEVX_OBJ_CREATE_CMD_OUT, cmd_out, cmd_out_len);
- if (err)
- goto obj_destroy;
-
if (opcode == MLX5_CMD_OP_CREATE_GENERAL_OBJECT)
obj_type = MLX5_GET(general_obj_in_cmd_hdr, cmd_in, obj_type);
obj->obj_id = get_enc_obj_id(opcode | obj_type << 16, obj_id);
@@ -1627,6 +1623,14 @@ static int UVERBS_HANDLER(MLX5_IB_METHOD_DEVX_OBJ_CREATE)(
if (err)
goto obj_destroy;
}
+
+ uverbs_finalize_uobj_create(attrs,
+ MLX5_IB_ATTR_DEVX_OBJ_CREATE_HANDLE);
+ err = uverbs_copy_to(attrs, MLX5_IB_ATTR_DEVX_OBJ_CREATE_CMD_OUT,
+ cmd_out, cmd_out_len);
+ if (err)
+ return err;
+
return 0;

obj_destroy:

base-commit: 7404c82cbf1246e0e84e2ec21834d23c26f9a8ed
--
2.43.0