Received: by 2002:a25:23cc:0:0:0:0:0 with SMTP id j195csp272153ybj; Fri, 8 May 2020 11:26:24 -0700 (PDT) X-Google-Smtp-Source: APiQypK2zrOZymxRkf9sGrnCkvEx4ppR+Qg8TT5gum1phj1M54B0QWvjLxA1AiiclsD9lEeiyXc+ X-Received: by 2002:a17:906:68cf:: with SMTP id y15mr2899670ejr.260.1588962384282; Fri, 08 May 2020 11:26:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588962384; cv=none; d=google.com; s=arc-20160816; b=igp1JljDmTxDMZsXMyZfX9vNmnocEjp4Kq7LWX8ZaEn0FuGeQjez/a5yV8731iTpjx CiFoGyklhJj5SJp7N8kWMqh8aOoLsIaeRuB+NOKrrrfLEuIc5wyWrIV9AEGCkHblJP6s A8Jf5M+30lURbXnKJbqeIeQ9zu50JowyUNeNpp5CFnUGNU/R4Wpz8ffgFA89QvLn8N2q wPC0GOzzGVuEl9BSRPIV+FipfY7ECPGoQkHqAuz9FrsdhmvHuRmEWAOM/q7LtV4sUyKN 7lEs2J/R4/oVMavQiGc/ghnGuG7y3xm9akElUMp/OHpC6BKuFcj7w56QnoXvLt+rgR7m CN1A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:mime-version :message-id:date:dkim-signature; bh=1V7q5o/ItajItDx2fBMJH7Cm/96EukZlRI5pzOV5Nh0=; b=0v2OPiuU0hEownOo++MqYysLb14csjRaY3UaCv6q1dTVQGKeequbyTCilptp5QH0+5 lEju/TCpJiSepg2FZtP/AaEGhOK4pTfQX0iGCRLEL+vXN2mu1rUp1R5IT4uypABFyb5L 8+rQfRfgi1aJG34VMibasjiOUQpCdZAp5V9yPv+b+MFUxVu82fhRjaFKwH/Cdm+taFsN FWACyMCJ+dtL8FnNzdFc00aESJ/WlSjHRCftAR6qOIz9g0f0CPZZL0CitBIyIRSRzcYL YA706wGhNLQ/iLD56mKh7P/LRsIzTzzIlVOg2bnw12KsH7wK00OiSmd9kP+/L/B176Se 4RJQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=EsInb96P; 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 h5si1503411edn.236.2020.05.08.11.26.01; Fri, 08 May 2020 11:26:24 -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=EsInb96P; 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 S1727122AbgEHSYa (ORCPT + 99 others); Fri, 8 May 2020 14:24:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56470 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1726807AbgEHSY3 (ORCPT ); Fri, 8 May 2020 14:24:29 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B902CC05BD09 for ; Fri, 8 May 2020 11:24:29 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id d206so3082118ybh.7 for ; Fri, 08 May 2020 11:24:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc; bh=1V7q5o/ItajItDx2fBMJH7Cm/96EukZlRI5pzOV5Nh0=; b=EsInb96PWReVQg3NpHEVUBr/5zp2mwXiQdj8C/5KnpjyUPjK/GVTmMZr/3tf2YpiF6 nCUDpMXRvZu2cZRjHsoMqjonTZQ6QPmtElreMhkQ+SBFvqcYTqY9XriUhCNSLKGAeAWV cRFj6xoNF7XP0I1iTYbFALuMgSrB87IIx5RnbfwFdV2FnCtMdLl2zOSRLqG1mqJb7+7e O9ZBP/LlJBy2ODeomKEFpGbSwe+rxGoqGykCcIGCveC+QC8Am4Ln9Ea+DRZQmQ3qzb94 si2Ehl6+ry3xT78xc0UxSbXVamCqGl0b8VuHUWFPvZOA9mly58BrZilrUn0lh6dFYwxn Ef1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=1V7q5o/ItajItDx2fBMJH7Cm/96EukZlRI5pzOV5Nh0=; b=DAFIe5Jl2+j0HaD1Ehj47k73AR+/bTd53wn6K5q3bmBXspF9zNAZ96AFYt52D6aOuQ B3RvaMbTHDzIwt3Sxq4UgoEwKIa1EBr8KSxGjPlgKCRWv0q5rdZpL5G9kzNLXb3hsJu8 xaWUpkwH3bzKo8wvBu/9m8OZGz3eLryBS153eVAQCsCI1zgHOP8hkFsM0RMVY65Tcp6X aZWjQS6hdFmfiFl5VI7paN/i6zUjahA36E2v2i+4VTk1AeUvQfoRbmQFnYl7tzW5XrOG ifoma6+R6//CB9tJaDQE82qur88NfLRPolcBAJENeP6U65+7KIpsX40Rr3sPRA4cBOKX m0Mw== X-Gm-Message-State: AGi0PuaNyIVNDesfvuB2MpdvS7w22Olvt6u7O7Gxoz45M1OvWK/Xl7Fu MngwrFGLll2g+RbRP9okcSSoXNeWMcZ7ZA== X-Received: by 2002:a25:d757:: with SMTP id o84mr6996891ybg.144.1588962268930; Fri, 08 May 2020 11:24:28 -0700 (PDT) Date: Fri, 8 May 2020 11:24:25 -0700 Message-Id: <20200508182425.69249-1-jcargill@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.26.2.645.ge9eca65c58-goog Subject: [PATCH] kvm: x86 mmu: avoid mmu_page_hash lookup for direct_map-only VM From: Jon Cargille To: Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , x86@kernel.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Peter Feiner , Jon Cargille Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Peter Feiner Optimization for avoiding lookups in mmu_page_hash. When there's a single direct root, a shadow page has at most one parent SPTE (non-root SPs have exactly one; the root has none). Thus, if an SPTE is non-present, it can be linked to a newly allocated SP without first checking if the SP already exists. This optimization has proven significant in batch large SP shattering where the hash lookup accounted for 95% of the overhead. Signed-off-by: Peter Feiner Signed-off-by: Jon Cargille Reviewed-by: Jim Mattson --- arch/x86/include/asm/kvm_host.h | 13 ++++++++ arch/x86/kvm/mmu/mmu.c | 55 +++++++++++++++++++-------------- 2 files changed, 45 insertions(+), 23 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index a239a297be33..9b70d764b626 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -913,6 +913,19 @@ struct kvm_arch { struct kvm_page_track_notifier_node mmu_sp_tracker; struct kvm_page_track_notifier_head track_notifier_head; + /* + * Optimization for avoiding lookups in mmu_page_hash. When there's a + * single direct root, a shadow page has at most one parent SPTE + * (non-root SPs have exactly one; the root has none). Thus, if an SPTE + * is non-present, it can be linked to a newly allocated SP without + * first checking if the SP already exists. + * + * False initially because there are no indirect roots. + * + * Guarded by mmu_lock. + */ + bool shadow_page_may_have_multiple_parents; + struct list_head assigned_dev_head; struct iommu_domain *iommu_domain; bool iommu_noncoherent; diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index e618472c572b..d94552b0ed77 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -2499,35 +2499,40 @@ static struct kvm_mmu_page *kvm_mmu_get_page(struct kvm_vcpu *vcpu, quadrant &= (1 << ((PT32_PT_BITS - PT64_PT_BITS) * level)) - 1; role.quadrant = quadrant; } - for_each_valid_sp(vcpu->kvm, sp, gfn) { - if (sp->gfn != gfn) { - collisions++; - continue; - } - if (!need_sync && sp->unsync) - need_sync = true; + if (vcpu->kvm->arch.shadow_page_may_have_multiple_parents || + level == vcpu->arch.mmu->root_level) { + for_each_valid_sp(vcpu->kvm, sp, gfn) { + if (sp->gfn != gfn) { + collisions++; + continue; + } - if (sp->role.word != role.word) - continue; + if (!need_sync && sp->unsync) + need_sync = true; - if (sp->unsync) { - /* The page is good, but __kvm_sync_page might still end - * up zapping it. If so, break in order to rebuild it. - */ - if (!__kvm_sync_page(vcpu, sp, &invalid_list)) - break; + if (sp->role.word != role.word) + continue; - WARN_ON(!list_empty(&invalid_list)); - kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu); - } + if (sp->unsync) { + /* The page is good, but __kvm_sync_page might + * still end up zapping it. If so, break in + * order to rebuild it. + */ + if (!__kvm_sync_page(vcpu, sp, &invalid_list)) + break; - if (sp->unsync_children) - kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu); + WARN_ON(!list_empty(&invalid_list)); + kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu); + } - __clear_sp_write_flooding_count(sp); - trace_kvm_mmu_get_page(sp, false); - goto out; + if (sp->unsync_children) + kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu); + + __clear_sp_write_flooding_count(sp); + trace_kvm_mmu_get_page(sp, false); + goto out; + } } ++vcpu->kvm->stat.mmu_cache_miss; @@ -3735,6 +3740,10 @@ static int mmu_alloc_shadow_roots(struct kvm_vcpu *vcpu) gfn_t root_gfn, root_pgd; int i; + spin_lock(&vcpu->kvm->mmu_lock); + vcpu->kvm->arch.shadow_page_may_have_multiple_parents = true; + spin_unlock(&vcpu->kvm->mmu_lock); + root_pgd = vcpu->arch.mmu->get_guest_pgd(vcpu); root_gfn = root_pgd >> PAGE_SHIFT; -- 2.26.2.303.gf8c07b1a785-goog