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: 13692691 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 CE8B5C27C4F for ; Tue, 11 Jun 2024 00:22:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5E0776B0098; Mon, 10 Jun 2024 20:22:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 573886B0099; Mon, 10 Jun 2024 20:22:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2F2706B009A; Mon, 10 Jun 2024 20:22:08 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 1015F6B0098 for ; Mon, 10 Jun 2024 20:22:08 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id C84C6C0F9B for ; Tue, 11 Jun 2024 00:22:07 +0000 (UTC) X-FDA: 82216705494.04.59E4758 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf12.hostedemail.com (Postfix) with ESMTP id E4FE440010 for ; Tue, 11 Jun 2024 00:22:05 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=B5VIXhjE; spf=pass (imf12.hostedemail.com: domain of 3rJhnZgoKCLUeocjpbcojibjjbgZ.Xjhgdips-hhfqVXf.jmb@flex--jthoughton.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3rJhnZgoKCLUeocjpbcojibjjbgZ.Xjhgdips-hhfqVXf.jmb@flex--jthoughton.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1718065325; a=rsa-sha256; cv=none; b=eHUvK4zWz+GXE1q/MHjLMinDqfxBfIt5RYylpr4L61Y4pUYJv9vxkvAROhGIm9lDvHbSNT IGBJp+VVmAb/0BhXPc1uv9/HDbv1eE3nkONWMXpE+ddv4KIsbuxM6shLEu3kzWbZriRh30 QJ+v5niO7qvORfDz3NJ8p2hAiwqvvdk= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=B5VIXhjE; spf=pass (imf12.hostedemail.com: domain of 3rJhnZgoKCLUeocjpbcojibjjbgZ.Xjhgdips-hhfqVXf.jmb@flex--jthoughton.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3rJhnZgoKCLUeocjpbcojibjjbgZ.Xjhgdips-hhfqVXf.jmb@flex--jthoughton.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=1718065325; 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=jRq1E0P4aXnPxzizQoW47AD5YHVJpyUxzEyvFYZC4Cg=; b=gc3pUrgGvwVOb3a/gxVfdlyHQlt1hp5r94y6+49NOjV2udLlTef+2qoLb5Y3ShL5B/9a6b WVWAn6BcfhSYfbMq13R3wRhPtrLueItKrqCnHE32pm07b4oU2TWaJA58LA3Np+b1vhUUy3 hpLbcjsV9SMr3QaQ6YMCWxQUKf5A5Vc= Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-62cec76c2f2so9511367b3.3 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=kvack.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=B5VIXhjEN2ISFPZxcTzx5sAniKunU/SFICTv21giaHNcajw4k6U6jMT3ZgRIO0aeVz HV4/jXe0wF1XTYyjNwB0pk9BOboZMc3enUmujux47vJs9+l3zFBFqSWPIIEcPvyAATsR 6YhDXexyExNjkdt2OgNHLclBe9aMNSNKaNVO9o9Z/d7N71hzmNixg4EC0yWAh9/KI9FC LxlnynypLO3OoD79EzO07Yvn8KGtH5gRzY6bSPjsdSTa9vFaS9PHzgoOQ+8wb6SjGUrK 66XpUXavOITc6m69pHkwS47QCoW8rdPfofcVByqqge5VtRdIS7ubuqL9dLumCT+GZG5f 7Isg== 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=LVTnXHO8+JV8D73uSJQysg+JxpK4TAansd+3qE5SBwPO0n4/7s4VXYumMTZbuG2xRH 1UOAUUkAfLUrGhNE7UXRMPx4kpWbjYjRqwfFpgHeb6kaGHRK0MeQX+9qcdhrGKcH8yr8 yTjGWPKMulkG7D9CTCRM3Sevrp8GA+/hpsJw1x2DDtSUZAe2HuKCIpOn2zwYb/kJjelk 2Y9NJIXVtDcepLoWQkSl8VnzrwEJ9QrNqVgM4UyuXxn/JA03DerbBovAErJLOqvZQ1P5 U1xUiXf1xs152e9MneDEAhchfpmu6S5VWiBvr8rlk0A6xB0O0XCuGpOBnZV4rKvyGsfL XNlg== X-Forwarded-Encrypted: i=1; AJvYcCUsg/djI5C3fxafD2cOY1XGZHQpj6UslWoMV87bFwY4Nvxsu0oBG9Epzx1Ml8On10yXygcJ//hCMJCz/vR7tAB8a8g= X-Gm-Message-State: AOJu0Yza4PLuFV1kqc7JSgs6hDtGBxGwfYHEMlk7jV4vvffLF9kwtIsp +/+3WYuBOtGX/ByRxK15D1Qn69xBiINxasVY67cmRgAzhSRIFoG//Pw1AEuAGzAO8MPp6YJ74W0 v/0lg+AZG82P5XyNHvw== 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-Stat-Signature: k9gix7i9gftnwbbkuiyooc7zpiwdnx6e X-Rspamd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nkyJOdwRTCy0Odv4XZc7dQ/BlV0NNkj19eKJ5v8HtY3gJLCWmNpa5ySR0v1o7k4LnvM+SZNDcQFpq+lnckbU2HJUGQc/ZfOSZEtSLDM7exwvyEjphHwFrO/nMqvGwHPWj6kdS5oX+Dd5lPTlTIOmTWyJTDQ0hzsGHuIKDWKxNb/CzTGO/qYDQE3BH8w6+oDRr/z8OLiLyBw/MBBJ1cBI8tkbzutYX8Xe/xAs/WkPHeU8yFv0malpUUWx5h36Bw6xTF3JIDT0LQuINbW/r1Mdprleb7c2kpiv0I+Q5C3kAS5Vn+SK1jyJWwX/PcT5ePhq/l7VIL8vZHrkudLmnS9ZJkGrxoir2C2qDtQR2SQlu4r1rZgmV+chmZYXfuRg+tjI/EmCM3upeYHwVHq1X73FdH6VZXIzCmlj1oxQkU5af4lncY4PLIuEdBHRbbeC9/4dp/sE3BtZewgY4WVdIy9/KiSeAFf1W7D9eeN6ycVjMFv0VK82u+J7CnTZYbOMdV4BiV+shfdLvTnb6/vS5keM4dtvIy+e1/37N/W9Z43pyRKpMqbwwASuN9dE5HpE7BsAQ3J62DC+bH4or 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: List-Subscribe: List-Unsubscribe: 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)