Skip to content

Commit 751efd8

Browse files
Robin Holttorvalds
Robin Holt
authored andcommitted
mmu_notifier_unregister NULL Pointer deref and multiple ->release() callouts
There is a race condition between mmu_notifier_unregister() and __mmu_notifier_release(). Assume two tasks, one calling mmu_notifier_unregister() as a result of a filp_close() ->flush() callout (task A), and the other calling mmu_notifier_release() from an mmput() (task B). A B t1 srcu_read_lock() t2 if (!hlist_unhashed()) t3 srcu_read_unlock() t4 srcu_read_lock() t5 hlist_del_init_rcu() t6 synchronize_srcu() t7 srcu_read_unlock() t8 hlist_del_rcu() <--- NULL pointer deref. Additionally, the list traversal in __mmu_notifier_release() is not protected by the by the mmu_notifier_mm->hlist_lock which can result in callouts to the ->release() notifier from both mmu_notifier_unregister() and __mmu_notifier_release(). -stable suggestions: The stable trees prior to 3.7.y need commits 21a9273 and 7040030 cherry-picked in that order prior to cherry-picking this commit. The 3.7.y tree already has those two commits. Signed-off-by: Robin Holt <holt@sgi.com> Cc: Andrea Arcangeli <aarcange@redhat.com> Cc: Wanpeng Li <liwanp@linux.vnet.ibm.com> Cc: Xiao Guangrong <xiaoguangrong@linux.vnet.ibm.com> Cc: Avi Kivity <avi@redhat.com> Cc: Hugh Dickins <hughd@google.com> Cc: Marcelo Tosatti <mtosatti@redhat.com> Cc: Sagi Grimberg <sagig@mellanox.co.il> Cc: Haggai Eran <haggaie@mellanox.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 c1f1949 commit 751efd8

File tree

1 file changed

+42
-40
lines changed

1 file changed

+42
-40
lines changed

mm/mmu_notifier.c

+42-40
Original file line numberDiff line numberDiff line change
@@ -37,49 +37,51 @@ static struct srcu_struct srcu;
3737
void __mmu_notifier_release(struct mm_struct *mm)
3838
{
3939
struct mmu_notifier *mn;
40-
struct hlist_node *n;
4140
int id;
4241

4342
/*
44-
* SRCU here will block mmu_notifier_unregister until
45-
* ->release returns.
43+
* srcu_read_lock() here will block synchronize_srcu() in
44+
* mmu_notifier_unregister() until all registered
45+
* ->release() callouts this function makes have
46+
* returned.
4647
*/
4748
id = srcu_read_lock(&srcu);
48-
hlist_for_each_entry_rcu(mn, n, &mm->mmu_notifier_mm->list, hlist)
49-
/*
50-
* if ->release runs before mmu_notifier_unregister it
51-
* must be handled as it's the only way for the driver
52-
* to flush all existing sptes and stop the driver
53-
* from establishing any more sptes before all the
54-
* pages in the mm are freed.
55-
*/
56-
if (mn->ops->release)
57-
mn->ops->release(mn, mm);
58-
srcu_read_unlock(&srcu, id);
59-
6049
spin_lock(&mm->mmu_notifier_mm->lock);
6150
while (unlikely(!hlist_empty(&mm->mmu_notifier_mm->list))) {
6251
mn = hlist_entry(mm->mmu_notifier_mm->list.first,
6352
struct mmu_notifier,
6453
hlist);
54+
6555
/*
66-
* We arrived before mmu_notifier_unregister so
67-
* mmu_notifier_unregister will do nothing other than
68-
* to wait ->release to finish and
69-
* mmu_notifier_unregister to return.
56+
* Unlink. This will prevent mmu_notifier_unregister()
57+
* from also making the ->release() callout.
7058
*/
7159
hlist_del_init_rcu(&mn->hlist);
60+
spin_unlock(&mm->mmu_notifier_mm->lock);
61+
62+
/*
63+
* Clear sptes. (see 'release' description in mmu_notifier.h)
64+
*/
65+
if (mn->ops->release)
66+
mn->ops->release(mn, mm);
67+
68+
spin_lock(&mm->mmu_notifier_mm->lock);
7269
}
7370
spin_unlock(&mm->mmu_notifier_mm->lock);
7471

7572
/*
76-
* synchronize_srcu here prevents mmu_notifier_release to
77-
* return to exit_mmap (which would proceed freeing all pages
78-
* in the mm) until the ->release method returns, if it was
79-
* invoked by mmu_notifier_unregister.
80-
*
81-
* The mmu_notifier_mm can't go away from under us because one
82-
* mm_count is hold by exit_mmap.
73+
* All callouts to ->release() which we have done are complete.
74+
* Allow synchronize_srcu() in mmu_notifier_unregister() to complete
75+
*/
76+
srcu_read_unlock(&srcu, id);
77+
78+
/*
79+
* mmu_notifier_unregister() may have unlinked a notifier and may
80+
* still be calling out to it. Additionally, other notifiers
81+
* may have been active via vmtruncate() et. al. Block here
82+
* to ensure that all notifier callouts for this mm have been
83+
* completed and the sptes are really cleaned up before returning
84+
* to exit_mmap().
8385
*/
8486
synchronize_srcu(&srcu);
8587
}
@@ -294,31 +296,31 @@ void mmu_notifier_unregister(struct mmu_notifier *mn, struct mm_struct *mm)
294296
{
295297
BUG_ON(atomic_read(&mm->mm_count) <= 0);
296298

299+
spin_lock(&mm->mmu_notifier_mm->lock);
297300
if (!hlist_unhashed(&mn->hlist)) {
298-
/*
299-
* SRCU here will force exit_mmap to wait ->release to finish
300-
* before freeing the pages.
301-
*/
302301
int id;
303302

304-
id = srcu_read_lock(&srcu);
305303
/*
306-
* exit_mmap will block in mmu_notifier_release to
307-
* guarantee ->release is called before freeing the
308-
* pages.
304+
* Ensure we synchronize up with __mmu_notifier_release().
309305
*/
306+
id = srcu_read_lock(&srcu);
307+
308+
hlist_del_rcu(&mn->hlist);
309+
spin_unlock(&mm->mmu_notifier_mm->lock);
310+
310311
if (mn->ops->release)
311312
mn->ops->release(mn, mm);
312-
srcu_read_unlock(&srcu, id);
313313

314-
spin_lock(&mm->mmu_notifier_mm->lock);
315-
hlist_del_rcu(&mn->hlist);
314+
/*
315+
* Allow __mmu_notifier_release() to complete.
316+
*/
317+
srcu_read_unlock(&srcu, id);
318+
} else
316319
spin_unlock(&mm->mmu_notifier_mm->lock);
317-
}
318320

319321
/*
320-
* Wait any running method to finish, of course including
321-
* ->release if it was run by mmu_notifier_relase instead of us.
322+
* Wait for any running method to finish, including ->release() if it
323+
* was run by __mmu_notifier_release() instead of us.
322324
*/
323325
synchronize_srcu(&srcu);
324326

0 commit comments

Comments
 (0)