From patchwork Sun Mar 9 06:20:55 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shuai Xue X-Patchwork-Id: 14008108 Received: from out30-100.freemail.mail.aliyun.com (out30-100.freemail.mail.aliyun.com [115.124.30.100]) (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 308B517A2E7; Sun, 9 Mar 2025 06:21:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.100 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741501276; cv=none; b=UruLBmfu2FVM1iNvGDwdigCNVWkmFK8u3uVnSu8TUhzUmCK28A3YcERmDA2KAAaaCLgy5A8QOFRXpvGo8GVC/tW531GwDgEuiDiPtCJvOEykfjgU8JfYipue97baTxUapWqCq4xgg6ZSDufrlN2c3EfbeW1Xse/VTCLVy47XsrI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741501276; c=relaxed/simple; bh=6Tp77EWvZ8bSko77/in35MLvAR8N97J5rjnacTNNm/Q=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=MAj8pWyi0op7++pBW3OxLVzPeF1gJpC0+7O1DByYJ34lBIbjbwp5c6qxjxPUT85zmxY4k09VK599oMAf+mQXEDIzd5ns0YeyYaBcmIxw6iB8zkrOAuu0z3pZuXevsSmKrK3TN433AZO1vyIkH8D/zx6tQ3HQNiXHNjngtIqrj3U= 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=HhN+KgDk; arc=none smtp.client-ip=115.124.30.100 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="HhN+KgDk" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1741501264; h=From:To:Subject:Date:Message-ID:MIME-Version; bh=WDWCiEH3p92JM6m/K5U+rsPTa1F3tZoA7/dPMUZkSMY=; b=HhN+KgDkA6i9Hb7L/MD0EIdQisLWNw+E5mZG3cNSnccBakjzWggH+ssOilPlwR9p5pQaegh9id1iycU/mPZqBpLf8ddvFli2EumjjbhRR/aQBQkpvPo0Tiw9ihoe89sQhvlb2evjLfM0DYXSB8XobgQa24EGYG4zqPEfWJVzf1o= Received: from localhost.localdomain(mailfrom:xueshuai@linux.alibaba.com fp:SMTPD_---0WQwVyk8_1741501263 cluster:ay36) by smtp.aliyun-inc.com; Sun, 09 Mar 2025 14:21:03 +0800 From: Shuai Xue To: vinicius.gomes@intel.com, dave.jiang@intel.com, Markus.Elfring@web.de, fenghuay@nvidia.com, vkoul@kernel.org Cc: xueshuai@linux.alibaba.com, dmaengine@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 6/9] dmaengine: idxd: fix memory leak in error handling path of idxd_alloc Date: Sun, 9 Mar 2025 14:20:55 +0800 Message-ID: <20250309062058.58910-7-xueshuai@linux.alibaba.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20250309062058.58910-1-xueshuai@linux.alibaba.com> References: <20250309062058.58910-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. Fixes: a8563a33a5e2 ("dmanegine: idxd: reformat opcap output to match bitmap_parse() input") Cc: stable@vger.kernel.org Signed-off-by: Shuai Xue Reviewed-by: Dave Jiang --- 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 cf5dc981be32..71d92c05c0c6 100644 --- a/drivers/dma/idxd/init.c +++ b/drivers/dma/idxd/init.c @@ -605,28 +605,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)