Received: by 2002:a05:6a10:a852:0:0:0:0 with SMTP id d18csp1301415pxy; Thu, 6 May 2021 05:17:13 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz3baPVSYDAcmYAbocCjvEivnLwK7yb+HLFgZwLP1WdwfLYGWXhfLOHOLgxb2rBdOXLVeXR X-Received: by 2002:a50:8fe6:: with SMTP id y93mr4794806edy.224.1620303433114; Thu, 06 May 2021 05:17:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620303433; cv=none; d=google.com; s=arc-20160816; b=sYZBC3jAho1kbNfkcFjcCI8eyCcjpasomKVHbKTpoJC2yi91ZGEPhZ37l6W6jVs+S5 OefB7hBI+E8qu7NK2hWuWR5gSKZOrSikoNapCGOU2uQkDm2J4LwF3I+SmtvrERp36ZhH I0oiVCrpqWf+yO+FyQ0d8cKO+KuJh6ECo2zSfpS3fXG5BamzSyHUUIZaf3LEJLioMeNr rHaOlXLd6XUU5V1TzINGGy5ku9yX9iBmThjfSb2HyOptIMA8uR7UjmyNoGKB2BWyZpwU 2uWqCsAWsYEsxDgueVk3FmgP0XgtxtC2ngThpaPXk2ywSmqUp0JoPDqqLXBTstgTb+t3 HpZQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:robot-unsubscribe :robot-id:message-id:mime-version:references:in-reply-to:cc:subject :to:reply-to:sender:from:dkim-signature:dkim-signature:date; bh=GAZROfdPR5Ma0vGOTUz0TuchvP7f6VZotsvygKerrnQ=; b=stkljLDhmRGZx2FapHNteki94nG4kCuTNHAO71MBgt3WGEf0ZSqIOZnEqyPZ9DUSof oQbk3BwhHJ1W4zfIuuGwd7Sfmq50fNffi4xTUBcTk/xWfaoYblZmCLc5/1xiwsGGzIDe O/f9O+7XyZ+owz5FVR6lysLw2jVLuPa3C+Reveq3RCusF9kdFumOzFIXD0PIqrFpVfsx /hn4C6EhdCI/Fza5VhOA9G1T3BFIvKc752L+X3S5j9XM+ZubUVBhM9TB2SviSAQMprjq 2dFUE6XOTomN8f5JGQyMHygWzZWGj02hdR9RH9jV+vI52/ZLXd4GBseAvmdNAfWZw+qx bENg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b="M0c/awy2"; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=yfH2IX8W; 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=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id o6si2865093edi.480.2021.05.06.05.16.34; Thu, 06 May 2021 05:17:13 -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=@linutronix.de header.s=2020 header.b="M0c/awy2"; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=yfH2IX8W; 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=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233227AbhEFMPO (ORCPT + 99 others); Thu, 6 May 2021 08:15:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44842 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232836AbhEFMPE (ORCPT ); Thu, 6 May 2021 08:15:04 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 67635C061761; Thu, 6 May 2021 05:14:05 -0700 (PDT) Date: Thu, 06 May 2021 12:14:02 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1620303243; h=from:from:sender:sender:reply-to: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=GAZROfdPR5Ma0vGOTUz0TuchvP7f6VZotsvygKerrnQ=; b=M0c/awy24zSnaX5vIXf7Dp3c1B5r5CENXfW9b9i4w/YzHb1YcypfetQ1zV+RyWaWE3aBY5 vN6KF0TvqANWfic2JjKf/ayDtQ2LmM+P4So1ZCuG5d/N4hjQbe5A1Ip3iMaSrnoZDO/LFM 2gFXGMyP2Z9juB2j3ip8n8coPgrR6O59DgAn601c+7JOFcoPty9QcszFHI4TZYvp5U+m4b DUiWeSK/HgHb3JeZ57iDk2M3PR/3IX5ZWPlwpJWIfcVofVOQIDqna+SLSZh1HXgGYBGgve TH1ynMY1nR0zFdEbNdw0DYJU6V75YjO9dWDlEmf4Dt/rPOFPcS4WnAPabTeR1Q== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1620303243; h=from:from:sender:sender:reply-to: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=GAZROfdPR5Ma0vGOTUz0TuchvP7f6VZotsvygKerrnQ=; b=yfH2IX8WMi227Xgrst2bIXTbmOQ+75JggOcSLuYzURYr+XZL1RwyYddDyfx1GDMF2nbFtW NVi/rTdS+pKJnPCw== From: "tip-bot2 for Sean Christopherson" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/urgent] KVM: x86: Consolidate guest enter/exit logic to common helpers Cc: Sean Christopherson , Thomas Gleixner , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20210505002735.1684165-9-seanjc@google.com> References: <20210505002735.1684165-9-seanjc@google.com> MIME-Version: 1.0 Message-ID: <162030324232.29796.17958886782368055460.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The following commit has been merged into the x86/urgent branch of tip: Commit-ID: bc908e091b3264672889162733020048901021fb Gitweb: https://git.kernel.org/tip/bc908e091b3264672889162733020048901021fb Author: Sean Christopherson AuthorDate: Tue, 04 May 2021 17:27:35 -07:00 Committer: Thomas Gleixner CommitterDate: Wed, 05 May 2021 22:54:12 +02:00 KVM: x86: Consolidate guest enter/exit logic to common helpers Move the enter/exit logic in {svm,vmx}_vcpu_enter_exit() to common helpers. Opportunistically update the somewhat stale comment about the updates needing to occur immediately after VM-Exit. No functional change intended. Signed-off-by: Sean Christopherson Signed-off-by: Thomas Gleixner Link: https://lore.kernel.org/r/20210505002735.1684165-9-seanjc@google.com --- arch/x86/kvm/svm/svm.c | 39 +----------------------------------- arch/x86/kvm/vmx/vmx.c | 39 +----------------------------------- arch/x86/kvm/x86.h | 45 +++++++++++++++++++++++++++++++++++++++++- 3 files changed, 49 insertions(+), 74 deletions(-) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index c400def..b649f92 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -3710,25 +3710,7 @@ static noinstr void svm_vcpu_enter_exit(struct kvm_vcpu *vcpu) struct vcpu_svm *svm = to_svm(vcpu); unsigned long vmcb_pa = svm->current_vmcb->pa; - /* - * VMENTER enables interrupts (host state), but the kernel state is - * interrupts disabled when this is invoked. Also tell RCU about - * it. This is the same logic as for exit_to_user_mode(). - * - * This ensures that e.g. latency analysis on the host observes - * guest mode as interrupt enabled. - * - * guest_enter_irqoff() informs context tracking about the - * transition to guest mode and if enabled adjusts RCU state - * accordingly. - */ - instrumentation_begin(); - trace_hardirqs_on_prepare(); - lockdep_hardirqs_on_prepare(CALLER_ADDR0); - instrumentation_end(); - - guest_enter_irqoff(); - lockdep_hardirqs_on(CALLER_ADDR0); + kvm_guest_enter_irqoff(); if (sev_es_guest(vcpu->kvm)) { __svm_sev_es_vcpu_run(vmcb_pa); @@ -3748,24 +3730,7 @@ static noinstr void svm_vcpu_enter_exit(struct kvm_vcpu *vcpu) vmload(__sme_page_pa(sd->save_area)); } - /* - * VMEXIT disables interrupts (host state), but tracing and lockdep - * have them in state 'on' as recorded before entering guest mode. - * Same as enter_from_user_mode(). - * - * context_tracking_guest_exit() restores host context and reinstates - * RCU if enabled and required. - * - * This needs to be done before the below as native_read_msr() - * contains a tracepoint and x86_spec_ctrl_restore_host() calls - * into world and some more. - */ - lockdep_hardirqs_off(CALLER_ADDR0); - context_tracking_guest_exit(); - - instrumentation_begin(); - trace_hardirqs_off_finish(); - instrumentation_end(); + kvm_guest_exit_irqoff(); } static __no_kcsan fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index e108fb4..d000cdd 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -6664,25 +6664,7 @@ static fastpath_t vmx_exit_handlers_fastpath(struct kvm_vcpu *vcpu) static noinstr void vmx_vcpu_enter_exit(struct kvm_vcpu *vcpu, struct vcpu_vmx *vmx) { - /* - * VMENTER enables interrupts (host state), but the kernel state is - * interrupts disabled when this is invoked. Also tell RCU about - * it. This is the same logic as for exit_to_user_mode(). - * - * This ensures that e.g. latency analysis on the host observes - * guest mode as interrupt enabled. - * - * guest_enter_irqoff() informs context tracking about the - * transition to guest mode and if enabled adjusts RCU state - * accordingly. - */ - instrumentation_begin(); - trace_hardirqs_on_prepare(); - lockdep_hardirqs_on_prepare(CALLER_ADDR0); - instrumentation_end(); - - guest_enter_irqoff(); - lockdep_hardirqs_on(CALLER_ADDR0); + kvm_guest_enter_irqoff(); /* L1D Flush includes CPU buffer clear to mitigate MDS */ if (static_branch_unlikely(&vmx_l1d_should_flush)) @@ -6698,24 +6680,7 @@ static noinstr void vmx_vcpu_enter_exit(struct kvm_vcpu *vcpu, vcpu->arch.cr2 = native_read_cr2(); - /* - * VMEXIT disables interrupts (host state), but tracing and lockdep - * have them in state 'on' as recorded before entering guest mode. - * Same as enter_from_user_mode(). - * - * context_tracking_guest_exit() restores host context and reinstates - * RCU if enabled and required. - * - * This needs to be done before the below as native_read_msr() - * contains a tracepoint and x86_spec_ctrl_restore_host() calls - * into world and some more. - */ - lockdep_hardirqs_off(CALLER_ADDR0); - context_tracking_guest_exit(); - - instrumentation_begin(); - trace_hardirqs_off_finish(); - instrumentation_end(); + kvm_guest_exit_irqoff(); } static fastpath_t vmx_vcpu_run(struct kvm_vcpu *vcpu) diff --git a/arch/x86/kvm/x86.h b/arch/x86/kvm/x86.h index 8ddd381..521f74e 100644 --- a/arch/x86/kvm/x86.h +++ b/arch/x86/kvm/x86.h @@ -8,6 +8,51 @@ #include "kvm_cache_regs.h" #include "kvm_emulate.h" +static __always_inline void kvm_guest_enter_irqoff(void) +{ + /* + * VMENTER enables interrupts (host state), but the kernel state is + * interrupts disabled when this is invoked. Also tell RCU about + * it. This is the same logic as for exit_to_user_mode(). + * + * This ensures that e.g. latency analysis on the host observes + * guest mode as interrupt enabled. + * + * guest_enter_irqoff() informs context tracking about the + * transition to guest mode and if enabled adjusts RCU state + * accordingly. + */ + instrumentation_begin(); + trace_hardirqs_on_prepare(); + lockdep_hardirqs_on_prepare(CALLER_ADDR0); + instrumentation_end(); + + guest_enter_irqoff(); + lockdep_hardirqs_on(CALLER_ADDR0); +} + +static __always_inline void kvm_guest_exit_irqoff(void) +{ + /* + * VMEXIT disables interrupts (host state), but tracing and lockdep + * have them in state 'on' as recorded before entering guest mode. + * Same as enter_from_user_mode(). + * + * context_tracking_guest_exit() restores host context and reinstates + * RCU if enabled and required. + * + * This needs to be done immediately after VM-Exit, before any code + * that might contain tracepoints or call out to the greater world, + * e.g. before x86_spec_ctrl_restore_host(). + */ + lockdep_hardirqs_off(CALLER_ADDR0); + context_tracking_guest_exit(); + + instrumentation_begin(); + trace_hardirqs_off_finish(); + instrumentation_end(); +} + #define KVM_NESTED_VMENTER_CONSISTENCY_CHECK(consistency_check) \ ({ \ bool failed = (consistency_check); \