From patchwork Wed Mar 17 06:23:50 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicholas Piggin X-Patchwork-Id: 12144881 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.5 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 29A9EC433DB for ; Wed, 17 Mar 2021 06:24:42 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id BE93564F8C for ; Wed, 17 Mar 2021 06:24:41 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org BE93564F8C 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 585226B0074; Wed, 17 Mar 2021 02:24:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 536F58D0001; Wed, 17 Mar 2021 02:24:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3AEB76B0078; Wed, 17 Mar 2021 02:24:41 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0077.hostedemail.com [216.40.44.77]) by kanga.kvack.org (Postfix) with ESMTP id 1FA026B0074 for ; Wed, 17 Mar 2021 02:24:41 -0400 (EDT) Received: from smtpin26.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id D3A55180AD82F for ; Wed, 17 Mar 2021 06:24:40 +0000 (UTC) X-FDA: 77928377520.26.44A95D7 Received: from mail-pf1-f178.google.com (mail-pf1-f178.google.com [209.85.210.178]) by imf17.hostedemail.com (Postfix) with ESMTP id 5ADF0407F8C4 for ; Wed, 17 Mar 2021 06:24:40 +0000 (UTC) Received: by mail-pf1-f178.google.com with SMTP id h3so405707pfr.12 for ; Tue, 16 Mar 2021 23:24:40 -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=lKopX2yPijMVEzlZ+by8oeAN6QHVUP6629vS5TVZX0w=; b=ljBGC2U1NROLE07LGNWEtUl7p0/i4HyY6IUXQn2OjTwDQSErggxTgWPRanpnwkR4Kf 84x0GwHuWOFIX4QhYsgbVHSqH6wW7Vns7iWtHasyamaZrIKi4fuoGNIx5HQEZtX5/6bC BIq7+o0sQRSMV0BLXUGa+iRqUR6wiU1AkkwnvE5rpGoTZFAeAB1AlbCsk4EUpePOOZGX VQfjPnGR9Gz+xYivrdFmvF+RRV5Jm4nBJResii1pYeB7pM3dEjmztPYd4V2ly8hW0jgH mvQBhxSnXyWSueeld7+YWhFzrr3OOAMZicG+2dcseG+FGIfOIe17g4NLjPf/40/8r8A0 fFmg== 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=lKopX2yPijMVEzlZ+by8oeAN6QHVUP6629vS5TVZX0w=; b=lGfONqxQ89wWvkEqlTOzBzy0cgeOe2efuffZ4t6LSFM5Bib8UAAuBjMtF+a/Korcdu mLJXIMJBJeE+O7TUvekwGKM4AsFd2lSqyCzetI+tNe2kvdkmHrHqoLfOTMROUlIAEc9l omXWFeto4ma4/6LnOv6arfkq+IoT1BxmoKchO08c2gEIPiakR6HHnt+6lhzO6iiz2bIF cwkKm5MhsNDD4/4C6j9DMv/cxzJVDjwj31N4mcpTBKibaen+AA4OnxHErY9RQSx+oQWB AYwy6aGTLYTGGyo4iybyYHfLy/VUMBO4Nlq2eP17gg/sRHs9Rvauf/gCIyeAIAaWiyl1 3rwQ== X-Gm-Message-State: AOAM532KfpBZGa6xtrWRMbl96OE8bImD2K3aUVrtJVK9E2ysp+Sp17Nn lRlhGRfBQMINg95SKMBBYB5kCyTrLtY= X-Google-Smtp-Source: ABdhPJz+3AmYll44tulolkLRLJTiuXsjmwqw8nQ4Q+GAOFyMSJVu9YQ6bjHhZwfR5lV89hqngDl0DQ== X-Received: by 2002:a63:1906:: with SMTP id z6mr1366452pgl.292.1615962278920; Tue, 16 Mar 2021 23:24:38 -0700 (PDT) Received: from bobo.ozlabs.ibm.com (58-6-239-121.tpgi.com.au. [58.6.239.121]) by smtp.gmail.com with ESMTPSA id s19sm17959620pfh.168.2021.03.16.23.24.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Mar 2021 23:24:38 -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, Jonathan Cameron , Christoph Hellwig , Christophe Leroy , Rick Edgecombe , Ding Tianhong , Miaohe Lin , Christoph Hellwig Subject: [PATCH v13 02/14] mm/vmalloc: fix HUGE_VMAP regression by enabling huge pages in vmalloc_to_page Date: Wed, 17 Mar 2021 16:23:50 +1000 Message-Id: <20210317062402.533919-3-npiggin@gmail.com> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20210317062402.533919-1-npiggin@gmail.com> References: <20210317062402.533919-1-npiggin@gmail.com> MIME-Version: 1.0 X-Stat-Signature: xpb4hirghoott74h3nwmo33tq7hf7pcx X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 5ADF0407F8C4 Received-SPF: none (gmail.com>: No applicable sender policy available) receiver=imf17; identity=mailfrom; envelope-from=""; helo=mail-pf1-f178.google.com; client-ip=209.85.210.178 X-HE-DKIM-Result: pass/pass X-HE-Tag: 1615962280-242870 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") Reviewed-by: Miaohe Lin Reviewed-by: Christoph Hellwig 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 4f5f8c907897..98e697ac764c 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -34,7 +34,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);