Skip to content

Commit 466e4d8

Browse files
Sebastian Andrzej SiewiorPeter Zijlstra
authored andcommitted
task_work: Add TWA_NMI_CURRENT as an additional notify mode.
Adding task_work from NMI context requires the following: - The kasan_record_aux_stack() is not NMU safe and must be avoided. - Using TWA_RESUME is NMI safe. If the NMI occurs while the CPU is in userland then it will continue in userland and not invoke the `work' callback. Add TWA_NMI_CURRENT as an additional notify mode. In this mode skip kasan and use irq_work in hardirq-mode to for needed interrupt. Set TIF_NOTIFY_RESUME within the irq_work callback due to k[ac]san instrumentation in test_and_set_bit() which does not look NMI safe in case of a report. Suggested-by: Peter Zijlstra <[email protected]> Signed-off-by: Sebastian Andrzej Siewior <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 058244c commit 466e4d8

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

include/linux/task_work.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ enum task_work_notify_mode {
1818
TWA_RESUME,
1919
TWA_SIGNAL,
2020
TWA_SIGNAL_NO_IPI,
21+
TWA_NMI_CURRENT,
2122
};
2223

2324
static inline bool task_work_pending(struct task_struct *task)

kernel/task_work.c

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
11
// SPDX-License-Identifier: GPL-2.0
2+
#include <linux/irq_work.h>
23
#include <linux/spinlock.h>
34
#include <linux/task_work.h>
45
#include <linux/resume_user_mode.h>
56

67
static struct callback_head work_exited; /* all we need is ->next == NULL */
78

9+
static void task_work_set_notify_irq(struct irq_work *entry)
10+
{
11+
test_and_set_tsk_thread_flag(current, TIF_NOTIFY_RESUME);
12+
}
13+
static DEFINE_PER_CPU(struct irq_work, irq_work_NMI_resume) =
14+
IRQ_WORK_INIT_HARD(task_work_set_notify_irq);
15+
816
/**
917
* task_work_add - ask the @task to execute @work->func()
1018
* @task: the task which should run the callback
1119
* @work: the callback to run
1220
* @notify: how to notify the targeted task
1321
*
1422
* Queue @work for task_work_run() below and notify the @task if @notify
15-
* is @TWA_RESUME, @TWA_SIGNAL, or @TWA_SIGNAL_NO_IPI.
23+
* is @TWA_RESUME, @TWA_SIGNAL, @TWA_SIGNAL_NO_IPI or @TWA_NMI_CURRENT.
1624
*
1725
* @TWA_SIGNAL works like signals, in that the it will interrupt the targeted
1826
* task and run the task_work, regardless of whether the task is currently
@@ -24,6 +32,8 @@ static struct callback_head work_exited; /* all we need is ->next == NULL */
2432
* kernel anyway.
2533
* @TWA_RESUME work is run only when the task exits the kernel and returns to
2634
* user mode, or before entering guest mode.
35+
* @TWA_NMI_CURRENT works like @TWA_RESUME, except it can only be used for the
36+
* current @task and if the current context is NMI.
2737
*
2838
* Fails if the @task is exiting/exited and thus it can't process this @work.
2939
* Otherwise @work->func() will be called when the @task goes through one of
@@ -44,8 +54,13 @@ int task_work_add(struct task_struct *task, struct callback_head *work,
4454
{
4555
struct callback_head *head;
4656

47-
/* record the work call stack in order to print it in KASAN reports */
48-
kasan_record_aux_stack(work);
57+
if (notify == TWA_NMI_CURRENT) {
58+
if (WARN_ON_ONCE(task != current))
59+
return -EINVAL;
60+
} else {
61+
/* record the work call stack in order to print it in KASAN reports */
62+
kasan_record_aux_stack(work);
63+
}
4964

5065
head = READ_ONCE(task->task_works);
5166
do {
@@ -66,6 +81,9 @@ int task_work_add(struct task_struct *task, struct callback_head *work,
6681
case TWA_SIGNAL_NO_IPI:
6782
__set_notify_signal(task);
6883
break;
84+
case TWA_NMI_CURRENT:
85+
irq_work_queue(this_cpu_ptr(&irq_work_NMI_resume));
86+
break;
6987
default:
7088
WARN_ON_ONCE(1);
7189
break;

0 commit comments

Comments
 (0)