[v4,05/18] KVM: x86/mmu: Add split_shadow_page_cache pages to global count of MMU cache pages
Message ID | 20230306224127.1689967-6-vipinsh@google.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp2114613wrd; Mon, 6 Mar 2023 14:46:06 -0800 (PST) X-Google-Smtp-Source: AK7set/5QPvFq7/ED2ojqgVR0kQOjJWCVsA5exUVopDTHnB0dw4wJfr3mGxB9tHwtz8+lRkBSdPF X-Received: by 2002:aa7:c0c5:0:b0:4c1:2252:f72c with SMTP id j5-20020aa7c0c5000000b004c12252f72cmr12600187edp.27.1678142766385; Mon, 06 Mar 2023 14:46:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1678142766; cv=none; d=google.com; s=arc-20160816; b=fnfAmhXZMpYNWtbwkQ7wItmNWCUa9NAJDsOE10+hsturHlhB23q5+oCbEP9BU2dv5G Wi28Y5KlTkGRwuETGTVslLaTWlDNjY54/PH9ErcEBGDnvJwmT6n9+BSkNpzFn7RWeUaL /5PoLHvK51neNacHzGCzzALEyAIM3gZZjjWHWTKz9IxU+qMTPgI5jLF7TsptfmfdC2Tp 9whvwIYwYYIloBrVUmoR2tm4nEDfRNI2TcBYIzzNXYqUXEb/qs8Plz6pjE31a/dboa1Q Vl19wm80S6DkWbg83qPKXD5gvjICVTIQsJAdKCXYuZEUImQC1m9NUJbpjPEu5gZXUkHH Oc2A== 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:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=ntsWJChvbXfy8t9EA+eQUOa7LNpcvZXYoCa+kOc7Xxo=; b=oOhFxOqQHi/+R6f6qt5tX1nrvexoTkngQwlzT8l97n8Qc4Jbp6cVMNfjck6MackRnO mYcPtPGPOLJNk2eEA7mCxPTZcbQzzjhnYuXzFVwAXzQRkMazkcpc1R7tkf9GMPhGEzem 3ctUB+t0yLnsvjgSkveX4QB0fMQM02BE4qjbZNJySG9XcJT5GLvTgh3p5DRQzQMvIh8v PeT4t+uCd2vPNevLDE6tkRjVu/GiFOqFUx5twpFaB7q3mx4nJrPUfTtJ0FvVtPlBpDyh YTkLW+4VpWo9Jhq2Z0lozuDJpAxtwti9vIuuItrGfZblS5ItO2TfSjeRHi3tS3fOR5i4 5jgw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=QE3wGxcp; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id og49-20020a1709071df100b008babc10d9f4si1054381ejc.141.2023.03.06.14.45.42; Mon, 06 Mar 2023 14:46:06 -0800 (PST) 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=QE3wGxcp; 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 S230183AbjCFWl7 (ORCPT <rfc822;toshivichauhan@gmail.com> + 99 others); Mon, 6 Mar 2023 17:41:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43278 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230105AbjCFWlr (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Mon, 6 Mar 2023 17:41:47 -0500 Received: from mail-pj1-x104a.google.com (mail-pj1-x104a.google.com [IPv6:2607:f8b0:4864:20::104a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8DD3B7430C for <linux-kernel@vger.kernel.org>; Mon, 6 Mar 2023 14:41:43 -0800 (PST) Received: by mail-pj1-x104a.google.com with SMTP id cl18-20020a17090af69200b0023470d96ae6so127343pjb.1 for <linux-kernel@vger.kernel.org>; Mon, 06 Mar 2023 14:41:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1678142503; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=ntsWJChvbXfy8t9EA+eQUOa7LNpcvZXYoCa+kOc7Xxo=; b=QE3wGxcpDkwXukGXapdK3z5AcvymfM9fTDSInE4aMcqqqhApqI13rGs3g8WKQUaPS/ zZ6kNb4R7uz/pdmbRfaqJcYVYuRq+bQgvKsN86OU0tlFIWLibxnJpuqg0yKTzMCyuR1X pL28Sm386yDPzPua4c49qPfANPTKuZn9aWIUurbKMbZucDPpluT8Syfvw708TGu5WMO0 hjDTHUxu7yWc5s4/oRuSPQU74v9qCJL0SWCVXIoKKH6qhy3+hgUFKF39UdJnzPCFwNtA PsAmluDoVGVvbeYF4h5LW3dAFwVLGK0yDNSdG3miH//Ijl6QzEc9pgGGk9eEMrDtuikC CFZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678142503; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ntsWJChvbXfy8t9EA+eQUOa7LNpcvZXYoCa+kOc7Xxo=; b=l2jsHc6NQk6UuCnfsVi6I6SMMWzQ4ePzF/94et/4PQ7xzoXnVoJrb4Y1uV1QEte514 nRA/LYVeBF2f/VGkffi5vu/NwsmYsbH1/QvOnbBNojdHpRsUUDRZBX7qoRI2OYvDdxhm XoLg8/iXdCIsoe6coYANrZjEAwkPgPesM4zOxjZ3Pp6pMlgkSMhpomcC61zSDQuXhxUw /YuWzurcCvMTVH/j4Z/lz0xlP619A0fymciEWWs+AzSZa+j36m6jLfJpf19mL0wTx9X8 tN9C7Aqjb2iCJzgqwBgxMSXxqrD1bGl/15Cz3ScfrdJl896r8Nq1DOg1ngHmN4p7Mmsn zlfg== X-Gm-Message-State: AO0yUKWEgzJm28cby3KOrQI/o/cVOC7kqWD9aFmxb3VaDVoqAUj2UoLM fPt1UkvrkpwUirRCz/diquVXBFi5Vot1 X-Received: from vipin.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:479f]) (user=vipinsh job=sendgmr) by 2002:a63:7e11:0:b0:503:913f:77b9 with SMTP id z17-20020a637e11000000b00503913f77b9mr4352737pgc.6.1678142503145; Mon, 06 Mar 2023 14:41:43 -0800 (PST) Date: Mon, 6 Mar 2023 14:41:14 -0800 In-Reply-To: <20230306224127.1689967-1-vipinsh@google.com> Mime-Version: 1.0 References: <20230306224127.1689967-1-vipinsh@google.com> X-Mailer: git-send-email 2.40.0.rc0.216.gc4246ad0f0-goog Message-ID: <20230306224127.1689967-6-vipinsh@google.com> Subject: [Patch v4 05/18] KVM: x86/mmu: Add split_shadow_page_cache pages to global count of MMU cache pages From: Vipin Sharma <vipinsh@google.com> To: seanjc@google.com, pbonzini@redhat.com, bgardon@google.com, dmatlack@google.com Cc: jmattson@google.com, mizhang@google.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Vipin Sharma <vipinsh@google.com> 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,USER_IN_DEF_DKIM_WL autolearn=unavailable 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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1759660229382684364?= X-GMAIL-MSGID: =?utf-8?q?1759660229382684364?= |
Series |
NUMA aware page table allocation
|
|
Commit Message
Vipin Sharma
March 6, 2023, 10:41 p.m. UTC
Add pages in split_shadow_page_cache to the global counter
kvm_total_unused_cached_pages. These pages will be freed by MMU shrinker
in future commit.
Signed-off-by: Vipin Sharma <vipinsh@google.com>
---
arch/x86/kvm/mmu/mmu.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
Comments
On Mon, 6 Mar 2023 14:41:14 -0800 Vipin Sharma <vipinsh@google.com> wrote: > Add pages in split_shadow_page_cache to the global counter > kvm_total_unused_cached_pages. These pages will be freed by MMU shrinker > in future commit. > > Signed-off-by: Vipin Sharma <vipinsh@google.com> > --- > arch/x86/kvm/mmu/mmu.c | 7 +++++-- > 1 file changed, 5 insertions(+), 2 deletions(-) > > diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c > index df8dcb7e5de7..0ebb8a2eaf47 100644 > --- a/arch/x86/kvm/mmu/mmu.c > +++ b/arch/x86/kvm/mmu/mmu.c > @@ -6149,7 +6149,9 @@ static void mmu_free_vm_memory_caches(struct kvm *kvm) > { > kvm_mmu_free_memory_cache(&kvm->arch.split_desc_cache); > kvm_mmu_free_memory_cache(&kvm->arch.split_page_header_cache); > - kvm_mmu_free_memory_cache(&kvm->arch.split_shadow_page_cache); > + mutex_lock(&kvm->slots_lock); > + mmu_free_sp_memory_cache(&kvm->arch.split_shadow_page_cache); > + mutex_unlock(&kvm->slots_lock); Taking the lock of the calling path in the layer of cache topping/free layer seems off. My vote goes to have a lock for each cache and take the lock of the cache when topping/free the cache. It is more self-contained and architecturally nice. > } > > void kvm_mmu_uninit_vm(struct kvm *kvm) > @@ -6303,7 +6305,7 @@ static int topup_split_caches(struct kvm *kvm) > if (r) > return r; > > - return kvm_mmu_topup_memory_cache(&kvm->arch.split_shadow_page_cache, 1); > + return mmu_topup_sp_memory_cache(&kvm->arch.split_shadow_page_cache, 1); > } > > static struct kvm_mmu_page *shadow_mmu_get_sp_for_split(struct kvm *kvm, u64 *huge_sptep) > @@ -6328,6 +6330,7 @@ static struct kvm_mmu_page *shadow_mmu_get_sp_for_split(struct kvm *kvm, u64 *hu > /* Direct SPs do not require a shadowed_info_cache. */ > caches.page_header_cache = &kvm->arch.split_page_header_cache; > caches.shadow_page_cache = &kvm->arch.split_shadow_page_cache; > + caches.count_shadow_page_allocation = true; > > /* Safe to pass NULL for vCPU since requesting a direct SP. */ > return __kvm_mmu_get_shadow_page(kvm, NULL, &caches, gfn, role);
On Thu, Mar 9, 2023 at 7:58 AM Zhi Wang <zhi.wang.linux@gmail.com> wrote: > > On Mon, 6 Mar 2023 14:41:14 -0800 > Vipin Sharma <vipinsh@google.com> wrote: > > > Add pages in split_shadow_page_cache to the global counter > > kvm_total_unused_cached_pages. These pages will be freed by MMU shrinker > > in future commit. > > > > Signed-off-by: Vipin Sharma <vipinsh@google.com> > > --- > > arch/x86/kvm/mmu/mmu.c | 7 +++++-- > > 1 file changed, 5 insertions(+), 2 deletions(-) > > > > diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c > > index df8dcb7e5de7..0ebb8a2eaf47 100644 > > --- a/arch/x86/kvm/mmu/mmu.c > > +++ b/arch/x86/kvm/mmu/mmu.c > > @@ -6149,7 +6149,9 @@ static void mmu_free_vm_memory_caches(struct kvm *kvm) > > { > > kvm_mmu_free_memory_cache(&kvm->arch.split_desc_cache); > > kvm_mmu_free_memory_cache(&kvm->arch.split_page_header_cache); > > - kvm_mmu_free_memory_cache(&kvm->arch.split_shadow_page_cache); > > + mutex_lock(&kvm->slots_lock); > > + mmu_free_sp_memory_cache(&kvm->arch.split_shadow_page_cache); > > + mutex_unlock(&kvm->slots_lock); > > Taking the lock of the calling path in the layer of cache topping/free layer > seems off. > > My vote goes to have a lock for each cache and take the lock of the cache when > topping/free the cache. It is more self-contained and architecturally nice. > Yeah, this can be one way. However, in future patches when I am adding per NUMA node cache, it will add up a lot of locks for the same code path before a topup. In split huge page case we know what NUMA node we need to allocate from so we can fine tune which lock to take but in fault path code we don't know what NUMA node the page will be coming from so we need to topup all of the NUMA caches. Having a single lock simplifies code a little bit. I agree with you on being more self-contained. I will wait for others to also chime in on this and go from there.
On Thu, Mar 09, 2023 at 11:59:00AM -0800, Vipin Sharma wrote: > On Thu, Mar 9, 2023 at 7:58 AM Zhi Wang <zhi.wang.linux@gmail.com> wrote: > > > > On Mon, 6 Mar 2023 14:41:14 -0800 > > Vipin Sharma <vipinsh@google.com> wrote: > > > > > Add pages in split_shadow_page_cache to the global counter > > > kvm_total_unused_cached_pages. These pages will be freed by MMU shrinker > > > in future commit. > > > > > > Signed-off-by: Vipin Sharma <vipinsh@google.com> > > > --- > > > arch/x86/kvm/mmu/mmu.c | 7 +++++-- > > > 1 file changed, 5 insertions(+), 2 deletions(-) > > > > > > diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c > > > index df8dcb7e5de7..0ebb8a2eaf47 100644 > > > --- a/arch/x86/kvm/mmu/mmu.c > > > +++ b/arch/x86/kvm/mmu/mmu.c > > > @@ -6149,7 +6149,9 @@ static void mmu_free_vm_memory_caches(struct kvm *kvm) > > > { > > > kvm_mmu_free_memory_cache(&kvm->arch.split_desc_cache); > > > kvm_mmu_free_memory_cache(&kvm->arch.split_page_header_cache); > > > - kvm_mmu_free_memory_cache(&kvm->arch.split_shadow_page_cache); > > > + mutex_lock(&kvm->slots_lock); > > > + mmu_free_sp_memory_cache(&kvm->arch.split_shadow_page_cache); > > > + mutex_unlock(&kvm->slots_lock); > > > > Taking the lock of the calling path in the layer of cache topping/free layer > > seems off. > > > > My vote goes to have a lock for each cache and take the lock of the cache when > > topping/free the cache. It is more self-contained and architecturally nice. > > > > Yeah, this can be one way. However, in future patches when I am adding > per NUMA node cache, it will add up a lot of locks for the same code > path before a topup. In split huge page case we know what NUMA node we > need to allocate from so we can fine tune which lock to take but in > fault path code we don't know what NUMA node the page will be coming > from so we need to topup all of the NUMA caches. Having a single lock > simplifies code a little bit. > > I agree with you on being more self-contained. I will wait for others > to also chime in on this and go from there. As a general rule, please only added locking when it's needed. Adding the lock in this commit is just confusing. But that aside, I don't think acquiring the slots lock is even needed in this commit. mmu_free_vm_memory_caches() is never called while the the VM is on vm_list. i.e. This can never race with the shrinker. If you want to be paranoid you can add a WARN to ensure that stays true going forward: /* ... comment ... */ WARN_ON_ONCE(!list_empty(&kvm->vm_list));
On Thu, Mar 9, 2023 at 4:05 PM David Matlack <dmatlack@google.com> wrote: > > On Thu, Mar 09, 2023 at 11:59:00AM -0800, Vipin Sharma wrote: > > On Thu, Mar 9, 2023 at 7:58 AM Zhi Wang <zhi.wang.linux@gmail.com> wrote: > > > > > > On Mon, 6 Mar 2023 14:41:14 -0800 > > > Vipin Sharma <vipinsh@google.com> wrote: > > > > > > > Add pages in split_shadow_page_cache to the global counter > > > > kvm_total_unused_cached_pages. These pages will be freed by MMU shrinker > > > > in future commit. > > > > > > > > Signed-off-by: Vipin Sharma <vipinsh@google.com> > > > > --- > > > > arch/x86/kvm/mmu/mmu.c | 7 +++++-- > > > > 1 file changed, 5 insertions(+), 2 deletions(-) > > > > > > > > diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c > > > > index df8dcb7e5de7..0ebb8a2eaf47 100644 > > > > --- a/arch/x86/kvm/mmu/mmu.c > > > > +++ b/arch/x86/kvm/mmu/mmu.c > > > > @@ -6149,7 +6149,9 @@ static void mmu_free_vm_memory_caches(struct kvm *kvm) > > > > { > > > > kvm_mmu_free_memory_cache(&kvm->arch.split_desc_cache); > > > > kvm_mmu_free_memory_cache(&kvm->arch.split_page_header_cache); > > > > - kvm_mmu_free_memory_cache(&kvm->arch.split_shadow_page_cache); > > > > + mutex_lock(&kvm->slots_lock); > > > > + mmu_free_sp_memory_cache(&kvm->arch.split_shadow_page_cache); > > > > + mutex_unlock(&kvm->slots_lock); > > > > > > Taking the lock of the calling path in the layer of cache topping/free layer > > > seems off. > > > > > > My vote goes to have a lock for each cache and take the lock of the cache when > > > topping/free the cache. It is more self-contained and architecturally nice. > > > > > > > Yeah, this can be one way. However, in future patches when I am adding > > per NUMA node cache, it will add up a lot of locks for the same code > > path before a topup. In split huge page case we know what NUMA node we > > need to allocate from so we can fine tune which lock to take but in > > fault path code we don't know what NUMA node the page will be coming > > from so we need to topup all of the NUMA caches. Having a single lock > > simplifies code a little bit. > > > > I agree with you on being more self-contained. I will wait for others > > to also chime in on this and go from there. > > As a general rule, please only added locking when it's needed. Adding > the lock in this commit is just confusing. > > But that aside, I don't think acquiring the slots lock is even needed in > this commit. Correction: even needed in the *next* commit > mmu_free_vm_memory_caches() is never called while the the > VM is on vm_list. i.e. This can never race with the shrinker. > > If you want to be paranoid you can add a WARN to ensure that stays true > going forward: > > /* ... comment ... */ > WARN_ON_ONCE(!list_empty(&kvm->vm_list));
diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index df8dcb7e5de7..0ebb8a2eaf47 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -6149,7 +6149,9 @@ static void mmu_free_vm_memory_caches(struct kvm *kvm) { kvm_mmu_free_memory_cache(&kvm->arch.split_desc_cache); kvm_mmu_free_memory_cache(&kvm->arch.split_page_header_cache); - kvm_mmu_free_memory_cache(&kvm->arch.split_shadow_page_cache); + mutex_lock(&kvm->slots_lock); + mmu_free_sp_memory_cache(&kvm->arch.split_shadow_page_cache); + mutex_unlock(&kvm->slots_lock); } void kvm_mmu_uninit_vm(struct kvm *kvm) @@ -6303,7 +6305,7 @@ static int topup_split_caches(struct kvm *kvm) if (r) return r; - return kvm_mmu_topup_memory_cache(&kvm->arch.split_shadow_page_cache, 1); + return mmu_topup_sp_memory_cache(&kvm->arch.split_shadow_page_cache, 1); } static struct kvm_mmu_page *shadow_mmu_get_sp_for_split(struct kvm *kvm, u64 *huge_sptep) @@ -6328,6 +6330,7 @@ static struct kvm_mmu_page *shadow_mmu_get_sp_for_split(struct kvm *kvm, u64 *hu /* Direct SPs do not require a shadowed_info_cache. */ caches.page_header_cache = &kvm->arch.split_page_header_cache; caches.shadow_page_cache = &kvm->arch.split_shadow_page_cache; + caches.count_shadow_page_allocation = true; /* Safe to pass NULL for vCPU since requesting a direct SP. */ return __kvm_mmu_get_shadow_page(kvm, NULL, &caches, gfn, role);