Skip to content

Commit 3195616

Browse files
apopple-nvidiatorvalds
authored andcommitted
mm/mmu_notifier.c: fix race in mmu_interval_notifier_remove()
In some cases it is possible for mmu_interval_notifier_remove() to race with mn_tree_inv_end() allowing it to return while the notifier data structure is still in use. Consider the following sequence: CPU0 - mn_tree_inv_end() CPU1 - mmu_interval_notifier_remove() ----------------------------------- ------------------------------------ spin_lock(subscriptions->lock); seq = subscriptions->invalidate_seq; spin_lock(subscriptions->lock); spin_unlock(subscriptions->lock); subscriptions->invalidate_seq++; wait_event(invalidate_seq != seq); return; interval_tree_remove(interval_sub); kfree(interval_sub); spin_unlock(subscriptions->lock); wake_up_all(); As the wait_event() condition is true it will return immediately. This can lead to use-after-free type errors if the caller frees the data structure containing the interval notifier subscription while it is still on a deferred list. Fix this by taking the appropriate lock when reading invalidate_seq to ensure proper synchronisation. I observed this whilst running stress testing during some development. You do have to be pretty unlucky, but it leads to the usual problems of use-after-free (memory corruption, kernel crash, difficult to diagnose WARN_ON, etc). Link: https://lkml.kernel.org/r/20220420043734.476348-1-apopple@nvidia.com Fixes: 99cb252 ("mm/mmu_notifier: add an interval tree notifier") Signed-off-by: Alistair Popple <apopple@nvidia.com> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com> Cc: Christian König <christian.koenig@amd.com> Cc: John Hubbard <jhubbard@nvidia.com> Cc: Ralph Campbell <rcampbell@nvidia.com> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent ecc0446 commit 3195616

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

mm/mmu_notifier.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1036,6 +1036,18 @@ int mmu_interval_notifier_insert_locked(
10361036
}
10371037
EXPORT_SYMBOL_GPL(mmu_interval_notifier_insert_locked);
10381038

1039+
static bool
1040+
mmu_interval_seq_released(struct mmu_notifier_subscriptions *subscriptions,
1041+
unsigned long seq)
1042+
{
1043+
bool ret;
1044+
1045+
spin_lock(&subscriptions->lock);
1046+
ret = subscriptions->invalidate_seq != seq;
1047+
spin_unlock(&subscriptions->lock);
1048+
return ret;
1049+
}
1050+
10391051
/**
10401052
* mmu_interval_notifier_remove - Remove a interval notifier
10411053
* @interval_sub: Interval subscription to unregister
@@ -1083,7 +1095,7 @@ void mmu_interval_notifier_remove(struct mmu_interval_notifier *interval_sub)
10831095
lock_map_release(&__mmu_notifier_invalidate_range_start_map);
10841096
if (seq)
10851097
wait_event(subscriptions->wq,
1086-
READ_ONCE(subscriptions->invalidate_seq) != seq);
1098+
mmu_interval_seq_released(subscriptions, seq));
10871099

10881100
/* pairs with mmgrab in mmu_interval_notifier_insert() */
10891101
mmdrop(mm);

0 commit comments

Comments
 (0)