From patchwork Mon Aug 10 02:27:25 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicholas Piggin X-Patchwork-Id: 11706909 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 7579F618 for ; Mon, 10 Aug 2020 02:30:00 +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 4F8122068F for ; Mon, 10 Aug 2020 02:30:00 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="AdgT162U"; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="uzlZkjSm" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 4F8122068F Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.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:MIME-Version:References:In-Reply-To:Message-Id:Date: Subject:To:From:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=SP99ccf+W8kdpVx4TS+leG/wWREXpzM5HtyWDwZdXo4=; b=AdgT162Uj7gKakbtidzkURZCn IHTnTbavgKWdWWj3VqNGzRor4IRROEqbePKIfT44WkSF5hbei6OCesrKYk3/uQoSZ3ddf4RXcSI6W Kqq9ioFcq8L3Z05MjECIUXWMQWVtRl9Po+ShmOv58vsjStibZ5mRn9NxRrM7tK+vhWb+VOzakYXsu C9FktAUbp1ILtlc39fJusz5U7SlPq+BcAgUmWd9MdSAr3X83CCvza0a0Kglwbt2jzUsaxetvTWYZF wba4uCbbg9dtwLVgVCJ5tzjBizbKybcXo9ZM4ZsMbIKDTk0zj1WvoJxMaVTSFQyyQ+5gr+wGCMFmA LhRLftirA==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1k4xXY-0004yX-EY; Mon, 10 Aug 2020 02:27:56 +0000 Received: from mail-pf1-x444.google.com ([2607:f8b0:4864:20::444]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1k4xXV-0004x1-2v for linux-arm-kernel@lists.infradead.org; Mon, 10 Aug 2020 02:27:55 +0000 Received: by mail-pf1-x444.google.com with SMTP id f193so4361711pfa.12 for ; Sun, 09 Aug 2020 19:27:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=+wqRNPVVkxxZAVOe5cjf4OdJxPseOUgKHXYAzqLL+cs=; b=uzlZkjSm9ZGrRLlsU85yYntg5uUa6TXp4vH+ikn0xWiGnZ80ZZ37DbPaomShZJfPnE 0oiZoFL0/mDnn+SocvSfP1WMGgn8TgdaPkindydu0o+r6R7Fc+gxEdQxx7JzfznITR1q mPWsHHhc4c1dQIfvFKE2oND4B0OzSWyERbhsTrQLzubUjimvhYHoaTsE2Hcrl5Fioz3X cjcoEbIvIfuf2WYH6Ug7ZJ+aiS1YTw6ChcwZVpAwjhDuGBDKbaXSxG1MiQCJaeuwTVKI R+BAlqiU5oVlpJVYU/bdKo/Pb9s+j72tB6r8z/RoTb4CRa3k/zRgZZC76K5epCbmm40n hKrw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=+wqRNPVVkxxZAVOe5cjf4OdJxPseOUgKHXYAzqLL+cs=; b=PHewNCkzqVKiPbkQGWoUm52aNtXDWhIZEAp2gf55C1PbpxapirfVE87cNvXfKbRtO2 MTJp6HuSrG/oUYFV7iubxpPSaHn+lmvVkDiov1j56fl/hLPOeGL0G6ClSm+k5V4duQtZ 7PjRZur8oKkdw8hg3Y4NZUhMPKzECWPPvTRkzr2usPlk4QDURnDJOiLud/EIIuyGbMBM gfoPyAHv+IiJrkPJwZxRefkKKv6RKHH23S2l7EaIuJe5EoUqNJDSkepR7C66HdTVLsPb 5iki6oSAVmbYloQ3HCWFsBsjSJMgfVWdPdA5phP84EF9vj9Jf2vu+eCmio8GFCoH8DUS FWVw== X-Gm-Message-State: AOAM530I4wFJLt7OVSLc0VWunSXV/r8W/WSz/stjfmt526BgldjjRfTA bbWcwIKaeB2Hieba+yJElCU= X-Google-Smtp-Source: ABdhPJxJ5wOs61jPiAtdiL9OZRHbgCxw1msWorFzPuXMtNPWII0pHR8QnAVNPZXVV89m8/gFh40qtQ== X-Received: by 2002:aa7:8b18:: with SMTP id f24mr23487412pfd.301.1597026471393; Sun, 09 Aug 2020 19:27:51 -0700 (PDT) Received: from bobo.ibm.com (193-116-100-32.tpgi.com.au. [193.116.100.32]) by smtp.gmail.com with ESMTPSA id l17sm21863475pff.126.2020.08.09.19.27.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 09 Aug 2020 19:27:50 -0700 (PDT) From: Nicholas Piggin To: linux-mm@kvack.org Subject: [PATCH v3 1/8] mm/vmalloc: fix vmalloc_to_page for huge vmap mappings Date: Mon, 10 Aug 2020 12:27:25 +1000 Message-Id: <20200810022732.1150009-2-npiggin@gmail.com> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20200810022732.1150009-1-npiggin@gmail.com> References: <20200810022732.1150009-1-npiggin@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20200809_222754_054718_95A192C4 X-CRM114-Status: GOOD ( 20.41 ) X-Spam-Score: -0.2 (/) X-Spam-Report: SpamAssassin version 3.4.4 on merlin.infradead.org summary: Content analysis details: (-0.2 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2607:f8b0:4864:20:0:0:0:444 listed in] [list.dnswl.org] 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.0 SPF_PASS SPF: sender matches SPF record 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider [npiggin[at]gmail.com] -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 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-arch@vger.kernel.org, Zefan Li , Catalin Marinas , x86@kernel.org, linuxppc-dev@lists.ozlabs.org, Nicholas Piggin , linux-kernel@vger.kernel.org, Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , Thomas Gleixner , Will Deacon , linux-arm-kernel@lists.infradead.org Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org vmalloc_to_page returns NULL for addresses mapped by larger pages[*]. Whether or not a vmap is huge depends on the architecture details, alignments, boot options, etc., which the caller can not be expected to know. Therefore HUGE_VMAP is a regression for vmalloc_to_page. This change teaches vmalloc_to_page about larger pages, and returns the struct page that corresponds to the offset within the large page. This makes the API agnostic to mapping implementation details. [*] As explained by commit 029c54b095995 ("mm/vmalloc.c: huge-vmap: fail gracefully on unexpected huge vmap mappings") Signed-off-by: Nicholas Piggin --- mm/vmalloc.c | 40 ++++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/mm/vmalloc.c b/mm/vmalloc.c index b482d240f9a2..49f225b0f855 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -38,6 +38,7 @@ #include #include +#include #include #include @@ -343,7 +344,9 @@ int is_vmalloc_or_module_addr(const void *x) } /* - * Walk a vmap address to the struct page it maps. + * Walk a vmap address to the struct page it maps. Huge vmap mappings will + * return the tail page that corresponds to the base page address, which + * matches small vmap mappings. */ struct page *vmalloc_to_page(const void *vmalloc_addr) { @@ -363,25 +366,33 @@ struct page *vmalloc_to_page(const void *vmalloc_addr) if (pgd_none(*pgd)) return NULL; + if (WARN_ON_ONCE(pgd_leaf(*pgd))) + return NULL; /* XXX: no allowance for huge pgd */ + if (WARN_ON_ONCE(pgd_bad(*pgd))) + return NULL; + p4d = p4d_offset(pgd, addr); if (p4d_none(*p4d)) return NULL; - pud = pud_offset(p4d, addr); + if (p4d_leaf(*p4d)) + return p4d_page(*p4d) + ((addr & ~P4D_MASK) >> PAGE_SHIFT); + if (WARN_ON_ONCE(p4d_bad(*p4d))) + return NULL; - /* - * Don't dereference bad PUD or PMD (below) entries. This will also - * identify huge mappings, which we may encounter on architectures - * that define CONFIG_HAVE_ARCH_HUGE_VMAP=y. Such regions will be - * identified as vmalloc addresses by is_vmalloc_addr(), but are - * not [unambiguously] associated with a struct page, so there is - * no correct value to return for them. - */ - WARN_ON_ONCE(pud_bad(*pud)); - if (pud_none(*pud) || pud_bad(*pud)) + pud = pud_offset(p4d, addr); + if (pud_none(*pud)) + return NULL; + if (pud_leaf(*pud)) + return pud_page(*pud) + ((addr & ~PUD_MASK) >> PAGE_SHIFT); + if (WARN_ON_ONCE(pud_bad(*pud))) return NULL; + pmd = pmd_offset(pud, addr); - WARN_ON_ONCE(pmd_bad(*pmd)); - if (pmd_none(*pmd) || pmd_bad(*pmd)) + if (pmd_none(*pmd)) + return NULL; + if (pmd_leaf(*pmd)) + return pmd_page(*pmd) + ((addr & ~PMD_MASK) >> PAGE_SHIFT); + if (WARN_ON_ONCE(pmd_bad(*pmd))) return NULL; ptep = pte_offset_map(pmd, addr); @@ -389,6 +400,7 @@ struct page *vmalloc_to_page(const void *vmalloc_addr) if (pte_present(pte)) page = pte_page(pte); pte_unmap(ptep); + return page; } EXPORT_SYMBOL(vmalloc_to_page);