Skip to contentSkip to conversation

mm/vma: drop mm asynchronously in vma_start_read()

PATCH In review Lorenzo Stoakes (ARM) 2 participants 2 messages Nov 24 – Nov 26

Subsystems (7)
lore
Share & tools
lore

Lorenzo Stoakes (ARM)

author maintainer
#0 lore raw

PATCH v2 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.

Remove the now-defunct comment on the 'heavy operation', but leave examining that to a follow up.

---
v2:
* Put the rcu_read_unlock() back where it was, a follow-up can address anything else here.

v1:
https://lore.kernel.org/r/[email protected]

---
 kernel/fork.c  | 16 ----------------
 mm/internal.h  |  3 +++
 mm/mmap_lock.c | 19 +++++++++++++++++--
 3 files changed, 20 insertions(+), 18 deletions(-)
kernel/fork.c +0 -16
index 5efceb8d58a7..4d5866c83481 100644
@@ -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
index de4593b3e3b6..cc95019c7e4b 100644
@@ -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 +17 -2
index 073c5125b424..d28bbc3ba734 100644
@@ -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);
 
 	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]>
maintainer Reviewed-by on v2 1/1
#1 lore raw
Quoted 62 lines · Lorenzo Stoakes (ARM)

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.

Remove the now-defunct comment on the 'heavy operation', but leave examining that to a follow up.

Quoted 8 lines
Quoted 101 lines

---
v2:
* Put the rcu_read_unlock() back where it was, a follow-up can address anything else here.

v1:
https://lore.kernel.org/r/[email protected]

---
 kernel/fork.c  | 16 ----------------
 mm/internal.h  |  3 +++
 mm/mmap_lock.c | 19 +++++++++++++++++--
 3 files changed, 20 insertions(+), 18 deletions(-)
kernel/fork.c +0 -16
index 5efceb8d58a7..4d5866c83481 100644
@@ -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
index de4593b3e3b6..cc95019c7e4b 100644
@@ -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 +17 -3
index 073c5125b424..d28bbc3ba734 100644
@@ -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);
 
 	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]>