FAILED: patch "[PATCH] mm: fix deadlock of mremap() against UFFDIO_MOVE" failed to apply to 6.18-stable tree
Subsystems (7)
Sasha Levin
maintainerThe patch below does not apply to the 6.18-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-6.18.y
git checkout FETCH_HEAD
git cherry-pick -x d03df42ddae196f20e9cfb775704e60269eb6b44
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '[email protected]' --subject-prefix 'PATCH 6.18.y' 'HEAD^..'
Thanks,
Sasha
------------------ original commit in Linus's tree ------------------
From d03df42ddae196f20e9cfb775704e60269eb6b44 Mon Sep 17 00:00:00 2001
From: Chris Mason <[email protected]>
Date: Sat, 31 Oct 2026 10:40:07 +0100
Subject: [PATCH] mm: fix deadlock of mremap() against UFFDIO_MOVE
With per-VMA locks, UFFDIO_MOVE can run concurrently with mremap() on different VMAs in the same mm. Those VMAs can share page tables, so both operations can acquire the same pair of page-table locks.
UFFDIO_MOVE orders the locks by address, while mremap() takes the source lock first. These conflicting orders can deadlock; mremap() holding mmap_lock for writing no longer helps.
Move double_pt_lock() and double_pt_unlock() to mm/internal.h and use them for all page-table moves in mremap(). Preserve the source entry checks after acquiring both locks. Remove the comments claiming that exclusive mmap_lock makes lock ordering irrelevant.
In move_ptes(), make sure to just recheck source and destination PMD after taking the locks and drop the overly-long comment.
Fix generated by an LLM and sent by Chris Mason in private.
[ david: use new helpers consistently; simplify description and comments;
move_ptes() fixup ]
--- include/linux/userfaultfd_k.h | 2 - mm/huge_memory.c | 19 +++----- mm/hugetlb.c | 14 ++---- mm/internal.h | 28 +++++++++++ mm/mremap.c | 87 ++++++++++++++--------------------- mm/userfaultfd.c | 27 ----------- 6 files changed, 72 insertions(+), 105 deletions(-)
include/linux/userfaultfd_k.h +0 -2
@@ -153,8 +153,6 @@ extern int mrwprotect_range(struct userfaultfd_ctx *ctx, unsigned long start, unsigned long len, bool enable_rwp);/* move_pages */ -void double_pt_lock (spinlock_t *ptl1,spinlock_t *ptl2); -void double_pt_unlock (spinlock_t *ptl1,spinlock_t *ptl2); int move_pages_huge_pmd(struct mm_struct *mm, pmd_t *dst_pmd, pmd_t *src_pmd, pmd_t dst_pmdval, struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,
mm/huge_memory.c +7 -12
@@ -2646,15 +2646,11 @@ bool move_huge_pmd(struct vm_area_struct *vma, unsigned long old_addr,return false; } -/* -* We don't have to worry about the ordering of src and dst -* ptlocks because exclusive mmap_lock prevents deadlock. -*/ - old_ptl =__pmd_trans_huge_lock (old_pmd, vma); -if (old_ptl) { - new_ptl =pmd_lockptr (mm, new_pmd); -if (new_ptl != old_ptl) -spin_lock_nested (new_ptl,SINGLE_DEPTH_NESTING ); + old_ptl =pmd_lockptr (mm, old_pmd); + new_ptl =pmd_lockptr (mm, new_pmd); + +double_pt_lock (old_ptl, new_ptl); +if (likely (pmd_is_huge (*old_pmd))) { pmd =pmdp_huge_get_and_clear (mm, old_addr, old_pmd);if (pmd_present (pmd)) force_flush = true;
@@ -2683,11 +2679,10 @@ bool move_huge_pmd(struct vm_area_struct *vma, unsigned long old_addr,set_pmd_at (mm, new_addr, new_pmd, pmd);if (force_flush)flush_pmd_tlb_range (vma, old_addr, old_addr +PMD_SIZE ); -if (new_ptl != old_ptl) -spin_unlock (new_ptl); -spin_unlock (old_ptl); +double_pt_unlock (old_ptl, new_ptl);return true; } +double_pt_unlock (old_ptl, new_ptl);return false; }
mm/hugetlb.c +3 -11
@@ -5153,15 +5153,9 @@ static void move_huge_pte(struct vm_area_struct *vma, unsigned long old_addr,spinlock_t *src_ptl, *dst_ptl;pte_t pte; - dst_ptl =huge_pte_lock (h, mm, dst_pte); + dst_ptl =huge_pte_lockptr (h, mm, dst_pte); src_ptl =huge_pte_lockptr (h, mm, src_pte); - -/* -* We don't have to worry about the ordering of src and dst ptlocks -* because exclusive mmap_lock (or the i_mmap_lock) prevents deadlock. -*/ -if (src_ptl != dst_ptl) -spin_lock_nested (src_ptl,SINGLE_DEPTH_NESTING ); +double_pt_lock (dst_ptl, src_ptl); pte =huge_ptep_get_and_clear (mm, old_addr, src_pte, sz);
@@ -5190,9 +5184,7 @@ static void move_huge_pte(struct vm_area_struct *vma, unsigned long old_addr,set_huge_pte_at (mm, new_addr, dst_pte, pte, sz); } -if (src_ptl != dst_ptl) -spin_unlock (src_ptl); -spin_unlock (dst_ptl); +double_pt_unlock (dst_ptl, src_ptl); } int move_hugetlb_page_tables(struct vm_area_struct *vma,
mm/internal.h +28 -0
@@ -624,6 +624,34 @@ static inline void force_page_cache_readahead(struct address_space *mapping,force_page_cache_ra (&ractl, nr_to_read); } +/* +* Take two page table locks at the same level in address order such that +* we cannot deadlock. +*/ +static inline void double_pt_lock (spinlock_t *ptl1,spinlock_t *ptl2) +__acquires (ptl1) +__acquires (ptl2) +{ +if (ptl1 > ptl2) +swap (ptl1, ptl2); +spin_lock (ptl1); +if (ptl1 != ptl2) +spin_lock_nested (ptl2,SINGLE_DEPTH_NESTING ); +else +__acquire (ptl2); +} + +static inline void double_pt_unlock (spinlock_t *ptl1,spinlock_t *ptl2) +__releases (ptl1) +__releases (ptl2) +{ +spin_unlock (ptl1); +if (ptl1 != ptl2) +spin_unlock (ptl2); +else +__release (ptl2); +} +unsigned find_lock_entries (struct address_space *mapping,pgoff_t *start,pgoff_t end,struct folio_batch *fbatch,pgoff_t *indices); unsigned find_get_entries(struct address_space *mapping, pgoff_t *start,
mm/mremap.c +34 -53
153 lines not on this page · raw message
mm/userfaultfd.c +0 -27
@@ -1206,33 +1206,6 @@ int mrwprotect_range(struct userfaultfd_ctx *ctx, unsigned long start,return 0 ; } -void double_pt_lock (spinlock_t *ptl1, -spinlock_t *ptl2) -__acquires (ptl1) -__acquires (ptl2) -{ -if (ptl1 > ptl2) -swap (ptl1, ptl2); -/* lock in virtual address order to avoid lock inversion */ -spin_lock (ptl1); -if (ptl1 != ptl2) -spin_lock_nested (ptl2,SINGLE_DEPTH_NESTING ); -else -__acquire (ptl2); -} - -void double_pt_unlock (spinlock_t *ptl1, -spinlock_t *ptl2) -__releases (ptl1) -__releases (ptl2) -{ -spin_unlock (ptl1); -if (ptl1 != ptl2) -spin_unlock (ptl2); -else -__release (ptl2); -} -static inline bool is_pte_pages_stable (pte_t *dst_pte,pte_t *src_pte,pte_t orig_dst_pte,pte_t orig_src_pte,pmd_t *dst_pmd,pmd_t dst_pmdval)
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 1/1 mm: fix deadlock of mremap() against UFFDIO_MOVE
From: Chris Mason <[email protected]>
[ Upstream commit d03df42ddae196f20e9cfb775704e60269eb6b44 ]
With per-VMA locks, UFFDIO_MOVE can run concurrently with mremap() on different VMAs in the same mm. Those VMAs can share page tables, so both operations can acquire the same pair of page-table locks.
UFFDIO_MOVE orders the locks by address, while mremap() takes the source lock first. These conflicting orders can deadlock; mremap() holding mmap_lock for writing no longer helps.
Move double_pt_lock() and double_pt_unlock() to mm/internal.h and use them for all page-table moves in mremap(). Preserve the source entry checks after acquiring both locks. Remove the comments claiming that exclusive mmap_lock makes lock ordering irrelevant.
In move_ptes(), make sure to just recheck source and destination PMD after taking the locks and drop the overly-long comment.
Fix generated by an LLM and sent by Chris Mason in private.
[ david: use new helpers consistently; simplify description and comments;
move_ptes() fixup ]
--- include/linux/userfaultfd_k.h | 2 - mm/huge_memory.c | 19 +++----- mm/hugetlb.c | 14 ++---- mm/internal.h | 28 +++++++++++ mm/mremap.c | 87 ++++++++++++++--------------------- mm/userfaultfd.c | 27 ----------- 6 files changed, 72 insertions(+), 105 deletions(-)
include/linux/userfaultfd_k.h +0 -2
@@ -140,8 +140,6 @@ extern long uffd_wp_range(struct vm_area_struct *vma, unsigned long start, unsigned long len, bool enable_wp);/* move_pages */ -void double_pt_lock (spinlock_t *ptl1,spinlock_t *ptl2); -void double_pt_unlock (spinlock_t *ptl1,spinlock_t *ptl2);ssize_t move_pages (struct userfaultfd_ctx *ctx,unsigned long dst_start,unsigned long src_start,unsigned long len,__u64 flags); int move_pages_huge_pmd(struct mm_struct *mm, pmd_t *dst_pmd, pmd_t *src_pmd, pmd_t dst_pmdval,
mm/huge_memory.c +7 -12
@@ -2423,15 +2423,11 @@ bool move_huge_pmd(struct vm_area_struct *vma, unsigned long old_addr,return false; } -/* -* We don't have to worry about the ordering of src and dst -* ptlocks because exclusive mmap_lock prevents deadlock. -*/ - old_ptl =__pmd_trans_huge_lock (old_pmd, vma); -if (old_ptl) { - new_ptl =pmd_lockptr (mm, new_pmd); -if (new_ptl != old_ptl) -spin_lock_nested (new_ptl,SINGLE_DEPTH_NESTING ); + old_ptl =pmd_lockptr (mm, old_pmd); + new_ptl =pmd_lockptr (mm, new_pmd); + +double_pt_lock (old_ptl, new_ptl); +if (likely (is_swap_pmd (*old_pmd) ||pmd_trans_huge (*old_pmd))) { pmd =pmdp_huge_get_and_clear (mm, old_addr, old_pmd);if (pmd_present (pmd)) force_flush = true;
@@ -2448,11 +2444,10 @@ bool move_huge_pmd(struct vm_area_struct *vma, unsigned long old_addr,set_pmd_at (mm, new_addr, new_pmd, pmd);if (force_flush)flush_pmd_tlb_range (vma, old_addr, old_addr +PMD_SIZE ); -if (new_ptl != old_ptl) -spin_unlock (new_ptl); -spin_unlock (old_ptl); +double_pt_unlock (old_ptl, new_ptl);return true; } +double_pt_unlock (old_ptl, new_ptl);return false; }
mm/hugetlb.c +3 -11
@@ -5808,15 +5808,9 @@ static void move_huge_pte(struct vm_area_struct *vma, unsigned long old_addr,spinlock_t *src_ptl, *dst_ptl;pte_t pte; - dst_ptl =huge_pte_lock (h, mm, dst_pte); + dst_ptl =huge_pte_lockptr (h, mm, dst_pte); src_ptl =huge_pte_lockptr (h, mm, src_pte); - -/* -* We don't have to worry about the ordering of src and dst ptlocks -* because exclusive mmap_lock (or the i_mmap_lock) prevents deadlock. -*/ -if (src_ptl != dst_ptl) -spin_lock_nested (src_ptl,SINGLE_DEPTH_NESTING ); +double_pt_lock (dst_ptl, src_ptl); pte =huge_ptep_get_and_clear (mm, old_addr, src_pte, sz);
@@ -5832,9 +5826,7 @@ static void move_huge_pte(struct vm_area_struct *vma, unsigned long old_addr,set_huge_pte_at (mm, new_addr, dst_pte, pte, sz); } -if (src_ptl != dst_ptl) -spin_unlock (src_ptl); -spin_unlock (dst_ptl); +double_pt_unlock (dst_ptl, src_ptl); } unsigned long move_hugetlb_page_tables(struct vm_area_struct *vma,
mm/internal.h +28 -0
@@ -467,6 +467,34 @@ static inline void force_page_cache_readahead(struct address_space *mapping,force_page_cache_ra (&ractl, nr_to_read); } +/* +* Take two page table locks at the same level in address order such that +* we cannot deadlock. +*/ +static inline void double_pt_lock (spinlock_t *ptl1,spinlock_t *ptl2) +__acquires (ptl1) +__acquires (ptl2) +{ +if (ptl1 > ptl2) +swap (ptl1, ptl2); +spin_lock (ptl1); +if (ptl1 != ptl2) +spin_lock_nested (ptl2,SINGLE_DEPTH_NESTING ); +else +__acquire (ptl2); +} + +static inline void double_pt_unlock (spinlock_t *ptl1,spinlock_t *ptl2) +__releases (ptl1) +__releases (ptl2) +{ +spin_unlock (ptl1); +if (ptl1 != ptl2) +spin_unlock (ptl2); +else +__release (ptl2); +} +unsigned find_lock_entries (struct address_space *mapping,pgoff_t *start,pgoff_t end,struct folio_batch *fbatch,pgoff_t *indices); unsigned find_get_entries(struct address_space *mapping, pgoff_t *start,
mm/mremap.c +34 -53
153 lines not on this page · raw message
mm/userfaultfd.c +0 -27
@@ -989,33 +989,6 @@ int mwriteprotect_range(struct userfaultfd_ctx *ctx, unsigned long start, } -void double_pt_lock (spinlock_t *ptl1, -spinlock_t *ptl2) -__acquires (ptl1) -__acquires (ptl2) -{ -if (ptl1 > ptl2) -swap (ptl1, ptl2); -/* lock in virtual address order to avoid lock inversion */ -spin_lock (ptl1); -if (ptl1 != ptl2) -spin_lock_nested (ptl2,SINGLE_DEPTH_NESTING ); -else -__acquire (ptl2); -} - -void double_pt_unlock (spinlock_t *ptl1, -spinlock_t *ptl2) -__releases (ptl1) -__releases (ptl2) -{ -spin_unlock (ptl1); -if (ptl1 != ptl2) -spin_unlock (ptl2); -else -__release (ptl2); -} -static inline bool is_pte_pages_stable (pte_t *dst_pte,pte_t *src_pte,pte_t orig_dst_pte,pte_t orig_src_pte,pmd_t *dst_pmd,pmd_t dst_pmdval)
Signature · 1 line
2.53.0