Skip to content

Commit c2da319

Browse files
xzpeterakpm00
authored andcommitted
mm/uffd: sanity check write bit for uffd-wp protected ptes
Let's add one sanity check for CONFIG_DEBUG_VM on the write bit in whatever chance we have when walking through the pgtables. It can bring the error earlier even before the app notices the data was corrupted on the snapshot. Also it helps us to identify this is a wrong pgtable setup, so hopefully a great information to have for debugging too. Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Peter Xu <[email protected]> Cc: Andrea Arcangeli <[email protected]> Cc: Alistair Popple <[email protected]> Cc: Axel Rasmussen <[email protected]> Cc: Mike Rapoport <[email protected]> Cc: Nadav Amit <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 25e9fa2 commit c2da319

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

arch/x86/include/asm/pgtable.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,23 @@ static inline pte_t pte_clear_flags(pte_t pte, pteval_t clear)
292292
#ifdef CONFIG_HAVE_ARCH_USERFAULTFD_WP
293293
static inline int pte_uffd_wp(pte_t pte)
294294
{
295-
return pte_flags(pte) & _PAGE_UFFD_WP;
295+
bool wp = pte_flags(pte) & _PAGE_UFFD_WP;
296+
297+
#ifdef CONFIG_DEBUG_VM
298+
/*
299+
* Having write bit for wr-protect-marked present ptes is fatal,
300+
* because it means the uffd-wp bit will be ignored and write will
301+
* just go through.
302+
*
303+
* Use any chance of pgtable walking to verify this (e.g., when
304+
* page swapped out or being migrated for all purposes). It means
305+
* something is already wrong. Tell the admin even before the
306+
* process crashes. We also nail it with wrong pgtable setup.
307+
*/
308+
WARN_ON_ONCE(wp && pte_write(pte));
309+
#endif
310+
311+
return wp;
296312
}
297313

298314
static inline pte_t pte_mkuffd_wp(pte_t pte)

0 commit comments

Comments
 (0)