[RFC PATCH 11/14] x86/mm: use relaxed TLB flushes when protection is removed

From: Nadav Amit
Date: Mon Jul 18 2022 - 15:37:44 EST


From: Nadav Amit <namit@xxxxxxxxxx>

When checking x86 PTE flags to determine whether a TLB flush is needed,
determine whether a relaxed TLB flush is sufficient. If protection is
added (NX removed or W added), indicate that a relaxed TLB flush would
suffice.

Cc: Andrea Arcangeli <aarcange@xxxxxxxxxx>
Cc: Andrew Cooper <andrew.cooper3@xxxxxxxxxx>
Cc: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
Cc: Andy Lutomirski <luto@xxxxxxxxxx>
Cc: Dave Hansen <dave.hansen@xxxxxxxxxxxxxxx>
Cc: David Hildenbrand <david@xxxxxxxxxx>
Cc: Peter Xu <peterx@xxxxxxxxxx>
Cc: Peter Zijlstra <peterz@xxxxxxxxxxxxx>
Cc: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
Cc: Will Deacon <will@xxxxxxxxxx>
Cc: Yu Zhao <yuzhao@xxxxxxxxxx>
Cc: Nick Piggin <npiggin@xxxxxxxxx>
Signed-off-by: Nadav Amit <namit@xxxxxxxxxx>
---
arch/x86/include/asm/tlbflush.h | 20 ++++++++++++++++----
1 file changed, 16 insertions(+), 4 deletions(-)

diff --git a/arch/x86/include/asm/tlbflush.h b/arch/x86/include/asm/tlbflush.h
index 230cd1d24fe6..4f98735ab07a 100644
--- a/arch/x86/include/asm/tlbflush.h
+++ b/arch/x86/include/asm/tlbflush.h
@@ -271,18 +271,23 @@ static inline enum pte_flush_type pte_flags_flush_type(unsigned long oldflags,
* dirty/access bit if needed without a fault.
*/
const pteval_t flush_on_clear = _PAGE_DIRTY | _PAGE_PRESENT |
- _PAGE_ACCESSED;
+ _PAGE_ACCESSED | _PAGE_RW;
+ const pteval_t flush_on_set = _PAGE_NX;
+ const pteval_t flush_on_set_relaxed = _PAGE_RW;
+ const pteval_t flush_on_clear_relaxed = _PAGE_NX;
const pteval_t software_flags = _PAGE_SOFTW1 | _PAGE_SOFTW2 |
_PAGE_SOFTW3 | _PAGE_SOFTW4;
- const pteval_t flush_on_change = _PAGE_RW | _PAGE_USER | _PAGE_PWT |
+ const pteval_t flush_on_change = _PAGE_USER | _PAGE_PWT |
_PAGE_PCD | _PAGE_PSE | _PAGE_GLOBAL | _PAGE_PAT |
_PAGE_PAT_LARGE | _PAGE_PKEY_BIT0 | _PAGE_PKEY_BIT1 |
- _PAGE_PKEY_BIT2 | _PAGE_PKEY_BIT3 | _PAGE_NX;
+ _PAGE_PKEY_BIT2 | _PAGE_PKEY_BIT3;
unsigned long diff = oldflags ^ newflags;

BUILD_BUG_ON(flush_on_clear & software_flags);
BUILD_BUG_ON(flush_on_clear & flush_on_change);
BUILD_BUG_ON(flush_on_change & software_flags);
+ BUILD_BUG_ON(flush_on_change & flush_on_clear_relaxed);
+ BUILD_BUG_ON(flush_on_change & flush_on_set_relaxed);

/* Ignore software flags */
diff &= ~software_flags;
@@ -301,9 +306,16 @@ static inline enum pte_flush_type pte_flags_flush_type(unsigned long oldflags,
if (diff & flush_on_change)
return PTE_FLUSH_STRICT;

+ if (diff & oldflags & flush_on_clear_relaxed)
+ return PTE_FLUSH_RELAXED;
+
+ if (diff & newflags & flush_on_set_relaxed)
+ return PTE_FLUSH_RELAXED;
+
/* Ensure there are no flags that were left behind */
if (IS_ENABLED(CONFIG_DEBUG_VM) &&
- (diff & ~(flush_on_clear | software_flags | flush_on_change))) {
+ (diff & ~(flush_on_clear | flush_on_set |
+ software_flags | flush_on_change))) {
VM_WARN_ON_ONCE(1);
return PTE_FLUSH_STRICT;
}
--
2.25.1