All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Jia He" <justin.he@arm.com>
Cc: kvmarm@lists.linux.dev, "Oliver Upton" <oupton@kernel.org>,
	"Marc Zyngier" <maz@kernel.org>
Subject: Re: [PATCH] KVM: arm64: nv: Allow the L1 VNCR page in cacheable PFNMAP memory
Date: Tue, 24 Nov 2026 03:08:28 +0000	[thread overview]
Message-ID: <sashiko-outbox-230439@kernel.org> (raw)
In-Reply-To: <20261124025336.4107575-1-justin.he@arm.com>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] KVM: arm64: nv: vncr_pfn_is_cacheable() rejects read-only
  memslots
It also noticed 1 related pre-existing issue this patch does not
introduce:
- [High] KVM: arm64: nv: unhandled -EPERM in kvm_handle_vncr_abort() [1]
--

commit 9fdd9f2ca0f89e14eb8d6921a8424af26dfde3e6
Author: Jia He <justin.he@arm.com>

KVM: arm64: nv: Allow the L1 VNCR page in cacheable PFNMAP memory

This commit allows the L1 VNCR page to reside in a cacheable VM_PFNMAP or
VM_MIXEDMAP VMA when the system supports cacheable PFNMAP mappings at
stage-2, matching the stage-2 fault path.

> diff --git a/arch/arm64/kvm/nested.c b/arch/arm64/kvm/nested.c
> index 78d5b18dbd2d6..85b97a888d71d 100644
> --- a/arch/arm64/kvm/nested.c
> +++ b/arch/arm64/kvm/nested.c
> @@ -1561,6 +1561,36 @@ static u64 read_vncr_el2(struct kvm_vcpu *vcpu)
>  	return (u64)sign_extend64(__vcpu_sys_reg(vcpu, VNCR_EL2), 48);
>  }
>  
> +/*
> + * Allow a non-memory PFN only if it comes from a cacheable VM_PFNMAP or
> + * VM_MIXEDMAP VMA, as for a stage-2 mapping. The VMA is looked up after the
> + * PFN has been faulted in: the caller must check mmu_invalidate_retry()
> + * against a sequence count sampled before the fault-in.
> + */
> +static bool vncr_pfn_is_cacheable(struct kvm_memory_slot *memslot, gfn_t gfn,
> +				  kvm_pfn_t pfn)
> +{
> +	struct vm_area_struct *vma;
> +	unsigned long hva;
> +	bool ret = false;
> +
> +	if (pfn_is_map_memory(pfn))
> +		return true;
> +
> +	if (!kvm_supports_cacheable_pfnmap())
> +		return false;
> +
> +	hva = gfn_to_hva_memslot(memslot, gfn);
> +
> +	mmap_read_lock(current->mm);
> +	vma = vma_lookup(current->mm, hva);
> +	if (vma && (vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP)))
> +		ret = kvm_vma_is_cacheable(vma);
> +	mmap_read_unlock(current->mm);
> +
> +	return ret;
> +}

[Severity: Medium]
Does gfn_to_hva_memslot() reject a KVM_MEM_READONLY memslot here?

gfn_to_hva_memslot() assumes a write access and returns KVM_HVA_ERR_RO_BAD
when the memslot is read-only.  On a read access to an L1 VNCR page in a
read-only memslot backed by a cacheable VM_PFNMAP or VM_MIXEDMAP VMA,
__kvm_faultin_pfn() faults in the PFN with writable = false, but
vma_lookup() then looks up KVM_HVA_ERR_RO_BAD and returns NULL, causing
kvm_translate_vncr() to inject an SEA instead of installing a read-only
VNCR mapping as supported since 2684e02bac41 ("KVM: arm64: nv: Respect
read-only PFN when mapping L1 VNCR").  Using __gfn_to_hva_memslot() (or
gfn_to_hva_memslot_prot() as in kvm_handle_guest_abort()) would avoid
failing on read-only memslots.

> @@ -1633,12 +1663,7 @@ static int kvm_translate_vncr(struct kvm_vcpu *vcpu, bool *is_gmem)
>  		writable = !(memslot->flags & KVM_MEM_READONLY);
>  	}
>  
> -	/*
> -	 * FIXME: This check is too restrictive as KVM allows cacheable memory
> -	 * attributes for PFNMAP VMAs that have cacheable attributes in host
> -	 * stage-1.
> -	 */
> -	if (!pfn_is_map_memory(pfn)) {
> +	if (!vncr_pfn_is_cacheable(memslot, gfn, pfn)) {
>  		kvm_release_faultin_page(vcpu->kvm, page, true, false);
>  		fail_s1_walk(&vt->wr, ESR_ELx_FSC_EXTABT, false);
>  		return -EINVAL;

[Severity: High]
This problem wasn't introduced by this patch, but can
kvm_handle_vncr_abort() drop a pending SEA and loop forever when
kvm_translate_vncr() returns -EPERM [1]?

When hardware Access Flag updates (TCR_EL2.HA) are enabled for guest
EL2&0 and a stage-1 descriptor with PTE_AF == 0 resides in a read-only
memslot, walk_s1() -> kvm_swap_s1_desc() -> __kvm_at_swap_desc() fails
with -EPERM and records ESR_ELx_FSC_SEA_TTW(level) in vt->wr.fst.
Because the switch statement in kvm_handle_vncr_abort() does not include
-EPERM in its fault-injection cases:

arch/arm64/kvm/nested.c:kvm_handle_vncr_abort() {
	...
	case -EFAULT:
	case -EIO:
	case -EHWPOISON:
		if (is_gmem)
			return 0;
		fallthrough;
	case -EINVAL:
	case -ENOENT:
	case -EACCES:
		/*
		 * Translation failed, inject the corresponding
		 * exception back to EL2.
		 */
		esr &= ~ESR_ELx_FSC;
		esr |= FIELD_PREP(ESR_ELx_FSC, vt->wr.fst);

		kvm_inject_nested_sync(vcpu, esr);
		break;
	...
}

KVM returns 1 without injecting the abort or mapping the VNCR page,
trapping the vCPU in an infinite VNCR abort loop.

[1] https://sashiko.dev/#/bug/linux-5a55db9b-f840-4142-8a1e-110e2882e8fa

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261124025336.4107575-1-justin.he@arm.com?part=1

  reply	other threads:[~2026-11-24  3:08 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24  2:53 [PATCH] KVM: arm64: nv: Allow the L1 VNCR page in cacheable PFNMAP memory Jia He
2026-11-24  3:08 ` sashiko-bot [this message]
2026-11-24  4:42   ` Justin He

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=sashiko-outbox-230439@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=justin.he@arm.com \
    --cc=kvmarm@lists.linux.dev \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.