Skip to content

Commit f7c7994

Browse files
committed
Merge tag 'riscv-for-linus-7.1-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git/riscv/linux
Pull RISC-V fixes from Paul Walmsley: "Relatively low-impact fixes. Probably the most notable one is that we no longer ask the monitor-mode firmware to delegate misaligned access handling to the kernel by default, since the kernel code needs significant improvement to match the functionality of the firmware. This change avoids functional problems at some cost in performance, but shouldn't affect any system with misaligned access handling in hardware. - Disable satp register probing when no5lvl is specified on the kernel command line - Fix a CFI-related issue with the misaligned access speed measurement code - Reduce the CFI shadow stack size limit from 4GB to 2GB (following ARM64 GCS) - Prevent the kernel from requesting delegation of misaligned access faults unless a new Kconfig option, RISCV_SBI_FWFT_DELEGATE_MISALIGNED, is enabled. This will depend on CONFIG_NONPORTABLE until the deficiencies of the kernel misaligned access fixup code are fixed - Fix some potential uninitialized memory accesses in error paths in compat_riscv_gpr_set() and compat_restore_sigcontext() - Fix a bug in the RISC-V MIPS vendor errata patching code where a logical-and was used in place of a bitwise-and - Drop some unnecessary code in riscv_fill_hwcap_from_isa_string() - Use macros for isa2hwcap indices in riscv_fill_hwcap(), rather than open-coding them - Fix some documentation typos (one affecting 'make htmldocs')" * tag 'riscv-for-linus-7.1-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git/riscv/linux: riscv: misaligned: Make enabling delegation depend on NONPORTABLE riscv: Docs: fix unmatched quote warning riscv: cfi: reduce shadow stack size limit from 4GB to 2GB riscv: cpufeature: Use pre-defined ISA ext macros to index isa2hwcap riscv: mm: Fixup no5lvl failure when vaddr is invalid riscv: Fix register corruption from uninitialized cregs on error riscv: errata: Fix bitwise vs logical AND in MIPS errata patching Documentation: riscv: cmodx: fix typos riscv: cpufeature: Drop this_hwcap clear in T-Head vector workaround riscv: Define __riscv_copy_{,vec_}{words,bytes}_unaligned() using SYM_TYPED_FUNC_START
2 parents 1405a07 + b69bcb1 commit f7c7994

12 files changed

Lines changed: 77 additions & 27 deletions

File tree

Documentation/arch/riscv/cmodx.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ call at each patchable function entry, and patches it dynamically at runtime to
2121
enable or disable the redirection. In the case of RISC-V, 2 instructions,
2222
AUIPC + JALR, are required to compose a function call. However, it is impossible
2323
to patch 2 instructions and expect that a concurrent read-side executes them
24-
without a race condition. This series makes atmoic code patching possible in
24+
without a race condition. This series makes atomic code patching possible in
2525
RISC-V ftrace. Kernel preemption makes things even worse as it allows the old
2626
state to persist across the patching process with stop_machine().
2727

2828
In order to get rid of stop_machine() and run dynamic ftrace with full kernel
2929
preemption, we partially initialize each patchable function entry at boot-time,
30-
setting the first instruction to AUIPC, and the second to NOP. Now, atmoic
30+
setting the first instruction to AUIPC, and the second to NOP. Now, atomic
3131
patching is possible because the kernel only has to update one instruction.
3232
According to Ziccif, as long as an instruction is naturally aligned, the ISA
3333
guarantee an atomic update.
@@ -36,8 +36,8 @@ By fixing down the first instruction, AUIPC, the range of the ftrace trampoline
3636
is limited to +-2K from the predetermined target, ftrace_caller, due to the lack
3737
of immediate encoding space in RISC-V. To address the issue, we introduce
3838
CALL_OPS, where an 8B naturally align metadata is added in front of each
39-
pacthable function. The metadata is resolved at the first trampoline, then the
40-
execution can be derect to another custom trampoline.
39+
patchable function. The metadata is resolved at the first trampoline, then the
40+
execution can be directed to another custom trampoline.
4141

4242
CMODX in the User Space
4343
-----------------------

Documentation/arch/riscv/zicfilp.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ the program.
7878

7979
Per-task indirect branch tracking state can be monitored and
8080
controlled via the :c:macro:`PR_GET_CFI` and :c:macro:`PR_SET_CFI`
81-
``prctl()` arguments (respectively), by supplying
81+
``prctl()`` arguments (respectively), by supplying
8282
:c:macro:`PR_CFI_BRANCH_LANDING_PADS` as the second argument. These
8383
are architecture-agnostic, and will return -EINVAL if the underlying
8484
functionality is not supported.

arch/riscv/Kconfig

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,28 @@ config RISCV_VECTOR_MISALIGNED
937937
help
938938
Enable detecting support for vector misaligned loads and stores.
939939

940+
config RISCV_SBI_FWFT_DELEGATE_MISALIGNED
941+
bool "Request firmware delegation of unaligned access exceptions"
942+
depends on RISCV_SBI
943+
depends on NONPORTABLE
944+
help
945+
Use SBI FWFT to request delegation of load address misaligned and
946+
store address misaligned exceptions, if possible, and prefer Linux
947+
kernel emulation of these accesses to firmware emulation.
948+
949+
Unfortunately, Linux's emulation is still incomplete. Namely, it
950+
currently does not handle vector instructions and KVM guest accesses.
951+
On platforms where these accesses would have been handled by firmware,
952+
enabling this causes unexpected kernel oopses, userspaces crashes and
953+
KVM guest crashes. If you are sure that these are not a problem for
954+
your platform, you can say Y here, which may improve performance.
955+
956+
Saying N here will not worsen emulation support for unaligned accesses
957+
even in the case where the firmware also has incomplete support. It
958+
simply keeps the firmware's emulation enabled.
959+
960+
If you don't know what to do here, say N.
961+
940962
choice
941963
prompt "Unaligned Accesses Support"
942964
default RISCV_PROBE_UNALIGNED_ACCESS

arch/riscv/errata/mips/errata.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ void mips_errata_patch_func(struct alt_entry *begin, struct alt_entry *end,
5757
}
5858

5959
tmp = (1U << alt->patch_id);
60-
if (cpu_req_errata && tmp) {
60+
if (cpu_req_errata & tmp) {
6161
mutex_lock(&text_mutex);
6262
patch_text_nosync(ALT_OLD_PTR(alt), ALT_ALT_PTR(alt),
6363
alt->alt_len);

arch/riscv/kernel/compat_signal.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ static long compat_restore_sigcontext(struct pt_regs *regs,
107107

108108
/* sc_regs is structured the same as the start of pt_regs */
109109
err = __copy_from_user(&cregs, &sc->sc_regs, sizeof(sc->sc_regs));
110+
if (unlikely(err))
111+
return err;
110112

111113
cregs_to_regs(&cregs, regs);
112114

arch/riscv/kernel/copy-unaligned.S

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* SPDX-License-Identifier: GPL-2.0 */
22
/* Copyright (C) 2023 Rivos Inc. */
33

4+
#include <linux/cfi_types.h>
45
#include <linux/linkage.h>
56
#include <asm/asm.h>
67

@@ -9,7 +10,7 @@
910
/* void __riscv_copy_words_unaligned(void *, const void *, size_t) */
1011
/* Performs a memcpy without aligning buffers, using word loads and stores. */
1112
/* Note: The size is truncated to a multiple of 8 * SZREG */
12-
SYM_FUNC_START(__riscv_copy_words_unaligned)
13+
SYM_TYPED_FUNC_START(__riscv_copy_words_unaligned)
1314
andi a4, a2, ~((8*SZREG)-1)
1415
beqz a4, 2f
1516
add a3, a1, a4
@@ -41,7 +42,7 @@ SYM_FUNC_END(__riscv_copy_words_unaligned)
4142
/* void __riscv_copy_bytes_unaligned(void *, const void *, size_t) */
4243
/* Performs a memcpy without aligning buffers, using only byte accesses. */
4344
/* Note: The size is truncated to a multiple of 8 */
44-
SYM_FUNC_START(__riscv_copy_bytes_unaligned)
45+
SYM_TYPED_FUNC_START(__riscv_copy_bytes_unaligned)
4546
andi a4, a2, ~(8-1)
4647
beqz a4, 2f
4748
add a3, a1, a4

arch/riscv/kernel/cpufeature.c

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -896,10 +896,8 @@ static void __init riscv_fill_hwcap_from_isa_string(unsigned long *isa2hwcap)
896896
* CPU cores with the ratified spec will contain non-zero
897897
* marchid.
898898
*/
899-
if (acpi_disabled && boot_vendorid == THEAD_VENDOR_ID && boot_archid == 0x0) {
900-
this_hwcap &= ~isa2hwcap[RISCV_ISA_EXT_v];
899+
if (acpi_disabled && boot_vendorid == THEAD_VENDOR_ID && boot_archid == 0x0)
901900
clear_bit(RISCV_ISA_EXT_v, source_isa);
902-
}
903901

904902
riscv_resolve_isa(source_isa, isainfo->isa, &this_hwcap, isa2hwcap);
905903

@@ -1104,16 +1102,16 @@ early_param("riscv_isa_fallback", riscv_isa_fallback_setup);
11041102
void __init riscv_fill_hwcap(void)
11051103
{
11061104
char print_str[NUM_ALPHA_EXTS + 1];
1107-
unsigned long isa2hwcap[26] = {0};
1105+
unsigned long isa2hwcap[RISCV_ISA_EXT_BASE] = {0};
11081106
int i, j;
11091107

1110-
isa2hwcap['i' - 'a'] = COMPAT_HWCAP_ISA_I;
1111-
isa2hwcap['m' - 'a'] = COMPAT_HWCAP_ISA_M;
1112-
isa2hwcap['a' - 'a'] = COMPAT_HWCAP_ISA_A;
1113-
isa2hwcap['f' - 'a'] = COMPAT_HWCAP_ISA_F;
1114-
isa2hwcap['d' - 'a'] = COMPAT_HWCAP_ISA_D;
1115-
isa2hwcap['c' - 'a'] = COMPAT_HWCAP_ISA_C;
1116-
isa2hwcap['v' - 'a'] = COMPAT_HWCAP_ISA_V;
1108+
isa2hwcap[RISCV_ISA_EXT_i] = COMPAT_HWCAP_ISA_I;
1109+
isa2hwcap[RISCV_ISA_EXT_m] = COMPAT_HWCAP_ISA_M;
1110+
isa2hwcap[RISCV_ISA_EXT_a] = COMPAT_HWCAP_ISA_A;
1111+
isa2hwcap[RISCV_ISA_EXT_f] = COMPAT_HWCAP_ISA_F;
1112+
isa2hwcap[RISCV_ISA_EXT_d] = COMPAT_HWCAP_ISA_D;
1113+
isa2hwcap[RISCV_ISA_EXT_c] = COMPAT_HWCAP_ISA_C;
1114+
isa2hwcap[RISCV_ISA_EXT_v] = COMPAT_HWCAP_ISA_V;
11171115

11181116
if (!acpi_disabled) {
11191117
riscv_fill_hwcap_from_isa_string(isa2hwcap);

arch/riscv/kernel/ptrace.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -577,8 +577,8 @@ static int compat_riscv_gpr_set(struct task_struct *target,
577577
struct compat_user_regs_struct cregs;
578578

579579
ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &cregs, 0, -1);
580-
581-
cregs_to_regs(&cregs, task_pt_regs(target));
580+
if (!ret)
581+
cregs_to_regs(&cregs, task_pt_regs(target));
582582

583583
return ret;
584584
}

arch/riscv/kernel/traps_misaligned.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ static int cpu_online_check_unaligned_access_emulated(unsigned int cpu)
584584

585585
static bool misaligned_traps_delegated;
586586

587-
#ifdef CONFIG_RISCV_SBI
587+
#if defined(CONFIG_RISCV_SBI_FWFT_DELEGATE_MISALIGNED)
588588

589589
static int cpu_online_sbi_unaligned_setup(unsigned int cpu)
590590
{

arch/riscv/kernel/usercfi.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,16 @@ void set_indir_lp_lock(struct task_struct *task, bool lock)
109109
task->thread_info.user_cfi_state.ufcfi_locked = lock;
110110
}
111111
/*
112-
* If size is 0, then to be compatible with regular stack we want it to be as big as
113-
* regular stack. Else PAGE_ALIGN it and return back
112+
* The shadow stack only stores the return address and not any variables
113+
* this should be more than sufficient for most applications.
114+
* Else PAGE_ALIGN it and return back
114115
*/
115116
static unsigned long calc_shstk_size(unsigned long size)
116117
{
117118
if (size)
118119
return PAGE_ALIGN(size);
119120

120-
return PAGE_ALIGN(min_t(unsigned long long, rlimit(RLIMIT_STACK), SZ_4G));
121+
return PAGE_ALIGN(min(rlimit(RLIMIT_STACK) / 2, SZ_2G));
121122
}
122123

123124
/*

0 commit comments

Comments
 (0)