Skip to contentSkip to conversation

x86: Flush the TLB after setting user-bit

PATCH Applied Nadav Amit 2 participants 2 messages Jun 17, 2021 KVM

lore
Share & tools
lore

Nadav Amit

author
#0 lore raw

PATCH 1/1 x86: Flush the TLB after setting user-bit

From: Nadav Amit <[email protected]>

According to Intel SDM 4.10.4.3 "Optional Invalidation": "If CR4.SMEP = 0 and a paging-structure entry is modified to change the U/S flag from 0 to 1, failure to perform an invalidation may result in a "spurious" page-fault exception (e.g., in response to an attempted user-mode access) but no other adverse behavior."

The access test actually causes in certain environments a spurious page-fault. So invalidate the relevant PTE after setting the user bit.

---
 x86/access.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
x86/access.c +5 -1
index 0ad677e..47807cc 100644
@@ -216,8 +216,12 @@ static unsigned set_cr4_smep(int smep)
     if (smep)
         ptl2[2] &= ~PT_USER_MASK;
     r = write_cr4_checking(cr4);
-    if (r || !smep)
+    if (r || !smep) {
         ptl2[2] |= PT_USER_MASK;
+
+	/* Flush to avoid spurious #PF */
+	invlpg((void *)(2 << 21));
+    }
     if (!r)
         shadow_cr4 = cr4;
     return r;
Signature · 1 line
2.25.1
maintainer Applied
#1 lore raw
Quoted 25 lines · Nadav Amit

From: Nadav Amit <[email protected]>

According to Intel SDM 4.10.4.3 "Optional Invalidation": "If CR4.SMEP = 0 and a paging-structure entry is modified to change the U/S flag from 0 to 1, failure to perform an invalidation may result in a "spurious" page-fault exception (e.g., in response to an attempted user-mode access) but no other adverse behavior."

The access test actually causes in certain environments a spurious page-fault. So invalidate the relevant PTE after setting the user bit.

---
  x86/access.c | 6 +++++-
  1 file changed, 5 insertions(+), 1 deletion(-)
x86/access.c +0 -0
index 0ad677e..47807cc 100644
@@ -216,8 +216,12 @@ static unsigned set_cr4_smep(int smep)
      if (smep)
          ptl2[2] &= ~PT_USER_MASK;
      r = write_cr4_checking(cr4);

x86/access.c static unsigned set_cr4_smep(int smep) · line 227

Quoted 10 lines
x86/access.c +5 -1
-    if (r || !smep)
+    if (r || !smep) {
          ptl2[2] |= PT_USER_MASK;
+
+	/* Flush to avoid spurious #PF */
+	invlpg((void *)(2 << 21));
+    }
      if (!r)
          shadow_cr4 = cr4;
      return r;

Queued, thanks.

Paolo