From patchwork Tue Jun 11 00:21:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Houghton X-Patchwork-Id: 13692731 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id D79FBC27C4F for ; Tue, 11 Jun 2024 01:32:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:Message-ID: References:Mime-Version:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=n6UTscINJERmwbhVf7m+NYuZwRwt205ZMR1On5H1+dU=; b=SywyNtb0TKPkncrowhtwcJOYzt dxV3i1nUi8ECV/CTZER/Np//354/Xs0U5IyVdKcsoh1azQsCOpzc+Iql91lpRbeEr47se/WBbLVmv cPtvGO/tg8SnYOdS9QuUinfyRKkHjwGEXUsK5FmIcK26k018Vlb1H4umz81eaBbOnat3SAW2ebF6W MF4ClFkZ0puITyn/CcO32M3cYrpVEbRSN8qpN9bf1C6RyJrsarzNwjUN4MkSPGUBGhZogQBU/I4Y6 zvnfGRREuRqNUwdX1N1t1IaQ2ofPKLXKRZY0CXZRpNvnUmVeybg6otV3s6bGRZeUsWxtcqG4hYl83 u5QKmsgQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sGqNP-000000074Zu-2nEx; Tue, 11 Jun 2024 01:32:43 +0000 Received: from mail-yw1-x1149.google.com ([2607:f8b0:4864:20::1149]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sGpH6-00000006vmC-3Z0h for linux-arm-kernel@lists.infradead.org; Tue, 11 Jun 2024 00:22:11 +0000 Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-62a080977a5so9587427b3.0 for ; Mon, 10 Jun 2024 17:22:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1718065325; x=1718670125; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=jRq1E0P4aXnPxzizQoW47AD5YHVJpyUxzEyvFYZC4Cg=; b=eB2b6SkKTN7MMaZL+w+lnyxrDr48Tv4BGfbZsloAr9pKK8RLDJ1UpeQFeC7eYz7OZL RWwf2gasMq3pheQ22LoU38BytCq5Qq5LUvsbj/M+dc/CABwKzqzIBiYi3DQQTIqNHbdE 8h6feDqwkXq+ijAaWb3+mn5miVGLz3NvEUYuPwiMnaGZsDPNEmdWseqTj5Fzx1SEUBQ3 /pKZ+ZG8n7Vjm5OQDq4RvWfWhFltVN9KC1bkt+W9q16h+UO7bBS0HA1VQOUifS0gMugm HxpdnRbxkR6jjCEepzJ076QGIzELpo7TIQU8T3iIOKWPOpkG/vodP3vKxDBQKOXo8Gwf iCDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718065325; x=1718670125; 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=jRq1E0P4aXnPxzizQoW47AD5YHVJpyUxzEyvFYZC4Cg=; b=hwuGFk3BKFuXxAFhPd2/jPfKyHDZoBuuPHeMMsVVrt1jmacEm1AowVmF7iVf/4RJwI Zm+OPUVdzYW3yOuBZEV4310K5wxeibuVWKNfhmrb+CsCUFEcX26BsW8zOy0UlNpp1Lsw oE4/hRyt77j0AkU8ot8BKftPwj6+20MrmWOQvl9YbN9QbtoWM8ea0wtkswe1MWb1xVgp KNq0oqFH6565sbEWX46EQTw+x2DYZj/I2AtQmtgoe3kOFHcJZIyhbP3y9WMhsbRWicPH pXNEqVoHVgXWsZ9rIaSF4iPHYk2ImklnXKWbKsZ91HEAeTy/u2Vq2svohmJytGmdgjwS e8lw== X-Forwarded-Encrypted: i=1; AJvYcCWEZhsqfcWLIMGIEGAF7x94NFYva7au7vQIHtE7GrxfV0fUUzzV9sXCB2X3EqBoNsBtaDEPKbIwfvhs8+obZAfvppdb/ozPAArDqMdkdwFbGHwt9IE= X-Gm-Message-State: AOJu0YxLphn8Xxusi9SEKDTVb6uOtyQ5NnMvaWYMzVKXY/iH1CqirRpy mDNnAJRB7g3G6Ms3GizFYoRawTv8UToAv+4jmn8tf0nXASQqRsn5ZWqgqYeTGjXC3zc9Vu4LZOy Eod3avYC3bF1vN8FhJQ== X-Google-Smtp-Source: AGHT+IFmXdVsVq2jHeT0x/0hUnfSFWa7WYtFFT8Isj0HD4sG8F4BOnOmk4JS+KLDOFafX3S7G6SXbQD7sfuchknU X-Received: from jthoughton.c.googlers.com ([fda3:e722:ac3:cc00:14:4d90:c0a8:2a4f]) (user=jthoughton job=sendgmr) by 2002:a05:690c:fca:b0:62c:fa1a:21d2 with SMTP id 00721157ae682-62cfa1a282cmr21119147b3.1.1718065324978; Mon, 10 Jun 2024 17:22:04 -0700 (PDT) Date: Tue, 11 Jun 2024 00:21:43 +0000 In-Reply-To: <20240611002145.2078921-1-jthoughton@google.com> Mime-Version: 1.0 References: <20240611002145.2078921-1-jthoughton@google.com> X-Mailer: git-send-email 2.45.2.505.gda0bf45e8d-goog Message-ID: <20240611002145.2078921-8-jthoughton@google.com> Subject: [PATCH v5 7/9] KVM: x86: Implement kvm_fast_test_age_gfn and kvm_fast_age_gfn From: James Houghton To: Andrew Morton , Paolo Bonzini Cc: Ankit Agrawal , Axel Rasmussen , Catalin Marinas , David Matlack , David Rientjes , James Houghton , James Morse , Jonathan Corbet , Marc Zyngier , Oliver Upton , Raghavendra Rao Ananta , Ryan Roberts , Sean Christopherson , Shaoqin Huang , Suzuki K Poulose , Wei Xu , Will Deacon , Yu Zhao , 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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240610_172209_086493_F506C284 X-CRM114-Status: GOOD ( 16.98 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org The fast-only notifier will only report an accurate result when the shadow MMU is not in use. Implement kvm_arch_young_notifier_likely_fast(), as MGLRU will check this function to see if it should even be attempting the fast-only notifier. We only want to attempt the notifier if there is a chance that it will succeed (i.e., that we're using the TDP MMU). Signed-off-by: James Houghton --- arch/x86/include/asm/kvm_host.h | 7 +++++ arch/x86/kvm/Kconfig | 1 + arch/x86/kvm/mmu/mmu.c | 50 ++++++++++++++++++++++++++++++--- 3 files changed, 54 insertions(+), 4 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 0dc1fa99cdbb..ca2fbc162e51 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -2338,4 +2338,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_young_notifier_likely_fast kvm_arch_young_notifier_likely_fast +static inline bool kvm_arch_young_notifier_likely_fast(void) +{ + return IS_ENABLED(CONFIG_X86_64) && tdp_mmu_enabled && + shadow_accessed_mask; +} + #endif /* _ASM_X86_KVM_HOST_H */ diff --git a/arch/x86/kvm/Kconfig b/arch/x86/kvm/Kconfig index 9dda7f8c72ed..84ae043c7d43 100644 --- a/arch/x86/kvm/Kconfig +++ b/arch/x86/kvm/Kconfig @@ -24,6 +24,7 @@ config KVM select KVM_COMMON select KVM_GENERIC_MMU_NOTIFIER select KVM_MMU_NOTIFIER_YOUNG_LOCKLESS + select HAVE_KVM_YOUNG_FAST_ONLY_NOTIFIER select HAVE_KVM_IRQCHIP select HAVE_KVM_PFNCACHE select HAVE_KVM_DIRTY_RING_TSO diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 51061f1fb3d1..ed50e78755ab 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -1629,11 +1629,15 @@ static void rmap_add(struct kvm_vcpu *vcpu, const struct kvm_memory_slot *slot, __rmap_add(vcpu->kvm, cache, slot, spte, gfn, access); } -bool kvm_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range) +static int __kvm_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range, + bool fast_only) { bool young = false; if (kvm_memslots_have_rmaps(kvm)) { + if (fast_only) + return -1; + write_lock(&kvm->mmu_lock); young = kvm_handle_gfn_range(kvm, range, kvm_age_rmap); write_unlock(&kvm->mmu_lock); @@ -1642,14 +1646,18 @@ bool kvm_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range) if (tdp_mmu_enabled) young |= kvm_tdp_mmu_age_gfn_range(kvm, range); - return young; + return (int)young; } -bool kvm_test_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range) +static int __kvm_test_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range, + bool fast_only) { bool young = false; if (kvm_memslots_have_rmaps(kvm)) { + if (fast_only) + return -1; + write_lock(&kvm->mmu_lock); young = kvm_handle_gfn_range(kvm, range, kvm_test_age_rmap); write_unlock(&kvm->mmu_lock); @@ -1658,7 +1666,41 @@ bool kvm_test_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range) if (tdp_mmu_enabled) young |= kvm_tdp_mmu_test_age_gfn(kvm, range); - return young; + return (int)young; +} + +bool kvm_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range) +{ + return __kvm_age_gfn(kvm, range, false); +} + +bool kvm_test_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range) +{ + return __kvm_test_age_gfn(kvm, range, false); +} + +bool kvm_fast_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range) +{ + int ret = __kvm_age_gfn(kvm, range, true); + + if (ret < 0) { + *range->arg.failed = true; + return false; + } + + return ret != 0; +} + +bool kvm_fast_test_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range) +{ + int ret = __kvm_test_age_gfn(kvm, range, true); + + if (ret < 0) { + *range->arg.failed = true; + return false; + } + + return ret != 0; } static void kvm_mmu_check_sptes_at_free(struct kvm_mmu_page *sp)