From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from cstnet.cn (smtp25.cstnet.cn [159.226.251.25]) (using TLSv1.2 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AD830125AA for ; Mon, 25 May 2026 01:47:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=159.226.251.25 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779673654; cv=none; b=kC2YmvK0RPz5/iohGITXrEYe/JowB1oNm5bnVXW+0MnolYrVPeLfcCtSrWWutSeDZpGiNjMd6CGxZvWw4ncwVU4DDV1HCKrf7IKYGbgQ/eQDzXhwTMXiMecSU1z3+GO/3MKTAhW4dzBHkLkG0+O1/Ejf5RVSIT9lZnD0a9mIjMA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779673654; c=relaxed/simple; bh=OuKe1ACNXH1sfJp9W2cWvT1SdXN4yW+37gXXM+oXTp0=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=VnpA7MX4lqa4WyPrCxGJlzgqSlMAgIL0XGUZB9LFZ5LWMUaFhwxUYXSXxyvU5yX7wwqK6GcCzAy62YUQrLXi7PhXJhwB5qItENiHM8herALrQra49hE+8sSIBI1YGNogB96PfoaGiE71OviwvfPNuvqhq+BQkZ31/kmd9IdGne4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=iscas.ac.cn; spf=pass smtp.mailfrom=iscas.ac.cn; arc=none smtp.client-ip=159.226.251.25 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=iscas.ac.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=iscas.ac.cn Received: from [192.168.11.252] (unknown [121.237.245.238]) by APP-05 (Coremail) with SMTP id zQCowACXHgLpqBNq7LpfEQ--.3566S2; Mon, 25 May 2026 09:42:02 +0800 (CST) Message-ID: <29c8ad17-8293-48ba-a79a-2520b9b58239@iscas.ac.cn> Date: Mon, 25 May 2026 09:42:01 +0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [RFC PATCH] riscv: clarify vector state semantics on syscall and context switch Content-Language: en-US To: Andy Chiu , daichengrong@iscas.ac.cn Cc: linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org, palmer@dabbelt.com, pjw@kernel.org, samuel.holland@sifive.com References: <20260319073709.18562-1-daichengrong@iscas.ac.cn> From: daichengrong In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-CM-TRANSID:zQCowACXHgLpqBNq7LpfEQ--.3566S2 X-Coremail-Antispam: 1UD129KBjvJXoWxGrykKry3Zw1DZr1Utr18Xwb_yoWrKw17pr Z8GF4Yk3yUJF4xZ3sFqw4xXF9Yk3yrG3yYyrZxGay5JFy5Kr93Krn3t345CF4DAFy5CFy0 vw4qg34kuw4DAFDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUvS14x267AKxVW8JVW5JwAFc2x0x2IEx4CE42xK8VAvwI8IcIk0 rVWrJVCq3wAFIxvE14AKwVWUJVWUGwA2ocxC64kIII0Yj41l84x0c7CEw4AK67xGY2AK02 1l84ACjcxK6xIIjxv20xvE14v26F1j6w1UM28EF7xvwVC0I7IYx2IY6xkF7I0E14v26r4U JVWxJr1l84ACjcxK6I8E87Iv67AKxVWxJr0_GcWl84ACjcxK6I8E87Iv6xkF7I0E14v26F 4UJVW0owAS0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0VAKzVAqx4xG6I80ewAv 7VC0I7IYx2IY67AKxVWUXVWUAwAv7VC2z280aVAFwI0_Gr0_Cr1lOx8S6xCaFVCjc4AY6r 1j6r4UM4x0Y48IcVAKI48JM4x0x7Aq67IIx4CEVc8vx2IErcIFxwCY1x0262kKe7AKxVWU AVWUtwCY02Avz4vE14v_KwCF04k20xvY0x0EwIxGrwCFx2IqxVCFs4IE7xkEbVWUJVW8Jw C20s026c02F40E14v26r1j6r18MI8I3I0E7480Y4vE14v26r106r1rMI8E67AF67kF1VAF wI0_JF0_Jw1lIxkGc2Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVWUJVWUCwCI42IY6xIIjx v20xvEc7CjxVAFwI0_Jr0_Gr1lIxAIcVCF04k26cxKx2IYs7xG6r1j6r1xMIIF0xvEx4A2 jsIE14v26r1j6r4UMIIF0xvEx4A2jsIEc7CjxVAFwI0_Jr0_GrUvcSsGvfC2KfnxnUUI43 ZEXa7VUjuWlPUUUUU== X-CM-SenderInfo: pgdluxxhqj201qj6x2xfdvhtffof0/ On 5/21/26 14:48, Andy Chiu wrote: > On Thu, Mar 19, 2026 at 03:37:09PM +0800, daichengrong wrote: >> 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 mstatus >> and discards it if necessary. After discard, the state is explicitly >> set to INIT instead of DIRTY, indicating that the vector registers >> no longer contain meaningful user data. >> >> 2. During context switch, the vector state is interpreted as follows: >> - INIT: no valid user data is present, so vector register data does >> not need to be saved. >> - non-INIT (e.g. DIRTY): vector register data must be saved. >> >> 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 potential data >> leakage from a previous task. >> >> This aligns the kernel's vector state tracking with the architectural >> "unspecified" semantics while ensuring correct lazy context switching >> and preventing cross-task data leakage. > Hi daichengrong, > > Good catch on spotting this optimization opportunity! > > I have a patch series[1] that happens to be very similar as yours, but I > think your coding style make the code more readable (I like the way you > use _vstate_check for CLEAN on restore path). > > Here are some issues where this patch breaks: > - first-use trap returns discarded vregs instead of zero'ed. > - ptrace gets stale vregs in PTRACE_SYSCALL > - context modifications through signal handler are dropped at syscall > stops. > > I will merge your patch into my v3 with all the above fixed. Hope that > sounds good to you! > Thanks for the detailed analysis and for pointing out these corner cases. I agree the first-use trap, ptrace and signal-handler interactions need to be handled correctly. Glad to hear the overall direction makes sense, and I’m happy for you to fold the idea into your v3 series. My original motivation was to explore whether the syscall boundary could serve as a natural entry point for managing the RVV state lifecycle across the whole system, especially for reducing unnecessary restore/init work on repeated syscall paths while still preserving the expected userspace semantics. > [1]: https://lore.kernel.org/linux-riscv/20260402043414.2421916-2-andybnac@gmail.com/ > >> >> Signed-off-by: daichengrong >> --- >> arch/riscv/include/asm/vector.h | 15 +++++++-------- >> 1 file changed, 7 insertions(+), 8 deletions(-) >> >> diff --git a/arch/riscv/include/asm/vector.h b/arch/riscv/include/asm/vector.h >> index 00cb9c0982b1..93c68a549b72 100644 >> --- a/arch/riscv/include/asm/vector.h >> +++ b/arch/riscv/include/asm/vector.h >> @@ -298,8 +298,9 @@ 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); > We can skip discarding vstate here as it will be done in the restore path >> + if (!__riscv_v_vstate_check(regs->status, INITIAL)) >> + __riscv_v_vstate_discard(); >> + riscv_v_vstate_on(regs); >> } >> } >> >> @@ -315,19 +316,17 @@ 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_check(regs->status, CLEAN)) >> __riscv_v_vstate_restore(vstate, vstate->datap); >> - __riscv_v_vstate_clean(regs); >> - } >> } >> >> 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); >> - } >> } >> >> #ifdef CONFIG_RISCV_ISA_V_PREEMPTIVE >> -- >> 2.25.1 >> >> >> _______________________________________________ >> linux-riscv mailing list >> linux-riscv@lists.infradead.org >> http://lists.infradead.org/mailman/listinfo/linux-riscv >> > > Cheers, > Andy