Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp1795033pxy; Fri, 23 Apr 2021 17:54:15 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx+loH9VnHW4JnnleXLkNENvXXheAHraGPdWEpFKFL1Yim/DPG2Rr/VJRoYXu/6/qQTCmzG X-Received: by 2002:a05:6402:1698:: with SMTP id a24mr7581156edv.62.1619225655729; Fri, 23 Apr 2021 17:54:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619225655; cv=none; d=google.com; s=arc-20160816; b=mX4G3g7HI1hRNsphFbuiCsHvK7TsazApFb8UwXv5cIOYjYyvo56ASXZrj0agQ1hl2S XSeKNElYUBeZKmDk5Hr9sR0ODOqh67195/yl7joXN6vr4PkZmg/BftAXDR0zkN97FFJf KU4NdXkb8PIl6usdu8VC9It9PDUtlPW/vAmimI3ezXfxVNRq2kAqAnupJfU0Qz1QCEz1 aHt+FRcQdvp50jO/3m6JYyNsJDLYw8/B27jNo+a+XJFLm3ZXYIDewyOF8M1NXpQh9gs4 COxxrTuGp5gEpNhYUMdPFBNMXz1W9Yos1ZvOY7v17isjqoap0N1D3f4MxLvTn+sdTKIA DEvg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:reply-to:dkim-signature; bh=JqcOnCGExfR3YjMtsB4lLkNrTfGGIsxEsg5SkDkGdDA=; b=RtwJdKqUr7dX+SodKo9CrWyWiGc4y+FgSKq2ykKOf8H2aOK5ByP9efpLjcLBjI+sXf 9w/i8pDPNkr+MNlJmQqc7oXZBH9aovP6qgkoLN7dHdCbKVsNReSyrJcUKjK4G3t0mzJh dM8W7vyQMWfvP43gysvQ/9HRbW4Tq/EV9vPoaFoPW1jrLattkP69A70KrXnlaJM1gkY+ 2ZLRsHNImHLkUcohh2AwAxz7j67RKba+Eh5lZnzjJA4xGxxwDfqF/TpOwW9mh4Aqk1eQ kUSu4XBAEAkleLXfaGOEw0e/p3J5FtIeaRrHb5h8xMLvCDm9vEfWaGquqpFrZS8mJRff bkqA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=hTIMu62j; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id e7si6378743ejj.140.2021.04.23.17.53.51; Fri, 23 Apr 2021 17:54:15 -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=@google.com header.s=20161025 header.b=hTIMu62j; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244501AbhDXAxY (ORCPT + 99 others); Fri, 23 Apr 2021 20:53:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36828 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244608AbhDXAvt (ORCPT ); Fri, 23 Apr 2021 20:51:49 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A0ADEC061347 for ; Fri, 23 Apr 2021 17:48:00 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id s34-20020a252d620000b02904e34d3a48abso26209888ybe.13 for ; Fri, 23 Apr 2021 17:48:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=JqcOnCGExfR3YjMtsB4lLkNrTfGGIsxEsg5SkDkGdDA=; b=hTIMu62j1JfVoMqUJDMRxt4mX6dRaa2v8VIO52tvPHuQXjRNo0B9jHOMT4XZ66Ge8h /OafbtDyprXRunXNaiH0JuDl+PhiPUF1F91bHHOkmTWu20fMrd+4/2lporx6mFkHLz/Q 53/btJAj3u5jCLr4l8M/FVswWMOcZFPHi5yObFtdEf5FGPv/ntYqMDsE/6HUniIWceLa UDlZz/XxdEuF8pNOW75zu1kPSBXUOuDz3ztcQ1zlQkCziPqxlZU/JyzbnAme7Bc26E9b e0jzQLjBdMzi4QrKpoa/olCS85DNJsb9g3SuAF0rJ4yfVDujU0y29sqcGcFb8DLkBAKw YlDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=JqcOnCGExfR3YjMtsB4lLkNrTfGGIsxEsg5SkDkGdDA=; b=nBADQexyYKWgLQdaUE4o0f2GD15FM9XoqJx5C/C7ulOSd4gdzqg/t3Hj8gNCXl/0mC XjJHhye/9Vr9v815lsijZKNPMK5wV5LSC1fxw8H4pNn5N9/rhUSjkVvWnwOEOV/p4o9v J6VpTTo4ZB9W25RwmlSeJ4k2I+/lW1vhobED1+Eu/Dy3NQaDIrZFXlYyfOBH2xOfB7Dl MZzTwOxuo6lh+bva37ZXriWAf/8dhi3pzhADBx0NGIQC2UNBFx65i5ovyYlsPewVu8Ls MNUPx5/TP4/zgSxrZrnDgV1iE5cpHUIh9nxStIhO4IFv/hQubfiodxhX8MTJCj4T5azh ikuA== X-Gm-Message-State: AOAM531s89/9+jYwHV7yStFEThpQpjFQllSGriYD1SaVUqTv/ci762oe K0iVupJK/TBof26IrYK9+jNx5rl7ezU= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:f:10:ad52:3246:e190:f070]) (user=seanjc job=sendgmr) by 2002:a25:d152:: with SMTP id i79mr10295563ybg.328.1619225279897; Fri, 23 Apr 2021 17:47:59 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 23 Apr 2021 17:46:29 -0700 In-Reply-To: <20210424004645.3950558-1-seanjc@google.com> Message-Id: <20210424004645.3950558-28-seanjc@google.com> Mime-Version: 1.0 References: <20210424004645.3950558-1-seanjc@google.com> X-Mailer: git-send-email 2.31.1.498.g6c1eba8ee3d-goog Subject: [PATCH 27/43] KVM: VMX: Skip emulation required checks during pmode/rmode transitions From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Don't refresh "emulation required" when stuffing segments during transitions to/from real mode when running without unrestricted guest. The checks are unnecessary as vmx_set_cr0() unconditionally rechecks "emulation required". They also happen to be broken, as enter_pmode() and enter_rmode() run with a stale vcpu->arch.cr0. Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/vmx.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 5f30181fd240..37bfa20a04dd 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -2827,6 +2827,8 @@ static __init int alloc_kvm_area(void) return 0; } +static void __vmx_set_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg); + static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg, struct kvm_segment *save) { @@ -2843,7 +2845,7 @@ static void fix_pmode_seg(struct kvm_vcpu *vcpu, int seg, save->dpl = save->selector & SEGMENT_RPL_MASK; save->s = 1; } - vmx_set_segment(vcpu, save, seg); + __vmx_set_segment(vcpu, save, seg); } static void enter_pmode(struct kvm_vcpu *vcpu) @@ -2864,7 +2866,7 @@ static void enter_pmode(struct kvm_vcpu *vcpu) vmx->rmode.vm86_active = 0; - vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR); + __vmx_set_segment(vcpu, &vmx->rmode.segs[VCPU_SREG_TR], VCPU_SREG_TR); flags = vmcs_readl(GUEST_RFLAGS); flags &= RMODE_GUEST_OWNED_EFLAGS_BITS; @@ -3399,7 +3401,7 @@ static u32 vmx_segment_access_rights(struct kvm_segment *var) return ar; } -void vmx_set_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg) +static void __vmx_set_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg) { struct vcpu_vmx *vmx = to_vmx(vcpu); const struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; @@ -3412,7 +3414,7 @@ void vmx_set_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg) vmcs_write16(sf->selector, var->selector); else if (var->s) fix_rmode_seg(seg, &vmx->rmode.segs[seg]); - goto out; + return; } vmcs_writel(sf->base, var->base); @@ -3434,9 +3436,13 @@ void vmx_set_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg) var->type |= 0x1; /* Accessed */ vmcs_write32(sf->ar_bytes, vmx_segment_access_rights(var)); +} -out: - vmx->emulation_required = emulation_required(vcpu); +void vmx_set_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg) +{ + __vmx_set_segment(vcpu, var, seg); + + to_vmx(vcpu)->emulation_required = emulation_required(vcpu); } static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l) -- 2.31.1.498.g6c1eba8ee3d-goog