From: Marc Zyngier <maz@kernel.org>
To: Dan Carpenter <dan@cryspen.com>
Cc: "Oliver Upton" <oupton@kernel.org>,
"Fuad Tabba" <fuad.tabba@linux.dev>,
"Joey Gouly" <joey.gouly@arm.com>,
"Steffen Eiden" <seiden@linux.ibm.com>,
"Suzuki K Poulose" <suzuki.poulose@arm.com>,
"Zenghui Yu" <yuzenghui@huawei.com>,
"Catalin Marinas" <catalin.marinas@arm.com>,
"Will Deacon" <will@kernel.org>,
"Mark Rutland" <mark.rutland@arm.com>,
linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev,
linux-kernel@vger.kernel.org,
"Karthik Bhargavan" <karthik@pereq.eu>,
"Clément Blaudeau" <clement@cryspen.com>
Subject: Re: [PATCH] KVM: arm64: nv: Invalidate whole L1 blocks on TLBI RIPAS2E1
Date: Wed, 11 Nov 2026 10:08:46 +0000 [thread overview]
Message-ID: <86o6bv1xzl.wl-maz@kernel.org> (raw)
In-Reply-To: <6817d113e5f0a9cafe0ce5bc0847276b7f14aacc.1794210561.git.dan@cryspen.com>
Hi Dan,
Thanks for this patch.
On Tue, 10 Nov 2026 06:14:20 +0000,
Dan Carpenter <dan@cryspen.com> wrote:
>
> The shadow stage-2 can map one of the L1's stage-2 blocks with several
> smaller leaves. This happens when the host backs the memory with
> smaller pages, when the memslot is dirty logging, or when the L1 block
> size falls between two host block sizes.
>
> When the L1 invalidates part of such a block, handle_ripas2e1is() only
> unmaps the requested range, so the shadow leaves for the rest of the
> block stay valid. A TLBI must remove every TLB entry for a block it
> touches, including the parts outside the range. The L1 does not need
> to issue another TLBI before reusing the rest of the block, so the L2
> can still reach those pages through the stale shadow entries.
>
> Use compute_tlb_inval_range() in s2_mmu_unmap_range() to find the L1
> block size, the same as the IPAS2E1 path does, and widen the range to
> whole L1 blocks. The other caller, handle_vmalls12e1is(), unmaps the
> whole IPA space, so the widening does not change anything there.
>
> This only affects nested virtualization: the host must be booted with
> kvm-arm.mode=nested and the VM must be given a virtual EL2. In such a
> VM it breaks the isolation that the L1 hypervisor provides. An L2 can
> keep reading and writing pages that the L1 has taken back and reused,
> either for itself or for another L2. The host, other VMs and VMs
> without nested virtualization are not affected, because the shadow
> stage-2 only maps the L1 VM's own memory.
>
> Fixes: 5d476ca57d7d ("KVM: arm64: nv: Add handling of range-based TLBI operations")
> Cc: stable@vger.kernel.org
> Signed-off-by: Dan Carpenter <dan@cryspen.com>
> ---
> This is part of our formal analysis work at Pereq. I'm not super
> familiar with this code. Claude wrote a self test that I used to
> test, but it's crap and not worth preserving.
Is there any chance you could get Claude to decrapify the self-test?
This is the sort of things that would be very valuable to have in our
toolbox.
>
> I also considered widening the range in handle_ripas2e1is(). We would
> need to use the largest block size in that scenario and it seemed like
> it might hurt performance.
It would probably result in over-invalidation, and only benefit guests
that play terrible games (multiple S2s tagged with the same VMID).
We're better off just computing the correct range for the given MMU
context as you are doing here.
>
> arch/arm64/kvm/sys_regs.c | 13 ++++++++++++++--
> 1 file changed, 12 insertions(+), 1 deletions(-)
>
> diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c
> index 3d3f4eb82fbd..e1a687dfeea7 100644
> --- a/arch/arm64/kvm/sys_regs.c
> +++ b/arch/arm64/kvm/sys_regs.c
> @@ -4116,6 +4116,17 @@ union tlbi_info {
> static void s2_mmu_unmap_range(struct kvm_s2_mmu *mmu,
> const union tlbi_info *info)
> {
> + u64 start = info->range.start;
> + u64 end = start + info->range.size;
> +
> + /*
> + * An L1 stage-2 block may be held in the shadow S2 as several smaller
> + * leaves, and R_TVTYQ requires the TLBI to invalidate all of them, so
> + * widen each end of the range to the L1 block containing it.
> + */
> + start = ALIGN_DOWN(start, compute_tlb_inval_range(mmu, start >> 12));
> + end = ALIGN(end, compute_tlb_inval_range(mmu, (end - 1) >> 12));
> +
> /*
> * The unmap operation is allowed to drop the MMU lock and block, which
> * means that @mmu could be used for a different context than the one
> @@ -4139,7 +4150,7 @@ static void s2_mmu_unmap_range(struct kvm_s2_mmu *mmu,
> * the L1 needs to put its stage-2 in a consistent state before doing
> * the TLBI.
> */
> - kvm_stage2_unmap_range(mmu, info->range.start, info->range.size, true);
> + kvm_stage2_unmap_range(mmu, start, end - start, true);
> }
>
> static bool handle_vmalls12e1is(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
The only snag here is that we end-up with two extra walks for the
(pretty common) TLBI VMALLS12E1* case (which by definition covers the
whole IPA space). I don't think I'll lose any sleep over it, but it
may come back to bite us at a later point.
In any case, I'll queue this.
Thanks,
M.
--
Without deviation from the norm, progress is not possible.
prev parent reply other threads:[~2026-11-11 10:08 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-10 6:14 Dan Carpenter
2026-11-11 10:08 ` Marc Zyngier [this message]
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=86o6bv1xzl.wl-maz@kernel.org \
--to=maz@kernel.org \
--cc=catalin.marinas@arm.com \
--cc=clement@cryspen.com \
--cc=dan@cryspen.com \
--cc=fuad.tabba@linux.dev \
--cc=joey.gouly@arm.com \
--cc=karthik@pereq.eu \
--cc=kvmarm@lists.linux.dev \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=oupton@kernel.org \
--cc=seiden@linux.ibm.com \
--cc=suzuki.poulose@arm.com \
--cc=will@kernel.org \
--cc=yuzenghui@huawei.com \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®