From mboxrd@z Thu Jan 1 00:00:00 1970 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 12B3BC27C53 for ; Wed, 12 Jun 2024 15:23:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To:From: Subject:Message-ID:References:Mime-Version:In-Reply-To:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=R0BqD6y0/VOUpbW8YRHgmGoMOpV6WFqVjQFCsKaKlao=; b=VRvBXK+6l/7AXr6Zry9vjhoitz FyZEKB2i/n9pa0IN5yalVyMdGsPveBvWkJNPUsBfJLNZ6X35njMt3FkYPMVuTFtSR3VMM8zhNpQz8 6P53aQe+eEBgZ2euZb5wm141OLOnNpmuXXWuPMInPmjuuheSAcd/T2TOcWnvi7FhLhe9DpoAfcQEE cQI0NY9gQcnsOQMuzZ1OvV3SsAadyT+485c+WtVwp9B5PlOnk1y0I/M3Gnvnwa10Aeoq845C5aRuo 9G1CY9GPCRFT+lRAOMBI09I11ff14eBHmQUC6J1sCBJIHiQ8kSXhAHPtvOaObr6LbpoMRidr9jAmD 1Woof1zw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sHPoR-0000000D9Up-3LAa; Wed, 12 Jun 2024 15:22:59 +0000 Received: from mail-yw1-x114a.google.com ([2607:f8b0:4864:20::114a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sHPoO-0000000D9S8-1kGF for linux-arm-kernel@lists.infradead.org; Wed, 12 Jun 2024 15:22:57 +0000 Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-6302f943413so9760007b3.1 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=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=R0BqD6y0/VOUpbW8YRHgmGoMOpV6WFqVjQFCsKaKlao=; b=Td/lwZu6IdGI+W4FgbpDzz8ZYtPCoAyG90W8sWco8F5W59UwpoWL+r2O/qSxAvOG04 uehaP3GGEkbIQmLkX9r5p8uloHbfjY6G1Dxt+djH1PiIyKMzofBHhm0v6cLA/bGzFNMn 22MpEiOK/B1cAqFzudKE0QVHgSPT5pngLPXroCeDJ9ykn5UCbCIHql3LCjNic8aQTy3Q N5MgeB5OaCCbRG5s1v5iKKhq0VDA+k3ve6+M/8sqUzoyBPsEvZk1OFKjeedr2THrODMy NOHiCkiuS5k5YptFitNbyRi7J8r+MqWpK4foSZ1Q30b4kLRtys9eQLP7U51PPHYqeYq7 /Yng== 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=hqkKIPU7Dafl3gqiGIb8L/AppKhyRox4Fzrf1OySSnKwxNaGkJm4lSg5S5+FHHY7I4 SkfWq/yV8K2QY9S7Mxvdn5ywmUYclp/3rQGtZv4M7ol0VoYaP7BeKbYQH/kqBdX1zn9E VIMWqwpWaLgNvtO0eFAihrinYk2BWQuJ11v/W7GbO1/Z6qJ0GHww3oNEXH8EG+smZxFl XZsPAwcNZTZBA+yScCjX4SBiy+/gWQquhNK0NOXWEaLpTCyp+7nZGnyKy5uLS3efUORV 6YJOs1qILyVjjlosw9Nz2hOrbh9AbY6ifS8FJy1fwVNAngFHbR4hKg5k2Dv4FEO0J/aH P81w== X-Forwarded-Encrypted: i=1; AJvYcCUMn8H/SlOvUsIR953N23lr/OupUGzTVlwousaMPhHnd1sGydu48CABKIdolidggCnPBSL8TqUV1IOD5LU6ukhs1HZwDnHXQHtCfn5ry4kyMB978H8= X-Gm-Message-State: AOJu0YxA0srtsLHvaTCm/570BzZlb+Cvs4P7vFV24gFpfPSNN4iGFyvV G7sCJVVOaiPdSI2FmhoMoS/8x4kHIBqU7qRe9aMUZeTdEJScwWDBQ7tN3UXm+83lsHdlYXXWOyG FMg== X-Google-Smtp-Source: AGHT+IGTqFQO1W9uWgNfx86uFJoJR25380gE+MX0EH8aoPTaRHgwGtgxURTOkAOuuR3wa2vaOk4BcpKiUBQ= 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> 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" X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240612_082256_464521_39A3E908 X-CRM114-Status: GOOD ( 39.55 ) 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 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.