From nobody Mon Apr 13 12:01:51 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C24BDC25B06 for ; Thu, 11 Aug 2022 21:06:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236350AbiHKVGS (ORCPT ); Thu, 11 Aug 2022 17:06:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41004 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236289AbiHKVGM (ORCPT ); Thu, 11 Aug 2022 17:06:12 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id F311B785A8 for ; Thu, 11 Aug 2022 14:06:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1660251968; 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=NqSPZEkWBGI46NvTgNx/Bt3rMwg89QmQhSCnM/Tpfm4=; b=AmNZSpomHflLObLsxhXxut/P4yepQ+ASsKkchcm4exb5YHr5r5nvJmXwYgQZwBa8wpBFty vXhGiHZsxdeHPlhnsZR4MWmOh7FlsiA7btmL6FAmcwtErh0P1KezWN3A2OrpnHLHknQcbv GRg17I2sZExln1cFHNyYHvPRclKbL4s= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-498-zw33dlhwODiy7MM-FRzIlQ-1; Thu, 11 Aug 2022 17:06:07 -0400 X-MC-Unique: zw33dlhwODiy7MM-FRzIlQ-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id D299C1C004FC; Thu, 11 Aug 2022 21:06:06 +0000 (UTC) Received: from virtlab701.virt.lab.eng.bos.redhat.com (virtlab701.virt.lab.eng.bos.redhat.com [10.19.152.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id ABA93492C3B; Thu, 11 Aug 2022 21:06:06 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: seanjc@google.com, mlevitsk@redhat.com, vkuznets@redhat.com Subject: [PATCH v2 3/9] KVM: x86: make kvm_vcpu_{block,halt} return whether vCPU is runnable Date: Thu, 11 Aug 2022 17:05:59 -0400 Message-Id: <20220811210605.402337-4-pbonzini@redhat.com> In-Reply-To: <20220811210605.402337-1-pbonzini@redhat.com> References: <20220811210605.402337-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.85 on 10.11.54.10 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This is currently returned via KVM_REQ_UNHALT, but this is completely unnecessary since all that the callers do is clear the request; it is never processed via the usual request loop. The same condition can be returned as a positive value from the functions. No functional change intended. Signed-off-by: Paolo Bonzini --- include/linux/kvm_host.h | 4 ++-- virt/kvm/kvm_main.c | 23 ++++++++++++++++++----- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index e7bd48d15db8..cbd9577e5447 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1338,8 +1338,8 @@ void kvm_gfn_to_pfn_cache_destroy(struct kvm *kvm, st= ruct gfn_to_pfn_cache *gpc) void kvm_sigset_activate(struct kvm_vcpu *vcpu); void kvm_sigset_deactivate(struct kvm_vcpu *vcpu); =20 -void kvm_vcpu_halt(struct kvm_vcpu *vcpu); -void kvm_vcpu_block(struct kvm_vcpu *vcpu); +int kvm_vcpu_halt(struct kvm_vcpu *vcpu); +int kvm_vcpu_block(struct kvm_vcpu *vcpu); void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu); void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu); bool kvm_vcpu_wake_up(struct kvm_vcpu *vcpu); diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 1f049c1d01b4..e827805b7b28 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -3402,6 +3402,12 @@ static void shrink_halt_poll_ns(struct kvm_vcpu *vcp= u) trace_kvm_halt_poll_ns_shrink(vcpu->vcpu_id, val, old); } =20 +/* + * Returns zero if the vCPU should remain in a blocked state, + * nonzero if it has been woken up, specifically: + * - 1 if it is runnable + * - -EINTR if it is not runnable (e.g. has a signal or a timer pending) + */ static int kvm_vcpu_check_block(struct kvm_vcpu *vcpu) { int ret =3D -EINTR; @@ -3409,6 +3415,7 @@ static int kvm_vcpu_check_block(struct kvm_vcpu *vcpu) =20 if (kvm_arch_vcpu_runnable(vcpu)) { kvm_make_request(KVM_REQ_UNHALT, vcpu); + ret =3D 1; goto out; } if (kvm_cpu_has_pending_timer(vcpu)) @@ -3429,9 +3436,10 @@ static int kvm_vcpu_check_block(struct kvm_vcpu *vcp= u) * pending. This is mostly used when halting a vCPU, but may also be used * directly for other vCPU non-runnable states, e.g. x86's Wait-For-SIPI. */ -void kvm_vcpu_block(struct kvm_vcpu *vcpu) +int kvm_vcpu_block(struct kvm_vcpu *vcpu) { struct rcuwait *wait =3D kvm_arch_vcpu_get_wait(vcpu); + int r; =20 vcpu->stat.generic.blocking =3D 1; =20 @@ -3443,7 +3451,8 @@ void kvm_vcpu_block(struct kvm_vcpu *vcpu) for (;;) { set_current_state(TASK_INTERRUPTIBLE); =20 - if (kvm_vcpu_check_block(vcpu) < 0) + r =3D kvm_vcpu_check_block(vcpu); + if (r !=3D 0) break; =20 schedule(); @@ -3455,6 +3464,7 @@ void kvm_vcpu_block(struct kvm_vcpu *vcpu) preempt_enable(); =20 vcpu->stat.generic.blocking =3D 0; + return r; } =20 static inline void update_halt_poll_stats(struct kvm_vcpu *vcpu, ktime_t s= tart, @@ -3485,12 +3495,13 @@ static inline void update_halt_poll_stats(struct kv= m_vcpu *vcpu, ktime_t start, * expensive block+unblock sequence if a wake event arrives soon after the= vCPU * is halted. */ -void kvm_vcpu_halt(struct kvm_vcpu *vcpu) +int kvm_vcpu_halt(struct kvm_vcpu *vcpu) { bool halt_poll_allowed =3D !kvm_arch_no_poll(vcpu); bool do_halt_poll =3D halt_poll_allowed && vcpu->halt_poll_ns; ktime_t start, cur, poll_end, stop; bool waited =3D false; + int r; u64 halt_ns; =20 start =3D cur =3D poll_end =3D ktime_get(); @@ -3501,14 +3512,15 @@ void kvm_vcpu_halt(struct kvm_vcpu *vcpu) * This sets KVM_REQ_UNHALT if an interrupt * arrives. */ - if (kvm_vcpu_check_block(vcpu) < 0) + r =3D kvm_vcpu_check_block(vcpu); + if (r !=3D 0) goto out; cpu_relax(); poll_end =3D cur =3D ktime_get(); } while (kvm_vcpu_can_poll(cur, stop)); =20 waited =3D true; - kvm_vcpu_block(vcpu); + r =3D kvm_vcpu_block(vcpu); =20 cur =3D ktime_get(); vcpu->stat.generic.halt_wait_ns +=3D @@ -3547,6 +3559,7 @@ void kvm_vcpu_halt(struct kvm_vcpu *vcpu) } =20 trace_kvm_vcpu_wakeup(halt_ns, waited, vcpu_valid_wakeup(vcpu)); + return r; } EXPORT_SYMBOL_GPL(kvm_vcpu_halt); =20 --=20 2.31.1