Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp627641iog; Fri, 24 Jun 2022 10:29:56 -0700 (PDT) X-Google-Smtp-Source: AGRyM1siCnYFkcdhNI8wr6KZ0ArBjPUt6RzXM5Str+X5p37NUIRLctRsjWV3yBiB+cm6hP8xKWEB X-Received: by 2002:a17:907:c002:b0:722:eeac:c8ee with SMTP id ss2-20020a170907c00200b00722eeacc8eemr111475ejc.88.1656091795683; Fri, 24 Jun 2022 10:29:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656091795; cv=none; d=google.com; s=arc-20160816; b=rGERQtdtf6PY8fWCPpEOIhShUIrflwqkEZCh5X+JokQNQySt53Qy5Auz+LwmJikRHR FwgoPw18n7nkGPkTYE3cSlyV4QEh1H5R5nGaXIfY+ovkPlVdFjZc/QkSsdU2bIBhb8ZM FQX3BOzAuXgCD9zFJgRcyOtD/K+ThPnR82VV3oTNNqRabLEe8/1Ar8lv+svIFNmVuR9a w9KFUpvmBmT1KQut2t3pYjKBbZ/np9r2pzIOWriX7INH2qT7xmQHeaCyyvQeXDcP0f+7 qwemYK2u40Mj9zMvgywh8qQQSC/BSCzUN1E/FC8eEO4ukiqWRF8tYtEvjuY0OlptM7+1 z+Iw== 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=M0UiwptlSyW8L+ANHt+ZpR61B6xI8IZ3E4fBNYs6MYg=; b=liO0YpvpNQwuyQ5fyW4+zkLngHh3vAm7jno5K9aY1Ji2aQknGvh4XW6z9JbTUgC+xj GpKHJw6aSgTfPT6T4rzWmV7UoIgMWiAr5l8NGpTjOZ5n3/nnXkx/gHd+PlCP0QG4yMWb 1fRZW7bDUBDkW4xElv8cjGcRicJR122Gg1Nd2dVJiKqvxfAuMJA0E6/Jbo3C6XhJP+It vO/GVA6mkExcDLHt+HiZdTB8YDuVxpntGxecl+X07vtMdCSf8io7vQD9ovXGVFh/kDJE lSC6j+jGaRQ1iDaZdmV04LzRY6eLih8ys3MQf0xadAcBNMmcgLpVvgNnUvAiCUthFF/U fMjA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=XrRDk09d; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ne13-20020a1709077b8d00b007262c1c2895si2945113ejc.913.2022.06.24.10.29.29; Fri, 24 Jun 2022 10:29:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=XrRDk09d; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S230388AbiFXRSa (ORCPT + 99 others); Fri, 24 Jun 2022 13:18:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38572 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232066AbiFXRSR (ORCPT ); Fri, 24 Jun 2022 13:18:17 -0400 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 900022DA99 for ; Fri, 24 Jun 2022 10:18:16 -0700 (PDT) Received: by mail-pl1-x64a.google.com with SMTP id q8-20020a17090311c800b0016a125c933fso1574969plh.4 for ; Fri, 24 Jun 2022 10:18:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=reply-to:date:in-reply-to:message-id:mime-version:references :subject:from:to:cc; bh=M0UiwptlSyW8L+ANHt+ZpR61B6xI8IZ3E4fBNYs6MYg=; b=XrRDk09dnK9G28hf25NsPO2C2O+/LCbUKVT9HMDoIVz3XYOG2M5T+Ff+QUGBKbWeQ+ lzl8ylTlJL2SO4Yb+KkHcwOmjaU7KyMkKX+2mkEGnOnKcaBtAU4laopLRua2YlgYbzni CrgEpoEvRdSlzEOrR4SjYkbLiAQMILSTc20i2WoG3Yd8n1Qu1T7g4zjiqTZXApodAnQA GMDSui5nHkJYMdalw4RAeTDx1Ch3xlqDHebtZvpC1DT+9ynLlvlwIcUQRPI2zj65BTlE 69wCilA/Soh7oQqcHPhvrYxTf0elWBUfQLoDoIaSeasZTWBWk0iNP67J/TBttFEqwDWu a/yw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:reply-to:date:in-reply-to:message-id :mime-version:references:subject:from:to:cc; bh=M0UiwptlSyW8L+ANHt+ZpR61B6xI8IZ3E4fBNYs6MYg=; b=Y2EJ+cbjToCOoVmu7gN++tIS8K5mjy8zAv/StV3rZU0leLeZQjyquQ4+Bz1k4lkXMf iz1FCWyflNl6T3J6LxnlHvTO79doN7fuxoxmWW3Xjsc71py3IeIaIu7KXZ8hBKtB1U7i +jiyyR0lPhe9Q6AjQ9Yr6UfkFc6cUd9TOsqwv1gV2ZT+vRTGW0VJ4S/fejXRSwZTz82U nzJUy5lyTF6mBwXbPCqf7EdJluq0ft9ie+iSTwrGBWcxhy/e/Zwrsafvg+1Y7xb/oJ1C ywtdt1i61OX1pbw2wwqW3Goe9xnk7XIg0v8CEhYfADEyhQerWMBa2LktMu1O+fP9di+v zQpg== X-Gm-Message-State: AJIora/YnhaA8DS+pMjvtX5pDsBrYeeJifnHU64dx+220rivpBlfpC7x UGXdNiVBlojx/f4QVWIkCZLuaa8i+0k= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:c94b:b0:16a:3f98:84fd with SMTP id i11-20020a170902c94b00b0016a3f9884fdmr120863pla.70.1656091095691; Fri, 24 Jun 2022 10:18:15 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 24 Jun 2022 17:18:08 +0000 In-Reply-To: <20220624171808.2845941-1-seanjc@google.com> Message-Id: <20220624171808.2845941-4-seanjc@google.com> Mime-Version: 1.0 References: <20220624171808.2845941-1-seanjc@google.com> X-Mailer: git-send-email 2.37.0.rc0.161.g10f37bed90-goog Subject: [PATCH 3/3] KVM: x86/mmu: Buffer nested MMU split_desc_cache only by default capacity 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, David Matlack Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Buffer split_desc_cache, the cache used to allcoate rmap list entries, only by the default cache capacity (currently 40), not by doubling the minimum (513). Aliasing L2 GPAs to L1 GPAs is uncommon, thus eager page splitting is unlikely to need 500+ entries. And because each object is a non-trivial 128 bytes (see struct pte_list_desc), those extra ~500 entries means KVM is in all likelihood wasting ~64kb of memory per VM. Link: https://lore.kernel.org/all/YrTDcrsn0%2F+alpzf@google.com Cc: David Matlack Signed-off-by: Sean Christopherson --- arch/x86/kvm/mmu/mmu.c | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index e2213eeadebc..069ddf874af1 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -6125,17 +6125,25 @@ static bool need_topup_split_caches_or_resched(struct kvm *kvm) static int topup_split_caches(struct kvm *kvm) { - int r; - - lockdep_assert_held(&kvm->slots_lock); - /* - * Setting capacity == min would cause KVM to drop mmu_lock even if - * just one object was consumed from the cache, so make capacity - * larger than min. + * Allocating rmap list entries when splitting huge pages for nested + * MMUs is uncommon as KVM needs to allocate if and only if there is + * more than one rmap entry for a gfn, i.e. requires an L1 gfn to be + * aliased by multiple L2 gfns. Aliasing gfns when using TDP is very + * atypical for VMMs; a few gfns are often aliased during boot, e.g. + * when remapping firmware, but aliasing rarely occurs post-boot). If + * there is only one rmap entry, rmap->val points directly at that one + * entry and doesn't need to allocate a list. Buffer the cache by the + * default capacity so that KVM doesn't have to topup the cache if it + * encounters an aliased gfn or two. */ - r = __kvm_mmu_topup_memory_cache(&kvm->arch.split_desc_cache, - 2 * SPLIT_DESC_CACHE_MIN_NR_OBJECTS, + const int capacity = SPLIT_DESC_CACHE_MIN_NR_OBJECTS + + KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE; + int r; + + lockdep_assert_held(&kvm->slots_lock); + + r = __kvm_mmu_topup_memory_cache(&kvm->arch.split_desc_cache, capacity, SPLIT_DESC_CACHE_MIN_NR_OBJECTS); if (r) return r; -- 2.37.0.rc0.161.g10f37bed90-goog