[PATCH v2 03/16] Introduce wake_up_key()

NeilBrown posted 16 patches 1 month, 1 week ago
[PATCH v2 03/16] Introduce wake_up_key()
Posted by NeilBrown 1 month, 1 week ago
There is a common pattern of passing a key to __wake_up().  The key is
used to select which waiter to wake.

Callers must currently use __wake_up() directly, which requires that a
TASK state and exclusive-waiter-count also be passed.  The desired state
is almost always TASK_NORMAL and these cases do not have exclusive
waiters so the count is not relevant.

This patch introduces
   wake_up_key(wq, key)
which simplifies the call, and changes relevant callers.  An exclusive
waiter count of '1' is used for consistency with wake_up().  In all
current cases this number is irrelevant.

Most callers (all but one) of __wake_up() are converted either to
wake_up_key(), or to the existing wake_up_poll().

Suggested-by: Al Viro <viro@zeniv.linux.org.uk>
Signed-off-by: NeilBrown <neil@brown.name>
---
 fs/gfs2/glock.c               | 2 +-
 fs/nfs/callback_proc.c        | 2 +-
 fs/userfaultfd.c              | 4 ++--
 include/linux/wait.h          | 3 ++-
 io_uring/io_uring.h           | 2 +-
 kernel/locking/percpu-rwsem.c | 2 +-
 kernel/sched/wait.c           | 2 +-
 kernel/sched/wait_bit.c       | 2 +-
 kernel/signal.c               | 3 +--
 mm/memcontrol-v1.c            | 2 +-
 10 files changed, 12 insertions(+), 12 deletions(-)

diff --git a/fs/gfs2/glock.c b/fs/gfs2/glock.c
index b6fd1cb17de7..cbf8f264f908 100644
--- a/fs/gfs2/glock.c
+++ b/fs/gfs2/glock.c
@@ -121,7 +121,7 @@ static void wake_up_glock(struct gfs2_glock *gl)
 	wait_queue_head_t *wq = glock_waitqueue(&gl->gl_name);
 
 	if (waitqueue_active(wq))
-		__wake_up(wq, TASK_NORMAL, 1, &gl->gl_name);
+		wake_up_key(wq, &gl->gl_name);
 }
 
 static void gfs2_glock_dealloc(struct rcu_head *rcu)
diff --git a/fs/nfs/callback_proc.c b/fs/nfs/callback_proc.c
index 8397c43358bd..b1d71c43c87d 100644
--- a/fs/nfs/callback_proc.c
+++ b/fs/nfs/callback_proc.c
@@ -689,7 +689,7 @@ __be32 nfs4_callback_notify_lock(void *argp, void *resp,
 
 	/* Don't wake anybody if the string looked bogus */
 	if (args->cbnl_valid)
-		__wake_up(&cps->clp->cl_lock_waitq, TASK_NORMAL, 0, args);
+		wake_up_key(&cps->clp->cl_lock_waitq, args);
 
 	return htonl(NFS4_OK);
 }
diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c
index 54c6cc7fe9c6..0d58c53bd583 100644
--- a/fs/userfaultfd.c
+++ b/fs/userfaultfd.c
@@ -874,7 +874,7 @@ static int userfaultfd_release(struct inode *inode, struct file *file)
 	 */
 	spin_lock_irq(&ctx->fault_pending_wqh.lock);
 	__wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL, &range);
-	__wake_up(&ctx->fault_wqh, TASK_NORMAL, 1, &range);
+	wake_up_key(&ctx->fault_wqh, &range);
 	spin_unlock_irq(&ctx->fault_pending_wqh.lock);
 
 	/* Flush pending events that may still wait on event_wqh */
@@ -1175,7 +1175,7 @@ static void __wake_userfault(struct userfaultfd_ctx *ctx,
 		__wake_up_locked_key(&ctx->fault_pending_wqh, TASK_NORMAL,
 				     range);
 	if (waitqueue_active(&ctx->fault_wqh))
-		__wake_up(&ctx->fault_wqh, TASK_NORMAL, 1, range);
+		wake_up_key(&ctx->fault_wqh, range);
 	spin_unlock_irq(&ctx->fault_pending_wqh.lock);
 }
 
diff --git a/include/linux/wait.h b/include/linux/wait.h
index 09855d819418..86d751893c9f 100644
--- a/include/linux/wait.h
+++ b/include/linux/wait.h
@@ -224,6 +224,7 @@ void __wake_up_pollfree(struct wait_queue_head *wq_head);
 #define wake_up_locked(x)		__wake_up_locked((x), TASK_NORMAL, 1)
 #define wake_up_all_locked(x)		__wake_up_locked((x), TASK_NORMAL, 0)
 #define wake_up_sync(x)			__wake_up_sync(x, TASK_NORMAL)
+#define wake_up_key(x,k)		__wake_up(x, TASK_NORMAL, 1, k);
 
 #define wake_up_interruptible(x)	__wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)
 #define wake_up_interruptible_nr(x, nr)	__wake_up(x, TASK_INTERRUPTIBLE, nr, NULL)
@@ -236,7 +237,7 @@ void __wake_up_pollfree(struct wait_queue_head *wq_head);
 #define poll_to_key(m) ((void *)(__force uintptr_t)(__poll_t)(m))
 #define key_to_poll(m) ((__force __poll_t)(uintptr_t)(void *)(m))
 #define wake_up_poll(x, m)							\
-	__wake_up(x, TASK_NORMAL, 1, poll_to_key(m))
+	wake_up_key(x, poll_to_key(m))
 #define wake_up_poll_on_current_cpu(x, m)					\
 	__wake_up_on_current_cpu(x, TASK_NORMAL, poll_to_key(m))
 #define wake_up_locked_poll(x, m)						\
diff --git a/io_uring/io_uring.h b/io_uring/io_uring.h
index abc6de227f74..11d8b656ad64 100644
--- a/io_uring/io_uring.h
+++ b/io_uring/io_uring.h
@@ -304,7 +304,7 @@ static inline void __io_wq_wake(struct wait_queue_head *wq)
 	 * epoll and should terminate multishot poll at that point.
 	 */
 	if (wq_has_sleeper(wq))
-		__wake_up(wq, TASK_NORMAL, 0, poll_to_key(EPOLL_URING_WAKE | EPOLLIN));
+		wake_up_poll(wq, EPOLL_URING_WAKE | EPOLLIN);
 }
 
 static inline void io_poll_wq_wake(struct io_ring_ctx *ctx)
diff --git a/kernel/locking/percpu-rwsem.c b/kernel/locking/percpu-rwsem.c
index ef234469baac..1d3e5f03e0f1 100644
--- a/kernel/locking/percpu-rwsem.c
+++ b/kernel/locking/percpu-rwsem.c
@@ -278,7 +278,7 @@ void percpu_up_write(struct percpu_rw_semaphore *sem)
 	/*
 	 * Prod any pending reader/writer to make progress.
 	 */
-	__wake_up(&sem->waiters, TASK_NORMAL, 1, sem);
+	wake_up_key(&sem->waiters, sem);
 
 	/*
 	 * Once this completes (at least one RCU-sched grace period hence) the
diff --git a/kernel/sched/wait.c b/kernel/sched/wait.c
index 20f27e2cf7ae..201d9827580f 100644
--- a/kernel/sched/wait.c
+++ b/kernel/sched/wait.c
@@ -227,7 +227,7 @@ EXPORT_SYMBOL_GPL(__wake_up_sync);	/* For internal use only */
 
 void __wake_up_pollfree(struct wait_queue_head *wq_head)
 {
-	__wake_up(wq_head, TASK_NORMAL, 0, poll_to_key(EPOLLHUP | POLLFREE));
+	wake_up_poll(wq_head, EPOLLHUP | POLLFREE);
 	/* POLLFREE must have cleared the queue. */
 	WARN_ON_ONCE(waitqueue_active(wq_head));
 }
diff --git a/kernel/sched/wait_bit.c b/kernel/sched/wait_bit.c
index 1088d3b7012c..87f9d1428e62 100644
--- a/kernel/sched/wait_bit.c
+++ b/kernel/sched/wait_bit.c
@@ -126,7 +126,7 @@ void __wake_up_bit(struct wait_queue_head *wq_head, unsigned long *word, int bit
 	struct wait_bit_key key = __WAIT_BIT_KEY_INITIALIZER(word, bit);
 
 	if (waitqueue_active(wq_head))
-		__wake_up(wq_head, TASK_NORMAL, 1, &key);
+		wake_up_key(wq_head, &key);
 }
 EXPORT_SYMBOL(__wake_up_bit);
 
diff --git a/kernel/signal.c b/kernel/signal.c
index e2c928de7d2c..e4adffab3a8d 100644
--- a/kernel/signal.c
+++ b/kernel/signal.c
@@ -2154,8 +2154,7 @@ void do_notify_pidfd(struct task_struct *task)
 
 	WARN_ON(task->exit_state == 0);
 
-	__wake_up(&pid->wait_pidfd, TASK_NORMAL, 0,
-			poll_to_key(EPOLLIN | EPOLLRDNORM));
+	wake_up_poll(&pid->wait_pidfd, EPOLLIN | EPOLLRDNORM);
 }
 
 /*
diff --git a/mm/memcontrol-v1.c b/mm/memcontrol-v1.c
index 4b94731305b9..8cb251aa2dcc 100644
--- a/mm/memcontrol-v1.c
+++ b/mm/memcontrol-v1.c
@@ -1328,7 +1328,7 @@ void memcg1_oom_recover(struct mem_cgroup *memcg)
 	 * triggering notification.
 	 */
 	if (memcg && memcg->under_oom)
-		__wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, memcg);
+		wake_up_key(&memcg_oom_waitq, memcg);
 }
 
 /**
-- 
2.50.0.107.gf914562f5916.dirty
Re: [PATCH v2 03/16] Introduce wake_up_key()
Posted by kernel test robot 1 month, 1 week ago
Hi NeilBrown,

kernel test robot noticed the following build errors:

[auto build test ERROR on brauner-vfs/vfs.all]
[also build test ERROR on trondmy-nfs/linux-next linus/master v6.17-rc2 next-20250822]
[cannot apply to tip/sched/core]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/NeilBrown/VFS-discard-err2-in-filename_create/20250822-081444
base:   https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git vfs.all
patch link:    https://lore.kernel.org/r/20250822000818.1086550-4-neil%40brown.name
patch subject: [PATCH v2 03/16] Introduce wake_up_key()
config: sh-randconfig-r073-20250822 (https://download.01.org/0day-ci/archive/20250822/202508222051.JpjiLYJb-lkp@intel.com/config)
compiler: sh4-linux-gcc (GCC) 10.5.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250822/202508222051.JpjiLYJb-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202508222051.JpjiLYJb-lkp@intel.com/

All errors (new ones prefixed by >>):

   drivers/gpio/gpiolib-cdev.c: In function 'linereq_put_event':
>> drivers/gpio/gpiolib-cdev.c:596:2: error: 'else' without a previous 'if'
     596 |  else
         |  ^~~~
   drivers/gpio/gpiolib-cdev.c: In function 'lineevent_irq_thread':
   drivers/gpio/gpiolib-cdev.c:2018:2: error: 'else' without a previous 'if'
    2018 |  else
         |  ^~~~
   drivers/gpio/gpiolib-cdev.c: In function 'lineinfo_changed_func':
   drivers/gpio/gpiolib-cdev.c:2536:2: error: 'else' without a previous 'if'
    2536 |  else
         |  ^~~~


vim +596 drivers/gpio/gpiolib-cdev.c

a0dda508bd66b9e Bartosz Golaszewski 2023-08-17  581  
73e0341992b68bb Kent Gibson         2020-09-28  582  static void linereq_put_event(struct linereq *lr,
73e0341992b68bb Kent Gibson         2020-09-28  583  			      struct gpio_v2_line_event *le)
73e0341992b68bb Kent Gibson         2020-09-28  584  {
73e0341992b68bb Kent Gibson         2020-09-28  585  	bool overflow = false;
73e0341992b68bb Kent Gibson         2020-09-28  586  
0ebeaab4d59eb37 Kent Gibson         2023-12-19  587  	scoped_guard(spinlock, &lr->wait.lock) {
73e0341992b68bb Kent Gibson         2020-09-28  588  		if (kfifo_is_full(&lr->events)) {
73e0341992b68bb Kent Gibson         2020-09-28  589  			overflow = true;
73e0341992b68bb Kent Gibson         2020-09-28  590  			kfifo_skip(&lr->events);
73e0341992b68bb Kent Gibson         2020-09-28  591  		}
73e0341992b68bb Kent Gibson         2020-09-28  592  		kfifo_in(&lr->events, le, 1);
0ebeaab4d59eb37 Kent Gibson         2023-12-19  593  	}
73e0341992b68bb Kent Gibson         2020-09-28  594  	if (!overflow)
73e0341992b68bb Kent Gibson         2020-09-28  595  		wake_up_poll(&lr->wait, EPOLLIN);
73e0341992b68bb Kent Gibson         2020-09-28 @596  	else
73e0341992b68bb Kent Gibson         2020-09-28  597  		pr_debug_ratelimited("event FIFO is full - event dropped\n");
73e0341992b68bb Kent Gibson         2020-09-28  598  }
73e0341992b68bb Kent Gibson         2020-09-28  599  

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki