Skip to content

[PW_SID:1098934] riscv: optimize Vector context restore on syscall#1992

Closed
linux-riscv-bot wants to merge 4 commits into
workflow__riscv__fixesfrom
pw1098934
Closed

[PW_SID:1098934] riscv: optimize Vector context restore on syscall#1992
linux-riscv-bot wants to merge 4 commits into
workflow__riscv__fixesfrom
pw1098934

Conversation

@linux-riscv-bot
Copy link
Copy Markdown

PR for series 1098934 applied to workflow__riscv__fixes

Name: riscv: optimize Vector context restore on syscall
URL: https://patchwork.kernel.org/project/linux-riscv/list/?series=1098934
Version: 3

tchiu-TT and others added 4 commits May 21, 2026 17:03
Lift riscv_v_{enable,disable} out of __*vstate_{save,restore,discard} so
that we can reuse some functions without repeatedly turning on/off
vector.

Also, refactor and document about the user context save in preempt_v to
make code more readable.

Signed-off-by: Andy Chiu <tchiu@tenstorrent.com>
Signed-off-by: Linux RISC-V bot <linux.riscv.bot@gmail.com>
The RISC-V vector specification states that executing a system call
causes all caller-saved vector registers (v0-v31, vl, vtype) and vstart
to become unspecified.

Currently, after calling riscv_v_vstate_discard(), the vector state
may still be marked as DIRTY, which can mislead the context switch
logic into treating the registers as containing valid user data.

This patch clarifies and tightens the kernel-side semantics:

1. On syscall entry, the kernel checks the vector state via sstatus
   and explicitly set it to INIT, indicating that the vector registers
   no longer contain meaningful user data.

2. During context switch, the vector state is saved only if the state is
   DIRTY. (no change)

3. On restore, if the state is INIT, the vector registers are treated
   as invalid and are not restored from memory. Instead, they are
   overwritten with a known initial value to avoid data leaakge.

Signed-off-by: daichengrong <daichengrong@iscas.ac.cn>
Co-developed-by: Andy Chiu <tchiu@tenstorrent.com>
Signed-off-by: Andy Chiu <tchiu@tenstorrent.com>
Signed-off-by: Linux RISC-V bot <linux.riscv.bot@gmail.com>
The last patch introduced the INITIAL vector state to avoid saving and
restoring vector registers across syscall boundaries. However, this
optimization did not fully account for the ptrace and signal handling
interfaces.

As a result, two issues emerged:
1. Ptrace reads at syscall stop could observe stale, non-nulled
   registers.
2. Modifications to the ucontext through signal interface during a
   syscall stop would be overwritten by the vector discaring macro.

This patch introduces riscv_v_ucontext_save() to synchronize these
paths with the INITIAL state:

- Ptrace reads during a syscall stop now explicitly execute the hardware
  discard macro and return the discarded state to prevent data leaks.
- Ptrace writes (PTRACE_SETREGSET) during a syscall stop are silently
  dropped (returning 0). Returning an error like EINVAL would break
  debbugers like GDB, which disables the optional regset on receiving
  such error.
- Signal handling (rt_sigreturn) now honor user-space modifications to
  the vector context (for user-space thread schedulers).

CC: Sergey Matyukevich <geomatsi@gmail.com>
CC: gdb@sourceware.org
Signed-off-by: Andy Chiu <tchiu@tenstorrent.com>
Signed-off-by: Linux RISC-V bot <linux.riscv.bot@gmail.com>
Add new test cases to verify the vector state restorations at syscall
stops for ptrace and signal interfaces. Specifically:
1. Signal handler should read all ones at syscall stop and modifying
   context should success.
2. Ptrace should read all ones but any modification to NT_RISCV_VECTOR
   is silently dropped.

Signed-off-by: Andy Chiu <tchiu@tenstorrent.com>
Signed-off-by: Linux RISC-V bot <linux.riscv.bot@gmail.com>
@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 1: "[v3,1/4] riscv: vector: refactor vector context operations"
build-rv32-defconfig
Desc: Builds riscv32 defconfig
Duration: 116.36 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 1: "[v3,1/4] riscv: vector: refactor vector context operations"
build-rv64-clang-allmodconfig
Desc: Builds riscv64 allmodconfig with Clang, and checks for errors and added warnings
Duration: 1141.04 seconds
Result: ERROR
Output:

Redirect to /build/tmp.RJry7sG0qk and /build/tmp.NnFjlhh8sz
Tree base:
cd9d4217f4c7b ("Adding CI files")
Building the whole tree with the patch
Building the tree before the patch
Building the tree with the patch
New errors added:
--- /build/tmp.xfQUHdwDwa	2026-05-21 17:28:03.624200489 +0000
+++ /build/tmp.xiBm84NM0l	2026-05-21 17:28:03.626200457 +0000
@@ -117,0 +118 @@
+      1 /build/tmp7s1xjqzr/arch/riscv/kernel/kernel_mode_vector.c:126:40: warning: unused variable 'uvstate' [-Wunused-variable]
Per-file breakdown
error/warning file pre:
error/warning file post:
pre: 118 post: 119



real	15m45.248s
user	587m52.926s
sys	122m12.607s

real	1m36.922s
user	3m48.184s
sys	2m28.045s

real	1m28.949s
user	3m2.898s
sys	2m20.617s

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 1: "[v3,1/4] riscv: vector: refactor vector context operations"
build-rv64-gcc-allmodconfig
Desc: Builds riscv64 allmodconfig with GCC, and checks for errors and added warnings
Duration: 1649.37 seconds
Result: ERROR
Output:

Redirect to /build/tmp.83isEwzb53 and /build/tmp.xdhobIAMSa
Tree base:
cd9d4217f4c7b ("Adding CI files")
Building the whole tree with the patch
Building the tree before the patch
Building the tree with the patch
New errors added:
--- /build/tmp.717cMDbYCm	2026-05-21 17:55:34.534385159 +0000
+++ /build/tmp.EQfkcBdX0I	2026-05-21 17:55:34.536385126 +0000
@@ -117,0 +118 @@
+      1 /build/tmp7s1xjqzr/arch/riscv/kernel/kernel_mode_vector.c:126:47: warning: unused variable 'uvstate' [-Wunused-variable]
Per-file breakdown
error/warning file pre:
pre: 118 post: 119



real	21m34.393s
user	772m38.954s
sys	144m7.379s

real	2m55.880s
user	5m16.095s
sys	2m25.151s

real	2m49.052s
user	4m9.846s
sys	2m12.302s

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 1: "[v3,1/4] riscv: vector: refactor vector context operations"
build-rv64-nommu-k210-defconfig
Desc: Builds riscv64 defconfig with NOMMU for K210
Duration: 19.82 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 1: "[v3,1/4] riscv: vector: refactor vector context operations"
build-rv64-nommu-k210-virt
Desc: Builds riscv64 defconfig with NOMMU for the virt platform
Duration: 20.99 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 1: "[v3,1/4] riscv: vector: refactor vector context operations"
checkpatch
Desc: Runs checkpatch.pl on the patch
Duration: 1.12 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 1: "[v3,1/4] riscv: vector: refactor vector context operations"
dtb-warn-rv64
Desc: Checks for Device Tree warnings/errors
Duration: 76.73 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 1: "[v3,1/4] riscv: vector: refactor vector context operations"
header-inline
Desc: Detects static functions without inline keyword in header files
Duration: 0.24 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 1: "[v3,1/4] riscv: vector: refactor vector context operations"
kdoc
Desc: Detects for kdoc errors
Duration: 0.71 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 1: "[v3,1/4] riscv: vector: refactor vector context operations"
module-param
Desc: Detect module_param changes
Duration: 0.25 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 1: "[v3,1/4] riscv: vector: refactor vector context operations"
verify-fixes
Desc: Verifies that the Fixes: tags exist
Duration: 0.23 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 1: "[v3,1/4] riscv: vector: refactor vector context operations"
verify-signedoff
Desc: Verifies that Signed-off-by: tags are correct
Duration: 0.26 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 2: "[v3,2/4] riscv: clarify vector state semantics on syscall and context switch"
build-rv32-defconfig
Desc: Builds riscv32 defconfig
Duration: 115.63 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 2: "[v3,2/4] riscv: clarify vector state semantics on syscall and context switch"
build-rv64-clang-allmodconfig
Desc: Builds riscv64 allmodconfig with Clang, and checks for errors and added warnings
Duration: 1137.67 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 2: "[v3,2/4] riscv: clarify vector state semantics on syscall and context switch"
build-rv64-gcc-allmodconfig
Desc: Builds riscv64 allmodconfig with GCC, and checks for errors and added warnings
Duration: 1651.10 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 2: "[v3,2/4] riscv: clarify vector state semantics on syscall and context switch"
build-rv64-nommu-k210-defconfig
Desc: Builds riscv64 defconfig with NOMMU for K210
Duration: 19.24 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 2: "[v3,2/4] riscv: clarify vector state semantics on syscall and context switch"
build-rv64-nommu-k210-virt
Desc: Builds riscv64 defconfig with NOMMU for the virt platform
Duration: 20.72 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 2: "[v3,2/4] riscv: clarify vector state semantics on syscall and context switch"
checkpatch
Desc: Runs checkpatch.pl on the patch
Duration: 2.03 seconds
Result: WARNING
Output:

WARNING: Argument 'regs' is not used in function-like macro
#114: FILE: arch/riscv/include/asm/vector.h:424:
+#define riscv_v_vstate_init(regs)		do {} while (0)

total: 0 errors, 1 warnings, 0 checks, 80 lines checked

NOTE: For some of the reported defects, checkpatch may be able to
      mechanically convert to the typical style using --fix or --fix-inplace.

Commit ec7291a3f23f ("riscv: clarify vector state semantics on syscall and context switch") has style problems, please review.

NOTE: Ignored message types: ALLOC_SIZEOF_STRUCT CAMELCASE COMMIT_LOG_LONG_LINE GIT_COMMIT_ID MACRO_ARG_REUSE NO_AUTHOR_SIGN_OFF

NOTE: If any of the errors are false positives, please report
      them to the maintainer, see CHECKPATCH in MAINTAINERS.
total: 0 errors, 1 warnings, 0 checks, 80 lines checked
WARNING: Argument 'regs' is not used in function-like macro


@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 2: "[v3,2/4] riscv: clarify vector state semantics on syscall and context switch"
dtb-warn-rv64
Desc: Checks for Device Tree warnings/errors
Duration: 76.35 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 2: "[v3,2/4] riscv: clarify vector state semantics on syscall and context switch"
header-inline
Desc: Detects static functions without inline keyword in header files
Duration: 0.23 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 2: "[v3,2/4] riscv: clarify vector state semantics on syscall and context switch"
kdoc
Desc: Detects for kdoc errors
Duration: 1.20 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 2: "[v3,2/4] riscv: clarify vector state semantics on syscall and context switch"
module-param
Desc: Detect module_param changes
Duration: 0.24 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 2: "[v3,2/4] riscv: clarify vector state semantics on syscall and context switch"
verify-fixes
Desc: Verifies that the Fixes: tags exist
Duration: 0.22 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 2: "[v3,2/4] riscv: clarify vector state semantics on syscall and context switch"
verify-signedoff
Desc: Verifies that Signed-off-by: tags are correct
Duration: 0.26 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 3: "[v3,3/4] riscv: vector: adjust ptrace and signal behavior for INITIAL state"
build-rv32-defconfig
Desc: Builds riscv32 defconfig
Duration: 117.64 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 3: "[v3,3/4] riscv: vector: adjust ptrace and signal behavior for INITIAL state"
build-rv64-clang-allmodconfig
Desc: Builds riscv64 allmodconfig with Clang, and checks for errors and added warnings
Duration: 1145.60 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 3: "[v3,3/4] riscv: vector: adjust ptrace and signal behavior for INITIAL state"
build-rv64-gcc-allmodconfig
Desc: Builds riscv64 allmodconfig with GCC, and checks for errors and added warnings
Duration: 1658.09 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 3: "[v3,3/4] riscv: vector: adjust ptrace and signal behavior for INITIAL state"
build-rv64-nommu-k210-defconfig
Desc: Builds riscv64 defconfig with NOMMU for K210
Duration: 19.22 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 3: "[v3,3/4] riscv: vector: adjust ptrace and signal behavior for INITIAL state"
build-rv64-nommu-k210-virt
Desc: Builds riscv64 defconfig with NOMMU for the virt platform
Duration: 20.57 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 3: "[v3,3/4] riscv: vector: adjust ptrace and signal behavior for INITIAL state"
checkpatch
Desc: Runs checkpatch.pl on the patch
Duration: 2.23 seconds
Result: WARNING
Output:

WARNING: Argument 'tsk' is not used in function-like macro
#57: FILE: arch/riscv/include/asm/vector.h:431:
+#define riscv_v_ucontext_save(tsk)		do {} while (0)

total: 0 errors, 1 warnings, 0 checks, 113 lines checked

NOTE: For some of the reported defects, checkpatch may be able to
      mechanically convert to the typical style using --fix or --fix-inplace.

Commit 2e571065f261 ("riscv: vector: adjust ptrace and signal behavior for INITIAL state") has style problems, please review.

NOTE: Ignored message types: ALLOC_SIZEOF_STRUCT CAMELCASE COMMIT_LOG_LONG_LINE GIT_COMMIT_ID MACRO_ARG_REUSE NO_AUTHOR_SIGN_OFF

NOTE: If any of the errors are false positives, please report
      them to the maintainer, see CHECKPATCH in MAINTAINERS.
total: 0 errors, 1 warnings, 0 checks, 113 lines checked
WARNING: Argument 'tsk' is not used in function-like macro


@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 3: "[v3,3/4] riscv: vector: adjust ptrace and signal behavior for INITIAL state"
dtb-warn-rv64
Desc: Checks for Device Tree warnings/errors
Duration: 75.42 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 3: "[v3,3/4] riscv: vector: adjust ptrace and signal behavior for INITIAL state"
header-inline
Desc: Detects static functions without inline keyword in header files
Duration: 0.25 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 3: "[v3,3/4] riscv: vector: adjust ptrace and signal behavior for INITIAL state"
kdoc
Desc: Detects for kdoc errors
Duration: 0.72 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 3: "[v3,3/4] riscv: vector: adjust ptrace and signal behavior for INITIAL state"
module-param
Desc: Detect module_param changes
Duration: 0.25 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 3: "[v3,3/4] riscv: vector: adjust ptrace and signal behavior for INITIAL state"
verify-fixes
Desc: Verifies that the Fixes: tags exist
Duration: 0.23 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 3: "[v3,3/4] riscv: vector: adjust ptrace and signal behavior for INITIAL state"
verify-signedoff
Desc: Verifies that Signed-off-by: tags are correct
Duration: 0.29 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 4: "[v3,4/4] selftests: riscv: extend vector tests for sigreturn and ptrace"
build-rv32-defconfig
Desc: Builds riscv32 defconfig
Duration: 114.75 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 4: "[v3,4/4] selftests: riscv: extend vector tests for sigreturn and ptrace"
build-rv64-clang-allmodconfig
Desc: Builds riscv64 allmodconfig with Clang, and checks for errors and added warnings
Duration: 1017.79 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 4: "[v3,4/4] selftests: riscv: extend vector tests for sigreturn and ptrace"
build-rv64-gcc-allmodconfig
Desc: Builds riscv64 allmodconfig with GCC, and checks for errors and added warnings
Duration: 1365.71 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 4: "[v3,4/4] selftests: riscv: extend vector tests for sigreturn and ptrace"
build-rv64-nommu-k210-defconfig
Desc: Builds riscv64 defconfig with NOMMU for K210
Duration: 19.06 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 4: "[v3,4/4] selftests: riscv: extend vector tests for sigreturn and ptrace"
build-rv64-nommu-k210-virt
Desc: Builds riscv64 defconfig with NOMMU for the virt platform
Duration: 20.63 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 4: "[v3,4/4] selftests: riscv: extend vector tests for sigreturn and ptrace"
checkpatch
Desc: Runs checkpatch.pl on the patch
Duration: 2.03 seconds
Result: WARNING
Output:

WARNING: line length of 102 exceeds 100 columns
#58: FILE: tools/testing/selftests/riscv/sigreturn/sigreturn.c:82:
+			struct __riscv_v_ext_state *v_state = (struct __riscv_v_ext_state *)(hdr + 1);

CHECK: Lines should not end with a '('
#81: FILE: tools/testing/selftests/riscv/sigreturn/sigreturn.c:105:
+	asm volatile (

CHECK: Lines should not end with a '('
#106: FILE: tools/testing/selftests/riscv/sigreturn/sigreturn.c:130:
+	asm volatile(

total: 0 errors, 1 warnings, 2 checks, 215 lines checked

NOTE: For some of the reported defects, checkpatch may be able to
      mechanically convert to the typical style using --fix or --fix-inplace.

Commit 3a0be60731c1 ("selftests: riscv: extend vector tests for sigreturn and ptrace") has style problems, please review.

NOTE: Ignored message types: ALLOC_SIZEOF_STRUCT CAMELCASE COMMIT_LOG_LONG_LINE GIT_COMMIT_ID MACRO_ARG_REUSE NO_AUTHOR_SIGN_OFF

NOTE: If any of the errors are false positives, please report
      them to the maintainer, see CHECKPATCH in MAINTAINERS.
total: 0 errors, 1 warnings, 2 checks, 215 lines checked
CHECK: Lines should not end with a '('
WARNING: line length of 102 exceeds 100 columns


@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 4: "[v3,4/4] selftests: riscv: extend vector tests for sigreturn and ptrace"
dtb-warn-rv64
Desc: Checks for Device Tree warnings/errors
Duration: 76.83 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 4: "[v3,4/4] selftests: riscv: extend vector tests for sigreturn and ptrace"
header-inline
Desc: Detects static functions without inline keyword in header files
Duration: 0.24 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 4: "[v3,4/4] selftests: riscv: extend vector tests for sigreturn and ptrace"
kdoc
Desc: Detects for kdoc errors
Duration: 0.72 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 4: "[v3,4/4] selftests: riscv: extend vector tests for sigreturn and ptrace"
module-param
Desc: Detect module_param changes
Duration: 0.25 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 4: "[v3,4/4] selftests: riscv: extend vector tests for sigreturn and ptrace"
verify-fixes
Desc: Verifies that the Fixes: tags exist
Duration: 0.24 seconds
Result: PASS

@linux-riscv-bot
Copy link
Copy Markdown
Author

Patch 4: "[v3,4/4] selftests: riscv: extend vector tests for sigreturn and ptrace"
verify-signedoff
Desc: Verifies that Signed-off-by: tags are correct
Duration: 0.27 seconds
Result: PASS

@linux-riscv-bot linux-riscv-bot deleted the pw1098934 branch May 21, 2026 23:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants