Skip to content

Commit 7e3ce3f

Browse files
xzpeterakpm00
authored andcommitted
mm: fix a few rare cases of using swapin error pte marker
This patch should harden commit 15520a3 ("mm: use pte markers for swap errors") on using pte markers for swapin errors on a few corner cases. 1. Propagate swapin errors across fork()s: if there're swapin errors in the parent mm, after fork()s the child should sigbus too when an error page is accessed. 2. Fix a rare condition race in pte_marker_clear() where a uffd-wp pte marker can be quickly switched to a swapin error. 3. Explicitly ignore swapin error pte markers in change_protection(). I mostly don't worry on (2) or (3) at all, but we should still have them. Case (1) is special because it can potentially cause silent data corrupt on child when parent has swapin error triggered with swapoff, but since swapin error is rare itself already it's probably not easy to trigger either. Currently there is a priority difference between the uffd-wp bit and the swapin error entry, in which the swapin error always has higher priority (e.g. we don't need to wr-protect a swapin error pte marker). If there will be a 3rd bit introduced, we'll probably need to consider a more involved approach so we may need to start operate on the bits. Let's leave that for later. This patch is tested with case (1) explicitly where we'll get corrupted data before in the child if there's existing swapin error pte markers, and after patch applied the child can be rightfully killed. We don't need to copy stable for this one since 15520a3 just landed as part of v6.2-rc1, only "Fixes" applied. Link: https://lkml.kernel.org/r/[email protected] Fixes: 15520a3 ("mm: use pte markers for swap errors") Signed-off-by: Peter Xu <[email protected]> Acked-by: David Hildenbrand <[email protected]> Reviewed-by: Miaohe Lin <[email protected]> Cc: Andrea Arcangeli <[email protected]> Cc: "Huang, Ying" <[email protected]> Cc: Nadav Amit <[email protected]> Cc: Pengfei Xu <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 49d6d7f commit 7e3ce3f

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

mm/hugetlb.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5051,6 +5051,9 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
50515051
entry = huge_pte_clear_uffd_wp(entry);
50525052
set_huge_pte_at(dst, addr, dst_pte, entry);
50535053
} else if (unlikely(is_pte_marker(entry))) {
5054+
/* No swap on hugetlb */
5055+
WARN_ON_ONCE(
5056+
is_swapin_error_entry(pte_to_swp_entry(entry)));
50545057
/*
50555058
* We copy the pte marker only if the dst vma has
50565059
* uffd-wp enabled.

mm/memory.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,7 @@ copy_nonpresent_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm,
828828
return -EBUSY;
829829
return -ENOENT;
830830
} else if (is_pte_marker_entry(entry)) {
831-
if (userfaultfd_wp(dst_vma))
831+
if (is_swapin_error_entry(entry) || userfaultfd_wp(dst_vma))
832832
set_pte_at(dst_mm, addr, dst_pte, pte);
833833
return 0;
834834
}
@@ -3625,8 +3625,12 @@ static vm_fault_t pte_marker_clear(struct vm_fault *vmf)
36253625
/*
36263626
* Be careful so that we will only recover a special uffd-wp pte into a
36273627
* none pte. Otherwise it means the pte could have changed, so retry.
3628+
*
3629+
* This should also cover the case where e.g. the pte changed
3630+
* quickly from a PTE_MARKER_UFFD_WP into PTE_MARKER_SWAPIN_ERROR.
3631+
* So is_pte_marker() check is not enough to safely drop the pte.
36283632
*/
3629-
if (is_pte_marker(*vmf->pte))
3633+
if (pte_same(vmf->orig_pte, *vmf->pte))
36303634
pte_clear(vmf->vma->vm_mm, vmf->address, vmf->pte);
36313635
pte_unmap_unlock(vmf->pte, vmf->ptl);
36323636
return 0;

mm/mprotect.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,13 @@ static unsigned long change_pte_range(struct mmu_gather *tlb,
245245
newpte = pte_swp_mksoft_dirty(newpte);
246246
if (pte_swp_uffd_wp(oldpte))
247247
newpte = pte_swp_mkuffd_wp(newpte);
248-
} else if (pte_marker_entry_uffd_wp(entry)) {
248+
} else if (is_pte_marker_entry(entry)) {
249+
/*
250+
* Ignore swapin errors unconditionally,
251+
* because any access should sigbus anyway.
252+
*/
253+
if (is_swapin_error_entry(entry))
254+
continue;
249255
/*
250256
* If this is uffd-wp pte marker and we'd like
251257
* to unprotect it, drop it; the next page

0 commit comments

Comments
 (0)