2021-07-21 08:02:41

by Alexandre Ghiti

[permalink] [raw]
Subject: [PATCH -fixes 0/3] Fixes regarding CONFIG_PHYS_RAM_BASE

The following commits:

7094e6acaf7a ("riscv: Simplify xip and !xip kernel address conversion macros")
9b79878ced8f ("riscv: Remove CONFIG_PHYS_RAM_BASE_FIXED")

expose CONFIG_PHYS_RAM_BASE for all kernel types whereas this value is
implementation-specific, so that breaks the kernel genericity.

The first patch in this patchset removes the usage of CONFIG_PHYS_RAM_BASE
by introducing a new global variable that holds this value.

The second patch reverts 9b79878ced8f ("riscv: Remove
CONFIG_PHYS_RAM_BASE_FIXED").

The last patch is an optimization 'symmetrical' to the one introduced in
the first patch: this is not a fix, then it is not necessary to pull
this into -fixes.

Alexandre Ghiti (3):
riscv: Get rid of CONFIG_PHYS_RAM_BASE in kernel physical address
conversion
Revert "riscv: Remove CONFIG_PHYS_RAM_BASE_FIXED"
riscv: Optimize kernel virtual address conversion macro

arch/riscv/Kconfig | 6 ++++++
arch/riscv/include/asm/page.h | 9 +++++----
arch/riscv/mm/init.c | 17 ++++++++++++-----
3 files changed, 23 insertions(+), 9 deletions(-)

--
2.30.2


2021-07-21 08:04:22

by Alexandre Ghiti

[permalink] [raw]
Subject: [PATCH -fixes 1/3] riscv: Get rid of CONFIG_PHYS_RAM_BASE in kernel physical address conversion

The usage of CONFIG_PHYS_RAM_BASE for all kernel types was a mistake:
this value is implementation-specific and this breaks the genericity of
the RISC-V kernel.

Fix this by introducing a new variable phys_ram_base that holds this
value at runtime and use it in the kernel physical address conversion
macro. Since this value is used only for XIP kernels, evaluate it only if
CONFIG_XIP_KERNEL is set which in addition optimizes this macro for
standard kernels at compile-time.

Signed-off-by: Alexandre Ghiti <[email protected]>
---
arch/riscv/include/asm/page.h | 7 ++++---
arch/riscv/mm/init.c | 17 ++++++++++++-----
2 files changed, 16 insertions(+), 8 deletions(-)

diff --git a/arch/riscv/include/asm/page.h b/arch/riscv/include/asm/page.h
index cca8764aed83..b0ca5058e7ae 100644
--- a/arch/riscv/include/asm/page.h
+++ b/arch/riscv/include/asm/page.h
@@ -103,6 +103,7 @@ struct kernel_mapping {
};

extern struct kernel_mapping kernel_map;
+extern phys_addr_t phys_ram_base;

#ifdef CONFIG_64BIT
#define is_kernel_mapping(x) \
@@ -113,9 +114,9 @@ extern struct kernel_mapping kernel_map;
#define linear_mapping_pa_to_va(x) ((void *)((unsigned long)(x) + kernel_map.va_pa_offset))
#define kernel_mapping_pa_to_va(y) ({ \
unsigned long _y = y; \
- (_y >= CONFIG_PHYS_RAM_BASE) ? \
- (void *)((unsigned long)(_y) + kernel_map.va_kernel_pa_offset + XIP_OFFSET) : \
- (void *)((unsigned long)(_y) + kernel_map.va_kernel_xip_pa_offset); \
+ (IS_ENABLED(CONFIG_XIP_KERNEL) && _y < phys_ram_base) ? \
+ (void *)((unsigned long)(_y) + kernel_map.va_kernel_xip_pa_offset) : \
+ (void *)((unsigned long)(_y) + kernel_map.va_kernel_pa_offset + XIP_OFFSET); \
})
#define __pa_to_va_nodebug(x) linear_mapping_pa_to_va(x)

diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c
index 269fc648ef3d..4ebe7e19c2b8 100644
--- a/arch/riscv/mm/init.c
+++ b/arch/riscv/mm/init.c
@@ -36,6 +36,9 @@ EXPORT_SYMBOL(kernel_map);
#define kernel_map (*(struct kernel_mapping *)XIP_FIXUP(&kernel_map))
#endif

+phys_addr_t phys_ram_base __ro_after_init;
+EXPORT_SYMBOL(phys_ram_base);
+
#ifdef CONFIG_XIP_KERNEL
extern char _xiprom[], _exiprom[];
#endif
@@ -153,7 +156,7 @@ static void __init setup_bootmem(void)
phys_addr_t vmlinux_end = __pa_symbol(&_end);
phys_addr_t vmlinux_start = __pa_symbol(&_start);
phys_addr_t max_mapped_addr = __pa(~(ulong)0);
- phys_addr_t dram_end;
+ phys_addr_t phys_ram_end;

#ifdef CONFIG_XIP_KERNEL
vmlinux_start = __pa_symbol(&_sdata);
@@ -174,18 +177,21 @@ static void __init setup_bootmem(void)
#endif
memblock_reserve(vmlinux_start, vmlinux_end - vmlinux_start);

- dram_end = memblock_end_of_DRAM();
+#ifndef CONFIG_XIP_KERNEL
+ phys_ram_base = memblock_start_of_DRAM();
+#endif
+ phys_ram_end = memblock_end_of_DRAM();
/*
* memblock allocator is not aware of the fact that last 4K bytes of
* the addressable memory can not be mapped because of IS_ERR_VALUE
* macro. Make sure that last 4k bytes are not usable by memblock
* if end of dram is equal to maximum addressable memory.
*/
- if (max_mapped_addr == (dram_end - 1))
+ if (max_mapped_addr == (phys_ram_end - 1))
memblock_set_current_limit(max_mapped_addr - 4096);

- min_low_pfn = PFN_UP(memblock_start_of_DRAM());
- max_low_pfn = max_pfn = PFN_DOWN(dram_end);
+ min_low_pfn = PFN_UP(phys_ram_base);
+ max_low_pfn = max_pfn = PFN_DOWN(phys_ram_end);

dma32_phys_limit = min(4UL * SZ_1G, (unsigned long)PFN_PHYS(max_low_pfn));
set_max_mapnr(max_low_pfn - ARCH_PFN_OFFSET);
@@ -544,6 +550,7 @@ asmlinkage void __init setup_vm(uintptr_t dtb_pa)
kernel_map.xiprom = (uintptr_t)CONFIG_XIP_PHYS_ADDR;
kernel_map.xiprom_sz = (uintptr_t)(&_exiprom) - (uintptr_t)(&_xiprom);

+ phys_ram_base = CONFIG_PHYS_RAM_BASE;
kernel_map.phys_addr = (uintptr_t)CONFIG_PHYS_RAM_BASE;
kernel_map.size = (uintptr_t)(&_end) - (uintptr_t)(&_sdata);

--
2.30.2

2021-07-21 08:04:58

by Alexandre Ghiti

[permalink] [raw]
Subject: [PATCH -fixes 2/3] Revert "riscv: Remove CONFIG_PHYS_RAM_BASE_FIXED"

This reverts commit 9b79878ced8f7ab85c57623f8b1f6882e484a316.

The removal of this config exposes CONFIG_PHYS_RAM_BASE for all kernel
types: this value being implementation-specific, this breaks the
genericity of the RISC-V kernel so revert it.

Signed-off-by: Alexandre Ghiti <[email protected]>
---
arch/riscv/Kconfig | 6 ++++++
1 file changed, 6 insertions(+)

diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig
index 8fcceb8eda07..6a02caf49cde 100644
--- a/arch/riscv/Kconfig
+++ b/arch/riscv/Kconfig
@@ -494,8 +494,13 @@ config STACKPROTECTOR_PER_TASK
def_bool y
depends on STACKPROTECTOR && CC_HAVE_STACKPROTECTOR_TLS

+config PHYS_RAM_BASE_FIXED
+ bool "Explicitly specified physical RAM address"
+ default n
+
config PHYS_RAM_BASE
hex "Platform Physical RAM address"
+ depends on PHYS_RAM_BASE_FIXED
default "0x80000000"
help
This is the physical address of RAM in the system. It has to be
@@ -508,6 +513,7 @@ config XIP_KERNEL
# This prevents XIP from being enabled by all{yes,mod}config, which
# fail to build since XIP doesn't support large kernels.
depends on !COMPILE_TEST
+ select PHYS_RAM_BASE_FIXED
help
Execute-In-Place allows the kernel to run from non-volatile storage
directly addressable by the CPU, such as NOR flash. This saves RAM
--
2.30.2

2021-07-21 08:10:09

by Alexandre Ghiti

[permalink] [raw]
Subject: [PATCH -fixes 3/3] riscv: Optimize kernel virtual address conversion macro

The current test in kernel_mapping_va_to_pa only applies when
CONFIG_XIP_KERNEL is set, so use IS_ENABLED to optimize this macro at
compile-time in standard kernels that do not require this test.

Signed-off-by: Alexandre Ghiti <[email protected]>
---
arch/riscv/include/asm/page.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/riscv/include/asm/page.h b/arch/riscv/include/asm/page.h
index b0ca5058e7ae..10dc063868f6 100644
--- a/arch/riscv/include/asm/page.h
+++ b/arch/riscv/include/asm/page.h
@@ -123,7 +123,7 @@ extern phys_addr_t phys_ram_base;
#define linear_mapping_va_to_pa(x) ((unsigned long)(x) - kernel_map.va_pa_offset)
#define kernel_mapping_va_to_pa(y) ({ \
unsigned long _y = y; \
- (_y < kernel_map.virt_addr + XIP_OFFSET) ? \
+ (IS_ENABLED(CONFIG_XIP_KERNEL) && _y < kernel_map.virt_addr + XIP_OFFSET) ? \
((unsigned long)(_y) - kernel_map.va_kernel_xip_pa_offset) : \
((unsigned long)(_y) - kernel_map.va_kernel_pa_offset - XIP_OFFSET); \
})
--
2.30.2

2021-07-21 18:47:01

by Emil Renner Berthing

[permalink] [raw]
Subject: Re: [PATCH -fixes 0/3] Fixes regarding CONFIG_PHYS_RAM_BASE

On Wed, 21 Jul 2021 at 10:00, Alexandre Ghiti <[email protected]> wrote:
>
> The following commits:
>
> 7094e6acaf7a ("riscv: Simplify xip and !xip kernel address conversion macros")
> 9b79878ced8f ("riscv: Remove CONFIG_PHYS_RAM_BASE_FIXED")
>
> expose CONFIG_PHYS_RAM_BASE for all kernel types whereas this value is
> implementation-specific, so that breaks the kernel genericity.
>
> The first patch in this patchset removes the usage of CONFIG_PHYS_RAM_BASE
> by introducing a new global variable that holds this value.
>
> The second patch reverts 9b79878ced8f ("riscv: Remove
> CONFIG_PHYS_RAM_BASE_FIXED").
>
> The last patch is an optimization 'symmetrical' to the one introduced in
> the first patch: this is not a fix, then it is not necessary to pull
> this into -fixes.

Hi Alex,

Thank you, this works fine on my BeagleV Beta board.

If I'm not mistaken after this series all uses of CONFIG_PHYS_RAM if
protected by #ifdef CONFIG_XIP_KERNEL, so maybe we can remove the
middleman, CONFIG_PHYS_RAM_BASE_FIXED, and just let CONFIG_PHYS_RAM
directly depend on CONFIG_XIP_KERNEL.

Don't let that delay this series though. I'd still rather have this
fixed in 5.14 as is.

If it makes any difference you can add this for the series:
Tested-by: Emil Renner Berthing <[email protected]>

/Emil

2021-07-22 15:38:12

by Jisheng Zhang

[permalink] [raw]
Subject: Re: [PATCH -fixes 0/3] Fixes regarding CONFIG_PHYS_RAM_BASE

On Wed, 21 Jul 2021 09:59:34 +0200
Alexandre Ghiti <[email protected]> wrote:

> The following commits:
>
> 7094e6acaf7a ("riscv: Simplify xip and !xip kernel address conversion macros")
> 9b79878ced8f ("riscv: Remove CONFIG_PHYS_RAM_BASE_FIXED")
>
> expose CONFIG_PHYS_RAM_BASE for all kernel types whereas this value is
> implementation-specific, so that breaks the kernel genericity.
>
> The first patch in this patchset removes the usage of CONFIG_PHYS_RAM_BASE
> by introducing a new global variable that holds this value.
>
> The second patch reverts 9b79878ced8f ("riscv: Remove
> CONFIG_PHYS_RAM_BASE_FIXED").
>
> The last patch is an optimization 'symmetrical' to the one introduced in
> the first patch: this is not a fix, then it is not necessary to pull
> this into -fixes.
>

Reviewed-by: Jisheng Zhang <[email protected]>

> Alexandre Ghiti (3):
> riscv: Get rid of CONFIG_PHYS_RAM_BASE in kernel physical address
> conversion
> Revert "riscv: Remove CONFIG_PHYS_RAM_BASE_FIXED"
> riscv: Optimize kernel virtual address conversion macro
>
> arch/riscv/Kconfig | 6 ++++++
> arch/riscv/include/asm/page.h | 9 +++++----
> arch/riscv/mm/init.c | 17 ++++++++++++-----
> 3 files changed, 23 insertions(+), 9 deletions(-)
>


2021-08-07 16:38:47

by Palmer Dabbelt

[permalink] [raw]
Subject: Re: [PATCH -fixes 3/3] riscv: Optimize kernel virtual address conversion macro

On Wed, 21 Jul 2021 00:59:37 PDT (-0700), [email protected] wrote:
> The current test in kernel_mapping_va_to_pa only applies when
> CONFIG_XIP_KERNEL is set, so use IS_ENABLED to optimize this macro at
> compile-time in standard kernels that do not require this test.
>
> Signed-off-by: Alexandre Ghiti <[email protected]>
> ---
> arch/riscv/include/asm/page.h | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/arch/riscv/include/asm/page.h b/arch/riscv/include/asm/page.h
> index b0ca5058e7ae..10dc063868f6 100644
> --- a/arch/riscv/include/asm/page.h
> +++ b/arch/riscv/include/asm/page.h
> @@ -123,7 +123,7 @@ extern phys_addr_t phys_ram_base;
> #define linear_mapping_va_to_pa(x) ((unsigned long)(x) - kernel_map.va_pa_offset)
> #define kernel_mapping_va_to_pa(y) ({ \
> unsigned long _y = y; \
> - (_y < kernel_map.virt_addr + XIP_OFFSET) ? \
> + (IS_ENABLED(CONFIG_XIP_KERNEL) && _y < kernel_map.virt_addr + XIP_OFFSET) ? \
> ((unsigned long)(_y) - kernel_map.va_kernel_xip_pa_offset) : \
> ((unsigned long)(_y) - kernel_map.va_kernel_pa_offset - XIP_OFFSET); \
> })

IIUC this isn't actually a fix? The other two are, though, so
they're on fixes.

Thanks!

2021-08-07 19:33:46

by Alexandre Ghiti

[permalink] [raw]
Subject: Re: [PATCH -fixes 3/3] riscv: Optimize kernel virtual address conversion macro

Le 7/08/2021 ? 18:36, Palmer Dabbelt a ?crit?:
> On Wed, 21 Jul 2021 00:59:37 PDT (-0700), [email protected] wrote:
>> The current test in kernel_mapping_va_to_pa only applies when
>> CONFIG_XIP_KERNEL is set, so use IS_ENABLED to optimize this macro at
>> compile-time in standard kernels that do not require this test.
>>
>> Signed-off-by: Alexandre Ghiti <[email protected]>
>> ---
>> ?arch/riscv/include/asm/page.h | 2 +-
>> ?1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/arch/riscv/include/asm/page.h
>> b/arch/riscv/include/asm/page.h
>> index b0ca5058e7ae..10dc063868f6 100644
>> --- a/arch/riscv/include/asm/page.h
>> +++ b/arch/riscv/include/asm/page.h
>> @@ -123,7 +123,7 @@ extern phys_addr_t phys_ram_base;
>> ?#define linear_mapping_va_to_pa(x)??? ((unsigned long)(x) -
>> kernel_map.va_pa_offset)
>> ?#define kernel_mapping_va_to_pa(y) ({??????????????????????? \
>> ???? unsigned long _y = y;??????????????????????????? \
>> -??? (_y < kernel_map.virt_addr + XIP_OFFSET) ???????????????????? \
>> +??? (IS_ENABLED(CONFIG_XIP_KERNEL) && _y < kernel_map.virt_addr +
>> XIP_OFFSET) ???? \
>> ???????? ((unsigned long)(_y) - kernel_map.va_kernel_xip_pa_offset)
>> :??????? \
>> ???????? ((unsigned long)(_y) - kernel_map.va_kernel_pa_offset -
>> XIP_OFFSET);??? \
>> ???? })
>
> IIUC this isn't actually a fix?? The other two are, though, so they're
> on fixes.

Indeed this is not a fix, this is symmetrical to what I have done in
patch 1 so that seemed natural to land in the same patchset. Let me know
if you want me to resend it on its own.

Thanks,

Alex

>
> Thanks!
>
> _______________________________________________
> linux-riscv mailing list
> [email protected]
> http://lists.infradead.org/mailman/listinfo/linux-riscv

2021-08-08 12:39:44

by Vitaly Wool

[permalink] [raw]
Subject: Re: [PATCH -fixes 3/3] riscv: Optimize kernel virtual address conversion macro

On Wed, Jul 21, 2021 at 10:04 AM Alexandre Ghiti <[email protected]> wrote:
>
> The current test in kernel_mapping_va_to_pa only applies when
> CONFIG_XIP_KERNEL is set, so use IS_ENABLED to optimize this macro at
> compile-time in standard kernels that do not require this test.
>
> Signed-off-by: Alexandre Ghiti <[email protected]>

Just in case you need it, here comes

Reviewed-By: Vitaly Wool <[email protected]>

> ---
> arch/riscv/include/asm/page.h | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/arch/riscv/include/asm/page.h b/arch/riscv/include/asm/page.h
> index b0ca5058e7ae..10dc063868f6 100644
> --- a/arch/riscv/include/asm/page.h
> +++ b/arch/riscv/include/asm/page.h
> @@ -123,7 +123,7 @@ extern phys_addr_t phys_ram_base;
> #define linear_mapping_va_to_pa(x) ((unsigned long)(x) - kernel_map.va_pa_offset)
> #define kernel_mapping_va_to_pa(y) ({ \
> unsigned long _y = y; \
> - (_y < kernel_map.virt_addr + XIP_OFFSET) ? \
> + (IS_ENABLED(CONFIG_XIP_KERNEL) && _y < kernel_map.virt_addr + XIP_OFFSET) ? \
> ((unsigned long)(_y) - kernel_map.va_kernel_xip_pa_offset) : \
> ((unsigned long)(_y) - kernel_map.va_kernel_pa_offset - XIP_OFFSET); \
> })
> --
> 2.30.2
>
>
> _______________________________________________
> linux-riscv mailing list
> [email protected]
> http://lists.infradead.org/mailman/listinfo/linux-riscv

2021-08-12 06:30:30

by Palmer Dabbelt

[permalink] [raw]
Subject: Re: [PATCH -fixes 3/3] riscv: Optimize kernel virtual address conversion macro

On Sat, 07 Aug 2021 12:31:24 PDT (-0700), [email protected] wrote:
> Le 7/08/2021 à 18:36, Palmer Dabbelt a écrit :
>> On Wed, 21 Jul 2021 00:59:37 PDT (-0700), [email protected] wrote:
>>> The current test in kernel_mapping_va_to_pa only applies when
>>> CONFIG_XIP_KERNEL is set, so use IS_ENABLED to optimize this macro at
>>> compile-time in standard kernels that do not require this test.
>>>
>>> Signed-off-by: Alexandre Ghiti <[email protected]>
>>> ---
>>>  arch/riscv/include/asm/page.h | 2 +-
>>>  1 file changed, 1 insertion(+), 1 deletion(-)
>>>
>>> diff --git a/arch/riscv/include/asm/page.h
>>> b/arch/riscv/include/asm/page.h
>>> index b0ca5058e7ae..10dc063868f6 100644
>>> --- a/arch/riscv/include/asm/page.h
>>> +++ b/arch/riscv/include/asm/page.h
>>> @@ -123,7 +123,7 @@ extern phys_addr_t phys_ram_base;
>>>  #define linear_mapping_va_to_pa(x)    ((unsigned long)(x) -
>>> kernel_map.va_pa_offset)
>>>  #define kernel_mapping_va_to_pa(y) ({                        \
>>>      unsigned long _y = y;                            \
>>> -    (_y < kernel_map.virt_addr + XIP_OFFSET) ?                    \
>>> +    (IS_ENABLED(CONFIG_XIP_KERNEL) && _y < kernel_map.virt_addr +
>>> XIP_OFFSET) ?    \
>>>          ((unsigned long)(_y) - kernel_map.va_kernel_xip_pa_offset)
>>> :        \
>>>          ((unsigned long)(_y) - kernel_map.va_kernel_pa_offset -
>>> XIP_OFFSET);    \
>>>      })
>>
>> IIUC this isn't actually a fix?  The other two are, though, so they're
>> on fixes.
>
> Indeed this is not a fix, this is symmetrical to what I have done in
> patch 1 so that seemed natural to land in the same patchset. Let me know
> if you want me to resend it on its own.

That's fine, it was just explicitly called out as a fix in the subject
line so I wanted to make sure I wasn't missing something. This is on
for-next.

Thanks!