optimize pgtable_l4|l5_enabled
cover optimize pgtable_l4|l5_enabled
In 2022, I tried to optimize pgtable_l4|l5_enabled use static branch[3] But as is known, static branch has some drawbacks.
This series is another round of optmizing pgtable_l4|l5_enabled, but with another mechanism. Below the is normal cover-letter.
The pgtable_l4|[l5]_enabled check sits at hot code path, performance is impacted a lot. Since pgtable_l4|[l5]_enabled isn't changed after boot, we can use alternative mechanism to optimize them.
So the question is whether we can add RISCV_ISA_EXT_SV48/SV5 and use riscv_has_extension_*() or not. Although, per [1] and [2], SV48 and SV57 are ISA exensions too, RISCV_ISA_EXT_SV48/SV57 are to describe hw supported extensions, while this doesn't mean the pgtable_l4|l5 is enabled, for example, we may pass no5lvl/no4lvl kernel boot args or explicitly ask for SV39 by setting dt mmu-type property as "riscv,sv39". If we clear RISCV_ISA_EXT_SV48|SV57, then internal extension queries and potentially userspace reporting can no longer distinguish “unsupported” from “supported but disabled.”
Introduce cap framework to describe the capabilities selected by kernel. It also uses similar alternatives mechanism as the riscv_has_extension_*() helpers.
After that, use it to optimize pgtable_l4|l5_enabled.
For the typical access_ok(addr, 1);
before the patch:
...
auipc a5,0xb43
lbu a5,100(a5) # ffffffff80b51f68 <pgtable_l5_enabled>
bnez a5,ffffffff8000ef46 <foo+0x56>
auipc a5,0xb43
lbu a5,91(a5) # ffffffff80b51f69 <pgtable_l4_enabled>
beqz a5,ffffffff8000ef5a <foo+0x6a>
...
after the patch:
there are only two j or nop instructions which avoid memory load and test branch.
Initial test lmbench's lat_syscall write on TH1520 platforms shows that the write syscall latency is reduced by about 2.38%.
Jisheng Zhang (3):
riscv: remove RISCV_ALTERNATIVE Kconfig option
riscv: convert pgtable_l4|l5_enabled to inline function
riscv: introduce cap framework and use it to optimize
pgtable_l4|l5_enabled
arch/riscv/Kconfig | 24 +------ arch/riscv/Kconfig.errata | 5 +- arch/riscv/include/asm/alternative-macros.h | 24 ------- arch/riscv/include/asm/alternative.h | 12 +--- arch/riscv/include/asm/cpufeature-macros.h | 54 +++++++++++++-- arch/riscv/include/asm/cpufeature.h | 8 +-- arch/riscv/include/asm/hwcap.h | 6 ++ arch/riscv/include/asm/pgalloc.h | 14 ++-- arch/riscv/include/asm/pgtable-32.h | 4 +- arch/riscv/include/asm/pgtable-64.h | 62 +++++++++++------ arch/riscv/include/asm/pgtable.h | 4 +- arch/riscv/include/asm/vendor_extensions.h | 18 ++--- arch/riscv/include/asm/vmalloc.h | 5 +- arch/riscv/kernel/Makefile | 2 +- arch/riscv/kernel/alternative.c | 24 +++++-- arch/riscv/kernel/cpu.c | 4 +- arch/riscv/kernel/cpufeature.c | 43 +++++++++--- arch/riscv/mm/init.c | 73 ++++++++++++--------- arch/riscv/mm/kasan_init.c | 20 +++--- arch/riscv/mm/pgtable.c | 4 +- arch/riscv/mm/ptdump.c | 4 +- 21 files changed, 230 insertions(+), 184 deletions(-)
Signature · 1 line
2.53.0
PATCH 1/3 riscv: remove RISCV_ALTERNATIVE Kconfig option
riscv always selects RISCV_ALTERNATIVE now, so we can remove this Kconfig option and enable RISCV_ALTERNATIVE code unconditionally.
--- arch/riscv/Kconfig | 23 -------------------- arch/riscv/Kconfig.errata | 5 +---- arch/riscv/include/asm/alternative-macros.h | 24 --------------------- arch/riscv/include/asm/alternative.h | 10 --------- arch/riscv/include/asm/cpufeature-macros.h | 10 ++------- arch/riscv/include/asm/cpufeature.h | 6 ++---- arch/riscv/include/asm/vendor_extensions.h | 18 ++++++---------- arch/riscv/kernel/Makefile | 2 +- arch/riscv/kernel/cpufeature.c | 2 -- 9 files changed, 12 insertions(+), 88 deletions(-)
arch/riscv/Kconfig +0 -23
@@ -218,7 +218,6 @@ config RISCV select PCI_ECAM if (ACPI && PCI) select PCI_MSI if PCI select RELOCATABLE if !MMU && !PHYS_RAM_BASE_FIXED - select RISCV_ALTERNATIVE select RISCV_APLIC select RISCV_IMSIC select RISCV_INTC
@@ -539,17 +538,8 @@ config RISCV_COMBO_SPINLOCKS endchoice -config RISCV_ALTERNATIVE - bool - help - This Kconfig allows the kernel to automatically patch the - erratum or cpufeature required by the execution platform at run - time. The code patching overhead is minimal, as it's only done - once at boot and once on each module load. - config RISCV_ALTERNATIVE_EARLY bool - depends on RISCV_ALTERNATIVE help Allows early patching of the kernel for special errata
@@ -578,7 +568,6 @@ config RISCV_ISA_SUPM config RISCV_ISA_SVNAPOT bool "Svnapot extension support for supervisor mode NAPOT pages" depends on 64BIT && MMU - depends on RISCV_ALTERNATIVE default y help Enable support for the Svnapot ISA-extension when it is detected
@@ -615,7 +604,6 @@ config RISCV_ISA_SSQOSID config RISCV_ISA_SVPBMT bool "Svpbmt extension support for supervisor mode page-based memory types" depends on 64BIT && MMU - depends on RISCV_ALTERNATIVE default y help Add support for the Svpbmt ISA-extension (Supervisor-mode:
@@ -685,7 +673,6 @@ config RISCV_ISA_V_PREEMPTIVE config RISCV_ISA_ZAWRS bool "Zawrs extension support for more efficient busy waiting" - depends on RISCV_ALTERNATIVE default y help The Zawrs extension defines instructions to be used in polling loops
@@ -704,7 +691,6 @@ config TOOLCHAIN_HAS_ZABHA config RISCV_ISA_ZABHA bool "Zabha extension support for atomic byte/halfword operations" depends on TOOLCHAIN_HAS_ZABHA - depends on RISCV_ALTERNATIVE default y help Enable the use of the Zabha ISA-extension to implement kernel
@@ -721,7 +707,6 @@ config TOOLCHAIN_HAS_ZACAS config RISCV_ISA_ZACAS bool "Zacas extension support for atomic CAS" - depends on RISCV_ALTERNATIVE default y help Enable the use of the Zacas ISA-extension to implement kernel atomic
@@ -766,7 +751,6 @@ config RISCV_ISA_ZBA config RISCV_ISA_ZBB bool "Zbb extension support for bit manipulation instructions" - depends on RISCV_ALTERNATIVE default y help Add support for enabling optimisations in the kernel when the
@@ -791,7 +775,6 @@ config RISCV_ISA_ZBC bool "Zbc extension support for carry-less multiplication instructions" depends on TOOLCHAIN_HAS_ZBC depends on MMU - depends on RISCV_ALTERNATIVE default y help Adds support to dynamically detect the presence of the Zbc
@@ -813,7 +796,6 @@ config TOOLCHAIN_HAS_ZBKB config RISCV_ISA_ZBKB bool "Zbkb extension support for bit manipulation instructions" depends on TOOLCHAIN_HAS_ZBKB - depends on RISCV_ALTERNATIVE default y help Adds support to dynamically detect the presence of the ZBKB
@@ -827,7 +809,6 @@ config RISCV_ISA_ZBKB config RISCV_ISA_ZICBOM bool "Zicbom extension support for non-coherent DMA operation" depends on MMU - depends on RISCV_ALTERNATIVE default y select RISCV_DMA_NONCOHERENT select DMA_DIRECT_REMAP
@@ -843,7 +824,6 @@ config RISCV_ISA_ZICBOM config RISCV_ISA_ZICBOZ bool "Zicboz extension support for faster zeroing of memory" - depends on RISCV_ALTERNATIVE default y help Enable the use of the Zicboz extension (cbo.zero instruction)
@@ -856,7 +836,6 @@ config RISCV_ISA_ZICBOZ config RISCV_ISA_ZICBOP bool "Zicbop extension support for cache block prefetch" depends on MMU - depends on RISCV_ALTERNATIVE default y help Adds support to dynamically detect the presence of the ZICBOP
@@ -871,7 +850,6 @@ config RISCV_ISA_ZICBOP config RISCV_ISA_SVRSW60T59B bool "Svrsw60t59b extension support for using PTE bits 60 and 59" depends on MMU && 64BIT - depends on RISCV_ALTERNATIVE default y help Adds support to dynamically detect the presence of the Svrsw60t59b
@@ -1204,7 +1182,6 @@ config RISCV_USER_CFI bool "riscv userspace control flow integrity" depends on 64BIT && MMU && \ $(cc-option,-mabi=lp64 -march=rv64ima_zicfiss_zicfilp -fcf-protection=full) - depends on RISCV_ALTERNATIVE select RISCV_SBI select ARCH_HAS_USER_SHADOW_STACK select ARCH_USES_HIGH_VMA_FLAGS
arch/riscv/Kconfig.errata +1 -4
@@ -2,7 +2,7 @@ menu "CPU errata selection" config ERRATA_ANDES bool "Andes AX45MP errata" - depends on RISCV_ALTERNATIVE && RISCV_SBI + depends on RISCV_SBI help All Andes errata Kconfig depend on this Kconfig. Disabling this Kconfig will disable all Andes errata. Please say "Y"
@@ -23,7 +23,6 @@ config ERRATA_ANDES_CMO config ERRATA_MIPS bool "MIPS errata" - depends on RISCV_ALTERNATIVE help All MIPS errata Kconfig depend on this Kconfig. Disabling this Kconfig will disable all MIPS errata. Please say "Y"
@@ -46,7 +45,6 @@ config ERRATA_MIPS_P8700_PAUSE_OPCODE config ERRATA_SIFIVE bool "SiFive errata" - depends on RISCV_ALTERNATIVE help All SiFive errata Kconfig depend on this Kconfig. Disabling this Kconfig will disable all SiFive errata. Please say "Y"
@@ -98,7 +96,6 @@ config ERRATA_STARFIVE_JH7100 config ERRATA_THEAD bool "T-HEAD errata" - depends on RISCV_ALTERNATIVE help All T-HEAD errata Kconfig depend on this Kconfig. Disabling this Kconfig will disable all T-HEAD errata. Please say "Y"
arch/riscv/include/asm/alternative-macros.h +0 -24
@@ -2,8 +2,6 @@#ifndef __ASM_ALTERNATIVE_MACROS_H#define __ASM_ALTERNATIVE_MACROS_H -#ifdef CONFIG_RISCV_ALTERNATIVE -#ifdef __ASSEMBLER__ .macro ALT_ENTRY oldptr newptr vendor_id patch_id new_len
@@ -108,28 +106,6 @@__ALTERNATIVE_CFG_2 (old_c, new_c_1, vendor_id_1, patch_id_1,IS_ENABLED (CONFIG_k_1), \ new_c_2, vendor_id_2, patch_id_2,IS_ENABLED (CONFIG_k_2)) -#else /* CONFIG_RISCV_ALTERNATIVE */ -#ifdef __ASSEMBLER__ - -.macro ALTERNATIVE_CFG old_c - \old_c -.endm - -#define __ALTERNATIVE_CFG (old_c, ...) ALTERNATIVE_CFG old_c -#define __ALTERNATIVE_CFG_2 (old_c, ...) ALTERNATIVE_CFG old_c - -#else /* !__ASSEMBLER__ */ - -#define __ALTERNATIVE_CFG (old_c, ...) old_c "\n" -#define __ALTERNATIVE_CFG_2 (old_c, ...) old_c "\n" - -#endif /* __ASSEMBLER__ */ - -#define _ALTERNATIVE_CFG (old_c, ...) __ALTERNATIVE_CFG(old_c) -#define _ALTERNATIVE_CFG_2 (old_c, ...) __ALTERNATIVE_CFG_2(old_c) - -#endif /* CONFIG_RISCV_ALTERNATIVE */ -/* * Usage: * ALTERNATIVE(old_content, new_content, vendor_id, patch_id, CONFIG_k)
arch/riscv/include/asm/alternative.h +0 -10
@@ -10,8 +10,6 @@#ifndef __ASSEMBLER__ -#ifdef CONFIG_RISCV_ALTERNATIVE -#include <linux/init.h> #include <linux/kernel.h> #include <linux/types.h>
@@ -61,13 +59,5 @@ void thead_errata_patch_func(struct alt_entry *begin, struct alt_entry *end,void riscv_cpufeature_patch_func (struct alt_entry *begin,struct alt_entry *end,unsigned int stage); -#else /* CONFIG_RISCV_ALTERNATIVE */ - -static inline void apply_boot_alternatives (void ) { } -static inline void apply_early_boot_alternatives (void ) { } -static inline void apply_module_alternatives (void *start,size_t length) { } - -#endif /* CONFIG_RISCV_ALTERNATIVE */ -#endif #endif
arch/riscv/include/asm/cpufeature-macros.h +2 -8
@@ -47,20 +47,14 @@ static __always_inline bool riscv_has_extension_unlikely(const unsigned long ext {compiletime_assert (ext <RISCV_ISA_EXT_MAX ,"ext must be < RISCV_ISA_EXT_MAX" ); -if (IS_ENABLED (CONFIG_RISCV_ALTERNATIVE )) -return __riscv_has_extension_unlikely (STANDARD_EXT , ext); - -return __riscv_isa_extension_available (NULL , ext); +return __riscv_has_extension_unlikely (STANDARD_EXT , ext); } static __always_inline bool riscv_has_extension_likely(const unsigned long ext) {compiletime_assert (ext <RISCV_ISA_EXT_MAX ,"ext must be < RISCV_ISA_EXT_MAX" ); -if (IS_ENABLED (CONFIG_RISCV_ALTERNATIVE )) -return __riscv_has_extension_likely (STANDARD_EXT , ext); - -return __riscv_isa_extension_available (NULL , ext); +return __riscv_has_extension_likely (STANDARD_EXT , ext); }#endif /* _ASM_CPUFEATURE_MACROS_H */
arch/riscv/include/asm/cpufeature.h +2 -4
@@ -134,8 +134,7 @@ static __always_inline bool riscv_cpu_has_extension_likely(int cpu, const unsign {compiletime_assert (ext <RISCV_ISA_EXT_MAX ,"ext must be < RISCV_ISA_EXT_MAX" ); -if (IS_ENABLED (CONFIG_RISCV_ALTERNATIVE ) && -__riscv_has_extension_likely (STANDARD_EXT , ext)) +if (__riscv_has_extension_likely (STANDARD_EXT , ext))return true;return __riscv_isa_extension_available (hart_isa[cpu].isa, ext);
@@ -145,8 +144,7 @@ static __always_inline bool riscv_cpu_has_extension_unlikely(int cpu, const unsi {compiletime_assert (ext <RISCV_ISA_EXT_MAX ,"ext must be < RISCV_ISA_EXT_MAX" ); -if (IS_ENABLED (CONFIG_RISCV_ALTERNATIVE ) && -__riscv_has_extension_unlikely (STANDARD_EXT , ext)) +if (__riscv_has_extension_unlikely (STANDARD_EXT , ext))return true;return __riscv_isa_extension_available (hart_isa[cpu].isa, ext);
arch/riscv/include/asm/vendor_extensions.h +6 -12
@@ -54,11 +54,9 @@ static __always_inline bool riscv_has_vendor_extension_likely(const unsigned lonif (!IS_ENABLED (CONFIG_RISCV_ISA_VENDOR_EXT ))return false; -if (IS_ENABLED (CONFIG_RISCV_ALTERNATIVE )) -return __riscv_has_extension_likely (vendor, - ext +RISCV_VENDOR_EXT_ALTERNATIVES_BASE ); +return __riscv_has_extension_likely (vendor, + ext +RISCV_VENDOR_EXT_ALTERNATIVES_BASE ); -return __riscv_isa_vendor_extension_available (VENDOR_EXT_ALL_CPUS , vendor, ext); } static __always_inline bool riscv_has_vendor_extension_unlikely(const unsigned long vendor,
@@ -67,11 +65,9 @@ static __always_inline bool riscv_has_vendor_extension_unlikely(const unsigned lif (!IS_ENABLED (CONFIG_RISCV_ISA_VENDOR_EXT ))return false; -if (IS_ENABLED (CONFIG_RISCV_ALTERNATIVE )) -return __riscv_has_extension_unlikely (vendor, - ext +RISCV_VENDOR_EXT_ALTERNATIVES_BASE ); +return __riscv_has_extension_unlikely (vendor, + ext +RISCV_VENDOR_EXT_ALTERNATIVES_BASE ); -return __riscv_isa_vendor_extension_available (VENDOR_EXT_ALL_CPUS , vendor, ext); } static __always_inline bool riscv_cpu_has_vendor_extension_likely(const unsigned long vendor,
@@ -80,8 +76,7 @@ static __always_inline bool riscv_cpu_has_vendor_extension_likely(const unsignedif (!IS_ENABLED (CONFIG_RISCV_ISA_VENDOR_EXT ))return false; -if (IS_ENABLED (CONFIG_RISCV_ALTERNATIVE ) && -__riscv_has_extension_likely (vendor, ext +RISCV_VENDOR_EXT_ALTERNATIVES_BASE )) +if (__riscv_has_extension_likely (vendor, ext +RISCV_VENDOR_EXT_ALTERNATIVES_BASE ))return true;return __riscv_isa_vendor_extension_available (cpu, vendor, ext);
@@ -94,8 +89,7 @@ static __always_inline bool riscv_cpu_has_vendor_extension_unlikely(const unsignif (!IS_ENABLED (CONFIG_RISCV_ISA_VENDOR_EXT ))return false; -if (IS_ENABLED (CONFIG_RISCV_ALTERNATIVE ) && -__riscv_has_extension_unlikely (vendor, ext +RISCV_VENDOR_EXT_ALTERNATIVES_BASE )) +if (__riscv_has_extension_unlikely (vendor, ext +RISCV_VENDOR_EXT_ALTERNATIVES_BASE ))return true;return __riscv_isa_vendor_extension_available (cpu, vendor, ext);
arch/riscv/kernel/Makefile +1 -1
@@ -48,7 +48,7 @@ always-$(KBUILD_BUILTIN) += vmlinux.lds obj-y += head.o obj-y += soc.o -obj-$(CONFIG_RISCV_ALTERNATIVE) += alternative.o +obj-y += alternative.o obj-y += cpu.o obj-y += cpufeature.o obj-y += entry.o
arch/riscv/kernel/cpufeature.c +0 -2
@@ -1217,7 +1217,6 @@ void __init riscv_user_isa_enable(void)pr_warn ("Zicbop disabled as it is unavailable on some harts\n" ); } -#ifdef CONFIG_RISCV_ALTERNATIVE /* * Alternative patch sites consider 48 bits when determining when to patch * the old instruction sequence with the new. These bits are broken into a
@@ -1306,4 +1305,3 @@ void __init_or_module riscv_cpufeature_patch_func(struct alt_entry *begin,mutex_unlock (&text_mutex); } } -#endif
Signature · 1 line
2.53.0
Re: [PATCH 1/3] riscv: remove RISCV_ALTERNATIVE Kconfig option
Quoted 2 lines · Jisheng Zhang
riscv always selects
RISCV_ALTERNATIVEnow, so we can remove thisKconfigoption and enableRISCV_ALTERNATIVEcode unconditionally.
I think it'd be nice to add a minor note about what's changed to make this true, but this seems sane to me.
Thanks,
Conor.
Quoted 390 lines
Signed-off-by Jisheng Zhang <[email protected]>--- arch/riscv/Kconfig | 23 -------------------- arch/riscv/Kconfig.errata | 5 +---- arch/riscv/include/asm/alternative-macros.h | 24 --------------------- arch/riscv/include/asm/alternative.h | 10 --------- arch/riscv/include/asm/cpufeature-macros.h | 10 ++------- arch/riscv/include/asm/cpufeature.h | 6 ++---- arch/riscv/include/asm/vendor_extensions.h | 18 ++++++---------- arch/riscv/kernel/Makefile | 2 +- arch/riscv/kernel/cpufeature.c | 2 -- 9 files changed, 12 insertions(+), 88 deletions(-)arch/riscv/Kconfig +0
-23@@ -218,7 +218,6 @@ config RISCV select PCI_ECAM if (ACPI && PCI) select PCI_MSI if PCI select RELOCATABLE if !MMU && !PHYS_RAM_BASE_FIXED - select RISCV_ALTERNATIVE select RISCV_APLIC select RISCV_IMSIC select RISCV_INTC@@ -539,17 +538,8 @@ config RISCV_COMBO_SPINLOCKS endchoice -config RISCV_ALTERNATIVE - bool - help - This Kconfig allows the kernel to automatically patch the - erratum or cpufeature required by the execution platform at run - time. The code patching overhead is minimal, as it's only done - once at boot and once on each module load. - config RISCV_ALTERNATIVE_EARLY bool - depends on RISCV_ALTERNATIVE help Allows early patching of the kernel for special errata@@ -578,7 +568,6 @@ config RISCV_ISA_SUPM config RISCV_ISA_SVNAPOT bool "Svnapot extension support for supervisor mode NAPOT pages" depends on 64BIT && MMU - depends on RISCV_ALTERNATIVE default y help Enable support for the Svnapot ISA-extension when it is detected@@ -615,7 +604,6 @@ config RISCV_ISA_SSQOSID config RISCV_ISA_SVPBMT bool "Svpbmt extension support for supervisor mode page-based memory types" depends on 64BIT && MMU - depends on RISCV_ALTERNATIVE default y help Add support for the Svpbmt ISA-extension (Supervisor-mode:@@ -685,7 +673,6 @@ config RISCV_ISA_V_PREEMPTIVE config RISCV_ISA_ZAWRS bool "Zawrs extension support for more efficient busy waiting" - depends on RISCV_ALTERNATIVE default y help The Zawrs extension defines instructions to be used in polling loops@@ -704,7 +691,6 @@ config TOOLCHAIN_HAS_ZABHA config RISCV_ISA_ZABHA bool "Zabha extension support for atomic byte/halfword operations" depends on TOOLCHAIN_HAS_ZABHA - depends on RISCV_ALTERNATIVE default y help Enable the use of the Zabha ISA-extension to implement kernel@@ -721,7 +707,6 @@ config TOOLCHAIN_HAS_ZACAS config RISCV_ISA_ZACAS bool "Zacas extension support for atomic CAS" - depends on RISCV_ALTERNATIVE default y help Enable the use of the Zacas ISA-extension to implement kernel atomic@@ -766,7 +751,6 @@ config RISCV_ISA_ZBA config RISCV_ISA_ZBB bool "Zbb extension support for bit manipulation instructions" - depends on RISCV_ALTERNATIVE default y help Add support for enabling optimisations in the kernel when the@@ -791,7 +775,6 @@ config RISCV_ISA_ZBC bool "Zbc extension support for carry-less multiplication instructions" depends on TOOLCHAIN_HAS_ZBC depends on MMU - depends on RISCV_ALTERNATIVE default y help Adds support to dynamically detect the presence of the Zbc@@ -813,7 +796,6 @@ config TOOLCHAIN_HAS_ZBKB config RISCV_ISA_ZBKB bool "Zbkb extension support for bit manipulation instructions" depends on TOOLCHAIN_HAS_ZBKB - depends on RISCV_ALTERNATIVE default y help Adds support to dynamically detect the presence of the ZBKB@@ -827,7 +809,6 @@ config RISCV_ISA_ZBKB config RISCV_ISA_ZICBOM bool "Zicbom extension support for non-coherent DMA operation" depends on MMU - depends on RISCV_ALTERNATIVE default y select RISCV_DMA_NONCOHERENT select DMA_DIRECT_REMAP@@ -843,7 +824,6 @@ config RISCV_ISA_ZICBOM config RISCV_ISA_ZICBOZ bool "Zicboz extension support for faster zeroing of memory" - depends on RISCV_ALTERNATIVE default y help Enable the use of the Zicboz extension (cbo.zero instruction)@@ -856,7 +836,6 @@ config RISCV_ISA_ZICBOZ config RISCV_ISA_ZICBOP bool "Zicbop extension support for cache block prefetch" depends on MMU - depends on RISCV_ALTERNATIVE default y help Adds support to dynamically detect the presence of the ZICBOP@@ -871,7 +850,6 @@ config RISCV_ISA_ZICBOP config RISCV_ISA_SVRSW60T59B bool "Svrsw60t59b extension support for using PTE bits 60 and 59" depends on MMU && 64BIT - depends on RISCV_ALTERNATIVE default y help Adds support to dynamically detect the presence of the Svrsw60t59b@@ -1204,7 +1182,6 @@ config RISCV_USER_CFI bool "riscv userspace control flow integrity" depends on 64BIT && MMU && \ $(cc-option,-mabi=lp64 -march=rv64ima_zicfiss_zicfilp -fcf-protection=full) - depends on RISCV_ALTERNATIVE select RISCV_SBI select ARCH_HAS_USER_SHADOW_STACK select ARCH_USES_HIGH_VMA_FLAGSarch/riscv/Kconfig.errata +1
-4@@ -2,7 +2,7 @@ menu "CPU errata selection" config ERRATA_ANDES bool "Andes AX45MP errata" - depends on RISCV_ALTERNATIVE && RISCV_SBI + depends on RISCV_SBI help All Andes errata Kconfig depend on this Kconfig. Disabling this Kconfig will disable all Andes errata. Please say "Y"@@ -23,7 +23,6 @@ config ERRATA_ANDES_CMO config ERRATA_MIPS bool "MIPS errata" - depends on RISCV_ALTERNATIVE help All MIPS errata Kconfig depend on this Kconfig. Disabling this Kconfig will disable all MIPS errata. Please say "Y"@@ -46,7 +45,6 @@ config ERRATA_MIPS_P8700_PAUSE_OPCODE config ERRATA_SIFIVE bool "SiFive errata" - depends on RISCV_ALTERNATIVE help All SiFive errata Kconfig depend on this Kconfig. Disabling this Kconfig will disable all SiFive errata. Please say "Y"@@ -98,7 +96,6 @@ config ERRATA_STARFIVE_JH7100 config ERRATA_THEAD bool "T-HEAD errata" - depends on RISCV_ALTERNATIVE help All T-HEAD errata Kconfig depend on this Kconfig. Disabling this Kconfig will disable all T-HEAD errata. Please say "Y"arch/riscv/include/asm/alternative-macros.h +0
-24@@ -2,8 +2,6 @@#ifndef __ASM_ALTERNATIVE_MACROS_H#define __ASM_ALTERNATIVE_MACROS_H -#ifdef CONFIG_RISCV_ALTERNATIVE -#ifdef __ASSEMBLER__ .macro ALT_ENTRY oldptr newptr vendor_id patch_id new_len@@ -108,28 +106,6 @@__ALTERNATIVE_CFG_2 (old_c, new_c_1, vendor_id_1, patch_id_1,IS_ENABLED (CONFIG_k_1), \ new_c_2, vendor_id_2, patch_id_2,IS_ENABLED (CONFIG_k_2)) -#else /* CONFIG_RISCV_ALTERNATIVE */ -#ifdef __ASSEMBLER__ - -.macro ALTERNATIVE_CFG old_c - \old_c -.endm - -#define __ALTERNATIVE_CFG (old_c, ...) ALTERNATIVE_CFG old_c -#define __ALTERNATIVE_CFG_2 (old_c, ...) ALTERNATIVE_CFG old_c - -#else /* !__ASSEMBLER__ */ - -#define __ALTERNATIVE_CFG (old_c, ...) old_c "\n" -#define __ALTERNATIVE_CFG_2 (old_c, ...) old_c "\n" - -#endif /* __ASSEMBLER__ */ - -#define _ALTERNATIVE_CFG (old_c, ...) __ALTERNATIVE_CFG(old_c) -#define _ALTERNATIVE_CFG_2 (old_c, ...) __ALTERNATIVE_CFG_2(old_c) - -#endif /* CONFIG_RISCV_ALTERNATIVE */ -/* * Usage: * ALTERNATIVE(old_content, new_content, vendor_id, patch_id, CONFIG_k) arch/riscv/include/asm/alternative.h +0
-10@@ -10,8 +10,6 @@#ifndef __ASSEMBLER__ -#ifdef CONFIG_RISCV_ALTERNATIVE -#include <linux/init.h> #include <linux/kernel.h> #include <linux/types.h> @@ -61,13 +59,5 @@ void thead_errata_patch_func(struct alt_entry *begin, struct alt_entry *end,void riscv_cpufeature_patch_func (struct alt_entry *begin,struct alt_entry *end,unsigned int stage); -#else /* CONFIG_RISCV_ALTERNATIVE */ - -static inline void apply_boot_alternatives (void ) { } -static inline void apply_early_boot_alternatives (void ) { } -static inline void apply_module_alternatives (void *start,size_t length) { } - -#endif /* CONFIG_RISCV_ALTERNATIVE */ -#endif #endif arch/riscv/include/asm/cpufeature-macros.h +2
-8@@ -47,20 +47,14 @@ static __always_inline bool riscv_has_extension_unlikely(const unsigned long ext {compiletime_assert (ext <RISCV_ISA_EXT_MAX ,"ext must be < RISCV_ISA_EXT_MAX" ); -if (IS_ENABLED (CONFIG_RISCV_ALTERNATIVE )) -return __riscv_has_extension_unlikely (STANDARD_EXT , ext); - -return __riscv_isa_extension_available (NULL , ext); +return __riscv_has_extension_unlikely (STANDARD_EXT , ext); } static __always_inline bool riscv_has_extension_likely(const unsigned long ext) {compiletime_assert (ext <RISCV_ISA_EXT_MAX ,"ext must be < RISCV_ISA_EXT_MAX" ); -if (IS_ENABLED (CONFIG_RISCV_ALTERNATIVE )) -return __riscv_has_extension_likely (STANDARD_EXT , ext); - -return __riscv_isa_extension_available (NULL , ext); +return __riscv_has_extension_likely (STANDARD_EXT , ext); }#endif /* _ASM_CPUFEATURE_MACROS_H */ arch/riscv/include/asm/cpufeature.h +2
-4@@ -134,8 +134,7 @@ static __always_inline bool riscv_cpu_has_extension_likely(int cpu, const unsign {compiletime_assert (ext <RISCV_ISA_EXT_MAX ,"ext must be < RISCV_ISA_EXT_MAX" ); -if (IS_ENABLED (CONFIG_RISCV_ALTERNATIVE ) && -__riscv_has_extension_likely (STANDARD_EXT , ext)) +if (__riscv_has_extension_likely (STANDARD_EXT , ext))return true;return __riscv_isa_extension_available (hart_isa[cpu].isa, ext);@@ -145,8 +144,7 @@ static __always_inline bool riscv_cpu_has_extension_unlikely(int cpu, const unsi {compiletime_assert (ext <RISCV_ISA_EXT_MAX ,"ext must be < RISCV_ISA_EXT_MAX" ); -if (IS_ENABLED (CONFIG_RISCV_ALTERNATIVE ) && -__riscv_has_extension_unlikely (STANDARD_EXT , ext)) +if (__riscv_has_extension_unlikely (STANDARD_EXT , ext))return true;return __riscv_isa_extension_available (hart_isa[cpu].isa, ext);arch/riscv/include/asm/vendor_extensions.h +6
-12@@ -54,11 +54,9 @@ static __always_inline bool riscv_has_vendor_extension_likely(const unsigned lonif (!IS_ENABLED (CONFIG_RISCV_ISA_VENDOR_EXT ))return false; -if (IS_ENABLED (CONFIG_RISCV_ALTERNATIVE )) -return __riscv_has_extension_likely (vendor, - ext +RISCV_VENDOR_EXT_ALTERNATIVES_BASE ); +return __riscv_has_extension_likely (vendor, + ext +RISCV_VENDOR_EXT_ALTERNATIVES_BASE ); -return __riscv_isa_vendor_extension_available (VENDOR_EXT_ALL_CPUS , vendor, ext); } static __always_inline bool riscv_has_vendor_extension_unlikely(const unsigned long vendor,@@ -67,11 +65,9 @@ static __always_inline bool riscv_has_vendor_extension_unlikely(const unsigned lif (!IS_ENABLED (CONFIG_RISCV_ISA_VENDOR_EXT ))return false; -if (IS_ENABLED (CONFIG_RISCV_ALTERNATIVE )) -return __riscv_has_extension_unlikely (vendor, - ext +RISCV_VENDOR_EXT_ALTERNATIVES_BASE ); +return __riscv_has_extension_unlikely (vendor, + ext +RISCV_VENDOR_EXT_ALTERNATIVES_BASE ); -return __riscv_isa_vendor_extension_available (VENDOR_EXT_ALL_CPUS , vendor, ext); } static __always_inline bool riscv_cpu_has_vendor_extension_likely(const unsigned long vendor,@@ -80,8 +76,7 @@ static __always_inline bool riscv_cpu_has_vendor_extension_likely(const unsignedif (!IS_ENABLED (CONFIG_RISCV_ISA_VENDOR_EXT ))return false; -if (IS_ENABLED (CONFIG_RISCV_ALTERNATIVE ) && -__riscv_has_extension_likely (vendor, ext +RISCV_VENDOR_EXT_ALTERNATIVES_BASE )) +if (__riscv_has_extension_likely (vendor, ext +RISCV_VENDOR_EXT_ALTERNATIVES_BASE ))return true;return __riscv_isa_vendor_extension_available (cpu, vendor, ext);@@ -94,8 +89,7 @@ static __always_inline bool riscv_cpu_has_vendor_extension_unlikely(const unsignif (!IS_ENABLED (CONFIG_RISCV_ISA_VENDOR_EXT ))return false; -if (IS_ENABLED (CONFIG_RISCV_ALTERNATIVE ) && -__riscv_has_extension_unlikely (vendor, ext +RISCV_VENDOR_EXT_ALTERNATIVES_BASE )) +if (__riscv_has_extension_unlikely (vendor, ext +RISCV_VENDOR_EXT_ALTERNATIVES_BASE ))return true;return __riscv_isa_vendor_extension_available (cpu, vendor, ext);arch/riscv/kernel/Makefile +1
-1@@ -48,7 +48,7 @@ always-$(KBUILD_BUILTIN) += vmlinux.lds obj-y += head.o obj-y += soc.o -obj-$(CONFIG_RISCV_ALTERNATIVE) += alternative.o +obj-y += alternative.o obj-y += cpu.o obj-y += cpufeature.o obj-y += entry.oarch/riscv/kernel/cpufeature.c +0
-2@@ -1217,7 +1217,6 @@ void __init riscv_user_isa_enable(void)pr_warn ("Zicbop disabled as it is unavailable on some harts\n" ); } -#ifdef CONFIG_RISCV_ALTERNATIVE /* * Alternative patch sites consider 48 bits when determining when to patch * the old instruction sequence with the new. These bits are broken into a @@ -1306,4 +1305,3 @@ void __init_or_module riscv_cpufeature_patch_func(struct alt_entry *begin,mutex_unlock (&text_mutex); } } -#endif Signature · 1 line
2.53.0
PATCH 2/3 riscv: convert pgtable_l4|l5_enabled to inline function
In reply to Jisheng ZhangThis is the preparation of optimizing pgtable_l4|l5_enabled(). No functionality change.
--- arch/riscv/include/asm/pgalloc.h | 14 +++---- arch/riscv/include/asm/pgtable-32.h | 4 +- arch/riscv/include/asm/pgtable-64.h | 50 +++++++++++++--------- arch/riscv/include/asm/pgtable.h | 4 +- arch/riscv/include/asm/vmalloc.h | 5 +-- arch/riscv/kernel/cpu.c | 4 +- arch/riscv/mm/init.c | 64 ++++++++++++++--------------- arch/riscv/mm/kasan_init.c | 20 ++++----- arch/riscv/mm/pgtable.c | 4 +- arch/riscv/mm/ptdump.c | 4 +- 10 files changed, 91 insertions(+), 82 deletions(-)
arch/riscv/include/asm/pgalloc.h +7 -7
@@ -41,7 +41,7 @@ static inline void pud_populate(struct mm_struct *mm, pud_t *pud, pmd_t *pmd)static inline void p4d_populate (struct mm_struct *mm,p4d_t *p4d,pud_t *pud) { -if (pgtable_l4_enabled) { +if (pgtable_l4_enabled ()) {unsigned long pfn =virt_to_pfn (pud);set_p4d (p4d,__p4d ((pfn << _PAGE_PFN_SHIFT) | _PAGE_TABLE));
@@ -51,7 +51,7 @@ static inline void p4d_populate(struct mm_struct *mm, p4d_t *p4d, pud_t *pud)static inline void p4d_populate_safe (struct mm_struct *mm,p4d_t *p4d,pud_t *pud) { -if (pgtable_l4_enabled) { +if (pgtable_l4_enabled ()) {unsigned long pfn =virt_to_pfn (pud); set_p4d_safe(p4d,
@@ -61,7 +61,7 @@ static inline void p4d_populate_safe(struct mm_struct *mm, p4d_t *p4d,static inline void pgd_populate (struct mm_struct *mm,pgd_t *pgd,p4d_t *p4d) { -if (pgtable_l5_enabled) { +if (pgtable_l5_enabled ()) {unsigned long pfn =virt_to_pfn (p4d);set_pgd (pgd,__pgd ((pfn << _PAGE_PFN_SHIFT) | _PAGE_TABLE));
@@ -71,7 +71,7 @@ static inline void pgd_populate(struct mm_struct *mm, pgd_t *pgd, p4d_t *p4d)static inline void pgd_populate_safe (struct mm_struct *mm,pgd_t *pgd,p4d_t *p4d) { -if (pgtable_l5_enabled) { +if (pgtable_l5_enabled ()) {unsigned long pfn =virt_to_pfn (p4d); set_pgd_safe(pgd,
@@ -82,21 +82,21 @@ static inline void pgd_populate_safe(struct mm_struct *mm, pgd_t *pgd,#define pud_free pud_freestatic inline void pud_free (struct mm_struct *mm,pud_t *pud) { -if (pgtable_l4_enabled) +if (pgtable_l4_enabled ())__pud_free (mm, pud); }static inline void __pud_free_tlb (struct mmu_gather *tlb,pud_t *pud,unsigned long addr) { -if (pgtable_l4_enabled) +if (pgtable_l4_enabled ())tlb_remove_ptdesc (tlb,virt_to_ptdesc (pud)); }static inline void __p4d_free_tlb (struct mmu_gather *tlb,p4d_t *p4d,unsigned long addr) { -if (pgtable_l5_enabled) +if (pgtable_l5_enabled ())tlb_remove_ptdesc (tlb,virt_to_ptdesc (p4d)); }#endif /* __PAGETABLE_PMD_FOLDED */
arch/riscv/include/asm/pgtable-32.h +2 -2
@@ -33,7 +33,7 @@ _PAGE_WRITE | _PAGE_EXEC | \ _PAGE_USER | _PAGE_GLOBAL)) -static const __maybe_unused int pgtable_l4_enabled; -static const __maybe_unused int pgtable_l5_enabled; +static inline bool pgtable_l4_enabled () {return false; } +static inline bool pgtable_l5_enabled () {return false; }#endif /* _ASM_RISCV_PGTABLE_32_H */
arch/riscv/include/asm/pgtable-64.h +30 -20
149 lines not on this page · raw message
arch/riscv/include/asm/pgtable.h +2 -2
@@ -75,8 +75,8 @@#define VA_BITS_SV48 48#define VA_BITS_SV57 57 -#define VA_BITS (pgtable_l5_enabled ? \ - VA_BITS_SV57 : (pgtable_l4_enabled ? VA_BITS_SV48 : VA_BITS_SV39)) +#define VA_BITS (pgtable_l5_enabled() ? \ + VA_BITS_SV57 : (pgtable_l4_enabled() ? VA_BITS_SV48 : VA_BITS_SV39))#else #define VA_BITS VA_BITS_SV32#endif
arch/riscv/include/asm/vmalloc.h +2 -3
@@ -3,15 +3,14 @@#define _ASM_RISCV_VMALLOC_H#ifdef CONFIG_HAVE_ARCH_HUGE_VMAP - -extern bool pgtable_l4_enabled, pgtable_l5_enabled; +#include <asm/pgtable.h> #define IOREMAP_MAX_ORDER (PUD_SHIFT)#define arch_vmap_pud_supported arch_vmap_pud_supportedstatic inline bool arch_vmap_pud_supported (pgprot_t prot) { -return pgtable_l4_enabled || pgtable_l5_enabled; +return pgtable_l4_enabled () ||pgtable_l5_enabled (); }#define arch_vmap_pmd_supported arch_vmap_pmd_supported
arch/riscv/kernel/cpu.c +2 -2
@@ -292,9 +292,9 @@ static void print_mmu(struct seq_file *f)#if defined(CONFIG_32BIT ) sv_type ="sv32" ;#elif defined(CONFIG_64BIT ) -if (pgtable_l5_enabled) +if (pgtable_l5_enabled ()) sv_type ="sv57" ; -else if (pgtable_l4_enabled) +else if (pgtable_l4_enabled ()) sv_type ="sv48" ;else sv_type ="sv39" ;
arch/riscv/mm/init.c +32 -32
179 lines not on this page · raw message
arch/riscv/mm/kasan_init.c +10 -10
@@ -168,7 +168,7 @@ static void __init kasan_early_clear_pud(p4d_t *p4dp,pud_t *pudp, *base_pud;unsigned long next; -if (!pgtable_l4_enabled) { +if (!pgtable_l4_enabled ()) { pudp = (pud_t *)p4dp; }else { base_pud = pt_ops.get_pud_virt (pfn_to_phys (_p4d_pfn (p4dp_get (p4dp))));
@@ -193,7 +193,7 @@ static void __init kasan_early_clear_p4d(pgd_t *pgdp,p4d_t *p4dp, *base_p4d;unsigned long next; -if (!pgtable_l5_enabled) { +if (!pgtable_l5_enabled ()) { p4dp = (p4d_t *)pgdp; }else { base_p4d = pt_ops.get_p4d_virt (pfn_to_phys (_pgd_pfn (pgdp_get (pgdp))));
@@ -203,7 +203,7 @@ static void __init kasan_early_clear_p4d(pgd_t *pgdp, do { next = p4d_addr_end(vaddr, end); - if (pgtable_l4_enabled && IS_ALIGNED(vaddr, P4D_SIZE) && + if (pgtable_l4_enabled() && IS_ALIGNED(vaddr, P4D_SIZE) && (next - vaddr) >= P4D_SIZE) { p4d_clear(p4dp); continue;
@@ -221,7 +221,7 @@ static void __init kasan_early_clear_pgd(pgd_t *pgdp, do { next = pgd_addr_end(vaddr, end); - if (pgtable_l5_enabled && IS_ALIGNED(vaddr, PGDIR_SIZE) && + if (pgtable_l5_enabled() && IS_ALIGNED(vaddr, PGDIR_SIZE) && (next - vaddr) >= PGDIR_SIZE) { pgd_clear(pgdp); continue;
@@ -239,7 +239,7 @@ static void __init kasan_early_populate_pud(p4d_t *p4dp,phys_addr_t phys_addr;unsigned long next; -if (!pgtable_l4_enabled) { +if (!pgtable_l4_enabled ()) { pudp = (pud_t *)p4dp; }else { base_pud = pt_ops.get_pud_virt (pfn_to_phys (_p4d_pfn (p4dp_get (p4dp))));
@@ -277,7 +277,7 @@ static void __init kasan_early_populate_p4d(pgd_t *pgdp,* Note that this test is then completely equivalent to * p4dp = p4d_offset(pgdp, vaddr) */ -if (!pgtable_l5_enabled) { +if (!pgtable_l5_enabled ()) { p4dp = (p4d_t *)pgdp; }else { base_p4d = pt_ops.get_p4d_virt (pfn_to_phys (_pgd_pfn (pgdp_get (pgdp))));
@@ -336,7 +336,7 @@ asmlinkage void __init kasan_early_init(void) (__pa ((uintptr_t )kasan_early_shadow_pte)), PAGE_TABLE)); -if (pgtable_l4_enabled) { +if (pgtable_l4_enabled ()) {for (i =0 ; i <PTRS_PER_PUD ; ++i) set_pud(kasan_early_shadow_pud + i, pfn_pud(PFN_DOWN
@@ -344,7 +344,7 @@ asmlinkage void __init kasan_early_init(void) PAGE_TABLE)); } -if (pgtable_l5_enabled) { +if (pgtable_l5_enabled ()) {for (i =0 ; i <PTRS_PER_P4D ; ++i) set_p4d(kasan_early_shadow_p4d + i, pfn_p4d(PFN_DOWN
@@ -461,7 +461,7 @@ static void __init create_tmp_mapping(void)memcpy (tmp_pg_dir, swapper_pg_dir,sizeof (pgd_t) *PTRS_PER_PGD );/* Copy the last p4d since it is shared with the kernel mapping. */ -if (pgtable_l5_enabled) { +if (pgtable_l5_enabled ()) { ptr = (p4d_t *)pgd_page_vaddr (pgdp_get (pgd_offset_k (KASAN_SHADOW_END )));memcpy (tmp_p4d, ptr,sizeof (p4d_t) *PTRS_PER_P4D ); set_pgd(&tmp_pg_dir[pgd_index(KASAN_SHADOW_END)],
@@ -472,7 +472,7 @@ static void __init create_tmp_mapping(void) }/* Copy the last pud since it is shared with the kernel mapping. */ -if (pgtable_l4_enabled) { +if (pgtable_l4_enabled ()) { ptr = (pud_t *)p4d_page_vaddr (p4dp_get (base_p4d +p4d_index (KASAN_SHADOW_END )));memcpy (tmp_pud, ptr,sizeof (pud_t) *PTRS_PER_PUD ); set_p4d(&base_p4d[p4d_index(KASAN_SHADOW_END)],
arch/riscv/mm/pgtable.c +2 -2
@@ -41,7 +41,7 @@ EXPORT_SYMBOL_GPL(ptep_test_and_clear_young);#ifdef CONFIG_64BIT pud_t *pud_offset (p4d_t *p4d,unsigned long address) { -if (pgtable_l4_enabled) +if (pgtable_l4_enabled ())return p4d_pgtable (p4dp_get (p4d)) +pud_index (address);return (pud_t *)p4d;
@@ -50,7 +50,7 @@ EXPORT_SYMBOL_GPL(pud_offset);p4d_t *p4d_offset (pgd_t *pgd,unsigned long address) { -if (pgtable_l5_enabled) +if (pgtable_l5_enabled ())return pgd_pgtable (pgdp_get (pgd)) +p4d_index (address);return (p4d_t *)pgd;
arch/riscv/mm/ptdump.c +2 -2
@@ -446,8 +446,8 @@ static int __init ptdump_init(void) kernel_ptd_info.base_addr =KERN_VIRT_START ; - pg_level[1 ].name = pgtable_l5_enabled ?"P4D" :"PGD" ; - pg_level[2 ].name = pgtable_l4_enabled ?"PUD" :"PGD" ; + pg_level[1 ].name =pgtable_l5_enabled () ?"P4D" :"PGD" ; + pg_level[2 ].name =pgtable_l4_enabled () ?"PUD" :"PGD" ;for (i =0 ; i <ARRAY_SIZE (pg_level); i++)for (j =0 ; j <ARRAY_SIZE (pte_bits); j++)
Signature · 1 line
2.53.0
PATCH 3/3 riscv: introduce cap framework and use it to optimize pgtable_l4|l5_enabled
In reply to Jisheng ZhangThe pgtable_l4|[l5]_enabled check sits at hot code path, performance is impacted a lot. Since pgtable_l4|[l5]_enabled isn't changed after boot, we can use alternative mechanism to optimize them.
So the question is whether we can add RISCV_ISA_EXT_SV48/SV5 and use riscv_has_extension_*() or not. Although, per [1] and [2], SV48 and SV57 are ISA exensions too, RISCV_ISA_EXT_SV48/SV57 are to describe hw supported extensions, while this doesn't mean the pgtable_l4|l5 is enabled, for example, we may pass no5lvl/no4lvl kernel boot args or explicitly ask for SV39 by setting dt mmu-type property as "riscv,sv39". If we clear RISCV_ISA_EXT_SV48|SV57, then internal extension queries and potentially userspace reporting can no longer distinguish “unsupported” from “supported but disabled.”
Introduce cap framework to describe the capabilities selected by kernel. It also uses similar alternatives mechanism as the riscv_has_extension_*() helpers.
After that, use it to optimize pgtable_l4|l5_enabled.
For the typical access_ok(addr, 1);
before the patch:
...
auipc a5,0xb43
lbu a5,100(a5) # ffffffff80b51f68 <pgtable_l5_enabled>
bnez a5,ffffffff8000ef46 <foo+0x56>
auipc a5,0xb43
lbu a5,91(a5) # ffffffff80b51f69 <pgtable_l4_enabled>
beqz a5,ffffffff8000ef5a <foo+0x6a>
...
after the patch:
there are only two j or nop instructions which avoid memory load and test branch.
Initial test lmbench's lat_syscall write on TH1520 platforms shows that the write syscall latency is reduced by about 2.38%.
--- arch/riscv/Kconfig | 1 + arch/riscv/include/asm/alternative.h | 2 +- arch/riscv/include/asm/cpufeature-macros.h | 46 ++++++++++++++++++++++ arch/riscv/include/asm/cpufeature.h | 2 + arch/riscv/include/asm/hwcap.h | 6 +++ arch/riscv/include/asm/pgtable-64.h | 12 ++++++ arch/riscv/kernel/alternative.c | 24 +++++++---- arch/riscv/kernel/cpufeature.c | 41 +++++++++++++++---- arch/riscv/mm/init.c | 9 +++++ 9 files changed, 128 insertions(+), 15 deletions(-)
arch/riscv/Kconfig +1 -0
@@ -218,6 +218,7 @@ config RISCV select PCI_ECAM if (ACPI && PCI) select PCI_MSI if PCI select RELOCATABLE if !MMU && !PHYS_RAM_BASE_FIXED + select RISCV_ALTERNATIVE_EARLY if 64BIT select RISCV_APLIC select RISCV_IMSIC select RISCV_INTC
arch/riscv/include/asm/alternative.h +1 -1
@@ -33,7 +33,7 @@ void __init apply_early_boot_alternatives(void);void apply_module_alternatives (void *start,size_t length);void riscv_alternative_fix_offsets (void *alt_ptr,unsigned int len, -int patch_offset); +int patch_offset,bool early);struct alt_entry {s32 old_offset;/* offset relative to original instruction or data */
arch/riscv/include/asm/cpufeature-macros.h +46 -0
56 lines not on this page · raw message
arch/riscv/include/asm/cpufeature.h +2 -0
@@ -36,6 +36,8 @@ extern const struct seq_operations cpuinfo_op;/* Per-cpu ISA extensions. */ extern struct riscv_isainfo hart_isa[NR_CPUS ]; +extern DECLARE_BITMAP(riscv_cap, RISCV_CAP_MAX - RISCV_ISA_EXT_MAX); +extern u32 thead_vlenb_of;void __init riscv_user_isa_enable (void );
arch/riscv/include/asm/hwcap.h +6 -0
@@ -128,6 +128,12 @@#define RISCV_ISA_EXT_MAX 128#define RISCV_ISA_EXT_INVALID U32_MAX +/* Kernel capabilities occupy the gap below vendor alternative IDs. */ +#define RISCV_CAP_PGTABLE_L4 RISCV_ISA_EXT_MAX +#define RISCV_CAP_PGTABLE_L5 (RISCV_ISA_EXT_MAX + 1) +#define RISCV_CAP_EARLY_MAX (RISCV_ISA_EXT_MAX + 2) +#define RISCV_CAP_MAX RISCV_CAP_EARLY_MAX +#ifdef CONFIG_RISCV_M_MODE #define RISCV_ISA_EXT_SxAIA RISCV_ISA_EXT_SMAIA#define RISCV_ISA_EXT_SUPM RISCV_ISA_EXT_SMNPM
arch/riscv/include/asm/pgtable-64.h +12 -0
@@ -13,6 +13,7 @@extern bool _pgtable_l4_enabled;extern bool _pgtable_l5_enabled; +#ifdef USE_EARLY_PGTABLE_LEVELS static __always_inline bool pgtable_l5_enabled(void) {return _pgtable_l5_enabled;
@@ -22,6 +23,17 @@ static __always_inline bool pgtable_l4_enabled(void) {return _pgtable_l4_enabled; } +#else +static __always_inline bool pgtable_l4_enabled(void) +{ +return riscv_has_cap_likely (RISCV_CAP_PGTABLE_L4 ); +} + +static __always_inline bool pgtable_l5_enabled(void) +{ +return riscv_has_cap_likely (RISCV_CAP_PGTABLE_L5 ); +} +#endif #define PGDIR_SHIFT_L3 30#define PGDIR_SHIFT_L4 39
arch/riscv/kernel/alternative.c +17 -7
@@ -75,7 +75,8 @@ static u32 riscv_instruction_at(void *p) }static void riscv_alternative_fix_auipc_jalr (void *ptr,u32 auipc_insn, -u32 jalr_insn,int patch_offset) +u32 jalr_insn,int patch_offset, +bool early) {u32 call[2 ] = { auipc_insn, jalr_insn };s32 imm;
@@ -88,10 +89,15 @@ static void riscv_alternative_fix_auipc_jalr(void *ptr, u32 auipc_insn,riscv_insn_insert_utype_itype_imm (&call[0 ], &call[1 ], imm);/* patch the call place again */ -patch_text_nosync (ptr, call,sizeof (u32) *2 ); +if (early) { +memcpy (ptr, call,sizeof (call)); + }else { +patch_text_nosync (ptr, call,sizeof (call)); + } } -static void riscv_alternative_fix_jal (void *ptr,u32 jal_insn,int patch_offset) +static void riscv_alternative_fix_jal (void *ptr,u32 jal_insn,int patch_offset, +bool early) {s32 imm;
@@ -103,11 +109,14 @@ static void riscv_alternative_fix_jal(void *ptr, u32 jal_insn, int patch_offset)riscv_insn_insert_jtype_imm (&jal_insn, imm);/* patch the call place again */ -patch_text_nosync (ptr, &jal_insn,sizeof (u32)); +if (early) +memcpy (ptr, &jal_insn,sizeof (u32)); +else +patch_text_nosync (ptr, &jal_insn,sizeof (u32)); }void riscv_alternative_fix_offsets (void *alt_ptr,unsigned int len, -int patch_offset) +int patch_offset,bool early) {int num_insn = len /sizeof (u32);int i;
@@ -131,7 +140,8 @@ void riscv_alternative_fix_offsets(void *alt_ptr, unsigned int len,continue ;riscv_alternative_fix_auipc_jalr (alt_ptr + i *sizeof (u32), - insn, insn2, patch_offset); + insn, insn2, patch_offset, + early); i++; }
@@ -144,7 +154,7 @@ void riscv_alternative_fix_offsets(void *alt_ptr, unsigned int len,continue ;riscv_alternative_fix_jal (alt_ptr + i *sizeof (u32), - insn, patch_offset); + insn, patch_offset, early); } } }
arch/riscv/kernel/cpufeature.c +34 -7
77 lines not on this page · raw message
arch/riscv/mm/init.c +9 -0
@@ -6,6 +6,11 @@* Nick Kossifidis <[email protected]> */ +#ifdef CONFIG_64BIT +/* riscv_has_cap_likely() cannot be used this early */ +#define USE_EARLY_PGTABLE_LEVELS +#endif +#include <linux/init.h> #include <linux/mm.h> #include <linux/memblock.h>
@@ -892,6 +897,10 @@ static __init void set_satp_mode(uintptr_t dtb_pa)memset (early_p4d,0 ,PAGE_SIZE );memset (early_pud,0 ,PAGE_SIZE );memset (early_pmd,0 ,PAGE_SIZE ); +if (pgtable_l4_enabled ()) +set_bit (RISCV_CAP_PGTABLE_L4 -RISCV_ISA_EXT_MAX , riscv_cap); +if (pgtable_l5_enabled ()) + set_bit(RISCV_CAP_PGTABLE_L5 - RISCV_ISA_EXT_MAX, riscv_cap); }#endif
Signature · 1 line
2.53.0
Re: [PATCH 3/3] riscv: introduce cap framework and use it to optimize pgtable_l4|l5_enabled
Quoted 4 lines · Jisheng Zhang
The pgtable_l4|[l5]_enabled check sits at hot code path, performance is impacted a lot. Since pgtable_l4|[l5]_enabled isn't changed after boot, we can use alternative mechanism to optimize them.
Quoted 9 lines
So the question is whether we can add RISCV_ISA_EXT_SV48/SV5 and use riscv_has_extension_*() or not. Although, per [1] and [2], SV48 and SV57 are ISA exensions too, RISCV_ISA_EXT_SV48/SV57 are to describe hw supported extensions, while this doesn't mean the pgtable_l4|l5 is enabled, for example, we may pass no5lvl/no4lvl kernel boot args or explicitly ask for SV39 by setting dt mmu-type property as "riscv,sv39". If we clear RISCV_ISA_EXT_SV48|SV57, then internal extension queries and potentially userspace reporting can no longer distinguish “unsupported” from “supported but disabled.”
That's the case for all extensions that the kernel does not support, like if the fpu is disabled because the kernel doesn't have the configuration option set. In fact, I think the CFI extensions are very similar here, because they also have riscv_nousercfi command line options that can disable them.
If that's the reason for making a new-but-similar mechanism, I think you should just introduce RISCV_ISA_EXT_SV{48,57} and use that.
Cheers,
Conor.
Quoted 373 lines
Introduce cap framework to describe the capabilities selected by kernel. It also uses similar alternatives mechanism as the riscv_has_extension_*() helpers.
After that, use it to optimize pgtable_l4|l5_enabled.
For the typical
access_ok(addr, 1);
before the patch:...
auipc a5,0xb43
lbu a5,100(a5) #ffffffff80b51f68<pgtable_l5_enabled>
bnez a5,ffffffff8000ef46 <foo+0x56>
auipc a5,0xb43
lbu a5,91(a5) #ffffffff80b51f69<pgtable_l4_enabled>
beqz a5,ffffffff8000ef5a <foo+0x6a>
...after the patch:
there are only two j or nop instructions which avoid memory load and test branch.Initial test lmbench's
lat_syscallwrite on TH1520 platforms shows that the write syscall latency is reduced by about 2.38%.Signed-off-by Jisheng Zhang <[email protected]>--- arch/riscv/Kconfig | 1 + arch/riscv/include/asm/alternative.h | 2 +- arch/riscv/include/asm/cpufeature-macros.h | 46 ++++++++++++++++++++++ arch/riscv/include/asm/cpufeature.h | 2 + arch/riscv/include/asm/hwcap.h | 6 +++ arch/riscv/include/asm/pgtable-64.h | 12 ++++++ arch/riscv/kernel/alternative.c | 24 +++++++---- arch/riscv/kernel/cpufeature.c | 41 +++++++++++++++---- arch/riscv/mm/init.c | 9 +++++ 9 files changed, 128 insertions(+), 15 deletions(-)arch/riscv/Kconfig +1
-0@@ -218,6 +218,7 @@ config RISCV select PCI_ECAM if (ACPI && PCI) select PCI_MSI if PCI select RELOCATABLE if !MMU && !PHYS_RAM_BASE_FIXED + select RISCV_ALTERNATIVE_EARLY if 64BIT select RISCV_APLIC select RISCV_IMSIC select RISCV_INTCarch/riscv/include/asm/alternative.h +1
-1@@ -33,7 +33,7 @@ void __init apply_early_boot_alternatives(void);void apply_module_alternatives (void *start,size_t length);void riscv_alternative_fix_offsets (void *alt_ptr,unsigned int len, -int patch_offset); +int patch_offset,bool early);struct alt_entry {s32 old_offset;/* offset relative to original instruction or data */ arch/riscv/include/asm/cpufeature-macros.h +46
-0@@ -1,6 +1,7 @@/* SPDX-License-Identifier: GPL-2.0-only */ /* * Copyright 2022-2024 Rivos, Inc +* Copyright 2026 Jisheng Zhang <[email protected]> */ #ifndef _ASM_CPUFEATURE_MACROS_H@@ -57,4 +58,49 @@ static __always_inline bool riscv_has_extension_likely(const unsigned long ext)return __riscv_has_extension_likely (STANDARD_EXT , ext); } +static __always_inline bool __riscv_has_cap_likely(const unsigned long cap) +{ + asm goto(ALTERNATIVE("j %l[l_no]", "nop", 0, %[cap], 1) + : + : [cap] "i" (cap) + : + : l_no); + +return true; +l_no: +return false; +} + +static __always_inline bool __riscv_has_cap_unlikely(const unsigned long cap) +{ + + asm goto(ALTERNATIVE("nop", "j %l[l_yes]", 0, %[cap], 1) + : + : [cap] "i" (cap) + : + : l_yes); + +return false; +l_yes: +return true; +} + +static __always_inline bool riscv_has_cap_unlikely(const unsigned long cap) +{ +compiletime_assert (cap >=RISCV_ISA_EXT_MAX && + cap <RISCV_CAP_MAX , +"cap must be >= RISCV_ISA_EXT_MAX and < RISCV_CAP_MAX" ); + +return __riscv_has_cap_unlikely (cap); +} + +static __always_inline bool riscv_has_cap_likely(const unsigned long cap) +{ +compiletime_assert (cap >=RISCV_ISA_EXT_MAX && + cap <RISCV_CAP_MAX , +"cap must be >= RISCV_ISA_EXT_MAX and < RISCV_CAP_MAX" ); + +return __riscv_has_cap_likely (cap); +} +#endif /* _ASM_CPUFEATURE_MACROS_H */ arch/riscv/include/asm/cpufeature.h +2
-0@@ -36,6 +36,8 @@ extern const struct seq_operations cpuinfo_op;/* Per-cpu ISA extensions. */ extern struct riscv_isainfo hart_isa[NR_CPUS ]; +extern DECLARE_BITMAP(riscv_cap, RISCV_CAP_MAX - RISCV_ISA_EXT_MAX); +extern u32 thead_vlenb_of;void __init riscv_user_isa_enable (void );arch/riscv/include/asm/hwcap.h +6
-0@@ -128,6 +128,12 @@#define RISCV_ISA_EXT_MAX 128#define RISCV_ISA_EXT_INVALID U32_MAX +/* Kernel capabilities occupy the gap below vendor alternative IDs. */ +#define RISCV_CAP_PGTABLE_L4 RISCV_ISA_EXT_MAX +#define RISCV_CAP_PGTABLE_L5 (RISCV_ISA_EXT_MAX + 1) +#define RISCV_CAP_EARLY_MAX (RISCV_ISA_EXT_MAX + 2) +#define RISCV_CAP_MAX RISCV_CAP_EARLY_MAX +#ifdef CONFIG_RISCV_M_MODE #define RISCV_ISA_EXT_SxAIA RISCV_ISA_EXT_SMAIA#define RISCV_ISA_EXT_SUPM RISCV_ISA_EXT_SMNPMarch/riscv/include/asm/pgtable-64.h +12
-0@@ -13,6 +13,7 @@extern bool _pgtable_l4_enabled;extern bool _pgtable_l5_enabled; +#ifdef USE_EARLY_PGTABLE_LEVELS static __always_inline bool pgtable_l5_enabled(void) {return _pgtable_l5_enabled;@@ -22,6 +23,17 @@ static __always_inline bool pgtable_l4_enabled(void) {return _pgtable_l4_enabled; } +#else +static __always_inline bool pgtable_l4_enabled(void) +{ +return riscv_has_cap_likely (RISCV_CAP_PGTABLE_L4 ); +} + +static __always_inline bool pgtable_l5_enabled(void) +{ +return riscv_has_cap_likely (RISCV_CAP_PGTABLE_L5 ); +} +#endif #define PGDIR_SHIFT_L3 30#define PGDIR_SHIFT_L4 39arch/riscv/kernel/alternative.c +17
-7@@ -75,7 +75,8 @@ static u32 riscv_instruction_at(void *p) }static void riscv_alternative_fix_auipc_jalr (void *ptr,u32 auipc_insn, -u32 jalr_insn,int patch_offset) +u32 jalr_insn,int patch_offset, +bool early) {u32 call[2 ] = { auipc_insn, jalr_insn };s32 imm;@@ -88,10 +89,15 @@ static void riscv_alternative_fix_auipc_jalr(void *ptr, u32 auipc_insn,riscv_insn_insert_utype_itype_imm (&call[0 ], &call[1 ], imm);/* patch the call place again */ -patch_text_nosync (ptr, call,sizeof (u32) *2 ); +if (early) { +memcpy (ptr, call,sizeof (call)); + }else { +patch_text_nosync (ptr, call,sizeof (call)); + } } -static void riscv_alternative_fix_jal (void *ptr,u32 jal_insn,int patch_offset) +static void riscv_alternative_fix_jal (void *ptr,u32 jal_insn,int patch_offset, +bool early) {s32 imm;@@ -103,11 +109,14 @@ static void riscv_alternative_fix_jal(void *ptr, u32 jal_insn, int patch_offset)riscv_insn_insert_jtype_imm (&jal_insn, imm);/* patch the call place again */ -patch_text_nosync (ptr, &jal_insn,sizeof (u32)); +if (early) +memcpy (ptr, &jal_insn,sizeof (u32)); +else +patch_text_nosync (ptr, &jal_insn,sizeof (u32)); }void riscv_alternative_fix_offsets (void *alt_ptr,unsigned int len, -int patch_offset) +int patch_offset,bool early) {int num_insn = len /sizeof (u32);int i;@@ -131,7 +140,8 @@ void riscv_alternative_fix_offsets(void *alt_ptr, unsigned int len,continue ;riscv_alternative_fix_auipc_jalr (alt_ptr + i *sizeof (u32), - insn, insn2, patch_offset); + insn, insn2, patch_offset, + early); i++; }@@ -144,7 +154,7 @@ void riscv_alternative_fix_offsets(void *alt_ptr, unsigned int len,continue ;riscv_alternative_fix_jal (alt_ptr + i *sizeof (u32), - insn, patch_offset); + insn, patch_offset, early); } } }arch/riscv/kernel/cpufeature.c +34
-7@@ -40,6 +40,7 @@ unsigned long elf_hwcap __read_mostly;/* Host ISA bitmap */ static DECLARE_BITMAP(riscv_isa, RISCV_ISA_EXT_MAX) __read_mostly; +DECLARE_BITMAP (riscv_cap,RISCV_CAP_MAX -RISCV_ISA_EXT_MAX )__read_mostly ;/* Per-cpu ISA extensions. */ struct riscv_isainfo hart_isa[NR_CPUS ];@@ -81,6 +82,14 @@ bool __riscv_isa_extension_available(const unsigned long *isa_bitmap, unsigned i }EXPORT_SYMBOL_GPL (__riscv_isa_extension_available); +static bool __riscv_cap_available (unsigned int bit) +{ +if (bit >=RISCV_CAP_MAX || bit <RISCV_ISA_EXT_MAX ) +return false; + +return test_bit (bit -RISCV_ISA_EXT_MAX , riscv_cap); +} +static int riscv_ext_f_depends (const struct riscv_isa_ext_data *data,const unsigned long *isa_bitmap) {@@ -1257,9 +1266,7 @@ void __init_or_module riscv_cpufeature_patch_func(struct alt_entry *begin,struct alt_entry *alt;void *oldptr, *altptr;u16 id, value, vendor; - -if (stage ==RISCV_ALTERNATIVES_EARLY_BOOT ) -return ; +bool early = stage ==RISCV_ALTERNATIVES_EARLY_BOOT ;for (alt = begin; alt < end; alt++) { id =PATCH_ID_CPUFEATURE_ID (alt->patch_id);@@ -1274,6 +1281,8 @@ void __init_or_module riscv_cpufeature_patch_func(struct alt_entry *begin,* vendor extension. */ if (id <RISCV_ISA_EXT_MAX ) { +if (early) +continue ;/* * This patch should be treated as errata so skip * processing here. @@ -1287,7 +1296,14 @@ void __init_or_module riscv_cpufeature_patch_func(struct alt_entry *begin, value =PATCH_ID_CPUFEATURE_VALUE (alt->patch_id);if (!riscv_cpufeature_patch_check (id, value))continue ; + }else if (id <RISCV_CAP_MAX ) { +if (id >=RISCV_CAP_EARLY_MAX && early) +continue ; +if (!__riscv_cap_available (id)) +continue ; }else if (id >=RISCV_VENDOR_EXT_ALTERNATIVES_BASE ) { +if (early) +continue ;if (!__riscv_isa_vendor_extension_available (VENDOR_EXT_ALL_CPUS , vendor, id -RISCV_VENDOR_EXT_ALTERNATIVES_BASE ))continue ;@@ -1299,9 +1315,20 @@ void __init_or_module riscv_cpufeature_patch_func(struct alt_entry *begin, oldptr =ALT_OLD_PTR (alt); altptr =ALT_ALT_PTR (alt); -mutex_lock (&text_mutex); -patch_text_nosync (oldptr, altptr, alt->alt_len); -riscv_alternative_fix_offsets (oldptr, alt->alt_len, oldptr - altptr); -mutex_unlock (&text_mutex); +if (early) { +/* oldptr is writable through the MMU-off kernel mapping. */ +memcpy (oldptr, altptr, alt->alt_len); +riscv_alternative_fix_offsets (oldptr, alt->alt_len, + oldptr - altptr, true); + }else { +mutex_lock (&text_mutex); +patch_text_nosync (oldptr, altptr, alt->alt_len); +riscv_alternative_fix_offsets (oldptr, alt->alt_len, + oldptr - altptr, false); +mutex_unlock (&text_mutex); + } } + +if (early) +local_flush_icache_all (); }arch/riscv/mm/init.c +9
-0@@ -6,6 +6,11 @@* Nick Kossifidis <[email protected]> */ +#ifdef CONFIG_64BIT +/* riscv_has_cap_likely() cannot be used this early */ +#define USE_EARLY_PGTABLE_LEVELS +#endif +#include <linux/init.h> #include <linux/mm.h> #include <linux/memblock.h> @@ -892,6 +897,10 @@ static __init void set_satp_mode(uintptr_t dtb_pa)memset (early_p4d,0 ,PAGE_SIZE );memset (early_pud,0 ,PAGE_SIZE );memset (early_pmd,0 ,PAGE_SIZE ); +if (pgtable_l4_enabled ()) +set_bit (RISCV_CAP_PGTABLE_L4 -RISCV_ISA_EXT_MAX , riscv_cap); +if (pgtable_l5_enabled ()) + set_bit(RISCV_CAP_PGTABLE_L5 - RISCV_ISA_EXT_MAX, riscv_cap); }#endif Signature · 1 line
2.53.0
Re: [PATCH 3/3] riscv: introduce cap framework and use it to optimize pgtable_l4|l5_enabled
Quoted 15 lines · Conor Dooley
Quoted 4 lines · Jisheng Zhang
The pgtable_l4|[l5]_enabled check sits at hot code path, performance is impacted a lot. Since pgtable_l4|[l5]_enabled isn't changed after boot, we can use alternative mechanism to optimize them.
Quoted 9 lines
So the question is whether we can add RISCV_ISA_EXT_SV48/SV5 and use riscv_has_extension_*() or not. Although, per [1] and [2], SV48 and SV57 are ISA exensions too, RISCV_ISA_EXT_SV48/SV57 are to describe hw supported extensions, while this doesn't mean the pgtable_l4|l5 is enabled, for example, we may pass no5lvl/no4lvl kernel boot args or explicitly ask for SV39 by setting dt mmu-type property as "riscv,sv39". If we clear RISCV_ISA_EXT_SV48|SV57, then internal extension queries and potentially userspace reporting can no longer distinguish “unsupported” from “supported but disabled.”
Quoted 3 lines
That's the case for all extensions that the kernel does not support, like if the fpu is disabled because the kernel doesn't have the configuration option set. In fact, I think the CFI extensions are very
To be honest, before introduce the cap framework, I did think about FPU, ZACAS, ZABHA etc. ISA extensions' cases. Finally I came to a conclusion that the SV48/57 is a different case: Let me take FPU for an example. FPU once probed, the ISA support is never disabled, IOW, the riscv_has_extension_likely(FPU) is always true no matter the CONFIG_FPU is enabled or not. There's "no supported but disabled" case for FPU. But for SV48 case, that's different. The underlying HW may support SV48 or SV57, but the "no5lvl and no4lvl" may explicitly force SV39, thus if we go with exisiting riscv_has_extension_*() API, we need to explicitly disable it by clear the isa bitmap. IOW, riscv_has_extension_likely(SV48) is false even the underlying HW supports SV48, thus the user can't distinguish between “unsupported” from “supported but disabled.”
What do you think?
Quoted 1 line
similar here, because they also have
riscv_nousercficommand line
This is another story, neither CPU ISA extension nor CPU capbility.
Quoted 380 lines
options that can disable them.
If that's the reason for making a new-but-similar mechanism, I think you should just introduce RISCV_ISA_EXT_SV{48,57} and use that.Cheers,
Conor.Quoted 373 lines
Introduce cap framework to describe the capabilities selected by kernel. It also uses similar alternatives mechanism as the riscv_has_extension_*() helpers.
After that, use it to optimize pgtable_l4|l5_enabled.
For the typical
access_ok(addr, 1);
before the patch:...
auipc a5,0xb43
lbu a5,100(a5) #ffffffff80b51f68<pgtable_l5_enabled>
bnez a5,ffffffff8000ef46 <foo+0x56>
auipc a5,0xb43
lbu a5,91(a5) #ffffffff80b51f69<pgtable_l4_enabled>
beqz a5,ffffffff8000ef5a <foo+0x6a>
...after the patch:
there are only two j or nop instructions which avoid memory load and test branch.Initial test lmbench's
lat_syscallwrite on TH1520 platforms shows that the write syscall latency is reduced by about 2.38%.Signed-off-by Jisheng Zhang <[email protected]>--- arch/riscv/Kconfig | 1 + arch/riscv/include/asm/alternative.h | 2 +- arch/riscv/include/asm/cpufeature-macros.h | 46 ++++++++++++++++++++++ arch/riscv/include/asm/cpufeature.h | 2 + arch/riscv/include/asm/hwcap.h | 6 +++ arch/riscv/include/asm/pgtable-64.h | 12 ++++++ arch/riscv/kernel/alternative.c | 24 +++++++---- arch/riscv/kernel/cpufeature.c | 41 +++++++++++++++---- arch/riscv/mm/init.c | 9 +++++ 9 files changed, 128 insertions(+), 15 deletions(-)arch/riscv/Kconfig +1
-0@@ -218,6 +218,7 @@ config RISCV select PCI_ECAM if (ACPI && PCI) select PCI_MSI if PCI select RELOCATABLE if !MMU && !PHYS_RAM_BASE_FIXED + select RISCV_ALTERNATIVE_EARLY if 64BIT select RISCV_APLIC select RISCV_IMSIC select RISCV_INTCarch/riscv/include/asm/alternative.h +1
-1@@ -33,7 +33,7 @@ void __init apply_early_boot_alternatives(void);void apply_module_alternatives (void *start,size_t length);void riscv_alternative_fix_offsets (void *alt_ptr,unsigned int len, -int patch_offset); +int patch_offset,bool early);struct alt_entry {s32 old_offset;/* offset relative to original instruction or data */ arch/riscv/include/asm/cpufeature-macros.h +46
-0@@ -1,6 +1,7 @@/* SPDX-License-Identifier: GPL-2.0-only */ /* * Copyright 2022-2024 Rivos, Inc +* Copyright 2026 Jisheng Zhang <[email protected]> */ #ifndef _ASM_CPUFEATURE_MACROS_H@@ -57,4 +58,49 @@ static __always_inline bool riscv_has_extension_likely(const unsigned long ext)return __riscv_has_extension_likely (STANDARD_EXT , ext); } +static __always_inline bool __riscv_has_cap_likely(const unsigned long cap) +{ + asm goto(ALTERNATIVE("j %l[l_no]", "nop", 0, %[cap], 1) + : + : [cap] "i" (cap) + : + : l_no); + +return true; +l_no: +return false; +} + +static __always_inline bool __riscv_has_cap_unlikely(const unsigned long cap) +{ + + asm goto(ALTERNATIVE("nop", "j %l[l_yes]", 0, %[cap], 1) + : + : [cap] "i" (cap) + : + : l_yes); + +return false; +l_yes: +return true; +} + +static __always_inline bool riscv_has_cap_unlikely(const unsigned long cap) +{ +compiletime_assert (cap >=RISCV_ISA_EXT_MAX && + cap <RISCV_CAP_MAX , +"cap must be >= RISCV_ISA_EXT_MAX and < RISCV_CAP_MAX" ); + +return __riscv_has_cap_unlikely (cap); +} + +static __always_inline bool riscv_has_cap_likely(const unsigned long cap) +{ +compiletime_assert (cap >=RISCV_ISA_EXT_MAX && + cap <RISCV_CAP_MAX , +"cap must be >= RISCV_ISA_EXT_MAX and < RISCV_CAP_MAX" ); + +return __riscv_has_cap_likely (cap); +} +#endif /* _ASM_CPUFEATURE_MACROS_H */ arch/riscv/include/asm/cpufeature.h +2
-0@@ -36,6 +36,8 @@ extern const struct seq_operations cpuinfo_op;/* Per-cpu ISA extensions. */ extern struct riscv_isainfo hart_isa[NR_CPUS ]; +extern DECLARE_BITMAP(riscv_cap, RISCV_CAP_MAX - RISCV_ISA_EXT_MAX); +extern u32 thead_vlenb_of;void __init riscv_user_isa_enable (void );arch/riscv/include/asm/hwcap.h +6
-0@@ -128,6 +128,12 @@#define RISCV_ISA_EXT_MAX 128#define RISCV_ISA_EXT_INVALID U32_MAX +/* Kernel capabilities occupy the gap below vendor alternative IDs. */ +#define RISCV_CAP_PGTABLE_L4 RISCV_ISA_EXT_MAX +#define RISCV_CAP_PGTABLE_L5 (RISCV_ISA_EXT_MAX + 1) +#define RISCV_CAP_EARLY_MAX (RISCV_ISA_EXT_MAX + 2) +#define RISCV_CAP_MAX RISCV_CAP_EARLY_MAX +#ifdef CONFIG_RISCV_M_MODE #define RISCV_ISA_EXT_SxAIA RISCV_ISA_EXT_SMAIA#define RISCV_ISA_EXT_SUPM RISCV_ISA_EXT_SMNPMarch/riscv/include/asm/pgtable-64.h +12
-0@@ -13,6 +13,7 @@extern bool _pgtable_l4_enabled;extern bool _pgtable_l5_enabled; +#ifdef USE_EARLY_PGTABLE_LEVELS static __always_inline bool pgtable_l5_enabled(void) {return _pgtable_l5_enabled;@@ -22,6 +23,17 @@ static __always_inline bool pgtable_l4_enabled(void) {return _pgtable_l4_enabled; } +#else +static __always_inline bool pgtable_l4_enabled(void) +{ +return riscv_has_cap_likely (RISCV_CAP_PGTABLE_L4 ); +} + +static __always_inline bool pgtable_l5_enabled(void) +{ +return riscv_has_cap_likely (RISCV_CAP_PGTABLE_L5 ); +} +#endif #define PGDIR_SHIFT_L3 30#define PGDIR_SHIFT_L4 39arch/riscv/kernel/alternative.c +17
-7@@ -75,7 +75,8 @@ static u32 riscv_instruction_at(void *p) }static void riscv_alternative_fix_auipc_jalr (void *ptr,u32 auipc_insn, -u32 jalr_insn,int patch_offset) +u32 jalr_insn,int patch_offset, +bool early) {u32 call[2 ] = { auipc_insn, jalr_insn };s32 imm;@@ -88,10 +89,15 @@ static void riscv_alternative_fix_auipc_jalr(void *ptr, u32 auipc_insn,riscv_insn_insert_utype_itype_imm (&call[0 ], &call[1 ], imm);/* patch the call place again */ -patch_text_nosync (ptr, call,sizeof (u32) *2 ); +if (early) { +memcpy (ptr, call,sizeof (call)); + }else { +patch_text_nosync (ptr, call,sizeof (call)); + } } -static void riscv_alternative_fix_jal (void *ptr,u32 jal_insn,int patch_offset) +static void riscv_alternative_fix_jal (void *ptr,u32 jal_insn,int patch_offset, +bool early) {s32 imm;@@ -103,11 +109,14 @@ static void riscv_alternative_fix_jal(void *ptr, u32 jal_insn, int patch_offset)riscv_insn_insert_jtype_imm (&jal_insn, imm);/* patch the call place again */ -patch_text_nosync (ptr, &jal_insn,sizeof (u32)); +if (early) +memcpy (ptr, &jal_insn,sizeof (u32)); +else +patch_text_nosync (ptr, &jal_insn,sizeof (u32)); }void riscv_alternative_fix_offsets (void *alt_ptr,unsigned int len, -int patch_offset) +int patch_offset,bool early) {int num_insn = len /sizeof (u32);int i;@@ -131,7 +140,8 @@ void riscv_alternative_fix_offsets(void *alt_ptr, unsigned int len,continue ;riscv_alternative_fix_auipc_jalr (alt_ptr + i *sizeof (u32), - insn, insn2, patch_offset); + insn, insn2, patch_offset, + early); i++; }@@ -144,7 +154,7 @@ void riscv_alternative_fix_offsets(void *alt_ptr, unsigned int len,continue ;riscv_alternative_fix_jal (alt_ptr + i *sizeof (u32), - insn, patch_offset); + insn, patch_offset, early); } } }arch/riscv/kernel/cpufeature.c +34
-7@@ -40,6 +40,7 @@ unsigned long elf_hwcap __read_mostly;/* Host ISA bitmap */ static DECLARE_BITMAP(riscv_isa, RISCV_ISA_EXT_MAX) __read_mostly; +DECLARE_BITMAP (riscv_cap,RISCV_CAP_MAX -RISCV_ISA_EXT_MAX )__read_mostly ;/* Per-cpu ISA extensions. */ struct riscv_isainfo hart_isa[NR_CPUS ];@@ -81,6 +82,14 @@ bool __riscv_isa_extension_available(const unsigned long *isa_bitmap, unsigned i }EXPORT_SYMBOL_GPL (__riscv_isa_extension_available); +static bool __riscv_cap_available (unsigned int bit) +{ +if (bit >=RISCV_CAP_MAX || bit <RISCV_ISA_EXT_MAX ) +return false; + +return test_bit (bit -RISCV_ISA_EXT_MAX , riscv_cap); +} +static int riscv_ext_f_depends (const struct riscv_isa_ext_data *data,const unsigned long *isa_bitmap) {@@ -1257,9 +1266,7 @@ void __init_or_module riscv_cpufeature_patch_func(struct alt_entry *begin,struct alt_entry *alt;void *oldptr, *altptr;u16 id, value, vendor; - -if (stage ==RISCV_ALTERNATIVES_EARLY_BOOT ) -return ; +bool early = stage ==RISCV_ALTERNATIVES_EARLY_BOOT ;for (alt = begin; alt < end; alt++) { id =PATCH_ID_CPUFEATURE_ID (alt->patch_id);@@ -1274,6 +1281,8 @@ void __init_or_module riscv_cpufeature_patch_func(struct alt_entry *begin,* vendor extension. */ if (id <RISCV_ISA_EXT_MAX ) { +if (early) +continue ;/* * This patch should be treated as errata so skip * processing here. @@ -1287,7 +1296,14 @@ void __init_or_module riscv_cpufeature_patch_func(struct alt_entry *begin, value =PATCH_ID_CPUFEATURE_VALUE (alt->patch_id);if (!riscv_cpufeature_patch_check (id, value))continue ; + }else if (id <RISCV_CAP_MAX ) { +if (id >=RISCV_CAP_EARLY_MAX && early) +continue ; +if (!__riscv_cap_available (id)) +continue ; }else if (id >=RISCV_VENDOR_EXT_ALTERNATIVES_BASE ) { +if (early) +continue ;if (!__riscv_isa_vendor_extension_available (VENDOR_EXT_ALL_CPUS , vendor, id -RISCV_VENDOR_EXT_ALTERNATIVES_BASE ))continue ;@@ -1299,9 +1315,20 @@ void __init_or_module riscv_cpufeature_patch_func(struct alt_entry *begin, oldptr =ALT_OLD_PTR (alt); altptr =ALT_ALT_PTR (alt); -mutex_lock (&text_mutex); -patch_text_nosync (oldptr, altptr, alt->alt_len); -riscv_alternative_fix_offsets (oldptr, alt->alt_len, oldptr - altptr); -mutex_unlock (&text_mutex); +if (early) { +/* oldptr is writable through the MMU-off kernel mapping. */ +memcpy (oldptr, altptr, alt->alt_len); +riscv_alternative_fix_offsets (oldptr, alt->alt_len, + oldptr - altptr, true); + }else { +mutex_lock (&text_mutex); +patch_text_nosync (oldptr, altptr, alt->alt_len); +riscv_alternative_fix_offsets (oldptr, alt->alt_len, + oldptr - altptr, false); +mutex_unlock (&text_mutex); + } } + +if (early) +local_flush_icache_all (); }arch/riscv/mm/init.c +9
-0@@ -6,6 +6,11 @@* Nick Kossifidis <[email protected]> */ +#ifdef CONFIG_64BIT +/* riscv_has_cap_likely() cannot be used this early */ +#define USE_EARLY_PGTABLE_LEVELS +#endif +#include <linux/init.h> #include <linux/mm.h> #include <linux/memblock.h> @@ -892,6 +897,10 @@ static __init void set_satp_mode(uintptr_t dtb_pa)memset (early_p4d,0 ,PAGE_SIZE );memset (early_pud,0 ,PAGE_SIZE );memset (early_pmd,0 ,PAGE_SIZE ); +if (pgtable_l4_enabled ()) +set_bit (RISCV_CAP_PGTABLE_L4 -RISCV_ISA_EXT_MAX , riscv_cap); +if (pgtable_l5_enabled ()) + set_bit(RISCV_CAP_PGTABLE_L5 - RISCV_ISA_EXT_MAX, riscv_cap); }#endif Signature · 1 line
2.53.0
Re: [PATCH 3/3] riscv: introduce cap framework and use it to optimize pgtable_l4|l5_enabled
Quoted 20 lines · Jisheng Zhang
Quoted 15 lines · Conor Dooley
Quoted 4 lines · Jisheng Zhang
The pgtable_l4|[l5]_enabled check sits at hot code path, performance is impacted a lot. Since pgtable_l4|[l5]_enabled isn't changed after boot, we can use alternative mechanism to optimize them.
Quoted 9 lines
So the question is whether we can add RISCV_ISA_EXT_SV48/SV5 and use riscv_has_extension_*() or not. Although, per [1] and [2], SV48 and SV57 are ISA exensions too, RISCV_ISA_EXT_SV48/SV57 are to describe hw supported extensions, while this doesn't mean the pgtable_l4|l5 is enabled, for example, we may pass no5lvl/no4lvl kernel boot args or explicitly ask for SV39 by setting dt mmu-type property as "riscv,sv39". If we clear RISCV_ISA_EXT_SV48|SV57, then internal extension queries and potentially userspace reporting can no longer distinguish “unsupported” from “supported but disabled.”
Quoted 3 lines
That's the case for all extensions that the kernel does not support, like if the fpu is disabled because the kernel doesn't have the configuration option set. In fact, I think the CFI extensions are very
Quoted 13 lines
To be honest, before introduce the cap framework, I did think about FPU, ZACAS, ZABHA etc. ISA extensions' cases. Finally I came to a conclusion that the SV48/57 is a different case: Let me take FPU for an example. FPU once probed, the ISA support is never disabled, IOW, the
riscv_has_extension_likely(FPU)is always true no matter theCONFIG_FPUis enabled or not. There's "no supported but disabled" case for FPU. But for SV48 case, that's different. The underlying HW may support SV48 or SV57, but the "no5lvl and no4lvl" may explicitly force SV39, thus if we go with exisiting riscv_has_extension_*() API, we need to explicitly disable it by clear the isa bitmap. IOW,riscv_has_extension_likely(SV48)is false even the underlying HW supports SV48, thus the user can't distinguish between “unsupported” from “supported but disabled.”
What do you think?
Other thoughts:
performance PoV: usually, an ISA extension improves the performance, so there's no case where we clear the ISA bit map. But SV48/SV57 is different, e.g on a board w/ only 4GB DDR but the cpu supports SV57, there's no reason to enable SV57 which impacts performance a bit. This is the reason why no4lvl/no5lvl are introduced, AIUI.
other cap usage PoV: the cap can be used to replace various static branch usage in arch/riscv, for example the fast_unaligned_access_speed_key As is known, the static branch has some drawbacks.
So no matter which direction this series goes to, we need similar cap framework. The key question is whether we can extend the riscv_has_extension_*() to cover cpu/HW cap, and change its meaning a bit from "the underlying HW supports the feature" to "the underlying HW enables the feature".
or keep it only for ISA extensions only.
X86's X86_FEATURE_* take the first path, I.E it only means the feature is enabled or disabled on current platform, but doesn't mean the current platform HW supports the feature or not.
Any comments is appreciated.
Quoted 392 lines
Quoted 2 lines
similar here, because they also have
riscv_nousercficommand lineThis is another story, neither CPU ISA extension nor CPU capbility.
Quoted 377 lines
options that can disable them.
If that's the reason for making a new-but-similar mechanism, I think you should just introduce RISCV_ISA_EXT_SV{48,57} and use that.Cheers,
Conor.Quoted 370 lines
Introduce cap framework to describe the capabilities selected by kernel. It also uses similar alternatives mechanism as the riscv_has_extension_*() helpers.
After that, use it to optimize pgtable_l4|l5_enabled.
For the typical
access_ok(addr, 1);
before the patch:...
auipc a5,0xb43
lbu a5,100(a5) #ffffffff80b51f68<pgtable_l5_enabled>
bnez a5,ffffffff8000ef46 <foo+0x56>
auipc a5,0xb43
lbu a5,91(a5) #ffffffff80b51f69<pgtable_l4_enabled>
beqz a5,ffffffff8000ef5a <foo+0x6a>
...after the patch:
there are only two j or nop instructions which avoid memory load and test branch.Initial test lmbench's
lat_syscallwrite on TH1520 platforms shows that the write syscall latency is reduced by about 2.38%.Signed-off-by Jisheng Zhang <[email protected]>--- arch/riscv/Kconfig | 1 + arch/riscv/include/asm/alternative.h | 2 +- arch/riscv/include/asm/cpufeature-macros.h | 46 ++++++++++++++++++++++ arch/riscv/include/asm/cpufeature.h | 2 + arch/riscv/include/asm/hwcap.h | 6 +++ arch/riscv/include/asm/pgtable-64.h | 12 ++++++ arch/riscv/kernel/alternative.c | 24 +++++++---- arch/riscv/kernel/cpufeature.c | 41 +++++++++++++++---- arch/riscv/mm/init.c | 9 +++++ 9 files changed, 128 insertions(+), 15 deletions(-)arch/riscv/Kconfig +1
-0@@ -218,6 +218,7 @@ config RISCV select PCI_ECAM if (ACPI && PCI) select PCI_MSI if PCI select RELOCATABLE if !MMU && !PHYS_RAM_BASE_FIXED + select RISCV_ALTERNATIVE_EARLY if 64BIT select RISCV_APLIC select RISCV_IMSIC select RISCV_INTCarch/riscv/include/asm/alternative.h +1
-1@@ -33,7 +33,7 @@ void __init apply_early_boot_alternatives(void);void apply_module_alternatives (void *start,size_t length);void riscv_alternative_fix_offsets (void *alt_ptr,unsigned int len, -int patch_offset); +int patch_offset,bool early);struct alt_entry {s32 old_offset;/* offset relative to original instruction or data */ arch/riscv/include/asm/cpufeature-macros.h +46
-0@@ -1,6 +1,7 @@/* SPDX-License-Identifier: GPL-2.0-only */ /* * Copyright 2022-2024 Rivos, Inc +* Copyright 2026 Jisheng Zhang <[email protected]> */ #ifndef _ASM_CPUFEATURE_MACROS_H@@ -57,4 +58,49 @@ static __always_inline bool riscv_has_extension_likely(const unsigned long ext)return __riscv_has_extension_likely (STANDARD_EXT , ext); } +static __always_inline bool __riscv_has_cap_likely(const unsigned long cap) +{ + asm goto(ALTERNATIVE("j %l[l_no]", "nop", 0, %[cap], 1) + : + : [cap] "i" (cap) + : + : l_no); + +return true; +l_no: +return false; +} + +static __always_inline bool __riscv_has_cap_unlikely(const unsigned long cap) +{ + + asm goto(ALTERNATIVE("nop", "j %l[l_yes]", 0, %[cap], 1) + : + : [cap] "i" (cap) + : + : l_yes); + +return false; +l_yes: +return true; +} + +static __always_inline bool riscv_has_cap_unlikely(const unsigned long cap) +{ +compiletime_assert (cap >=RISCV_ISA_EXT_MAX && + cap <RISCV_CAP_MAX , +"cap must be >= RISCV_ISA_EXT_MAX and < RISCV_CAP_MAX" ); + +return __riscv_has_cap_unlikely (cap); +} + +static __always_inline bool riscv_has_cap_likely(const unsigned long cap) +{ +compiletime_assert (cap >=RISCV_ISA_EXT_MAX && + cap <RISCV_CAP_MAX , +"cap must be >= RISCV_ISA_EXT_MAX and < RISCV_CAP_MAX" ); + +return __riscv_has_cap_likely (cap); +} +#endif /* _ASM_CPUFEATURE_MACROS_H */ arch/riscv/include/asm/cpufeature.h +2
-0@@ -36,6 +36,8 @@ extern const struct seq_operations cpuinfo_op;/* Per-cpu ISA extensions. */ extern struct riscv_isainfo hart_isa[NR_CPUS ]; +extern DECLARE_BITMAP(riscv_cap, RISCV_CAP_MAX - RISCV_ISA_EXT_MAX); +extern u32 thead_vlenb_of;void __init riscv_user_isa_enable (void );arch/riscv/include/asm/hwcap.h +6
-0@@ -128,6 +128,12 @@#define RISCV_ISA_EXT_MAX 128#define RISCV_ISA_EXT_INVALID U32_MAX +/* Kernel capabilities occupy the gap below vendor alternative IDs. */ +#define RISCV_CAP_PGTABLE_L4 RISCV_ISA_EXT_MAX +#define RISCV_CAP_PGTABLE_L5 (RISCV_ISA_EXT_MAX + 1) +#define RISCV_CAP_EARLY_MAX (RISCV_ISA_EXT_MAX + 2) +#define RISCV_CAP_MAX RISCV_CAP_EARLY_MAX +#ifdef CONFIG_RISCV_M_MODE #define RISCV_ISA_EXT_SxAIA RISCV_ISA_EXT_SMAIA#define RISCV_ISA_EXT_SUPM RISCV_ISA_EXT_SMNPMarch/riscv/include/asm/pgtable-64.h +12
-0@@ -13,6 +13,7 @@extern bool _pgtable_l4_enabled;extern bool _pgtable_l5_enabled; +#ifdef USE_EARLY_PGTABLE_LEVELS static __always_inline bool pgtable_l5_enabled(void) {return _pgtable_l5_enabled;@@ -22,6 +23,17 @@ static __always_inline bool pgtable_l4_enabled(void) {return _pgtable_l4_enabled; } +#else +static __always_inline bool pgtable_l4_enabled(void) +{ +return riscv_has_cap_likely (RISCV_CAP_PGTABLE_L4 ); +} + +static __always_inline bool pgtable_l5_enabled(void) +{ +return riscv_has_cap_likely (RISCV_CAP_PGTABLE_L5 ); +} +#endif #define PGDIR_SHIFT_L3 30#define PGDIR_SHIFT_L4 39arch/riscv/kernel/alternative.c +17
-7@@ -75,7 +75,8 @@ static u32 riscv_instruction_at(void *p) }static void riscv_alternative_fix_auipc_jalr (void *ptr,u32 auipc_insn, -u32 jalr_insn,int patch_offset) +u32 jalr_insn,int patch_offset, +bool early) {u32 call[2 ] = { auipc_insn, jalr_insn };s32 imm;@@ -88,10 +89,15 @@ static void riscv_alternative_fix_auipc_jalr(void *ptr, u32 auipc_insn,riscv_insn_insert_utype_itype_imm (&call[0 ], &call[1 ], imm);/* patch the call place again */ -patch_text_nosync (ptr, call,sizeof (u32) *2 ); +if (early) { +memcpy (ptr, call,sizeof (call)); + }else { +patch_text_nosync (ptr, call,sizeof (call)); + } } -static void riscv_alternative_fix_jal (void *ptr,u32 jal_insn,int patch_offset) +static void riscv_alternative_fix_jal (void *ptr,u32 jal_insn,int patch_offset, +bool early) {s32 imm;@@ -103,11 +109,14 @@ static void riscv_alternative_fix_jal(void *ptr, u32 jal_insn, int patch_offset)riscv_insn_insert_jtype_imm (&jal_insn, imm);/* patch the call place again */ -patch_text_nosync (ptr, &jal_insn,sizeof (u32)); +if (early) +memcpy (ptr, &jal_insn,sizeof (u32)); +else +patch_text_nosync (ptr, &jal_insn,sizeof (u32)); }void riscv_alternative_fix_offsets (void *alt_ptr,unsigned int len, -int patch_offset) +int patch_offset,bool early) {int num_insn = len /sizeof (u32);int i;@@ -131,7 +140,8 @@ void riscv_alternative_fix_offsets(void *alt_ptr, unsigned int len,continue ;riscv_alternative_fix_auipc_jalr (alt_ptr + i *sizeof (u32), - insn, insn2, patch_offset); + insn, insn2, patch_offset, + early); i++; }@@ -144,7 +154,7 @@ void riscv_alternative_fix_offsets(void *alt_ptr, unsigned int len,continue ;riscv_alternative_fix_jal (alt_ptr + i *sizeof (u32), - insn, patch_offset); + insn, patch_offset, early); } } }arch/riscv/kernel/cpufeature.c +34
-7@@ -40,6 +40,7 @@ unsigned long elf_hwcap __read_mostly;/* Host ISA bitmap */ static DECLARE_BITMAP(riscv_isa, RISCV_ISA_EXT_MAX) __read_mostly; +DECLARE_BITMAP (riscv_cap,RISCV_CAP_MAX -RISCV_ISA_EXT_MAX )__read_mostly ;/* Per-cpu ISA extensions. */ struct riscv_isainfo hart_isa[NR_CPUS ];@@ -81,6 +82,14 @@ bool __riscv_isa_extension_available(const unsigned long *isa_bitmap, unsigned i }EXPORT_SYMBOL_GPL (__riscv_isa_extension_available); +static bool __riscv_cap_available (unsigned int bit) +{ +if (bit >=RISCV_CAP_MAX || bit <RISCV_ISA_EXT_MAX ) +return false; + +return test_bit (bit -RISCV_ISA_EXT_MAX , riscv_cap); +} +static int riscv_ext_f_depends (const struct riscv_isa_ext_data *data,const unsigned long *isa_bitmap) {@@ -1257,9 +1266,7 @@ void __init_or_module riscv_cpufeature_patch_func(struct alt_entry *begin,struct alt_entry *alt;void *oldptr, *altptr;u16 id, value, vendor; - -if (stage ==RISCV_ALTERNATIVES_EARLY_BOOT ) -return ; +bool early = stage ==RISCV_ALTERNATIVES_EARLY_BOOT ;for (alt = begin; alt < end; alt++) { id =PATCH_ID_CPUFEATURE_ID (alt->patch_id);@@ -1274,6 +1281,8 @@ void __init_or_module riscv_cpufeature_patch_func(struct alt_entry *begin,* vendor extension. */ if (id <RISCV_ISA_EXT_MAX ) { +if (early) +continue ;/* * This patch should be treated as errata so skip * processing here. @@ -1287,7 +1296,14 @@ void __init_or_module riscv_cpufeature_patch_func(struct alt_entry *begin, value =PATCH_ID_CPUFEATURE_VALUE (alt->patch_id);if (!riscv_cpufeature_patch_check (id, value))continue ; + }else if (id <RISCV_CAP_MAX ) { +if (id >=RISCV_CAP_EARLY_MAX && early) +continue ; +if (!__riscv_cap_available (id)) +continue ; }else if (id >=RISCV_VENDOR_EXT_ALTERNATIVES_BASE ) { +if (early) +continue ;if (!__riscv_isa_vendor_extension_available (VENDOR_EXT_ALL_CPUS , vendor, id -RISCV_VENDOR_EXT_ALTERNATIVES_BASE ))continue ;@@ -1299,9 +1315,20 @@ void __init_or_module riscv_cpufeature_patch_func(struct alt_entry *begin, oldptr =ALT_OLD_PTR (alt); altptr =ALT_ALT_PTR (alt); -mutex_lock (&text_mutex); -patch_text_nosync (oldptr, altptr, alt->alt_len); -riscv_alternative_fix_offsets (oldptr, alt->alt_len, oldptr - altptr); -mutex_unlock (&text_mutex); +if (early) { +/* oldptr is writable through the MMU-off kernel mapping. */ +memcpy (oldptr, altptr, alt->alt_len); +riscv_alternative_fix_offsets (oldptr, alt->alt_len, + oldptr - altptr, true); + }else { +mutex_lock (&text_mutex); +patch_text_nosync (oldptr, altptr, alt->alt_len); +riscv_alternative_fix_offsets (oldptr, alt->alt_len, + oldptr - altptr, false); +mutex_unlock (&text_mutex); + } } + +if (early) +local_flush_icache_all (); }arch/riscv/mm/init.c +9
-0@@ -6,6 +6,11 @@* Nick Kossifidis <[email protected]> */ +#ifdef CONFIG_64BIT +/* riscv_has_cap_likely() cannot be used this early */ +#define USE_EARLY_PGTABLE_LEVELS +#endif +#include <linux/init.h> #include <linux/mm.h> #include <linux/memblock.h> @@ -892,6 +897,10 @@ static __init void set_satp_mode(uintptr_t dtb_pa)memset (early_p4d,0 ,PAGE_SIZE );memset (early_pud,0 ,PAGE_SIZE );memset (early_pmd,0 ,PAGE_SIZE ); +if (pgtable_l4_enabled ()) +set_bit (RISCV_CAP_PGTABLE_L4 -RISCV_ISA_EXT_MAX , riscv_cap); +if (pgtable_l5_enabled ()) + set_bit(RISCV_CAP_PGTABLE_L5 - RISCV_ISA_EXT_MAX, riscv_cap); }#endif Quoted 3 lines
Quoted 3 lines
Signature · 1 line
2.53.0_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Re: [PATCH 3/3] riscv: introduce cap framework and use it to optimize pgtable_l4|l5_enabled
Quoted 21 lines · Jisheng Zhang
Quoted 20 lines · Jisheng Zhang
Quoted 18 lines · Conor Dooley
Quoted 4 lines · Jisheng Zhang
The pgtable_l4|[l5]_enabled check sits at hot code path, performance is impacted a lot. Since pgtable_l4|[l5]_enabled isn't changed after boot, we can use alternative mechanism to optimize them.
Quoted 9 lines
So the question is whether we can add RISCV_ISA_EXT_SV48/SV5 and use riscv_has_extension_*() or not. Although, per [1] and [2], SV48 and SV57 are ISA exensions too, RISCV_ISA_EXT_SV48/SV57 are to describe hw supported extensions, while this doesn't mean the pgtable_l4|l5 is enabled, for example, we may pass no5lvl/no4lvl kernel boot args or explicitly ask for SV39 by setting dt mmu-type property as "riscv,sv39". If we clear RISCV_ISA_EXT_SV48|SV57, then internal extension queries and potentially userspace reporting can no longer distinguish “unsupported” from “supported but disabled.”
That's the case for all extensions that the kernel does not support, like if the fpu is disabled because the kernel doesn't have the configuration option set. In fact, I think the CFI extensions are very
Quoted 13 lines
Quoted 5 lines
To be honest, before introduce the cap framework, I did think about FPU, ZACAS, ZABHA etc. ISA extensions' cases. Finally I came to a conclusion that the SV48/57 is a different case: Let me take FPU for an example. FPU once probed, the ISA support is never disabled, IOW, the
riscv_has_extension_likely(FPU)is always true no matter theCONFIG_FPUQuoted 8 lines
is enabled or not. There's "no supported but disabled" case for FPU. But for SV48 case, that's different. The underlying HW may support SV48 or SV57, but the "no5lvl and no4lvl" may explicitly force SV39, thus if we go with exisiting riscv_has_extension_*() API, we need to explicitly disable it by clear the isa bitmap. IOW,
riscv_has_extension_likely(SV48)is false even the underlying HW supports SV48, thus the user can't distinguish between “unsupported” from “supported but disabled.”
What do you think?
(moving things around a little bit)
Quoted 11 lines
and change its meaning a
bit from "the underlying HW supports the feature" to "the underlying HW enables the feature".or keep it only for ISA extensions only.
X86's X86_FEATURE_* take the first path, I.E it only means the feature is enabled or disabled on current platform, but doesn't mean the current platform HW supports the feature or not.
Any comments is appreciated.
On this particular point, this is already how it works. riscv_has_extension_[un]likely() and other related functions report whether the extension is supported and enabled on the platform, not just whether the underlying hardware can support it. As I mentioned yesterday, the F and D extensions depend on CONFIG_FPU being enabled. If CONFIG_FPU is disabled, but F and D appear in the DT or ACPI, we clear the relevant bits in the bitmap. In turn, if there are extensions that depend on F and D being enabled, they'll get turned off too.
Quoted 8 lines
Other thoughts:
performance PoV: usually, an ISA extension improves the performance, so there's no case where we clear the ISA bit map. But SV48/SV57 is different, e.g on a board w/ only 4GB DDR but the cpu supports SV57, there's no reason to enable SV57 which impacts performance a bit. This is the reason why no4lvl/no5lvl are introduced, AIUI.
Again, I think this applies to CFI, I don't think what's being done here is fundamentally different to that.
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
similar here, because they also have
riscv_nousercficommand lineThis is another story, neither CPU ISA extension nor CPU capbility.
They are extensions though, riscv_nousercfi disables the cfilp and cfiss extensions. You can see that in cpufeature.c. Maybe there's some semantic argument about whether they're /CPU/ extensions, but there's other stuff that aren't really CPU extensions and we treat them all the same. I guess the difference for sv48/sv57 is that we'd just be setting the bitmap bits rather than parsing it from DT/ACPI - but there's prior art for that already.
Quoted 5 lines
Quoted 4 lines
Quoted 4 lines
options that can disable them.
If that's the reason for making a new-but-similar mechanism, I think you should just introduce RISCV_ISA_EXT_SV{48,57} and use that.
Quoted 7 lines
other cap usage PoV: the cap can be used to replace various static branch usage in
arch/riscv, for example thefast_unaligned_access_speed_keyAs is known, the static branch has some drawbacks.So no matter which direction this series goes to, we need similar cap framework. The key question is whether we can extend the riscv_has_extension_*() to cover cpu/HW cap,
It'd be nice to see if we could reuse the functions, if we don't want to invent a bunch of XLINUXFOO for them. The vendor extension stuff that Charlie added reuses it, but since there's no Linux Foundation JEDEC ID (AFAIK anyway) perhaps there's just no easy way to do it and duplicating the functions with a different name is required.
This particular case is an extension, so I think we should try to make it work using that framework though.
Re: [PATCH 3/3] riscv: introduce cap framework and use it to optimize pgtable_l4|l5_enabled
Quoted 48 lines · Conor Dooley
Quoted 21 lines · Jisheng Zhang
Quoted 20 lines · Jisheng Zhang
Quoted 18 lines · Conor Dooley
Quoted 4 lines · Jisheng Zhang
The pgtable_l4|[l5]_enabled check sits at hot code path, performance is impacted a lot. Since pgtable_l4|[l5]_enabled isn't changed after boot, we can use alternative mechanism to optimize them.
Quoted 9 lines
So the question is whether we can add RISCV_ISA_EXT_SV48/SV5 and use riscv_has_extension_*() or not. Although, per [1] and [2], SV48 and SV57 are ISA exensions too, RISCV_ISA_EXT_SV48/SV57 are to describe hw supported extensions, while this doesn't mean the pgtable_l4|l5 is enabled, for example, we may pass no5lvl/no4lvl kernel boot args or explicitly ask for SV39 by setting dt mmu-type property as "riscv,sv39". If we clear RISCV_ISA_EXT_SV48|SV57, then internal extension queries and potentially userspace reporting can no longer distinguish “unsupported” from “supported but disabled.”
That's the case for all extensions that the kernel does not support, like if the fpu is disabled because the kernel doesn't have the configuration option set. In fact, I think the CFI extensions are very
Quoted 13 lines
Quoted 5 lines
To be honest, before introduce the cap framework, I did think about FPU, ZACAS, ZABHA etc. ISA extensions' cases. Finally I came to a conclusion that the SV48/57 is a different case: Let me take FPU for an example. FPU once probed, the ISA support is never disabled, IOW, the
riscv_has_extension_likely(FPU)is always true no matter theCONFIG_FPUQuoted 8 lines
is enabled or not. There's "no supported but disabled" case for FPU. But for SV48 case, that's different. The underlying HW may support SV48 or SV57, but the "no5lvl and no4lvl" may explicitly force SV39, thus if we go with exisiting riscv_has_extension_*() API, we need to explicitly disable it by clear the isa bitmap. IOW,
riscv_has_extension_likely(SV48)is false even the underlying HW supports SV48, thus the user can't distinguish between “unsupported” from “supported but disabled.”
What do you think?(moving things around a little bit)
Quoted 10 lines
and change its meaning a
bit from "the underlying HW supports the feature" to "the underlying HW enables the feature".or keep it only for ISA extensions only.
X86's X86_FEATURE_* take the first path, I.E it only means the feature is enabled or disabled on current platform, but doesn't mean the current platform HW supports the feature or not.
Quoted 6 lines
Quoted 1 line
Any comments is appreciated.
On this particular point, this is already how it works. riscv_has_extension_[un]likely() and other related functions report whether the extension is supported and enaboled on the platform, not just whether the underlying hardware can support it. As I mentioned
So this goes with the X86 style. I don't have any preference, but just want to mention that we have another direction. If X86 style is agreed, I will make it in v2. But I have a question below.
Quoted 42 lines
yesterday, the F and D extensions depend on
CONFIG_FPUbeing enabled. IfCONFIG_FPUis disabled, but F and D appear in the DT or ACPI, we clear the relevant bits in the bitmap. In turn, if there are extensions that depend on F and D being enabled, they'll get turned off too.Quoted 8 lines
Other thoughts:
performance PoV: usually, an ISA extension improves the performance, so there's no case where we clear the ISA bit map. But SV48/SV57 is different, e.g on a board w/ only 4GB DDR but the cpu supports SV57, there's no reason to enable SV57 which impacts performance a bit. This is the reason why no4lvl/no5lvl are introduced, AIUI.
Again, I think this applies to CFI, I don't think what's being done here is fundamentally different to that.
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
similar here, because they also have
riscv_nousercficommand lineThis is another story, neither CPU ISA extension nor CPU capbility.
They are extensions though,
riscv_nousercfidisables the cfilp and cfiss extensions. You can see that incpufeature.c. Maybe there's some semantic argument about whether they're /CPU/ extensions, but there's other stuff that aren't really CPU extensions and we treat them all the same. I guess the difference for sv48/sv57 is that we'd just be setting the bitmap bits rather than parsing it from DT/ACPI - but there's prior art for that already.Quoted 5 lines
Quoted 4 lines
Quoted 4 lines
options that can disable them.
If that's the reason for making a new-but-similar mechanism, I think you should just introduce RISCV_ISA_EXT_SV{48,57} and use that.Quoted 7 lines
other cap usage PoV: the cap can be used to replace various static branch usage in
arch/riscv, for example thefast_unaligned_access_speed_keyAs is known, the static branch has some drawbacks.So no matter which direction this series goes to, we need similar cap framework. The key question is whether we can extend the riscv_has_extension_*() to cover cpu/HW cap,
Quoted 5 lines
It'd be nice to see if we could reuse the functions, if we don't want to invent a bunch of XLINUXFOO for them. The vendor extension stuff that Charlie added reuses it, but since there's no Linux Foundation JEDEC ID (AFAIK anyway) perhaps there's just no easy way to do it and duplicating the functions with a different name is required.
what about if convert fast_unaligned_access_speed_key from static branch to alternative? It's a cpu HW capbility not extension. rename the riscv_has_extension_*() to riscv_has_cap_*()?
Quoted 3 lines
This particular case is an extension, so I think we should try to make it work using that framework though.
Re: [PATCH 3/3] riscv: introduce cap framework and use it to optimize pgtable_l4|l5_enabled
Quoted 14 lines · Jisheng Zhang
Quoted 8 lines
Quoted 7 lines
other cap usage PoV: the cap can be used to replace various static branch usage in
arch/riscv, for example thefast_unaligned_access_speed_keyAs is known, the static branch has some drawbacks.So no matter which direction this series goes to, we need similar cap framework. The key question is whether we can extend the riscv_has_extension_*() to cover cpu/HW cap,
Quoted 5 lines
It'd be nice to see if we could reuse the functions, if we don't want to invent a bunch of XLINUXFOO for them. The vendor extension stuff that Charlie added reuses it, but since there's no Linux Foundation JEDEC ID (AFAIK anyway) perhaps there's just no easy way to do it and duplicating the functions with a different name is required.
Quoted 3 lines
what about if convert
fast_unaligned_access_speed_keyfrom static branch to alternative? It's a cpu HW capbility not extension. rename the riscv_has_extension_*() to riscv_has_cap_*()?
Is there any value in actually moving that one to an alternative? I think it currently only has one user?
If there is value, I'd rather see __riscv_has_extension_*() get renamed to __riscv_has_cap*(), so that things like unaligned access speed get a different namespace to standard and vendor extensions. IOW you'd end up with
static __always_inline bool riscv_has_extension_unlikely(const unsigned long ext)
{
compiletime_assert(ext < RISCV_ISA_EXT_MAX, "ext must be < RISCV_ISA_EXT_MAX");
return __riscv_has_cap_unlikely(STANDARD_EXT, ext);
}static __always_inline bool riscv_has_vendor_extension_unlikely(const unsigned long vendor,
const unsigned long ext)
{
if (!IS_ENABLED(CONFIG_RISCV_ISA_VENDOR_EXT))
return false;
return __riscv_has_cap_unlikely(vendor, ext + RISCV_VENDOR_EXT_ALTERNATIVES_BASE);
}static __always_inline bool riscv_has_cap_unlikely(const unsigned long cap)
{
return __riscv_has_cap_unlikely(LINUX_CAP, cap);
}Currently standard extensions use 0 as the vendor, perhaps we could then use -1 (the value is an unsigned long) for non extension related caps?