From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.221.48 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796207182; b=nBKbPHvZp3z/pGKMDaLF4F+WUn+yt1M+rQo3u5gLbLnMLCGI0LHRKfO7xnrtQjTpROny iS8lOSwmKAxk1WVnEHrODx9EMqQHcKQH1aSSywP3DuJKYJzM3l7B6M/+Fm9Dstnpul9Z6 hH35b7MLuEGQgr0i6myqwzvyU/HAxAxC0bmwLj1pLqmfRnlt7FKc1S/3tpi7qKbjvhe5i 56hk7Mmvs2mDmG2Mi1JHTBiEjd3fooAFWd5OqTYeRUlN5DMzcC460ihSXC9IuLScaEc97 Goi6kZzCwEJ/O6pBNxDFai4Ptb/qfThBNIMRp7fRfrQKRGrhs274zO0u3bNre+PpnYA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796207182; 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=Zx7ZLtwvUFzt0lO6fICBgbE9aX6Fb7a1/R101HO3TDE=; b=fe2F9JwaZ5/yUoPWD6G2sh5Lq1iHYAaSjp76HNzrppXjwYolrhhLhhUOsZgcDa0ydcVJ liIg6l8/u2Oj4eU7lk7K4oRhQOCMhXL71OcXQk5kuIiQNK/Z41H5kR9KRdfjVsmRKRamf ofamTpSWA00/Q/4PxytBHbLtC4ZCYwT7bhzUZeu4ByL8+Uz1TgRmDeyLy4FzaXoTBThDk rbGLxd3ipHUMH3AZxSkhyd6DHd1fvvU6Qs0tkf36EAkjfavxLrIvzI9YDMRbC0JhGBub3 8HEZxXDTpHZlMsF9QFkWAFoirn0rsXprvL9Adyt0OXMtQJSrLlG5BkuvnvQcrOmggBg== 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="Rd/ZF0eY"; arc=none smtp.remote-ip=209.85.221.48 Received: from mail-wr1-f48.google.com (mail-wr1-f48.google.com [209.85.221.48]) (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 25C6F606A322 for ; Wed, 2 Dec 2026 10:26:21 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 25C6F606A322 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) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=Rd/ZF0eY Received: by mail-wr1-f48.google.com with SMTP id ffacd0b85a97d-490d59c3edaso8800254f8f.1 for ; Wed, 02 Dec 2026 02:26:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796207180; x=1796811980; 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=Zx7ZLtwvUFzt0lO6fICBgbE9aX6Fb7a1/R101HO3TDE=; b=Rd/ZF0eYipsNi3SkF31IpKOUxVJxH2AyLhef9CnqD+ywv20xT/tGUNfnYx3aaUIIFo RjcVGxbFThb5Ya8fpGCei3Sxx7+cbsvosx3QQgbXRTAUlwz+Nm4X+rxn/AQE05RZuce1 MHUiJXmfBlXv3BUnbZ7xUb5LPwsgQRH5wZhnhGppGoHyk3Nc/bMlRoRNM0jV9mXwgeyu db8OCtnZnPeThg8EufUsENY5WrcRzXQ5Djy1BWEm7w0IjasgwxQkV4hb+f74Srsc3PvN hKQOK/AIMxNjD3CfhZJ5fIGHy7KLwAwVDm1lMhRfXL5riAGJcTxo5e5uP7kw4z0M3la8 wM9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796207180; x=1796811980; 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=Zx7ZLtwvUFzt0lO6fICBgbE9aX6Fb7a1/R101HO3TDE=; b=jc31xPPfvQRo8B8hxE3cXrJQvCTVz0oKG6KsIAZml/8rac5/2bDBGWGOcWfLHRToZc jU/fHJP+pgAftbtsRLi1oSI/0YTgagkq8uNjDtMMDk+U8sJFR1LuTvYPsxooHTrXPU6R 2TMers/NQ2BcYGr/1bNP7WtXOCzUL+mblwnewRL1rsIF0xpjjSh1tb7gWBdK+/eAngSN AuchtmCnJe5/zLXOwcdVsY8pf9DCLJJu2rAb4OzNcc3ylhbxuTvmGW1/SR1hXJe4CFBM 1UEN9uYMt5TWwBK6ggXf642phMQxhL7xra2arjOP7SXGbd6MvPCJoFrW+HwPYVResKpY B3zw== X-Gm-Message-State: APuU4Z+qj6sUXpv2mWlK5Q9zAz9E6pboZFfh9n3AX12RODyZrTXhj22q l4HGM49ofeUbwiTRRyiugrxsgKgCnaCpS9jw6r6Efy/kEFgf8qLIy24R2qt/HodSlg== X-Gm-Gg: AZFdTZfAX3UwkX4Dv2/WM9OeewRU8xN3GTw0Gqr1ffQ2XYE/LG8elQqsThB4MBPaqB4 kKTysYAE5x8bWzvEhPMCSGLWpk7pvgYf5o74YCDCQmh/yjSgoPeyWsBmpIAwKwV4eHmpJKkVCdM nMpLX1cQk6RZqzmkk8kQHILLryIkIY3uHso9nAmu0P4YDw7aJxr9fItp2wEmt33/4A1s7p1pILu AkE5loLuV+dpUQc/zD6cxtElf+lIDVXt0erLUB/dKJfleHxsBVaWljfQtGEYuU/RVCDXlUt0wZp kGhpX2YCxgGj2XcILJb8XnTP1HsHUJmY0pYs3WWcxmvJnjvdhlPdqWiwMAg1SqGxgao6pEeLlZ2 f3HQQxM5LB025KbAkeqV/RekjcrJ6cPywKYj1lya8ILFEyvCgtP+s7hGxEAhXFtLqeLHKEeBHfd cPl4KjzueENUDeD/hgpzO+SJKXzkHkfBTTGx0ldBP1ulZaCvST+RYXFishr5A2yqZSGbrP1sFrC wFCOL3ovSGvtTHOZ6c1nz5TMJZwjBFsLhUfdrHxNAgCwC3BhDJCQWYeIU4xBPCsIZncF1+P51YZ BoDz85wtsJOX5VpMxrKvxq22uKq/T5lNJoo4mnktbZU/63cvXQfcTR6pYRYkDR9dInFthI6HBTP tFseS6EHOHo1ld88S5YV9 X-Received: by 2002:a05:6000:2681:b0:4a7:dcc2:a36f with SMTP id ffacd0b85a97d-4aa5a2dc7bfmr103920696f8f.26.1796207179997; Wed, 02 Dec 2026 02:26:19 -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-4ab6c61f558sm78429891f8f.58.2026.12.02.02.26.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Dec 2026 02:26:19 -0800 (PST) Date: Wed, 2 Dec 2026 10:26:16 +0000 From: Vincent Donnefort To: sashiko-reviews@lists.linux.dev Cc: kvmarm@lists.linux.dev, Oliver Upton , Marc Zyngier Subject: Re: [PATCH v5 25/25] KVM: arm64: Stage-2 huge mappings for protected VMs Message-ID: References: <20261130155710.2371874-1-vdonnefort@google.com> <20261130155710.2371874-26-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 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 > > 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