Skip to content

Commit 86ffe24

Browse files
committed
fsnotify: Move object pointer to fsnotify_mark_connector
Move pointer to inode / vfsmount from mark itself to the fsnotify_mark_connector structure. This is another step on the path towards decoupling inode / vfsmount lifetime from notification mark lifetime. Reviewed-by: Miklos Szeredi <mszeredi@redhat.com> Reviewed-by: Amir Goldstein <amir73il@gmail.com> Signed-off-by: Jan Kara <jack@suse.cz>
1 parent 9dd813c commit 86ffe24

8 files changed

Lines changed: 74 additions & 49 deletions

File tree

‎fs/notify/dnotify/dnotify.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ static void dnotify_recalc_inode_mask(struct fsnotify_mark *fsn_mark)
6969
if (old_mask == new_mask)
7070
return;
7171

72-
if (fsn_mark->inode)
73-
fsnotify_recalc_inode_mask(fsn_mark->inode);
72+
if (fsn_mark->connector)
73+
fsnotify_recalc_inode_mask(fsn_mark->connector->inode);
7474
}
7575

7676
/*

‎fs/notify/fdinfo.c‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ static void inotify_fdinfo(struct seq_file *m, struct fsnotify_mark *mark)
7676
struct inotify_inode_mark *inode_mark;
7777
struct inode *inode;
7878

79-
if (!(mark->flags & FSNOTIFY_MARK_FLAG_INODE))
79+
if (!(mark->connector->flags & FSNOTIFY_OBJ_TYPE_INODE))
8080
return;
8181

8282
inode_mark = container_of(mark, struct inotify_inode_mark, fsn_mark);
83-
inode = igrab(mark->inode);
83+
inode = igrab(mark->connector->inode);
8484
if (inode) {
8585
/*
8686
* IN_ALL_EVENTS represents all of the mask bits
@@ -115,8 +115,8 @@ static void fanotify_fdinfo(struct seq_file *m, struct fsnotify_mark *mark)
115115
if (mark->flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY)
116116
mflags |= FAN_MARK_IGNORED_SURV_MODIFY;
117117

118-
if (mark->flags & FSNOTIFY_MARK_FLAG_INODE) {
119-
inode = igrab(mark->inode);
118+
if (mark->connector->flags & FSNOTIFY_OBJ_TYPE_INODE) {
119+
inode = igrab(mark->connector->inode);
120120
if (!inode)
121121
return;
122122
seq_printf(m, "fanotify ino:%lx sdev:%x mflags:%x mask:%x ignored_mask:%x ",
@@ -125,8 +125,8 @@ static void fanotify_fdinfo(struct seq_file *m, struct fsnotify_mark *mark)
125125
show_mark_fhandle(m, inode);
126126
seq_putc(m, '\n');
127127
iput(inode);
128-
} else if (mark->flags & FSNOTIFY_MARK_FLAG_VFSMOUNT) {
129-
struct mount *mnt = real_mount(mark->mnt);
128+
} else if (mark->connector->flags & FSNOTIFY_OBJ_TYPE_VFSMOUNT) {
129+
struct mount *mnt = real_mount(mark->connector->mnt);
130130

131131
seq_printf(m, "fanotify mnt_id:%x mflags:%x mask:%x ignored_mask:%x\n",
132132
mnt->mnt_id, mflags, mark->mask, mark->ignored_mask);

‎fs/notify/fsnotify.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ extern void fsnotify_set_inode_mark_mask_locked(struct fsnotify_mark *fsn_mark,
2626
/* Add mark to a proper place in mark list */
2727
extern int fsnotify_add_mark_list(struct fsnotify_mark_connector **connp,
2828
struct fsnotify_mark *mark,
29+
struct inode *inode, struct vfsmount *mnt,
2930
int allow_dups);
3031
/* add a mark to an inode */
3132
extern int fsnotify_add_inode_mark(struct fsnotify_mark *mark,
@@ -44,7 +45,7 @@ extern void fsnotify_destroy_inode_mark(struct fsnotify_mark *mark);
4445
extern struct fsnotify_mark *fsnotify_find_mark(
4546
struct fsnotify_mark_connector *conn,
4647
struct fsnotify_group *group);
47-
/* Destroy all marks in the given list protected by 'lock' */
48+
/* Destroy all marks connected via given connector protected by 'lock' */
4849
extern void fsnotify_destroy_marks(struct fsnotify_mark_connector *conn,
4950
spinlock_t *lock);
5051
/* run the list of all marks associated with inode and destroy them */

‎fs/notify/inode_mark.c‎

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ void fsnotify_recalc_inode_mask(struct inode *inode)
4545

4646
void fsnotify_destroy_inode_mark(struct fsnotify_mark *mark)
4747
{
48-
struct inode *inode = mark->inode;
48+
struct inode *inode = mark->connector->inode;
4949

5050
BUG_ON(!mutex_is_locked(&mark->group->mark_mutex));
5151
assert_spin_locked(&mark->lock);
5252

5353
spin_lock(&inode->i_lock);
5454

5555
hlist_del_init_rcu(&mark->obj_list);
56-
mark->inode = NULL;
56+
mark->connector = NULL;
5757

5858
/*
5959
* this mark is now off the inode->i_fsnotify_marks list and we
@@ -69,7 +69,7 @@ void fsnotify_destroy_inode_mark(struct fsnotify_mark *mark)
6969
*/
7070
void fsnotify_clear_inode_marks_by_group(struct fsnotify_group *group)
7171
{
72-
fsnotify_clear_marks_by_group_flags(group, FSNOTIFY_MARK_FLAG_INODE);
72+
fsnotify_clear_marks_by_group_flags(group, FSNOTIFY_OBJ_TYPE_INODE);
7373
}
7474

7575
/*
@@ -99,11 +99,10 @@ void fsnotify_set_inode_mark_mask_locked(struct fsnotify_mark *mark,
9999

100100
assert_spin_locked(&mark->lock);
101101

102-
if (mask &&
103-
mark->inode &&
102+
if (mask && mark->connector &&
104103
!(mark->flags & FSNOTIFY_MARK_FLAG_OBJECT_PINNED)) {
105104
mark->flags |= FSNOTIFY_MARK_FLAG_OBJECT_PINNED;
106-
inode = igrab(mark->inode);
105+
inode = igrab(mark->connector->inode);
107106
/*
108107
* we shouldn't be able to get here if the inode wasn't
109108
* already safely held in memory. But bug in case it
@@ -126,15 +125,12 @@ int fsnotify_add_inode_mark(struct fsnotify_mark *mark,
126125
{
127126
int ret;
128127

129-
mark->flags |= FSNOTIFY_MARK_FLAG_INODE;
130-
131128
BUG_ON(!mutex_is_locked(&group->mark_mutex));
132129
assert_spin_locked(&mark->lock);
133130

134131
spin_lock(&inode->i_lock);
135-
mark->inode = inode;
136-
ret = fsnotify_add_mark_list(&inode->i_fsnotify_marks, mark,
137-
allow_dups);
132+
ret = fsnotify_add_mark_list(&inode->i_fsnotify_marks, mark, inode,
133+
NULL, allow_dups);
138134
inode->i_fsnotify_mask = fsnotify_recalc_mask(inode->i_fsnotify_marks);
139135
spin_unlock(&inode->i_lock);
140136

‎fs/notify/mark.c‎

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,10 @@ void fsnotify_detach_mark(struct fsnotify_mark *mark)
142142

143143
mark->flags &= ~FSNOTIFY_MARK_FLAG_ATTACHED;
144144

145-
if (mark->flags & FSNOTIFY_MARK_FLAG_INODE) {
146-
inode = mark->inode;
145+
if (mark->connector->flags & FSNOTIFY_OBJ_TYPE_INODE) {
146+
inode = mark->connector->inode;
147147
fsnotify_destroy_inode_mark(mark);
148-
} else if (mark->flags & FSNOTIFY_MARK_FLAG_VFSMOUNT)
148+
} else if (mark->connector->flags & FSNOTIFY_OBJ_TYPE_VFSMOUNT)
149149
fsnotify_destroy_vfsmount_mark(mark);
150150
else
151151
BUG();
@@ -275,7 +275,7 @@ void fsnotify_set_mark_mask_locked(struct fsnotify_mark *mark, __u32 mask)
275275

276276
mark->mask = mask;
277277

278-
if (mark->flags & FSNOTIFY_MARK_FLAG_INODE)
278+
if (mark->connector && mark->connector->flags & FSNOTIFY_OBJ_TYPE_INODE)
279279
fsnotify_set_inode_mark_mask_locked(mark, mask);
280280
}
281281

@@ -323,14 +323,23 @@ int fsnotify_compare_groups(struct fsnotify_group *a, struct fsnotify_group *b)
323323
}
324324

325325
static int fsnotify_attach_connector_to_object(
326-
struct fsnotify_mark_connector **connp)
326+
struct fsnotify_mark_connector **connp,
327+
struct inode *inode,
328+
struct vfsmount *mnt)
327329
{
328330
struct fsnotify_mark_connector *conn;
329331

330332
conn = kmem_cache_alloc(fsnotify_mark_connector_cachep, GFP_ATOMIC);
331333
if (!conn)
332334
return -ENOMEM;
333335
INIT_HLIST_HEAD(&conn->list);
336+
if (inode) {
337+
conn->flags = FSNOTIFY_OBJ_TYPE_INODE;
338+
conn->inode = inode;
339+
} else {
340+
conn->flags = FSNOTIFY_OBJ_TYPE_VFSMOUNT;
341+
conn->mnt = mnt;
342+
}
334343
/*
335344
* Make sure 'conn' initialization is visible. Matches
336345
* lockless_dereference() in fsnotify().
@@ -348,15 +357,16 @@ static int fsnotify_attach_connector_to_object(
348357
* priority, highest number first, and then by the group's location in memory.
349358
*/
350359
int fsnotify_add_mark_list(struct fsnotify_mark_connector **connp,
351-
struct fsnotify_mark *mark, int allow_dups)
360+
struct fsnotify_mark *mark, struct inode *inode,
361+
struct vfsmount *mnt, int allow_dups)
352362
{
353363
struct fsnotify_mark *lmark, *last = NULL;
354364
struct fsnotify_mark_connector *conn;
355365
int cmp;
356366
int err;
357367

358368
if (!*connp) {
359-
err = fsnotify_attach_connector_to_object(connp);
369+
err = fsnotify_attach_connector_to_object(connp, inode, mnt);
360370
if (err)
361371
return err;
362372
}
@@ -365,7 +375,7 @@ int fsnotify_add_mark_list(struct fsnotify_mark_connector **connp,
365375
/* is mark the first mark? */
366376
if (hlist_empty(&conn->list)) {
367377
hlist_add_head_rcu(&mark->obj_list, &conn->list);
368-
return 0;
378+
goto added;
369379
}
370380

371381
/* should mark be in the middle of the current list? */
@@ -378,13 +388,15 @@ int fsnotify_add_mark_list(struct fsnotify_mark_connector **connp,
378388
cmp = fsnotify_compare_groups(lmark->group, mark->group);
379389
if (cmp >= 0) {
380390
hlist_add_before_rcu(&mark->obj_list, &lmark->obj_list);
381-
return 0;
391+
goto added;
382392
}
383393
}
384394

385395
BUG_ON(last == NULL);
386396
/* mark should be the last entry. last is the current last entry */
387397
hlist_add_behind_rcu(&mark->obj_list, &last->obj_list);
398+
added:
399+
mark->connector = conn;
388400
return 0;
389401
}
390402

@@ -507,7 +519,7 @@ void fsnotify_clear_marks_by_group_flags(struct fsnotify_group *group,
507519
*/
508520
mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
509521
list_for_each_entry_safe(mark, lmark, &group->marks_list, g_list) {
510-
if (mark->flags & flags)
522+
if (mark->connector->flags & flags)
511523
list_move(&mark->g_list, &to_free);
512524
}
513525
mutex_unlock(&group->mark_mutex);

‎fs/notify/vfsmount_mark.c‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
void fsnotify_clear_vfsmount_marks_by_group(struct fsnotify_group *group)
3333
{
34-
fsnotify_clear_marks_by_group_flags(group, FSNOTIFY_MARK_FLAG_VFSMOUNT);
34+
fsnotify_clear_marks_by_group_flags(group, FSNOTIFY_OBJ_TYPE_VFSMOUNT);
3535
}
3636

3737
/*
@@ -49,7 +49,7 @@ void fsnotify_recalc_vfsmount_mask(struct vfsmount *mnt)
4949

5050
void fsnotify_destroy_vfsmount_mark(struct fsnotify_mark *mark)
5151
{
52-
struct vfsmount *mnt = mark->mnt;
52+
struct vfsmount *mnt = mark->connector->mnt;
5353
struct mount *m = real_mount(mnt);
5454

5555
BUG_ON(!mutex_is_locked(&mark->group->mark_mutex));
@@ -58,7 +58,7 @@ void fsnotify_destroy_vfsmount_mark(struct fsnotify_mark *mark)
5858
spin_lock(&mnt->mnt_root->d_lock);
5959

6060
hlist_del_init_rcu(&mark->obj_list);
61-
mark->mnt = NULL;
61+
mark->connector = NULL;
6262

6363
m->mnt_fsnotify_mask = fsnotify_recalc_mask(m->mnt_fsnotify_marks);
6464
spin_unlock(&mnt->mnt_root->d_lock);
@@ -93,14 +93,12 @@ int fsnotify_add_vfsmount_mark(struct fsnotify_mark *mark,
9393
struct mount *m = real_mount(mnt);
9494
int ret;
9595

96-
mark->flags |= FSNOTIFY_MARK_FLAG_VFSMOUNT;
97-
9896
BUG_ON(!mutex_is_locked(&group->mark_mutex));
9997
assert_spin_locked(&mark->lock);
10098

10199
spin_lock(&mnt->mnt_root->d_lock);
102-
mark->mnt = mnt;
103-
ret = fsnotify_add_mark_list(&m->mnt_fsnotify_marks, mark, allow_dups);
100+
ret = fsnotify_add_mark_list(&m->mnt_fsnotify_marks, mark, NULL, mnt,
101+
allow_dups);
104102
m->mnt_fsnotify_mask = fsnotify_recalc_mask(m->mnt_fsnotify_marks);
105103
spin_unlock(&mnt->mnt_root->d_lock);
106104

‎include/linux/fsnotify_backend.h‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,13 @@ struct fsnotify_group {
200200
* freed.
201201
*/
202202
struct fsnotify_mark_connector {
203+
#define FSNOTIFY_OBJ_TYPE_INODE 0x01
204+
#define FSNOTIFY_OBJ_TYPE_VFSMOUNT 0x02
205+
unsigned int flags; /* Type of object [lock] */
206+
union { /* Object pointer [lock] */
207+
struct inode *inode;
208+
struct vfsmount *mnt;
209+
};
203210
struct hlist_head list;
204211
};
205212

@@ -234,14 +241,10 @@ struct fsnotify_mark {
234241
spinlock_t lock;
235242
/* List of marks for inode / vfsmount [obj_lock] */
236243
struct hlist_node obj_list;
237-
union { /* Object pointer [mark->lock, group->mark_mutex] */
238-
struct inode *inode; /* inode this mark is associated with */
239-
struct vfsmount *mnt; /* vfsmount this mark is associated with */
240-
};
244+
/* Head of list of marks for an object [mark->lock, group->mark_mutex] */
245+
struct fsnotify_mark_connector *connector;
241246
/* Events types to ignore [mark->lock, group->mark_mutex] */
242247
__u32 ignored_mask;
243-
#define FSNOTIFY_MARK_FLAG_INODE 0x01
244-
#define FSNOTIFY_MARK_FLAG_VFSMOUNT 0x02
245248
#define FSNOTIFY_MARK_FLAG_OBJECT_PINNED 0x04
246249
#define FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY 0x08
247250
#define FSNOTIFY_MARK_FLAG_ALIVE 0x10
@@ -353,7 +356,7 @@ extern void fsnotify_free_mark(struct fsnotify_mark *mark);
353356
extern void fsnotify_clear_vfsmount_marks_by_group(struct fsnotify_group *group);
354357
/* run all the marks in a group, and clear all of the inode marks */
355358
extern void fsnotify_clear_inode_marks_by_group(struct fsnotify_group *group);
356-
/* run all the marks in a group, and clear all of the marks where mark->flags & flags is true*/
359+
/* run all the marks in a group, and clear all of the marks attached to given object type */
357360
extern void fsnotify_clear_marks_by_group_flags(struct fsnotify_group *group, unsigned int flags);
358361
extern void fsnotify_connector_free(struct fsnotify_mark_connector **connp);
359362
extern void fsnotify_get_mark(struct fsnotify_mark *mark);

‎kernel/audit_tree.c‎

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,25 @@ 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.
175178
*/
179+
static unsigned long __chunk_to_key(struct audit_chunk *chunk)
180+
{
181+
if (!chunk->mark.connector)
182+
return 0;
183+
return (unsigned long)chunk->mark.connector->inode;
184+
}
185+
176186
static unsigned long chunk_to_key(struct audit_chunk *chunk)
177187
{
178-
return (unsigned long)chunk->mark.inode;
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;
179194
}
180195

181196
static inline struct list_head *chunk_hash(unsigned long key)
@@ -187,7 +202,7 @@ static inline struct list_head *chunk_hash(unsigned long key)
187202
/* hash_lock & entry->lock is held by caller */
188203
static void insert_hash(struct audit_chunk *chunk)
189204
{
190-
unsigned long key = chunk_to_key(chunk);
205+
unsigned long key = __chunk_to_key(chunk);
191206
struct list_head *list;
192207

193208
if (!(chunk->mark.flags & FSNOTIFY_MARK_FLAG_ATTACHED))
@@ -276,8 +291,8 @@ static void untag_chunk(struct node *p)
276291
if (!new)
277292
goto Fallback;
278293

279-
if (fsnotify_add_mark_locked(&new->mark, entry->group, entry->inode,
280-
NULL, 1)) {
294+
if (fsnotify_add_mark_locked(&new->mark, entry->group,
295+
entry->connector->inode, NULL, 1)) {
281296
fsnotify_put_mark(&new->mark);
282297
goto Fallback;
283298
}
@@ -418,7 +433,7 @@ static int tag_chunk(struct inode *inode, struct audit_tree *tree)
418433
}
419434

420435
if (fsnotify_add_mark_locked(chunk_entry, old_entry->group,
421-
old_entry->inode, NULL, 1)) {
436+
old_entry->connector->inode, NULL, 1)) {
422437
spin_unlock(&old_entry->lock);
423438
mutex_unlock(&old_entry->group->mark_mutex);
424439
fsnotify_put_mark(chunk_entry);

0 commit comments

Comments
 (0)