MIPS: Select the R10000 LL/SC workaround at boot
Subsystems (3)
cover MIPS: Select the R10000 LL/SC workaround at boot
Kernels for IP27, IP28 and IP30 select CONFIG_WAR_R10000_LLSC, which retries ll/sc sequences with a branch-likely that only R10000s before revision 3.0 need. On an R12000 Octane, dropping it makes fork, pipe, futex and file creation 1-4% faster.
This series keeps it built in and decides at boot. Unless an affected CPU is present, sc branches become beqz, modules are patched at load, and tlbex and the BPF JIT emit beqz.
Patch 3 conflicts in context with the IP35 series' "MIPS: SGI-IP27:
give each CPU its own topology package id"; it resolves in one line.
Patch 5 depends on patch 2, so I'd like the whole series to go through the MIPS tree, with an Acked-by from the BPF maintainers on patch 5.
Tested on an R12000 Octane, and the BPF JIT with test_bpf on QEMU 64-bit Malta. No R10000 before revision 3.0 was available.
Imre Kaloz (5):
MIPS: Use SC_BEQZ in the futex and sysmips ll/sc loops
MIPS: Select the R10000 LL/SC workaround at boot
MIPS: SGI-IP27: Report all CPUs to the R10000 LL/SC workaround
MIPS: tlbex: Follow the boot-time R10000 LL/SC choice bpf, mips: Follow the boot-time R10000 LL/SC choice
arch/mips/Kconfig | 5 +- arch/mips/include/asm/asm.h | 7 +- arch/mips/include/asm/futex.h | 5 +- arch/mips/include/asm/r10k-llsc.h | 29 ++++++++ arch/mips/kernel/Makefile | 1 + arch/mips/kernel/module.c | 5 ++ arch/mips/kernel/r10k-llsc.c | 111 ++++++++++++++++++++++++++++++ arch/mips/kernel/setup.c | 2 + arch/mips/kernel/smp.c | 2 + arch/mips/kernel/syscall.c | 2 +- arch/mips/kernel/vmlinux.lds.S | 8 +++ arch/mips/mm/tlbex.c | 3 +- arch/mips/net/bpf_jit_comp.c | 5 +- arch/mips/net/bpf_jit_comp.h | 12 ++-- arch/mips/net/bpf_jit_comp32.c | 1 + arch/mips/net/bpf_jit_comp64.c | 5 +- arch/mips/sgi-ip27/ip27-smp.c | 2 + 17 files changed, 188 insertions(+), 17 deletions(-) create mode 100644 arch/mips/include/asm/r10k-llsc.h create mode 100644 arch/mips/kernel/r10k-llsc.c
base-commit: 24a19b953e2a3d0ea5a8c4951a0ab8f32720c907
Signature · 1 line
2.47.3
PATCH 1/5 MIPS: Use SC_BEQZ in the futex and sysmips ll/sc loops
The R10000 variants of the futex operations and of sysmips(MIPS_ATOMIC_SET) open-code the beqzl that SC_BEQZ expands to under CONFIG_WAR_R10000_LLSC. Use SC_BEQZ, so they follow any change to it.
No functional change.
---
Notes:
checkpatch warns about whitespace before the quoted newlines: the replaced lines keep the column alignment of the surrounding asm, as the beqzl lines they replace did.
arch/mips/include/asm/futex.h | 5 +++-- arch/mips/kernel/syscall.c | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-)
arch/mips/include/asm/futex.h +3 -2
@@ -12,6 +12,7 @@#include <linux/futex.h> #include <linux/uaccess.h> +#include <asm/asm.h> #include <asm/asm-eva.h> #include <asm/barrier.h> #include <asm/compiler.h>
@@ -35,7 +36,7 @@" " insn" \n" \" .set arch=r4000 \n" \"2: sc $1, %2 \n" \ -" beqzl $1, 1b \n" \ + " " __stringify(SC_BEQZ) " $1, 1b \n" \ __stringify(__WEAK_LLSC_MB) " \n" \"3: \n" \" .insn \n" \
@@ -151,7 +152,7 @@ futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr," move $1, %z5 \n" " .set arch=r4000 \n" "2: sc $1, %2 \n" -" beqzl $1, 1b \n" + " " __stringify(SC_BEQZ) " $1, 1b \n" __stringify(__WEAK_LLSC_MB) " \n""3: \n" " .insn \n"
arch/mips/kernel/syscall.c +1 -1
@@ -115,7 +115,7 @@ static inline int mips_atomic_set(unsigned long addr, unsigned long new)"1: ll %[old], (%[addr]) \n" " move %[tmp], %[new] \n" "2: sc %[tmp], (%[addr]) \n" -" beqzl %[tmp], 1b \n" + " " __stringify(SC_BEQZ) " %[tmp], 1b \n""3: \n" " .insn \n" " .section .fixup,\"ax\" \n"
Signature · 1 line
2.47.3
PATCH 2/5 MIPS: Select the R10000 LL/SC workaround at boot
In reply to Imre KalozOnly R10000s before revision 3.0 need the branch-likely sc retry of CONFIG_WAR_R10000_LLSC, which the whole family mispredicts.
Patch the SC_BEQZ sites to beqz at boot and module load unless a CPU needs it; once patched, do not start such a CPU.
---
Notes:
checkpatch reports an ERROR (spaces around ':') and a do-while WARNING for SC_BEQZ. Its body is assembler text pasted into inline asm through __stringify(), not C: '9999:' is a GAS local label and ';' separates assembler statements. The new files fall under the MIPS entry in MAINTAINERS (F: arch/mips/).
arch/mips/Kconfig | 5 +- arch/mips/include/asm/asm.h | 7 +- arch/mips/include/asm/r10k-llsc.h | 29 ++++++++ arch/mips/kernel/Makefile | 1 + arch/mips/kernel/module.c | 5 ++ arch/mips/kernel/r10k-llsc.c | 111 ++++++++++++++++++++++++++++++ arch/mips/kernel/setup.c | 2 + arch/mips/kernel/smp.c | 2 + arch/mips/kernel/vmlinux.lds.S | 8 +++ 9 files changed, 166 insertions(+), 4 deletions(-) create mode 100644 arch/mips/include/asm/r10k-llsc.h create mode 100644 arch/mips/kernel/r10k-llsc.c
arch/mips/Kconfig +3 -2
@@ -2540,8 +2540,9 @@ config WAR_TX49XX_ICACHE_INDEX_INV config WAR_ICACHE_REFILLS bool -# On the R10000 up to version 2.6 (not sure about 2.7) there is a bug that -# may cause ll / sc and lld / scd sequences to execute non-atomically. +# R10000 CPUs before revision 3.0 may execute ll/sc and lld/scd sequences +# non-atomically. Platforms that may have such a CPU select this, and the +# workaround is dropped at boot when no such CPU is present. config WAR_R10000_LLSC bool
arch/mips/include/asm/asm.h +5 -2
@@ -335,10 +335,13 @@ symbol = value/* * Using a branch-likely instruction to check the result of an sc instruction * works around a bug present in R10000 CPUs prior to revision 3.0 that could -* cause ll-sc sequences to execute non-atomically. +* cause ll-sc sequences to execute non-atomically. The address of each such +* branch is recorded so that it can be turned into a beqz at boot when no +* affected CPU is present. */ #ifdef CONFIG_WAR_R10000_LLSC -# define SC_BEQZ beqzl +# define SC_BEQZ .pushsection __r10k_llsc, "a"; PTR_WD 9999f; \ + .popsection; 9999: beqzl#elif !defined(CONFIG_CC_HAS_BROKEN_INLINE_COMPAT_BRANCH ) &&MIPS_ISA_REV >=6 # define SC_BEQZ beqzc#else
arch/mips/include/asm/r10k-llsc.h new file +29 -0
@@ -0,0 +1,29 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_R10K_LLSC_H +#define _ASM_R10K_LLSC_H + +#include <linux/types.h> + +#ifdef CONFIG_WAR_R10000_LLSC +extern bool r10k_llsc_war; +extern const unsigned long __start___r10k_llsc[], __stop___r10k_llsc[]; + +void r10k_llsc_note_cpu (unsigned int prid); +void r10k_llsc_init (void ); +void r10k_llsc_check_secondary (void ); +unsigned int r10k_llsc_patch (const unsigned long *start, +const unsigned long *end); +#else +#define r10k_llsc_war false + +static inline void r10k_llsc_note_cpu (unsigned int prid) { } +static inline void r10k_llsc_init (void ) { } +static inline void r10k_llsc_check_secondary (void ) { } +static inline unsigned int r10k_llsc_patch (const unsigned long *start, +const unsigned long *end) +{ +return 0 ; +} +#endif + +#endif /* _ASM_R10K_LLSC_H */
arch/mips/kernel/Makefile +1 -0
@@ -52,6 +52,7 @@ obj-$(CONFIG_CPU_R2300_FPU) += r2300_fpu.o obj-$(CONFIG_CPU_R4K_FPU) += r4k_fpu.o obj-$(CONFIG_SMP) += smp.o +obj-$(CONFIG_WAR_R10000_LLSC) += r10k-llsc.o obj-$(CONFIG_SMP_UP) += smp-up.o obj-$(CONFIG_CPU_BMIPS) += smp-bmips.o bmips_vec.o bmips_5xxx_init.o
arch/mips/kernel/module.c +5 -0
@@ -20,6 +20,7 @@#include <linux/spinlock.h> #include <linux/jump_label.h> #include <asm/jump_label.h> +#include <asm/r10k-llsc.h> struct mips_hi16 {struct mips_hi16 *next;
@@ -423,6 +424,10 @@ int module_finalize(const Elf_Ehdr *hdr,INIT_LIST_HEAD (&me->arch.dbe_list);for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) { +if (IS_ENABLED (CONFIG_WAR_R10000_LLSC ) && + !strcmp ("__r10k_llsc" , secstrings + s->sh_name)) +r10k_llsc_patch ((void *)s->sh_addr, + (void *)s->sh_addr + s->sh_size);if (strcmp ("__dbe_table" , secstrings + s->sh_name) !=0 )continue ; me->arch.dbe_start = (void *)s->sh_addr;
arch/mips/kernel/r10k-llsc.c new file +111 -0
111 lines not on this page · raw message
arch/mips/kernel/setup.c +2 -0
@@ -46,6 +46,7 @@#include <asm/smp-ops.h> #include <asm/mips-cps.h> #include <asm/prom.h> +#include <asm/r10k-llsc.h> #include <asm/fw/fw.h> #ifdef CONFIG_MIPS_ELF_APPENDED_DTB
@@ -783,6 +784,7 @@ void __init setup_arch(char **cmdline_p)prefill_possible_map ();cpu_cache_init (); +r10k_llsc_init ();pagetable_init ();memblock_dump_all ();
arch/mips/kernel/smp.c +2 -0
@@ -33,6 +33,7 @@#include <asm/processor.h> #include <asm/idle.h> #include <asm/r4k-timer.h> +#include <asm/r10k-llsc.h> #include <asm/mips-cps.h> #include <asm/mmu_context.h> #include <asm/time.h>
@@ -360,6 +361,7 @@ asmlinkage void start_secondary(void)unsigned int cpu =raw_smp_processor_id ();cpu_probe (); +r10k_llsc_check_secondary ();per_cpu_trap_init (false);rcutree_report_cpu_starting (cpu);mips_clockevent_init ();
arch/mips/kernel/vmlinux.lds.S +8 -0
@@ -128,6 +128,14 @@ SECTIONS __mips_machines_end = .; } +#ifdef CONFIG_WAR_R10000_LLSC + __r10k_llsc : AT(ADDR(__r10k_llsc) - LOAD_OFFSET) { + __start___r10k_llsc = .; + KEEP(*(__r10k_llsc)) + __stop___r10k_llsc = .; + } +#endif + /* .exit.text is discarded at runtime, not link time, to deal with * references from .rodata */
Signature · 1 line
2.47.3
PATCH 3/5 MIPS: SGI-IP27: Report all CPUs to the R10000 LL/SC workaround
In reply to Imre KalozPass the PRId of every enabled CPU in KLCONFIG to r10k_llsc_note_cpu(), so that an affected CPU on any node keeps the workaround.
--- arch/mips/sgi-ip27/ip27-smp.c | 2 ++ 1 file changed, 2 insertions(+)
arch/mips/sgi-ip27/ip27-smp.c +2 -0
@@ -15,6 +15,7 @@#include <asm/page.h> #include <asm/processor.h> #include <asm/ptrace.h> +#include <asm/r10k-llsc.h> #include <asm/sn/agent.h> #include <asm/sn/arch.h> #include <asm/sn/gda.h>
@@ -49,6 +50,7 @@ static int node_scan_cpus(nasid_t nasid, int highest)cputoslice (cpus_found) = acpu->cpu_info.physid; sn_cpu_info[cpus_found].p_speed = acpu->cpu_speed; +r10k_llsc_note_cpu (acpu->cpu_prid); cpus_found++; } acpu = (klcpu_t *)find_component(brd, (klinfo_t *)acpu,
Signature · 1 line
2.47.3
PATCH 4/5 MIPS: tlbex: Follow the boot-time R10000 LL/SC choice
In reply to Imre KalozThe TLB handlers are generated after the boot-time R10000 LL/SC choice. Emit the branch-likely in their ll/sc loop only when the workaround is in use.
--- arch/mips/mm/tlbex.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
arch/mips/mm/tlbex.c +2 -1
@@ -35,6 +35,7 @@#include <asm/mipsregs.h> #include <asm/mmu_context.h> #include <asm/regdef.h> +#include <asm/r10k-llsc.h> #include <asm/uasm.h> #include <asm/setup.h> #include <asm/tlbex.h>
@@ -95,7 +96,7 @@ static inline int __maybe_unused bcm1250_m3_war(void)static inline int __maybe_unused r10000_llsc_war (void ) { -return IS_ENABLED (CONFIG_WAR_R10000_LLSC ); +return r10k_llsc_war; }static int use_bbit_insns (void )
Signature · 1 line
2.47.3
PATCH 5/5 bpf, mips: Follow the boot-time R10000 LL/SC choice
In reply to Imre KalozPrograms are JITed after the R10000 LL/SC workaround has been selected at boot. Emit the branch-likely in the atomic and cmpxchg ll/sc loops only when the workaround is in use.
--- arch/mips/net/bpf_jit_comp.c | 5 +++-- arch/mips/net/bpf_jit_comp.h | 12 +++++++----- arch/mips/net/bpf_jit_comp32.c | 1 + arch/mips/net/bpf_jit_comp64.c | 5 +++-- 4 files changed, 14 insertions(+), 9 deletions(-)
arch/mips/net/bpf_jit_comp.c +3 -2
@@ -112,6 +112,7 @@#include <asm/cacheflush.h> #include <asm/cpu-features.h> #include <asm/isa-rev.h> +#include <asm/r10k-llsc.h> #include <asm/uasm.h> #include "bpf_jit_comp.h"
@@ -432,7 +433,7 @@ void emit_atomic_r(struct jit_context *ctx, u8 dst, u8 src, s16 off, u8 code)break ; }emit (ctx, sc,MIPS_R_T8 , off, dst); -emit (ctx, LLSC_beqz,MIPS_R_T8 ,-16 - LLSC_offset); +emit_llsc_beqz (ctx,MIPS_R_T8 ,-16 - LLSC_offset);emit (ctx, nop);/* Delay slot */ if (code &BPF_FETCH ) {
@@ -449,7 +450,7 @@ void emit_cmpxchg_r(struct jit_context *ctx, u8 dst, u8 src, u8 res, s16 off)emit (ctx, bne,MIPS_R_T9 , res,12 );emit (ctx, move,MIPS_R_T8 , src);/* Delay slot */ emit (ctx, sc,MIPS_R_T8 , off, dst); -emit (ctx, LLSC_beqz,MIPS_R_T8 ,-20 - LLSC_offset); +emit_llsc_beqz (ctx,MIPS_R_T8 ,-20 - LLSC_offset);emit (ctx, move, res,MIPS_R_T9 );/* Delay slot */ clobber_reg (ctx, res); }
arch/mips/net/bpf_jit_comp.h +7 -5
@@ -98,11 +98,13 @@ do { \#define emit (...) __emit(__VA_ARGS__)/* Workaround for R10000 ll/sc errata */ -#ifdef CONFIG_WAR_R10000_LLSC -#define LLSC_beqz beqzl -#else -#define LLSC_beqz beqz -#endif +static inline void emit_llsc_beqz (struct jit_context *ctx,u8 rs,s16 off) +{ +if (r10k_llsc_war) +emit (ctx, beqzl, rs, off); +else +emit (ctx, beqz, rs, off); +}/* Workaround for Loongson-3 ll/sc errata */ #ifdef CONFIG_CPU_LOONGSON3_WORKAROUNDS
arch/mips/net/bpf_jit_comp32.c +1 -0
@@ -18,6 +18,7 @@#include <linux/bpf.h> #include <asm/cpu-features.h> #include <asm/isa-rev.h> +#include <asm/r10k-llsc.h> #include <asm/uasm.h> #include "bpf_jit_comp.h"
arch/mips/net/bpf_jit_comp64.c +3 -2
@@ -17,6 +17,7 @@#include <linux/bpf.h> #include <asm/cpu-features.h> #include <asm/isa-rev.h> +#include <asm/r10k-llsc.h> #include <asm/uasm.h> #include "bpf_jit_comp.h"
@@ -407,7 +408,7 @@ static void emit_atomic_r64(struct jit_context *ctx,break ; }emit (ctx, scd, t2, off, dst); -emit (ctx, LLSC_beqz, t2,-16 - LLSC_offset); +emit_llsc_beqz (ctx, t2,-16 - LLSC_offset);emit (ctx, nop);/* Delay slot */ if (code &BPF_FETCH ) {
@@ -428,7 +429,7 @@ static void emit_cmpxchg_r64(struct jit_context *ctx, u8 dst, u8 src, s16 off)emit (ctx, bne, t1, r0,12 );emit (ctx, move, t2, src);/* Delay slot */ emit (ctx, scd, t2, off, dst); -emit (ctx, LLSC_beqz, t2,-20 - LLSC_offset); +emit_llsc_beqz (ctx, t2,-20 - LLSC_offset);emit (ctx, move, r0, t1);/* Delay slot */ clobber_reg (ctx, r0);
Signature · 1 line
2.47.3