Received: by 10.223.185.116 with SMTP id b49csp2023706wrg; Thu, 15 Feb 2018 05:27:57 -0800 (PST) X-Google-Smtp-Source: AH8x225Gc5GsQ2pKZ+uefQCVfFlay1lrSUE1fw+1d6Sl80RTAT2ShCshJyd9hhpyzCgN8hpX5OGJ X-Received: by 10.101.80.204 with SMTP id s12mr2224627pgp.185.1518701277395; Thu, 15 Feb 2018 05:27:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1518701277; cv=none; d=google.com; s=arc-20160816; b=pF5aAgDMJA7NhZK4qJs/pbbwiq460tGfXMPe7272W4a8Ys+VZDgV0acdlx7YYxsyyG X98SnPlx/bMeYapeNtChmpDVss9txTqaAJIpEbzoQFCB7CXx978l3kZlJX+5puZMiDCA 8PZVgGev0SM98YtMWrCZwwQTVZGXjTjO9q6bCTPFBYLoq6HRS/gDrciDMgwPUKDvp/jo VL6wK9PFAwIPLkPZpdqES2NxE/otUIf5YHwK5q9IgX4QgPNFzswl4z1T195yLlFQ+wA0 vCUAmxtB7MqiRo1rNqSXXDtSsZZC9C5Npr0UaFzi9INraVv7gVS8sFupu3+9Y/WiomCw XMeQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:in-reply-to:references:date :from:cc:to:subject:arc-authentication-results; bh=1ti+1qls5Z+OMiYxnFA/ssaS7qQ11PK2gmNIqmdO/jU=; b=LYHucBno77RJWklnhtghUj1OZzSeI7mZQvSDKphMok8RET4Q+D90vRvG9RKUzBPRMU kAxIIAT+QHDKs2FYmpiJBw5jPt19gYAbBCJk09EhZlebWXIOzcECI+m4u9n0ytukeQhL wkSLrjwYXbvbOqiem2Bf2xs5kv3mniMHtNJE1n/YLhLJ3qCS0RC8FzXYCn1HoDhGPt5P Uug7wpiCrxDYp1CmDQkJjO2a2vw7sZIaT40CekwjuWeYHeRb1CELRqnTpvqXcK4RkeF6 GrumDmmEyQKl/UOj6JRmO5YYd8VonUQOGCoy1klP3JW0ZzNk92IP/eIsyYQlYYvA/K5i ht0A== 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 q187si1481522pga.547.2018.02.15.05.27.42; Thu, 15 Feb 2018 05:27:57 -0800 (PST) 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 S1032369AbeBONZU (ORCPT + 99 others); Thu, 15 Feb 2018 08:25:20 -0500 Received: from mga06.intel.com ([134.134.136.31]:57116 "EHLO mga06.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1031104AbeBONZT (ORCPT ); Thu, 15 Feb 2018 08:25:19 -0500 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga104.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 15 Feb 2018 05:25:18 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.46,517,1511856000"; d="scan'208";a="18354248" Received: from viggo.jf.intel.com (HELO localhost.localdomain) ([10.54.39.119]) by orsmga008.jf.intel.com with ESMTP; 15 Feb 2018 05:25:18 -0800 Subject: [PATCH 1/3] x86/mm: factor out conditional pageattr PTE bit setting code To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, Dave Hansen , luto@kernel.org, torvalds@linux-foundation.org, keescook@google.com, hughd@google.com, jgross@suse.com, x86@kernel.org From: Dave Hansen Date: Thu, 15 Feb 2018 05:20:54 -0800 References: <20180215132053.6C9B48C8@viggo.jf.intel.com> In-Reply-To: <20180215132053.6C9B48C8@viggo.jf.intel.com> Message-Id: <20180215132054.431CDDE3@viggo.jf.intel.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Dave Hansen The pageattr code has a pattern repeated where it sets a PTE bit for present PTEs but clears it for non-present PTEs. This helps to keep pte_none() from getting messed up. _PAGE_GLOBAL is the most frequent target of this pattern. This pattern also includes a nice, copy-and-pasted comment. I want to do some special stuff with _PAGE_GLOBAL in a moment, so refactor this a _bit_ to centralize the comment and the bit operations. Signed-off-by: Dave Hansen Cc: Andy Lutomirski Cc: Linus Torvalds Cc: Kees Cook Cc: Hugh Dickins Cc: Juergen Gross Cc: x86@kernel.org --- b/arch/x86/mm/pageattr.c | 65 ++++++++++++++--------------------------------- 1 file changed, 20 insertions(+), 45 deletions(-) diff -puN arch/x86/mm/pageattr.c~kpti-centralize-global-setting arch/x86/mm/pageattr.c --- a/arch/x86/mm/pageattr.c~kpti-centralize-global-setting 2018-02-13 15:17:55.602210062 -0800 +++ b/arch/x86/mm/pageattr.c 2018-02-13 15:17:55.606210062 -0800 @@ -512,6 +512,22 @@ static void __set_pmd_pte(pte_t *kpte, u #endif } +static pgprot_t pgprot_set_on_present(pgprot_t prot, pteval_t flags) +{ + /* + * Set 'flags' only if PRESENT. Ensures that we do not + * set flags in an otherwise empty PTE breaking pte_none(). + * A later function (such as canon_pgprot()) must clear + * possibly unsupported flags (like _PAGE_GLOBAL). + */ + if (pgprot_val(prot) & _PAGE_PRESENT) + pgprot_val(prot) |= flags; + else + pgprot_val(prot) &= ~flags; + + return prot; +} + static int try_preserve_large_page(pte_t *kpte, unsigned long address, struct cpa_data *cpa) @@ -577,18 +593,7 @@ try_preserve_large_page(pte_t *kpte, uns * 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. - */ - if (pgprot_val(req_prot) & _PAGE_PRESENT) - pgprot_val(req_prot) |= _PAGE_PSE | _PAGE_GLOBAL; - else - pgprot_val(req_prot) &= ~(_PAGE_PSE | _PAGE_GLOBAL); - + req_prot = pgprot_set_on_present(req_prot, _PAGE_GLOBAL | _PAGE_PSE); req_prot = canon_pgprot(req_prot); /* @@ -698,16 +703,7 @@ __split_large_page(struct cpa_data *cpa, 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_set_on_present(ref_prot, _PAGE_GLOBAL); /* * Get the target pfn from the original entry: @@ -930,18 +926,7 @@ static void populate_pte(struct cpa_data 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_set_on_present(pgprot, _PAGE_GLOBAL); pgprot = canon_pgprot(pgprot); while (num_pages-- && start < end) { @@ -1234,17 +1219,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_set_on_present(new_prot, _PAGE_GLOBAL); /* * We need to keep the pfn from the existing PTE, _