Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp890874pxb; Wed, 27 Oct 2021 14:34:01 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxG7EMxgRLW7xcTzRKYJxkM6sRbBrZ8MoRTWPGFRPVCBoNv32sLBMsoJB4wFITQ3GFy12N4 X-Received: by 2002:aa7:d693:: with SMTP id d19mr462581edr.341.1635370441543; Wed, 27 Oct 2021 14:34:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1635370441; cv=none; d=google.com; s=arc-20160816; b=u5TQ4oyyezW+3VrJIdpJYGoJwcBHo/WawJQnKNPsMBwFRr+4WbK21tU9u1HtWQZOmQ UkW/5utLPEU3ON0tPPGmt2Ke/sLWM4UPmJeCz3Tv/XqJ2ZIXl5O8zAGr+/pLoWigRJ1Z yQt/ivopjg00fsV70/V4VS9uLMghr4z0Jd+qsf/9f2HDS8I94MJN+31EUPI77etBk1V5 hZgS4ChBEIfz4fiu3yEuy7DNwYWw59+N5zrCRpk71FHc4P683wbDHCdn/duyj2ZiIJIh OCsj310XSsR3CzLa0cX4zKKcVRQnL/70eYkbMcMsVtGBQ/u9ogfROk0ulbqoDQ3hRYm8 soLg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:date:cc:to:from:subject :message-id:dkim-signature; bh=Wylhzf4CyCl3uScvUg3UWqXTSn56cxOkW4GodPhaZEk=; b=ftd2NTz+dNppTmh8TnfgolR1W+iLSkXCPMCYR0ni4yijUlivA/p3/FFsT7KF0zh6z7 2bUbWo4Nkw8kP3n0Nodu+WaIlKXIKYxhXtfsxH7pjiF0sOOYst2WfIuNZFQr3Z3z9FrH WlqViqUVOzAS0ZBzG8LmQMbD0vGISakpTx9v2P2Ox+G1tvc4UnOrotYcQPtLIwEh8qJW d0cspJXr2TNKboK8Ky6iU59YVwnssY7ptYKCAX0r14N6D2h4wKGbDoL8y/ZsrP/5T+Vg 3W6BpnujEYfwgJ+l1on4mD6p8lwEHpws+BdrpsJdM31yp6vbTI7Q8zW+kIaX7vp8hfYb iF+A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="gmt/ooFx"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a4si1270120ejf.311.2021.10.27.14.33.38; Wed, 27 Oct 2021 14:34:01 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="gmt/ooFx"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240748AbhJ0Tai (ORCPT + 97 others); Wed, 27 Oct 2021 15:30:38 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:32465 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232220AbhJ0Tah (ORCPT ); Wed, 27 Oct 2021 15:30:37 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1635362891; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Wylhzf4CyCl3uScvUg3UWqXTSn56cxOkW4GodPhaZEk=; b=gmt/ooFxQQ/Z8BmanF7aZcWOJLaO/Sr3UMARvfPwsz6785U2h0xMKHnRivjpqucJLaOhjx YaVwJxyEZ2BxKKg+RrCE70PzyL/symcjykeEl6gIqCTpZBdeVtv63rYH68eT1sv4/xDW9m A2A5zyAdkyQH2D07s3h1tsf/SkMmXxU= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-589-6XBNbbgXN7iWziBFLLfjYg-1; Wed, 27 Oct 2021 15:28:07 -0400 X-MC-Unique: 6XBNbbgXN7iWziBFLLfjYg-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 3230C5074B; Wed, 27 Oct 2021 19:28:03 +0000 (UTC) Received: from starship (unknown [10.40.194.243]) by smtp.corp.redhat.com (Postfix) with ESMTP id F2E20171FF; Wed, 27 Oct 2021 19:27:23 +0000 (UTC) Message-ID: <495f9106220b87ffa9eaf15fcf39b950628adee1.camel@redhat.com> Subject: Re: [PATCH v2 19/43] KVM: Add helpers to wake/query blocking vCPU From: Maxim Levitsky To: Sean Christopherson , Marc Zyngier , Huacai Chen , Aleksandar Markovic , Paul Mackerras , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Christian Borntraeger , Janosch Frank , Paolo Bonzini Cc: James Morse , Alexandru Elisei , Suzuki K Poulose , Atish Patra , David Hildenbrand , Cornelia Huck , Claudio Imbrenda , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-mips@vger.kernel.org, kvm@vger.kernel.org, kvm-ppc@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, David Matlack , Oliver Upton , Jing Zhang Date: Wed, 27 Oct 2021 22:27:22 +0300 In-Reply-To: <20211009021236.4122790-20-seanjc@google.com> References: <20211009021236.4122790-1-seanjc@google.com> <20211009021236.4122790-20-seanjc@google.com> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.36.5 (3.36.5-2.fc32) MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, 2021-10-08 at 19:12 -0700, Sean Christopherson wrote: > Add helpers to wake and query a blocking vCPU. In addition to providing > nice names, the helpers reduce the probability of KVM neglecting to use > kvm_arch_vcpu_get_wait(). > > No functional change intended. > > Signed-off-by: Sean Christopherson > --- > arch/arm64/kvm/arch_timer.c | 3 +-- > arch/arm64/kvm/arm.c | 2 +- > arch/x86/kvm/lapic.c | 2 +- > include/linux/kvm_host.h | 14 ++++++++++++++ > virt/kvm/async_pf.c | 2 +- > virt/kvm/kvm_main.c | 8 ++------ > 6 files changed, 20 insertions(+), 11 deletions(-) > > diff --git a/arch/arm64/kvm/arch_timer.c b/arch/arm64/kvm/arch_timer.c > index 7e8396f74010..addd53b6eba6 100644 > --- a/arch/arm64/kvm/arch_timer.c > +++ b/arch/arm64/kvm/arch_timer.c > @@ -649,7 +649,6 @@ void kvm_timer_vcpu_put(struct kvm_vcpu *vcpu) > { > struct arch_timer_cpu *timer = vcpu_timer(vcpu); > struct timer_map map; > - struct rcuwait *wait = kvm_arch_vcpu_get_wait(vcpu); > > if (unlikely(!timer->enabled)) > return; > @@ -672,7 +671,7 @@ void kvm_timer_vcpu_put(struct kvm_vcpu *vcpu) > if (map.emul_ptimer) > soft_timer_cancel(&map.emul_ptimer->hrtimer); > > - if (rcuwait_active(wait)) > + if (kvm_vcpu_is_blocking(vcpu)) > kvm_timer_blocking(vcpu); > > /* > diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c > index 268b1e7bf700..9ff0e85a9f16 100644 > --- a/arch/arm64/kvm/arm.c > +++ b/arch/arm64/kvm/arm.c > @@ -622,7 +622,7 @@ void kvm_arm_resume_guest(struct kvm *kvm) > > kvm_for_each_vcpu(i, vcpu, kvm) { > vcpu->arch.pause = false; > - rcuwait_wake_up(kvm_arch_vcpu_get_wait(vcpu)); > + __kvm_vcpu_wake_up(vcpu); > } > } > > diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c > index 76fb00921203..0cd7ed21b205 100644 > --- a/arch/x86/kvm/lapic.c > +++ b/arch/x86/kvm/lapic.c > @@ -1931,7 +1931,7 @@ void kvm_lapic_expired_hv_timer(struct kvm_vcpu *vcpu) > /* If the preempt notifier has already run, it also called apic_timer_expired */ > if (!apic->lapic_timer.hv_timer_in_use) > goto out; > - WARN_ON(rcuwait_active(&vcpu->wait)); > + WARN_ON(kvm_vcpu_is_blocking(vcpu)); > apic_timer_expired(apic, false); > cancel_hv_timer(apic); > > diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h > index bdaa0e70b060..1fa38dc00b87 100644 > --- a/include/linux/kvm_host.h > +++ b/include/linux/kvm_host.h > @@ -1151,6 +1151,20 @@ static inline struct rcuwait *kvm_arch_vcpu_get_wait(struct kvm_vcpu *vcpu) > #endif > } > > +/* > + * Wake a vCPU if necessary, but don't do any stats/metadata updates. Returns > + * true if the vCPU was blocking and was awakened, false otherwise. > + */ > +static inline bool __kvm_vcpu_wake_up(struct kvm_vcpu *vcpu) > +{ > + return !!rcuwait_wake_up(kvm_arch_vcpu_get_wait(vcpu)); > +} > + > +static inline bool kvm_vcpu_is_blocking(struct kvm_vcpu *vcpu) > +{ > + return rcuwait_active(kvm_arch_vcpu_get_wait(vcpu)); > +} > + > #ifdef __KVM_HAVE_ARCH_INTC_INITIALIZED > /* > * returns true if the virtual interrupt controller is initialized and > diff --git a/virt/kvm/async_pf.c b/virt/kvm/async_pf.c > index ccb35c22785e..9bfe1d6f6529 100644 > --- a/virt/kvm/async_pf.c > +++ b/virt/kvm/async_pf.c > @@ -85,7 +85,7 @@ static void async_pf_execute(struct work_struct *work) > > trace_kvm_async_pf_completed(addr, cr2_or_gpa); > > - rcuwait_wake_up(kvm_arch_vcpu_get_wait(vcpu)); > + __kvm_vcpu_wake_up(vcpu); > > mmput(mm); > kvm_put_kvm(vcpu->kvm); > diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c > index 481e8178b43d..c870cae7e776 100644 > --- a/virt/kvm/kvm_main.c > +++ b/virt/kvm/kvm_main.c > @@ -3332,10 +3332,7 @@ EXPORT_SYMBOL_GPL(kvm_vcpu_halt); > > bool kvm_vcpu_wake_up(struct kvm_vcpu *vcpu) > { > - struct rcuwait *waitp; > - > - waitp = kvm_arch_vcpu_get_wait(vcpu); > - if (rcuwait_wake_up(waitp)) { > + if (__kvm_vcpu_wake_up(vcpu)) { > WRITE_ONCE(vcpu->ready, true); > ++vcpu->stat.generic.halt_wakeup; > return true; > @@ -3490,8 +3487,7 @@ void kvm_vcpu_on_spin(struct kvm_vcpu *me, bool yield_to_kernel_mode) > continue; > if (vcpu == me) > continue; > - if (rcuwait_active(kvm_arch_vcpu_get_wait(vcpu)) && > - !vcpu_dy_runnable(vcpu)) > + if (kvm_vcpu_is_blocking(vcpu) && !vcpu_dy_runnable(vcpu)) > continue; > if (READ_ONCE(vcpu->preempted) && yield_to_kernel_mode && > !kvm_arch_dy_has_pending_interrupt(vcpu) && Reviewed-by: Maxim Levitsky