2022-11-24 00:31:41

by Bhatnagar, Rishabh

[permalink] [raw]
Subject: [PATCH 5.4 0/2] Fix epoll issue in 5.4 kernels

Hi Greg
After upgrading to 5.4.211 we were started seeing some nodes getting
stuck in our Kubernetes cluster. All nodes are running this kernel
version. After taking a closer look it seems that runc was command getting
stuck. Looking at the stack it appears the thread is stuck in epoll wait for
sometime.
[<0>] do_syscall_64+0x48/0xf0
[<0>] entry_SYSCALL_64_after_hwframe+0x5c/0xc1
[<0>] ep_poll+0x48d/0x4e0
[<0>] do_epoll_wait+0xab/0xc0
[<0>] __x64_sys_epoll_pwait+0x4d/0xa0
[<0>] do_syscall_64+0x48/0xf0
[<0>] entry_SYSCALL_64_after_hwframe+0x5c/0xc1
[<0>] futex_wait_queue_me+0xb6/0x110
[<0>] futex_wait+0xe2/0x260
[<0>] do_futex+0x372/0x4f0
[<0>] __x64_sys_futex+0x134/0x180
[<0>] do_syscall_64+0x48/0xf0
[<0>] entry_SYSCALL_64_after_hwframe+0x5c/0xc1

I noticed there are other discussions going on as well
regarding this.
https://lore.kernel.org/all/[email protected]/
Reverting the below patch does fix the issue:
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?h=linux-5.4.y&id=cf2db24ec4b8e9d399005ececd6f6336916ab6fc
We don't see this issue in latest upstream kernel or even latest 5.10
stable tree. Looking at the patches that went in for 5.10 stable there's
one that stands out that seems to be missing in 5.4.
289caf5d8f6c61c6d2b7fd752a7f483cd153f182 (epoll: check for events when removing
a timed out thread from the wait queue)

Backporting this patch to 5.4 we don't see the hangups anymore. Looks like
this patch fixes time out scenarios which might cause missed wake ups.
The other patch in the patch series also fixes a race and is needed for
the second patch to apply.

Roman Penyaev (1):
epoll: call final ep_events_available() check under the lock

Soheil Hassas Yeganeh (1):
epoll: check for events when removing a timed out thread from the wait
queue

fs/eventpoll.c | 68 ++++++++++++++++++++++++++++++--------------------
1 file changed, 41 insertions(+), 27 deletions(-)

--
2.37.1


2022-11-24 00:33:56

by Bhatnagar, Rishabh

[permalink] [raw]
Subject: [PATCH 5.4 1/2] epoll: call final ep_events_available() check under the lock

From: Roman Penyaev <[email protected]>

Commit 65759097d804d2a9ad2b687db436319704ba7019 upstream.

There is a possible race when ep_scan_ready_list() leaves ->rdllist and
->obflist empty for a short period of time although some events are
pending. It is quite likely that ep_events_available() observes empty
lists and goes to sleep.

Since commit 339ddb53d373 ("fs/epoll: remove unnecessary wakeups of
nested epoll") we are conservative in wakeups (there is only one place
for wakeup and this is ep_poll_callback()), thus ep_events_available()
must always observe correct state of two lists.

The easiest and correct way is to do the final check under the lock.
This does not impact the performance, since lock is taken anyway for
adding a wait entry to the wait queue.

The discussion of the problem can be found here:

https://lore.kernel.org/linux-fsdevel/[email protected]/

In this patch barrierless __set_current_state() is used. This is safe
since waitqueue_active() is called under the same lock on wakeup side.

Short-circuit for fatal signals (i.e. fatal_signal_pending() check) is
moved to the line just before actual events harvesting routine. This is
fully compliant to what is said in the comment of the patch where the
actual fatal_signal_pending() check was added: c257a340ede0 ("fs, epoll:
short circuit fetching events if thread has been killed").

Fixes: 339ddb53d373 ("fs/epoll: remove unnecessary wakeups of nested epoll")
Reported-by: Jason Baron <[email protected]>
Reported-by: Randy Dunlap <[email protected]>
Signed-off-by: Roman Penyaev <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Reviewed-by: Jason Baron <[email protected]>
Cc: Khazhismel Kumykov <[email protected]>
Cc: Alexander Viro <[email protected]>
Cc: <[email protected]>
Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Linus Torvalds <[email protected]>
Signed-off-by: Rishabh Bhatnagar <[email protected]>
---
fs/eventpoll.c | 47 +++++++++++++++++++++++++++--------------------
1 file changed, 27 insertions(+), 20 deletions(-)

diff --git a/fs/eventpoll.c b/fs/eventpoll.c
index 7e11135bc915..e5496483a882 100644
--- a/fs/eventpoll.c
+++ b/fs/eventpoll.c
@@ -1905,33 +1905,31 @@ static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events,
init_wait(&wait);
wait.func = ep_autoremove_wake_function;
write_lock_irq(&ep->lock);
- __add_wait_queue_exclusive(&ep->wq, &wait);
- write_unlock_irq(&ep->lock);
-
/*
- * We don't want to sleep if the ep_poll_callback() sends us
- * a wakeup in between. That's why we set the task state
- * to TASK_INTERRUPTIBLE before doing the checks.
+ * Barrierless variant, waitqueue_active() is called under
+ * the same lock on wakeup ep_poll_callback() side, so it
+ * is safe to avoid an explicit barrier.
*/
- set_current_state(TASK_INTERRUPTIBLE);
+ __set_current_state(TASK_INTERRUPTIBLE);
+
/*
- * Always short-circuit for fatal signals to allow
- * threads to make a timely exit without the chance of
- * finding more events available and fetching
- * repeatedly.
+ * Do the final check under the lock. ep_scan_ready_list()
+ * plays with two lists (->rdllist and ->ovflist) and there
+ * is always a race when both lists are empty for short
+ * period of time although events are pending, so lock is
+ * important.
*/
- if (fatal_signal_pending(current)) {
- res = -EINTR;
- break;
+ eavail = ep_events_available(ep);
+ if (!eavail) {
+ if (signal_pending(current))
+ res = -EINTR;
+ else
+ __add_wait_queue_exclusive(&ep->wq, &wait);
}
+ write_unlock_irq(&ep->lock);

- eavail = ep_events_available(ep);
- if (eavail)
- break;
- if (signal_pending(current)) {
- res = -EINTR;
+ if (eavail || res)
break;
- }

if (!schedule_hrtimeout_range(to, slack, HRTIMER_MODE_ABS)) {
timed_out = 1;
@@ -1952,6 +1950,15 @@ static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events,
}

send_events:
+ if (fatal_signal_pending(current)) {
+ /*
+ * Always short-circuit for fatal signals to allow
+ * threads to make a timely exit without the chance of
+ * finding more events available and fetching
+ * repeatedly.
+ */
+ res = -EINTR;
+ }
/*
* Try to transfer events to user space. In case we get 0 events and
* there's still timeout left over, we go trying again in search of
--
2.37.1

2022-11-24 00:34:50

by Bhatnagar, Rishabh

[permalink] [raw]
Subject: [PATCH 2/2] epoll: check for events when removing a timed out thread from the wait queue

From: Soheil Hassas Yeganeh <[email protected]>

Commit 289caf5d8f6c61c6d2b7fd752a7f483cd153f182 upstream.

Patch series "simplify ep_poll".

This patch series is a followup based on the suggestions and feedback by
Linus:
https://lkml.kernel.org/r/CAHk-=wizk=OxUyQPbO8MS41w2Pag1kniUV5WdD5qWL-gq1kjDA@mail.gmail.com

The first patch in the series is a fix for the epoll race in presence of
timeouts, so that it can be cleanly backported to all affected stable
kernels.

The rest of the patch series simplify the ep_poll() implementation. Some
of these simplifications result in minor performance enhancements as well.
We have kept these changes under self tests and internal benchmarks for a
few days, and there are minor (1-2%) performance enhancements as a result.

This patch (of 8):

After abc610e01c66 ("fs/epoll: avoid barrier after an epoll_wait(2)
timeout"), we break out of the ep_poll loop upon timeout, without checking
whether there is any new events available. Prior to that patch-series we
always called ep_events_available() after exiting the loop.

This can cause races and missed wakeups. For example, consider the
following scenario reported by Guantao Liu:

Suppose we have an eventfd added using EPOLLET to an epollfd.

Thread 1: Sleeps for just below 5ms and then writes to an eventfd.
Thread 2: Calls epoll_wait with a timeout of 5 ms. If it sees an
event of the eventfd, it will write back on that fd.
Thread 3: Calls epoll_wait with a negative timeout.

Prior to abc610e01c66, it is guaranteed that Thread 3 will wake up either
by Thread 1 or Thread 2. After abc610e01c66, Thread 3 can be blocked
indefinitely if Thread 2 sees a timeout right before the write to the
eventfd by Thread 1. Thread 2 will be woken up from
schedule_hrtimeout_range and, with evail 0, it will not call
ep_send_events().

To fix this issue:
1) Simplify the timed_out case as suggested by Linus.
2) while holding the lock, recheck whether the thread was woken up
after its time out has reached.

Note that (2) is different from Linus' original suggestion: It do not set
"eavail = ep_events_available(ep)" to avoid unnecessary contention (when
there are too many timed-out threads and a small number of events), as
well as races mentioned in the discussion thread.

This is the first patch in the series so that the backport to stable
releases is straightforward.

Link: https://lkml.kernel.org/r/[email protected]
Link: https://lkml.kernel.org/r/CAHk-=wizk=OxUyQPbO8MS41w2Pag1kniUV5WdD5qWL-gq1kjDA@mail.gmail.com
Link: https://lkml.kernel.org/r/[email protected]
Fixes: abc610e01c66 ("fs/epoll: avoid barrier after an epoll_wait(2) timeout")
Signed-off-by: Soheil Hassas Yeganeh <[email protected]>
Tested-by: Guantao Liu <[email protected]>
Suggested-by: Linus Torvalds <[email protected]>
Reported-by: Guantao Liu <[email protected]>
Reviewed-by: Eric Dumazet <[email protected]>
Reviewed-by: Willem de Bruijn <[email protected]>
Reviewed-by: Khazhismel Kumykov <[email protected]>
Reviewed-by: Davidlohr Bueso <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
Signed-off-by: Rishabh Bhatnagar <[email protected]>
---
fs/eventpoll.c | 25 ++++++++++++++++---------
1 file changed, 16 insertions(+), 9 deletions(-)

diff --git a/fs/eventpoll.c b/fs/eventpoll.c
index e5496483a882..877f9f61a4e8 100644
--- a/fs/eventpoll.c
+++ b/fs/eventpoll.c
@@ -1928,23 +1928,30 @@ static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events,
}
write_unlock_irq(&ep->lock);

- if (eavail || res)
- break;
+ if (!eavail && !res)
+ timed_out = !schedule_hrtimeout_range(to, slack,
+ HRTIMER_MODE_ABS);

- if (!schedule_hrtimeout_range(to, slack, HRTIMER_MODE_ABS)) {
- timed_out = 1;
- break;
- }
-
- /* We were woken up, thus go and try to harvest some events */
+ /*
+ * We were woken up, thus go and try to harvest some events.
+ * If timed out and still on the wait queue, recheck eavail
+ * carefully under lock, below.
+ */
eavail = 1;
-
} while (0);

__set_current_state(TASK_RUNNING);

if (!list_empty_careful(&wait.entry)) {
write_lock_irq(&ep->lock);
+ /*
+ * If the thread timed out and is not on the wait queue, it
+ * means that the thread was woken up after its timeout expired
+ * before it could reacquire the lock. Thus, when wait.entry is
+ * empty, it needs to harvest events.
+ */
+ if (timed_out)
+ eavail = list_empty(&wait.entry);
__remove_wait_queue(&ep->wq, &wait);
write_unlock_irq(&ep->lock);
}
--
2.37.1

Subject: Re: [PATCH 5.4 1/2] epoll: call final ep_events_available() check under the lock

On Thu, Nov 24, 2022 at 12:11:22AM +0000, Rishabh Bhatnagar wrote:
> From: Roman Penyaev <[email protected]>
>
> Commit 65759097d804d2a9ad2b687db436319704ba7019 upstream.
>
> There is a possible race when ep_scan_ready_list() leaves ->rdllist and
> ->obflist empty for a short period of time although some events are
> pending. It is quite likely that ep_events_available() observes empty
> lists and goes to sleep.
>
> Since commit 339ddb53d373 ("fs/epoll: remove unnecessary wakeups of
> nested epoll") we are conservative in wakeups (there is only one place
> for wakeup and this is ep_poll_callback()), thus ep_events_available()
> must always observe correct state of two lists.
>
> The easiest and correct way is to do the final check under the lock.
> This does not impact the performance, since lock is taken anyway for
> adding a wait entry to the wait queue.
>
> The discussion of the problem can be found here:
>
> https://lore.kernel.org/linux-fsdevel/[email protected]/
>
> In this patch barrierless __set_current_state() is used. This is safe
> since waitqueue_active() is called under the same lock on wakeup side.
>
> Short-circuit for fatal signals (i.e. fatal_signal_pending() check) is
> moved to the line just before actual events harvesting routine. This is
> fully compliant to what is said in the comment of the patch where the
> actual fatal_signal_pending() check was added: c257a340ede0 ("fs, epoll:
> short circuit fetching events if thread has been killed").
>
> Fixes: 339ddb53d373 ("fs/epoll: remove unnecessary wakeups of nested epoll")
> Reported-by: Jason Baron <[email protected]>
> Reported-by: Randy Dunlap <[email protected]>
> Signed-off-by: Roman Penyaev <[email protected]>
> Signed-off-by: Andrew Morton <[email protected]>
> Reviewed-by: Jason Baron <[email protected]>
> Cc: Khazhismel Kumykov <[email protected]>
> Cc: Alexander Viro <[email protected]>
> Cc: <[email protected]>
> Link: http://lkml.kernel.org/r/[email protected]
> Signed-off-by: Linus Torvalds <[email protected]>
> Signed-off-by: Rishabh Bhatnagar <[email protected]>

Acked-by: Thadeu Lima de Souza Cascardo <[email protected]>

I ended up picking these two fixes to our kernels as well, even though we could
not pinpoint the process kernel stacktrace as you did as a way to determine the
failure has happened. We are still testing that this is really fixed with these
two commits.

On the other hand,
tools/testing/selftests/filesystems/epoll/epoll_wakeup_test.c epoll61 test
starts passing once these two commits are applied.

Cascardo.


> ---
> fs/eventpoll.c | 47 +++++++++++++++++++++++++++--------------------
> 1 file changed, 27 insertions(+), 20 deletions(-)
>
> diff --git a/fs/eventpoll.c b/fs/eventpoll.c
> index 7e11135bc915..e5496483a882 100644
> --- a/fs/eventpoll.c
> +++ b/fs/eventpoll.c
> @@ -1905,33 +1905,31 @@ static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events,
> init_wait(&wait);
> wait.func = ep_autoremove_wake_function;
> write_lock_irq(&ep->lock);
> - __add_wait_queue_exclusive(&ep->wq, &wait);
> - write_unlock_irq(&ep->lock);
> -
> /*
> - * We don't want to sleep if the ep_poll_callback() sends us
> - * a wakeup in between. That's why we set the task state
> - * to TASK_INTERRUPTIBLE before doing the checks.
> + * Barrierless variant, waitqueue_active() is called under
> + * the same lock on wakeup ep_poll_callback() side, so it
> + * is safe to avoid an explicit barrier.
> */
> - set_current_state(TASK_INTERRUPTIBLE);
> + __set_current_state(TASK_INTERRUPTIBLE);
> +
> /*
> - * Always short-circuit for fatal signals to allow
> - * threads to make a timely exit without the chance of
> - * finding more events available and fetching
> - * repeatedly.
> + * Do the final check under the lock. ep_scan_ready_list()
> + * plays with two lists (->rdllist and ->ovflist) and there
> + * is always a race when both lists are empty for short
> + * period of time although events are pending, so lock is
> + * important.
> */
> - if (fatal_signal_pending(current)) {
> - res = -EINTR;
> - break;
> + eavail = ep_events_available(ep);
> + if (!eavail) {
> + if (signal_pending(current))
> + res = -EINTR;
> + else
> + __add_wait_queue_exclusive(&ep->wq, &wait);
> }
> + write_unlock_irq(&ep->lock);
>
> - eavail = ep_events_available(ep);
> - if (eavail)
> - break;
> - if (signal_pending(current)) {
> - res = -EINTR;
> + if (eavail || res)
> break;
> - }
>
> if (!schedule_hrtimeout_range(to, slack, HRTIMER_MODE_ABS)) {
> timed_out = 1;
> @@ -1952,6 +1950,15 @@ static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events,
> }
>
> send_events:
> + if (fatal_signal_pending(current)) {
> + /*
> + * Always short-circuit for fatal signals to allow
> + * threads to make a timely exit without the chance of
> + * finding more events available and fetching
> + * repeatedly.
> + */
> + res = -EINTR;
> + }
> /*
> * Try to transfer events to user space. In case we get 0 events and
> * there's still timeout left over, we go trying again in search of
> --
> 2.37.1
>

Subject: Re: [PATCH 2/2] epoll: check for events when removing a timed out thread from the wait queue

On Thu, Nov 24, 2022 at 12:11:23AM +0000, Rishabh Bhatnagar wrote:
> From: Soheil Hassas Yeganeh <[email protected]>
>
> Commit 289caf5d8f6c61c6d2b7fd752a7f483cd153f182 upstream.
>
> Patch series "simplify ep_poll".
>
> This patch series is a followup based on the suggestions and feedback by
> Linus:
> https://lkml.kernel.org/r/CAHk-=wizk=OxUyQPbO8MS41w2Pag1kniUV5WdD5qWL-gq1kjDA@mail.gmail.com
>
> The first patch in the series is a fix for the epoll race in presence of
> timeouts, so that it can be cleanly backported to all affected stable
> kernels.
>
> The rest of the patch series simplify the ep_poll() implementation. Some
> of these simplifications result in minor performance enhancements as well.
> We have kept these changes under self tests and internal benchmarks for a
> few days, and there are minor (1-2%) performance enhancements as a result.
>
> This patch (of 8):
>
> After abc610e01c66 ("fs/epoll: avoid barrier after an epoll_wait(2)
> timeout"), we break out of the ep_poll loop upon timeout, without checking
> whether there is any new events available. Prior to that patch-series we
> always called ep_events_available() after exiting the loop.
>
> This can cause races and missed wakeups. For example, consider the
> following scenario reported by Guantao Liu:
>
> Suppose we have an eventfd added using EPOLLET to an epollfd.
>
> Thread 1: Sleeps for just below 5ms and then writes to an eventfd.
> Thread 2: Calls epoll_wait with a timeout of 5 ms. If it sees an
> event of the eventfd, it will write back on that fd.
> Thread 3: Calls epoll_wait with a negative timeout.
>
> Prior to abc610e01c66, it is guaranteed that Thread 3 will wake up either
> by Thread 1 or Thread 2. After abc610e01c66, Thread 3 can be blocked
> indefinitely if Thread 2 sees a timeout right before the write to the
> eventfd by Thread 1. Thread 2 will be woken up from
> schedule_hrtimeout_range and, with evail 0, it will not call
> ep_send_events().
>
> To fix this issue:
> 1) Simplify the timed_out case as suggested by Linus.
> 2) while holding the lock, recheck whether the thread was woken up
> after its time out has reached.
>
> Note that (2) is different from Linus' original suggestion: It do not set
> "eavail = ep_events_available(ep)" to avoid unnecessary contention (when
> there are too many timed-out threads and a small number of events), as
> well as races mentioned in the discussion thread.
>
> This is the first patch in the series so that the backport to stable
> releases is straightforward.
>
> Link: https://lkml.kernel.org/r/[email protected]
> Link: https://lkml.kernel.org/r/CAHk-=wizk=OxUyQPbO8MS41w2Pag1kniUV5WdD5qWL-gq1kjDA@mail.gmail.com
> Link: https://lkml.kernel.org/r/[email protected]
> Fixes: abc610e01c66 ("fs/epoll: avoid barrier after an epoll_wait(2) timeout")
> Signed-off-by: Soheil Hassas Yeganeh <[email protected]>
> Tested-by: Guantao Liu <[email protected]>
> Suggested-by: Linus Torvalds <[email protected]>
> Reported-by: Guantao Liu <[email protected]>
> Reviewed-by: Eric Dumazet <[email protected]>
> Reviewed-by: Willem de Bruijn <[email protected]>
> Reviewed-by: Khazhismel Kumykov <[email protected]>
> Reviewed-by: Davidlohr Bueso <[email protected]>
> Signed-off-by: Andrew Morton <[email protected]>
> Signed-off-by: Linus Torvalds <[email protected]>
> Signed-off-by: Rishabh Bhatnagar <[email protected]>

Acked-by: Thadeu Lima de Souza Cascardo <[email protected]>

> ---
> fs/eventpoll.c | 25 ++++++++++++++++---------
> 1 file changed, 16 insertions(+), 9 deletions(-)
>
> diff --git a/fs/eventpoll.c b/fs/eventpoll.c
> index e5496483a882..877f9f61a4e8 100644
> --- a/fs/eventpoll.c
> +++ b/fs/eventpoll.c
> @@ -1928,23 +1928,30 @@ static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events,
> }
> write_unlock_irq(&ep->lock);
>
> - if (eavail || res)
> - break;
> + if (!eavail && !res)
> + timed_out = !schedule_hrtimeout_range(to, slack,
> + HRTIMER_MODE_ABS);
>
> - if (!schedule_hrtimeout_range(to, slack, HRTIMER_MODE_ABS)) {
> - timed_out = 1;
> - break;
> - }
> -
> - /* We were woken up, thus go and try to harvest some events */
> + /*
> + * We were woken up, thus go and try to harvest some events.
> + * If timed out and still on the wait queue, recheck eavail
> + * carefully under lock, below.
> + */
> eavail = 1;
> -
> } while (0);
>
> __set_current_state(TASK_RUNNING);
>
> if (!list_empty_careful(&wait.entry)) {
> write_lock_irq(&ep->lock);
> + /*
> + * If the thread timed out and is not on the wait queue, it
> + * means that the thread was woken up after its timeout expired
> + * before it could reacquire the lock. Thus, when wait.entry is
> + * empty, it needs to harvest events.
> + */
> + if (timed_out)
> + eavail = list_empty(&wait.entry);
> __remove_wait_queue(&ep->wq, &wait);
> write_unlock_irq(&ep->lock);
> }
> --
> 2.37.1
>

2022-11-28 21:09:27

by Benjamin Segall

[permalink] [raw]
Subject: Re: [PATCH 5.4 0/2] Fix epoll issue in 5.4 kernels

Rishabh Bhatnagar <[email protected]> writes:

> Hi Greg
> After upgrading to 5.4.211 we were started seeing some nodes getting
> stuck in our Kubernetes cluster. All nodes are running this kernel
> version. After taking a closer look it seems that runc was command getting
> stuck. Looking at the stack it appears the thread is stuck in epoll wait for
> sometime.
> [<0>] do_syscall_64+0x48/0xf0
> [<0>] entry_SYSCALL_64_after_hwframe+0x5c/0xc1
> [<0>] ep_poll+0x48d/0x4e0
> [<0>] do_epoll_wait+0xab/0xc0
> [<0>] __x64_sys_epoll_pwait+0x4d/0xa0
> [<0>] do_syscall_64+0x48/0xf0
> [<0>] entry_SYSCALL_64_after_hwframe+0x5c/0xc1
> [<0>] futex_wait_queue_me+0xb6/0x110
> [<0>] futex_wait+0xe2/0x260
> [<0>] do_futex+0x372/0x4f0
> [<0>] __x64_sys_futex+0x134/0x180
> [<0>] do_syscall_64+0x48/0xf0
> [<0>] entry_SYSCALL_64_after_hwframe+0x5c/0xc1
>
> I noticed there are other discussions going on as well
> regarding this.
> https://lore.kernel.org/all/[email protected]/
> Reverting the below patch does fix the issue:
> https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?h=linux-5.4.y&id=cf2db24ec4b8e9d399005ececd6f6336916ab6fc
> We don't see this issue in latest upstream kernel or even latest 5.10
> stable tree. Looking at the patches that went in for 5.10 stable there's
> one that stands out that seems to be missing in 5.4.
> 289caf5d8f6c61c6d2b7fd752a7f483cd153f182 (epoll: check for events when removing
> a timed out thread from the wait queue)
>
> Backporting this patch to 5.4 we don't see the hangups anymore. Looks like
> this patch fixes time out scenarios which might cause missed wake ups.
> The other patch in the patch series also fixes a race and is needed for
> the second patch to apply.

Yes, this definitely makes sense to me; the aggressive removal was only
valid because the rest of the epoll machinery did plenty of extra
checking. And I didn't as carefully check the backports when I saw the
-stable emails.

2022-12-01 04:49:40

by Samuel Mendoza-Jonas

[permalink] [raw]
Subject: Re: [PATCH 5.4 1/2] epoll: call final ep_events_available() check under the lock

On Thu, Nov 24, 2022 at 12:11:22AM +0000, Rishabh Bhatnagar wrote:
> From: Roman Penyaev <[email protected]>
>
> Commit 65759097d804d2a9ad2b687db436319704ba7019 upstream.
>
> There is a possible race when ep_scan_ready_list() leaves ->rdllist and
> ->obflist empty for a short period of time although some events are
> pending. It is quite likely that ep_events_available() observes empty
> lists and goes to sleep.
>
> Since commit 339ddb53d373 ("fs/epoll: remove unnecessary wakeups of
> nested epoll") we are conservative in wakeups (there is only one place
> for wakeup and this is ep_poll_callback()), thus ep_events_available()
> must always observe correct state of two lists.
>
> The easiest and correct way is to do the final check under the lock.
> This does not impact the performance, since lock is taken anyway for
> adding a wait entry to the wait queue.
>
> The discussion of the problem can be found here:
>
> https://lore.kernel.org/linux-fsdevel/[email protected]/
>
> In this patch barrierless __set_current_state() is used. This is safe
> since waitqueue_active() is called under the same lock on wakeup side.
>
> Short-circuit for fatal signals (i.e. fatal_signal_pending() check) is
> moved to the line just before actual events harvesting routine. This is
> fully compliant to what is said in the comment of the patch where the
> actual fatal_signal_pending() check was added: c257a340ede0 ("fs, epoll:
> short circuit fetching events if thread has been killed").
>
> Fixes: 339ddb53d373 ("fs/epoll: remove unnecessary wakeups of nested epoll")

We may want to consider pulling in this commit as well to 5.4.
339ddb53d373 was merged in v5.5, but there are at least two other
commits already in 5.4 stable that reference it, e.g.

$ git log --oneline v5.4..stable/linux-5.4.y --grep '339ddb53d373'
ee922a2f6be9 eventpoll: fix missing wakeup for ovflist in ep_poll_callback
5d77631de15a epoll: atomically remove wait entry on wake up

Technically it broke things originally, but it smells bad to have
several fixes in the tree for removed code that is still there - any
thoughts?

Cheers,
Sam

> Reported-by: Jason Baron <[email protected]>
> Reported-by: Randy Dunlap <[email protected]>
> Signed-off-by: Roman Penyaev <[email protected]>
> Signed-off-by: Andrew Morton <[email protected]>
> Reviewed-by: Jason Baron <[email protected]>
> Cc: Khazhismel Kumykov <[email protected]>
> Cc: Alexander Viro <[email protected]>
> Cc: <[email protected]>
> Link: http://lkml.kernel.org/r/[email protected]
> Signed-off-by: Linus Torvalds <[email protected]>
> Signed-off-by: Rishabh Bhatnagar <[email protected]>
> ---
> fs/eventpoll.c | 47 +++++++++++++++++++++++++++--------------------
> 1 file changed, 27 insertions(+), 20 deletions(-)
>
> diff --git a/fs/eventpoll.c b/fs/eventpoll.c
> index 7e11135bc915..e5496483a882 100644
> --- a/fs/eventpoll.c
> +++ b/fs/eventpoll.c
> @@ -1905,33 +1905,31 @@ static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events,
> init_wait(&wait);
> wait.func = ep_autoremove_wake_function;
> write_lock_irq(&ep->lock);
> - __add_wait_queue_exclusive(&ep->wq, &wait);
> - write_unlock_irq(&ep->lock);
> -
> /*
> - * We don't want to sleep if the ep_poll_callback() sends us
> - * a wakeup in between. That's why we set the task state
> - * to TASK_INTERRUPTIBLE before doing the checks.
> + * Barrierless variant, waitqueue_active() is called under
> + * the same lock on wakeup ep_poll_callback() side, so it
> + * is safe to avoid an explicit barrier.
> */
> - set_current_state(TASK_INTERRUPTIBLE);
> + __set_current_state(TASK_INTERRUPTIBLE);
> +
> /*
> - * Always short-circuit for fatal signals to allow
> - * threads to make a timely exit without the chance of
> - * finding more events available and fetching
> - * repeatedly.
> + * Do the final check under the lock. ep_scan_ready_list()
> + * plays with two lists (->rdllist and ->ovflist) and there
> + * is always a race when both lists are empty for short
> + * period of time although events are pending, so lock is
> + * important.
> */
> - if (fatal_signal_pending(current)) {
> - res = -EINTR;
> - break;
> + eavail = ep_events_available(ep);
> + if (!eavail) {
> + if (signal_pending(current))
> + res = -EINTR;
> + else
> + __add_wait_queue_exclusive(&ep->wq, &wait);
> }
> + write_unlock_irq(&ep->lock);
>
> - eavail = ep_events_available(ep);
> - if (eavail)
> - break;
> - if (signal_pending(current)) {
> - res = -EINTR;
> + if (eavail || res)
> break;
> - }
>
> if (!schedule_hrtimeout_range(to, slack, HRTIMER_MODE_ABS)) {
> timed_out = 1;
> @@ -1952,6 +1950,15 @@ static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events,
> }
>
> send_events:
> + if (fatal_signal_pending(current)) {
> + /*
> + * Always short-circuit for fatal signals to allow
> + * threads to make a timely exit without the chance of
> + * finding more events available and fetching
> + * repeatedly.
> + */
> + res = -EINTR;
> + }
> /*
> * Try to transfer events to user space. In case we get 0 events and
> * there's still timeout left over, we go trying again in search of
> --
> 2.37.1
>