History

userfaultfd: gate must_wait writability check on pte_present()

userfaultfd_must_wait() and userfaultfd_huge_must_wait() read the PTE without taking the page table lock and then apply pte_write() / huge_pte_write() to it. Those accessors decode bits from the present encoding only; on a swap or migration entry they read the offset bits that happen to share the same position and return an undefined result. The intent of the check is "is this fault still WP-blocked?". A non-marker swap entry means the page is in transit -- the userfault context the original fault delivered against is no longer the same, and the swap-in or migration completion path will re-deliver a fresh fault if userspace still needs to handle it. Worst case under the current code the garbage write bit says "wait", and the thread stays asleep until a UFFDIO_WAKE that may never arrive. Gate the writability check on pte_present() so the lockless re-check only inspects present-PTE bits when the entry is actually present. The non-present, non-marker case returns "don't wait" and lets the fault path retry.

Kiryl Shutsemau (Meta) committed 5 months ago

Commit 8e80af51 parent f7e2c211 file · +20 −0Browse files

Unified · Split

mm/userfaultfd.c+20−0
@@ -2542,6 +2542,15 @@ static inline bool userfaultfd_huge_must_wait(struct userfaultfd_ctx *ctx, · 2541 unmodified lines
2542 /* UFFD PTE markers require userspace to resolve the fault. */2542 /* UFFD PTE markers require userspace to resolve the fault. */
2543 if (pte_is_uffd_marker(pte))2543 if (pte_is_uffd_marker(pte))
2544 return true;2544 return true;
2545 /*
2546 * Concurrent migration may have replaced the present PTE with a
2547 * non-marker swap entry between fault delivery and this lockless
2548 * re-check. huge_pte_write() on a swap entry decodes random offset
2549 * bits, so gate it on pte_present(). The migration completion path
2550 * will re-deliver the fault if it still needs userspace.
2551 */
2552 if (!pte_present(pte))
2553 return false;
2545 /*2554 /*
2546 * If VMA has UFFD WP faults enabled and WP fault, wait for userspace to2555 * If VMA has UFFD WP faults enabled and WP fault, wait for userspace to
2547 * resolve the fault.2556 * resolve the fault.
@@ -2628,6 +2637,17 @@ again: · 80 unmodified lines
2628 /* UFFD PTE markers require userspace to resolve the fault. */2637 /* UFFD PTE markers require userspace to resolve the fault. */
2629 if (pte_is_uffd_marker(ptent))2638 if (pte_is_uffd_marker(ptent))
2630 goto out;2639 goto out;
2640 /*
2641 * Concurrent swap-out / migration may have replaced the present PTE
2642 * with a non-marker swap entry between fault delivery and this
2643 * lockless re-check. pte_write() on a swap entry decodes random
2644 * offset bits, so gate it on pte_present(). The page-in path will
2645 * re-deliver the fault if it still needs userspace.
2646 */
2647 if (!pte_present(ptent)) {
2648 ret = false;
2649 goto out;
2650 }
2631 /*2651 /*
2632 * If VMA has UFFD WP faults enabled and WP fault, wait for userspace to2652 * If VMA has UFFD WP faults enabled and WP fault, wait for userspace to
2633 * resolve the fault.2653 * resolve the fault.