Skip to content

Commit

Permalink
fanotify: limit number of event merge attempts
Browse files Browse the repository at this point in the history
Event merges are expensive when event queue size is large, so limit the
linear search to 128 merge tests.

In combination with 128 size hash table, there is a potential to merge
with up to 16K events in the hashed queue.

Link: https://lore.kernel.org/r/20210304104826.3993892-6-amir73il@gmail.com
Signed-off-by: Amir Goldstein <amir73il@gmail.com>
Signed-off-by: Jan Kara <jack@suse.cz>
  • Loading branch information
amir73il authored and jankara committed Mar 16, 2021
1 parent 94e00d2 commit b8cd0ee
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions fs/notify/fanotify/fanotify.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,17 @@ static bool fanotify_should_merge(struct fanotify_event *old,
return false;
}

/* Limit event merges to limit CPU overhead per event */
#define FANOTIFY_MAX_MERGE_EVENTS 128

/* and the list better be locked by something too! */
static int fanotify_merge(struct fsnotify_group *group,
struct fsnotify_event *event)
{
struct fanotify_event *old, *new = FANOTIFY_E(event);
unsigned int bucket = fanotify_event_hash_bucket(group, new);
struct hlist_head *hlist = &group->fanotify_data.merge_hash[bucket];
int i = 0;

pr_debug("%s: group=%p event=%p bucket=%u\n", __func__,
group, event, bucket);
Expand All @@ -168,6 +172,8 @@ static int fanotify_merge(struct fsnotify_group *group,
return 0;

hlist_for_each_entry(old, hlist, merge_list) {
if (++i > FANOTIFY_MAX_MERGE_EVENTS)
break;
if (fanotify_should_merge(old, new)) {
old->mask |= new->mask;
return 1;
Expand Down

0 comments on commit b8cd0ee

Please sign in to comment.