This is a note to let you know that I've just added the patch titled fanotify: limit number of event merge attempts to the 5.10-stable tree which can be found at: http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary The filename of the patch is: fanotify-limit-number-of-event-merge-attempts.patch and it can be found in the queue-5.10 subdirectory. If you, or anyone else, feels it should not be added to the stable tree, please let <stable@xxxxxxxxxxxxxxx> know about it. commit 76608fa6240eaaefbae1926e528eec908054dccd Author: Amir Goldstein <amir73il@xxxxxxxxx> Date: Thu Mar 4 12:48:26 2021 +0200 fanotify: limit number of event merge attempts [ Upstream commit b8cd0ee8cda68a888a317991c1e918a8cba1a568 ] 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@xxxxxxxxx Signed-off-by: Amir Goldstein <amir73il@xxxxxxxxx> Signed-off-by: Jan Kara <jack@xxxxxxx> Signed-off-by: Chuck Lever <chuck.lever@xxxxxxxxxx> Signed-off-by: Sasha Levin <sashal@xxxxxxxxxx> diff --git a/fs/notify/fanotify/fanotify.c b/fs/notify/fanotify/fanotify.c index 50b3abc062156..754e27ead8742 100644 --- a/fs/notify/fanotify/fanotify.c +++ b/fs/notify/fanotify/fanotify.c @@ -148,6 +148,9 @@ 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) @@ -155,6 +158,7 @@ static int fanotify_merge(struct fsnotify_group *group, 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); @@ -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;