From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A10A4606A60A for ; Fri, 20 Nov 2026 16:41:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=ybxHXew+; dkim=fail reason="signature verification failed" (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=bJNgXs/n DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References: Message-ID:Subject:Cc:To:From:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Unsubscribe-Post:List-Owner; bh=41CZEzchoUf7+NwWP1OdbIwJbvjdgIgLagwxX7ESD0E=; b=ybxHXew+1w/7xZBaifG03aFlmB WnRT5L64PV58t7d3PrSO+dWkxrkl/gqRqOf+r093jzETCU3xrY8+eFJDeKSg28TyKBjglcck27l7i L0lg9PDqTXXsXdKUrbU7a5zOS9YuEnm8ZgNChBupetV47FI8QGBmuv3kW/zQPupoRU/7bgv0ZLQpQ ysHsP+VtpCx3leeBkcP6be0uERsVHkUl1uoQ4xCiOXWzCHZVoK4NOboHsaRGwF1MRDb66I0Myhna/ c/W1Or51U69udiNmci+dEBLp7FHSztQQLWQzrplqR1OOw2LVjj+tlykynGU61aqhYC1lrhAQqy82l 83ckxOqg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xURfm-0000000FZoK-2q9x; Fri, 20 Nov 2026 16:41:14 +0000 Received: from out-201.mta1.migadu.com ([95.215.58.201] helo=mta1.migadu.com) by bombadil.infradead.org with esmtps (Exim 4.100.1 #2 (Red Hat Linux)) id 1xURfe-0000000FZmH-3iMK for linux-riscv@lists.infradead.org; Fri, 20 Nov 2026 16:41:13 +0000 X-Envelope-To: linux-riscv@lists.infradead.org DKIM-Signature: a=rsa-sha256; bh=ZoM2yf3MA8k5sZbw96Hh1V2kwRR1l1ZCz3uIHytu5jY=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1795192864; v=1; x=1795797664; b=bJNgXs/n1QT3IoJuJIbKvzdSmhhdwzylK2Sj7kxe2/0hHFFfJyWva9lJHMhWlt1dxYYqWpz4 IB+fG3eztkt5lXlMnmK3H7g9/dIsemLKV5O99g6qXR6goFI5VLkjhu2I28PIbtErTczMU5ui1eo 5rkqrgpOrrDhDpzG5McbIsbI= X-Envelope-To: linux-riscv@lists.infradead.org Received: by smtp.migadu.com with ESMTPS id 883b19b9dfbc7c0f; Fri, 20 Nov 2026 16:41:04 +0000 X-Mizu-Trace-ID: 883b19b9dfbc7c0f X-Migadu-Flow: FLOW_OUT Date: Fri, 20 Nov 2026 17:40:29 +0100 From: Andrew Jones To: Koichiro Den Cc: Anup Patel , Atish Patra , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti , Bingyu Xian , 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 Message-ID: References: <20261118062242.2914385-1-den@valinux.co.jp> <20261118062242.2914385-2-den@valinux.co.jp> X-Archived-By: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20261118062242.2914385-2-den@valinux.co.jp> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.9.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20261120_084108_835565_86938DCA X-CRM114-Status: GOOD ( 20.05 ) X-Spam-Score: -2.1 (--) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-riscv" X-Spam-Status: No, score=-13.9 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 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 > --- > 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 > #include > #include > +#include > > +#include > #include > #include > #include > #include > #include > -#include > > 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 _______________________________________________ linux-riscv mailing list linux-riscv@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-riscv