Skip to content

Commit 75c1be4

Browse files
committed
fsnotify: srcu to protect read side of inode and vfsmount locks
Currently reading the inode->i_fsnotify_marks or vfsmount->mnt_fsnotify_marks lists are protected by a spinlock on both the read and the write side. This patch protects the read side of those lists with a new single srcu. Signed-off-by: Eric Paris <eparis@redhat.com>
1 parent 700307a commit 75c1be4

5 files changed

Lines changed: 111 additions & 40 deletions

File tree

‎fs/notify/fsnotify.c‎

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -144,29 +144,30 @@ void __fsnotify_flush_ignored_mask(struct inode *inode, void *data, int data_is)
144144
{
145145
struct fsnotify_mark *mark;
146146
struct hlist_node *node;
147+
int idx;
148+
149+
idx = srcu_read_lock(&fsnotify_mark_srcu);
147150

148151
if (!hlist_empty(&inode->i_fsnotify_marks)) {
149-
spin_lock(&inode->i_lock);
150-
hlist_for_each_entry(mark, node, &inode->i_fsnotify_marks, i.i_list) {
152+
hlist_for_each_entry_rcu(mark, node, &inode->i_fsnotify_marks, i.i_list) {
151153
if (!(mark->flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY))
152154
mark->ignored_mask = 0;
153155
}
154-
spin_unlock(&inode->i_lock);
155156
}
156157

157158
if (data_is == FSNOTIFY_EVENT_FILE) {
158159
struct vfsmount *mnt;
159160

160161
mnt = ((struct file *)data)->f_path.mnt;
161162
if (mnt && !hlist_empty(&mnt->mnt_fsnotify_marks)) {
162-
spin_lock(&mnt->mnt_root->d_lock);
163-
hlist_for_each_entry(mark, node, &mnt->mnt_fsnotify_marks, m.m_list) {
163+
hlist_for_each_entry_rcu(mark, node, &mnt->mnt_fsnotify_marks, m.m_list) {
164164
if (!(mark->flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY))
165165
mark->ignored_mask = 0;
166166
}
167-
spin_unlock(&mnt->mnt_root->d_lock);
168167
}
169168
}
169+
170+
srcu_read_unlock(&fsnotify_mark_srcu, idx);
170171
}
171172

172173
static int send_to_group(struct fsnotify_group *group, struct inode *to_tell,
@@ -208,8 +209,10 @@ static bool needed_by_vfsmount(__u32 test_mask, struct vfsmount *mnt)
208209
int fsnotify(struct inode *to_tell, __u32 mask, void *data, int data_is,
209210
const unsigned char *file_name, u32 cookie)
210211
{
212+
struct fsnotify_mark *mark;
211213
struct fsnotify_group *group;
212214
struct fsnotify_event *event = NULL;
215+
struct hlist_node *node;
213216
struct vfsmount *mnt = NULL;
214217
int idx, ret = 0;
215218
/* global tests shouldn't care about events on child only the specific event */
@@ -237,35 +240,47 @@ int fsnotify(struct inode *to_tell, __u32 mask, void *data, int data_is,
237240
!needed_by_vfsmount(test_mask, mnt))
238241
return 0;
239242

240-
/*
241-
* SRCU!! the groups list is very very much read only and the path is
242-
* very hot. The VAST majority of events are not going to need to do
243-
* anything other than walk the list so it's crazy to pre-allocate.
244-
*/
245-
idx = srcu_read_lock(&fsnotify_grp_srcu);
243+
idx = srcu_read_lock(&fsnotify_mark_srcu);
246244

247245
if (test_mask & to_tell->i_fsnotify_mask) {
248-
list_for_each_entry_rcu(group, &fsnotify_inode_groups, inode_group_list) {
249-
if (test_mask & group->mask) {
250-
ret = send_to_group(group, to_tell, NULL, mask, data, data_is,
251-
cookie, file_name, &event);
246+
hlist_for_each_entry_rcu(mark, node, &to_tell->i_fsnotify_marks, i.i_list) {
247+
248+
pr_debug("%s: inode_loop: mark=%p mark->mask=%x mark->ignored_mask=%x\n",
249+
__func__, mark, mark->mask, mark->ignored_mask);
250+
251+
if (test_mask & mark->mask & ~mark->ignored_mask) {
252+
group = mark->group;
253+
if (!group)
254+
continue;
255+
ret = send_to_group(group, to_tell, NULL, mask,
256+
data, data_is, cookie, file_name,
257+
&event);
252258
if (ret)
253259
goto out;
254260
}
255261
}
256262
}
257-
if (needed_by_vfsmount(test_mask, mnt)) {
258-
list_for_each_entry_rcu(group, &fsnotify_vfsmount_groups, vfsmount_group_list) {
259-
if (test_mask & group->mask) {
260-
ret = send_to_group(group, to_tell, mnt, mask, data, data_is,
261-
cookie, file_name, &event);
263+
264+
if (mnt && (test_mask & mnt->mnt_fsnotify_mask)) {
265+
hlist_for_each_entry_rcu(mark, node, &mnt->mnt_fsnotify_marks, m.m_list) {
266+
267+
pr_debug("%s: mnt_loop: mark=%p mark->mask=%x mark->ignored_mask=%x\n",
268+
__func__, mark, mark->mask, mark->ignored_mask);
269+
270+
if (test_mask & mark->mask & ~mark->ignored_mask) {
271+
group = mark->group;
272+
if (!group)
273+
continue;
274+
ret = send_to_group(group, to_tell, mnt, mask,
275+
data, data_is, cookie, file_name,
276+
&event);
262277
if (ret)
263278
goto out;
264279
}
265280
}
266281
}
267282
out:
268-
srcu_read_unlock(&fsnotify_grp_srcu, idx);
283+
srcu_read_unlock(&fsnotify_mark_srcu, idx);
269284
/*
270285
* fsnotify_create_event() took a reference so the event can't be cleaned
271286
* up while we are still trying to add it to lists, drop that one.
@@ -279,8 +294,14 @@ EXPORT_SYMBOL_GPL(fsnotify);
279294

280295
static __init int fsnotify_init(void)
281296
{
297+
int ret;
298+
282299
BUG_ON(hweight32(ALL_FSNOTIFY_EVENTS) != 23);
283300

284-
return init_srcu_struct(&fsnotify_grp_srcu);
301+
ret = init_srcu_struct(&fsnotify_mark_srcu);
302+
if (ret)
303+
panic("initializing fsnotify_mark_srcu");
304+
305+
return 0;
285306
}
286-
subsys_initcall(fsnotify_init);
307+
core_initcall(fsnotify_init);

‎fs/notify/fsnotify.h‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
#include <linux/srcu.h>
77
#include <linux/types.h>
88

9-
/* protects reads of fsnotify_groups */
10-
extern struct srcu_struct fsnotify_grp_srcu;
119
/* all groups which receive inode fsnotify events */
1210
extern struct list_head fsnotify_inode_groups;
1311
/* all groups which receive vfsmount fsnotify events */
@@ -20,6 +18,9 @@ extern __u32 fsnotify_vfsmount_mask;
2018
/* destroy all events sitting in this groups notification queue */
2119
extern void fsnotify_flush_notify(struct fsnotify_group *group);
2220

21+
/* protects reads of inode and vfsmount marks list */
22+
extern struct srcu_struct fsnotify_mark_srcu;
23+
2324
extern void fsnotify_set_inode_mark_mask_locked(struct fsnotify_mark *fsn_mark,
2425
__u32 mask);
2526
/* add a mark to an inode */

‎fs/notify/group.c‎

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@
3030

3131
/* protects writes to fsnotify_groups and fsnotify_mask */
3232
static DEFINE_MUTEX(fsnotify_grp_mutex);
33-
/* protects reads while running the fsnotify_groups list */
34-
struct srcu_struct fsnotify_grp_srcu;
3533
/* all groups registered to receive inode filesystem notifications */
3634
LIST_HEAD(fsnotify_inode_groups);
3735
/* all groups registered to receive mount point filesystem notifications */
@@ -50,18 +48,17 @@ void fsnotify_recalc_global_mask(void)
5048
struct fsnotify_group *group;
5149
__u32 inode_mask = 0;
5250
__u32 vfsmount_mask = 0;
53-
int idx;
5451

55-
idx = srcu_read_lock(&fsnotify_grp_srcu);
52+
mutex_lock(&fsnotify_grp_mutex);
5653
list_for_each_entry_rcu(group, &fsnotify_inode_groups, inode_group_list)
5754
inode_mask |= group->mask;
5855
list_for_each_entry_rcu(group, &fsnotify_vfsmount_groups, vfsmount_group_list)
5956
vfsmount_mask |= group->mask;
60-
61-
srcu_read_unlock(&fsnotify_grp_srcu, idx);
6257

6358
fsnotify_inode_mask = inode_mask;
6459
fsnotify_vfsmount_mask = vfsmount_mask;
60+
61+
mutex_unlock(&fsnotify_grp_mutex);
6562
}
6663

6764
/*
@@ -168,6 +165,8 @@ static void fsnotify_destroy_group(struct fsnotify_group *group)
168165
/* clear all inode marks for this group */
169166
fsnotify_clear_marks_by_group(group);
170167

168+
synchronize_srcu(&fsnotify_mark_srcu);
169+
171170
/* past the point of no return, matches the initial value of 1 */
172171
if (atomic_dec_and_test(&group->num_marks))
173172
fsnotify_final_destroy_group(group);
@@ -216,12 +215,7 @@ void fsnotify_put_group(struct fsnotify_group *group)
216215
*/
217216
__fsnotify_evict_group(group);
218217

219-
/*
220-
* now it's off the list, so the only thing we might care about is
221-
* srcu access....
222-
*/
223218
mutex_unlock(&fsnotify_grp_mutex);
224-
synchronize_srcu(&fsnotify_grp_srcu);
225219

226220
/* and now it is really dead. _Nothing_ could be seeing it */
227221
fsnotify_recalc_global_mask();

‎fs/notify/mark.c‎

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,24 @@
8585
#include <linux/fs.h>
8686
#include <linux/init.h>
8787
#include <linux/kernel.h>
88+
#include <linux/kthread.h>
8889
#include <linux/module.h>
8990
#include <linux/mutex.h>
9091
#include <linux/slab.h>
9192
#include <linux/spinlock.h>
93+
#include <linux/srcu.h>
9294
#include <linux/writeback.h> /* for inode_lock */
9395

9496
#include <asm/atomic.h>
9597

9698
#include <linux/fsnotify_backend.h>
9799
#include "fsnotify.h"
98100

101+
struct srcu_struct fsnotify_mark_srcu;
102+
static DEFINE_SPINLOCK(destroy_lock);
103+
static LIST_HEAD(destroy_list);
104+
static DECLARE_WAIT_QUEUE_HEAD(destroy_waitq);
105+
99106
void fsnotify_get_mark(struct fsnotify_mark *mark)
100107
{
101108
atomic_inc(&mark->refcnt);
@@ -144,11 +151,14 @@ void fsnotify_destroy_mark(struct fsnotify_mark *mark)
144151

145152
list_del_init(&mark->g_list);
146153

147-
fsnotify_put_mark(mark); /* for i_list and g_list */
148-
149154
spin_unlock(&group->mark_lock);
150155
spin_unlock(&mark->lock);
151156

157+
spin_lock(&destroy_lock);
158+
list_add(&mark->destroy_list, &destroy_list);
159+
spin_unlock(&destroy_lock);
160+
wake_up(&destroy_waitq);
161+
152162
/*
153163
* Some groups like to know that marks are being freed. This is a
154164
* callback to the group function to let it know that this mark
@@ -263,12 +273,17 @@ int fsnotify_add_mark(struct fsnotify_mark *mark,
263273
err:
264274
mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE;
265275
list_del_init(&mark->g_list);
276+
mark->group = NULL;
266277
atomic_dec(&group->num_marks);
267-
fsnotify_put_mark(mark);
268278

269279
spin_unlock(&group->mark_lock);
270280
spin_unlock(&mark->lock);
271281

282+
spin_lock(&destroy_lock);
283+
list_add(&mark->destroy_list, &destroy_list);
284+
spin_unlock(&destroy_lock);
285+
wake_up(&destroy_waitq);
286+
272287
return ret;
273288
}
274289

@@ -326,3 +341,42 @@ void fsnotify_init_mark(struct fsnotify_mark *mark,
326341
atomic_set(&mark->refcnt, 1);
327342
mark->free_mark = free_mark;
328343
}
344+
345+
static int fsnotify_mark_destroy(void *ignored)
346+
{
347+
struct fsnotify_mark *mark, *next;
348+
LIST_HEAD(private_destroy_list);
349+
350+
for (;;) {
351+
spin_lock(&destroy_lock);
352+
list_for_each_entry_safe(mark, next, &destroy_list, destroy_list) {
353+
list_del(&mark->destroy_list);
354+
list_add(&mark->destroy_list, &private_destroy_list);
355+
}
356+
spin_unlock(&destroy_lock);
357+
358+
synchronize_srcu(&fsnotify_mark_srcu);
359+
360+
list_for_each_entry_safe(mark, next, &private_destroy_list, destroy_list) {
361+
list_del_init(&mark->destroy_list);
362+
fsnotify_put_mark(mark);
363+
}
364+
365+
wait_event_interruptible(destroy_waitq, !list_empty(&destroy_list));
366+
}
367+
368+
return 0;
369+
}
370+
371+
static int __init fsnotify_mark_init(void)
372+
{
373+
struct task_struct *thread;
374+
375+
thread = kthread_run(fsnotify_mark_destroy, NULL,
376+
"fsnotify_mark");
377+
if (IS_ERR(thread))
378+
panic("unable to start fsnotify mark destruction thread.");
379+
380+
return 0;
381+
}
382+
device_initcall(fsnotify_mark_init);

‎include/linux/fsnotify_backend.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ struct fsnotify_mark {
302302
#define FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY 0x08
303303
#define FSNOTIFY_MARK_FLAG_ALIVE 0x10
304304
unsigned int flags; /* vfsmount or inode mark? */
305+
struct list_head destroy_list;
305306
void (*free_mark)(struct fsnotify_mark *mark); /* called on final put+free */
306307
};
307308

0 commit comments

Comments
 (0)