Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp1605363pxa; Sun, 16 Aug 2020 04:43:39 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzvvCBuXen8AlugIZVJ00GwyRIg//KCBtbGmE/lVL3+5ZjUS/eU+h+JRL5DPhAl69vjI9hU X-Received: by 2002:a17:906:5a93:: with SMTP id l19mr10296287ejq.418.1597578219435; Sun, 16 Aug 2020 04:43:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597578219; cv=none; d=google.com; s=arc-20160816; b=Bq5KxLX23iA5GETmqi9sJDOml1GpxI2qDzX4elM93pLgCs+BfP4NraLGelENlEtkVI MmSRkv0oVn4fU3ovQYg6o5HHSgg7SCxla4y7jeYxb/sVjzdMnRl+NY73Y/U4FnKYihpL W3gKXoRjujJyQL8Cps011qVBuy2Dkvv8ny9DPYUnIDle8BJo9MXyrPhprOiMw93Mr/Fz RJvjbdxj0uIlO8Mo9IhUmtdpnybyPd/jqRNBA+VRq9KkxxQ1E9IMywN+bVZ/rc29JF3o dUrli9bq3KWzBuyv91E4aOh6aOVV04Of1wEx2wOqomyxUGxPgFncjkPLIBnjwY28+YKv 71wg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=+wqRNPVVkxxZAVOe5cjf4OdJxPseOUgKHXYAzqLL+cs=; b=eunNPaANSN8hGPeZG5M/9qhr9p+kyMG92ZiAgnf5J6ZI78cH4FnTG8x+EuGM0c4JQX JnqbWVA/X0+EsryvuoEy/u0lGQ5sFbWIXDF5rgtC6up1YyRUPbbmAzM8d4T1FPn821I3 9jpzB3XAte2ig2raH2xmIrnWmi8atYujcsoEsnBEvxknHjkWq3JXtOnfIYGRixQRRBbX ti56oyp2qjaS+kiouUTx7DSUc+jr/XwfdQTyxJcEXgD0jkbFH0t8n+PrN7BSIU6iPy6j UUsThGf/ORMS39Fk81xTsuk1nNgLKQ/anzkXgJIXUoKlY+hRBnZKRTzXkW97rfzi/uR0 baAQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=EA7yKwrJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id m27si8988108edj.262.2020.08.16.04.43.17; Sun, 16 Aug 2020 04:43:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=EA7yKwrJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728700AbgHPJJ2 (ORCPT + 99 others); Sun, 16 Aug 2020 05:09:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36610 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728488AbgHPJJX (ORCPT ); Sun, 16 Aug 2020 05:09:23 -0400 Received: from mail-pf1-x442.google.com (mail-pf1-x442.google.com [IPv6:2607:f8b0:4864:20::442]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7143BC061756; Sun, 16 Aug 2020 02:09:23 -0700 (PDT) Received: by mail-pf1-x442.google.com with SMTP id 17so6670466pfw.9; Sun, 16 Aug 2020 02:09:23 -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=EA7yKwrJ6XMClXzMrMaldEGD6T0ONt/b7yucodcpi2bwP1vk+eiWi4GJ0TmCJsOLNB p7Wlwa96VnXwdnOrBaddEuriy3Eb0sUQMuHy7YjQbUmgTiqkk5LCJYeZIY0v0vOYGTY1 3cPDR77QB9eua4GSDy4zUL730tY9ZDbMg+SXtYUKtQUZ/+DeffhpRACd2dta2Z9+KrZI k/ygx8c5qU1iWmEliqszYl8TQ4EmYUlW7RGM+AXjaj6FHIliHnQSkqB7tFI1LLnZFbAr znQlNJBbdlX0bJq6EixSSIs1uju+X6XZDrd3oZDKOLOo7FkuglAmK3MDwSptKgUZ5ceI Qieg== 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=t6r0AjcFMz9y21pNWUCHzLg+xmT7QHO5zFZEPUeZCgkK7wkVYaOKnT0M/YFTB0hemv Dch0h5LTlsqOxuZwOyN4psDkATQkUWdtH33V5fE0/nqny2uwXCuIfx/mKt6n3brDfyCq JMynUSSkIKs0u5TAJrgW31HCCEmsSLcAO93toF1z7wC4tOpisG/8fKDGSvuLnyo5RokU H0w0Nvvfc/6gO7rTpG1hlh3vQ+XwvDEWLQkiFeLxjszx7hjYJR6sILwV1f7sbwu9eAIN t6zRbLCmUWGlug2Yp2XEuR4GmPQusSmiJhpLmWyZl9HYtt4ZTc4hzhudSDxfnAfzchgQ O27w== X-Gm-Message-State: AOAM5333QTkVrwuCKkN3mq9dtJJjVdYJvusf12lo9b8Rhl7HIP6jvLi9 XtZvnhDgIgIJNqrj8mMbHi+aVXio0Y4= X-Received: by 2002:a63:e245:: with SMTP id y5mr2171726pgj.51.1597568962365; Sun, 16 Aug 2020 02:09:22 -0700 (PDT) Received: from bobo.ozlabs.ibm.com (193-116-193-175.tpgi.com.au. [193.116.193.175]) by smtp.gmail.com with ESMTPSA id o19sm12768369pjs.8.2020.08.16.02.09.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 16 Aug 2020 02:09:21 -0700 (PDT) From: Nicholas Piggin To: linux-mm@kvack.org Cc: Nicholas Piggin , linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, Catalin Marinas , Will Deacon , linux-arm-kernel@lists.infradead.org, Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" , Zefan Li , Jonathan Cameron Subject: [PATCH v4 1/8] mm/vmalloc: fix vmalloc_to_page for huge vmap mappings Date: Sun, 16 Aug 2020 19:08:57 +1000 Message-Id: <20200816090904.83947-2-npiggin@gmail.com> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20200816090904.83947-1-npiggin@gmail.com> References: <20200816090904.83947-1-npiggin@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.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); -- 2.23.0