Skip to content

Commit 890daed

Browse files
committed
Merge tag 'riscv-for-linus-6.11-rc7' of git://git.kernel.org/pub/scm/linux/kernel/git/riscv/linux
Pull RISC-V fixes from Palmer Dabbelt: - A revert for the mmap() change that ties the allocation range to the hint adress, as what we tried to do ended up regressing on other userspace workloads. - A fix to avoid a kernel memory leak when emulating misaligned accesses from userspace. - A Kconfig fix for toolchain vector detection, which now correctly detects vector support on toolchains where the V extension depends on the M extension. - A fix to avoid failing the linear mapping bootmem bounds check on NOMMU systems. - A fix for early alternatives on relocatable kernels. * tag 'riscv-for-linus-6.11-rc7' of git://git.kernel.org/pub/scm/linux/kernel/git/riscv/linux: riscv: Fix RISCV_ALTERNATIVE_EARLY riscv: Do not restrict memory size because of linear mapping on nommu riscv: Fix toolchain vector detection riscv: misaligned: Restrict user access to kernel memory riscv: mm: Do not restrict mmap address based on hint riscv: selftests: Remove mmap hint address checks Revert "RISC-V: mm: Document mmap changes"
2 parents a78d7dc + 1ff95eb commit 890daed

File tree

12 files changed

+79
-181
lines changed

12 files changed

+79
-181
lines changed

Documentation/arch/riscv/vm-layout.rst

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -134,19 +134,3 @@ RISC-V Linux Kernel SV57
134134
ffffffff00000000 | -4 GB | ffffffff7fffffff | 2 GB | modules, BPF
135135
ffffffff80000000 | -2 GB | ffffffffffffffff | 2 GB | kernel
136136
__________________|____________|__________________|_________|____________________________________________________________
137-
138-
139-
Userspace VAs
140-
--------------------
141-
To maintain compatibility with software that relies on the VA space with a
142-
maximum of 48 bits the kernel will, by default, return virtual addresses to
143-
userspace from a 48-bit range (sv48). This default behavior is achieved by
144-
passing 0 into the hint address parameter of mmap. On CPUs with an address space
145-
smaller than sv48, the CPU maximum supported address space will be the default.
146-
147-
Software can "opt-in" to receiving VAs from another VA space by providing
148-
a hint address to mmap. When a hint address is passed to mmap, the returned
149-
address will never use more bits than the hint address. For example, if a hint
150-
address of `1 << 40` is passed to mmap, a valid returned address will never use
151-
bits 41 through 63. If no mappable addresses are available in that range, mmap
152-
will return `MAP_FAILED`.

arch/riscv/Kconfig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -552,8 +552,8 @@ config RISCV_ISA_SVPBMT
552552
config TOOLCHAIN_HAS_V
553553
bool
554554
default y
555-
depends on !64BIT || $(cc-option,-mabi=lp64 -march=rv64iv)
556-
depends on !32BIT || $(cc-option,-mabi=ilp32 -march=rv32iv)
555+
depends on !64BIT || $(cc-option,-mabi=lp64 -march=rv64imv)
556+
depends on !32BIT || $(cc-option,-mabi=ilp32 -march=rv32imv)
557557
depends on LLD_VERSION >= 140000 || LD_VERSION >= 23800
558558
depends on AS_HAS_OPTION_ARCH
559559

arch/riscv/include/asm/processor.h

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,36 +14,14 @@
1414

1515
#include <asm/ptrace.h>
1616

17-
/*
18-
* addr is a hint to the maximum userspace address that mmap should provide, so
19-
* this macro needs to return the largest address space available so that
20-
* mmap_end < addr, being mmap_end the top of that address space.
21-
* See Documentation/arch/riscv/vm-layout.rst for more details.
22-
*/
2317
#define arch_get_mmap_end(addr, len, flags) \
2418
({ \
25-
unsigned long mmap_end; \
26-
typeof(addr) _addr = (addr); \
27-
if ((_addr) == 0 || is_compat_task() || \
28-
((_addr + len) > BIT(VA_BITS - 1))) \
29-
mmap_end = STACK_TOP_MAX; \
30-
else \
31-
mmap_end = (_addr + len); \
32-
mmap_end; \
19+
STACK_TOP_MAX; \
3320
})
3421

3522
#define arch_get_mmap_base(addr, base) \
3623
({ \
37-
unsigned long mmap_base; \
38-
typeof(addr) _addr = (addr); \
39-
typeof(base) _base = (base); \
40-
unsigned long rnd_gap = DEFAULT_MAP_WINDOW - (_base); \
41-
if ((_addr) == 0 || is_compat_task() || \
42-
((_addr + len) > BIT(VA_BITS - 1))) \
43-
mmap_base = (_base); \
44-
else \
45-
mmap_base = (_addr + len) - rnd_gap; \
46-
mmap_base; \
24+
base; \
4725
})
4826

4927
#ifdef CONFIG_64BIT

arch/riscv/include/asm/sbi.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <linux/types.h>
1111
#include <linux/cpumask.h>
12+
#include <linux/jump_label.h>
1213

1314
#ifdef CONFIG_RISCV_SBI
1415
enum sbi_ext_id {
@@ -304,6 +305,7 @@ struct sbiret {
304305
};
305306

306307
void sbi_init(void);
308+
long __sbi_base_ecall(int fid);
307309
struct sbiret __sbi_ecall(unsigned long arg0, unsigned long arg1,
308310
unsigned long arg2, unsigned long arg3,
309311
unsigned long arg4, unsigned long arg5,
@@ -373,7 +375,23 @@ static inline unsigned long sbi_mk_version(unsigned long major,
373375
| (minor & SBI_SPEC_VERSION_MINOR_MASK);
374376
}
375377

376-
int sbi_err_map_linux_errno(int err);
378+
static inline int sbi_err_map_linux_errno(int err)
379+
{
380+
switch (err) {
381+
case SBI_SUCCESS:
382+
return 0;
383+
case SBI_ERR_DENIED:
384+
return -EPERM;
385+
case SBI_ERR_INVALID_PARAM:
386+
return -EINVAL;
387+
case SBI_ERR_INVALID_ADDRESS:
388+
return -EFAULT;
389+
case SBI_ERR_NOT_SUPPORTED:
390+
case SBI_ERR_FAILURE:
391+
default:
392+
return -ENOTSUPP;
393+
};
394+
}
377395

378396
extern bool sbi_debug_console_available;
379397
int sbi_debug_console_write(const char *bytes, unsigned int num_bytes);

arch/riscv/kernel/Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,21 @@ endif
2020
ifdef CONFIG_RISCV_ALTERNATIVE_EARLY
2121
CFLAGS_alternative.o := -mcmodel=medany
2222
CFLAGS_cpufeature.o := -mcmodel=medany
23+
CFLAGS_sbi_ecall.o := -mcmodel=medany
2324
ifdef CONFIG_FTRACE
2425
CFLAGS_REMOVE_alternative.o = $(CC_FLAGS_FTRACE)
2526
CFLAGS_REMOVE_cpufeature.o = $(CC_FLAGS_FTRACE)
27+
CFLAGS_REMOVE_sbi_ecall.o = $(CC_FLAGS_FTRACE)
2628
endif
2729
ifdef CONFIG_RELOCATABLE
2830
CFLAGS_alternative.o += -fno-pie
2931
CFLAGS_cpufeature.o += -fno-pie
32+
CFLAGS_sbi_ecall.o += -fno-pie
3033
endif
3134
ifdef CONFIG_KASAN
3235
KASAN_SANITIZE_alternative.o := n
3336
KASAN_SANITIZE_cpufeature.o := n
37+
KASAN_SANITIZE_sbi_ecall.o := n
3438
endif
3539
endif
3640

@@ -88,7 +92,7 @@ obj-$(CONFIG_DYNAMIC_FTRACE) += mcount-dyn.o
8892

8993
obj-$(CONFIG_PERF_EVENTS) += perf_callchain.o
9094
obj-$(CONFIG_HAVE_PERF_REGS) += perf_regs.o
91-
obj-$(CONFIG_RISCV_SBI) += sbi.o
95+
obj-$(CONFIG_RISCV_SBI) += sbi.o sbi_ecall.o
9296
ifeq ($(CONFIG_RISCV_SBI), y)
9397
obj-$(CONFIG_SMP) += sbi-ipi.o
9498
obj-$(CONFIG_SMP) += cpu_ops_sbi.o

arch/riscv/kernel/sbi.c

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414
#include <asm/smp.h>
1515
#include <asm/tlbflush.h>
1616

17-
#define CREATE_TRACE_POINTS
18-
#include <asm/trace.h>
19-
2017
/* default SBI version is 0.1 */
2118
unsigned long sbi_spec_version __ro_after_init = SBI_SPEC_VERSION_DEFAULT;
2219
EXPORT_SYMBOL(sbi_spec_version);
@@ -27,55 +24,6 @@ static int (*__sbi_rfence)(int fid, const struct cpumask *cpu_mask,
2724
unsigned long start, unsigned long size,
2825
unsigned long arg4, unsigned long arg5) __ro_after_init;
2926

30-
struct sbiret __sbi_ecall(unsigned long arg0, unsigned long arg1,
31-
unsigned long arg2, unsigned long arg3,
32-
unsigned long arg4, unsigned long arg5,
33-
int fid, int ext)
34-
{
35-
struct sbiret ret;
36-
37-
trace_sbi_call(ext, fid);
38-
39-
register uintptr_t a0 asm ("a0") = (uintptr_t)(arg0);
40-
register uintptr_t a1 asm ("a1") = (uintptr_t)(arg1);
41-
register uintptr_t a2 asm ("a2") = (uintptr_t)(arg2);
42-
register uintptr_t a3 asm ("a3") = (uintptr_t)(arg3);
43-
register uintptr_t a4 asm ("a4") = (uintptr_t)(arg4);
44-
register uintptr_t a5 asm ("a5") = (uintptr_t)(arg5);
45-
register uintptr_t a6 asm ("a6") = (uintptr_t)(fid);
46-
register uintptr_t a7 asm ("a7") = (uintptr_t)(ext);
47-
asm volatile ("ecall"
48-
: "+r" (a0), "+r" (a1)
49-
: "r" (a2), "r" (a3), "r" (a4), "r" (a5), "r" (a6), "r" (a7)
50-
: "memory");
51-
ret.error = a0;
52-
ret.value = a1;
53-
54-
trace_sbi_return(ext, ret.error, ret.value);
55-
56-
return ret;
57-
}
58-
EXPORT_SYMBOL(__sbi_ecall);
59-
60-
int sbi_err_map_linux_errno(int err)
61-
{
62-
switch (err) {
63-
case SBI_SUCCESS:
64-
return 0;
65-
case SBI_ERR_DENIED:
66-
return -EPERM;
67-
case SBI_ERR_INVALID_PARAM:
68-
return -EINVAL;
69-
case SBI_ERR_INVALID_ADDRESS:
70-
return -EFAULT;
71-
case SBI_ERR_NOT_SUPPORTED:
72-
case SBI_ERR_FAILURE:
73-
default:
74-
return -ENOTSUPP;
75-
};
76-
}
77-
EXPORT_SYMBOL(sbi_err_map_linux_errno);
78-
7927
#ifdef CONFIG_RISCV_SBI_V01
8028
static unsigned long __sbi_v01_cpumask_to_hartmask(const struct cpumask *cpu_mask)
8129
{
@@ -535,17 +483,6 @@ long sbi_probe_extension(int extid)
535483
}
536484
EXPORT_SYMBOL(sbi_probe_extension);
537485

538-
static long __sbi_base_ecall(int fid)
539-
{
540-
struct sbiret ret;
541-
542-
ret = sbi_ecall(SBI_EXT_BASE, fid, 0, 0, 0, 0, 0, 0);
543-
if (!ret.error)
544-
return ret.value;
545-
else
546-
return sbi_err_map_linux_errno(ret.error);
547-
}
548-
549486
static inline long sbi_get_spec_version(void)
550487
{
551488
return __sbi_base_ecall(SBI_EXT_BASE_GET_SPEC_VERSION);

arch/riscv/kernel/sbi_ecall.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2024 Rivos Inc. */
3+
4+
#include <asm/sbi.h>
5+
#define CREATE_TRACE_POINTS
6+
#include <asm/trace.h>
7+
8+
long __sbi_base_ecall(int fid)
9+
{
10+
struct sbiret ret;
11+
12+
ret = sbi_ecall(SBI_EXT_BASE, fid, 0, 0, 0, 0, 0, 0);
13+
if (!ret.error)
14+
return ret.value;
15+
else
16+
return sbi_err_map_linux_errno(ret.error);
17+
}
18+
EXPORT_SYMBOL(__sbi_base_ecall);
19+
20+
struct sbiret __sbi_ecall(unsigned long arg0, unsigned long arg1,
21+
unsigned long arg2, unsigned long arg3,
22+
unsigned long arg4, unsigned long arg5,
23+
int fid, int ext)
24+
{
25+
struct sbiret ret;
26+
27+
trace_sbi_call(ext, fid);
28+
29+
register uintptr_t a0 asm ("a0") = (uintptr_t)(arg0);
30+
register uintptr_t a1 asm ("a1") = (uintptr_t)(arg1);
31+
register uintptr_t a2 asm ("a2") = (uintptr_t)(arg2);
32+
register uintptr_t a3 asm ("a3") = (uintptr_t)(arg3);
33+
register uintptr_t a4 asm ("a4") = (uintptr_t)(arg4);
34+
register uintptr_t a5 asm ("a5") = (uintptr_t)(arg5);
35+
register uintptr_t a6 asm ("a6") = (uintptr_t)(fid);
36+
register uintptr_t a7 asm ("a7") = (uintptr_t)(ext);
37+
asm volatile ("ecall"
38+
: "+r" (a0), "+r" (a1)
39+
: "r" (a2), "r" (a3), "r" (a4), "r" (a5), "r" (a6), "r" (a7)
40+
: "memory");
41+
ret.error = a0;
42+
ret.value = a1;
43+
44+
trace_sbi_return(ext, ret.error, ret.value);
45+
46+
return ret;
47+
}
48+
EXPORT_SYMBOL(__sbi_ecall);

arch/riscv/kernel/traps_misaligned.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ int handle_misaligned_load(struct pt_regs *regs)
417417

418418
val.data_u64 = 0;
419419
if (user_mode(regs)) {
420-
if (raw_copy_from_user(&val, (u8 __user *)addr, len))
420+
if (copy_from_user(&val, (u8 __user *)addr, len))
421421
return -1;
422422
} else {
423423
memcpy(&val, (u8 *)addr, len);
@@ -515,7 +515,7 @@ int handle_misaligned_store(struct pt_regs *regs)
515515
return -EOPNOTSUPP;
516516

517517
if (user_mode(regs)) {
518-
if (raw_copy_to_user((u8 __user *)addr, &val, len))
518+
if (copy_to_user((u8 __user *)addr, &val, len))
519519
return -1;
520520
} else {
521521
memcpy((u8 *)addr, &val, len);

arch/riscv/mm/init.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ static void __init setup_bootmem(void)
252252
* The size of the linear page mapping may restrict the amount of
253253
* usable RAM.
254254
*/
255-
if (IS_ENABLED(CONFIG_64BIT)) {
255+
if (IS_ENABLED(CONFIG_64BIT) && IS_ENABLED(CONFIG_MMU)) {
256256
max_mapped_addr = __pa(PAGE_OFFSET) + KERN_VIRT_SIZE;
257257
memblock_cap_memory_range(phys_ram_base,
258258
max_mapped_addr - phys_ram_base);

tools/testing/selftests/riscv/mm/mmap_bottomup.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
TEST(infinite_rlimit)
88
{
99
EXPECT_EQ(BOTTOM_UP, memory_layout());
10-
11-
TEST_MMAPS;
1210
}
1311

1412
TEST_HARNESS_MAIN

0 commit comments

Comments
 (0)