* [PATCH v2 0/1] Optimize Vector context restore on syscall
@ 2026-04-02 4:34 Andy Chiu
2026-04-02 4:34 ` [PATCH v2] riscv: vector: treat VS_INITIAL as discard Andy Chiu
0 siblings, 1 reply; 2+ messages in thread
From: Andy Chiu @ 2026-04-02 4:34 UTC (permalink / raw)
To: linux-riscv, palmer
Cc: Andy Chiu, linux-kernel, Alexandre Ghiti, bjorn, alexghiti,
paul.walmsley, greentime.hu, nick.hu, nylon.chen, eric.lin,
vincent.chen, zong.li, yongxuan.wang, samuel.holland
Accroding to the ABI, Vector registers are not preserved across a
syscall. Therefore, the kernel invalidates all V-reg at the entry of the
trap handler and marks the state dirty. This patch provide an
optimization such that the invalidation is performed only once before
returning back to the user space. Also by tracking this state,
specifically reserving VS_INITIAL for this optimized restore, we cut the
cost of saving and restoring V-reg at context switch for processes
calling syscalls.
---
Changelog v2: rebase on top of for-next
Andy Chiu (1):
riscv: vector: treat VS_INITIAL as discard
arch/riscv/include/asm/vector.h | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)
--
2.53.0
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply [flat|nested] 2+ messages in thread
* [PATCH v2] riscv: vector: treat VS_INITIAL as discard
2026-04-02 4:34 [PATCH v2 0/1] Optimize Vector context restore on syscall Andy Chiu
@ 2026-04-02 4:34 ` Andy Chiu
0 siblings, 0 replies; 2+ messages in thread
From: Andy Chiu @ 2026-04-02 4:34 UTC (permalink / raw)
To: linux-riscv, palmer
Cc: Andy Chiu, Andy Chiu, linux-kernel, Alexandre Ghiti, bjorn,
alexghiti, paul.walmsley, greentime.hu, nick.hu, nylon.chen,
eric.lin, vincent.chen, zong.li, yongxuan.wang, samuel.holland
From: Andy Chiu <andy.chiu@sifive.com>
The purpose of riscv_v_vstate_discard() is to invalidate v context at
entries of syscalls. So users happen to use v after a syscall without
re-configuring would see a failure. It was achieved by setting vector
registers and CSRs to -1 and marking the context busy. However, this
results in redundant saving of v-context if the process is scheduled out
in a syscall. Moreover, restoring the invalidated context from memory is
a costly operation. In fact, all can be prevented if we can delay
vstate_discard before returning back to the user space. To be more
specific, the kernel can mark v-context as INITIAL and set the restore
flag at syscall entries. This is the indication for the vstate_restore,
so it awares that the vstate has to be invalidated before returning back
to the user space.
After applying this patch, the context switch performance has improved
6.78% on vector enabled lmbench running on a FPGA with VLEN=512. The
result was obtained by averaging the output from the following command.
$ lat_ctx 2
Before the patch: 599.8357692
After the patch: 559.1748148
Signed-off-by: Andy Chiu <andy.chiu@sifive.com>
Signed-off-by: Andy Chiu <andybnac@gmail.com>
---
arch/riscv/include/asm/vector.h | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)
diff --git a/arch/riscv/include/asm/vector.h b/arch/riscv/include/asm/vector.h
index 00cb9c0982b1..90f77e511cad 100644
--- a/arch/riscv/include/asm/vector.h
+++ b/arch/riscv/include/asm/vector.h
@@ -298,8 +298,8 @@ static inline void __riscv_v_vstate_discard(void)
static inline void riscv_v_vstate_discard(struct pt_regs *regs)
{
if (riscv_v_vstate_query(regs)) {
- __riscv_v_vstate_discard();
- __riscv_v_vstate_dirty(regs);
+ set_tsk_thread_flag(current, TIF_RISCV_V_DEFER_RESTORE);
+ riscv_v_vstate_on(regs);
}
}
@@ -315,7 +315,9 @@ static inline void riscv_v_vstate_save(struct __riscv_v_ext_state *vstate,
static inline void riscv_v_vstate_restore(struct __riscv_v_ext_state *vstate,
struct pt_regs *regs)
{
- if (riscv_v_vstate_query(regs)) {
+ if (__riscv_v_vstate_check(regs->status, INITIAL)) {
+ __riscv_v_vstate_discard();
+ } else if (riscv_v_vstate_query(regs)) {
__riscv_v_vstate_restore(vstate, vstate->datap);
__riscv_v_vstate_clean(regs);
}
@@ -326,7 +328,7 @@ static inline void riscv_v_vstate_set_restore(struct task_struct *task,
{
if (riscv_v_vstate_query(regs)) {
set_tsk_thread_flag(task, TIF_RISCV_V_DEFER_RESTORE);
- riscv_v_vstate_on(regs);
+ __riscv_v_vstate_clean(regs);
}
}
--
2.53.0
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
^ permalink raw reply related [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-04-02 4:34 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-02 4:34 [PATCH v2 0/1] Optimize Vector context restore on syscall Andy Chiu
2026-04-02 4:34 ` [PATCH v2] riscv: vector: treat VS_INITIAL as discard Andy Chiu
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox