From patchwork Fri Jan 10 08:22:33 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shuai Xue X-Patchwork-Id: 13933927 Received: from out30-118.freemail.mail.aliyun.com (out30-118.freemail.mail.aliyun.com [115.124.30.118]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B93418F54; Fri, 10 Jan 2025 08:22:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.118 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736497366; cv=none; b=hLYQDrPuqcdfmP5hNbA+XfoOylMD3J/oCYifDqH+6XqPvdZd5EEf/JB6LrvTWnbiYZ+P0C5SqqCzeDMW8s2tYFz5UHyTBHAdEOUX6/keI/Hw1wVs3mcOUA0nTInXI12vuZOerdcKOAfA8CgdGi+YI7ze++mCd/4AT2jvkv/+h24= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736497366; c=relaxed/simple; bh=IElGyJw+SrDCt/hiQANTJdV06h4bSsP3ho0A9Vq4IBM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Ys1BwvbCGoY3PYcdcgyVsKjgkM1kQG/DjZYLZTyHCcICPrkiFeLnhPVrB5id6gpyJeUYvRUPRDVpOe1o4g7tiDQnP9AkwokEga8hE1Y+VFk7RJRdiJMn4A/ghufkfrWkaixHmL2eS1pBug/xQ9nrsnZn/n770MDNB9lb0X5au7g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b=mbpm4XNn; arc=none smtp.client-ip=115.124.30.118 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b="mbpm4XNn" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1736497360; h=From:To:Subject:Date:Message-ID:MIME-Version; bh=87EsWey0RhJOOSVtnxQRiNfjF2nSIAafUIVDhuFBxbM=; b=mbpm4XNno77pBt4HqHrR/+zSU94iKFxLVnzT58pslPT88AdahfWOCf+Stc6lMnC1ZvfXWARfhSananDMeXroVAcYVKWmR6KN1ZVxO51rUWLVytCcBSYsZxrSpEFohiKhRzD+OE9Vnde4QpKDN3ETwGGmik2UwTVLkWyitEDLhfU= Received: from localhost.localdomain(mailfrom:xueshuai@linux.alibaba.com fp:SMTPD_---0WNKKAxX_1736497359 cluster:ay36) by smtp.aliyun-inc.com; Fri, 10 Jan 2025 16:22:39 +0800 From: Shuai Xue To: fenghua.yu@intel.com, dave.jiang@intel.com, vkoul@kernel.org Cc: xueshuai@linux.alibaba.com, dmaengine@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v1 1/5] dmaengine: idxd: fix memory leak in error handling path of idxd_setup_wqs Date: Fri, 10 Jan 2025 16:22:33 +0800 Message-ID: <20250110082237.21135-2-xueshuai@linux.alibaba.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20250110082237.21135-1-xueshuai@linux.alibaba.com> References: <20250110082237.21135-1-xueshuai@linux.alibaba.com> Precedence: bulk X-Mailing-List: dmaengine@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Memory allocated for wqs is not freed if an error occurs during idxd_setup_wqs(). To fix it, free the allocated memory in the reverse order of allocation before exiting the function in case of an error. Signed-off-by: Shuai Xue --- drivers/dma/idxd/init.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/drivers/dma/idxd/init.c b/drivers/dma/idxd/init.c index 234c1c658ec7..6772d9251cd7 100644 --- a/drivers/dma/idxd/init.c +++ b/drivers/dma/idxd/init.c @@ -167,8 +167,8 @@ static int idxd_setup_wqs(struct idxd_device *idxd) idxd->wq_enable_map = bitmap_zalloc_node(idxd->max_wqs, GFP_KERNEL, dev_to_node(dev)); if (!idxd->wq_enable_map) { - kfree(idxd->wqs); - return -ENOMEM; + rc = -ENOMEM; + goto err_bitmap; } for (i = 0; i < idxd->max_wqs; i++) { @@ -189,6 +189,7 @@ static int idxd_setup_wqs(struct idxd_device *idxd) rc = dev_set_name(conf_dev, "wq%d.%d", idxd->id, wq->id); if (rc < 0) { put_device(conf_dev); + kfree(wq); goto err; } @@ -202,6 +203,7 @@ static int idxd_setup_wqs(struct idxd_device *idxd) wq->wqcfg = kzalloc_node(idxd->wqcfg_size, GFP_KERNEL, dev_to_node(dev)); if (!wq->wqcfg) { put_device(conf_dev); + kfree(wq); rc = -ENOMEM; goto err; } @@ -209,7 +211,9 @@ static int idxd_setup_wqs(struct idxd_device *idxd) if (idxd->hw.wq_cap.op_config) { wq->opcap_bmap = bitmap_zalloc(IDXD_MAX_OPCAP_BITS, GFP_KERNEL); if (!wq->opcap_bmap) { + kfree(wq->wqcfg); put_device(conf_dev); + kfree(wq); rc = -ENOMEM; goto err; } @@ -223,11 +227,21 @@ static int idxd_setup_wqs(struct idxd_device *idxd) return 0; err: - while (--i >= 0) { + while (i-- > 0) { wq = idxd->wqs[i]; + if (idxd->hw.wq_cap.op_config) + bitmap_free(wq->opcap_bmap); + kfree(wq->wqcfg); conf_dev = wq_confdev(wq); put_device(conf_dev); + kfree(wq); + } + bitmap_free(idxd->wq_enable_map); + +err_bitmap: + kfree(idxd->wqs); + return rc; } From patchwork Fri Jan 10 08:22:34 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shuai Xue X-Patchwork-Id: 13933930 Received: from out30-98.freemail.mail.aliyun.com (out30-98.freemail.mail.aliyun.com [115.124.30.98]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 26FFA2080F5; Fri, 10 Jan 2025 08:22:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.98 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736497371; cv=none; b=Rj064DlgCx5l+AdtGgwWBYdkpyjjmZeV3airsrVMlgmnHUYg6/iOPgD1HGpvKHVexKFvsQcvWF07p9ypHo3T6d8FCpQNtHHZxh7wBeSSV/VlLW4kbOF27DWNCKVT73HdFE+k3MjE/dU0diiDcTJatvHZzDBXAu+JV1JhFm5npOM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736497371; c=relaxed/simple; bh=6ynP5QCemGa7ciqesmiSqlv75Ch+kPi8NjIPF5qgKgI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=aWNx3JL1LO+9dyjDVN5zjTKe2gAuIwrbOXX2ysqbowbS8Sth+LPpfRKQHuJ1dnI0anffvSAIM5+ugQcJrm+CmYZu+BrVsGGnxhD/yZtFx4++ovDYYBxGpd/TGLfqp1mEZeZwSKa1jyRKyEz7dc6Llkp5cyeJ+bKvMarSgTZ18JY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b=W5UM5Od/; arc=none smtp.client-ip=115.124.30.98 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b="W5UM5Od/" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1736497361; h=From:To:Subject:Date:Message-ID:MIME-Version; bh=YK9ONjM2RkwwA5jSpeWYcpHTJVS9yzZfconXO/4rrbI=; b=W5UM5Od/PY2oS2hJ4BPHQgKI91ZEUioWa+MbrEH06qqh5Hs+QsbSK8kNgFBhzP/T6MsOOevSsKiBr+lXZG8G90dNLc66Y8f7LfD5iqLGGJMgVY4OX/1DmFh6XomlDdihxY0Q0k3NiZVSFDWTsz1DhI88os4T9MW2pWubmxwiqcc= Received: from localhost.localdomain(mailfrom:xueshuai@linux.alibaba.com fp:SMTPD_---0WNKKAxz_1736497359 cluster:ay36) by smtp.aliyun-inc.com; Fri, 10 Jan 2025 16:22:40 +0800 From: Shuai Xue To: fenghua.yu@intel.com, dave.jiang@intel.com, vkoul@kernel.org Cc: xueshuai@linux.alibaba.com, dmaengine@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v1 2/5] dmaengine: idxd: fix memory leak in error handling path of idxd_setup_engines Date: Fri, 10 Jan 2025 16:22:34 +0800 Message-ID: <20250110082237.21135-3-xueshuai@linux.alibaba.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20250110082237.21135-1-xueshuai@linux.alibaba.com> References: <20250110082237.21135-1-xueshuai@linux.alibaba.com> Precedence: bulk X-Mailing-List: dmaengine@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Memory allocated for engines is not freed if an error occurs during idxd_setup_engines(). To fix it, free the allocated memory in the reverse order of allocation before exiting the function in case of an error. Signed-off-by: Shuai Xue --- drivers/dma/idxd/init.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/drivers/dma/idxd/init.c b/drivers/dma/idxd/init.c index 6772d9251cd7..12df895dcbe9 100644 --- a/drivers/dma/idxd/init.c +++ b/drivers/dma/idxd/init.c @@ -275,6 +275,7 @@ static int idxd_setup_engines(struct idxd_device *idxd) rc = dev_set_name(conf_dev, "engine%d.%d", idxd->id, engine->id); if (rc < 0) { put_device(conf_dev); + kfree(engine); goto err; } @@ -288,7 +289,10 @@ static int idxd_setup_engines(struct idxd_device *idxd) engine = idxd->engines[i]; conf_dev = engine_confdev(engine); put_device(conf_dev); + kfree(engine); } + kfree(idxd->engines); + return rc; } From patchwork Fri Jan 10 08:22:35 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shuai Xue X-Patchwork-Id: 13933929 Received: from out30-97.freemail.mail.aliyun.com (out30-97.freemail.mail.aliyun.com [115.124.30.97]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F2EF52080F4; Fri, 10 Jan 2025 08:22:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.97 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736497371; cv=none; b=J76u7weLe3WH/Cw+wXDDFOp3a9Fo7QsOluYR2MuA1rzLg+Z8FvUJrRZGAguWnxd2MyZRmrcDci2jXxoYb5s+/JLSBISciRT+Km7RbZNaim58353X2E2xbKOkT81SpOdABedJDgmJx/lvFY+8skAt8JyqWmRgLQ6BjcDUK/jrUwc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736497371; c=relaxed/simple; bh=H4QyKG/CIPygzjcUse2kBVn446AYwzUzYW4wUkNpEbQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=pcm5aotbW851mmRq9dW2NvAKp69q4v4IdqQX2v1seqdgi7soZhIx5SGXPU74/9QqrqyBAj+03hCik2988oJwHPWcy7A8cESamBSMsPFTl7SnlW7kdAsPYyFXZR4fC/aYK2SOTXGo4Y8jX4EwdTr8MJzy8G+svKDs9ij9zDS/PNk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b=fYSP68zh; arc=none smtp.client-ip=115.124.30.97 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b="fYSP68zh" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1736497361; h=From:To:Subject:Date:Message-ID:MIME-Version; bh=gyz7gxL8sTioSa9ZkEFHWicCkHY5IqxE613ISdg8wqY=; b=fYSP68zhiOIN+SSOf614CCesrLPjpqax15QMA2JxVtukWO9eULKKGqqt96Bh5G9SmheqhpmanJe7UTG1WozlkofNzK4VhoNfOu2fmWvHr4eC6q3c97eXE1SjBxMTbpYAsQ1BBv87zBdZuve6/emoetKsoGebHPWZdVz/rSnXLU8= Received: from localhost.localdomain(mailfrom:xueshuai@linux.alibaba.com fp:SMTPD_---0WNKKAyC_1736497360 cluster:ay36) by smtp.aliyun-inc.com; Fri, 10 Jan 2025 16:22:40 +0800 From: Shuai Xue To: fenghua.yu@intel.com, dave.jiang@intel.com, vkoul@kernel.org Cc: xueshuai@linux.alibaba.com, dmaengine@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v1 3/5] dmaengine: idxd: fix memory leak in error handling path of idxd_setup_groups Date: Fri, 10 Jan 2025 16:22:35 +0800 Message-ID: <20250110082237.21135-4-xueshuai@linux.alibaba.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20250110082237.21135-1-xueshuai@linux.alibaba.com> References: <20250110082237.21135-1-xueshuai@linux.alibaba.com> Precedence: bulk X-Mailing-List: dmaengine@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Memory allocated for groups is not freed if an error occurs during idxd_setup_groups(). To fix it, free the allocated memory in the reverse order of allocation before exiting the function in case of an error. Signed-off-by: Shuai Xue --- drivers/dma/idxd/init.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/drivers/dma/idxd/init.c b/drivers/dma/idxd/init.c index 12df895dcbe9..04a7d7706e53 100644 --- a/drivers/dma/idxd/init.c +++ b/drivers/dma/idxd/init.c @@ -326,6 +326,7 @@ static int idxd_setup_groups(struct idxd_device *idxd) rc = dev_set_name(conf_dev, "group%d.%d", idxd->id, group->id); if (rc < 0) { put_device(conf_dev); + kfree(group); goto err; } @@ -350,7 +351,10 @@ static int idxd_setup_groups(struct idxd_device *idxd) while (--i >= 0) { group = idxd->groups[i]; put_device(group_confdev(group)); + kfree(group); } + kfree(idxd->groups); + return rc; } From patchwork Fri Jan 10 08:22:36 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shuai Xue X-Patchwork-Id: 13933931 Received: from out30-97.freemail.mail.aliyun.com (out30-97.freemail.mail.aliyun.com [115.124.30.97]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5C8AE208965; Fri, 10 Jan 2025 08:22:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.97 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736497372; cv=none; b=n7g3C/0fchbOyAKN7bdsj6C+ZJnkgNMWVC0Jos6FXIpYHEgMtTnDAZsIlLvtxaBLqDazGZe80DSJ/bP0ETqWxQHD/8HYFLudtswNZ7JhVJeJCIVChIBag/iND7i10JeHZHJVU24X3ZaXyZL4n0YQNnYcI7DjxdhZ1RPVQ7r8n4k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736497372; c=relaxed/simple; bh=eZ6qGRp8Sl50vHQ6fafEjmFm4UqS3/j9h1j1tUXJIt8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=SPd23S2hOpGLd3QRat9O2TmaDEuYmDENEJnzvs/kyQ1Ujoww+sRLnQR8VY99BjRxWzEoO+RBzrx3CByWn3ARRcu8GHfNB4rrevDEA5R0nBG+HnB68GQRsKUFY+sJrK6OULZ8i0hcxAe1serOFfrhWHTGrw4vbv11PELmJsb2e+I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b=I94tHZX6; arc=none smtp.client-ip=115.124.30.97 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b="I94tHZX6" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1736497362; h=From:To:Subject:Date:Message-ID:MIME-Version; bh=Y39bmo1kfIoLJrmzBnU9g6zt0b6qm/1GfUFR/ePXUA0=; b=I94tHZX6XtjEbIaQ5WaxBIr1fVv085CUNxudavYGZG8Qux4bogMDaSVc46C33Wer234qlnWTgPIuLbmMkBj0HMxuQJ8cUWFGK1qGWHHdGSYRTNfEJ1Pzed0u78+oeE6BsUfi1iAA6oHMih4mT9gb2JQQAG5P6aporyMR0uDmJFc= Received: from localhost.localdomain(mailfrom:xueshuai@linux.alibaba.com fp:SMTPD_---0WNKKAyX_1736497360 cluster:ay36) by smtp.aliyun-inc.com; Fri, 10 Jan 2025 16:22:41 +0800 From: Shuai Xue To: fenghua.yu@intel.com, dave.jiang@intel.com, vkoul@kernel.org Cc: xueshuai@linux.alibaba.com, dmaengine@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v1 4/5] dmaengine: idxd: fix memory leak in error handling path of idxd_alloc Date: Fri, 10 Jan 2025 16:22:36 +0800 Message-ID: <20250110082237.21135-5-xueshuai@linux.alibaba.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20250110082237.21135-1-xueshuai@linux.alibaba.com> References: <20250110082237.21135-1-xueshuai@linux.alibaba.com> Precedence: bulk X-Mailing-List: dmaengine@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Memory allocated for idxd is not freed if an error occurs during idxd_alloc(). To fix it, free the allocated memory in the reverse order of allocation before exiting the function in case of an error. Signed-off-by: Shuai Xue --- drivers/dma/idxd/init.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/drivers/dma/idxd/init.c b/drivers/dma/idxd/init.c index 04a7d7706e53..f0e3244d630d 100644 --- a/drivers/dma/idxd/init.c +++ b/drivers/dma/idxd/init.c @@ -565,28 +565,34 @@ static struct idxd_device *idxd_alloc(struct pci_dev *pdev, struct idxd_driver_d idxd_dev_set_type(&idxd->idxd_dev, idxd->data->type); idxd->id = ida_alloc(&idxd_ida, GFP_KERNEL); if (idxd->id < 0) - return NULL; + goto err_ida; idxd->opcap_bmap = bitmap_zalloc_node(IDXD_MAX_OPCAP_BITS, GFP_KERNEL, dev_to_node(dev)); - if (!idxd->opcap_bmap) { - ida_free(&idxd_ida, idxd->id); - return NULL; - } + if (!idxd->opcap_bmap) + goto err_opcap; device_initialize(conf_dev); conf_dev->parent = dev; conf_dev->bus = &dsa_bus_type; conf_dev->type = idxd->data->dev_type; rc = dev_set_name(conf_dev, "%s%d", idxd->data->name_prefix, idxd->id); - if (rc < 0) { - put_device(conf_dev); - return NULL; - } + if (rc < 0) + goto err_name; spin_lock_init(&idxd->dev_lock); spin_lock_init(&idxd->cmd_lock); return idxd; + +err_name: + put_device(conf_dev); + bitmap_free(idxd->opcap_bmap); +err_opcap: + ida_free(&idxd_ida, idxd->id); +err_ida: + kfree(idxd); + + return NULL; } static int idxd_enable_system_pasid(struct idxd_device *idxd) From patchwork Fri Jan 10 08:22:37 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shuai Xue X-Patchwork-Id: 13933932 Received: from out30-97.freemail.mail.aliyun.com (out30-97.freemail.mail.aliyun.com [115.124.30.97]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 10650209F42; Fri, 10 Jan 2025 08:22:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.97 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736497376; cv=none; b=kZk8XDTCAED8YLJbMETZgjyeIJ0/19CNoL1rocuguDmhTe3aCKKpfM1bC7w9KegmbX8XAf43Pe+v7anZ2fHtgKRn6gfI/qtc/W+tK1g4l4ytWVt4DMQrqu02yUt0hPlah7vANDKyQCHreR0o9HdyxWoXtifeNdB5ud2E2Gb9ZnI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736497376; c=relaxed/simple; bh=jAXyNdGpLusIMcN0H/AcL5jBYzQPf6ZZaDI5zT7gpcw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=OjpxpBN1NBLsVj3+gnwcZHLaKZCL1jbEC4qhu+wuB2wW8dzmWHOnveDPCH/jmMWYEt/N8tr8Bmu/4mm3AfroFEb8X4cpPS1mPIWFlsyXVCpqWzoPPIlVTlItHTM37lco04x/hQPSAtLs3fEbjg26LSuePUGZT2eIrn7lZfam3w0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b=WnJ8kU0n; arc=none smtp.client-ip=115.124.30.97 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b="WnJ8kU0n" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1736497362; h=From:To:Subject:Date:Message-ID:MIME-Version; bh=d9edESLYCYiUNjixE+f/sSXJEX3fsz5wEeQXrpg40es=; b=WnJ8kU0nkl+1GpVKWIQgi7dJLa5mZ2O9+kovgv3RFDl9/saYwW1KSAhJp3xbIy1TRz6xtgMUfT4quoQZ++ezOfMd2FCpOj5vBlHou9r1IgB/ffvhDCoHRNwrnuwoUyvtSHL/oHzjcUcOCiF8tSUyjvMFwoeidotr+nMGIBOia0Y= Received: from localhost.localdomain(mailfrom:xueshuai@linux.alibaba.com fp:SMTPD_---0WNKKAyo_1736497361 cluster:ay36) by smtp.aliyun-inc.com; Fri, 10 Jan 2025 16:22:41 +0800 From: Shuai Xue To: fenghua.yu@intel.com, dave.jiang@intel.com, vkoul@kernel.org Cc: xueshuai@linux.alibaba.com, dmaengine@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v1 5/5] dmaengine: idxd: fix memory leak in error handling path of idxd_pci_probe Date: Fri, 10 Jan 2025 16:22:37 +0800 Message-ID: <20250110082237.21135-6-xueshuai@linux.alibaba.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20250110082237.21135-1-xueshuai@linux.alibaba.com> References: <20250110082237.21135-1-xueshuai@linux.alibaba.com> Precedence: bulk X-Mailing-List: dmaengine@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Memory allocated for idxd is not freed if an error occurs during idxd_pci_probe(). To fix it, free the allocated memory in the reverse order of allocation before exiting the function in case of an error. Signed-off-by: Shuai Xue --- drivers/dma/idxd/init.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/drivers/dma/idxd/init.c b/drivers/dma/idxd/init.c index f0e3244d630d..9b44f5d38d3a 100644 --- a/drivers/dma/idxd/init.c +++ b/drivers/dma/idxd/init.c @@ -548,6 +548,14 @@ static void idxd_read_caps(struct idxd_device *idxd) idxd->hw.iaa_cap.bits = ioread64(idxd->reg_base + IDXD_IAACAP_OFFSET); } +static void idxd_free(struct idxd_device *idxd) +{ + put_device(idxd_confdev(idxd)); + bitmap_free(idxd->opcap_bmap); + ida_free(&idxd_ida, idxd->id); + kfree(idxd); +} + static struct idxd_device *idxd_alloc(struct pci_dev *pdev, struct idxd_driver_data *data) { struct device *dev = &pdev->dev; @@ -820,7 +828,7 @@ static int idxd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) err: pci_iounmap(pdev, idxd->reg_base); err_iomap: - put_device(idxd_confdev(idxd)); + idxd_free(idxd); err_idxd_alloc: pci_disable_device(pdev); return rc;