Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp4053067pxv; Tue, 13 Jul 2021 09:39:18 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz022EUpnyZTbmuvJwGL52sL/X1AOkm6cusLkYSap9/UBp5v+MZebZaNYEYWAhvO8iQWy7e X-Received: by 2002:a17:906:5d05:: with SMTP id g5mr6484905ejt.201.1626194358130; Tue, 13 Jul 2021 09:39:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626194358; cv=none; d=google.com; s=arc-20160816; b=pY6m3WeXZlMSh3TZV+eekYpxkyoRVzPqqHM1qM5/pBDrJPIYEFzl5hClEqy2DrVy5T VFCZwOQcfsyg4qM5Y5It0hId3mzRLuJzmtn0iWaz3U4SjlGMRPv8j9gSA3kj/7w7GpGn Y//CUwKIz8ctmmeRVhXxZ3zhMFJblRmEIKBdQNdSJWCPKbEURLuUQVbZlkuLNgk7t9+U I/uMnjfLDTgos0wMU7SI3I0/VdmaPWu/GyqPADYYoTTn9pBj2F7MRvSGoQPIx7qT5JOS IrdLfqNZSbstTsDKEhCpq0KZFsqscNiJmeXxv2u+oOzfd1qwt5n+k70aX6fiNgvs5bTY 4bHQ== 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=Uj2yMP906hWJGI2naVvWw65tlAlIQnpXZj8DT6KTFYY=; b=m7sAbFK8rpcjipk1AUKM57DJ7JVbKaPhBGUhGSNqp6WmF+SquMB7AvVzg1p0oc+0vH /59l3wXYWeXOMFVsR2o5RA6QoLT/6WiNwqnbjam0dNRIE2ydHmi4BHQxXOrO1eZGYYEN Vg1zMqfp9PYopypF+JkBhzKnKX0H9A0qxbcsDQaR5gVEwKCcj6b+2UjnF9qhDnveslxi zWstonnvJR4+Zn4OlzBDrfWKs7HcKVR8X2u9CHHWQaTPAiKoKjnAJVOuF6xhjL6tVzR4 EZnBr278u1WFJSqfLM7ANOFZUHS+IuZsb/rSrGAshkbTYDWuhlJ0UabzjsQNUQRauttU L2pw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=BxKkSE4n; 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 w26si3971876edu.538.2021.07.13.09.38.55; Tue, 13 Jul 2021 09:39:18 -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=BxKkSE4n; 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 S234558AbhGMQhu (ORCPT + 99 others); Tue, 13 Jul 2021 12:37:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45098 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234577AbhGMQhj (ORCPT ); Tue, 13 Jul 2021 12:37:39 -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 D91F6C0612FC for ; Tue, 13 Jul 2021 09:34:23 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 132-20020a25158a0000b029055791ebe1e6so27981048ybv.20 for ; Tue, 13 Jul 2021 09:34:23 -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=Uj2yMP906hWJGI2naVvWw65tlAlIQnpXZj8DT6KTFYY=; b=BxKkSE4nzeAN82oc/h3WNbMOSRY3kLdlaT0lHNjn7J4r+J+n8aJkIo3ROQbdwAp/yH 0ZINHZQZNXvGy2cM95myujd5l78dY2Tp1gB8s9W351mOgogMtBCeIUtqk1kBT5iXq0qD R9NRqcVKVpWgTxsU2jc9PhQejQngk8QL5FmFxA8zChtI0o/mFvRWRjqIhaUJq3/zmDvw K2jjCA7L3U+jh1vFCyx9Da+qHBAOKUAil5rAkUGPsAmCMaJnJP2BQe+sanJ0HRjV+nLj PMzoupGosc0XCIFqqcYrkAD0LHta7Xj18h701jf9TC+e+1whDLbXelZVJOLif7Zqym5i +H6g== 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=Uj2yMP906hWJGI2naVvWw65tlAlIQnpXZj8DT6KTFYY=; b=ZDz0tBYNQxDI25r+4zwc6z1erwVXaGGNcS0MEGpXG561jsvnRVhTZLjBkpOHMch2dN B0soYRRCh+gGqPXz2MAuND6xm8QLas+fJQkz6HaJcNCFojk5jTFN1yUl4mBivCrEKDaE Qu15nSGBuDb+na3CcAXx9BXOo7hiWfRLj+g9kMwwpsIHU7MEYeCp2j+XUHxNQkGVyqgL +wdiBndSWwB2Si5Yh+2urw3/asEE7a8I7Wl2eFlsZO6+8wKuHhtgEqEKH3ltBg0b7f8x U8cuVcYMwRmllTIluG1+F9gw5UsFGbgnYIQui+XCWT2QDD20fA1yDHha3N7HMehtS0nX zqXA== X-Gm-Message-State: AOAM530a5kgGZNabXQnquKTcMVnvWzjcSVLoUUt3e6Qai6RUHpq6+JZd NHa1GQrzytJMd3i2TH+89WyvS5rlCJs= X-Received: from seanjc798194.pdx.corp.google.com ([2620:15c:90:200:825e:11a1:364b:8109]) (user=seanjc job=sendgmr) by 2002:a5b:303:: with SMTP id j3mr6439721ybp.433.1626194063094; Tue, 13 Jul 2021 09:34:23 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 13 Jul 2021 09:33:04 -0700 In-Reply-To: <20210713163324.627647-1-seanjc@google.com> Message-Id: <20210713163324.627647-27-seanjc@google.com> Mime-Version: 1.0 References: <20210713163324.627647-1-seanjc@google.com> X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH v2 26/46] KVM: x86/mmu: Skip the permission_fault() check on MMIO if CR0.PG=0 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, Reiji Watanabe Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Skip the MMU permission_fault() check if paging is disabled when verifying the cached MMIO GVA is usable. The check is unnecessary and can theoretically get a false positive since the MMU doesn't zero out "permissions" or "pkru_mask" when guest paging is disabled. The obvious alternative is to zero out all the bitmasks when configuring nonpaging MMUs, but that's unnecessary work and doesn't align with the MMU's general approach of doing as little as possible for flows that are supposed to be unreachable. This is nearly a nop as the false positive is nothing more than an insignificant performance blip, and more or less limited to string MMIO when L1 is running with paging disabled. KVM doesn't cache MMIO if L2 is active with nested TDP since the "GVA" is really an L2 GPA. If L2 is active without nested TDP, then paging can't be disabled as neither VMX nor SVM allows entering the guest without paging of some form. Jumping back to L1 with paging disabled, in that case direct_map is true and so KVM will use CR2 as a GPA; the only time it doesn't is if the fault from the emulator doesn't match or emulator_can_use_gpa(), and that fails only on string MMIO and other instructions with multiple memory operands. Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index fd9026437fdd..6a11ec5d38ac 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -6557,9 +6557,9 @@ static int vcpu_mmio_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva, * there is no pkey in EPT page table for L1 guest or EPT * shadow page table for L2 guest. */ - if (vcpu_match_mmio_gva(vcpu, gva) - && !permission_fault(vcpu, vcpu->arch.walk_mmu, - vcpu->arch.mmio_access, 0, access)) { + if (vcpu_match_mmio_gva(vcpu, gva) && (!is_paging(vcpu) || + !permission_fault(vcpu, vcpu->arch.walk_mmu, + vcpu->arch.mmio_access, 0, access))) { *gpa = vcpu->arch.mmio_gfn << PAGE_SHIFT | (gva & (PAGE_SIZE - 1)); trace_vcpu_match_mmio(gva, *gpa, write, false); -- 2.32.0.93.g670b81a890-goog