From patchwork Tue Oct 2 11:06:02 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Will Deacon X-Patchwork-Id: 10623359 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id C56DD13BB for ; Tue, 2 Oct 2018 11:05:52 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AC9982881A for ; Tue, 2 Oct 2018 11:05:52 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A10D22882E; Tue, 2 Oct 2018 11:05:52 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 18FCD2881A for ; Tue, 2 Oct 2018 11:05:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E5D316B026A; Tue, 2 Oct 2018 07:05:42 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id BE7616B0271; Tue, 2 Oct 2018 07:05:42 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 901196B026C; Tue, 2 Oct 2018 07:05:42 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-ot1-f69.google.com (mail-ot1-f69.google.com [209.85.210.69]) by kanga.kvack.org (Postfix) with ESMTP id 575226B026F for ; Tue, 2 Oct 2018 07:05:42 -0400 (EDT) Received: by mail-ot1-f69.google.com with SMTP id p23-v6so1037831otl.23 for ; Tue, 02 Oct 2018 04:05:42 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references; bh=gyaouhO6M09bNRRp3jEmM8H6PKnPVZFp5tgRSOGTTCE=; b=qJOO9y9f8/WybPI+kZvoqN/HxlqEnHfGVjAYokvcFv5JoW0he6OPx9mCdFnAkhN66f xiAx6luYds3njHFVi4hv6LubPq/Cvjb9QFiecVqa0OreegHpkW7roDFG6SPul7M5x55U 0MtYxJD+/0BtCRo+ammpV++PorM8YZCOQKXKZOXRmaLz7pJalhGG8n89Rlt55IpzS5cB AehN8k4PpCHLgFm3JUSq479T+E04T9WeXoIn3oSJRkYmABjukP8cpNivGJMhp0beXTHp V2gvoGTejJqixeGuMPJ0vIi9ouejrNSBQihDFwMg7c1WyT/tV10INm906EaWMAVFiB5N duSA== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of will.deacon@arm.com designates 217.140.101.70 as permitted sender) smtp.mailfrom=will.deacon@arm.com X-Gm-Message-State: ABuFfojOv/1qt5vnDwSc+QQmLDO1Ft/hsedHUvhubH9Yaup5MmrYDBnK RlDV0F962sutQ507B1z2YxvvWUbQas50d2RSo4lQciX1jbBQ7pPCa/oM3Om/OxaCUZk0tNVPAbZ 6zMtGuodnJ3kbwF0Q7ZxT1TyToStXWFNlu5ggQhwq9UnOWH26GeMHE+NYBanlh5VcCQ== X-Received: by 2002:a9d:2a44:: with SMTP id t62mr3651323ota.321.1538478342084; Tue, 02 Oct 2018 04:05:42 -0700 (PDT) X-Google-Smtp-Source: ACcGV60q2NMyR1BIS0ZoY93vlvdhkNShwdf7HlRC2l8rwNI/SH4hSAha7129HJ5WpBdYsaQzcrxT X-Received: by 2002:a9d:2a44:: with SMTP id t62mr3651293ota.321.1538478341241; Tue, 02 Oct 2018 04:05:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1538478341; cv=none; d=google.com; s=arc-20160816; b=zUzddZtS3+NqSHBv1PuL5UL294Gl2/eRLx4CSTfwhvVUMzy83hVKzMC9chbzp3i8Rz eXZsVDiWZHqIiyXwo22fjvDE1JVvN7AyZ/icGFkwrTvp+a0XLh11CfpT7JoixRMJVuee GQMNYjOAW62T7vbfG22QOHfq0iupy63uo+0J65OzXJ7kcsUpmn2wq+p9S7zTT2WZrSj6 GcTfFWCjZKhgT+GBBGt7/rvxCKlPJejM1ssKwaGMWP1WYSgRYznpSIKkq2lFCV/NUwJt BB4CmpAbf0i97hHOcKf5kFSwwKXmhhqvRpvzA4zBz1mv1G1RBAhJIGVYc3y4V8DvRreu ZTJg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from; bh=gyaouhO6M09bNRRp3jEmM8H6PKnPVZFp5tgRSOGTTCE=; b=FwJPSij6p+TnlPRwKvnKheIe4/e//HAfWIKQuBNiDD34zvX7PMfjlrIiU+WjcUKUNR aH57jhJULzDvs573N7q1AgTzC0DFMuV5usYWSUPab2pzeSY+w8ozVkgAa93PPmdXhawA dkBdKPIcBLx4MznLK9lwCnhYletRFnpEW3IBK0mMPfd80XYLACDPpqXc0yEV3t74NU3x hayGpo/qPmbEr7Ax+4aK1mYGVEiDQNdbPRYOIPlAzvdtEKI9Tjy+c7/V9NwG95cRw0kw SgsZ8jzHIGXpNfm/6foiBoDcdJ4KenJfQfa6g5cbgACnfNBoGD9AWaltJdCywm1fgzqU 6mpA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of will.deacon@arm.com designates 217.140.101.70 as permitted sender) smtp.mailfrom=will.deacon@arm.com Received: from foss.arm.com (usa-sjc-mx-foss1.foss.arm.com. [217.140.101.70]) by mx.google.com with ESMTP id v50-v6si8112938otb.289.2018.10.02.04.05.40 for ; Tue, 02 Oct 2018 04:05:41 -0700 (PDT) Received-SPF: pass (google.com: domain of will.deacon@arm.com designates 217.140.101.70 as permitted sender) client-ip=217.140.101.70; Authentication-Results: mx.google.com; spf=pass (google.com: domain of will.deacon@arm.com designates 217.140.101.70 as permitted sender) smtp.mailfrom=will.deacon@arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id C2DCA7A9; Tue, 2 Oct 2018 04:05:40 -0700 (PDT) Received: from edgewater-inn.cambridge.arm.com (usa-sjc-imap-foss1.foss.arm.com [10.72.51.249]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 93D573F774; Tue, 2 Oct 2018 04:05:40 -0700 (PDT) Received: by edgewater-inn.cambridge.arm.com (Postfix, from userid 1000) id 7E2771AE402D; Tue, 2 Oct 2018 12:06:04 +0100 (BST) From: Will Deacon To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: cpandya@codeaurora.org, toshi.kani@hpe.com, tglx@linutronix.de, mhocko@suse.com, akpm@linux-foundation.org, sean.j.christopherson@intel.com, Will Deacon Subject: [PATCH v2 4/5] lib/ioremap: Ensure phys_addr actually corresponds to a physical address Date: Tue, 2 Oct 2018 12:06:02 +0100 Message-Id: <1538478363-16255-5-git-send-email-will.deacon@arm.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1538478363-16255-1-git-send-email-will.deacon@arm.com> References: <1538478363-16255-1-git-send-email-will.deacon@arm.com> X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP The current ioremap() code uses a phys_addr variable at each level of page table, which is confusingly offset by subtracting the base virtual address being mapped so that adding the current virtual address back on when iterating through the page table entries gives back the corresponding physical address. This is fairly confusing and results in all users of phys_addr having to add the current virtual address back on. Instead, this patch just updates phys_addr when iterating over the page table entries, ensuring that it's always up-to-date and doesn't require explicit offsetting. Cc: Chintan Pandya Cc: Toshi Kani Cc: Thomas Gleixner Cc: Michal Hocko Cc: Andrew Morton Cc: Sean Christopherson Signed-off-by: Will Deacon --- lib/ioremap.c | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/lib/ioremap.c b/lib/ioremap.c index 6c72764af19c..10d7c5485c39 100644 --- a/lib/ioremap.c +++ b/lib/ioremap.c @@ -101,19 +101,18 @@ static inline int ioremap_pmd_range(pud_t *pud, unsigned long addr, pmd_t *pmd; unsigned long next; - phys_addr -= addr; pmd = pmd_alloc(&init_mm, pud, addr); if (!pmd) return -ENOMEM; do { next = pmd_addr_end(addr, end); - if (ioremap_try_huge_pmd(pmd, addr, next, phys_addr + addr, prot)) + if (ioremap_try_huge_pmd(pmd, addr, next, phys_addr, prot)) continue; - if (ioremap_pte_range(pmd, addr, next, phys_addr + addr, prot)) + if (ioremap_pte_range(pmd, addr, next, phys_addr, prot)) return -ENOMEM; - } while (pmd++, addr = next, addr != end); + } while (pmd++, phys_addr += (next - addr), addr = next, addr != end); return 0; } @@ -142,19 +141,18 @@ static inline int ioremap_pud_range(p4d_t *p4d, unsigned long addr, pud_t *pud; unsigned long next; - phys_addr -= addr; pud = pud_alloc(&init_mm, p4d, addr); if (!pud) return -ENOMEM; do { next = pud_addr_end(addr, end); - if (ioremap_try_huge_pud(pud, addr, next, phys_addr + addr, prot)) + if (ioremap_try_huge_pud(pud, addr, next, phys_addr, prot)) continue; - if (ioremap_pmd_range(pud, addr, next, phys_addr + addr, prot)) + if (ioremap_pmd_range(pud, addr, next, phys_addr, prot)) return -ENOMEM; - } while (pud++, addr = next, addr != end); + } while (pud++, phys_addr += (next - addr), addr = next, addr != end); return 0; } @@ -164,7 +162,6 @@ static inline int ioremap_p4d_range(pgd_t *pgd, unsigned long addr, p4d_t *p4d; unsigned long next; - phys_addr -= addr; p4d = p4d_alloc(&init_mm, pgd, addr); if (!p4d) return -ENOMEM; @@ -173,14 +170,14 @@ static inline int ioremap_p4d_range(pgd_t *pgd, unsigned long addr, if (ioremap_p4d_enabled() && ((next - addr) == P4D_SIZE) && - IS_ALIGNED(phys_addr + addr, P4D_SIZE)) { - if (p4d_set_huge(p4d, phys_addr + addr, prot)) + IS_ALIGNED(phys_addr, P4D_SIZE)) { + if (p4d_set_huge(p4d, phys_addr, prot)) continue; } - if (ioremap_pud_range(p4d, addr, next, phys_addr + addr, prot)) + if (ioremap_pud_range(p4d, addr, next, phys_addr, prot)) return -ENOMEM; - } while (p4d++, addr = next, addr != end); + } while (p4d++, phys_addr += (next - addr), addr = next, addr != end); return 0; } @@ -196,14 +193,13 @@ int ioremap_page_range(unsigned long addr, BUG_ON(addr >= end); start = addr; - phys_addr -= addr; pgd = pgd_offset_k(addr); do { next = pgd_addr_end(addr, end); - err = ioremap_p4d_range(pgd, addr, next, phys_addr+addr, prot); + err = ioremap_p4d_range(pgd, addr, next, phys_addr, prot); if (err) break; - } while (pgd++, addr = next, addr != end); + } while (pgd++, phys_addr += (next - addr), addr = next, addr != end); flush_cache_vmap(start, end);