Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp210139iob; Tue, 3 May 2022 15:18:44 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzKPhClHioafEt0yaB8VkxGIxh7Y2t2789pty67BMyDgODVN3ftKXSTOo91YWuQ/oTpFQXB X-Received: by 2002:a17:906:8696:b0:6f3:a043:6ea7 with SMTP id g22-20020a170906869600b006f3a0436ea7mr17808848ejx.387.1651616324448; Tue, 03 May 2022 15:18:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1651616324; cv=none; d=google.com; s=arc-20160816; b=sBM2wpM8P9RXK40bvOyGXD7NDjmfz8pBbC2W4lKl28gcMGo7CfyDN2EimGBV8Q1pbg COtMuh5o+zuIiiRa/LwN7lnwBxPsiYaM9OyMgdThFYOQvP4mMMYCWZ8LUZdW+yl8Vaff WbrU56r0tAPAutJW/gCP+yjOSiNr+2+ZP0Ywp8ilE9O3R8McKYqIspb+NTyPlqm84aX+ T+K4UMo0rAx5U6567G23aQwe63NImqiUBbS1vIHsF+Uwx8JJ2HobC0IDQKXzxhPd0gDn U0/DvrZGtAyvSWN+mV/uX3xz8LzJRRZkBjxwpMCdJy6UgWQGaj8zRMcn5RIi4s02dJGn OqYQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:dkim-filter; bh=bo5OqnK+Z6C/zfKXy1FN6D2uyOimMZxXIfJNT8kWNlI=; b=wC1KnoyXhWUviXgiZfTns9AEzAo5nwkwazQP+0o1EuuhggJFJFGQWio3+GeQ7sYSIr uWP3EOPBzTPi3KFE23N9SV1IuzCO2apib8doYFdwa5B21G4dCQzvYxeJmOJBVXi54Cba H7ykijOUfGjGoNS9SqxUkYYBV001DpyXpyq6ZWTG9fzjJCiRAe8NIE2hxoFc/ULylGRx tEPaMiDcedF8clb7s1T0h5xD44I4sh4fAf8OVk2hNW+Kj5Wxki0KosBBowbuazJOP7zZ iQ9KuQGVTKmTFjSEXUBhvXjUyCnzcPN2XhUQ6w93POs/t3pRUnivu0fXATWPrHE/g+bO pEwA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@baikalelectronics.ru header.s=mail header.b=icKemu7S; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f7-20020a170906084700b006e874b9da7fsi14532122ejd.28.2022.05.03.15.18.01; Tue, 03 May 2022 15:18:44 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@baikalelectronics.ru header.s=mail header.b=icKemu7S; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242899AbiECV1f (ORCPT + 99 others); Tue, 3 May 2022 17:27:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41838 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242887AbiECV1W (ORCPT ); Tue, 3 May 2022 17:27:22 -0400 Received: from mail.baikalelectronics.ru (mail.baikalelectronics.com [87.245.175.226]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 221464162E; Tue, 3 May 2022 14:23:36 -0700 (PDT) Received: from mail.baikalelectronics.ru (unknown [192.168.51.25]) by mail.baikalelectronics.ru (Postfix) with ESMTP id 5D53816A9; Wed, 4 May 2022 00:24:09 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 mail.baikalelectronics.ru 5D53816A9 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baikalelectronics.ru; s=mail; t=1651613049; bh=bo5OqnK+Z6C/zfKXy1FN6D2uyOimMZxXIfJNT8kWNlI=; h=From:To:CC:Subject:Date:In-Reply-To:References:From; b=icKemu7SIwVe5HDWo/ep1D9y8aOHLzTYni1wpNEF53a3vC9mME5CA6sE+3K6iE3NE 4yxxXUuURFofMY9YRBUEsQ2hYa1P5mL9cGNmPf5MrxrT86g2UwgopxDo5vp+e7Qqn4 2H11maKoNHZiZfaLEfpHYaufos/aY7SHAYGV0tIM= Received: from localhost (192.168.53.207) by mail (192.168.51.25) with Microsoft SMTP Server (TLS) id 15.0.1395.4; Wed, 4 May 2022 00:23:35 +0300 From: Serge Semin To: Jingoo Han , Gustavo Pimentel , Bjorn Helgaas , Lorenzo Pieralisi , Rob Herring , =?UTF-8?q?Krzysztof=20Wilczy=C5=84ski?= , Niklas Cassel , Joao Pinto CC: Serge Semin , Serge Semin , Alexey Malahov , Pavel Parkhomenko , Frank Li , Manivannan Sadhasivam , , Subject: [PATCH v2 10/13] PCI: dwc: Deallocate EPC memory on EP init error Date: Wed, 4 May 2022 00:22:57 +0300 Message-ID: <20220503212300.30105-11-Sergey.Semin@baikalelectronics.ru> In-Reply-To: <20220503212300.30105-1-Sergey.Semin@baikalelectronics.ru> References: <20220503212300.30105-1-Sergey.Semin@baikalelectronics.ru> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: MAIL.baikal.int (192.168.51.25) To mail (192.168.51.25) X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org If the dw_pcie_ep_init() method fails to perform any action after the EPC memory is initialized and the MSI memory region is allocated, the later parts won't be undone thus causing the memory leak. Let's fix that by introducing the cleanup-on-error path in the dw_pcie_ep_init() method, which will be taken should any consequent erroneous situation happens. Fixes: 2fd0c9d966cc ("PCI: designware-ep: Pre-allocate memory for MSI in dw_pcie_ep_init") Signed-off-by: Serge Semin --- Changelog v2: - This is a new patch create as a result of the discussion in: Link: https://lore.kernel.org/linux-pci/20220324014836.19149-26-Sergey.Semin@baikalelectronics.ru --- .../pci/controller/dwc/pcie-designware-ep.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/drivers/pci/controller/dwc/pcie-designware-ep.c b/drivers/pci/controller/dwc/pcie-designware-ep.c index 7c9315fffe24..7ad349c32082 100644 --- a/drivers/pci/controller/dwc/pcie-designware-ep.c +++ b/drivers/pci/controller/dwc/pcie-designware-ep.c @@ -780,8 +780,9 @@ int dw_pcie_ep_init(struct dw_pcie_ep *ep) ep->msi_mem = pci_epc_mem_alloc_addr(epc, &ep->msi_mem_phys, epc->mem->window.page_size); if (!ep->msi_mem) { + ret = -ENOMEM; dev_err(dev, "Failed to reserve memory for MSI/MSI-X\n"); - return -ENOMEM; + goto err_exit_epc_mem; } if (ep->ops->get_features) { @@ -790,6 +791,19 @@ int dw_pcie_ep_init(struct dw_pcie_ep *ep) return 0; } - return dw_pcie_ep_init_complete(ep); + ret = dw_pcie_ep_init_complete(ep); + if (ret) + goto err_free_epc_mem; + + return 0; + +err_free_epc_mem: + pci_epc_mem_free_addr(epc, ep->msi_mem_phys, ep->msi_mem, + epc->mem->window.page_size); + +err_exit_epc_mem: + pci_epc_mem_exit(epc); + + return ret; } EXPORT_SYMBOL_GPL(dw_pcie_ep_init); -- 2.35.1