Received: by 2002:ab2:6c55:0:b0:1fd:c486:4f03 with SMTP id v21csp520895lqp; Wed, 12 Jun 2024 08:23:05 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXLM98N9KQyvlQgI2QtkRTApagdG7scMiyALtQHC/2nLRl36SrdE/Nbprpe4FgPhwU52ujdhigzx/BTrnLrsqO6LLGxaQVYPLFk0yGwbw== X-Google-Smtp-Source: AGHT+IGOfnVZrba/MEz57puEKYQ/buhpLP20nQn99qGjwEttx0h0m7fCZytdORx8pmr0TY9GM3BR X-Received: by 2002:a05:6808:1207:b0:3d2:21a5:d3bd with SMTP id 5614622812f47-3d23e11f8d4mr2074734b6e.52.1718205784787; Wed, 12 Jun 2024 08:23:04 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1718205784; cv=pass; d=google.com; s=arc-20160816; b=OG9+hnl8gce6w5RCa2Wfi9KTe/+Cl0JLqgYlgu0fnNGi5RFPGAsmOZmaeWwXwXYR8e G7e14mGSE7jZAXEufHMLH7j/mjVgOtzuoRqwhvVBPJ6Y/MbEi0hfLHg/smEeKsLKvOnI RPTKOsk6b9NMEkYpVyEGPNsBYgktIzwThfKDgnVQnaItyBzSDA9SZKZjrpHOU6ZKx7M/ jSaIH28A3TXR9JQXIhwGezAxNREUFfB/5ovSxy+/do9SG+VduQPtEXP8pgdI7QlYhl5b jNpNyU483J/R1op1N1EHra7n4qww6W4JF50/mjPL/PNTLgo5VFLiHhafvVZKkcoQFaTc rBEg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=R0BqD6y0/VOUpbW8YRHgmGoMOpV6WFqVjQFCsKaKlao=; fh=6/ba9ETztxEfscgS4f5Pu0ng9I2xDfHQGOqMpf16dyo=; b=NlcBHCo7aZLr9mjsDr/vuCrdjC37RCYKa5upQrf0ugGmnED4EY/JkLEu3gyAXEHsV7 IbmNDwbx7tgD7+W/S28OnnHt9u9+JKCtk77qFirrJRCrD6ZWcfM2usJ6qtZlShPTmBne ZLVTZxzFO0h8ioIkkWQybo4tbCmkowKS7I6UNkPZm89aWuxYWbkGGjipO5InQlaavm/c a6xcUkgFeV03PE2DTEMqv5C2eJpeBQDHl75GgNMt2ir5uuo/H1dU5OX43iU8fwvKUWZX hULk1IR3iKbxrBykKleNmsVt4m6PJgjN54M4Tg+Xmmxag7+Lc6t67ut7he/Yi1D8ZsBf QPjw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=AikIDcOP; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-211781-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-211781-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 a1e0cc1a2514c-80b8313fe8fsi2238726241.28.2024.06.12.08.23.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Jun 2024 08:23:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-211781-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=AikIDcOP; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-211781-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-211781-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 69D0A1C217AB for ; Wed, 12 Jun 2024 15:23:04 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2DE9017E442; Wed, 12 Jun 2024 15:22:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="AikIDcOP" Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (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 68EDD17BB23 for ; Wed, 12 Jun 2024 15:22:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718205774; cv=none; b=DmZnyiUvcT/t5cXj5SfenZ/wPHdzM3jOrgaXDyjpceRbdTXbaLwCwEfqi/b2AOzWmCMd8/2hQp7N55JDhMiYJsO1jL+QPpOFk4ZHvBCs1AIZiiAXsaeadVXrOMHqPH/ptSrvrPPmzqZDwJE8PTDqDSsrsyUsIiXygkq6g0T5tpI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718205774; c=relaxed/simple; bh=tzPtwpV4j1pdrwYCvIYwV29IQG2d6cN6yQ3kAiIoajg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=HkJdHfEX7WNp7Oc70rPPR6B4n/+o2dio1TaH5y3UWB0jat5xThHy2z49LBn0jGoGJsWAkr9nLFBlScS41SnsgfhxQFy4z9E3wnFJOx1lrOLkdHLBIhCJM+0Ov8Uu3pP1E9oQ0mTENSTCgP7nPL2cd//3FZP0SROZhqInxYw9fXM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=AikIDcOP; arc=none smtp.client-ip=209.85.128.201 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=flex--seanjc.bounces.google.com Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-62f9fc2cbf6so17409417b3.2 for ; Wed, 12 Jun 2024 08:22:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1718205771; x=1718810571; darn=vger.kernel.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=R0BqD6y0/VOUpbW8YRHgmGoMOpV6WFqVjQFCsKaKlao=; b=AikIDcOP4Vnh4tezmavbFD+CmFuh8QjYsWGaAfFTgJDLHiaq+z3B2sp9P+0kCZ2xy9 3tCO+xKVYFd0eXKthsvImF+ABQRWdVrt3F2FCpYz70eqr2ovLU4XHkD3cYFyDaSL4wyg QuGMwY1x3vEpT2QXMmKX5dsUX9QVEKYqEyHoB3LKnUngbGqAGJ/yBQSuN05/+h9gVQeR rQHi4jDXBf/M6JagQHlnQSr7chGNL+UQB3xrQ29/CQdttbRUQDX7jFghPWBM8COQf3o3 QNtqfkvnMYPTZptgrGC2bDmzHN/AJqrfKjFJS0veAgU+dBtIHbD80gBP5YwmWiCss7Gr fAEw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718205771; x=1718810571; 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=R0BqD6y0/VOUpbW8YRHgmGoMOpV6WFqVjQFCsKaKlao=; b=Rviy9Xz/ofvRJPCHXYQxj6wmZa0OOp/llRxvlxywI6LkJVvhZ9+/2tZgD9tAp4Zfyl 7HxjreLBD3h7mwp2HD3imtX0UtQ3bl6q3yYK+Q/y3Dg2g+5c/YmZoqLtB1tGyJUL+yLt icb6hd0OjuFNPMWGsItXnVG5xNhqCnnJKTown+Ha797r4c+MVBcODGsO89n6yHG+V5h1 RNaH0bnU/bPVOzJdfR/gRxVhAxHUl5pe9hvryObx+0IV6ufdz9CCCpqWDywf1tnp1Ri2 OFETt8E2iAlJO9YxyCFlNhGTwHC0uazXWg56wcE7mxJXgnkItBKGQfr8xsMChI6Odqmk dXlQ== X-Forwarded-Encrypted: i=1; AJvYcCU1l0leb1cz+vmlr0pP0CSZjDj82C4PkTEy3biSwvXhOJgCJh6seLygKrflwJGSECD+Lpf81q4up/Kh1SwTu+1ZvX/PEXC6D33O/kch X-Gm-Message-State: AOJu0YwdbJfiGLwIGl0e2DqeWDdFYl7PCd5j1wxxbnOP6LzXfTacGVQU ikowERZ7vePkM26UE+qrfi5Fk2ltoOSbtthVwywmrZr22Td/UQ0YPk7h2q4aBh6wzINUGdOrmw8 Jmw== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:690c:660c:b0:62f:f535:f2c with SMTP id 00721157ae682-62ff53512f5mr5078687b3.2.1718205771477; Wed, 12 Jun 2024 08:22:51 -0700 (PDT) Date: Wed, 12 Jun 2024 08:22:49 -0700 In-Reply-To: <20240419112432.GB2972@willie-the-truck> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240405115815.3226315-1-pbonzini@redhat.com> <20240405115815.3226315-2-pbonzini@redhat.com> <20240412104408.GA27645@willie-the-truck> <86jzl2sovz.wl-maz@kernel.org> <86h6g5si0m.wl-maz@kernel.org> <20240418141932.GA1855@willie-the-truck> <20240419112432.GB2972@willie-the-truck> Message-ID: Subject: Re: [PATCH 1/4] KVM: delete .change_pte MMU notifier callback From: Sean Christopherson To: Will Deacon Cc: Marc Zyngier , Paolo Bonzini , linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Oliver Upton , Tianrui Zhao , Bibo Mao , Thomas Bogendoerfer , Nicholas Piggin , Anup Patel , Atish Patra , Andrew Morton , David Hildenbrand , linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, loongarch@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Content-Type: text/plain; charset="us-ascii" On Fri, Apr 19, 2024, Will Deacon wrote: > On Thu, Apr 18, 2024 at 12:53:26PM -0700, Sean Christopherson wrote: > > On Thu, Apr 18, 2024, Will Deacon wrote: > > > > I assume the idea would be to let arch code do single-page invalidations of > > > > stage-2 entries for each gfn? > > > > > > Right, as it's the only code which knows which ptes actually ended up > > > being aged. > > > > > > > Unless I'm having a brain fart, x86 can't make use of that functionality. Intel > > > > doesn't provide any way to do targeted invalidation of stage-2 mappings. AMD > > > > provides an instruction to do broadcast invalidations, but it takes a virtual > > > > address, i.e. a stage-1 address. I can't tell if it's a host virtual address or > > > > a guest virtual address, but it's a moot point because KVM doen't have the guest > > > > virtual address, and if it's a host virtual address, there would need to be valid > > > > mappings in the host page tables for it to work, which KVM can't guarantee. > > > > > > Ah, so it sounds like it would need to be an arch opt-in then. > > > > Even if x86 (or some other arch code) could use the precise tracking, I think it > > would make sense to have the behavior be arch specific. Adding infrastructure > > to get information from arch code, only to turn around and give it back to arch > > code would be odd. > > Sorry, yes, that's what I had in mind. Basically, a way for the arch code > to say "I've handled the TLBI, don't worry about it." > > > Unless arm64 can't do the invalidation immediately after aging the stage-2 PTE, > > the best/easiest solution would be to let arm64 opt out of the common TLB flush > > when a SPTE is made young. > > > > With the range-based flushing bundled in, this? > > > > --- > > include/linux/kvm_host.h | 2 ++ > > virt/kvm/kvm_main.c | 40 +++++++++++++++++++++++++--------------- > > 2 files changed, 27 insertions(+), 15 deletions(-) > > > > diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h > > index afbc99264ffa..8fe5f5e16919 100644 > > --- a/include/linux/kvm_host.h > > +++ b/include/linux/kvm_host.h > > @@ -2010,6 +2010,8 @@ extern const struct kvm_stats_header kvm_vcpu_stats_header; > > extern const struct _kvm_stats_desc kvm_vcpu_stats_desc[]; > > > > #ifdef CONFIG_KVM_GENERIC_MMU_NOTIFIER > > +int kvm_arch_flush_tlb_if_young(void); > > + > > static inline int mmu_invalidate_retry(struct kvm *kvm, unsigned long mmu_seq) > > { > > if (unlikely(kvm->mmu_invalidate_in_progress)) > > diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c > > index 38b498669ef9..5ebef8ef239c 100644 > > --- a/virt/kvm/kvm_main.c > > +++ b/virt/kvm/kvm_main.c > > @@ -595,6 +595,11 @@ static void kvm_null_fn(void) > > } > > #define IS_KVM_NULL_FN(fn) ((fn) == (void *)kvm_null_fn) > > > > +int __weak kvm_arch_flush_tlb_if_young(void) > > +{ > > + return true; > > +} > > I tend to find __weak functions a little ugly, but I think the gist of the > diff looks good to me. Thanks for putting it together! Circling back to this, I don't think we should pursue this specific tweak, at least not without hard data for a concrete use case. The clear_flush_young() hook is the only callback that overloads the return value, e.g. for invalidate_range_start(), arch code can simply return false if the flush has already been performed. And clear_flush_young() _always_ operates on a single page, i.e. the range will only ever cover a single page in the primary MMU. It's obviously possible that KVM's MMU has mapped a transparent hugepage using multiple smaller pages, but that should be relatively uncommon, and probably not worth optimizing for.