From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.221.44 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796395835; b=i+5zC7aP/fqxG+arHwgdxY18gMXJaEaT+cHtaUhOqKiapMCDQzsuinfUYZ8AMbKiT2nk 6TX2zAvwdrIc8+sKBqghg/llSn+yUh64v0F4TjQzVD2MJj2nUgQlKCLphTz1IFZTSjbLG m8uKsaZYpjMgavqWD4m/de4arENyhnmBn9Jj+AbhrJ5YsQ229g/y8tsPoU4tarv6ym/9d r+X5U1jOgeORL3XZxYn+HdtnZwVZAIvPNrPUjEcMnxpi2cU5IJodOMHxQN68s78cA/k23 sjWTNrxqVB4kaRf+quHNTH2R/Idy4iNK2v26dn0Atro1StSmo76ZHXKTn+Zj6VIm8HA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796395835; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:Date:From:To:Cc: Subject:Message-ID:References:MIME-Version:Content-Type: Content-Disposition:Content-Transfer-Encoding:In-Reply-To; bh=vXUQuRV2ofduY1lngoVWI3Ph4vXl1Hj+i5rKcZym/hs=; b=uFXxUoG++cNFbbKBmiIX2YkpAZnLZBi6TZHH66CWDPTF4nP4ydYS6LPTu4sluRZ0qodb Izss1CtiSCVbg/0ioE/qxvOuQW/IwvWJYQobwgUqnNCVRcHvbyk8CbqmfJQvjp2zbn4Sb ftm23A3Opq7nb/08A/zNjuKNwFpqJaJz8h/svEjEcX12WDIzRgqJjdf01RIiOFOJbCiPw XH7JWOWcvk3EMRdYBCApRwG1AZm3EIDLEIqgjMoucaJ3SmeY0aX/+iw8rZ6g/zZ0Z5HyN 48VuX+nlqIwrs5Y8Ws5Y6gk/0JS086papQKXx9VzuWAJa8EF3Tx6lM0o5BoYa6TRu7w== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=MAfaONju; arc=none smtp.remote-ip=209.85.221.44 Received: from mail-wr1-f44.google.com (mail-wr1-f44.google.com [209.85.221.44]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 26BD1606A322 for ; Fri, 4 Dec 2026 14:50:34 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 26BD1606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=MAfaONju Received: by mail-wr1-f44.google.com with SMTP id ffacd0b85a97d-492690dd5daso800895f8f.1 for ; Fri, 04 Dec 2026 06:50:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796395833; x=1797000633; darn=lists.linux.dev; h=in-reply-to:content-transfer-encoding:content-disposition :content-type:mime-version:references:message-id:subject:cc:to:from :date:from:to:cc:subject:date:message-id:reply-to:content-type; bh=vXUQuRV2ofduY1lngoVWI3Ph4vXl1Hj+i5rKcZym/hs=; b=MAfaONjuHT1FKpVeTmostsfFjOGRLA+qar0TEwu5+vbcTPB87ULXHLsGbxrRR2ZOFB b3DSH2kJIw0NPWqvWhCIu8WNR8OBBNjx/S8dp6Rlt4hpnYWJdd12Rf6VbTRwRdzv11WH HeEkd3kXCcEdroACbbNA1dOBW6dT94UIkd+13DyKpXNeOOxCunTaqkTx3al6lTPno/4L zxGUGdWpXsAP0fLzu0Q20Goes0ZJo1zcSRMRqg42YNVKHU1Nd2WJR/Cxye6rp78BmxWs G/mLsjUVjrAnYW49kIkOXoFk7BG2s/ZuEHho3kAEQk1Hk/jUdG6iOrOgwrpgtEGgpIaM keQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796395833; x=1797000633; h=in-reply-to:content-transfer-encoding:content-disposition :content-type:mime-version:references:message-id:subject:cc:to:from :date:x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to:content-type; bh=vXUQuRV2ofduY1lngoVWI3Ph4vXl1Hj+i5rKcZym/hs=; b=Y17eB5IeUGJcQqNBxl0qs6cwWbrBu2jZHMnrkuYH73zzikYrwm1kq4J6U/QnLSYLWH V/ELEwKyeYcx457tNUDe1WPkoZHoagyQXqiYdzwdkZrMZqAkkymWaeCsPZdoNsNUnkEC oBfs0G4iBGLQOj0vsqCAhrl6Hg4bQlo+KArS+IIWI67sz3vwYT3Wihz14QTMrrWBmebm eUxdDFWr9JmfbSVbE9YiwzLXDYXaEUBUm6PxLHAM2SEvqaTry1ScFcGMnlVydOZn7ZqZ ZAn4cayOfA3nCMiKRG+ev8VoZDXoG6Mkd+Jh7arN6UrK6hRYJLWqxI2AxZ/RZG7qcZ/6 Udrw== X-Gm-Message-State: AB4q+uGkPflicIWoqVciOtUeIVL44AzxgeH62nqgu5CDqkiuxmm3Hy3v DSgyrT3rKUMYazOtqS8+J+AYyO7EM8jCMdUsP0Uui38fPQ8Y+RUYKik/pbrdYEuK7g== X-Gm-Gg: AZFdTZdKZhYtdLD5yUCIlxjYzNpciwOcTedsHiQr5jXB41lttvpq+Elgq+sgOD4ZXEb 6Ww5HME7E/FUOfKSc6sduJkgPxyd+Ue5xOskRShgcYBeWR3S/EuIGFPBZJVMzQ6hXmi7pRtl2Gr sZ4s2zM8mGNt0jNIPIaKKeZsyuCHbna67tlj3g5a/wNrLcGK7XfxmrTyOzellltyXzvIIQZ0nVw k5cCI1/vFHqs0wcylzxOr4HIo2FtVxa3nI9eBtMQ1jQlBZfmzRLAkTV8kRlwYG5KYhFpz3tefgL NLgh0L9D6SDeouH5DXIh5pOQbpnLztTIO+4RYjjUsSBBmwbb8fk6tj5Rwm7EdRcb/zwBCSORKP2 4u/qxlVeCesxAZkh0kAhxEw2Um6SP5xvLeddViuzyGDrpAMCICpt48DE8FscoByiRQ3ElERDDE1 XKA/sZqEHutSaj9Bv0+qeR4tB9QJBffBKrI5RMhg3kfDgy2HDm/FR0P2/1J7e2D+LUYmIFTvvyN red3MIUd5XmLqhTPWxBx39RpgOIPD7M3H83cBMRMqQxUqUqUgTiV6hWIzKpSdRG2m7pMEjY334R Mk1zAB1eWd05qm22NMgArjh5Q/GEUuP6rLaOBchbLQ1CpSWGugs213WKq5h+pVeTZENTpfZiq+T yMtrEMGzAgte19tg8cR+j X-Received: by 2002:a5d:4389:0:b0:4a6:b87:d7a1 with SMTP id ffacd0b85a97d-4d1ec9188edmr3692504f8f.49.1796395832931; Fri, 04 Dec 2026 06:50:32 -0800 (PST) Received: from google.com (0.182.34.34.bc.googleusercontent.com. [34.34.182.0]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4d1723b8aadsm5800727f8f.18.2026.12.04.06.50.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Dec 2026 06:50:32 -0800 (PST) Date: Fri, 4 Dec 2026 14:50:28 +0000 From: Vincent Donnefort To: sashiko-reviews@lists.linux.dev Cc: kvmarm@lists.linux.dev, Oliver Upton , Marc Zyngier Subject: Re: [PATCH v6 24/24] KVM: arm64: Stage-2 huge mappings for protected VMs Message-ID: References: <20261203115209.1690207-1-vdonnefort@google.com> <20261203115209.1690207-25-vdonnefort@google.com> X-Mailing-List: kvmarm@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: 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 > > 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