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 Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 54272C43334 for ; Fri, 24 Jun 2022 21:31:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231467AbiFXVbG (ORCPT ); Fri, 24 Jun 2022 17:31:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58060 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231508AbiFXVa6 (ORCPT ); Fri, 24 Jun 2022 17:30:58 -0400 Received: from mail-pl1-x649.google.com (mail-pl1-x649.google.com [IPv6:2607:f8b0:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0139A81702 for ; Fri, 24 Jun 2022 14:30:58 -0700 (PDT) Received: by mail-pl1-x649.google.com with SMTP id u13-20020a170902e5cd00b0016a53274671so1843212plf.15 for ; Fri, 24 Jun 2022 14:30:57 -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=FBTaEZTEg5GSVpewRBYKdCI+FLeByH1Vcwz0M7m4O+0=; b=SWbZ73EHxyjh8o5NT5+YNokKeaatVJ46ekafm3q2KaueYufnteAQ+nNnv0g8VuQmzQ dHdFDYemFGBvCT+4wkiipM0eUtXvZUQYQ/2yu1xl/ziQv8ITCGWREJevCH/qZKYhaPqx JGlwkxDPC2I9qUSoxy/vzMyPDYGWqWUePXYQNHNloS7yfJiyjPmc5xjvxftfm3WGFA4x Ws8ly60Yx3TPDrBH5Iy6hhgA1rHtTn7701i3Tn/rhUObgTU8LfBPMX/VmGPMyk4xFrsf cd+bqJ0ZwVcILvOgSExuBkQ2KKX58+0nCQINRuuQjWpSwdslQIsAuu7Bxm1i+TquTdws OeYg== 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=FBTaEZTEg5GSVpewRBYKdCI+FLeByH1Vcwz0M7m4O+0=; b=htIXv32aeleqn0UBWhSuRzLvsCD6nk9u4NvxUYNYsr1/onBL0QyAvil2D3rFFRIufZ sxaAI7dUp8PO0f2sus/9W6/Ly4V7WFCfUjUjvDVyMycxO0FSxdMxSEQWKfWyl0Fbareu sLFV1SrgEtRwF8ZKql4NHP+k71ETNEeGoXEVjVtvw7UD9u2E5yYu8x/xvk5aEgGfelFh Am/pyrTcNgJ3ndAV4ih0H1OPC+rcoH9OXA73Bl2ICDRxc03PpSXoyBa4Xp8FXYUVhGu5 y/bKRlS218I9RPCxdpDd8GM1Ny1po6WF8g6fk6SOpL0EPY6dN6g/flpozqY3Y/K30PAx CjVA== X-Gm-Message-State: AJIora/n4Re7a7QZpxn2YMzp6kg9rzDhgiaOv+6mvmh4CkGs+pE/8M12 mhx6iAb7XL+dn2Pc/zBE9O32C1d59xY= X-Google-Smtp-Source: AGRyM1sugnfEsmqVDhbHA4jyTxXo0iuQG8dGD8pRvqXU8Z2EFcOSud0AzOY1pj5YaZMeBTqxatGprCqYfyI= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:d2c4:b0:16a:5c48:8312 with SMTP id n4-20020a170902d2c400b0016a5c488312mr1169374plc.45.1656106257561; Fri, 24 Jun 2022 14:30:57 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 24 Jun 2022 21:30:39 +0000 In-Reply-To: <20220624213039.2872507-1-seanjc@google.com> Message-Id: <20220624213039.2872507-5-seanjc@google.com> Mime-Version: 1.0 References: <20220624213039.2872507-1-seanjc@google.com> X-Mailer: git-send-email 2.37.0.rc0.161.g10f37bed90-goog Subject: [PATCH v2 4/4] 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" 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 (currently) a non-trivial 128 bytes (see struct pte_list_desc), those extra ~500 entries means KVM is in all likelihood wasting ~64kb of memory. Link: https://lore.kernel.org/all/YrTDcrsn0%2F+alpzf@google.com Reviewed-by: David Matlack Signed-off-by: Sean Christopherson --- arch/x86/kvm/mmu/mmu.c | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index eae5c801e442..52664c3caaab 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -6123,17 +6123,26 @@ 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 use a list 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 and/or from multiple nested roots with + * different roles. Aliasing gfns when using TDP is atypical for VMMs; + * a few gfns are often aliased during boot, e.g. when remapping BIOS, + * but aliasing rarely occurs post-boot or for many gfns. 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 drop mmu_lock to topup if KVM + * 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