Skip to content

Commit 7053aee

Browse files
jankaratorvalds
authored andcommitted
fsnotify: do not share events between notification groups
Currently fsnotify framework creates one event structure for each notification event and links this event into all interested notification groups. This is done so that we save memory when several notification groups are interested in the event. However the need for event structure shared between inotify & fanotify bloats the event structure so the result is often higher memory consumption. Another problem is that fsnotify framework keeps path references with outstanding events so that fanotify can return open file descriptors with its events. This has the undesirable effect that filesystem cannot be unmounted while there are outstanding events - a regression for inotify compared to a situation before it was converted to fsnotify framework. For fanotify this problem is hard to avoid and users of fanotify should kind of expect this behavior when they ask for file descriptors from notified files. This patch changes fsnotify and its users to create separate event structure for each group. This allows for much simpler code (~400 lines removed by this patch) and also smaller event structures. For example on 64-bit system original struct fsnotify_event consumes 120 bytes, plus additional space for file name, additional 24 bytes for second and each subsequent group linking the event, and additional 32 bytes for each inotify group for private data. After the conversion inotify event consumes 48 bytes plus space for file name which is considerably less memory unless file names are long and there are several groups interested in the events (both of which are uncommon). Fanotify event fits in 56 bytes after the conversion (fanotify doesn't care about file names so its events don't have to have it allocated). A win unless there are four or more fanotify groups interested in the event. The conversion also solves the problem with unmount when only inotify is used as we don't have to grab path references for inotify events. [hughd@google.com: fanotify: fix corruption preventing startup] Signed-off-by: Jan Kara <jack@suse.cz> Reviewed-by: Christoph Hellwig <hch@lst.de> Cc: Eric Paris <eparis@parisplace.org> Cc: Al Viro <viro@zeniv.linux.org.uk> Signed-off-by: Hugh Dickins <hughd@google.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent e9fe690 commit 7053aee

13 files changed

Lines changed: 318 additions & 708 deletions

File tree

‎fs/notify/dnotify/dnotify.c‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,21 +82,20 @@ static void dnotify_recalc_inode_mask(struct fsnotify_mark *fsn_mark)
8282
* events.
8383
*/
8484
static int dnotify_handle_event(struct fsnotify_group *group,
85+
struct inode *inode,
8586
struct fsnotify_mark *inode_mark,
8687
struct fsnotify_mark *vfsmount_mark,
87-
struct fsnotify_event *event)
88+
u32 mask, void *data, int data_type,
89+
const unsigned char *file_name)
8890
{
8991
struct dnotify_mark *dn_mark;
90-
struct inode *to_tell;
9192
struct dnotify_struct *dn;
9293
struct dnotify_struct **prev;
9394
struct fown_struct *fown;
94-
__u32 test_mask = event->mask & ~FS_EVENT_ON_CHILD;
95+
__u32 test_mask = mask & ~FS_EVENT_ON_CHILD;
9596

9697
BUG_ON(vfsmount_mark);
9798

98-
to_tell = event->to_tell;
99-
10099
dn_mark = container_of(inode_mark, struct dnotify_mark, fsn_mark);
101100

102101
spin_lock(&inode_mark->lock);
@@ -155,7 +154,7 @@ static struct fsnotify_ops dnotify_fsnotify_ops = {
155154
.should_send_event = dnotify_should_send_event,
156155
.free_group_priv = NULL,
157156
.freeing_mark = NULL,
158-
.free_event_priv = NULL,
157+
.free_event = NULL,
159158
};
160159

161160
/*

‎fs/notify/fanotify/fanotify.c‎

Lines changed: 104 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -9,91 +9,56 @@
99
#include <linux/types.h>
1010
#include <linux/wait.h>
1111

12-
static bool should_merge(struct fsnotify_event *old, struct fsnotify_event *new)
12+
#include "fanotify.h"
13+
14+
static bool should_merge(struct fsnotify_event *old_fsn,
15+
struct fsnotify_event *new_fsn)
1316
{
14-
pr_debug("%s: old=%p new=%p\n", __func__, old, new);
17+
struct fanotify_event_info *old, *new;
1518

16-
if (old->to_tell == new->to_tell &&
17-
old->data_type == new->data_type &&
18-
old->tgid == new->tgid) {
19-
switch (old->data_type) {
20-
case (FSNOTIFY_EVENT_PATH):
2119
#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
22-
/* dont merge two permission events */
23-
if ((old->mask & FAN_ALL_PERM_EVENTS) &&
24-
(new->mask & FAN_ALL_PERM_EVENTS))
25-
return false;
20+
/* dont merge two permission events */
21+
if ((old_fsn->mask & FAN_ALL_PERM_EVENTS) &&
22+
(new_fsn->mask & FAN_ALL_PERM_EVENTS))
23+
return false;
2624
#endif
27-
if ((old->path.mnt == new->path.mnt) &&
28-
(old->path.dentry == new->path.dentry))
29-
return true;
30-
break;
31-
case (FSNOTIFY_EVENT_NONE):
32-
return true;
33-
default:
34-
BUG();
35-
};
36-
}
25+
pr_debug("%s: old=%p new=%p\n", __func__, old_fsn, new_fsn);
26+
old = FANOTIFY_E(old_fsn);
27+
new = FANOTIFY_E(new_fsn);
28+
29+
if (old_fsn->inode == new_fsn->inode && old->tgid == new->tgid &&
30+
old->path.mnt == new->path.mnt &&
31+
old->path.dentry == new->path.dentry)
32+
return true;
3733
return false;
3834
}
3935

4036
/* and the list better be locked by something too! */
4137
static struct fsnotify_event *fanotify_merge(struct list_head *list,
4238
struct fsnotify_event *event)
4339
{
44-
struct fsnotify_event_holder *test_holder;
45-
struct fsnotify_event *test_event = NULL;
46-
struct fsnotify_event *new_event;
40+
struct fsnotify_event *test_event;
41+
bool do_merge = false;
4742

4843
pr_debug("%s: list=%p event=%p\n", __func__, list, event);
4944

50-
51-
list_for_each_entry_reverse(test_holder, list, event_list) {
52-
if (should_merge(test_holder->event, event)) {
53-
test_event = test_holder->event;
45+
list_for_each_entry_reverse(test_event, list, list) {
46+
if (should_merge(test_event, event)) {
47+
do_merge = true;
5448
break;
5549
}
5650
}
5751

58-
if (!test_event)
52+
if (!do_merge)
5953
return NULL;
6054

61-
fsnotify_get_event(test_event);
62-
63-
/* if they are exactly the same we are done */
64-
if (test_event->mask == event->mask)
65-
return test_event;
66-
67-
/*
68-
* if the refcnt == 2 this is the only queue
69-
* for this event and so we can update the mask
70-
* in place.
71-
*/
72-
if (atomic_read(&test_event->refcnt) == 2) {
73-
test_event->mask |= event->mask;
74-
return test_event;
75-
}
76-
77-
new_event = fsnotify_clone_event(test_event);
78-
79-
/* done with test_event */
80-
fsnotify_put_event(test_event);
81-
82-
/* couldn't allocate memory, merge was not possible */
83-
if (unlikely(!new_event))
84-
return ERR_PTR(-ENOMEM);
85-
86-
/* build new event and replace it on the list */
87-
new_event->mask = (test_event->mask | event->mask);
88-
fsnotify_replace_event(test_holder, new_event);
89-
90-
/* we hold a reference on new_event from clone_event */
91-
return new_event;
55+
test_event->mask |= event->mask;
56+
return test_event;
9257
}
9358

9459
#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
9560
static int fanotify_get_response_from_access(struct fsnotify_group *group,
96-
struct fsnotify_event *event)
61+
struct fanotify_event_info *event)
9762
{
9863
int ret;
9964

@@ -106,7 +71,6 @@ static int fanotify_get_response_from_access(struct fsnotify_group *group,
10671
return 0;
10772

10873
/* userspace responded, convert to something usable */
109-
spin_lock(&event->lock);
11074
switch (event->response) {
11175
case FAN_ALLOW:
11276
ret = 0;
@@ -116,7 +80,6 @@ static int fanotify_get_response_from_access(struct fsnotify_group *group,
11680
ret = -EPERM;
11781
}
11882
event->response = 0;
119-
spin_unlock(&event->lock);
12083

12184
pr_debug("%s: group=%p event=%p about to return ret=%d\n", __func__,
12285
group, event, ret);
@@ -125,57 +88,17 @@ static int fanotify_get_response_from_access(struct fsnotify_group *group,
12588
}
12689
#endif
12790

128-
static int fanotify_handle_event(struct fsnotify_group *group,
129-
struct fsnotify_mark *inode_mark,
130-
struct fsnotify_mark *fanotify_mark,
131-
struct fsnotify_event *event)
132-
{
133-
int ret = 0;
134-
struct fsnotify_event *notify_event = NULL;
135-
136-
BUILD_BUG_ON(FAN_ACCESS != FS_ACCESS);
137-
BUILD_BUG_ON(FAN_MODIFY != FS_MODIFY);
138-
BUILD_BUG_ON(FAN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE);
139-
BUILD_BUG_ON(FAN_CLOSE_WRITE != FS_CLOSE_WRITE);
140-
BUILD_BUG_ON(FAN_OPEN != FS_OPEN);
141-
BUILD_BUG_ON(FAN_EVENT_ON_CHILD != FS_EVENT_ON_CHILD);
142-
BUILD_BUG_ON(FAN_Q_OVERFLOW != FS_Q_OVERFLOW);
143-
BUILD_BUG_ON(FAN_OPEN_PERM != FS_OPEN_PERM);
144-
BUILD_BUG_ON(FAN_ACCESS_PERM != FS_ACCESS_PERM);
145-
BUILD_BUG_ON(FAN_ONDIR != FS_ISDIR);
146-
147-
pr_debug("%s: group=%p event=%p\n", __func__, group, event);
148-
149-
notify_event = fsnotify_add_notify_event(group, event, NULL, fanotify_merge);
150-
if (IS_ERR(notify_event))
151-
return PTR_ERR(notify_event);
152-
153-
#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
154-
if (event->mask & FAN_ALL_PERM_EVENTS) {
155-
/* if we merged we need to wait on the new event */
156-
if (notify_event)
157-
event = notify_event;
158-
ret = fanotify_get_response_from_access(group, event);
159-
}
160-
#endif
161-
162-
if (notify_event)
163-
fsnotify_put_event(notify_event);
164-
165-
return ret;
166-
}
167-
16891
static bool fanotify_should_send_event(struct fsnotify_group *group,
169-
struct inode *to_tell,
92+
struct inode *inode,
17093
struct fsnotify_mark *inode_mark,
17194
struct fsnotify_mark *vfsmnt_mark,
17295
__u32 event_mask, void *data, int data_type)
17396
{
17497
__u32 marks_mask, marks_ignored_mask;
17598
struct path *path = data;
17699

177-
pr_debug("%s: group=%p to_tell=%p inode_mark=%p vfsmnt_mark=%p "
178-
"mask=%x data=%p data_type=%d\n", __func__, group, to_tell,
100+
pr_debug("%s: group=%p inode=%p inode_mark=%p vfsmnt_mark=%p "
101+
"mask=%x data=%p data_type=%d\n", __func__, group, inode,
179102
inode_mark, vfsmnt_mark, event_mask, data, data_type);
180103

181104
/* if we don't have enough info to send an event to userspace say no */
@@ -217,6 +140,70 @@ static bool fanotify_should_send_event(struct fsnotify_group *group,
217140
return false;
218141
}
219142

143+
static int fanotify_handle_event(struct fsnotify_group *group,
144+
struct inode *inode,
145+
struct fsnotify_mark *inode_mark,
146+
struct fsnotify_mark *fanotify_mark,
147+
u32 mask, void *data, int data_type,
148+
const unsigned char *file_name)
149+
{
150+
int ret = 0;
151+
struct fanotify_event_info *event;
152+
struct fsnotify_event *fsn_event;
153+
struct fsnotify_event *notify_fsn_event;
154+
155+
BUILD_BUG_ON(FAN_ACCESS != FS_ACCESS);
156+
BUILD_BUG_ON(FAN_MODIFY != FS_MODIFY);
157+
BUILD_BUG_ON(FAN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE);
158+
BUILD_BUG_ON(FAN_CLOSE_WRITE != FS_CLOSE_WRITE);
159+
BUILD_BUG_ON(FAN_OPEN != FS_OPEN);
160+
BUILD_BUG_ON(FAN_EVENT_ON_CHILD != FS_EVENT_ON_CHILD);
161+
BUILD_BUG_ON(FAN_Q_OVERFLOW != FS_Q_OVERFLOW);
162+
BUILD_BUG_ON(FAN_OPEN_PERM != FS_OPEN_PERM);
163+
BUILD_BUG_ON(FAN_ACCESS_PERM != FS_ACCESS_PERM);
164+
BUILD_BUG_ON(FAN_ONDIR != FS_ISDIR);
165+
166+
pr_debug("%s: group=%p inode=%p mask=%x\n", __func__, group, inode,
167+
mask);
168+
169+
event = kmem_cache_alloc(fanotify_event_cachep, GFP_KERNEL);
170+
if (unlikely(!event))
171+
return -ENOMEM;
172+
173+
fsn_event = &event->fse;
174+
fsnotify_init_event(fsn_event, inode, mask);
175+
event->tgid = get_pid(task_tgid(current));
176+
if (data_type == FSNOTIFY_EVENT_PATH) {
177+
struct path *path = data;
178+
event->path = *path;
179+
path_get(&event->path);
180+
} else {
181+
event->path.mnt = NULL;
182+
event->path.dentry = NULL;
183+
}
184+
#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
185+
event->response = 0;
186+
#endif
187+
188+
notify_fsn_event = fsnotify_add_notify_event(group, fsn_event,
189+
fanotify_merge);
190+
if (notify_fsn_event) {
191+
/* Our event wasn't used in the end. Free it. */
192+
fsnotify_destroy_event(group, fsn_event);
193+
if (IS_ERR(notify_fsn_event))
194+
return PTR_ERR(notify_fsn_event);
195+
/* We need to ask about a different events after a merge... */
196+
event = FANOTIFY_E(notify_fsn_event);
197+
fsn_event = notify_fsn_event;
198+
}
199+
200+
#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
201+
if (fsn_event->mask & FAN_ALL_PERM_EVENTS)
202+
ret = fanotify_get_response_from_access(group, event);
203+
#endif
204+
return ret;
205+
}
206+
220207
static void fanotify_free_group_priv(struct fsnotify_group *group)
221208
{
222209
struct user_struct *user;
@@ -226,10 +213,20 @@ static void fanotify_free_group_priv(struct fsnotify_group *group)
226213
free_uid(user);
227214
}
228215

216+
static void fanotify_free_event(struct fsnotify_event *fsn_event)
217+
{
218+
struct fanotify_event_info *event;
219+
220+
event = FANOTIFY_E(fsn_event);
221+
path_put(&event->path);
222+
put_pid(event->tgid);
223+
kmem_cache_free(fanotify_event_cachep, event);
224+
}
225+
229226
const struct fsnotify_ops fanotify_fsnotify_ops = {
230227
.handle_event = fanotify_handle_event,
231228
.should_send_event = fanotify_should_send_event,
232229
.free_group_priv = fanotify_free_group_priv,
233-
.free_event_priv = NULL,
230+
.free_event = fanotify_free_event,
234231
.freeing_mark = NULL,
235232
};

‎fs/notify/fanotify/fanotify.h‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <linux/fsnotify_backend.h>
2+
#include <linux/path.h>
3+
#include <linux/slab.h>
4+
5+
extern struct kmem_cache *fanotify_event_cachep;
6+
7+
struct fanotify_event_info {
8+
struct fsnotify_event fse;
9+
/*
10+
* We hold ref to this path so it may be dereferenced at any point
11+
* during this object's lifetime
12+
*/
13+
struct path path;
14+
struct pid *tgid;
15+
#ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
16+
u32 response; /* userspace answer to question */
17+
#endif
18+
};
19+
20+
static inline struct fanotify_event_info *FANOTIFY_E(struct fsnotify_event *fse)
21+
{
22+
return container_of(fse, struct fanotify_event_info, fse);
23+
}

0 commit comments

Comments
 (0)