From patchwork Wed Oct 28 01:39:45 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dan Williams X-Patchwork-Id: 7506151 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id D9DDD9F71A for ; Wed, 28 Oct 2015 01:47:59 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 020E72091E for ; Wed, 28 Oct 2015 01:47:59 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 01A1920600 for ; Wed, 28 Oct 2015 01:47:58 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1ZrFoZ-0000Nc-Us; Wed, 28 Oct 2015 01:46:11 +0000 Received: from mga02.intel.com ([134.134.136.20]) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1ZrFoW-0008V0-Qz for linux-arm-kernel@lists.infradead.org; Wed, 28 Oct 2015 01:46:09 +0000 Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by orsmga101.jf.intel.com with ESMTP; 27 Oct 2015 18:45:27 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.20,207,1444719600"; d="scan'208";a="837090052" Received: from dwillia2-desk3.jf.intel.com (HELO dwillia2-desk3.amr.corp.intel.com) ([10.54.39.39]) by fmsmga002.fm.intel.com with ESMTP; 27 Oct 2015 18:45:27 -0700 Subject: [for 4.4 PATCH] memremap: fix highmem support From: Dan Williams To: linux-arm-kernel@lists.infradead.org Date: Tue, 27 Oct 2015 21:39:45 -0400 Message-ID: <20151028013557.31764.66161.stgit@dwillia2-desk3.amr.corp.intel.com> User-Agent: StGit/0.17.1-9-g687f MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20151027_184608_942260_48AC49B9 X-CRM114-Status: GOOD ( 17.47 ) X-Spam-Score: -6.9 (------) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: rmk+kernel@arm.linux.org.uk, "Rafael J. Wysocki" , linux-kernel@vger.kernel.org, Ard Biesheuvel Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Currently memremap checks if the range is "System RAM" and returns the kernel linear address. This is broken for highmem platforms where a range may be "System RAM", but is not part of the kernel linear mapping. Similar to acpi_map(), use kmap() for PAGE_SIZE memremap() requests for highmem, and fall back to ioremap_cache() otherwise. The impact of this bug is low for now since the pmem driver is the only user of memremap(), but this is important to fix before more conversions to memremap arrive in 4.4. Cc: Rafael J. Wysocki Cc: Russell King Reported-by: Ard Biesheuvel Signed-off-by: Dan Williams --- Russell, I question whether the kmap fallback is needed. This is borrowed from the current implementation of acpi_map(), and I added it since arm ioremap warns if passed pfn_valid() addresses. include/linux/highmem.h | 12 ++++++++++++ kernel/memremap.c | 28 +++++++++++++++++++++++++--- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/include/linux/highmem.h b/include/linux/highmem.h index 6aefcd0031a6..c20cf24c76dd 100644 --- a/include/linux/highmem.h +++ b/include/linux/highmem.h @@ -41,6 +41,13 @@ void kmap_flush_unused(void); struct page *kmap_to_page(void *addr); +static inline bool is_kmap_addr(const void *x) +{ + unsigned long addr = (unsigned long) x; + + return addr >= PKMAP_ADDR(0) && addr < PKMAP_ADDR(LAST_PKMAP); +} + #else /* CONFIG_HIGHMEM */ static inline unsigned int nr_free_highpages(void) { return 0; } @@ -50,6 +57,11 @@ static inline struct page *kmap_to_page(void *addr) return virt_to_page(addr); } +static inline bool is_kmap_addr(const void *x) +{ + return false; +} + #define totalhigh_pages 0UL #ifndef ARCH_HAS_KMAP diff --git a/kernel/memremap.c b/kernel/memremap.c index 72b0c66628b6..901d7ec3583a 100644 --- a/kernel/memremap.c +++ b/kernel/memremap.c @@ -10,6 +10,7 @@ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. */ +#include #include #include #include @@ -24,6 +25,25 @@ __weak void __iomem *ioremap_cache(resource_size_t offset, unsigned long size) } #endif +static void *try_ram_remap(resource_size_t offset, size_t size) +{ + struct page *page = pfn_to_page(offset >> PAGE_SHIFT); + unsigned int pg_off = offset & ~PAGE_MASK; + + /* In the simple case just return the existing linear address */ + if (!PageHighMem(page)) + return __va(offset); + + /* + * Try kmap first since some arch ioremap implementations fail when + * being passed a ram address. + */ + if (pg_off + size <= PAGE_SIZE) + return kmap(page) + pg_off; + + return NULL; +} + /** * memremap() - remap an iomem_resource as cacheable memory * @offset: iomem resource start address @@ -66,8 +86,8 @@ void *memremap(resource_size_t offset, size_t size, unsigned long flags) * the requested range is potentially in "System RAM" */ if (is_ram == REGION_INTERSECTS) - addr = __va(offset); - else + addr = try_ram_remap(offset, size); + if (!addr) addr = ioremap_cache(offset, size); } @@ -94,7 +114,9 @@ EXPORT_SYMBOL(memremap); void memunmap(void *addr) { - if (is_vmalloc_addr(addr)) + if (is_kmap_addr(addr)) + kunmap(addr); + else if (is_vmalloc_addr(addr)) iounmap((void __iomem *) addr); } EXPORT_SYMBOL(memunmap);