Thread (94 messages) 94 messages, 9 authors, 2023-08-01

Re: [PATCH v9 11/42] x86/mm: Update ptep/pmdp_set_wrprotect() for _PAGE_SAVED_DIRTY

From: Linus Torvalds <torvalds@linux-foundation.org>
Date: 2023-06-13 18:08:01
Also in: linux-api, linux-arch, linux-mm, lkml

On Mon, Jun 12, 2023 at 5:14 PM Rick Edgecombe
[off-list ref] wrote:
quoted hunk ↗ jump to hunk
+++ b/arch/x86/include/asm/pgtable.h
@@ -1189,7 +1189,17 @@ static inline pte_t ptep_get_and_clear_full(struct mm_struct *mm,
 static inline void ptep_set_wrprotect(struct mm_struct *mm,
                                      unsigned long addr, pte_t *ptep)
 {
-       clear_bit(_PAGE_BIT_RW, (unsigned long *)&ptep->pte);
+       /*
+        * Avoid accidentally creating shadow stack PTEs
+        * (Write=0,Dirty=1).  Use cmpxchg() to prevent races with
+        * the hardware setting Dirty=1.
+        */
+       pte_t old_pte, new_pte;
+
+       old_pte = READ_ONCE(*ptep);
+       do {
+               new_pte = pte_wrprotect(old_pte);
+       } while (!try_cmpxchg((long *)&ptep->pte, (long *)&old_pte, *(long *)&new_pte));
 }
Thanks. Much nicer with this all being done just one way and no need
for ifdeffery on config options and runtime static branches.

                  Linus
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help