Skip to content

Commit a2673b6

Browse files
jankaratorvalds
authored andcommitted
fsnotify: fix oops in fsnotify_clear_marks_by_group_flags()
fsnotify_clear_marks_by_group_flags() can race with fsnotify_destroy_marks() so when fsnotify_destroy_mark_locked() drops mark_mutex, a mark from the list iterated by fsnotify_clear_marks_by_group_flags() can be freed and we dereference free memory in the loop there. Fix the problem by keeping mark_mutex held in fsnotify_destroy_mark_locked(). The reason why we drop that mutex is that we need to call a ->freeing_mark() callback which may acquire mark_mutex again. To avoid this and similar lock inversion issues, we move the call to ->freeing_mark() callback to the kthread destroying the mark. Signed-off-by: Jan Kara <[email protected]> Reported-by: Ashish Sangwan <[email protected]> Suggested-by: Lino Sanfilippo <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 3581d45 commit a2673b6

File tree

1 file changed

+14
-20
lines changed

1 file changed

+14
-20
lines changed

fs/notify/mark.c

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -152,31 +152,15 @@ void fsnotify_destroy_mark_locked(struct fsnotify_mark *mark,
152152
BUG();
153153

154154
list_del_init(&mark->g_list);
155-
156155
spin_unlock(&mark->lock);
157156

158157
if (inode && (mark->flags & FSNOTIFY_MARK_FLAG_OBJECT_PINNED))
159158
iput(inode);
160-
/* release lock temporarily */
161-
mutex_unlock(&group->mark_mutex);
162159

163160
spin_lock(&destroy_lock);
164161
list_add(&mark->g_list, &destroy_list);
165162
spin_unlock(&destroy_lock);
166163
wake_up(&destroy_waitq);
167-
/*
168-
* We don't necessarily have a ref on mark from caller so the above destroy
169-
* may have actually freed it, unless this group provides a 'freeing_mark'
170-
* function which must be holding a reference.
171-
*/
172-
173-
/*
174-
* Some groups like to know that marks are being freed. This is a
175-
* callback to the group function to let it know that this mark
176-
* is being freed.
177-
*/
178-
if (group->ops->freeing_mark)
179-
group->ops->freeing_mark(mark, group);
180164

181165
/*
182166
* __fsnotify_update_child_dentry_flags(inode);
@@ -191,8 +175,6 @@ void fsnotify_destroy_mark_locked(struct fsnotify_mark *mark,
191175
*/
192176

193177
atomic_dec(&group->num_marks);
194-
195-
mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
196178
}
197179

198180
void fsnotify_destroy_mark(struct fsnotify_mark *mark,
@@ -205,7 +187,10 @@ void fsnotify_destroy_mark(struct fsnotify_mark *mark,
205187

206188
/*
207189
* Destroy all marks in the given list. The marks must be already detached from
208-
* the original inode / vfsmount.
190+
* the original inode / vfsmount. Note that we can race with
191+
* fsnotify_clear_marks_by_group_flags(). However we hold a reference to each
192+
* mark so they won't get freed from under us and nobody else touches our
193+
* free_list list_head.
209194
*/
210195
void fsnotify_destroy_marks(struct list_head *to_free)
211196
{
@@ -406,7 +391,7 @@ struct fsnotify_mark *fsnotify_find_mark(struct hlist_head *head,
406391
}
407392

408393
/*
409-
* clear any marks in a group in which mark->flags & flags is true
394+
* Clear any marks in a group in which mark->flags & flags is true.
410395
*/
411396
void fsnotify_clear_marks_by_group_flags(struct fsnotify_group *group,
412397
unsigned int flags)
@@ -460,6 +445,7 @@ static int fsnotify_mark_destroy(void *ignored)
460445
{
461446
struct fsnotify_mark *mark, *next;
462447
struct list_head private_destroy_list;
448+
struct fsnotify_group *group;
463449

464450
for (;;) {
465451
spin_lock(&destroy_lock);
@@ -471,6 +457,14 @@ static int fsnotify_mark_destroy(void *ignored)
471457

472458
list_for_each_entry_safe(mark, next, &private_destroy_list, g_list) {
473459
list_del_init(&mark->g_list);
460+
group = mark->group;
461+
/*
462+
* Some groups like to know that marks are being freed.
463+
* This is a callback to the group function to let it
464+
* know that this mark is being freed.
465+
*/
466+
if (group && group->ops->freeing_mark)
467+
group->ops->freeing_mark(mark, group);
474468
fsnotify_put_mark(mark);
475469
}
476470

0 commit comments

Comments
 (0)