public inbox for linux-riscv@lists.infradead.org
 help / color / mirror / Atom feed
From: Andy Chiu <andybnac@gmail.com>
To: linux-riscv@lists.infradead.org, palmer@dabbelt.com
Cc: Andy Chiu <andy.chiu@sifive.com>, Andy Chiu <andybnac@gmail.com>,
	linux-kernel@vger.kernel.org, Alexandre Ghiti <alex@ghiti.fr>,
	bjorn@kernel.org, alexghiti@rivosinc.com,
	paul.walmsley@sifive.com, greentime.hu@sifive.com,
	nick.hu@sifive.com, nylon.chen@sifive.com, eric.lin@sifive.com,
	vincent.chen@sifive.com, zong.li@sifive.com,
	yongxuan.wang@sifive.com, samuel.holland@sifive.com
Subject: [PATCH v2] riscv: vector: treat VS_INITIAL as discard
Date: Wed,  1 Apr 2026 23:34:14 -0500	[thread overview]
Message-ID: <20260402043414.2421916-2-andybnac@gmail.com> (raw)
In-Reply-To: <20260402043414.2421916-1-andybnac@gmail.com>

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

      reply	other threads:[~2026-04-02  4:34 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-02  4:34 [PATCH v2 0/1] Optimize Vector context restore on syscall Andy Chiu
2026-04-02  4:34 ` Andy Chiu [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260402043414.2421916-2-andybnac@gmail.com \
    --to=andybnac@gmail.com \
    --cc=alex@ghiti.fr \
    --cc=alexghiti@rivosinc.com \
    --cc=andy.chiu@sifive.com \
    --cc=bjorn@kernel.org \
    --cc=eric.lin@sifive.com \
    --cc=greentime.hu@sifive.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-riscv@lists.infradead.org \
    --cc=nick.hu@sifive.com \
    --cc=nylon.chen@sifive.com \
    --cc=palmer@dabbelt.com \
    --cc=paul.walmsley@sifive.com \
    --cc=samuel.holland@sifive.com \
    --cc=vincent.chen@sifive.com \
    --cc=yongxuan.wang@sifive.com \
    --cc=zong.li@sifive.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox