From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C7FABC49EA1 for ; Tue, 6 Aug 2024 23:00:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:In-Reply-To:Content-Type: MIME-Version:References:Message-ID:Subject:Cc:To:From:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=WqjCWx3iKIIOBiEbdFwU6vk7tX5x9+cQDaS5Z4GhKug=; b=nwAyM+K8Yf1APyyTQzKBnmrFlZ CRg6ZQ8W9ATIyyRgRHmYi1tZwizgQIzNB43jxphe+iSoH4QizR+idTHUUc1GSwggjsrqGsXPQh3Ex LYBowj6X6m+PFw1P6RMRmcrY5t1qXzYm1AzFkahRjbGHrgaOOFpRfxYax2ZhmngVsDQDyYkymBWx1 eIrk+PKBu+sd4+B+EirR2ttQukibrP4HyWPspPFdXd4U9/RxnYMwimlgWKiBwXggH7nBF0sWjdCbQ UBxqGUb7PCOojfmZ5BhIIF1D6PFowd6qsAFbtfvJJb5CGID9I/POUaioab1Z99Y7x+m+QeibNIvBS doZFoUZA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sbTA5-00000003EVG-0vLr; Tue, 06 Aug 2024 23:00:13 +0000 Received: from out-179.mta0.migadu.com ([91.218.175.179]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sbT8x-00000003EBt-1Ex8 for linux-arm-kernel@lists.infradead.org; Tue, 06 Aug 2024 22:59:05 +0000 Date: Tue, 6 Aug 2024 15:58:52 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1722985137; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=WqjCWx3iKIIOBiEbdFwU6vk7tX5x9+cQDaS5Z4GhKug=; b=MT7SOl/8Qd4gf4AEhDylb4Tm6ZeYRaOaSslKQvFF4ia4a3atanaVQO6Og1knthtaT3Wpvu f1jJgXNaYtwNpEJFN/BBB/A1miGtQhrkA/cL3/4wYElzBYDnAH/craMZsRCUWOso2hStin AowjlBMtaUV65lr67ZlbOX8CE/OebHI= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Oliver Upton To: Sean Christopherson Cc: Marc Zyngier , Paolo Bonzini , linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Steve Rutherford Subject: Re: [PATCH 2/2] KVM: Protect vCPU's "last run PID" with rwlock, not RCU Message-ID: References: <20240802200136.329973-1-seanjc@google.com> <20240802200136.329973-3-seanjc@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240802200136.329973-3-seanjc@google.com> X-Migadu-Flow: FLOW_OUT X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240806_155903_801281_1B412F28 X-CRM114-Status: GOOD ( 20.23 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Fri, Aug 02, 2024 at 01:01:36PM -0700, Sean Christopherson wrote: > To avoid jitter on KVM_RUN due to synchronize_rcu(), use a rwlock instead > of RCU to protect vcpu->pid, a.k.a. the pid of the task last used to a > vCPU. When userspace is doing M:N scheduling of tasks to vCPUs, e.g. to > run SEV migration helper vCPUs during post-copy, the synchronize_rcu() > needed to change the PID associated with the vCPU can stall for hundreds > of milliseconds, which is problematic for latency sensitive post-copy > operations. > > In the directed yield path, do not acquire the lock if it's contended, > i.e. if the associated PID is changing, as that means the vCPU's task is > already running. > > Reported-by: Steve Rutherford > Signed-off-by: Sean Christopherson > --- > arch/arm64/include/asm/kvm_host.h | 2 +- > include/linux/kvm_host.h | 3 ++- > virt/kvm/kvm_main.c | 32 +++++++++++++++++-------------- > 3 files changed, 21 insertions(+), 16 deletions(-) > > diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h > index a33f5996ca9f..7199cb014806 100644 > --- a/arch/arm64/include/asm/kvm_host.h > +++ b/arch/arm64/include/asm/kvm_host.h > @@ -1115,7 +1115,7 @@ int __kvm_arm_vcpu_set_events(struct kvm_vcpu *vcpu, > void kvm_arm_halt_guest(struct kvm *kvm); > void kvm_arm_resume_guest(struct kvm *kvm); > > -#define vcpu_has_run_once(vcpu) !!rcu_access_pointer((vcpu)->pid) > +#define vcpu_has_run_once(vcpu) (!!READ_ONCE((vcpu)->pid)) > > #ifndef __KVM_NVHE_HYPERVISOR__ > #define kvm_call_hyp_nvhe(f, ...) \ > diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h > index 689e8be873a7..d6f4e8b2b44c 100644 > --- a/include/linux/kvm_host.h > +++ b/include/linux/kvm_host.h > @@ -342,7 +342,8 @@ struct kvm_vcpu { > #ifndef __KVM_HAVE_ARCH_WQP > struct rcuwait wait; > #endif > - struct pid __rcu *pid; > + struct pid *pid; > + rwlock_t pid_lock; > int sigset_active; > sigset_t sigset; > unsigned int halt_poll_ns; Adding yet another lock is never exciting, but this looks fine. Can you nest this lock inside of the vcpu->mutex acquisition in kvm_vm_ioctl_create_vcpu() so lockdep gets the picture? > @@ -4466,7 +4469,7 @@ static long kvm_vcpu_ioctl(struct file *filp, > r = -EINVAL; > if (arg) > goto out; > - oldpid = rcu_access_pointer(vcpu->pid); > + oldpid = vcpu->pid; It'd be good to add a comment here about how this is guarded by the vcpu->mutex, as Steve points out. -- Thanks, Oliver