From patchwork Fri May 26 23:44:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yu Zhao X-Patchwork-Id: 13257446 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id DACB2C77B7C for ; Fri, 26 May 2023 23:44:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4F29D280007; Fri, 26 May 2023 19:44:58 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 47A01280001; Fri, 26 May 2023 19:44:58 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 31B44280007; Fri, 26 May 2023 19:44:58 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 1ECF8280001 for ; Fri, 26 May 2023 19:44:58 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id EBADF80ED3 for ; Fri, 26 May 2023 23:44:57 +0000 (UTC) X-FDA: 80834039034.15.428470A Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf26.hostedemail.com (Postfix) with ESMTP id 1F036140015 for ; Fri, 26 May 2023 23:44:55 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=yrD5cypj; spf=pass (imf26.hostedemail.com: domain of 3d0RxZAYKCO4okpXQeWeeWbU.SecbYdkn-ccalQSa.ehW@flex--yuzhao.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3d0RxZAYKCO4okpXQeWeeWbU.SecbYdkn-ccalQSa.ehW@flex--yuzhao.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1685144696; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=M3AF7fBmc1xnJAMOiZdfCsfXZHiS6oPk41ReBM4zbdo=; b=cgUS1gbK+cfXOfJayyYXaz7eXXDqmzUxRhu0WMEPTfVqwQikGU3jQgBdZK5c63vxkP4tBO +CYRK/LCZ9SL8L4hBy+NbnM1VPZibAADTQKgrI7Le8KtlCQfLGsGzOqTJVTamblnE9A6vl X+iXJ5IbgdMn3BE+BhuZju4W+11Kjj0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1685144696; a=rsa-sha256; cv=none; b=SYWmc0edeldSQ3scI1GSFCl9VZSZMmOV7vx3+iKmhHSIRG3TKblpWEHbXj1AhKgsU0JoTO sOtbSjQWYQktfSaM23G0jC/DL2+TxrAMHkiwk1RdJ1eoc/f6uQYdRoGNeZvxyfUkmx08XD vGnPTJFTh7gmXUJTeSEYAgIzxtq3PJE= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=yrD5cypj; spf=pass (imf26.hostedemail.com: domain of 3d0RxZAYKCO4okpXQeWeeWbU.SecbYdkn-ccalQSa.ehW@flex--yuzhao.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3d0RxZAYKCO4okpXQeWeeWbU.SecbYdkn-ccalQSa.ehW@flex--yuzhao.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-bad0f5d6a7dso2621781276.0 for ; Fri, 26 May 2023 16:44:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1685144695; x=1687736695; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=M3AF7fBmc1xnJAMOiZdfCsfXZHiS6oPk41ReBM4zbdo=; b=yrD5cypjwj76oGySeMKoL+EObdL+VlHBR0ZE4dlUQxoYbTEWiiVpSKdOJmkcaaiF/Z 4zilNg4E7S1qiA0OC/fMHUKvaq4lLe3h3XrcfBRajVZEzuTWsMjYGvjMBJ/wRyLbmfd/ x0p59R3lRKLAmUF0+sTBQhD8rRciT1hrCi2rbco+ObNSFPCgz7YI6gzDi295X90OlgMM zrJNip6i/8aKO3FHb/HgogYFOfPOgx60+DOedoHxymQQDAPdmrus+K28IPt1M7E0g0cI asbBmGj30I6PgAgE5VquUWTvHUX2/5VYaFLlsw0xBC7qUsoM7lDeTwedn+JU6nSVVIPC w2tA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685144695; x=1687736695; 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=M3AF7fBmc1xnJAMOiZdfCsfXZHiS6oPk41ReBM4zbdo=; b=ZyigEojnWAnyX0whqY+mYMzdgkaBB8znBriLON0THAw4OfyrwBaX185M0WIuOkpiqQ 0x01oz4w2ZQ4rULYJaCOsaydbWjj/8KDN0ZG4kv53gMzcz6kr+LtcGCekmS+UHfGx2t4 DyT4qXhiH3TLcHDsav7Ipz0K1phLNGBU57ooOk8AErpXlXUik7VOTkd+bKMotDFGt7wK oNo/Km+Lbll7O+t99e/Vx8Eg2hnmOWbTPu9BgptvmFsOS1O3qYmQDmPTHsKSmybTImoP XWh1Xy9YBlYuGdZJb318PCZUmWMaZfSzCMXW17D7qoIxgoWk5c/FLJEZyjjPAhWJwy8M Egog== X-Gm-Message-State: AC+VfDyF18SGEV9gKBU500nZyNyU6uAL7HBIkpk4jzRZtAw/W8AKUKvx AhU2+2e+7whJQvcF/DNThtoDywci7Dc= X-Google-Smtp-Source: ACHHUZ4QgTvrucO0Q6McE6sU/ZYyF1Ujw9/eAceyjgq3u2aouYR16g/56Sc2t/zSHX7sxuPwIu+K0d0WjYg= X-Received: from yuzhao.bld.corp.google.com ([2620:15c:183:200:910f:8a15:592b:2087]) (user=yuzhao job=sendgmr) by 2002:a05:6902:1341:b0:bac:6bb:2549 with SMTP id g1-20020a056902134100b00bac06bb2549mr1840571ybu.7.1685144695170; Fri, 26 May 2023 16:44:55 -0700 (PDT) Date: Fri, 26 May 2023 17:44:34 -0600 In-Reply-To: <20230526234435.662652-1-yuzhao@google.com> Message-Id: <20230526234435.662652-10-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 09/10] kvm/x86: add kvm_arch_test_clear_young() From: Yu Zhao To: Andrew Morton , Paolo Bonzini Cc: Alistair Popple , Anup Patel , Ben Gardon , Borislav Petkov , Catalin Marinas , Chao Peng , Christophe Leroy , Dave Hansen , Fabiano Rosas , Gaosheng Cui , Gavin Shan , "H. Peter Anvin" , Ingo Molnar , James Morse , "Jason A. Donenfeld" , Jason Gunthorpe , Jonathan Corbet , Marc Zyngier , Masami Hiramatsu , Michael Ellerman , Michael Larabel , Mike Rapoport , Nicholas Piggin , Oliver Upton , Paul Mackerras , Peter Xu , Sean Christopherson , Steven Rostedt , Suzuki K Poulose , Thomas Gleixner , Thomas Huth , Will Deacon , Zenghui Yu , 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 X-Rspamd-Queue-Id: 1F036140015 X-Rspam-User: X-Stat-Signature: qgcc6ipecptgtotwudxhk1j55dkr6cpi X-Rspamd-Server: rspam03 X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t03Cw/bjhtd5MzoOwOJH7g+fW7QRMnXX4+XocO33hXWvV13+yxILnaRs/UrzLUEZ60Ssofd673yPcmqzEhlqObzqyDdnvQ1IPOJvv4mLY2S/1VGK6zImxPriWXi5p4oiqyGWzKB13/aqs/+yVSI67+8I/8AGFNAPTyo3ADj36YKGpd5SefSIrAtFtu21HarEfX7Fp3for4msTsAyJLvyQjEEQzq7apV8sNRepFjdOgrmsFXDkQTlzZveyb8dEPqtyRWO3Lu/LZ53n9L+XHgaqpp+S6mtJtIIQ6SU4qSmjqBZ5lTt3qkdifhush39ij0qXNONlGA4F+XgsKJvtNg7QTOhz3qGbGjhAbuqr8ss2Xt5+PyF7t7ZIMqO36XBfoj2ckUI1rk5DCl7TJaTaLzyEGARESwnzXErKsDxFEzQEWKX+3JmkZ6qujSCeiyFVJHKt+PPNssPg19C0XdlQELD+kPEEzPCdVY+EDZhvsyVwI76YIsNIYQUloaDi9G7FSXcIZsiJqbz4zOH2vbYeTec//+ipeEPiY4MJWAnTcTzvAWOAPoM+gCQXfHTWTxxHZyWeJJCDOXmz+w/Hv2WKKAN0rJmOzz1AGiQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Implement kvm_arch_test_clear_young() to support the fast path in mmu_notifier_ops->test_clear_young(). It focuses on a simple case, i.e., TDP MMU sets the accessed bit in KVM PTEs and VMs are not nested, where it can rely on RCU and clear_bit() to safely clear the accessed bit without taking kvm->mmu_lock. Complex cases fall back to the existing slow path where kvm->mmu_lock is then taken. Signed-off-by: Yu Zhao --- arch/x86/include/asm/kvm_host.h | 7 +++++++ arch/x86/kvm/mmu/tdp_mmu.c | 34 +++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 753c67072c47..d6dfdebe3d94 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -2223,4 +2223,11 @@ int memslot_rmap_alloc(struct kvm_memory_slot *slot, unsigned long npages); */ #define KVM_EXIT_HYPERCALL_MBZ GENMASK_ULL(31, 1) +#define kvm_arch_has_test_clear_young kvm_arch_has_test_clear_young +static inline bool kvm_arch_has_test_clear_young(void) +{ + return IS_ENABLED(CONFIG_X86_64) && + (!IS_REACHABLE(CONFIG_KVM) || (tdp_mmu_enabled && shadow_accessed_mask)); +} + #endif /* _ASM_X86_KVM_HOST_H */ diff --git a/arch/x86/kvm/mmu/tdp_mmu.c b/arch/x86/kvm/mmu/tdp_mmu.c index 08340219c35a..6875a819e007 100644 --- a/arch/x86/kvm/mmu/tdp_mmu.c +++ b/arch/x86/kvm/mmu/tdp_mmu.c @@ -1232,6 +1232,40 @@ 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) +{ + struct kvm_mmu_page *root; + int offset = ffs(shadow_accessed_mask) - 1; + + if (kvm_shadow_root_allocated(kvm)) + return true; + + rcu_read_lock(); + + list_for_each_entry_rcu(root, &kvm->arch.tdp_mmu_roots, link) { + struct tdp_iter iter; + + if (kvm_mmu_page_as_id(root) != range->slot->as_id) + continue; + + tdp_root_for_each_leaf_pte(iter, root, range->start, range->end) { + u64 *sptep = rcu_dereference(iter.sptep); + + VM_WARN_ON_ONCE(!page_count(virt_to_page(sptep))); + + if (!(iter.old_spte & shadow_accessed_mask)) + continue; + + if (kvm_should_clear_young(range, iter.gfn)) + clear_bit(offset, (unsigned long *)sptep); + } + } + + rcu_read_unlock(); + + return false; +} + static bool set_spte_gfn(struct kvm *kvm, struct tdp_iter *iter, struct kvm_gfn_range *range) {