Skip to content

Commit 12db8b6

Browse files
axboeKAGA-KOKO
authored andcommitted
entry: Add support for TIF_NOTIFY_SIGNAL
Add TIF_NOTIFY_SIGNAL handling in the generic entry code, which if set, will return true if signal_pending() is used in a wait loop. That causes an exit of the loop so that notify_signal tracehooks can be run. If the wait loop is currently inside a system call, the system call is restarted once task_work has been processed. In preparation for only having arch_do_signal() handle syscall restarts if _TIF_SIGPENDING isn't set, rename it to arch_do_signal_or_restart(). Pass in a boolean that tells the architecture specific signal handler if it should attempt to get a signal, or just process a potential syscall restart. For !CONFIG_GENERIC_ENTRY archs, add the TIF_NOTIFY_SIGNAL handling to get_signal(). This is done to minimize the needed architecture changes to support this feature. Signed-off-by: Jens Axboe <axboe@kernel.dk> Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Reviewed-by: Oleg Nesterov <oleg@redhat.com> Link: https://lore.kernel.org/r/20201026203230.386348-3-axboe@kernel.dk
1 parent 5c251e9 commit 12db8b6

8 files changed

Lines changed: 77 additions & 11 deletions

File tree

‎arch/x86/kernel/signal.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -804,11 +804,11 @@ static inline unsigned long get_nr_restart_syscall(const struct pt_regs *regs)
804804
* want to handle. Thus you cannot kill init even with a SIGKILL even by
805805
* mistake.
806806
*/
807-
void arch_do_signal(struct pt_regs *regs)
807+
void arch_do_signal_or_restart(struct pt_regs *regs, bool has_signal)
808808
{
809809
struct ksignal ksig;
810810

811-
if (get_signal(&ksig)) {
811+
if (has_signal && get_signal(&ksig)) {
812812
/* Whee! Actually deliver the signal. */
813813
handle_signal(&ksig, regs);
814814
return;

‎include/linux/entry-common.h‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@
3737
# define _TIF_UPROBE (0)
3838
#endif
3939

40+
#ifndef _TIF_NOTIFY_SIGNAL
41+
# define _TIF_NOTIFY_SIGNAL (0)
42+
#endif
43+
4044
/*
4145
* TIF flags handled in syscall_enter_from_usermode()
4246
*/
@@ -69,7 +73,7 @@
6973

7074
#define EXIT_TO_USER_MODE_WORK \
7175
(_TIF_SIGPENDING | _TIF_NOTIFY_RESUME | _TIF_UPROBE | \
72-
_TIF_NEED_RESCHED | _TIF_PATCH_PENDING | \
76+
_TIF_NEED_RESCHED | _TIF_PATCH_PENDING | _TIF_NOTIFY_SIGNAL | \
7377
ARCH_EXIT_TO_USER_MODE_WORK)
7478

7579
/**
@@ -226,12 +230,13 @@ static __always_inline void arch_exit_to_user_mode(void) { }
226230
#endif
227231

228232
/**
229-
* arch_do_signal - Architecture specific signal delivery function
233+
* arch_do_signal_or_restart - Architecture specific signal delivery function
230234
* @regs: Pointer to currents pt_regs
235+
* @has_signal: actual signal to handle
231236
*
232237
* Invoked from exit_to_user_mode_loop().
233238
*/
234-
void arch_do_signal(struct pt_regs *regs);
239+
void arch_do_signal_or_restart(struct pt_regs *regs, bool has_signal);
235240

236241
/**
237242
* arch_syscall_exit_tracehook - Wrapper around tracehook_report_syscall_exit()

‎include/linux/entry-kvm.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
# define ARCH_XFER_TO_GUEST_MODE_WORK (0)
1212
#endif
1313

14-
#define XFER_TO_GUEST_MODE_WORK \
15-
(_TIF_NEED_RESCHED | _TIF_SIGPENDING | \
14+
#define XFER_TO_GUEST_MODE_WORK \
15+
(_TIF_NEED_RESCHED | _TIF_SIGPENDING | _TIF_NOTIFY_SIGNAL | \
1616
_TIF_NOTIFY_RESUME | ARCH_XFER_TO_GUEST_MODE_WORK)
1717

1818
struct kvm_vcpu;

‎include/linux/sched/signal.h‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,15 @@ static inline int task_sigpending(struct task_struct *p)
360360

361361
static inline int signal_pending(struct task_struct *p)
362362
{
363+
#if defined(TIF_NOTIFY_SIGNAL)
364+
/*
365+
* TIF_NOTIFY_SIGNAL isn't really a signal, but it requires the same
366+
* behavior in terms of ensuring that we break out of wait loops
367+
* so that notify signal callbacks can be processed.
368+
*/
369+
if (unlikely(test_tsk_thread_flag(p, TIF_NOTIFY_SIGNAL)))
370+
return 1;
371+
#endif
363372
return task_sigpending(p);
364373
}
365374

@@ -507,7 +516,7 @@ extern int set_user_sigmask(const sigset_t __user *umask, size_t sigsetsize);
507516
static inline void restore_saved_sigmask_unless(bool interrupted)
508517
{
509518
if (interrupted)
510-
WARN_ON(!test_thread_flag(TIF_SIGPENDING));
519+
WARN_ON(!signal_pending(current));
511520
else
512521
restore_saved_sigmask();
513522
}

‎include/linux/tracehook.h‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,4 +198,31 @@ static inline void tracehook_notify_resume(struct pt_regs *regs)
198198
blkcg_maybe_throttle_current();
199199
}
200200

201+
/*
202+
* called by exit_to_user_mode_loop() if ti_work & _TIF_NOTIFY_SIGNAL. This
203+
* is currently used by TWA_SIGNAL based task_work, which requires breaking
204+
* wait loops to ensure that task_work is noticed and run.
205+
*/
206+
static inline void tracehook_notify_signal(void)
207+
{
208+
#if defined(TIF_NOTIFY_SIGNAL)
209+
clear_thread_flag(TIF_NOTIFY_SIGNAL);
210+
smp_mb__after_atomic();
211+
if (current->task_works)
212+
task_work_run();
213+
#endif
214+
}
215+
216+
/*
217+
* Called when we have work to process from exit_to_user_mode_loop()
218+
*/
219+
static inline void set_notify_signal(struct task_struct *task)
220+
{
221+
#if defined(TIF_NOTIFY_SIGNAL)
222+
if (!test_and_set_tsk_thread_flag(task, TIF_NOTIFY_SIGNAL) &&
223+
!wake_up_state(task, TASK_INTERRUPTIBLE))
224+
kick_process(task);
225+
#endif
226+
}
227+
201228
#endif /* <linux/tracehook.h> */

‎kernel/entry/common.c‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,15 @@ static __always_inline void exit_to_user_mode(void)
109109
}
110110

111111
/* Workaround to allow gradual conversion of architecture code */
112-
void __weak arch_do_signal(struct pt_regs *regs) { }
112+
void __weak arch_do_signal_or_restart(struct pt_regs *regs, bool has_signal) { }
113+
114+
static void handle_signal_work(struct pt_regs *regs, unsigned long ti_work)
115+
{
116+
if (ti_work & _TIF_NOTIFY_SIGNAL)
117+
tracehook_notify_signal();
118+
119+
arch_do_signal_or_restart(regs, ti_work & _TIF_SIGPENDING);
120+
}
113121

114122
static unsigned long exit_to_user_mode_loop(struct pt_regs *regs,
115123
unsigned long ti_work)
@@ -131,8 +139,8 @@ static unsigned long exit_to_user_mode_loop(struct pt_regs *regs,
131139
if (ti_work & _TIF_PATCH_PENDING)
132140
klp_update_patch_state(current);
133141

134-
if (ti_work & _TIF_SIGPENDING)
135-
arch_do_signal(regs);
142+
if (ti_work & (_TIF_SIGPENDING | _TIF_NOTIFY_SIGNAL))
143+
handle_signal_work(regs, ti_work);
136144

137145
if (ti_work & _TIF_NOTIFY_RESUME) {
138146
clear_thread_flag(TIF_NOTIFY_RESUME);

‎kernel/entry/kvm.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ static int xfer_to_guest_mode_work(struct kvm_vcpu *vcpu, unsigned long ti_work)
88
do {
99
int ret;
1010

11+
if (ti_work & _TIF_NOTIFY_SIGNAL)
12+
tracehook_notify_signal();
13+
1114
if (ti_work & _TIF_SIGPENDING) {
1215
kvm_handle_signal_exit(vcpu);
1316
return -EINTR;

‎kernel/signal.c‎

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

2532+
/*
2533+
* For non-generic architectures, check for TIF_NOTIFY_SIGNAL so
2534+
* that the arch handlers don't all have to do it. If we get here
2535+
* without TIF_SIGPENDING, just exit after running signal work.
2536+
*/
2537+
#ifdef TIF_NOTIFY_SIGNAL
2538+
if (!IS_ENABLED(CONFIG_GENERIC_ENTRY)) {
2539+
if (test_thread_flag(TIF_NOTIFY_SIGNAL))
2540+
tracehook_notify_signal();
2541+
if (!task_sigpending(current))
2542+
return false;
2543+
}
2544+
#endif
2545+
25322546
if (unlikely(uprobe_deny_signal()))
25332547
return false;
25342548

0 commit comments

Comments
 (0)