entry: Provide generic implementation for host and guest entry/exit work
cover entry: Provide generic implementation for host and guest entry/exit work
When working on a way to move out the posix cpu timer expiry out of the timer interrupt context, I noticed that KVM is not handling pending task work before entering a guest. A quick hack was to add that to the x86 KVM handling loop. The discussion ended with a request to make this a generic infrastructure possible with also moving the per arch implementations of the enter from and return to user space handling generic.
https://lore.kernel.org/r/[email protected]
You asked for it, so don't complain that you have to review it :)
The series implements the syscall enter/exit and the general exit to userspace work handling along with the pre guest enter functionality.
The series converts x86 and ARM64. x86 is fully tested including selftests etc. ARM64 is only compile tested for now as my only ARM64 testbox is not available right now.
Thanks,
tglx
--- /Makefile | 3 arch/Kconfig | 3 arch/arm64/Kconfig | 1 arch/arm64/include/asm/kvm_host.h | 1 arch/arm64/kernel/entry.S | 18 - arch/arm64/kernel/ptrace.c | 65 ------ arch/arm64/kernel/signal.c | 45 ---- arch/arm64/kernel/syscall.c | 49 ---- arch/x86/Kconfig | 1 arch/x86/entry/common.c | 265 +------------------------- arch/x86/entry/entry_32.S | 13 - arch/x86/entry/entry_64.S | 12 - arch/x86/entry/entry_64_compat.S | 21 -- arch/x86/include/asm/signal.h | 1 arch/x86/include/asm/thread_info.h | 9 arch/x86/kernel/signal.c | 2 arch/x86/kvm/x86.c | 17 - b/arch/arm64/include/asm/entry-common.h | 76 +++++++ b/arch/x86/include/asm/entry-common.h | 104 ++++++++++ b/include/linux/entry-common.h | 324 ++++++++++++++++++++++++++++++++ b/kernel/entry/common.c | 220 +++++++++++++++++++++ kernel/Makefile | 1 22 files changed, 776 insertions(+), 475 deletions(-)
RFC PATCH 1/15 entry: Provide generic syscall entry functionality
On syscall entry certain work needs to be done conditionally like tracing, seccomp etc. This code is duplicated in all architectures.
Provide a generic version.
--- arch/Kconfig | 3 + include/linux/entry-common.h | 122 +++++++++++++++++++++++++++++++++++++++++++ kernel/Makefile | 1 kernel/entry/Makefile | 3 + kernel/entry/common.c | 33 +++++++++++ 5 files changed, 162 insertions(+)
arch/Kconfig +3 -0
@@ -27,6 +27,9 @@ config HAVE_IMA_KEXEC config HOTPLUG_SMT bool +config GENERIC_ENTRY + bool + config OPROFILE tristate "OProfile system profiling" depends on PROFILING
include/linux/entry-common.h new file +122 -0
122 lines not on this page · raw message
kernel/Makefile +1 -0
@@ -43,6 +43,7 @@ obj-y += irq/ obj-y += rcu/ obj-y += livepatch/ obj-y += dma/ +obj-y += entry/ obj-$(CONFIG_CHECKPOINT_RESTORE) += kcmp.o obj-$(CONFIG_FREEZER) += freezer.o
kernel/entry/Makefile new file +3 -0
@@ -0,0 +1,3 @@ +# SPDX-License-Identifier: GPL-2.0 + +obj-$(CONFIG_GENERIC_ENTRY) += common.o
kernel/entry/common.c new file +33 -0
@@ -0,0 +1,33 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/context_tracking.h> +#include <linux/entry-common.h> + +#define CREATE_TRACE_POINTS +#include <trace/events/syscalls.h> + +long core_syscall_enter_from_usermode (struct pt_regs *regs,long syscall) +{ +unsigned long ti_work =READ_ONCE (current_thread_info ()->flags); +unsigned long ret =0 ; + +if (ti_work & (_TIF_SYSCALL_TRACE | _TIF_SYSCALL_EMU)) { + ret =arch_syscall_enter_tracehook (regs); +if (ret || (ti_work & _TIF_SYSCALL_EMU)) +return -1L ; + } + +/* Do seccomp after ptrace, to catch any tracer changes. */ +if (ti_work & _TIF_SECCOMP) { + ret =arch_syscall_enter_seccomp (regs); +if (ret ==-1L ) +return ret; + } + +if (unlikely (test_thread_flag (TIF_SYSCALL_TRACEPOINT ))) +trace_sys_enter (regs, syscall); + +arch_syscall_enter_audit (regs); + +return ret ? : syscall; +}
Andy Lutomirski
maintainerRe: [RFC patch 01/15] entry: Provide generic syscall entry functionality
Quoted 55 lines · Thomas Gleixner
On syscall entry certain work needs to be done conditionally like tracing, seccomp etc. This code is duplicated in all architectures.
Provide a generic version.
Signed-off-by Thomas Gleixner <[email protected]>--- arch/Kconfig | 3 + include/linux/entry-common.h | 122 +++++++++++++++++++++++++++++++++++++++++++ kernel/Makefile | 1 kernel/entry/Makefile | 3 + kernel/entry/common.c | 33 +++++++++++ 5 files changed, 162 insertions(+)arch/Kconfig +3
-0@@ -27,6 +27,9 @@ config HAVE_IMA_KEXEC config HOTPLUG_SMT bool +config GENERIC_ENTRY + bool + config OPROFILE tristate "OProfile system profiling" depends on PROFILINGinclude/linux/entry-common.h new file +25
-0@@ -0,0 +1,122 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __LINUX_ENTRYCOMMON_H +#define __LINUX_ENTRYCOMMON_H + +#include <linux/tracehook.h> +#include <linux/syscalls.h> +#include <linux/seccomp.h> +#include <linux/sched.h> +#include <linux/audit.h> + +#include <asm/entry-common.h> + +/* +* Define dummy _TIF work flags if not defined by the architecture or for +* disabled functionality. +*/ +#ifndef _TIF_SYSCALL_TRACE +# define _TIF_SYSCALL_TRACE (0) +#endif + +#ifndef _TIF_SYSCALL_EMU +# define _TIF_SYSCALL_EMU (0) +#endif + +#ifndef _TIF_SYSCALL_TRACEPOINT
include/linux/entry-common.h line 35
Quoted 10 lines
include/linux/entry-common.h new file +10
-0+# define _TIF_SYSCALL_TRACEPOINT (0) +#endif + +#ifndef _TIF_SECCOMP +# define _TIF_SECCOMP (0) +#endif + +#ifndef _TIF_AUDIT +# define _TIF_AUDIT (0) +#endif
I'm wondering if these should be __TIF (double-underscore) or MAYBE_TIF_ or something to avoid errors where people do flags |= TIF_WHATEVER and get surprised.
include/linux/entry-common.h line 108
Quoted 11 lines
include/linux/entry-common.h new file +11
-0+/** +* syscall_enter_from_usermode - Check and handle work before invoking +* a syscall +* @regs: Pointer to currents pt_regs +* @syscall: The syscall number +* +* Invoked from architecture specific syscall entry code with interrupts +* enabled. +* +* Returns: The original or a modified syscall number +*/
Maybe document that it can return -1 to skip the syscall and that, if this happens, it may use syscall_set_error() or syscall_set_return_value() first. If neither of those is called and -1 is returned, then the syscall will fail with ENOSYS.
Re: [RFC patch 01/15] entry: Provide generic syscall entry functionality
Quoted 8 lines · Andy Lutomirski
include/linux/entry-common.h line 35
Quoted 3 lines · Thomas Gleixner
include/linux/entry-common.h new file +3
-0+#ifndef _TIF_AUDIT +# define _TIF_AUDIT (0) +#endif I'm wondering if these should be
__TIF(double-underscore) orMAYBE_TIF_or something to avoid errors where people do flags |=TIF_WHATEVERand get surprised.
That's what exists today already. See arch/*/include/asm/threadinfo.h
Quoted 12 lines
Quoted 11 lines
+/**
+ *syscall_enter_from_usermode- Check and handle work before invoking
+ * a syscall
+ * @regs: Pointer to currentspt_regs
+ * @syscall: The syscall number
+ *
+ * Invoked from architecture specific syscall entry code with interrupts
+ * enabled.
+ *
+ * Returns: The original or a modified syscall number
+ */
Quoted 4 lines
Maybe document that it can return -1 to skip the syscall and that, if this happens, it may use
syscall_set_error()orsyscall_set_return_value()first. If neither of those is called and -1 is returned, then the syscall will fail with ENOSYS.
Sure.
Thanks,
tglx
Re: [RFC patch 01/15] entry: Provide generic syscall entry functionality
In reply to Thomas GleixnerQuoted 75 lines · Thomas Gleixner
On syscall entry certain work needs to be done conditionally like tracing, seccomp etc. This code is duplicated in all architectures.
Provide a generic version.
Signed-off-by Thomas Gleixner <[email protected]>--- arch/Kconfig | 3 + include/linux/entry-common.h | 122 +++++++++++++++++++++++++++++++++++++++++++ kernel/Makefile | 1 kernel/entry/Makefile | 3 + kernel/entry/common.c | 33 +++++++++++ 5 files changed, 162 insertions(+)arch/Kconfig +3
-0@@ -27,6 +27,9 @@ config HAVE_IMA_KEXEC config HOTPLUG_SMT bool +config GENERIC_ENTRY + bool + config OPROFILE tristate "OProfile system profiling" depends on PROFILINGinclude/linux/entry-common.h new file +46
-0@@ -0,0 +1,122 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __LINUX_ENTRYCOMMON_H +#define __LINUX_ENTRYCOMMON_H + +#include <linux/tracehook.h> +#include <linux/syscalls.h> +#include <linux/seccomp.h> +#include <linux/sched.h> +#include <linux/audit.h> + +#include <asm/entry-common.h> + +/* +* Define dummy _TIF work flags if not defined by the architecture or for +* disabled functionality. +*/ +#ifndef _TIF_SYSCALL_TRACE +# define _TIF_SYSCALL_TRACE (0) +#endif + +#ifndef _TIF_SYSCALL_EMU +# define _TIF_SYSCALL_EMU (0) +#endif + +#ifndef _TIF_SYSCALL_TRACEPOINT +# define _TIF_SYSCALL_TRACEPOINT (0) +#endif + +#ifndef _TIF_SECCOMP +# define _TIF_SECCOMP (0) +#endif + +#ifndef _TIF_AUDIT +# define _TIF_AUDIT (0) +#endif + +/* +* TIF flags handled in syscall_enter_from_usermode() +*/ +#ifndef ARCH_SYSCALL_ENTER_WORK +# define ARCH_SYSCALL_ENTER_WORK (0) +#endif + +#define SYSCALL_ENTER_WORK \ + (_TIF_SYSCALL_TRACE | _TIF_SYSCALL_AUDIT | TIF_SECCOMP | \ + _TIF_SYSCALL_TRACEPOINT | _TIF_SYSCALL_EMU | \
include/linux/entry-common.h line 56
Quoted 10 lines
include/linux/entry-common.h new file +10
-0+ ARCH_SYSCALL_ENTER_WORK) + +/** +* arch_syscall_enter_tracehook - Wrapper around tracehook_report_syscall_entry() +* +* Defaults to tracehook_report_syscall_entry(). Can be replaced by +* architecture specific code. +* +* Invoked from syscall_enter_from_usermode() +*/
Nit: the kernel-doc here and in other places in the patchset lacks parameter and return value descriptions, which will create lots of warnings for 'make *docs'.
Quoted 120 lines
include/linux/entry-common.h new file +66
-0+static inline __must_check int arch_syscall_enter_tracehook (struct pt_regs *regs); + +#ifndef arch_syscall_enter_tracehook +static inline __must_check int arch_syscall_enter_tracehook (struct pt_regs *regs) +{ +return tracehook_report_syscall_entry (regs); +} +#endif + +/** +* arch_syscall_enter_seccomp - Architecture specific seccomp invocation +* @regs: Pointer to currents pt_regs +* +* Invoked from syscall_enter_from_usermode(). Can be replaced by +* architecture specific code. +*/ +static inline long arch_syscall_enter_seccomp (struct pt_regs *regs); + +#ifndef arch_syscall_enter_seccomp +static inline long arch_syscall_enter_seccomp (struct pt_regs *regs) +{ +return secure_computing (NULL ); +} +#endif + +/** +* arch_syscall_enter_audit - Architecture specific audit invocation +* @regs: Pointer to currents pt_regs +* +* Invoked from syscall_enter_from_usermode(). Must be replaced by +* architecture specific code if the architecture supports audit. +*/ +static inline void arch_syscall_enter_audit (struct pt_regs *regs); + +#ifndef arch_syscall_enter_audit +static inline void arch_syscall_enter_audit (struct pt_regs *regs) { } +#endif + +/* Common syscall enter function */ +long core_syscall_enter_from_usermode (struct pt_regs *regs,long syscall); + +/** +* syscall_enter_from_usermode - Check and handle work before invoking +* a syscall +* @regs: Pointer to currents pt_regs +* @syscall: The syscall number +* +* Invoked from architecture specific syscall entry code with interrupts +* enabled. +* +* Returns: The original or a modified syscall number +*/ +static inline long syscall_enter_from_usermode (struct pt_regs *regs, +long syscall) +{ +unsigned long ti_work =READ_ONCE (current_thread_info ()->flags); + +if (IS_ENABLED (CONFIG_DEBUG_ENTRY )) +BUG_ON (regs !=task_pt_regs (current)); + +if (ti_work &SYSCALL_ENTER_WORK ) + syscall =core_syscall_enter_from_usermode (regs, syscall); +return syscall; +} + +#endif kernel/Makefile +1
-0@@ -43,6 +43,7 @@ obj-y += irq/ obj-y += rcu/ obj-y += livepatch/ obj-y += dma/ +obj-y += entry/ obj-$(CONFIG_CHECKPOINT_RESTORE) += kcmp.o obj-$(CONFIG_FREEZER) += freezer.okernel/entry/Makefile new file +3
-0@@ -0,0 +1,3 @@ +# SPDX-License-Identifier: GPL-2.0 + +obj-$(CONFIG_GENERIC_ENTRY) += common.okernel/entry/common.c new file +33
-0@@ -0,0 +1,33 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/context_tracking.h> +#include <linux/entry-common.h> + +#define CREATE_TRACE_POINTS +#include <trace/events/syscalls.h> + +long core_syscall_enter_from_usermode (struct pt_regs *regs,long syscall) +{ +unsigned long ti_work =READ_ONCE (current_thread_info ()->flags); +unsigned long ret =0 ; + +if (ti_work & (_TIF_SYSCALL_TRACE | _TIF_SYSCALL_EMU)) { + ret =arch_syscall_enter_tracehook (regs); +if (ret || (ti_work & _TIF_SYSCALL_EMU)) +return -1L ; + } + +/* Do seccomp after ptrace, to catch any tracer changes. */ +if (ti_work & _TIF_SECCOMP) { + ret =arch_syscall_enter_seccomp (regs); +if (ret ==-1L ) +return ret; + } + +if (unlikely (test_thread_flag (TIF_SYSCALL_TRACEPOINT ))) +trace_sys_enter (regs, syscall); + +arch_syscall_enter_audit (regs); + +return ret ? : syscall; +}
Signature · 2 lines
Sincerely yours, Mike.
RFC PATCH 2/15 x86/entry: Remove _TIF_NOHZ from _TIF_WORK_SYSCALL_ENTRY
In reply to Thomas GleixnerEvaluating _TIF_NOHZ to decide whether to use the slow syscall entry path is not only pointless, it's actually counterproductive:
1) Context tracking code is invoked unconditionally before that flag is evaluated.
2) If the flag is set the slow path is invoked for nothing due to #1
Remove it.
--- arch/x86/include/asm/thread_info.h | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)
arch/x86/include/asm/thread_info.h +2 -6
@@ -133,14 +133,10 @@ struct thread_info {#define _TIF_X32 (1 << TIF_X32)#define _TIF_FSCHECK (1 << TIF_FSCHECK) -/* -* work to do in syscall_trace_enter(). Also includes TIF_NOHZ for -* enter_from_user_mode() -*/ +/* Work to do before invoking the actual syscall. */ #define _TIF_WORK_SYSCALL_ENTRY \ (_TIF_SYSCALL_TRACE | _TIF_SYSCALL_EMU | _TIF_SYSCALL_AUDIT | \ - _TIF_SECCOMP | _TIF_SYSCALL_TRACEPOINT | \ - _TIF_NOHZ) + _TIF_SECCOMP | _TIF_SYSCALL_TRACEPOINT)/* flags to check in __switch_to() */ #define _TIF_WORK_CTXSW_BASE \
Andy Lutomirski
maintainerRe: [RFC patch 02/15] x86/entry: Remove _TIF_NOHZ from _TIF_WORK_SYSCALL_ENTRY
Quoted 8 lines · Thomas Gleixner
Evaluating
_TIF_NOHZto decide whether to use the slow syscall entry path is not only pointless, it's actually counterproductive:1) Context tracking code is invoked unconditionally before that flag is evaluated.
2) If the flag is set the slow path is invoked for nothing due to #1
Can we also get rid of TIF_NOHZ on x86?
Re: [RFC patch 02/15] x86/entry: Remove _TIF_NOHZ from _TIF_WORK_SYSCALL_ENTRY
Quoted 11 lines · Andy Lutomirski
Quoted 8 lines · Thomas Gleixner
Evaluating
_TIF_NOHZto decide whether to use the slow syscall entry path is not only pointless, it's actually counterproductive:1) Context tracking code is invoked unconditionally before that flag is evaluated.
2) If the flag is set the slow path is invoked for nothing due to #1
Can we also get rid of
TIF_NOHZon x86?
If we make the usage in context_tracking_cpu_set() conditional.
Thanks,
tglx
RFC PATCH 3/15 x86/entry: Use generic syscall entry function
In reply to Thomas GleixnerReplace the syscall entry work handling with the generic version, Provide the necessary helper inlines to handle the real architecture specific parts, e.g. audit and seccomp invocations.
--- arch/x86/Kconfig | 1 arch/x86/entry/common.c | 108 +++--------------------------------- arch/x86/include/asm/entry-common.h | 59 +++++++++++++++++++ arch/x86/include/asm/thread_info.h | 5 - 4 files changed, 70 insertions(+), 103 deletions(-)
arch/x86/Kconfig +1 -0
@@ -110,6 +110,7 @@ config X86 select GENERIC_CPU_AUTOPROBE select GENERIC_CPU_VULNERABILITIES select GENERIC_EARLY_IOREMAP + select GENERIC_ENTRY select GENERIC_FIND_FIRST_BIT select GENERIC_IOMAP select GENERIC_IRQ_EFFECTIVE_AFF_MASK if SMP
arch/x86/entry/common.c +10 -98
151 lines not on this page · raw message
arch/x86/include/asm/entry-common.h new file +59 -0
59 lines not on this page · raw message
arch/x86/include/asm/thread_info.h +0 -5
@@ -133,11 +133,6 @@ struct thread_info {#define _TIF_X32 (1 << TIF_X32)#define _TIF_FSCHECK (1 << TIF_FSCHECK) -/* Work to do before invoking the actual syscall. */ -#define _TIF_WORK_SYSCALL_ENTRY \ - (_TIF_SYSCALL_TRACE | _TIF_SYSCALL_EMU | _TIF_SYSCALL_AUDIT | \ - _TIF_SECCOMP | _TIF_SYSCALL_TRACEPOINT) -/* flags to check in __switch_to() */ #define _TIF_WORK_CTXSW_BASE \ (_TIF_IO_BITMAP|_TIF_NOCPUID|_TIF_NOTSC|_TIF_BLOCKSTEP| \
Andy Lutomirski
maintainerRe: [RFC patch 03/15] x86/entry: Use generic syscall entry function
arch/x86/entry/common.c line 86
Quoted 10 lines · Thomas Gleixner
Replace the syscall entry work handling with the generic version, Provide the necessary helper inlines to handle the real architecture specific parts, e.g. audit and seccomp invocations.
arch/x86/entry/common.c +0
-5-if (work & (_TIF_SYSCALL_TRACE | _TIF_SYSCALL_EMU)) { - ret =tracehook_report_syscall_entry (regs); -if (ret || (work & _TIF_SYSCALL_EMU)) -return -1L ; - }
Unless I missed something, you lost the _TIF_SYSCALL_EMU abomination.
Peter Zijlstra
maintainerRe: [RFC patch 03/15] x86/entry: Use generic syscall entry function
Quoted 6 lines · Andy Lutomirski
Quoted 4 lines · Thomas Gleixner
Replace the syscall entry work handling with the generic version, Provide the necessary helper inlines to handle the real architecture specific parts, e.g. audit and seccomp invocations.
Quoted 7 lines
arch/x86/entry/common.c line 86
Quoted 5 lines
arch/x86/entry/common.c +0
-5-if (work & (_TIF_SYSCALL_TRACE | _TIF_SYSCALL_EMU)) { - ret =tracehook_report_syscall_entry (regs); -if (ret || (work & _TIF_SYSCALL_EMU)) -return -1L ; - }Unless I missed something, you lost the
_TIF_SYSCALL_EMUabomination.
IIUC that's actually in patch #1.
Re: [RFC patch 03/15] x86/entry: Use generic syscall entry function
Quoted 13 lines · Peter Zijlstra
Quoted 12 lines · Andy Lutomirski
Quoted 10 lines · Thomas Gleixner
Replace the syscall entry work handling with the generic version, Provide the necessary helper inlines to handle the real architecture specific parts, e.g. audit and seccomp invocations.
arch/x86/entry/common.c +0
-5-if (work & (_TIF_SYSCALL_TRACE | _TIF_SYSCALL_EMU)) { - ret =tracehook_report_syscall_entry (regs); -if (ret || (work & _TIF_SYSCALL_EMU)) -return -1L ; - }
Quoted 3 lines
Quoted 1 line
Unless I missed something, you lost the
_TIF_SYSCALL_EMUabomination.IIUC that's actually in patch #1.
Correct:
+ if (ti_work & (_TIF_SYSCALL_TRACE | _TIF_SYSCALL_EMU)) {
+ ret = arch_syscall_enter_tracehook(regs);
+ if (ret || (ti_work & _TIF_SYSCALL_EMU))
+ return -1L;
+ }
RFC PATCH 4/15 arm64/entry: Use generic syscall entry function
In reply to Thomas GleixnerReplace the syscall entry work handling with the generic version, Provide the necessary helper inlines to handle the real architecture specific parts, e.g. audit and seccomp invocations.
--- arch/arm64/Kconfig | 1 arch/arm64/include/asm/entry-common.h | 39 ++++++++++++++++++++++++++++++++++ arch/arm64/kernel/ptrace.c | 29 ------------------------- arch/arm64/kernel/syscall.c | 15 +++++-------- 4 files changed, 47 insertions(+), 37 deletions(-)
arch/arm64/Kconfig +1 -0
@@ -99,6 +99,7 @@ config ARM64 select GENERIC_CPU_AUTOPROBE select GENERIC_CPU_VULNERABILITIES select GENERIC_EARLY_IOREMAP + select GENERIC_ENTRY select GENERIC_IDLE_POLL_SETUP select GENERIC_IRQ_MULTI_HANDLER select GENERIC_IRQ_PROBE
arch/arm64/include/asm/entry-common.h new file +39 -0
@@ -0,0 +1,39 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Copyright (C) 2017 ARM Ltd. +*/ +#ifndef __ASM_ENTRY_COMMON_H +#define __ASM_ENTRY_COMMON_H + +enum ptrace_syscall_dir { +PTRACE_SYSCALL_ENTER =0 , +PTRACE_SYSCALL_EXIT , +}; + +/* +* A scratch register (ip(r12) on AArch32, x7 on AArch64) is +* used to denote syscall entry/exit for the tracehooks +*/ +static inline __must_check int arch_syscall_enter_tracehook (struct pt_regs *regs) +{ +int regno = (is_compat_task () ?12 :7 ); +unsigned long reg = regs->regs[regno]; +long ret; + + regs->regs[regno] =PTRACE_SYSCALL_ENTER ; + ret =tracehook_report_syscall_entry (regs); +if (ret) +forget_syscall (regs); + regs->regs[regno] = reg; +return ret; +} +#define arch_syscall_enter_tracehook arch_syscall_enter_tracehook + +static inline void arch_syscall_enter_audit (struct pt_regs *regs) +{ +audit_syscall_entry (regs->syscallno, regs->orig_x0, regs->regs[1 ], + regs->regs[2 ], regs->regs[3 ]); +} +#define arch_syscall_enter_audit arch_syscall_enter_audit + +#endif
arch/arm64/kernel/ptrace.c +1 -28
@@ -13,6 +13,7 @@#include <linux/kernel.h> #include <linux/sched/signal.h> #include <linux/sched/task_stack.h> +#include <linux/entry-common.h> #include <linux/mm.h> #include <linux/nospec.h> #include <linux/smp.h>
@@ -41,7 +42,6 @@#include <asm/traps.h> #include <asm/system_misc.h> -#define CREATE_TRACE_POINTS #include <trace/events/syscalls.h> struct pt_regs_offset {
@@ -1779,11 +1779,6 @@ long arch_ptrace(struct task_struct *chireturn ptrace_request (child, request, addr, data); } -enum ptrace_syscall_dir { -PTRACE_SYSCALL_ENTER =0 , -PTRACE_SYSCALL_EXIT , -}; -static void tracehook_report_syscall (struct pt_regs *regs,enum ptrace_syscall_dir dir) {
@@ -1806,28 +1801,6 @@ static void tracehook_report_syscall(str regs->regs[regno] = saved_reg; } -int syscall_trace_enter (struct pt_regs *regs) -{ -if (test_thread_flag (TIF_SYSCALL_TRACE ) || -test_thread_flag (TIF_SYSCALL_EMU )) { -tracehook_report_syscall (regs,PTRACE_SYSCALL_ENTER ); -if (!in_syscall (regs) ||test_thread_flag (TIF_SYSCALL_EMU )) -return -1 ; - } - -/* Do the secure computing after ptrace; failures should be fast. */ -if (secure_computing (NULL ) ==-1 ) -return -1 ; - -if (test_thread_flag (TIF_SYSCALL_TRACEPOINT )) -trace_sys_enter (regs, regs->syscallno); - -audit_syscall_entry (regs->syscallno, regs->orig_x0, regs->regs[1 ], - regs->regs[2 ], regs->regs[3 ]); - -return regs->syscallno; -} -void syscall_trace_exit (struct pt_regs *regs) {audit_syscall_exit (regs);
arch/arm64/kernel/syscall.c +6 -9
@@ -1,6 +1,7 @@// SPDX-License-Identifier: GPL-2.0 #include <linux/compiler.h> +#include <linux/entry-common.h> #include <linux/context_tracking.h> #include <linux/errno.h> #include <linux/nospec.h>
@@ -58,7 +59,6 @@ static inline bool has_syscall_work(unsireturn unlikely (flags & _TIF_SYSCALL_WORK); } -int syscall_trace_enter (struct pt_regs *regs);void syscall_trace_exit (struct pt_regs *regs);#ifdef CONFIG_ARM64_ERRATUM_1463225
@@ -97,19 +97,16 @@ static void el0_svc_common(struct pt_reg regs->orig_x0 = regs->regs[0 ]; regs->syscallno = scno; +/* Set default error number */ + regs->regs[0 ] = -ENOSYS ;cortex_a76_erratum_1463225_svc_handler ();local_daif_restore (DAIF_PROCCTX );user_exit (); -if (has_syscall_work (flags)) { -/* set default errno for user-issued syscall(-1) */ -if (scno ==NO_SYSCALL ) - regs->regs[0 ] = -ENOSYS ; - scno =syscall_trace_enter (regs); -if (scno ==NO_SYSCALL ) - goto trace_exit; - } + scno =syscall_enter_from_usermode (regs, scno); +if (scno ==NO_SYSCALL ) + goto trace_exit;invoke_syscall (regs, scno, sc_nr, syscall_table);
Catalin Marinas
maintainerRe: [RFC patch 04/15] arm64/entry: Use generic syscall entry function
arch/arm64/kernel/syscall.c static void el0_svc_common(struct pt_reg · line 101
Quoted 7 lines · Thomas Gleixner
#ifdef
CONFIG_ARM64_ERRATUM_1463225arch/arm64/kernel/syscall.c +2
-0@@ -97,19 +97,16 @@ static void el0_svc_common(struct pt_reg regs->orig_x0 = regs->regs[0 ]; regs->syscallno = scno; +/* Set default error number */ + regs->regs[0 ] = -ENOSYS ;
I think this corrupts the first argument of all valid syscalls. SC_ARM64_REGS_TO_ARGS uses regs[0] instead of orig_x0. ptrace should be fine since it calls syscall_get_arguments() which uses orig_x0.
We could change the SC_ARM64_REGS_TO_ARGS macro though (in theory there shouldn't be any performance hit as it's already cached).
Quoted 18 lines
arch/arm64/kernel/syscall.c +3
-8cortex_a76_erratum_1463225_svc_handler ();local_daif_restore (DAIF_PROCCTX );user_exit (); -if (has_syscall_work (flags)) { -/* set default errno for user-issued syscall(-1) */ -if (scno ==NO_SYSCALL ) - regs->regs[0 ] = -ENOSYS ; - scno =syscall_trace_enter (regs); -if (scno ==NO_SYSCALL ) - goto trace_exit; - } + scno =syscall_enter_from_usermode (regs, scno); +if (scno ==NO_SYSCALL ) + goto trace_exit;invoke_syscall (regs, scno, sc_nr, syscall_table);
Signature · 1 line
Catalin
RFC PATCH 5/15 entry: Provide generic syscall exit function
In reply to Thomas GleixnerSimilar to syscall entry all architectures have similar and pointlessly different code to handle pending work before returning from a syscall to user space.
Provide a generic version.
--- include/linux/entry-common.h | 31 ++++++++++++++++++++++++ kernel/entry/common.c | 55 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+)
include/linux/entry-common.h +31 -0
@@ -45,6 +45,17 @@ _TIF_SYSCALL_TRACEPOINT | _TIF_SYSCALL_EMU | \ ARCH_SYSCALL_ENTER_WORK) +/* +* TIF flags handled in syscall_exit_to_usermode() +*/ +#ifndef ARCH_SYSCALL_EXIT_WORK +# define ARCH_SYSCALL_EXIT_WORK (0) +#endif + +#define SYSCALL_EXIT_WORK \ + (_TIF_SYSCALL_TRACE | _TIF_SYSCALL_AUDIT | \ + _TIF_SYSCALL_TRACEPOINT | ARCH_SYSCALL_EXIT_WORK) +/** * arch_syscall_enter_tracehook - Wrapper around tracehook_report_syscall_entry() *
@@ -118,4 +129,24 @@ static inline long syscall_enter_from_us return syscall; } +/** +* arch_syscall_exit_tracehook - Wrapper around tracehook_report_syscall_exit() +* +* Defaults to tracehook_report_syscall_exit(). Can be replaced by +* architecture specific code. +* +* Invoked from syscall_exit_to_usermode() +*/ +static inline void arch_syscall_exit_tracehook (struct pt_regs *regs,bool step); + +#ifndef arch_syscall_exit_tracehook +static inline void arch_syscall_exit_tracehook (struct pt_regs *regs,bool step) +{ +tracehook_report_syscall_exit (regs, step); +} +#endif + +/* Common syscall exit function */ +void syscall_exit_to_usermode (struct pt_regs *regs,long syscall,long retval); +#endif
kernel/entry/common.c +55 -0
58 lines not on this page · raw message
RFC PATCH 6/15 x86/entry: Use generic syscall exit functionality
In reply to Thomas GleixnerReplace the x86 variant with the generic version.
--- arch/x86/entry/common.c | 44 ------------------------------------ arch/x86/include/asm/entry-common.h | 2 + 2 files changed, 3 insertions(+), 43 deletions(-)
arch/x86/entry/common.c +1 -43
@@ -139,55 +139,13 @@ static void exit_to_usermode_loop(structmds_user_clear_cpu_buffers (); } -#define SYSCALL_EXIT_WORK_FLAGS \ - (_TIF_SYSCALL_TRACE | _TIF_SYSCALL_AUDIT | \ - _TIF_SINGLESTEP | _TIF_SYSCALL_TRACEPOINT) - -static void syscall_slow_exit_work (struct pt_regs *regs,u32 cached_flags) -{ -bool step; - -audit_syscall_exit (regs); - -if (cached_flags & _TIF_SYSCALL_TRACEPOINT) -trace_sys_exit (regs, regs->ax); - -/* -* If TIF_SYSCALL_EMU is set, we only get here because of -* TIF_SINGLESTEP (i.e. this is PTRACE_SYSEMU_SINGLESTEP). -* We already reported this syscall instruction in -* syscall_trace_enter(). -*/ - step =unlikely ( - (cached_flags & (_TIF_SINGLESTEP | _TIF_SYSCALL_EMU)) - == _TIF_SINGLESTEP); -if (step || cached_flags & _TIF_SYSCALL_TRACE) -tracehook_report_syscall_exit (regs, step); -} -/* * Called with IRQs on and fully valid regs. Returns with IRQs off in a * state such that we can immediately switch to user mode. */ __visible inline void syscall_return_slowpath(struct pt_regs *regs) { -struct thread_info *ti =current_thread_info (); -u32 cached_flags =READ_ONCE (ti->flags); - -CT_WARN_ON (ct_state () !=CONTEXT_KERNEL ); - -if (IS_ENABLED (CONFIG_PROVE_LOCKING ) && -WARN (irqs_disabled (),"syscall %ld left IRQs disabled" , regs->orig_ax)) -local_irq_enable (); - -rseq_syscall (regs); - -/* -* First do one-time work. If these work items are enabled, we -* want to run them exactly once per syscall exit with IRQs on. -*/ -if (unlikely (cached_flags &SYSCALL_EXIT_WORK_FLAGS )) -syscall_slow_exit_work (regs, cached_flags); +syscall_exit_to_usermode (regs, regs->orig_ax, regs->ax);local_irq_disable ();prepare_exit_to_usermode (regs);
arch/x86/include/asm/entry-common.h +2 -0
@@ -5,6 +5,8 @@#include <linux/seccomp.h> #include <linux/audit.h> +#define ARCH_SYSCALL_EXIT_WORK (_TIF_SINGLESTEP) +static inline long arch_syscall_enter_seccomp (struct pt_regs *regs) {#ifdef CONFIG_SECCOMP
RFC PATCH 7/15 arm64/syscall: Remove obscure flag check
In reply to Thomas GleixnerThe syscall handling code has an obscure check of pending work which does a shortcut before returning to user space. It calls into the exit work code when the flags at entry time required an entry into the slowpath. That does not make sense because the underlying work functionality will reevaluate the flags anyway and not do anything.
Replace that by a straight forward test for work flags. Preparatory change for switching to the generic syscall exit work handling code.
--- arch/arm64/kernel/syscall.c | 32 ++++++-------------------------- 1 file changed, 6 insertions(+), 26 deletions(-)
arch/arm64/kernel/syscall.c +6 -26
@@ -93,8 +93,6 @@ static void cortex_a76_erratum_1463225_sstatic void el0_svc_common (struct pt_regs *regs,int scno,int sc_nr,const syscall_fn_t syscall_table[]) { -unsigned long flags =current_thread_info ()->flags; - regs->orig_x0 = regs->regs[0 ]; regs->syscallno = scno;/* Set default error number */
@@ -105,33 +103,15 @@ static void el0_svc_common(struct pt_reguser_exit (); scno =syscall_enter_from_usermode (regs, scno); -if (scno ==NO_SYSCALL ) - goto trace_exit; - -invoke_syscall (regs, scno, sc_nr, syscall_table); +if (scno !=NO_SYSCALL ) +invoke_syscall (regs, scno, sc_nr, syscall_table); -/* -* The tracing status may have changed under our feet, so we have to -* check again. However, if we were tracing entry, then we always trace -* exit regardless, as the old entry assembly did. -*/ -if (!has_syscall_work (flags) && !IS_ENABLED (CONFIG_DEBUG_RSEQ )) { -local_daif_mask (); - flags =current_thread_info ()->flags; -if (!has_syscall_work (flags)) { -/* -* We're off to userspace, where interrupts are -* always enabled after we restore the flags from -* the SPSR. -*/ -trace_hardirqs_on (); -return ; - } +local_daif_mask (); +if (has_syscall_work (current_thread_info ()->flags) || +IS_ENABLED (CONFIG_DEBUG_RSEQ )) {local_daif_restore (DAIF_PROCCTX ); +syscall_trace_exit (regs); } - -trace_exit: -syscall_trace_exit (regs); }static inline void sve_user_discard (void )
Catalin Marinas
maintainerRe: [RFC patch 07/15] arm64/syscall: Remove obscure flag check
Quoted 5 lines · Thomas Gleixner
The syscall handling code has an obscure check of pending work which does a shortcut before returning to user space. It calls into the exit work code when the flags at entry time required an entry into the slowpath. That does not make sense because the underlying work functionality will reevaluate the flags anyway and not do anything.
The current C code was just matching the original behaviour in asm (converted by commit f37099b6992a0b81). The idea IIRC was to always pair a syscall_trace_enter() with a syscall_trace_exit() irrespective of the thread flag changes. I think the behaviour is preserved with your patch if no-one clears the work flags during el0_svc_common().
Quoted 24 lines
arch/arm64/kernel/syscall.c +2
-17@@ -105,33 +103,15 @@ static void el0_svc_common(struct pt_reguser_exit (); scno =syscall_enter_from_usermode (regs, scno); -if (scno ==NO_SYSCALL ) - goto trace_exit; - -invoke_syscall (regs, scno, sc_nr, syscall_table); +if (scno !=NO_SYSCALL ) +invoke_syscall (regs, scno, sc_nr, syscall_table); -/* -* The tracing status may have changed under our feet, so we have to -* check again. However, if we were tracing entry, then we always trace -* exit regardless, as the old entry assembly did. -*/ -if (!has_syscall_work (flags) && !IS_ENABLED (CONFIG_DEBUG_RSEQ )) { -local_daif_mask (); - flags =current_thread_info ()->flags; -if (!has_syscall_work (flags)) { -/* -* We're off to userspace, where interrupts are -* always enabled after we restore the flags from -* the SPSR.
arch/arm64/kernel/syscall.c static void el0_svc_common(struct pt_reg · line 114
Quoted 10 lines
arch/arm64/kernel/syscall.c +4
-4-*/ -trace_hardirqs_on (); -return ; - } +local_daif_mask (); +if (has_syscall_work (current_thread_info ()->flags) || +IS_ENABLED (CONFIG_DEBUG_RSEQ )) {local_daif_restore (DAIF_PROCCTX ); +syscall_trace_exit (regs); }
That's missing a trace_hardirqs_on() (off done in local_daif_mask()) before returning.
Quoted 4 lines
arch/arm64/kernel/syscall.c +0
-3- -trace_exit: -syscall_trace_exit (regs); }
Signature · 1 line
Catalin
RFC PATCH 8/15 arm64/syscall: Use generic syscall exit functionality
In reply to Thomas GleixnerReplace the syscall exit handling code with the generic version. That gets rid of the interrupts disabled check for work flags. That's a non-issue because the flags are checked with READ_ONCE() in the core and not reevaluated. That's perfectly fine because the interrupts disabled check is also just a snapshot.
--- arch/arm64/include/asm/entry-common.h | 11 +++++++++ arch/arm64/kernel/ptrace.c | 38 ---------------------------------- arch/arm64/kernel/syscall.c | 14 ------------ 3 files changed, 12 insertions(+), 51 deletions(-)
arch/arm64/include/asm/entry-common.h +11 -0
@@ -29,6 +29,17 @@ static inline __must_check int arch_sysc }#define arch_syscall_enter_tracehook arch_syscall_enter_tracehook +static inline void arch_syscall_exit_tracehook (struct pt_regs *regs,bool step) +{ +int regno = (is_compat_task () ?12 :7 ); +unsigned long reg = regs->regs[regno]; + + regs->regs[regno] =PTRACE_SYSCALL_EXIT ; +tracehook_report_syscall_exit (regs, step); + regs->regs[regno] = reg; +} +#define arch_syscall_exit_tracehook arch_syscall_exit_tracehook +static inline void arch_syscall_enter_audit (struct pt_regs *regs) { audit_syscall_entry(regs->syscallno, regs->orig_x0, regs->regs[1],
arch/arm64/kernel/ptrace.c +0 -38
@@ -13,13 +13,11 @@#include <linux/kernel.h> #include <linux/sched/signal.h> #include <linux/sched/task_stack.h> -#include <linux/entry-common.h> #include <linux/mm.h> #include <linux/nospec.h> #include <linux/smp.h> #include <linux/ptrace.h> #include <linux/user.h> -#include <linux/seccomp.h> #include <linux/security.h> #include <linux/init.h> #include <linux/signal.h>
@@ -28,7 +26,6 @@#include <linux/perf_event.h> #include <linux/hw_breakpoint.h> #include <linux/regset.h> -#include <linux/tracehook.h> #include <linux/elf.h> #include <asm/compat.h>
@@ -1779,41 +1776,6 @@ long arch_ptrace(struct task_struct *chireturn ptrace_request (child, request, addr, data); } -static void tracehook_report_syscall (struct pt_regs *regs, -enum ptrace_syscall_dir dir) -{ -int regno; -unsigned long saved_reg; - -/* -* A scratch register (ip(r12) on AArch32, x7 on AArch64) is -* used to denote syscall entry/exit: -*/ - regno = (is_compat_task () ?12 :7 ); - saved_reg = regs->regs[regno]; - regs->regs[regno] = dir; - -if (dir ==PTRACE_SYSCALL_EXIT ) -tracehook_report_syscall_exit (regs,0 ); -else if (tracehook_report_syscall_entry (regs)) -forget_syscall (regs); - - regs->regs[regno] = saved_reg; -} - -void syscall_trace_exit (struct pt_regs *regs) -{ -audit_syscall_exit (regs); - -if (test_thread_flag (TIF_SYSCALL_TRACEPOINT )) -trace_sys_exit (regs,regs_return_value (regs)); - -if (test_thread_flag (TIF_SYSCALL_TRACE )) -tracehook_report_syscall (regs,PTRACE_SYSCALL_EXIT ); - -rseq_syscall (regs); -} -/* * SPSR_ELx bits which are always architecturally RES0 per ARM DDI 0487D.a. * We permit userspace to set SSBS (AArch64 bit 12, AArch32 bit 23) which is
arch/arm64/kernel/syscall.c +1 -13
@@ -54,13 +54,6 @@ static void invoke_syscall(struct pt_reg regs->regs[0 ] = ret; } -static inline bool has_syscall_work (unsigned long flags) -{ -return unlikely (flags & _TIF_SYSCALL_WORK); -} - -void syscall_trace_exit (struct pt_regs *regs); -#ifdef CONFIG_ARM64_ERRATUM_1463225 DECLARE_PER_CPU (int, __in_cortex_a76_erratum_1463225_wa);
@@ -106,12 +99,7 @@ static void el0_svc_common(struct pt_regif (scno !=NO_SYSCALL )invoke_syscall (regs, scno, sc_nr, syscall_table); -local_daif_mask (); -if (has_syscall_work (current_thread_info ()->flags) || -IS_ENABLED (CONFIG_DEBUG_RSEQ )) { -local_daif_restore (DAIF_PROCCTX ); -syscall_trace_exit (regs); - } +syscall_exit_to_usermode (regs, scno,regs_return_value (regs)); }static inline void sve_user_discard (void )
RFC PATCH 9/15 entry: Provide generic exit to usermode functionality
In reply to Thomas GleixnerProvide a generic facility to handle the exit to usermode work. That's aimed to replace the pointlessly different copies in each architecture.
--- include/linux/entry-common.h | 105 +++++++++++++++++++++++++++++++++++++++++++ kernel/entry/common.c | 88 ++++++++++++++++++++++++++++++++++++ 2 files changed, 193 insertions(+)
include/linux/entry-common.h +105 -0
@@ -34,6 +34,30 @@# define _TIF_AUDIT (0)#endif +#ifndef _TIF_UPROBE +# define _TIF_UPROBE (0) +#endif + +#ifndef _TIF_PATCH_PENDING +# define _TIF_PATCH_PENDING (0) +#endif + +#ifndef _TIF_NOTIFY_RESUME +# define _TIF_NOTIFY_RESUME (0) +#endif + +/* +* TIF flags handled in exit_to_usermode() +*/ +#ifndef ARCH_EXIT_TO_USERMODE_WORK +# define ARCH_EXIT_TO_USERMODE_WORK (0) +#endif + +#define EXIT_TO_USERMODE_WORK \ + (_TIF_SIGPENDING | _TIF_NOTIFY_RESUME | _TIF_UPROBE | \ + _TIF_NEED_RESCHED | _TIF_PATCH_PENDING | \ + ARCH_EXIT_TO_USERMODE_WORK) +/* * TIF flags handled in syscall_enter_from_usermode() */
@@ -58,6 +82,87 @@ _TIF_SYSCALL_TRACEPOINT | ARCH_SYSCALL_EXIT_WORK)/** +* local_irq_enable_exit_to_user - Exit to user variant of local_irq_enable() +* @ti_work: Cached TIF flags gathered with interrupts disabled +* +* Defaults to local_irq_enable(). Can be supplied by architecture specific +* code. +*/ +static inline void local_irq_enable_exit_to_user (unsigned long ti_work); + +#ifndef local_irq_enable_exit_to_user +static inline void local_irq_enable_exit_to_user (unsigned long ti_work) +{ +local_irq_enable (); +} +#endif + +/** +* local_irq_disable_exit_to_user - Exit to user variant of local_irq_disable() +* +* Defaults to local_irq_disable(). Can be supplied by architecture specific +* code. +*/ +static inline void local_irq_disable_exit_to_user (void ); + +#ifndef local_irq_disable_exit_to_user +static inline void local_irq_disable_exit_to_user (void ) +{ +local_irq_disable (); +} +#endif + +/** +* arch_exit_to_usermode_work - Architecture specific TIF work for +* exit to user mode. +* @regs: Pointer to currents pt_regs +* @ti_work: Cached TIF flags gathered with interrupts disabled +* +* Invoked from exit_to_usermode() with interrupt disabled +* +* Defaults to NOOP. Can be supplied by architecture specific code. +*/ +static inline void arch_exit_to_usermode_work (struct pt_regs *regs, +unsigned long ti_work); + +#ifndef arch_exit_to_usermode_work +static inline void arch_exit_to_usermode_work (struct pt_regs *regs, +unsigned long ti_work) +{ +} +#endif + +/** +* arch_exit_to_usermode - Architecture specific preparation for +* exit to user mode. +* @regs: Pointer to currents pt_regs +* @ti_work: Cached TIF flags gathered with interrupts disabled +* +* Invoked from exit_to_usermode() with interrupt disabled as the last +* function before return. +*/ +static inline void arch_exit_to_usermode (struct pt_regs *regs, +unsigned long ti_work); + +#ifndef arch_exit_to_usermode +static inline void arch_exit_to_usermode (struct pt_regs *regs, +unsigned long ti_work) +{ +} +#endif + +/* Common exit to usermode function to handle TIF work */ +asmlinkage __visible void exit_to_usermode(struct pt_regs *regs); + +/** +* arch_do_signal - Architecture specific signal delivery function +* @regs: Pointer to currents pt_regs +* +* Invoked from exit_to_usermode() +*/ +void arch_do_signal (struct pt_regs *regs); + +/** * arch_syscall_enter_tracehook - Wrapper around tracehook_report_syscall_entry() * * Defaults to tracehook_report_syscall_entry(). Can be replaced by
kernel/entry/common.c +88 -0
102 lines not on this page · raw message
Peter Zijlstra
maintainerRe: [RFC patch 09/15] entry: Provide generic exit to usermode functionality
Quoted 48 lines · Thomas Gleixner
kernel/entry/common.c +48
-0+static unsigned long core_exit_to_usermode_work (struct pt_regs *regs, +unsigned long ti_work) +{ +/* +* Before returning to user space ensure that all pending work +* items have been completed. +*/ +while (ti_work &EXIT_TO_USERMODE_WORK ) { + +local_irq_enable_exit_to_user (ti_work); + +if (ti_work & _TIF_NEED_RESCHED) +schedule (); + +if (ti_work & _TIF_UPROBE) +uprobe_notify_resume (regs); + +if (ti_work & _TIF_PATCH_PENDING) +klp_update_patch_state (current); + +if (ti_work & _TIF_SIGPENDING) +arch_do_signal (regs); + +if (ti_work & _TIF_NOTIFY_RESUME) { +clear_thread_flag (TIF_NOTIFY_RESUME ); +tracehook_notify_resume (regs); +rseq_handle_notify_resume (NULL , regs); + } + +/* Architecture specific TIF work */ +arch_exit_to_usermode_work (regs, ti_work); + +/* +* Disable interrupts and reevaluate the work flags as they +* might have changed while interrupts and preemption was +* enabled above. +*/ +local_irq_disable_exit_to_user (); + ti_work =READ_ONCE (current_thread_info ()->flags); + } +/* +* Was checked in exit_to_usermode_work() already, but the above +* loop might have wreckaged it. +*/ +addr_limit_user_check (); +return ti_work; +} +
kernel/entry/common.c line 68
Quoted 10 lines
kernel/entry/common.c +10
-0+static void do_exit_to_usermode (struct pt_regs *regs) +{ +unsigned long ti_work =READ_ONCE (current_thread_info ()->flags); + +lockdep_sys_exit (); + +addr_limit_user_check (); + +if (unlikely (ti_work &EXIT_TO_USERMODE_WORK )) + ti_work =core_exit_to_usermode_work (regs, ti_work);
would it make sense to do:
lockdep_sys_exit(); addr_limit_user_check();
here instead of before core_exit_to_usermode_work(); that would also allow getting rid of that second addr_limit_user_check() invocation.
And movind that lockdep check later would catch any of the EXIT_TO_USERMODE_WORK users leaking a lock.
Quoted 5 lines
kernel/entry/common.c +5
-0+ +arch_exit_to_usermode (regs, ti_work); +/* Return to userspace right after this which turns on interrupts */ +trace_hardirqs_on (); +}
RFC PATCH 10/15 x86/entry: Move irq tracing to C code
In reply to Thomas GleixnerTo prepare for converting the exit to usermode code to the generic version, move the irqflags tracing into C code.
--- arch/x86/entry/common.c | 10 ++++++++++ arch/x86/entry/entry_32.S | 11 +---------- arch/x86/entry/entry_64.S | 10 ++-------- arch/x86/entry/entry_64_compat.S | 21 --------------------- 4 files changed, 13 insertions(+), 39 deletions(-)
arch/x86/entry/common.c +10 -0
@@ -102,6 +102,8 @@ static void exit_to_usermode_loop(structstruct thread_info *ti =current_thread_info ();u32 cached_flags; +trace_hardirqs_off (); +addr_limit_user_check ();lockdep_assert_irqs_disabled ();
@@ -137,6 +139,8 @@ static void exit_to_usermode_loop(structuser_enter_irqoff ();mds_user_clear_cpu_buffers (); + +trace_hardirqs_on (); }/*
@@ -154,6 +158,8 @@ static void exit_to_usermode_loop(struct#ifdef CONFIG_X86_64 __visible void do_syscall_64(unsigned long nr, struct pt_regs *regs) { +/* User to kernel transition disabled interrupts. */ +trace_hardirqs_off ();enter_from_user_mode ();local_irq_enable ();
@@ -221,6 +227,7 @@ static __always_inline void do_syscall_3/* Handles int $0x80 */ __visible void do_int80_syscall_32(struct pt_regs *regs) { +trace_hardirqs_off ();enter_from_user_mode ();local_irq_enable ();do_syscall_32_irqs_on (regs);
@@ -237,6 +244,9 @@ static __always_inline void do_syscall_3unsigned long landing_pad = (unsigned long )current->mm->context.vdso + vdso_image_32.sym_int80_landing_pad; +/* User to kernel transition disabled interrupts. */ +trace_hardirqs_off (); +/* * SYSENTER loses EIP, and even SYSCALL32 needs us to skip forward * so that 'regs->ip -= 2' lands back on an int $0x80 instruction.
arch/x86/entry/entry_32.S +1 -10
@@ -827,7 +827,6 @@ END(ret_from_fork) ENTRY(resume_userspace) DISABLE_INTERRUPTS(CLBR_ANY) - TRACE_IRQS_OFF movl %esp, %eax call prepare_exit_to_usermode jmp restore_all
@@ -1049,12 +1048,6 @@ ENTRY(entry_INT80_32) SAVE_ALL pt_regs_ax=$-ENOSYS switch_stacks=1 /* save rest */ - /* - * User mode is traced as though IRQs are on, and the interrupt gate - * turned them off. - */ - TRACE_IRQS_OFF - movl %esp, %eax call do_int80_syscall_32 .Lsyscall_32_done:
@@ -1062,11 +1055,8 @@ ENTRY(entry_INT80_32) STACKLEAK_ERASE restore_all: - TRACE_IRQS_IRET SWITCH_TO_ENTRY_STACK -.Lrestore_all_notrace: CHECK_AND_APPLY_ESPFIX -.Lrestore_nocheck: /* Switch back to user CR3 */ SWITCH_TO_USER_CR3 scratch_reg=%eax
@@ -1086,6 +1076,7 @@ ENTRY(entry_INT80_32) restore_all_kernel: #ifdef CONFIG_PREEMPTION DISABLE_INTERRUPTS(CLBR_ANY) + TRACE_IRQS_OFF cmpl $0, PER_CPU_VAR(__preempt_count) jnz .Lno_preempt testl $X86_EFLAGS_IF, PT_EFLAGS(%esp) # interrupts off (exception path) ?
arch/x86/entry/entry_64.S +2 -8
@@ -167,15 +167,11 @@ GLOBAL(entry_SYSCALL_64_after_hwframe) PUSH_AND_CLEAR_REGS rax=$-ENOSYS - TRACE_IRQS_OFF - /* IRQs are off. */ movq %rax, %rdi movq %rsp, %rsi call do_syscall_64 /* returns with IRQs disabled */ - TRACE_IRQS_IRETQ /* we're about to change IF */ - /* * Try to use SYSRET instead of IRET if we're returning to * a completely clean 64-bit userspace context. If we're not,
@@ -342,7 +338,6 @@ ENTRY(ret_from_fork) UNWIND_HINT_REGS movq %rsp, %rdi call syscall_return_slowpath /* returns with IRQs disabled */ - TRACE_IRQS_ON /* user mode is traced as IRQS on */ jmp swapgs_restore_regs_and_return_to_usermode 1:
@@ -608,7 +603,6 @@ END(common_spurious) /* 0(%rsp): old RSP */ ret_from_intr: DISABLE_INTERRUPTS(CLBR_ANY) - TRACE_IRQS_OFF LEAVE_IRQ_STACK
@@ -619,7 +613,6 @@ END(common_spurious) GLOBAL(retint_user) mov %rsp,%rdi call prepare_exit_to_usermode - TRACE_IRQS_IRETQ GLOBAL(swapgs_restore_regs_and_return_to_usermode) #ifdef CONFIG_DEBUG_ENTRY
@@ -666,6 +659,7 @@ GLOBAL(swapgs_restore_regs_and_return_to retint_kernel: #ifdef CONFIG_PREEMPTION /* Interrupts are off */ + TRACE_IRQS_OFF /* Check if we need preemption */ btl $9, EFLAGS(%rsp) /* were interrupts off? */ jnc 1f
@@ -1367,9 +1361,9 @@ ENTRY(error_entry) END(error_entry) ENTRY(error_exit) - UNWIND_HINT_REGS DISABLE_INTERRUPTS(CLBR_ANY) TRACE_IRQS_OFF + UNWIND_HINT_REGS testb $3, CS(%rsp) jz retint_kernel jmp retint_user
arch/x86/entry/entry_64_compat.S +0 -21
@@ -129,12 +129,6 @@ ENTRY(entry_SYSENTER_compat) jnz .Lsysenter_fix_flags .Lsysenter_flags_fixed: - /* - * User mode is traced as though IRQs are on, and SYSENTER - * turned them off. - */ - TRACE_IRQS_OFF - movq %rsp, %rdi call do_fast_syscall_32 /* XEN PV guests always use IRET path */
@@ -247,12 +241,6 @@ GLOBAL(entry_SYSCALL_compat_after_hwfram pushq $0 /* pt_regs->r15 = 0 */ xorl %r15d, %r15d /* nospec r15 */ - /* - * User mode is traced as though IRQs are on, and SYSENTER - * turned them off. - */ - TRACE_IRQS_OFF - movq %rsp, %rdi call do_fast_syscall_32 /* XEN PV guests always use IRET path */
@@ -266,7 +254,6 @@ GLOBAL(entry_SYSCALL_compat_after_hwfram * stack. So let's erase the thread stack right now. */ STACKLEAK_ERASE - TRACE_IRQS_ON /* User mode traces as IRQs on. */ movq RBX(%rsp), %rbx /* pt_regs->rbx */ movq RBP(%rsp), %rbp /* pt_regs->rbp */ movq EFLAGS(%rsp), %r11 /* pt_regs->flags (in r11) */
@@ -403,17 +390,9 @@ ENTRY(entry_INT80_compat) xorl %r15d, %r15d /* nospec r15 */ cld - /* - * User mode is traced as though IRQs are on, and the interrupt - * gate turned them off. - */ - TRACE_IRQS_OFF - movq %rsp, %rdi call do_int80_syscall_32 .Lsyscall_32_done: - /* Go back to user mode. */ - TRACE_IRQS_ON jmp swapgs_restore_regs_and_return_to_usermode END(entry_INT80_compat)
Peter Zijlstra
maintainerRe: [RFC patch 10/15] x86/entry: Move irq tracing to C code
Quoted 14 lines · Thomas Gleixner
To prepare for converting the exit to usermode code to the generic version, move the irqflags tracing into C code.
Signed-off-by Thomas Gleixner <[email protected]>--- arch/x86/entry/common.c | 10 ++++++++++ arch/x86/entry/entry_32.S | 11 +---------- arch/x86/entry/entry_64.S | 10 ++-------- arch/x86/entry/entry_64_compat.S | 21 --------------------- 4 files changed, 13 insertions(+), 39 deletions(-)
arch/x86/entry/common.c static void exit_to_usermode_loop(struct · line 105
Quoted 4 lines
arch/x86/entry/common.c +1
-0@@ -102,6 +102,8 @@ static void exit_to_usermode_loop(structstruct thread_info *ti =current_thread_info ();u32 cached_flags; +trace_hardirqs_off ();
Bah.. so this gets called from:
- C code, with IRQs disabled
- entry_64.S:error_exit
- entry_32.S:resume_userspace
The first obviously doesn't need this annotation, but this patch doesn't remove the TRACE_IRQS_OFF from entry_64.S and only the 32bit case is changed.
Is that entry_64.S case an oversight, or do we need an extensive comment on this one?
arch/x86/entry/common.c static void exit_to_usermode_loop(struct · line 142
Quoted 8 lines
arch/x86/entry/common.c +1
-0addr_limit_user_check ();lockdep_assert_irqs_disabled ();@@ -137,6 +139,8 @@ static void exit_to_usermode_loop(structuser_enter_irqoff ();mds_user_clear_cpu_buffers (); +
/* Return to userspace (IRET) will re-enable interrupts */
Quoted 2 lines
arch/x86/entry/common.c +1
-0+trace_hardirqs_on (); }
Re: [RFC patch 10/15] x86/entry: Move irq tracing to C code
Quoted 26 lines · Peter Zijlstra
Quoted 14 lines · Thomas Gleixner
To prepare for converting the exit to usermode code to the generic version, move the irqflags tracing into C code.
Signed-off-by Thomas Gleixner <[email protected]>--- arch/x86/entry/common.c | 10 ++++++++++ arch/x86/entry/entry_32.S | 11 +---------- arch/x86/entry/entry_64.S | 10 ++-------- arch/x86/entry/entry_64_compat.S | 21 --------------------- 4 files changed, 13 insertions(+), 39 deletions(-)arch/x86/entry/common.c static void exit_to_usermode_loop(struct · line 105
Quoted 4 lines
arch/x86/entry/common.c +1
-0@@ -102,6 +102,8 @@ static void exit_to_usermode_loop(structstruct thread_info *ti =current_thread_info ();u32 cached_flags; +trace_hardirqs_off ();Bah.. so this gets called from:
- C code, with IRQs disabled
- entry_64.S:error_exit
- entry_32.S:resume_userspace
Quoted 6 lines
The first obviously doesn't need this annotation, but this patch doesn't remove the
TRACE_IRQS_OFFfromentry_64.Sand only the 32bit case is changed.Is that
entry_64.Scase an oversight, or do we need an extensive comment on this one?
Lemme stare at that again. At some point I probably lost track in that maze.
Thanks,
tglx
Peter Zijlstra
maintainerRe: [RFC patch 10/15] x86/entry: Move irq tracing to C code
Quoted 28 lines · Thomas Gleixner
Quoted 26 lines · Peter Zijlstra
Quoted 14 lines · Thomas Gleixner
To prepare for converting the exit to usermode code to the generic version, move the irqflags tracing into C code.
Signed-off-by Thomas Gleixner <[email protected]>--- arch/x86/entry/common.c | 10 ++++++++++ arch/x86/entry/entry_32.S | 11 +---------- arch/x86/entry/entry_64.S | 10 ++-------- arch/x86/entry/entry_64_compat.S | 21 --------------------- 4 files changed, 13 insertions(+), 39 deletions(-)arch/x86/entry/common.c static void exit_to_usermode_loop(struct · line 105
Quoted 4 lines
arch/x86/entry/common.c +1
-0@@ -102,6 +102,8 @@ static void exit_to_usermode_loop(structstruct thread_info *ti =current_thread_info ();u32 cached_flags; +trace_hardirqs_off ();Bah.. so this gets called from:
- C code, with IRQs disabled
- entry_64.S:error_exit
- entry_32.S:resume_userspace
Quoted 8 lines
Quoted 6 lines
The first obviously doesn't need this annotation, but this patch doesn't remove the
TRACE_IRQS_OFFfromentry_64.Sand only the 32bit case is changed.Is that
entry_64.Scase an oversight, or do we need an extensive comment on this one?Lemme stare at that again. At some point I probably lost track in that maze.
While walking the kids to school I wondered WTH we need to call TRACE_IRQS_OFF in the first place. If this is the return from exception path, interrupts had better be disabled already (in exception enter).
For entry_64.S we have:
- idtentry_part; which does TRACE_IRQS_OFF at the start and error_exit at the end.
- xen_do_hypervisor_callback, xen_failsafe_callback -- which are confusing.
So in the normal case, it appears we can simply do:
arch/x86/entry/entry_64.S +0 -2
@@ -1368,8 +1368,6 @@ END(error_entry) ENTRY(error_exit) UNWIND_HINT_REGS - DISABLE_INTERRUPTS(CLBR_ANY) - TRACE_IRQS_OFF testb $3, CS(%rsp) jz retint_kernel jmp retint_user
and all should be well. This leaves Xen...
For entry_32.S it looks like nothing uses 'resume_userspace' so that ENTRY can go away. Which then leaves:
* ret_from_intr:
- common_spurious: TRACE_IRQS_OFF
- common_interrupt: TRACE_IRQS_OFF
- BUILD_INTERRUPT3: TRACE_IRQS_OFF
- xen_do_upcall: ...
* ret_from_exception:
- xen_failsafe_callback: ...
- common_exception_read_cr2: TRACE_IRQS_OFF
- common_exception: TRACE_IRQS_OFF
- int3: TRACE_IRQS_OFF
Which again suggests:
arch/x86/entry/entry_32.S +0 -3
@@ -825,9 +825,6 @@ END(ret_from_fork) cmpl $USER_RPL, %eax jb restore_all_kernel # not returning to v8086 or userspace -ENTRY(resume_userspace) - DISABLE_INTERRUPTS(CLBR_ANY) - TRACE_IRQS_OFF movl %esp, %eax call prepare_exit_to_usermode jmp restore_all
with the notable exception (oh teh pun!) being Xen... _again_.
With these patchlets on, we'd want prepare_exit_to_usermode() to validate the IRQ state, but AFAICT it _should_ all just 'work' (famous last words).
Cc Juergen because Xen
Peter Zijlstra
maintainerRe: [RFC patch 10/15] x86/entry: Move irq tracing to C code
Quoted 62 lines · Peter Zijlstra
While walking the kids to school I wondered WTH we need to call
TRACE_IRQS_OFFin the first place. If this is the return from exception path, interrupts had better be disabled already (in exception enter).For
entry_64.Swe have:-
idtentry_part; which doesTRACE_IRQS_OFFat the start anderror_exitat the end.-
xen_do_hypervisor_callback,xen_failsafe_callback-- which are confusing.So in the normal case, it appears we can simply do:
arch/x86/entry/entry_64.S +0
-2@@ -1368,8 +1368,6 @@ END(error_entry) ENTRY(error_exit) UNWIND_HINT_REGS - DISABLE_INTERRUPTS(CLBR_ANY) - TRACE_IRQS_OFF testb $3, CS(%rsp) jz retint_kernel jmp retint_userand all should be well. This leaves Xen...
For
entry_32.Sit looks like nothing uses 'resume_userspace' so that ENTRY can go away. Which then leaves:*
ret_from_intr:
-common_spurious:TRACE_IRQS_OFF
-common_interrupt:TRACE_IRQS_OFF
-BUILD_INTERRUPT3:TRACE_IRQS_OFF
-xen_do_upcall: ...*
ret_from_exception:
-xen_failsafe_callback: ...
-common_exception_read_cr2:TRACE_IRQS_OFF
-common_exception:TRACE_IRQS_OFF
- int3:TRACE_IRQS_OFFWhich again suggests:
arch/x86/entry/entry_32.S +0
-3@@ -825,9 +825,6 @@ END(ret_from_fork) cmpl $USER_RPL, %eax jb restore_all_kernel # not returning to v8086 or userspace -ENTRY(resume_userspace) - DISABLE_INTERRUPTS(CLBR_ANY) - TRACE_IRQS_OFF movl %esp, %eax call prepare_exit_to_usermode jmp restore_all
Quoted 7 lines
with the notable exception (oh teh pun!) being Xen... _again_.
With these patchlets on, we'd want
prepare_exit_to_usermode()to validate the IRQ state, but AFAICT it _should_ all just 'work' (famous last words).Cc Juergen because Xen
Arrgh.. faults!! they do local_irq_enable() but never disable them again. Arguably those functions should be symmetric and restore IF when they muck with it instead of relying on the exit path fixing things up.
Peter Zijlstra
maintainerRe: [RFC patch 10/15] x86/entry: Move irq tracing to C code
Quoted 69 lines · Peter Zijlstra
Quoted 68 lines · Peter Zijlstra
While walking the kids to school I wondered WTH we need to call
TRACE_IRQS_OFFin the first place. If this is the return from exception path, interrupts had better be disabled already (in exception enter).For
entry_64.Swe have:-
idtentry_part; which doesTRACE_IRQS_OFFat the start anderror_exitat the end.-
xen_do_hypervisor_callback,xen_failsafe_callback-- which are confusing.So in the normal case, it appears we can simply do:
arch/x86/entry/entry_64.S +0
-2@@ -1368,8 +1368,6 @@ END(error_entry) ENTRY(error_exit) UNWIND_HINT_REGS - DISABLE_INTERRUPTS(CLBR_ANY) - TRACE_IRQS_OFF testb $3, CS(%rsp) jz retint_kernel jmp retint_userand all should be well. This leaves Xen...
For
entry_32.Sit looks like nothing uses 'resume_userspace' so that ENTRY can go away. Which then leaves:*
ret_from_intr:
-common_spurious:TRACE_IRQS_OFF
-common_interrupt:TRACE_IRQS_OFF
-BUILD_INTERRUPT3:TRACE_IRQS_OFF
-xen_do_upcall: ...*
ret_from_exception:
-xen_failsafe_callback: ...
-common_exception_read_cr2:TRACE_IRQS_OFF
-common_exception:TRACE_IRQS_OFF
- int3:TRACE_IRQS_OFFWhich again suggests:
arch/x86/entry/entry_32.S +0
-3@@ -825,9 +825,6 @@ END(ret_from_fork) cmpl $USER_RPL, %eax jb restore_all_kernel # not returning to v8086 or userspace -ENTRY(resume_userspace) - DISABLE_INTERRUPTS(CLBR_ANY) - TRACE_IRQS_OFF movl %esp, %eax call prepare_exit_to_usermode jmp restore_allwith the notable exception (oh teh pun!) being Xen... _again_.
With these patchlets on, we'd want
prepare_exit_to_usermode()to validate the IRQ state, but AFAICT it _should_ all just 'work' (famous last words).
Quoted 5 lines
Quoted 1 line
Cc Juergen because Xen
Arrgh.. faults!! they do
local_irq_enable()but never disable them again. Arguably those functions should be symmetric and restore IF when they muck with it instead of relying on the exit path fixing things up.
---
arch/x86/entry/entry_32.S +0 -3
@@ -825,9 +825,6 @@ END(ret_from_fork) cmpl $USER_RPL, %eax jb restore_all_kernel # not returning to v8086 or userspace -ENTRY(resume_userspace) - DISABLE_INTERRUPTS(CLBR_ANY) - TRACE_IRQS_OFF movl %esp, %eax call prepare_exit_to_usermode jmp restore_all
arch/x86/entry/entry_64.S +0 -2
@@ -1368,8 +1368,6 @@ END(error_entry) ENTRY(error_exit) UNWIND_HINT_REGS - DISABLE_INTERRUPTS(CLBR_ANY) - TRACE_IRQS_OFF testb $3, CS(%rsp) jz retint_kernel jmp retint_user
arch/x86/kernel/traps.c +20 -11
@@ -276,6 +276,7 @@ static void do_error_trap(struct pt_regs *regs, long error_code, char *str, NOTIFY_STOP) {cond_local_irq_enable (regs);do_trap (trapnr, signr, str, regs, error_code, sicode, addr); +cond_local_irq_disable (regs); } }
@@ -501,6 +502,7 @@ dotraplinkage void do_bounds(struct pt_regs *regs, long error_code)die ("bounds" , regs, error_code); } +cond_local_irq_disable (regs);return ; exit_trap:
@@ -512,6 +514,7 @@ dotraplinkage void do_bounds(struct pt_regs *regs, long error_code)* time.. */ do_trap (X86_TRAP_BR ,SIGSEGV ,"bounds" , regs, error_code,0 ,NULL ); +cond_local_irq_disable (regs); }dotraplinkage void
@@ -525,19 +528,19 @@ do_general_protection(struct pt_regs *regs, long error_code)if (static_cpu_has (X86_FEATURE_UMIP )) {if (user_mode (regs) &&fixup_umip_exception (regs)) -return ; + goto exit_trap; }if (v8086_mode (regs)) {local_irq_enable ();handle_vm86_fault ((struct kernel_vm86_regs *) regs, error_code); -return ; + goto exit_trap; } tsk = current;if (!user_mode (regs)) {if (fixup_exception (regs,X86_TRAP_GP , error_code,0 )) -return ; + goto exit_trap; tsk->thread.error_code = error_code; tsk->thread.trap_nr =X86_TRAP_GP ;
@@ -549,12 +552,12 @@ do_general_protection(struct pt_regs *regs, long error_code)*/ if (!preemptible () &&kprobe_running () &&kprobe_fault_handler (regs,X86_TRAP_GP )) -return ; + goto exit_trap;if (notify_die (DIE_GPF , desc, regs, error_code,X86_TRAP_GP ,SIGSEGV ) !=NOTIFY_STOP )die (desc, regs, error_code); -return ; + goto exit_trap; } tsk->thread.error_code = error_code;
@@ -563,6 +566,8 @@ do_general_protection(struct pt_regs *regs, long error_code)show_signal (tsk,SIGSEGV ,"" , desc, regs, error_code);force_sig (SIGSEGV ); +exit_trap: +cond_local_irq_disable (regs); }NOKPROBE_SYMBOL (do_general_protection);
@@ -783,9 +788,7 @@ dotraplinkage void do_debug(struct pt_regs *regs, long error_code)if (v8086_mode (regs)) {handle_vm86_trap ((struct kernel_vm86_regs *) regs, error_code,X86_TRAP_DB ); -cond_local_irq_disable (regs); -debug_stack_usage_dec (); - goto exit; + goto exit_irq; }if (WARN_ON_ONCE ((dr6 &DR_STEP ) && !user_mode (regs))) {
@@ -802,6 +805,8 @@ dotraplinkage void do_debug(struct pt_regs *regs, long error_code) si_code =get_si_code (tsk->thread.debugreg6);if (tsk->thread.debugreg6 & (DR_STEP |DR_TRAP_BITS ) || user_icebp)send_sigtrap (regs, error_code, si_code); + +exit_irq:cond_local_irq_disable (regs);debug_stack_usage_dec ();
@@ -827,7 +832,7 @@ static void math_error(struct pt_regs *regs, int error_code, int trapnr)if (!user_mode (regs)) {if (fixup_exception (regs, trapnr, error_code,0 )) -return ; + goto exit_trap; task->thread.error_code = error_code; task->thread.trap_nr = trapnr;
@@ -835,7 +840,7 @@ static void math_error(struct pt_regs *regs, int error_code, int trapnr)if (notify_die (DIE_TRAP , str, regs, error_code, trapnr,SIGFPE ) !=NOTIFY_STOP )die (str, regs, error_code); -return ; + goto exit_trap; }/*
@@ -849,10 +854,12 @@ static void math_error(struct pt_regs *regs, int error_code, int trapnr) si_code =fpu__exception_code (fpu, trapnr);/* Retry when we get spurious exceptions: */ if (!si_code) -return ; + goto exit_trap;force_sig_fault (SIGFPE , si_code, (void __user *)uprobe_get_trap_addr (regs)); +exit_trap: +cond_local_irq_disable (regs); } dotraplinkage void do_coprocessor_error(struct pt_regs *regs, long error_code)
@@ -889,6 +896,8 @@ do_device_not_available(struct pt_regs *regs, long error_code) info.regs = regs;math_emulate (&info); + +cond_local_irq_disable (regs); return; }#endif
arch/x86/mm/fault.c +5 -2
@@ -1500,10 +1500,13 @@ __do_page_fault(struct pt_regs *regs, unsigned long hw_error_code,return ;/* Was the fault on kernel-controlled part of the address space? */ -if (unlikely (fault_in_kernel_space (address))) +if (unlikely (fault_in_kernel_space (address))) {do_kern_addr_fault (regs, hw_error_code, address); -else + }else {do_user_addr_fault (regs, hw_error_code, address); +if (regs->flags &X86_EFLAGS_IF ) +local_irq_disable (); + } }NOKPROBE_SYMBOL (__do_page_fault);
Andy Lutomirski
maintainerRe: [RFC patch 10/15] x86/entry: Move irq tracing to C code
Quoted 83 lines · Peter Zijlstra
Quoted 69 lines · Peter Zijlstra
Quoted 68 lines · Peter Zijlstra
While walking the kids to school I wondered WTH we need to call
TRACE_IRQS_OFFin the first place. If this is the return from exception path, interrupts had better be disabled already (in exception enter).For
entry_64.Swe have:-
idtentry_part; which doesTRACE_IRQS_OFFat the start anderror_exitat the end.-
xen_do_hypervisor_callback,xen_failsafe_callback-- which are confusing.So in the normal case, it appears we can simply do:
arch/x86/entry/entry_64.S +0
-2@@ -1368,8 +1368,6 @@ END(error_entry) ENTRY(error_exit) UNWIND_HINT_REGS - DISABLE_INTERRUPTS(CLBR_ANY) - TRACE_IRQS_OFF testb $3, CS(%rsp) jz retint_kernel jmp retint_userand all should be well. This leaves Xen...
For
entry_32.Sit looks like nothing uses 'resume_userspace' so that ENTRY can go away. Which then leaves:*
ret_from_intr:
-common_spurious:TRACE_IRQS_OFF
-common_interrupt:TRACE_IRQS_OFF
-BUILD_INTERRUPT3:TRACE_IRQS_OFF
-xen_do_upcall: ...*
ret_from_exception:
-xen_failsafe_callback: ...
-common_exception_read_cr2:TRACE_IRQS_OFF
-common_exception:TRACE_IRQS_OFF
- int3:TRACE_IRQS_OFFWhich again suggests:
arch/x86/entry/entry_32.S +0
-3@@ -825,9 +825,6 @@ END(ret_from_fork) cmpl $USER_RPL, %eax jb restore_all_kernel # not returning to v8086 or userspace -ENTRY(resume_userspace) - DISABLE_INTERRUPTS(CLBR_ANY) - TRACE_IRQS_OFF movl %esp, %eax call prepare_exit_to_usermode jmp restore_allwith the notable exception (oh teh pun!) being Xen... _again_.
With these patchlets on, we'd want
prepare_exit_to_usermode()to validate the IRQ state, but AFAICT it _should_ all just 'work' (famous last words).Quoted 5 lines
Quoted 1 line
Cc Juergen because Xen
Arrgh.. faults!! they do
local_irq_enable()but never disable them again. Arguably those functions should be symmetric and restore IF when they muck with it instead of relying on the exit path fixing things up.---
arch/x86/entry/entry_32.S +0
-0
arch/x86/entry/entry_32.S END(ret_from_fork) · line 826
Quoted 2 lines
arch/x86/entry/entry_32.S +0
-0@@ -825,9 +825,6 @@ END(ret_from_fork) cmpl $USER_RPL, %eax jb restore_all_kernel # not returning to v8086 or userspace
...
Yes, please, but can you add an assertion under CONFIG_DEBUG_ENTRY that IRQs are actually off?
Re: [RFC patch 10/15] x86/entry: Move irq tracing to C code
In reply to Thomas Gleixnerarch/x86/entry/entry_64.S ENTRY(error_entry) · line 1369
Quoted 8 lines · Thomas Gleixner
ENTRY(error_exit)arch/x86/entry/entry_64.S +1
-1- UNWIND_HINT_REGS DISABLE_INTERRUPTS(CLBR_ANY) TRACE_IRQS_OFF + UNWIND_HINT_REGS testb $3, CS(%rsp) jz retint_kernel jmp retint_user
I don't think this UNWIND_HINT_REGS needs to be moved?
Signature · 1 line
Josh
RFC PATCH 11/15 x86/entry: Use generic exit to usermode
In reply to Thomas GleixnerReplace the x86 specific exit to usermode code with the generic implementation.
--- arch/x86/entry/common.c | 111 ------------------------------------ arch/x86/entry/entry_32.S | 2 arch/x86/entry/entry_64.S | 2 arch/x86/include/asm/entry-common.h | 47 ++++++++++++++- arch/x86/include/asm/signal.h | 1 arch/x86/kernel/signal.c | 2 6 files changed, 51 insertions(+), 114 deletions(-)
arch/x86/entry/common.c +3 -108
@@ -15,15 +15,9 @@#include <linux/smp.h> #include <linux/errno.h> #include <linux/ptrace.h> -#include <linux/tracehook.h> -#include <linux/audit.h> #include <linux/signal.h> #include <linux/export.h> -#include <linux/context_tracking.h> -#include <linux/user-return-notifier.h> #include <linux/nospec.h> -#include <linux/uprobes.h> -#include <linux/livepatch.h> #include <linux/syscalls.h> #include <linux/uaccess.h>
@@ -47,102 +41,6 @@static inline void enter_from_user_mode (void ) {}#endif -#define EXIT_TO_USERMODE_LOOP_FLAGS \ - (_TIF_SIGPENDING | _TIF_NOTIFY_RESUME | _TIF_UPROBE | \ - _TIF_NEED_RESCHED | _TIF_USER_RETURN_NOTIFY | _TIF_PATCH_PENDING) - -static void exit_to_usermode_loop (struct pt_regs *regs,u32 cached_flags) -{ -/* -* In order to return to user mode, we need to have IRQs off with -* none of EXIT_TO_USERMODE_LOOP_FLAGS set. Several of these flags -* can be set at any time on preemptible kernels if we have IRQs on, -* so we need to loop. Disabling preemption wouldn't help: doing the -* work to clear some of the flags can sleep. -*/ -while (true) { -/* We have work to do. */ -local_irq_enable (); - -if (cached_flags & _TIF_NEED_RESCHED) -schedule (); - -if (cached_flags & _TIF_UPROBE) -uprobe_notify_resume (regs); - -if (cached_flags & _TIF_PATCH_PENDING) -klp_update_patch_state (current); - -/* deal with pending signal delivery */ -if (cached_flags & _TIF_SIGPENDING) -do_signal (regs); - -if (cached_flags & _TIF_NOTIFY_RESUME) { -clear_thread_flag (TIF_NOTIFY_RESUME ); -tracehook_notify_resume (regs); -rseq_handle_notify_resume (NULL , regs); - } - -if (cached_flags & _TIF_USER_RETURN_NOTIFY) -fire_user_return_notifiers (); - -/* Disable IRQs and retry */ -local_irq_disable (); - - cached_flags =READ_ONCE (current_thread_info ()->flags); - -if (!(cached_flags &EXIT_TO_USERMODE_LOOP_FLAGS )) -break ; - } -} - -/* Called with IRQs disabled. */ -__visible inline void prepare_exit_to_usermode(struct pt_regs *regs) -{ -struct thread_info *ti =current_thread_info (); -u32 cached_flags; - -trace_hardirqs_off (); - -addr_limit_user_check (); - -lockdep_assert_irqs_disabled (); -lockdep_sys_exit (); - - cached_flags =READ_ONCE (ti->flags); - -if (unlikely (cached_flags &EXIT_TO_USERMODE_LOOP_FLAGS )) -exit_to_usermode_loop (regs, cached_flags); - -/* Reload ti->flags; we may have rescheduled above. */ - cached_flags =READ_ONCE (ti->flags); - -fpregs_assert_state_consistent (); -if (unlikely (cached_flags & _TIF_NEED_FPU_LOAD)) -switch_fpu_return (); - -#ifdef CONFIG_COMPAT -/* -* Compat syscalls set TS_COMPAT. Make sure we clear it before -* returning to user mode. We need to clear it *after* signal -* handling, because syscall restart has a fixup for compat -* syscalls. The fixup is exercised by the ptrace_syscall_32 -* selftest. -* -* We also need to clear TS_REGS_POKED_I386: the 32-bit tracer -* special case only applies after poking regs and before the -* very next return to user mode. -*/ - ti->status &= ~(TS_COMPAT |TS_I386_REGS_POKED ); -#endif - -user_enter_irqoff (); - -mds_user_clear_cpu_buffers (); - -trace_hardirqs_on (); -} -/* * Called with IRQs on and fully valid regs. Returns with IRQs off in a * state such that we can immediately switch to user mode.
@@ -150,9 +48,6 @@ static void exit_to_usermode_loop(struct __visible inline void syscall_return_slowpath(struct pt_regs *regs) {syscall_exit_to_usermode (regs, regs->orig_ax, regs->ax); - -local_irq_disable (); -prepare_exit_to_usermode (regs); }#ifdef CONFIG_X86_64
@@ -177,7 +72,7 @@ static void exit_to_usermode_loop(struct #endif } -syscall_return_slowpath (regs); +syscall_exit_to_usermode (regs, regs->orig_ax, regs->ax); }#endif
@@ -221,7 +116,7 @@ static __always_inline void do_syscall_3#endif /* CONFIG_IA32_EMULATION */ } -syscall_return_slowpath (regs); +syscall_exit_to_usermode (regs, regs->orig_ax, regs->ax); }/* Handles int $0x80 */
@@ -276,7 +171,7 @@ static __always_inline void do_syscall_3/* User code screwed up. */ local_irq_disable (); regs->ax = -EFAULT ; -prepare_exit_to_usermode (regs); +exit_to_usermode (regs);return 0 ;/* Keep it simple: use IRET. */ }
arch/x86/entry/entry_32.S +1 -1
@@ -828,7 +828,7 @@ END(ret_from_fork) ENTRY(resume_userspace) DISABLE_INTERRUPTS(CLBR_ANY) movl %esp, %eax - call prepare_exit_to_usermode + call exit_to_usermode jmp restore_all END(ret_from_exception)
arch/x86/entry/entry_64.S +1 -1
@@ -612,7 +612,7 @@ END(common_spurious) /* Interrupt came from user space */ GLOBAL(retint_user) mov %rsp,%rdi - call prepare_exit_to_usermode + call exit_to_usermode GLOBAL(swapgs_restore_regs_and_return_to_usermode) #ifdef CONFIG_DEBUG_ENTRY
arch/x86/include/asm/entry-common.h +45 -2
56 lines not on this page · raw message
arch/x86/include/asm/signal.h +0 -1
@@ -35,7 +35,6 @@ typedef sigset_t compat_sigset_t;#endif /* __ASSEMBLY__ */ #include <uapi/asm/signal.h> #ifndef __ASSEMBLY__ -extern void do_signal (struct pt_regs *regs);#define __ARCH_HAS_SA_RESTORER
arch/x86/kernel/signal.c +1 -1
@@ -808,7 +808,7 @@ static inline unsigned long get_nr_resta* want to handle. Thus you cannot kill init even with a SIGKILL even by * mistake. */ -void do_signal (struct pt_regs *regs) +void arch_do_signal (struct pt_regs *regs) {struct ksignal ksig;
RFC PATCH 12/15 arm64/entry: Use generic exit to usermode
In reply to Thomas GleixnerReplace the exit to usermode code with the generic version.
--- arch/arm64/include/asm/entry-common.h | 29 +++++++++++++++++++++ arch/arm64/kernel/entry.S | 18 ++----------- arch/arm64/kernel/signal.c | 45 ---------------------------------- 3 files changed, 33 insertions(+), 59 deletions(-)
arch/arm64/include/asm/entry-common.h +29 -0
@@ -5,6 +5,35 @@#ifndef __ASM_ENTRY_COMMON_H#define __ASM_ENTRY_COMMON_H +#include <asm/daifflags.h> + +#define ARCH_EXIT_TO_USERMODE_WORK (_TIF_FOREIGN_FPSTATE) + +static inline void local_irq_enable_exit_to_user (unsigned long ti_work) +{ +if (ti_work & _TIF_NEED_RESCHED) +local_daif_restore (DAIF_PROCCTX_NOIRQ ); +else +local_daif_restore (DAIF_PROCCTX ); +} +#define local_irq_enable_exit_to_user local_irq_enable_exit_to_user + +static inline void local_irq_disable_exit_to_user (void ) +{ +local_daif_mask (); +} +#define local_irq_disable_exit_to_user local_irq_disable_exit_to_user + +static inline void arch_exit_to_usermode_work (struct pt_regs *regs, +unsigned long ti_work) +{ +/* Must this be inside the work loop ? */ +if (ti_work & _TIF_FOREIGN_FPSTATE) +fpsimd_restore_current_state (); + +} +#define arch_exit_to_usermode_work arch_exit_to_usermode_work +enum ptrace_syscall_dir {PTRACE_SYSCALL_ENTER =0 , PTRACE_SYSCALL_EXIT,
arch/arm64/kernel/entry.S +3 -15
@@ -971,26 +971,14 @@ ENDPROC(el1_error) ENDPROC(el0_error) /* - * Ok, we need to do extra processing, enter the slow path. - */ -work_pending: - mov x0, sp // 'regs' - bl do_notify_resume -#ifdef CONFIG_TRACE_IRQFLAGS - bl trace_hardirqs_on // enabled while in userspace -#endif - ldr x1, [tsk, #TSK_TI_FLAGS] // re-check for single-step - b finish_ret_to_user -/* * "slow" syscall return path. */ ret_to_user: disable_daif gic_prio_kentry_setup tmp=x3 - ldr x1, [tsk, #TSK_TI_FLAGS] - and x2, x1, #_TIF_WORK_MASK - cbnz x2, work_pending -finish_ret_to_user: + mov x0, sp // 'regs' + bl exit_to_usermode + ldr x1, [tsk, #TSK_TI_FLAGS] // re-check for single-step enable_step_tsk x1, x2 #ifdef CONFIG_GCC_PLUGIN_STACKLEAK bl stackleak_erase
arch/arm64/kernel/signal.c +1 -44
57 lines not on this page · raw message
RFC PATCH 13/15 arm64/entry: Move FPU restore out of exit_to_usermode() loop
In reply to Thomas GleixnerRestoring the FPU is not required to be insided the loop. The final point to do that is before returning to user space. Move it there.
--- arch/arm64/include/asm/entry-common.h | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-)
arch/arm64/include/asm/entry-common.h +3 -6
@@ -7,8 +7,6 @@#include <asm/daifflags.h> -#define ARCH_EXIT_TO_USERMODE_WORK (_TIF_FOREIGN_FPSTATE) -static inline void local_irq_enable_exit_to_user (unsigned long ti_work) {if (ti_work & _TIF_NEED_RESCHED)
@@ -24,15 +22,14 @@ static inline void local_irq_disable_exi }#define local_irq_disable_exit_to_user local_irq_disable_exit_to_user -static inline void arch_exit_to_usermode_work (struct pt_regs *regs, -unsigned long ti_work) +static inline void arch_exit_to_usermode (struct pt_regs *regs, +unsigned long ti_work) { -/* Must this be inside the work loop ? */ if (ti_work & _TIF_FOREIGN_FPSTATE)fpsimd_restore_current_state (); } -#define arch_exit_to_usermode_work arch_exit_to_usermode_work +#define arch_exit_to_usermode arch_exit_to_usermodeenum ptrace_syscall_dir {PTRACE_SYSCALL_ENTER =0 ,
RFC PATCH 14/15 workpending: Provide infrastructure for work before entering a guest
In reply to Thomas GleixnerEntering a guest is similar to exiting to user space. Pending work like handling signals, rescheduling, task work etc. needs to be handled before that.
Provide generic infrastructure to avoid duplication of the same handling code all over the place.
Update ARM64 struct kvm_vcpu_stat with a signal_exit member so the generic code compiles.
--- arch/arm64/include/asm/kvm_host.h | 1 include/linux/entry-common.h | 66 ++++++++++++++++++++++++++++++++++++++ kernel/entry/common.c | 44 +++++++++++++++++++++++++ 3 files changed, 111 insertions(+)
arch/arm64/include/asm/kvm_host.h +1 -0
@@ -409,6 +409,7 @@ struct kvm_vcpu_stat {u64 wfi_exit_stat;u64 mmio_exit_user;u64 mmio_exit_kernel; +u64 signal_exits;u64 exits; };
include/linux/entry-common.h +66 -0
70 lines not on this page · raw message
kernel/entry/common.c +44 -0
47 lines not on this page · raw message
Paolo Bonzini
maintainerRe: [RFC patch 14/15] workpending: Provide infrastructure for work before entering a guest
Quick API review before I dive into the implementation.
Quoted 21 lines · Thomas Gleixner
kernel/entry/common.c +21
-0+/* +* Before returning to guest mode handle all pending work +*/ +if (ti_work & _TIF_SIGPENDING) { + vcpu->run->exit_reason =KVM_EXIT_INTR ; + vcpu->stat.signal_exits++; +return -EINTR ; + } + +if (ti_work & _TIF_NEED_RESCHED) { +srcu_read_unlock (&kvm->srcu, vcpu->srcu_idx); +schedule (); + vcpu->srcu_idx =srcu_read_lock (&kvm->srcu); + } + +if (ti_work & _TIF_PATCH_PENDING) { +srcu_read_unlock (&kvm->srcu, vcpu->srcu_idx); +klp_update_patch_state (current); + vcpu->srcu_idx =srcu_read_lock (&kvm->srcu); + } +
kernel/entry/common.c void syscall_exit_to_usermode(struct pt_ · line 218
Quoted 10 lines
kernel/entry/common.c +10
-0+if (ti_work & _TIF_NOTIFY_RESUME) { +srcu_read_unlock (&kvm->srcu, vcpu->srcu_idx); +clear_thread_flag (TIF_NOTIFY_RESUME ); +tracehook_notify_resume (NULL ); + vcpu->srcu_idx =srcu_read_lock (&kvm->srcu); + } + +/* Any extra architecture specific work */ +return arch_exit_to_guestmode_work (kvm, vcpu, ti_work); +}
Perhaps, in virt/kvm/kvm_main.c:
int kvm_exit_to_guestmode_work(struct kvm *kvm, struct kvm_vcpu *vcpu,
unsigned long ti_work)
{
int r;
/*
* Before returning to guest mode handle all pending work
*/
if (ti_work & _TIF_SIGPENDING) {
vcpu->run->exit_reason = KVM_EXIT_INTR;
vcpu->stat.signal_exits++;
return -EINTR;
}
srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
core_exit_to_guestmode_work(ti_work);
vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
return r;
}and in kernel/entry/common.c:
int core_exit_to_guestmode_work(unsigned long ti_work)
{
/*
* Before returning to guest mode handle all pending work
*/
if (ti_work & _TIF_NEED_RESCHED)
schedule();
if (ti_work & _TIF_PATCH_PENDING)
klp_update_patch_state(current);
if (ti_work & _TIF_NOTIFY_RESUME) {
clear_thread_flag(TIF_NOTIFY_RESUME);
tracehook_notify_resume(NULL);
}
return arch_exit_to_guestmode_work(ti_work);
}so that kernel/entry/ is not polluted with KVM structs and APIs.
Perhaps even extract the body of core_exit_to_usermode_work's while loop to a separate function, and call it as
core_exit_to_usermode_work_once(NULL, ti_work & EXIT_TO_GUESTMODE_WORK);
from core_exit_to_guestmode_work.
In general I don't mind having these exit_to_guestmode functions in kvm_host.h, and only having entry-common.h export EXIT_TO_GUESTMODE_WORK and ARCH_EXIT_TO_GUESTMODE_WORK. Unless you had good reasons to do the opposite...
Paolo
Re: [RFC patch 14/15] workpending: Provide infrastructure for work before entering a guest
Quoted 9 lines · Paolo Bonzini
kernel/entry/common.c void syscall_exit_to_usermode(struct pt_ · line 218
Quoted 3 lines
kernel/entry/common.c +3
-0+/* Any extra architecture specific work */ +return arch_exit_to_guestmode_work (kvm, vcpu, ti_work); +}Perhaps, in
virt/kvm/kvm_main.c:int kvm_exit_to_guestmode_work(struct kvm *kvm, struct
kvm_vcpu*vcpu,
unsigned longti_work)
{
...
Quoted 6 lines
}
and in
kernel/entry/common.c:int
core_exit_to_guestmode_work(unsigned long ti_work){
...
Quoted 1 line
}
Makes sense.
Quoted 7 lines
so that
kernel/entry/is not polluted with KVM structs and APIs.Perhaps even extract the body of core_exit_to_usermode_work's while loop to a separate function, and call it as
core_exit_to_usermode_work_once(NULL, ti_work & EXIT_TO_GUESTMODE_WORK);
Doh, its too obvious now that you mention it :)
Quoted 6 lines
from
core_exit_to_guestmode_work.In general I don't mind having these
exit_to_guestmodefunctions inkvm_host.h, and only havingentry-common.hexportEXIT_TO_GUESTMODE_WORKandARCH_EXIT_TO_GUESTMODE_WORK. Unless you had good reasons to do the opposite...
That was an arbitrary choice and it does not matter much where it lives.
Thanks,
tglx
Andy Lutomirski
maintainerRe: [RFC patch 14/15] workpending: Provide infrastructure for work before entering a guest
In reply to Thomas GleixnerQuoted 50 lines · Thomas Gleixner
Entering a guest is similar to exiting to user space. Pending work like handling signals, rescheduling, task work etc. needs to be handled before that.
Provide generic infrastructure to avoid duplication of the same handling code all over the place.
Update ARM64 struct
kvm_vcpu_statwith asignal_exitmember so the generic code compiles.Signed-off-by Thomas Gleixner <[email protected]>--- arch/arm64/include/asm/kvm_host.h | 1 include/linux/entry-common.h | 66 ++++++++++++++++++++++++++++++++++++++ kernel/entry/common.c | 44 +++++++++++++++++++++++++ 3 files changed, 111 insertions(+)arch/arm64/include/asm/kvm_host.h +1
-0@@ -409,6 +409,7 @@ struct kvm_vcpu_stat {u64 wfi_exit_stat;u64 mmio_exit_user;u64 mmio_exit_kernel; +u64 signal_exits;u64 exits; };include/linux/entry-common.h +16
-0@@ -255,4 +255,70 @@ static inline void arch_syscall_exit_tra/* Common syscall exit function */ void syscall_exit_to_usermode (struct pt_regs *regs,long syscall,long retval); +#if IS_ENABLED (CONFIG_KVM ) + +#include <linux/kvm_host.h> + +#ifndef ARCH_EXIT_TO_GUESTMODE_WORK +# define ARCH_EXIT_TO_GUESTMODE_WORK (0) +#endif + +#define EXIT_TO_GUESTMODE_WORK \ + (_TIF_NEED_RESCHED | _TIF_SIGPENDING | _TIF_NOTIFY_RESUME | \ + ARCH_EXIT_TO_GUESTMODE_WORK) + +int core_exit_to_guestmode_work (struct kvm *kvm,struct kvm_vcpu *vcpu, +unsigned long ti_work); + +/**
include/linux/entry-common.h static inline void arch_syscall_exit_tra · line 283
Quoted 10 lines
include/linux/entry-common.h +10
-0+* arch_exit_to_guestmode - Architecture specific exit to guest mode function +* @kvm: Pointer to the guest instance +* @vcpu: Pointer to current's VCPU data +* @ti_work: Cached TIF flags gathered in exit_to_guestmode() +* +* Invoked from core_exit_to_guestmode_work(). Can be replaced by +* architecture specific code. +*/ +static inline int arch_exit_to_guestmode (struct kvm *kvm,struct kvm_vcpu *vcpu, +unsigned long ti_work);
Can you add a comment about whether IRQs are supposed to be off (I assume they are) and perhaps a lockdep assertion to verify it?
Miroslav Benes
maintainerRe: [RFC patch 14/15] workpending: Provide infrastructure for work before entering a guest
In reply to Thomas GleixnerQuoted 2 lines
include/linux/entry-common.h +0
-0
[...]
Quoted 3 lines
include/linux/entry-common.h +3
-0+#define EXIT_TO_GUESTMODE_WORK \ + (_TIF_NEED_RESCHED | _TIF_SIGPENDING | _TIF_NOTIFY_RESUME | \ + ARCH_EXIT_TO_GUESTMODE_WORK)
[...]
Quoted 16 lines
kernel/entry/common.c +13
-0+int core_exit_to_guestmode_work (struct kvm *kvm,struct kvm_vcpu *vcpu, +unsigned long ti_work) +{ +/* +* Before returning to guest mode handle all pending work +*/ +if (ti_work & _TIF_SIGPENDING) { + vcpu->run->exit_reason =KVM_EXIT_INTR ; + vcpu->stat.signal_exits++; +return -EINTR ; + } + +if (ti_work & _TIF_NEED_RESCHED) {
kernel/entry/common.c void syscall_exit_to_usermode(struct pt_ · line 207
Quoted 10 lines
kernel/entry/common.c +10
-0+srcu_read_unlock (&kvm->srcu, vcpu->srcu_idx); +schedule (); + vcpu->srcu_idx =srcu_read_lock (&kvm->srcu); + } + +if (ti_work & _TIF_PATCH_PENDING) { +srcu_read_unlock (&kvm->srcu, vcpu->srcu_idx); +klp_update_patch_state (current); + vcpu->srcu_idx =srcu_read_lock (&kvm->srcu); + }
If I am reading the code correctly, _TIF_PATCH_PENDING is not a part of EXIT_TO_GUESTMODE_WORK, so the handling code here would not be called on any arch as of now.
I also think that _TIF_PATCH_PENDING must not be handled here generally. It could break consistency guarantees when live patching KVM (and we do that from time to time).
Adding live-patching ML to CC.
Miroslav
Quoted 10 lines
kernel/entry/common.c +10
-0+if (ti_work & _TIF_NOTIFY_RESUME) { +srcu_read_unlock (&kvm->srcu, vcpu->srcu_idx); +clear_thread_flag (TIF_NOTIFY_RESUME ); +tracehook_notify_resume (NULL ); + vcpu->srcu_idx =srcu_read_lock (&kvm->srcu); + } + +/* Any extra architecture specific work */ +return arch_exit_to_guestmode_work (kvm, vcpu, ti_work); +}
RFC PATCH 15/15 x86/kvm: Use GENERIC_EXIT_WORKPENDING
In reply to Thomas GleixnerUse the generic infrastructure to check for and handle pending work before entering into guest mode.
--- arch/x86/kvm/x86.c | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-)
arch/x86/kvm/x86.c +5 -12
@@ -52,6 +52,7 @@#include <linux/irqbypass.h> #include <linux/sched/stat.h> #include <linux/sched/isolation.h> +#include <linux/entry-common.h> #include <linux/mem_encrypt.h> #include <trace/events/kvm.h>
@@ -7984,8 +7985,8 @@ static int vcpu_enter_guest(struct kvm_vif (kvm_lapic_enabled (vcpu) && vcpu->arch.apicv_active) kvm_x86_ops->sync_pir_to_irr (vcpu); -if (vcpu->mode ==EXITING_GUEST_MODE ||kvm_request_pending (vcpu) - ||need_resched () ||signal_pending (current)) { +if (vcpu->mode ==EXITING_GUEST_MODE ||kvm_request_pending (vcpu) || +exit_to_guestmode_work_pending ()) { vcpu->mode =OUTSIDE_GUEST_MODE ;smp_wmb ();local_irq_enable ();
@@ -8178,17 +8179,9 @@ static int vcpu_run(struct kvm_vcpu *vcpkvm_check_async_pf_completion (vcpu); -if (signal_pending (current)) { - r = -EINTR ; - vcpu->run->exit_reason =KVM_EXIT_INTR ; - ++vcpu->stat.signal_exits; + r =exit_to_guestmode (kvm, vcpu); +if (r)break ; - } -if (need_resched ()) { -srcu_read_unlock (&kvm->srcu, vcpu->srcu_idx); -cond_resched (); - vcpu->srcu_idx =srcu_read_lock (&kvm->srcu); - } }srcu_read_unlock (&kvm->srcu, vcpu->srcu_idx);
Paolo Bonzini
maintainerRe: [RFC patch 15/15] x86/kvm: Use GENERIC_EXIT_WORKPENDING
Quoted 4 lines · Thomas Gleixner
Use the generic infrastructure to check for and handle pending work before entering into guest mode.
Signed-off-by Thomas Gleixner <[email protected]>
Subject should be "x86/kvm: use exit_to_guestmode".
Paolo
Quoted 47 lines
--- arch/x86/kvm/x86.c | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-)arch/x86/kvm/x86.c +5
-12@@ -52,6 +52,7 @@#include <linux/irqbypass.h> #include <linux/sched/stat.h> #include <linux/sched/isolation.h> +#include <linux/entry-common.h> #include <linux/mem_encrypt.h> #include <trace/events/kvm.h> @@ -7984,8 +7985,8 @@ static int vcpu_enter_guest(struct kvm_vif (kvm_lapic_enabled (vcpu) && vcpu->arch.apicv_active) kvm_x86_ops->sync_pir_to_irr (vcpu); -if (vcpu->mode ==EXITING_GUEST_MODE ||kvm_request_pending (vcpu) - ||need_resched () ||signal_pending (current)) { +if (vcpu->mode ==EXITING_GUEST_MODE ||kvm_request_pending (vcpu) || +exit_to_guestmode_work_pending ()) { vcpu->mode =OUTSIDE_GUEST_MODE ;smp_wmb ();local_irq_enable ();@@ -8178,17 +8179,9 @@ static int vcpu_run(struct kvm_vcpu *vcpkvm_check_async_pf_completion (vcpu); -if (signal_pending (current)) { - r = -EINTR ; - vcpu->run->exit_reason =KVM_EXIT_INTR ; - ++vcpu->stat.signal_exits; + r =exit_to_guestmode (kvm, vcpu); +if (r)break ; - } -if (need_resched ()) { -srcu_read_unlock (&kvm->srcu, vcpu->srcu_idx); -cond_resched (); - vcpu->srcu_idx =srcu_read_lock (&kvm->srcu); - } }srcu_read_unlock (&kvm->srcu, vcpu->srcu_idx);
Mark Rutland
reviewerHi Thomas,
As a heads-up, I'm going to be away next week, and I likely won't have the chance to look at this in detail before October.
Quoted 10 lines · Thomas Gleixner
When working on a way to move out the posix cpu timer expiry out of the timer interrupt context, I noticed that KVM is not handling pending task work before entering a guest. A quick hack was to add that to the x86 KVM handling loop. The discussion ended with a request to make this a generic infrastructure possible with also moving the per arch implementations of the enter from and return to user space handling generic.
https://lore.kernel.org/r/[email protected]
You asked for it, so don't complain that you have to review it :)
I never asked for this! ;)
Quoted 6 lines
The series implements the syscall enter/exit and the general exit to userspace work handling along with the pre guest enter functionality.
The series converts x86 and ARM64. x86 is fully tested including selftests etc. ARM64 is only compile tested for now as my only ARM64 testbox is not available right now.
I've been working on converting the arm64 entry code to C for a while now [1], gradually upstreaming the bits I can.
James has picked up some of that [2] as a prerequisite for some RAS error handling, and I think building the arm64 bits atop of that would be preferable. IIUC that should get posted as a series come -rc1.
Since there's immense scope for subtle breakage, I'd prefer that we do the arm64-specific asm->C conversion before migrating arm64 to generic code. That way us arm64 folk can ensure the asm->C conversion retains the existing behaviour, and it'll be easier for everyone to compare the arm64 and generic C implementations.
Thanks,
Mark.
[1] git://git.kernel.org/pub/scm/linux/kernel/git/mark/linux.git
[2] git://linux-arm.org/linux-jm.git -b deasm_sync_only/v1
Quoted 31 lines
Thanks,
tglx
--- /Makefile | 3 arch/Kconfig | 3 arch/arm64/Kconfig | 1 arch/arm64/include/asm/kvm_host.h | 1 arch/arm64/kernel/entry.S | 18 - arch/arm64/kernel/ptrace.c | 65 ------ arch/arm64/kernel/signal.c | 45 ---- arch/arm64/kernel/syscall.c | 49 ---- arch/x86/Kconfig | 1 arch/x86/entry/common.c | 265 +------------------------- arch/x86/entry/entry_32.S | 13 - arch/x86/entry/entry_64.S | 12 - arch/x86/entry/entry_64_compat.S | 21 -- arch/x86/include/asm/signal.h | 1 arch/x86/include/asm/thread_info.h | 9 arch/x86/kernel/signal.c | 2 arch/x86/kvm/x86.c | 17 - b/arch/arm64/include/asm/entry-common.h | 76 +++++++ b/arch/x86/include/asm/entry-common.h | 104 ++++++++++ b/include/linux/entry-common.h | 324 ++++++++++++++++++++++++++++++++ b/kernel/entry/common.c | 220 +++++++++++++++++++++ kernel/Makefile | 1 22 files changed, 776 insertions(+), 475 deletions(-)
Quoted 7 lines · Mark Rutland
I've been working on converting the arm64 entry code to C for a while now [1], gradually upstreaming the bits I can.
James has picked up some of that [2] as a prerequisite for some RAS error handling, and I think building the arm64 bits atop of that would be preferable. IIUC that should get posted as a series come -rc1.
Quoted 5 lines
Since there's immense scope for subtle breakage, I'd prefer that we do the arm64-specific
asm->Cconversion before migrating arm64 to generic code. That way us arm64 folk can ensure theasm->Cconversion retains the existing behaviour, and it'll be easier for everyone to compare the arm64 and generic C implementations.
Right. It still would be nice to have some feedback on the general approach.
That sais I'm happy to let you screw your entry code up yourself :)
Thanks
tglx
Andy Lutomirski
maintainerQuoted 10 lines · Thomas Gleixner
When working on a way to move out the posix cpu timer expiry out of the timer interrupt context, I noticed that KVM is not handling pending task work before entering a guest. A quick hack was to add that to the x86 KVM handling loop. The discussion ended with a request to make this a generic infrastructure possible with also moving the per arch implementations of the enter from and return to user space handling generic.
Quoted 8 lines
You asked for it, so don't complain that you have to review it :)
The series implements the syscall enter/exit and the general exit to userspace work handling along with the pre guest enter functionality.
The series converts x86 and ARM64. x86 is fully tested including selftests etc. ARM64 is only compile tested for now as my only ARM64 testbox is not available right now.
Other than the comments I sent so far, I like this series.
Quoted 9 lines · Thomas Gleixner
When working on a way to move out the posix cpu timer expiry out of the timer interrupt context, I noticed that KVM is not handling pending task work before entering a guest. A quick hack was to add that to the x86 KVM handling loop. The discussion ended with a request to make this a generic infrastructure possible with also moving the per arch implementations of the enter from and return to user space handling generic.
Quoted 8 lines
You asked for it, so don't complain that you have to review it :)
The series implements the syscall enter/exit and the general exit to userspace work handling along with the pre guest enter functionality.
The series converts x86 and ARM64. x86 is fully tested including selftests etc. ARM64 is only compile tested for now as my only ARM64 testbox is not available right now.
It seems that s390x would also need to look into TIF_NOTIFY_PENDING before entering a KVM guest. Given that the s390x entry path is still in assembler this might not be something to do quickly.
Would it make sense to actually start with a minimal solution (e.g. one that provides notify_resume_pending like your original patch) as a fix. That would also be simple to backport. And then we can do the proper rework on top.
Or do we consider anything that depends on TIF_NOTIFY_PENDING before entering a guest as not important enough for stable?
After all the vcpu_run ioctl almost never returns to userspace and nothing was obviously broken.
Another question: Are there callbacks due to TIF_NOTIFY_PENDING that should NOT happen as long as we stay in the vpcu loop?