Received: by 2002:ab2:6203:0:b0:1f5:f2ab:c469 with SMTP id o3csp891518lqt; Fri, 19 Apr 2024 13:48:42 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCU3ZPnQnZnq13tc59yrzy4eZQL2bzwH+aZ4IzmA9x60o/gj/YQ6VIxQ9VNIW3uwRMtT3iGjUtcSaqmZwU1M7oqo6IINkVYElC4dAYr2MQ== X-Google-Smtp-Source: AGHT+IEGE83pXNndK+qP52SvgH5QlzNHFt56ul92BhjJ1edkYypgdNV1GXTIF/LWy1dTtZT7M0sU X-Received: by 2002:a05:620a:1373:b0:78d:767f:248 with SMTP id d19-20020a05620a137300b0078d767f0248mr3822553qkl.2.1713559722440; Fri, 19 Apr 2024 13:48:42 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713559722; cv=pass; d=google.com; s=arc-20160816; b=q766AbF1Lgc7MhRnI9P1QYLUQ1QhWHL7u4RZY4KbyZnAPvK5vYg2q0fRdhlnP2WN9X mBnoUYNSBtb3C2BmxJSiOPmXh0NJq8dKDmTXslcAx+jL/s2HPBsXdk7ZO29VTMaIGils CmPK/1DE+Mf7SHcCmKiJpVMbXqjwZI4U8Lm2WaGzGLL9jd2/dDNNFAAhpmceoXnSzwcj D2YQF7Gu8JEU5PV6ATFpXayO9C5A1RqIoMEVfkvFtphKlk58FrFOnjJbDt2bJhEqrZW9 4S3LQIQ9+Y3iBnoiPMTPb91VxUWgjmz1Az8qb3fGgudWgFq8/oxATpdHMaSbeRdhmf3b ISPg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=jdwBN6tcT1rkPndt3s/q21EflrWzoZs9NJ2XL0c5/ME=; fh=/yX56cFeHCwYT0DiAarAkDRztt6Ka2YmlukNficgUE0=; b=sGbddbBm7DJ9JhyrWH7zZlixz539Rlini02D49U6QDBCks+zCpdBhUQv/js5voEz/a JkXM/1NVm5eoKKPV22eDNgIHVvY0cUtIhKvth+107VRvdb49S/NUhcNxB9fz/OFVbRem g9TjFSOxcUKv0hCJTzKvyNK6MUdLtdRqqAESVZ4n+Tei7E1fZjHL8d4yoSReZ8cw8j2O +xmYsGEm9TudBOdxUflcaP6Lg82oXGxrgwr3yLA09KQPvZ/ThXGAs83/WdVoRRgR63Kd eQkO/0FKZ3AyywZmNtZjvtwKcG461J4PNl510l3r4A3/B8dhgnX6AkTj45xW3EibcsOi Tc5w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=GxjpTcA7; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-151948-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-151948-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id 1-20020a05620a04c100b0078efe14d82esi4702297qks.309.2024.04.19.13.48.42 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Apr 2024 13:48:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-151948-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=GxjpTcA7; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-151948-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-151948-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 024CA1C211CB for ; Fri, 19 Apr 2024 20:48:42 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id DD3622D05D; Fri, 19 Apr 2024 20:48:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="GxjpTcA7" Received: from mail-qt1-f175.google.com (mail-qt1-f175.google.com [209.85.160.175]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F22DE2A1CF for ; Fri, 19 Apr 2024 20:48:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.175 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713559711; cv=none; b=QkE4SDMPMibD0fkiSerUnZuY/s3i1scjc+r7BN4UIyJdb7XHDc46sTfpVBaqfAXT22erNQf5OulGBRk7MWt/Oedk+/A6/ZJA1fppPArpD7bEjfbdZ6sUke865nmI+I00CJqZnvPildMFvr28h6rU7ugYpFjBJRBUZKm9Y73yw0g= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713559711; c=relaxed/simple; bh=DvvQfQjHZvt3I094t8XIXjqIQhk2wrjAku9v1k9ONJQ=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=HTGYdqvFa/xYGhi7SB04x3D7hg8/SxCWhRMGRdQcf1RJbCPLuRtjOWbsIIALj2PR6G4ZrNtDaz50xRg7sJTW15fJkS8zXCzrnM/K1c+sftBYmBNFCeh1FjcXp+/7JfreugMkhq0qMkgAgorfCizgJYp3+YwPdMo58HZG+8xOTfA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=GxjpTcA7; arc=none smtp.client-ip=209.85.160.175 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Received: by mail-qt1-f175.google.com with SMTP id d75a77b69052e-436ed871225so25621cf.1 for ; Fri, 19 Apr 2024 13:48:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1713559709; x=1714164509; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=jdwBN6tcT1rkPndt3s/q21EflrWzoZs9NJ2XL0c5/ME=; b=GxjpTcA7+UbRRdnjfGsMs6aV1sdFnyRjrayiNO6R6u737NqFmpMHdsCYKUZfc1KUUE Sf0PaJ6X2Vh4KvIwJhfXQnKwizT9BD3QwQeI6GkSZxP0yVhmxyAOA0ykxq9/Gl3XKVjF aVZEBUohzw57DOMclt9+VfbT4CfUdxtcEQm265xrmoda2bkVsrVHVm12s517vullFr+g 0JUs0Vmi3SVyQ7nj3mxZvb6KpbQF6eZKR/SkozWer4KYL2f8MdKcIJONyXHNMjou2BFD 60dHMhkl7cf+QZj3v7izgW09a1gHPqBkm5kw0Ng9n+aHBoGHg1bQ8CtyF7WK9V0xEI3C RS9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713559709; x=1714164509; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=jdwBN6tcT1rkPndt3s/q21EflrWzoZs9NJ2XL0c5/ME=; b=rx6Gco5VSHKO/DDgrN4e8tRgNSbc/BBvPcrX2sLIHm2vuVVcsk9KsscxsamRaGHso4 szmJ8jDCG9Jk0T4aCts2z5Hl7ApQjGS4mpCpq/uRYwhVKJf73SCO5PeEnIyHnf1BUddb vopVcdAOVzjnWr97vVm8/1ldZqp6ei4E9pazz2jzVr+tgvvuM1v5VKhfTZQZMMBPdiiW 6BUn/V4jjmsquvtmHqjBdIJpuu/pF6ArT+oi7Cc6z+TU7E1Qb5kgDE5hEAngHaIDm167 tJes8hEJdAsCL1Z6kdRiS8G/A764fAHwNupdWDg4YKAy88CxKnkSITJoWUI/4JYDpCFc noFA== X-Forwarded-Encrypted: i=1; AJvYcCXAiu1U1uIWq+0tZzjWgsMwyCTpIy2eJGY3toDw7O+UaZLWdIsEed9Kqlt6BIEwRH0vnU7zfKT+k7Grpve9uiJh4NgkuIkdxMqBk7zc X-Gm-Message-State: AOJu0YyDr6gb/zL+KCiuv5b73FyBbMtHFPcQR38N/gNXaJTEBVeC88fS ecQjLjUTgGNmtymKSFrwfBkNHWAek7fcRtdQ7MTw5pbSw0GmoSE3YioEAs2IPswJjAONUpFSFrt MKbtNFLsfeWhTWkorJlUPYgo7ZY3GKWLDnT/H X-Received: by 2002:ac8:5309:0:b0:437:b985:8523 with SMTP id t9-20020ac85309000000b00437b9858523mr8583qtn.25.1713559708789; Fri, 19 Apr 2024 13:48:28 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240401232946.1837665-1-jthoughton@google.com> <20240401232946.1837665-6-jthoughton@google.com> In-Reply-To: From: James Houghton Date: Fri, 19 Apr 2024 13:47:52 -0700 Message-ID: Subject: Re: [PATCH v3 5/7] KVM: x86: Participate in bitmap-based PTE aging To: David Matlack Cc: Andrew Morton , Paolo Bonzini , Yu Zhao , Marc Zyngier , Oliver Upton , Sean Christopherson , Jonathan Corbet , James Morse , Suzuki K Poulose , Zenghui Yu , Catalin Marinas , Will Deacon , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , Shaoqin Huang , Gavin Shan , Ricardo Koller , Raghavendra Rao Ananta , Ryan Roberts , David Rientjes , Axel Rasmussen , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, kvm@vger.kernel.org, linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Thu, Apr 11, 2024 at 10:28=E2=80=AFAM David Matlack wrote: > > On 2024-04-11 10:08 AM, David Matlack wrote: > > On 2024-04-01 11:29 PM, James Houghton wrote: > > > Only handle the TDP MMU case for now. In other cases, if a bitmap was > > > not provided, fallback to the slowpath that takes mmu_lock, or, if a > > > bitmap was provided, inform the caller that the bitmap is unreliable. > > > > > > Suggested-by: Yu Zhao > > > Signed-off-by: James Houghton > > > --- > > > arch/x86/include/asm/kvm_host.h | 14 ++++++++++++++ > > > arch/x86/kvm/mmu/mmu.c | 16 ++++++++++++++-- > > > arch/x86/kvm/mmu/tdp_mmu.c | 10 +++++++++- > > > 3 files changed, 37 insertions(+), 3 deletions(-) > > > > > > diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/k= vm_host.h > > > index 3b58e2306621..c30918d0887e 100644 > > > --- a/arch/x86/include/asm/kvm_host.h > > > +++ b/arch/x86/include/asm/kvm_host.h > > > @@ -2324,4 +2324,18 @@ int memslot_rmap_alloc(struct kvm_memory_slot = *slot, unsigned long npages); > > > */ > > > #define KVM_EXIT_HYPERCALL_MBZ GENMASK_ULL(31, 1) > > > > > > +#define kvm_arch_prepare_bitmap_age kvm_arch_prepare_bitmap_age > > > +static inline bool kvm_arch_prepare_bitmap_age(struct mmu_notifier *= mn) > > > +{ > > > + /* > > > + * Indicate that we support bitmap-based aging when using the TDP= MMU > > > + * and the accessed bit is available in the TDP page tables. > > > + * > > > + * We have no other preparatory work to do here, so we do not nee= d to > > > + * redefine kvm_arch_finish_bitmap_age(). > > > + */ > > > + return IS_ENABLED(CONFIG_X86_64) && tdp_mmu_enabled > > > + && shadow_accessed_mask; > > > +} > > > + > > > #endif /* _ASM_X86_KVM_HOST_H */ > > > diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c > > > index 992e651540e8..fae1a75750bb 100644 > > > --- a/arch/x86/kvm/mmu/mmu.c > > > +++ b/arch/x86/kvm/mmu/mmu.c > > > @@ -1674,8 +1674,14 @@ bool kvm_age_gfn(struct kvm *kvm, struct kvm_g= fn_range *range) > > > { > > > bool young =3D false; > > > > > > - if (kvm_memslots_have_rmaps(kvm)) > > > + if (kvm_memslots_have_rmaps(kvm)) { > > > + if (range->lockless) { > > > + kvm_age_set_unreliable(range); > > > + return false; > > > + } > > > > If a VM has TDP MMU enabled, supports A/D bits, and is using nested > > virtualization, MGLRU will effectively be blind to all accesses made by > > the VM. > > > > kvm_arch_prepare_bitmap_age() will return true indicating that the > > bitmap is supported. But then kvm_age_gfn() and kvm_test_age_gfn() will > > return false immediately and indicate the bitmap is unreliable because = a > > shadow root is allocate. The notfier will then return > > MMU_NOTIFIER_YOUNG_BITMAP_UNRELIABLE. > > > > Looking at the callers, MMU_NOTIFIER_YOUNG_BITMAP_UNRELIABLE is never > > consumed or used. So I think MGLRU will assume all memory is > > unaccessed? > > > > One way to improve the situation would be to re-order the TDP MMU > > function first and return young instead of false, so that way MGLRU at > > least has visibility into accesses made by L1 (and L2 if EPT is disable > > in L2). But that still means MGLRU is blind to accesses made by L2. > > > > What about grabbing the mmu_lock if there's a shadow root allocated and > > get rid of MMU_NOTIFIER_YOUNG_BITMAP_UNRELIABLE altogether? > > > > if (kvm_memslots_have_rmaps(kvm)) { > > write_lock(&kvm->mmu_lock); > > young |=3D kvm_handle_gfn_range(kvm, range, kvm_age_rmap)= ; > > write_unlock(&kvm->mmu_lock); > > } > > > > The TDP MMU walk would still be lockless. KVM only has to take the > > mmu_lock to collect accesses made by L2. > > > > kvm_age_rmap() and kvm_test_age_rmap() will need to become bitmap-aware > > as well, but that seems relatively simple with the helper functions. > > Wait, even simpler, just check kvm_memslots_have_rmaps() in > kvm_arch_prepare_bitmap_age() and skip the shadow MMU when processing a > bitmap request. > > i.e. > > static inline bool kvm_arch_prepare_bitmap_age(struct kvm *kvm, struct mm= u_notifier *mn) > { > /* > * Indicate that we support bitmap-based aging when using the TDP= MMU > * and the accessed bit is available in the TDP page tables. > * > * We have no other preparatory work to do here, so we do not nee= d to > * redefine kvm_arch_finish_bitmap_age(). > */ > return IS_ENABLED(CONFIG_X86_64) > && tdp_mmu_enabled > && shadow_accessed_mask > && !kvm_memslots_have_rmaps(kvm); > } > > bool kvm_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range) > { > bool young =3D false; > > if (!range->arg.metadata->bitmap && kvm_memslots_have_rmaps(kvm)) > young =3D kvm_handle_gfn_range(kvm, range, kvm_age_rmap); > > if (tdp_mmu_enabled) > young |=3D kvm_tdp_mmu_age_gfn_range(kvm, range); > > return young; > } > > bool kvm_test_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range) > { > bool young =3D false; > > if (!range->arg.metadata->bitmap && kvm_memslots_have_rmaps(kvm)) > young =3D kvm_handle_gfn_range(kvm, range, kvm_test_age_r= map); > > if (tdp_mmu_enabled) > young |=3D kvm_tdp_mmu_test_age_gfn(kvm, range); > > return young; Yeah I think this is the right thing to do. Given your other suggestions (on patch 3), I think this will look something like this -- let me know if I've misunderstood something: bool check_rmap =3D !bitmap && kvm_memslot_have_rmaps(kvm); if (check_rmap) KVM_MMU_LOCK(kvm); rcu_read_lock(); // perhaps only do this when we don't take the MMU lock? if (check_rmap) kvm_handle_gfn_range(/* ... */ kvm_test_age_rmap) if (tdp_mmu_enabled) kvm_tdp_mmu_test_age_gfn() // modified to be RCU-safe rcu_read_unlock(); if (check_rmap) KVM_MMU_UNLOCK(kvm); > } > > Sure this could race with the creation of a shadow root but so can the > non-bitmap code.