optimize pgtable_l4|l5_enabled
v2 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. Per [1] and [2], SV48 and SV57 are ISA exensions too. From another side, riscv_has_extension_[un]likely() and other related functions report whether the extension is supported and enabled on the platform. So SV48 and SV57 can be supported with current isa extension alternative mechanism.
However, to use it to optimize pgtable_l4|l5_enabled, we have support the "early" characteristic, I.E besides risc_isa bitmap setting, we need to support appling alternative early before MMU on.
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:
These memory load and test branch instructions are replaced with only two j or nop instructions.
Initial test lmbench's lat_syscall write on TH1520 platforms shows that the write syscall latency is reduced by about 2.38%.
After that, introduce RISCV_ISA_SV48 and RISCV_ISA_SV57 under NONPORTABLE, so that the embedded platforms can choose the best option themselves, while still keep the feature of unified one kernel Image for all SV39, SV48 and SV57 when !NONPORTABLE.
Tested on TH1520 platforms with both RISCV_ISA_SV48 and RISCV_ISA_SV48 disabled, we saved 20 instructions for access_ok(addr, 1)
Since v1:
- use current isa extension to support SV48/SV57
- collect Reviewed-by tag
- add patch4 to introduce RISCV_ISA_SV48 and RISCV_ISA_SV57 under NONPORTABLE
- add patch5 to unexport _pgtable_l4_enabled and _pgtable_l5_enabled
Jisheng Zhang (5):
riscv: remove RISCV_ALTERNATIVE Kconfig option
riscv: convert pgtable_l4|l5_enabled to inline function
riscv: support early isa ext and use it to optimize
pgtable_l4|l5_enabled
riscv: RISCV_ISA_SV48 and RISCV_ISA_SV57 under NONPORTABLE
riscv: mm: unexport _pgtable_l4_enabled and _pgtable_l5_enabled
arch/riscv/Kconfig | 48 ++++++------ 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 | 10 +-- arch/riscv/include/asm/cpufeature.h | 8 +- arch/riscv/include/asm/hwcap.h | 2 + arch/riscv/include/asm/pgalloc.h | 14 ++-- arch/riscv/include/asm/pgtable-32.h | 4 +- arch/riscv/include/asm/pgtable-64.h | 74 ++++++++++++++----- 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 | 62 ++++++++++++---- arch/riscv/mm/init.c | 81 +++++++++++++-------- arch/riscv/mm/kasan_init.c | 20 ++--- arch/riscv/mm/pgtable.c | 4 +- arch/riscv/mm/ptdump.c | 4 +- 21 files changed, 238 insertions(+), 191 deletions(-)
Signature · 1 line
2.53.0
PATCH v2 1/5 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
PATCH v2 2/5 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 (void ) {return false; } +static inline bool pgtable_l5_enabled (void ) {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 v2 3/5 riscv: support early isa ext 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. Per [1] and [2], SV48 and SV57 are ISA exensions too. From another side, riscv_has_extension_[un]likely() and other related functions report whether the extension is supported and enabled on the platform. So SV48 and SV57 can be supported with current isa extension alternative mechanism.
However, to use it to optimize pgtable_l4|l5_enabled, we have support the "early" characteristic, I.E besides risc_isa bitmap setting, we need to support appling alternative early before MMU on.
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:
These memory load and test branch instructions are replaced with only two j or nop instructions.
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.h | 2 + arch/riscv/include/asm/hwcap.h | 2 + arch/riscv/include/asm/pgtable-64.h | 12 ++++++ arch/riscv/kernel/alternative.c | 24 +++++++---- arch/riscv/kernel/cpufeature.c | 60 ++++++++++++++++++++++------ arch/riscv/mm/init.c | 9 +++++ 8 files changed, 91 insertions(+), 21 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.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_isa, RISCV_ISA_EXT_MAX); +extern u32 thead_vlenb_of;void __init riscv_user_isa_enable (void );
arch/riscv/include/asm/hwcap.h +2 -0
@@ -122,6 +122,8 @@#define RISCV_ISA_EXT_ZICCAMOA 113#define RISCV_ISA_EXT_ZICCIF 114#define RISCV_ISA_EXT_ZA64RS 115 +#define RISCV_ISA_EXT_SV48 116 +#define RISCV_ISA_EXT_SV57 117#define RISCV_ISA_EXT_XLINUXENVCFG 127
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_extension_likely (RISCV_ISA_EXT_SV48 ); +} + +static __always_inline bool pgtable_l5_enabled(void) +{ +return riscv_has_extension_likely (RISCV_ISA_EXT_SV57 ); +} +#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 +47 -13
142 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_extension_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_ISA_EXT_SV48 , riscv_isa); +if (pgtable_l5_enabled ()) + set_bit(RISCV_ISA_EXT_SV57, riscv_isa); }#endif
Signature · 1 line
2.53.0
PATCH v2 4/5 riscv: RISCV_ISA_SV48 and RISCV_ISA_SV57 under NONPORTABLE
In reply to Jisheng ZhangIn last commit, we have optimized the pgtable_l4|l5_enabled with isa extension alternative mechanism, thus a typical access_ok(addr, 1); is compiled as:
ffffffff8001dd30: 1141 addi sp,sp,-16 ffffffff8001dd32: e022 sd s0,0(sp) ffffffff8001dd34: e406 sd ra,8(sp) ffffffff8001dd36: 0800 addi s0,sp,16 ffffffff8001dd38: 00000013 nop ffffffff8001dd3c: 02a0006f j ffffffff8001dd66 <foo+0x3e> ffffffff8001dd40: 60a2 ld ra,8(sp) ffffffff8001dd42: 6402 ld s0,0(sp) ffffffff8001dd44: 57fd li a5,-1 ffffffff8001dd46: 83a1 srli a5,a5,0x8
ffffffff8001dd48: 00a7b533 sltu a0,a5,a0ffffffff8001dd4c: 00154513 xori a0,a0,1ffffffff8001dd50: 0141 addi sp,sp,16ffffffff8001dd52: 8082 ret
...ffffffff8001dd64: bfe1 j ffffffff8001dd3c <foo+0x14>ffffffff8001dd66: 0180006f j ffffffff8001dd7e <foo+0x56>
ffffffff8001dd6a: 60a2 ld ra,8(sp) ffffffff8001dd6c: 6402 ld s0,0(sp) ffffffff8001dd6e: 57fd li a5,-1 ffffffff8001dd70: 83c5 srli a5,a5,0x11 ffffffff8001dd72: 00a7b533 sltu a0,a5,a0 ffffffff8001dd76: 00154513 xori a0,a0,1 ffffffff8001dd7a: 0141 addi sp,sp,16 ffffffff8001dd7c: 8082 ret ffffffff8001dd7e: 60a2 ld ra,8(sp) ffffffff8001dd80: 6402 ld s0,0(sp) ffffffff8001dd82: 57fd li a5,-1 ffffffff8001dd84: 83e9 srli a5,a5,0x1a
ffffffff8001dd86: 00a7b533 sltu a0,a5,a0ffffffff8001dd8a: 00154513 xori a0,a0,1ffffffff8001dd8e: 0141 addi sp,sp,16ffffffff8001dd90: 8082 ret
As can be seen, different branches for SV39/SV48/SV57 are still there, since access_ok() sits at hot code path, why not reduce the code size to optimize to make the instruction cache happy?
Introduce RISCV_ISA_SV48 and RISCV_ISA_SV57 under NONPORTABLE, so that the embedded platforms can choose the best option themselves, while still keep the feature of unified one kernel Image for all SV39, SV48 and SV57.
Before the patch, vmlinux built with pure RISCV64 defconfig
text data bss dec hex filename 13439437 6790446 483221 20713104 13c0e90 /tmp/old/vmlinux
After the patch, vmlinux built with pure RISCV64 defconfig:
text data bss dec hex filename 13314445 6787934 466837 20569216 139dc80 /tmp/new/vmlinux
.text section is reduced by 122KB!
And after the patch, typical access_ok(addr, 1) is compiled as:
ffffffff8001809a: 1141 addi sp,sp,-16 ffffffff8001809c: e022 sd s0,0(sp) ffffffff8001809e: e406 sd ra,8(sp) ffffffff800180a0: 0800 addi s0,sp,16 ffffffff800180a2: 00000013 nop ffffffff800180a6: 60a2 ld ra,8(sp) ffffffff800180a8: 6402 ld s0,0(sp) ffffffff800180aa: 4785 li a5,1 ffffffff800180ac: 179a slli a5,a5,0x26
ffffffff800180ae: 00f53533 sltu a0,a0,a5ffffffff800180b2: 0141 addi sp,sp,16ffffffff800180b4: 8082 ret
only 12 instruction!! For refernece, before the patch, the access(addr,
1) needs 32 instructions.
--- arch/riscv/Kconfig | 24 ++++++++++++++++++++++++ arch/riscv/include/asm/pgtable-64.h | 12 ++++++++++++ arch/riscv/mm/init.c | 10 ++++++++++ 3 files changed, 46 insertions(+)
arch/riscv/Kconfig +24 -0
@@ -602,6 +602,30 @@ config RISCV_ISA_SSQOSID cache partitioning scheme and use the MCID to track how much cache a process, or a group of processes, is using. +config RISCV_ISA_SV48 + bool "Page-Based 48-bit Virtual-Memory System" + depends on 64BIT && NONPORTABLE + default y + help + Add support for the Page-Based 48-bit Virtual-Memory System. + + Enable this option for systems for which a 39-bit virtual address + space is insufficient. But if 39-bit is enough, disabling this + option will result in smaller kernel size and slighly better + performance. + +config RISCV_ISA_SV57 + bool "Page-Based 57-bit Virtual-Memory System" + depends on 64BIT && NONPORTABLE + default y + help + Add support for the Page-Based 57-bit Virtual-Memory System. + + Enable this option for systems for which a 48-bit virtual address + space is insufficient. But if 48-bit is enough, disabling this + option will result in smaller kernel size and slighly better + performance. + config RISCV_ISA_SVPBMT bool "Svpbmt extension support for supervisor mode page-based memory types" depends on 64BIT && MMU
arch/riscv/include/asm/pgtable-64.h +12 -0
@@ -16,21 +16,33 @@ extern bool _pgtable_l5_enabled;#ifdef USE_EARLY_PGTABLE_LEVELS static __always_inline bool pgtable_l5_enabled(void) { +if (IS_ENABLED (CONFIG_NONPORTABLE ) && !IS_ENABLED (CONFIG_RISCV_ISA_SV57 )) +return false; +return _pgtable_l5_enabled; } static __always_inline bool pgtable_l4_enabled(void) { +if (IS_ENABLED (CONFIG_NONPORTABLE ) && !IS_ENABLED (CONFIG_RISCV_ISA_SV48 )) +return false; +return _pgtable_l4_enabled; }#else static __always_inline bool pgtable_l4_enabled(void) { +if (IS_ENABLED (CONFIG_NONPORTABLE ) && !IS_ENABLED (CONFIG_RISCV_ISA_SV48 )) +return false; +return riscv_has_extension_likely (RISCV_ISA_EXT_SV48 ); } static __always_inline bool pgtable_l5_enabled(void) { +if (IS_ENABLED (CONFIG_NONPORTABLE ) && !IS_ENABLED (CONFIG_RISCV_ISA_SV57 )) +return false; +return riscv_has_extension_likely (RISCV_ISA_EXT_SV57 ); }#endif
arch/riscv/mm/init.c +10 -0
@@ -844,6 +844,16 @@ static __init void set_satp_mode(uintptr_t dtb_pa) kernel_map.page_offset =PAGE_OFFSET_L5 ; +/* Fall back to Sv48 if Sv57 is unsupported on non-portable systems */ +if (IS_ENABLED (CONFIG_NONPORTABLE ) && !IS_ENABLED (CONFIG_RISCV_ISA_SV57 ) && + satp_mode_limit ==0 ) + satp_mode_limit =SATP_MODE_48 ; + +/* Fall back to Sv39 if Sv48 is unsupported on non-portable systems */ +if (IS_ENABLED (CONFIG_NONPORTABLE ) && !IS_ENABLED (CONFIG_RISCV_ISA_SV48 ) && + satp_mode_limit ==SATP_MODE_48 ) + satp_mode_limit =SATP_MODE_39 ; +if (satp_mode_limit ==SATP_MODE_48 ) {disable_pgtable_l5 (); }else if (satp_mode_limit ==SATP_MODE_39 ) {
Signature · 1 line
2.53.0
PATCH v2 5/5 riscv: mm: unexport _pgtable_l4_enabled and _pgtable_l5_enabled
In reply to Jisheng ZhangNow, these two vars are only used by kernel itself, especially the code before MMU on. No modules users any more, unexport them.
--- arch/riscv/mm/init.c | 2 -- 1 file changed, 2 deletions(-)
arch/riscv/mm/init.c +0 -2
@@ -59,8 +59,6 @@ EXPORT_SYMBOL(satp_mode);#ifdef CONFIG_64BIT bool _pgtable_l4_enabled__ro_after_init = true;bool _pgtable_l5_enabled__ro_after_init = true; -EXPORT_SYMBOL (_pgtable_l4_enabled); -EXPORT_SYMBOL (_pgtable_l5_enabled);#endif phys_addr_t phys_ram_base__ro_after_init ;
Signature · 1 line
2.53.0
Re: [PATCH v2 4/5] riscv: RISCV_ISA_SV48 and RISCV_ISA_SV57 under NONPORTABLE
In reply to Jisheng ZhangQuoted 86 lines · Jisheng Zhang
In last commit, we have optimized the pgtable_l4|l5_enabled with isa extension alternative mechanism, thus a typical
access_ok(addr, 1); is compiled as:ffffffff8001dd30: 1141 addi sp,sp,-16 ffffffff8001dd32: e022 sd s0,0(sp) ffffffff8001dd34: e406 sd ra,8(sp) ffffffff8001dd36: 0800 addi s0,sp,16 ffffffff8001dd38: 00000013 nop ffffffff8001dd3c: 02a0006f j ffffffff8001dd66 <foo+0x3e> ffffffff8001dd40: 60a2 ld ra,8(sp) ffffffff8001dd42: 6402 ld s0,0(sp) ffffffff8001dd44: 57fd li a5,-1 ffffffff8001dd46: 83a1 srli a5,a5,0x8
ffffffff8001dd48:00a7b533sltu a0,a5,a0ffffffff8001dd4c: 00154513 xori a0,a0,1ffffffff8001dd50: 0141 addi sp,sp,16ffffffff8001dd52: 8082 ret
...ffffffff8001dd64: bfe1 jffffffff8001dd3c<foo+0x14>ffffffff8001dd66:0180006fjffffffff8001dd7e<foo+0x56>ffffffff8001dd6a: 60a2 ld ra,8(sp) ffffffff8001dd6c: 6402 ld s0,0(sp) ffffffff8001dd6e: 57fd li a5,-1 ffffffff8001dd70: 83c5 srli a5,a5,0x11 ffffffff8001dd72: 00a7b533 sltu a0,a5,a0 ffffffff8001dd76: 00154513 xori a0,a0,1 ffffffff8001dd7a: 0141 addi sp,sp,16 ffffffff8001dd7c: 8082 ret ffffffff8001dd7e: 60a2 ld ra,8(sp) ffffffff8001dd80: 6402 ld s0,0(sp) ffffffff8001dd82: 57fd li a5,-1 ffffffff8001dd84: 83e9 srli a5,a5,0x1a
ffffffff8001dd86:00a7b533sltu a0,a5,a0ffffffff8001dd8a: 00154513 xori a0,a0,1ffffffff8001dd8e: 0141 addi sp,sp,16ffffffff8001dd90: 8082 retAs can be seen, different branches for SV39/SV48/SV57 are still there, since
access_ok()sits at hot code path, why not reduce the code size to optimize to make the instruction cache happy?Introduce
RISCV_ISA_SV48andRISCV_ISA_SV57under NONPORTABLE, so that the embedded platforms can choose the best option themselves, while still keep the feature of unified one kernel Image for all SV39, SV48 and SV57.Before the patch, vmlinux built with pure RISCV64 defconfig
text data bss dec hex filename 13439437 6790446 483221 20713104 13c0e90 /tmp/old/vmlinuxAfter the patch, vmlinux built with pure RISCV64 defconfig:
text data bss dec hex filename 13314445 6787934 466837 20569216 139dc80 /tmp/new/vmlinux.text section is reduced by 122KB!
And after the patch, typical
access_ok(addr, 1)is compiled as:ffffffff8001809a: 1141 addi sp,sp,-16 ffffffff8001809c: e022 sd s0,0(sp) ffffffff8001809e: e406 sd ra,8(sp) ffffffff800180a0: 0800 addi s0,sp,16 ffffffff800180a2: 00000013 nop ffffffff800180a6: 60a2 ld ra,8(sp) ffffffff800180a8: 6402 ld s0,0(sp) ffffffff800180aa: 4785 li a5,1 ffffffff800180ac: 179a slli a5,a5,0x26
ffffffff800180ae:00f53533sltu a0,a0,a5ffffffff800180b2: 0141 addi sp,sp,16ffffffff800180b4: 8082 retonly 12 instruction!! For refernece, before the patch, the access(addr,
1) needs 32 instructions.Signed-off-by Jisheng Zhang <[email protected]>--- arch/riscv/Kconfig | 24 ++++++++++++++++++++++++ arch/riscv/include/asm/pgtable-64.h | 12 ++++++++++++ arch/riscv/mm/init.c | 10 ++++++++++ 3 files changed, 46 insertions(+)arch/riscv/Kconfig +0
-0
arch/riscv/Kconfig config RISCV_ISA_SSQOSID · line 608
Quoted 7 lines
arch/riscv/Kconfig +4
-0@@ -602,6 +602,30 @@ config RISCV_ISA_SSQOSID cache partitioning scheme and use the MCID to track how much cache a process, or a group of processes, is using. +config RISCV_ISA_SV48 + bool "Page-Based 48-bit Virtual-Memory System" + depends on 64BIT && NONPORTABLE + default y
I don't understand how you've got this set up here.
If this is genuinely non-portable, then you should not set the default to enabled. However, I'm not really sure that this is actually non-portable at all....
Quoted 51 lines
arch/riscv/Kconfig +20
-0+ help + Add support for the Page-Based 48-bit Virtual-Memory System. + + Enable this option for systems for which a 39-bit virtual address + space is insufficient. But if 39-bit is enough, disabling this + option will result in smaller kernel size and slighly better + performance. + +config RISCV_ISA_SV57 + bool "Page-Based 57-bit Virtual-Memory System" + depends on 64BIT && NONPORTABLE + default y + help + Add support for the Page-Based 57-bit Virtual-Memory System. + + Enable this option for systems for which a 48-bit virtual address + space is insufficient. But if 48-bit is enough, disabling this + option will result in smaller kernel size and slighly better + performance. + config RISCV_ISA_SVPBMT bool "Svpbmt extension support for supervisor mode page-based memory types" depends on 64BIT && MMUarch/riscv/include/asm/pgtable-64.h +9
-0@@ -16,21 +16,33 @@ extern bool _pgtable_l5_enabled;#ifdef USE_EARLY_PGTABLE_LEVELS static __always_inline bool pgtable_l5_enabled(void) { +if (IS_ENABLED (CONFIG_NONPORTABLE ) && !IS_ENABLED (CONFIG_RISCV_ISA_SV57 )) +return false; +return _pgtable_l5_enabled; } static __always_inline bool pgtable_l4_enabled(void) { +if (IS_ENABLED (CONFIG_NONPORTABLE ) && !IS_ENABLED (CONFIG_RISCV_ISA_SV48 )) +return false; +return _pgtable_l4_enabled; }#else static __always_inline bool pgtable_l4_enabled(void) { +if (IS_ENABLED (CONFIG_NONPORTABLE ) && !IS_ENABLED (CONFIG_RISCV_ISA_SV48 )) +return false; +return riscv_has_extension_likely (RISCV_ISA_EXT_SV48 );
arch/riscv/include/asm/pgtable-64.h extern bool _pgtable_l5_enabled; · line 48
Quoted 10 lines
arch/riscv/include/asm/pgtable-64.h +3
-0} static __always_inline bool pgtable_l5_enabled(void) { +if (IS_ENABLED (CONFIG_NONPORTABLE ) && !IS_ENABLED (CONFIG_RISCV_ISA_SV57 )) +return false; +return riscv_has_extension_likely (RISCV_ISA_EXT_SV57 ); }#endif
...because the code here won't enable sv57 etc if not supported...
arch/riscv/mm/init.c static __init void set_satp_mode(uintptr_t dtb_pa) · line 850
Quoted 12 lines
arch/riscv/mm/init.c +4
-0@@ -844,6 +844,16 @@ static __init void set_satp_mode(uintptr_t dtb_pa) kernel_map.page_offset =PAGE_OFFSET_L5 ; +/* Fall back to Sv48 if Sv57 is unsupported on non-portable systems */ +if (IS_ENABLED (CONFIG_NONPORTABLE ) && !IS_ENABLED (CONFIG_RISCV_ISA_SV57 ) && + satp_mode_limit ==0 ) + satp_mode_limit =SATP_MODE_48 ;
...and neither does the code here.
Quoted 4 lines
Introduce
RISCV_ISA_SV48andRISCV_ISA_SV57under NONPORTABLE, so that the embedded platforms can choose the best option themselves, while still keep the feature of unified one kernel Image for all SV39, SV48 and SV57.
NONPORTABLE is what you're meant to depend on when the kernel that gets built cannot run on platforms without the feature.
Quoted 12 lines
arch/riscv/mm/init.c +6
-0+ +/* Fall back to Sv39 if Sv48 is unsupported on non-portable systems */ +if (IS_ENABLED (CONFIG_NONPORTABLE ) && !IS_ENABLED (CONFIG_RISCV_ISA_SV48 ) && + satp_mode_limit ==SATP_MODE_48 ) + satp_mode_limit =SATP_MODE_39 ; +if (satp_mode_limit ==SATP_MODE_48 ) {disable_pgtable_l5 (); }else if (satp_mode_limit ==SATP_MODE_39 ) {Signature · 1 line
2.53.0
Re: [PATCH v2 3/5] riscv: support early isa ext and use it to optimize pgtable_l4|l5_enabled
In reply to Jisheng ZhangQuoted 222 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.
So the question is whether we can add RISCV_ISA_EXT_SV48/SV5 and use riscv_has_extension_*() or not. Per [1] and [2], SV48 and SV57 are ISA exensions too. From another side, riscv_has_extension_[un]likely() and other related functions report whether the extension is supported and enabled on the platform. So SV48 and SV57 can be supported with current isa extension alternative mechanism.
However, to use it to optimize pgtable_l4|l5_enabled, we have support the "early" characteristic, I.E besides
risc_isabitmap setting, we need to support appling alternative early before MMU on.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:
These memory load and test branch instructions are replaced with only two j or nop instructions.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.h | 2 + arch/riscv/include/asm/hwcap.h | 2 + arch/riscv/include/asm/pgtable-64.h | 12 ++++++ arch/riscv/kernel/alternative.c | 24 +++++++---- arch/riscv/kernel/cpufeature.c | 60 ++++++++++++++++++++++------ arch/riscv/mm/init.c | 9 +++++ 8 files changed, 91 insertions(+), 21 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.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_isa, RISCV_ISA_EXT_MAX); +extern u32 thead_vlenb_of;void __init riscv_user_isa_enable (void );arch/riscv/include/asm/hwcap.h +2
-0@@ -122,6 +122,8 @@#define RISCV_ISA_EXT_ZICCAMOA 113#define RISCV_ISA_EXT_ZICCIF 114#define RISCV_ISA_EXT_ZA64RS 115 +#define RISCV_ISA_EXT_SV48 116 +#define RISCV_ISA_EXT_SV57 117#define RISCV_ISA_EXT_XLINUXENVCFG 127arch/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_extension_likely (RISCV_ISA_EXT_SV48 ); +} + +static __always_inline bool pgtable_l5_enabled(void) +{ +return riscv_has_extension_likely (RISCV_ISA_EXT_SV57 ); +} +#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 +3
-1@@ -39,7 +39,7 @@ static bool any_cpu_has_zicbom;unsigned long elf_hwcap__read_mostly ;/* Host ISA bitmap */ -static DECLARE_BITMAP(riscv_isa, RISCV_ISA_EXT_MAX) __read_mostly; +DECLARE_BITMAP (riscv_isa,RISCV_ISA_EXT_MAX )__read_mostly ;/* Per-cpu ISA extensions. */ struct riscv_isainfo hart_isa[NR_CPUS ];@@ -624,6 +624,8 @@ const struct riscv_isa_ext_data riscv_isa_ext[] = {__RISCV_ISA_EXT_DATA (svpbmt,RISCV_ISA_EXT_SVPBMT ),__RISCV_ISA_EXT_DATA (svrsw60t59b,RISCV_ISA_EXT_SVRSW60T59B ),__RISCV_ISA_EXT_DATA (svvptc,RISCV_ISA_EXT_SVVPTC ), +__RISCV_ISA_EXT_DATA (sv48,RISCV_ISA_EXT_SV48 ), + __RISCV_ISA_EXT_DATA(sv57, RISCV_ISA_EXT_SV57), };const size_t riscv_isa_ext_count =ARRAY_SIZE (riscv_isa_ext);
arch/riscv/kernel/cpufeature.c static void __init riscv_parse_isa_string(const char *isa, unsigned long *bitmap · line 868
Quoted 6 lines
arch/riscv/kernel/cpufeature.c +2
-1@@ -862,7 +864,8 @@ static void __init riscv_parse_isa_string(const char *isa, unsigned long *bitmap } } -static void __init riscv_fill_hwcap_from_isa_string (unsigned long *isa2hwcap) +static void __init riscv_fill_hwcap_from_isa_string (unsigned long *isa2hwcap, +unsigned long *riscv_isa_early)
All of the "early" business in this file seems overly complicated. Can we just do something like this?
arch/riscv/kernel/cpufeature.c +10 -0
@@ -922,6 +922,11 @@ static void __init riscv_fill_hwcap_from_isa_string(unsigned long *isa2hwcap)set_bit (RISCV_ISA_EXT_ZIHPM , source_isa); } +if (_pgtable_l4_enabled) +set_bit (RISCV_ISA_EXT_SV48 , source_isa); +if (_pgtable_l5_enabled) +set_bit (RISCV_ISA_EXT_SV57 , source_isa); +/* * "V" in ISA strings is ambiguous in practice: it should mean * just the standard V-1.0 but vendors aren't well behaved.
@@ -1082,6 +1087,11 @@ static int __init riscv_fill_hwcap_from_ext_list(unsigned long *isa2hwcap)riscv_isa_set_ext (ext, source_isa); } +if (_pgtable_l4_enabled) +set_bit (RISCV_ISA_EXT_SV48 , source_isa); +if (_pgtable_l5_enabled) +set_bit (RISCV_ISA_EXT_SV57 , source_isa); +riscv_resolve_isa (source_isa, isainfo->isa, &this_hwcap, isa2hwcap);riscv_fill_cpu_vendor_ext (cpu_node, cpu);
The pagetable levels are the only thing we do this kind of early and probed detection for, adding a framework for them and only them that mostly just complicates the normal case seems rather excessive. Doing something simple like this is also quite explicit as to what's being done and what the basis for it is.
Cheers,
Conor.
Quoted 160 lines
arch/riscv/kernel/cpufeature.c +42
-11{struct device_node *node;const char *isa;@@ -933,6 +936,7 @@ static void __init riscv_fill_hwcap_from_isa_string(unsigned long *isa2hwcap)if (acpi_disabled && boot_vendorid ==THEAD_VENDOR_ID && boot_archid ==0x0 )clear_bit (RISCV_ISA_EXT_V , source_isa); +bitmap_or (source_isa, source_isa, riscv_isa_early,RISCV_ISA_EXT_MAX );riscv_resolve_isa (source_isa, isainfo->isa, &this_hwcap, isa2hwcap);/* @@ -1050,7 +1054,8 @@ static int has_thead_homogeneous_vlenb(void)return 0 ; } -static int __init riscv_fill_hwcap_from_ext_list (unsigned long *isa2hwcap) +static int __init riscv_fill_hwcap_from_ext_list (unsigned long *isa2hwcap, +unsigned long *riscv_isa_early) {unsigned int cpu;bool mitigated;@@ -1082,6 +1087,7 @@ static int __init riscv_fill_hwcap_from_ext_list(unsigned long *isa2hwcap)riscv_isa_set_ext (ext, source_isa); } +bitmap_or (source_isa, source_isa, riscv_isa_early,RISCV_ISA_EXT_MAX );riscv_resolve_isa (source_isa, isainfo->isa, &this_hwcap, isa2hwcap);riscv_fill_cpu_vendor_ext (cpu_node, cpu);@@ -1137,6 +1143,7 @@ void __init riscv_fill_hwcap(void) {char print_str[NUM_ALPHA_EXTS +1 ];unsigned long isa2hwcap[RISCV_ISA_EXT_BASE ] = {0 }; +DECLARE_BITMAP (riscv_isa_early,RISCV_ISA_EXT_MAX );int i, j; isa2hwcap[RISCV_ISA_EXT_I ] =COMPAT_HWCAP_ISA_I ;@@ -1147,14 +1154,17 @@ void __init riscv_fill_hwcap(void) isa2hwcap[RISCV_ISA_EXT_C ] =COMPAT_HWCAP_ISA_C ; isa2hwcap[RISCV_ISA_EXT_V ] =COMPAT_HWCAP_ISA_V ; +bitmap_copy (riscv_isa_early, riscv_isa,RISCV_ISA_EXT_MAX ); +bitmap_zero (riscv_isa,RISCV_ISA_EXT_MAX ); +if (!acpi_disabled) { -riscv_fill_hwcap_from_isa_string (isa2hwcap); +riscv_fill_hwcap_from_isa_string (isa2hwcap, riscv_isa_early); }else { -int ret =riscv_fill_hwcap_from_ext_list (isa2hwcap); +int ret =riscv_fill_hwcap_from_ext_list (isa2hwcap, riscv_isa_early);if (ret && riscv_isa_fallback) {pr_info ("Falling back to deprecated \"riscv,isa\"\n" ); -riscv_fill_hwcap_from_isa_string (isa2hwcap); +riscv_fill_hwcap_from_isa_string (isa2hwcap, riscv_isa_early); } }@@ -1250,6 +1260,17 @@ static bool riscv_cpufeature_patch_check(u16 id, u16 value)return false; } +static bool __init_or_module riscv_is_isa_ext_early_id(u16 id) +{ +switch (id) { +case RISCV_ISA_EXT_SV48 : +case RISCV_ISA_EXT_SV57 : +return true; + } + +return false; +} + void __init_or_module riscv_cpufeature_patch_func(struct alt_entry *begin,struct alt_entry *end,unsigned int stage)@@ -1257,9 +1278,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 +1293,8 @@ void __init_or_module riscv_cpufeature_patch_func(struct alt_entry *begin,* vendor extension. */ if (id <RISCV_ISA_EXT_MAX ) { +if (early && !riscv_is_isa_ext_early_id (id)) +continue ;/* * This patch should be treated as errata so skip * processing here. @@ -1288,6 +1309,8 @@ void __init_or_module riscv_cpufeature_patch_func(struct alt_entry *begin,if (!riscv_cpufeature_patch_check (id, value))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 +1322,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_extension_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_ISA_EXT_SV48 , riscv_isa); +if (pgtable_l5_enabled ()) + set_bit(RISCV_ISA_EXT_SV57, riscv_isa); }#endif Signature · 1 line
2.53.0
Re: [PATCH v2 3/5] riscv: support early isa ext and use it to optimize pgtable_l4|l5_enabled
Quoted 260 lines · Conor Dooley
Quoted 222 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.
So the question is whether we can add RISCV_ISA_EXT_SV48/SV5 and use riscv_has_extension_*() or not. Per [1] and [2], SV48 and SV57 are ISA exensions too. From another side, riscv_has_extension_[un]likely() and other related functions report whether the extension is supported and enabled on the platform. So SV48 and SV57 can be supported with current isa extension alternative mechanism.
However, to use it to optimize pgtable_l4|l5_enabled, we have support the "early" characteristic, I.E besides
risc_isabitmap setting, we need to support appling alternative early before MMU on.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:
These memory load and test branch instructions are replaced with only two j or nop instructions.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.h | 2 + arch/riscv/include/asm/hwcap.h | 2 + arch/riscv/include/asm/pgtable-64.h | 12 ++++++ arch/riscv/kernel/alternative.c | 24 +++++++---- arch/riscv/kernel/cpufeature.c | 60 ++++++++++++++++++++++------ arch/riscv/mm/init.c | 9 +++++ 8 files changed, 91 insertions(+), 21 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.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_isa, RISCV_ISA_EXT_MAX); +extern u32 thead_vlenb_of;void __init riscv_user_isa_enable (void );arch/riscv/include/asm/hwcap.h +2
-0@@ -122,6 +122,8 @@#define RISCV_ISA_EXT_ZICCAMOA 113#define RISCV_ISA_EXT_ZICCIF 114#define RISCV_ISA_EXT_ZA64RS 115 +#define RISCV_ISA_EXT_SV48 116 +#define RISCV_ISA_EXT_SV57 117#define RISCV_ISA_EXT_XLINUXENVCFG 127arch/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_extension_likely (RISCV_ISA_EXT_SV48 ); +} + +static __always_inline bool pgtable_l5_enabled(void) +{ +return riscv_has_extension_likely (RISCV_ISA_EXT_SV57 ); +} +#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 +3
-1@@ -39,7 +39,7 @@ static bool any_cpu_has_zicbom;unsigned long elf_hwcap__read_mostly ;/* Host ISA bitmap */ -static DECLARE_BITMAP(riscv_isa, RISCV_ISA_EXT_MAX) __read_mostly; +DECLARE_BITMAP (riscv_isa,RISCV_ISA_EXT_MAX )__read_mostly ;/* Per-cpu ISA extensions. */ struct riscv_isainfo hart_isa[NR_CPUS ];@@ -624,6 +624,8 @@ const struct riscv_isa_ext_data riscv_isa_ext[] = {__RISCV_ISA_EXT_DATA (svpbmt,RISCV_ISA_EXT_SVPBMT ),__RISCV_ISA_EXT_DATA (svrsw60t59b,RISCV_ISA_EXT_SVRSW60T59B ),__RISCV_ISA_EXT_DATA (svvptc,RISCV_ISA_EXT_SVVPTC ), +__RISCV_ISA_EXT_DATA (sv48,RISCV_ISA_EXT_SV48 ), + __RISCV_ISA_EXT_DATA(sv57, RISCV_ISA_EXT_SV57), };const size_t riscv_isa_ext_count =ARRAY_SIZE (riscv_isa_ext);arch/riscv/kernel/cpufeature.c static void __init riscv_parse_isa_string(const char *isa, unsigned long *bitmap · line 868
Quoted 6 lines
arch/riscv/kernel/cpufeature.c +2
-1@@ -862,7 +864,8 @@ static void __init riscv_parse_isa_string(const char *isa, unsigned long *bitmap } } -static void __init riscv_fill_hwcap_from_isa_string (unsigned long *isa2hwcap) +static void __init riscv_fill_hwcap_from_isa_string (unsigned long *isa2hwcap, +unsigned long *riscv_isa_early)All of the "early" business in this file seems overly complicated. Can we just do something like this?
arch/riscv/kernel/cpufeature.c +10
-0@@ -922,6 +922,11 @@ static void __init riscv_fill_hwcap_from_isa_string(unsigned long *isa2hwcap)set_bit (RISCV_ISA_EXT_ZIHPM , source_isa); } +if (_pgtable_l4_enabled) +set_bit (RISCV_ISA_EXT_SV48 , source_isa); +if (_pgtable_l5_enabled) +set_bit (RISCV_ISA_EXT_SV57 , source_isa); +/* * "V" in ISA strings is ambiguous in practice: it should mean * just the standard V-1.0 but vendors aren't well behaved. @@ -1082,6 +1087,11 @@ static int __init riscv_fill_hwcap_from_ext_list(unsigned long *isa2hwcap)riscv_isa_set_ext (ext, source_isa); } +if (_pgtable_l4_enabled) +set_bit (RISCV_ISA_EXT_SV48 , source_isa); +if (_pgtable_l5_enabled) +set_bit (RISCV_ISA_EXT_SV57 , source_isa); +riscv_resolve_isa (source_isa, isainfo->isa, &this_hwcap, isa2hwcap);riscv_fill_cpu_vendor_ext (cpu_node, cpu);
Quoted 4 lines
The pagetable levels are the only thing we do this kind of early and probed detection for, adding a framework for them and only them that mostly just complicates the normal case seems rather excessive. Doing something simple like this is also quite explicit as to what's being
This assumes SV48 and SV57 are the only "early" users, but
other ISA ext may also need "early", who knows. So if future some extensions need the "early", the code is ready, the author doesn't need to care about the isa filling at all.
So I prefer my patch as is. What's your opinion?
Quoted 165 lines
done and what the basis for it is.
Cheers,
Conor.Quoted 160 lines
arch/riscv/kernel/cpufeature.c +42
-11{struct device_node *node;const char *isa;@@ -933,6 +936,7 @@ static void __init riscv_fill_hwcap_from_isa_string(unsigned long *isa2hwcap)if (acpi_disabled && boot_vendorid ==THEAD_VENDOR_ID && boot_archid ==0x0 )clear_bit (RISCV_ISA_EXT_V , source_isa); +bitmap_or (source_isa, source_isa, riscv_isa_early,RISCV_ISA_EXT_MAX );riscv_resolve_isa (source_isa, isainfo->isa, &this_hwcap, isa2hwcap);/* @@ -1050,7 +1054,8 @@ static int has_thead_homogeneous_vlenb(void)return 0 ; } -static int __init riscv_fill_hwcap_from_ext_list (unsigned long *isa2hwcap) +static int __init riscv_fill_hwcap_from_ext_list (unsigned long *isa2hwcap, +unsigned long *riscv_isa_early) {unsigned int cpu;bool mitigated;@@ -1082,6 +1087,7 @@ static int __init riscv_fill_hwcap_from_ext_list(unsigned long *isa2hwcap)riscv_isa_set_ext (ext, source_isa); } +bitmap_or (source_isa, source_isa, riscv_isa_early,RISCV_ISA_EXT_MAX );riscv_resolve_isa (source_isa, isainfo->isa, &this_hwcap, isa2hwcap);riscv_fill_cpu_vendor_ext (cpu_node, cpu);@@ -1137,6 +1143,7 @@ void __init riscv_fill_hwcap(void) {char print_str[NUM_ALPHA_EXTS +1 ];unsigned long isa2hwcap[RISCV_ISA_EXT_BASE ] = {0 }; +DECLARE_BITMAP (riscv_isa_early,RISCV_ISA_EXT_MAX );int i, j; isa2hwcap[RISCV_ISA_EXT_I ] =COMPAT_HWCAP_ISA_I ;@@ -1147,14 +1154,17 @@ void __init riscv_fill_hwcap(void) isa2hwcap[RISCV_ISA_EXT_C ] =COMPAT_HWCAP_ISA_C ; isa2hwcap[RISCV_ISA_EXT_V ] =COMPAT_HWCAP_ISA_V ; +bitmap_copy (riscv_isa_early, riscv_isa,RISCV_ISA_EXT_MAX ); +bitmap_zero (riscv_isa,RISCV_ISA_EXT_MAX ); +if (!acpi_disabled) { -riscv_fill_hwcap_from_isa_string (isa2hwcap); +riscv_fill_hwcap_from_isa_string (isa2hwcap, riscv_isa_early); }else { -int ret =riscv_fill_hwcap_from_ext_list (isa2hwcap); +int ret =riscv_fill_hwcap_from_ext_list (isa2hwcap, riscv_isa_early);if (ret && riscv_isa_fallback) {pr_info ("Falling back to deprecated \"riscv,isa\"\n" ); -riscv_fill_hwcap_from_isa_string (isa2hwcap); +riscv_fill_hwcap_from_isa_string (isa2hwcap, riscv_isa_early); } }@@ -1250,6 +1260,17 @@ static bool riscv_cpufeature_patch_check(u16 id, u16 value)return false; } +static bool __init_or_module riscv_is_isa_ext_early_id(u16 id) +{ +switch (id) { +case RISCV_ISA_EXT_SV48 : +case RISCV_ISA_EXT_SV57 : +return true; + } + +return false; +} + void __init_or_module riscv_cpufeature_patch_func(struct alt_entry *begin,struct alt_entry *end,unsigned int stage)@@ -1257,9 +1278,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 +1293,8 @@ void __init_or_module riscv_cpufeature_patch_func(struct alt_entry *begin,* vendor extension. */ if (id <RISCV_ISA_EXT_MAX ) { +if (early && !riscv_is_isa_ext_early_id (id)) +continue ;/* * This patch should be treated as errata so skip * processing here. @@ -1288,6 +1309,8 @@ void __init_or_module riscv_cpufeature_patch_func(struct alt_entry *begin,if (!riscv_cpufeature_patch_check (id, value))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 +1322,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_extension_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_ISA_EXT_SV48 , riscv_isa); +if (pgtable_l5_enabled ()) + set_bit(RISCV_ISA_EXT_SV57, riscv_isa); }#endif Signature · 1 line
2.53.0
Re: [PATCH v2 4/5] riscv: RISCV_ISA_SV48 and RISCV_ISA_SV57 under NONPORTABLE
In reply to Conor DooleyQuoted 179 lines · Conor Dooley
Quoted 86 lines · Jisheng Zhang
In last commit, we have optimized the pgtable_l4|l5_enabled with isa extension alternative mechanism, thus a typical
access_ok(addr, 1); is compiled as:ffffffff8001dd30: 1141 addi sp,sp,-16 ffffffff8001dd32: e022 sd s0,0(sp) ffffffff8001dd34: e406 sd ra,8(sp) ffffffff8001dd36: 0800 addi s0,sp,16 ffffffff8001dd38: 00000013 nop ffffffff8001dd3c: 02a0006f j ffffffff8001dd66 <foo+0x3e> ffffffff8001dd40: 60a2 ld ra,8(sp) ffffffff8001dd42: 6402 ld s0,0(sp) ffffffff8001dd44: 57fd li a5,-1 ffffffff8001dd46: 83a1 srli a5,a5,0x8
ffffffff8001dd48:00a7b533sltu a0,a5,a0ffffffff8001dd4c: 00154513 xori a0,a0,1ffffffff8001dd50: 0141 addi sp,sp,16ffffffff8001dd52: 8082 ret
...ffffffff8001dd64: bfe1 jffffffff8001dd3c<foo+0x14>ffffffff8001dd66:0180006fjffffffff8001dd7e<foo+0x56>ffffffff8001dd6a: 60a2 ld ra,8(sp) ffffffff8001dd6c: 6402 ld s0,0(sp) ffffffff8001dd6e: 57fd li a5,-1 ffffffff8001dd70: 83c5 srli a5,a5,0x11 ffffffff8001dd72: 00a7b533 sltu a0,a5,a0 ffffffff8001dd76: 00154513 xori a0,a0,1 ffffffff8001dd7a: 0141 addi sp,sp,16 ffffffff8001dd7c: 8082 ret ffffffff8001dd7e: 60a2 ld ra,8(sp) ffffffff8001dd80: 6402 ld s0,0(sp) ffffffff8001dd82: 57fd li a5,-1 ffffffff8001dd84: 83e9 srli a5,a5,0x1a
ffffffff8001dd86:00a7b533sltu a0,a5,a0ffffffff8001dd8a: 00154513 xori a0,a0,1ffffffff8001dd8e: 0141 addi sp,sp,16ffffffff8001dd90: 8082 retAs can be seen, different branches for SV39/SV48/SV57 are still there, since
access_ok()sits at hot code path, why not reduce the code size to optimize to make the instruction cache happy?Introduce
RISCV_ISA_SV48andRISCV_ISA_SV57under NONPORTABLE, so that the embedded platforms can choose the best option themselves, while still keep the feature of unified one kernel Image for all SV39, SV48 and SV57.Before the patch, vmlinux built with pure RISCV64 defconfig
text data bss dec hex filename 13439437 6790446 483221 20713104 13c0e90 /tmp/old/vmlinuxAfter the patch, vmlinux built with pure RISCV64 defconfig:
text data bss dec hex filename 13314445 6787934 466837 20569216 139dc80 /tmp/new/vmlinux.text section is reduced by 122KB!
And after the patch, typical
access_ok(addr, 1)is compiled as:ffffffff8001809a: 1141 addi sp,sp,-16 ffffffff8001809c: e022 sd s0,0(sp) ffffffff8001809e: e406 sd ra,8(sp) ffffffff800180a0: 0800 addi s0,sp,16 ffffffff800180a2: 00000013 nop ffffffff800180a6: 60a2 ld ra,8(sp) ffffffff800180a8: 6402 ld s0,0(sp) ffffffff800180aa: 4785 li a5,1 ffffffff800180ac: 179a slli a5,a5,0x26
ffffffff800180ae:00f53533sltu a0,a0,a5ffffffff800180b2: 0141 addi sp,sp,16ffffffff800180b4: 8082 retonly 12 instruction!! For refernece, before the patch, the access(addr,
1) needs 32 instructions.Signed-off-by Jisheng Zhang <[email protected]>--- arch/riscv/Kconfig | 24 ++++++++++++++++++++++++ arch/riscv/include/asm/pgtable-64.h | 12 ++++++++++++ arch/riscv/mm/init.c | 10 ++++++++++ 3 files changed, 46 insertions(+)arch/riscv/Kconfig +0
-0arch/riscv/Kconfig config RISCV_ISA_SSQOSID · line 608
Quoted 7 lines
arch/riscv/Kconfig +4
-0@@ -602,6 +602,30 @@ config RISCV_ISA_SSQOSID cache partitioning scheme and use the MCID to track how much cache a process, or a group of processes, is using. +config RISCV_ISA_SV48 + bool "Page-Based 48-bit Virtual-Memory System" + depends on 64BIT && NONPORTABLE + default yI don't understand how you've got this set up here.
If this is genuinely non-portable, then you should not set the default to enabled. However, I'm not really sure that this is actually non-portable at all....Quoted 51 lines
arch/riscv/Kconfig +20
-0+ help + Add support for the Page-Based 48-bit Virtual-Memory System. + + Enable this option for systems for which a 39-bit virtual address + space is insufficient. But if 39-bit is enough, disabling this + option will result in smaller kernel size and slighly better + performance. + +config RISCV_ISA_SV57 + bool "Page-Based 57-bit Virtual-Memory System" + depends on 64BIT && NONPORTABLE + default y + help + Add support for the Page-Based 57-bit Virtual-Memory System. + + Enable this option for systems for which a 48-bit virtual address + space is insufficient. But if 48-bit is enough, disabling this + option will result in smaller kernel size and slighly better + performance. + config RISCV_ISA_SVPBMT bool "Svpbmt extension support for supervisor mode page-based memory types" depends on 64BIT && MMUarch/riscv/include/asm/pgtable-64.h +9
-0@@ -16,21 +16,33 @@ extern bool _pgtable_l5_enabled;#ifdef USE_EARLY_PGTABLE_LEVELS static __always_inline bool pgtable_l5_enabled(void) { +if (IS_ENABLED (CONFIG_NONPORTABLE ) && !IS_ENABLED (CONFIG_RISCV_ISA_SV57 )) +return false; +return _pgtable_l5_enabled; } static __always_inline bool pgtable_l4_enabled(void) { +if (IS_ENABLED (CONFIG_NONPORTABLE ) && !IS_ENABLED (CONFIG_RISCV_ISA_SV48 )) +return false; +return _pgtable_l4_enabled; }#else static __always_inline bool pgtable_l4_enabled(void) { +if (IS_ENABLED (CONFIG_NONPORTABLE ) && !IS_ENABLED (CONFIG_RISCV_ISA_SV48 )) +return false; +return riscv_has_extension_likely (RISCV_ISA_EXT_SV48 );arch/riscv/include/asm/pgtable-64.h extern bool _pgtable_l5_enabled; · line 48
Quoted 10 lines
arch/riscv/include/asm/pgtable-64.h +3
-0} static __always_inline bool pgtable_l5_enabled(void) { +if (IS_ENABLED (CONFIG_NONPORTABLE ) && !IS_ENABLED (CONFIG_RISCV_ISA_SV57 )) +return false; +return riscv_has_extension_likely (RISCV_ISA_EXT_SV57 ); }#endif ...because the code here won't enable sv57 etc if not supported...
arch/riscv/mm/init.c static __init void set_satp_mode(uintptr_t dtb_pa) · line 850
Quoted 12 lines
arch/riscv/mm/init.c +4
-0@@ -844,6 +844,16 @@ static __init void set_satp_mode(uintptr_t dtb_pa) kernel_map.page_offset =PAGE_OFFSET_L5 ; +/* Fall back to Sv48 if Sv57 is unsupported on non-portable systems */ +if (IS_ENABLED (CONFIG_NONPORTABLE ) && !IS_ENABLED (CONFIG_RISCV_ISA_SV57 ) && + satp_mode_limit ==0 ) + satp_mode_limit =SATP_MODE_48 ;...and neither does the code here.
Quoted 7 lines
Quoted 4 lines
Introduce
RISCV_ISA_SV48andRISCV_ISA_SV57under NONPORTABLE, so that the embedded platforms can choose the best option themselves, while still keep the feature of unified one kernel Image for all SV39, SV48 and SV57.NONPORTABLE is what you're meant to depend on when the kernel that gets built cannot run on platforms without the feature.
The reason I put these two Kconfig options under NONPORTABLE is: all distributions won't enable NONPORTABLE for their defconfig, and I guess the distributions want to kept unified one kernel Image for all SV39, SV48 and SV57.
I have no preference whether move them out of NONPORTABLE or not.
Quoted 13 lines
Quoted 13 lines
arch/riscv/mm/init.c +6
-0+ +/* Fall back to Sv39 if Sv48 is unsupported on non-portable systems */ +if (IS_ENABLED (CONFIG_NONPORTABLE ) && !IS_ENABLED (CONFIG_RISCV_ISA_SV48 ) && + satp_mode_limit ==SATP_MODE_48 ) + satp_mode_limit =SATP_MODE_39 ; +if (satp_mode_limit ==SATP_MODE_48 ) {disable_pgtable_l5 (); }else if (satp_mode_limit ==SATP_MODE_39 ) {Signature · 1 line
2.53.0
Re: [PATCH v2 3/5] riscv: support early isa ext and use it to optimize pgtable_l4|l5_enabled
In reply to Jisheng ZhangQuoted 271 lines · Jisheng Zhang
Quoted 260 lines · Conor Dooley
Quoted 222 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.
So the question is whether we can add RISCV_ISA_EXT_SV48/SV5 and use riscv_has_extension_*() or not. Per [1] and [2], SV48 and SV57 are ISA exensions too. From another side, riscv_has_extension_[un]likely() and other related functions report whether the extension is supported and enabled on the platform. So SV48 and SV57 can be supported with current isa extension alternative mechanism.
However, to use it to optimize pgtable_l4|l5_enabled, we have support the "early" characteristic, I.E besides
risc_isabitmap setting, we need to support appling alternative early before MMU on.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:
These memory load and test branch instructions are replaced with only two j or nop instructions.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.h | 2 + arch/riscv/include/asm/hwcap.h | 2 + arch/riscv/include/asm/pgtable-64.h | 12 ++++++ arch/riscv/kernel/alternative.c | 24 +++++++---- arch/riscv/kernel/cpufeature.c | 60 ++++++++++++++++++++++------ arch/riscv/mm/init.c | 9 +++++ 8 files changed, 91 insertions(+), 21 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.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_isa, RISCV_ISA_EXT_MAX); +extern u32 thead_vlenb_of;void __init riscv_user_isa_enable (void );arch/riscv/include/asm/hwcap.h +2
-0@@ -122,6 +122,8 @@#define RISCV_ISA_EXT_ZICCAMOA 113#define RISCV_ISA_EXT_ZICCIF 114#define RISCV_ISA_EXT_ZA64RS 115 +#define RISCV_ISA_EXT_SV48 116 +#define RISCV_ISA_EXT_SV57 117#define RISCV_ISA_EXT_XLINUXENVCFG 127arch/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_extension_likely (RISCV_ISA_EXT_SV48 ); +} + +static __always_inline bool pgtable_l5_enabled(void) +{ +return riscv_has_extension_likely (RISCV_ISA_EXT_SV57 ); +} +#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 +3
-1@@ -39,7 +39,7 @@ static bool any_cpu_has_zicbom;unsigned long elf_hwcap__read_mostly ;/* Host ISA bitmap */ -static DECLARE_BITMAP(riscv_isa, RISCV_ISA_EXT_MAX) __read_mostly; +DECLARE_BITMAP (riscv_isa,RISCV_ISA_EXT_MAX )__read_mostly ;/* Per-cpu ISA extensions. */ struct riscv_isainfo hart_isa[NR_CPUS ];@@ -624,6 +624,8 @@ const struct riscv_isa_ext_data riscv_isa_ext[] = {__RISCV_ISA_EXT_DATA (svpbmt,RISCV_ISA_EXT_SVPBMT ),__RISCV_ISA_EXT_DATA (svrsw60t59b,RISCV_ISA_EXT_SVRSW60T59B ),__RISCV_ISA_EXT_DATA (svvptc,RISCV_ISA_EXT_SVVPTC ), +__RISCV_ISA_EXT_DATA (sv48,RISCV_ISA_EXT_SV48 ), + __RISCV_ISA_EXT_DATA(sv57, RISCV_ISA_EXT_SV57), };const size_t riscv_isa_ext_count =ARRAY_SIZE (riscv_isa_ext);arch/riscv/kernel/cpufeature.c static void __init riscv_parse_isa_string(const char *isa, unsigned long *bitmap · line 868
Quoted 6 lines
arch/riscv/kernel/cpufeature.c +2
-1@@ -862,7 +864,8 @@ static void __init riscv_parse_isa_string(const char *isa, unsigned long *bitmap } } -static void __init riscv_fill_hwcap_from_isa_string (unsigned long *isa2hwcap) +static void __init riscv_fill_hwcap_from_isa_string (unsigned long *isa2hwcap, +unsigned long *riscv_isa_early)All of the "early" business in this file seems overly complicated. Can we just do something like this?
arch/riscv/kernel/cpufeature.c +10
-0@@ -922,6 +922,11 @@ static void __init riscv_fill_hwcap_from_isa_string(unsigned long *isa2hwcap)set_bit (RISCV_ISA_EXT_ZIHPM , source_isa); } +if (_pgtable_l4_enabled) +set_bit (RISCV_ISA_EXT_SV48 , source_isa); +if (_pgtable_l5_enabled) +set_bit (RISCV_ISA_EXT_SV57 , source_isa); +/* * "V" in ISA strings is ambiguous in practice: it should mean * just the standard V-1.0 but vendors aren't well behaved. @@ -1082,6 +1087,11 @@ static int __init riscv_fill_hwcap_from_ext_list(unsigned long *isa2hwcap)riscv_isa_set_ext (ext, source_isa); } +if (_pgtable_l4_enabled) +set_bit (RISCV_ISA_EXT_SV48 , source_isa); +if (_pgtable_l5_enabled) +set_bit (RISCV_ISA_EXT_SV57 , source_isa); +riscv_resolve_isa (source_isa, isainfo->isa, &this_hwcap, isa2hwcap);riscv_fill_cpu_vendor_ext (cpu_node, cpu);Quoted 4 lines
The pagetable levels are the only thing we do this kind of early and probed detection for, adding a framework for them and only them that mostly just complicates the normal case seems rather excessive. Doing something simple like this is also quite explicit as to what's being
This assumes SV48 and SV57 are the only "early" users, but
other ISA ext may also need "early", who knows. So if future some extensions need the "early", the code is ready, the author doesn't need to care about the isa filling at all.
Quoted 1 line
So I prefer my patch as is. What's your opinion?
We're about decade into the port being merged and this is the only thing behaving in this way. I don't think the code in this patch is worth it on the off chance that something else comes along. If it does, we can always fish this implementation back up and use it.
I'd also like to differentiate this code from "needing early", because this is about populating the information early in the extension bitmap, rather than about actually needing the information. There's no advantage gained, as far as I can tell, by setting this early and it only makes the code more complicated.
Cheers,
Conor.
Quoted 168 lines
Quoted 166 lines
done and what the basis for it is.
Cheers,
Conor.Quoted 160 lines
arch/riscv/kernel/cpufeature.c +42
-11{struct device_node *node;const char *isa;@@ -933,6 +936,7 @@ static void __init riscv_fill_hwcap_from_isa_string(unsigned long *isa2hwcap)if (acpi_disabled && boot_vendorid ==THEAD_VENDOR_ID && boot_archid ==0x0 )clear_bit (RISCV_ISA_EXT_V , source_isa); +bitmap_or (source_isa, source_isa, riscv_isa_early,RISCV_ISA_EXT_MAX );riscv_resolve_isa (source_isa, isainfo->isa, &this_hwcap, isa2hwcap);/* @@ -1050,7 +1054,8 @@ static int has_thead_homogeneous_vlenb(void)return 0 ; } -static int __init riscv_fill_hwcap_from_ext_list (unsigned long *isa2hwcap) +static int __init riscv_fill_hwcap_from_ext_list (unsigned long *isa2hwcap, +unsigned long *riscv_isa_early) {unsigned int cpu;bool mitigated;@@ -1082,6 +1087,7 @@ static int __init riscv_fill_hwcap_from_ext_list(unsigned long *isa2hwcap)riscv_isa_set_ext (ext, source_isa); } +bitmap_or (source_isa, source_isa, riscv_isa_early,RISCV_ISA_EXT_MAX );riscv_resolve_isa (source_isa, isainfo->isa, &this_hwcap, isa2hwcap);riscv_fill_cpu_vendor_ext (cpu_node, cpu);@@ -1137,6 +1143,7 @@ void __init riscv_fill_hwcap(void) {char print_str[NUM_ALPHA_EXTS +1 ];unsigned long isa2hwcap[RISCV_ISA_EXT_BASE ] = {0 }; +DECLARE_BITMAP (riscv_isa_early,RISCV_ISA_EXT_MAX );int i, j; isa2hwcap[RISCV_ISA_EXT_I ] =COMPAT_HWCAP_ISA_I ;@@ -1147,14 +1154,17 @@ void __init riscv_fill_hwcap(void) isa2hwcap[RISCV_ISA_EXT_C ] =COMPAT_HWCAP_ISA_C ; isa2hwcap[RISCV_ISA_EXT_V ] =COMPAT_HWCAP_ISA_V ; +bitmap_copy (riscv_isa_early, riscv_isa,RISCV_ISA_EXT_MAX ); +bitmap_zero (riscv_isa,RISCV_ISA_EXT_MAX ); +if (!acpi_disabled) { -riscv_fill_hwcap_from_isa_string (isa2hwcap); +riscv_fill_hwcap_from_isa_string (isa2hwcap, riscv_isa_early); }else { -int ret =riscv_fill_hwcap_from_ext_list (isa2hwcap); +int ret =riscv_fill_hwcap_from_ext_list (isa2hwcap, riscv_isa_early);if (ret && riscv_isa_fallback) {pr_info ("Falling back to deprecated \"riscv,isa\"\n" ); -riscv_fill_hwcap_from_isa_string (isa2hwcap); +riscv_fill_hwcap_from_isa_string (isa2hwcap, riscv_isa_early); } }@@ -1250,6 +1260,17 @@ static bool riscv_cpufeature_patch_check(u16 id, u16 value)return false; } +static bool __init_or_module riscv_is_isa_ext_early_id(u16 id) +{ +switch (id) { +case RISCV_ISA_EXT_SV48 : +case RISCV_ISA_EXT_SV57 : +return true; + } + +return false; +} + void __init_or_module riscv_cpufeature_patch_func(struct alt_entry *begin,struct alt_entry *end,unsigned int stage)@@ -1257,9 +1278,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 +1293,8 @@ void __init_or_module riscv_cpufeature_patch_func(struct alt_entry *begin,* vendor extension. */ if (id <RISCV_ISA_EXT_MAX ) { +if (early && !riscv_is_isa_ext_early_id (id)) +continue ;/* * This patch should be treated as errata so skip * processing here. @@ -1288,6 +1309,8 @@ void __init_or_module riscv_cpufeature_patch_func(struct alt_entry *begin,if (!riscv_cpufeature_patch_check (id, value))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 +1322,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_extension_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_ISA_EXT_SV48 , riscv_isa); +if (pgtable_l5_enabled ()) + set_bit(RISCV_ISA_EXT_SV57, riscv_isa); }#endif Signature · 1 line
2.53.0
Re: [PATCH v2 4/5] riscv: RISCV_ISA_SV48 and RISCV_ISA_SV57 under NONPORTABLE
In reply to Jisheng ZhangQuoted 188 lines · Jisheng Zhang
Quoted 179 lines · Conor Dooley
Quoted 86 lines · Jisheng Zhang
In last commit, we have optimized the pgtable_l4|l5_enabled with isa extension alternative mechanism, thus a typical
access_ok(addr, 1); is compiled as:ffffffff8001dd30: 1141 addi sp,sp,-16 ffffffff8001dd32: e022 sd s0,0(sp) ffffffff8001dd34: e406 sd ra,8(sp) ffffffff8001dd36: 0800 addi s0,sp,16 ffffffff8001dd38: 00000013 nop ffffffff8001dd3c: 02a0006f j ffffffff8001dd66 <foo+0x3e> ffffffff8001dd40: 60a2 ld ra,8(sp) ffffffff8001dd42: 6402 ld s0,0(sp) ffffffff8001dd44: 57fd li a5,-1 ffffffff8001dd46: 83a1 srli a5,a5,0x8
ffffffff8001dd48:00a7b533sltu a0,a5,a0ffffffff8001dd4c: 00154513 xori a0,a0,1ffffffff8001dd50: 0141 addi sp,sp,16ffffffff8001dd52: 8082 ret
...ffffffff8001dd64: bfe1 jffffffff8001dd3c<foo+0x14>ffffffff8001dd66:0180006fjffffffff8001dd7e<foo+0x56>ffffffff8001dd6a: 60a2 ld ra,8(sp) ffffffff8001dd6c: 6402 ld s0,0(sp) ffffffff8001dd6e: 57fd li a5,-1 ffffffff8001dd70: 83c5 srli a5,a5,0x11 ffffffff8001dd72: 00a7b533 sltu a0,a5,a0 ffffffff8001dd76: 00154513 xori a0,a0,1 ffffffff8001dd7a: 0141 addi sp,sp,16 ffffffff8001dd7c: 8082 ret ffffffff8001dd7e: 60a2 ld ra,8(sp) ffffffff8001dd80: 6402 ld s0,0(sp) ffffffff8001dd82: 57fd li a5,-1 ffffffff8001dd84: 83e9 srli a5,a5,0x1a
ffffffff8001dd86:00a7b533sltu a0,a5,a0ffffffff8001dd8a: 00154513 xori a0,a0,1ffffffff8001dd8e: 0141 addi sp,sp,16ffffffff8001dd90: 8082 retAs can be seen, different branches for SV39/SV48/SV57 are still there, since
access_ok()sits at hot code path, why not reduce the code size to optimize to make the instruction cache happy?Introduce
RISCV_ISA_SV48andRISCV_ISA_SV57under NONPORTABLE, so that the embedded platforms can choose the best option themselves, while still keep the feature of unified one kernel Image for all SV39, SV48 and SV57.Before the patch, vmlinux built with pure RISCV64 defconfig
text data bss dec hex filename 13439437 6790446 483221 20713104 13c0e90 /tmp/old/vmlinuxAfter the patch, vmlinux built with pure RISCV64 defconfig:
text data bss dec hex filename 13314445 6787934 466837 20569216 139dc80 /tmp/new/vmlinux.text section is reduced by 122KB!
And after the patch, typical
access_ok(addr, 1)is compiled as:ffffffff8001809a: 1141 addi sp,sp,-16 ffffffff8001809c: e022 sd s0,0(sp) ffffffff8001809e: e406 sd ra,8(sp) ffffffff800180a0: 0800 addi s0,sp,16 ffffffff800180a2: 00000013 nop ffffffff800180a6: 60a2 ld ra,8(sp) ffffffff800180a8: 6402 ld s0,0(sp) ffffffff800180aa: 4785 li a5,1 ffffffff800180ac: 179a slli a5,a5,0x26
ffffffff800180ae:00f53533sltu a0,a0,a5ffffffff800180b2: 0141 addi sp,sp,16ffffffff800180b4: 8082 retonly 12 instruction!! For refernece, before the patch, the access(addr,
1) needs 32 instructions.Signed-off-by Jisheng Zhang <[email protected]>--- arch/riscv/Kconfig | 24 ++++++++++++++++++++++++ arch/riscv/include/asm/pgtable-64.h | 12 ++++++++++++ arch/riscv/mm/init.c | 10 ++++++++++ 3 files changed, 46 insertions(+)arch/riscv/Kconfig +0
-0arch/riscv/Kconfig config RISCV_ISA_SSQOSID · line 608
Quoted 7 lines
arch/riscv/Kconfig +4
-0@@ -602,6 +602,30 @@ config RISCV_ISA_SSQOSID cache partitioning scheme and use the MCID to track how much cache a process, or a group of processes, is using. +config RISCV_ISA_SV48 + bool "Page-Based 48-bit Virtual-Memory System" + depends on 64BIT && NONPORTABLE + default yI don't understand how you've got this set up here.
If this is genuinely non-portable, then you should not set the default to enabled. However, I'm not really sure that this is actually non-portable at all....Quoted 51 lines
arch/riscv/Kconfig +20
-0+ help + Add support for the Page-Based 48-bit Virtual-Memory System. + + Enable this option for systems for which a 39-bit virtual address + space is insufficient. But if 39-bit is enough, disabling this + option will result in smaller kernel size and slighly better + performance. + +config RISCV_ISA_SV57 + bool "Page-Based 57-bit Virtual-Memory System" + depends on 64BIT && NONPORTABLE + default y + help + Add support for the Page-Based 57-bit Virtual-Memory System. + + Enable this option for systems for which a 48-bit virtual address + space is insufficient. But if 48-bit is enough, disabling this + option will result in smaller kernel size and slighly better + performance. + config RISCV_ISA_SVPBMT bool "Svpbmt extension support for supervisor mode page-based memory types" depends on 64BIT && MMUarch/riscv/include/asm/pgtable-64.h +9
-0@@ -16,21 +16,33 @@ extern bool _pgtable_l5_enabled;#ifdef USE_EARLY_PGTABLE_LEVELS static __always_inline bool pgtable_l5_enabled(void) { +if (IS_ENABLED (CONFIG_NONPORTABLE ) && !IS_ENABLED (CONFIG_RISCV_ISA_SV57 )) +return false; +return _pgtable_l5_enabled; } static __always_inline bool pgtable_l4_enabled(void) { +if (IS_ENABLED (CONFIG_NONPORTABLE ) && !IS_ENABLED (CONFIG_RISCV_ISA_SV48 )) +return false; +return _pgtable_l4_enabled; }#else static __always_inline bool pgtable_l4_enabled(void) { +if (IS_ENABLED (CONFIG_NONPORTABLE ) && !IS_ENABLED (CONFIG_RISCV_ISA_SV48 )) +return false; +return riscv_has_extension_likely (RISCV_ISA_EXT_SV48 );arch/riscv/include/asm/pgtable-64.h extern bool _pgtable_l5_enabled; · line 48
Quoted 10 lines
arch/riscv/include/asm/pgtable-64.h +3
-0} static __always_inline bool pgtable_l5_enabled(void) { +if (IS_ENABLED (CONFIG_NONPORTABLE ) && !IS_ENABLED (CONFIG_RISCV_ISA_SV57 )) +return false; +return riscv_has_extension_likely (RISCV_ISA_EXT_SV57 ); }#endif ...because the code here won't enable sv57 etc if not supported...
arch/riscv/mm/init.c static __init void set_satp_mode(uintptr_t dtb_pa) · line 850
Quoted 12 lines
arch/riscv/mm/init.c +4
-0@@ -844,6 +844,16 @@ static __init void set_satp_mode(uintptr_t dtb_pa) kernel_map.page_offset =PAGE_OFFSET_L5 ; +/* Fall back to Sv48 if Sv57 is unsupported on non-portable systems */ +if (IS_ENABLED (CONFIG_NONPORTABLE ) && !IS_ENABLED (CONFIG_RISCV_ISA_SV57 ) && + satp_mode_limit ==0 ) + satp_mode_limit =SATP_MODE_48 ;...and neither does the code here.
Quoted 7 lines
Quoted 4 lines
Introduce
RISCV_ISA_SV48andRISCV_ISA_SV57under NONPORTABLE, so that the embedded platforms can choose the best option themselves, while still keep the feature of unified one kernel Image for all SV39, SV48 and SV57.NONPORTABLE is what you're meant to depend on when the kernel that gets built cannot run on platforms without the feature.
Quoted 6 lines
The reason I put these two
Kconfigoptions under NONPORTABLE is: all distributions won't enable NONPORTABLE for their defconfig, and I guess the distributions want to kept unified one kernel Image for all SV39, SV48 and SV57.I have no preference whether move them out of NONPORTABLE or not.
Oh right I think I made a mistake in my understanding of what's going on here.
If you turn on NONPORTABLE (but not these new options) then you only get sv39 support. I think what you should do to make the behaviour of these options clearer is invert them.
e.g. something like RISCV_ISA_NO_SV57 and RISCV_ISA_NO_SV48. These would then depend on NONPORTABLE and be default n.
arch/riscv/include/asm/pgtable-64.h +12 -0
@@ -16,21 +16,33 @@ extern bool _pgtable_l5_enabled;#ifdef USE_EARLY_PGTABLE_LEVELS static __always_inline bool pgtable_l5_enabled(void) { +if (IS_ENABLED (CONFIG_RISCV_ISA_NO_SV57 )) +return false; +return _pgtable_l5_enabled; } static __always_inline bool pgtable_l4_enabled(void) { +if (IS_ENABLED (CONFIG_RISCV_ISA_SV48 )) +return false; +return _pgtable_l4_enabled; }#else static __always_inline bool pgtable_l4_enabled(void) { +if (IS_ENABLED (CONFIG_RISCV_ISA_NO_SV48 )) +return false; +return riscv_has_extension_likely (RISCV_ISA_EXT_SV48 ); } static __always_inline bool pgtable_l5_enabled(void) { +if (IS_ENABLED (CONFIG_RISCV_ISA_NO_SV57 )) +return false; +return riscv_has_extension_likely (RISCV_ISA_EXT_SV57 ); }#endif
arch/riscv/mm/init.c +9 -0
@@ -844,6 +844,16 @@ static __init void set_satp_mode(uintptr_t dtb_pa) kernel_map.page_offset =PAGE_OFFSET_L5 ; +/* Fall back to Sv48 if Sv57 is unsupported on non-portable systems */ +if (IS_ENABLED (CONFIG_RISCV_ISA_NO_SV57 ) && + satp_mode_limit ==0 ) + satp_mode_limit =SATP_MODE_48 ; + +/* Fall back to Sv39 if Sv48 is unsupported on non-portable systems */ +if (IS_ENABLED (CONFIG_RISCV_ISA_NO_SV48 ) && + satp_mode_limit ==SATP_MODE_48 ) + satp_mode_limit =SATP_MODE_39 ;
I think that code's behaviour is a bit easier to understand and the options become more obvious in terms of what they do.
Cheers,
Conor.
Quoted 15 lines
Quoted 13 lines
Quoted 13 lines
arch/riscv/mm/init.c +6
-0+ +/* Fall back to Sv39 if Sv48 is unsupported on non-portable systems */ +if (IS_ENABLED (CONFIG_NONPORTABLE ) && !IS_ENABLED (CONFIG_RISCV_ISA_SV48 ) && + satp_mode_limit ==SATP_MODE_48 ) + satp_mode_limit =SATP_MODE_39 ; +if (satp_mode_limit ==SATP_MODE_48 ) {disable_pgtable_l5 (); }else if (satp_mode_limit ==SATP_MODE_39 ) {Signature · 1 line
2.53.0
Re: [PATCH v2 3/5] riscv: support early isa ext and use it to optimize pgtable_l4|l5_enabled
In reply to Conor DooleyQuoted 272 lines · Conor Dooley
Quoted 271 lines · Jisheng Zhang
Quoted 260 lines · Conor Dooley
Quoted 222 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.
So the question is whether we can add RISCV_ISA_EXT_SV48/SV5 and use riscv_has_extension_*() or not. Per [1] and [2], SV48 and SV57 are ISA exensions too. From another side, riscv_has_extension_[un]likely() and other related functions report whether the extension is supported and enabled on the platform. So SV48 and SV57 can be supported with current isa extension alternative mechanism.
However, to use it to optimize pgtable_l4|l5_enabled, we have support the "early" characteristic, I.E besides
risc_isabitmap setting, we need to support appling alternative early before MMU on.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:
These memory load and test branch instructions are replaced with only two j or nop instructions.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.h | 2 + arch/riscv/include/asm/hwcap.h | 2 + arch/riscv/include/asm/pgtable-64.h | 12 ++++++ arch/riscv/kernel/alternative.c | 24 +++++++---- arch/riscv/kernel/cpufeature.c | 60 ++++++++++++++++++++++------ arch/riscv/mm/init.c | 9 +++++ 8 files changed, 91 insertions(+), 21 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.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_isa, RISCV_ISA_EXT_MAX); +extern u32 thead_vlenb_of;void __init riscv_user_isa_enable (void );arch/riscv/include/asm/hwcap.h +2
-0@@ -122,6 +122,8 @@#define RISCV_ISA_EXT_ZICCAMOA 113#define RISCV_ISA_EXT_ZICCIF 114#define RISCV_ISA_EXT_ZA64RS 115 +#define RISCV_ISA_EXT_SV48 116 +#define RISCV_ISA_EXT_SV57 117#define RISCV_ISA_EXT_XLINUXENVCFG 127arch/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_extension_likely (RISCV_ISA_EXT_SV48 ); +} + +static __always_inline bool pgtable_l5_enabled(void) +{ +return riscv_has_extension_likely (RISCV_ISA_EXT_SV57 ); +} +#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 +3
-1@@ -39,7 +39,7 @@ static bool any_cpu_has_zicbom;unsigned long elf_hwcap__read_mostly ;/* Host ISA bitmap */ -static DECLARE_BITMAP(riscv_isa, RISCV_ISA_EXT_MAX) __read_mostly; +DECLARE_BITMAP (riscv_isa,RISCV_ISA_EXT_MAX )__read_mostly ;/* Per-cpu ISA extensions. */ struct riscv_isainfo hart_isa[NR_CPUS ];@@ -624,6 +624,8 @@ const struct riscv_isa_ext_data riscv_isa_ext[] = {__RISCV_ISA_EXT_DATA (svpbmt,RISCV_ISA_EXT_SVPBMT ),__RISCV_ISA_EXT_DATA (svrsw60t59b,RISCV_ISA_EXT_SVRSW60T59B ),__RISCV_ISA_EXT_DATA (svvptc,RISCV_ISA_EXT_SVVPTC ), +__RISCV_ISA_EXT_DATA (sv48,RISCV_ISA_EXT_SV48 ), + __RISCV_ISA_EXT_DATA(sv57, RISCV_ISA_EXT_SV57), };const size_t riscv_isa_ext_count =ARRAY_SIZE (riscv_isa_ext);arch/riscv/kernel/cpufeature.c static void __init riscv_parse_isa_string(const char *isa, unsigned long *bitmap · line 868
Quoted 6 lines
arch/riscv/kernel/cpufeature.c +2
-1@@ -862,7 +864,8 @@ static void __init riscv_parse_isa_string(const char *isa, unsigned long *bitmap } } -static void __init riscv_fill_hwcap_from_isa_string (unsigned long *isa2hwcap) +static void __init riscv_fill_hwcap_from_isa_string (unsigned long *isa2hwcap, +unsigned long *riscv_isa_early)All of the "early" business in this file seems overly complicated. Can we just do something like this?
arch/riscv/kernel/cpufeature.c +10
-0@@ -922,6 +922,11 @@ static void __init riscv_fill_hwcap_from_isa_string(unsigned long *isa2hwcap)set_bit (RISCV_ISA_EXT_ZIHPM , source_isa); } +if (_pgtable_l4_enabled) +set_bit (RISCV_ISA_EXT_SV48 , source_isa); +if (_pgtable_l5_enabled) +set_bit (RISCV_ISA_EXT_SV57 , source_isa); +/* * "V" in ISA strings is ambiguous in practice: it should mean * just the standard V-1.0 but vendors aren't well behaved. @@ -1082,6 +1087,11 @@ static int __init riscv_fill_hwcap_from_ext_list(unsigned long *isa2hwcap)riscv_isa_set_ext (ext, source_isa); } +if (_pgtable_l4_enabled) +set_bit (RISCV_ISA_EXT_SV48 , source_isa); +if (_pgtable_l5_enabled) +set_bit (RISCV_ISA_EXT_SV57 , source_isa); +riscv_resolve_isa (source_isa, isainfo->isa, &this_hwcap, isa2hwcap);riscv_fill_cpu_vendor_ext (cpu_node, cpu);Quoted 4 lines
The pagetable levels are the only thing we do this kind of early and probed detection for, adding a framework for them and only them that mostly just complicates the normal case seems rather excessive. Doing something simple like this is also quite explicit as to what's being
This assumes SV48 and SV57 are the only "early" users, but
other ISA ext may also need "early", who knows. So if future some extensions need the "early", the code is ready, the author doesn't need to care about the isa filling at all.
Quoted 3 lines
Quoted 1 line
So I prefer my patch as is. What's your opinion?
We're about decade into the port being merged and this is the only thing
but the isa extension alternative is not, it was introduced by me three years ago. But no matter how many years,
Quoted 2 lines
behaving in this way. I don't think the code in this patch is worth it on the off chance that something else comes along. If it does, we can
this makes sense. Let me cook a new version
Quoted 6 lines
always fish this implementation back up and use it.
I'd also like to differentiate this code from "needing early", because this is about populating the information early in the extension bitmap, rather than about actually needing the information. There's no advantage
the code after arch/riscv/mm/init.c but before mmu on needs the bitmap informaion when pgtable_l5|l4_enabled() is called (w/o USE_EARLY_PGTABLE_LEVELS)
Quoted 3 lines
gained, as far as I can tell, by setting this early and it only makes the code more complicated.
Re: [PATCH v2 4/5] riscv: RISCV_ISA_SV48 and RISCV_ISA_SV57 under NONPORTABLE
In reply to Conor DooleyQuoted 194 lines · Conor Dooley
Quoted 193 lines · Jisheng Zhang
Quoted 179 lines · Conor Dooley
Quoted 86 lines · Jisheng Zhang
In last commit, we have optimized the pgtable_l4|l5_enabled with isa extension alternative mechanism, thus a typical
access_ok(addr, 1); is compiled as:ffffffff8001dd30: 1141 addi sp,sp,-16 ffffffff8001dd32: e022 sd s0,0(sp) ffffffff8001dd34: e406 sd ra,8(sp) ffffffff8001dd36: 0800 addi s0,sp,16 ffffffff8001dd38: 00000013 nop ffffffff8001dd3c: 02a0006f j ffffffff8001dd66 <foo+0x3e> ffffffff8001dd40: 60a2 ld ra,8(sp) ffffffff8001dd42: 6402 ld s0,0(sp) ffffffff8001dd44: 57fd li a5,-1 ffffffff8001dd46: 83a1 srli a5,a5,0x8
ffffffff8001dd48:00a7b533sltu a0,a5,a0ffffffff8001dd4c: 00154513 xori a0,a0,1ffffffff8001dd50: 0141 addi sp,sp,16ffffffff8001dd52: 8082 ret
...ffffffff8001dd64: bfe1 jffffffff8001dd3c<foo+0x14>ffffffff8001dd66:0180006fjffffffff8001dd7e<foo+0x56>ffffffff8001dd6a: 60a2 ld ra,8(sp) ffffffff8001dd6c: 6402 ld s0,0(sp) ffffffff8001dd6e: 57fd li a5,-1 ffffffff8001dd70: 83c5 srli a5,a5,0x11 ffffffff8001dd72: 00a7b533 sltu a0,a5,a0 ffffffff8001dd76: 00154513 xori a0,a0,1 ffffffff8001dd7a: 0141 addi sp,sp,16 ffffffff8001dd7c: 8082 ret ffffffff8001dd7e: 60a2 ld ra,8(sp) ffffffff8001dd80: 6402 ld s0,0(sp) ffffffff8001dd82: 57fd li a5,-1 ffffffff8001dd84: 83e9 srli a5,a5,0x1a
ffffffff8001dd86:00a7b533sltu a0,a5,a0ffffffff8001dd8a: 00154513 xori a0,a0,1ffffffff8001dd8e: 0141 addi sp,sp,16ffffffff8001dd90: 8082 retAs can be seen, different branches for SV39/SV48/SV57 are still there, since
access_ok()sits at hot code path, why not reduce the code size to optimize to make the instruction cache happy?Introduce
RISCV_ISA_SV48andRISCV_ISA_SV57under NONPORTABLE, so that the embedded platforms can choose the best option themselves, while still keep the feature of unified one kernel Image for all SV39, SV48 and SV57.Before the patch, vmlinux built with pure RISCV64 defconfig
text data bss dec hex filename 13439437 6790446 483221 20713104 13c0e90 /tmp/old/vmlinuxAfter the patch, vmlinux built with pure RISCV64 defconfig:
text data bss dec hex filename 13314445 6787934 466837 20569216 139dc80 /tmp/new/vmlinux.text section is reduced by 122KB!
And after the patch, typical
access_ok(addr, 1)is compiled as:ffffffff8001809a: 1141 addi sp,sp,-16 ffffffff8001809c: e022 sd s0,0(sp) ffffffff8001809e: e406 sd ra,8(sp) ffffffff800180a0: 0800 addi s0,sp,16 ffffffff800180a2: 00000013 nop ffffffff800180a6: 60a2 ld ra,8(sp) ffffffff800180a8: 6402 ld s0,0(sp) ffffffff800180aa: 4785 li a5,1 ffffffff800180ac: 179a slli a5,a5,0x26
ffffffff800180ae:00f53533sltu a0,a0,a5ffffffff800180b2: 0141 addi sp,sp,16ffffffff800180b4: 8082 retonly 12 instruction!! For refernece, before the patch, the access(addr,
1) needs 32 instructions.Signed-off-by Jisheng Zhang <[email protected]>--- arch/riscv/Kconfig | 24 ++++++++++++++++++++++++ arch/riscv/include/asm/pgtable-64.h | 12 ++++++++++++ arch/riscv/mm/init.c | 10 ++++++++++ 3 files changed, 46 insertions(+)arch/riscv/Kconfig +0
-0arch/riscv/Kconfig config RISCV_ISA_SSQOSID · line 608
Quoted 7 lines
arch/riscv/Kconfig +4
-0@@ -602,6 +602,30 @@ config RISCV_ISA_SSQOSID cache partitioning scheme and use the MCID to track how much cache a process, or a group of processes, is using. +config RISCV_ISA_SV48 + bool "Page-Based 48-bit Virtual-Memory System" + depends on 64BIT && NONPORTABLE + default yI don't understand how you've got this set up here.
If this is genuinely non-portable, then you should not set the default to enabled. However, I'm not really sure that this is actually non-portable at all....Quoted 51 lines
arch/riscv/Kconfig +20
-0+ help + Add support for the Page-Based 48-bit Virtual-Memory System. + + Enable this option for systems for which a 39-bit virtual address + space is insufficient. But if 39-bit is enough, disabling this + option will result in smaller kernel size and slighly better + performance. + +config RISCV_ISA_SV57 + bool "Page-Based 57-bit Virtual-Memory System" + depends on 64BIT && NONPORTABLE + default y + help + Add support for the Page-Based 57-bit Virtual-Memory System. + + Enable this option for systems for which a 48-bit virtual address + space is insufficient. But if 48-bit is enough, disabling this + option will result in smaller kernel size and slighly better + performance. + config RISCV_ISA_SVPBMT bool "Svpbmt extension support for supervisor mode page-based memory types" depends on 64BIT && MMUarch/riscv/include/asm/pgtable-64.h +9
-0@@ -16,21 +16,33 @@ extern bool _pgtable_l5_enabled;#ifdef USE_EARLY_PGTABLE_LEVELS static __always_inline bool pgtable_l5_enabled(void) { +if (IS_ENABLED (CONFIG_NONPORTABLE ) && !IS_ENABLED (CONFIG_RISCV_ISA_SV57 )) +return false; +return _pgtable_l5_enabled; } static __always_inline bool pgtable_l4_enabled(void) { +if (IS_ENABLED (CONFIG_NONPORTABLE ) && !IS_ENABLED (CONFIG_RISCV_ISA_SV48 )) +return false; +return _pgtable_l4_enabled; }#else static __always_inline bool pgtable_l4_enabled(void) { +if (IS_ENABLED (CONFIG_NONPORTABLE ) && !IS_ENABLED (CONFIG_RISCV_ISA_SV48 )) +return false; +return riscv_has_extension_likely (RISCV_ISA_EXT_SV48 );arch/riscv/include/asm/pgtable-64.h extern bool _pgtable_l5_enabled; · line 48
Quoted 10 lines
arch/riscv/include/asm/pgtable-64.h +3
-0} static __always_inline bool pgtable_l5_enabled(void) { +if (IS_ENABLED (CONFIG_NONPORTABLE ) && !IS_ENABLED (CONFIG_RISCV_ISA_SV57 )) +return false; +return riscv_has_extension_likely (RISCV_ISA_EXT_SV57 ); }#endif ...because the code here won't enable sv57 etc if not supported...
arch/riscv/mm/init.c static __init void set_satp_mode(uintptr_t dtb_pa) · line 850
Quoted 12 lines
arch/riscv/mm/init.c +4
-0@@ -844,6 +844,16 @@ static __init void set_satp_mode(uintptr_t dtb_pa) kernel_map.page_offset =PAGE_OFFSET_L5 ; +/* Fall back to Sv48 if Sv57 is unsupported on non-portable systems */ +if (IS_ENABLED (CONFIG_NONPORTABLE ) && !IS_ENABLED (CONFIG_RISCV_ISA_SV57 ) && + satp_mode_limit ==0 ) + satp_mode_limit =SATP_MODE_48 ;...and neither does the code here.
Quoted 7 lines
Quoted 4 lines
Introduce
RISCV_ISA_SV48andRISCV_ISA_SV57under NONPORTABLE, so that the embedded platforms can choose the best option themselves, while still keep the feature of unified one kernel Image for all SV39, SV48 and SV57.NONPORTABLE is what you're meant to depend on when the kernel that gets built cannot run on platforms without the feature.
The reason I put these two
Kconfigoptions under NONPORTABLE is: all distributions won't enable NONPORTABLE for their defconfig, and I guess the distributions want to kept unified one kernel Image for all SV39, SV48 and SV57.
Quoted 8 lines
Quoted 1 line
I have no preference whether move them out of NONPORTABLE or not.
Oh right I think I made a mistake in my understanding of what's going on here.
If you turn on NONPORTABLE (but not these new options) then you only get sv39 support. I think what you should do to make the behaviour of these options clearer is invert them.
hmm, other ISA ext Kconfig options are not inverted, so I prefer moving SV48|57 out of NONPORTABLE and make them y by default.
Quoted 80 lines
e.g. something like
RISCV_ISA_NO_SV57andRISCV_ISA_NO_SV48. These would then depend on NONPORTABLE and be default n.arch/riscv/include/asm/pgtable-64.h +12
-0@@ -16,21 +16,33 @@ extern bool _pgtable_l5_enabled;#ifdef USE_EARLY_PGTABLE_LEVELS static __always_inline bool pgtable_l5_enabled(void) { +if (IS_ENABLED (CONFIG_RISCV_ISA_NO_SV57 )) +return false; +return _pgtable_l5_enabled; } static __always_inline bool pgtable_l4_enabled(void) { +if (IS_ENABLED (CONFIG_RISCV_ISA_SV48 )) +return false; +return _pgtable_l4_enabled; }#else static __always_inline bool pgtable_l4_enabled(void) { +if (IS_ENABLED (CONFIG_RISCV_ISA_NO_SV48 )) +return false; +return riscv_has_extension_likely (RISCV_ISA_EXT_SV48 ); } static __always_inline bool pgtable_l5_enabled(void) { +if (IS_ENABLED (CONFIG_RISCV_ISA_NO_SV57 )) +return false; +return riscv_has_extension_likely (RISCV_ISA_EXT_SV57 ); }#endif arch/riscv/mm/init.c +9
-0@@ -844,6 +844,16 @@ static __init void set_satp_mode(uintptr_t dtb_pa) kernel_map.page_offset =PAGE_OFFSET_L5 ; +/* Fall back to Sv48 if Sv57 is unsupported on non-portable systems */ +if (IS_ENABLED (CONFIG_RISCV_ISA_NO_SV57 ) && + satp_mode_limit ==0 ) + satp_mode_limit =SATP_MODE_48 ; + +/* Fall back to Sv39 if Sv48 is unsupported on non-portable systems */ +if (IS_ENABLED (CONFIG_RISCV_ISA_NO_SV48 ) && + satp_mode_limit ==SATP_MODE_48 ) + satp_mode_limit =SATP_MODE_39 ;I think that code's behaviour is a bit easier to understand and the options become more obvious in terms of what they do.
Cheers,
Conor.Quoted 15 lines
Quoted 13 lines
Quoted 13 lines
arch/riscv/mm/init.c +6
-0+ +/* Fall back to Sv39 if Sv48 is unsupported on non-portable systems */ +if (IS_ENABLED (CONFIG_NONPORTABLE ) && !IS_ENABLED (CONFIG_RISCV_ISA_SV48 ) && + satp_mode_limit ==SATP_MODE_48 ) + satp_mode_limit =SATP_MODE_39 ; +if (satp_mode_limit ==SATP_MODE_48 ) {disable_pgtable_l5 (); }else if (satp_mode_limit ==SATP_MODE_39 ) {Signature · 1 line
2.53.0
Re: [PATCH v2 4/5] riscv: RISCV_ISA_SV48 and RISCV_ISA_SV57 under NONPORTABLE
Quoted 200 lines · Jisheng Zhang
Quoted 199 lines · Conor Dooley
Quoted 188 lines · Jisheng Zhang
Quoted 179 lines · Conor Dooley
Quoted 86 lines · Jisheng Zhang
In last commit, we have optimized the pgtable_l4|l5_enabled with isa extension alternative mechanism, thus a typical
access_ok(addr, 1); is compiled as:ffffffff8001dd30: 1141 addi sp,sp,-16 ffffffff8001dd32: e022 sd s0,0(sp) ffffffff8001dd34: e406 sd ra,8(sp) ffffffff8001dd36: 0800 addi s0,sp,16 ffffffff8001dd38: 00000013 nop ffffffff8001dd3c: 02a0006f j ffffffff8001dd66 <foo+0x3e> ffffffff8001dd40: 60a2 ld ra,8(sp) ffffffff8001dd42: 6402 ld s0,0(sp) ffffffff8001dd44: 57fd li a5,-1 ffffffff8001dd46: 83a1 srli a5,a5,0x8
ffffffff8001dd48:00a7b533sltu a0,a5,a0ffffffff8001dd4c: 00154513 xori a0,a0,1ffffffff8001dd50: 0141 addi sp,sp,16ffffffff8001dd52: 8082 ret
...ffffffff8001dd64: bfe1 jffffffff8001dd3c<foo+0x14>ffffffff8001dd66:0180006fjffffffff8001dd7e<foo+0x56>ffffffff8001dd6a: 60a2 ld ra,8(sp) ffffffff8001dd6c: 6402 ld s0,0(sp) ffffffff8001dd6e: 57fd li a5,-1 ffffffff8001dd70: 83c5 srli a5,a5,0x11 ffffffff8001dd72: 00a7b533 sltu a0,a5,a0 ffffffff8001dd76: 00154513 xori a0,a0,1 ffffffff8001dd7a: 0141 addi sp,sp,16 ffffffff8001dd7c: 8082 ret ffffffff8001dd7e: 60a2 ld ra,8(sp) ffffffff8001dd80: 6402 ld s0,0(sp) ffffffff8001dd82: 57fd li a5,-1 ffffffff8001dd84: 83e9 srli a5,a5,0x1a
ffffffff8001dd86:00a7b533sltu a0,a5,a0ffffffff8001dd8a: 00154513 xori a0,a0,1ffffffff8001dd8e: 0141 addi sp,sp,16ffffffff8001dd90: 8082 retAs can be seen, different branches for SV39/SV48/SV57 are still there, since
access_ok()sits at hot code path, why not reduce the code size to optimize to make the instruction cache happy?Introduce
RISCV_ISA_SV48andRISCV_ISA_SV57under NONPORTABLE, so that the embedded platforms can choose the best option themselves, while still keep the feature of unified one kernel Image for all SV39, SV48 and SV57.Before the patch, vmlinux built with pure RISCV64 defconfig
text data bss dec hex filename 13439437 6790446 483221 20713104 13c0e90 /tmp/old/vmlinuxAfter the patch, vmlinux built with pure RISCV64 defconfig:
text data bss dec hex filename 13314445 6787934 466837 20569216 139dc80 /tmp/new/vmlinux.text section is reduced by 122KB!
And after the patch, typical
access_ok(addr, 1)is compiled as:ffffffff8001809a: 1141 addi sp,sp,-16 ffffffff8001809c: e022 sd s0,0(sp) ffffffff8001809e: e406 sd ra,8(sp) ffffffff800180a0: 0800 addi s0,sp,16 ffffffff800180a2: 00000013 nop ffffffff800180a6: 60a2 ld ra,8(sp) ffffffff800180a8: 6402 ld s0,0(sp) ffffffff800180aa: 4785 li a5,1 ffffffff800180ac: 179a slli a5,a5,0x26
ffffffff800180ae:00f53533sltu a0,a0,a5ffffffff800180b2: 0141 addi sp,sp,16ffffffff800180b4: 8082 retonly 12 instruction!! For refernece, before the patch, the access(addr,
1) needs 32 instructions.Signed-off-by Jisheng Zhang <[email protected]>--- arch/riscv/Kconfig | 24 ++++++++++++++++++++++++ arch/riscv/include/asm/pgtable-64.h | 12 ++++++++++++ arch/riscv/mm/init.c | 10 ++++++++++ 3 files changed, 46 insertions(+)arch/riscv/Kconfig +0
-0arch/riscv/Kconfig config RISCV_ISA_SSQOSID · line 608
Quoted 7 lines
arch/riscv/Kconfig +4
-0@@ -602,6 +602,30 @@ config RISCV_ISA_SSQOSID cache partitioning scheme and use the MCID to track how much cache a process, or a group of processes, is using. +config RISCV_ISA_SV48 + bool "Page-Based 48-bit Virtual-Memory System" + depends on 64BIT && NONPORTABLE + default yI don't understand how you've got this set up here.
If this is genuinely non-portable, then you should not set the default to enabled. However, I'm not really sure that this is actually non-portable at all....Quoted 51 lines
arch/riscv/Kconfig +20
-0+ help + Add support for the Page-Based 48-bit Virtual-Memory System. + + Enable this option for systems for which a 39-bit virtual address + space is insufficient. But if 39-bit is enough, disabling this + option will result in smaller kernel size and slighly better + performance. + +config RISCV_ISA_SV57 + bool "Page-Based 57-bit Virtual-Memory System" + depends on 64BIT && NONPORTABLE + default y + help + Add support for the Page-Based 57-bit Virtual-Memory System. + + Enable this option for systems for which a 48-bit virtual address + space is insufficient. But if 48-bit is enough, disabling this + option will result in smaller kernel size and slighly better + performance. + config RISCV_ISA_SVPBMT bool "Svpbmt extension support for supervisor mode page-based memory types" depends on 64BIT && MMUarch/riscv/include/asm/pgtable-64.h +9
-0@@ -16,21 +16,33 @@ extern bool _pgtable_l5_enabled;#ifdef USE_EARLY_PGTABLE_LEVELS static __always_inline bool pgtable_l5_enabled(void) { +if (IS_ENABLED (CONFIG_NONPORTABLE ) && !IS_ENABLED (CONFIG_RISCV_ISA_SV57 )) +return false; +return _pgtable_l5_enabled; } static __always_inline bool pgtable_l4_enabled(void) { +if (IS_ENABLED (CONFIG_NONPORTABLE ) && !IS_ENABLED (CONFIG_RISCV_ISA_SV48 )) +return false; +return _pgtable_l4_enabled; }#else static __always_inline bool pgtable_l4_enabled(void) { +if (IS_ENABLED (CONFIG_NONPORTABLE ) && !IS_ENABLED (CONFIG_RISCV_ISA_SV48 )) +return false; +return riscv_has_extension_likely (RISCV_ISA_EXT_SV48 );arch/riscv/include/asm/pgtable-64.h extern bool _pgtable_l5_enabled; · line 48
Quoted 10 lines
arch/riscv/include/asm/pgtable-64.h +3
-0} static __always_inline bool pgtable_l5_enabled(void) { +if (IS_ENABLED (CONFIG_NONPORTABLE ) && !IS_ENABLED (CONFIG_RISCV_ISA_SV57 )) +return false; +return riscv_has_extension_likely (RISCV_ISA_EXT_SV57 ); }#endif ...because the code here won't enable sv57 etc if not supported...
arch/riscv/mm/init.c static __init void set_satp_mode(uintptr_t dtb_pa) · line 850
Quoted 12 lines
arch/riscv/mm/init.c +4
-0@@ -844,6 +844,16 @@ static __init void set_satp_mode(uintptr_t dtb_pa) kernel_map.page_offset =PAGE_OFFSET_L5 ; +/* Fall back to Sv48 if Sv57 is unsupported on non-portable systems */ +if (IS_ENABLED (CONFIG_NONPORTABLE ) && !IS_ENABLED (CONFIG_RISCV_ISA_SV57 ) && + satp_mode_limit ==0 ) + satp_mode_limit =SATP_MODE_48 ;...and neither does the code here.
Quoted 7 lines
Quoted 4 lines
Introduce
RISCV_ISA_SV48andRISCV_ISA_SV57under NONPORTABLE, so that the embedded platforms can choose the best option themselves, while still keep the feature of unified one kernel Image for all SV39, SV48 and SV57.NONPORTABLE is what you're meant to depend on when the kernel that gets built cannot run on platforms without the feature.
Quoted 6 lines
The reason I put these two
Kconfigoptions under NONPORTABLE is: all distributions won't enable NONPORTABLE for their defconfig, and I guess the distributions want to kept unified one kernel Image for all SV39, SV48 and SV57.I have no preference whether move them out of NONPORTABLE or not.
Oh right I think I made a mistake in my understanding of what's going on here.
Quoted 6 lines
Quoted 3 lines
If you turn on NONPORTABLE (but not these new options) then you only get sv39 support. I think what you should do to make the behaviour of these options clearer is invert them.
hmm, other ISA ext
Kconfigoptions are not inverted, so I prefer moving SV48|57 out of NONPORTABLE and make them y by default.
Sure, that works for me.
Re: [PATCH v2 3/5] riscv: support early isa ext and use it to optimize pgtable_l4|l5_enabled
In reply to Jisheng ZhangQuoted 20 lines · Jisheng Zhang
Quoted 8 lines
Quoted 6 lines
This assumes SV48 and SV57 are the only "early" users, but
other ISA ext may also need "early", who knows. So if future some extensions need the "early", the code is ready, the author doesn't need to care about the isa filling at all.So I prefer my patch as is. What's your opinion?
We're about decade into the port being merged and this is the only thing
but the isa extension alternative is not, it was introduced by me three years ago. But no matter how many years,
Quoted 2 lines
behaving in this way. I don't think the code in this patch is worth it on the off chance that something else comes along. If it does, we can
this makes sense. Let me cook a new version
Quoted 3 lines
always fish this implementation back up and use it.
Quoted 7 lines
Quoted 3 lines
I'd also like to differentiate this code from "needing early", because this is about populating the information early in the extension bitmap, rather than about actually needing the information. There's no advantage
the code after
arch/riscv/mm/init.cbut before mmu on needs the bitmap informaion when pgtable_l5|l4_enabled() is called (w/oUSE_EARLY_PGTABLE_LEVELS)
Oh, I must have missed something then, I didn't realise you were patching the alternatives early - I thought you were skipping them until the information became available at the normal time. Sorry bout that.
How bad is the damage btw, if you implement pgtable_l5_enabled() as
static __always_inline bool pgtable_l5_enabled(void)
{
if (riscv_has_extension_likely(RISCV_ISA_EXT_SV57))
return true;
return _pgtable_l5_enabled;
}? It shouldn't be too bad since it should get expanded to stuff like
if (riscv_has_extension_likely(RISCV_ISA_EXT_SV57) || _pgtable_l5_enabled)
by the compiler. I wonder if you can do this and get rid of USE_EARLY_PGTABLE_LEVELS entirely, since the unpatched alternative should return false?
Cheers,
Conor.
(btw, I am kinda unavailable til the 22nd, so sorry if I take some time to reply here or to a new revision)
Quoted 5 lines
Quoted 4 lines
gained, as far as I can tell, by setting this early and it only makes the code more complicated.
Re: [PATCH v2 3/5] riscv: support early isa ext and use it to optimize pgtable_l4|l5_enabled
Quoted 25 lines · Conor Dooley
Quoted 24 lines · Jisheng Zhang
Quoted 8 lines
Quoted 6 lines
This assumes SV48 and SV57 are the only "early" users, but
other ISA ext may also need "early", who knows. So if future some extensions need the "early", the code is ready, the author doesn't need to care about the isa filling at all.So I prefer my patch as is. What's your opinion?
We're about decade into the port being merged and this is the only thing
but the isa extension alternative is not, it was introduced by me three years ago. But no matter how many years,
Quoted 2 lines
behaving in this way. I don't think the code in this patch is worth it on the off chance that something else comes along. If it does, we can
this makes sense. Let me cook a new version
Quoted 6 lines
always fish this implementation back up and use it.
I'd also like to differentiate this code from "needing early", because this is about populating the information early in the extension bitmap, rather than about actually needing the information. There's no advantage
Quoted 7 lines
Quoted 3 lines
the code after
arch/riscv/mm/init.cbut before mmu on needs the bitmap informaion when pgtable_l5|l4_enabled() is called (w/oUSE_EARLY_PGTABLE_LEVELS)Oh, I must have missed something then, I didn't realise you were patching the alternatives early - I thought you were skipping them until the information became available at the normal time. Sorry bout that.
Aha, your suggestion about the simplifying the isa bitmap filling still works.
Quoted 11 lines
How bad is the damage btw, if you implement
pgtable_l5_enabled()asstatic
__always_inlineboolpgtable_l5_enabled(void){ if (riscv_has_extension_likely(RISCV_ISA_EXT_SV57)) return true; return _pgtable_l5_enabled; }
Quoted 7 lines
? It shouldn't be too bad since it should get expanded to stuff like
if (riscv_has_extension_likely(RISCV_ISA_EXT_SV57) || _pgtable_l5_enabled)by the compiler. I wonder if you can do this and get rid of
USE_EARLY_PGTABLE_LEVELSentirely, since the unpatched alternative should return false?
I know why you want this. AIUI, the early patched alternative is still needed outside arch/riscv/mm/init.c but before mmu on. For example, on a SV57 capable platform, the set_satp_mode() will detect pgtable cap correctly, then _pgtable_l5_enabled = true and _pgtable_l4_enabled = true, then any pgtable_l5|l4_enabled() calling outside init.c but before mmu on also expects true. so we need to set the isa bitmap so that latter apply_early_boot_alternatives() can correctly patch .text section for us.
BTW, the USE_EARLY_PGTABLE_LEVELS is stolen from the x86 world, see its USE_EARLY_PGTABLE_L5 ;)
Quoted 6 lines
Cheers,
Conor.(btw, I am kinda unavailable til the 22nd, so sorry if I take some time to reply here or to a new revision)
I just sent out v3, could you plz kindly review it when convenient
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
gained, as far as I can tell, by setting this early and it only makes the code more complicated.