Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp1048718iog; Mon, 13 Jun 2022 20:06:40 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sVZyyFA3OfAPY5c/pPSmrnnt3L4Cn4hEre0ixRr9MXsQN+wDItgv289b5Fp9H+3F/TJ2RW X-Received: by 2002:a17:902:e552:b0:163:6a5e:4e08 with SMTP id n18-20020a170902e55200b001636a5e4e08mr2399810plf.130.1655176000014; Mon, 13 Jun 2022 20:06:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655176000; cv=none; d=google.com; s=arc-20160816; b=iR94qVu7IxAWhylLVUj5HnNrrNRVcqFE63Tk7LMyfjgTJzbKvS2m5uUns40tpAhFyI n3pCb1HBN8scYlNh4sYXcT6MtZ0J11MeGPBONmfDP45ecFOJyuOZe6V5Tr670H6DN0qu gkmNVHb6PjHfZkALOPJZtIoJ3uqrQBLSgT/rCRiUJ6tLWYPGGlxZjFbqKWQiUQVXU44/ opcJ2/zrO0W0PEJsHc1mm95l1iZYhycJRwwgnn8NRBtVFBj/RsjhAKTHtXOVEL46g5PX nAOVjrCKg0J0fu+kg3lEA0W/kUFTTEXBhStOBSPnabbPFQ7EhaLvh8RnvPsDXXxQq/kr gVsg== 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=PrqZa/HfljWayWXKld6o0cPQmrkumgTTJquai6q0lFc=; b=o4tmuPQw0RLb3MU8gv5SIj1RsHai0MviHp5dNcFjybV55ZOo0A/k3fjq2dTBBjLQEY pgXNlBsDthHAr3wvHwN9ybLRwG07Q0f9wCOnaIKeNkbGL7pBtNK5TGTO1dXABy8axJ7c sl7COm5L46TfIa0605NOnml4oKaGOxCUR7zRhoPZMdFg4qDo9ibyQ2dO8R8GKMFfLD3g reX36NZUK1ymCmt069i8GXqQEOIjDShq3hbEBMpLLEDPjUqvSIAf4yj/ZcIIk5A+oa9o IeSCbJnsN2FaHpy3EsRJc4BjoqhRe4IrnlAH7S5ibmZZ9CdRrkHlZ3JRA6i1NaQj0VNE VXLA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=bhi3snlD; 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=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d14-20020a655ace000000b003faadaeee37si12389596pgt.750.2022.06.13.20.06.27; Mon, 13 Jun 2022 20:06:40 -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=@kernel.org header.s=k20201202 header.b=bhi3snlD; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1353159AbiFNCdM (ORCPT + 99 others); Mon, 13 Jun 2022 22:33:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44772 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1356008AbiFNCaK (ORCPT ); Mon, 13 Jun 2022 22:30:10 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E987843394; Mon, 13 Jun 2022 19:11:50 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 6431BB816A4; Tue, 14 Jun 2022 02:11:50 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E10A1C3411B; Tue, 14 Jun 2022 02:11:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1655172709; bh=V3TSzVV9i2ntLV+h4+Q9H9UFVn0/1FK7R0MjMTVOVcM=; h=From:To:Cc:Subject:Date:From; b=bhi3snlDWs2W7AZIgKuUbaDbVth9TBCw1H9/H6UBitGCzGRP29l7i65EUJruIPjFT n8OSjXE4L1bo91OACAqnp2nca2oe6UJlr8K2kw5KAtDf4zqgJAj2vLzDNLDAgoPGR9 4RYyyMS1L6SXSyCw/P0a/Y+yKsxFHSK6srDjujgG8WZFmDIJ6IHMG0gwOhS7vbpHwq VakC6illdqQ/p4OPTmt3ad+QuzgI6xbrAOJqQoWSgcpy3GUPkJfkudGE7wuOUKiO4W Mh1VMiC5KSORIOFPZb6URv97DB+HVGBr6qFhj25mZBKA95lDv2tfdD2WRxEcMTQcQo dNqoIJbrhvusg== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Paolo Bonzini , Jann Horn , Sasha Levin , tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, kvm@vger.kernel.org Subject: [PATCH MANUALSEL 5.15 1/4] KVM: x86: do not report a vCPU as preempted outside instruction boundaries Date: Mon, 13 Jun 2022 22:11:37 -0400 Message-Id: <20220614021141.1101486-1-sashal@kernel.org> X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-8.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham 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: Paolo Bonzini [ Upstream commit 6cd88243c7e03845a450795e134b488fc2afb736 ] If a vCPU is outside guest mode and is scheduled out, it might be in the process of making a memory access. A problem occurs if another vCPU uses the PV TLB flush feature during the period when the vCPU is scheduled out, and a virtual address has already been translated but has not yet been accessed, because this is equivalent to using a stale TLB entry. To avoid this, only report a vCPU as preempted if sure that the guest is at an instruction boundary. A rescheduling request will be delivered to the host physical CPU as an external interrupt, so for simplicity consider any vmexit *not* instruction boundary except for external interrupts. It would in principle be okay to report the vCPU as preempted also if it is sleeping in kvm_vcpu_block(): a TLB flush IPI will incur the vmentry/vmexit overhead unnecessarily, and optimistic spinning is also unlikely to succeed. However, leave it for later because right now kvm_vcpu_check_block() is doing memory accesses. Even though the TLB flush issue only applies to virtual memory address, it's very much preferrable to be conservative. Reported-by: Jann Horn Signed-off-by: Paolo Bonzini Signed-off-by: Sasha Levin --- arch/x86/include/asm/kvm_host.h | 3 +++ arch/x86/kvm/svm/svm.c | 2 ++ arch/x86/kvm/vmx/vmx.c | 1 + arch/x86/kvm/x86.c | 22 ++++++++++++++++++++++ 4 files changed, 28 insertions(+) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 49d814b2a341..a35f5e23fc2a 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -642,6 +642,7 @@ struct kvm_vcpu_arch { u64 ia32_misc_enable_msr; u64 smbase; u64 smi_count; + bool at_instruction_boundary; bool tpr_access_reporting; bool xsaves_enabled; u64 ia32_xss; @@ -1271,6 +1272,8 @@ struct kvm_vcpu_stat { u64 nested_run; u64 directed_yield_attempted; u64 directed_yield_successful; + u64 preemption_reported; + u64 preemption_other; u64 guest_mode; }; diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 26f2da1590ed..5b51156712f7 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -4263,6 +4263,8 @@ static int svm_check_intercept(struct kvm_vcpu *vcpu, static void svm_handle_exit_irqoff(struct kvm_vcpu *vcpu) { + if (to_svm(vcpu)->vmcb->control.exit_code == SVM_EXIT_INTR) + vcpu->arch.at_instruction_boundary = true; } static void svm_sched_in(struct kvm_vcpu *vcpu, int cpu) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 16a660a0ed5f..08f9f05a6cb2 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -6390,6 +6390,7 @@ static void handle_external_interrupt_irqoff(struct kvm_vcpu *vcpu) return; handle_interrupt_nmi_irqoff(vcpu, gate_offset(desc)); + vcpu->arch.at_instruction_boundary = true; } static void vmx_handle_exit_irqoff(struct kvm_vcpu *vcpu) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 23905ba3058a..fbf10fa99507 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -277,6 +277,8 @@ const struct _kvm_stats_desc kvm_vcpu_stats_desc[] = { STATS_DESC_COUNTER(VCPU, nested_run), STATS_DESC_COUNTER(VCPU, directed_yield_attempted), STATS_DESC_COUNTER(VCPU, directed_yield_successful), + STATS_DESC_COUNTER(VCPU, preemption_reported), + STATS_DESC_COUNTER(VCPU, preemption_other), STATS_DESC_ICOUNTER(VCPU, guest_mode) }; @@ -4368,6 +4370,19 @@ static void kvm_steal_time_set_preempted(struct kvm_vcpu *vcpu) struct kvm_memslots *slots; static const u8 preempted = KVM_VCPU_PREEMPTED; + /* + * The vCPU can be marked preempted if and only if the VM-Exit was on + * an instruction boundary and will not trigger guest emulation of any + * kind (see vcpu_run). Vendor specific code controls (conservatively) + * when this is true, for example allowing the vCPU to be marked + * preempted if and only if the VM-Exit was due to a host interrupt. + */ + if (!vcpu->arch.at_instruction_boundary) { + vcpu->stat.preemption_other++; + return; + } + + vcpu->stat.preemption_reported++; if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED)) return; @@ -9936,6 +9951,13 @@ static int vcpu_run(struct kvm_vcpu *vcpu) vcpu->arch.l1tf_flush_l1d = true; for (;;) { + /* + * If another guest vCPU requests a PV TLB flush in the middle + * of instruction emulation, the rest of the emulation could + * use a stale page translation. Assume that any code after + * this point can start executing an instruction. + */ + vcpu->arch.at_instruction_boundary = false; if (kvm_vcpu_running(vcpu)) { r = vcpu_enter_guest(vcpu); } else { -- 2.35.1