Skip to content

Commit e79719a

Browse files
amir73iljankara
authored andcommitted
fanotify: use fsnotify group lock helpers
Direct reclaim from fanotify mark allocation context may try to evict inodes with evictable marks of the same group and hit this deadlock: [<0>] fsnotify_destroy_mark+0x1f/0x3a [<0>] fsnotify_destroy_marks+0x71/0xd9 [<0>] __destroy_inode+0x24/0x7e [<0>] destroy_inode+0x2c/0x67 [<0>] dispose_list+0x49/0x68 [<0>] prune_icache_sb+0x5b/0x79 [<0>] super_cache_scan+0x11c/0x16f [<0>] shrink_slab.constprop.0+0x23e/0x40f [<0>] shrink_node+0x218/0x3e7 [<0>] do_try_to_free_pages+0x12a/0x2d2 [<0>] try_to_free_pages+0x166/0x242 [<0>] __alloc_pages_slowpath.constprop.0+0x30c/0x903 [<0>] __alloc_pages+0xeb/0x1c7 [<0>] cache_grow_begin+0x6f/0x31e [<0>] fallback_alloc+0xe0/0x12d [<0>] ____cache_alloc_node+0x15a/0x17e [<0>] kmem_cache_alloc_trace+0xa1/0x143 [<0>] fanotify_add_mark+0xd5/0x2b2 [<0>] do_fanotify_mark+0x566/0x5eb [<0>] __x64_sys_fanotify_mark+0x21/0x24 [<0>] do_syscall_64+0x6d/0x80 [<0>] entry_SYSCALL_64_after_hwframe+0x44/0xae Set the FSNOTIFY_GROUP_NOFS flag to prevent going into direct reclaim from allocations under fanotify group lock and use the safe group lock helpers. Link: https://lore.kernel.org/r/20220422120327.3459282-16-amir73il@gmail.com Suggested-by: Jan Kara <jack@suse.cz> Link: https://lore.kernel.org/r/20220321112310.vpr7oxro2xkz5llh@quack3.lan/ Signed-off-by: Amir Goldstein <amir73il@gmail.com> Signed-off-by: Jan Kara <jack@suse.cz>
1 parent 7d5e005 commit e79719a

1 file changed

Lines changed: 7 additions & 7 deletions

File tree

fs/notify/fanotify/fanotify_user.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,10 +1035,10 @@ static int fanotify_remove_mark(struct fsnotify_group *group,
10351035
__u32 removed;
10361036
int destroy_mark;
10371037

1038-
mutex_lock(&group->mark_mutex);
1038+
fsnotify_group_lock(group);
10391039
fsn_mark = fsnotify_find_mark(connp, group);
10401040
if (!fsn_mark) {
1041-
mutex_unlock(&group->mark_mutex);
1041+
fsnotify_group_unlock(group);
10421042
return -ENOENT;
10431043
}
10441044

@@ -1048,7 +1048,7 @@ static int fanotify_remove_mark(struct fsnotify_group *group,
10481048
fsnotify_recalc_mask(fsn_mark->connector);
10491049
if (destroy_mark)
10501050
fsnotify_detach_mark(fsn_mark);
1051-
mutex_unlock(&group->mark_mutex);
1051+
fsnotify_group_unlock(group);
10521052
if (destroy_mark)
10531053
fsnotify_free_mark(fsn_mark);
10541054

@@ -1196,13 +1196,13 @@ static int fanotify_add_mark(struct fsnotify_group *group,
11961196
bool recalc;
11971197
int ret = 0;
11981198

1199-
mutex_lock(&group->mark_mutex);
1199+
fsnotify_group_lock(group);
12001200
fsn_mark = fsnotify_find_mark(connp, group);
12011201
if (!fsn_mark) {
12021202
fsn_mark = fanotify_add_new_mark(group, connp, obj_type,
12031203
fan_flags, fsid);
12041204
if (IS_ERR(fsn_mark)) {
1205-
mutex_unlock(&group->mark_mutex);
1205+
fsnotify_group_unlock(group);
12061206
return PTR_ERR(fsn_mark);
12071207
}
12081208
}
@@ -1231,7 +1231,7 @@ static int fanotify_add_mark(struct fsnotify_group *group,
12311231
fsnotify_recalc_mask(fsn_mark->connector);
12321232

12331233
out:
1234-
mutex_unlock(&group->mark_mutex);
1234+
fsnotify_group_unlock(group);
12351235

12361236
fsnotify_put_mark(fsn_mark);
12371237
return ret;
@@ -1385,7 +1385,7 @@ SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags)
13851385

13861386
/* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */
13871387
group = fsnotify_alloc_group(&fanotify_fsnotify_ops,
1388-
FSNOTIFY_GROUP_USER);
1388+
FSNOTIFY_GROUP_USER | FSNOTIFY_GROUP_NOFS);
13891389
if (IS_ERR(group)) {
13901390
return PTR_ERR(group);
13911391
}

0 commit comments

Comments
 (0)