From patchwork Tue Dec 5 10:37:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxim Levitsky X-Patchwork-Id: 173939 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp3339383vqy; Tue, 5 Dec 2023 02:39:11 -0800 (PST) X-Google-Smtp-Source: AGHT+IF8/aZdyIG59idwK2aFPdqeAvXWgfEulCHUBvepxyzAGxKMUcXa4onUhJsjht4d5KXhnC5t X-Received: by 2002:a17:90b:3586:b0:286:6cc0:8862 with SMTP id mm6-20020a17090b358600b002866cc08862mr724527pjb.79.1701772750975; Tue, 05 Dec 2023 02:39:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701772750; cv=none; d=google.com; s=arc-20160816; b=gi35a1qTn5797nwN/atet+Amc4zeMEC380mTSSB3c/Tn36T692HRsYLtrrnAHuxKR+ PAxkLo5C2TYASQffm8zPCqiqRtr73bPAPe+v2P13sGi85jNokHWqkNHYAXMEF90U2iOb urOt5X7tPF/F5sjU+GsNSzYmVKI2TaVc8P6K/VsrTFGGAERol9KSPf5MX6MIv1qdMDFX g9za84RF9KTn2svEiBe70+sErJsmToB4BivcplIbdVwF4lngr3UKUpSaAppAOO7wvweD phXoH5NajPSO2ditmaD5ThUXfjmLsVJ8NWZTWYRsUSGOBUX0zbpkNBtNIdCfjm5XMOf1 xpoA== 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 2620:137:e000::3:3 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 lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id o9-20020a1709026b0900b001d0969ccef4si3510164plk.229.2023.12.05.02.39.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Dec 2023 02:39:10 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; 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 2620:137:e000::3:3 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 C305080417C7; Tue, 5 Dec 2023 02:39:05 -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 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:06 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784438064019942395 X-GMAIL-MSGID: 1784438064019942395 - 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); From patchwork Tue Dec 5 10:37:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxim Levitsky X-Patchwork-Id: 173940 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp3339447vqy; Tue, 5 Dec 2023 02:39:20 -0800 (PST) X-Google-Smtp-Source: AGHT+IHeAYVghGAbnd4L63q0eecob7NOWwET9hAmVarUQJLwmnmkYZNDRZneBrhS84wIT0JLEi/O X-Received: by 2002:a17:902:e904:b0:1d0:9979:7be5 with SMTP id k4-20020a170902e90400b001d099797be5mr1310389pld.31.1701772760271; Tue, 05 Dec 2023 02:39:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701772760; cv=none; d=google.com; s=arc-20160816; b=iu/0joOw8epwL4SyQOj6R1xK6HJ8AWse6cMRAQ9Zi3KyHGakZL4zvQXDP5k54QQMJa xgpk6ffLvlllbAAJaiCSdmAEHS0e0fXqbNSqD726hxLCTTXke3KUooBsQImP24dOw3mm cqAgJqzoWRRp7paDNgBQWsyUOC6rF5YFOWbVMI1XsLA2W2uIoPtSXHwMKJuxhpV3CiZq bsp9J87Wf4R1Jxv7PzkxJpauQMEjnGmaEPdLloZoyIbRn39yu4Hn2RUqflWnJXkXAKDa YidFMMcCy5+U7Izi5v+WGdEKAu13t+kJNjgstqMaX6hx6Y8NbliL9TSTm6rfIWhA5Jby j4YQ== 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=tjWk7hRVR22y9K4b1+8Cn8jc4sdz8J7ZvP+cd1cDwUg=; fh=N2PMIgUB1WENx8VVMRFY1EHLciTqX2wQ6sXFWT4E2R4=; b=PkJS0eIDdWmhXYPUpIN3Ek26mES5USFDwX+q+oHFWvBBOYB9+wKt6eT6h2+X2pbsWD ebE4lGPXY5sJXNtOsARZYfpXneXJXyyE0gL7bt00JR6dCjbPWJuNFqQMYdNKdYAyGB0i Q6RqNlyenwehhe0EVVACErAsZa25NKLIL0BBO1HlA1IjRHTDE7o/EF3/2J4zHDhBUVTW UZpYFArNR4+4p8kn/QCW7KezvgL91rhnM0Igzq+qXXxawkVir4QhyUx+0HQZmKgg5wwZ LGcV9/0JtLaPns9twnuOE7X8g5LbddaXZRPoNfpmZqIxoFxBnqpBfonjn/bVhxjunjTE bl4g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=QEqH86bz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 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 agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id z11-20020a170903018b00b001d094751433si3830686plg.430.2023.12.05.02.39.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Dec 2023 02:39:20 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=QEqH86bz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 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 agentk.vger.email (Postfix) with ESMTP id 61F72804B117; Tue, 5 Dec 2023 02:39:17 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1376592AbjLEKiw (ORCPT + 99 others); Tue, 5 Dec 2023 05:38:52 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58534 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235505AbjLEKif (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 2A2BE136 for ; Tue, 5 Dec 2023 02:38:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1701772679; 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=tjWk7hRVR22y9K4b1+8Cn8jc4sdz8J7ZvP+cd1cDwUg=; b=QEqH86bzBykOikYRjfSTNlgBvxlxp1xhQJwWZnhxM+vIbxB5YeSPgotJLYkLhQlulC0GSe NZLh9pgrML8FQ71P3456PuDFH9gD+wVvIjv5wlVuWUDppBej2dplY+ktZfAs2XoYuLb5dL +/iWQxu6D1n87LL9otB0sChoxMvAe6Q= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-570-CIAvvymCPImLRCsr7U8M6A-1; Tue, 05 Dec 2023 05:37:55 -0500 X-MC-Unique: CIAvvymCPImLRCsr7U8M6A-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 C0FD383B821; Tue, 5 Dec 2023 10:37:54 +0000 (UTC) Received: from localhost.localdomain (unknown [10.45.225.152]) by smtp.corp.redhat.com (Postfix) with ESMTP id 575A7C15968; Tue, 5 Dec 2023 10:37:52 +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 2/4] KVM: x86: add more information to the kvm_entry tracepoint Date: Tue, 5 Dec 2023 12:37:43 +0200 Message-Id: <20231205103745.506724-3-mlevitsk@redhat.com> In-Reply-To: <20231205103745.506724-1-mlevitsk@redhat.com> References: <20231205103745.506724-1-mlevitsk@redhat.com> MIME-Version: 1.0 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 agentk.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 (agentk.vger.email [0.0.0.0]); Tue, 05 Dec 2023 02:39:17 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784438073859136395 X-GMAIL-MSGID: 1784438073859136395 Add VMX/SVM specific interrupt injection info to vm entry tracepoint. Also add a flag showing that immediate vm exit is set to happen after the entry. Reviewed-by: Paolo Bonzini 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 | 17 +++++++++++++++++ arch/x86/kvm/trace.h | 15 +++++++++++++-- arch/x86/kvm/vmx/vmx.c | 12 ++++++++++++ 5 files changed, 47 insertions(+), 3 deletions(-) diff --git a/arch/x86/include/asm/kvm-x86-ops.h b/arch/x86/include/asm/kvm-x86-ops.h index 3aeb7c669a0b09b..68fc0ef91ccbedc 100644 --- a/arch/x86/include/asm/kvm-x86-ops.h +++ b/arch/x86/include/asm/kvm-x86-ops.h @@ -99,6 +99,7 @@ KVM_X86_OP(get_l2_tsc_multiplier) KVM_X86_OP(write_tsc_offset) KVM_X86_OP(write_tsc_multiplier) KVM_X86_OP(get_exit_info) +KVM_X86_OP(get_entry_info) KVM_X86_OP(check_intercept) KVM_X86_OP(handle_exit_irqoff) KVM_X86_OP(sched_in) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 044b4f9265c5427..97a5fecc16c8542 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1689,13 +1689,16 @@ struct kvm_x86_ops { void (*write_tsc_multiplier)(struct kvm_vcpu *vcpu); /* - * Retrieve somewhat arbitrary exit information. Intended to + * Retrieve somewhat arbitrary exit/entry information. Intended to * be used only from within tracepoints or error paths. */ void (*get_exit_info)(struct kvm_vcpu *vcpu, u32 *reason, u64 *info1, u64 *info2, u32 *exit_int_info, u32 *exit_int_info_err_code); + void (*get_entry_info)(struct kvm_vcpu *vcpu, + u32 *inj_info, u32 *inj_info_error_code); + int (*check_intercept)(struct kvm_vcpu *vcpu, struct x86_instruction_info *info, enum x86_intercept_stage stage, diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index d2c6ff9036009dd..ad27e07da767f99 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -3492,6 +3492,22 @@ static void svm_get_exit_info(struct kvm_vcpu *vcpu, u32 *reason, *error_code = 0; } +static void svm_get_entry_info(struct kvm_vcpu *vcpu, + u32 *inj_info, + u32 *inj_info_error_code) +{ + struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control; + + *inj_info = control->event_inj; + + if ((*inj_info & SVM_EXITINTINFO_VALID) && + (*inj_info & SVM_EXITINTINFO_VALID_ERR)) + *inj_info_error_code = control->event_inj_err; + else + *inj_info_error_code = 0; + +} + static int svm_handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath) { struct vcpu_svm *svm = to_svm(vcpu); @@ -4983,6 +4999,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .required_apicv_inhibits = AVIC_REQUIRED_APICV_INHIBITS, .get_exit_info = svm_get_exit_info, + .get_entry_info = svm_get_entry_info, .vcpu_after_set_cpuid = svm_vcpu_after_set_cpuid, diff --git a/arch/x86/kvm/trace.h b/arch/x86/kvm/trace.h index 83843379813ee3e..28e8a63368cc021 100644 --- a/arch/x86/kvm/trace.h +++ b/arch/x86/kvm/trace.h @@ -21,14 +21,25 @@ TRACE_EVENT(kvm_entry, TP_STRUCT__entry( __field( unsigned int, vcpu_id ) __field( unsigned long, rip ) - ), + __field( u32, inj_info ) + __field( u32, inj_info_err ) + __field( bool, req_imm_exit ) + ), TP_fast_assign( __entry->vcpu_id = vcpu->vcpu_id; __entry->rip = kvm_rip_read(vcpu); + __entry->req_imm_exit = vcpu->arch.req_immediate_exit; + + static_call(kvm_x86_get_entry_info)(vcpu, + &__entry->inj_info, + &__entry->inj_info_err); ), - TP_printk("vcpu %u, rip 0x%lx", __entry->vcpu_id, __entry->rip) + TP_printk("vcpu %u, rip 0x%lx inj 0x%08x inj_error_code 0x%08x%s", + __entry->vcpu_id, __entry->rip, + __entry->inj_info, __entry->inj_info_err, + __entry->req_imm_exit ? " [req_imm_exit]" : "") ); /* diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index b8fa16f9e621878..2d78c94e464295d 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -6156,6 +6156,17 @@ static void vmx_get_exit_info(struct kvm_vcpu *vcpu, u32 *reason, } } +static void vmx_get_entry_info(struct kvm_vcpu *vcpu, + u32 *inj_info, + u32 *inj_info_error_code) +{ + *inj_info = vmcs_read32(VM_ENTRY_INTR_INFO_FIELD); + if (is_exception_with_error_code(*inj_info)) + *inj_info_error_code = vmcs_read32(VM_ENTRY_EXCEPTION_ERROR_CODE); + else + *inj_info_error_code = 0; +} + static void vmx_destroy_pml_buffer(struct vcpu_vmx *vmx) { if (vmx->pml_pg) { @@ -8294,6 +8305,7 @@ static struct kvm_x86_ops vmx_x86_ops __initdata = { .get_mt_mask = vmx_get_mt_mask, .get_exit_info = vmx_get_exit_info, + .get_entry_info = vmx_get_entry_info, .vcpu_after_set_cpuid = vmx_vcpu_after_set_cpuid, From patchwork Tue Dec 5 10:37:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxim Levitsky X-Patchwork-Id: 173941 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp3339479vqy; Tue, 5 Dec 2023 02:39:26 -0800 (PST) X-Google-Smtp-Source: AGHT+IG0H1DiLw20hIEyyiMC3xd1sg8ppp0U0z6GH+TTbB9oKIZtnm+3ROvkM2tCeLI4K8Xs76v7 X-Received: by 2002:a54:4589:0:b0:3ae:4cab:526a with SMTP id z9-20020a544589000000b003ae4cab526amr5758785oib.14.1701772766269; Tue, 05 Dec 2023 02:39:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701772766; cv=none; d=google.com; s=arc-20160816; b=QQuVsT40COPfwKcZPue4vXfZToyMuOtlb5UECYDaebuIzKoDFu/zygNpvdGXOnCgqT C5ed1s57qSIdCO6QpLiwA8LtkhtOxOd2ZY5e4ca43Beli+65XOhrqXIRqUJ5pcSYELA2 tcOAK6X6k/pzS4xkdTs5HI3N5/Lhiytzar3qwlNnSy0g7xTBF1vXXrqB88ozfqz64S6w qKVDPiTmR87y5pGadoCymx5kgvCWCJN5IaGdmA7Fk8ST8MextAV7QWvHo9pDhddqdlOu 4ibDRNuTXWqdA830Hbro8fV8wMIjTb5+4wKLHwKhOf1zZO/vqK/oDY2QfQoSF0k9zqFX iTHQ== 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=gK4jXLoCS0bqGNjgQzNnw64KFoSSKVngg1q4fCtnDfA=; fh=N2PMIgUB1WENx8VVMRFY1EHLciTqX2wQ6sXFWT4E2R4=; b=n3Phouw43VojtBQu6flOaDtn7tLPoEdjfvXGtC/GfkWn74pQhNuZdYtU/h33F4Pv5n EUyAsrFsl8SScv6gb7R+qCMe+LXTYay19N9IA8JrITv+k5GBAbp097WVDcthOHVKJVSm UNlMwUJKn+PKEUpWnKdZlL2jnkjAm4iNh9DE30+RNigECqvYYErbBM+FnYo3pcDcadFC 3zK+nyc6fh4xdnY3jlqTiqGiiG5YLBW8238+cPH+z2vaDVRGWRsZZ+X3Ws4FRjq34H5z 3D3GChORQ+IYWZGB+aCsjTRnreiyJqoa8wWyAbM0u7wvHLR+1Mii2tbXMJv5VS/tM8Tc APPQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=CeimODA5; 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 lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id r14-20020a63204e000000b005c5fe04e4eesi9418331pgm.852.2023.12.05.02.39.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Dec 2023 02:39:26 -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=CeimODA5; 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 A740180417D8; Tue, 5 Dec 2023 02:39:23 -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 S1376633AbjLEKi5 (ORCPT + 99 others); Tue, 5 Dec 2023 05:38:57 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41982 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235579AbjLEKii (ORCPT ); Tue, 5 Dec 2023 05:38:38 -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 27CBA1BFD for ; Tue, 5 Dec 2023 02:38:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1701772684; 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=gK4jXLoCS0bqGNjgQzNnw64KFoSSKVngg1q4fCtnDfA=; b=CeimODA5HiwtVBIiWVzRhO3MQHwGkd270ZvqF4fFIYupKC8hNRfa4oF5fn2d12RNF+UQk2 YKQyicJjpenGaWkcJt5JWfTpkP9NeQSORAvOVcQK2RrR8n+00mO1NMxmGfvYBfjuNl3To5 GZ6NiyHikMzXBdgVIqmVNoz9hw0Dqsg= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-68-pUCADUVKOrqcI-bkOCOCOQ-1; Tue, 05 Dec 2023 05:37:58 -0500 X-MC-Unique: pUCADUVKOrqcI-bkOCOCOQ-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 8BA6E85A589; Tue, 5 Dec 2023 10:37:57 +0000 (UTC) Received: from localhost.localdomain (unknown [10.45.225.152]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2217EC15E6A; Tue, 5 Dec 2023 10:37:54 +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 3/4] KVM: x86: add information about pending requests to kvm_exit tracepoint Date: Tue, 5 Dec 2023 12:37:44 +0200 Message-Id: <20231205103745.506724-4-mlevitsk@redhat.com> In-Reply-To: <20231205103745.506724-1-mlevitsk@redhat.com> References: <20231205103745.506724-1-mlevitsk@redhat.com> MIME-Version: 1.0 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:23 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784438080282333300 X-GMAIL-MSGID: 1784438080282333300 This allows to gather information on how often kvm interrupts vCPUs due to specific requests. Reviewed-by: Paolo Bonzini Signed-off-by: Maxim Levitsky --- arch/x86/kvm/trace.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/trace.h b/arch/x86/kvm/trace.h index 28e8a63368cc021..e275a02a21e5233 100644 --- a/arch/x86/kvm/trace.h +++ b/arch/x86/kvm/trace.h @@ -316,12 +316,14 @@ TRACE_EVENT(name, \ __field( u32, intr_info ) \ __field( u32, error_code ) \ __field( unsigned int, vcpu_id ) \ + __field( u64, requests ) \ ), \ \ TP_fast_assign( \ __entry->guest_rip = kvm_rip_read(vcpu); \ __entry->isa = isa; \ __entry->vcpu_id = vcpu->vcpu_id; \ + __entry->requests = READ_ONCE(vcpu->requests); \ static_call(kvm_x86_get_exit_info)(vcpu, \ &__entry->exit_reason, \ &__entry->info1, \ @@ -331,11 +333,13 @@ TRACE_EVENT(name, \ ), \ \ TP_printk("vcpu %u reason %s%s%s rip 0x%lx info1 0x%016llx " \ - "info2 0x%016llx intr_info 0x%08x error_code 0x%08x", \ + "info2 0x%016llx intr_info 0x%08x error_code 0x%08x " \ + "requests 0x%016llx", \ __entry->vcpu_id, \ kvm_print_exit_reason(__entry->exit_reason, __entry->isa), \ __entry->guest_rip, __entry->info1, __entry->info2, \ - __entry->intr_info, __entry->error_code) \ + __entry->intr_info, __entry->error_code, \ + __entry->requests) \ ) /* From patchwork Tue Dec 5 10:37:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxim Levitsky X-Patchwork-Id: 173942 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp3339513vqy; Tue, 5 Dec 2023 02:39:30 -0800 (PST) X-Google-Smtp-Source: AGHT+IGq7YSHZYxtmT53ZKp2otpiH2UE6EjegoMFJs287T0TTZhxSIK94JMlUNTlXAGYQoKNHmHq X-Received: by 2002:a05:6a20:a2a5:b0:18f:9c4:d345 with SMTP id a37-20020a056a20a2a500b0018f09c4d345mr1180773pzl.53.1701772770165; Tue, 05 Dec 2023 02:39:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701772770; cv=none; d=google.com; s=arc-20160816; b=jbSw3StFNK55T2EkNAmRJJCgzZbD02lImu1rLn6tqdJQlMR6H9eNqrYZpAdXjWE+N2 flGuvC5V4XYHLh1yMEZnFNUnOF+s8li4MWLLkSz0JtQL0ndVQJh3B5lsc27sp258A7t4 Hl3FKE4mPo96ekS2zfmRIvXIWNBPlf5Q+g61o1Z35RG4O5X4C8SeEz3aiSya8lS0ECGH 1akcVb112mdnqjR3eg18p/F/U1TFhGiA8wTCxTeCDLCzwjeKzN7UdCM4q43RoU3xMlo3 LieR2aUAPSejYn5LP/Px3sI6ogktwwg5zG3PwCOWIeVHGXdbt0icSkCDcl6Xw4EBGn/v SH8g== 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=JqrUdeRCIHO7c322SPH7VrnU7d0mlWYAQF7EMV1tQ3M=; fh=N2PMIgUB1WENx8VVMRFY1EHLciTqX2wQ6sXFWT4E2R4=; b=eBwAcoI/I4XFdGFR3hJlqBFxXckTqz5qG+evbY8DG7n0h3hPnKAVuZdXSS3j/b5n1y y4PRStKCWIlJA3cLAUftCREQUUwedSDVWWmZVk1ycy3z7Ul62/N20lfsGmivchiG2ntb QKvTmZ+N+gx2CuyDFbxJnw2jmjb5wVsmweg1z4DQ2hX1t9Pw8NPF3NHBpMfni53tso49 KQxo4Tnk8vDcPdgvfQedsPdcLnXRmHmD50CZQOK7/+tRlh3k9mrMec0uTKhCT3DGC6BN /DWi1xCCbStlqBuEoc9U//auLNc6Wb6FyK+B/VqlfGKj4vq6cZHLg9V1C6NTIfKaRVZ4 zieQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=dhJKPbHZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 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 morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id k187-20020a6324c4000000b005c69246e16bsi2968210pgk.2.2023.12.05.02.39.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Dec 2023 02:39:30 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=dhJKPbHZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 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 morse.vger.email (Postfix) with ESMTP id 4152580E70A0; Tue, 5 Dec 2023 02:39:26 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346860AbjLEKjI (ORCPT + 99 others); Tue, 5 Dec 2023 05:39:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58326 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235608AbjLEKij (ORCPT ); Tue, 5 Dec 2023 05:38:39 -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 2D5CB1999 for ; Tue, 5 Dec 2023 02:38:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1701772683; 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=JqrUdeRCIHO7c322SPH7VrnU7d0mlWYAQF7EMV1tQ3M=; b=dhJKPbHZspamWrSLg0L0f6KeSHrZsEMyoM9K+WRjQkjzkJJh6sh6+ZJkfGi0lDZaYEwGbc vPW0g9+7/GEsngR86QMLpujPTYZlb8Y4FPzt0lVcuEOtn8H7W58sNE+OVwBO2mrsC6lR/R ce5/s9M4ylVFuP360LTZNLg01bHp9+c= 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-90-NkA2zqv2M_WOP4_fHwQmyg-1; Tue, 05 Dec 2023 05:38:00 -0500 X-MC-Unique: NkA2zqv2M_WOP4_fHwQmyg-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 559D329AA395; Tue, 5 Dec 2023 10:38:00 +0000 (UTC) Received: from localhost.localdomain (unknown [10.45.225.152]) by smtp.corp.redhat.com (Postfix) with ESMTP id E109DC15E6A; Tue, 5 Dec 2023 10:37:57 +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 4/4] KVM: x86: add new nested vmexit tracepoints Date: Tue, 5 Dec 2023 12:37:45 +0200 Message-Id: <20231205103745.506724-5-mlevitsk@redhat.com> In-Reply-To: <20231205103745.506724-1-mlevitsk@redhat.com> References: <20231205103745.506724-1-mlevitsk@redhat.com> MIME-Version: 1.0 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 morse.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 (morse.vger.email [0.0.0.0]); Tue, 05 Dec 2023 02:39:26 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784438083950686297 X-GMAIL-MSGID: 1784438083950686297 Add 3 new tracepoints for nested VM exits which are intended to capture extra information to gain insights about the nested guest behavior. The new tracepoints are: - kvm_nested_msr - kvm_nested_hypercall These tracepoints capture extra register state to be able to know which MSR or which hypercall was done. - kvm_nested_page_fault This tracepoint allows to capture extra info about which host pagefault error code caused the nested page fault. Reviewed-by: Paolo Bonzini Signed-off-by: Maxim Levitsky --- arch/x86/kvm/svm/nested.c | 22 +++++++++++ arch/x86/kvm/trace.h | 82 +++++++++++++++++++++++++++++++++++++-- arch/x86/kvm/vmx/nested.c | 27 +++++++++++++ arch/x86/kvm/x86.c | 3 ++ 4 files changed, 131 insertions(+), 3 deletions(-) diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c index 3fea8c47679e689..05fc257818fc112 100644 --- a/arch/x86/kvm/svm/nested.c +++ b/arch/x86/kvm/svm/nested.c @@ -38,6 +38,8 @@ static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu, { struct vcpu_svm *svm = to_svm(vcpu); struct vmcb *vmcb = svm->vmcb; + u64 host_error_code = vmcb->control.exit_info_1; + if (vmcb->control.exit_code != SVM_EXIT_NPF) { /* @@ -48,11 +50,15 @@ static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu, vmcb->control.exit_code_hi = 0; vmcb->control.exit_info_1 = (1ULL << 32); vmcb->control.exit_info_2 = fault->address; + host_error_code = 0; } vmcb->control.exit_info_1 &= ~0xffffffffULL; vmcb->control.exit_info_1 |= fault->error_code; + trace_kvm_nested_page_fault(fault->address, host_error_code, + fault->error_code); + nested_svm_vmexit(svm); } @@ -1139,6 +1145,22 @@ int nested_svm_vmexit(struct vcpu_svm *svm) vmcb12->control.exit_int_info_err, KVM_ISA_SVM); + /* Collect some info about nested VM exits */ + switch (vmcb12->control.exit_code) { + case SVM_EXIT_MSR: + trace_kvm_nested_msr(vmcb12->control.exit_info_1 == 1, + kvm_rcx_read(vcpu), + (vmcb12->save.rax & 0xFFFFFFFFull) | + (((u64)kvm_rdx_read(vcpu) << 32))); + break; + case SVM_EXIT_VMMCALL: + trace_kvm_nested_hypercall(vmcb12->save.rax, + kvm_rbx_read(vcpu), + kvm_rcx_read(vcpu), + kvm_rdx_read(vcpu)); + break; + } + kvm_vcpu_unmap(vcpu, &map, true); nested_svm_transition_tlb_flush(vcpu); diff --git a/arch/x86/kvm/trace.h b/arch/x86/kvm/trace.h index e275a02a21e5233..782c435bddfd454 100644 --- a/arch/x86/kvm/trace.h +++ b/arch/x86/kvm/trace.h @@ -610,7 +610,7 @@ TRACE_EVENT(kvm_pv_eoi, ); /* - * Tracepoint for nested VMRUN + * Tracepoint for nested VMRUN/VMENTER */ TRACE_EVENT(kvm_nested_vmenter, TP_PROTO(__u64 rip, __u64 vmcb, __u64 nested_rip, __u32 int_ctl, @@ -743,8 +743,84 @@ TRACE_EVENT(kvm_nested_intr_vmexit, TP_printk("rip: 0x%016llx", __entry->rip) ); + /* - * Tracepoint for nested #vmexit because of interrupt pending + * Tracepoint for nested guest MSR access. + */ +TRACE_EVENT(kvm_nested_msr, + TP_PROTO(bool write, u32 ecx, u64 data), + TP_ARGS(write, ecx, data), + + TP_STRUCT__entry( + __field( bool, write ) + __field( u32, ecx ) + __field( u64, data ) + ), + + TP_fast_assign( + __entry->write = write; + __entry->ecx = ecx; + __entry->data = data; + ), + + TP_printk("msr_%s %x = 0x%llx", + __entry->write ? "write" : "read", + __entry->ecx, __entry->data) +); + +/* + * Tracepoint for nested hypercalls, capturing generic info about the + * hypercall + */ + +TRACE_EVENT(kvm_nested_hypercall, + TP_PROTO(u64 rax, u64 rbx, u64 rcx, u64 rdx), + TP_ARGS(rax, rbx, rcx, rdx), + + TP_STRUCT__entry( + __field( u64, rax ) + __field( u64, rbx ) + __field( u64, rcx ) + __field( u64, rdx ) + ), + + TP_fast_assign( + __entry->rax = rax; + __entry->rbx = rbx; + __entry->rcx = rcx; + __entry->rdx = rdx; + ), + + TP_printk("rax 0x%llx rbx 0x%llx rcx 0x%llx rdx 0x%llx", + __entry->rax, __entry->rbx, __entry->rcx, __entry->rdx) +); + + +TRACE_EVENT(kvm_nested_page_fault, + TP_PROTO(u64 gpa, u64 host_error_code, u64 guest_error_code), + TP_ARGS(gpa, host_error_code, guest_error_code), + + TP_STRUCT__entry( + __field( u64, gpa ) + __field( u64, host_error_code ) + __field( u64, guest_errror_code ) + ), + + TP_fast_assign( + __entry->gpa = gpa; + __entry->host_error_code = host_error_code; + __entry->guest_errror_code = guest_error_code; + ), + + TP_printk("gpa 0x%llx host err 0x%llx guest err 0x%llx", + __entry->gpa, + __entry->host_error_code, + __entry->guest_errror_code) +); + + +/* + * Tracepoint for invlpga */ TRACE_EVENT(kvm_invlpga, TP_PROTO(__u64 rip, int asid, u64 address), @@ -767,7 +843,7 @@ TRACE_EVENT(kvm_invlpga, ); /* - * Tracepoint for nested #vmexit because of interrupt pending + * Tracepoint for skinit */ TRACE_EVENT(kvm_skinit, TP_PROTO(__u64 rip, __u32 slb), diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c index c5ec0ef51ff78fa..23325a7af8fc2ab 100644 --- a/arch/x86/kvm/vmx/nested.c +++ b/arch/x86/kvm/vmx/nested.c @@ -402,6 +402,16 @@ static void nested_ept_inject_page_fault(struct kvm_vcpu *vcpu, */ nested_ept_invalidate_addr(vcpu, vmcs12->ept_pointer, fault->address); + + /* + * vmx_get_exit_qual() returns the original exit qualification, + * before it was overridden with exit qualification that + * is about to be injected to the guest. + */ + + trace_kvm_nested_page_fault(fault->address, + vmx_get_exit_qual(vcpu), + exit_qualification); } nested_vmx_vmexit(vcpu, vm_exit_reason, 0, exit_qualification); @@ -4877,6 +4887,23 @@ void nested_vmx_vmexit(struct kvm_vcpu *vcpu, u32 vm_exit_reason, vmcs12->vm_exit_intr_error_code, KVM_ISA_VMX); + switch ((u16)vmcs12->vm_exit_reason) { + case EXIT_REASON_MSR_READ: + case EXIT_REASON_MSR_WRITE: + trace_kvm_nested_msr(vmcs12->vm_exit_reason == EXIT_REASON_MSR_WRITE, + kvm_rcx_read(vcpu), + (kvm_rax_read(vcpu) & 0xFFFFFFFFull) | + (((u64)kvm_rdx_read(vcpu)) << 32)); + break; + case EXIT_REASON_VMCALL: + trace_kvm_nested_hypercall(kvm_rax_read(vcpu), + kvm_rbx_read(vcpu), + kvm_rcx_read(vcpu), + kvm_rdx_read(vcpu)); + break; + + } + load_vmcs12_host_state(vcpu, vmcs12); return; diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 2089a0b08ce08c0..c75001abca8e835 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -13787,6 +13787,9 @@ EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmenter); EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit); EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit_inject); EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intr_vmexit); +EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_hypercall); +EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_page_fault); +EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_msr); EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmenter_failed); EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga); EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit);