Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp779846rwl; Wed, 29 Mar 2023 08:21:27 -0700 (PDT) X-Google-Smtp-Source: AKy350aBfyOKDSp/WfFg9dIP2fG7OnhLnV50TH9IoiBaenzrHyNK8e7nV7jQvgVnFLhjhkxeioiX X-Received: by 2002:a17:90b:4b51:b0:22b:b375:ec3f with SMTP id mi17-20020a17090b4b5100b0022bb375ec3fmr20338636pjb.21.1680103287612; Wed, 29 Mar 2023 08:21:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680103287; cv=none; d=google.com; s=arc-20160816; b=e9mVVhLX40lyxqfmgZyXNuAegH7pLRXsNytzJI5AvU1bJg0w0lDyFABe5PP881RCJK 89/SyeAbO24RBYjsyXSgMeUQU+gL9D/ABnNo0MR7VniCS5GeSt90csoxBityJbEPeR/Y TlucbwE/OhVJa8ZrlbfGfbeTpKm/CvPNxbxPlNHbtEvK8t6VFx/O5kUL+hTThhlAqoPy FAGtvsxCTws2yAW1o/ET9/IHxEJ0fmmeLtiK0lPNIYyuLp7SusMsjjtOrTR9psdKtN1a QN2uKbrhLGu8pJwh9TcEV8+Vwe7ZAT1WwGAIt6euILWmGDUh/vCAYzjz5SvaB2sh9fC0 PKhA== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=UNmFvgrAdCajFRIpt45plktjvRtUsWCMCvtd+JVhsYc=; b=HFeb8+Qxo+y90GzuRmzL0sqpv3Y0KMI8AG1IgwyZyAu/oEZ6Jhaw/71i+jEKGh9x62 XXiCKtqU3JOI776d++BNVTVDUJ4PxK0JlfhnuSaGbq2uFklh2fnm1/5EzONAmCQxAUTs sqEf8nixU3gU9W325JS5Xy7ZWoTAUGuu1ssTDg/0RVTDyi6+uxFjp9+QQBliuKFZcD3z lm3tMLSI99SKirpCMx0qcyZ823ZjBQt22cL4i6F1Li1WJ3JC4qu58INOjKxoNHoNWxNa +9c1X2F4Yhd2B6IhxB2CUBg1n+Fk1m5FFH0Rp+UMKsd+Wjsv0zylS6zXfrgfQYs/nwnr R1mA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Ewq8u5E9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f20-20020a63f114000000b00502d85bfb5fsi16879788pgi.451.2023.03.29.08.21.15; Wed, 29 Mar 2023 08:21:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Ewq8u5E9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230389AbjC2PR6 (ORCPT + 99 others); Wed, 29 Mar 2023 11:17:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56428 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230338AbjC2PRz (ORCPT ); Wed, 29 Mar 2023 11:17:55 -0400 Received: from mail-lf1-x12c.google.com (mail-lf1-x12c.google.com [IPv6:2a00:1450:4864:20::12c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4312430F0 for ; Wed, 29 Mar 2023 08:17:53 -0700 (PDT) Received: by mail-lf1-x12c.google.com with SMTP id j11so20650965lfg.13 for ; Wed, 29 Mar 2023 08:17:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1680103071; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=UNmFvgrAdCajFRIpt45plktjvRtUsWCMCvtd+JVhsYc=; b=Ewq8u5E9Bmp8HxucWtSxN/ywJhD+EFahfVH8pTqWAIOnHaI059A3GMgXy2M0ZHoFej SafOfq56AiijatCuZZNbUqP7gXo4SDp2rXw9YYJi8+YMjjCsxBFkQIz+bySOit6jhHM+ pEWEvcldXQTxCaG9epFXxphbJDOzqd7IFm1r6PWLU2SvEvv8R5uwe5KXYEnhtljK/EA+ tcIy4I3lX4Ig+8DGlHGkC0B/9xRcJ6dGItsugo45spqwQtEmdN5TJOqHYi1sHGxGzSrn t9KgsEC2XZIZMGeBfITXk9VFg5sYhjxZCK8vN+/oAiiBNr6xbVS1FPj77rx0oWgiRjoz eJwQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680103071; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=UNmFvgrAdCajFRIpt45plktjvRtUsWCMCvtd+JVhsYc=; b=GZyGhodo3ospAgCvYqWj6egM9Kz9hqpDkhXm+LREfM/1Uve4rtxnAmEIhr86pGXiFo XeS75d4DATIRPrmBNeE3bvTvzSwhiwJwhq1+zkfSM17H+UtZCq52Pjv2C+ZWwjaSzguA uCVrNOEYmNPgQU3Ku5PyvEWsderrFS1og31ZOPFMgTvgPDLxUrQbDRNjkkbv41j+dbHi BCYsNfFGqCgBdZEnl5mZp7g4DVk6h7vVb3rAmSjQRzo9SPvepPZ+IrCZvWIGLagNTH4f FkhfiA/JvACVXDE48BoKK/2lCJ0HvmMgdalJSn+/v0MiwbH8UD78eCmzMMcPra+ohEkO FHvg== X-Gm-Message-State: AAQBX9frVWlzk32Si+n4pm9A/zUoxZegiiSiLEGGprSQmQ/idwir5/xr 7hGDnLcaY41Tyv6VEM9hQZ0SkQ== X-Received: by 2002:ac2:4834:0:b0:4dd:985a:1dd3 with SMTP id 20-20020ac24834000000b004dd985a1dd3mr5072648lft.15.1680103071521; Wed, 29 Mar 2023 08:17:51 -0700 (PDT) Received: from ta1.c.googlers.com.com (61.215.228.35.bc.googleusercontent.com. [35.228.215.61]) by smtp.gmail.com with ESMTPSA id m18-20020a195212000000b004e95f53adc7sm5510692lfb.27.2023.03.29.08.17.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 Mar 2023 08:17:51 -0700 (PDT) From: Tudor Ambarus To: pbonzini@redhat.com, stable@vger.kernel.org, seanjc@google.com, joro@8bytes.org Cc: vkuznets@redhat.com, wanpengli@tencent.com, jmattson@google.com, suravee.suthikulpanit@amd.com, kvm@vger.kernel.org, iommu@lists.linux-foundation.org, linux-kernel@vger.kernel.org, mlevitsk@redhat.com, joneslee@google.com, syzbot+b6a74be92b5063a0f1ff@syzkaller.appspotmail.com, Tudor Ambarus Subject: [PATCH][for stable/linux-5.15.y] KVM: VMX: Move preemption timer <=> hrtimer dance to common x86 Date: Wed, 29 Mar 2023 15:17:47 +0000 Message-Id: <20230329151747.2938509-1-tudor.ambarus@linaro.org> X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Sean Christopherson commit 98c25ead5eda5e9d41abe57839ad3e8caf19500c upstream. Handle the switch to/from the hypervisor/software timer when a vCPU is blocking in common x86 instead of in VMX. Even though VMX is the only user of a hypervisor timer, the logic and all functions involved are generic x86 (unless future CPUs do something completely different and implement a hypervisor timer that runs regardless of mode). Handling the switch in common x86 will allow for the elimination of the pre/post_blocks hooks, and also lets KVM switch back to the hypervisor timer if and only if it was in use (without additional params). Add a comment explaining why the switch cannot be deferred to kvm_sched_out() or kvm_vcpu_block(). Signed-off-by: Sean Christopherson Reviewed-by: Maxim Levitsky Message-Id: <20211208015236.1616697-8-seanjc@google.com> Signed-off-by: Paolo Bonzini [ta: Fix conflicts in vmx_pre_block and vmx_post_block as per Paolo's suggestion. Add Reported-by and Link tags.] Reported-by: syzbot+b6a74be92b5063a0f1ff@syzkaller.appspotmail.com Link: https://syzkaller.appspot.com/bug?id=489beb3d76ef14cc6cd18125782dc6f86051a605 Tested-by: Tudor Ambarus Signed-off-by: Tudor Ambarus --- arch/x86/kvm/vmx/vmx.c | 6 ------ arch/x86/kvm/x86.c | 21 +++++++++++++++++++++ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 9ce45554d637..c95c3675e8d5 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -7597,17 +7597,11 @@ static int vmx_pre_block(struct kvm_vcpu *vcpu) if (pi_pre_block(vcpu)) return 1; - if (kvm_lapic_hv_timer_in_use(vcpu)) - kvm_lapic_switch_to_sw_timer(vcpu); - return 0; } static void vmx_post_block(struct kvm_vcpu *vcpu) { - if (kvm_x86_ops.set_hv_timer) - kvm_lapic_switch_to_hv_timer(vcpu); - pi_post_block(vcpu); } diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 0622256cd768..5cb4af42ba64 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -10043,12 +10043,28 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) static inline int vcpu_block(struct kvm *kvm, struct kvm_vcpu *vcpu) { + bool hv_timer; + if (!kvm_arch_vcpu_runnable(vcpu) && (!kvm_x86_ops.pre_block || static_call(kvm_x86_pre_block)(vcpu) == 0)) { + /* + * Switch to the software timer before halt-polling/blocking as + * the guest's timer may be a break event for the vCPU, and the + * hypervisor timer runs only when the CPU is in guest mode. + * Switch before halt-polling so that KVM recognizes an expired + * timer before blocking. + */ + hv_timer = kvm_lapic_hv_timer_in_use(vcpu); + if (hv_timer) + kvm_lapic_switch_to_sw_timer(vcpu); + srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx); kvm_vcpu_block(vcpu); vcpu->srcu_idx = srcu_read_lock(&kvm->srcu); + if (hv_timer) + kvm_lapic_switch_to_hv_timer(vcpu); + if (kvm_x86_ops.post_block) static_call(kvm_x86_post_block)(vcpu); @@ -10287,6 +10303,11 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) r = -EINTR; goto out; } + /* + * It should be impossible for the hypervisor timer to be in + * use before KVM has ever run the vCPU. + */ + WARN_ON_ONCE(kvm_lapic_hv_timer_in_use(vcpu)); kvm_vcpu_block(vcpu); if (kvm_apic_accept_events(vcpu) < 0) { r = 0; -- 2.40.0.348.gf938b09366-goog