Skip to contentSkip to conversation

epoll: fix possible lost wakeup on epoll_ctl() path

PATCH Superseded Roman Penyaev 2 participants 5 messages Feb 10, 2020

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/3 epoll: fix possible lost wakeup on epoll_ctl() path

This fixes possible lost wakeup introduced by the a218cc491420. Originally modifications to ep->wq were serialized by ep->wq.lock, but in the a218cc491420 new rw lock was introduced in order to relax fd event path, i.e. callers of ep_poll_callback() function.

After the change ep_modify and ep_insert (both are called on epoll_ctl() path) were switched to ep->lock, but ep_poll (epoll_wait) was using ep->wq.lock on wqueue list modification.

The bug doesn't lead to any wqueue list corruptions, because wake up path and list modifications were serialized by ep->wq.lock internally, but actual waitqueue_active() check prior wake_up() call can be reordered with modifications of ep ready list, thus wake up can be lost.

And yes, can be healed by explicit smp_mb():

list_add_tail(&epi->rdlink, &ep->rdllist);
smp_mb();
if (waitqueue_active(&ep->wq))

	wake_up(&ep->wp);

But let's make it simple, thus current patch replaces ep->wq.lock with the ep->lock for wqueue modifications, thus wake up path always observes activeness of the wqueue correcty.

Fixes a218cc491420 ("epoll: use rwlock in order to reduce ep_poll_callback() contention")

References: https://bugzilla.kernel.org/show_bug.cgi?id=205933

---
Nothing interesting in v2:
changed the comment a bit and specified Reported-by and Bisected-by tags

 fs/eventpoll.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
fs/eventpoll.c +4 -4
index b041b66002db..eee3c92a9ebf 100644
@@ -1854,9 +1854,9 @@ static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events,
 		waiter = true;
 		init_waitqueue_entry(&wait, current);
 
-		spin_lock_irq(&ep->wq.lock);
+		write_lock_irq(&ep->lock);
 		__add_wait_queue_exclusive(&ep->wq, &wait);
-		spin_unlock_irq(&ep->wq.lock);
+		write_unlock_irq(&ep->lock);
 	}
 
 	for (;;) {
@@ -1904,9 +1904,9 @@ static int ep_poll(struct eventpoll *ep, struct epoll_event __user *events,
 		goto fetch_events;
 
 	if (waiter) {
-		spin_lock_irq(&ep->wq.lock);
+		write_lock_irq(&ep->lock);
 		__remove_wait_queue(&ep->wq, &wait);
-		spin_unlock_irq(&ep->wq.lock);
+		write_unlock_irq(&ep->lock);
 	}
 
 	return res;
Signature · 1 line
2.24.1
#1 lore raw

PATCH v2 2/3 epoll: ep->wq can be woken up unlocked in certain cases

Now ep->lock is responsible for wqueue serialization, thus if ep->lock is taken on write path, wake_up_locked() can be invoked.

Though, read path is different. Since concurrent cpus can enter the wake up function it needs to be internally serialized, thus wake_up() variant is used which implies internal spin lock.

Cc Max Neunhoeffer <[email protected]>
Cc Jakub Kicinski <[email protected]>
Cc Christopher Kohlhoff <[email protected]>
Cc Davidlohr Bueso <[email protected]>
Cc Jason Baron <[email protected]>
Cc Andrew Morton <[email protected]>

---
Nothing interesting in v2:
changed the comment a bit

 fs/eventpoll.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
fs/eventpoll.c +9 -3
index eee3c92a9ebf..6e218234bd4a 100644
@@ -1173,7 +1173,7 @@ static inline bool chain_epi_lockless(struct epitem *epi)
  * Another thing worth to mention is that ep_poll_callback() can be called
  * concurrently for the same @epi from different CPUs if poll table was inited
  * with several wait queues entries.  Plural wakeup from different CPUs of a
- * single wait queue is serialized by wq.lock, but the case when multiple wait
+ * single wait queue is serialized by ep->lock, but the case when multiple wait
  * queues are used should be detected accordingly.  This is detected using
  * cmpxchg() operation.
  */
@@ -1248,6 +1248,12 @@ static int ep_poll_callback(wait_queue_entry_t *wait, unsigned mode, int sync, v
 				break;
 			}
 		}
+		/*
+		 * Since here we have the read lock (ep->lock) taken, plural
+		 * wakeup from different CPUs can occur, thus we call wake_up()
+		 * variant which implies its own lock on wqueue. All other paths
+		 * take write lock.
+		 */
 		wake_up(&ep->wq);
 	}
 	if (waitqueue_active(&ep->poll_wait))
@@ -1551,7 +1557,7 @@ static int ep_insert(struct eventpoll *ep, const struct epoll_event *event,
 
 		/* Notify waiting tasks that events are available */
 		if (waitqueue_active(&ep->wq))
-			wake_up(&ep->wq);
+			wake_up_locked(&ep->wq);
 		if (waitqueue_active(&ep->poll_wait))
 			pwake++;
 	}
@@ -1657,7 +1663,7 @@ static int ep_modify(struct eventpoll *ep, struct epitem *epi,
 
 			/* Notify waiting tasks that events are available */
 			if (waitqueue_active(&ep->wq))
-				wake_up(&ep->wq);
+				wake_up_locked(&ep->wq);
 			if (waitqueue_active(&ep->poll_wait))
 				pwake++;
 		}
Signature · 1 line
2.24.1
#2 lore raw

Re: [PATCH v2 2/3] epoll: ep->wq can be woken up unlocked in certain cases

Quoted 50 lines · Roman Penyaev

Now ep->lock is responsible for wqueue serialization, thus if ep->lock is taken on write path, wake_up_locked() can be invoked.

Though, read path is different. Since concurrent cpus can enter the wake up function it needs to be internally serialized, thus wake_up() variant is used which implies internal spin lock.

Cc Max Neunhoeffer <[email protected]>
Cc Jakub Kicinski <[email protected]>
Cc Christopher Kohlhoff <[email protected]>
Cc Davidlohr Bueso <[email protected]>
Cc Jason Baron <[email protected]>
Cc Andrew Morton <[email protected]>

---
Nothing interesting in v2:
changed the comment a bit

 fs/eventpoll.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
fs/eventpoll.c +7 -1
index eee3c92a9ebf..6e218234bd4a 100644
@@ -1173,7 +1173,7 @@ static inline bool chain_epi_lockless(struct epitem *epi)
  * Another thing worth to mention is that ep_poll_callback() can be called
  * concurrently for the same @epi from different CPUs if poll table was inited
  * with several wait queues entries.  Plural wakeup from different CPUs of a
- * single wait queue is serialized by wq.lock, but the case when multiple wait
+ * single wait queue is serialized by ep->lock, but the case when multiple wait
  * queues are used should be detected accordingly.  This is detected using
  * cmpxchg() operation.
  */
@@ -1248,6 +1248,12 @@ static int ep_poll_callback(wait_queue_entry_t *wait, unsigned mode, int sync, v
 				break;
 			}
 		}
+		/*
+		 * Since here we have the read lock (ep->lock) taken, plural
+		 * wakeup from different CPUs can occur, thus we call wake_up()
+		 * variant which implies its own lock on wqueue. All other paths
+		 * take write lock.
+		 */
 		wake_up(&ep->wq);
 	}
 	if (waitqueue_active(&ep->poll_wait))

fs/eventpoll.c static int ep_insert(struct eventpoll *ep, const struct epoll_event *event, · line 1560

Quoted 5 lines
fs/eventpoll.c +1 -1
@@ -1551,7 +1557,7 @@ static int ep_insert(struct eventpoll *ep, const struct epoll_event *event,
 
 		/* Notify waiting tasks that events are available */
 		if (waitqueue_active(&ep->wq))
-			wake_up(&ep->wq);
+			wake_up_locked(&ep->wq);

So I think this will now hit the 'lockdep_assert_held()' in __wake_up_common()? I agree that its correct, but I think it will confuse lockdep here...

Thanks,

-Jason

#3 lore raw

Re: [PATCH v2 2/3] epoll: ep->wq can be woken up unlocked in certain cases

Quoted 61 lines · Jason Baron
Quoted 60 lines · Roman Penyaev

Now ep->lock is responsible for wqueue serialization, thus if ep->lock is taken on write path, wake_up_locked() can be invoked.

Though, read path is different. Since concurrent cpus can enter the wake up function it needs to be internally serialized, thus wake_up() variant is used which implies internal spin lock.

Cc Max Neunhoeffer <[email protected]>
Cc Jakub Kicinski <[email protected]>
Cc Christopher Kohlhoff <[email protected]>
Cc Davidlohr Bueso <[email protected]>
Cc Jason Baron <[email protected]>
Cc Andrew Morton <[email protected]>

---
Nothing interesting in v2:
changed the comment a bit

 fs/eventpoll.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
fs/eventpoll.c +0 -0
index eee3c92a9ebf..6e218234bd4a 100644
@@ -1173,7 +1173,7 @@ static inline bool chain_epi_lockless(struct 
 epitem *epi)
  * Another thing worth to mention is that ep_poll_callback() can be 

called
* concurrently for the same @epi from different CPUs if poll table
was inited
* with several wait queues entries. Plural wakeup from different
CPUs of a
- * single wait queue is serialized by wq.lock, but the case when multiple wait
+ * single wait queue is serialized by ep->lock, but the case when multiple wait
* queues are used should be detected accordingly. This is detected
using
* cmpxchg() operation.
*/

fs/eventpoll.c +6 -0
@@ -1248,6 +1248,12 @@ static int ep_poll_callback(wait_queue_entry_t 
 *wait, unsigned mode, int sync, v
 				break;
 			}
 		}
+		/*
+		 * Since here we have the read lock (ep->lock) taken, plural
+		 * wakeup from different CPUs can occur, thus we call wake_up()
+		 * variant which implies its own lock on wqueue. All other paths
+		 * take write lock.
+		 */
 		wake_up(&ep->wq);
 	}
fs/eventpoll.c +0 -0
 	if (waitqueue_active(&ep->poll_wait))
@@ -1551,7 +1557,7 @@ static int ep_insert(struct eventpoll *ep, const 
 struct epoll_event *event,
 
Quoted 9 lines

fs/eventpoll.c static int ep_insert(struct eventpoll *ep, const struct epoll_event *event, · line 1560

Quoted 4 lines
fs/eventpoll.c +1 -1
 		/* Notify waiting tasks that events are available */
 		if (waitqueue_active(&ep->wq))
-			wake_up(&ep->wq);
+			wake_up_locked(&ep->wq);

So I think this will now hit the 'lockdep_assert_held()' in __wake_up_common()? I agree that its correct, but I think it will confuse lockdep here...

Argh! True. And I do not see any neat way to shut up lockdep here (Calling lock_acquire() manually seems not an option for such minor thing).

Then this optimization is not needed, patch is cancelled.

Thanks for noting that.

--
Roman

#4 lore raw

PATCH v2 3/3 kselftest: introduce new epoll test case

In reply to Roman Penyaev

This testcase repeats epollbug.c from the bug:

https://bugzilla.kernel.org/show_bug.cgi?id=205933

What it tests? It tests the race between epoll_ctl() and epoll_wait(). New event mask passed to epoll_ctl() triggers wake up, which can be missed because of the bug described in the link. Reproduction is 100%, so easy to fix. Kudos, Max, for wonderful testcase.

Cc Max Neunhoeffer <[email protected]>
Cc Jakub Kicinski <[email protected]>
Cc Christopher Kohlhoff <[email protected]>
Cc Davidlohr Bueso <[email protected]>
Cc Jason Baron <[email protected]>
Cc Andrew Morton <[email protected]>

---
Nothing interesting in v2:
changed the comment a bit

 .../filesystems/epoll/epoll_wakeup_test.c     | 67 ++++++++++++++++++-
 1 file changed, 66 insertions(+), 1 deletion(-)
tools/testing/selftests/filesystems/epoll/epoll_wakeup_test.c +66 -1
index 37a04dab56f0..11eee0b60040 100644
@@ -7,13 +7,14 @@
 #include <pthread.h>
 #include <sys/epoll.h>
 #include <sys/socket.h>
+#include <sys/eventfd.h>
 #include "../../kselftest_harness.h"
 
 struct epoll_mtcontext
 {
 	int efd[3];
 	int sfd[4];
-	int count;
+	volatile int count;
 
 	pthread_t main;
 	pthread_t waiter;
@@ -3071,4 +3072,68 @@ TEST(epoll58)
 	close(ctx.sfd[3]);
 }
 
+static void *epoll59_thread(void *ctx_)
+{
+	struct epoll_mtcontext *ctx = ctx_;
+	struct epoll_event e;
+	int i;
+
+	for (i = 0; i < 100000; i++) {
+		while (ctx->count == 0)
+			;
+
+		e.events = EPOLLIN | EPOLLERR | EPOLLET;
+		epoll_ctl(ctx->efd[0], EPOLL_CTL_MOD, ctx->sfd[0], &e);
+		ctx->count = 0;
+	}
+
+	return NULL;
+}
+
+/*
+ *        t0
+ *      (p) \
+ *           e0
+ *     (et) /
+ *        e0
+ *
+ * Based on https://bugzilla.kernel.org/show_bug.cgi?id=205933
+ */
+TEST(epoll59)
+{
+	pthread_t emitter;
+	struct pollfd pfd;
+	struct epoll_event e;
+	struct epoll_mtcontext ctx = { 0 };
+	int i, ret;
+
+	signal(SIGUSR1, signal_handler);
+
+	ctx.efd[0] = epoll_create1(0);
+	ASSERT_GE(ctx.efd[0], 0);
+
+	ctx.sfd[0] = eventfd(1, 0);
+	ASSERT_GE(ctx.sfd[0], 0);
+
+	e.events = EPOLLIN | EPOLLERR | EPOLLET;
+	ASSERT_EQ(epoll_ctl(ctx.efd[0], EPOLL_CTL_ADD, ctx.sfd[0], &e), 0);
+
+	ASSERT_EQ(pthread_create(&emitter, NULL, epoll59_thread, &ctx), 0);
+
+	for (i = 0; i < 100000; i++) {
+		ret = epoll_wait(ctx.efd[0], &e, 1, 1000);
+		ASSERT_GT(ret, 0);
+
+		while (ctx.count != 0)
+			;
+		ctx.count = 1;
+	}
+	if (pthread_tryjoin_np(emitter, NULL) < 0) {
+		pthread_kill(emitter, SIGUSR1);
+		pthread_join(emitter, NULL);
+	}
+	close(ctx.efd[0]);
+	close(ctx.sfd[0]);
+}
+
 TEST_HARNESS_MAIN
Signature · 1 line
2.24.1