From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796326101; b=YmLtbyM5Hmzkz31Bwu8w3xBRyg6K0qNwF1vZGoAYcAcJuUbT9OYR9npUtyfrp8y+1OiN o13U94dd146uB/SsAHmIWn3Lld3B76PrQukHXkZB5lIGEe3nnP/IQUbIewtA2ExKae+iP 39TRrBSOkuIFxLC+e05U56NhyjpOZ1ceu6SsHr/op4g0IW5U3dLps/SpMVsZiBw4v3nKg DcuuCEQ4CzIxLzos+1S1vrJceTDTmliSntZ1/BFO2fOjFwQkaA6UrU3y0h1/j+62EcEyK 7tYiRyn4J5stVG1gAJkM6zhT60tu+hXog+rN8SV32Oej8CsQbyz44t0QXumk5wLAM3w== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796326101; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=UlxtFl0S6UZhpfyHExpFORq0glOpshVSjCNlmK8qCnA=; b=qfyu+hHQzjCIy25mHs6IcbFMUpecsdLh76zecwRSo7JkLJk68+8QDwrgqX2EhA9Cf57w uLF0uGHXGWCHjfXslSc+DIQQdfpgkwP1AFgBH4BuFQE1cnnlhn6DjvkEOG3LdZg7Z/c5O rD2xZKJk93IEI+ltwVtHK2xGzIxolOWAcMuNc0aBhJrMmMGlkV7dyC3ZHn8gdkn9bgj1f Mmb4kXpDgjh3ISxfPZMFgyKrLlkUMydussHaKGkzfMcuanQh9wu+qNWr+Lk9TqaoexjEN bTSv7IHk1uaQJqJPcxe6HH7Mb9847lFcGjVnrd0JTbPRgDoboUkC1SEKrb/vkT3AToA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=nkRCWlU8; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EC843606A6A6 for ; Thu, 3 Dec 2026 19:28:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=nkRCWlU8 Received: by smtp.kernel.org (Postfix) with ESMTPSA id A8B021F000CE; Thu, 3 Dec 2026 19:28:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796326100; bh=UlxtFl0S6UZhpfyHExpFORq0glOpshVSjCNlmK8qCnA=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=nkRCWlU8BaSS0KQCKQdywcFt1lRzO0vqiP8dQySQSTJyzK74gcYhidP5yekoaHkph abc3t0q/SYoO86TJToOtiPooGXcxu37Ihv7E0bov+PtCRasUZd3pnQT8jmplrWUV/S eqtmLAX/F0SYHBdNcxGWT1O4ZqFciS/rmxIqe02QSonNrf7Llh1sC5Rbabv6udYXtu UAT6hQZDkT2ZgDBlZlnFpSnwVKYQE2VZIN9vGvEfMTgG2+fMYm344VgKfZt2q8f9MX JWc3BKQPnhigfMpCUVr5qHUYZqLbx45yczpDPnN1wWosWHrLR0JRNl+43e0Y7o8bR0 TFEWYZMBl/Iaw== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v6 24/24] KVM: arm64: Stage-2 huge mappings for protected VMs Reply-To: sashiko-reviews@lists.linux.dev To: "Vincent Donnefort" , kvmarm@lists.linux.dev Cc: "Oliver Upton" , "Marc Zyngier" In-Reply-To: <20261203115209.1690207-25-vdonnefort@google.com> References: <20261203115209.1690207-1-vdonnefort@google.com> <20261203115209.1690207-25-vdonnefort@google.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Thu, 03 Dec 2026 19:28:20 +0000 X-Mailing-List: kvmarm@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list 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 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(con= st struct kvm_s2_fault_desc *s > if (map_size =3D=3D PAGE_SIZE) > return true; > =20 > - /* pKVM only supports PMD_SIZE huge-mappings for non-protected VMs */ > - if (is_protected_kvm_enabled()) { > - if (vcpu_is_protected(s2fd->vcpu) || map_size !=3D PMD_SIZE) > - return false; > - } > + /* pKVM only supports PMD_SIZE huge-mappings */ > + if (is_protected_kvm_enabled() && map_size !=3D 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. [ ... ] > + s2vi->pfn =3D page_to_pfn(s2vi->page); > + > + ret =3D transparent_hugepage_adjust(s2fd, &s2vi->pfn, &s2vi->gfn); > + if (ret < 0) { > + unpin_user_page(s2vi->page); > + return ret; > + } > + > + s2vi->vma_pagesize =3D ret; [Severity: High] Does calling transparent_hugepage_adjust() here return unaligned gfn and pfn values with PMD_SIZE when CONFIG_PGTABLE_LEVELS =3D=3D 2? arch/arm64/kvm/mmu.c:transparent_hugepage_adjust() { ... gfn &=3D ~(PTRS_PER_PMD - 1); *gfnp =3D gfn; pfn &=3D ~(PTRS_PER_PMD - 1); *pfnp =3D 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. [ ... ] > +account: > + size =3D s2vi.vma_pagesize; > + ret =3D account_locked_vm(mm, size / PAGE_SIZE, true); > + > +retry: > + /* undo transparent_hugepage_adjust() and try again */ > + if (ret && size > PAGE_SIZE) { > + s2vi.vma_pagesize =3D PAGE_SIZE; > + s2vi.gfn =3D gpa_to_gfn(s2fd->fault_ipa); > + s2vi.pfn =3D page_to_pfn(s2vi.page); > + goto account; > + } > + > + if (ret) > goto unpin; > + > + kvm_fault_lock(kvm); > + if (mmu_invalidate_retry(kvm, s2vi.mmu_seq)) > + ret =3D -EAGAIN; > + else > + ret =3D 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 =3D=3D -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. > 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 =3D=3D 1 && size > PAGE_SIZE) > + return -E2BIG; [ ... ] > - ret =3D kvm_call_hyp_nvhe(__pkvm_host_donate_guest, pfn, gfn, 1); > + ret =3D kvm_call_hyp_nvhe(__pkvm_host_donate_guest, pfn, gfn, size / P= AGE_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. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261203115209.1690= 207-1-vdonnefort@google.com?part=3D24