All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Radim Krčmář" <rkrcmar@ventanamicro.com>
To: "Vivian Wang" <wangruikang@iscas.ac.cn>,
	"Drew Fustini" <fustini@kernel.org>,
	"Palmer Dabbelt" <palmer@dabbelt.com>,
	"Björn Töpel" <bjorn@rivosinc.com>,
	"Alexandre Ghiti" <alex@ghiti.fr>,
	"Paul Walmsley" <paul.walmsley@sifive.com>,
	"Samuel Holland" <samuel.holland@sifive.com>,
	"Drew Fustini" <dfustini@tenstorrent.com>,
	"Andy Chiu" <andybnac@gmail.com>,
	"Conor Dooley" <conor.dooley@microchip.com>,
	linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org
Cc: "linux-riscv" <linux-riscv-bounces@lists.infradead.org>
Subject: Re: [PATCH] riscv: Add sysctl to control discard of vstate during syscall
Date: Fri, 25 Jul 2025 20:47:04 +0200	[thread overview]
Message-ID: <DBLCYPBLQBSQ.170ND7Z93GPK4@ventanamicro.com> (raw)
In-Reply-To: <feb3549f-da91-4eaa-a624-b9f35db6ba3c@iscas.ac.cn>

2025-07-25T23:01:03+08:00, Vivian Wang <wangruikang@iscas.ac.cn>:
> On 7/25/25 18:18, Radim Krčmář wrote:
>> 2025-07-24T05:55:54+08:00, Vivian Wang <wangruikang@iscas.ac.cn>:
>>> On 7/19/25 11:39, Drew Fustini wrote:
>>>> From: Drew Fustini <dfustini@tenstorrent.com>
>>>> Clobbering the vector registers can significantly increase system call
>>>> latency for some implementations. To mitigate this performance impact, a
>>>> policy mechanism is provided to administrators, distro maintainers, and
>>>> developers to control vector state discard in the form of a sysctl knob:
>>> So I had an idea: Is it possible to avoid repeatedly discarding the
>>> state on every syscall by setting VS to Initial after discarding, and
>>> avoiding discarding when VS is Initial? So:
>>>
>>> if (VS == Clean || VS == Dirty) {
>>>     clobber;
>>>     VS = Initial;
>>> }
>>>
>>> This would avoid this problem with syscall-heavy user programs while
>>> adding minimum overhead for everything else.
>> I think your proposal improves the existing code, but if a userspace is
>> using vectors, it's likely also restoring them after a syscall, so the
>> state would immediately get dirty, and the next syscall would again
>> needlessly clobber vector registers.
>
> Without any data to back it up, I would say that my understanding is
> that this should be a rare case, only happening if e.g. someone is
> adding printf debugging to their vector code. Otherwise, vector loops
> should not have syscalls in them.
>
> A more reasonable worry would be programs using RVV everywhere in all
> sorts of common operations. In that case, alternating syscalls and
> vectors would make the discarding wasteful.

Good point.  Yeah, auto-vectorization might be hindered.

In the worst case, users could just notice that it's slowing programs
down, and disable it without looking for the cause.

>> Preserving the vector state still seems better for userspaces that use
>> both vectors and syscalls.
>
> If we can expect e.g. userspace programs to primarily repeatedly use RVV
> with no syscalls between loops, *or* primarily repeatedly use syscalls
> with rare occurrences of RVV between syscalls. This way, the primarily
> syscall programs can benefit from slightly switching, since there's no
> need to save and restore state for those most of the time. In effect,
> syscalls serves as a hint that RVV is over.

This would need deeper analysis, and we will probably never be correct
with a system-wide policy regardless -- a room for prctl?

I think there might be a lot of programs that have a repeating pattern
of compute -> syscall (e.g. to write results), and clobbering is losing
performance if a program does more than a single loop per switch.

>                                             The primarily RVV programs
> should not be switching as much - if they are, that's a sign of CPU
> resources being oversubscribed.

Yes, but clobbering only gives benefits on a switch, so we don't want to
clobber if there are more syscall than switches.

Well, there is a way: a syscall could just set VS=Initial, and if
userspace doesn't dirty vector registers, a restore would set the
registers to whatever the initial state is.
No vector registers touched on syscall, or save.

This works as we don't have to do anything when "clobbering" -- the
registers are unspecified after a syscall.
The downside is that users might (incorrectly) depend on the unspecified
value without dirtying, so the unspecified value could change at an
arbitrary point, which would provide some interesting debugging cases.

(And it's still suboptimal if software actually wants to preserve
 vectors across syscalls.)

> Having said all of that, I am actually slightly more interested in why
> vmv.v.vi is *so slow* on SiFive X280. I wonder if there would be a more
> microarchitectural favorable ways to just put a bunch of ones in some
> vector registers? Would 0 be better?

No idea, and there are a lot of options to try, but it would be quite
sad if we had to have special case for each implementation.

Thanks.

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

WARNING: multiple messages have this Message-ID (diff)
From: "Radim Krčmář" <rkrcmar@ventanamicro.com>
To: "Vivian Wang" <wangruikang@iscas.ac.cn>,
	"Drew Fustini" <fustini@kernel.org>,
	"Palmer Dabbelt" <palmer@dabbelt.com>,
	"Björn Töpel" <bjorn@rivosinc.com>,
	"Alexandre Ghiti" <alex@ghiti.fr>,
	"Paul Walmsley" <paul.walmsley@sifive.com>,
	"Samuel Holland" <samuel.holland@sifive.com>,
	"Drew Fustini" <dfustini@tenstorrent.com>,
	"Andy Chiu" <andybnac@gmail.com>,
	"Conor Dooley" <conor.dooley@microchip.com>,
	linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org
Cc: "linux-riscv" <linux-riscv-bounces@lists.infradead.org>
Subject: Re: [PATCH] riscv: Add sysctl to control discard of vstate during syscall
Date: Fri, 25 Jul 2025 20:47:04 +0200	[thread overview]
Message-ID: <DBLCYPBLQBSQ.170ND7Z93GPK4@ventanamicro.com> (raw)
In-Reply-To: <feb3549f-da91-4eaa-a624-b9f35db6ba3c@iscas.ac.cn>

2025-07-25T23:01:03+08:00, Vivian Wang <wangruikang@iscas.ac.cn>:
> On 7/25/25 18:18, Radim Krčmář wrote:
>> 2025-07-24T05:55:54+08:00, Vivian Wang <wangruikang@iscas.ac.cn>:
>>> On 7/19/25 11:39, Drew Fustini wrote:
>>>> From: Drew Fustini <dfustini@tenstorrent.com>
>>>> Clobbering the vector registers can significantly increase system call
>>>> latency for some implementations. To mitigate this performance impact, a
>>>> policy mechanism is provided to administrators, distro maintainers, and
>>>> developers to control vector state discard in the form of a sysctl knob:
>>> So I had an idea: Is it possible to avoid repeatedly discarding the
>>> state on every syscall by setting VS to Initial after discarding, and
>>> avoiding discarding when VS is Initial? So:
>>>
>>> if (VS == Clean || VS == Dirty) {
>>>     clobber;
>>>     VS = Initial;
>>> }
>>>
>>> This would avoid this problem with syscall-heavy user programs while
>>> adding minimum overhead for everything else.
>> I think your proposal improves the existing code, but if a userspace is
>> using vectors, it's likely also restoring them after a syscall, so the
>> state would immediately get dirty, and the next syscall would again
>> needlessly clobber vector registers.
>
> Without any data to back it up, I would say that my understanding is
> that this should be a rare case, only happening if e.g. someone is
> adding printf debugging to their vector code. Otherwise, vector loops
> should not have syscalls in them.
>
> A more reasonable worry would be programs using RVV everywhere in all
> sorts of common operations. In that case, alternating syscalls and
> vectors would make the discarding wasteful.

Good point.  Yeah, auto-vectorization might be hindered.

In the worst case, users could just notice that it's slowing programs
down, and disable it without looking for the cause.

>> Preserving the vector state still seems better for userspaces that use
>> both vectors and syscalls.
>
> If we can expect e.g. userspace programs to primarily repeatedly use RVV
> with no syscalls between loops, *or* primarily repeatedly use syscalls
> with rare occurrences of RVV between syscalls. This way, the primarily
> syscall programs can benefit from slightly switching, since there's no
> need to save and restore state for those most of the time. In effect,
> syscalls serves as a hint that RVV is over.

This would need deeper analysis, and we will probably never be correct
with a system-wide policy regardless -- a room for prctl?

I think there might be a lot of programs that have a repeating pattern
of compute -> syscall (e.g. to write results), and clobbering is losing
performance if a program does more than a single loop per switch.

>                                             The primarily RVV programs
> should not be switching as much - if they are, that's a sign of CPU
> resources being oversubscribed.

Yes, but clobbering only gives benefits on a switch, so we don't want to
clobber if there are more syscall than switches.

Well, there is a way: a syscall could just set VS=Initial, and if
userspace doesn't dirty vector registers, a restore would set the
registers to whatever the initial state is.
No vector registers touched on syscall, or save.

This works as we don't have to do anything when "clobbering" -- the
registers are unspecified after a syscall.
The downside is that users might (incorrectly) depend on the unspecified
value without dirtying, so the unspecified value could change at an
arbitrary point, which would provide some interesting debugging cases.

(And it's still suboptimal if software actually wants to preserve
 vectors across syscalls.)

> Having said all of that, I am actually slightly more interested in why
> vmv.v.vi is *so slow* on SiFive X280. I wonder if there would be a more
> microarchitectural favorable ways to just put a bunch of ones in some
> vector registers? Would 0 be better?

No idea, and there are a lot of options to try, but it would be quite
sad if we had to have special case for each implementation.

Thanks.

  reply	other threads:[~2025-07-25 18:47 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-19  3:39 [PATCH] riscv: Add sysctl to control discard of vstate during syscall Drew Fustini
2025-07-19  3:39 ` Drew Fustini
2025-07-21 12:13 ` Darius Rad
2025-07-21 12:13   ` Darius Rad
2025-07-21 20:59   ` Drew Fustini
2025-07-21 20:59     ` Drew Fustini
2025-07-21 21:28     ` Drew Fustini
2025-07-21 21:28       ` Drew Fustini
2025-07-21 12:35 ` Radim Krčmář
2025-07-21 12:35   ` Radim Krčmář
2025-07-21 14:54   ` Radim Krčmář
2025-07-21 14:54     ` Radim Krčmář
2025-07-21 21:20     ` Drew Fustini
2025-07-21 21:20       ` Drew Fustini
2025-07-31  1:05     ` Palmer Dabbelt
2025-07-31  1:05       ` Palmer Dabbelt
2025-07-31 12:24       ` Radim Krčmář
2025-07-31 12:24         ` Radim Krčmář
2025-08-01 21:41       ` Drew Fustini
2025-08-01 21:41         ` Drew Fustini
2025-08-05 18:51         ` Drew Fustini
2025-08-05 18:51           ` Drew Fustini
2025-07-21 21:16   ` Drew Fustini
2025-07-21 21:16     ` Drew Fustini
2025-07-27 17:29     ` Drew Fustini
2025-07-27 17:29       ` Drew Fustini
2025-07-23 21:55 ` Vivian Wang
2025-07-23 21:55   ` Vivian Wang
2025-07-25 10:18   ` Radim Krčmář
2025-07-25 10:18     ` Radim Krčmář
2025-07-25 15:01     ` Vivian Wang
2025-07-25 15:01       ` Vivian Wang
2025-07-25 18:47       ` Radim Krčmář [this message]
2025-07-25 18:47         ` Radim Krčmář
2025-07-26 18:37         ` Drew Fustini
2025-07-26 18:37           ` Drew Fustini

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=DBLCYPBLQBSQ.170ND7Z93GPK4@ventanamicro.com \
    --to=rkrcmar@ventanamicro.com \
    --cc=alex@ghiti.fr \
    --cc=andybnac@gmail.com \
    --cc=bjorn@rivosinc.com \
    --cc=conor.dooley@microchip.com \
    --cc=dfustini@tenstorrent.com \
    --cc=fustini@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-riscv-bounces@lists.infradead.org \
    --cc=linux-riscv@lists.infradead.org \
    --cc=palmer@dabbelt.com \
    --cc=paul.walmsley@sifive.com \
    --cc=samuel.holland@sifive.com \
    --cc=wangruikang@iscas.ac.cn \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.