FAILED: patch "[PATCH] fork: take mmap_lock before copying mm_struct" failed to apply to 6.18-stable tree
Subsystems (6)
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 ef694e2d210111cc08c52fd25da9ab130170bfa3
# <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 ef694e2d210111cc08c52fd25da9ab130170bfa3 Mon Sep 17 00:00:00 2001
From: "David Hildenbrand (Arm)" <[email protected]>
Date: Sat, 31 Oct 2026 10:39:31 +0100
Subject: [PATCH] fork: take mmap_lock before copying mm_struct
dup_mm() copies the parent's mm_struct before dup_mmap() takes the mmap_lock. A concurrent brk() can leave the child with a stale mm->brk and newer heap mappings, causing later brk() requests to fail.
Other operations (e.g., PR_SET_MM) on the parent's mm_struct can similarly cause inconsistencies.
As a simple and robust fix, grab the parent's mmap_lock earlier. This way we don't need to update individual mm fields in dup_mmap() and keep missing ones. Stop updating the 4 existing members (total_vm, data_vm, exec_vm, stack_vm), as that is now no longer required.
Keep releasing the parent's mmap_lock in dup_mmap(), before userfaultfd completion, to avoid moving the userfaultfd handling into the caller. This should get cleaned up later.
Move uprobe_start_dup_mmap() to preserve the lock ordering.
--- kernel/fork.c | 20 ++++++++++++++------ mm/mmap.c | 8 +------- mm/nommu.c | 2 +- 3 files changed, 16 insertions(+), 14 deletions(-)
kernel/fork.c +14 -6
@@ -1533,12 +1533,19 @@ static struct mm_struct *dup_mm(struct task_struct *tsk,if (!mm) goto fail_nomem; +uprobe_start_dup_mmap (); +if (mmap_write_lock_killable (oldmm)) { +free_mm (mm); + goto fail_uprobe; + } +memcpy (mm, oldmm,sizeof (*mm)); -if (!mm_init (mm, tsk)) - goto fail_nomem; +if (!mm_init (mm, tsk)) { +mmap_write_unlock (oldmm); + goto fail_uprobe; + } -uprobe_start_dup_mmap (); err =dup_mmap (mm, oldmm);if (err) goto free_pt;
@@ -1557,9 +1564,10 @@ static struct mm_struct *dup_mm(struct task_struct *tsk, mm->binfmt =NULL ;mm_init_owner (mm,NULL );mmput (mm); -if (err) -uprobe_end_dup_mmap (); - +if (!err) +return NULL ; +fail_uprobe: +uprobe_end_dup_mmap (); fail_nomem:return NULL ; }
mm/mmap.c +1 -7
@@ -1713,8 +1713,7 @@ __latent_entropy int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)LIST_HEAD (uf);VMA_ITERATOR (vmi, mm,0 ); -if (mmap_write_lock_killable (oldmm)) -return -EINTR ; +mmap_assert_write_locked (oldmm);flush_cache_dup_mm (oldmm);uprobe_dup_mmap (oldmm, mm);/*
@@ -1725,11 +1724,6 @@ __latent_entropy int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)/* No ordering required: file already has been exposed. */ dup_mm_exe_file (mm, oldmm); - mm->total_vm = oldmm->total_vm; - mm->data_vm = oldmm->data_vm; - mm->exec_vm = oldmm->exec_vm; - mm->stack_vm = oldmm->stack_vm; -/* Use __mt_dup() to efficiently build an identical maple tree. */ retval =__mt_dup (&oldmm->mm_mt, &mm->mm_mt,GFP_KERNEL );if (unlikely (retval))
mm/nommu.c +1 -1
@@ -1887,7 +1887,7 @@ subsys_initcall(init_admin_reserve);int dup_mmap (struct mm_struct *mm,struct mm_struct *oldmm) { -mmap_write_lock (oldmm); +mmap_assert_write_locked (oldmm);dup_mm_exe_file (mm, oldmm);mmap_write_unlock (oldmm);return 0 ;
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 1/1 fork: take mmap_lock before copying mm_struct
From: "David Hildenbrand (Arm)" <[email protected]>
[ Upstream commit ef694e2d210111cc08c52fd25da9ab130170bfa3 ]
dup_mm() copies the parent's mm_struct before dup_mmap() takes the mmap_lock. A concurrent brk() can leave the child with a stale mm->brk and newer heap mappings, causing later brk() requests to fail.
Other operations (e.g., PR_SET_MM) on the parent's mm_struct can similarly cause inconsistencies.
As a simple and robust fix, grab the parent's mmap_lock earlier. This way we don't need to update individual mm fields in dup_mmap() and keep missing ones. Stop updating the 4 existing members (total_vm, data_vm, exec_vm, stack_vm), as that is now no longer required.
Keep releasing the parent's mmap_lock in dup_mmap(), before userfaultfd completion, to avoid moving the userfaultfd handling into the caller. This should get cleaned up later.
Move uprobe_start_dup_mmap() to preserve the lock ordering.
--- kernel/fork.c | 20 ++++++++++++++------ mm/mmap.c | 8 +------- mm/nommu.c | 2 +- 3 files changed, 16 insertions(+), 14 deletions(-)
kernel/fork.c +14 -6
@@ -1487,12 +1487,19 @@ static struct mm_struct *dup_mm(struct task_struct *tsk,if (!mm) goto fail_nomem; +uprobe_start_dup_mmap (); +if (mmap_write_lock_killable (oldmm)) { +free_mm (mm); + goto fail_uprobe; + } +memcpy (mm, oldmm,sizeof (*mm)); -if (!mm_init (mm, tsk, mm->user_ns)) - goto fail_nomem; +if (!mm_init (mm, tsk, mm->user_ns)) { +mmap_write_unlock (oldmm); + goto fail_uprobe; + } -uprobe_start_dup_mmap (); err =dup_mmap (mm, oldmm);if (err) goto free_pt;
@@ -1511,9 +1518,10 @@ static struct mm_struct *dup_mm(struct task_struct *tsk, mm->binfmt =NULL ;mm_init_owner (mm,NULL );mmput (mm); -if (err) -uprobe_end_dup_mmap (); - +if (!err) +return NULL ; +fail_uprobe: +uprobe_end_dup_mmap (); fail_nomem:return NULL ; }
mm/mmap.c +1 -7
@@ -1724,8 +1724,7 @@ __latent_entropy int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)LIST_HEAD (uf);VMA_ITERATOR (vmi, mm,0 ); -if (mmap_write_lock_killable (oldmm)) -return -EINTR ; +mmap_assert_write_locked (oldmm);flush_cache_dup_mm (oldmm);uprobe_dup_mmap (oldmm, mm);/*
@@ -1736,11 +1735,6 @@ __latent_entropy int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)/* No ordering required: file already has been exposed. */ dup_mm_exe_file (mm, oldmm); - mm->total_vm = oldmm->total_vm; - mm->data_vm = oldmm->data_vm; - mm->exec_vm = oldmm->exec_vm; - mm->stack_vm = oldmm->stack_vm; -/* Use __mt_dup() to efficiently build an identical maple tree. */ retval =__mt_dup (&oldmm->mm_mt, &mm->mm_mt,GFP_KERNEL );if (unlikely (retval))
mm/nommu.c +1 -1
@@ -1896,7 +1896,7 @@ subsys_initcall(init_admin_reserve);int dup_mmap (struct mm_struct *mm,struct mm_struct *oldmm) { -mmap_write_lock (oldmm); +mmap_assert_write_locked (oldmm);dup_mm_exe_file (mm, oldmm);mmap_write_unlock (oldmm);return 0 ;
Signature · 1 line
2.53.0