From nobody Sat Jun 20 18:32:38 2026 Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9A66236CE03 for ; Sat, 11 Apr 2026 15:35:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775921704; cv=none; b=kZ975cGSvICf7B73gm31wL/VzSh4iR5DboyAZZ+fxLo9pjiC90o0A1r8Pho7MBLbyJcEb1ep4OYs/+Nrh1RNzMHdXyERj/nyvDK/WfjYMOUEeXkyUzhGXuAai63T4K11CPXclMEFYIvq76our5SNl//ky0nw5JKSan9q5B0xhk8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775921704; c=relaxed/simple; bh=9gn7wIBOcFYMsLzD9sSx+XVudwVdXhut3Y3kl4QYXSg=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=tJX4mpQxuEYWSUNguZXt2fI2sGbHJRkDwXEKm6tjoyFjfespYge8BFSeDm9KumBAnConDPcFlnLY25mywfNL0wNrojXBtP89zoNK219/jZ69XmI6aO02z6RAuSZtdz05n5Mw7K0ZIBPuD96igomAYYIdlx26PRVJ5rCX9wtH1G0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Wf8nywKx; arc=none smtp.client-ip=209.85.214.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Wf8nywKx" Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-2b258576d8cso17844695ad.0 for ; Sat, 11 Apr 2026 08:35:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1775921703; x=1776526503; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=6G9c1hswwZhFnlkchy0rcgR6pUj1NyepQxxTFJFOZMg=; b=Wf8nywKxx9RdjYLM9EBkvT5pBlrPXzWwig5/SCk1uPhrDmIrS8SiI4bNzrsaAcuDh/ xOzktxK3dxQHNG1wBxQQ/SOEXnClMdb74d9NwBqsgemaXk1Pl4J8zReniBlBDt02lcCT S9dpK9Sja8MgzqvZ0dFitMX4ZOpM29r00QBaDcgn1xn8EpJZojjHsT8pkq7arlJaVKq/ ZCVlkIKMHUQKLDf1yYrJFrJmfy58ebfmXMbRQAJXtqfjD9kRH6vzC1X+8uRFatz+LhjW tF2XaP6Xy70xOEgzLqmQo8+/fpiZvuTILONNEEP0+PirC3lWfSsjWL0d4IMzhZJ4hNYZ o3cg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775921703; x=1776526503; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=6G9c1hswwZhFnlkchy0rcgR6pUj1NyepQxxTFJFOZMg=; b=WlIM0IBLU4O701/bX6+pA1mjT2mi6+JoXxwdn6hsDR9ES78CtmRCS70EVRRohkdRtw WFQTxX9wQEDqy4Ux3inMqrpao1i7N1VMLOUWuZ8hVHnx4eMfrK5R5lwaldus2fi/RBCN CoFN/M5NqZ3PY5RqOtvd3qwzRf8eBxvBfANIgWC5N+WqIbgYhjEfMnxkcNAvI7jaOilP A20mVxB2bLBhMTX1fT4H1ajucC1FQAScBTIxXR6455QSQl+PoSfe3iHldr2ezt6Tbpct eRBCJX4uHwCM/qE/p9mlo5GNTgAD2nf3/IRaVLU7SYc5ZMgBQoGstiYdZ8LsmJELNSO4 ARgA== X-Forwarded-Encrypted: i=1; AFNElJ+7PL89yBaL/XOKNqO9BkWmTV7yykoti+J4hMKOb5/9tpSHo9UC6+xIM2xkQ5hBjk5pI14AMzmozfBPaio=@vger.kernel.org X-Gm-Message-State: AOJu0Yzo5KpVr7PBlQb+B/+ELyuju1ZMwoHOGa1YdS9tu01OZ5gG4dM5 S11mLoDmiubHC/al6AArGfr83t60f/LHHPZUmUEO67+PzOc8TaYCJfDJ X-Gm-Gg: AeBDiesC0BkXDHuhogjc1xCREBhOtkSpTS05yRpp5XxFJoixUs82jUocCfpDduCnvW9 OSQUUAOcHBdxD7dIyHNZhPGCY76ozKgKVzxdttdQzgYibFyKQc46BIrT2/AaRiD11ERKQ+uv9OD mdyxONMhwMNMLgBlBDQ69bftlGH/Axc53ss4dYUqFG+JN35lPu0wd7ZB+AqD5oXL5rwiTN/2hrk DbjxNqQIDENowBExxQDbWVkHLzyN1C+fgMMYpGMqrFlEHNUDa6o683TAzuBzlnnE+HiuXlVfgan t06czn7+Qii3kIzPJMamUrZFUWbLXpi4f0jH7OAgpTXsQISDQw2P6zmtPXspjlWWJfVdF5kJQT9 vqhDJJm4TVLC48H8dqYeFMLEMtjdd5t3C/tq1Y2CjAKbin8nBFhggcILA3zTAQssUrZZWKMcTvN ws7FIjUkZP+hafEEo6pOqewQ== X-Received: by 2002:a17:902:f785:b0:2b0:7b57:830f with SMTP id d9443c01a7336-2b2d5a5b511mr81392905ad.33.1775921703022; Sat, 11 Apr 2026 08:35:03 -0700 (PDT) Received: from lgs.. ([112.224.67.108]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2b2d83190ccsm55363005ad.38.2026.04.11.08.34.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 11 Apr 2026 08:35:02 -0700 (PDT) From: Guangshuo Li To: Vinicius Costa Gomes , Dave Jiang , Vinod Koul , Fenghua Yu , dmaengine@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Guangshuo Li , stable@vger.kernel.org Subject: [PATCH] dmaengine: idxd: fix double free in idxd_cdev_open() error path Date: Sat, 11 Apr 2026 23:34:45 +0800 Message-ID: <20260411153445.2324473-1-lgs201920130244@gmail.com> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When dev_set_name() or device_add() fails, the call chain is: idxd_cdev_open() -> device_initialize(fdev) -> dev_set_name() / device_add() -> failure -> put_device(fdev) -> idxd_file_dev_release() -> kfree(ctx) Then control returns to idxd_cdev_open(), where the error path continues with: failed: -> kfree(ctx) Thus, ctx is freed twice. In addition, idxd_file_dev_release() also calls ida_free() and idxd_wq_put(), but in the current code ctx->id is allocated and the wq reference is taken only after device_add() succeeds. If put_device(fdev) runs the release callback before that point, the cleanup is not balanced. Allocate the file ida and take the wq reference before device_initialize(), so the device release callback can own the cleanup after put_device(). For the dev_set_name() and device_add() failure path, let put_device() and the release callback handle resource teardown. Fixes: e6fd6d7e5f0fe ("dmaengine: idxd: add a device to represent the file = opened") Cc: stable@vger.kernel.org Signed-off-by: Guangshuo Li --- drivers/dma/idxd/cdev.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/drivers/dma/idxd/cdev.c b/drivers/dma/idxd/cdev.c index 7e4715f92773..524146b0000c 100644 --- a/drivers/dma/idxd/cdev.c +++ b/drivers/dma/idxd/cdev.c @@ -225,6 +225,7 @@ static int idxd_cdev_open(struct inode *inode, struct f= ile *filp) struct iommu_sva *sva =3D NULL; unsigned int pasid; struct idxd_cdev *idxd_cdev; + bool wq_ref =3D false; =20 wq =3D inode_wq(inode); idxd =3D wq->idxd; @@ -280,12 +281,16 @@ static int idxd_cdev_open(struct inode *inode, struct= file *filp) } } =20 - idxd_cdev =3D wq->idxd_cdev; ctx->id =3D ida_alloc(&file_ida, GFP_KERNEL); if (ctx->id < 0) { dev_warn(dev, "ida alloc failure\n"); goto failed_ida; } + + idxd_wq_get(wq); + wq_ref =3D true; + + idxd_cdev =3D wq->idxd_cdev; ctx->idxd_dev.type =3D IDXD_DEV_CDEV_FILE; fdev =3D user_ctx_dev(ctx); device_initialize(fdev); @@ -305,20 +310,23 @@ static int idxd_cdev_open(struct inode *inode, struct= file *filp) goto failed_dev_add; } =20 - idxd_wq_get(wq); mutex_unlock(&wq->wq_lock); return 0; =20 failed_dev_add: failed_dev_name: put_device(fdev); -failed_ida: + mutex_unlock(&wq->wq_lock); + return rc; failed_set_pasid: if (device_user_pasid_enabled(idxd)) idxd_xa_pasid_remove(ctx); failed_get_pasid: if (device_user_pasid_enabled(idxd) && !IS_ERR_OR_NULL(sva)) iommu_sva_unbind_device(sva); +failed_ida: + if (wq_ref) + idxd_wq_put(wq); failed: mutex_unlock(&wq->wq_lock); kfree(ctx); --=20 2.43.0