Linux kernel -stable discussions
 help / color / mirror / Atom feed
* [PATCH v2 0/2] sched/wait: Queue non-exclusive waiters at the head in do_wait_intr*()
@ 2026-11-23  0:35 Mani Tofigh via B4 Relay
  2026-11-23  0:35 ` [PATCH v2 1/2] " Mani Tofigh via B4 Relay
  0 siblings, 1 reply; 6+ messages in thread
From: Mani Tofigh via B4 Relay @ 2026-11-23  0:35 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar
  Cc: Oleg Nesterov, Linus Torvalds, Christian Brauner,
	Michal Nazarewicz, Wen Yang, linux-fsdevel, linux-kernel, tz2294,
	stable, Mani Tofigh

Patch 1 is the fix from v1, rebased on top of Linus's commit
cea5f91daa3a ("wait-event: clean up the internal helper arguments").
Patch 2 makes __wait_event_interruptible_locked() take wq_flags like
___wait_event() now does, as Oleg suggested.

Reproducer: https://github.com/manitofigh/files/blob/main/linux-repros/evexcl.c

gpiolib-cdev and cros_ec_chardev use the same helpers and can be
polled too, so this fixes them as well.

---
Changes in v2:
- Rebased on Linus's master, on top of cea5f91daa3a.
- New patch 2: pass wq_flags instead of a 0/1 "exclusive" argument (Oleg).
- Link to v1: https://patch.msgid.link/20261111-b4-sched-wait-intr-head-v1-1-17cf2af5b964@columbia.edu
- Discussion: https://lore.kernel.org/all/awMhxEuMHULvk8SA@redhat.com/

---
Mani Tofigh (2):
      sched/wait: Queue non-exclusive waiters at the head in do_wait_intr*()
      sched/wait: Pass wq_flags to __wait_event_interruptible_locked()

 include/linux/wait.h | 11 ++++++-----
 kernel/sched/wait.c  | 16 ++++++++++++----
 2 files changed, 18 insertions(+), 9 deletions(-)
---
base-commit: 6e4122f0242a3262512ee3e6f2e7e6de0bd5792b
change-id: 20261111-b4-sched-wait-intr-head-c5e8f0b541f4

Best regards,
--  
Mani Tofigh <mt4022@columbia.edu>



^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v2 1/2] sched/wait: Queue non-exclusive waiters at the head in do_wait_intr*()
  2026-11-23  0:35 [PATCH v2 0/2] sched/wait: Queue non-exclusive waiters at the head in do_wait_intr*() Mani Tofigh via B4 Relay
@ 2026-11-23  0:35 ` Mani Tofigh via B4 Relay
  2026-11-23 14:28   ` Oleg Nesterov
  2026-11-23 23:52   ` Linus Torvalds
  0 siblings, 2 replies; 6+ messages in thread
From: Mani Tofigh via B4 Relay @ 2026-11-23  0:35 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar
  Cc: Oleg Nesterov, Linus Torvalds, Christian Brauner,
	Michal Nazarewicz, Wen Yang, linux-fsdevel, linux-kernel, tz2294,
	stable, Mani Tofigh

From: Mani Tofigh <mt4022@columbia.edu>

A task blocked in read() on an eventfd or timerfd can sleep forever
while the file is readable, if an epoll(EPOLLEXCLUSIVE) watcher of the
same file was added before it.

eventfd and timerfd wait with wait_event_interruptible_locked_irq().
do_wait_intr_irq() adds the reader at the tail, behind the exclusive
epoll entry, and __wake_up_common() stops after waking that entry.  If
the woken epoll task is killed or leaves without reading, nothing wakes
the reader:

  Task E                   Task A                 Task C
  epoll_wait(ep, -1)
    /* EPOLLEXCLUSIVE */
                           read(efd)
                             /* queued after E */
                                                  write(efd, 1)
                                                    /* wakes E, stops */
                                                  kill(E, SIGKILL)
                           /* still asleep, efd is readable */

A periodic timerfd doesn't wake A either, since the timer is restarted
only by a read.

prepare_to_wait_event() and add_wait_queue() add non-exclusive waiters
at the head, which is what __wake_up_common() relies on.  eventfd did
the same itself before it switched to these helpers.

Do the same in do_wait_intr() and do_wait_intr_irq().  This only changes
the order in which non-exclusive waiters are woken.  Each of them still
gets every wakeup.  With the fix, A is woken by the write.

Fixes: 22c43c81a51e ("wait_event_interruptible_locked() interface")
Cc: stable@vger.kernel.org
Signed-off-by: Mani Tofigh <mt4022@columbia.edu>
---
 kernel/sched/wait.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)

diff --git a/kernel/sched/wait.c b/kernel/sched/wait.c
index f8d6d8b9d03e..342dec5fd937 100644
--- a/kernel/sched/wait.c
+++ b/kernel/sched/wait.c
@@ -319,8 +319,12 @@ EXPORT_SYMBOL(prepare_to_wait_event);
  */
 int do_wait_intr(wait_queue_head_t *wq, wait_queue_entry_t *wait)
 {
-	if (likely(list_empty(&wait->entry)))
-		__add_wait_queue_entry_tail(wq, wait);
+	if (likely(list_empty(&wait->entry))) {
+		if (wait->flags & WQ_FLAG_EXCLUSIVE)
+			__add_wait_queue_entry_tail(wq, wait);
+		else
+			__add_wait_queue(wq, wait);
+	}
 
 	set_current_state(TASK_INTERRUPTIBLE);
 	if (signal_pending(current))
@@ -336,8 +340,12 @@ EXPORT_SYMBOL(do_wait_intr);
 
 int do_wait_intr_irq(wait_queue_head_t *wq, wait_queue_entry_t *wait)
 {
-	if (likely(list_empty(&wait->entry)))
-		__add_wait_queue_entry_tail(wq, wait);
+	if (likely(list_empty(&wait->entry))) {
+		if (wait->flags & WQ_FLAG_EXCLUSIVE)
+			__add_wait_queue_entry_tail(wq, wait);
+		else
+			__add_wait_queue(wq, wait);
+	}
 
 	set_current_state(TASK_INTERRUPTIBLE);
 	if (signal_pending(current))

-- 
2.53.0



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH v2 1/2] sched/wait: Queue non-exclusive waiters at the head in do_wait_intr*()
  2026-11-23  0:35 ` [PATCH v2 1/2] " Mani Tofigh via B4 Relay
@ 2026-11-23 14:28   ` Oleg Nesterov
  2026-11-23 23:52   ` Linus Torvalds
  1 sibling, 0 replies; 6+ messages in thread
From: Oleg Nesterov @ 2026-11-23 14:28 UTC (permalink / raw)
  To: Mani Tofigh via B4 Relay
  Cc: Peter Zijlstra, Ingo Molnar, Linus Torvalds, Christian Brauner,
	Michal Nazarewicz, Wen Yang, linux-fsdevel, linux-kernel, tz2294,
	stable, Mani Tofigh

On 11/22, Mani Tofigh via B4 Relay wrote:
>
> --- a/kernel/sched/wait.c
> +++ b/kernel/sched/wait.c
> @@ -319,8 +319,12 @@ EXPORT_SYMBOL(prepare_to_wait_event);
>   */
>  int do_wait_intr(wait_queue_head_t *wq, wait_queue_entry_t *wait)
>  {
> -	if (likely(list_empty(&wait->entry)))
> -		__add_wait_queue_entry_tail(wq, wait);
> +	if (likely(list_empty(&wait->entry))) {
> +		if (wait->flags & WQ_FLAG_EXCLUSIVE)
> +			__add_wait_queue_entry_tail(wq, wait);
> +		else
> +			__add_wait_queue(wq, wait);
> +	}
>  
>  	set_current_state(TASK_INTERRUPTIBLE);
>  	if (signal_pending(current))
> @@ -336,8 +340,12 @@ EXPORT_SYMBOL(do_wait_intr);
>  
>  int do_wait_intr_irq(wait_queue_head_t *wq, wait_queue_entry_t *wait)
>  {
> -	if (likely(list_empty(&wait->entry)))
> -		__add_wait_queue_entry_tail(wq, wait);
> +	if (likely(list_empty(&wait->entry))) {
> +		if (wait->flags & WQ_FLAG_EXCLUSIVE)
> +			__add_wait_queue_entry_tail(wq, wait);
> +		else
> +			__add_wait_queue(wq, wait);
> +	}
>  
>  	set_current_state(TASK_INTERRUPTIBLE);
>  	if (signal_pending(current))

perhaps it makes sense to add the new trivial helper later,
prepare_to_wait_event() can use it too.

Reviewed-by: Oleg Nesterov <oleg@redhat.com>


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2 1/2] sched/wait: Queue non-exclusive waiters at the head in do_wait_intr*()
  2026-11-23  0:35 ` [PATCH v2 1/2] " Mani Tofigh via B4 Relay
  2026-11-23 14:28   ` Oleg Nesterov
@ 2026-11-23 23:52   ` Linus Torvalds
  2026-11-24 11:54     ` Oleg Nesterov
  2026-11-24 13:36     ` Oleg Nesterov
  1 sibling, 2 replies; 6+ messages in thread
From: Linus Torvalds @ 2026-11-23 23:52 UTC (permalink / raw)
  To: mt4022, Jens Axboe
  Cc: Peter Zijlstra, Ingo Molnar, Oleg Nesterov, Christian Brauner,
	Michal Nazarewicz, Wen Yang, linux-fsdevel, linux-kernel, tz2294,
	stable

On Sun, 22 Nov 2026 at 16:35, Mani Tofigh via B4 Relay
<devnull+mt4022.columbia.edu@kernel.org> wrote:
>
> A task blocked in read() on an eventfd or timerfd can sleep forever
> while the file is readable, if an epoll(EPOLLEXCLUSIVE) watcher of the
> same file was added before it.

Honestly, I don't think this is the right approach.

We need to fix the disaster area that is epoll, rather than work around
more epoll bugs elsewhere and make other places more fragile and
complex.

I think we should stop playing games with wait-queue ordering entirely.

The problem WQ_EXCLUSIVE tries to fix is the thundering herd issue, and
I think we could afford to just always walk the whole wait-queue list -
just not wake up multiple exclusive waiters.

The wait-queue traversal itself was never the problem, and stopping
early was a premature optimization that causes these kinds of problems.

For example, right now that ordering means not only that we have that
subtle __add_wait_queue_entry_tail() vs __add_wait_queue() thing - we
also have that disgusting WQ_FLAG_PRIORITY thing.

I think the (single) user of add_wait_queue_priority_exclusive() should
just walk the queue itself, and not mess up the global wait queue logic
with its braindamage.

It's literally just kvm eventfd that is being special - and it's the "my
mom told me I'm special" kind of special, not the "it's really important
and fundamental" kind of special.

The whole hypervisor WQ_FLAG_EXCLUSIVE | WQ_FLAG_PRIORITY games should
be removed from the core entirely and the those eventfd things should
move to their own helpers that nobody else needs to care about.

And I think we should always add new waiters to the end of the queue,
and stop any other games. Just walk the whole queue at wakeup time.

Finally, I think we should make the return value from the wakeup
function be a bitmask, and count "exclusive" as only "we haven't seen
this bit returned yet". And then *real* exclusive users that actually
guarantee that they react to the wakeup return all bits set, but the
sh*t-for-brains garbage that is "epoll" only returns its own bit set -
so we only wake up *one* epoll user, but we wake up other users anyway,
because epoll clearl yis lying about its "exclusivity".

Because trying to fix the mess that epoll has caused is not worth it. We
need to just accept the fact that epoll is garbage, but we also need to
fundamentally make that garbage not mess with the rest of the kernel.

Anyway, because I wanted to see who plays games with the wake-up
function, and because that thing is horribly named ('func' - not exactly
unambiguous), I did the attached patch. It makes it a bit more obvious
who actually uses anything but the normal wakeup functions.

It incidentally also made it obvious that very few people knew about out
'init_wait_func()' helper. We had a couple of users. But we had more
users that did

        init_wait(wait);
        wait->func = im_so_special_wake_fn;

by hand instead. And memcontrol-v1 - that everybody loves - didn't do
even that, but seemed to write things out by hand entirely.

And the i915 driver did its own thing entirely and re-implemented the
whole wakeup side. Christ. nilfss is doing something odd too.

No, this patch has not been tested. It seems to compile, at least on
x86-64 allmodconfig.

Comments? Would anybody be willing to do a deep dive into the whole
wakeup situation? I'd also love to make the wakeup functions return void
instead of the number of things woken - because of that whole "exclusive
would be better off not entirely exclusive" issue, but it looks like the
sbitmap code actually uses it. Nobody else seems to.

Added Jens because he's the sbitmap person.

               Linus

--- untested patch just because I couldn't tell who played games ---

 drivers/gpu/drm/i915/display/intel_display_rps.c |  2 +-
 drivers/gpu/drm/i915/i915_active.c               |  2 +-
 drivers/gpu/drm/i915/i915_sw_fence.c             | 10 +++++-----
 drivers/gpu/drm/radeon/radeon_fence.c            |  2 +-
 fs/dax.c                                         |  9 +++------
 fs/eventpoll.c                                   |  3 +--
 fs/gfs2/glock.c                                  |  3 +--
 fs/nfs/nfs4proc.c                                |  3 +--
 fs/nilfs2/segment.c                              |  2 +-
 include/linux/sbitmap.h                          |  2 +-
 include/linux/wait.h                             | 12 ++++++------
 include/linux/wait_bit.h                         |  2 +-
 io_uring/io-wq.c                                 |  2 +-
 io_uring/rw.c                                    |  2 +-
 kernel/sched/wait.c                              |  4 ++--
 kernel/sched/wait_bit.c                          |  8 ++++----
 mm/filemap.c                                     |  6 ++----
 mm/memcontrol-v1.c                               |  5 +----
 18 files changed, 34 insertions(+), 45 deletions(-)

diff --git a/drivers/gpu/drm/i915/display/intel_display_rps.c
b/drivers/gpu/drm/i915/display/intel_display_rps.c
index 2f8248458826..fa707a144d5b 100644
--- a/drivers/gpu/drm/i915/display/intel_display_rps.c
+++ b/drivers/gpu/drm/i915/display/intel_display_rps.c
@@ -68,7 +68,7 @@ void intel_display_rps_boost_after_vblank(struct
drm_crtc *crtc,
 	wait->fence = dma_fence_get(fence);
 	wait->crtc = crtc;

-	wait->wait.func = do_rps_boost;
+	wait->wait.wake_up_fn = do_rps_boost;
 	wait->wait.flags = 0;

 	add_wait_queue(drm_crtc_vblank_waitqueue(crtc), &wait->wait);
diff --git a/drivers/gpu/drm/i915/i915_active.c
b/drivers/gpu/drm/i915/i915_active.c
index b2442f05825e..1c207ddab5ba 100644
--- a/drivers/gpu/drm/i915/i915_active.c
+++ b/drivers/gpu/drm/i915/i915_active.c
@@ -666,7 +666,7 @@ static int __await_barrier(struct i915_active
*ref, struct i915_sw_fence *fence)
 	}

 	wb->base.flags = 0;
-	wb->base.func = barrier_wake;
+	wb->base.wake_up_fn = barrier_wake;
 	wb->base.private = fence;
 	wb->ref = ref;

diff --git a/drivers/gpu/drm/i915/i915_sw_fence.c
b/drivers/gpu/drm/i915/i915_sw_fence.c
index f24f616e23ee..156a65f009eb 100644
--- a/drivers/gpu/drm/i915/i915_sw_fence.c
+++ b/drivers/gpu/drm/i915/i915_sw_fence.c
@@ -161,7 +161,7 @@ static void __i915_sw_fence_wake_up_all(struct
i915_sw_fence *fence,
 			if (pos->flags & I915_SW_FENCE_FLAG_FENCE)
 				list_move_tail(&pos->entry, continuation);
 			else
-				pos->func(pos, TASK_NORMAL, 0, continuation);
+				pos->wake_up_fn(pos, TASK_NORMAL, 0, continuation);
 		}
 	} else {
 		LIST_HEAD(extra);
@@ -174,7 +174,7 @@ static void __i915_sw_fence_wake_up_all(struct
i915_sw_fence *fence,
 				if (pos->flags & I915_SW_FENCE_FLAG_FENCE)
 					wake_flags = fence->error;

-				pos->func(pos, TASK_NORMAL, wake_flags, &extra);
+				pos->wake_up_fn(pos, TASK_NORMAL, wake_flags, &extra);
 			}

 			if (list_empty(&extra))
@@ -291,7 +291,7 @@ static bool __i915_sw_fence_check_if_after(struct
i915_sw_fence *fence,
 		return true;

 	list_for_each_entry(wq, &fence->wait.head, entry) {
-		if (wq->func != i915_sw_fence_wake)
+		if (wq->wake_up_fn != i915_sw_fence_wake)
 			continue;

 		if (__i915_sw_fence_check_if_after(wq->private, signaler))
@@ -309,7 +309,7 @@ static void
__i915_sw_fence_clear_checked_bit(struct i915_sw_fence *fence)
 		return;

 	list_for_each_entry(wq, &fence->wait.head, entry) {
-		if (wq->func != i915_sw_fence_wake)
+		if (wq->wake_up_fn != i915_sw_fence_wake)
 			continue;

 		__i915_sw_fence_clear_checked_bit(wq->private);
@@ -375,7 +375,7 @@ static int __i915_sw_fence_await_sw_fence(struct
i915_sw_fence *fence,

 	INIT_LIST_HEAD(&wq->entry);
 	wq->flags = pending;
-	wq->func = i915_sw_fence_wake;
+	wq->wake_up_fn = i915_sw_fence_wake;
 	wq->private = fence;

 	i915_sw_fence_await(fence);
diff --git a/drivers/gpu/drm/radeon/radeon_fence.c
b/drivers/gpu/drm/radeon/radeon_fence.c
index 8e9402b8811b..d254278d0cb5 100644
--- a/drivers/gpu/drm/radeon/radeon_fence.c
+++ b/drivers/gpu/drm/radeon/radeon_fence.c
@@ -409,7 +409,7 @@ static bool radeon_fence_enable_signaling(struct
dma_fence *f)

 	fence->fence_wake.flags = 0;
 	fence->fence_wake.private = NULL;
-	fence->fence_wake.func = radeon_fence_check_signaled;
+	fence->fence_wake.wake_up_fn = radeon_fence_check_signaled;
 	__add_wait_queue(&rdev->fence_queue, &fence->fence_wake);
 	dma_fence_get(f);
 	return true;
diff --git a/fs/dax.c b/fs/dax.c
index 9d161d7ef3ef..4e8cad4dcb95 100644
--- a/fs/dax.c
+++ b/fs/dax.c
@@ -215,8 +215,7 @@ static void *get_next_unlocked_entry(struct
xa_state *xas, unsigned int order)
 	struct wait_exceptional_entry_queue ewait;
 	wait_queue_head_t *wq;

-	init_wait(&ewait.wait);
-	ewait.wait.func = wake_exceptional_entry_func;
+	init_wait_func(&ewait.wait, wake_exceptional_entry_func);

 	for (;;) {
 		entry = xas_find_conflict(xas);
@@ -248,8 +247,7 @@ static void *wait_entry_unlocked_exclusive(struct
xa_state *xas, void *entry)
 	struct wait_exceptional_entry_queue ewait;
 	wait_queue_head_t *wq;

-	init_wait(&ewait.wait);
-	ewait.wait.func = wake_exceptional_entry_func;
+	init_wait_func(&ewait.wait, wake_exceptional_entry_func);

 	while (unlikely(dax_is_locked(entry))) {
 		wq = dax_entry_waitqueue(xas, entry, &ewait.key);
@@ -279,8 +277,7 @@ static void wait_entry_unlocked(struct xa_state
*xas, void *entry)
 	struct wait_exceptional_entry_queue ewait;
 	wait_queue_head_t *wq;

-	init_wait(&ewait.wait);
-	ewait.wait.func = wake_exceptional_entry_func;
+	init_wait_func(&ewait.wait, wake_exceptional_entry_func);

 	wq = dax_entry_waitqueue(xas, entry, &ewait.key);
 	/*
diff --git a/fs/eventpoll.c b/fs/eventpoll.c
index b959af04772c..0e13477f8be5 100644
--- a/fs/eventpoll.c
+++ b/fs/eventpoll.c
@@ -2333,8 +2333,7 @@ static int ep_poll(struct eventpoll *ep, struct
epoll_event __user *events,
 		 * performance issue if a process is killed, causing all of its
 		 * threads to wake up without being removed normally.
 		 */
-		init_wait(&wait);
-		wait.func = ep_autoremove_wake_function;
+		init_wait_func(&wait, ep_autoremove_wake_function);

 		spin_lock_irq(&ep->lock);
 		/*
diff --git a/fs/gfs2/glock.c b/fs/gfs2/glock.c
index d22a088c66cd..cd3ad3cb91a8 100644
--- a/fs/gfs2/glock.c
+++ b/fs/gfs2/glock.c
@@ -1009,8 +1009,7 @@ static struct gfs2_glock
*find_insert_glock(struct lm_lockname *name,
 	struct gfs2_glock *gl;

 	wait.name = name;
-	init_wait(&wait.wait);
-	wait.wait.func = glock_wake_function;
+	init_wait_func(&wait.wait, glock_wake_function);

 again:
 	prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
diff --git a/fs/nfs/nfs4proc.c b/fs/nfs/nfs4proc.c
index 6759fa61b9b4..d817d12249da 100644
--- a/fs/nfs/nfs4proc.c
+++ b/fs/nfs/nfs4proc.c
@@ -7757,8 +7757,7 @@ nfs4_retry_setlk(struct nfs4_state *state, int
cmd, struct file_lock *request)
 	if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
 		return nfs4_retry_setlk_simple(state, cmd, request);

-	init_wait(&waiter.wait);
-	waiter.wait.func = nfs4_wake_lock_waiter;
+	init_wait_func(&waiter.wait, nfs4_wake_lock_waiter);
 	add_wait_queue(q, &waiter.wait);

 	do {
diff --git a/fs/nilfs2/segment.c b/fs/nilfs2/segment.c
index af0c3560444f..ec14365762c2 100644
--- a/fs/nilfs2/segment.c
+++ b/fs/nilfs2/segment.c
@@ -2297,7 +2297,7 @@ static void nilfs_segctor_wakeup(struct
nilfs_sc_info *sci, int err, bool force)
 			atomic_set(&wrq->done, 1);
 		}
 		if (atomic_read(&wrq->done)) {
-			wrq->wq.func(&wrq->wq,
+			wrq->wq.wake_up_fn(&wrq->wq,
 				     TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE,
 				     0, NULL);
 		}
diff --git a/include/linux/sbitmap.h b/include/linux/sbitmap.h
index cc7ad189caa5..6317a60d8df2 100644
--- a/include/linux/sbitmap.h
+++ b/include/linux/sbitmap.h
@@ -595,7 +595,7 @@ struct sbq_wait {
 		.sbq = NULL,							\
 		.wait = {							\
 			.private	= current,				\
-			.func		= autoremove_wake_function,		\
+			.wake_up_fn	= autoremove_wake_function,		\
 			.entry		= LIST_HEAD_INIT((name).wait.entry),	\
 		}								\
 	}
diff --git a/include/linux/wait.h b/include/linux/wait.h
index b3e94c71cd3e..0252fc690b26 100644
--- a/include/linux/wait.h
+++ b/include/linux/wait.h
@@ -28,7 +28,7 @@ int default_wake_function(struct wait_queue_entry
*wq_entry, unsigned mode, int
 struct wait_queue_entry {
 	unsigned int		flags;
 	void			*private;
-	wait_queue_func_t	func;
+	wait_queue_func_t	wake_up_fn;
 	struct list_head	entry;
 };

@@ -46,7 +46,7 @@ struct task_struct;

 #define __WAITQUEUE_INITIALIZER(name, tsk) {					\
 	.private	= tsk,							\
-	.func		= default_wake_function,				\
+	.wake_up_fn	= default_wake_function,				\
 	.entry		= { NULL, NULL } }

 #define DECLARE_WAITQUEUE(name, tsk)						\
@@ -81,7 +81,7 @@ static inline void init_waitqueue_entry(struct
wait_queue_entry *wq_entry, struc
 {
 	wq_entry->flags		= 0;
 	wq_entry->private	= p;
-	wq_entry->func		= default_wake_function;
+	wq_entry->wake_up_fn	= default_wake_function;
 }

 static inline void
@@ -89,7 +89,7 @@ init_waitqueue_func_entry(struct wait_queue_entry
*wq_entry, wait_queue_func_t f
 {
 	wq_entry->flags		= 0;
 	wq_entry->private	= NULL;
-	wq_entry->func		= func;
+	wq_entry->wake_up_fn	= func;
 }

 /**
@@ -1228,7 +1228,7 @@ int autoremove_wake_function(struct
wait_queue_entry *wq_entry, unsigned mode, i
 #define DEFINE_WAIT_FUNC(name, function)					\
 	struct wait_queue_entry name = {					\
 		.private	= current,					\
-		.func		= function,					\
+		.wake_up_fn	= function,					\
 		.entry		= LIST_HEAD_INIT((name).entry),			\
 	}

@@ -1237,7 +1237,7 @@ int autoremove_wake_function(struct
wait_queue_entry *wq_entry, unsigned mode, i
 #define init_wait_func(wait, function)						\
 	do {									\
 		(wait)->private = current;					\
-		(wait)->func = function;					\
+		(wait)->wake_up_fn = function;					\
 		INIT_LIST_HEAD(&(wait)->entry);					\
 		(wait)->flags = 0;						\
 	} while (0)
diff --git a/include/linux/wait_bit.h b/include/linux/wait_bit.h
index af077ed4caf6..0bec1e7ea485 100644
--- a/include/linux/wait_bit.h
+++ b/include/linux/wait_bit.h
@@ -41,7 +41,7 @@ int wake_bit_function(struct wait_queue_entry
*wq_entry, unsigned mode, int sync
 		.key = __WAIT_BIT_KEY_INITIALIZER(word, bit),			\
 		.wq_entry = {							\
 			.private	= current,				\
-			.func		= wake_bit_function,			\
+			.wake_up_fn	= wake_bit_function,			\
 			.entry		=					\
 				LIST_HEAD_INIT((name).wq_entry.entry),		\
 		},								\
diff --git a/io_uring/io-wq.c b/io_uring/io-wq.c
index 9609ae50dbd7..faf159d37026 100644
--- a/io_uring/io-wq.c
+++ b/io_uring/io-wq.c
@@ -1409,7 +1409,7 @@ struct io_wq *io_wq_create(unsigned bounded,
struct io_wq_data *data)
 	wq->acct[IO_WQ_ACCT_UNBOUND].max_workers =
 				task_rlimit(current, RLIMIT_NPROC);
 	INIT_LIST_HEAD(&wq->wait.entry);
-	wq->wait.func = io_wq_hash_wake;
+	wq->wait.wake_up_fn = io_wq_hash_wake;
 	for (i = 0; i < IO_WQ_ACCT_NR; i++) {
 		struct io_wq_acct *acct = &wq->acct[i];

diff --git a/io_uring/rw.c b/io_uring/rw.c
index cf20597ba6a8..3e29bf8a1e57 100644
--- a/io_uring/rw.c
+++ b/io_uring/rw.c
@@ -804,7 +804,7 @@ static bool io_rw_should_retry(struct io_kiocb *req)
 	    !(req->file->f_op->fop_flags & FOP_BUFFER_RASYNC))
 		return false;

-	wait->wait.func = io_async_buf_func;
+	wait->wait.wake_up_fn = io_async_buf_func;
 	wait->wait.private = req;
 	wait->wait.flags = 0;
 	INIT_LIST_HEAD(&wait->wait.entry);
diff --git a/kernel/sched/wait.c b/kernel/sched/wait.c
index f8d6d8b9d03e..5339faf2c84d 100644
--- a/kernel/sched/wait.c
+++ b/kernel/sched/wait.c
@@ -105,7 +105,7 @@ static int __wake_up_common(struct wait_queue_head
*wq_head, unsigned int mode,
 		unsigned flags = curr->flags;
 		int ret;

-		ret = curr->func(curr, mode, wake_flags, key);
+		ret = curr->wake_up_fn(curr, mode, wake_flags, key);
 		if (ret < 0)
 			break;
 		if (ret && (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
@@ -269,7 +269,7 @@ void init_wait_entry(struct wait_queue_entry
*wq_entry, int flags)
 {
 	wq_entry->flags = flags;
 	wq_entry->private = current;
-	wq_entry->func = autoremove_wake_function;
+	wq_entry->wake_up_fn = autoremove_wake_function;
 	INIT_LIST_HEAD(&wq_entry->entry);
 }
 EXPORT_SYMBOL(init_wait_entry);
diff --git a/kernel/sched/wait_bit.c b/kernel/sched/wait_bit.c
index 348f7211b4aa..2936887fad6f 100644
--- a/kernel/sched/wait_bit.c
+++ b/kernel/sched/wait_bit.c
@@ -198,10 +198,10 @@ void init_wait_var_entry(struct
wait_bit_queue_entry *wbq_entry, void *var, int
 			.bit_nr = -1,
 		},
 		.wq_entry = {
-			.flags	 = flags,
-			.private = current,
-			.func	 = var_wake_function,
-			.entry	 = LIST_HEAD_INIT(wbq_entry->wq_entry.entry),
+			.flags	    = flags,
+			.private    = current,
+			.wake_up_fn = var_wake_function,
+			.entry	    = LIST_HEAD_INIT(wbq_entry->wq_entry.entry),
 		},
 	};
 }
diff --git a/mm/filemap.c b/mm/filemap.c
index a1b2ca5b933c..931de8fedc36 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -1263,8 +1263,7 @@ static inline int folio_wait_bit_common(struct
folio *folio, int bit_nr,
 		thrashing = true;
 	}

-	init_wait(wait);
-	wait->func = wake_page_function;
+	init_wait_func(wait, wake_page_function);
 	wait_page.folio = folio;
 	wait_page.bit_nr = bit_nr;

@@ -1430,8 +1429,7 @@ void softleaf_entry_wait_on_locked(softleaf_t
entry, spinlock_t *ptl)
 		thrashing = true;
 	}

-	init_wait(wait);
-	wait->func = wake_page_function;
+	init_wait_func(wait, wake_page_function);
 	wait_page.folio = folio;
 	wait_page.bit_nr = PG_locked;
 	wait->flags = 0;
diff --git a/mm/memcontrol-v1.c b/mm/memcontrol-v1.c
index b7f286888507..cc5c64cbdfff 100644
--- a/mm/memcontrol-v1.c
+++ b/mm/memcontrol-v1.c
@@ -1053,10 +1053,7 @@ bool mem_cgroup_oom_synchronize(bool handle)
 		goto cleanup;

 	owait.memcg = memcg;
-	owait.wait.flags = 0;
-	owait.wait.func = memcg_oom_wake_function;
-	owait.wait.private = current;
-	INIT_LIST_HEAD(&owait.wait.entry);
+	init_wait_func(&owait.wait, memcg_oom_wake_function);

 	prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
 	mem_cgroup_mark_under_oom(memcg);

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH v2 1/2] sched/wait: Queue non-exclusive waiters at the head in do_wait_intr*()
  2026-11-23 23:52   ` Linus Torvalds
@ 2026-11-24 11:54     ` Oleg Nesterov
  2026-11-24 13:36     ` Oleg Nesterov
  1 sibling, 0 replies; 6+ messages in thread
From: Oleg Nesterov @ 2026-11-24 11:54 UTC (permalink / raw)
  To: Linus Torvalds
  Cc: mt4022, Jens Axboe, Peter Zijlstra, Ingo Molnar,
	Christian Brauner, Michal Nazarewicz, Wen Yang, linux-fsdevel,
	linux-kernel, tz2294, stable

[-- Attachment #1: Type: text/plain, Size: 2595 bytes --]

On 11/23, Linus Torvalds wrote:
>
> On Sun, 22 Nov 2026 at 16:35, Mani Tofigh via B4 Relay
> <devnull+mt4022.columbia.edu@kernel.org> wrote:
> >
> > A task blocked in read() on an eventfd or timerfd can sleep forever
> > while the file is readable, if an epoll(EPOLLEXCLUSIVE) watcher of the
> > same file was added before it.
>
> Honestly, I don't think this is the right approach.
>
> We need to fix the disaster area that is epoll, rather than work around
> more epoll bugs elsewhere and make other places more fragile and
> complex.

I agree. But to me it would be better to apply this patch from Mani,
then do something else. Not because it "fixes" the scenario described
in the changelog. As for eventfd_read(), it probably makes sense to
change it to use wait_event_interruptible_exclusive_locked_irq() at
least if !EFD_SEMAPHORE, and then we will have the same problem with
EPOLLEXCLUSIVE again.

But simply because do_wait_intr*() are wrong regardless, and the
change is trivial. And perhaps the changelog should not mention
EPOLLEXCLUSIVE at all.

> And I think we should always add new waiters to the end of the queue,
> and stop any other games. Just walk the whole queue at wakeup time.
>
> Finally, I think we should make the return value from the wakeup
> function be a bitmask, and count "exclusive" as only "we haven't seen
> this bit returned yet". And then *real* exclusive users that actually
> guarantee that they react to the wakeup return all bits set, but the
> sh*t-for-brains garbage that is "epoll" only returns its own bit set -
> so we only wake up *one* epoll user, but we wake up other users anyway,
> because epoll clearl yis lying about its "exclusivity".

Perhaps... But it is not clear how to do this gradually...

And lets forget about epoll for the moment. Lets suppose we change
__wake_up_common() to do

	list_for_each_entry_safe(curr, next, &wq_head->head, entry) {
		bool excl = curr->flags & WQ_FLAG_EXCLUSIVE;
		int ret;

		if (excl && !nr_exclusive)
			continue;
		ret = curr->func(curr, mode, wake_flags, key);
		if (ret < 0)
			break;
		if (ret && excl)
			--nr_exclusive;
	}

This will change the current behaviour if ret < 0. Say wake_page_function() does

	if (flags & WQ_FLAG_EXCLUSIVE) {
		if (test_bit(key->bit_nr, &key->folio->flags.f))
			return -1;
		...

so this logic depends on wait-queue ordering too. "ret < 0" can be encoded
in the bitmask, but it is not clear what exactly __wake_up_common() needs
to do in this case.

Can't we at least cleanup __wake_up_common() ? See the patch below, I already
sent it but it was ignored.

Oleg.


[-- Attachment #2: 0001-sched-wait-kill-leftover-bookmark-logic-in-__wake_up.patch --]
[-- Type: text/plain, Size: 1289 bytes --]

From 887e7597e23b1beb5c6ed536cb342a2daa740ce5 Mon Sep 17 00:00:00 2001
From: Oleg Nesterov <oleg@redhat.com>
Date: Thu, 12 Nov 2026 15:53:53 +0100
Subject: [PATCH] sched/wait: kill leftover bookmark logic in
 __wake_up_common()

The manual list head check and list_for_each_entry_safe_from() in
__wake_up_common() are leftovers from commit 2554db916586 ("sched/wait:
Break up long wake list walk").

This logic was later removed by commit 37acade0ce89 ("sched: remove
wait bookmarks") but the now-unnecessary list hacks were left behind.

Signed-off-by: Oleg Nesterov <oleg@redhat.com>
---
 kernel/sched/wait.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)

diff --git a/kernel/sched/wait.c b/kernel/sched/wait.c
index f8d6d8b9d03e..d0521b54eeb3 100644
--- a/kernel/sched/wait.c
+++ b/kernel/sched/wait.c
@@ -96,12 +96,7 @@ static int __wake_up_common(struct wait_queue_head *wq_head, unsigned int mode,
 
 	lockdep_assert_held(&wq_head->lock);
 
-	curr = list_first_entry(&wq_head->head, wait_queue_entry_t, entry);
-
-	if (&curr->entry == &wq_head->head)
-		return nr_exclusive;
-
-	list_for_each_entry_safe_from(curr, next, &wq_head->head, entry) {
+	list_for_each_entry_safe(curr, next, &wq_head->head, entry) {
 		unsigned flags = curr->flags;
 		int ret;
 
-- 
2.55.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH v2 1/2] sched/wait: Queue non-exclusive waiters at the head in do_wait_intr*()
  2026-11-23 23:52   ` Linus Torvalds
  2026-11-24 11:54     ` Oleg Nesterov
@ 2026-11-24 13:36     ` Oleg Nesterov
  1 sibling, 0 replies; 6+ messages in thread
From: Oleg Nesterov @ 2026-11-24 13:36 UTC (permalink / raw)
  To: Linus Torvalds
  Cc: mt4022, Jens Axboe, Peter Zijlstra, Ingo Molnar,
	Christian Brauner, Michal Nazarewicz, Wen Yang, linux-fsdevel,
	linux-kernel, tz2294, stable

On 11/23, Linus Torvalds wrote:
>
> Finally, I think we should make the return value from the wakeup
> function be a bitmask, and count "exclusive" as only "we haven't seen
> this bit returned yet". And then *real* exclusive users that actually
> guarantee that they react to the wakeup return all bits set, but the
> sh*t-for-brains garbage that is "epoll" only returns its own bit set -
> so we only wake up *one* epoll user, but we wake up other users anyway,
> because epoll clearl yis lying about its "exclusivity".

Ah, I don't think this can work... Suppose that ep_poll_callback()
returns its own EP_XXX bit set. Should __wake_up_common() wake up
the next waiter? It can't know until it calls ->wake_up_fn() which
can wakeup another epoll waiter.

Well. It is not that I like this idea (and I bet neither you ;) but
we can add WQ_FLAG_EPOLLEXCLUSIVE set by ep_ptable_queue_proc() and
__io_queue_proc(), and change __wake_up_common() to check it

	static int __wake_up_common(struct wait_queue_head *wq_head, unsigned int mode,
				int nr_exclusive, int wake_flags, void *key)
	{
		wait_queue_entry_t *curr, *next;
		bool seen_epoll = false;

		lockdep_assert_held(&wq_head->lock);

		list_for_each_entry_safe(curr, next, &wq_head->head, entry) {
			unsigned flags = curr->flags;
			int ret;

			if ((flags & WQ_FLAG_EPOLLEXCLUSIVE) && seen_epoll)
				continue;

			ret = curr->func(curr, mode, wake_flags, key);
			if (ret < 0)
				break;
			if (ret && (flags & WQ_FLAG_EXCLUSIVE)) {
				if (flags & WQ_FLAG_EPOLLEXCLUSIVE)
					seen_epoll = true;
				else if (!--nr_exclusive)
					break;
			}
		}

		return nr_exclusive;
	}

for the start.

Oleg.


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-11-24 13:36 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-23  0:35 [PATCH v2 0/2] sched/wait: Queue non-exclusive waiters at the head in do_wait_intr*() Mani Tofigh via B4 Relay
2026-11-23  0:35 ` [PATCH v2 1/2] " Mani Tofigh via B4 Relay
2026-11-23 14:28   ` Oleg Nesterov
2026-11-23 23:52   ` Linus Torvalds
2026-11-24 11:54     ` Oleg Nesterov
2026-11-24 13:36     ` Oleg Nesterov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox