Linux-RISC-V Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Charlie Jenkins <thecharlesjenkins@gmail.com>
To: Andy Chiu <tchiu@tenstorrent.com>
Cc: Paul Walmsley <pjw@kernel.org>,
	Palmer Dabbelt <palmer@dabbelt.com>,
	Albert Ou <aou@eecs.berkeley.edu>,
	Alexandre Ghiti <alex@ghiti.fr>, Oleg Nesterov <oleg@redhat.com>,
	Andy Chiu <andybnac@gmail.com>,
	Eric Biggers <ebiggers@google.com>,
	linux-riscv@lists.infradead.org,
	Guobin Zhang <guobin.zhang@intel.com>,
	dfustini@oss.tenstorrent.com, greentime.hu@sifive.com,
	Anup Patel <anup@brainfault.org>,
	Kaiwen Xue <kaiwenx@rivosinc.com>,
	Deepak Gupta <debug@rivosinc.com>,
	Sergey Matyukevich <geomatsi@gmail.com>,
	Drew Fustini <fustini@kernel.org>,
	Yicong Yang <yang.yicong@picoheart.com>,
	Ivy Lopez <skunkolee@gmail.com>,
	Aurelien Jarno <aurelien@aurel32.net>,
	Troy Mitchell <troy.mitchell@linux.dev>,
	Jisheng Zhang <jszhang@kernel.org>,
	Conor Dooley <conor.dooley@microchip.com>,
	Zong Li <zong.li@sifive.com>, Nam Cao <namcao@linutronix.de>,
	Samuel Holland <samuel.holland@sifive.com>,
	Vivian Wang <wangruikang@iscas.ac.cn>,
	Yunhui Cui <cuiyunhui@bytedance.com>,
	Yong-Xuan Wang <yongxuan.wang@sifive.com>,
	Andrew Jones <andrew.jones@linux.dev>,
	Zishun Yi <vulab@iscas.ac.cn>
Subject: Re: [EXT] Re: [PATCH v1] riscv: fix missed update on an interrupted RMW of regs->status
Date: Thu, 3 Dec 2026 01:08:38 -0800	[thread overview]
Message-ID: <axExllv43XmKIvQB@blinky> (raw)
In-Reply-To: <CAP14cn+WZPGreDuUZukJFwq1ZkgWU=44padK8+_TgLoQk8s10Q@mail.gmail.com>

On Wed, Dec 02, 2026 at 05:33:23PM -0600, Andy Chiu wrote:
> Hi Charlie,
> 
> On Wed, Dec 2, 2026 at 1:58 AM Charlie Jenkins
> <thecharlesjenkins@gmail.com> wrote:
> >
> > > Before this patch, preempt_disable is not enough to protect the SR_FS
> > > update in signal handling. As kernel-mode vector, which also update
> > > regs->status, is allowed in softirq context and it can come right after
> > > serving an interrupt.
> > >
> > > Move SR_FS and SR_VS into its own separate field in pt_regs so that
> > > RMW of SR_FS does not need to carry the same synchronization primitive
> > > as non-atomic update of SR_VS. Also, export vendor's alternative macros
> > > so that they can be reused in assembly code.
> > >
> > > CC: Guobin Zhang <guobin.zhang@intel.com>
> > > Fixes: 956895b9d8f7 ("riscv: vector: make Vector always available for softirq context")
> > > Signed-off-by: Andy Chiu <tchiu@tenstorrent.com>
> > >
> > > diff --git a/arch/riscv/include/asm/csr.h b/arch/riscv/include/asm/csr.h
> > > index 621f84d1898f..71c7fd9d8f26 100644
> > > --- a/arch/riscv/include/asm/csr.h
> > > +++ b/arch/riscv/include/asm/csr.h
> > > @@ -27,6 +27,14 @@
> > >  #define SR_ELP               SR_SPELP
> > >  #endif
> > >
> > > +#define SR_OFF               _AC(0x00000000, UL)
> > > +#define SR_INITIAL   _AC(0x00000001, UL)
> > > +#define SR_CLEAN     _AC(0x00000002, UL)
> > > +#define SR_DIRTY     _AC(0x00000003, UL)
> > > +#define SR_VS_THEAD_SHIFT            23
> > > +#define SR_VS_SHIFT                  9
> > > +#define SR_FS_SHIFT                  13
> > > +
> > >  #define SR_FS                _AC(0x00006000, UL) /* Floating-point Status */
> > >  #define SR_FS_OFF    _AC(0x00000000, UL)
> > >  #define SR_FS_INITIAL        _AC(0x00002000, UL)
> > > @@ -52,6 +60,7 @@
> > >  #define SR_XS_DIRTY  _AC(0x00018000, UL)
> > >
> > >  #define SR_FS_VS     (SR_FS | SR_VS) /* Vector and Floating-Point Unit */
> > > +#define SR_FS_VS_THEAD       (SR_FS | SR_VS_THEAD)
> > >
> > >  #ifndef CONFIG_64BIT
> > >  #define SR_SD                _AC(0x80000000, UL) /* FS/VS/XS dirty */
> > > diff --git a/arch/riscv/include/asm/ptrace.h b/arch/riscv/include/asm/ptrace.h
> > > index addc8188152f..a56498055218 100644
> > > --- a/arch/riscv/include/asm/ptrace.h
> > > +++ b/arch/riscv/include/asm/ptrace.h
> > > @@ -49,6 +49,8 @@ struct pt_regs {
> > >       unsigned long t6;
> > >       /* Supervisor/Machine CSRs */
> > >       unsigned long status;
> > > +     u8 sr_vs;
> > > +     u8 sr_fs;
> > >       unsigned long badaddr;
> > >       unsigned long cause;
> > >       /* a0 value before the syscall */
> > > diff --git a/arch/riscv/include/asm/switch_to.h b/arch/riscv/include/asm/switch_to.h
> > > index 970f89935f0e..61588f7b03cf 100644
> > > --- a/arch/riscv/include/asm/switch_to.h
> > > +++ b/arch/riscv/include/asm/switch_to.h
> > > @@ -22,21 +22,19 @@ extern void __fstate_restore(struct task_struct *restore_from);
> > >
> > >  static inline void __fstate_clean(struct pt_regs *regs)
> > >  {
> > > -     regs->status = (regs->status & ~SR_FS) | SR_FS_CLEAN;
> > > +     regs->sr_fs = SR_CLEAN;
> > >  }
> > >
> > >  static inline void fstate_off(struct task_struct *task,
> > >                             struct pt_regs *regs)
> > >  {
> > > -     preempt_disable();
> > > -     regs->status = (regs->status & ~SR_FS) | SR_FS_OFF;
> > > -     preempt_enable();
> > > +     regs->sr_fs = SR_FS_OFF;
> > I think these operations might be able to be done atomically and then we
> > don't need to disable preemption or split these away from sstatus for
> > clean/off.
> >
> > __fstate_clean is only ever entered when initial, clean, or dirty.
> >
> > There is a single entry point when regs->status=initial and that is
> > start_thread(), which immediately goes on to call fstate_restore() which
> > sets regs->status to clean. So as it stands, I don't think there is any
> > functional difference between initial and clean. We could just have all
> > threads start at clean and get rid of setting it to initial in the first
> > place. The semantics of having a thread start off as "initial" only to
> > immediately have registers restored seems off to me anyway, I would
> > expect "initial" to be some default fp values instead of parent fp
> > values as it is being used here.
> >
> > When __fstate_clean is entered with regs->status=clean/dirty a simple
> > atomic and of ~0x2000 can be performed to arrive at clean again.
> >
> 
> Thanks for the feedback! The idea works for SR_FS as it should really
> be swinging between "dirty" and "clean". Using amo operations on SR_VS
> does not help as an onging series is making "initial" as a valid state
> [1], e.g. to optimize context tracking on the syscall path.

Aww okay I didn't realize that patch was doing that! I did just look at
that series and I think it's possible to make these ideas not conflict.
You are using INITIAL will the goal of avoiding unecessary save/restores
which ties perfectly since the save/restore paths that are setting the
VS bits can be bypassed so we avoid the issue of needing to transition
from INITIAL->CLEAN. I guess my point is that I don't think it makes
semantic sense to be able to transition from INITIAL->CLEAN. INITIAL
should stay INITIAL until there is state that needs to be restored and
then it can transition to DIRTY before transitioning to CLEAN. It looks
like how you are currently using it, INITIAL will be persisted across
the syscall path and not transition at all since it will be discarded at
the end anyways which makes sense.

> Most SR_VS
> bit ops are properly guarded, one thing that is missing on the current
> code is at __restore_v_state() in sigreturn. Therefore, to fully adopt
> this approach, we can do something like:
> - Use AMO for SR_FS cleaning
> - Use the legacy way to operate on SR_VS
> - Guard the vstate clearing op at sigreturn with
> get/put_cpu_vector_context(), and find any leftover code that does not
> properly guard it
> 
> AMO store on some systems may not be as fast as a regular store, that
> could be something worth benchmarking

Thinking about this more, this does not need to be atomic. To make that
claim I feel the need to give a potentially over thought explanation.
The condition we care about is that we are not missing a restore. Our
goal is to guarantee that everytime there is a save, that a restore
follows. It would be functionally acceptable if there are multiple
duplicated saves. But this is not a race that we are concerned about
because the two cases that we see when we are changing the FS/VS bits
are 1) save/restore happens on the same core so ordering will be correct
2) save happens on one core, gets migrated to another core, restore
happens but during the migration flushing is done so no concern there
either. The actual race we are seeing is that between reading the status
bit and doing the OR operation and then writing back, that the
underlying status register changes and we overwrite intermediate
changes. We can avoid this tearing by using WRITE_ONCE/READ_ONCE and we
do not need atomics.

> 
> [1]: https://lore.kernel.org/all/20261111184359.1239821-1-tchiu@tenstorrent.com/
> 
> Please let me know if you have any preference, thanks!

My hesitancy around adding to pt_regs is that it does end up eating up
some kernel stack space which is precious and I am not confident it is
necessary, and I would like to add to pt_regs only if it is necessary.

- Charlie

> 
> Cheers,
> Andy
> 
> > __fstate_off previously has the somewhat silly operation of
> > "(regs->status & ~SR_FS) | SR_FS_OFF" which is the same as "regs->status
> > & ~SR_FS" since SR_FS_OFF is 0. An atomic and can represent this as
> > well.
> >
> > The same idea should be true for vector.
> >
> > Let me know if I am missing some condition here :)
> >
> > --
> > - Charlie
> >

_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

      reply	other threads:[~2026-12-03  9:09 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30 22:01 [PATCH v1] riscv: fix missed update on an interrupted RMW of regs->status Andy Chiu
2026-12-01 18:10 ` Frédéric Mahé
2026-12-02  7:58 ` Charlie Jenkins
2026-12-02 23:33   ` [EXT] " Andy Chiu
2026-12-03  9:08     ` Charlie Jenkins [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=axExllv43XmKIvQB@blinky \
    --to=thecharlesjenkins@gmail.com \
    --cc=alex@ghiti.fr \
    --cc=andrew.jones@linux.dev \
    --cc=andybnac@gmail.com \
    --cc=anup@brainfault.org \
    --cc=aou@eecs.berkeley.edu \
    --cc=aurelien@aurel32.net \
    --cc=conor.dooley@microchip.com \
    --cc=cuiyunhui@bytedance.com \
    --cc=debug@rivosinc.com \
    --cc=dfustini@oss.tenstorrent.com \
    --cc=ebiggers@google.com \
    --cc=fustini@kernel.org \
    --cc=geomatsi@gmail.com \
    --cc=greentime.hu@sifive.com \
    --cc=guobin.zhang@intel.com \
    --cc=jszhang@kernel.org \
    --cc=kaiwenx@rivosinc.com \
    --cc=linux-riscv@lists.infradead.org \
    --cc=namcao@linutronix.de \
    --cc=oleg@redhat.com \
    --cc=palmer@dabbelt.com \
    --cc=pjw@kernel.org \
    --cc=samuel.holland@sifive.com \
    --cc=skunkolee@gmail.com \
    --cc=tchiu@tenstorrent.com \
    --cc=troy.mitchell@linux.dev \
    --cc=vulab@iscas.ac.cn \
    --cc=wangruikang@iscas.ac.cn \
    --cc=yang.yicong@picoheart.com \
    --cc=yongxuan.wang@sifive.com \
    --cc=zong.li@sifive.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox