Skip to contentSkip to conversation

fanotify: Avoid softlockups when reading many events

PATCH In review Jan Kara 2 participants 3 messages Jul 15, 2020

Subsystems (3)
lore
Share & tools
lore

Jan Kara

author maintainer
#0 lore raw

PATCH 1/1 fanotify: Avoid softlockups when reading many events

When user provides large buffer for events and there are lots of events available, we can try to copy them all to userspace without scheduling which can softlockup the kernel (furthermore exacerbated by the contention on notification_lock). Add a scheduling point after copying each event.

Note that usually the real underlying problem is the cost of fanotify event merging and the resulting contention on notification_lock but this is a cheap way to somewhat reduce the problem until we can properly address that.

---
 fs/notify/fanotify/fanotify_user.c | 5 +++++
 1 file changed, 5 insertions(+)

This is a quick mending we can do immediately and is probably a good idea nevertheless... I'll queue it up if Amir agrees.

fs/notify/fanotify/fanotify_user.c +5 -0
index 63b5dffdca9e..d7f63aeca992 100644
@@ -412,6 +412,11 @@ static ssize_t fanotify_read(struct file *file, char __user *buf,
 
 	add_wait_queue(&group->notification_waitq, &wait);
 	while (1) {
+		/*
+		 * User can supply arbitrarily large buffer. Avoid softlockups
+		 * in case there are lots of available events.
+		 */
+		cond_resched();
 		event = get_one_event(group, count);
 		if (IS_ERR(event)) {
 			ret = PTR_ERR(event);
Signature · 1 line
2.16.4
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 12 lines · Jan Kara

When user provides large buffer for events and there are lots of events available, we can try to copy them all to userspace without scheduling which can softlockup the kernel (furthermore exacerbated by the contention on notification_lock). Add a scheduling point after copying each event.

Note that usually the real underlying problem is the cost of fanotify event merging and the resulting contention on notification_lock but this is a cheap way to somewhat reduce the problem until we can properly address that.

Quoted 9 lines
---
 fs/notify/fanotify/fanotify_user.c | 5 +++++
 1 file changed, 5 insertions(+)

This is a quick mending we can do immediately and is probably a good idea nevertheless... I'll queue it up if Amir agrees.

Sure. fine by me.
Maybe add a lore link to the issue report.

Quoted 19 lines
fs/notify/fanotify/fanotify_user.c +5 -1
index 63b5dffdca9e..d7f63aeca992 100644
@@ -412,6 +412,11 @@ static ssize_t fanotify_read(struct file *file, char __user *buf,
 
        add_wait_queue(&group->notification_waitq, &wait);
        while (1) {
+               /*
+                * User can supply arbitrarily large buffer. Avoid softlockups
+                * in case there are lots of available events.
+                */
+               cond_resched();
                event = get_one_event(group, count);
                if (IS_ERR(event)) {
                        ret = PTR_ERR(event);
--

2.16.4

author maintainer
#2 lore raw
Quoted 19 lines · Amir Goldstein
Quoted 18 lines · Jan Kara

When user provides large buffer for events and there are lots of events available, we can try to copy them all to userspace without scheduling which can softlockup the kernel (furthermore exacerbated by the contention on notification_lock). Add a scheduling point after copying each event.

Note that usually the real underlying problem is the cost of fanotify event merging and the resulting contention on notification_lock but this is a cheap way to somewhat reduce the problem until we can properly address that.

---
 fs/notify/fanotify/fanotify_user.c | 5 +++++
 1 file changed, 5 insertions(+)
Quoted 8 lines
Quoted 3 lines

This is a quick mending we can do immediately and is probably a good idea nevertheless... I'll queue it up if Amir agrees.

Sure. fine by me.
Maybe add a lore link to the issue report.

Thanks. I've added the link and pushed out the patch to my tree.

Honza

Quoted 20 lines
Quoted 20 lines
fs/notify/fanotify/fanotify_user.c +5 -1
index 63b5dffdca9e..d7f63aeca992 100644
@@ -412,6 +412,11 @@ static ssize_t fanotify_read(struct file *file, char __user *buf,
 
        add_wait_queue(&group->notification_waitq, &wait);
        while (1) {
+               /*
+                * User can supply arbitrarily large buffer. Avoid softlockups
+                * in case there are lots of available events.
+                */
+               cond_resched();
                event = get_one_event(group, count);
                if (IS_ERR(event)) {
                        ret = PTR_ERR(event);
--

2.16.4

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR