stable.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "tip-bot2 for Juergen Gross" <tip-bot2@linutronix.de>
To: linux-tip-commits@vger.kernel.org
Cc: Juergen Gross <jgross@suse.com>, Borislav Petkov <bp@suse.de>,
	<stable@vger.kernel.org>,
	x86@kernel.org, linux-kernel@vger.kernel.org
Subject: [tip: x86/urgent] x86/pat: Fix x86_has_pat_wp()
Date: Wed, 13 Jul 2022 10:52:10 -0000	[thread overview]
Message-ID: <165770953023.15455.12477223338024740459.tip-bot2@tip-bot2> (raw)
In-Reply-To: <20220503132207.17234-1-jgross@suse.com>

The following commit has been merged into the x86/urgent branch of tip:

Commit-ID:     230ec83d4299b30c51a1c133b4f2a669972cc08a
Gitweb:        https://git.kernel.org/tip/230ec83d4299b30c51a1c133b4f2a669972cc08a
Author:        Juergen Gross <jgross@suse.com>
AuthorDate:    Fri, 08 Jul 2022 15:14:56 +02:00
Committer:     Borislav Petkov <bp@suse.de>
CommitterDate: Wed, 13 Jul 2022 12:44:04 +02:00

x86/pat: Fix x86_has_pat_wp()

x86_has_pat_wp() is using a wrong test, as it relies on the normal
PAT configuration used by the kernel. In case the PAT MSR has been
setup by another entity (e.g. Xen hypervisor) it might return false
even if the PAT configuration is allowing WP mappings. This due to the
fact that when running as Xen PV guest the PAT MSR is setup by the
hypervisor and cannot be changed by the guest. This results in the WP
related entry to be at a different position when running as Xen PV
guest compared to the bare metal or fully virtualized case.

The correct way to test for WP support is:

1. Get the PTE protection bits needed to select WP mode by reading
   __cachemode2pte_tbl[_PAGE_CACHE_MODE_WP] (depending on the PAT MSR
   setting this might return protection bits for a stronger mode, e.g.
   UC-)
2. Translate those bits back into the real cache mode selected by those
   PTE bits by reading __pte2cachemode_tbl[__pte2cm_idx(prot)]
3. Test for the cache mode to be _PAGE_CACHE_MODE_WP

Fixes: f88a68facd9a ("x86/mm: Extend early_memremap() support with additional attrs")
Signed-off-by: Juergen Gross <jgross@suse.com>
Signed-off-by: Borislav Petkov <bp@suse.de>
Cc: <stable@vger.kernel.org> # 4.14
Link: https://lore.kernel.org/r/20220503132207.17234-1-jgross@suse.com
---
 arch/x86/mm/init.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/arch/x86/mm/init.c b/arch/x86/mm/init.c
index d8cfce2..57ba550 100644
--- a/arch/x86/mm/init.c
+++ b/arch/x86/mm/init.c
@@ -77,10 +77,20 @@ static uint8_t __pte2cachemode_tbl[8] = {
 	[__pte2cm_idx(_PAGE_PWT | _PAGE_PCD | _PAGE_PAT)] = _PAGE_CACHE_MODE_UC,
 };
 
-/* Check that the write-protect PAT entry is set for write-protect */
+/*
+ * Check that the write-protect PAT entry is set for write-protect.
+ * To do this without making assumptions how PAT has been set up (Xen has
+ * another layout than the kernel), translate the _PAGE_CACHE_MODE_WP cache
+ * mode via the __cachemode2pte_tbl[] into protection bits (those protection
+ * bits will select a cache mode of WP or better), and then translate the
+ * protection bits back into the cache mode using __pte2cm_idx() and the
+ * __pte2cachemode_tbl[] array. This will return the really used cache mode.
+ */
 bool x86_has_pat_wp(void)
 {
-	return __pte2cachemode_tbl[_PAGE_CACHE_MODE_WP] == _PAGE_CACHE_MODE_WP;
+	uint16_t prot = __cachemode2pte_tbl[_PAGE_CACHE_MODE_WP];
+
+	return __pte2cachemode_tbl[__pte2cm_idx(prot)] == _PAGE_CACHE_MODE_WP;
 }
 
 enum page_cache_mode pgprot2cachemode(pgprot_t pgprot)

      parent reply	other threads:[~2022-07-13 10:52 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20220503132207.17234-1-jgross@suse.com>
     [not found] ` <20220503132207.17234-3-jgross@suse.com>
     [not found]   ` <1d86d8ff-6878-5488-e8c4-cbe8a5e8f624@suse.com>
     [not found]     ` <0dcb05d0-108f-6252-e768-f75b393a7f5c@suse.com>
     [not found]       ` <77255e5b-12bf-5390-6910-dafbaff18e96@netscape.net>
     [not found]         ` <a2e95587-418b-879f-2468-8699a6df4a6a@suse.com>
     [not found]           ` <8b1ebea5-7820-69c4-2e2b-9866d55bc180@netscape.net>
     [not found]             ` <c5fa3c3f-e602-ed68-d670-d59b93c012a0@netscape.net>
     [not found]               ` <3bff3562-bb1e-04e6-6eca-8d9bc355f2eb@suse.com>
     [not found]                 ` <3ca084a9-768e-a6f5-ace4-cd347978dec7@netscape.net>
     [not found]                   ` <9af0181a-e143-4474-acda-adbe72fc6227@suse.com>
2022-05-20 14:48                     ` [PATCH 2/2] x86/pat: add functions to query specific cache mode availability Chuck Zmudzinski
2022-05-21 10:47                       ` Thorsten Leemhuis
2022-05-24 18:32                         ` Chuck Zmudzinski
2022-05-25  7:45                           ` Thorsten Leemhuis
2022-05-25  8:04                             ` Juergen Gross
2022-05-25  8:37                             ` Jan Beulich
2022-05-25  8:51                               ` Thorsten Leemhuis
2022-05-25 19:25                             ` Chuck Zmudzinski
2022-05-20 15:46                     ` [REGRESSION} " Chuck Zmudzinski
2022-05-20 17:13                       ` Chuck Zmudzinski
2022-05-20 17:17                         ` Chuck Zmudzinski
2022-07-11  9:46 ` [tip: x86/urgent] x86/pat: Fix x86_has_pat_wp() tip-bot2 for Juergen Gross
2022-07-13 10:45 ` tip-bot2 for Juergen Gross
2022-07-13 10:52 ` tip-bot2 for Juergen Gross [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=165770953023.15455.12477223338024740459.tip-bot2@tip-bot2 \
    --to=tip-bot2@linutronix.de \
    --cc=bp@suse.de \
    --cc=jgross@suse.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-tip-commits@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).