mm/vma: drop mm asynchronously in vma_start_read()
Subsystems (7)
Lorenzo Stoakes (ARM)
maintainerPATCH 1/1 mm/vma: drop mm asynchronously in vma_start_read()
Commit bee9ef4a40a2 ("bpf: switch task_vma iterator from mmap_lock to per-VMA locks") open-codes a lock_next_vma() implementation that does roughly the same thing in bpf_iter_task_vma_find_next(), only without fallback to an mmap_lock.
This is because bpf_iter_task_vma_find_next() can be called from non-sleepable contexts.
However, lock_vma_under_rcu() gave no guarantee as to which contexts it is safe to be called from, and indeed it turns out this assumption is unsafe.
Since commit 9bbffee67ffd ("mm: fix a UAF when vma->mm is freed after vma->vm_refcnt got dropped"), this call chain is possible on x86:
lock_vma_under_rcu()
-> vma_start_read()
-> mmdrop()
-> __mmdrop()
-> mm_free_pgd()
-> pgd_free()
-> pgd_dtor()
-> spin_lock(&pgd_lock)Thus, a plain spin_lock() can occur when lock_vma_under_rcu() is called. Since bpf_iter_task_vma_find_next() can be called in softirq context, this can deadlock, for example:
pgd_alloc()
-> spin_lock(&pgd_lock)
<softirq>
bpf_iter_task_vma_next()
-> lock_vma_under_rcu()
...
-> spin_lock(&pgd_lock) [ DEADLOCK ]Alexei Starovoitov also pointed out that a BPF program attached to trace_contention_end() can similarly cause a deadlock:
pgd_alloc()
spin_lock(&pgd_lock)
queued_spin_lock_slowpath()
trace_contention_end() <- pgd_lock is held
tp_btf/contention_end prog
bpf_iter_task_vma_next()
lock_vma_under_rcu()
vma_start_read()
mmdrop(other_mm) <- last put takes pgd_lock again
Resolve the issue by having vma_start_read() use mmdrop_async() instead of mmdrop() so the work is deferred to a work queue.
Make mmdrop_async() an internal mm function, defined in mm/internal.h, to avoid abuse elsewhere (kernel/fork.c includes this header, so still retains access).
Commit 0b16f8bed19c ("mm: change vma_start_read() to drop RCU lock on failure") accounted for the fact that the RCU lock had to be dropped for this operation, which is no longer applicable.
For the sake of a backported fix, retain this behaviour, however drop the RCU lock last and remove the now irrelevant comment.
--- kernel/fork.c | 16 ---------------- mm/internal.h | 3 +++ mm/mmap_lock.c | 21 ++++++++++++++++++--- 3 files changed, 21 insertions(+), 19 deletions(-)
kernel/fork.c +0 -16
@@ -744,22 +744,6 @@ void __mmdrop(struct mm_struct *mm) }EXPORT_SYMBOL_GPL (__mmdrop); -static void mmdrop_async_fn (struct work_struct *work) -{ -struct mm_struct *mm; - - mm = container_of(work, struct mm_struct, async_put_work); -__mmdrop (mm); -} - -static void mmdrop_async (struct mm_struct *mm) -{ -if (unlikely (atomic_dec_and_test (&mm->mm_count))) { -INIT_WORK (&mm->async_put_work, mmdrop_async_fn); -schedule_work (&mm->async_put_work); - } -} -static inline void free_signal_struct (struct signal_struct *sig) {taskstats_tgid_free (sig);
mm/internal.h +3 -0
@@ -1671,4 +1671,7 @@ static inline bool can_spin_trylock(void)/* char-mem.c */ bool file_is_dev_zero (const struct file *file); +/* mmap_lock.c */ +void mmdrop_async (struct mm_struct *mm); +#endif /* __MM_INTERNAL_H */
mm/mmap_lock.c +18 -3
@@ -306,11 +306,10 @@ static struct vm_area_struct *vma_start_read(struct mm_struct *mm,*/ other_mm = vma->vm_mm;/* use a copy as vma can be freed after we drop vm_refcnt */ -/* __mmdrop() is a heavy operation, do it after dropping RCU lock. */ -rcu_read_unlock ();mmgrab (other_mm);vma_refcount_put (vma); -mmdrop (other_mm); +mmdrop_async (other_mm); +rcu_read_unlock ();return NULL ; }
@@ -560,6 +559,22 @@ void __mm_reset_vma_seqcounts(struct mm_struct *mm) }EXPORT_SYMBOL (__mm_reset_vma_seqcounts); +static void mmdrop_async_fn (struct work_struct *work) +{ +struct mm_struct *mm; + + mm = container_of(work, struct mm_struct, async_put_work); +__mmdrop (mm); +} + +void mmdrop_async (struct mm_struct *mm) +{ +if (unlikely (atomic_dec_and_test (&mm->mm_count))) { +INIT_WORK (&mm->async_put_work, mmdrop_async_fn); +schedule_work (&mm->async_put_work); + } +} +#ifdef CONFIG_LOCK_MM_AND_FIND_VMA #include <linux/extable.h>
---
base-commit: 913390e679e23e3a45643028ee7f992889094051
change-id: 20261124-vma-read-lock-mmdrop-async-ba417cd1c1f0
Cheers,
Signature · 1 line
Lorenzo Stoakes (ARM) <[email protected]>
Quoted 55 lines · Lorenzo Stoakes (ARM)
Commit
bee9ef4a40a2("bpf: switchtask_vmaiterator frommmap_lockto per-VMA locks") open-codes alock_next_vma()implementation that does roughly the same thing inbpf_iter_task_vma_find_next(), only without fallback to anmmap_lock.This is because
bpf_iter_task_vma_find_next()can be called from non-sleepable contexts.However,
lock_vma_under_rcu()gave no guarantee as to which contexts it is safe to be called from, and indeed it turns out this assumption is unsafe.Since commit
9bbffee67ffd("mm: fix a UAF whenvma->mmis freed aftervma->vm_refcntgot dropped"), this call chain is possible on x86:
lock_vma_under_rcu()
->vma_start_read()-> mmdrop() -> __mmdrop() -> mm_free_pgd() -> pgd_free() -> pgd_dtor() -> spin_lock(&pgd_lock)Thus, a plain
spin_lock()can occur whenlock_vma_under_rcu()is called. Sincebpf_iter_task_vma_find_next()can be called in softirq context, this can deadlock, for example:
pgd_alloc()
->spin_lock(&pgd_lock)<softirq> bpf_iter_task_vma_next() -> lock_vma_under_rcu() ... -> spin_lock(&pgd_lock) [ DEADLOCK ]Alexei Starovoitov also pointed out that a BPF program attached to
trace_contention_end()can similarly cause a deadlock:
pgd_alloc()
spin_lock(&pgd_lock)
queued_spin_lock_slowpath()
trace_contention_end()<-pgd_lockis held
tp_btf/contention_end prog
bpf_iter_task_vma_next()
lock_vma_under_rcu()
vma_start_read()
mmdrop(other_mm)<- last put takespgd_lockagainResolve the issue by having
vma_start_read()usemmdrop_async()instead ofmmdrop()so the work is deferred to a work queue.Make
mmdrop_async()an internal mm function, defined inmm/internal.h, to avoid abuse elsewhere (kernel/fork.cincludes this header, so still retains access).
Quoted 6 lines
Commit
0b16f8bed19c("mm: changevma_start_read()to drop RCU lock on failure") accounted for the fact that the RCU lock had to be dropped for this operation, which is no longer applicable.For the sake of a backported fix, retain this behaviour, however drop the RCU lock last and remove the now irrelevant comment.
Is there any particular reason for unlocking the RCU comment last? after the mmdrop_async().
Quoted 6 lines
Reported-by Chris Mason <[email protected]>Fixes bee9ef4a40a2 ("bpf: switch task_vma iterator from mmap_lock to per-VMA locks")Signed-off-by Lorenzo Stoakes (ARM) <[email protected]>
Signature · 1 line
Pedro
Quoted 57 lines · Pedro Falcato
Quoted 55 lines · Lorenzo Stoakes (ARM)
Commit
bee9ef4a40a2("bpf: switchtask_vmaiterator frommmap_lockto per-VMA locks") open-codes alock_next_vma()implementation that does roughly the same thing inbpf_iter_task_vma_find_next(), only without fallback to anmmap_lock.This is because
bpf_iter_task_vma_find_next()can be called from non-sleepable contexts.However,
lock_vma_under_rcu()gave no guarantee as to which contexts it is safe to be called from, and indeed it turns out this assumption is unsafe.Since commit
9bbffee67ffd("mm: fix a UAF whenvma->mmis freed aftervma->vm_refcntgot dropped"), this call chain is possible on x86:
lock_vma_under_rcu()
->vma_start_read()-> mmdrop() -> __mmdrop() -> mm_free_pgd() -> pgd_free() -> pgd_dtor() -> spin_lock(&pgd_lock)Thus, a plain
spin_lock()can occur whenlock_vma_under_rcu()is called. Sincebpf_iter_task_vma_find_next()can be called in softirq context, this can deadlock, for example:
pgd_alloc()
->spin_lock(&pgd_lock)<softirq> bpf_iter_task_vma_next() -> lock_vma_under_rcu() ... -> spin_lock(&pgd_lock) [ DEADLOCK ]Alexei Starovoitov also pointed out that a BPF program attached to
trace_contention_end()can similarly cause a deadlock:
pgd_alloc()
spin_lock(&pgd_lock)
queued_spin_lock_slowpath()
trace_contention_end()<-pgd_lockis held
tp_btf/contention_end prog
bpf_iter_task_vma_next()
lock_vma_under_rcu()
vma_start_read()
mmdrop(other_mm)<- last put takespgd_lockagainResolve the issue by having
vma_start_read()usemmdrop_async()instead ofmmdrop()so the work is deferred to a work queue.Make
mmdrop_async()an internal mm function, defined inmm/internal.h, to avoid abuse elsewhere (kernel/fork.cincludes this header, so still retains access).
Quoted 8 lines
Quoted 6 lines
Commit
0b16f8bed19c("mm: changevma_start_read()to drop RCU lock on failure") accounted for the fact that the RCU lock had to be dropped for this operation, which is no longer applicable.For the sake of a backported fix, retain this behaviour, however drop the RCU lock last and remove the now irrelevant comment.
Is there any particular reason for unlocking the RCU comment last? after the
mmdrop_async().
Yeah, I think moving the RCU unlocking was unnecessary (unlock as soon as possible is in general advisable) and we could just remove the comment. Both versions however seem correct to me.
Quoted 9 lines
Quoted 7 lines
Reported-by Chris Mason <[email protected]>Fixes bee9ef4a40a2 ("bpf: switch task_vma iterator from mmap_lock to per-VMA locks")Signed-off-by Lorenzo Stoakes (ARM) <[email protected]>Acked-by Pedro Falcato <[email protected]>
Quoted 3 lines
--
Pedro
Suren Baghdasaryan
maintainerQuoted 63 lines · Suren Baghdasaryan
Quoted 61 lines · Pedro Falcato
Quoted 59 lines · Lorenzo Stoakes (ARM)
Commit
bee9ef4a40a2("bpf: switchtask_vmaiterator frommmap_lockto per-VMA locks") open-codes alock_next_vma()implementation that does roughly the same thing inbpf_iter_task_vma_find_next(), only without fallback to anmmap_lock.This is because
bpf_iter_task_vma_find_next()can be called from non-sleepable contexts.However,
lock_vma_under_rcu()gave no guarantee as to which contexts it is safe to be called from, and indeed it turns out this assumption is unsafe.Since commit
9bbffee67ffd("mm: fix a UAF whenvma->mmis freed aftervma->vm_refcntgot dropped"), this call chain is possible on x86:
lock_vma_under_rcu()
->vma_start_read()-> mmdrop() -> __mmdrop() -> mm_free_pgd() -> pgd_free() -> pgd_dtor() -> spin_lock(&pgd_lock)Thus, a plain
spin_lock()can occur whenlock_vma_under_rcu()is called. Sincebpf_iter_task_vma_find_next()can be called in softirq context, this can deadlock, for example:
pgd_alloc()
->spin_lock(&pgd_lock)<softirq> bpf_iter_task_vma_next() -> lock_vma_under_rcu() ... -> spin_lock(&pgd_lock) [ DEADLOCK ]Alexei Starovoitov also pointed out that a BPF program attached to
trace_contention_end()can similarly cause a deadlock:
pgd_alloc()
spin_lock(&pgd_lock)
queued_spin_lock_slowpath()
trace_contention_end()<-pgd_lockis held
tp_btf/contention_end prog
bpf_iter_task_vma_next()
lock_vma_under_rcu()
vma_start_read()
mmdrop(other_mm)<- last put takespgd_lockagainResolve the issue by having
vma_start_read()usemmdrop_async()instead ofmmdrop()so the work is deferred to a work queue.Make
mmdrop_async()an internal mm function, defined inmm/internal.h, to avoid abuse elsewhere (kernel/fork.cincludes this header, so still retains access).Commit
0b16f8bed19c("mm: changevma_start_read()to drop RCU lock on failure") accounted for the fact that the RCU lock had to be dropped for this operation, which is no longer applicable.
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
For the sake of a backported fix, retain this behaviour, however drop the RCU lock last and remove the now irrelevant comment.
Is there any particular reason for unlocking the RCU comment last? after the
mmdrop_async().Yeah, I think moving the RCU unlocking was unnecessary (unlock as soon as possible is in general advisable) and we could just remove the comment. Both versions however seem correct to me.
Actually, VMAs are allocated from a SLAB_TYPESAFE_BY_RCU slub. With that RCU unlocking move you don't need other_mm copy and the comment that comes with it. VMA won't be freed until RCU lock is dropped.
Quoted 16 lines
Quoted 10 lines
Quoted 8 lines
Reported-by Chris Mason <[email protected]>Fixes bee9ef4a40a2 ("bpf: switch task_vma iterator from mmap_lock to per-VMA locks")Signed-off-by Lorenzo Stoakes (ARM) <[email protected]>Acked-by Pedro Falcato <[email protected]>Reviewed-by Suren Baghdasaryan <[email protected]>Quoted 3 lines
--
Pedro
Lorenzo Stoakes (ARM)
maintainerQuoted 3 lines · Suren Baghdasaryan
Actually, VMAs are allocated from a
SLAB_TYPESAFE_BY_RCUslub. With that RCU unlocking move you don't needother_mmcopy and the comment that comes with it. VMA won't be freed until RCU lock is dropped.
Yeah but they can be reassigned? They won't be _freed_ until a grace period but they can go back to the slab and then allocated elsewhere right?
--
Cheers, Lorenzo
Quoted 9 lines · Lorenzo Stoakes (ARM)
Quoted 3 lines · Suren Baghdasaryan
Actually, VMAs are allocated from a
SLAB_TYPESAFE_BY_RCUslub. With that RCU unlocking move you don't needother_mmcopy and the comment that comes with it. VMA won't be freed until RCU lock is dropped.Yeah but they can be reassigned? They won't be _freed_ until a grace period but they can go back to the slab and then allocated elsewhere right?
Yes, they can go back to the slab and become a different VMA.
Suren Baghdasaryan
maintainerQuoted 8 lines · Puranjay Mohan
Quoted 6 lines · Lorenzo Stoakes (ARM)
Quoted 3 lines · Suren Baghdasaryan
Actually, VMAs are allocated from a
SLAB_TYPESAFE_BY_RCUslub. With that RCU unlocking move you don't needother_mmcopy and the comment that comes with it. VMA won't be freed until RCU lock is dropped.
Quoted 5 lines
Quoted 3 lines
Yeah but they can be reassigned? They won't be _freed_ until a grace period but they can go back to the slab and then allocated elsewhere right?
Yes, they can go back to the slab and become a different VMA.
Yeah, you are right about that. So, we either have to keep the RCU unlock in place to keep other_mm comment valid (my personal preference) or update the comment next to other_mm to explain the new reason it is needed. Either way is fine with me.
Lorenzo Stoakes (ARM)
maintainerQuoted 13 lines · Suren Baghdasaryan
Quoted 12 lines · Puranjay Mohan
Quoted 9 lines · Lorenzo Stoakes (ARM)
Quoted 3 lines · Suren Baghdasaryan
Actually, VMAs are allocated from a
SLAB_TYPESAFE_BY_RCUslub. With that RCU unlocking move you don't needother_mmcopy and the comment that comes with it. VMA won't be freed until RCU lock is dropped.Yeah but they can be reassigned? They won't be _freed_ until a grace period but they can go back to the slab and then allocated elsewhere right?
Quoted 6 lines
Quoted 1 line
Yes, they can go back to the slab and become a different VMA.
Yeah, you are right about that. So, we either have to keep the RCU unlock in place to keep
other_mmcomment valid (my personal preference) or update the comment next toother_mmto explain the new reason it is needed. Either way is fine with me.
I think let's just change the comment?
Andrew could you change:
* stable and can be freed in the narrow window after vma->vm_refcnt
->
* stable and can be reused in the narrow window after vma->vm_refcnt ^ | just this freed -> reused :)
Thanks!
--
Cheers, Lorenzo
Andrew Morton
maintainerQuoted 25 lines · On Tue, 24 Nov 2026 17:23:35 +0000 "Lorenzo Stoakes (ARM)" <[email protected]>…
Quoted 13 lines · Suren Baghdasaryan
Quoted 12 lines · Puranjay Mohan
Quoted 9 lines · Lorenzo Stoakes (ARM)
Quoted 3 lines · Suren Baghdasaryan
Actually, VMAs are allocated from a
SLAB_TYPESAFE_BY_RCUslub. With that RCU unlocking move you don't needother_mmcopy and the comment that comes with it. VMA won't be freed until RCU lock is dropped.Yeah but they can be reassigned? They won't be _freed_ until a grace period but they can go back to the slab and then allocated elsewhere right?
Quoted 6 lines
Quoted 1 line
Yes, they can go back to the slab and become a different VMA.
Yeah, you are right about that. So, we either have to keep the RCU unlock in place to keep
other_mmcomment valid (my personal preference) or update the comment next toother_mmto explain the new reason it is needed. Either way is fine with me.I think let's just change the comment?
Andrew could you change:
Quoted 9 lines
* stable and can be freed in the narrow window after
vma->vm_refcnt->
* stable and can be reused in the narrow window after vma->vm_refcnt ^ | just this freed -> reused :)
np.
mm/mmap_lock.c renamed from mm/mmap_lock.c~mm-vma-drop-mm-asynchronously-in-vma_start_read-fix +1 -1
@@ -300,7 +300,7 @@ err: err_unstable:/* * If vma got attached to another mm from under us, that mm is not -* stable and can be freed in the narrow window after vma->vm_refcnt +* stable and can be reused in the narrow window after vma->vm_refcnt * is dropped and before rcuwait_wake_up(mm) is called. Grab it before * releasing vma->vm_refcnt. */
_
Suren Baghdasaryan
maintainerQuoted 43 lines · Andrew Morton
Quoted 25 lines · On Tue, 24 Nov 2026 17:23:35 +0000 "Lorenzo Stoakes (ARM)" <[email protected]>…
Quoted 13 lines · Suren Baghdasaryan
Quoted 12 lines · Puranjay Mohan
Quoted 9 lines · Lorenzo Stoakes (ARM)
Quoted 3 lines · Suren Baghdasaryan
Actually, VMAs are allocated from a
SLAB_TYPESAFE_BY_RCUslub. With that RCU unlocking move you don't needother_mmcopy and the comment that comes with it. VMA won't be freed until RCU lock is dropped.Yeah but they can be reassigned? They won't be _freed_ until a grace period but they can go back to the slab and then allocated elsewhere right?
Quoted 6 lines
Quoted 1 line
Yes, they can go back to the slab and become a different VMA.
Yeah, you are right about that. So, we either have to keep the RCU unlock in place to keep
other_mmcomment valid (my personal preference) or update the comment next toother_mmto explain the new reason it is needed. Either way is fine with me.I think let's just change the comment?
Andrew could you change:
Quoted 9 lines
* stable and can be freed in the narrow window after
vma->vm_refcnt->
* stable and can be reused in the narrow window after vma->vm_refcnt ^ | just this freed -> reused :)np.
mm/mmap_lock.c err: · line 306
Quoted 8 lines
mm/mmap_lock.c renamed from mm/mmap_lock.c~mm-vma-drop-mm-asynchronously-in-vma_start_read-fix +1
-1@@ -300,7 +300,7 @@ err: err_unstable:/* * If vma got attached to another mm from under us, that mm is not -* stable and can be freed in the narrow window after vma->vm_refcnt +* stable and can be reused in the narrow window after vma->vm_refcnt * is dropped and before rcuwait_wake_up(mm) is called. Grab it before * releasing vma->vm_refcnt. */
Sorry for being pedantic :)
There is also this comment later on:
other_mm = vma->vm_mm; /* use a copy as vma can be freed after
we drop vm_refcnt */
That should be replaced too with:
other_mm = vma->vm_mm; /* use a copy as vma can be reused
after we drop vm_refcnt */
Again, just replacing "freed" to "reused".
Thanks!
Quoted 2 lines
_
Vlastimil Babka (SUSE)
reviewerQuoted 41 lines · Suren Baghdasaryan
Quoted 40 lines · Andrew Morton
Quoted 25 lines · On Tue, 24 Nov 2026 17:23:35 +0000 "Lorenzo Stoakes (ARM)" <[email protected]>…
Quoted 13 lines · Suren Baghdasaryan
Quoted 12 lines · Puranjay Mohan
Quoted 9 lines · Lorenzo Stoakes (ARM)
Quoted 3 lines · Suren Baghdasaryan
Actually, VMAs are allocated from a
SLAB_TYPESAFE_BY_RCUslub. With that RCU unlocking move you don't needother_mmcopy and the comment that comes with it. VMA won't be freed until RCU lock is dropped.Yeah but they can be reassigned? They won't be _freed_ until a grace period but they can go back to the slab and then allocated elsewhere right?
Quoted 6 lines
Quoted 1 line
Yes, they can go back to the slab and become a different VMA.
Yeah, you are right about that. So, we either have to keep the RCU unlock in place to keep
other_mmcomment valid (my personal preference) or update the comment next toother_mmto explain the new reason it is needed. Either way is fine with me.I think let's just change the comment?
Andrew could you change:
Quoted 9 lines
* stable and can be freed in the narrow window after
vma->vm_refcnt->
* stable and can be reused in the narrow window after vma->vm_refcnt ^ | just this freed -> reused :)np.
Quoted 8 lines
mm/mmap_lock.c err: · line 303
Quoted 8 lines
mm/mmap_lock.c renamed from mm/mmap_lock.c~mm-vma-drop-mm-asynchronously-in-vma_start_read-fix +1
-1@@ -300,7 +300,7 @@ err: err_unstable:/* * If vma got attached to another mm from under us, that mm is not -* stable and can be freed in the narrow window after vma->vm_refcnt +* stable and can be reused in the narrow window after vma->vm_refcnt
But this is talking about the mm, not the vma, so the motivation to change that was wrong? (it's not a typesafe cache). Moreover...
Quoted 8 lines
mm/mmap_lock.c err: · line 306
Quoted 3 lines
mm/mmap_lock.c renamed from mm/mmap_lock.c~mm-vma-drop-mm-asynchronously-in-vma_start_read-fix +0
-0* is dropped and before rcuwait_wake_up(mm) is called. Grab it before * releasing vma->vm_refcnt. */ Sorry for being pedantic :)
There is also this comment later on:other_mm = vma->vm_mm; /* use a copy as vma can be freed after
Quoted 8 lines
we drop
vm_refcnt*/That should be replaced too with:
other_mm = vma->vm_mm; /* use a copy as vma can be reusedafter we drop
vm_refcnt*/Again, just replacing "freed" to "reused".
Nah let's not overthink this. It's freed back to the allocator first. Which naturally allows it to be reused, yes. It doesn't matter if the underlying page can be freed or not. UAF is a UAF.
Quoted 4 lines
Thanks!
Quoted 2 lines
_
Lorenzo Stoakes (ARM)
maintainerQuoted 66 lines · Vlastimil Babka (SUSE)
Quoted 41 lines · Suren Baghdasaryan
Quoted 40 lines · Andrew Morton
Quoted 25 lines · On Tue, 24 Nov 2026 17:23:35 +0000 "Lorenzo Stoakes (ARM)" <[email protected]>…
Quoted 13 lines · Suren Baghdasaryan
Quoted 12 lines · Puranjay Mohan
Quoted 9 lines · Lorenzo Stoakes (ARM)
Quoted 3 lines · Suren Baghdasaryan
Actually, VMAs are allocated from a
SLAB_TYPESAFE_BY_RCUslub. With that RCU unlocking move you don't needother_mmcopy and the comment that comes with it. VMA won't be freed until RCU lock is dropped.Yeah but they can be reassigned? They won't be _freed_ until a grace period but they can go back to the slab and then allocated elsewhere right?
Quoted 6 lines
Quoted 1 line
Yes, they can go back to the slab and become a different VMA.
Yeah, you are right about that. So, we either have to keep the RCU unlock in place to keep
other_mmcomment valid (my personal preference) or update the comment next toother_mmto explain the new reason it is needed. Either way is fine with me.I think let's just change the comment?
Andrew could you change:
Quoted 9 lines
* stable and can be freed in the narrow window after
vma->vm_refcnt->
* stable and can be reused in the narrow window after vma->vm_refcnt ^ | just this freed -> reused :)np.
Quoted 8 lines
mm/mmap_lock.c err: · line 303
Quoted 8 lines
mm/mmap_lock.c renamed from mm/mmap_lock.c~mm-vma-drop-mm-asynchronously-in-vma_start_read-fix +1
-1@@ -300,7 +300,7 @@ err: err_unstable:/* * If vma got attached to another mm from under us, that mm is not -* stable and can be freed in the narrow window after vma->vm_refcnt +* stable and can be reused in the narrow window after vma->vm_refcnt But this is talking about the mm, not the vma, so the motivation to change that was wrong? (it's not a typesafe cache). Moreover...
Quoted 12 lines
mm/mmap_lock.c err: · line 306
Quoted 3 lines
mm/mmap_lock.c renamed from mm/mmap_lock.c~mm-vma-drop-mm-asynchronously-in-vma_start_read-fix +0
-0* is dropped and before rcuwait_wake_up(mm) is called. Grab it before * releasing vma->vm_refcnt. */ Sorry for being pedantic :)
There is also this comment later on:other_mm = vma->vm_mm; /* use a copy as vma can be freed afterwe drop
vm_refcnt*/That should be replaced too with:
Quoted 8 lines
Quoted 4 lines
other_mm = vma->vm_mm; /* use a copy as vma can be reusedafter we drop
vm_refcnt*/Again, just replacing "freed" to "reused".
Nah let's not overthink this. It's freed back to the allocator first. Which naturally allows it to be reused, yes. It doesn't matter if the underlying page can be freed or not. UAF is a UAF.
Let me just send a v2 that puts the rcu_read_unlock() back where it was, I don't really want to get into some long debate about nitpicky details. I just have too much to do sorry.
--
Cheers, Lorenzo
[folded-merged] mm-vma-drop-mm-asynchronously-in-vma_start_read-fix-2.patch removed from -mm tree
In reply to Suren BaghdasaryanThe quilt patch titled Subject: mm-vma-drop-mm-asynchronously-in-vma_start_read-fix-2 has been removed from the -mm tree. Its filename was mm-vma-drop-mm-asynchronously-in-vma_start_read-fix-2.patch
The quilt patch titled
Subject: mm-vma-drop-mm-asynchronously-in-vma_start_read-fix-2
has been removed from the -mm tree. Its filename was
mm-vma-drop-mm-asynchronously-in-vma_start_read-fix-2.patch
This patch was dropped because it was folded into mm-vma-drop-mm-asynchronously-in-vma_start_read.patch
------------------------------------------------------
From: Andrew Morton <[email protected]>
Subject: mm-vma-drop-mm-asynchronously-in-vma_start_read-fix-2
Date: Tue Nov 24 02:50:40 PM PST 2026
comment fixup, per Suren
mm/mmap_lock.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
mm/mmap_lock.c renamed from mm/mmap_lock.c~mm-vma-drop-mm-asynchronously-in-vma_start_read-fix-2 +1 -1
@@ -304,7 +304,7 @@ err_unstable:* is dropped and before rcuwait_wake_up(mm) is called. Grab it before * releasing vma->vm_refcnt. */ - other_mm = vma->vm_mm;/* use a copy as vma can be freed after we drop vm_refcnt */ + other_mm = vma->vm_mm;/* use a copy as vma can be reused after we drop vm_refcnt */ mmgrab (other_mm);vma_refcount_put (vma);
_
Patches currently in -mm which might be from [email protected] are
mm-vma-drop-mm-asynchronously-in-vma_start_read.patch kselftest-mm-rename-read_pmd_pagesize-to-pmd_psize-fix.patch selftests-mm-check-madv_cold-and-madv_pageout-on-partly-advised-thps-fix.patch selftests-mm-add-uffdio_move-huge-zeropage-pmd-regression-test-fix.patch selftests-mm-allocate-nr_pages-based-on-pmd-size-for-uffd-pmd_move-tests-fix.patch selftests-mm-retain-move-pmd-coverage-and-verify-huge-pmds.patch lib-test_xarray-check-unused-spare-node-after-range-creation.patch mm-memory-failure-dont-unpoison-a-leaked-raw_hwp_unreliable-folio-fix.patch mm-hugetlb-delay-anon-folio-rmap-removal-until-after-the-tlb-flush-fix.patch mm-hugetlb-delay-anon-folio-rmap-removal-until-after-the-tlb-flush-fix-2.patch mm-vmstat_kunit-add-synthetic-benchmark-for-mm-stats-fix.patch mm-prevent-pre-content-bypass-through-mremap-and-remap_file_pages.patch afs-do-not-put-error-pointer-after-address-lookup-failure.patch selftests-mm-check-that-fsync-reports-hard-poisoned-dirty-page-cache.patch selftests-mm-memory-failure-truncate-poisoned-fuse-page-cache-before-teardown.patch selftests-mm-memory-failure-skip-poisoned-dirty-page-cache-on-nilfs2.patch selftest-mm-memory-failure-keep-page-cache-files-linked-until-cleanup.patch selftest-mm-memory-failure-keep-page-cache-files-linked-until-cleanup-fix.patch selftests-mm-memory-failure-skip-hard-dirty-page-cache-test-on-cephfs.patch selftests-mm-memory-failure-close-dirty-page-cache-files-before-unlinking-them.patch selftests-mm-memory-failure-extend-page-cache-test-files-with-pwrite.patch selftests-mm-memory-failure-write-the-whole-page-cache-test-file-for-omfs.patch selftests-mm-memory-failure-accept-eio-from-close-after-poisoning-dirty-data.patch alloc_tag-introduce-ioctls-to-toggle-allocation-tracepoints-fix-fix.patch alloc_tag-introduce-ioctls-to-toggle-allocation-tracepoints-fix.patch mm-vmscan-scope-freeze-writeback-skip-to-the-reclaim-target.patch mm-vmscan-scope-freeze-writeback-skip-to-the-reclaim-target-fix.patch mm-hmm-refuse-secretmem-vmas-in-hmm_range_fault-fix.patch mm-huge_memory-set-memcg_data-before-a-split-page-leaves-the-folio-fix.patch ocfs2-unregister-the-jbd2-shrinker-before-journal-shutdown.patch ipc-msg-clamp-negative-msg_info-usage-counts.patch squashfs-fix-leaked-page-locks-when-page-actor-allocation-fails-fix.patch ocfs2-skip-network-flush-when-no-domain-handlers-are-registered.patch selftests-ptrace-check-that-ptrace_set_syscall_info-rejects-invalid-error-values-fix.patch lib-atomic64-separate-hashed-locks-by-smp-cache-line.patch nvdimm-btt-separate-map-locks-by-smp-cache-line.patch percpu_ref-replace-the-global-switch-lock-with-hashed-locks-fix.patch drivers-media-v4l2-core-v4l2-vp9c-reduce-inlining.patch
+ mm-vma-drop-mm-asynchronously-in-vma_start_read-fix.patch added to mm-hotfixes-unstable branch
In reply to Lorenzo Stoakes (ARM)The patch titled Subject: mm-vma-drop-mm-asynchronously-in-vma_start_read-fix has been added to the -mm mm-hotfixes-unstable branch. Its filename is…
The patch titled
Subject: mm-vma-drop-mm-asynchronously-in-vma_start_read-fix
has been added to the -mm mm-hotfixes-unstable branch. Its filename is
mm-vma-drop-mm-asynchronously-in-vma_start_read-fix.patch
This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-vma-drop-mm-asynchronously-in-vma_start_read-fix.patch
This patch will later appear in the mm-hotfixes-unstable branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's
*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***
The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days
------------------------------------------------------
From: Andrew Morton <[email protected]>
Subject: mm-vma-drop-mm-asynchronously-in-vma_start_read-fix
Date: Tue Nov 24 09:31:33 AM PST 2026
tweak comment, per Lorenzo
mm/mmap_lock.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
mm/mmap_lock.c renamed from mm/mmap_lock.c~mm-vma-drop-mm-asynchronously-in-vma_start_read-fix +1 -1
@@ -300,7 +300,7 @@ err: err_unstable:/* * If vma got attached to another mm from under us, that mm is not -* stable and can be freed in the narrow window after vma->vm_refcnt +* stable and can be reused in the narrow window after vma->vm_refcnt * is dropped and before rcuwait_wake_up(mm) is called. Grab it before * releasing vma->vm_refcnt. */
_
Patches currently in -mm which might be from [email protected] are
mm-vma-drop-mm-asynchronously-in-vma_start_read-fix.patch kselftest-mm-rename-read_pmd_pagesize-to-pmd_psize-fix.patch selftests-mm-check-madv_cold-and-madv_pageout-on-partly-advised-thps-fix.patch alloc_tag-introduce-ioctls-to-toggle-allocation-tracepoints-fix-fix.patch alloc_tag-introduce-ioctls-to-toggle-allocation-tracepoints-fix.patch selftests-mm-add-uffdio_move-huge-zeropage-pmd-regression-test-fix.patch selftests-mm-allocate-nr_pages-based-on-pmd-size-for-uffd-pmd_move-tests-fix.patch selftests-mm-retain-move-pmd-coverage-and-verify-huge-pmds.patch lib-test_xarray-check-unused-spare-node-after-range-creation.patch mm-memory-failure-dont-unpoison-a-leaked-raw_hwp_unreliable-folio-fix.patch mm-vmstat_kunit-add-synthetic-benchmark-for-mm-stats-fix.patch lib-test_hmm-invalidate-all-mirrors-on-device-private-migration.patch lib-test_hmm-invalidate-all-mirrors-on-device-private-migration-fix.patch mm-prevent-pre-content-bypass-through-mremap-and-remap_file_pages.patch afs-do-not-put-error-pointer-after-address-lookup-failure.patch selftests-mm-check-that-fsync-reports-hard-poisoned-dirty-page-cache.patch selftests-mm-check-that-fsync-reports-hard-poisoned-dirty-page-cache-fix-0.patch selftests-mm-check-that-fsync-reports-hard-poisoned-dirty-page-cache-fix-1.patch selftests-mm-check-that-fsync-reports-hard-poisoned-dirty-page-cache-fix-2.patch selftests-mm-check-that-fsync-reports-hard-poisoned-dirty-page-cache-fix-3.patch selftests-mm-check-that-fsync-reports-hard-poisoned-dirty-page-cache-fix-4.patch selftests-mm-check-that-fsync-reports-hard-poisoned-dirty-page-cache-fix-5.patch selftests-mm-check-that-fsync-reports-hard-poisoned-dirty-page-cache-fix-6.patch selftests-mm-check-that-fsync-reports-hard-poisoned-dirty-page-cache-fix-7.patch selftests-mm-hmm-speed-up-migrate_remap_fault.patch ocfs2-unregister-the-jbd2-shrinker-before-journal-shutdown.patch ipc-msg-clamp-negative-msg_info-usage-counts.patch squashfs-fix-leaked-page-locks-when-page-actor-allocation-fails-fix.patch squashfs-dont-compute-start-or-alloc-memory-on-empty-directory-or-index-fix.patch ocfs2-skip-network-flush-when-no-domain-handlers-are-registered.patch selftests-ptrace-check-that-ptrace_set_syscall_info-rejects-invalid-error-values-fix.patch lib-asn1_encoder-fix-encoding-of-zero-oid-components.patch proc-serialize-task-owner-updates.patch drivers-media-v4l2-core-v4l2-vp9c-reduce-inlining.patch
[folded-merged] mm-vma-drop-mm-asynchronously-in-vma_start_read-fix.patch removed from -mm tree
In reply to Lorenzo Stoakes (ARM)The quilt patch titled Subject: mm-vma-drop-mm-asynchronously-in-vma_start_read-fix has been removed from the -mm tree. Its filename was mm-vma-drop-mm-asynchronously-in-vma_start_read-fix.patch
The quilt patch titled
Subject: mm-vma-drop-mm-asynchronously-in-vma_start_read-fix
has been removed from the -mm tree. Its filename was
mm-vma-drop-mm-asynchronously-in-vma_start_read-fix.patch
This patch was dropped because it was folded into mm-vma-drop-mm-asynchronously-in-vma_start_read.patch
------------------------------------------------------
From: Andrew Morton <[email protected]>
Subject: mm-vma-drop-mm-asynchronously-in-vma_start_read-fix
Date: Tue Nov 24 09:31:33 AM PST 2026
tweak comment, per Lorenzo
mm/mmap_lock.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
mm/mmap_lock.c renamed from mm/mmap_lock.c~mm-vma-drop-mm-asynchronously-in-vma_start_read-fix +1 -1
@@ -300,7 +300,7 @@ err: err_unstable:/* * If vma got attached to another mm from under us, that mm is not -* stable and can be freed in the narrow window after vma->vm_refcnt +* stable and can be reused in the narrow window after vma->vm_refcnt * is dropped and before rcuwait_wake_up(mm) is called. Grab it before * releasing vma->vm_refcnt. */
_
Patches currently in -mm which might be from [email protected] are
mm-vma-drop-mm-asynchronously-in-vma_start_read.patch mm-vma-drop-mm-asynchronously-in-vma_start_read-fix-2.patch kselftest-mm-rename-read_pmd_pagesize-to-pmd_psize-fix.patch selftests-mm-check-madv_cold-and-madv_pageout-on-partly-advised-thps-fix.patch selftests-mm-add-uffdio_move-huge-zeropage-pmd-regression-test-fix.patch selftests-mm-allocate-nr_pages-based-on-pmd-size-for-uffd-pmd_move-tests-fix.patch selftests-mm-retain-move-pmd-coverage-and-verify-huge-pmds.patch lib-test_xarray-check-unused-spare-node-after-range-creation.patch mm-memory-failure-dont-unpoison-a-leaked-raw_hwp_unreliable-folio-fix.patch mm-hugetlb-delay-anon-folio-rmap-removal-until-after-the-tlb-flush-fix.patch mm-hugetlb-delay-anon-folio-rmap-removal-until-after-the-tlb-flush-fix-2.patch mm-vmstat_kunit-add-synthetic-benchmark-for-mm-stats-fix.patch mm-prevent-pre-content-bypass-through-mremap-and-remap_file_pages.patch afs-do-not-put-error-pointer-after-address-lookup-failure.patch selftests-mm-check-that-fsync-reports-hard-poisoned-dirty-page-cache.patch selftests-mm-memory-failure-truncate-poisoned-fuse-page-cache-before-teardown.patch selftests-mm-memory-failure-skip-poisoned-dirty-page-cache-on-nilfs2.patch selftest-mm-memory-failure-keep-page-cache-files-linked-until-cleanup.patch selftest-mm-memory-failure-keep-page-cache-files-linked-until-cleanup-fix.patch selftests-mm-memory-failure-skip-hard-dirty-page-cache-test-on-cephfs.patch selftests-mm-memory-failure-close-dirty-page-cache-files-before-unlinking-them.patch selftests-mm-memory-failure-extend-page-cache-test-files-with-pwrite.patch selftests-mm-memory-failure-write-the-whole-page-cache-test-file-for-omfs.patch selftests-mm-memory-failure-accept-eio-from-close-after-poisoning-dirty-data.patch alloc_tag-introduce-ioctls-to-toggle-allocation-tracepoints-fix-fix.patch alloc_tag-introduce-ioctls-to-toggle-allocation-tracepoints-fix.patch mm-vmscan-scope-freeze-writeback-skip-to-the-reclaim-target.patch mm-vmscan-scope-freeze-writeback-skip-to-the-reclaim-target-fix.patch mm-hmm-refuse-secretmem-vmas-in-hmm_range_fault-fix.patch mm-huge_memory-set-memcg_data-before-a-split-page-leaves-the-folio-fix.patch ocfs2-unregister-the-jbd2-shrinker-before-journal-shutdown.patch ipc-msg-clamp-negative-msg_info-usage-counts.patch squashfs-fix-leaked-page-locks-when-page-actor-allocation-fails-fix.patch ocfs2-skip-network-flush-when-no-domain-handlers-are-registered.patch selftests-ptrace-check-that-ptrace_set_syscall_info-rejects-invalid-error-values-fix.patch lib-atomic64-separate-hashed-locks-by-smp-cache-line.patch nvdimm-btt-separate-map-locks-by-smp-cache-line.patch percpu_ref-replace-the-global-switch-lock-with-hashed-locks-fix.patch drivers-media-v4l2-core-v4l2-vp9c-reduce-inlining.patch
Lorenzo Stoakes (ARM)
maintainerQuoted 6 lines · Suren Baghdasaryan
Quoted 1 line · Pedro Falcato
Is there any particular reason for unlocking the RCU comment last? after the
mmdrop_async().Yeah, I think moving the RCU unlocking was unnecessary (unlock as soon as possible is in general advisable) and we could just remove the comment. Both versions however seem correct to me.
For simplicity. 'The operation is done under an RCU lock' :)
I alluded above, but intent is to revert 0b16f8bed19c which I _think_ is fine. And at that point it actively simplifies.
The operations done there are only done in a rare scenario (VMA recycled) and now the extra bits done under RCU there are really quite small so I think fine.
I will follow up on that later.
Quoted 3 lines
Quoted 1 line
Acked-by Pedro Falcato <[email protected]>Reviewed-by Suren Baghdasaryan <[email protected]>
Thanks x2!
Quoted 4 lines
Quoted 4 lines
--
Pedro
--
Cheers, Lorenzo
+ mm-vma-drop-mm-asynchronously-in-vma_start_read.patch added to mm-hotfixes-unstable branch
In reply to Lorenzo Stoakes (ARM)The patch titled Subject: mm/vma: drop mm asynchronously in vma_start_read() has been added to the -mm mm-hotfixes-unstable branch. Its filename is…
The patch titled
Subject: mm/vma: drop mm asynchronously in vma_start_read()
has been added to the -mm mm-hotfixes-unstable branch. Its filename is
mm-vma-drop-mm-asynchronously-in-vma_start_read.patch
This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-vma-drop-mm-asynchronously-in-vma_start_read.patch
This patch will later appear in the mm-hotfixes-unstable branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's
*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***
The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days
------------------------------------------------------
From: "Lorenzo Stoakes (ARM)" <[email protected]>
Subject: mm/vma: drop mm asynchronously in vma_start_read()
Date: Tue, 24 Nov 2026 12:26:19 +0000
Commit bee9ef4a40a2 ("bpf: switch task_vma iterator from mmap_lock to per-VMA locks") open-codes a lock_next_vma() implementation that does roughly the same thing in bpf_iter_task_vma_find_next(), only without fallback to an mmap_lock.
This is because bpf_iter_task_vma_find_next() can be called from non-sleepable contexts.
However, lock_vma_under_rcu() gave no guarantee as to which contexts it is safe to be called from, and indeed it turns out this assumption is unsafe.
Since commit 9bbffee67ffd ("mm: fix a UAF when vma->mm is freed after vma->vm_refcnt got dropped"), this call chain is possible on x86:
lock_vma_under_rcu()
-> vma_start_read()
-> mmdrop()
-> __mmdrop()
-> mm_free_pgd()
-> pgd_free()
-> pgd_dtor()
-> spin_lock(&pgd_lock)Thus, a plain spin_lock() can occur when lock_vma_under_rcu() is called. Since bpf_iter_task_vma_find_next() can be called in softirq context, this can deadlock, for example:
pgd_alloc()
-> spin_lock(&pgd_lock)
<softirq>
bpf_iter_task_vma_next()
-> lock_vma_under_rcu()
...
-> spin_lock(&pgd_lock) [ DEADLOCK ]Alexei Starovoitov also pointed out that a BPF program attached to trace_contention_end() can similarly cause a deadlock:
pgd_alloc()
spin_lock(&pgd_lock)
queued_spin_lock_slowpath()
trace_contention_end() <- pgd_lock is held
tp_btf/contention_end prog
bpf_iter_task_vma_next()
lock_vma_under_rcu()
vma_start_read()
mmdrop(other_mm) <- last put takes pgd_lock again
Resolve the issue by having vma_start_read() use mmdrop_async() instead of mmdrop() so the work is deferred to a work queue.
Make mmdrop_async() an internal mm function, defined in mm/internal.h, to avoid abuse elsewhere (kernel/fork.c includes this header, so still retains access).
Commit 0b16f8bed19c ("mm: change vma_start_read() to drop RCU lock on failure") accounted for the fact that the RCU lock had to be dropped for this operation, which is no longer applicable.
For the sake of a backported fix, retain this behaviour, however drop the RCU lock last and remove the now irrelevant comment.
kernel/fork.c | 16 ---------------- mm/internal.h | 3 +++ mm/mmap_lock.c | 21 ++++++++++++++++++--- 3 files changed, 21 insertions(+), 19 deletions(-)
kernel/fork.c renamed from kernel/fork.c~mm-vma-drop-mm-asynchronously-in-vma_start_read +0 -16
@@ -744,22 +744,6 @@ void __mmdrop(struct mm_struct *mm) }EXPORT_SYMBOL_GPL (__mmdrop); -static void mmdrop_async_fn (struct work_struct *work) -{ -struct mm_struct *mm; - - mm = container_of(work, struct mm_struct, async_put_work); -__mmdrop (mm); -} - -static void mmdrop_async (struct mm_struct *mm) -{ -if (unlikely (atomic_dec_and_test (&mm->mm_count))) { -INIT_WORK (&mm->async_put_work, mmdrop_async_fn); -schedule_work (&mm->async_put_work); - } -} -static inline void free_signal_struct (struct signal_struct *sig) {taskstats_tgid_free (sig);
mm/internal.h renamed from mm/internal.h~mm-vma-drop-mm-asynchronously-in-vma_start_read +3 -0
@@ -1691,4 +1691,7 @@ static inline bool can_spin_trylock(void/* char-mem.c */ bool file_is_dev_zero (const struct file *file); +/* mmap_lock.c */ +void mmdrop_async (struct mm_struct *mm); +#endif /* __MM_INTERNAL_H */
mm/mmap_lock.c renamed from mm/mmap_lock.c~mm-vma-drop-mm-asynchronously-in-vma_start_read +18 -3
@@ -306,11 +306,10 @@ err_unstable:*/ other_mm = vma->vm_mm;/* use a copy as vma can be freed after we drop vm_refcnt */ -/* __mmdrop() is a heavy operation, do it after dropping RCU lock. */ -rcu_read_unlock ();mmgrab (other_mm);vma_refcount_put (vma); -mmdrop (other_mm); +mmdrop_async (other_mm); +rcu_read_unlock ();return NULL ; }
@@ -560,6 +559,22 @@ void __mm_reset_vma_seqcounts(struct mm_ }EXPORT_SYMBOL (__mm_reset_vma_seqcounts); +static void mmdrop_async_fn (struct work_struct *work) +{ +struct mm_struct *mm; + + mm = container_of(work, struct mm_struct, async_put_work); +__mmdrop (mm); +} + +void mmdrop_async (struct mm_struct *mm) +{ +if (unlikely (atomic_dec_and_test (&mm->mm_count))) { +INIT_WORK (&mm->async_put_work, mmdrop_async_fn); +schedule_work (&mm->async_put_work); + } +} +#ifdef CONFIG_LOCK_MM_AND_FIND_VMA #include <linux/extable.h>
_
Patches currently in -mm which might be from [email protected] are
mm-msync-return-enomem-if-page-aligned-length-overflows.patch mm-vma-drop-mm-asynchronously-in-vma_start_read.patch mm-vma-predicate-vma_assert_stabilised-on-config_debug_vm.patch mm-only-assert-mmap-locks-if-config_debug_vm-or-lockdep-is-set.patch mm-only-assert-vma-locks-if-config_debug_vm-is-set.patch mm-convert-warn_on_onces-to-vm_warn_on_once-in-vma-lock-code.patch mm-madvise-zap-markers-from-guard-regions.patch mm-mmap-free-gathered-pages-before-mmf_oom_skip.patch percpu-fix-contig-hints-that-are-too-low.patch percpu-search-the-whole-reserved-chunk.patch
Quoted 59 lines · Lorenzo Stoakes (ARM)
Commit
bee9ef4a40a2("bpf: switchtask_vmaiterator frommmap_lockto per-VMA locks") open-codes alock_next_vma()implementation that does roughly the same thing inbpf_iter_task_vma_find_next(), only without fallback to anmmap_lock.This is because
bpf_iter_task_vma_find_next()can be called from non-sleepable contexts.However,
lock_vma_under_rcu()gave no guarantee as to which contexts it is safe to be called from, and indeed it turns out this assumption is unsafe.Since commit
9bbffee67ffd("mm: fix a UAF whenvma->mmis freed aftervma->vm_refcntgot dropped"), this call chain is possible on x86:
lock_vma_under_rcu()
->vma_start_read()-> mmdrop() -> __mmdrop() -> mm_free_pgd() -> pgd_free() -> pgd_dtor() -> spin_lock(&pgd_lock)Thus, a plain
spin_lock()can occur whenlock_vma_under_rcu()is called. Sincebpf_iter_task_vma_find_next()can be called in softirq context, this can deadlock, for example:
pgd_alloc()
->spin_lock(&pgd_lock)<softirq> bpf_iter_task_vma_next() -> lock_vma_under_rcu() ... -> spin_lock(&pgd_lock) [ DEADLOCK ]Alexei Starovoitov also pointed out that a BPF program attached to
trace_contention_end()can similarly cause a deadlock:
pgd_alloc()
spin_lock(&pgd_lock)
queued_spin_lock_slowpath()
trace_contention_end()<-pgd_lockis held
tp_btf/contention_end prog
bpf_iter_task_vma_next()
lock_vma_under_rcu()
vma_start_read()
mmdrop(other_mm)<- last put takespgd_lockagainResolve the issue by having
vma_start_read()usemmdrop_async()instead ofmmdrop()so the work is deferred to a work queue.Make
mmdrop_async()an internal mm function, defined inmm/internal.h, to avoid abuse elsewhere (kernel/fork.cincludes this header, so still retains access).Commit
0b16f8bed19c("mm: changevma_start_read()to drop RCU lock on failure") accounted for the fact that the RCU lock had to be dropped for this operation, which is no longer applicable.
Quoted 8 lines
For the sake of a backported fix, retain this behaviour, however drop the RCU lock last and remove the now irrelevant comment.
Reported-by Chris Mason <[email protected]>Fixes bee9ef4a40a2 ("bpf: switch task_vma iterator from mmap_lock to per-VMA locks")Signed-off-by Lorenzo Stoakes (ARM) <[email protected]>
Given how it should be rare to actually drop the mm here, it should be fine.
Quoted 92 lines
--- kernel/fork.c | 16 ---------------- mm/internal.h | 3 +++ mm/mmap_lock.c | 21 ++++++++++++++++++--- 3 files changed, 21 insertions(+), 19 deletions(-)kernel/fork.c +0
-16@@ -744,22 +744,6 @@ void __mmdrop(struct mm_struct *mm) }EXPORT_SYMBOL_GPL (__mmdrop); -static void mmdrop_async_fn (struct work_struct *work) -{ -struct mm_struct *mm; - - mm = container_of(work, struct mm_struct, async_put_work); -__mmdrop (mm); -} - -static void mmdrop_async (struct mm_struct *mm) -{ -if (unlikely (atomic_dec_and_test (&mm->mm_count))) { -INIT_WORK (&mm->async_put_work, mmdrop_async_fn); -schedule_work (&mm->async_put_work); - } -} -static inline void free_signal_struct (struct signal_struct *sig) {taskstats_tgid_free (sig);mm/internal.h +3
-0@@ -1671,4 +1671,7 @@ static inline bool can_spin_trylock(void)/* char-mem.c */ bool file_is_dev_zero (const struct file *file); +/* mmap_lock.c */ +void mmdrop_async (struct mm_struct *mm); +#endif /* __MM_INTERNAL_H */ mm/mmap_lock.c +18
-4@@ -306,11 +306,10 @@ static struct vm_area_struct *vma_start_read(struct mm_struct *mm,*/ other_mm = vma->vm_mm;/* use a copy as vma can be freed after we drop vm_refcnt */ -/* __mmdrop() is a heavy operation, do it after dropping RCU lock. */ -rcu_read_unlock ();mmgrab (other_mm);vma_refcount_put (vma); -mmdrop (other_mm); +mmdrop_async (other_mm); +rcu_read_unlock ();return NULL ; }@@ -560,6 +559,22 @@ void __mm_reset_vma_seqcounts(struct mm_struct *mm) }EXPORT_SYMBOL (__mm_reset_vma_seqcounts); +static void mmdrop_async_fn (struct work_struct *work) +{ +struct mm_struct *mm; + + mm = container_of(work, struct mm_struct, async_put_work); +__mmdrop (mm); +} + +void mmdrop_async (struct mm_struct *mm) +{ +if (unlikely (atomic_dec_and_test (&mm->mm_count))) { +INIT_WORK (&mm->async_put_work, mmdrop_async_fn); +schedule_work (&mm->async_put_work); + } +} +#ifdef CONFIG_LOCK_MM_AND_FIND_VMA #include <linux/extable.h> ---base-commit:
913390e679e23e3a45643028ee7f992889094051
change-id: 20261124-vma-read-lock-mmdrop-async-ba417cd1c1f0Cheers,
[merged mm-hotfixes-stable] mm-vma-drop-mm-asynchronously-in-vma_start_read.patch removed from -mm tree
In reply to Lorenzo Stoakes (ARM)The quilt patch titled Subject: mm/vma: drop mm asynchronously in vma_start_read() has been removed from the -mm tree. Its filename was mm-vma-drop-mm-asynchronously-in-vma_start_read.patch
The quilt patch titled
Subject: mm/vma: drop mm asynchronously in vma_start_read()
has been removed from the -mm tree. Its filename was
mm-vma-drop-mm-asynchronously-in-vma_start_read.patch
This patch was dropped because it was merged into the mm-hotfixes-stable branch of git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
------------------------------------------------------
From: "Lorenzo Stoakes (ARM)" <[email protected]>
Subject: mm/vma: drop mm asynchronously in vma_start_read()
Date: Tue, 24 Nov 2026 12:26:19 +0000
Commit bee9ef4a40a2 ("bpf: switch task_vma iterator from mmap_lock to per-VMA locks") open-codes a lock_next_vma() implementation that does roughly the same thing in bpf_iter_task_vma_find_next(), only without fallback to an mmap_lock.
This is because bpf_iter_task_vma_find_next() can be called from non-sleepable contexts.
However, lock_vma_under_rcu() gave no guarantee as to which contexts it is safe to be called from, and indeed it turns out this assumption is unsafe.
Since commit 9bbffee67ffd ("mm: fix a UAF when vma->mm is freed after vma->vm_refcnt got dropped"), this call chain is possible on x86:
lock_vma_under_rcu()
-> vma_start_read()
-> mmdrop()
-> __mmdrop()
-> mm_free_pgd()
-> pgd_free()
-> pgd_dtor()
-> spin_lock(&pgd_lock)Thus, a plain spin_lock() can occur when lock_vma_under_rcu() is called. Since bpf_iter_task_vma_find_next() can be called in softirq context, this can deadlock, for example:
pgd_alloc()
-> spin_lock(&pgd_lock)
<softirq>
bpf_iter_task_vma_next()
-> lock_vma_under_rcu()
...
-> spin_lock(&pgd_lock) [ DEADLOCK ]Alexei Starovoitov also pointed out that a BPF program attached to trace_contention_end() can similarly cause a deadlock:
pgd_alloc()
spin_lock(&pgd_lock)
queued_spin_lock_slowpath()
trace_contention_end() <- pgd_lock is held
tp_btf/contention_end prog
bpf_iter_task_vma_next()
lock_vma_under_rcu()
vma_start_read()
mmdrop(other_mm) <- last put takes pgd_lock again
Resolve the issue by having vma_start_read() use mmdrop_async() instead of mmdrop() so the work is deferred to a work queue.
Make mmdrop_async() an internal mm function, defined in mm/internal.h, to avoid abuse elsewhere (kernel/fork.c includes this header, so still retains access).
Commit 0b16f8bed19c ("mm: change vma_start_read() to drop RCU lock on failure") accounted for the fact that the RCU lock had to be dropped for this operation, which is no longer applicable.
For the sake of a backported fix, retain this behaviour, however drop the RCU lock last and remove the now irrelevant comment.
kernel/fork.c | 16 ---------------- mm/internal.h | 3 +++ mm/mmap_lock.c | 25 ++++++++++++++++++++----- 3 files changed, 23 insertions(+), 21 deletions(-)
kernel/fork.c renamed from kernel/fork.c~mm-vma-drop-mm-asynchronously-in-vma_start_read +0 -16
@@ -744,22 +744,6 @@ void __mmdrop(struct mm_struct *mm) }EXPORT_SYMBOL_GPL (__mmdrop); -static void mmdrop_async_fn (struct work_struct *work) -{ -struct mm_struct *mm; - - mm = container_of(work, struct mm_struct, async_put_work); -__mmdrop (mm); -} - -static void mmdrop_async (struct mm_struct *mm) -{ -if (unlikely (atomic_dec_and_test (&mm->mm_count))) { -INIT_WORK (&mm->async_put_work, mmdrop_async_fn); -schedule_work (&mm->async_put_work); - } -} -static inline void free_signal_struct (struct signal_struct *sig) {taskstats_tgid_free (sig);
mm/internal.h renamed from mm/internal.h~mm-vma-drop-mm-asynchronously-in-vma_start_read +3 -0
@@ -1692,4 +1692,7 @@ static inline bool can_spin_trylock(void/* char-mem.c */ bool file_is_dev_zero (const struct file *file); +/* mmap_lock.c */ +void mmdrop_async (struct mm_struct *mm); +#endif /* __MM_INTERNAL_H */
mm/mmap_lock.c renamed from mm/mmap_lock.c~mm-vma-drop-mm-asynchronously-in-vma_start_read +20 -5
@@ -300,17 +300,16 @@ err: err_unstable:/* * If vma got attached to another mm from under us, that mm is not -* stable and can be freed in the narrow window after vma->vm_refcnt +* stable and can be reused in the narrow window after vma->vm_refcnt * is dropped and before rcuwait_wake_up(mm) is called. Grab it before * releasing vma->vm_refcnt. */ - other_mm = vma->vm_mm;/* use a copy as vma can be freed after we drop vm_refcnt */ + other_mm = vma->vm_mm;/* use a copy as vma can be reused after we drop vm_refcnt */ -/* __mmdrop() is a heavy operation, do it after dropping RCU lock. */ -rcu_read_unlock ();mmgrab (other_mm);vma_refcount_put (vma); -mmdrop (other_mm); +mmdrop_async (other_mm); +rcu_read_unlock ();return NULL ; }
@@ -560,6 +559,22 @@ void __mm_reset_vma_seqcounts(struct mm_ }EXPORT_SYMBOL (__mm_reset_vma_seqcounts); +static void mmdrop_async_fn (struct work_struct *work) +{ +struct mm_struct *mm; + + mm = container_of(work, struct mm_struct, async_put_work); +__mmdrop (mm); +} + +void mmdrop_async (struct mm_struct *mm) +{ +if (unlikely (atomic_dec_and_test (&mm->mm_count))) { +INIT_WORK (&mm->async_put_work, mmdrop_async_fn); +schedule_work (&mm->async_put_work); + } +} +#ifdef CONFIG_LOCK_MM_AND_FIND_VMA #include <linux/extable.h>
_
Patches currently in -mm which might be from [email protected] are
mm-vma-predicate-vma_assert_stabilised-on-config_debug_vm.patch mm-only-assert-mmap-locks-if-config_debug_vm-or-lockdep-is-set.patch mm-only-assert-vma-locks-if-config_debug_vm-is-set.patch mm-convert-warn_on_onces-to-vm_warn_on_once-in-vma-lock-code.patch mm-madvise-zap-markers-from-guard-regions.patch mm-mmap-free-gathered-pages-before-mmf_oom_skip.patch percpu-fix-contig-hints-that-are-too-low.patch percpu-search-the-whole-reserved-chunk.patch xen-privcmd-convert-mmap-hook-to-mmap_prepare.patch kvm-ppc-book3s-convert-spapr-tce-mmap-hook-to-mmap_prepare.patch mshv_vtl-convert-mmap-hooks-to-mmap_prepare.patch x86-sgx-convert-vepc-mmap-hook-to-mmap_prepare.patch udf-convert-mmap-hook-to-mmap_prepare.patch selinuxfs-convert-mmap-hooks-to-mmap_prepare.patch xen-convert-xenstore-interface-mmap-hooks-to-mmap_prepare.patch soc-aspeed-convert-lpc-and-p2a-control-mmap-hooks-to-mmap_prepare.patch soc-qcom-rmtfs_mem-convert-mmap-hook-to-mmap_prepare.patch tee-convert-shm-mmap-hook-to-mmap_prepare.patch misc-bcm-vk-convert-mmap-hook-to-mmap_prepare.patch ptp-vmclock-convert-mmap-hook-to-mmap_prepare.patch vhost-vdpa-convert-mmap-hook-to-mmap_prepare.patch mm-vma-error-out-on-invalid-bounds-in-unmapped_area_topdown.patch mm-vma-lock-vmas-before-taking-mm_all_locks_mutex.patch mm-filemap-convert-fault-and-readahead-logic-to-use-vma_flags_t.patch mm-madvise-convert-madvise_vma_behavior-to-use-vma_flags_t.patch mm-convert-miscellaneous-vma-flag-tests-to-use-vma_flags_t.patch fs-proc-task_mmu-convert-vma-flag-tests-to-use-vma_flags_t.patch fs-coredump-convert-vma_dump_size-to-use-vma_flags_t.patch uprobes-convert-vma-flag-tests-to-use-vma_flags_t.patch perf-convert-vma-flag-tests-to-use-vma_flags_t.patch selinux-convert-vma-flag-tests-to-use-vma_flags_t.patch apparmor-convert-vma-flag-tests-to-use-vma_flags_t.patch ima-convert-vma-flag-tests-to-use-vma_flags_t.patch ipe-convert-vma-flag-tests-to-use-vma_flags_t.patch coda-never-point-a-files-f_mapping-at-a-containers-mapping.patch fs-proc-task_mmu-write-protect-only-with-ptrace-attach.patch