All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andrew Jones <andrew.jones@linux.dev>
To: Koichiro Den <den@valinux.co.jp>
Cc: Anup Patel <anup@brainfault.org>,
	Atish Patra <atish.patra@linux.dev>,
	 Paul Walmsley <pjw@kernel.org>,
	Palmer Dabbelt <palmer@dabbelt.com>,
	 Albert Ou <aou@eecs.berkeley.edu>,
	Alexandre Ghiti <alex@ghiti.fr>,
	 Bingyu Xian <shanbeeyoo@gmail.com>,
	kvm@vger.kernel.org, kvm-riscv@lists.infradead.org,
	 linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 1/2] RISC-V: KVM: Batch user accesses when updating steal time
Date: Fri, 20 Nov 2026 17:40:29 +0100	[thread overview]
Message-ID: <awB1hZGh3LYztjqL@orel> (raw)
In-Reply-To: <20261118062242.2914385-2-den@valinux.co.jp>

On Wed, Nov 18, 2026 at 03:22:41PM +0900, Koichiro Den wrote:
> Updating the SBI STA shared structure performs 2x get_user() + 3x
> put_user() calls, each of which toggles sstatus.SUM.
> 
> Keep SUM enabled across all five accesses to the shared structure.
> 
> Note that write barriers are added to order the odd sequence store
> before the steal-time update and the even sequence store after it.

The barrier additions should probably be posted separately with a Fixes
tag.

> 
> Signed-off-by: Koichiro Den <den@valinux.co.jp>
> ---
> To reviewers: please let me know if you'd prefer
> scoped_user_rw_access() here.
> 
>  arch/riscv/kvm/vcpu_sbi_sta.c | 59 ++++++++++++++++++++---------------
>  1 file changed, 34 insertions(+), 25 deletions(-)
> 
> diff --git a/arch/riscv/kvm/vcpu_sbi_sta.c b/arch/riscv/kvm/vcpu_sbi_sta.c
> index 60e50296a008..cfc6f22c3e33 100644
> --- a/arch/riscv/kvm/vcpu_sbi_sta.c
> +++ b/arch/riscv/kvm/vcpu_sbi_sta.c
> @@ -8,13 +8,14 @@
>  #include <linux/kvm_host.h>
>  #include <linux/mm.h>
>  #include <linux/sizes.h>
> +#include <linux/uaccess.h>
>  
> +#include <asm/barrier.h>
>  #include <asm/bug.h>
>  #include <asm/current.h>
>  #include <asm/kvm_vcpu_sbi.h>
>  #include <asm/page.h>
>  #include <asm/sbi.h>
> -#include <asm/uaccess.h>
>  
>  static void kvm_riscv_vcpu_sbi_sta_reset(struct kvm_vcpu *vcpu)
>  {
> @@ -26,13 +27,13 @@ void kvm_riscv_vcpu_record_steal_time(struct kvm_vcpu *vcpu)
>  {
>  	gpa_t shmem = vcpu->arch.sta.shmem;
>  	u64 last_steal = vcpu->arch.sta.last_steal;
> -	__le32 __user *sequence_ptr;
> -	__le64 __user *steal_ptr;
> +	struct sbi_sta_struct __user *st;
>  	__le32 sequence_le;
>  	__le64 steal_le;
>  	u32 sequence;
>  	u64 steal;
>  	unsigned long hva;
> +	bool dirty = false, success = false;
>  	gfn_t gfn;
>  
>  	if (shmem == INVALID_GPA)
> @@ -51,31 +52,39 @@ void kvm_riscv_vcpu_record_steal_time(struct kvm_vcpu *vcpu)
>  		return;
>  	}
>  
> -	sequence_ptr = (__le32 __user *)(hva + offset_in_page(shmem) +
> -			       offsetof(struct sbi_sta_struct, sequence));
> -	steal_ptr = (__le64 __user *)(hva + offset_in_page(shmem) +
> -			    offsetof(struct sbi_sta_struct, steal));
> -
> -	if (WARN_ON(get_user(sequence_le, sequence_ptr)))
> -		return;
> -
> -	sequence = le32_to_cpu(sequence_le);
> -	sequence += 1;
> -
> -	if (WARN_ON(put_user(cpu_to_le32(sequence), sequence_ptr)))
> +	st = (struct sbi_sta_struct __user *)(hva + offset_in_page(shmem));
> +	might_fault();

We're probably safe to drop this might_fault(). But either way it's fine.

> +	if (WARN_ON(!user_access_begin(st, sizeof(*st))))
>  		return;
>  
> -	if (!WARN_ON(get_user(steal_le, steal_ptr))) {
> -		steal = le64_to_cpu(steal_le);
> -		vcpu->arch.sta.last_steal = READ_ONCE(current->sched_info.run_delay);
> -		steal += vcpu->arch.sta.last_steal - last_steal;
> -		WARN_ON(put_user(cpu_to_le64(steal), steal_ptr));
> -	}
> -
> +	unsafe_get_user(sequence_le, &st->sequence, fault);
> +	sequence = le32_to_cpu(sequence_le) + 1;
> +	unsafe_put_user(cpu_to_le32(sequence), &st->sequence, fault);
> +	dirty = true;
> +
> +	/* Publish the odd sequence before updating stolen time. */
> +	smp_wmb();
> +	unsafe_get_user(steal_le, &st->steal, update_sequence);
> +	steal = le64_to_cpu(steal_le);
> +	vcpu->arch.sta.last_steal = READ_ONCE(current->sched_info.run_delay);
> +	steal += vcpu->arch.sta.last_steal - last_steal;
> +	unsafe_put_user(cpu_to_le64(steal), &st->steal, update_sequence);
> +	success = true;
> +
> +update_sequence:
> +	/* Publish stolen time before completing the update. */
> +	smp_wmb();
>  	sequence += 1;
> -	WARN_ON(put_user(cpu_to_le32(sequence), sequence_ptr));
> -
> -	kvm_vcpu_mark_page_dirty(vcpu, gfn);
> +	unsafe_put_user(cpu_to_le32(sequence), &st->sequence, fault);
> +	goto out;
> +
> +fault:
> +	success = false;
> +out:
> +	user_access_end();
> +	WARN_ON(!success);
> +	if (dirty)
> +		kvm_vcpu_mark_page_dirty(vcpu, gfn);
>  }
>  
>  static int kvm_sbi_sta_steal_time_set_shmem(struct kvm_vcpu *vcpu)
> -- 
> 2.54.0
>

Otherwise,

Reviewed-by: Andrew Jones <andrew.jones@linux.dev>

WARNING: multiple messages have this Message-ID (diff)
From: Andrew Jones <andrew.jones@linux.dev>
To: Koichiro Den <den@valinux.co.jp>
Cc: Anup Patel <anup@brainfault.org>,
	Atish Patra <atish.patra@linux.dev>,
	 Paul Walmsley <pjw@kernel.org>,
	Palmer Dabbelt <palmer@dabbelt.com>,
	 Albert Ou <aou@eecs.berkeley.edu>,
	Alexandre Ghiti <alex@ghiti.fr>,
	 Bingyu Xian <shanbeeyoo@gmail.com>,
	kvm@vger.kernel.org, kvm-riscv@lists.infradead.org,
	 linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 1/2] RISC-V: KVM: Batch user accesses when updating steal time
Date: Fri, 20 Nov 2026 17:40:29 +0100	[thread overview]
Message-ID: <awB1hZGh3LYztjqL@orel> (raw)
In-Reply-To: <20261118062242.2914385-2-den@valinux.co.jp>

On Wed, Nov 18, 2026 at 03:22:41PM +0900, Koichiro Den wrote:
> Updating the SBI STA shared structure performs 2x get_user() + 3x
> put_user() calls, each of which toggles sstatus.SUM.
> 
> Keep SUM enabled across all five accesses to the shared structure.
> 
> Note that write barriers are added to order the odd sequence store
> before the steal-time update and the even sequence store after it.

The barrier additions should probably be posted separately with a Fixes
tag.

> 
> Signed-off-by: Koichiro Den <den@valinux.co.jp>
> ---
> To reviewers: please let me know if you'd prefer
> scoped_user_rw_access() here.
> 
>  arch/riscv/kvm/vcpu_sbi_sta.c | 59 ++++++++++++++++++++---------------
>  1 file changed, 34 insertions(+), 25 deletions(-)
> 
> diff --git a/arch/riscv/kvm/vcpu_sbi_sta.c b/arch/riscv/kvm/vcpu_sbi_sta.c
> index 60e50296a008..cfc6f22c3e33 100644
> --- a/arch/riscv/kvm/vcpu_sbi_sta.c
> +++ b/arch/riscv/kvm/vcpu_sbi_sta.c
> @@ -8,13 +8,14 @@
>  #include <linux/kvm_host.h>
>  #include <linux/mm.h>
>  #include <linux/sizes.h>
> +#include <linux/uaccess.h>
>  
> +#include <asm/barrier.h>
>  #include <asm/bug.h>
>  #include <asm/current.h>
>  #include <asm/kvm_vcpu_sbi.h>
>  #include <asm/page.h>
>  #include <asm/sbi.h>
> -#include <asm/uaccess.h>
>  
>  static void kvm_riscv_vcpu_sbi_sta_reset(struct kvm_vcpu *vcpu)
>  {
> @@ -26,13 +27,13 @@ void kvm_riscv_vcpu_record_steal_time(struct kvm_vcpu *vcpu)
>  {
>  	gpa_t shmem = vcpu->arch.sta.shmem;
>  	u64 last_steal = vcpu->arch.sta.last_steal;
> -	__le32 __user *sequence_ptr;
> -	__le64 __user *steal_ptr;
> +	struct sbi_sta_struct __user *st;
>  	__le32 sequence_le;
>  	__le64 steal_le;
>  	u32 sequence;
>  	u64 steal;
>  	unsigned long hva;
> +	bool dirty = false, success = false;
>  	gfn_t gfn;
>  
>  	if (shmem == INVALID_GPA)
> @@ -51,31 +52,39 @@ void kvm_riscv_vcpu_record_steal_time(struct kvm_vcpu *vcpu)
>  		return;
>  	}
>  
> -	sequence_ptr = (__le32 __user *)(hva + offset_in_page(shmem) +
> -			       offsetof(struct sbi_sta_struct, sequence));
> -	steal_ptr = (__le64 __user *)(hva + offset_in_page(shmem) +
> -			    offsetof(struct sbi_sta_struct, steal));
> -
> -	if (WARN_ON(get_user(sequence_le, sequence_ptr)))
> -		return;
> -
> -	sequence = le32_to_cpu(sequence_le);
> -	sequence += 1;
> -
> -	if (WARN_ON(put_user(cpu_to_le32(sequence), sequence_ptr)))
> +	st = (struct sbi_sta_struct __user *)(hva + offset_in_page(shmem));
> +	might_fault();

We're probably safe to drop this might_fault(). But either way it's fine.

> +	if (WARN_ON(!user_access_begin(st, sizeof(*st))))
>  		return;
>  
> -	if (!WARN_ON(get_user(steal_le, steal_ptr))) {
> -		steal = le64_to_cpu(steal_le);
> -		vcpu->arch.sta.last_steal = READ_ONCE(current->sched_info.run_delay);
> -		steal += vcpu->arch.sta.last_steal - last_steal;
> -		WARN_ON(put_user(cpu_to_le64(steal), steal_ptr));
> -	}
> -
> +	unsafe_get_user(sequence_le, &st->sequence, fault);
> +	sequence = le32_to_cpu(sequence_le) + 1;
> +	unsafe_put_user(cpu_to_le32(sequence), &st->sequence, fault);
> +	dirty = true;
> +
> +	/* Publish the odd sequence before updating stolen time. */
> +	smp_wmb();
> +	unsafe_get_user(steal_le, &st->steal, update_sequence);
> +	steal = le64_to_cpu(steal_le);
> +	vcpu->arch.sta.last_steal = READ_ONCE(current->sched_info.run_delay);
> +	steal += vcpu->arch.sta.last_steal - last_steal;
> +	unsafe_put_user(cpu_to_le64(steal), &st->steal, update_sequence);
> +	success = true;
> +
> +update_sequence:
> +	/* Publish stolen time before completing the update. */
> +	smp_wmb();
>  	sequence += 1;
> -	WARN_ON(put_user(cpu_to_le32(sequence), sequence_ptr));
> -
> -	kvm_vcpu_mark_page_dirty(vcpu, gfn);
> +	unsafe_put_user(cpu_to_le32(sequence), &st->sequence, fault);
> +	goto out;
> +
> +fault:
> +	success = false;
> +out:
> +	user_access_end();
> +	WARN_ON(!success);
> +	if (dirty)
> +		kvm_vcpu_mark_page_dirty(vcpu, gfn);
>  }
>  
>  static int kvm_sbi_sta_steal_time_set_shmem(struct kvm_vcpu *vcpu)
> -- 
> 2.54.0
>

Otherwise,

Reviewed-by: Andrew Jones <andrew.jones@linux.dev>

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

WARNING: multiple messages have this Message-ID (diff)
From: Andrew Jones <andrew.jones@linux.dev>
To: Koichiro Den <den@valinux.co.jp>
Cc: Anup Patel <anup@brainfault.org>,
	Atish Patra <atish.patra@linux.dev>,
	 Paul Walmsley <pjw@kernel.org>,
	Palmer Dabbelt <palmer@dabbelt.com>,
	 Albert Ou <aou@eecs.berkeley.edu>,
	Alexandre Ghiti <alex@ghiti.fr>,
	 Bingyu Xian <shanbeeyoo@gmail.com>,
	kvm@vger.kernel.org, kvm-riscv@lists.infradead.org,
	 linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 1/2] RISC-V: KVM: Batch user accesses when updating steal time
Date: Fri, 20 Nov 2026 17:40:29 +0100	[thread overview]
Message-ID: <awB1hZGh3LYztjqL@orel> (raw)
In-Reply-To: <20261118062242.2914385-2-den@valinux.co.jp>

On Wed, Nov 18, 2026 at 03:22:41PM +0900, Koichiro Den wrote:
> Updating the SBI STA shared structure performs 2x get_user() + 3x
> put_user() calls, each of which toggles sstatus.SUM.
> 
> Keep SUM enabled across all five accesses to the shared structure.
> 
> Note that write barriers are added to order the odd sequence store
> before the steal-time update and the even sequence store after it.

The barrier additions should probably be posted separately with a Fixes
tag.

> 
> Signed-off-by: Koichiro Den <den@valinux.co.jp>
> ---
> To reviewers: please let me know if you'd prefer
> scoped_user_rw_access() here.
> 
>  arch/riscv/kvm/vcpu_sbi_sta.c | 59 ++++++++++++++++++++---------------
>  1 file changed, 34 insertions(+), 25 deletions(-)
> 
> diff --git a/arch/riscv/kvm/vcpu_sbi_sta.c b/arch/riscv/kvm/vcpu_sbi_sta.c
> index 60e50296a008..cfc6f22c3e33 100644
> --- a/arch/riscv/kvm/vcpu_sbi_sta.c
> +++ b/arch/riscv/kvm/vcpu_sbi_sta.c
> @@ -8,13 +8,14 @@
>  #include <linux/kvm_host.h>
>  #include <linux/mm.h>
>  #include <linux/sizes.h>
> +#include <linux/uaccess.h>
>  
> +#include <asm/barrier.h>
>  #include <asm/bug.h>
>  #include <asm/current.h>
>  #include <asm/kvm_vcpu_sbi.h>
>  #include <asm/page.h>
>  #include <asm/sbi.h>
> -#include <asm/uaccess.h>
>  
>  static void kvm_riscv_vcpu_sbi_sta_reset(struct kvm_vcpu *vcpu)
>  {
> @@ -26,13 +27,13 @@ void kvm_riscv_vcpu_record_steal_time(struct kvm_vcpu *vcpu)
>  {
>  	gpa_t shmem = vcpu->arch.sta.shmem;
>  	u64 last_steal = vcpu->arch.sta.last_steal;
> -	__le32 __user *sequence_ptr;
> -	__le64 __user *steal_ptr;
> +	struct sbi_sta_struct __user *st;
>  	__le32 sequence_le;
>  	__le64 steal_le;
>  	u32 sequence;
>  	u64 steal;
>  	unsigned long hva;
> +	bool dirty = false, success = false;
>  	gfn_t gfn;
>  
>  	if (shmem == INVALID_GPA)
> @@ -51,31 +52,39 @@ void kvm_riscv_vcpu_record_steal_time(struct kvm_vcpu *vcpu)
>  		return;
>  	}
>  
> -	sequence_ptr = (__le32 __user *)(hva + offset_in_page(shmem) +
> -			       offsetof(struct sbi_sta_struct, sequence));
> -	steal_ptr = (__le64 __user *)(hva + offset_in_page(shmem) +
> -			    offsetof(struct sbi_sta_struct, steal));
> -
> -	if (WARN_ON(get_user(sequence_le, sequence_ptr)))
> -		return;
> -
> -	sequence = le32_to_cpu(sequence_le);
> -	sequence += 1;
> -
> -	if (WARN_ON(put_user(cpu_to_le32(sequence), sequence_ptr)))
> +	st = (struct sbi_sta_struct __user *)(hva + offset_in_page(shmem));
> +	might_fault();

We're probably safe to drop this might_fault(). But either way it's fine.

> +	if (WARN_ON(!user_access_begin(st, sizeof(*st))))
>  		return;
>  
> -	if (!WARN_ON(get_user(steal_le, steal_ptr))) {
> -		steal = le64_to_cpu(steal_le);
> -		vcpu->arch.sta.last_steal = READ_ONCE(current->sched_info.run_delay);
> -		steal += vcpu->arch.sta.last_steal - last_steal;
> -		WARN_ON(put_user(cpu_to_le64(steal), steal_ptr));
> -	}
> -
> +	unsafe_get_user(sequence_le, &st->sequence, fault);
> +	sequence = le32_to_cpu(sequence_le) + 1;
> +	unsafe_put_user(cpu_to_le32(sequence), &st->sequence, fault);
> +	dirty = true;
> +
> +	/* Publish the odd sequence before updating stolen time. */
> +	smp_wmb();
> +	unsafe_get_user(steal_le, &st->steal, update_sequence);
> +	steal = le64_to_cpu(steal_le);
> +	vcpu->arch.sta.last_steal = READ_ONCE(current->sched_info.run_delay);
> +	steal += vcpu->arch.sta.last_steal - last_steal;
> +	unsafe_put_user(cpu_to_le64(steal), &st->steal, update_sequence);
> +	success = true;
> +
> +update_sequence:
> +	/* Publish stolen time before completing the update. */
> +	smp_wmb();
>  	sequence += 1;
> -	WARN_ON(put_user(cpu_to_le32(sequence), sequence_ptr));
> -
> -	kvm_vcpu_mark_page_dirty(vcpu, gfn);
> +	unsafe_put_user(cpu_to_le32(sequence), &st->sequence, fault);
> +	goto out;
> +
> +fault:
> +	success = false;
> +out:
> +	user_access_end();
> +	WARN_ON(!success);
> +	if (dirty)
> +		kvm_vcpu_mark_page_dirty(vcpu, gfn);
>  }
>  
>  static int kvm_sbi_sta_steal_time_set_shmem(struct kvm_vcpu *vcpu)
> -- 
> 2.54.0
>

Otherwise,

Reviewed-by: Andrew Jones <andrew.jones@linux.dev>

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

  reply	other threads:[~2026-11-20 16:41 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18  6:22 [PATCH 0/2] RISC-V: KVM: Lessen steal time update overhead Koichiro Den
2026-11-18  6:22 ` Koichiro Den
2026-11-18  6:22 ` Koichiro Den
2026-11-18  6:22 ` [PATCH 1/2] RISC-V: KVM: Batch user accesses when updating steal time Koichiro Den
2026-11-18  6:22   ` Koichiro Den
2026-11-18  6:22   ` Koichiro Den
2026-11-20 16:40   ` Andrew Jones [this message]
2026-11-20 16:40     ` Andrew Jones
2026-11-20 16:40     ` Andrew Jones
2026-11-20 17:20     ` Koichiro Den
2026-11-20 17:20       ` Koichiro Den
2026-11-20 17:20       ` Koichiro Den
2026-11-18  6:22 ` [PATCH 2/2] RISC-V: KVM: Skip steal-time updates when run_delay is unchanged Koichiro Den
2026-11-18  6:22   ` Koichiro Den
2026-11-18  6:22   ` Koichiro Den
2026-11-20 16:45   ` Andrew Jones
2026-11-20 16:45     ` Andrew Jones
2026-11-20 16:45     ` Andrew Jones
2026-11-20 17:25     ` Koichiro Den
2026-11-20 17:25       ` Koichiro Den
2026-11-20 17:25       ` Koichiro Den

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=awB1hZGh3LYztjqL@orel \
    --to=andrew.jones@linux.dev \
    --cc=alex@ghiti.fr \
    --cc=anup@brainfault.org \
    --cc=aou@eecs.berkeley.edu \
    --cc=atish.patra@linux.dev \
    --cc=den@valinux.co.jp \
    --cc=kvm-riscv@lists.infradead.org \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-riscv@lists.infradead.org \
    --cc=palmer@dabbelt.com \
    --cc=pjw@kernel.org \
    --cc=shanbeeyoo@gmail.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 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.