From nobody Sun Jun 14 09:54:33 2026 Received: from mail-ot1-f53.google.com (mail-ot1-f53.google.com [209.85.210.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5EF01360722 for ; Thu, 2 Apr 2026 04:34:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775104476; cv=none; b=c0BMJ45kPHqjC6SnqE1yuw6rEbC7hiCn4NV/RqFpLL7X+YlyojwwLz6+FZxUn4D5EguHoBknAEcdElwM3s3mAepN4Is5LJ5lrZwbFS965dL8tCLp3VLjexmUL/yRHcPuZl39qjSp/UMnpk2KThisB1jStv2eJAeULL6QhAz6oSE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775104476; c=relaxed/simple; bh=Hx3jVTNeHzN+8bwA+c/5Q6EUHRix/THV3IfBYJFKoxw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Fj2MRc8bnYh6BlH11BjyuCONPsFnQl/xgU0TOlnG5Mrwbkrnr52Oqi2O7ZKThigdITjeU167vwMZG/v40tvvsX2+8q+y/PxSsM0pcEl5oFhn5s2xrz6EHMgbeGo7Iq6aL2exRetP94g+YXP/bFMCzJAIs/KGhKZuZEGXqQ+G1xs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=ijFVA0L5; arc=none smtp.client-ip=209.85.210.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ijFVA0L5" Received: by mail-ot1-f53.google.com with SMTP id 46e09a7af769-7d86eb7c854so261666a34.3 for ; Wed, 01 Apr 2026 21:34:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1775104474; x=1775709274; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=dYRBDWPBDmLdbk71cyiKzWoWf8/A9NYHO+wwt8aJjzs=; b=ijFVA0L5Jof7Gv3ULdFv80TIl8A+PFVjWJzRittj5rMbhgq2uMEW+mejbJJBy9D64B ZeC/YN6LOJX+aEpioL26PnN4KUhvRtpbWf9dZ2nZcncCk/JP9t61lW9U3kEgXpyQ8YwY 8roTAms+G1rk+7OH1g64QrtpbkjFCrIwmSvc3+CeOtBHn+m5d74nrY7KHuCgvj/Rrt4M DZCwClOsrKhkEIogWwStPTWhlwi0sBrEtZF2e5NzjXzuan6pNquL3Dtw96K0qGjY5dZ6 giIIRMdVxrJnFG/nRcLJJsyMtgKsOtaTlpgS2WmROIw+zVIVBP6Pwfgq49Dafq2BDr+q N2SA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775104474; x=1775709274; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=dYRBDWPBDmLdbk71cyiKzWoWf8/A9NYHO+wwt8aJjzs=; b=QLAuUPvLQbhpNJXxAsLd5WkRWoNPydR2uutvCpEcCzEijxwFskotVpucm2c3sURqOg WqnpptmluOrd98dL+UzbJF2wzWsHrvuZiiSC5w9yNm/Cmtyzs4NCBBg4SUXUVVEC4Wge ZqjLC0v3Sy8TwXSkaQsB7Xin/2RTIwJ8dgNo9aUsZcwSRW60uzZTOaZxXVpH8wMUS4LQ Y6uqEVys6A+XXlU9Yz6+J5n8ktda8W7M6QXldh+d+1D1HLDX9FltLDsmXBpX/X/FnJla iIvosn/WUzIFwJTGczcN8UB3t7cnO3A9ktRoIsqwOp72j85SoQBItmXqNL1Q/K3SpuA/ vDhQ== X-Forwarded-Encrypted: i=1; AJvYcCW35VEbSxbWcxqtzM3qQPCZJNmEfkfja+3BcmdPQxv/MlzwBQVu4IKwAN+GE4el5H3eCx5Cbz/2mkk83hc=@vger.kernel.org X-Gm-Message-State: AOJu0YxSa5RrgfF+UKwMO5MyxWW8k8hnMju4zU8AFLCFRqVKipFVXUP5 5aZQHQvz3YlisbVwMKekiPX0Tk3Qf4p/okgHFnSugKNNRBjwii6018/d X-Gm-Gg: ATEYQzyM1lhCpBzqvDCqCmpOh2T+uElVknbkn7Nu5Z1a7hOi01mtB8QBXPK/+cJ8xF0 F3EsEqWNoz2VyD/Uu9OfTIDKgJoNJKBrOF8rp7e6myu8qmiNwAF9TkYhrNx9MKbyQCoTwkjVUnS f5oiqvnPshmbnNpEVanFUOzSRMrbt7WPkHYW5XvbOZXDMlk0Cehhimlo6CWFS0obzvc4y5E4/19 JAaqjt8NYp4JGVGO5RJ9Yc5J+yCZ/cRHDlvoV9Ph+y0YNtzQxvlG2/W4f6AEBd9BpLiULYnhQrb PUSeg4A2RcK6y61ugOR7D7FyJ21KvmPttnTbcwlueh+Ts62NdvMydbfhUTfv0d+OORxcUuXgXCz y6QQA92n3VWyakXWPoseGgP69/6KIz07niKHLLKBLG09YJqo8OTTtvFLNVyTw49geCfoa8a/UvR 3eewWtpXgeZC7wwPkH X-Received: by 2002:a05:6820:212:b0:67b:b847:f0fa with SMTP id 006d021491bc7-67fabc24b56mr3260080eaf.17.1775104474300; Wed, 01 Apr 2026 21:34:34 -0700 (PDT) Received: from hinoki ([185.255.130.248]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-680a63c7fd1sm1139558eaf.8.2026.04.01.21.34.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Apr 2026 21:34:33 -0700 (PDT) From: Andy Chiu To: linux-riscv@lists.infradead.org, palmer@dabbelt.com Cc: Andy Chiu , Andy Chiu , linux-kernel@vger.kernel.org, Alexandre Ghiti , 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 Message-ID: <20260402043414.2421916-2-andybnac@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260402043414.2421916-1-andybnac@gmail.com> References: <20260402043414.2421916-1-andybnac@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Andy Chiu 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=3D512. 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 Signed-off-by: Andy Chiu --- 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/vecto= r.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); } } =20 @@ -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 *vsta= te, 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 ta= sk_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); } } =20 --=20 2.53.0