From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_SANE_1 autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id BB948C43461 for ; Fri, 11 Sep 2020 09:53:07 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 23F492075A for ; Fri, 11 Sep 2020 09:53:07 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="SrvZ13tl" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 23F492075A Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=arm.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References:Message-ID: Subject:To:From:Date:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=wIaeGvKznyS1eafpu2ShxlSYzrL7WaSjnvqouvsEfHw=; b=SrvZ13tl3pZDnZ80OzlFK3nbe mSadXOH9gCY4lbAQe0cybYfOTP3fNJgnE+ZJ02XP4pGKlkeei3LtslwgMGvlxzAxIlGmlHsm24FnA X4vFjNyRfMQhkQFUdUWyYufymbltBHwyTtXx+lbSGx5RHXGEJewCAG5zaBTUNyRtgmp6ucKxW6CtZ mdIx0UoZzu+FCMTaIoE/tRUNiqm8iGV7QvppLhht7lHXsfoON01iU5cdMz3IGx0oQSTKpcbL+1qWj Q85SYKxsb0LTpEGcCU9J1Sj/Yek7sS8spYybk/Z7Cq5Gjh3pKm7P3PmO8FB6mIb9HoHYWKTH919D7 ZeICRc7uA==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kGfip-0007KY-31; Fri, 11 Sep 2020 09:51:59 +0000 Received: from foss.arm.com ([217.140.110.172]) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kGfil-0007Jn-PV for linux-arm-kernel@lists.infradead.org; Fri, 11 Sep 2020 09:51:56 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 783B8106F; Fri, 11 Sep 2020 02:51:54 -0700 (PDT) Received: from e121166-lin.cambridge.arm.com (e121166-lin.cambridge.arm.com [10.1.196.255]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 41E733F68F; Fri, 11 Sep 2020 02:51:53 -0700 (PDT) Date: Fri, 11 Sep 2020 10:51:47 +0100 From: Lorenzo Pieralisi To: Catalin Marinas Subject: Re: [PATCH] arm64: PCI: fix memleak when calling pci_iomap/unmap() Message-ID: <20200911095147.GA5900@e121166-lin.cambridge.arm.com> References: <20200907104546.GC26513@gaia> <20200907112118.GD26513@gaia> <20200909113613.GB6384@e121166-lin.cambridge.arm.com> <20200909135400.GB13047@gaia> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20200909135400.GB13047@gaia> User-Agent: Mutt/1.9.4 (2018-02-28) X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20200911_055155_914208_EF6286D2 X-CRM114-Status: GOOD ( 31.52 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "linux-pci@vger.kernel.org" , "guohanjun@huawei.com" , "will.deacon@arm.com" , "linux-kernel@vger.kernel.org" , George Cherian , Yang Yingliang , "bhelgaas@google.com" , "linux-arm-kernel@lists.infradead.org" Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Wed, Sep 09, 2020 at 02:54:01PM +0100, Catalin Marinas wrote: > On Wed, Sep 09, 2020 at 12:36:13PM +0100, Lorenzo Pieralisi wrote: > > On Mon, Sep 07, 2020 at 12:21:19PM +0100, Catalin Marinas wrote: > > > On Mon, Sep 07, 2020 at 10:51:21AM +0000, George Cherian wrote: > > > > Catalin Marinas wrote: > > > > > On Sat, Sep 05, 2020 at 10:48:11AM +0800, Yang Yingliang wrote: > > > > > > diff --git a/arch/arm64/kernel/pci.c b/arch/arm64/kernel/pci.c index > > > > > > 1006ed2d7c604..ddfa1c53def48 100644 > > > > > > --- a/arch/arm64/kernel/pci.c > > > > > > +++ b/arch/arm64/kernel/pci.c > > > > > > @@ -217,4 +217,9 @@ void pcibios_remove_bus(struct pci_bus *bus) > > > > > > acpi_pci_remove_bus(bus); > > > > > > } > > > > > > > > > > > > +void pci_iounmap(struct pci_dev *dev, void __iomem *addr) { > > > > > > + iounmap(addr); > > > > > > +} > > > > > > +EXPORT_SYMBOL(pci_iounmap); > > > > > > > > > > So, what's wrong with the generic pci_iounmap() implementation? > > > > > Shouldn't it call iounmap() already? > > > > > > > > Since ARM64 selects CONFIG_GENERIC_PCI_IOMAP and not > > > > CONFIG_GENERIC_IOMAP, the pci_iounmap function is reduced to a NULL > > > > function. Due to this, even the managed release variants or even the explicit > > > > pci_iounmap calls doesn't really remove the mappings leading to leak. > > > > > > Ah, I missed the fact that pci_iounmap() depends on a different > > > config option. > > > > > > > https://lkml.org/lkml/2020/8/20/28 > > > > > > So is this going to be fixed in the generic code? That would be my > > > preference. > > > > > > A problem with the iounmap() in the proposed patch is that the region > > > may have been an I/O port, so we could end up unmapping the I/O space. > > > > It boils down to finding a way to match a VA to a BAR resource so that > > we can mirror on pci_iounmap() what's done in pci_iomap_range() (ie > > check BAR resource flags to define how/if to unmap them), that would do > > as a generic pci_iounmap() implementation. > > In the !CONFIG_GENERIC_IOMAP case (arm64), for IORESOURCE_IO, > pci_iomap_range() calls __pci_ioport_map() which, with the default > ioport_map(), it ends up with a simple PCI_IOBASE + (port & > IO_SPACE_LIMIT). > > pci_iounmap() could check whether the pointer is in the PCI_IOBASE - > PCI_IOBASE+IO_SPACE_LIMIT range before calling ioremap(), unless the > arch code re-defined ioport_map. Something like below (not even > compiled): For everyone's information, I shall post a fix when I manage to make the kbuild bot happy on all arches. Lorenzo > diff --git a/include/asm-generic/io.h b/include/asm-generic/io.h > index dabf8cb7203b..fada420c9cd6 100644 > --- a/include/asm-generic/io.h > +++ b/include/asm-generic/io.h > @@ -919,6 +919,11 @@ extern void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long max); > #define pci_iounmap pci_iounmap > static inline void pci_iounmap(struct pci_dev *dev, void __iomem *p) > { > +#ifndef ARCH_HAS_IOPORT_MAP > + if (p >= PCI_IOBASE && p < PCI_IOBASE + IO_SPACE_LIMIT) > + return; > + iounmap(p); > +#endif > } > #endif > #endif /* CONFIG_GENERIC_IOMAP */ > @@ -1009,7 +1014,9 @@ static inline void __iomem *ioremap_uc(phys_addr_t offset, size_t size) > > #ifdef CONFIG_HAS_IOPORT_MAP > #ifndef CONFIG_GENERIC_IOMAP > -#ifndef ioport_map > +#ifdef ioport_map > +#define ARCH_HAS_IOPORT_MAP > +#else > #define ioport_map ioport_map > static inline void __iomem *ioport_map(unsigned long port, unsigned int nr) > { > > -- > Catalin _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel