From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-yx1-f66.google.com (mail-yx1-f66.google.com [74.125.224.66]) (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 611E6399350 for ; Thu, 21 May 2026 06:48:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.224.66 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779346139; cv=none; b=hSpRCopGVtjy3tCbbMMvuhB+pu8uMVgRr94agsvDLacc5uZQh+fJu213AoB5boJ8G1SPn+kBmvid1nJwzMapuiEQs/+RcrC65yXUsZj5jPLoy8fS0rNQzI/0Vo3zRxHpvFfL+ShyTy4gS5JfEf1aTKCbqnzM/PGHGJ6a8rDUvOo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779346139; c=relaxed/simple; bh=y7HuuVwEHFM0k+RbonMBFoUgrALyJ/ZjCFw5XeH2ndc=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=r7CMTb8iERS7ED7xsbZqr3QbamvH9I6xNcbetC0PfawAskzLlnUYHqZbOqDqVh/Ct2E55Z8K8SYAgDf1Ov/j+y3D9j/l2ma6pG6G24wBIFOBKLQ7Q0BL83ztW8huvHWHa9tH8MOq/8MMpqt/LkNnOnKGl5mWeQ+bweNIhp9x5I8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=tenstorrent.com; spf=pass smtp.mailfrom=tenstorrent.com; dkim=pass (2048-bit key) header.d=tenstorrent.com header.i=@tenstorrent.com header.b=JeLJ2HiQ; arc=none smtp.client-ip=74.125.224.66 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=tenstorrent.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=tenstorrent.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=tenstorrent.com header.i=@tenstorrent.com header.b="JeLJ2HiQ" Received: by mail-yx1-f66.google.com with SMTP id 956f58d0204a3-65c2cd216c9so4899015d50.3 for ; Wed, 20 May 2026 23:48:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tenstorrent.com; s=google; t=1779346136; x=1779950936; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=+A6cxApH+5sNlVYfEhEvEKn9bzG631q7W7KYWfcJkvU=; b=JeLJ2HiQiAeeR6D9XlLYn/OMH9ClfX8UBDOSJmC9yZVgl6/xFevJcpsuiLrQtYJkke AsB8DMCURUzC65Q1HYz8CrRGk8gUoH/liNiHEe89+g1gYHm3IJSs1IQrvBiQbnh/8T52 SIl3R0/brjAX5bPGnCCiCzfNW5fKKtrfBqynVF1tPkyJSH+ifsV/TdKI/nyBLlTwIp9b wb+S15ULDG6ON/WBl5iJihMEni7d5PscH37ZERZ5y/QU44/uQFD3/CPS6wJnOCSrJmCK EpANrAhKTb/3G4KhUW2SVaVzJ4dsA+J91glhK+oaT9QN0ALDwaDZihSIaC0/+mLqmQiE RcIQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779346136; x=1779950936; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=+A6cxApH+5sNlVYfEhEvEKn9bzG631q7W7KYWfcJkvU=; b=ORfzNPgkmW0YZnWG3DpcI4V2rduOS+3f/3/zXIu6D/AVH7/FVKWxbkDs+uQgm6h0k1 HKJVeYEgwiAsqwT1woYEZsNPDOGmeWc2HTrklwtwm6BuBX5hSl5NNdw0f44hEmVYrFNl ESAHiUWbJg+sveiGGezDbdB/AZ+LJ6CoiMJlf+SUnlXTu+7zlk7Lz+s4qaeK9S0+yt5v 4jEnFuH7NntPxBetIOX9+76sYxUOWg/Wr/0ph8PSI4Lj75bMXda7mJOKvFcL7NQzwl2z 3HXb0k4ega/s6OSJHH+h+kYOucgD9t1zc8GXmCu86vV/uugC+3aYntHwoRxNrJaB8dwh AfoQ== X-Gm-Message-State: AOJu0YzURxJ08FPZK0socLnNwgHh/HuP7VMrnHWqyo2bULUK8Ut2Dm4I nTpmtpXjLwpJ3WRkazy/nzZqdiQvuL3WhchDEhuBkSCEr84WQTTvmgSEUqd3TMpa5dQ= X-Gm-Gg: Acq92OE0mu0UDSU2KfPREjma7jMm4GcnEGyaqIG/VhGRIHqRadPHkFbMYuybsJ1giEu rKbLVihiRT+sZMhmjrCWWFVs+Y9cCQ10YPHDOETEVjrlgFvYY7PX5dyWsiBf/Irv37AqwCsb4cM I0qM6+QWSu+JAXTsz1JBKBfSLfa1TZRASyT3neN8bN5Tic3nn4GmFuH98QAUt8+zZ3wOUBHHrmh Gqc9/TMbZxizmB+au8ESeB+DsTQXM/6YdmJ18NxCduBxmiUx/CKTbE5+LS7JgrdFUMRKuu69yz0 pLvvSE17F9BFaqKQMAN++DfQlWjn8+IkToMh9oSc+K2knZZVt9JHiT/oYq9KTNofOcI/nxZiYwr 6hRP4k5CBIeJdVByzao7qMXsGCdPZqxeZayUKzv1A4j8njx7BXKYDhc6FjOcF+5tIH440KIlfdL Rq31pIjLBKniVJk/7ASYnoozLQQeUWBBVGZSMx/WPQ X-Received: by 2002:a05:690e:1285:b0:64e:f106:60ea with SMTP id 956f58d0204a3-65eae27ab87mr1101526d50.44.1779346136273; Wed, 20 May 2026 23:48:56 -0700 (PDT) Received: from localhost ([2600:1700:220:5c80:f8e3:7ff3:dc2:1810]) by smtp.gmail.com with ESMTPSA id 956f58d0204a3-65e0d8e7585sm10368092d50.9.2026.05.20.23.48.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 May 2026 23:48:55 -0700 (PDT) Date: Thu, 21 May 2026 01:48:55 -0500 From: Andy Chiu To: 20260319073709.18562-1-daichengrong@iscas.ac.cn, 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 Subject: Re: [RFC PATCH] riscv: clarify vector state semantics on syscall and context switch Message-ID: References: <20260319073709.18562-1-daichengrong@iscas.ac.cn> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20260319073709.18562-1-daichengrong@iscas.ac.cn> 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! [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