From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0AFC23F8DED; Fri, 27 Mar 2026 14:49:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774622955; cv=none; b=JNRPdpj7iv9pCg6a9Hb5yxHIh4O63RPIU+usbgS/npPBPfdzE3AFuEYFWE5ebHXn0ucNkroTEX1HVKUwTDR8Ud3lHjPGTkyygX+B2sbiXIPcHcocxFPpftvVp/b5e9Me6szYreJZeg5HJHyiGPMZyFRPS2y9zsvX+Ice5XrBdfo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774622955; c=relaxed/simple; bh=bGI/cAugrNnig4KQOyBbkhQzweiFkpeRdiDXpd6DHe8=; h=Date:Message-ID:From:To:Cc:Subject:In-Reply-To:References: MIME-Version:Content-Type; b=inFWNyrF7VV90u22F9BIA/iQgkeOVpvBBJ5pcrbB/9hvf32ZrXkYI1DbHc1C2FctvqML5q1kj8Jbl8fuJqtdC2KN1TKDBHjezhiSqmBk8iY3cWHyVhtmUhwERKMqJHIXOkLu34cTt3oHyssXplrOwRYEMSe22YfUj9AUj+HQrOs= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=C/jm2dmR; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="C/jm2dmR" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8BFC4C19423; Fri, 27 Mar 2026 14:49:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1774622954; bh=bGI/cAugrNnig4KQOyBbkhQzweiFkpeRdiDXpd6DHe8=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=C/jm2dmRjOh6fraMMQ4iojohVQmL/HM+bEnLFDCyIdSen6wG4z7QHm+U8z32tHi4k uC/DCDo9SJ6w0SJO0jjwSVJxpnbSX9Liw3QWjyMkKXpVpBDd4Ny9ZlTxs47xQP3F8F CiIGMA2I7wRfeh0QlMQPliOsWuKqjqFM8HYtwKyh6mQkr6gEROCPSvESPs5D3AslTZ CiiIMHUvftAXSSkLFt07sHBOHnto1QFryRr8+XIfoVUjGwlfwz6JsjjU9HMEUjHGGA 6WWmn81OJQetqULZ0KPFgXf6IU+KXaYgY8Ub6CN81OrfKf8gkjYR4BUL8zA7G+AIst nOKf8YcPuc0ZA== Received: from sofa.misterjones.org ([185.219.108.64] helo=goblin-girl.misterjones.org) by disco-boy.misterjones.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1w68Uq-00000006OdQ-0zc8; Fri, 27 Mar 2026 14:49:12 +0000 Date: Fri, 27 Mar 2026 14:49:11 +0000 Message-ID: <86ecl549aw.wl-maz@kernel.org> From: Marc Zyngier To: kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, kvm@vger.kernel.org Cc: Joey Gouly , Suzuki K Poulose , Oliver Upton , Zenghui Yu , Fuad Tabba , Will Deacon , Quentin Perret Subject: Re: [PATCH v2 21/30] KVM: arm64: Kill topup_memcache from kvm_s2_fault In-Reply-To: <20260327113618.4051534-22-maz@kernel.org> References: <20260327113618.4051534-1-maz@kernel.org> <20260327113618.4051534-22-maz@kernel.org> User-Agent: Wanderlust/2.15.9 (Almost Unreal) SEMI-EPG/1.14.7 (Harue) FLIM-LB/1.14.9 (=?UTF-8?B?R29qxY0=?=) APEL-LB/10.8 EasyPG/1.0.0 Emacs/30.1 (aarch64-unknown-linux-gnu) MULE/6.0 (HANACHIRUSATO) Precedence: bulk X-Mailing-List: kvmarm@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") Content-Type: text/plain; charset=US-ASCII X-SA-Exim-Connect-IP: 185.219.108.64 X-SA-Exim-Rcpt-To: kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, kvm@vger.kernel.org, joey.gouly@arm.com, suzuki.poulose@arm.com, oupton@kernel.org, yuzenghui@huawei.com, tabba@google.com, will@kernel.org, qperret@google.com X-SA-Exim-Mail-From: maz@kernel.org X-SA-Exim-Scanned: No (on disco-boy.misterjones.org); SAEximRunCond expanded to false On Fri, 27 Mar 2026 11:36:09 +0000, Marc Zyngier wrote: > > The topup_memcache field can be easily replaced by the equivalent > conditions, and the resulting code is not much worse. > > Tested-by: Fuad Tabba > Reviewed-by: Fuad Tabba > Reviewed-by: Suzuki K Poulose > Signed-off-by: Marc Zyngier > --- > arch/arm64/kvm/mmu.c | 12 ++++++------ > 1 file changed, 6 insertions(+), 6 deletions(-) > > diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c > index e8bda71e862b2..5b05caecdbd92 100644 > --- a/arch/arm64/kvm/mmu.c > +++ b/arch/arm64/kvm/mmu.c > @@ -1712,7 +1712,6 @@ static short kvm_s2_resolve_vma_size(const struct kvm_s2_fault_desc *s2fd, > > struct kvm_s2_fault { > bool writable; > - bool topup_memcache; > bool mte_allowed; > bool is_vma_cacheable; > bool s2_force_noncacheable; > @@ -1983,9 +1982,8 @@ static int user_mem_abort(const struct kvm_s2_fault_desc *s2fd) > .logging_active = logging_active, > .force_pte = logging_active, > .prot = KVM_PGTABLE_PROT_R, > - .topup_memcache = !perm_fault || (logging_active && kvm_is_write_fault(s2fd->vcpu)), > }; > - void *memcache; > + void *memcache = NULL; > int ret; > > /* > @@ -1994,9 +1992,11 @@ static int user_mem_abort(const struct kvm_s2_fault_desc *s2fd) > * only exception to this is when dirty logging is enabled at runtime > * and a write fault needs to collapse a block entry into a table. > */ > - ret = prepare_mmu_memcache(s2fd->vcpu, fault.topup_memcache, &memcache); > - if (ret) > - return ret; > + if (!perm_fault || (logging_active && kvm_is_write_fault(s2fd->vcpu))) { > + ret = prepare_mmu_memcache(s2fd->vcpu, true, &memcache); > + if (ret) > + return ret; > + } > > /* > * Let's check if we will get back a huge page backed by hugetlbfs, or Sashiko has spotted [1] an interesting corner case here, which is that the original code always initialises memcache to its correct value, while we now only do it in a limited number of cases. I'm proposing to restore the original behaviour by folding the following change into this patch, splitting the retrieval of the memcache pointer from the top-up and avoiding the ugly pointer indirection: diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c index 1fe7182be45ac..03e1f389339c7 100644 --- a/arch/arm64/kvm/mmu.c +++ b/arch/arm64/kvm/mmu.c @@ -1513,25 +1513,22 @@ static bool kvm_vma_is_cacheable(struct vm_area_struct *vma) } } -static int prepare_mmu_memcache(struct kvm_vcpu *vcpu, bool topup_memcache, - void **memcache) +static void *get_mmu_memcache(struct kvm_vcpu *vcpu) { - int min_pages; - if (!is_protected_kvm_enabled()) - *memcache = &vcpu->arch.mmu_page_cache; + return &vcpu->arch.mmu_page_cache; else - *memcache = &vcpu->arch.pkvm_memcache; - - if (!topup_memcache) - return 0; + return &vcpu->arch.pkvm_memcache; +} - min_pages = kvm_mmu_cache_min_pages(vcpu->arch.hw_mmu); +static int topup_mmu_memcache(struct kvm_vcpu *vcpu, void *memcache) +{ + int min_pages = kvm_mmu_cache_min_pages(vcpu->arch.hw_mmu); if (!is_protected_kvm_enabled()) - return kvm_mmu_topup_memory_cache(*memcache, min_pages); + return kvm_mmu_topup_memory_cache(memcache, min_pages); - return topup_hyp_memcache(*memcache, min_pages); + return topup_hyp_memcache(memcache, min_pages); } /* @@ -1589,7 +1586,8 @@ static int gmem_abort(const struct kvm_s2_fault_desc *s2fd) gfn_t gfn; int ret; - ret = prepare_mmu_memcache(s2fd->vcpu, true, &memcache); + memcache = get_mmu_memcache(s2fd->vcpu); + ret = topup_mmu_memcache(s2fd->vcpu, memcache); if (ret) return ret; @@ -1993,7 +1991,7 @@ static int user_mem_abort(const struct kvm_s2_fault_desc *s2fd) bool perm_fault = kvm_vcpu_trap_is_permission_fault(s2fd->vcpu); struct kvm_s2_fault_vma_info s2vi = {}; enum kvm_pgtable_prot prot; - void *memcache = NULL; + void *memcache; int ret; /* @@ -2002,9 +2000,10 @@ static int user_mem_abort(const struct kvm_s2_fault_desc *s2fd) * only exception to this is when dirty logging is enabled at runtime * and a write fault needs to collapse a block entry into a table. */ + memcache = get_mmu_memcache(s2fd->vcpu); if (!perm_fault || (memslot_is_logging(s2fd->memslot) && kvm_is_write_fault(s2fd->vcpu))) { - ret = prepare_mmu_memcache(s2fd->vcpu, true, &memcache); + ret = topup_mmu_memcache(s2fd->vcpu, memcache); if (ret) return ret; } The bot has also pointed out a couple of cases where memcache and permission faults interact badly. I'll look into them separately, as they predate this rework. Thanks, M. [1] https://sashiko.dev/#/patchset/20260327113618.4051534-1-maz%40kernel.org?patch=12134 -- Without deviation from the norm, progress is not possible.