Skip to content

Commit abc7757

Browse files
committed
fsnotify: Provide framework for dropping SRCU lock in ->handle_event
fanotify wants to drop fsnotify_mark_srcu lock when waiting for response from userspace so that the whole notification subsystem is not blocked during that time. This patch provides a framework for safely getting mark reference for a mark found in the object list which pins the mark in that list. We can then drop fsnotify_mark_srcu, wait for userspace response and then safely continue iteration of the object list once we reaquire fsnotify_mark_srcu. Reviewed-by: Miklos Szeredi <mszeredi@redhat.com> Reviewed-by: Amir Goldstein <amir73il@gmail.com> Signed-off-by: Jan Kara <jack@suse.cz>
1 parent f09b04a commit abc7757

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed

fs/notify/fsnotify.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88

99
#include "../mount.h"
1010

11+
struct fsnotify_iter_info {
12+
struct fsnotify_mark *inode_mark;
13+
struct fsnotify_mark *vfsmount_mark;
14+
int srcu_idx;
15+
};
16+
1117
/* destroy all events sitting in this groups notification queue */
1218
extern void fsnotify_flush_notify(struct fsnotify_group *group);
1319

fs/notify/group.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ struct fsnotify_group *fsnotify_alloc_group(const struct fsnotify_ops *ops)
126126
/* set to 0 when there a no external references to this group */
127127
atomic_set(&group->refcnt, 1);
128128
atomic_set(&group->num_marks, 0);
129+
atomic_set(&group->user_waits, 0);
129130

130131
spin_lock_init(&group->notification_lock);
131132
INIT_LIST_HEAD(&group->notification_list);

fs/notify/mark.c

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,16 @@ void fsnotify_get_mark(struct fsnotify_mark *mark)
109109
atomic_inc(&mark->refcnt);
110110
}
111111

112+
/*
113+
* Get mark reference when we found the mark via lockless traversal of object
114+
* list. Mark can be already removed from the list by now and on its way to be
115+
* destroyed once SRCU period ends.
116+
*/
117+
static bool fsnotify_get_mark_safe(struct fsnotify_mark *mark)
118+
{
119+
return atomic_inc_not_zero(&mark->refcnt);
120+
}
121+
112122
static void __fsnotify_recalc_mask(struct fsnotify_mark_connector *conn)
113123
{
114124
u32 new_mask = 0;
@@ -243,6 +253,72 @@ void fsnotify_put_mark(struct fsnotify_mark *mark)
243253
FSNOTIFY_REAPER_DELAY);
244254
}
245255

256+
bool fsnotify_prepare_user_wait(struct fsnotify_iter_info *iter_info)
257+
{
258+
struct fsnotify_group *group;
259+
260+
if (WARN_ON_ONCE(!iter_info->inode_mark && !iter_info->vfsmount_mark))
261+
return false;
262+
263+
if (iter_info->inode_mark)
264+
group = iter_info->inode_mark->group;
265+
else
266+
group = iter_info->vfsmount_mark->group;
267+
268+
/*
269+
* Since acquisition of mark reference is an atomic op as well, we can
270+
* be sure this inc is seen before any effect of refcount increment.
271+
*/
272+
atomic_inc(&group->user_waits);
273+
274+
if (iter_info->inode_mark) {
275+
/* This can fail if mark is being removed */
276+
if (!fsnotify_get_mark_safe(iter_info->inode_mark))
277+
goto out_wait;
278+
}
279+
if (iter_info->vfsmount_mark) {
280+
if (!fsnotify_get_mark_safe(iter_info->vfsmount_mark))
281+
goto out_inode;
282+
}
283+
284+
/*
285+
* Now that both marks are pinned by refcount in the inode / vfsmount
286+
* lists, we can drop SRCU lock, and safely resume the list iteration
287+
* once userspace returns.
288+
*/
289+
srcu_read_unlock(&fsnotify_mark_srcu, iter_info->srcu_idx);
290+
291+
return true;
292+
out_inode:
293+
if (iter_info->inode_mark)
294+
fsnotify_put_mark(iter_info->inode_mark);
295+
out_wait:
296+
if (atomic_dec_and_test(&group->user_waits) && group->shutdown)
297+
wake_up(&group->notification_waitq);
298+
return false;
299+
}
300+
301+
void fsnotify_finish_user_wait(struct fsnotify_iter_info *iter_info)
302+
{
303+
struct fsnotify_group *group = NULL;
304+
305+
iter_info->srcu_idx = srcu_read_lock(&fsnotify_mark_srcu);
306+
if (iter_info->inode_mark) {
307+
group = iter_info->inode_mark->group;
308+
fsnotify_put_mark(iter_info->inode_mark);
309+
}
310+
if (iter_info->vfsmount_mark) {
311+
group = iter_info->vfsmount_mark->group;
312+
fsnotify_put_mark(iter_info->vfsmount_mark);
313+
}
314+
/*
315+
* We abuse notification_waitq on group shutdown for waiting for all
316+
* marks pinned when waiting for userspace.
317+
*/
318+
if (atomic_dec_and_test(&group->user_waits) && group->shutdown)
319+
wake_up(&group->notification_waitq);
320+
}
321+
246322
/*
247323
* Mark mark as detached, remove it from group list. Mark still stays in object
248324
* list until its last reference is dropped. Note that we rely on mark being
@@ -647,6 +723,12 @@ void fsnotify_detach_group_marks(struct fsnotify_group *group)
647723
fsnotify_free_mark(mark);
648724
fsnotify_put_mark(mark);
649725
}
726+
/*
727+
* Some marks can still be pinned when waiting for response from
728+
* userspace. Wait for those now. fsnotify_prepare_user_wait() will
729+
* not succeed now so this wait is race-free.
730+
*/
731+
wait_event(group->notification_waitq, !atomic_read(&group->user_waits));
650732
}
651733

652734
/* Destroy all marks attached to inode / vfsmount */

include/linux/fsnotify_backend.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ struct fsnotify_event;
8080
struct fsnotify_mark;
8181
struct fsnotify_event_private_data;
8282
struct fsnotify_fname;
83+
struct fsnotify_iter_info;
8384

8485
/*
8586
* Each group much define these ops. The fsnotify infrastructure will call
@@ -163,6 +164,8 @@ struct fsnotify_group {
163164
struct fsnotify_event *overflow_event; /* Event we queue when the
164165
* notification list is too
165166
* full */
167+
atomic_t user_waits; /* Number of tasks waiting for user
168+
* response */
166169

167170
/* groups can define private fields here or use the void *private */
168171
union {
@@ -368,6 +371,8 @@ extern void fsnotify_clear_marks_by_group_flags(struct fsnotify_group *group, un
368371
extern void fsnotify_get_mark(struct fsnotify_mark *mark);
369372
extern void fsnotify_put_mark(struct fsnotify_mark *mark);
370373
extern void fsnotify_unmount_inodes(struct super_block *sb);
374+
extern void fsnotify_finish_user_wait(struct fsnotify_iter_info *iter_info);
375+
extern bool fsnotify_prepare_user_wait(struct fsnotify_iter_info *iter_info);
371376

372377
/* put here because inotify does some weird stuff when destroying watches */
373378
extern void fsnotify_init_event(struct fsnotify_event *event,

0 commit comments

Comments
 (0)