Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp189118rdb; Tue, 5 Dec 2023 02:39:17 -0800 (PST) X-Google-Smtp-Source: AGHT+IHteOkS3CnFmOpWw3YYVgg67fMliBlFDpbWvDjh2nOsT6FS8eUaAMOQ4PUHLym+CiPy+S4e X-Received: by 2002:a05:6358:60c1:b0:170:17ea:f4d4 with SMTP id i1-20020a05635860c100b0017017eaf4d4mr6347205rwi.33.1701772756682; Tue, 05 Dec 2023 02:39:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701772756; cv=none; d=google.com; s=arc-20160816; b=LHjeJDLB0KnCFt9Q3q3JNwbEmoplkZGnVCSQRewEM/v6usVjI6n+gHyzlPR6Yd6rSx O74pGtmpeoMSip/SCFiBc6np2c/5MWmD8qfeLpcf/WFqASNwJ7GUdJo7UH/xKKpF6u41 tiq3tsKRz+bs9qcR/yVV/0FAv24c61zF5pgFIui8BFs11LZQAEp6GxQuEnVv/EvJkI5k YoiEpOrosLjRE6XjVa/MPujjVLW8VnjKYnPNQD79LORvPM86TTP6QveIv3qQ5VeO0YlE Yg9nnmJ8HsgtZMJKzEfQ99ThVTN4W+WhRu+EKvaz4e9ESRNpn5A6msIm2zqnP6K3xUR1 rC1A== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=WhrltLpYpRfruoeHLu41cyXbAA0zn8v5CL/MVSEdybo=; fh=N2PMIgUB1WENx8VVMRFY1EHLciTqX2wQ6sXFWT4E2R4=; b=JnJdfVoXvv6a7RynP7XjCIZzUAVl98yKDJGY7oi7w6v7AEorNRm1GDJzn76YdJ4D1T KTkbX60ZdkHP8b0sixDZNuKaPgdRMkyrSGrRvSrlxcy7hXPzZSNUvH4WBpdI2Xkl+uIu deYpEfJ76xXKu7C0RShJbKRafHllR/HZBSdMui1/kk8Ewp99BIR57a4xn5kcTZD42Frz tImKXMoI3RZKhThsegum1t76dTnoG/JOKTggvLIKb0vvpc8vhmPKolu+AjGYb5G0QDwQ Ra8rF+ari4IA1pduYyNOHb/P/rEquiYNpz1L15QrkI79WQdyOXE0UOex8RmYOlk7VpOf xKVQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=jMs7ZQPc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 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 lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id q16-20020a056a00085000b006cdff067bacsi7506514pfk.214.2023.12.05.02.39.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Dec 2023 02:39:16 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=jMs7ZQPc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id E2BD6804196A; Tue, 5 Dec 2023 02:39:12 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346799AbjLEKiv (ORCPT + 99 others); Tue, 5 Dec 2023 05:38:51 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41864 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235496AbjLEKif (ORCPT ); Tue, 5 Dec 2023 05:38:35 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CA5EC1BF1 for ; Tue, 5 Dec 2023 02:37:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1701772677; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=WhrltLpYpRfruoeHLu41cyXbAA0zn8v5CL/MVSEdybo=; b=jMs7ZQPcTw8Pby6xFZNy5+l0xpcwANsCsb8/um8VpM6JOptA9PJ0MW0ZHoKkwh+P7v/pcc mkLGQlZxrbAlUCmSDY9m1dXXll0f9bKeqEJaSlKmLas7cREJOwA64wtviyRWh55asOF+di PYnFL8UyTjjDETbg+n2u2I7rjMdU+/I= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-394-_2FxBfz2Pg2Q_Bkz_mhF6A-1; Tue, 05 Dec 2023 05:37:52 -0500 X-MC-Unique: _2FxBfz2Pg2Q_Bkz_mhF6A-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 01DA938135E0; Tue, 5 Dec 2023 10:37:52 +0000 (UTC) Received: from localhost.localdomain (unknown [10.45.225.152]) by smtp.corp.redhat.com (Postfix) with ESMTP id 614D0C15A0C; Tue, 5 Dec 2023 10:37:49 +0000 (UTC) From: Maxim Levitsky To: kvm@vger.kernel.org Cc: Sean Christopherson , Dave Hansen , Ingo Molnar , x86@kernel.org, linux-kernel@vger.kernel.org, Paolo Bonzini , Borislav Petkov , Thomas Gleixner , "H. Peter Anvin" , Maxim Levitsky Subject: [PATCH v4 1/4] KVM: x86: refactor req_immediate_exit logic Date: Tue, 5 Dec 2023 12:37:42 +0200 Message-Id: <20231205103745.506724-2-mlevitsk@redhat.com> In-Reply-To: <20231205103745.506724-1-mlevitsk@redhat.com> References: <20231205103745.506724-1-mlevitsk@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Tue, 05 Dec 2023 02:39:13 -0800 (PST) - move req_immediate_exit variable from arch specific to common code. - remove arch specific callback .request_immediate_exit and move the code down to the arch's vcpu_run's code. No functional change is intended. Signed-off-by: Maxim Levitsky --- arch/x86/include/asm/kvm-x86-ops.h | 1 - arch/x86/include/asm/kvm_host.h | 5 ++--- arch/x86/kvm/svm/svm.c | 7 ++++--- arch/x86/kvm/vmx/vmx.c | 18 ++++++----------- arch/x86/kvm/vmx/vmx.h | 2 -- arch/x86/kvm/x86.c | 31 +++++++++++++----------------- 6 files changed, 25 insertions(+), 39 deletions(-) diff --git a/arch/x86/include/asm/kvm-x86-ops.h b/arch/x86/include/asm/kvm-x86-ops.h index 26b628d84594b93..3aeb7c669a0b09b 100644 --- a/arch/x86/include/asm/kvm-x86-ops.h +++ b/arch/x86/include/asm/kvm-x86-ops.h @@ -101,7 +101,6 @@ KVM_X86_OP(write_tsc_multiplier) KVM_X86_OP(get_exit_info) KVM_X86_OP(check_intercept) KVM_X86_OP(handle_exit_irqoff) -KVM_X86_OP(request_immediate_exit) KVM_X86_OP(sched_in) KVM_X86_OP_OPTIONAL(update_cpu_dirty_logging) KVM_X86_OP_OPTIONAL(vcpu_blocking) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index d7036982332e33d..044b4f9265c5427 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1021,6 +1021,8 @@ struct kvm_vcpu_arch { */ bool pdptrs_from_userspace; + bool req_immediate_exit; + #if IS_ENABLED(CONFIG_HYPERV) hpa_t hv_root_tdp; #endif @@ -1700,8 +1702,6 @@ struct kvm_x86_ops { struct x86_exception *exception); void (*handle_exit_irqoff)(struct kvm_vcpu *vcpu); - void (*request_immediate_exit)(struct kvm_vcpu *vcpu); - void (*sched_in)(struct kvm_vcpu *vcpu, int cpu); /* @@ -2187,7 +2187,6 @@ extern bool kvm_find_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn); int kvm_skip_emulated_instruction(struct kvm_vcpu *vcpu); int kvm_complete_insn_gp(struct kvm_vcpu *vcpu, int err); -void __kvm_request_immediate_exit(struct kvm_vcpu *vcpu); void __user *__x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size); diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 1855a6d7c976ad2..d2c6ff9036009dd 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -4137,9 +4137,12 @@ static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu) * is enough to force an immediate vmexit. */ disable_nmi_singlestep(svm); - smp_send_reschedule(vcpu->cpu); + vcpu->arch.req_immediate_exit = true; } + if (vcpu->arch.req_immediate_exit) + smp_send_reschedule(vcpu->cpu); + pre_svm_run(vcpu); sync_lapic_to_cr8(vcpu); @@ -4995,8 +4998,6 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .check_intercept = svm_check_intercept, .handle_exit_irqoff = svm_handle_exit_irqoff, - .request_immediate_exit = __kvm_request_immediate_exit, - .sched_in = svm_sched_in, .nested_ops = &svm_nested_ops, diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index be20a60047b1f29..b8fa16f9e621878 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -67,6 +67,8 @@ #include "x86.h" #include "smm.h" +#include + MODULE_AUTHOR("Qumranet"); MODULE_LICENSE("GPL"); @@ -1288,8 +1290,6 @@ void vmx_prepare_switch_to_guest(struct kvm_vcpu *vcpu) u16 fs_sel, gs_sel; int i; - vmx->req_immediate_exit = false; - /* * Note that guest MSRs to be saved/restored can also be changed * when guest state is loaded. This happens when guest transitions @@ -5996,7 +5996,7 @@ static fastpath_t handle_fastpath_preemption_timer(struct kvm_vcpu *vcpu) { struct vcpu_vmx *vmx = to_vmx(vcpu); - if (!vmx->req_immediate_exit && + if (!vcpu->arch.req_immediate_exit && !unlikely(vmx->loaded_vmcs->hv_timer_soft_disabled)) { kvm_lapic_expired_hv_timer(vcpu); return EXIT_FASTPATH_REENTER_GUEST; @@ -7154,7 +7154,7 @@ static void vmx_update_hv_timer(struct kvm_vcpu *vcpu) u64 tscl; u32 delta_tsc; - if (vmx->req_immediate_exit) { + if (vcpu->arch.req_immediate_exit) { vmcs_write32(VMX_PREEMPTION_TIMER_VALUE, 0); vmx->loaded_vmcs->hv_timer_soft_disabled = false; } else if (vmx->hv_deadline_tsc != -1) { @@ -7357,6 +7357,8 @@ static fastpath_t vmx_vcpu_run(struct kvm_vcpu *vcpu) if (enable_preemption_timer) vmx_update_hv_timer(vcpu); + else if (vcpu->arch.req_immediate_exit) + smp_send_reschedule(vcpu->cpu); kvm_wait_lapic_expire(vcpu); @@ -7899,11 +7901,6 @@ static __init void vmx_set_cpu_caps(void) kvm_cpu_cap_check_and_set(X86_FEATURE_WAITPKG); } -static void vmx_request_immediate_exit(struct kvm_vcpu *vcpu) -{ - to_vmx(vcpu)->req_immediate_exit = true; -} - static int vmx_check_intercept_io(struct kvm_vcpu *vcpu, struct x86_instruction_info *info) { @@ -8312,8 +8309,6 @@ static struct kvm_x86_ops vmx_x86_ops __initdata = { .check_intercept = vmx_check_intercept, .handle_exit_irqoff = vmx_handle_exit_irqoff, - .request_immediate_exit = vmx_request_immediate_exit, - .sched_in = vmx_sched_in, .cpu_dirty_log_size = PML_ENTITY_NUM, @@ -8571,7 +8566,6 @@ static __init int hardware_setup(void) if (!enable_preemption_timer) { vmx_x86_ops.set_hv_timer = NULL; vmx_x86_ops.cancel_hv_timer = NULL; - vmx_x86_ops.request_immediate_exit = __kvm_request_immediate_exit; } kvm_caps.supported_mce_cap |= MCG_LMCE_P; diff --git a/arch/x86/kvm/vmx/vmx.h b/arch/x86/kvm/vmx/vmx.h index c2130d2c8e24bb5..4dabd16a3d7180e 100644 --- a/arch/x86/kvm/vmx/vmx.h +++ b/arch/x86/kvm/vmx/vmx.h @@ -330,8 +330,6 @@ struct vcpu_vmx { unsigned int ple_window; bool ple_window_dirty; - bool req_immediate_exit; - /* Support for PML */ #define PML_ENTITY_NUM 512 struct page *pml_pg; diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 2c924075f6f112a..2089a0b08ce08c0 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -10179,8 +10179,7 @@ static void kvm_inject_exception(struct kvm_vcpu *vcpu) * ordering between that side effect, the instruction completing, _and_ the * delivery of the asynchronous event. */ -static int kvm_check_and_inject_events(struct kvm_vcpu *vcpu, - bool *req_immediate_exit) +static int kvm_check_and_inject_events(struct kvm_vcpu *vcpu) { bool can_inject; int r; @@ -10357,8 +10356,9 @@ static int kvm_check_and_inject_events(struct kvm_vcpu *vcpu, if (is_guest_mode(vcpu) && kvm_x86_ops.nested_ops->has_events && - kvm_x86_ops.nested_ops->has_events(vcpu)) - *req_immediate_exit = true; + kvm_x86_ops.nested_ops->has_events(vcpu)) { + vcpu->arch.req_immediate_exit = true; + } /* * KVM must never queue a new exception while injecting an event; KVM @@ -10375,10 +10375,9 @@ static int kvm_check_and_inject_events(struct kvm_vcpu *vcpu, WARN_ON_ONCE(vcpu->arch.exception.pending || vcpu->arch.exception_vmexit.pending); return 0; - out: if (r == -EBUSY) { - *req_immediate_exit = true; + vcpu->arch.req_immediate_exit = true; r = 0; } return r; @@ -10605,12 +10604,6 @@ static void kvm_vcpu_reload_apic_access_page(struct kvm_vcpu *vcpu) static_call_cond(kvm_x86_set_apic_access_page_addr)(vcpu); } -void __kvm_request_immediate_exit(struct kvm_vcpu *vcpu) -{ - smp_send_reschedule(vcpu->cpu); -} -EXPORT_SYMBOL_GPL(__kvm_request_immediate_exit); - /* * Called within kvm->srcu read side. * Returns 1 to let vcpu_run() continue the guest execution loop without @@ -10625,7 +10618,6 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) kvm_cpu_accept_dm_intr(vcpu); fastpath_t exit_fastpath; - bool req_immediate_exit = false; if (kvm_request_pending(vcpu)) { if (kvm_check_request(KVM_REQ_VM_DEAD, vcpu)) { @@ -10787,7 +10779,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) goto out; } - r = kvm_check_and_inject_events(vcpu, &req_immediate_exit); + r = kvm_check_and_inject_events(vcpu); if (r < 0) { r = 0; goto out; @@ -10856,10 +10848,9 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) goto cancel_injection; } - if (req_immediate_exit) { + + if (vcpu->arch.req_immediate_exit) kvm_make_request(KVM_REQ_EVENT, vcpu); - static_call(kvm_x86_request_immediate_exit)(vcpu); - } fpregs_assert_state_consistent(); if (test_thread_flag(TIF_NEED_FPU_LOAD)) @@ -10891,6 +10882,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) (kvm_get_apic_mode(vcpu) != LAPIC_MODE_DISABLED)); exit_fastpath = static_call(kvm_x86_vcpu_run)(vcpu); + if (likely(exit_fastpath != EXIT_FASTPATH_REENTER_GUEST)) break; @@ -10906,6 +10898,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) ++vcpu->stat.exits; } + vcpu->arch.req_immediate_exit = false; /* * Do this here before restoring debug registers on the host. And * since we do this before handling the vmexit, a DR access vmexit @@ -10993,8 +10986,10 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) return r; cancel_injection: - if (req_immediate_exit) + if (vcpu->arch.req_immediate_exit) { + vcpu->arch.req_immediate_exit = false; kvm_make_request(KVM_REQ_EVENT, vcpu); + } static_call(kvm_x86_cancel_injection)(vcpu); if (unlikely(vcpu->arch.apic_attention)) kvm_lapic_sync_from_vapic(vcpu); -- 2.26.3