Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Lorenzo Stoakes (ARM)" <ljs@kernel.org>
Cc: "Oliver Upton" <oupton@kernel.org>,
	kvmarm@lists.linux.dev, "Marc Zyngier" <maz@kernel.org>,
	kvm@vger.kernel.org
Subject: Re: [PATCH v3 07/14] KVM: arm64: Size the stage-2 memcache from the fault MMU
Date: Tue, 22 Sep 2026 14:31:56 +0000	[thread overview]
Message-ID: <20260922143157.0E84B1F000FF@smtp.kernel.org> (raw)
In-Reply-To: <20260922-kvm-arm-prefault-v3-7-787bd3bc7e3f@kernel.org>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] arm64: kvm: spurious allocation in kvm_s2_fault_map during prefault
--

commit 9bf98edb6fb89edb3f2ce746e5754cac7f11bfe0
Author: Lorenzo Stoakes (ARM) <ljs@kernel.org>
KVM: arm64: Size the stage-2 memcache from the fault MMU

This commit modifies topup_mmu_memcache() to use the target MMU from s2fd->mmu
rather than the active vCPU MMU. This is a preparatory step for stage-2
pre-faulting, which needs to target the canonical MMU regardless of the vCPU's
context.

> diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c
> index 5c429065a4feb..221ea069f9bb6 100644
> --- a/arch/arm64/kvm/mmu.c
> +++ b/arch/arm64/kvm/mmu.c
> @@ -2251,7 +2251,7 @@ static int user_mem_abort(const struct kvm_s2_fault_desc *s2fd,
>  	memcache = get_mmu_memcache(s2fd->vcpu);
>  	if (!perm_fault || memslot_is_logging(s2fd->memslot) ||
>  	    is_protected_kvm_enabled()) {
> -		ret = topup_mmu_memcache(s2fd->vcpu, memcache);
> +		ret = topup_mmu_memcache(s2fd->mmu, memcache);
>  		if (ret)
>  			return ret;
>  	}

[Severity: Medium]
Does this decoupling of s2fd->mmu and vcpu->arch.hw_mmu cause a spurious
allocation downstream in kvm_s2_fault_map()?

When pre-faulting is executed on a nested vCPU, s2fd->mmu is set to the
non-nested canonical MMU, while vcpu->arch.hw_mmu remains nested. In
kvm_s2_fault_map(), the allocation of the mapping struct is gated on the active
vCPU MMU:

arch/arm64/kvm/mmu.c:kvm_s2_fault_map() {
	if (kvm_is_nested_s2_mmu(kvm, s2fd->vcpu->arch.hw_mmu)) {
		mapping = kmalloc_obj(struct kvm_guest_s2_mapping,
				      GFP_KERNEL_ACCOUNT);
		if (!mapping) {
			kvm_release_page_unused(s2vi->page);
			return -ENOMEM;
		}
	}
}

However, the consumption and tracking of this mapping relies on pgt->mmu, which
resolves to the target s2fd->mmu:

arch/arm64/kvm/mmu.c:kvm_s2_fault_map() {
		if ((ret == 0 || ret == -EAGAIN) && kvm_is_nested_s2_mmu(kvm, pgt->mmu)) {
			kvm_record_guest_s2_mapping(pgt->mmu, canonical_ipa,
						    gfn_to_gpa(gfn), mapping_size, mapping);
			mapping = NULL;
		}
}

Because kvm_is_nested_s2_mmu() for pgt->mmu evaluates to false during a
pre-fault, the allocation is never used and is subsequently safely freed at the
end of the function.

Could this lead to an unnecessary allocation in the ioctl path that might
spuriously fail and return -ENOMEM to userspace?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260922-kvm-arm-prefault-v3-0-787bd3bc7e3f@kernel.org?part=7

  reply	other threads:[~2026-09-22 14:31 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-22 14:17 [PATCH v3 00/14] KVM: arm64: Add KVM_PRE_FAULT_MEMORY support Lorenzo Stoakes (ARM)
2026-09-22 14:17 ` [PATCH v3 01/14] KVM: Allow architectures to disallow pre-fault Lorenzo Stoakes (ARM)
2026-09-22 16:49   ` Oliver Upton
2026-09-22 17:23     ` Sean Christopherson
2026-09-22 17:30       ` Lorenzo Stoakes (ARM)
2026-09-22 17:36         ` Sean Christopherson
2026-09-22 18:01           ` Lorenzo Stoakes (ARM)
2026-09-22 18:40             ` Sean Christopherson
2026-09-22 18:52               ` Lorenzo Stoakes (ARM)
2026-09-22 18:07           ` Oliver Upton
2026-09-22 18:35             ` Lorenzo Stoakes (ARM)
2026-09-22 18:46               ` Sean Christopherson
2026-09-22 18:54                 ` Lorenzo Stoakes (ARM)
2026-09-23 10:54             ` Fuad Tabba
2026-09-23 13:26               ` Lorenzo Stoakes (ARM)
2026-09-22 17:31     ` Lorenzo Stoakes (ARM)
2026-09-22 14:17 ` [PATCH v3 02/14] arm64: Add ESR fault helpers Lorenzo Stoakes (ARM)
2026-09-22 17:00   ` Oliver Upton
2026-09-22 17:45     ` Lorenzo Stoakes (ARM)
2026-09-22 18:13       ` Oliver Upton
2026-09-22 14:17 ` [PATCH v3 03/14] KVM: arm64: Use ESR helpers in guest abort handling Lorenzo Stoakes (ARM)
2026-09-22 14:17 ` [PATCH v3 04/14] KVM: arm64: Propagate and use esr in s2fd when handling guest aborts Lorenzo Stoakes (ARM)
2026-09-22 14:17 ` [PATCH v3 05/14] KVM: arm64: Propagate and use mmu " Lorenzo Stoakes (ARM)
2026-09-22 14:18 ` [PATCH v3 06/14] KVM: arm64: Propagate and use kvm_s2_fault_result on S2 fault Lorenzo Stoakes (ARM)
2026-09-22 14:18 ` [PATCH v3 07/14] KVM: arm64: Size the stage-2 memcache from the fault MMU Lorenzo Stoakes (ARM)
2026-09-22 14:31   ` sashiko-bot [this message]
2026-09-23 11:00     ` Fuad Tabba
2026-09-23 13:30       ` Lorenzo Stoakes (ARM)
2026-09-22 14:18 ` [PATCH v3 08/14] KVM: arm64: Propagate EHWPOISON in kvm_s2_fault_pin_pfn() Lorenzo Stoakes (ARM)
2026-09-23 11:05   ` Fuad Tabba
2026-09-22 14:18 ` [PATCH v3 09/14] KVM: arm64: Pass walk flags to kvm_pgtable_get_leaf() Lorenzo Stoakes (ARM)
2026-09-23 11:07   ` Fuad Tabba
2026-09-22 14:18 ` [PATCH v3 10/14] KVM: arm64: Implement KVM_PRE_FAULT_MEMORY Lorenzo Stoakes (ARM)
2026-09-23 11:18   ` Fuad Tabba
2026-09-23 13:28     ` Lorenzo Stoakes (ARM)
2026-09-23 13:32       ` Lorenzo Stoakes (ARM)
2026-09-23 13:33         ` Lorenzo Stoakes (ARM)
2026-09-22 14:18 ` [PATCH v3 11/14] Documentation: KVM: document arm64 KVM_PRE_FAULT_MEMORY Lorenzo Stoakes (ARM)
2026-09-22 14:18 ` [PATCH v3 12/14] KVM: selftests: Enable pre_fault_memory_test for arm64 Lorenzo Stoakes (ARM)
2026-09-22 14:18 ` [PATCH v3 13/14] KVM: selftests: Add option for different backing in pre-fault tests Lorenzo Stoakes (ARM)
2026-09-22 14:18 ` [PATCH v3 14/14] KVM: selftests: Add nested pre-fault test for arm64 Lorenzo Stoakes (ARM)
2026-09-22 20:42 ` [PATCH v3 00/14] KVM: arm64: Add KVM_PRE_FAULT_MEMORY support Oliver Upton
2026-09-23 10:03 ` Fuad Tabba

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260922143157.0E84B1F000FF@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=kvmarm@lists.linux.dev \
    --cc=ljs@kernel.org \
    --cc=maz@kernel.org \
    --cc=oupton@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox