Received: by 10.213.65.68 with SMTP id h4csp2870362imn; Mon, 9 Apr 2018 10:17:34 -0700 (PDT) X-Google-Smtp-Source: AIpwx4+ElLERioGn1SA77KOzj0OXvIPhiki9urRTZ+MB+n49TGyGe6AyJPnSaznPmGhH20rUbTDo X-Received: by 2002:a17:902:3181:: with SMTP id x1-v6mr40471768plb.338.1523294254106; Mon, 09 Apr 2018 10:17:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1523294254; cv=none; d=google.com; s=arc-20160816; b=xYmG/djcrtP6ukWCXgJFe5TD5txUm/6JlxJP0zkwlVWxmfx894xoqpFX1B+t6m6Y0H ydb0ENNE+KXmNBH9FkY+3Bl/6IgQxaoMBm9wzDaWSgcd9wNf2tC5svJm1dv642qZ0Y8h Tued5ZlLIaHLB/j3xlM9A1u8ZROVWszwHoNvW81h4i5R4ki/HEqrt7ofzVCJsdkZLsLv pHwiKc/AxrJZ+8cftTdjGxRzk4YLWnG6VeTH4Db48ZwdGdxbnSJ7Mzxm2fMC6BHiBWlx SHJv0psI77ITnlK/Ac2EGoGU/OssGCe5MWbnPkPVclegkEQnnC5/qR/0uNzkRhkh5RUj JWvA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-disposition :content-transfer-encoding:mime-version:robot-unsubscribe:robot-id :git-commit-id:subject:to:references:in-reply-to:reply-to:cc :message-id:from:date:arc-authentication-results; bh=Z8EZzMhd1xwJjtOb/rSy8no4mZZYDuX8PaxOI19Nx7w=; b=PVb+huGGQ9eUr09N/IkV0CPYrd/d5C4H/v7cX/Hl9GFNpf/2cNoSJVIaQFjPDRCTGH zCgIOPdpo/ylQowTeCX7H/PsvQYJ0+mY8LWv0Y8LSmJYRvJCOYPaLIfvtqFDNljwC0xi uBkLitENw7LY683QO/nr0giGcvFFAmgxFtC3Peg5ESeX9Gyofrp13RyQopaN4IK4zpb4 M+wfIy7N0440eM+FkS8ee1zF9IDpWysPuWrYsjTwob4zHrl0HloXda6F1PVDm+9lB4oQ Vo5AbXkt5n2HCaWSKaKE7bfYxu/mHgVgEbs4Li+1kqT0JjwKuF8306PY7oXnZmdGkNrp mGcw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id s4-v6si674437plp.266.2018.04.09.10.16.57; Mon, 09 Apr 2018 10:17:34 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753788AbeDIRNH (ORCPT + 99 others); Mon, 9 Apr 2018 13:13:07 -0400 Received: from terminus.zytor.com ([198.137.202.136]:38403 "EHLO terminus.zytor.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753526AbeDIRNF (ORCPT ); Mon, 9 Apr 2018 13:13:05 -0400 Received: from terminus.zytor.com (localhost [127.0.0.1]) by terminus.zytor.com (8.15.2/8.15.2) with ESMTP id w39HBsPs1893969; Mon, 9 Apr 2018 10:11:54 -0700 Received: (from tipbot@localhost) by terminus.zytor.com (8.15.2/8.15.2/Submit) id w39HBsVu1893966; Mon, 9 Apr 2018 10:11:54 -0700 Date: Mon, 9 Apr 2018 10:11:54 -0700 X-Authentication-Warning: terminus.zytor.com: tipbot set sender to tipbot@zytor.com using -f From: tip-bot for Dave Hansen Message-ID: Cc: jpoimboe@redhat.com, bp@alien8.de, luto@kernel.org, torvalds@linux-foundation.org, gregkh@linuxfoundation.org, arjan@linux.intel.com, dan.j.williams@intel.com, jgross@suse.com, dave.hansen@linux.intel.com, dwmw2@infradead.org, aarcange@redhat.com, tglx@linutronix.de, mingo@kernel.org, hpa@zytor.com, namit@vmware.com, peterz@infradead.org, keescook@google.com, linux-kernel@vger.kernel.org, hughd@google.com Reply-To: jgross@suse.com, dan.j.williams@intel.com, dave.hansen@linux.intel.com, bp@alien8.de, luto@kernel.org, jpoimboe@redhat.com, torvalds@linux-foundation.org, gregkh@linuxfoundation.org, arjan@linux.intel.com, peterz@infradead.org, keescook@google.com, hughd@google.com, linux-kernel@vger.kernel.org, aarcange@redhat.com, dwmw2@infradead.org, mingo@kernel.org, tglx@linutronix.de, namit@vmware.com, hpa@zytor.com In-Reply-To: <20180406205502.86E199DA@viggo.jf.intel.com> References: <20180406205502.86E199DA@viggo.jf.intel.com> To: linux-tip-commits@vger.kernel.org Subject: [tip:x86/pti] x86/mm: Factor out pageattr _PAGE_GLOBAL setting Git-Commit-ID: d1440b23c922d845ff039f64694a32ff356e89fa X-Mailer: tip-git-log-daemon Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=UTF-8 Content-Disposition: inline X-Spam-Status: No, score=-2.9 required=5.0 tests=ALL_TRUSTED,BAYES_00 autolearn=ham autolearn_force=no version=3.4.1 X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on terminus.zytor.com Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Commit-ID: d1440b23c922d845ff039f64694a32ff356e89fa Gitweb: https://git.kernel.org/tip/d1440b23c922d845ff039f64694a32ff356e89fa Author: Dave Hansen AuthorDate: Fri, 6 Apr 2018 13:55:02 -0700 Committer: Ingo Molnar CommitDate: Mon, 9 Apr 2018 18:27:32 +0200 x86/mm: Factor out pageattr _PAGE_GLOBAL setting The pageattr code has a pattern repeated where it sets _PAGE_GLOBAL for present PTEs but clears it for non-present PTEs. The intention is to keep _PAGE_GLOBAL from getting confused with _PAGE_PROTNONE since _PAGE_GLOBAL is for present PTEs and _PAGE_PROTNONE is for non-present But, this pattern makes no sense. Effectively, it says, if you use the pageattr code, always set _PAGE_GLOBAL when _PAGE_PRESENT. canon_pgprot() will clear it if unsupported (because it masks the value with __supported_pte_mask) but we *always* set it. Even if canon_pgprot() did not filter _PAGE_GLOBAL, it would be OK. _PAGE_GLOBAL is ignored when CR4.PGE=0 by the hardware. This unconditional setting of _PAGE_GLOBAL is a problem when we have PTI and non-PTI and we want some areas to have _PAGE_GLOBAL and some not. This updated version of the code says: 1. Clear _PAGE_GLOBAL when !_PAGE_PRESENT 2. Never set _PAGE_GLOBAL implicitly 3. Allow _PAGE_GLOBAL to be in cpa.set_mask 4. Allow _PAGE_GLOBAL to be inherited from previous PTE Signed-off-by: Dave Hansen Cc: Andrea Arcangeli Cc: Andy Lutomirski Cc: Arjan van de Ven Cc: Borislav Petkov Cc: Dan Williams Cc: David Woodhouse Cc: Greg Kroah-Hartman Cc: Hugh Dickins Cc: Josh Poimboeuf Cc: Juergen Gross Cc: Kees Cook Cc: Linus Torvalds Cc: Nadav Amit Cc: Peter Zijlstra Cc: Thomas Gleixner Cc: linux-mm@kvack.org Link: http://lkml.kernel.org/r/20180406205502.86E199DA@viggo.jf.intel.com Signed-off-by: Ingo Molnar --- arch/x86/mm/pageattr.c | 66 ++++++++++++++++++-------------------------------- 1 file changed, 23 insertions(+), 43 deletions(-) diff --git a/arch/x86/mm/pageattr.c b/arch/x86/mm/pageattr.c index 85cf12219dea..4d369d5c04c5 100644 --- a/arch/x86/mm/pageattr.c +++ b/arch/x86/mm/pageattr.c @@ -512,6 +512,23 @@ static void __set_pmd_pte(pte_t *kpte, unsigned long address, pte_t pte) #endif } +static pgprot_t pgprot_clear_protnone_bits(pgprot_t prot) +{ + /* + * _PAGE_GLOBAL means "global page" for present PTEs. + * But, it is also used to indicate _PAGE_PROTNONE + * for non-present PTEs. + * + * This ensures that a _PAGE_GLOBAL PTE going from + * present to non-present is not confused as + * _PAGE_PROTNONE. + */ + if (!(pgprot_val(prot) & _PAGE_PRESENT)) + pgprot_val(prot) &= ~_PAGE_GLOBAL; + + return prot; +} + static int try_preserve_large_page(pte_t *kpte, unsigned long address, struct cpa_data *cpa) @@ -577,18 +594,11 @@ try_preserve_large_page(pte_t *kpte, unsigned long address, * different bit positions in the two formats. */ req_prot = pgprot_4k_2_large(req_prot); - - /* - * Set the PSE and GLOBAL flags only if the PRESENT flag is - * set otherwise pmd_present/pmd_huge will return true even on - * a non present pmd. The canon_pgprot will clear _PAGE_GLOBAL - * for the ancient hardware that doesn't support it. - */ + req_prot = pgprot_clear_protnone_bits(req_prot); if (pgprot_val(req_prot) & _PAGE_PRESENT) - pgprot_val(req_prot) |= _PAGE_PSE | _PAGE_GLOBAL; + pgprot_val(req_prot) |= _PAGE_PSE; else - pgprot_val(req_prot) &= ~(_PAGE_PSE | _PAGE_GLOBAL); - + pgprot_val(req_prot) &= ~_PAGE_PSE; req_prot = canon_pgprot(req_prot); /* @@ -698,16 +708,7 @@ __split_large_page(struct cpa_data *cpa, pte_t *kpte, unsigned long address, return 1; } - /* - * Set the GLOBAL flags only if the PRESENT flag is set - * otherwise pmd/pte_present will return true even on a non - * present pmd/pte. The canon_pgprot will clear _PAGE_GLOBAL - * for the ancient hardware that doesn't support it. - */ - if (pgprot_val(ref_prot) & _PAGE_PRESENT) - pgprot_val(ref_prot) |= _PAGE_GLOBAL; - else - pgprot_val(ref_prot) &= ~_PAGE_GLOBAL; + ref_prot = pgprot_clear_protnone_bits(ref_prot); /* * Get the target pfn from the original entry: @@ -930,18 +931,7 @@ static void populate_pte(struct cpa_data *cpa, pte = pte_offset_kernel(pmd, start); - /* - * Set the GLOBAL flags only if the PRESENT flag is - * set otherwise pte_present will return true even on - * a non present pte. The canon_pgprot will clear - * _PAGE_GLOBAL for the ancient hardware that doesn't - * support it. - */ - if (pgprot_val(pgprot) & _PAGE_PRESENT) - pgprot_val(pgprot) |= _PAGE_GLOBAL; - else - pgprot_val(pgprot) &= ~_PAGE_GLOBAL; - + pgprot = pgprot_clear_protnone_bits(pgprot); pgprot = canon_pgprot(pgprot); while (num_pages-- && start < end) { @@ -1234,17 +1224,7 @@ repeat: new_prot = static_protections(new_prot, address, pfn); - /* - * Set the GLOBAL flags only if the PRESENT flag is - * set otherwise pte_present will return true even on - * a non present pte. The canon_pgprot will clear - * _PAGE_GLOBAL for the ancient hardware that doesn't - * support it. - */ - if (pgprot_val(new_prot) & _PAGE_PRESENT) - pgprot_val(new_prot) |= _PAGE_GLOBAL; - else - pgprot_val(new_prot) &= ~_PAGE_GLOBAL; + new_prot = pgprot_clear_protnone_bits(new_prot); /* * We need to keep the pfn from the existing PTE,