sched: add a few helpers to wake up tasks on the current cpu
authorAndrei Vagin <avagin@google.com>
Wed, 8 Mar 2023 07:31:58 +0000 (23:31 -0800)
committerKees Cook <keescook@chromium.org>
Mon, 17 Jul 2023 23:08:08 +0000 (16:08 -0700)
Add complete_on_current_cpu, wake_up_poll_on_current_cpu helpers to wake
up tasks on the current CPU.

These two helpers are useful when the task needs to make a synchronous context
switch to another task. In this context, synchronous means it wakes up the
target task and falls asleep right after that.

One example of such workloads is seccomp user notifies. This mechanism allows
the  supervisor process handles system calls on behalf of a target process.
While the supervisor is handling an intercepted system call, the target process
will be blocked in the kernel, waiting for a response to come back.

On-CPU context switches are much faster than regular ones.

Signed-off-by: Andrei Vagin <avagin@google.com>
Acked-by: "Peter Zijlstra (Intel)" <peterz@infradead.org>
Link: https://lore.kernel.org/r/20230308073201.3102738-4-avagin@google.com
Signed-off-by: Kees Cook <keescook@chromium.org>
include/linux/completion.h
include/linux/swait.h
include/linux/wait.h
kernel/sched/completion.c
kernel/sched/core.c
kernel/sched/swait.c
kernel/sched/wait.c

index 62b32b19e0a86817cd84f8b311624dc61b3f73d3..fb291567657432083162031ddb949a7e581e2848 100644 (file)
@@ -116,6 +116,7 @@ extern bool try_wait_for_completion(struct completion *x);
 extern bool completion_done(struct completion *x);
 
 extern void complete(struct completion *);
+extern void complete_on_current_cpu(struct completion *x);
 extern void complete_all(struct completion *);
 
 #endif
index 6a8c22b8c2a5fdeb3a6e9f7b3f9108920167e49c..d324419482a0f5282ceb9174d091ff4204a49022 100644 (file)
@@ -146,7 +146,7 @@ static inline bool swq_has_sleeper(struct swait_queue_head *wq)
 
 extern void swake_up_one(struct swait_queue_head *q);
 extern void swake_up_all(struct swait_queue_head *q);
-extern void swake_up_locked(struct swait_queue_head *q);
+extern void swake_up_locked(struct swait_queue_head *q, int wake_flags);
 
 extern void prepare_to_swait_exclusive(struct swait_queue_head *q, struct swait_queue *wait, int state);
 extern long prepare_to_swait_event(struct swait_queue_head *q, struct swait_queue *wait, int state);
index a0307b516b099ed50d7e9aca6b89e3019078b949..5ec7739400f4d7b923734f66fb50dfcb58243f2a 100644 (file)
@@ -210,6 +210,7 @@ __remove_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_entry *wq
 }
 
 int __wake_up(struct wait_queue_head *wq_head, unsigned int mode, int nr, void *key);
+void __wake_up_on_current_cpu(struct wait_queue_head *wq_head, unsigned int mode, void *key);
 void __wake_up_locked_key(struct wait_queue_head *wq_head, unsigned int mode, void *key);
 void __wake_up_locked_key_bookmark(struct wait_queue_head *wq_head,
                unsigned int mode, void *key, wait_queue_entry_t *bookmark);
@@ -237,6 +238,8 @@ void __wake_up_pollfree(struct wait_queue_head *wq_head);
 #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))
+#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)                                              \
        __wake_up_locked_key((x), TASK_NORMAL, poll_to_key(m))
 #define wake_up_interruptible_poll(x, m)                                       \
index d57a5c1c1cd97ac15bc23a0e1d56f1d3ea7ee83f..3561ab533dd4e33ddb5284bcab51736f9b9ab6bf 100644 (file)
  * Waiting for completion is a typically sync point, but not an exclusion point.
  */
 
+static void complete_with_flags(struct completion *x, int wake_flags)
+{
+       unsigned long flags;
+
+       raw_spin_lock_irqsave(&x->wait.lock, flags);
+
+       if (x->done != UINT_MAX)
+               x->done++;
+       swake_up_locked(&x->wait, wake_flags);
+       raw_spin_unlock_irqrestore(&x->wait.lock, flags);
+}
+
+void complete_on_current_cpu(struct completion *x)
+{
+       return complete_with_flags(x, WF_CURRENT_CPU);
+}
+
 /**
  * complete: - signals a single thread waiting on this completion
  * @x:  holds the state of this particular completion
  */
 void complete(struct completion *x)
 {
-       unsigned long flags;
-
-       raw_spin_lock_irqsave(&x->wait.lock, flags);
-
-       if (x->done != UINT_MAX)
-               x->done++;
-       swake_up_locked(&x->wait);
-       raw_spin_unlock_irqrestore(&x->wait.lock, flags);
+       complete_with_flags(x, 0);
 }
 EXPORT_SYMBOL(complete);
 
index 1574989627b0727ff92aab7ed7d67216e61c0a30..4d63e063608a6794e54a9e30226cde72acc79017 100644 (file)
@@ -7029,7 +7029,7 @@ asmlinkage __visible void __sched preempt_schedule_irq(void)
 int default_wake_function(wait_queue_entry_t *curr, unsigned mode, int wake_flags,
                          void *key)
 {
-       WARN_ON_ONCE(IS_ENABLED(CONFIG_SCHED_DEBUG) && wake_flags & ~WF_SYNC);
+       WARN_ON_ONCE(IS_ENABLED(CONFIG_SCHED_DEBUG) && wake_flags & ~(WF_SYNC|WF_CURRENT_CPU));
        return try_to_wake_up(curr->private, mode, wake_flags);
 }
 EXPORT_SYMBOL(default_wake_function);
index 76b9b796e695671b502b650035c9cc5afc55cb05..72505cd3b60a3e16c093d07da38eab6093155b2e 100644 (file)
@@ -18,7 +18,7 @@ EXPORT_SYMBOL(__init_swait_queue_head);
  * If for some reason it would return 0, that means the previously waiting
  * task is already running, so it will observe condition true (or has already).
  */
-void swake_up_locked(struct swait_queue_head *q)
+void swake_up_locked(struct swait_queue_head *q, int wake_flags)
 {
        struct swait_queue *curr;
 
@@ -26,7 +26,7 @@ void swake_up_locked(struct swait_queue_head *q)
                return;
 
        curr = list_first_entry(&q->task_list, typeof(*curr), task_list);
-       wake_up_process(curr->task);
+       try_to_wake_up(curr->task, TASK_NORMAL, wake_flags);
        list_del_init(&curr->task_list);
 }
 EXPORT_SYMBOL(swake_up_locked);
@@ -41,7 +41,7 @@ EXPORT_SYMBOL(swake_up_locked);
 void swake_up_all_locked(struct swait_queue_head *q)
 {
        while (!list_empty(&q->task_list))
-               swake_up_locked(q);
+               swake_up_locked(q, 0);
 }
 
 void swake_up_one(struct swait_queue_head *q)
@@ -49,7 +49,7 @@ void swake_up_one(struct swait_queue_head *q)
        unsigned long flags;
 
        raw_spin_lock_irqsave(&q->lock, flags);
-       swake_up_locked(q);
+       swake_up_locked(q, 0);
        raw_spin_unlock_irqrestore(&q->lock, flags);
 }
 EXPORT_SYMBOL(swake_up_one);
index 48c53e4739ea4ea367078250ce3bbf694991e548..802d98cf2de317aa601100f4b153aabc0bb236b3 100644 (file)
@@ -161,6 +161,11 @@ int __wake_up(struct wait_queue_head *wq_head, unsigned int mode,
 }
 EXPORT_SYMBOL(__wake_up);
 
+void __wake_up_on_current_cpu(struct wait_queue_head *wq_head, unsigned int mode, void *key)
+{
+       __wake_up_common_lock(wq_head, mode, 1, WF_CURRENT_CPU, key);
+}
+
 /*
  * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
  */