Skip to content

Commit 6b3f05d

Browse files
committed
fsnotify: Detach mark from object list when last reference is dropped
Instead of removing mark from object list from fsnotify_detach_mark(), remove the mark when last reference to the mark is dropped. This will allow fanotify to wait for userspace response to event without having to hold onto fsnotify_mark_srcu. To avoid pinning inodes by elevated refcount (and thus e.g. delaying file deletion) while someone holds mark reference, we detach connector from the object also from fsnotify_destroy_marks() and not only after removing last mark from the list as it was now. Reviewed-by: Miklos Szeredi <mszeredi@redhat.com> Reviewed-by: Amir Goldstein <amir73il@gmail.com> Signed-off-by: Jan Kara <jack@suse.cz>
1 parent 1137514 commit 6b3f05d

3 files changed

Lines changed: 105 additions & 77 deletions

File tree

‎fs/notify/mark.c‎

Lines changed: 88 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,13 @@
4949
*
5050
* A list of notification marks relating to inode / mnt is contained in
5151
* fsnotify_mark_connector. That structure is alive as long as there are any
52-
* marks in the list and is also protected by fsnotify_mark_srcu.
52+
* marks in the list and is also protected by fsnotify_mark_srcu. A mark gets
53+
* detached from fsnotify_mark_connector when last reference to the mark is
54+
* dropped. Thus having mark reference is enough to protect mark->connector
55+
* pointer and to make sure fsnotify_mark_connector cannot disappear. Also
56+
* because we remove mark from g_list before dropping mark reference associated
57+
* with that, any mark found through g_list is guaranteed to have
58+
* mark->connector set until we drop group->mark_mutex.
5359
*
5460
* LIFETIME:
5561
* Inode marks survive between when they are added to an inode and when their
@@ -103,26 +109,16 @@ void fsnotify_get_mark(struct fsnotify_mark *mark)
103109
atomic_inc(&mark->refcnt);
104110
}
105111

106-
void fsnotify_put_mark(struct fsnotify_mark *mark)
107-
{
108-
if (atomic_dec_and_test(&mark->refcnt)) {
109-
spin_lock(&destroy_lock);
110-
list_add(&mark->g_list, &destroy_list);
111-
spin_unlock(&destroy_lock);
112-
queue_delayed_work(system_unbound_wq, &reaper_work,
113-
FSNOTIFY_REAPER_DELAY);
114-
}
115-
}
116-
117112
static void __fsnotify_recalc_mask(struct fsnotify_mark_connector *conn)
118113
{
119114
u32 new_mask = 0;
120115
struct fsnotify_mark *mark;
121116

122117
assert_spin_locked(&conn->lock);
123-
hlist_for_each_entry(mark, &conn->list, obj_list)
124-
new_mask |= mark->mask;
125-
118+
hlist_for_each_entry(mark, &conn->list, obj_list) {
119+
if (mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)
120+
new_mask |= mark->mask;
121+
}
126122
if (conn->flags & FSNOTIFY_OBJ_TYPE_INODE)
127123
conn->inode->i_fsnotify_mask = new_mask;
128124
else if (conn->flags & FSNOTIFY_OBJ_TYPE_VFSMOUNT)
@@ -131,8 +127,9 @@ static void __fsnotify_recalc_mask(struct fsnotify_mark_connector *conn)
131127

132128
/*
133129
* Calculate mask of events for a list of marks. The caller must make sure
134-
* connector cannot disappear under us (usually by holding a mark->lock or
135-
* mark->group->mark_mutex for a mark on this list).
130+
* connector and connector->inode cannot disappear under us. Callers achieve
131+
* this by holding a mark->lock or mark->group->mark_mutex for a mark on this
132+
* list.
136133
*/
137134
void fsnotify_recalc_mask(struct fsnotify_mark_connector *conn)
138135
{
@@ -164,7 +161,6 @@ static void fsnotify_connector_destroy_workfn(struct work_struct *work)
164161
}
165162
}
166163

167-
168164
static struct inode *fsnotify_detach_connector_from_object(
169165
struct fsnotify_mark_connector *conn)
170166
{
@@ -187,14 +183,34 @@ static struct inode *fsnotify_detach_connector_from_object(
187183
return inode;
188184
}
189185

190-
static struct inode *fsnotify_detach_from_object(struct fsnotify_mark *mark)
186+
static void fsnotify_final_mark_destroy(struct fsnotify_mark *mark)
187+
{
188+
if (mark->group)
189+
fsnotify_put_group(mark->group);
190+
mark->free_mark(mark);
191+
}
192+
193+
void fsnotify_put_mark(struct fsnotify_mark *mark)
191194
{
192195
struct fsnotify_mark_connector *conn;
193196
struct inode *inode = NULL;
194197
bool free_conn = false;
195198

199+
/* Catch marks that were actually never attached to object */
200+
if (!mark->connector) {
201+
if (atomic_dec_and_test(&mark->refcnt))
202+
fsnotify_final_mark_destroy(mark);
203+
return;
204+
}
205+
206+
/*
207+
* We have to be careful so that traversals of obj_list under lock can
208+
* safely grab mark reference.
209+
*/
210+
if (!atomic_dec_and_lock(&mark->refcnt, &mark->connector->lock))
211+
return;
212+
196213
conn = mark->connector;
197-
spin_lock(&conn->lock);
198214
hlist_del_init_rcu(&mark->obj_list);
199215
if (hlist_empty(&conn->list)) {
200216
inode = fsnotify_detach_connector_from_object(conn);
@@ -205,27 +221,40 @@ static struct inode *fsnotify_detach_from_object(struct fsnotify_mark *mark)
205221
mark->connector = NULL;
206222
spin_unlock(&conn->lock);
207223

224+
iput(inode);
225+
208226
if (free_conn) {
209227
spin_lock(&destroy_lock);
210228
conn->destroy_next = connector_destroy_list;
211229
connector_destroy_list = conn;
212230
spin_unlock(&destroy_lock);
213231
queue_work(system_unbound_wq, &connector_reaper_work);
214232
}
215-
216-
return inode;
233+
/*
234+
* Note that we didn't update flags telling whether inode cares about
235+
* what's happening with children. We update these flags from
236+
* __fsnotify_parent() lazily when next event happens on one of our
237+
* children.
238+
*/
239+
spin_lock(&destroy_lock);
240+
list_add(&mark->g_list, &destroy_list);
241+
spin_unlock(&destroy_lock);
242+
queue_delayed_work(system_unbound_wq, &reaper_work,
243+
FSNOTIFY_REAPER_DELAY);
217244
}
218245

219246
/*
220-
* Remove mark from inode / vfsmount list, group list, drop inode reference
221-
* if we got one.
247+
* Mark mark as detached, remove it from group list. Mark still stays in object
248+
* list until its last reference is dropped. Note that we rely on mark being
249+
* removed from group list before corresponding reference to it is dropped. In
250+
* particular we rely on mark->connector being valid while we hold
251+
* group->mark_mutex if we found the mark through g_list.
222252
*
223253
* Must be called with group->mark_mutex held. The caller must either hold
224254
* reference to the mark or be protected by fsnotify_mark_srcu.
225255
*/
226256
void fsnotify_detach_mark(struct fsnotify_mark *mark)
227257
{
228-
struct inode *inode = NULL;
229258
struct fsnotify_group *group = mark->group;
230259

231260
WARN_ON_ONCE(!mutex_is_locked(&group->mark_mutex));
@@ -234,31 +263,15 @@ void fsnotify_detach_mark(struct fsnotify_mark *mark)
234263
!!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED));
235264

236265
spin_lock(&mark->lock);
237-
238266
/* something else already called this function on this mark */
239267
if (!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) {
240268
spin_unlock(&mark->lock);
241269
return;
242270
}
243-
244271
mark->flags &= ~FSNOTIFY_MARK_FLAG_ATTACHED;
245-
246-
inode = fsnotify_detach_from_object(mark);
247-
248-
/*
249-
* Note that we didn't update flags telling whether inode cares about
250-
* what's happening with children. We update these flags from
251-
* __fsnotify_parent() lazily when next event happens on one of our
252-
* children.
253-
*/
254-
255272
list_del_init(&mark->g_list);
256-
257273
spin_unlock(&mark->lock);
258274

259-
if (inode)
260-
iput(inode);
261-
262275
atomic_dec(&group->num_marks);
263276

264277
/* Drop mark reference acquired in fsnotify_add_mark_locked() */
@@ -458,7 +471,9 @@ static int fsnotify_add_mark_list(struct fsnotify_mark *mark,
458471
hlist_for_each_entry(lmark, &conn->list, obj_list) {
459472
last = lmark;
460473

461-
if ((lmark->group == mark->group) && !allow_dups) {
474+
if ((lmark->group == mark->group) &&
475+
(lmark->flags & FSNOTIFY_MARK_FLAG_ATTACHED) &&
476+
!allow_dups) {
462477
err = -EEXIST;
463478
goto out_err;
464479
}
@@ -509,7 +524,7 @@ int fsnotify_add_mark_locked(struct fsnotify_mark *mark,
509524
mark->group = group;
510525
list_add(&mark->g_list, &group->marks_list);
511526
atomic_inc(&group->num_marks);
512-
fsnotify_get_mark(mark); /* for i_list and g_list */
527+
fsnotify_get_mark(mark); /* for g_list */
513528
spin_unlock(&mark->lock);
514529

515530
ret = fsnotify_add_mark_list(mark, inode, mnt, allow_dups);
@@ -557,7 +572,8 @@ struct fsnotify_mark *fsnotify_find_mark(
557572
return NULL;
558573

559574
hlist_for_each_entry(mark, &conn->list, obj_list) {
560-
if (mark->group == group) {
575+
if (mark->group == group &&
576+
(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) {
561577
fsnotify_get_mark(mark);
562578
spin_unlock(&conn->lock);
563579
return mark;
@@ -637,23 +653,38 @@ void fsnotify_detach_group_marks(struct fsnotify_group *group)
637653
void fsnotify_destroy_marks(struct fsnotify_mark_connector __rcu **connp)
638654
{
639655
struct fsnotify_mark_connector *conn;
640-
struct fsnotify_mark *mark;
656+
struct fsnotify_mark *mark, *old_mark = NULL;
657+
struct inode *inode;
641658

642-
while ((conn = fsnotify_grab_connector(connp))) {
643-
/*
644-
* We have to be careful since we can race with e.g.
645-
* fsnotify_clear_marks_by_group() and once we drop the list
646-
* lock, mark can get removed from the obj_list and destroyed.
647-
* But we are holding mark reference so mark cannot be freed
648-
* and calling fsnotify_destroy_mark() more than once is fine.
649-
*/
650-
mark = hlist_entry(conn->list.first, struct fsnotify_mark,
651-
obj_list);
659+
conn = fsnotify_grab_connector(connp);
660+
if (!conn)
661+
return;
662+
/*
663+
* We have to be careful since we can race with e.g.
664+
* fsnotify_clear_marks_by_group() and once we drop the conn->lock, the
665+
* list can get modified. However we are holding mark reference and
666+
* thus our mark cannot be removed from obj_list so we can continue
667+
* iteration after regaining conn->lock.
668+
*/
669+
hlist_for_each_entry(mark, &conn->list, obj_list) {
652670
fsnotify_get_mark(mark);
653671
spin_unlock(&conn->lock);
672+
if (old_mark)
673+
fsnotify_put_mark(old_mark);
674+
old_mark = mark;
654675
fsnotify_destroy_mark(mark, mark->group);
655-
fsnotify_put_mark(mark);
676+
spin_lock(&conn->lock);
656677
}
678+
/*
679+
* Detach list from object now so that we don't pin inode until all
680+
* mark references get dropped. It would lead to strange results such
681+
* as delaying inode deletion or blocking unmount.
682+
*/
683+
inode = fsnotify_detach_connector_from_object(conn);
684+
spin_unlock(&conn->lock);
685+
if (old_mark)
686+
fsnotify_put_mark(old_mark);
687+
iput(inode);
657688
}
658689

659690
/*
@@ -686,9 +717,7 @@ void fsnotify_mark_destroy_list(void)
686717

687718
list_for_each_entry_safe(mark, next, &private_destroy_list, g_list) {
688719
list_del_init(&mark->g_list);
689-
if (mark->group)
690-
fsnotify_put_group(mark->group);
691-
mark->free_mark(mark);
720+
fsnotify_final_mark_destroy(mark);
692721
}
693722
}
694723

‎include/linux/fsnotify_backend.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,9 @@ struct fsnotify_mark {
245245
struct list_head g_list;
246246
/* Protects inode / mnt pointers, flags, masks */
247247
spinlock_t lock;
248-
/* List of marks for inode / vfsmount [connector->lock] */
248+
/* List of marks for inode / vfsmount [connector->lock, mark ref] */
249249
struct hlist_node obj_list;
250-
/* Head of list of marks for an object [mark->lock, group->mark_mutex] */
250+
/* Head of list of marks for an object [mark ref] */
251251
struct fsnotify_mark_connector *connector;
252252
/* Events types to ignore [mark->lock, group->mark_mutex] */
253253
__u32 ignored_mask;

‎kernel/audit_tree.c‎

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -172,27 +172,18 @@ static unsigned long inode_to_key(const struct inode *inode)
172172
/*
173173
* Function to return search key in our hash from chunk. Key 0 is special and
174174
* should never be present in the hash.
175-
*
176-
* Must be called with chunk->mark.lock held to protect from connector
177-
* becoming NULL.
178175
*/
179-
static unsigned long __chunk_to_key(struct audit_chunk *chunk)
176+
static unsigned long chunk_to_key(struct audit_chunk *chunk)
180177
{
181-
if (!chunk->mark.connector)
178+
/*
179+
* We have a reference to the mark so it should be attached to a
180+
* connector.
181+
*/
182+
if (WARN_ON_ONCE(!chunk->mark.connector))
182183
return 0;
183184
return (unsigned long)chunk->mark.connector->inode;
184185
}
185186

186-
static unsigned long chunk_to_key(struct audit_chunk *chunk)
187-
{
188-
unsigned long key;
189-
190-
spin_lock(&chunk->mark.lock);
191-
key = __chunk_to_key(chunk);
192-
spin_unlock(&chunk->mark.lock);
193-
return key;
194-
}
195-
196187
static inline struct list_head *chunk_hash(unsigned long key)
197188
{
198189
unsigned long n = key / L1_CACHE_BYTES;
@@ -202,7 +193,7 @@ static inline struct list_head *chunk_hash(unsigned long key)
202193
/* hash_lock & entry->lock is held by caller */
203194
static void insert_hash(struct audit_chunk *chunk)
204195
{
205-
unsigned long key = __chunk_to_key(chunk);
196+
unsigned long key = chunk_to_key(chunk);
206197
struct list_head *list;
207198

208199
if (!(chunk->mark.flags & FSNOTIFY_MARK_FLAG_ATTACHED))
@@ -263,6 +254,10 @@ static void untag_chunk(struct node *p)
263254

264255
mutex_lock(&entry->group->mark_mutex);
265256
spin_lock(&entry->lock);
257+
/*
258+
* mark_mutex protects mark from getting detached and thus also from
259+
* mark->connector->inode getting NULL.
260+
*/
266261
if (chunk->dead || !(entry->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) {
267262
spin_unlock(&entry->lock);
268263
mutex_unlock(&entry->group->mark_mutex);
@@ -423,6 +418,10 @@ static int tag_chunk(struct inode *inode, struct audit_tree *tree)
423418

424419
mutex_lock(&old_entry->group->mark_mutex);
425420
spin_lock(&old_entry->lock);
421+
/*
422+
* mark_mutex protects mark from getting detached and thus also from
423+
* mark->connector->inode getting NULL.
424+
*/
426425
if (!(old_entry->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) {
427426
/* old_entry is being shot, lets just lie */
428427
spin_unlock(&old_entry->lock);

0 commit comments

Comments
 (0)