From patchwork Fri Aug 21 04:44:20 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicholas Piggin X-Patchwork-Id: 11728109 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 7AAFD109B for ; Fri, 21 Aug 2020 04:44:45 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 47A76208E4 for ; Fri, 21 Aug 2020 04:44:45 +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="JswMtJ4G" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 47A76208E4 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 517AF8D000F; Fri, 21 Aug 2020 00:44:44 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 4C70B8D0005; Fri, 21 Aug 2020 00:44:44 -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 3B5E98D000F; Fri, 21 Aug 2020 00:44:44 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0065.hostedemail.com [216.40.44.65]) by kanga.kvack.org (Postfix) with ESMTP id 227E78D0005 for ; Fri, 21 Aug 2020 00:44:44 -0400 (EDT) Received: from smtpin28.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id D08E63633 for ; Fri, 21 Aug 2020 04:44:43 +0000 (UTC) X-FDA: 77173335246.28.elbow86_520099327036 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin28.hostedemail.com (Postfix) with ESMTP id A8D266D71 for ; Fri, 21 Aug 2020 04:44:43 +0000 (UTC) X-Spam-Summary: 1,0,0,7ebeba319f860fa7,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:4321:5007:6119:6261:6653:7514:7903:9036:9413:10004:11026:11232:11473:11658:11914:12043:12219:12291:12295:12296:12297:12438:12517:12519:12555:12683:12895:12986:13161:13229:13894:14181:14394:14687:14721:21080:21433:21444:21451:21627:21666:21990:30003:30054:30062:30070:30074:30090,0,RBL:209.85.215.194:@gmail.com:.lbl8.mailshell.net-66.100.201.100 62.18.0.100;04yrh853g138nmasf1rir61oiyqoiopwu1uepyi5f98ekzhbpbjw988g7yuwf7q.16pf537emigd1cnrgfxw4da7nsuwipafthxmuuio8amwygrfniwwyrnahicpsyh.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: elbow86_520099327036 X-Filterd-Recvd-Size: 6287 Received: from mail-pg1-f194.google.com (mail-pg1-f194.google.com [209.85.215.194]) by imf50.hostedemail.com (Postfix) with ESMTP for ; Fri, 21 Aug 2020 04:44:43 +0000 (UTC) Received: by mail-pg1-f194.google.com with SMTP id m34so413363pgl.11 for ; Thu, 20 Aug 2020 21:44:43 -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=JswMtJ4GRdG0XC7MN25+zcAR+FDhPMWqW0gS8oiXOKwZi451vgTtEaLszHOpU0HB4J 9H9u538gKqdzyBxKTJmAWqN56+57QcsEN5HPK6SZCW1ti6UBNMjVN/iA4y9FHpljJT+u wy1/OngEQwe/K6gcPyALLZYd8Pc6Jx4XOcGr79jiTCpIjIKsPQbCvWqEB+1Lhxj0lP3d Nqeoeik+DX2xY7QRyAS+whQzQTubpZrt+U5lvQHBCqNMtNjIG4Wx9uG1i4B0keKVkoLr sclYucLHw3o+bXw826g+zsIfsgQDnhblCrJQpV4HiqD2SqSebtf0UwG34fg8oRLgfCw6 rcJQ== 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=DMqsWCMrMkrxWBbkJh6wFmXFgBKUWcSB5TJi/hzRYEwUSA3QoGLf4RyGOB4iOa9D0M OIYynOgKcEc96/RTpHD8ctstG9BqSr20XCV8UO1Z0gDFlW7Lf6EIh/JU8yFq9/BE6p6b SUIQPoikBZLo4tZOWT+cNXHmbEtQU6yjiwfPtSfjkPC6JJulgq3OCNDsZQ4LJDKL7+CQ gp+aePgtJtO41zanZ0r7op3uPcqS4YY2tITeZopAsf5b0EwUPJTDo6PMvkURZPpEFq+1 nYJsLpBoRoRUFZVffpVfxAuSATAinlQukoGVVD+Pd1JuCUKEvcKh94L2wa5Rkw/xdE3U WD9Q== X-Gm-Message-State: AOAM533iqx9HV8eL31xye7Um5c9lR7WS14zefA8HOkJ3m9PC+3oRrG6+ MWPUtLJc57mHf74E0mJbBT0K8qajAVA= X-Google-Smtp-Source: ABdhPJyMB3FHce5MTh/dSOT0zrOsoCyzxRf35b8HaRFN1ex/qAd5m4QLLGdfd8b6PLmPj7hs7h1Xvw== X-Received: by 2002:a62:8f4b:: with SMTP id n72mr1007665pfd.5.1597985082100; Thu, 20 Aug 2020 21:44:42 -0700 (PDT) Received: from bobo.ibm.com (61-68-212-105.tpgi.com.au. [61.68.212.105]) by smtp.gmail.com with ESMTPSA id l9sm683374pgg.29.2020.08.20.21.44.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Aug 2020 21:44:41 -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 Subject: [PATCH v5 1/8] mm/vmalloc: fix vmalloc_to_page for huge vmap mappings Date: Fri, 21 Aug 2020 14:44:20 +1000 Message-Id: <20200821044427.736424-2-npiggin@gmail.com> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20200821044427.736424-1-npiggin@gmail.com> References: <20200821044427.736424-1-npiggin@gmail.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: A8D266D71 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 | 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);