All of lore.kernel.org
 help / color / mirror / Atom feed
From: Vincent Donnefort <vdonnefort@google.com>
To: sashiko-reviews@lists.linux.dev
Cc: kvmarm@lists.linux.dev, Oliver Upton <oupton@kernel.org>,
	Marc Zyngier <maz@kernel.org>
Subject: Re: [PATCH v6 24/24] KVM: arm64: Stage-2 huge mappings for protected VMs
Date: Fri, 4 Dec 2026 14:50:28 +0000	[thread overview]
Message-ID: <axLTNBaoLvHkJ7JN@google.com> (raw)
In-Reply-To: <sashiko-outbox-248504@kernel.org>

On Thu, Dec 03, 2026 at 07:28:20PM +0000, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 4 potential
> issues to consider:
> - [High] KVM: arm64: unaligned PMD mapping breaks pKVM under 2-level
>   pagetables
> - [Medium] KVM: arm64: missing PAGE_SIZE fallback on -EPERM in
>   protected_pkvm_mem_abort()
> - [Medium] KVM: arm64: PMD hyp_poison_range() holds EL2 host_mmu.lock
>   with IRQs off
> - [Medium] KVM: arm64: locked_vm over-charge and lock churn in
>   protected_pkvm_mem_abort()
> --
> 
> commit 9c8f56cac08156f554f633ca7346cbaaeadec9a8
> Author: Vincent Donnefort <vdonnefort@google.com>
> 
> KVM: arm64: Stage-2 huge mappings for protected VMs
> 
> This commit enables PMD_SIZE stage-2 block mappings for protected VMs
> under pKVM when the host stage-1 mapping is backed by a transparent huge
> page.  It adjusts protected_pkvm_mem_abort() and pkvm_pgtable_stage2_map()
> to donate PMD_SIZE ranges and fall back to PAGE_SIZE on -E2BIG or locked
> memory accounting failures.
> 
> > diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c
> > index 1db10d8d6db73..33cd54cee0e42 100644
> > --- a/arch/arm64/kvm/mmu.c
> > +++ b/arch/arm64/kvm/mmu.c
> > @@ -1489,11 +1489,9 @@ static bool fault_supports_stage2_huge_mapping(const struct kvm_s2_fault_desc *s
> >  	if (map_size == PAGE_SIZE)
> >  		return true;
> >  
> > -	/* pKVM only supports PMD_SIZE huge-mappings for non-protected VMs */
> > -	if (is_protected_kvm_enabled()) {
> > -		if (vcpu_is_protected(s2fd->vcpu) || map_size != PMD_SIZE)
> > -			return false;
> > -	}
> > +	/* pKVM only supports PMD_SIZE huge-mappings */
> > +	if (is_protected_kvm_enabled() && map_size != PMD_SIZE)
> > +		return false;
> 
> [Severity: Medium]
> Can enabling PMD_SIZE mappings for protected VMs hold host_mmu.lock at EL2
> with IRQs masked for the entire block during reclaim?
> 
> On protected VM teardown, __pkvm_pgtable_stage2_reclaim() passes the full
> mapping->nr_pages (512, 2048, or 8192 pages depending on PAGE_SIZE) to
> __pkvm_host_reclaim_guest(), which calls hyp_poison_range() under
> host_mmu.lock and vm->lock.  Unlike non-protected VM CMOs,
> __hyp_poison_page() unconditionally zeroes and flushes the full range to
> PoC, and __pkvm_host_force_reclaim_page_guest() additionally holds
> vm_table_lock across it.
> 
> This stalls host IRQs on the calling CPU and blocks concurrent host and
> guest stage-2 transitions across all CPUs for up to 2MB, 32MB, or 512MB
> at a time.  On 64KB kernels, HAS_FIXBLOCK is disabled, so a single 512MB
> reclaim loops 8,192 times issuing per-page broadcast tlbi vale2is
> shootdowns while holding host_mmu.lock.

This is out of scope. I will make a separate patch to improve locking in the
reclaim path.

> 
> [ ... ]
> 
> > +	s2vi->pfn = page_to_pfn(s2vi->page);
> > +
> > +	ret = transparent_hugepage_adjust(s2fd, &s2vi->pfn, &s2vi->gfn);
> > +	if (ret < 0) {
> > +		unpin_user_page(s2vi->page);
> > +		return ret;
> > +	}
> > +
> > +	s2vi->vma_pagesize = ret;
> 
> [Severity: High]
> Does calling transparent_hugepage_adjust() here return unaligned gfn and
> pfn values with PMD_SIZE when CONFIG_PGTABLE_LEVELS == 2?
> 
> arch/arm64/kvm/mmu.c:transparent_hugepage_adjust() {
>     ...
> 		gfn &= ~(PTRS_PER_PMD - 1);
> 		*gfnp = gfn;
> 		pfn &= ~(PTRS_PER_PMD - 1);
> 		*pfnp = pfn;
> 
> 		return PMD_SIZE;
>     ...
> }
> 
> Under 2-level page tables (16KB pages with 36-bit VA, or 64KB pages with
> 42-bit VA), PTRS_PER_PMD is 1 while PMD_SIZE is 32MB or 512MB, so those
> masks are no-ops.  pkvm_pgtable_stage2_map() only checks
> PAGE_ALIGNED(addr) rather than IS_ALIGNED(addr | phys, size).
> 
> On any non-PMD-aligned fault in a THP, the unaligned range either fails
> in __guest_check_transition_size() with -EINVAL (aborting KVM_RUN because
> protected_pkvm_mem_abort() only retries -E2BIG), or overlaps a mapping in
> the next PMD and returns -EAGAIN in an infinite fault loop.  Aligning gfn
> and pfn to PMD_SIZE / PAGE_SIZE in transparent_hugepage_adjust() and
> checking IS_ALIGNED(addr | phys, size) in pkvm_pgtable_stage2_map() would
> avoid this.
> 
> [ ... ]

This isn't related to this series, I will make a separate fix.


> 
> > +account:
> > +	size = s2vi.vma_pagesize;
> > +	ret = account_locked_vm(mm, size / PAGE_SIZE, true);
> > +
> > +retry:
> > +	/* undo transparent_hugepage_adjust() and try again */
> > +	if (ret && size > PAGE_SIZE) {
> > +		s2vi.vma_pagesize = PAGE_SIZE;
> > +		s2vi.gfn = gpa_to_gfn(s2fd->fault_ipa);
> > +		s2vi.pfn = page_to_pfn(s2vi.page);
> > +		goto account;
> > +	}
> > +
> > +	if (ret)
> >  		goto unpin;
> > +
> > +	kvm_fault_lock(kvm);
> > +	if (mmu_invalidate_retry(kvm, s2vi.mmu_seq))
> > +		ret = -EAGAIN;
> > +	else
> > +		ret = pkvm_pgtable_stage2_map(pgt, gfn_to_gpa(s2vi.gfn), size,
> > +					      __pfn_to_phys(s2vi.pfn), KVM_PGTABLE_PROT_RWX,
> > +					      hyp_memcache, 0);
> > +	kvm_fault_unlock(kvm);
> > +
> > +	if (ret) {
> > +		account_locked_vm(mm, size / PAGE_SIZE, false);
> > +		if (ret == -E2BIG)
> > +			goto retry;
> >  	}
> 
> [Severity: Medium]
> Could charging PMD_SIZE / PAGE_SIZE pages before checking
> pgt->pkvm_mappings temporarily over-charge mm->locked_vm and churn
> mmap_write_lock(mm)?
> 
> Donating a single page (or splitting a PMD via
> __pkvm_pgtable_stage2_split()) leaves the host stage-1 PMD intact, so
> get_user_mapping_size() keeps returning PMD_SIZE for later faults in that
> PMD.  Each fault charges PMD_SIZE / PAGE_SIZE pages under
> mmap_write_lock(mm) while waiting on and holding write_lock(&kvm->mmu_lock),
> which can push concurrent faults or VMM mlock() calls over RLIMIT_MEMLOCK.
> 
> When pkvm_pgtable_stage2_map() then returns -E2BIG, this drops
> kvm_fault_lock(kvm), takes mmap_write_lock(mm) twice more to uncharge all
> pages and re-charge one, and re-acquires write_lock(&kvm->mmu_lock) (plus
> a fourth mmap_write_lock(mm) if the retry hits -EAGAIN).
> 
> Pre-checking pgt->pkvm_mappings before accounting, or retrying at
> PAGE_SIZE under the same kvm_fault_lock(kvm) and uncharging the excess
> pages once, would avoid the extra lock acquisitions and over-charge.
> 

This is a retry path that should never be hit unless the VMM has played games
with the mappings. So it doesn't feel like optimizing the number of times we
take the mmap_write_lock() is really important...

> > diff --git a/arch/arm64/kvm/pkvm.c b/arch/arm64/kvm/pkvm.c
> > index edc5416bbd8a5..0342f8e93b6df 100644
> > --- a/arch/arm64/kvm/pkvm.c
> > +++ b/arch/arm64/kvm/pkvm.c
> 
> [ ... ]
> 
> > +		if (mapping && mapping->nr_pages == 1 && size > PAGE_SIZE)
> > +			return -E2BIG;
> 
> [ ... ]
> 
> > -		ret = kvm_call_hyp_nvhe(__pkvm_host_donate_guest, pfn, gfn, 1);
> > +		ret = kvm_call_hyp_nvhe(__pkvm_host_donate_guest, pfn, gfn, size / PAGE_SIZE);
> 
> [Severity: Medium]
> Should protected_pkvm_mem_abort() also fall back to PAGE_SIZE when
> __pkvm_host_donate_guest() returns -EPERM or -EINVAL for a PMD_SIZE
> donation?
> 
> pkvm_pgtable_stage2_map() only returns -E2BIG for overlaps in the current
> VM's pgt->pkvm_mappings tree.  If a sibling subpage of a swapbacked THP
> (such as memfd) is not PKVM_PAGE_OWNED, __pkvm_host_donate_guest() ->
> __host_check_page_state_range() returns -EPERM (or -EINVAL across memblocks).
> 
> Because protected_pkvm_mem_abort() only retries on -E2BIG, KVM_RUN now
> fails on a host-owned page that succeeded at PAGE_SIZE before this patch.
> Retrying at PAGE_SIZE on -EPERM and -EINVAL when size > PAGE_SIZE lets
> the faulting page succeed while still failing if it is not host-owned.

That would mean two VMs are sharing the same mappings? Is any VMM doing that? It
feels to me for protected VM we'd rather be quite restrictive... 

-- 
Vincent


> 
> -- 
> Sashiko AI review · https://sashiko.dev/#/patchset/20261203115209.1690207-1-vdonnefort@google.com?part=24

  reply	other threads:[~2026-12-04 14:50 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 11:51 [PATCH v6 00/24] Huge mapping support for protected VMs Vincent Donnefort
2026-12-03 11:51 ` [PATCH v6 01/24] KVM: arm64: Introduce kvm_pgtable_stage2_table_install() Vincent Donnefort
2026-12-03 12:07   ` sashiko-bot
2026-12-03 11:51 ` [PATCH v6 02/24] KVM: arm64: Preserve idmap stage-2 entries on block split Vincent Donnefort
2026-12-03 12:30   ` sashiko-bot
2026-12-03 11:51 ` [PATCH v6 03/24] KVM: arm64: Allow block-level stage-2 annotation Vincent Donnefort
2026-12-03 12:52   ` sashiko-bot
2026-12-03 11:51 ` [PATCH v6 04/24] KVM: arm64: Use block-level annotations when setting up the host stage-2 Vincent Donnefort
2026-12-03 11:51 ` [PATCH v6 05/24] KVM: arm64: Make pKVM ownership selftest an HVC Vincent Donnefort
2026-12-03 11:51 ` [PATCH v6 06/24] KVM: arm64: Reject zero-sized ranges in pfn_range_is_valid() Vincent Donnefort
2026-12-03 11:51 ` [PATCH v6 07/24] KVM: arm64: Add a range to __pkvm_host_share/unshare_hyp() Vincent Donnefort
2026-12-03 13:46   ` sashiko-bot
2026-12-03 11:51 ` [PATCH v6 08/24] KVM: arm64: Add a range to __pkvm_host_donate_guest() Vincent Donnefort
2026-12-03 11:51 ` [PATCH v6 09/24] KVM: arm64: Add a range to hyp_poison_page() Vincent Donnefort
2026-12-03 14:26   ` sashiko-bot
2026-12-03 11:51 ` [PATCH v6 10/24] KVM: arm64: Add a range to __pkvm_host_reclaim_guest() Vincent Donnefort
2026-12-03 14:47   ` sashiko-bot
2026-12-03 11:51 ` [PATCH v6 11/24] KVM: arm64: Add a range to __pkvm_guest_share_host() Vincent Donnefort
2026-12-03 11:51 ` [PATCH v6 12/24] KVM: arm64: Add a range to __pkvm_guest_unshare_host() Vincent Donnefort
2026-12-03 11:51 ` [PATCH v6 13/24] KVM: arm64: Handle huge mappings in __pkvm_host_force_reclaim_page_guest() Vincent Donnefort
2026-12-03 15:37   ` sashiko-bot
2026-12-03 11:51 ` [PATCH v6 14/24] KVM: arm64: Handle huge mappings in __pkvm_vcpu_in_poison_fault() Vincent Donnefort
2026-12-03 11:52 ` [PATCH v6 15/24] KVM: arm64: Add a range to pKVM ownership selftest Vincent Donnefort
2026-12-03 16:17   ` sashiko-bot
2026-12-03 11:52 ` [PATCH v6 16/24] KVM: arm64: Warn on pKVM guest stage-2 block collapse Vincent Donnefort
2026-12-03 11:52 ` [PATCH v6 17/24] KVM: arm64: Add vcpu conduit for pkvm_hyp_req Vincent Donnefort
2026-12-03 17:07   ` sashiko-bot
2026-12-03 11:52 ` [PATCH v6 18/24] KVM: arm64: Factor out host_stage2_encode_annotation() Vincent Donnefort
2026-12-03 11:52 ` [PATCH v6 19/24] KVM: arm64: Return VM handle from host_stage2_decode_gfn_meta() Vincent Donnefort
2026-12-03 11:52 ` [PATCH v6 20/24] KVM: arm64: Add __pkvm_host_split_guest HVC Vincent Donnefort
2026-12-03 17:49   ` sashiko-bot
2026-12-03 11:52 ` [PATCH v6 21/24] KVM: arm64: Extend pKVM page ownership selftests to cover guest block split Vincent Donnefort
2026-12-03 11:52 ` [PATCH v6 22/24] KVM: arm64: Add PKVM_HYP_REQ_SPLIT Vincent Donnefort
2026-12-03 11:52 ` [PATCH v6 23/24] KVM: arm64: Raise PKVM_HYP_REQ_SPLIT on guest to host sharing Vincent Donnefort
2026-12-03 11:52 ` [PATCH v6 24/24] KVM: arm64: Stage-2 huge mappings for protected VMs Vincent Donnefort
2026-12-03 19:28   ` sashiko-bot
2026-12-04 14:50     ` Vincent Donnefort [this message]
2026-12-03 19:27 ` [PATCH v6 00/24] Huge mapping support " 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=axLTNBaoLvHkJ7JN@google.com \
    --to=vdonnefort@google.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.