From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id ULRNLJq79Go3pAIAWB0awg (envelope-from ) for ; Wed, 11 Nov 2026 14:01:46 -0500 Authentication-Results: simark.ca; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=Z3ZsRF/B; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 99E6F1E04F; Wed, 11 Nov 2026 14:01:46 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-2.4 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,RCVD_IN_VALIDITY_CERTIFIED_BLOCKED, RCVD_IN_VALIDITY_RPBL_BLOCKED,RCVD_IN_VALIDITY_SAFE_BLOCKED autolearn=ham autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [38.145.34.32]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 7A8F81E04F for ; Wed, 11 Nov 2026 14:01:44 -0500 (EST) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 136644BC7ECE for ; Wed, 11 Nov 2026 19:01:43 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 136644BC7ECE DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1794423703; bh=B1oPuIWu7B9M1Uly5bqaDdGLLTriJbsjACORZORkqD4=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=Z3ZsRF/BPRkrCwSmgmQosiq08YXEaI+hyqlnhwp2ndhHancgZST9po7bsG2K703Jt UdGTeBULmZdJII/5Zkerp1vZPydDHSOsqqUHiHqMfvCAqkRrUPqLH2WVguGrSHix88 EDdNQCJ9TUYeCcu5rDs6kd7+OvqzZisNB6gUuLMY= Received: from mail-yw1-x112c.google.com (mail-yw1-x112c.google.com [IPv6:2607:f8b0:4864:20::112c]) by sourceware.org (Postfix) with ESMTPS id B8C9B4BBCDB8 for ; Wed, 11 Nov 2026 19:01:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B8C9B4BBCDB8 ARC-Filter: OpenARC Filter v1.0.0 sourceware.org B8C9B4BBCDB8 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1794423667; cv=none; b=eJdkxYxmEX8ApViQdoLEgZwu1qjpBFUru9+OHZj9BufCU0Xvl0BboHiANQt5yC/MPErL8UTsZl64u8OjSy7XxAXvVPrkFHPblrdgTLWmj6Q7yfNUmnPJnvnQ1EnpyKQN+P1/zgJlNx8BGS3DXx7MhxQVDMCuhgkyzPZxk460svc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1794423667; c=relaxed/simple; bh=SJhz7PDOsmaMSuhWTY6VFjTOEgddrKSIcMrVqYMgOiI=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=MQw637/vmrn0tC1vaOF+zTG3/BWEyKQvOJi5xe3oPVj7LHvuhgz3az/vJcgyJhCFdZOH9I8AfQTSRoAKP3CLqTryJTBDlrzjcUTC7pNw9eMfRwYf7d58OJqAZfgIWlThrWsU8X3h3jN30C4Vny42Ssi6zggJCiqcRPj6srIdxFM= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=tenstorrent.com header.i=@tenstorrent.com header.a=rsa-sha256 header.s=google header.b=KI5YkRbB DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B8C9B4BBCDB8 Received: by mail-yw1-x112c.google.com with SMTP id 00721157ae682-8b33de9d2b1so28743887b3.3 for ; Wed, 11 Nov 2026 11:01:06 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1794423666; x=1795028466; 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:content-type; bh=B1oPuIWu7B9M1Uly5bqaDdGLLTriJbsjACORZORkqD4=; b=Tx+XDmti3P/o4AG9cfj16ziWdaAZUiLImgfoq/yfMG18BaR4W5vv+JgoFXsIxHuiAs QG6vnq3w1NLW+Fynl5rJKJHP6Vf27cnHbvThYMVQo6ysUk8NzVrbQH7iIGLX02qsgCfP qgDrEazF4KO6JS0UlWtn+Ao9KbGgGHrpujN6yz8Re/uDny2onlZxvuOk5rU3RhtGSybp 9igWGcUHTEgP8Q5IQECutLo+9fd20C77BajgetdnAfIh9514aLNzCgiOW6r4s8LRPQfm bQle6CQcjTf9Qb6wB6XfQ2yiyQOhE3NmtciOEgqnwZGBEMPcvzQrVAZaisBOzPvrdCZ7 ehHg== X-Forwarded-Encrypted: i=1; AL+hErEtJYmBxLEdz1ijpNiavR7ik8nc2U0Bsa3jTbVpvHF6IUX7QCpmAuo4oVH1Sbp2ecUrvsY=@sourceware.org X-Gm-Message-State: APuU4Z/oJhnae7S0NDKDdMELgSgpeoj1MANhP8gPNjeHrTKoc0oHHY8p YnzDIQI6afnpAdRKkisbp9WUA3PcF88jWGm2p0S7c6ML2LIZpV3DVpd7Gy5cFses/AE= X-Gm-Gg: AYBFou1lYMYeS0k08PSDw7e/07F0L1Zh7GNXRGVBCJRcjD07qdP0CNkZjHD3eY7GwmC slxNwjgCRu54h3H0Rs2NU7SfvcvKqFOHoEQvxIIf8QzqM2tj0fF3XH81cPYkCoWAb/z/F/tzlgo h+5Q7/rDtA9dTyDRAG9gC/mC2eGjbBHz2IT9cJQM/IUpEjoyuS/5gmfLDvy202ivAoHa8qShfdF eRYnEld4wNz9E5/tdmWVtuKx5QbMfCTxIjOnVIsvF44w4LXb9c7s6xnuTPOuX39WjPGpJtgTsTH 2yixI9OaVPUSm1vJ3aufTHIFjGZ2L1wl4U98uq98W9y2Ewjs1DSYoJiSNpX0e7G84Z8xIIzYDiv 9d9DFAj2v3vrLt25ePULuJijAlXrL+etdwnxxa3k2Ix99ZYcr/Ej8qf0ek4czDrGypR5UI2En3M 2ts5HEvEY1lC6/SJENhK0hxdn5wv4zjjbV+jCfwiYsRgjZCilzlXvVEDwGAHjGUkB01U5p7nbPx rYsKogIqibDWitYjsPa2lRHJ1HlzKqsvVfE+w6u9fnIFkYOcqbZsAf5XzEu0vqSS/Hf4Ru9v0Gs Yy8Mxkbv1X4XT5A9PVPUmwZdh3weMCqbprJ0Y/1kuv7+nkKL4wV24LmEDBdl9zC/4x9B/hff8Wr H8ahquUqrmMrHad+4ePlKahC84rRDJksG/l4aZejfKphkP2g= X-Received: by 2002:a05:690c:e1c2:10b0:8b3:47ec:b20a with SMTP id 00721157ae682-8b3a80c2e4emr15072607b3.43.1794423665829; Wed, 11 Nov 2026 11:01:05 -0800 (PST) Received: from lima-default.tail89d63.ts.net (108-196-157-19.lightspeed.austtx.sbcglobal.net. [108.196.157.19]) by smtp.gmail.com with ESMTPSA id 956f58d0204a3-67c589a2354sm5209197d50.6.2026.11.11.11.01.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Nov 2026 11:01:05 -0800 (PST) To: Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti , Oleg Nesterov , linux-riscv@lists.infradead.org Cc: andybnac@gmail.com, daichengrong , gdb@sourceware.org, Andy Chiu , dfustini@oss.tenstorrent.com, greentime.hu@sifive.com, Yong-Xuan Wang , Aurelien Jarno , Anup Patel , Guobin Zhang , Troy Mitchell , Deepak Gupta , Sergey Matyukevich Subject: [PATCH v7 3/6] riscv: clarify vector state semantics on syscall and context switch Date: Wed, 11 Nov 2026 12:43:25 -0600 Message-ID: <20261111184359.1239821-4-tchiu@tenstorrent.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261111184359.1239821-1-tchiu@tenstorrent.com> References: <20261111184359.1239821-1-tchiu@tenstorrent.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: gdb@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Andy Chiu via Gdb Reply-To: Andy Chiu Errors-To: gdb-bounces~public-inbox=simark.ca@sourceware.org Sender: "Gdb" From: daichengrong 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 set it to INIT if not already, indicating that the vector registers no longer contain meaningful user data. Context invalidation is required on restore. If the state is already set to INIT, it means that the user has not touched vector since the last invalidation. So no further action is required on restore. 2. During context switch, the vector state is saved only if the state is DIRTY. (no change) 3. On restore, if invalidation is required, the vector registers are overwritten with a known initial value and the state is set to INIT. 4. 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. On the otherhand, ptrace reads at a syscall stop always returns the nulled context. Performance improvements on Blackhole x280: Latency of getpid() with different status.VS upon syscall entry in ns: status.VS Before Patch After Patch Improvement DIRTY 235.9 242.4 +6.5 (+2.7%) # regress INIT 234.8 174.4 -60.4 (-25.7%) OFF 178.2 174.5 -3.7 (-2.1%) Context switch latencies in us: Metric Before Patch After Patch Improvement Mean Latency 14.19 11.62 -2.57 (-18.1%) Median Latency 13.51 11.01 -2.50 (-18.5%) Max Latency 27.76 21.92 -5.84 (-21.0%) Min Latency 8.99 8.12 -0.87 (-9.7%) The metrics on context switch latencies are obtained by running the following command 100 times: $ taskset 0x2 ./lat_ctx_v -N 10000 -s 512 2 The program lat_ctx_v is a modified lat_ctx where benchmark processes (parent/child) do a `vsetvli` to dirtify VS before making each write syscall. CC: gdb@sourceware.org Signed-off-by: daichengrong Co-developed-by: Andy Chiu Signed-off-by: Andy Chiu --- Changelog v7: - Fold the next patch from v6 into this to prevent exposing breaks in signal and ptrace. Changelog v4: - Provide performance meassurement (Olof) - Aggregate riscv_v_{enable,disable} for better performance (reduces to ~240 ns/call when the user touches vector) - Do not invalidate again if user doesn't touch vector Changelog v3: - rename vstate_on to vstate_init to prevent confusion - set context as clean at first-use trap to return zero'ed context - reduce context nulling operations by defering __vstate_discard to exit_to_user_mode_prepare. --- arch/riscv/include/asm/vector.h | 45 ++++++++++++++++++--------------- arch/riscv/kernel/ptrace.c | 7 ++++- arch/riscv/kernel/signal.c | 7 ++++- arch/riscv/kernel/vector.c | 29 +++++++++++++++++++-- 4 files changed, 64 insertions(+), 24 deletions(-) diff --git a/arch/riscv/include/asm/vector.h b/arch/riscv/include/asm/vector.h index 7cd20f661596..e83f9e0046a9 100644 --- a/arch/riscv/include/asm/vector.h +++ b/arch/riscv/include/asm/vector.h @@ -40,6 +40,15 @@ _res; \ }) +#define __riscv_v_vstate_check_gt(_val, TYPE) ({ \ + bool _res; \ + if (has_xtheadvector()) \ + _res = ((_val) & SR_VS_THEAD) > SR_VS_##TYPE##_THEAD; \ + else \ + _res = ((_val) & SR_VS) > SR_VS_##TYPE; \ + _res; \ +}) + extern unsigned long riscv_v_vsize; int riscv_v_setup_vsize(void); bool insn_is_vector(u32 insn_buf); @@ -101,7 +110,7 @@ static inline void riscv_v_vstate_off(struct pt_regs *regs) regs->status = __riscv_v_vstate_or(regs->status, OFF); } -static inline void riscv_v_vstate_on(struct pt_regs *regs) +static inline void riscv_v_vstate_init(struct pt_regs *regs) { regs->status = __riscv_v_vstate_or(regs->status, INITIAL); } @@ -294,16 +303,6 @@ static inline void __riscv_v_vstate_discard(void) : "=&r" (vl) : "r" (vtype_inval)); } -static inline void riscv_v_vstate_discard(struct pt_regs *regs) -{ - if (riscv_v_vstate_query(regs)) { - riscv_v_enable(); - __riscv_v_vstate_discard(); - riscv_v_disable(); - __riscv_v_vstate_dirty(regs); - } -} - static inline void riscv_v_vstate_save(struct __riscv_v_ext_state *vstate, struct pt_regs *regs) { @@ -319,21 +318,26 @@ static inline void riscv_v_vstate_restore(struct __riscv_v_ext_state *vstate, struct pt_regs *regs) { WARN_ON_ONCE(preemptible()); - - if (riscv_v_vstate_query(regs)) { - riscv_v_enable(); + riscv_v_enable(); + if (__riscv_v_vstate_check(regs->status, INITIAL)) + __riscv_v_vstate_discard(); + else if (__riscv_v_vstate_check(regs->status, CLEAN)) __riscv_v_vstate_restore(vstate, vstate->datap); - riscv_v_disable(); - __riscv_v_vstate_clean(regs); - } + riscv_v_disable(); } static inline void riscv_v_vstate_set_restore(struct task_struct *task, struct pt_regs *regs) { - if (riscv_v_vstate_query(regs)) { + if (riscv_v_vstate_query(regs)) set_tsk_thread_flag(task, TIF_RISCV_V_DEFER_RESTORE); - riscv_v_vstate_on(regs); +} + +static inline void riscv_v_vstate_discard(struct pt_regs *regs) +{ + if (__riscv_v_vstate_check_gt(regs->status, INITIAL)) { + riscv_v_vstate_set_restore(current, regs); + riscv_v_vstate_init(regs); } } @@ -404,6 +408,7 @@ static inline void __switch_to_vector(struct task_struct *prev, riscv_preempt_v_set_restore(next); } } else { + /* VS is never DIRTY at this point, there's no need to alter vstate here */ riscv_v_vstate_set_restore(next, task_pt_regs(next)); } } @@ -429,7 +434,7 @@ static inline bool riscv_v_vstate_ctrl_user_allowed(void) { return false; } #define riscv_v_vstate_restore(vstate, regs) do {} while (0) #define __switch_to_vector(__prev, __next) do {} while (0) #define riscv_v_vstate_off(regs) do {} while (0) -#define riscv_v_vstate_on(regs) do {} while (0) +#define riscv_v_vstate_init(regs) do {} while (0) #define riscv_v_thread_free(tsk) do {} while (0) #define riscv_v_setup_ctx_cache() do {} while (0) #define riscv_v_thread_alloc(tsk) do {} while (0) diff --git a/arch/riscv/kernel/ptrace.c b/arch/riscv/kernel/ptrace.c index 88442ac4f6ae..d329518cceb6 100644 --- a/arch/riscv/kernel/ptrace.c +++ b/arch/riscv/kernel/ptrace.c @@ -218,13 +218,18 @@ static int riscv_vr_set(struct task_struct *target, int ret; struct __riscv_v_ext_state *vstate = &target->thread.vstate; struct __riscv_v_regset_state ptrace_vstate; + struct pt_regs *regs = task_pt_regs(target); if (!(has_vector() || has_xtheadvector())) return -EINVAL; - if (!riscv_v_vstate_query(task_pt_regs(target))) + if (!riscv_v_vstate_query(regs)) return -ENODATA; + /* Silently drop the modification to tracee as no vreg lives across a syscall */ + if (__riscv_v_vstate_check(regs->status, INITIAL)) + return 0; + /* Copy rest of the vstate except datap */ memset(&ptrace_vstate, 0, sizeof(ptrace_vstate)); ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &ptrace_vstate, 0, diff --git a/arch/riscv/kernel/signal.c b/arch/riscv/kernel/signal.c index 50c77bcf2b09..dc785aca1cc4 100644 --- a/arch/riscv/kernel/signal.c +++ b/arch/riscv/kernel/signal.c @@ -119,10 +119,15 @@ static long __restore_v_state(struct pt_regs *regs, void __user *sc_vec) /* * Mark the vstate as clean prior performing the actual copy, * to avoid getting the vstate incorrectly clobbered by the - * discarded vector state. + * discarded vector state. + * + * This also allows user to modify vregs through the signal + * interface at a syscall stop. e.g. to support user space + * context switching. */ preempt_disable(); riscv_v_vstate_set_restore(current, regs); + __riscv_v_vstate_clean(regs); preempt_enable(); /* Copy everything of __sc_riscv_v_state except datap. */ diff --git a/arch/riscv/kernel/vector.c b/arch/riscv/kernel/vector.c index 924baa71af7e..6fd541f5d5cb 100644 --- a/arch/riscv/kernel/vector.c +++ b/arch/riscv/kernel/vector.c @@ -29,15 +29,40 @@ static struct kmem_cache *riscv_v_kernel_cachep; unsigned long riscv_v_vsize __read_mostly; EXPORT_SYMBOL_GPL(riscv_v_vsize); +/* + * Context memory is not coherent to register when sstatus.vs is set to INITIAL. This function + * take the INITIAL state into consideration and reflect the nulled state into context memory. + * Assume the target task is not actively running when tsk != current + */ void riscv_v_ucontext_save(struct task_struct *tsk) { struct __riscv_v_ext_state *vstate = &tsk->thread.vstate; struct pt_regs *regs = task_pt_regs(tsk); + /* + * Do not set vstate as clean when it is INITIAL, otherwise we lose track of the nulled + * state in ptrace. + */ if (tsk == current) { get_cpu_vector_context(); - riscv_v_vstate_save(vstate, regs); + if (__riscv_v_vstate_check(regs->status, INITIAL)) { + riscv_v_enable(); + __riscv_v_vstate_discard(); + __riscv_v_vstate_save(vstate, vstate->datap); + riscv_v_disable(); + } else { + riscv_v_vstate_save(vstate, regs); + } put_cpu_vector_context(); + } else if (__riscv_v_vstate_check(regs->status, INITIAL)) { + /* + * If we are not current and VS == INITIAL, null out the context memory for tsk + * using kernel mode vector. + */ + kernel_vector_begin(); + __riscv_v_vstate_discard(); + __riscv_v_vstate_save(vstate, vstate->datap); + kernel_vector_end(); } } @@ -233,7 +258,7 @@ bool riscv_v_first_use_handler(struct pt_regs *regs) return true; } - riscv_v_vstate_on(regs); + __riscv_v_vstate_clean(regs); riscv_v_vstate_set_restore(current, regs); return true; -- 2.43.0