Received: by 2002:ab2:7988:0:b0:1f4:b336:87c4 with SMTP id g8csp109976lqj; Thu, 11 Apr 2024 11:12:24 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCU8Ggh+gFbW5advbuXqSw9UQSidTboL+81bdWZFPecyXGlHjWfisJZbBytTXSg2ZHTJaUQZLOWFMHT3tjcwnI7bo9XptTWstpec+WuAdA== X-Google-Smtp-Source: AGHT+IFW1NunI2to5gbEFajaCDqSPNbQk9UNNVzdHKryldGrUagwuqpx0wmqn6cS9h41xdnngjFK X-Received: by 2002:a05:6870:4153:b0:22a:6d1:b0f3 with SMTP id r19-20020a056870415300b0022a06d1b0f3mr301797oad.15.1712859144286; Thu, 11 Apr 2024 11:12:24 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712859144; cv=pass; d=google.com; s=arc-20160816; b=xy/fauwVPsQDTq7v5taKfJLKUiC4GuGWi/ULl1/1k7FyLP0qcagTOOHmgE2A2HEboS PFc1gOkh2SB9i1sLVrB5M2DOTFR3isMQAr3OuGNAsgv4znsNSWoHuhbWJFIydBl8808I RMas+AdSRGukDx8M4Ollm4AQxm4IGLdLqQBUfUQM1t8X3J/22Cn3S2IrcyHT0nY08G42 MB1BPpucQKQMpSuEIWzSK3yVyy2Jz07I2baiSUC054uxaeI//9LkUalH4vr1dE3kG3Jr 5R2jZCt70ms6M3DuGSv/iJ1mKtwmwBmrYPnRbXQkFG7DiO29fc9dyCZvVXi6oPOzpPVD U8HA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=vEGKpy4oX/yIMuuLIeB2ya0zq8zO1o7RbrrJFAy2vfY=; fh=VlYavJkpvFEiwOZme2Vs4BV1Q1g+5Bh7caZhWXa1810=; b=ru7311JFxSHgGFgaDL3AFGVM0gDJ39wldjcMkv/DYPSU3bqZB2UJ34Po5ISxId+6NA pFOavPABECepMNFZHFRNYrjbNOEXrV/fIqzaLNpvx/jo0PJ9KPb3VK0sAEWg6B6jTeny aizFi0EpsyMAfRgFveT6XTDoMHywV7IJsGH+g2sIdSfVgdX0410gE/a1bAxYdgH8BrxF MpUunHadOTYugEKsxcJzldglVJKv/7lnZ3TF6n3Vocixxe2Xmj9uFdtdl9/f3QPdpyDd NCX71huUPPqD9FH72z9A0GcoypRAAs+mmaCihmCasOMsu9qknOL+NghGoUVzdygdBKH+ aSGw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=4T9TKkAC; 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-141381-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-141381-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 g10-20020ac870ca000000b004317517ccfasi1776020qtp.392.2024.04.11.11.12.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Apr 2024 11:12:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-141381-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=4T9TKkAC; 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-141381-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-141381-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 EBBEB1C21EB9 for ; Thu, 11 Apr 2024 18:12:23 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0CF711E2464; Thu, 11 Apr 2024 17:08:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="4T9TKkAC" Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) (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 9F0061E2453 for ; Thu, 11 Apr 2024 17:08:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.181 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712855332; cv=none; b=UFU8FsGuwq6Rxq7XlSCsoF6ZeMolK++ZrzI33rHruaS0k11iPe5b4iXRC0CIdXoI2hjZCMbZOjFSXpz24y2bzO3pVAit1+xW+bgsVVm7G0faivuyTtOR542I0oIwhH081pX0B1M5qghiodc5PTQkpP/50vmnAOtEbhfMaFGb+Ok= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712855332; c=relaxed/simple; bh=osOLzVNRJUx7/kwh0j8ShhoMeBYb2lNqWbY8A8z5eUU=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=uaKY+8B8T4aI2vVruTyZa17sTFTbXzC8K2U+/Q4om2WGNn66byym3Nah3BRhdCAk9zc5Ibiw7rAaWuMh5oV3/ICj9i7j5wv4WQLnYY7uPRmuDyzSiAWHEKi0I4ovA3RLFakj7C1TAdIwMoGFS51dXk3d2qDSWKB7tW2GEbkUo34= 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=4T9TKkAC; arc=none smtp.client-ip=209.85.214.181 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-pl1-f181.google.com with SMTP id d9443c01a7336-1e0bfc42783so725385ad.0 for ; Thu, 11 Apr 2024 10:08:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1712855330; x=1713460130; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=vEGKpy4oX/yIMuuLIeB2ya0zq8zO1o7RbrrJFAy2vfY=; b=4T9TKkACi8SqvCu3GueLC7cdH9GUN3Dab7eFh6Zi0N98y2OzCixry40P3WcP41y1CL DIJlsqu3t+oNU8KKZRflHTjeQkarwI45gz9qskHEMxVAooCfJXOwWm/LlD1L57tB6Y0x JXZvTDJyzm/XwBheHBxUkhKVRjwUyHWjKxQ0R9gARCx9YndsiAFBJXk3zIrsr7M7zkA1 xxE4+L3Hkl2fqqsj/qG1epi6xdGmObl7tsRqc6anq1b7T5e/rMNRWGmAJUOlgbf8o8Sl YzKZ41FgxUtx6wGph/K50SEDTMyhrM86m/BGRrcmfI0RV9bnXzf8nh9eiXkd3oJ3wud1 u1CQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712855330; x=1713460130; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=vEGKpy4oX/yIMuuLIeB2ya0zq8zO1o7RbrrJFAy2vfY=; b=nwst+/X8dNo2w+xgoUGkxPe5atQpWk5YWKUQwgR9yjuMMN5auy8X/VbVujDJ78fDGy ivlp6I7eIwnStMFGCL8Qgs8NTQDvDkedlDs1BJzTKEEofiZZv1sb0Dvy/AU87UFN5BDf ugnk/gQ8MgIIpESnZYXWJpKYsZCDFt67FqTdslKFQMUTsl86Q2I3rD22Q2TZCpxfS7AB X9VZ8fCrhNuKBBK8FD/68D48xY1xXpzJNfwK+KKkX3qLP4zLB+Y39GRcCGDP2r/cvSND r99h+H6Q+TWnJJAYmqStXiFKgkKcMh6aq439UnEdrDD0F24stIHwCYXYT3GmtdAC18NB HBdg== X-Forwarded-Encrypted: i=1; AJvYcCXa9hWCCshjD/KQ38vQ+h5q3aRmpfHMRuoCoU89anOtEBgbEPPd78v3j9r0yjNq/O6LCyKQoVS3R51QniWyBop2VcHAfFyxbNGMA+fq X-Gm-Message-State: AOJu0YwrPJQzK3bn37SS5arLXBalGQNu7PK601jyLApgvfmRGUNPTOZB JqhG4xEKjRK1VKHM4Rhxo6XsLbh1UEz7EJKL4RKIyghKQuzWm7YqWWwEni6R8w== X-Received: by 2002:a17:902:d303:b0:1e0:bae4:48f9 with SMTP id b3-20020a170902d30300b001e0bae448f9mr88599plc.32.1712855329667; Thu, 11 Apr 2024 10:08:49 -0700 (PDT) Received: from google.com (210.73.125.34.bc.googleusercontent.com. [34.125.73.210]) by smtp.gmail.com with ESMTPSA id a5-20020a1709027d8500b001e197cee600sm1413805plm.3.2024.04.11.10.08.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Apr 2024 10:08:49 -0700 (PDT) Date: Thu, 11 Apr 2024 10:08:44 -0700 From: David Matlack To: James Houghton 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 Subject: Re: [PATCH v3 5/7] KVM: x86: Participate in bitmap-based PTE aging Message-ID: References: <20240401232946.1837665-1-jthoughton@google.com> <20240401232946.1837665-6-jthoughton@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240401232946.1837665-6-jthoughton@google.com> 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/kvm_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 need 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_gfn_range *range) > { > bool young = 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 |= 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.