Message ID | 20230526234435.662652-9-yuzhao@google.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp27059vqr; Fri, 26 May 2023 16:46:31 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5YVTT+oT3uAKI2tLiB+x5ez00FWVTQ7u4Av6ffQ+F5advuBA3NJYcWArYl6TZr2SObNuQq X-Received: by 2002:a05:6a20:a40a:b0:10c:80a:480c with SMTP id z10-20020a056a20a40a00b0010c080a480cmr702839pzk.41.1685144791183; Fri, 26 May 2023 16:46:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685144791; cv=none; d=google.com; s=arc-20160816; b=bjpPH7fWTD942qmAlnFw5NMn16YQY3IWNzMPJ+oWACtTOdzI9Q8JVptQEaz0L1xVQU o1fjEM5XOcb/8ABF+lZGp/kQHJXMPLEXAiAYIQ59pkEdvVcPGSiqns1cR2qrahhcoWvc Ik/WQQq6FqkxRCT4Q8SMAOA+XREicZGPuKBzFhABq4SAxUPa4n5bW3kUlsOue4ac+Eob QTvyrjbmocdly3/UbXljOF9W5bczQbcyhDlCHLqFcRBsWWK/Yrjw2VNRkCTtUGPDAZjn SnkOr2B5F7pcgW4tKCZ9O7zUs2oqdRVwn0SkNsAtNYNlwORFFvza2u5H2rAd9A2PPnAW rvFQ== 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:dkim-signature; bh=7JLLUnbPXAaoH9i/N14KC8FRF4yfK0mZvbZkcTBOq5Y=; b=DPMv8UagG7RCFlquDHyENgS3kQ8rh8EGIwgZPF0nRA85CU+hGsz5ORonU4UKhfCe8o 7CcrCbe7EkIk4AJudrMDTp8+QgUzwaQpCYLDui970KX1xgsKA5RqXEwLoHwCf1VxM51O NVia/yQBHUA2u4Y7tmtLgfSulXcMyK3OasY+V8IcwSqQ6/cLkDT3UTrz2JqW50H2UJX/ 8gCKMyM5w8iP2Q/xcsDZgxln4h7bQmnEuUM41fecfSG4HCv3AZTrTGfzQgyPpKB/dxDE GHVmMWd1eLBXacH+l5iygrND7zrxedPaFfKz3gw7AihXq1ALBK63yb4yximt7I+9cI1Z Yh2w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b="Ne/YRQ/s"; 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 v128-20020a626186000000b0064f74cf9df3si2910859pfb.110.2023.05.26.16.46.16; Fri, 26 May 2023 16:46:31 -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=20221208 header.b="Ne/YRQ/s"; 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 S243342AbjEZXpY (ORCPT <rfc822;zhanglyra.2023@gmail.com> + 99 others); Fri, 26 May 2023 19:45:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59012 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238032AbjEZXo5 (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Fri, 26 May 2023 19:44:57 -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 B0738E42 for <linux-kernel@vger.kernel.org>; Fri, 26 May 2023 16:44:53 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-bad475920a8so633623276.1 for <linux-kernel@vger.kernel.org>; Fri, 26 May 2023 16:44:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1685144693; x=1687736693; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=7JLLUnbPXAaoH9i/N14KC8FRF4yfK0mZvbZkcTBOq5Y=; b=Ne/YRQ/svpJFGXhXpgHmwxc0D9cLMxIoz8hghy2YEExILLlaUzGTTLTu8b32WLIGJt Pl2W5ENMW5Sa2Kwrgh0yVLYa4j/IXKl/wxvEFqusCYfl9i2LCglCwg4qFOwnu/SiZlAZ epK5zgrczZWcQF0/2oLgZ8laj6CsPXbNpnBrvrF3XhoGhjELY5RWTIQgLbNJgOlO3uxt 4d+XiEr2KggZNk0D2a/3M0F1IRHplA5m+1NXrAN0tP918Y2nokL0n9zRLbfOWAMyaJ2Q beEb9W4X5NKj/DpGHg7ZQVC9QnQZ7wDRATscsNx8htwusD9k+VmLxpoV1j1SSPGezxSx 58Rg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685144693; x=1687736693; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=7JLLUnbPXAaoH9i/N14KC8FRF4yfK0mZvbZkcTBOq5Y=; b=anMb0t9Glpu3K95EuJXc7QOY2b0zO+34FivWTruuX8GUWh9+cB53hYSaSYR7AdVAvT eebD6UF42ihI6kmCRyQrb8aRdSwFX7J+Xj8jePLYvLdb/PJgPkxBZCYGG43B7L+mBQ/g dfKwlZ0vRHJ/jT7xMZaV/5UIzOdkc3BWIU9MB5XxUaCMP0Ixar4co6Yn4zFZ9LXc9JhG KxubRouyFC0Dk88uG+VrbtDy1Cy9tfSwATit6uh0bjjjjNyoAQjBlPVR6pYzEO2VVB+S z5eNmsLpWAzZ9zMQ47cgupdW5mOe4qUwub3JeKHMu+QYIhEwxncEZaVjJPUDJVgExuhw N+rg== X-Gm-Message-State: AC+VfDyXVMTYSEOw4sCOzneYmANBcBIA9XX7eRoH2NYBv0ftFzYCOtEK 880LF5iLs+IYebsZ6DC0304UGNxQKfo= X-Received: from yuzhao.bld.corp.google.com ([2620:15c:183:200:910f:8a15:592b:2087]) (user=yuzhao job=sendgmr) by 2002:a5b:9c6:0:b0:ba8:381b:f764 with SMTP id y6-20020a5b09c6000000b00ba8381bf764mr354063ybq.3.1685144692904; Fri, 26 May 2023 16:44:52 -0700 (PDT) Date: Fri, 26 May 2023 17:44:33 -0600 In-Reply-To: <20230526234435.662652-1-yuzhao@google.com> Message-Id: <20230526234435.662652-9-yuzhao@google.com> Mime-Version: 1.0 References: <20230526234435.662652-1-yuzhao@google.com> X-Mailer: git-send-email 2.41.0.rc0.172.g3f132b7071-goog Subject: [PATCH mm-unstable v2 08/10] kvm/x86: move tdp_mmu_enabled and shadow_accessed_mask From: Yu Zhao <yuzhao@google.com> To: Andrew Morton <akpm@linux-foundation.org>, Paolo Bonzini <pbonzini@redhat.com> Cc: Alistair Popple <apopple@nvidia.com>, Anup Patel <anup@brainfault.org>, Ben Gardon <bgardon@google.com>, Borislav Petkov <bp@alien8.de>, Catalin Marinas <catalin.marinas@arm.com>, Chao Peng <chao.p.peng@linux.intel.com>, Christophe Leroy <christophe.leroy@csgroup.eu>, Dave Hansen <dave.hansen@linux.intel.com>, Fabiano Rosas <farosas@linux.ibm.com>, Gaosheng Cui <cuigaosheng1@huawei.com>, Gavin Shan <gshan@redhat.com>, "H. Peter Anvin" <hpa@zytor.com>, Ingo Molnar <mingo@redhat.com>, James Morse <james.morse@arm.com>, "Jason A. Donenfeld" <Jason@zx2c4.com>, Jason Gunthorpe <jgg@ziepe.ca>, Jonathan Corbet <corbet@lwn.net>, Marc Zyngier <maz@kernel.org>, Masami Hiramatsu <mhiramat@kernel.org>, Michael Ellerman <mpe@ellerman.id.au>, Michael Larabel <michael@michaellarabel.com>, Mike Rapoport <rppt@kernel.org>, Nicholas Piggin <npiggin@gmail.com>, Oliver Upton <oliver.upton@linux.dev>, Paul Mackerras <paulus@ozlabs.org>, Peter Xu <peterx@redhat.com>, Sean Christopherson <seanjc@google.com>, Steven Rostedt <rostedt@goodmis.org>, Suzuki K Poulose <suzuki.poulose@arm.com>, Thomas Gleixner <tglx@linutronix.de>, Thomas Huth <thuth@redhat.com>, Will Deacon <will@kernel.org>, Zenghui Yu <yuzenghui@huawei.com>, kvmarm@lists.linux.dev, kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linuxppc-dev@lists.ozlabs.org, linux-trace-kernel@vger.kernel.org, x86@kernel.org, linux-mm@google.com, Yu Zhao <yuzhao@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,T_SCC_BODY_TEXT_LINE,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?1767002384507311671?= X-GMAIL-MSGID: =?utf-8?q?1767002384507311671?= |
Series |
mm/kvm: locklessly clear the accessed bit
|
|
Commit Message
Yu Zhao
May 26, 2023, 11:44 p.m. UTC
tdp_mmu_enabled and shadow_accessed_mask are needed to implement
kvm_arch_has_test_clear_young().
Signed-off-by: Yu Zhao <yuzhao@google.com>
---
arch/x86/include/asm/kvm_host.h | 6 ++++++
arch/x86/kvm/mmu.h | 6 ------
arch/x86/kvm/mmu/spte.h | 1 -
3 files changed, 6 insertions(+), 7 deletions(-)
Comments
On Fri, May 26, 2023, Yu Zhao wrote: > tdp_mmu_enabled and shadow_accessed_mask are needed to implement > kvm_arch_has_test_clear_young(). > > Signed-off-by: Yu Zhao <yuzhao@google.com> > --- > arch/x86/include/asm/kvm_host.h | 6 ++++++ > arch/x86/kvm/mmu.h | 6 ------ > arch/x86/kvm/mmu/spte.h | 1 - > 3 files changed, 6 insertions(+), 7 deletions(-) > > diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h > index fb9d1f2d6136..753c67072c47 100644 > --- a/arch/x86/include/asm/kvm_host.h > +++ b/arch/x86/include/asm/kvm_host.h > @@ -1772,6 +1772,7 @@ struct kvm_arch_async_pf { > > extern u32 __read_mostly kvm_nr_uret_msrs; > extern u64 __read_mostly host_efer; > +extern u64 __read_mostly shadow_accessed_mask; > extern bool __read_mostly allow_smaller_maxphyaddr; > extern bool __read_mostly enable_apicv; > extern struct kvm_x86_ops kvm_x86_ops; > @@ -1855,6 +1856,11 @@ void kvm_fire_mask_notifiers(struct kvm *kvm, unsigned irqchip, unsigned pin, > bool mask); > > extern bool tdp_enabled; > +#ifdef CONFIG_X86_64 > +extern bool tdp_mmu_enabled; > +#else > +#define tdp_mmu_enabled false > +#endif I would much prefer that these be kept in kvm/mmu.h. And looking at all the arch code, there's no reason to make kvm_arch_has_test_clear_young() a runtime callback, all of the logic is constant relative to when KVM is loaded. So rather than have generic KVM pull from arch code, what if we have arch code push info to generic KVM? We could even avoid #ifdefs if arch code passed in its handler. That might result in an extra indirect branch though, so it might be better to just use a flag? E.g. the x86 conversion would be something like this. --- arch/x86/kvm/mmu/mmu.c | 5 +++++ arch/x86/kvm/mmu/tdp_mmu.c | 2 +- arch/x86/kvm/mmu/tdp_mmu.h | 1 + include/linux/kvm_host.h | 24 ++++-------------------- virt/kvm/kvm_main.c | 14 ++++++++++---- 5 files changed, 21 insertions(+), 25 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index c8ebe542c565..84a4a83540f0 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -5809,6 +5809,11 @@ void kvm_configure_mmu(bool enable_tdp, int tdp_forced_root_level, max_huge_page_level = PG_LEVEL_1G; else max_huge_page_level = PG_LEVEL_2M; + + if (tdp_mmu_enabled && kvm_ad_enabled()) + kvm_init_test_clear_young(kvm_tdp_mmu_test_clear_young); + else + kvm_init_test_clear_young(NULL); } EXPORT_SYMBOL_GPL(kvm_configure_mmu); diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c index f463d54228f8..e878c88f0e02 100644 --- a/arch/x86/kvm/mmu/tdp_mmu.c +++ b/arch/x86/kvm/mmu/tdp_mmu.c @@ -1308,7 +1308,7 @@ bool kvm_tdp_mmu_test_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range) return kvm_tdp_mmu_handle_gfn(kvm, range, test_age_gfn); } -bool kvm_arch_test_clear_young(struct kvm *kvm, struct kvm_gfn_range *range) +bool kvm_tdp_mmu_test_clear_young(struct kvm *kvm, struct kvm_gfn_range *range) { struct kvm_mmu_page *root; int offset = ffs(shadow_accessed_mask) - 1; diff --git a/arch/x86/kvm/mmu/tdp_mmu.h b/arch/x86/kvm/mmu/tdp_mmu.h index 0a63b1afabd3..aaa0b75b3896 100644 --- a/arch/x86/kvm/mmu/tdp_mmu.h +++ b/arch/x86/kvm/mmu/tdp_mmu.h @@ -34,6 +34,7 @@ bool kvm_tdp_mmu_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range, bool kvm_tdp_mmu_age_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range); bool kvm_tdp_mmu_test_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range); bool kvm_tdp_mmu_set_spte_gfn(struct kvm *kvm, struct kvm_gfn_range *range); +bool kvm_tdp_mmu_test_clear_young(struct kvm *kvm, struct kvm_gfn_range *range); bool kvm_tdp_mmu_wrprot_slot(struct kvm *kvm, const struct kvm_memory_slot *slot, int min_level); diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 1714f82a0c47..7a0922cbc36f 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -264,31 +264,15 @@ struct kvm_gfn_range { pte_t pte; bool may_block; }; + +typedef bool (*hva_handler_t)(struct kvm *kvm, struct kvm_gfn_range *range); + bool kvm_unmap_gfn_range(struct kvm *kvm, struct kvm_gfn_range *range); bool kvm_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range); bool kvm_test_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range); bool kvm_set_spte_gfn(struct kvm *kvm, struct kvm_gfn_range *range); bool kvm_should_clear_young(struct kvm_gfn_range *range, gfn_t gfn); -bool kvm_arch_test_clear_young(struct kvm *kvm, struct kvm_gfn_range *range); -#endif - -/* - * Architectures that implement kvm_arch_test_clear_young() should override - * kvm_arch_has_test_clear_young(). - * - * kvm_arch_has_test_clear_young() is allowed to return false positive, i.e., it - * can return true if kvm_arch_test_clear_young() is supported but disabled due - * to some runtime constraint. In this case, kvm_arch_test_clear_young() should - * return true; otherwise, it should return false. - * - * For each young KVM PTE, kvm_arch_test_clear_young() should call - * kvm_should_clear_young() to decide whether to clear the accessed bit. - */ -#ifndef kvm_arch_has_test_clear_young -static inline bool kvm_arch_has_test_clear_young(void) -{ - return false; -} +void kvm_init_test_clear_young(hva_handler_t arch_test_clear_young); #endif enum { diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index ef2790469fda..ac83cfb30771 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -530,8 +530,6 @@ static void kvm_mmu_notifier_invalidate_range(struct mmu_notifier *mn, srcu_read_unlock(&kvm->srcu, idx); } -typedef bool (*hva_handler_t)(struct kvm *kvm, struct kvm_gfn_range *range); - typedef void (*on_lock_fn_t)(struct kvm *kvm, unsigned long start, unsigned long end); @@ -859,6 +857,14 @@ bool kvm_should_clear_young(struct kvm_gfn_range *range, gfn_t gfn) return args->clear; } +static hva_handler_t kvm_test_clear_young; + +void kvm_init_test_clear_young(hva_handler_t arch_test_clear_young) +{ + WARN_ON_ONCE(!list_empty(&vm_list)); + kvm_test_clear_young = arch_test_clear_young; +} + static int kvm_mmu_notifier_test_clear_young(struct mmu_notifier *mn, struct mm_struct *mm, unsigned long start, unsigned long end, bool clear, unsigned long *bitmap) @@ -873,7 +879,7 @@ static int kvm_mmu_notifier_test_clear_young(struct mmu_notifier *mn, struct mm_ trace_kvm_age_hva(start, end); - if (kvm_arch_has_test_clear_young()) { + if (kvm_test_clear_young) { struct test_clear_young_args args = { .bitmap = bitmap, .end = end, @@ -882,7 +888,7 @@ static int kvm_mmu_notifier_test_clear_young(struct mmu_notifier *mn, struct mm_ range.args = &args; range.lockless = true; - range.handler = kvm_arch_test_clear_young; + range.handler = kvm_test_clear_young; if (!__kvm_handle_hva_range(kvm, &range)) return args.young ? MMU_NOTIFIER_RANGE_LOCKLESS : 0; base-commit: 39ca80f27cc0d2a37b4e3d07bbf763d4954934d7 --
diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index fb9d1f2d6136..753c67072c47 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1772,6 +1772,7 @@ struct kvm_arch_async_pf { extern u32 __read_mostly kvm_nr_uret_msrs; extern u64 __read_mostly host_efer; +extern u64 __read_mostly shadow_accessed_mask; extern bool __read_mostly allow_smaller_maxphyaddr; extern bool __read_mostly enable_apicv; extern struct kvm_x86_ops kvm_x86_ops; @@ -1855,6 +1856,11 @@ void kvm_fire_mask_notifiers(struct kvm *kvm, unsigned irqchip, unsigned pin, bool mask); extern bool tdp_enabled; +#ifdef CONFIG_X86_64 +extern bool tdp_mmu_enabled; +#else +#define tdp_mmu_enabled false +#endif u64 vcpu_tsc_khz(struct kvm_vcpu *vcpu); diff --git a/arch/x86/kvm/mmu.h b/arch/x86/kvm/mmu.h index 92d5a1924fc1..84aedb2671ef 100644 --- a/arch/x86/kvm/mmu.h +++ b/arch/x86/kvm/mmu.h @@ -253,12 +253,6 @@ static inline bool kvm_shadow_root_allocated(struct kvm *kvm) return smp_load_acquire(&kvm->arch.shadow_root_allocated); } -#ifdef CONFIG_X86_64 -extern bool tdp_mmu_enabled; -#else -#define tdp_mmu_enabled false -#endif - static inline bool kvm_memslots_have_rmaps(struct kvm *kvm) { return !tdp_mmu_enabled || kvm_shadow_root_allocated(kvm); diff --git a/arch/x86/kvm/mmu/spte.h b/arch/x86/kvm/mmu/spte.h index 1279db2eab44..a82c4fa1c47b 100644 --- a/arch/x86/kvm/mmu/spte.h +++ b/arch/x86/kvm/mmu/spte.h @@ -153,7 +153,6 @@ extern u64 __read_mostly shadow_mmu_writable_mask; extern u64 __read_mostly shadow_nx_mask; extern u64 __read_mostly shadow_x_mask; /* mutual exclusive with nx_mask */ extern u64 __read_mostly shadow_user_mask; -extern u64 __read_mostly shadow_accessed_mask; extern u64 __read_mostly shadow_dirty_mask; extern u64 __read_mostly shadow_mmio_value; extern u64 __read_mostly shadow_mmio_mask;