From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 61AEFC07E95 for ; Tue, 13 Jul 2021 16:35:03 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1352D61003 for ; Tue, 13 Jul 2021 16:35:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235536AbhGMQhv (ORCPT ); Tue, 13 Jul 2021 12:37:51 -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-Google-Smtp-Source: ABdhPJyBu4WEipwbM+p875QlkcqwEMGe7LqGyGF0Q+kW0j9wqkKUWt9cLevGQ/uZA/fwiAZ3QM+IQGUEZ0Y= 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