Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp447773pxk; Wed, 9 Sep 2020 09:25:42 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy9IHcClLXlc7icJDM9EVWqMN7uitziHpGc8IkaGvkZWcaiPzQkGIcpsTTjzBYg5hG9bct1 X-Received: by 2002:a17:906:5246:: with SMTP id y6mr4437831ejm.316.1599668742091; Wed, 09 Sep 2020 09:25:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1599668742; cv=none; d=google.com; s=arc-20160816; b=Hq6qdOM0Kblt/VD1pcSlO2Qlke3kf5SemPaQ6MET8Qrg65NdWqfvCUIaKapPQcEGMV lVeh2pUPuAhGAXxXEbJFDF6mvjEilR7YeScbrzkqmS1Mjfn5Q/TRsnl3LR0GOOgQ/894 2FiEjOUL7FldSxTZeDQiqQnDp+9EATrRXsgmacnjUOr4PyWaVkKuh9wquaIBa6p0WIQq mTWfljGLcLiqdvzadje6YUlteqDAhyhEzkuNEcjUvQ7e4ThCZ2UTzpjQAp0uM6IrkmO6 07MoWmdiqD2xDvb6izljwwp3av9vVllVbk+avqG68SyfPzou7HBZ73c7xbQAvVxTuJy8 BosQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=ZYdjLyBWMwXeAOojw4igCy8sg6HCGkWFCR79osmiyEg=; b=F8CEKmPAoPOWbAqzx6UGKY3+9/BSE1csnRkybNIZmOh4uH1P0swBB5zrtln6bngeF/ x7Jt0eK47qDr9i5lVtDECJRz/MgRGuu5Oio6aEHKRdc4jdA52f7s7C1WlNJmAHVZhqd0 i7nGsZVqx6fay0iEBHtTIeS6e8LkBjD4H7c6ibqKK5v4RkpisSxMOkFKmdLLxRyLBomo doBDVLUY3LGhaQojm7iBbHhyZKbWELNaQ7rf94Z9We4dGn8cIMNjpOEUHcrHKT4WjA4L 5PtKqATTNTEJaVnOQZX7gyEPW8NcRO2qwlnDb9hzqzdk/9f3eGCs4HWtBgf0NxaxWXKQ 5RfQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ot12si1817562ejb.692.2020.09.09.09.25.18; Wed, 09 Sep 2020 09:25:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730616AbgIIQYP (ORCPT + 99 others); Wed, 9 Sep 2020 12:24:15 -0400 Received: from mail.kernel.org ([198.145.29.99]:45292 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731057AbgIIQX4 (ORCPT ); Wed, 9 Sep 2020 12:23:56 -0400 Received: from gaia (unknown [46.69.195.48]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id AE68B21D40; Wed, 9 Sep 2020 13:54:03 +0000 (UTC) Date: Wed, 9 Sep 2020 14:54:01 +0100 From: Catalin Marinas To: Lorenzo Pieralisi Cc: George Cherian , Yang Yingliang , "linux-kernel@vger.kernel.org" , "linux-pci@vger.kernel.org" , "linux-arm-kernel@lists.infradead.org" , "will.deacon@arm.com" , "bhelgaas@google.com" , "guohanjun@huawei.com" Subject: Re: [PATCH] arm64: PCI: fix memleak when calling pci_iomap/unmap() Message-ID: <20200909135400.GB13047@gaia> References: <20200907104546.GC26513@gaia> <20200907112118.GD26513@gaia> <20200909113613.GB6384@e121166-lin.cambridge.arm.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200909113613.GB6384@e121166-lin.cambridge.arm.com> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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): 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