V3 available here:
Link: https://lore.kernel.org/all/4C3A8815-67FF-41EB-A703-981920CA1201@linux.ibm.com/T/
Implement a syscall wrapper, causing arguments to handlers to be passed
via a struct pt_regs on the stack. The syscall wrapper is implemented
for all platforms other than the Cell processor, from which SPUs expect
the ability to directly call syscall handler symbols with the regular
in-register calling convention.
Adopting syscall wrappers requires redefinition of architecture-specific
syscalls and compatibility syscalls to use the SYSCALL_DEFINE and
COMPAT_SYSCALL_DEFINE macros, as well as removal of direct-references to
the emitted syscall-handler symbols from within the kernel. This work
lead to the following modernisations of powerpc's syscall handlers:
- Replace syscall 82 semantics with sys_old_select and remove
ppc_select handler, which features direct call to both sys_old_select
and sys_select.
- Use a generic fallocate compatibility syscall
Replace asm implementation of syscall table with C implementation for
more compile-time checks.
Many compatibility syscalls are candidates to be removed in favour of
generically defined handlers, but exhibit different parameter orderings
and numberings due to 32-bit ABI support for 64-bit parameters. The
parameter reorderings are however consistent with arm. A future patch
series will serve to modernise syscalls by providing generic
implementations featuring these reorderings.
The design of this syscall is very similar to the s390, x86 and arm64
implementations. See also Commit 4378a7d4be30 (arm64: implement syscall wrappers).
The motivation for this change is that it allows for the clearing of
register state when entering the kernel via through interrupt handlers
on 64-bit servers. This serves to reduce the influence of values in
registers carried over from the interrupted process, e.g. syscall
parameters from user space, or user state at the site of a pagefault.
All values in registers are saved and zeroized at the entry to an
interrupt handler and restored afterward. While this may sound like a
heavy-weight mitigation, many gprs are already saved and restored on
handling of an interrupt, and the mmap_bench benchmark on Power 9 guest,
repeatedly invoking the pagefault handler suggests at most ~0.8%
regression in performance. Realistic workloads are not constantly
producing interrupts, and so this does not indicate realistic slowdown.
Using wrapped syscalls yields to a performance improvement of ~5.6% on
the null_syscall benchmark on pseries guests, by removing the need for
system_call_exception to allocate its own stack frame. This amortises
the additional costs of saving and restoring non-volatile registers
(register clearing is cheap on super scalar platforms), and so the
final mitigation actually yields a net performance improvement of ~0.6%
on the null_syscall benchmark.
Patch Changelog:
- Fix instances where NULLIFY_GPRS were still present
- Minimise unrecoverable windows in entry_32.S between SRR0/1 restores
and RFI
- Remove all references to syscall symbols prior to introducing syscall
wrapper.
- Remove unnecessary duplication of syscall handlers with sys_... and
powerpc_sys_... symbols.
- Clear non-volatile registers on Book3E systems, as some of these
systems feature hardware speculation, and we already unconditionally
restore NVGPRS.
Rohan McLure (20):
powerpc: Remove asmlinkage from syscall handler definitions
powerpc: Use generic fallocate compatibility syscall
powerpc/32: Remove powerpc select specialisation
powerpc: Provide do_ppc64_personality helper
powerpc: Remove direct call to personality syscall handler
powerpc: Remove direct call to mmap2 syscall handlers
powerpc: Adopt SYSCALL_DEFINE for arch-specific syscall handlers
powerpc: Include all arch-specific syscall prototypes
powerpc: Enable compile-time check for syscall handlers
powerpc: Use common syscall handler type
powerpc: Add ZEROIZE_GPRS macros for register clears
Revert "powerpc/syscall: Save r3 in regs->orig_r3"
powerpc: Provide syscall wrapper
powerpc/64s: Clear/restore caller gprs in syscall interrupt/return
powerpc/64s: Use {ZEROIZE,SAVE,REST}_GPRS macros in sc, scv 0 handlers
powerpc/32: Clarify interrupt restores with REST_GPR macro in
entry_32.S
powerpc/64e: Clarify register saves and clears with
{SAVE,ZEROIZE}_GPRS
powerpc/64s: Fix comment on interrupt handler prologue
powerpc/64s: Clear gprs on interrupt routine entry in Book3S
powerpc/64e: Clear gprs on interrupt routine entry
arch/powerpc/Kconfig | 1 +
arch/powerpc/include/asm/compat.h | 5 +
arch/powerpc/include/asm/interrupt.h | 3 +-
arch/powerpc/include/asm/ppc_asm.h | 22 +++
arch/powerpc/include/asm/syscall.h | 11 +-
arch/powerpc/include/asm/syscall_wrapper.h | 84 +++++++++++
arch/powerpc/include/asm/syscalls.h | 128 +++++++++++++----
.../ppc32.h => include/asm/syscalls_32.h} | 0
arch/powerpc/include/asm/unistd.h | 1 +
arch/powerpc/kernel/entry_32.S | 40 +++---
arch/powerpc/kernel/exceptions-64e.S | 31 ++--
arch/powerpc/kernel/exceptions-64s.S | 25 ++--
arch/powerpc/kernel/interrupt_64.S | 92 +++++-------
arch/powerpc/kernel/signal_32.c | 2 +-
arch/powerpc/kernel/sys_ppc32.c | 54 ++++---
arch/powerpc/kernel/syscall.c | 32 ++---
arch/powerpc/kernel/syscalls.c | 51 ++++---
arch/powerpc/kernel/syscalls/syscall.tbl | 24 ++--
arch/powerpc/kernel/{systbl.S => systbl.c} | 29 ++--
arch/powerpc/kernel/vdso.c | 6 +-
arch/powerpc/perf/callchain_32.c | 2 +-
arch/powerpc/platforms/cell/spu_callbacks.c | 6 +-
.../arch/powerpc/entry/syscalls/syscall.tbl | 24 ++--
23 files changed, 415 insertions(+), 258 deletions(-)
create mode 100644 arch/powerpc/include/asm/syscall_wrapper.h
rename arch/powerpc/{kernel/ppc32.h => include/asm/syscalls_32.h} (100%)
rename arch/powerpc/kernel/{systbl.S => systbl.c} (55%)
--
2.34.1
The powerpc fallocate compat syscall handler is identical to the
generic implementation provided by commit 59c10c52f573f ("riscv:
compat: syscall: Add compat_sys_call_table implementation"), and as
such can be removed in favour of the generic implementation.
A future patch series will replace more architecture-defined syscall
handlers with generic implementations, dependent on introducing generic
implementations that are compatible with powerpc and arm's parameter
reorderings.
Reported-by: Arnd Bergmann <arnd@arndb.de>
Signed-off-by: Rohan McLure <redacted>
---
V1 -> V2: Remove arch-specific fallocate handler.
V2 -> V3: Remove generic fallocate prototype. Move to beginning of
series.
---
arch/powerpc/include/asm/compat.h | 5 +++++
arch/powerpc/include/asm/syscalls.h | 2 --
arch/powerpc/include/asm/unistd.h | 1 +
3 files changed, 6 insertions(+), 2 deletions(-)
The asmlinkage macro has no special meaning in powerpc, and prior to
this patch is used sporadically on some syscall handler definitions. On
architectures that do not define asmlinkage, it resolves to extern "C"
for C++ compilers and a nop otherwise. The current invocations of
asmlinkage provide far from complete support for C++ toolchains, and so
the macro serves no purpose in powerpc.
Remove all invocations of asmlinkage in arch/powerpc. These incidentally
only occur in syscall definitions and prototypes.
Signed-off-by: Rohan McLure <redacted>
---
V2 -> V3: new patch
---
arch/powerpc/include/asm/syscalls.h | 16 ++++++++--------
arch/powerpc/kernel/sys_ppc32.c | 8 ++++----
2 files changed, 12 insertions(+), 12 deletions(-)
Avoid duplication in future patch that will define the ppc64_personality
syscall handler in terms of the SYSCALL_DEFINE and COMPAT_SYSCALL_DEFINE
macros, by extracting the common body of ppc64_personality into a helper
function.
Signed-off-by: Rohan McLure <redacted>
---
V2 -> V3: New commit.
---
arch/powerpc/kernel/syscalls.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
@@ -76,6 +76,10 @@ long ppc64_personality(unsigned long personality)ret=(ret&~PER_MASK)|PER_LINUX;returnret;}+longppc64_personality(unsignedlongpersonality)+{+returndo_ppc64_personality(personality);+}#endiflongppc_fadvise64_64(intfd,intadvice,u32offset_high,u32offset_low,
Syscall #82 has been implemented for 32-bit platforms in a unique way on
powerpc systems. This hack will in effect guess whether the caller is
expecting new select semantics or old select semantics. It does so via a
guess, based off the first parameter. In new select, this parameter
represents the length of a user-memory array of file descriptors, and in
old select this is a pointer to an arguments structure.
The heuristic simply interprets sufficiently large values of its first
parameter as being a call to old select. The following is a discussion
on how this syscall should be handled.
Link: https://lore.kernel.org/lkml/13737de5-0eb7-e881-9af0-163b0d29a1a0@csgroup.eu/
As discussed in this thread, the existence of such a hack suggests that for
whatever powerpc binaries may predate glibc, it is most likely that they
would have taken use of the old select semantics. x86 and arm64 both
implement this syscall with oldselect semantics.
Remove the powerpc implementation, and update syscall.tbl to refer to emit
a reference to sys_old_select for 32-bit binaries, in keeping with how
other architectures support syscall #82.
Signed-off-by: Rohan McLure <redacted>
---
V1 -> V2: Remove arch-specific select handler
V2 -> V3: Remove ppc_old_select prototype in <asm/syscalls.h>. Move to
earlier in patch series
---
arch/powerpc/include/asm/syscalls.h | 2 --
arch/powerpc/kernel/syscalls.c | 17 -----------------
arch/powerpc/kernel/syscalls/syscall.tbl | 2 +-
.../arch/powerpc/entry/syscalls/syscall.tbl | 2 +-
4 files changed, 2 insertions(+), 21 deletions(-)
@@ -18,8 +18,6 @@ long sys_mmap2(unsigned long addr, size_t len,unsignedlongfd,unsignedlongpgoff);longppc64_personality(unsignedlongpersonality);longsys_rtas(structrtas_args__user*uargs);-intppc_select(intn,fd_set__user*inp,fd_set__user*outp,-fd_set__user*exp,struct__kernel_old_timeval__user*tvp);longppc_fadvise64_64(intfd,intadvice,u32offset_high,u32offset_low,u32len_high,u32len_low);
Syscall handlers should not be invoked internally by their symbol names,
as these symbols defined by the architecture-defined SYSCALL_DEFINE
macro. Fortunately, in the case of ppc64_personality, its call to
sys_personality can be replaced with an invocation to the
equivalent ksys_personality inline helper in <linux/syscalls.h>.
Signed-off-by: Rohan McLure <redacted>
---
V1 -> V2: Use inline helper to deduplicate bodies in compat/regular
implementations.
V3 -> V4: Move to be applied before syscall wrapper.
---
arch/powerpc/kernel/syscalls.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
@@ -71,7 +71,7 @@ static inline long do_ppc64_personality(unsigned long personality)if(personality(current->personality)==PER_LINUX32&&personality(personality)==PER_LINUX)personality=(personality&~PER_MASK)|PER_LINUX32;-ret=sys_personality(personality);+ret=ksys_personality(personality);if(personality(ret)==PER_LINUX32)ret=(ret&~PER_MASK)|PER_LINUX;returnret;
Syscall handlers should not be invoked internally by their symbol names,
as these symbols defined by the architecture-defined SYSCALL_DEFINE
macro. Move the compatibility syscall definition for mmap2 to
syscalls.c, so that all mmap implementations can share an inline helper
function, as is done with the personality handlers.
Signed-off-by: Rohan McLure <redacted>
---
V1 -> V2: Move mmap2 compat implementation to asm/kernel/syscalls.c.
V3 -> V4: Move to be applied before syscall wrapper introduced.
---
arch/powerpc/kernel/sys_ppc32.c | 9 ---------
arch/powerpc/kernel/syscalls.c | 11 +++++++++++
2 files changed, 11 insertions(+), 9 deletions(-)
@@ -48,14 +47,6 @@#include<asm/syscalls.h>#include<asm/switch_to.h>-unsignedlongcompat_sys_mmap2(unsignedlongaddr,size_tlen,-unsignedlongprot,unsignedlongflags,-unsignedlongfd,unsignedlongpgoff)-{-/* This should remain 12 even if PAGE_SIZE changes */-returnsys_mmap(addr,len,prot,flags,fd,pgoff<<12);-}-/* *longlongmunging:*The32bitABIpasseslonglongsinanoddevenregisterpair.
@@ -56,6 +56,17 @@ SYSCALL_DEFINE6(mmap2, unsigned long, addr, size_t, len,returndo_mmap2(addr,len,prot,flags,fd,pgoff,PAGE_SHIFT-12);}+#ifdef CONFIG_COMPAT+COMPAT_SYSCALL_DEFINE6(mmap2,+unsignedlong,addr,size_t,len,+unsignedlong,prot,unsignedlong,flags,+unsignedlong,fd,unsignedlong,pgoff)+{+/* This should remain 12 even if PAGE_SIZE changes */+returndo_mmap2(addr,len,prot,flags,fd,pgoff<<12,PAGE_SHIFT-12);+}+#endif+SYSCALL_DEFINE6(mmap,unsignedlong,addr,size_t,len,unsignedlong,prot,unsignedlong,flags,unsignedlong,fd,off_t,offset)
Arch-specific implementations of syscall handlers are currently used
over generic implementations for the following reasons:
1. Semantics unique to powerpc
2. Compatibility syscalls require 'argument padding' to comply with
64-bit argument convention in ELF32 abi.
3. Parameter types or order is different in other architectures.
These syscall handlers have been defined prior to this patch series
without invoking the SYSCALL_DEFINE or COMPAT_SYSCALL_DEFINE macros with
custom input and output types. We remove every such direct definition in
favour of the aforementioned macros.
Also update syscalls.tbl in order to refer to the symbol names generated
by each of these macros. Since ppc64_personality can be called by both
64 bit and 32 bit binaries through compatibility, we must generate both
both compat_sys_ and sys_ symbols for this handler.
A number of architectures including arm and powerpc agree on an
alternative argument order and numbering for most of these arch-specific
handlers. A future patch series may allow for asm/unistd.h to signal
through its defines that a generic implementation of these syscall
handlers with the correct calling convention be omitted, through the
__ARCH_WANT_COMPAT_SYS_... convention.
Signed-off-by: Rohan McLure <redacted>
---
V1 -> V2: All syscall handlers wrapped by this macro.
V2 -> V3: Move creation of do_ppc64_personality helper to prior patch.
V3 -> V4: Fix parenthesis alignment. Don't emit sys_*** symbols.
---
arch/powerpc/include/asm/syscalls.h | 10 ++--
arch/powerpc/kernel/sys_ppc32.c | 45 ++++++++++--------
arch/powerpc/kernel/syscalls.c | 17 +++++--
arch/powerpc/kernel/syscalls/syscall.tbl | 22 ++++-----
.../arch/powerpc/entry/syscalls/syscall.tbl | 22 ++++-----
5 files changed, 64 insertions(+), 52 deletions(-)
Forward declare all syscall handler prototypes where a generic prototype
is not provided in either linux/syscalls.h or linux/compat.h in
asm/syscalls.h. This is required for compile-time type-checking for
syscall handlers, which is implemented later in this series.
32-bit compatibility syscall handlers are expressed in terms of types in
ppc32.h. Expose this header globally.
Signed-off-by: Rohan McLure <redacted>
---
V1 -> V2: Explicitly include prototypes.
V2 -> V3: Remove extraneous #include <asm/compat.h> and ppc_fallocate
prototype. Rename header.
---
arch/powerpc/include/asm/syscalls.h | 90 +++++++++++++-----
.../ppc32.h => include/asm/syscalls_32.h} | 0
arch/powerpc/kernel/signal_32.c | 2 +-
arch/powerpc/perf/callchain_32.c | 2 +-
4 files changed, 70 insertions(+), 24 deletions(-)
Cause syscall handlers to be typed as follows when called indirectly
throughout the kernel.
typedef long (*syscall_fn)(unsigned long, unsigned long, unsigned long,
unsigned long, unsigned long, unsigned long);
Since both 32 and 64-bit abis allow for at least the first six
machine-word length parameters to a function to be passed by registers,
even handlers which admit fewer than six parameters may be viewed as
having the above type.
Fixup comparisons in VDSO to avoid pointer-integer comparison. Introduce
explicit cast on systems with SPUs.
Signed-off-by: Rohan McLure <redacted>
---
V1 -> V2: New patch.
V2 -> V3: Remove unnecessary cast from const syscall_fn to syscall_fn
---
arch/powerpc/include/asm/syscall.h | 7 +++++--
arch/powerpc/include/asm/syscalls.h | 1 +
arch/powerpc/kernel/systbl.c | 6 +++---
arch/powerpc/kernel/vdso.c | 4 ++--
arch/powerpc/platforms/cell/spu_callbacks.c | 6 +++---
5 files changed, 14 insertions(+), 10 deletions(-)
The table of syscall handlers and registered compatibility syscall
handlers has in past been produced using assembly, with function
references resolved at link time. This moves link-time errors to
compile-time, by rewriting systbl.S in C, and including the
linux/syscalls.h, linux/compat.h and asm/syscalls.h headers for
prototypes.
Reported-by: Arnd Bergmann <arnd@arndb.de>
Signed-off-by: Rohan McLure <redacted>
---
V1 -> V2: New patch.
---
arch/powerpc/kernel/{systbl.S => systbl.c} | 27 ++++++++++----------
1 file changed, 14 insertions(+), 13 deletions(-)
diff --git a/arch/powerpc/kernel/systbl.S b/arch/powerpc/kernel/systbl.csimilarity index 59%rename from arch/powerpc/kernel/systbl.Srename to arch/powerpc/kernel/systbl.cindex cb3358886203..99ffdfef6b9c 100644--- a/arch/powerpc/kernel/systbl.S+++ b/arch/powerpc/kernel/systbl.c
Macros for restoring and saving registers to and from the stack exist.
Provide macros with the same interface for clearing a range of gprs by
setting each register's value in that range to zero.
The resulting macros are called ZEROIZE_GPRS and ZEROIZE_NVGPRS, keeping
with the naming of the accompanying restore and save macros, and usage
of zeroize to describe this operation elsewhere in the kernel.
Signed-off-by: Rohan McLure <redacted>
---
V1 -> V2: Change 'ZERO' usage in naming to 'NULLIFY', a more obvious verb
V2 -> V3: Change 'NULLIFY' usage in naming to 'ZEROIZE', which has
precedent in kernel and explicitly specifies that we are zeroing.
V3 -> V4: Update commit message to use zeroize.
---
arch/powerpc/include/asm/ppc_asm.h | 22 ++++++++++++++++++++++
1 file changed, 22 insertions(+)
This reverts commit 8875f47b7681aa4e4484a9b612577b044725f839.
Save caller's original r3 state to the kernel stackframe before entering
system_call_exception. This allows for user registers to be cleared by
the time system_call_exception is entered, reducing the influence of
user registers on speculation within the kernel.
Prior to this commit, orig_r3 was saved at the beginning of
system_call_exception. Instead, save orig_r3 while the user value is
still live in r3.
Also replicate this early save in 32-bit. A similar save was removed in
commit 6f76a01173cc ("powerpc/syscall: implement system call entry/exit logic in C for PPC32")
when 32-bit adopted system_call_exception. Revert its removal of orig_r3
saves.
Signed-off-by: Rohan McLure <redacted>
---
V2 -> V3: New commit.
---
arch/powerpc/kernel/entry_32.S | 1 +
arch/powerpc/kernel/interrupt_64.S | 2 ++
arch/powerpc/kernel/syscall.c | 1 -
3 files changed, 3 insertions(+), 1 deletion(-)
@@ -25,7 +25,6 @@ notrace long system_call_exception(long r3, long r4, long r5,kuap_lock();add_random_kstack_offset();-regs->orig_gpr3=r3;if(IS_ENABLED(CONFIG_PPC_IRQ_SOFT_MASK_DEBUG))BUG_ON(irq_soft_mask_return()!=IRQS_ALL_DISABLED);
Implement syscall wrapper as per s390, x86, arm64. When enabled
cause handlers to accept parameters from a stack frame rather than
from user scratch register state. This allows for user registers to be
safely cleared in order to reduce caller influence on speculation
within syscall routine. The wrapper is a macro that emits syscall
handler symbols that call into the target handler, obtaining its
parameters from a struct pt_regs on the stack.
As registers are already saved to the stack prior to calling
system_call_exception, it appears that this function is executed more
efficiently with the new stack-pointer convention than with parameters
passed by registers, avoiding the allocation of a stack frame for this
method. On a 32-bit system, we see >20% performance increases on the
null_syscall microbenchmark, and on a Power 8 the performance gains
amortise the cost of clearing and restoring registers which is
implemented at the end of this series, seeing final result of ~5.6%
performance improvement on null_syscall.
Syscalls are wrapped in this fashion on all platforms except for the
Cell processor as this commit does not provide SPU support. This can be
quickly fixed in a successive patch, but requires spu_sys_callback to
allocate a pt_regs structure to satisfy the wrapped calling convention.
Co-developed-by: Andrew Donnellan <redacted>
Signed-off-by: Andrew Donnellan <redacted>
Signed-off-by: Rohan McLure <redacted>
---
V1 -> V2: Generate prototypes for symbols produced by the wrapper.
V2 -> V3: Rebased to remove conflict with 1547db7d1f44
("powerpc: Move system_call_exception() to syscall.c"). Also remove copy
from gpr3 save slot on stackframe to orig_r3's slot. Fix whitespace with
preprocessor defines in system_call_exception.
---
arch/powerpc/Kconfig | 1 +
arch/powerpc/include/asm/interrupt.h | 3 +-
arch/powerpc/include/asm/syscall.h | 4 +
arch/powerpc/include/asm/syscall_wrapper.h | 84 ++++++++++++++++++++
arch/powerpc/include/asm/syscalls.h | 25 +++++-
arch/powerpc/kernel/entry_32.S | 6 +-
arch/powerpc/kernel/interrupt_64.S | 16 ++--
arch/powerpc/kernel/syscall.c | 31 +++-----
arch/powerpc/kernel/systbl.c | 2 +
arch/powerpc/kernel/vdso.c | 2 +
10 files changed, 142 insertions(+), 32 deletions(-)
@@ -12,12 +12,8 @@#include<asm/unistd.h>-typedeflong(*syscall_fn)(long,long,long,long,long,long);-/* Has to run notrace because it is entered not completely "reconciled" */-notracelongsystem_call_exception(longr3,longr4,longr5,-longr6,longr7,longr8,-unsignedlongr0,structpt_regs*regs)+notracelongsystem_call_exception(unsignedlongr0,structpt_regs*regs){longret;syscall_fnf;
@@ -138,12 +134,6 @@ notrace long system_call_exception(long r3, long r4, long r5,r0=do_syscall_trace_enter(regs);if(unlikely(r0>=NR_syscalls))returnregs->gpr[3];-r3=regs->gpr[3];-r4=regs->gpr[4];-r5=regs->gpr[5];-r6=regs->gpr[6];-r7=regs->gpr[7];-r8=regs->gpr[8];}elseif(unlikely(r0>=NR_syscalls)){if(unlikely(trap_is_unsupported_scv(regs))){
@@ -160,18 +150,23 @@ notrace long system_call_exception(long r3, long r4, long r5,if(unlikely(is_compat_task())){f=(void*)compat_sys_call_table[r0];-r3&=0x00000000ffffffffULL;-r4&=0x00000000ffffffffULL;-r5&=0x00000000ffffffffULL;-r6&=0x00000000ffffffffULL;-r7&=0x00000000ffffffffULL;-r8&=0x00000000ffffffffULL;+regs->gpr[3]&=0x00000000ffffffffULL;+regs->gpr[4]&=0x00000000ffffffffULL;+regs->gpr[5]&=0x00000000ffffffffULL;+regs->gpr[6]&=0x00000000ffffffffULL;+regs->gpr[7]&=0x00000000ffffffffULL;+regs->gpr[8]&=0x00000000ffffffffULL;}else{f=(void*)sys_call_table[r0];}-ret=f(r3,r4,r5,r6,r7,r8);+#ifdef CONFIG_ARCH_HAS_SYSCALL_WRAPPER+ret=f(regs);+#else+ret=f(regs->gpr[3],regs->gpr[4],regs->gpr[5],+regs->gpr[6],regs->gpr[7],regs->gpr[8]);+#endif/**Ultimately,thisvaluewillgetlimitedbyKSTACK_OFFSET_MAX(),
Use the convenience macros for saving/clearing/restoring gprs in keeping
with syscall calling conventions. The plural variants of these macros
can store a range of registers for concision.
This works well when the user gpr value we are hoping to save is still
live. In the syscall interrupt handlers, user register state is
sometimes juggled between registers. Hold-off from issuing the SAVE_GPR
macro for applicable neighbouring lines to highlight the delicate
register save logic.
Signed-off-by: Rohan McLure <redacted>
---
V1 -> V2: Update summary
V2 -> V3: Update summary regarding exclusions for the SAVE_GPR marco.
Acknowledge new name for ZEROIZE_GPR{,S} macros.
---
arch/powerpc/kernel/interrupt_64.S | 43 ++++++----------------------
1 file changed, 9 insertions(+), 34 deletions(-)
The common interrupt handler prologue macro and the bad_stack
trampolines include consecutive sequences of register saves, and some
register clears. Neaten such instances by expanding use of the SAVE_GPRS
macro and employing the ZEROIZE_GPR macro when appropriate.
Also simplify an invocation of SAVE_GPRS targetting all non-volatile
registers to SAVE_NVGPRS.
Signed-off-by: Rohan Mclure <redacted>
---
V3 -> V4: New commit.
---
arch/powerpc/kernel/exceptions-64e.S | 27 +++++++++++---------------
1 file changed, 11 insertions(+), 16 deletions(-)
Restoring the register state of the interrupted thread involves issuing
a large number of predictable loads to the kernel stack frame. Issue the
REST_GPR{,S} macros to clearly signal when this is happening, and bunch
together restores at the end of the interrupt handler where the saved
value is not consumed earlier in the handler code.
Signed-off-by: Rohan McLure <redacted>
Reported-by: Christophe Leroy <redacted>
---
V2 -> V3: New patch.
V3 -> V4: Minimise restores in the unrecoverable window between
restoring SRR0/1 and return from interrupt.
---
arch/powerpc/kernel/entry_32.S | 33 +++++++++++++-------------------
1 file changed, 13 insertions(+), 20 deletions(-)
Interrupt handlers on 64s systems will often need to save register state
from the interrupted process to make space for loading special purpose
registers or for internal state.
Fix a comment documenting a common code path macro in the beginning of
interrupt handlers where r10 is saved to the PACA to afford space for
the value of the CFAR. Comment is currently written as if r10-r12 are
saved to PACA, but in fact only r10 is saved, with r11-r12 saved much
later. The distance in code between these saves has grown over the many
revisions of this macro. Fix this by signalling with a comment where
r11-r12 are saved to the PACA.
Signed-off-by: Rohan McLure <redacted>
---
V1 -> V2: Given its own commit
V2 -> V3: Annotate r11-r12 save locations with comment.
---
arch/powerpc/kernel/exceptions-64s.S | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
Clear user state in gprs (assign to zero) to reduce the influence of user
registers on speculation within kernel syscall handlers. Clears occur
at the very beginning of the sc and scv 0 interrupt handlers, with
restores occurring following the execution of the syscall handler.
Signed-off-by: Rohan McLure <redacted>
---
V1 -> V2: Update summary
V2 -> V3: Remove erroneous summary paragraph on syscall_exit_prepare
V3 -> V4: Use ZEROIZE instead of NULLIFY
---
arch/powerpc/kernel/interrupt_64.S | 22 ++++++++++++++++++----
1 file changed, 18 insertions(+), 4 deletions(-)
Zero GPRS r14-r31 on entry into the kernel for interrupt sources to
limit influence of user-space values in potential speculation gadgets.
Prior to this commit, all other GPRS are reassigned during the common
prologue to interrupt handlers and so need not be zeroised explicitly.
This may be done safely, without loss of register state prior to the
interrupt, as the common prologue saves the initial values of
non-volatiles, which are unconditionally restored in interrupt_64.S.
Signed-off-by: Rohan McLure <redacted>
---
V3 -> V4: New patch.
---
arch/powerpc/kernel/exceptions-64e.S | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
Zero GPRS r0, r2-r11, r14-r31, on entry into the kernel for all
other interrupt sources to limit influence of user-space values
in potential speculation gadgets. The remaining gprs are overwritten by
entry macros to interrupt handlers, irrespective of whether or not a
given handler consumes these register values.
Prior to this commit, r14-r31 are restored on a per-interrupt basis at
exit, but now they are always restored. Remove explicit REST_NVGPRS
invocations as non-volatiles must now always be restored. 32-bit systems
do not clear user registers on interrupt, and continue to depend on the
return value of interrupt_exit_user_prepare to determine whether or not
to restore non-volatiles.
The mmap_bench benchmark in selftests should rapidly invoke pagefaults.
See ~0.8% performance regression with this mitigation, but this
indicates the worst-case performance due to heavier-weight interrupt
handlers.
Signed-off-by: Rohan McLure <redacted>
---
V1 -> V2: Add benchmark data
V2 -> V3: Use ZEROIZE_GPR{,S} macro renames, clarify
interrupt_exit_user_prepare changes in summary.
---
arch/powerpc/kernel/exceptions-64s.S | 21 ++++++++-------------
arch/powerpc/kernel/interrupt_64.S | 9 ++-------
2 files changed, 10 insertions(+), 20 deletions(-)
@@ -2485,7 +2482,6 @@ EXC_COMMON_BEGIN(h_facility_unavailable_common)GEN_COMMONh_facility_unavailableaddir3,r1,STACK_FRAME_OVERHEADblfacility_unavailable_exception-REST_NVGPRS(r1)/*XXXShouldn't be necessary in practice */binterrupt_return_hsrr
@@ -432,9 +432,6 @@ interrupt_return_\srr\()_user: /* make backtraces match the _kernel variant */ _ASM_NOKPROBE_SYMBOL(interrupt_return_\srr\()_user)addir3,r1,STACK_FRAME_OVERHEADblinterrupt_exit_user_prepare-cmpdir3,0-bne-.Lrestore_nvgprs_\srr-.Lrestore_nvgprs_\srr\()_cont:stdr1,PACA_EXIT_SAVE_R1(r13)/*saver1forrestart*/#ifdef CONFIG_PPC_BOOK3S.Linterrupt_return_\srr\()_user_rst_start:
@@ -448,6 +445,7 @@ _ASM_NOKPROBE_SYMBOL(interrupt_return_\srr\()_user)stbr11,PACAIRQHAPPENED(r13)# clear out possible HARD_DIS.Lfast_user_interrupt_return_\srr\():+REST_NVGPRS(r1)#ifdef CONFIG_PPC_BOOK3S.ifc\srr,srrlbzr4,PACASRR_VALID(r13)
From: Andrew Donnellan <hidden> Date: 2022-08-25 07:05:10
On Wed, 2022-08-24 at 12:05 +1000, Rohan McLure wrote:
The asmlinkage macro has no special meaning in powerpc, and prior to
this patch is used sporadically on some syscall handler definitions.
On
architectures that do not define asmlinkage, it resolves to extern
"C"
for C++ compilers and a nop otherwise. The current invocations of
asmlinkage provide far from complete support for C++ toolchains, and
so
the macro serves no purpose in powerpc.
Remove all invocations of asmlinkage in arch/powerpc. These
incidentally
only occur in syscall definitions and prototypes.
Signed-off-by: Rohan McLure <redacted>
This does indeed get rid of every reference to asmlinkage in
arch/powerpc.
Reviewed-by: Andrew Donnellan <redacted>
struct rtas_args;
-asmlinkage long sys_mmap(unsigned long addr, size_t len,
- unsigned long prot, unsigned long flags,
- unsigned long fd, off_t offset);
-asmlinkage long sys_mmap2(unsigned long addr, size_t len,
- unsigned long prot, unsigned long flags,
- unsigned long fd, unsigned long pgoff);
-asmlinkage long ppc64_personality(unsigned long personality);
-asmlinkage long sys_rtas(struct rtas_args __user *uargs);
+long sys_mmap(unsigned long addr, size_t len,
+ unsigned long prot, unsigned long flags,
+ unsigned long fd, off_t offset);
+long sys_mmap2(unsigned long addr, size_t len,
+ unsigned long prot, unsigned long flags,
+ unsigned long fd, unsigned long pgoff);
+long ppc64_personality(unsigned long personality);
+long sys_rtas(struct rtas_args __user *uargs);
int ppc_select(int n, fd_set __user *inp, fd_set __user *outp,
fd_set __user *exp, struct __kernel_old_timeval __user
*tvp);
long ppc_fadvise64_64(int fd, int advice, u32 offset_high, u32
offset_low,
diff --git a/arch/powerpc/kernel/sys_ppc32.c
b/arch/powerpc/kernel/sys_ppc32.c
index 16ff0399a257..f4edcc9489fb 100644
Any comments for this revision? Hopefully these revisions address 32-bit and embedded systems appropriately.
Thanks,
Rohan
On 24 Aug 2022, at 12:05 pm, Rohan McLure [off-list ref] wrote:
V3 available here:
Link: https://lore.kernel.org/all/4C3A8815-67FF-41EB-A703-981920CA1201@linux.ibm.com/T/
Implement a syscall wrapper, causing arguments to handlers to be passed
via a struct pt_regs on the stack. The syscall wrapper is implemented
for all platforms other than the Cell processor, from which SPUs expect
the ability to directly call syscall handler symbols with the regular
in-register calling convention.
Adopting syscall wrappers requires redefinition of architecture-specific
syscalls and compatibility syscalls to use the SYSCALL_DEFINE and
COMPAT_SYSCALL_DEFINE macros, as well as removal of direct-references to
the emitted syscall-handler symbols from within the kernel. This work
lead to the following modernisations of powerpc's syscall handlers:
- Replace syscall 82 semantics with sys_old_select and remove
ppc_select handler, which features direct call to both sys_old_select
and sys_select.
- Use a generic fallocate compatibility syscall
Replace asm implementation of syscall table with C implementation for
more compile-time checks.
Many compatibility syscalls are candidates to be removed in favour of
generically defined handlers, but exhibit different parameter orderings
and numberings due to 32-bit ABI support for 64-bit parameters. The
parameter reorderings are however consistent with arm. A future patch
series will serve to modernise syscalls by providing generic
implementations featuring these reorderings.
The design of this syscall is very similar to the s390, x86 and arm64
implementations. See also Commit 4378a7d4be30 (arm64: implement syscall wrappers).
The motivation for this change is that it allows for the clearing of
register state when entering the kernel via through interrupt handlers
on 64-bit servers. This serves to reduce the influence of values in
registers carried over from the interrupted process, e.g. syscall
parameters from user space, or user state at the site of a pagefault.
All values in registers are saved and zeroized at the entry to an
interrupt handler and restored afterward. While this may sound like a
heavy-weight mitigation, many gprs are already saved and restored on
handling of an interrupt, and the mmap_bench benchmark on Power 9 guest,
repeatedly invoking the pagefault handler suggests at most ~0.8%
regression in performance. Realistic workloads are not constantly
producing interrupts, and so this does not indicate realistic slowdown.
Using wrapped syscalls yields to a performance improvement of ~5.6% on
the null_syscall benchmark on pseries guests, by removing the need for
system_call_exception to allocate its own stack frame. This amortises
the additional costs of saving and restoring non-volatile registers
(register clearing is cheap on super scalar platforms), and so the
final mitigation actually yields a net performance improvement of ~0.6%
on the null_syscall benchmark.
Patch Changelog:
- Fix instances where NULLIFY_GPRS were still present
- Minimise unrecoverable windows in entry_32.S between SRR0/1 restores
and RFI
- Remove all references to syscall symbols prior to introducing syscall
wrapper.
- Remove unnecessary duplication of syscall handlers with sys_... and
powerpc_sys_... symbols.
- Clear non-volatile registers on Book3E systems, as some of these
systems feature hardware speculation, and we already unconditionally
restore NVGPRS.
Rohan McLure (20):
powerpc: Remove asmlinkage from syscall handler definitions
powerpc: Use generic fallocate compatibility syscall
powerpc/32: Remove powerpc select specialisation
powerpc: Provide do_ppc64_personality helper
powerpc: Remove direct call to personality syscall handler
powerpc: Remove direct call to mmap2 syscall handlers
powerpc: Adopt SYSCALL_DEFINE for arch-specific syscall handlers
powerpc: Include all arch-specific syscall prototypes
powerpc: Enable compile-time check for syscall handlers
powerpc: Use common syscall handler type
powerpc: Add ZEROIZE_GPRS macros for register clears
Revert "powerpc/syscall: Save r3 in regs->orig_r3"
powerpc: Provide syscall wrapper
powerpc/64s: Clear/restore caller gprs in syscall interrupt/return
powerpc/64s: Use {ZEROIZE,SAVE,REST}_GPRS macros in sc, scv 0 handlers
powerpc/32: Clarify interrupt restores with REST_GPR macro in
entry_32.S
powerpc/64e: Clarify register saves and clears with
{SAVE,ZEROIZE}_GPRS
powerpc/64s: Fix comment on interrupt handler prologue
powerpc/64s: Clear gprs on interrupt routine entry in Book3S
powerpc/64e: Clear gprs on interrupt routine entry
arch/powerpc/Kconfig | 1 +
arch/powerpc/include/asm/compat.h | 5 +
arch/powerpc/include/asm/interrupt.h | 3 +-
arch/powerpc/include/asm/ppc_asm.h | 22 +++
arch/powerpc/include/asm/syscall.h | 11 +-
arch/powerpc/include/asm/syscall_wrapper.h | 84 +++++++++++
arch/powerpc/include/asm/syscalls.h | 128 +++++++++++++----
.../ppc32.h => include/asm/syscalls_32.h} | 0
arch/powerpc/include/asm/unistd.h | 1 +
arch/powerpc/kernel/entry_32.S | 40 +++---
arch/powerpc/kernel/exceptions-64e.S | 31 ++--
arch/powerpc/kernel/exceptions-64s.S | 25 ++--
arch/powerpc/kernel/interrupt_64.S | 92 +++++-------
arch/powerpc/kernel/signal_32.c | 2 +-
arch/powerpc/kernel/sys_ppc32.c | 54 ++++---
arch/powerpc/kernel/syscall.c | 32 ++---
arch/powerpc/kernel/syscalls.c | 51 ++++---
arch/powerpc/kernel/syscalls/syscall.tbl | 24 ++--
arch/powerpc/kernel/{systbl.S => systbl.c} | 29 ++--
arch/powerpc/kernel/vdso.c | 6 +-
arch/powerpc/perf/callchain_32.c | 2 +-
arch/powerpc/platforms/cell/spu_callbacks.c | 6 +-
.../arch/powerpc/entry/syscalls/syscall.tbl | 24 ++--
23 files changed, 415 insertions(+), 258 deletions(-)
create mode 100644 arch/powerpc/include/asm/syscall_wrapper.h
rename arch/powerpc/{kernel/ppc32.h => include/asm/syscalls_32.h} (100%)
rename arch/powerpc/kernel/{systbl.S => systbl.c} (55%)
--
2.34.1
On Wed Aug 24, 2022 at 12:05 PM AEST, Rohan McLure wrote:
The asmlinkage macro has no special meaning in powerpc, and prior to
this patch is used sporadically on some syscall handler definitions. On
architectures that do not define asmlinkage, it resolves to extern "C"
for C++ compilers and a nop otherwise. The current invocations of
asmlinkage provide far from complete support for C++ toolchains, and so
the macro serves no purpose in powerpc.
Remove all invocations of asmlinkage in arch/powerpc. These incidentally
only occur in syscall definitions and prototypes.
Signed-off-by: Rohan McLure <redacted>
On Wed Aug 24, 2022 at 12:05 PM AEST, Rohan McLure wrote:
quoted hunk
The powerpc fallocate compat syscall handler is identical to the
generic implementation provided by commit 59c10c52f573f ("riscv:
compat: syscall: Add compat_sys_call_table implementation"), and as
such can be removed in favour of the generic implementation.
A future patch series will replace more architecture-defined syscall
handlers with generic implementations, dependent on introducing generic
implementations that are compatible with powerpc and arm's parameter
reorderings.
Reported-by: Arnd Bergmann <arnd@arndb.de>
Signed-off-by: Rohan McLure <redacted>
---
V1 -> V2: Remove arch-specific fallocate handler.
V2 -> V3: Remove generic fallocate prototype. Move to beginning of
series.
---
arch/powerpc/include/asm/compat.h | 5 +++++
arch/powerpc/include/asm/syscalls.h | 2 --
arch/powerpc/include/asm/unistd.h | 1 +
3 files changed, 6 insertions(+), 2 deletions(-)
Is there a reason not to put this in asm-generic/compat.h?
Possibly you want to put this with the other compat definitions and
above the asm-generic include. The generic header expects the arch to
include it after defining what it wants to override.
Not sure why x_lo gets cast from u32 to u64 and masked before the |
there, but generic code does the same so this isn't the place to
change it.
Thanks,
Nick
On Wed Aug 24, 2022 at 12:05 PM AEST, Rohan McLure wrote:
Syscall #82 has been implemented for 32-bit platforms in a unique way on
powerpc systems. This hack will in effect guess whether the caller is
expecting new select semantics or old select semantics. It does so via a
guess, based off the first parameter. In new select, this parameter
represents the length of a user-memory array of file descriptors, and in
old select this is a pointer to an arguments structure.
The heuristic simply interprets sufficiently large values of its first
parameter as being a call to old select. The following is a discussion
on how this syscall should be handled.
Link: https://lore.kernel.org/lkml/13737de5-0eb7-e881-9af0-163b0d29a1a0@csgroup.eu/
Seems okay to me, probably Christophe needs to ack it.
Should some of that history be included directly in this changelog?
Should ppc64 compat be added back too, if this is being updated instead
of removed? I don't know much about compat but it seems odd not provide
it (considering it's just using compat_sys_old_select, isn't it?
Reviewed-by: Nicholas Piggin <npiggin@gmail.com>
quoted hunk
As discussed in this thread, the existence of such a hack suggests that for
whatever powerpc binaries may predate glibc, it is most likely that they
would have taken use of the old select semantics. x86 and arm64 both
implement this syscall with oldselect semantics.
Remove the powerpc implementation, and update syscall.tbl to refer to emit
a reference to sys_old_select for 32-bit binaries, in keeping with how
other architectures support syscall #82.
Signed-off-by: Rohan McLure <redacted>
---
V1 -> V2: Remove arch-specific select handler
V2 -> V3: Remove ppc_old_select prototype in <asm/syscalls.h>. Move to
earlier in patch series
---
arch/powerpc/include/asm/syscalls.h | 2 --
arch/powerpc/kernel/syscalls.c | 17 -----------------
arch/powerpc/kernel/syscalls/syscall.tbl | 2 +-
.../arch/powerpc/entry/syscalls/syscall.tbl | 2 +-
4 files changed, 2 insertions(+), 21 deletions(-)
@@ -18,8 +18,6 @@ long sys_mmap2(unsigned long addr, size_t len,unsignedlongfd,unsignedlongpgoff);longppc64_personality(unsignedlongpersonality);longsys_rtas(structrtas_args__user*uargs);-intppc_select(intn,fd_set__user*inp,fd_set__user*outp,-fd_set__user*exp,struct__kernel_old_timeval__user*tvp);longppc_fadvise64_64(intfd,intadvice,u32offset_high,u32offset_low,u32len_high,u32len_low);
On Wed Aug 24, 2022 at 12:05 PM AEST, Rohan McLure wrote:
quoted hunk
Avoid duplication in future patch that will define the ppc64_personality
syscall handler in terms of the SYSCALL_DEFINE and COMPAT_SYSCALL_DEFINE
macros, by extracting the common body of ppc64_personality into a helper
function.
Signed-off-by: Rohan McLure <redacted>
---
V2 -> V3: New commit.
---
arch/powerpc/kernel/syscalls.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
If this was merged in patch 7 it would just include the above, right?
I don't really mind if you prefer to split it this way though and make
fewer changes in patch 7.
Could you drop the 'inline'? I don't think it's necessary for modern
compliers. do_mmap2 could drop it too.
I did have a question about the duplication in patch 7, but assuming
you still need this patch
Reviewed-by: Nicholas Piggin <npiggin@gmail.com>
long ppc_fadvise64_64(int fd, int advice, u32 offset_high, u32 offset_low,
--
2.34.1
On Wed Aug 24, 2022 at 12:05 PM AEST, Rohan McLure wrote:
Syscall handlers should not be invoked internally by their symbol names,
as these symbols defined by the architecture-defined SYSCALL_DEFINE
macro. Fortunately, in the case of ppc64_personality, its call to
sys_personality can be replaced with an invocation to the
equivalent ksys_personality inline helper in <linux/syscalls.h>.
Huh. I wonder why sys_personality doesn't just call ksys_personality
too. Anyway this looks good.
Reviewed-by: Nicholas Piggin <npiggin@gmail.com>
Several of your steps like this one look like they apply to other archs
as well. You might consider ccing linux-arch for some of these.
quoted hunk
Signed-off-by: Rohan McLure <redacted>
---
V1 -> V2: Use inline helper to deduplicate bodies in compat/regular
implementations.
V3 -> V4: Move to be applied before syscall wrapper.
---
arch/powerpc/kernel/syscalls.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
@@ -71,7 +71,7 @@ static inline long do_ppc64_personality(unsigned long personality)if(personality(current->personality)==PER_LINUX32&&personality(personality)==PER_LINUX)personality=(personality&~PER_MASK)|PER_LINUX32;-ret=sys_personality(personality);+ret=ksys_personality(personality);if(personality(ret)==PER_LINUX32)ret=(ret&~PER_MASK)|PER_LINUX;returnret;
On Wed Aug 24, 2022 at 12:05 PM AEST, Rohan McLure wrote:
Syscall handlers should not be invoked internally by their symbol names,
as these symbols defined by the architecture-defined SYSCALL_DEFINE
macro. Move the compatibility syscall definition for mmap2 to
syscalls.c, so that all mmap implementations can share an inline helper
function, as is done with the personality handlers.
Signed-off-by: Rohan McLure <redacted>
Is there any point to keping sys_ppc32.c at all? Might as well move them
all to syscall.c IMO.
Reviewed-by: Nicholas Piggin <npiggin@gmail.com>
quoted hunk
---
V1 -> V2: Move mmap2 compat implementation to asm/kernel/syscalls.c.
V3 -> V4: Move to be applied before syscall wrapper introduced.
---
arch/powerpc/kernel/sys_ppc32.c | 9 ---------
arch/powerpc/kernel/syscalls.c | 11 +++++++++++
2 files changed, 11 insertions(+), 9 deletions(-)
@@ -48,14 +47,6 @@#include<asm/syscalls.h>#include<asm/switch_to.h>-unsignedlongcompat_sys_mmap2(unsignedlongaddr,size_tlen,-unsignedlongprot,unsignedlongflags,-unsignedlongfd,unsignedlongpgoff)-{-/* This should remain 12 even if PAGE_SIZE changes */-returnsys_mmap(addr,len,prot,flags,fd,pgoff<<12);-}-/* *longlongmunging:*The32bitABIpasseslonglongsinanoddevenregisterpair.
@@ -56,6 +56,17 @@ SYSCALL_DEFINE6(mmap2, unsigned long, addr, size_t, len,returndo_mmap2(addr,len,prot,flags,fd,pgoff,PAGE_SHIFT-12);}+#ifdef CONFIG_COMPAT+COMPAT_SYSCALL_DEFINE6(mmap2,+unsignedlong,addr,size_t,len,+unsignedlong,prot,unsignedlong,flags,+unsignedlong,fd,unsignedlong,pgoff)+{+/* This should remain 12 even if PAGE_SIZE changes */+returndo_mmap2(addr,len,prot,flags,fd,pgoff<<12,PAGE_SHIFT-12);+}+#endif+SYSCALL_DEFINE6(mmap,unsignedlong,addr,size_t,len,unsignedlong,prot,unsignedlong,flags,unsignedlong,fd,off_t,offset)
On Mon, Sep 12, 2022, at 10:38 AM, Nicholas Piggin wrote:
On Wed Aug 24, 2022 at 12:05 PM AEST, Rohan McLure wrote:
quoted
The powerpc fallocate compat syscall handler is identical to the
generic implementation provided by commit 59c10c52f573f ("riscv:
compat: syscall: Add compat_sys_call_table implementation"), and as
such can be removed in favour of the generic implementation.
A future patch series will replace more architecture-defined syscall
handlers with generic implementations, dependent on introducing generic
implementations that are compatible with powerpc and arm's parameter
reorderings.
Reported-by: Arnd Bergmann <arnd@arndb.de>
Signed-off-by: Rohan McLure <redacted>
---
V1 -> V2: Remove arch-specific fallocate handler.
V2 -> V3: Remove generic fallocate prototype. Move to beginning of
series.
---
Is there a reason not to put this in asm-generic/compat.h?
Possibly you want to put this with the other compat definitions and
above the asm-generic include. The generic header expects the arch to
include it after defining what it wants to override.
Yes, makes sense. I think the riscv people added this to asm-generic,
they tried to do only the minimal parts.
In theory, any architecture could have its own calling conventions
for each syscall and have them in the opposite order for one
endianess. I checked the seven non-generic implementations of the
sys_fallocate() syscall and all except powerpc have the same
ABI as the generic one.
The powerpc difference is that in little-endian mode, only
the 'len' argument is swapped but the 'offset' argument is
still high/low:
long compat_sys_fallocate(int fd, int mode, u32 offset1, u32 offset2,
u32 len1, u32 len2)
{
return ksys_fallocate(fd, mode, ((loff_t)offset1 << 32) | offset2,
merge_64(len1, len2));
}
It's probably best to first fix this by using merge_64(offset1,
offset2) and allow that patch to be backported to stable kernels,
before changing it over to the generic code in a separate patch
within that series.
A related issue seems to exist in ppc_fadvise64_64(), which
uses the wrong argument order on ppc32le compat tasks, in addition
to having at least three different calling conventions across
architectures.
Arnd
On Wed Aug 24, 2022 at 12:05 PM AEST, Rohan McLure wrote:
Arch-specific implementations of syscall handlers are currently used
over generic implementations for the following reasons:
1. Semantics unique to powerpc
2. Compatibility syscalls require 'argument padding' to comply with
64-bit argument convention in ELF32 abi.
3. Parameter types or order is different in other architectures.
These syscall handlers have been defined prior to this patch series
without invoking the SYSCALL_DEFINE or COMPAT_SYSCALL_DEFINE macros with
custom input and output types. We remove every such direct definition in
favour of the aforementioned macros.
And what about sys_fallocate, where did that go?
Also update syscalls.tbl in order to refer to the symbol names generated
by each of these macros. Since ppc64_personality can be called by both
64 bit and 32 bit binaries through compatibility, we must generate both
both compat_sys_ and sys_ symbols for this handler.
Actually I don't have a concern about this patch, I thoughtthere was
something odd going on with ppc64_personality but I misread, it all
looks pretty good.
A number of architectures including arm and powerpc agree on an
alternative argument order and numbering for most of these arch-specific
handlers. A future patch series may allow for asm/unistd.h to signal
through its defines that a generic implementation of these syscall
handlers with the correct calling convention be omitted, through the
__ARCH_WANT_COMPAT_SYS_... convention.
I'm on the fence about including this in changelog. arm developers
might not see it, for example. Putting it in include/linux/compat.h
as a comment might be better.
If you keep it in the changelog, maybe preface with something like
aside, side note, in future or something so that's clear it's not part
of the current patch.
Thanks,
Nick
On Wed Aug 24, 2022 at 12:05 PM AEST, Rohan McLure wrote:
quoted hunk
Forward declare all syscall handler prototypes where a generic prototype
is not provided in either linux/syscalls.h or linux/compat.h in
asm/syscalls.h. This is required for compile-time type-checking for
syscall handlers, which is implemented later in this series.
32-bit compatibility syscall handlers are expressed in terms of types in
ppc32.h. Expose this header globally.
Signed-off-by: Rohan McLure <redacted>
---
V1 -> V2: Explicitly include prototypes.
V2 -> V3: Remove extraneous #include <asm/compat.h> and ppc_fallocate
prototype. Rename header.
---
arch/powerpc/include/asm/syscalls.h | 90 +++++++++++++-----
.../ppc32.h => include/asm/syscalls_32.h} | 0
arch/powerpc/kernel/signal_32.c | 2 +-
arch/powerpc/perf/callchain_32.c | 2 +-
4 files changed, 70 insertions(+), 24 deletions(-)
Should this be under PPC32 since you're adding the ifdefs?
Because you added a new comment here... This is register padding
to do with something, even/odd pair calling convention? I can't
remember the details would you be able to expand the comment a bit
because I'm sure I'll forget it again too.
Thanks,
Nick
On Wed Aug 24, 2022 at 12:05 PM AEST, Rohan McLure wrote:
The table of syscall handlers and registered compatibility syscall
handlers has in past been produced using assembly, with function
references resolved at link time. This moves link-time errors to
compile-time, by rewriting systbl.S in C, and including the
linux/syscalls.h, linux/compat.h and asm/syscalls.h headers for
prototypes.
Well this is pretty cool.
Unrelated, but since commit ab66dcc76d6a, is
arch/powerpc/kernel/systbl_chk.sh unused in the tree?
diff --git a/arch/powerpc/kernel/systbl.S b/arch/powerpc/kernel/systbl.csimilarity index 59%rename from arch/powerpc/kernel/systbl.Srename to arch/powerpc/kernel/systbl.cindex cb3358886203..99ffdfef6b9c 100644--- a/arch/powerpc/kernel/systbl.S+++ b/arch/powerpc/kernel/systbl.c
Humour me, the asm version had this in rodata, does this change that or
does it somehow get put back in there? Should this be const?
Otherwise,
Reviewed-by: Nicholas Piggin <npiggin@gmail.com>
On Wed Aug 24, 2022 at 12:05 PM AEST, Rohan McLure wrote:
Cause syscall handlers to be typed as follows when called indirectly
throughout the kernel.
typedef long (*syscall_fn)(unsigned long, unsigned long, unsigned long,
unsigned long, unsigned long, unsigned long);
The point is... better type checking?
quoted hunk
Since both 32 and 64-bit abis allow for at least the first six
machine-word length parameters to a function to be passed by registers,
even handlers which admit fewer than six parameters may be viewed as
having the above type.
Fixup comparisons in VDSO to avoid pointer-integer comparison. Introduce
explicit cast on systems with SPUs.
Signed-off-by: Rohan McLure <redacted>
---
V1 -> V2: New patch.
V2 -> V3: Remove unnecessary cast from const syscall_fn to syscall_fn
---
arch/powerpc/include/asm/syscall.h | 7 +++++--
arch/powerpc/include/asm/syscalls.h | 1 +
arch/powerpc/kernel/systbl.c | 6 +++---
arch/powerpc/kernel/vdso.c | 4 ++--
arch/powerpc/platforms/cell/spu_callbacks.c | 6 +++---
5 files changed, 14 insertions(+), 10 deletions(-)
Ah you constify it in this patch. I think the previous patch should have
kept the const, and it should keep the unsigned long type rather than
use void *. Either that or do this patch first.
quoted hunk
static inline int syscall_get_nr(struct task_struct *task, struct pt_regs *regs)
{
On Mon, Sep 12, 2022, at 10:38 AM, Nicholas Piggin wrote:
quoted
On Wed Aug 24, 2022 at 12:05 PM AEST, Rohan McLure wrote:
quoted
The powerpc fallocate compat syscall handler is identical to the
generic implementation provided by commit 59c10c52f573f ("riscv:
compat: syscall: Add compat_sys_call_table implementation"), and as
such can be removed in favour of the generic implementation.
A future patch series will replace more architecture-defined syscall
handlers with generic implementations, dependent on introducing generic
implementations that are compatible with powerpc and arm's parameter
reorderings.
Reported-by: Arnd Bergmann <arnd@arndb.de>
Signed-off-by: Rohan McLure <redacted>
---
V1 -> V2: Remove arch-specific fallocate handler.
V2 -> V3: Remove generic fallocate prototype. Move to beginning of
series.
---
Is there a reason not to put this in asm-generic/compat.h?
Possibly you want to put this with the other compat definitions and
above the asm-generic include. The generic header expects the arch to
include it after defining what it wants to override.
Yes, makes sense. I think the riscv people added this to asm-generic,
they tried to do only the minimal parts.
In theory, any architecture could have its own calling conventions
for each syscall and have them in the opposite order for one
endianess. I checked the seven non-generic implementations of the
sys_fallocate() syscall and all except powerpc have the same
ABI as the generic one.
The powerpc difference is that in little-endian mode, only
the 'len' argument is swapped but the 'offset' argument is
still high/low:
long compat_sys_fallocate(int fd, int mode, u32 offset1, u32 offset2,
u32 len1, u32 len2)
{
return ksys_fallocate(fd, mode, ((loff_t)offset1 << 32) | offset2,
merge_64(len1, len2));
}
It's probably best to first fix this by using merge_64(offset1,
offset2) and allow that patch to be backported to stable kernels,
before changing it over to the generic code in a separate patch
within that series.
A related issue seems to exist in ppc_fadvise64_64(), which
uses the wrong argument order on ppc32le compat tasks, in addition
to having at least three different calling conventions across
architectures.
Do ppc32le exist at all ?
Native ppc32 is be only, and I'm not aware that ppc64 is able to run
ppc32le compat tasks.
Christophe
On Mon, Sep 12, 2022, at 1:00 PM, Christophe Leroy wrote:
Le 12/09/2022 à 11:57, Arnd Bergmann a écrit :
quoted
On Mon, Sep 12, 2022, at 10:38 AM, Nicholas Piggin wrote:
The powerpc difference is that in little-endian mode, only
the 'len' argument is swapped but the 'offset' argument is
still high/low:
long compat_sys_fallocate(int fd, int mode, u32 offset1, u32 offset2,
u32 len1, u32 len2)
{
return ksys_fallocate(fd, mode, ((loff_t)offset1 << 32) | offset2,
merge_64(len1, len2));
}
It's probably best to first fix this by using merge_64(offset1,
offset2) and allow that patch to be backported to stable kernels,
before changing it over to the generic code in a separate patch
within that series.
A related issue seems to exist in ppc_fadvise64_64(), which
uses the wrong argument order on ppc32le compat tasks, in addition
to having at least three different calling conventions across
architectures.
Do ppc32le exist at all ?
Native ppc32 is be only, and I'm not aware that ppc64 is able to run
ppc32le compat tasks.
I'm not aware of anyone using it, but commit 6e944aed8859
("powerpc/64: Make COMPAT user-selectable disabled on littleendian
by default.") added support to the kernel, and commit
57f48b4b74e7 ("powerpc/compat_sys: swap hi/lo parts of 64-bit
syscall args in LE mode") changed the compat syscall helpers
to pass 64-bit arguments correctly but apparently got fallocate()
and fadvise64_64() wrong.
With Rohan's series, we use generic implementation, which
is the sensible ABI but the change is technically an ABI
change for ppc32le, and it makes sense to split the change
that fixes the behavior from the cleanup.
Arnd
On Wed Aug 24, 2022 at 12:05 PM AEST, Rohan McLure wrote:
Macros for restoring and saving registers to and from the stack exist.
Provide macros with the same interface for clearing a range of gprs by
setting each register's value in that range to zero.
Can I bikeshed this a bit more and ask if you would change the order?
Saving and restoring macros are incidental, and are neither the change
nor the reson for it. I think the changelog reads better if you state
the change (or the problem) up front.
Provid register zeroing macros ... follow the same convention as
existing register stack save/restore macros ... will be used in later
change to zero registers.
Or not, if you disagree.
Reviewed-by: Nicholas Piggin <npiggin@gmail.com>
quoted hunk
The resulting macros are called ZEROIZE_GPRS and ZEROIZE_NVGPRS, keeping
with the naming of the accompanying restore and save macros, and usage
of zeroize to describe this operation elsewhere in the kernel.
Signed-off-by: Rohan McLure <redacted>
---
V1 -> V2: Change 'ZERO' usage in naming to 'NULLIFY', a more obvious verb
V2 -> V3: Change 'NULLIFY' usage in naming to 'ZEROIZE', which has
precedent in kernel and explicitly specifies that we are zeroing.
V3 -> V4: Update commit message to use zeroize.
---
arch/powerpc/include/asm/ppc_asm.h | 22 ++++++++++++++++++++++
1 file changed, 22 insertions(+)
On Wed Aug 24, 2022 at 12:05 PM AEST, Rohan McLure wrote:
This reverts commit 8875f47b7681aa4e4484a9b612577b044725f839.
Can you use short hash and commit title format? Also it's no longer
just reverting that patch, so maybe just come up with a new title
for this patch and reference the two patches here?
Reviewed-by: Nicholas Piggin <npiggin@gmail.com>
Oh, I meant to say for the last patch and this one. Can you move
them to after patch 13? That way all your build and wrapper mucking
are in the first patches, and then all the zeroizing comes next.
Thanks,
Nick
quoted hunk
Save caller's original r3 state to the kernel stackframe before entering
system_call_exception. This allows for user registers to be cleared by
the time system_call_exception is entered, reducing the influence of
user registers on speculation within the kernel.
Prior to this commit, orig_r3 was saved at the beginning of
system_call_exception. Instead, save orig_r3 while the user value is
still live in r3.
Also replicate this early save in 32-bit. A similar save was removed in
commit 6f76a01173cc ("powerpc/syscall: implement system call entry/exit logic in C for PPC32")
when 32-bit adopted system_call_exception. Revert its removal of orig_r3
saves.
Signed-off-by: Rohan McLure <redacted>
---
V2 -> V3: New commit.
---
arch/powerpc/kernel/entry_32.S | 1 +
arch/powerpc/kernel/interrupt_64.S | 2 ++
arch/powerpc/kernel/syscall.c | 1 -
3 files changed, 3 insertions(+), 1 deletion(-)
@@ -25,7 +25,6 @@ notrace long system_call_exception(long r3, long r4, long r5,kuap_lock();add_random_kstack_offset();-regs->orig_gpr3=r3;if(IS_ENABLED(CONFIG_PPC_IRQ_SOFT_MASK_DEBUG))BUG_ON(irq_soft_mask_return()!=IRQS_ALL_DISABLED);
On Wed Aug 24, 2022 at 12:05 PM AEST, Rohan McLure wrote:
Implement syscall wrapper as per s390, x86, arm64. When enabled
cause handlers to accept parameters from a stack frame rather than
from user scratch register state. This allows for user registers to be
safely cleared in order to reduce caller influence on speculation
within syscall routine. The wrapper is a macro that emits syscall
handler symbols that call into the target handler, obtaining its
parameters from a struct pt_regs on the stack.
As registers are already saved to the stack prior to calling
system_call_exception, it appears that this function is executed more
efficiently with the new stack-pointer convention than with parameters
passed by registers, avoiding the allocation of a stack frame for this
method. On a 32-bit system, we see >20% performance increases on the
null_syscall microbenchmark, and on a Power 8 the performance gains
amortise the cost of clearing and restoring registers which is
implemented at the end of this series, seeing final result of ~5.6%
performance improvement on null_syscall.
Syscalls are wrapped in this fashion on all platforms except for the
Cell processor as this commit does not provide SPU support. This can be
quickly fixed in a successive patch, but requires spu_sys_callback to
allocate a pt_regs structure to satisfy the wrapped calling convention.
I didn't look closely at the syscall wrapper macro gunk but it
generally looks pretty fine, not much change to actual code which
is good...
quoted hunk
Co-developed-by: Andrew Donnellan <redacted>
Signed-off-by: Andrew Donnellan <redacted>
Signed-off-by: Rohan McLure <redacted>
---
V1 -> V2: Generate prototypes for symbols produced by the wrapper.
V2 -> V3: Rebased to remove conflict with 1547db7d1f44
("powerpc: Move system_call_exception() to syscall.c"). Also remove copy
from gpr3 save slot on stackframe to orig_r3's slot. Fix whitespace with
preprocessor defines in system_call_exception.
---
arch/powerpc/Kconfig | 1 +
arch/powerpc/include/asm/interrupt.h | 3 +-
arch/powerpc/include/asm/syscall.h | 4 +
arch/powerpc/include/asm/syscall_wrapper.h | 84 ++++++++++++++++++++
arch/powerpc/include/asm/syscalls.h | 25 +++++-
arch/powerpc/kernel/entry_32.S | 6 +-
arch/powerpc/kernel/interrupt_64.S | 16 ++--
arch/powerpc/kernel/syscall.c | 31 +++-----
arch/powerpc/kernel/systbl.c | 2 +
arch/powerpc/kernel/vdso.c | 2 +
10 files changed, 142 insertions(+), 32 deletions(-)
Can you change the calling convention to be regs then r0 so regs matches
the argument position in the syscall?
Then we perhaps should look at storing the registers to pt_regs using
the same r3 as base register to save the values in the asm too. That
way it's friendlier to possible static load hit store predictors. Not
for your series but just a possible later idea.
Thanks,
Nick
quoted hunk
notrace unsigned long syscall_exit_prepare(unsigned long r3, struct pt_regs *regs, long scv);
notrace unsigned long interrupt_exit_user_prepare(struct pt_regs *regs);
notrace unsigned long interrupt_exit_kernel_prepare(struct pt_regs *regs);
@@ -12,12 +12,8 @@#include<asm/unistd.h>-typedeflong(*syscall_fn)(long,long,long,long,long,long);-/* Has to run notrace because it is entered not completely "reconciled" */-notracelongsystem_call_exception(longr3,longr4,longr5,-longr6,longr7,longr8,-unsignedlongr0,structpt_regs*regs)+notracelongsystem_call_exception(unsignedlongr0,structpt_regs*regs){longret;syscall_fnf;
@@ -138,12 +134,6 @@ notrace long system_call_exception(long r3, long r4, long r5,r0=do_syscall_trace_enter(regs);if(unlikely(r0>=NR_syscalls))returnregs->gpr[3];-r3=regs->gpr[3];-r4=regs->gpr[4];-r5=regs->gpr[5];-r6=regs->gpr[6];-r7=regs->gpr[7];-r8=regs->gpr[8];}elseif(unlikely(r0>=NR_syscalls)){if(unlikely(trap_is_unsupported_scv(regs))){
@@ -160,18 +150,23 @@ notrace long system_call_exception(long r3, long r4, long r5,if(unlikely(is_compat_task())){f=(void*)compat_sys_call_table[r0];-r3&=0x00000000ffffffffULL;-r4&=0x00000000ffffffffULL;-r5&=0x00000000ffffffffULL;-r6&=0x00000000ffffffffULL;-r7&=0x00000000ffffffffULL;-r8&=0x00000000ffffffffULL;+regs->gpr[3]&=0x00000000ffffffffULL;+regs->gpr[4]&=0x00000000ffffffffULL;+regs->gpr[5]&=0x00000000ffffffffULL;+regs->gpr[6]&=0x00000000ffffffffULL;+regs->gpr[7]&=0x00000000ffffffffULL;+regs->gpr[8]&=0x00000000ffffffffULL;}else{f=(void*)sys_call_table[r0];}-ret=f(r3,r4,r5,r6,r7,r8);+#ifdef CONFIG_ARCH_HAS_SYSCALL_WRAPPER+ret=f(regs);+#else+ret=f(regs->gpr[3],regs->gpr[4],regs->gpr[5],+regs->gpr[6],regs->gpr[7],regs->gpr[8]);+#endif/**Ultimately,thisvaluewillgetlimitedbyKSTACK_OFFSET_MAX(),
On Wed Aug 24, 2022 at 12:05 PM AEST, Rohan McLure wrote:
quoted hunk
Clear user state in gprs (assign to zero) to reduce the influence of user
registers on speculation within kernel syscall handlers. Clears occur
at the very beginning of the sc and scv 0 interrupt handlers, with
restores occurring following the execution of the syscall handler.
Signed-off-by: Rohan McLure <redacted>
---
V1 -> V2: Update summary
V2 -> V3: Remove erroneous summary paragraph on syscall_exit_prepare
V3 -> V4: Use ZEROIZE instead of NULLIFY
---
arch/powerpc/kernel/interrupt_64.S | 22 ++++++++++++++++++----
1 file changed, 18 insertions(+), 4 deletions(-)
Ther's not much reason for this to go right against
system_call_exception. You could set it up above where you set r4,
and zero r0 as well with the rest of the volatiles? I guess it'll
be overwritten by mflr r0 right away... although that could be an
implementation detail not everything requires it and mcount call
gets noped out. So probably doesn't hurt to zero it, while we're
being paranoid.
Thanks,
Nick
@@ -249,7 +256,7 @@ END_BTB_FLUSH_SECTION ld r2,PACATOC(r13) mfcr r12 li r11,0- /* Can we avoid saving r3-r8 in common case? */+ /* Save syscall parameters in r3-r8 */ std r3,GPR3(r1) std r4,GPR4(r1) std r5,GPR5(r1)
@@ -300,6 +307,13 @@ END_BTB_FLUSH_SECTION wrteei 1 #endif+ /*+ * Zero user registers to prevent influencing speculative execution+ * state of kernel code.+ */+ ZEROIZE_GPRS(5, 12)+ ZEROIZE_NVGPRS()+ /* Calling convention has r3 = orig r0, r4 = regs */ mr r3,r0 bl system_call_exception
@@ -342,6 +356,7 @@ BEGIN_FTR_SECTION stdcx. r0,0,r1 /* to clear the reservation */ END_FTR_SECTION_IFCLR(CPU_FTR_STCX_CHECKS_ADDRESS)+ REST_NVGPRS(r1) cmpdi r3,0 bne .Lsyscall_restore_regs /* Zero volatile regs that may contain sensitive kernel data */
On Wed Aug 24, 2022 at 12:05 PM AEST, Rohan McLure wrote:
Use the convenience macros for saving/clearing/restoring gprs in keeping
with syscall calling conventions. The plural variants of these macros
can store a range of registers for concision.
This works well when the user gpr value we are hoping to save is still
live. In the syscall interrupt handlers, user register state is
sometimes juggled between registers. Hold-off from issuing the SAVE_GPR
macro for applicable neighbouring lines to highlight the delicate
register save logic.
Reviewed-by: Nicholas Piggin <npiggin@gmail.com>
quoted hunk
Signed-off-by: Rohan McLure <redacted>
---
V1 -> V2: Update summary
V2 -> V3: Update summary regarding exclusions for the SAVE_GPR marco.
Acknowledge new name for ZEROIZE_GPR{,S} macros.
---
arch/powerpc/kernel/interrupt_64.S | 43 ++++++----------------------
1 file changed, 9 insertions(+), 34 deletions(-)
On Wed Aug 24, 2022 at 12:05 PM AEST, Rohan McLure wrote:
Interrupt handlers on 64s systems will often need to save register state
from the interrupted process to make space for loading special purpose
registers or for internal state.
Fix a comment documenting a common code path macro in the beginning of
interrupt handlers where r10 is saved to the PACA to afford space for
the value of the CFAR. Comment is currently written as if r10-r12 are
saved to PACA, but in fact only r10 is saved, with r11-r12 saved much
later. The distance in code between these saves has grown over the many
revisions of this macro. Fix this by signalling with a comment where
r11-r12 are saved to the PACA.
Wondering if this kind of comment is useful at all really... But it is
better than broken comment.
Reviewed-by: Nicholas Piggin <npiggin@gmail.com>
quoted hunk
Signed-off-by: Rohan McLure <redacted>
---
V1 -> V2: Given its own commit
V2 -> V3: Annotate r11-r12 save locations with comment.
---
arch/powerpc/kernel/exceptions-64s.S | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
On Wed Aug 24, 2022 at 12:05 PM AEST, Rohan McLure wrote:
Zero GPRS r0, r2-r11, r14-r31, on entry into the kernel for all
other interrupt sources to limit influence of user-space values
in potential speculation gadgets. The remaining gprs are overwritten by
entry macros to interrupt handlers, irrespective of whether or not a
given handler consumes these register values.
Prior to this commit, r14-r31 are restored on a per-interrupt basis at
exit, but now they are always restored. Remove explicit REST_NVGPRS
invocations as non-volatiles must now always be restored. 32-bit systems
do not clear user registers on interrupt, and continue to depend on the
return value of interrupt_exit_user_prepare to determine whether or not
to restore non-volatiles.
The mmap_bench benchmark in selftests should rapidly invoke pagefaults.
See ~0.8% performance regression with this mitigation, but this
indicates the worst-case performance due to heavier-weight interrupt
handlers.
Ow, my heart :(
Are we not keeping a CONFIG option to rid ourselves of this vile
performance robbing thing? Are we getting rid of the whole
_TIF_RESTOREALL thing too, or does PPC32 want to keep it?
Kernel always has to keep r13 so no need to comment that. Keeping r11,
is that for those annoying fp_unavailable etc handlers?
There's probably not much a user can do with this, given they're set
from the MSR. Use can influence some bits of its MSR though. So long
as we're being paranoid, you could add an IOPTION to retain r11 only for
the handlers that need it, or have them load it from MSR and zero it
here.
Thanks,
Nick
@@ -1368,11 +1374,6 @@ ALT_MMU_FTR_SECTION_END_IFCLR(MMU_FTR_TYPE_RADIX) b interrupt_return_srr 1: bl do_break- /*- * do_break() may have changed the NV GPRS while handling a breakpoint.- * If so, we need to restore them with their updated values.- */- REST_NVGPRS(r1) b interrupt_return_srr
@@ -432,9 +432,6 @@ interrupt_return_\srr\()_user: /* make backtraces match the _kernel variant */ _ASM_NOKPROBE_SYMBOL(interrupt_return_\srr\()_user)addir3,r1,STACK_FRAME_OVERHEADblinterrupt_exit_user_prepare-cmpdir3,0-bne-.Lrestore_nvgprs_\srr-.Lrestore_nvgprs_\srr\()_cont:stdr1,PACA_EXIT_SAVE_R1(r13)/*saver1forrestart*/#ifdef CONFIG_PPC_BOOK3S.Linterrupt_return_\srr\()_user_rst_start:
@@ -448,6 +445,7 @@ _ASM_NOKPROBE_SYMBOL(interrupt_return_\srr\()_user)stbr11,PACAIRQHAPPENED(r13)# clear out possible HARD_DIS.Lfast_user_interrupt_return_\srr\():+REST_NVGPRS(r1)#ifdef CONFIG_PPC_BOOK3S.ifc\srr,srrlbzr4,PACASRR_VALID(r13)
On Wed Aug 24, 2022 at 12:05 PM AEST, Rohan McLure wrote:
The common interrupt handler prologue macro and the bad_stack
trampolines include consecutive sequences of register saves, and some
register clears. Neaten such instances by expanding use of the SAVE_GPRS
macro and employing the ZEROIZE_GPR macro when appropriate.
Also simplify an invocation of SAVE_GPRS targetting all non-volatile
registers to SAVE_NVGPRS.
From: Michael Ellerman <mpe@ellerman.id.au> Date: 2022-09-13 02:30:20
"Nicholas Piggin" [off-list ref] writes:
On Wed Aug 24, 2022 at 12:05 PM AEST, Rohan McLure wrote:
...
quoted
diff --git a/arch/powerpc/kernel/systbl.S b/arch/powerpc/kernel/systbl.csimilarity index 59%rename from arch/powerpc/kernel/systbl.Srename to arch/powerpc/kernel/systbl.cindex cb3358886203..99ffdfef6b9c 100644--- a/arch/powerpc/kernel/systbl.S+++ b/arch/powerpc/kernel/systbl.c
Humour me, the asm version had this in rodata, does this change that or
does it somehow get put back in there? Should this be const?
Even with const I still see it not landing in rodata for some reason.
$ grep -e start_rodata -e end_rodata -e sys_call_table .build/System.map
c000000000f90000 D __start_rodata
c0000000012b0000 R __end_rodata
c0000000027d51b0 D sys_call_table
vs before the series:
c000000000f80000 D __start_rodata
c000000000f805f0 D sys_call_table
c0000000012a0000 R __end_rodata
cheers
On 12 Sep 2022, at 8:33 pm, Nicholas Piggin [off-list ref] wrote:
On Wed Aug 24, 2022 at 12:05 PM AEST, Rohan McLure wrote:
quoted
Forward declare all syscall handler prototypes where a generic prototype
is not provided in either linux/syscalls.h or linux/compat.h in
asm/syscalls.h. This is required for compile-time type-checking for
syscall handlers, which is implemented later in this series.
32-bit compatibility syscall handlers are expressed in terms of types in
ppc32.h. Expose this header globally.
Signed-off-by: Rohan McLure <redacted>
---
V1 -> V2: Explicitly include prototypes.
V2 -> V3: Remove extraneous #include <asm/compat.h> and ppc_fallocate
prototype. Rename header.
---
arch/powerpc/include/asm/syscalls.h | 90 +++++++++++++-----
.../ppc32.h => include/asm/syscalls_32.h} | 0
arch/powerpc/kernel/signal_32.c | 2 +-
arch/powerpc/perf/callchain_32.c | 2 +-
4 files changed, 70 insertions(+), 24 deletions(-)
Should this be under PPC32 since you're adding the ifdefs?
Because you added a new comment here... This is register padding
to do with something, even/odd pair calling convention? I can't
remember the details would you be able to expand the comment a bit
because I'm sure I'll forget it again too.
Cool. I’ll neaten this up and change that comment.
On 12 Sep 2022, at 7:03 pm, Nicholas Piggin [off-list ref] wrote:
On Wed Aug 24, 2022 at 12:05 PM AEST, Rohan McLure wrote:
quoted
Syscall #82 has been implemented for 32-bit platforms in a unique way on
powerpc systems. This hack will in effect guess whether the caller is
expecting new select semantics or old select semantics. It does so via a
guess, based off the first parameter. In new select, this parameter
represents the length of a user-memory array of file descriptors, and in
old select this is a pointer to an arguments structure.
The heuristic simply interprets sufficiently large values of its first
parameter as being a call to old select. The following is a discussion
on how this syscall should be handled.
Link: https://lore.kernel.org/lkml/13737de5-0eb7-e881-9af0-163b0d29a1a0@csgroup.eu/
Seems okay to me, probably Christophe needs to ack it.
Should some of that history be included directly in this changelog?
Should ppc64 compat be added back too, if this is being updated instead
of removed? I don't know much about compat but it seems odd not provide
it (considering it's just using compat_sys_old_select, isn't it?
That would make sense to me. I’ll put that in syscall.tbl.
Reviewed-by: Nicholas Piggin <npiggin@gmail.com>
quoted
As discussed in this thread, the existence of such a hack suggests that for
whatever powerpc binaries may predate glibc, it is most likely that they
would have taken use of the old select semantics. x86 and arm64 both
implement this syscall with oldselect semantics.
Remove the powerpc implementation, and update syscall.tbl to refer to emit
a reference to sys_old_select for 32-bit binaries, in keeping with how
other architectures support syscall #82.
Signed-off-by: Rohan McLure <redacted>
---
V1 -> V2: Remove arch-specific select handler
V2 -> V3: Remove ppc_old_select prototype in <asm/syscalls.h>. Move to
earlier in patch series
---
arch/powerpc/include/asm/syscalls.h | 2 --
arch/powerpc/kernel/syscalls.c | 17 -----------------
arch/powerpc/kernel/syscalls/syscall.tbl | 2 +-
.../arch/powerpc/entry/syscalls/syscall.tbl | 2 +-
4 files changed, 2 insertions(+), 21 deletions(-)
return do_mmap2(addr, len, prot, flags, fd, offset, PAGE_SHIFT);
}
-#ifdef CONFIG_PPC32
-/*
- * Due to some executables calling the wrong select we sometimes
- * get wrong args. This determines how the args are being passed
- * (a single ptr to them all args passed) then calls
- * sys_select() with the appropriate args. -- Cort
- */
-int
-ppc_select(int n, fd_set __user *inp, fd_set __user *outp, fd_set __user *exp, struct __kernel_old_timeval __user *tvp)
-{
- if ((unsigned long)n >= 4096)
- return sys_old_select((void __user *)n);
-
- return sys_select(n, inp, outp, exp, tvp);
-}
-#endif
-
#ifdef CONFIG_PPC64
long ppc64_personality(unsigned long personality)
{
On 12 Sep 2022, at 7:47 pm, Nicholas Piggin [off-list ref] wrote:
On Wed Aug 24, 2022 at 12:05 PM AEST, Rohan McLure wrote:
quoted
Syscall handlers should not be invoked internally by their symbol names,
as these symbols defined by the architecture-defined SYSCALL_DEFINE
macro. Move the compatibility syscall definition for mmap2 to
syscalls.c, so that all mmap implementations can share an inline helper
function, as is done with the personality handlers.
Signed-off-by: Rohan McLure <redacted>
Is there any point to keping sys_ppc32.c at all? Might as well move them
all to syscall.c IMO.
Currently serves as a fairly arbitrary distinginction between compat calls
and others, noting that a compat variant of personality is in syscalls.c.
May as well get rid of it.
Reviewed-by: Nicholas Piggin <npiggin@gmail.com>
quoted
---
V1 -> V2: Move mmap2 compat implementation to asm/kernel/syscalls.c.
V3 -> V4: Move to be applied before syscall wrapper introduced.
---
arch/powerpc/kernel/sys_ppc32.c | 9 ---------
arch/powerpc/kernel/syscalls.c | 11 +++++++++++
2 files changed, 11 insertions(+), 9 deletions(-)
#include <asm/syscalls.h>
#include <asm/switch_to.h>
-unsigned long compat_sys_mmap2(unsigned long addr, size_t len,
- unsigned long prot, unsigned long flags,
- unsigned long fd, unsigned long pgoff)
-{
- /* This should remain 12 even if PAGE_SIZE changes */
- return sys_mmap(addr, len, prot, flags, fd, pgoff << 12);
-}
-
/*
* long long munging:
* The 32 bit ABI passes long longs in an odd even register pair.
On 12 Sep 2022, at 8:56 pm, Nicholas Piggin [off-list ref] wrote:
On Wed Aug 24, 2022 at 12:05 PM AEST, Rohan McLure wrote:
quoted
Cause syscall handlers to be typed as follows when called indirectly
throughout the kernel.
typedef long (*syscall_fn)(unsigned long, unsigned long, unsigned long,
unsigned long, unsigned long, unsigned long);
The point is... better type checking?
quoted
Since both 32 and 64-bit abis allow for at least the first six
machine-word length parameters to a function to be passed by registers,
even handlers which admit fewer than six parameters may be viewed as
having the above type.
Fixup comparisons in VDSO to avoid pointer-integer comparison. Introduce
explicit cast on systems with SPUs.
Signed-off-by: Rohan McLure <redacted>
---
V1 -> V2: New patch.
V2 -> V3: Remove unnecessary cast from const syscall_fn to syscall_fn
---
arch/powerpc/include/asm/syscall.h | 7 +++++--
arch/powerpc/include/asm/syscalls.h | 1 +
arch/powerpc/kernel/systbl.c | 6 +++---
arch/powerpc/kernel/vdso.c | 4 ++--
arch/powerpc/platforms/cell/spu_callbacks.c | 6 +++---
5 files changed, 14 insertions(+), 10 deletions(-)
Ah you constify it in this patch. I think the previous patch should have
kept the const, and it should keep the unsigned long type rather than
use void *. Either that or do this patch first.
quoted
static inline int syscall_get_nr(struct task_struct *task, struct pt_regs *regs)
{
Also perhaps this should have been in the prior pach and this pach
should change the cast from void to syscall_fn ?
This cast to (void *) kicks in when casting functions with six or fewer
parameters to six-parameter type accepting and returning u64. Sadly I can’t
find a way to avoid -Wcast-function-type even with (__force syscall_fn) short
of an ugly casti to void* here. Any suggestions?
On 12 Sep 2022, at 9:09 pm, Nicholas Piggin [off-list ref] wrote:
On Wed Aug 24, 2022 at 12:05 PM AEST, Rohan McLure wrote:
quoted
Macros for restoring and saving registers to and from the stack exist.
Provide macros with the same interface for clearing a range of gprs by
setting each register's value in that range to zero.
Can I bikeshed this a bit more and ask if you would change the order?
Saving and restoring macros are incidental, and are neither the change
nor the reson for it. I think the changelog reads better if you state
the change (or the problem) up front.
Provid register zeroing macros ... follow the same convention as
existing register stack save/restore macros ... will be used in later
change to zero registers.
Thanks for the suggestion, that should read better.
Or not, if you disagree.
Reviewed-by: Nicholas Piggin <npiggin@gmail.com>
quoted
The resulting macros are called ZEROIZE_GPRS and ZEROIZE_NVGPRS, keeping
with the naming of the accompanying restore and save macros, and usage
of zeroize to describe this operation elsewhere in the kernel.
Signed-off-by: Rohan McLure <redacted>
---
V1 -> V2: Change 'ZERO' usage in naming to 'NULLIFY', a more obvious verb
V2 -> V3: Change 'NULLIFY' usage in naming to 'ZEROIZE', which has
precedent in kernel and explicitly specifies that we are zeroing.
V3 -> V4: Update commit message to use zeroize.
---
arch/powerpc/include/asm/ppc_asm.h | 22 ++++++++++++++++++++++
1 file changed, 22 insertions(+)
.endr
.endm
+/*
+ * This expands to a sequence of register clears for regs start to end
+ * inclusive, of the form:
+ *
+ * li rN, 0
+ */
+.macro ZEROIZE_REGS start, end
+ .Lreg=\start
+ .rept (\end - \start + 1)
+ li .Lreg, 0
+ .Lreg=.Lreg+1
+ .endr
+.endm
+
/*
* Macros for storing registers into and loading registers from
* exception frames.
On 12 Sep 2022, at 10:15 pm, Nicholas Piggin [off-list ref] wrote:
On Wed Aug 24, 2022 at 12:05 PM AEST, Rohan McLure wrote:
quoted
Zero GPRS r0, r2-r11, r14-r31, on entry into the kernel for all
other interrupt sources to limit influence of user-space values
in potential speculation gadgets. The remaining gprs are overwritten by
entry macros to interrupt handlers, irrespective of whether or not a
given handler consumes these register values.
Prior to this commit, r14-r31 are restored on a per-interrupt basis at
exit, but now they are always restored. Remove explicit REST_NVGPRS
invocations as non-volatiles must now always be restored. 32-bit systems
do not clear user registers on interrupt, and continue to depend on the
return value of interrupt_exit_user_prepare to determine whether or not
to restore non-volatiles.
The mmap_bench benchmark in selftests should rapidly invoke pagefaults.
See ~0.8% performance regression with this mitigation, but this
indicates the worst-case performance due to heavier-weight interrupt
handlers.
Ow, my heart :(
Are we not keeping a CONFIG option to rid ourselves of this vile
performance robbing thing? Are we getting rid of the whole
_TIF_RESTOREALL thing too, or does PPC32 want to keep it?
I see no reason not to include a CONFIG option for this
mitigation here other than simplicity. Any suggestions for a name?
I’m thinking PPC64_SANITIZE_INTERRUPTS. Defaults on Book3E_64, optional
on Book3S_64.
std r10,0(r1) /* make stack chain pointer */
std r0,GPR0(r1) /* save r0 in stackframe */
std r10,GPR1(r1) /* save r1 in stackframe */
+ ZEROIZE_GPR(0)
/* Mark our [H]SRRs valid for return */
li r10,1
Kernel always has to keep r13 so no need to comment that. Keeping r11,
is that for those annoying fp_unavailable etc handlers?
There's probably not much a user can do with this, given they're set
from the MSR. Use can influence some bits of its MSR though. So long
as we're being paranoid, you could add an IOPTION to retain r11 only for
the handlers that need it, or have them load it from MSR and zero it
here.
Good suggestion. Presume you’re referring to r12 here. I might go the
IOPTION route.
b interrupt_return_srr
1: bl do_break
- /*
- * do_break() may have changed the NV GPRS while handling a breakpoint.
- * If so, we need to restore them with their updated values.
- */
- REST_NVGPRS(r1)
b interrupt_return_srr
GEN_COMMON h_facility_unavailable
addi r3,r1,STACK_FRAME_OVERHEAD
bl facility_unavailable_exception
- REST_NVGPRS(r1) /* XXX Shouldn't be necessary in practice */
b interrupt_return_hsrr
On Thu Sep 15, 2022 at 4:55 PM AEST, Rohan McLure wrote:
quoted
On 12 Sep 2022, at 10:15 pm, Nicholas Piggin [off-list ref] wrote:
On Wed Aug 24, 2022 at 12:05 PM AEST, Rohan McLure wrote:
quoted
Zero GPRS r0, r2-r11, r14-r31, on entry into the kernel for all
other interrupt sources to limit influence of user-space values
in potential speculation gadgets. The remaining gprs are overwritten by
entry macros to interrupt handlers, irrespective of whether or not a
given handler consumes these register values.
Prior to this commit, r14-r31 are restored on a per-interrupt basis at
exit, but now they are always restored. Remove explicit REST_NVGPRS
invocations as non-volatiles must now always be restored. 32-bit systems
do not clear user registers on interrupt, and continue to depend on the
return value of interrupt_exit_user_prepare to determine whether or not
to restore non-volatiles.
The mmap_bench benchmark in selftests should rapidly invoke pagefaults.
See ~0.8% performance regression with this mitigation, but this
indicates the worst-case performance due to heavier-weight interrupt
handlers.
Ow, my heart :(
Are we not keeping a CONFIG option to rid ourselves of this vile
performance robbing thing? Are we getting rid of the whole
_TIF_RESTOREALL thing too, or does PPC32 want to keep it?
I see no reason not to include a CONFIG option for this
mitigation here other than simplicity. Any suggestions for a name?
I’m thinking PPC64_SANITIZE_INTERRUPTS. Defaults on Book3E_64, optional
on Book3S_64.
std r10,0(r1) /* make stack chain pointer */
std r0,GPR0(r1) /* save r0 in stackframe */
std r10,GPR1(r1) /* save r1 in stackframe */
+ ZEROIZE_GPR(0)
/* Mark our [H]SRRs valid for return */
li r10,1
Kernel always has to keep r13 so no need to comment that. Keeping r11,
is that for those annoying fp_unavailable etc handlers?
There's probably not much a user can do with this, given they're set
from the MSR. Use can influence some bits of its MSR though. So long
as we're being paranoid, you could add an IOPTION to retain r11 only for
the handlers that need it, or have them load it from MSR and zero it
here.
Good suggestion. Presume you’re referring to r12 here. I might go the
IOPTION route.
Yeah r12, I think you need it because some of those assembly handlers
expect it there to check SRR1 bits. Having an IOPTION is probably good
and it documents that quirk of those handlers. That's bitten me before.
Thanks,
Nick
On Thu Sep 15, 2022 at 3:45 PM AEST, Rohan McLure wrote:
quoted
On 12 Sep 2022, at 8:56 pm, Nicholas Piggin [off-list ref] wrote:
On Wed Aug 24, 2022 at 12:05 PM AEST, Rohan McLure wrote:
quoted
Cause syscall handlers to be typed as follows when called indirectly
throughout the kernel.
typedef long (*syscall_fn)(unsigned long, unsigned long, unsigned long,
unsigned long, unsigned long, unsigned long);
The point is... better type checking?
quoted
Since both 32 and 64-bit abis allow for at least the first six
machine-word length parameters to a function to be passed by registers,
even handlers which admit fewer than six parameters may be viewed as
having the above type.
Fixup comparisons in VDSO to avoid pointer-integer comparison. Introduce
explicit cast on systems with SPUs.
Signed-off-by: Rohan McLure <redacted>
---
V1 -> V2: New patch.
V2 -> V3: Remove unnecessary cast from const syscall_fn to syscall_fn
---
arch/powerpc/include/asm/syscall.h | 7 +++++--
arch/powerpc/include/asm/syscalls.h | 1 +
arch/powerpc/kernel/systbl.c | 6 +++---
arch/powerpc/kernel/vdso.c | 4 ++--
arch/powerpc/platforms/cell/spu_callbacks.c | 6 +++---
5 files changed, 14 insertions(+), 10 deletions(-)
Ah you constify it in this patch. I think the previous patch should have
kept the const, and it should keep the unsigned long type rather than
use void *. Either that or do this patch first.
quoted
static inline int syscall_get_nr(struct task_struct *task, struct pt_regs *regs)
{
Also perhaps this should have been in the prior pach and this pach
should change the cast from void to syscall_fn ?
This cast to (void *) kicks in when casting functions with six or fewer
Right, I was just wondering if it needs to be in the previous patch
because that's where you changed the type from unsigned long to void *.
Maybe there's some reason it's not required, I didn't entirely follow
all the macro expansion.
parameters to six-parameter type accepting and returning u64. Sadly I can’t
find a way to avoid -Wcast-function-type even with (__force syscall_fn) short
of an ugly casti to void* here. Any suggestions?
Ah okay. I think __force is a sparse specific attribute. Not sure if
gcc/clang can do it. There is a diag thing which maybe can turn off
warnings selectively, but if (void *) is turning off the warning
selectively then there would be no benefit to using it :) That's fine to
keep using void *.
Thanks,
Nick