mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Andy Chiu <andy.chiu@sifive.com>
To: Paul Walmsley <paul.walmsley@sifive.com>,
	 Palmer Dabbelt <palmer@dabbelt.com>,
	Albert Ou <aou@eecs.berkeley.edu>
Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org,
	"Björn Töpel" <bjorn@rivosinc.com>,
	"Andy Chiu" <andy.chiu@sifive.com>
Subject: [PATCH] riscv: vector: treat VS_INITIAL as discard
Date: Fri, 28 Jun 2024 19:49:12 +0800	[thread overview]
Message-ID: <20240628-dev-vstate_discard-v1-1-18e1c5d7997e@sifive.com> (raw)

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>
---
 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 be7d309cca8a..1221f8b8f564 100644
--- a/arch/riscv/include/asm/vector.h
+++ b/arch/riscv/include/asm/vector.h
@@ -170,8 +170,8 @@ static inline void riscv_v_vstate_discard(struct pt_regs *regs)
 	if ((regs->status & SR_VS) == SR_VS_OFF)
 		return;
 
-	__riscv_v_vstate_discard();
-	__riscv_v_vstate_dirty(regs);
+	set_tsk_thread_flag(current, TIF_RISCV_V_DEFER_RESTORE);
+	riscv_v_vstate_on(regs);
 }
 
 static inline void riscv_v_vstate_save(struct __riscv_v_ext_state *vstate,
@@ -186,7 +186,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 ((regs->status & SR_VS) != SR_VS_OFF) {
+	if ((regs->status & SR_VS) == SR_VS_INITIAL) {
+		__riscv_v_vstate_discard();
+	} else if ((regs->status & SR_VS) != SR_VS_OFF) {
 		__riscv_v_vstate_restore(vstate, vstate->datap);
 		__riscv_v_vstate_clean(regs);
 	}
@@ -197,7 +199,7 @@ static inline void riscv_v_vstate_set_restore(struct task_struct *task,
 {
 	if ((regs->status & SR_VS) != SR_VS_OFF) {
 		set_tsk_thread_flag(task, TIF_RISCV_V_DEFER_RESTORE);
-		riscv_v_vstate_on(regs);
+		__riscv_v_vstate_clean(regs);
 	}
 }
 

---
base-commit: 60a6707f582ebbdfb6b378f45d7bf929106a1cd5
change-id: 20240628-dev-vstate_discard-5784652968a3

Best regards,
-- 
Andy Chiu <andy.chiu@sifive.com>


                 reply	other threads:[~2024-06-28 11:49 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20240628-dev-vstate_discard-v1-1-18e1c5d7997e@sifive.com \
    --to=andy.chiu@sifive.com \
    --cc=aou@eecs.berkeley.edu \
    --cc=bjorn@rivosinc.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-riscv@lists.infradead.org \
    --cc=palmer@dabbelt.com \
    --cc=paul.walmsley@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

all inboxes | Powered by JetHome®