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 v5 25/25] KVM: arm64: Stage-2 huge mappings for protected VMs
Date: Wed, 2 Dec 2026 10:26:16 +0000 [thread overview]
Message-ID: <aw_ySIAMK9X04Cf7@google.com> (raw)
In-Reply-To: <sashiko-outbox-241597@kernel.org>
On Mon, Nov 30, 2026 at 04:38:59PM +0000, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 1 potential
> issue to consider:
> - [Medium] KVM: arm64: repeated PMD locked_vm over-charge in
> protected_pkvm_mem_abort()
> --
>
> commit 941a975a4322f6921d62e29967a4f68fd50e4378
> Author: Vincent Donnefort <vdonnefort@google.com>
>
> KVM: arm64: Stage-2 huge mappings for protected VMs
>
> This commit enables PMD-sized stage-2 block mappings for protected VMs
> when the backing host stage-1 mapping is a THP, falling back to a
> PAGE_SIZE donation if the PMD range overlaps an existing smaller mapping.
>
> > diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c
> > index 1db10d8d6db73..df8ec7c798ddc 100644
> > --- a/arch/arm64/kvm/mmu.c
> > +++ b/arch/arm64/kvm/mmu.c
> [ ... ]
> > @@ -1893,26 +1872,83 @@ static int protected_pkvm_mem_abort(const struct kvm_s2_fault_desc *s2fd)
> [ ... ]
> > +static int protected_pkvm_mem_abort(const struct kvm_s2_fault_desc *s2fd)
> > +{
> > + struct kvm_s2_fault_vma_info s2vi = {};
> > + struct kvm_vcpu *vcpu = s2fd->vcpu;
> > + struct kvm_pgtable *pgt = s2fd->mmu->pgt;
> > + struct mm_struct *mm = current->mm;
> > + struct kvm *kvm = vcpu->kvm;
> > + unsigned long unaccount;
> > + void *hyp_memcache;
> > + u64 size;
> > + int ret;
> > +
> > + hyp_memcache = get_mmu_memcache(vcpu);
> > + ret = topup_mmu_memcache(s2fd->mmu, hyp_memcache);
> > + if (ret)
> > + return -ENOMEM;
> > +
> > + ret = pkvm_pin_user_pages(s2fd, &s2vi);
> > + if (ret == -EHWPOISON || ret == -EAGAIN)
> > + return 0;
> > + else if (ret)
> > + return ret;
> > +
> > + size = s2vi.vma_pagesize;
> > +
> > + ret = account_locked_vm(mm, size / PAGE_SIZE, true);
> > + 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);
> > +
> > + /* undo transparent_hugepage_adjust() and try again */
> > + if (ret == -E2BIG) {
> > + u64 ipa = ALIGN_DOWN(s2fd->fault_ipa, PAGE_SIZE);
> > +
> > + size = PAGE_SIZE;
> > + ret = pkvm_pgtable_stage2_map(pgt, ipa, size,
> > + page_to_phys(s2vi.page),
> > + KVM_PGTABLE_PROT_RWX,
> > + hyp_memcache, 0);
> > + }
> > }
> > + kvm_fault_unlock(kvm);
> >
> > - write_lock(&kvm->mmu_lock);
> > - ret = pkvm_pgtable_stage2_map(pgt, gfn_to_gpa(s2vi.gfn), PAGE_SIZE,
> > - page_to_phys(s2vi.page), KVM_PGTABLE_PROT_RWX,
> > - hyp_memcache, 0);
> > - write_unlock(&kvm->mmu_lock);
> > + unaccount = (ret ? s2vi.vma_pagesize : s2vi.vma_pagesize - size) / PAGE_SIZE;
> > + account_locked_vm(mm, unaccount, false);
>
> [Severity: Medium]
> Does charging s2vi.vma_pagesize before checking for overlapping stage-2
> mappings cause repeated PMD over-accounting and double mmap_write_lock(mm)
> acquisitions across a partially mapped THP range?
>
> When a 2MB guest IPA range already has at least one 4KB mapping in
> pgt->pkvm_mappings while the host stage-1 mapping is a PMD THP,
> get_user_mapping_size() still returns PMD_SIZE because donating a 4KB page
> leaves the host stage-1 PMD intact. Each subsequent 4KB fault in that 2MB
> range charges 512 pages under mmap_write_lock(mm), hits -E2BIG in
> pkvm_pgtable_stage2_map(), falls back to PAGE_SIZE, and then takes
> mmap_write_lock(mm) a second time to uncharge 511 pages.
>
> Across the remaining faults in that 2MB range, mm->locked_vm is temporarily
> charged up to 511 + 512 = 1023 pages for a 512-page region, which can cause
> spurious -ENOMEM fault failures when RLIMIT_MEMLOCK is sized to the VM memory.
>
> -- Sashiko AI review ·
> https://sashiko.dev/#/patchset/20261130155710.2371874-1-vdonnefort@google.com?part=25
No accounting would leak.
For anonymous memory if a THP overlaps an existing PTE-level mapping, then the
VMM has played games with the mappings and I believe it is alright to punish it.
What could be slightly improve is to fallback to PAGE_SIZE if
account_locked_vm() fails with PMD_SIZE... but that would make unaccount more
complex.
--
Vincent
next prev parent reply other threads:[~2026-12-02 10:26 UTC|newest]
Thread overview: 35+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-30 15:56 [PATCH v5 00/25] Huge mapping support for protected VMs Vincent Donnefort
2026-11-30 15:56 ` [PATCH v5 01/25] KVM: arm64: Prefault host stage-2 entries on block split Vincent Donnefort
2026-11-30 18:52 ` Fuad Tabba
2026-12-01 7:34 ` Oliver Upton
2026-12-02 10:29 ` Vincent Donnefort
2026-11-30 15:56 ` [PATCH v5 02/25] KVM: arm64: Propagate host stage-2 annotated " Vincent Donnefort
2026-11-30 15:56 ` [PATCH v5 03/25] KVM: arm64: Allow block-level stage-2 annotation Vincent Donnefort
2026-11-30 15:56 ` [PATCH v5 04/25] KVM: arm64: Use block-level annotations when setting up the host stage-2 Vincent Donnefort
2026-11-30 15:56 ` [PATCH v5 05/25] KVM: arm64: Make pKVM ownership selftest an HVC Vincent Donnefort
2026-11-30 15:56 ` [PATCH v5 06/25] KVM: arm64: Reject zero-sized ranges in pfn_range_is_valid() Vincent Donnefort
2026-11-30 15:56 ` [PATCH v5 07/25] KVM: arm64: Add a range to __pkvm_host_share/unshare_hyp() Vincent Donnefort
2026-11-30 15:56 ` [PATCH v5 08/25] KVM: arm64: Add a range to __pkvm_host_donate_guest() Vincent Donnefort
2026-11-30 15:56 ` [PATCH v5 09/25] KVM: arm64: Add a range to hyp_poison_page() Vincent Donnefort
2026-11-30 15:56 ` [PATCH v5 10/25] KVM: arm64: Add a range to __pkvm_host_reclaim_guest() Vincent Donnefort
2026-11-30 15:56 ` [PATCH v5 11/25] KVM: arm64: Add a range to __pkvm_guest_share_host() Vincent Donnefort
2026-11-30 15:56 ` [PATCH v5 12/25] KVM: arm64: Add a range to __pkvm_guest_unshare_host() Vincent Donnefort
2026-11-30 15:56 ` [PATCH v5 13/25] KVM: arm64: Handle huge mappings in __pkvm_host_force_reclaim_page_guest() Vincent Donnefort
2026-11-30 15:56 ` [PATCH v5 14/25] KVM: arm64: Handle huge mappings in __pkvm_vcpu_in_poison_fault() Vincent Donnefort
2026-11-30 15:56 ` [PATCH v5 15/25] KVM: arm64: Add a range to pKVM ownership selftest Vincent Donnefort
2026-11-30 15:57 ` [PATCH v5 16/25] KVM: arm64: Warn on pKVM guest stage-2 block collapse Vincent Donnefort
2026-11-30 15:57 ` [PATCH v5 17/25] KVM: arm64: Add vcpu conduit for pkvm_hyp_req Vincent Donnefort
2026-11-30 15:57 ` [PATCH v5 18/25] KVM: arm64: Introduce kvm_pgtable_stage2_table_install() Vincent Donnefort
2026-11-30 15:57 ` [PATCH v5 19/25] KVM: arm64: Factor out host_stage2_encode_annotation() Vincent Donnefort
2026-11-30 15:57 ` [PATCH v5 20/25] KVM: arm64: Return VM handle from host_stage2_decode_gfn_meta() Vincent Donnefort
2026-11-30 15:57 ` [PATCH v5 21/25] KVM: arm64: Add __pkvm_host_split_guest HVC Vincent Donnefort
2026-11-30 15:57 ` [PATCH v5 22/25] KVM: arm64: Extend pKVM page ownership selftests to cover guest block split Vincent Donnefort
2026-11-30 15:57 ` [PATCH v5 23/25] KVM: arm64: Add PKVM_HYP_REQ_SPLIT Vincent Donnefort
2026-11-30 15:57 ` [PATCH v5 24/25] KVM: arm64: Raise PKVM_HYP_REQ_SPLIT on guest to host sharing Vincent Donnefort
2026-11-30 15:57 ` [PATCH v5 25/25] KVM: arm64: Stage-2 huge mappings for protected VMs Vincent Donnefort
2026-11-30 16:38 ` sashiko-bot
2026-12-02 10:26 ` Vincent Donnefort [this message]
2026-11-30 21:01 ` Fuad Tabba
2026-12-02 10:26 ` Vincent Donnefort
2026-11-30 21:33 ` [PATCH v5 00/25] Huge mapping support " Fuad Tabba
2026-12-02 10:27 ` Vincent Donnefort
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=aw_ySIAMK9X04Cf7@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.