riscv: support kernel-mode Vector
Subsystems (2)
v4 cover riscv: support kernel-mode Vector
This series provides support for running Vector code in kernel mode. Along with the suport, we add some Vector optimized routines. And provide a simple threshold to decide when to run the vectorized functions.
We decided to split the patch series into 2 parts. The first part contains running kernel-mode Vector with preemption disabled. The second part will add support for running Vector with preemption.
This patch is tested on a QEMU with V and verified that booting, normal userspace operations all work as usual with thresholds set to 0.
v3 of this series can be found at [1]
Patch summary (on current order):
- Updated patches: 1, 4
- New patch: 2, 5, 6
- Unchanged patch: 3
- Deleted patch: Patches related to preempt_v will be sent separately
Changelog v4:
- Rebase on top of riscv for-next (6.7-rc1)
- Use kernel_v_flags and helpers to track vector context.
- Prevent softirq from nesting V context for non-preempt V
- Add user copy and mem* routines
Changelog v3:
- Rebase on top of riscv for-next (6.6-rc1)
- Fix a build issue (Conor)
- Guard vstate_save, vstate_restore with {get,put}_cpu_vector_context.
- Save V context after disabling preemption. (Guo)
- Remove irqs_disabled() check from may_use_simd(). (Björn)
- Comment about nesting V context.
Changelog v2:
- fix build issues
- Follow arm's way of starting kernel-mode simd code:
- add include/asm/simd.h and rename may_use_vector() ->
may_use_simd()
- return void in kernel_vector_begin(), and BUG_ON if may_use_simd() fails
- Change naming scheme for functions/macros (Conor):
- remove KMV
- 's/rvv/vector/'
- 's/RISCV_ISA_V_PREEMPTIVE_KMV/RISCV_ISA_V_PREEMPTIVE/'
- 's/TIF_RISCV_V_KMV/TIF_RISCV_V_KERNEL_MODE/'
Andy Chiu (4):
riscv: vector: make Vector always available for softirq context
riscv: sched: defer restoring Vector context for user
riscv: lib: vectorize copy_to_user/copy_from_user
riscv: lib: add vectorized mem* routines
Greentime Hu (2):
riscv: Add support for kernel mode vector
riscv: Add vector extension XOR implementation
arch/riscv/include/asm/entry-common.h | 17 +++++ arch/riscv/include/asm/processor.h | 15 +++- arch/riscv/include/asm/simd.h | 46 +++++++++++ arch/riscv/include/asm/thread_info.h | 2 + arch/riscv/include/asm/vector.h | 32 +++++++- arch/riscv/include/asm/xor.h | 82 ++++++++++++++++++++ arch/riscv/kernel/Makefile | 1 + arch/riscv/kernel/kernel_mode_vector.c | 101 +++++++++++++++++++++++++ arch/riscv/kernel/process.c | 4 +- arch/riscv/kernel/ptrace.c | 5 +- arch/riscv/kernel/signal.c | 5 +- arch/riscv/kernel/vector.c | 2 +- arch/riscv/lib/Makefile | 6 ++ arch/riscv/lib/memcpy_vector.S | 29 +++++++ arch/riscv/lib/memmove_vector.S | 49 ++++++++++++ arch/riscv/lib/memset.S | 2 +- arch/riscv/lib/memset_vector.S | 33 ++++++++ arch/riscv/lib/riscv_v_helpers.c | 59 +++++++++++++++ arch/riscv/lib/uaccess.S | 11 +++ arch/riscv/lib/uaccess_vector.S | 55 ++++++++++++++ arch/riscv/lib/xor.S | 81 ++++++++++++++++++++ 21 files changed, 630 insertions(+), 7 deletions(-) create mode 100644 arch/riscv/include/asm/simd.h create mode 100644 arch/riscv/include/asm/xor.h create mode 100644 arch/riscv/kernel/kernel_mode_vector.c create mode 100644 arch/riscv/lib/memcpy_vector.S create mode 100644 arch/riscv/lib/memmove_vector.S create mode 100644 arch/riscv/lib/memset_vector.S create mode 100644 arch/riscv/lib/riscv_v_helpers.c create mode 100644 arch/riscv/lib/uaccess_vector.S create mode 100644 arch/riscv/lib/xor.S
Signature · 7 lines
2.17.1 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
PATCH v4 1/6 riscv: Add support for kernel mode vector
From: Greentime Hu <[email protected]>
Add kernel_vector_begin() and kernel_vector_end() function declarations and corresponding definitions in kernel_mode_vector.c
These are needed to wrap uses of vector in kernel mode.
---
Changelog v4:
- Use kernel_v_flags and helpers to track vector context. Thus remove A-b from Cornor.
Changelog v3:
- Reorder patch 1 to patch 3 to make use of {get,put}_cpu_vector_context later.
- Export {get,put}_cpu_vector_context.
- Save V context after disabling preemption. (Guo)
- Fix a build fail. (Conor)
- Remove irqs_disabled() check as it is not needed, fix styling. (Björn)
Changelog v2:
- 's/kernel_rvv/kernel_vector' and return void in kernel_vector_begin (Conor)
- export may_use_simd to include/asm/simd.h
--- arch/riscv/include/asm/processor.h | 15 +++- arch/riscv/include/asm/simd.h | 42 ++++++++++++ arch/riscv/include/asm/vector.h | 21 ++++++ arch/riscv/kernel/Makefile | 1 + arch/riscv/kernel/kernel_mode_vector.c | 95 ++++++++++++++++++++++++++ arch/riscv/kernel/process.c | 2 +- 6 files changed, 174 insertions(+), 2 deletions(-) create mode 100644 arch/riscv/include/asm/simd.h create mode 100644 arch/riscv/kernel/kernel_mode_vector.c
arch/riscv/include/asm/processor.h +14 -1
@@ -72,6 +72,18 @@struct task_struct ;struct pt_regs ; +/* +* We use a flag to track in-kernel Vector context. Currently the flag has the +* following meaning: +* +* - bit 0 indicates whether the in-kernel Vector context is active. The +* activation of this state disables the preemption. +*/ + +#define RISCV_KERNEL_MODE_V_MASK 0x1 + +#define RISCV_KERNEL_MODE_V 0x1 +/* CPU-specific state of a task */ struct thread_struct {/* Callee-saved registers */
@@ -80,7 +92,8 @@ struct thread_struct {unsigned long s[12 ];/* s[0]: frame pointer */ struct __riscv_d_ext_state fstate;unsigned long bad_cause; -unsigned long vstate_ctrl; +u32 riscv_v_flags; +u32 vstate_ctrl;struct __riscv_v_ext_state vstate; };
arch/riscv/include/asm/simd.h new file +42 -0
42 lines not on this page · raw message
arch/riscv/include/asm/vector.h +21 -0
@@ -22,6 +22,27 @@extern unsigned long riscv_v_vsize;int riscv_v_setup_vsize (void );bool riscv_v_first_use_handler (struct pt_regs *regs); +void kernel_vector_begin (void ); +void kernel_vector_end (void ); +void get_cpu_vector_context (void ); +void put_cpu_vector_context (void ); + +static inline void riscv_v_ctx_cnt_add (u32 offset) +{ + current->thread.riscv_v_flags += offset; +barrier (); +} + +static inline void riscv_v_ctx_cnt_sub (u32 offset) +{ +barrier (); + current->thread.riscv_v_flags -= offset; +} + +static inline u32 riscv_v_ctx_cnt (void ) +{ +return READ_ONCE (current->thread.riscv_v_flags); +} static __always_inline bool has_vector(void) {
arch/riscv/kernel/Makefile +1 -0
@@ -62,6 +62,7 @@ obj-$(CONFIG_MMU) += vdso.o vdso/ obj-$(CONFIG_RISCV_M_MODE) += traps_misaligned.o obj-$(CONFIG_FPU) += fpu.o obj-$(CONFIG_RISCV_ISA_V) += vector.o +obj-$(CONFIG_RISCV_ISA_V) += kernel_mode_vector.o obj-$(CONFIG_SMP) += smpboot.o obj-$(CONFIG_SMP) += smp.o obj-$(CONFIG_SMP) += cpu_ops.o
arch/riscv/kernel/kernel_mode_vector.c new file +95 -0
95 lines not on this page · raw message
arch/riscv/kernel/process.c +1 -1
@@ -169,7 +169,6 @@ int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src) *dst = *src;/* clear entire V context, including datap for a new task */ memset (&dst->thread.vstate,0 ,sizeof (struct __riscv_v_ext_state )); -return 0 ; }
@@ -203,6 +202,7 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) childregs->a0 =0 ;/* Return value of fork() */ p->thread.s[0 ] =0 ; } + p->thread.riscv_v_flags =0 ; p->thread.ra = (unsigned long )ret_from_fork; p->thread.sp = (unsigned long )childregs;/* kernel sp */ return 0 ;
Signature · 7 lines
2.17.1 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
PATCH v4 2/6 riscv: vector: make Vector always available for softirq context
In reply to Andy ChiuBy disabling bottom halves in active kerne-mode Vector, softirq will not be able to nest on top of any kernel-mode Vector.
After this patch, Vector context cannot start with irqs disabled. Otherwise local_bh_enable() may run in a wrong context.
Disabling bh is not enough for RT-kernel to prevent preeemption. So we must disable preemption, which also implies disabling bh on RT.
Related-to: commit 696207d4258b ("arm64/sve: Make kernel FPU protection RT friendly")
Related-to: commit 66c3ec5a7120 ("arm64: neon: Forbid when irqs are disabled")
---
Changelog v4:
- new patch since v4
--- arch/riscv/include/asm/simd.h | 6 +++++- arch/riscv/kernel/kernel_mode_vector.c | 10 ++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-)
arch/riscv/include/asm/simd.h +5 -1
@@ -26,8 +26,12 @@ static __must_check inline bool may_use_simd(void)/* * RISCV_KERNEL_MODE_V is only set while preemption is disabled, * and is clear whenever preemption is enabled. +* +* Kernel-mode Vector temperarily disables bh. So we must not return +* true on irq_disabled(). Otherwise we would fail the lockdep check +* calling local_bh_enable() */ - return !in_hardirq() && !in_nmi() && !(riscv_v_ctx_cnt() & RISCV_KERNEL_MODE_V_MASK); + return !in_hardirq() && !in_nmi() && !irqs_disabled() && !(riscv_v_ctx_cnt() & RISCV_KERNEL_MODE_V_MASK); }#else /* ! CONFIG_RISCV_ISA_V */
arch/riscv/kernel/kernel_mode_vector.c +8 -2
@@ -23,7 +23,10 @@*/ void get_cpu_vector_context (void ) { -preempt_disable (); +if (!IS_ENABLED (CONFIG_PREEMPT_RT )) +local_bh_disable (); +else +preempt_disable ();WARN_ON (riscv_v_ctx_cnt () &RISCV_KERNEL_MODE_V_MASK );riscv_v_ctx_cnt_add (RISCV_KERNEL_MODE_V );
@@ -41,7 +44,10 @@ void put_cpu_vector_context(void)WARN_ON (!(riscv_v_ctx_cnt () &RISCV_KERNEL_MODE_V_MASK ));riscv_v_ctx_cnt_sub (RISCV_KERNEL_MODE_V ); -preempt_enable (); +if (!IS_ENABLED (CONFIG_PREEMPT_RT )) +local_bh_enable (); +else +preempt_enable (); }/*
Signature · 7 lines
2.17.1 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
PATCH v4 3/6 riscv: Add vector extension XOR implementation
In reply to Andy ChiuFrom: Greentime Hu <[email protected]>
This patch adds support for vector optimized XOR and it is tested in qemu.
---
Changelog v2:
- 's/rvv/vector/' (Conor)
--- arch/riscv/include/asm/xor.h | 82 ++++++++++++++++++++++++++++++++++++ arch/riscv/lib/Makefile | 1 + arch/riscv/lib/xor.S | 81 +++++++++++++++++++++++++++++++++++ 3 files changed, 164 insertions(+) create mode 100644 arch/riscv/include/asm/xor.h create mode 100644 arch/riscv/lib/xor.S
arch/riscv/include/asm/xor.h new file +82 -0
@@ -0,0 +1,82 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* +* Copyright (C) 2021 SiFive +*/ + +#include <linux/hardirq.h> +#include <asm-generic/xor.h> +#ifdef CONFIG_RISCV_ISA_V +#include <asm/vector.h> +#include <asm/switch_to.h> + +void xor_regs_2_ (unsigned long bytes,unsigned long *__restrict p1, +const unsigned long *__restrict p2); +void xor_regs_3_ (unsigned long bytes,unsigned long *__restrict p1, +const unsigned long *__restrict p2, +const unsigned long *__restrict p3); +void xor_regs_4_ (unsigned long bytes,unsigned long *__restrict p1, +const unsigned long *__restrict p2, +const unsigned long *__restrict p3, +const unsigned long *__restrict p4); +void xor_regs_5_ (unsigned long bytes,unsigned long *__restrict p1, +const unsigned long *__restrict p2, +const unsigned long *__restrict p3, +const unsigned long *__restrict p4, +const unsigned long *__restrict p5); + +static void xor_vector_2 (unsigned long bytes,unsigned long *__restrict p1, +const unsigned long *__restrict p2) +{ +kernel_vector_begin (); +xor_regs_2_ (bytes, p1, p2); +kernel_vector_end (); +} + +static void xor_vector_3 (unsigned long bytes,unsigned long *__restrict p1, +const unsigned long *__restrict p2, +const unsigned long *__restrict p3) +{ +kernel_vector_begin (); +xor_regs_3_ (bytes, p1, p2, p3); +kernel_vector_end (); +} + +static void xor_vector_4 (unsigned long bytes,unsigned long *__restrict p1, +const unsigned long *__restrict p2, +const unsigned long *__restrict p3, +const unsigned long *__restrict p4) +{ +kernel_vector_begin (); +xor_regs_4_ (bytes, p1, p2, p3, p4); +kernel_vector_end (); +} + +static void xor_vector_5 (unsigned long bytes,unsigned long *__restrict p1, +const unsigned long *__restrict p2, +const unsigned long *__restrict p3, +const unsigned long *__restrict p4, +const unsigned long *__restrict p5) +{ +kernel_vector_begin (); +xor_regs_5_ (bytes, p1, p2, p3, p4, p5); +kernel_vector_end (); +} + +static struct xor_block_template xor_block_rvv = { + .name ="rvv" , + .do_2 = xor_vector_2, + .do_3 = xor_vector_3, + .do_4 = xor_vector_4, + .do_5 = xor_vector_5 +}; + +#undef XOR_TRY_TEMPLATES +#define XOR_TRY_TEMPLATES \ + do { \ + xor_speed(&xor_block_8regs); \ + xor_speed(&xor_block_32regs); \ + if (has_vector()) { \ + xor_speed(&xor_block_rvv);\ + } \ + } while (0) +#endif
arch/riscv/lib/Makefile +1 -0
@@ -11,3 +11,4 @@ lib-$(CONFIG_64BIT) += tishift.o lib-$(CONFIG_RISCV_ISA_ZICBOZ) += clear_page.o obj-$(CONFIG_FUNCTION_ERROR_INJECTION) += error-inject.o +lib-$(CONFIG_RISCV_ISA_V) += xor.o
arch/riscv/lib/xor.S new file +81 -0
81 lines not on this page · raw message
Signature · 7 lines
2.17.1 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
PATCH v4 4/6 riscv: sched: defer restoring Vector context for user
In reply to Andy ChiuUser will use its Vector registers only after the kernel really returns to the userspace. So we can delay restoring Vector registers as long as we are still running in kernel mode. So, add a thread flag to indicates the need of restoring Vector and do the restore at the last arch-specific exit-to-user hook. This save the context restoring cost when we switch over multiple processes that run V in kernel mode. For example, if the kernel performs a context swicth from A->B->C, and returns to C's userspace, then there is no need to restore B's V-register.
Besides, this also prevents us from repeatedly restoring V context when executing kernel-mode Vector multiple times.
The cost of this is that we must disable preemption and mark vector as busy during vstate_{save,restore}. Because then the V context will not get restored back immediately when a trap-causing context switch happens in the middle of vstate_{save,restore}.
---
Changelog v4:
- fix typos and re-add Conor's A-b.
Changelog v3:
- Guard {get,put}_cpu_vector_context between vstate_* operation and explain it in the commit msg.
- Drop R-b from Björn and A-b from Conor.
Changelog v2:
- rename and add comment for the new thread flag (Conor)
--- arch/riscv/include/asm/entry-common.h | 17 +++++++++++++++++ arch/riscv/include/asm/thread_info.h | 2 ++ arch/riscv/include/asm/vector.h | 11 ++++++++++- arch/riscv/kernel/kernel_mode_vector.c | 2 +- arch/riscv/kernel/process.c | 2 ++ arch/riscv/kernel/ptrace.c | 5 ++++- arch/riscv/kernel/signal.c | 5 ++++- arch/riscv/kernel/vector.c | 2 +- 8 files changed, 41 insertions(+), 5 deletions(-)
arch/riscv/include/asm/entry-common.h +17 -0
@@ -4,6 +4,23 @@#define _ASM_RISCV_ENTRY_COMMON_H#include <asm/stacktrace.h> +#include <asm/thread_info.h> +#include <asm/vector.h> + +static inline void arch_exit_to_user_mode_prepare (struct pt_regs *regs, +unsigned long ti_work) +{ +if (ti_work & _TIF_RISCV_V_DEFER_RESTORE) { +clear_thread_flag (TIF_RISCV_V_DEFER_RESTORE ); +/* +* We are already called with irq disabled, so go without +* keeping track of vector_context_busy. +*/ +riscv_v_vstate_restore (current, regs); + } +} + +#define arch_exit_to_user_mode_prepare arch_exit_to_user_mode_preparevoid handle_page_fault (struct pt_regs *regs);void handle_break (struct pt_regs *regs);
arch/riscv/include/asm/thread_info.h +2 -0
@@ -93,12 +93,14 @@ int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src);#define TIF_NOTIFY_SIGNAL 9/* signal notifications exist */ #define TIF_UPROBE 10/* uprobe breakpoint or singlestep */ #define TIF_32BIT 11/* compat-mode 32bit process */ +#define TIF_RISCV_V_DEFER_RESTORE 12/* restore Vector before returing to user */ #define _TIF_NOTIFY_RESUME (1 << TIF_NOTIFY_RESUME)#define _TIF_SIGPENDING (1 << TIF_SIGPENDING)#define _TIF_NEED_RESCHED (1 << TIF_NEED_RESCHED)#define _TIF_NOTIFY_SIGNAL (1 << TIF_NOTIFY_SIGNAL)#define _TIF_UPROBE (1 << TIF_UPROBE) +#define _TIF_RISCV_V_DEFER_RESTORE (1 << TIF_RISCV_V_DEFER_RESTORE)#define _TIF_WORK_MASK \ (_TIF_NOTIFY_RESUME | _TIF_SIGPENDING | _TIF_NEED_RESCHED | \
arch/riscv/include/asm/vector.h +10 -1
@@ -205,6 +205,15 @@ static inline void riscv_v_vstate_restore(struct task_struct *task, } } +static inline void riscv_v_vstate_set_restore (struct task_struct *task, +struct pt_regs *regs) +{ +if ((regs->status &SR_VS ) !=SR_VS_OFF ) { +set_tsk_thread_flag (task,TIF_RISCV_V_DEFER_RESTORE ); +riscv_v_vstate_on (regs); + } +} +static inline void __switch_to_vector (struct task_struct *prev,struct task_struct *next) {
@@ -212,7 +221,7 @@ static inline void __switch_to_vector(struct task_struct *prev, regs =task_pt_regs (prev);riscv_v_vstate_save (prev, regs); -riscv_v_vstate_restore (next,task_pt_regs (next)); +riscv_v_vstate_set_restore (next,task_pt_regs (next)); }void riscv_v_vstate_ctrl_init (struct task_struct *tsk);
arch/riscv/kernel/kernel_mode_vector.c +1 -1
@@ -92,7 +92,7 @@ void kernel_vector_end(void)if (WARN_ON (!has_vector ()))return ; -riscv_v_vstate_restore (current,task_pt_regs (current)); +riscv_v_vstate_set_restore (current,task_pt_regs (current));riscv_v_disable ();
arch/riscv/kernel/process.c +2 -0
@@ -153,6 +153,7 @@ void flush_thread(void)riscv_v_vstate_off (task_pt_regs (current));kfree (current->thread.vstate.datap);memset (¤t->thread.vstate,0 ,sizeof (struct __riscv_v_ext_state )); +clear_tsk_thread_flag (current,TIF_RISCV_V_DEFER_RESTORE );#endif }
@@ -169,6 +170,7 @@ int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src) *dst = *src;/* clear entire V context, including datap for a new task */ memset (&dst->thread.vstate,0 ,sizeof (struct __riscv_v_ext_state )); +clear_tsk_thread_flag (dst,TIF_RISCV_V_DEFER_RESTORE );return 0 ; }
arch/riscv/kernel/ptrace.c +4 -1
@@ -99,8 +99,11 @@ static int riscv_vr_get(struct task_struct *target,* Ensure the vector registers have been saved to the memory before * copying them to membuf. */ -if (target == current) +if (target == current) { +get_cpu_vector_context ();riscv_v_vstate_save (current,task_pt_regs (current)); +put_cpu_vector_context (); + } ptrace_vstate.vstart = vstate->vstart; ptrace_vstate.vl = vstate->vl;
arch/riscv/kernel/signal.c +4 -1
@@ -86,7 +86,10 @@ static long save_v_state(struct pt_regs *regs, void __user **sc_vec)/* datap is designed to be 16 byte aligned for better performance */ WARN_ON (unlikely (!IS_ALIGNED ((unsigned long )datap,16 ))); +get_cpu_vector_context ();riscv_v_vstate_save (current, regs); +put_cpu_vector_context (); +/* Copy everything of vstate but datap. */ err =__copy_to_user (&state->v_state, ¤t->thread.vstate, offsetof(struct __riscv_v_ext_state , datap));
@@ -134,7 +137,7 @@ static long __restore_v_state(struct pt_regs *regs, void __user *sc_vec)if (unlikely (err))return err; -riscv_v_vstate_restore (current, regs); +riscv_v_vstate_set_restore (current, regs);return err; }
arch/riscv/kernel/vector.c +1 -1
@@ -167,7 +167,7 @@ bool riscv_v_first_use_handler(struct pt_regs *regs)return true; }riscv_v_vstate_on (regs); -riscv_v_vstate_restore (current, regs); +riscv_v_vstate_set_restore (current, regs);return true; }
Signature · 7 lines
2.17.1 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
PATCH v4 5/6 riscv: lib: vectorize copy_to_user/copy_from_user
In reply to Andy ChiuThis patch utilizes Vector to perform copy_to_user/copy_from_user. If Vector is available and the size of copy is large enough for Vector to perform better than scalar, then direct the kernel to do Vector copies for userspace. Though the best programming practice for users is to reduce the copy, this provides a faster variant when copies are inevitable.
The optimal size for using Vector, copy_to_user_thres, is only a heuristic for now. We can add DT parsing if people feel the need of customizing it.
The exception fixup code of the __asm_vector_usercopy must fallback to the scalar one because accessing user pages might fault, and must be sleepable. Current kernel-mode Vector does not allow tasks to be preemptible, so we must disactivate Vector and perform a scalar fallback in such case.
The original implementation of Vector operations comes from
https://github.com/sifive/sifive-libc, which we agree to contribute to Linux kernel.
---
Changelog v4:
- new patch since v4
--- arch/riscv/lib/Makefile | 2 ++ arch/riscv/lib/riscv_v_helpers.c | 38 ++++++++++++++++++++++ arch/riscv/lib/uaccess.S | 11 +++++++ arch/riscv/lib/uaccess_vector.S | 55 ++++++++++++++++++++++++++++++++ 4 files changed, 106 insertions(+) create mode 100644 arch/riscv/lib/riscv_v_helpers.c create mode 100644 arch/riscv/lib/uaccess_vector.S
arch/riscv/lib/Makefile +2 -0
@@ -12,3 +12,5 @@ lib-$(CONFIG_RISCV_ISA_ZICBOZ) += clear_page.o obj-$(CONFIG_FUNCTION_ERROR_INJECTION) += error-inject.o lib-$(CONFIG_RISCV_ISA_V) += xor.o +lib-$(CONFIG_RISCV_ISA_V) += riscv_v_helpers.o +lib-$(CONFIG_RISCV_ISA_V) += uaccess_vector.o
arch/riscv/lib/riscv_v_helpers.c new file +38 -0
@@ -0,0 +1,38 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (C) 2023 SiFive +* Author: Andy Chiu <[email protected]> +*/ +#include <linux/linkage.h> +#include <asm/asm.h> + +#include <asm/vector.h> +#include <asm/simd.h> + +size_t riscv_v_usercopy_thres =768 ; +int __asm_vector_usercopy (void *dst,void *src,size_t n); +int fallback_scalar_usercopy (void *dst,void *src,size_t n); +asmlinkage int enter_vector_usercopy(void *dst, void *src, size_t n) +{ +size_t remain, copied; + +/* skip has_vector() check because it has been done by the asm */ +if (!may_use_simd ()) + goto fallback; + +kernel_vector_begin (); + remain =__asm_vector_usercopy (dst, src, n); +kernel_vector_end (); + +if (remain) { + copied = n - remain; + dst += copied; + src += copied; + goto fallback; + } + +return remain; + +fallback: +return fallback_scalar_usercopy (dst, src, n); +}
arch/riscv/lib/uaccess.S +11 -0
@@ -3,6 +3,8 @@ #include <asm/asm.h> #include <asm/asm-extable.h> #include <asm/csr.h> +#include <asm/hwcap.h> +#include <asm/alternative-macros.h> .macro fixup op reg addr lbl 100:
@@ -12,6 +14,14 @@ ENTRY(__asm_copy_to_user) ENTRY(__asm_copy_from_user) +#ifdef CONFIG_RISCV_ISA_V + ALTERNATIVE("j fallback_scalar_usercopy", "nop", 0, RISCV_ISA_EXT_v, CONFIG_RISCV_ISA_V) + la t0, riscv_v_usercopy_thres + REG_L t0, (t0) + bltu a2, t0, fallback_scalar_usercopy + tail enter_vector_usercopy +#endif +ENTRY(fallback_scalar_usercopy) /* Enable access to user memory */ li t6, SR_SUM
@@ -181,6 +191,7 @@ ENTRY(__asm_copy_from_user) csrc CSR_STATUS, t6 sub a0, t5, a0 ret +ENDPROC(fallback_scalar_usercopy) ENDPROC(__asm_copy_to_user) ENDPROC(__asm_copy_from_user) EXPORT_SYMBOL(__asm_copy_to_user)
arch/riscv/lib/uaccess_vector.S new file +55 -0
55 lines not on this page · raw message
Signature · 7 lines
2.17.1 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
PATCH v4 6/6 riscv: lib: add vectorized mem* routines
In reply to Andy ChiuProvide vectorized memcpy/memset/memmove to accelerate common memory operations. Also, group them into V_OPT_TEMPLATE3 macro because their setup/tear-down and fallback logics are the same.
The original implementation of Vector operations comes from
https://github.com/sifive/sifive-libc, which we agree to contribute to Linux kernel.
---
Changelog v4:
- new patch since v4
--- arch/riscv/lib/Makefile | 3 ++ arch/riscv/lib/memcpy_vector.S | 29 +++++++++++++++++++ arch/riscv/lib/memmove_vector.S | 49 ++++++++++++++++++++++++++++++++ arch/riscv/lib/memset.S | 2 +- arch/riscv/lib/memset_vector.S | 33 +++++++++++++++++++++ arch/riscv/lib/riscv_v_helpers.c | 21 ++++++++++++++ 6 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 arch/riscv/lib/memcpy_vector.S create mode 100644 arch/riscv/lib/memmove_vector.S create mode 100644 arch/riscv/lib/memset_vector.S
arch/riscv/lib/Makefile +3 -0
@@ -14,3 +14,6 @@ obj-$(CONFIG_FUNCTION_ERROR_INJECTION) += error-inject.o lib-$(CONFIG_RISCV_ISA_V) += xor.o lib-$(CONFIG_RISCV_ISA_V) += riscv_v_helpers.o lib-$(CONFIG_RISCV_ISA_V) += uaccess_vector.o +lib-$(CONFIG_RISCV_ISA_V) += memset_vector.o +lib-$(CONFIG_RISCV_ISA_V) += memcpy_vector.o +lib-$(CONFIG_RISCV_ISA_V) += memmove_vector.o
arch/riscv/lib/memcpy_vector.S new file +29 -0
@@ -0,0 +1,29 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#include <linux/linkage.h> +#include <asm/asm.h> + +#define pDst a0 +#define pSrc a1 +#define iNum a2 + +#define iVL a3 +#define pDstPtr a4 + +#define ELEM_LMUL_SETTING m8 +#define vData v0 + + +/* void *memcpy(void *, const void *, size_t) */ +SYM_FUNC_START(__asm_memcpy_vector) + mv pDstPtr, pDst +loop: + vsetvli iVL, iNum, e8, ELEM_LMUL_SETTING, ta, ma + vle8.v vData, (pSrc) + sub iNum, iNum, iVL + add pSrc, pSrc, iVL + vse8.v vData, (pDstPtr) + add pDstPtr, pDstPtr, iVL + bnez iNum, loop + ret +SYM_FUNC_END(__asm_memcpy_vector)
arch/riscv/lib/memmove_vector.S new file +49 -0
49 lines not on this page · raw message
arch/riscv/lib/memset.S +1 -1
@@ -110,4 +110,4 @@ WEAK(memset) bltu t0, a3, 5b 6: ret -END(__memset) +ENDPROC(__memset)
arch/riscv/lib/memset_vector.S new file +33 -0
@@ -0,0 +1,33 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#include <linux/linkage.h> +#include <asm/asm.h> + +#define pDst a0 +#define iValue a1 +#define iNum a2 + +#define iVL a3 +#define iTemp a4 +#define pDstPtr a5 + +#define ELEM_LMUL_SETTING m8 +#define vData v0 + +/* void *memset(void *, int, size_t) */ +SYM_FUNC_START(__asm_memset_vector) + + mv pDstPtr, pDst + + vsetvli iVL, iNum, e8, ELEM_LMUL_SETTING, ta, ma + vmv.v.x vData, iValue + +loop: + vse8.v vData, (pDstPtr) + sub iNum, iNum, iVL + add pDstPtr, pDstPtr, iVL + vsetvli iVL, iNum, e8, ELEM_LMUL_SETTING, ta, ma + bnez iNum, loop + + ret + +SYM_FUNC_END(__asm_memset_vector)
arch/riscv/lib/riscv_v_helpers.c +21 -0
@@ -36,3 +36,24 @@ asmlinkage int enter_vector_usercopy(void *dst, void *src, size_t n) fallback:return fallback_scalar_usercopy (dst, src, n); } + +#define V_OPT_TEMPLATE3 (prefix, type_r, type_0, type_1) \ +extern type_r __asm_##prefix##_vector(type_0, type_1, size_t n); \ +type_r prefix(type_0 a0, type_1 a1, size_t n) \ +{ \ + type_r ret; \ + if (has_vector() && may_use_simd() && n > riscv_v_##prefix##_thres) { \ + kernel_vector_begin(); \ + ret = __asm_##prefix##_vector(a0, a1, n); \ + kernel_vector_end(); \ + return ret; \ + } \ + return __##prefix(a0, a1, n); \ +} + +static size_t riscv_v_memset_thres =1280 ; +V_OPT_TEMPLATE3(memset, void *, void*, int) +static size_t riscv_v_memcpy_thres =768 ; +V_OPT_TEMPLATE3(memcpy, void *, void*, const void *) +static size_t riscv_v_memmove_thres = 512; +V_OPT_TEMPLATE3(memmove, void *, void*, const void *)
Signature · 7 lines
2.17.1 _______________________________________________ linux-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-riscv
Quoted 17 lines · Andy Chiu
This series provides support for running Vector code in kernel mode. Along with the suport, we add some Vector optimized routines. And provide a simple threshold to decide when to run the vectorized functions.
We decided to split the patch series into 2 parts. The first part contains running kernel-mode Vector with preemption disabled. The second part will add support for running Vector with preemption.
This patch is tested on a QEMU with V and verified that booting, normal userspace operations all work as usual with thresholds set to 0.
v3 of this series can be found at [1]
Quoted 8 lines
Patch summary (on current order):
- Updated patches: 1, 4
- New patch: 2, 5, 6
- Unchanged patch: 3
- Deleted patch: Patches related topreempt_vwill be sent separatelyChangelog v4:
- Rebase on top of riscv for-next (6.7-rc1)
Sorry, it's too bad that I missed this. I found that this patch is actually based on top of 6.6-rc1. I will send out a v5 later today, which will be tested and based on top of the tip. Sorry for disturbing the list.
Quoted 67 lines
- Use
kernel_v_flagsand helpers to track vector context.
- Prevent softirq from nesting V context for non-preempt V
- Add user copy and mem* routinesChangelog v3:
- Rebase on top of riscv for-next (6.6-rc1)
- Fix a build issue (Conor)
- Guardvstate_save,vstate_restorewith {get,put}_cpu_vector_context.
- Save V context after disabling preemption. (Guo)
- Removeirqs_disabled()check frommay_use_simd(). (Björn)
- Comment about nesting V context.Changelog v2:
- fix build issues
- Follow arm's way of starting kernel-mode simd code:
- addinclude/asm/simd.hand renamemay_use_vector()->may_use_simd()- return void in
kernel_vector_begin(), andBUG_ONifmay_use_simd()fails
- Change naming scheme for functions/macros (Conor):
- remove KMV
- 's/rvv/vector/'
- 's/RISCV_ISA_V_PREEMPTIVE_KMV/RISCV_ISA_V_PREEMPTIVE/'
- 's/TIF_RISCV_V_KMV/TIF_RISCV_V_KERNEL_MODE/'Andy Chiu (4):
riscv: vector: make Vector always available for softirq context
riscv: sched: defer restoring Vector context for user
riscv: lib: vectorize copy_to_user/copy_from_user
riscv: lib: add vectorized mem* routinesGreentime Hu (2):
riscv: Add support for kernel mode vector
riscv: Add vector extension XOR implementationarch/riscv/include/asm/entry-common.h | 17 +++++ arch/riscv/include/asm/processor.h | 15 +++- arch/riscv/include/asm/simd.h | 46 +++++++++++ arch/riscv/include/asm/thread_info.h | 2 + arch/riscv/include/asm/vector.h | 32 +++++++- arch/riscv/include/asm/xor.h | 82 ++++++++++++++++++++ arch/riscv/kernel/Makefile | 1 + arch/riscv/kernel/kernel_mode_vector.c | 101 +++++++++++++++++++++++++ arch/riscv/kernel/process.c | 4 +- arch/riscv/kernel/ptrace.c | 5 +- arch/riscv/kernel/signal.c | 5 +- arch/riscv/kernel/vector.c | 2 +- arch/riscv/lib/Makefile | 6 ++ arch/riscv/lib/memcpy_vector.S | 29 +++++++ arch/riscv/lib/memmove_vector.S | 49 ++++++++++++ arch/riscv/lib/memset.S | 2 +- arch/riscv/lib/memset_vector.S | 33 ++++++++ arch/riscv/lib/riscv_v_helpers.c | 59 +++++++++++++++ arch/riscv/lib/uaccess.S | 11 +++ arch/riscv/lib/uaccess_vector.S | 55 ++++++++++++++ arch/riscv/lib/xor.S | 81 ++++++++++++++++++++ 21 files changed, 630 insertions(+), 7 deletions(-) create mode 100644 arch/riscv/include/asm/simd.h create mode 100644 arch/riscv/include/asm/xor.h create mode 100644 arch/riscv/kernel/kernel_mode_vector.c create mode 100644 arch/riscv/lib/memcpy_vector.S create mode 100644 arch/riscv/lib/memmove_vector.S create mode 100644 arch/riscv/lib/memset_vector.S create mode 100644 arch/riscv/lib/riscv_v_helpers.c create mode 100644 arch/riscv/lib/uaccess_vector.S create mode 100644 arch/riscv/lib/xor.S
Quoted 3 lines
--
2.17.1
Regards,
Andy
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv