From patchwork Tue Aug 25 14:57:42 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicholas Piggin X-Patchwork-Id: 11735839 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 7FC48722 for ; Tue, 25 Aug 2020 14:58:11 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 4C00820782 for ; Tue, 25 Aug 2020 14:58:11 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="dQccofU4" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 4C00820782 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 691A58E000D; Tue, 25 Aug 2020 10:58:10 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 642958D0001; Tue, 25 Aug 2020 10:58:10 -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 4E2B88E000D; Tue, 25 Aug 2020 10:58:10 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0098.hostedemail.com [216.40.44.98]) by kanga.kvack.org (Postfix) with ESMTP id 328428D0001 for ; Tue, 25 Aug 2020 10:58:10 -0400 (EDT) Received: from smtpin10.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id D75761EE6 for ; Tue, 25 Aug 2020 14:58:09 +0000 (UTC) X-FDA: 77189396298.10.show51_42111ce2705c Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin10.hostedemail.com (Postfix) with ESMTP id 9ADC416A07E for ; Tue, 25 Aug 2020 14:58:09 +0000 (UTC) X-Spam-Summary: 1,0,0,1a10914ff3f5df75,d41d8cd98f00b204,npiggin@gmail.com,,RULES_HIT:41:69:355:379:541:800:960:973:988:989:1260:1311:1314:1345:1359:1431:1437:1515:1535:1543:1711:1730:1747:1777:1792:2393:2553:2559:2562:2693:3138:3139:3140:3141:3142:3308:3355:3865:3867:3868:3871:3872:3874:4117:4250:5007:6119:6261:6653:7514:7903:9036:9413:9592:10004:11026:11232:11473:11658:11914:12043:12219:12291:12295:12296:12297:12438:12517:12519:12555:12683:12895:13161:13229:13894:14181:14394:14687:14721:21080:21433:21444:21451:21627:21666:21990:30003:30054:30062:30070:30074:30090,0,RBL:209.85.210.194:@gmail.com:.lbl8.mailshell.net-62.50.0.100 66.100.201.100;04yfpwim8ajbctmx1suqrw7poynr9opwu1uepyi5f98ekzhbpbjw988g7yuwf7q.16pf537emigd1cnfmenaarp9iquwipafthxmuuio8amwyantu1zntonahicpsyh.1-lbl8.mailshell.net-223.238.255.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:23,LUA_SUMMARY:none X-HE-Tag: show51_42111ce2705c X-Filterd-Recvd-Size: 6463 Received: from mail-pf1-f194.google.com (mail-pf1-f194.google.com [209.85.210.194]) by imf49.hostedemail.com (Postfix) with ESMTP for ; Tue, 25 Aug 2020 14:58:09 +0000 (UTC) Received: by mail-pf1-f194.google.com with SMTP id u20so7565302pfn.0 for ; Tue, 25 Aug 2020 07:58:09 -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=6FG53WANq96sGtoLSKrRzKN8EzzN/tCT8YeKUFryJxY=; b=dQccofU4xh0H65J3wLVfbSsGxfXEbG4tZaPJjU1doX6AedshRyYkztWc2h50Qm3z93 KgscBr6qG9Fo44eG2OeRT2QZf2KmnmSNJ3lJqfRXlwMxr28f4//s+/k+WSkzhJG+zZXq 7e1A8Xe1Vb3oektoAvwsrr1HG/8T9+BhGHxeBxqhkUW/QAL+qYsEJzsjQGDLV40ToZlb cFFecAKq8Dmuuzx1yFyxfPa/KmQyWvfZIEsC7eIW5afSBgJPUE4uZ6p89M7FoGQqUVAQ TCo3qp5Y+8G9k+LVGo8fN1/xRq//BUSFIL+BRNmAN8TwMnHLloOkZCcwYUYknVes0/b3 AtTA== 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=6FG53WANq96sGtoLSKrRzKN8EzzN/tCT8YeKUFryJxY=; b=ro5iIyNaEjDsXiTwCy/MB9wxWWqBUTu9LZcNi0ApdRK8K+IacoAadiIIt9UoIhohCw 6MBaVHvaz513KlPyCHbxQlxnRLdcDdL+TdO3DVJ8BtTfGU6vE34dssIAdfos3inAa/r5 otmCnCHnBnPc83ZGfX8E4b47UtjEPK/611f++UIfWN9suuF+n3Yh4GH3cQ8s9CvoaWw6 xPao3toC9BR1+yN6K3VsP0u+Ic2Y7CIkUf/MsWXM+jCn9BMQAZErxEcU9sOcman55OS/ uYlrQT1/53vhwiWlH3e1OlMrbFbvqntd7sv5qpSKz1IRQsxg/UnywjT5m5S3kciqL6ID VSww== X-Gm-Message-State: AOAM530io/QmnYoPuMON7/563IIYnqJAFLoTBuf6J4Az0D5h2iLromdR E5Z2O10ufIRsOkvBgtQarcErXEd8k1M= X-Google-Smtp-Source: ABdhPJxbIN9TEoKvgUhetFCwKB5gJVl/+6wG+sb6ODHZ7G+F/ibIU6KSibeJKKELCF6GtNzzgFkFGA== X-Received: by 2002:a05:6a00:1515:: with SMTP id q21mr8428082pfu.126.1598367488211; Tue, 25 Aug 2020 07:58:08 -0700 (PDT) Received: from bobo.ozlabs.ibm.com (61-68-212-105.tpgi.com.au. [61.68.212.105]) by smtp.gmail.com with ESMTPSA id e29sm15755956pfj.92.2020.08.25.07.58.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Aug 2020 07:58:07 -0700 (PDT) From: Nicholas Piggin To: linux-mm@kvack.org, Andrew Morton Cc: Nicholas Piggin , linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, Zefan Li , Jonathan Cameron , Christoph Hellwig , Christophe Leroy Subject: [PATCH v7 01/12] mm/vmalloc: fix vmalloc_to_page for huge vmap mappings Date: Wed, 26 Aug 2020 00:57:42 +1000 Message-Id: <20200825145753.529284-2-npiggin@gmail.com> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20200825145753.529284-1-npiggin@gmail.com> References: <20200825145753.529284-1-npiggin@gmail.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 9ADC416A07E X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam05 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: 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 | 41 ++++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/mm/vmalloc.c b/mm/vmalloc.c index b482d240f9a2..4e9b21adc73d 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -36,7 +36,7 @@ #include #include #include - +#include #include #include #include @@ -343,7 +343,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 +365,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 +399,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);