Skip to content

Commit e91b481

Browse files
oleg-nesterovaxboe
authored andcommitted
task_work: teach task_work_add() to do signal_wake_up()
So that the target task will exit the wait_event_interruptible-like loop and call task_work_run() asap. The patch turns "bool notify" into 0,TWA_RESUME,TWA_SIGNAL enum, the new TWA_SIGNAL flag implies signal_wake_up(). However, it needs to avoid the race with recalc_sigpending(), so the patch also adds the new JOBCTL_TASK_WORK bit included in JOBCTL_PENDING_MASK. TODO: once this patch is merged we need to change all current users of task_work_add(notify = true) to use TWA_RESUME. Cc: [email protected] # v5.7 Acked-by: Peter Zijlstra (Intel) <[email protected]> Signed-off-by: Oleg Nesterov <[email protected]> Signed-off-by: Jens Axboe <[email protected]>
1 parent d60b5fb commit e91b481

File tree

4 files changed

+28
-7
lines changed

4 files changed

+28
-7
lines changed

include/linux/sched/jobctl.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ struct task_struct;
1919
#define JOBCTL_TRAPPING_BIT 21 /* switching to TRACED */
2020
#define JOBCTL_LISTENING_BIT 22 /* ptracer is listening for events */
2121
#define JOBCTL_TRAP_FREEZE_BIT 23 /* trap for cgroup freezer */
22+
#define JOBCTL_TASK_WORK_BIT 24 /* set by TWA_SIGNAL */
2223

2324
#define JOBCTL_STOP_DEQUEUED (1UL << JOBCTL_STOP_DEQUEUED_BIT)
2425
#define JOBCTL_STOP_PENDING (1UL << JOBCTL_STOP_PENDING_BIT)
@@ -28,9 +29,10 @@ struct task_struct;
2829
#define JOBCTL_TRAPPING (1UL << JOBCTL_TRAPPING_BIT)
2930
#define JOBCTL_LISTENING (1UL << JOBCTL_LISTENING_BIT)
3031
#define JOBCTL_TRAP_FREEZE (1UL << JOBCTL_TRAP_FREEZE_BIT)
32+
#define JOBCTL_TASK_WORK (1UL << JOBCTL_TASK_WORK_BIT)
3133

3234
#define JOBCTL_TRAP_MASK (JOBCTL_TRAP_STOP | JOBCTL_TRAP_NOTIFY)
33-
#define JOBCTL_PENDING_MASK (JOBCTL_STOP_PENDING | JOBCTL_TRAP_MASK)
35+
#define JOBCTL_PENDING_MASK (JOBCTL_STOP_PENDING | JOBCTL_TRAP_MASK | JOBCTL_TASK_WORK)
3436

3537
extern bool task_set_jobctl_pending(struct task_struct *task, unsigned long mask);
3638
extern void task_clear_jobctl_trapping(struct task_struct *task);

include/linux/task_work.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ init_task_work(struct callback_head *twork, task_work_func_t func)
1313
twork->func = func;
1414
}
1515

16-
int task_work_add(struct task_struct *task, struct callback_head *twork, bool);
16+
#define TWA_RESUME 1
17+
#define TWA_SIGNAL 2
18+
int task_work_add(struct task_struct *task, struct callback_head *twork, int);
19+
1720
struct callback_head *task_work_cancel(struct task_struct *, task_work_func_t);
1821
void task_work_run(void);
1922

kernel/signal.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2529,9 +2529,6 @@ bool get_signal(struct ksignal *ksig)
25292529
struct signal_struct *signal = current->signal;
25302530
int signr;
25312531

2532-
if (unlikely(current->task_works))
2533-
task_work_run();
2534-
25352532
if (unlikely(uprobe_deny_signal()))
25362533
return false;
25372534

@@ -2544,6 +2541,13 @@ bool get_signal(struct ksignal *ksig)
25442541

25452542
relock:
25462543
spin_lock_irq(&sighand->siglock);
2544+
current->jobctl &= ~JOBCTL_TASK_WORK;
2545+
if (unlikely(current->task_works)) {
2546+
spin_unlock_irq(&sighand->siglock);
2547+
task_work_run();
2548+
goto relock;
2549+
}
2550+
25472551
/*
25482552
* Every stopped thread goes here after wakeup. Check to see if
25492553
* we should notify the parent, prepare_signal(SIGCONT) encodes

kernel/task_work.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ static struct callback_head work_exited; /* all we need is ->next == NULL */
2525
* 0 if succeeds or -ESRCH.
2626
*/
2727
int
28-
task_work_add(struct task_struct *task, struct callback_head *work, bool notify)
28+
task_work_add(struct task_struct *task, struct callback_head *work, int notify)
2929
{
3030
struct callback_head *head;
31+
unsigned long flags;
3132

3233
do {
3334
head = READ_ONCE(task->task_works);
@@ -36,8 +37,19 @@ task_work_add(struct task_struct *task, struct callback_head *work, bool notify)
3637
work->next = head;
3738
} while (cmpxchg(&task->task_works, head, work) != head);
3839

39-
if (notify)
40+
switch (notify) {
41+
case TWA_RESUME:
4042
set_notify_resume(task);
43+
break;
44+
case TWA_SIGNAL:
45+
if (lock_task_sighand(task, &flags)) {
46+
task->jobctl |= JOBCTL_TASK_WORK;
47+
signal_wake_up(task, 0);
48+
unlock_task_sighand(task, &flags);
49+
}
50+
break;
51+
}
52+
4153
return 0;
4254
}
4355

0 commit comments

Comments
 (0)