Skip to content

Commit 6f63904

Browse files
avaginkees
authored andcommitted
sched: add a few helpers to wake up tasks on the current cpu
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 <[email protected]> Acked-by: "Peter Zijlstra (Intel)" <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Kees Cook <[email protected]>
1 parent ab83f45 commit 6f63904

File tree

7 files changed

+33
-14
lines changed

7 files changed

+33
-14
lines changed

include/linux/completion.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ extern bool try_wait_for_completion(struct completion *x);
116116
extern bool completion_done(struct completion *x);
117117

118118
extern void complete(struct completion *);
119+
extern void complete_on_current_cpu(struct completion *x);
119120
extern void complete_all(struct completion *);
120121

121122
#endif

include/linux/swait.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ static inline bool swq_has_sleeper(struct swait_queue_head *wq)
146146

147147
extern void swake_up_one(struct swait_queue_head *q);
148148
extern void swake_up_all(struct swait_queue_head *q);
149-
extern void swake_up_locked(struct swait_queue_head *q);
149+
extern void swake_up_locked(struct swait_queue_head *q, int wake_flags);
150150

151151
extern void prepare_to_swait_exclusive(struct swait_queue_head *q, struct swait_queue *wait, int state);
152152
extern long prepare_to_swait_event(struct swait_queue_head *q, struct swait_queue *wait, int state);

include/linux/wait.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ __remove_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_entry *wq
210210
}
211211

212212
int __wake_up(struct wait_queue_head *wq_head, unsigned int mode, int nr, void *key);
213+
void __wake_up_on_current_cpu(struct wait_queue_head *wq_head, unsigned int mode, void *key);
213214
void __wake_up_locked_key(struct wait_queue_head *wq_head, unsigned int mode, void *key);
214215
void __wake_up_locked_key_bookmark(struct wait_queue_head *wq_head,
215216
unsigned int mode, void *key, wait_queue_entry_t *bookmark);
@@ -237,6 +238,8 @@ void __wake_up_pollfree(struct wait_queue_head *wq_head);
237238
#define key_to_poll(m) ((__force __poll_t)(uintptr_t)(void *)(m))
238239
#define wake_up_poll(x, m) \
239240
__wake_up(x, TASK_NORMAL, 1, poll_to_key(m))
241+
#define wake_up_poll_on_current_cpu(x, m) \
242+
__wake_up_on_current_cpu(x, TASK_NORMAL, poll_to_key(m))
240243
#define wake_up_locked_poll(x, m) \
241244
__wake_up_locked_key((x), TASK_NORMAL, poll_to_key(m))
242245
#define wake_up_interruptible_poll(x, m) \

kernel/sched/completion.c

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,23 @@
1313
* Waiting for completion is a typically sync point, but not an exclusion point.
1414
*/
1515

16+
static void complete_with_flags(struct completion *x, int wake_flags)
17+
{
18+
unsigned long flags;
19+
20+
raw_spin_lock_irqsave(&x->wait.lock, flags);
21+
22+
if (x->done != UINT_MAX)
23+
x->done++;
24+
swake_up_locked(&x->wait, wake_flags);
25+
raw_spin_unlock_irqrestore(&x->wait.lock, flags);
26+
}
27+
28+
void complete_on_current_cpu(struct completion *x)
29+
{
30+
return complete_with_flags(x, WF_CURRENT_CPU);
31+
}
32+
1633
/**
1734
* complete: - signals a single thread waiting on this completion
1835
* @x: holds the state of this particular completion
@@ -27,14 +44,7 @@
2744
*/
2845
void complete(struct completion *x)
2946
{
30-
unsigned long flags;
31-
32-
raw_spin_lock_irqsave(&x->wait.lock, flags);
33-
34-
if (x->done != UINT_MAX)
35-
x->done++;
36-
swake_up_locked(&x->wait);
37-
raw_spin_unlock_irqrestore(&x->wait.lock, flags);
47+
complete_with_flags(x, 0);
3848
}
3949
EXPORT_SYMBOL(complete);
4050

kernel/sched/core.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7029,7 +7029,7 @@ asmlinkage __visible void __sched preempt_schedule_irq(void)
70297029
int default_wake_function(wait_queue_entry_t *curr, unsigned mode, int wake_flags,
70307030
void *key)
70317031
{
7032-
WARN_ON_ONCE(IS_ENABLED(CONFIG_SCHED_DEBUG) && wake_flags & ~WF_SYNC);
7032+
WARN_ON_ONCE(IS_ENABLED(CONFIG_SCHED_DEBUG) && wake_flags & ~(WF_SYNC|WF_CURRENT_CPU));
70337033
return try_to_wake_up(curr->private, mode, wake_flags);
70347034
}
70357035
EXPORT_SYMBOL(default_wake_function);

kernel/sched/swait.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ EXPORT_SYMBOL(__init_swait_queue_head);
1818
* If for some reason it would return 0, that means the previously waiting
1919
* task is already running, so it will observe condition true (or has already).
2020
*/
21-
void swake_up_locked(struct swait_queue_head *q)
21+
void swake_up_locked(struct swait_queue_head *q, int wake_flags)
2222
{
2323
struct swait_queue *curr;
2424

2525
if (list_empty(&q->task_list))
2626
return;
2727

2828
curr = list_first_entry(&q->task_list, typeof(*curr), task_list);
29-
wake_up_process(curr->task);
29+
try_to_wake_up(curr->task, TASK_NORMAL, wake_flags);
3030
list_del_init(&curr->task_list);
3131
}
3232
EXPORT_SYMBOL(swake_up_locked);
@@ -41,15 +41,15 @@ EXPORT_SYMBOL(swake_up_locked);
4141
void swake_up_all_locked(struct swait_queue_head *q)
4242
{
4343
while (!list_empty(&q->task_list))
44-
swake_up_locked(q);
44+
swake_up_locked(q, 0);
4545
}
4646

4747
void swake_up_one(struct swait_queue_head *q)
4848
{
4949
unsigned long flags;
5050

5151
raw_spin_lock_irqsave(&q->lock, flags);
52-
swake_up_locked(q);
52+
swake_up_locked(q, 0);
5353
raw_spin_unlock_irqrestore(&q->lock, flags);
5454
}
5555
EXPORT_SYMBOL(swake_up_one);

kernel/sched/wait.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,11 @@ int __wake_up(struct wait_queue_head *wq_head, unsigned int mode,
161161
}
162162
EXPORT_SYMBOL(__wake_up);
163163

164+
void __wake_up_on_current_cpu(struct wait_queue_head *wq_head, unsigned int mode, void *key)
165+
{
166+
__wake_up_common_lock(wq_head, mode, 1, WF_CURRENT_CPU, key);
167+
}
168+
164169
/*
165170
* Same as __wake_up but called with the spinlock in wait_queue_head_t held.
166171
*/

0 commit comments

Comments
 (0)