Skip to content

Commit

Permalink
task_work: allow TWA_SIGNAL without a rescheduling IPI
Browse files Browse the repository at this point in the history
Some use cases don't always need an IPI when sending a TWA_SIGNAL
notification. Add TWA_SIGNAL_NO_IPI, which is just like TWA_SIGNAL,
except it doesn't send an IPI to the target task. It merely sets
TIF_NOTIFY_SIGNAL and wakes up the task.

Reviewed-by: Pavel Begunkov <asml.silence@gmail.com>
Link: https://lore.kernel.org/r/20220426014904.60384-2-axboe@kernel.dk
Signed-off-by: Jens Axboe <axboe@kernel.dk>
  • Loading branch information
Jens Axboe committed Apr 26, 2022
1 parent 69cc1b6 commit c0c8459
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
13 changes: 11 additions & 2 deletions include/linux/sched/signal.h
Original file line number Diff line number Diff line change
Expand Up @@ -355,14 +355,23 @@ static inline void clear_notify_signal(void)
smp_mb__after_atomic();
}

/*
* Returns 'true' if kick_process() is needed to force a transition from
* user -> kernel to guarantee expedient run of TWA_SIGNAL based task_work.
*/
static inline bool __set_notify_signal(struct task_struct *task)
{
return !test_and_set_tsk_thread_flag(task, TIF_NOTIFY_SIGNAL) &&
!wake_up_state(task, TASK_INTERRUPTIBLE);
}

/*
* Called to break out of interruptible wait loops, and enter the
* exit_to_user_mode_loop().
*/
static inline void set_notify_signal(struct task_struct *task)
{
if (!test_and_set_tsk_thread_flag(task, TIF_NOTIFY_SIGNAL) &&
!wake_up_state(task, TASK_INTERRUPTIBLE))
if (__set_notify_signal(task))
kick_process(task);
}

Expand Down
1 change: 1 addition & 0 deletions include/linux/task_work.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ enum task_work_notify_mode {
TWA_NONE,
TWA_RESUME,
TWA_SIGNAL,
TWA_SIGNAL_NO_IPI,
};

static inline bool task_work_pending(struct task_struct *task)
Expand Down
15 changes: 10 additions & 5 deletions kernel/task_work.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ static struct callback_head work_exited; /* all we need is ->next == NULL */
*
* Queue @work for task_work_run() below and notify the @task if @notify
* is @TWA_RESUME or @TWA_SIGNAL. @TWA_SIGNAL works like signals, in that the
* it will interrupt the targeted task and run the task_work. @TWA_RESUME
* work is run only when the task exits the kernel and returns to user mode,
* or before entering guest mode. Fails if the @task is exiting/exited and thus
* it can't process this @work. Otherwise @work->func() will be called when the
* @task goes through one of the aforementioned transitions, or exits.
* it will interrupt the targeted task and run the task_work. @TWA_SIGNAL_NO_IPI
* works like @TWA_SIGNAL, except it doesn't send a reschedule IPI to force the
* targeted task to reschedule and run task_work. @TWA_RESUME work is run only
* when the task exits the kernel and returns to user mode, or before entering
* guest mode. Fails if the @task is exiting/exited and thus it can't process
* this @work. Otherwise @work->func() will be called when the @task goes
* through one of the aforementioned transitions, or exits.
*
* If the targeted task is exiting, then an error is returned and the work item
* is not queued. It's up to the caller to arrange for an alternative mechanism
Expand Down Expand Up @@ -53,6 +55,9 @@ int task_work_add(struct task_struct *task, struct callback_head *work,
case TWA_SIGNAL:
set_notify_signal(task);
break;
case TWA_SIGNAL_NO_IPI:
__set_notify_signal(task);
break;
default:
WARN_ON_ONCE(1);
break;
Expand Down

0 comments on commit c0c8459

Please sign in to comment.