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=-15.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS 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 80816C4361B for ; Fri, 18 Dec 2020 09:35:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 572E823A7D for ; Fri, 18 Dec 2020 09:35:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1733133AbgLRJfS (ORCPT ); Fri, 18 Dec 2020 04:35:18 -0500 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:20072 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726798AbgLRJfS (ORCPT ); Fri, 18 Dec 2020 04:35:18 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1608284031; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=6szuUC/E+b51oLf2cT47Z8pxpNkrhEO7MYClDvDXDJs=; b=agwN4VXf7C/c0O4KFl28bpmnl8ZX1oD1ZvFPd7IIRkK4xemvm6poxQirmx7lyleOkjfxF0 mZoSuzwHYiWvqLsDn2UAMmV3LnnBHPW3nZrgcFLz0VkFjHD8xqlLwc+Spv+QNsxI/NH7K2 UO9+cu7fAFcMofDdW8I4UMXQpMebkFY= Received: from mail-ed1-f70.google.com (mail-ed1-f70.google.com [209.85.208.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-451-yWD1nFcjM5monmy-HkvHTg-1; Fri, 18 Dec 2020 04:33:49 -0500 X-MC-Unique: yWD1nFcjM5monmy-HkvHTg-1 Received: by mail-ed1-f70.google.com with SMTP id h5so876371edq.3 for ; Fri, 18 Dec 2020 01:33:49 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:in-reply-to:references:date :message-id:mime-version; bh=6szuUC/E+b51oLf2cT47Z8pxpNkrhEO7MYClDvDXDJs=; b=cyMljeNBmjSPy+iC3XiXxhqU/3SazRXZGuCBUDG6Y0jPfUZbjJC8TyvxKG2ZHKn5la tqOxGG/gU789DCHhydWRibcGnH3eKKaD+pj1rYpQqpamBhhrr09PtYH6dK19OJbPmc4V BaU2RSAIOk9tgc9V7xZP3MR/vUvDg0gbg36Wh84cWKAit8Q0zHKFrDyYeEDpNRv8Bl6s sK//sBdb4iXoV5pM5KWRq75bnohZYuGbZTiJ5SEQnE1LYwSCmBbXZ4U1wdD/B9gJXK7q tlVqv0rWinRKI2REabNpHUsKJ1O60lck+tKmEt9PQa6AgVWj+K48B5+6SDxCNI5rLiuy 4soQ== X-Gm-Message-State: AOAM531ZvwddHHWuO5eDuD9hsaWvpjlXsQ0jNG0fu8Boh8Tq5gNQ+YOn 0nH7Rx4uE9q00rUtthKP2PrGWU8IdtxHz56CuJKEQ1l9bPHnx6ibpWMQf7HrhJP5R3lNfrIg5L6 EgbISByhtBHX1xHjt/gdKW2Fo X-Received: by 2002:aa7:c652:: with SMTP id z18mr3389744edr.60.1608284028698; Fri, 18 Dec 2020 01:33:48 -0800 (PST) X-Google-Smtp-Source: ABdhPJw/ns2TM1N8Va0SMdnRxXx42M29Uge5oKuIU7+km2Y8+qK4HKaDt+nvTWh+zrYr37Ey9luaag== X-Received: by 2002:aa7:c652:: with SMTP id z18mr3389731edr.60.1608284028520; Fri, 18 Dec 2020 01:33:48 -0800 (PST) Received: from vitty.brq.redhat.com (g-server-2.ign.cz. [91.219.240.2]) by smtp.gmail.com with ESMTPSA id d13sm10988021edx.27.2020.12.18.01.33.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 18 Dec 2020 01:33:47 -0800 (PST) From: Vitaly Kuznetsov To: Sean Christopherson , Paolo Bonzini Cc: Sean Christopherson , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Ben Gardon , Richard Herbert Subject: Re: [PATCH 4/4] KVM: x86/mmu: Optimize not-present/MMIO SPTE check in get_mmio_spte() In-Reply-To: <20201218003139.2167891-5-seanjc@google.com> References: <20201218003139.2167891-1-seanjc@google.com> <20201218003139.2167891-5-seanjc@google.com> Date: Fri, 18 Dec 2020 10:33:47 +0100 Message-ID: <87lfdvtq4k.fsf@vitty.brq.redhat.com> MIME-Version: 1.0 Content-Type: text/plain Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Sean Christopherson writes: > Check only the terminal leaf for a "!PRESENT || MMIO" SPTE when looking > for reserved bits on valid, non-MMIO SPTEs. The get_walk() helpers > terminate their walks if a not-present or MMIO SPTE is encountered, i.e. > the non-terminal SPTEs have already been verified to be regular SPTEs. > This eliminates an extra check-and-branch in a relatively hot loop. > > Signed-off-by: Sean Christopherson > --- > arch/x86/kvm/mmu/mmu.c | 20 +++++++++++++------- > 1 file changed, 13 insertions(+), 7 deletions(-) > > diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c > index 4798a4472066..769855f5f0a1 100644 > --- a/arch/x86/kvm/mmu/mmu.c > +++ b/arch/x86/kvm/mmu/mmu.c > @@ -3511,7 +3511,7 @@ static int get_walk(struct kvm_vcpu *vcpu, u64 addr, u64 *sptes, int *root_level > return leaf; > } > > -/* return true if reserved bit is detected on spte. */ > +/* return true if reserved bit(s) are detected on a valid, non-MMIO SPTE. */ > static bool get_mmio_spte(struct kvm_vcpu *vcpu, u64 addr, u64 *sptep) > { > u64 sptes[PT64_ROOT_MAX_LEVEL + 1]; > @@ -3534,11 +3534,20 @@ static bool get_mmio_spte(struct kvm_vcpu *vcpu, u64 addr, u64 *sptep) > return reserved; > } > > + *sptep = sptes[leaf]; > + > + /* > + * Skip reserved bits checks on the terminal leaf if it's not a valid > + * SPTE. Note, this also (intentionally) skips MMIO SPTEs, which, by > + * design, always have reserved bits set. The purpose of the checks is > + * to detect reserved bits on non-MMIO SPTEs. i.e. buggy SPTEs. > + */ > + if (!is_shadow_present_pte(sptes[leaf])) > + leaf++; > + > rsvd_check = &vcpu->arch.mmu->shadow_zero_check; > > - for (level = root; level >= leaf; level--) { > - if (!is_shadow_present_pte(sptes[level])) > - break; > + for (level = root; level >= leaf; level--) > /* > * Use a bitwise-OR instead of a logical-OR to aggregate the > * reserved bit and EPT's invalid memtype/XWR checks to avoid > @@ -3546,7 +3555,6 @@ static bool get_mmio_spte(struct kvm_vcpu *vcpu, u64 addr, u64 *sptep) > */ > reserved |= __is_bad_mt_xwr(rsvd_check, sptes[level]) | > __is_rsvd_bits_set(rsvd_check, sptes[level], level); > - } > > if (reserved) { > pr_err("%s: detect reserved bits on spte, addr 0x%llx, dump hierarchy:\n", > @@ -3556,8 +3564,6 @@ static bool get_mmio_spte(struct kvm_vcpu *vcpu, u64 addr, u64 *sptep) > sptes[level], level); > } > > - *sptep = sptes[leaf]; > - > return reserved; > } FWIW (as I got a bit lost in tdp-mmu code when checking that is_shadow_present_pte() is always performed) Reviewed-by: Vitaly Kuznetsov -- Vitaly