From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.167.200 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796170418; b=xQ+ObyYLKwkEWN9fwhwkntpilwbKc3V5wC3hNG/0B1P4XRHnI+dEu/A0yHFIlczOcRe1 TI3XFkqHND70LYfTJd/aAY5z9rDFmv3yqCiZjVdxWRKBf9ndRzIEQA76PntUumW3oTj35 8tkMFZsh7g1pfRf10tOG4T1E+whawMoNVyoODYnjy1e1XxDYzRCV9WU+ucTyN7NHOmCVa kjG94QIKK9w5ME2F4UJs5Pfx2bmQxB5mGjEH99ekxnPkEngYAcPds1fDPgBL1s2QyWH/w qsDTRCUPA4TSjrEBKS7D4OJd+eFWjd8CdeyyWK5qX5TfYb649i8tWF6YIMm5bbEwwBQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796170418; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=OVdZ7nEYSjbmdas8KwAVCG7B+bFdqh6CLGD3hNa2hxA=; b=cYg6MNqCeiafchLFxHdKIUgl5GCWpp2u4+waH7Zs3vJTGeqcmAj7fQBMpr+9nt+wim3x aJGgm4dR5St8h6P22NfTkzXtPTYjP2NWfDPIo9jVauZ1GFuXE6tyhQip/BIWJ6oOd4yzg /i37TvEadtjc5N2xcNxmPBRKxNrVnlHsovTpYRQOgHteQf4fzbZ1x0lQeE3vUgZhbxQPi XrljV+UWgb5UnTd/K+t59RQ/tdPOGCv1lZHoofcsLsC8ll2g1GnQCvy2iPL3wtI0kb5Kj QGZgDfd2vheJEXxbfakuPmRpIfZHkgWMVKqXkccQYWQxWs5kAMdUmfAgAcUimvP+ewg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--coltonlewis.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=m93Lhp3e; arc=none smtp.remote-ip=209.85.167.200 Received: from mail-oi1-f200.google.com (mail-oi1-f200.google.com [209.85.167.200]) (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 9CCC6606A327 for ; Wed, 2 Dec 2026 00:13:37 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 9CCC6606A327 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=flex--coltonlewis.bounces.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=m93Lhp3e Received: by mail-oi1-f200.google.com with SMTP id 5614622812f47-4dc57e2a3f2so15192648b6e.0 for ; Tue, 01 Dec 2026 16:13:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796170416; x=1796775216; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=OVdZ7nEYSjbmdas8KwAVCG7B+bFdqh6CLGD3hNa2hxA=; b=m93Lhp3eQ8mmFy8Uvqp2lQsV6I4U6xiKCpQ0I9aCRriTlPTQluhr3qUyppBN0OdFEN +czdV9yruHCiZE6kyw8uUcLsMv6SGl2MSKV389e4aSj0I9T8R2xxhdfnbV1TzTHZDeCl fKwlFZiEH2kKeK62zY5zR//lr8cWV5i/nOlovtvYFSKwB1cwfdqFzBnV73dWCR4aS2gg rQMzCnwArisyx8YXQLvA4obrOnfyzpJhAAVyeBWL+BQDNmOBz/KmO1Wq12/zifWkRN52 L4R8v98jgYPYaBhYbRtfHicMtamDwqQhihtrE868J6idC9UTSOJIgkem3+BQYrdoyCoC dIAQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796170416; x=1796775216; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=OVdZ7nEYSjbmdas8KwAVCG7B+bFdqh6CLGD3hNa2hxA=; b=Ah5XbcXtXsSB+WPlmVUNYpqn+8R/hGguSmW7RDBMF+KVqL3FEYGRisuVUnpTK989pr YUlNCq6uVM9yD2Iq8DBAK1q4kIoTtI6vq006FdGxCAxANPRLR5fIf9MWjEdrvK7G1+dB /jsK3DXtjwDZbEkodYrhjFss23mUEwVet2GCxMdonpdBes2iVgouWWV9qScQ/NLoi3Y2 8z6y8xpYC9FhChtcBXMWM/S1q0zQQ9nHCStk3Dh0GeLKT7avlYWLz/3svqqJjaAxvDv5 hQN9a7UngX4rto3cI5yNJPnOv3n/o7l+54N2Enc/LdE/wCWyDALTTLrLU8sUTazYIf9F EIgg== X-Forwarded-Encrypted: i=1; AL+hErFzjAUhAaxjn4ju0k2lwdUA9SbVFzwHwwYWMoVh4MleEYoETawo3uLs/kk1LQ+zW2+oY9I7gd8X0c5UW34=@vger.kernel.org X-Gm-Message-State: APuU4Z/YiNhGvE5tqbf+7fQFo6xY9/upLs0Y6cCxuY/NC8xUsSjG0nQX ufGEuX7KGcsRCmrUwyKptFY+7KDkbdod9WGWy/J7PzZMqmtwdU2ub1DxGD6DM23PaRckZ9poXLK XfRugoozG/XCZEEVBQfguXwvHjg== X-Received: from ilbel20-n1.prod.google.com ([2002:a05:6e02:5014:10b0:54e:ab26:adde]) (user=coltonlewis job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6808:c3cb:b0:4f6:a430:1822 with SMTP id 5614622812f47-57a056c86f8mr42311218b6e.7.1796170416338; Tue, 01 Dec 2026 16:13:36 -0800 (PST) Date: Wed, 2 Dec 2026 00:13:34 +0000 In-Reply-To: <20261202001335.2306714-1-coltonlewis@google.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261202001335.2306714-1-coltonlewis@google.com> X-Mailer: git-send-email 2.56.1.911.g883d8bcb20-goog Message-ID: <20261202001335.2306714-2-coltonlewis@google.com> Subject: [PATCH v3 1/2] KVM: arm64: nv: Remove unmapped guest stage-2 mapping entries From: Colton Lewis To: Marc Zyngier , Oliver Upton Cc: Wei-Lin Chang , Fuad Tabba , Joey Gouly , Steffen Eiden , Suzuki K Poulose , Zenghui Yu , Catalin Marinas , Will Deacon , Mark Rutland , kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" kvm_pgtable_stage2_unmap_tracked() can be called with a size smaller than a tracked block mapping (for example, from a TLBI RIPAS2E1IS range or a TLBI IPAS2E1IS with a 4kB TTL hint on a 2MB block). kvm_pgtable_stage2_unmap() clears the entire block in the shadow stage-2 page table, but kvm_remove_guest_s2_mappings() only removes tracking entries fully enclosed by the unmap range, leaving a stale kvm_guest_s2_mapping entry behind in guest_s2_mappings. Partially overlapping entries cannot be removed unconditionally, because the tracked range may instead contain smaller page mappings in a child table (such as under dirty logging) that remain valid outside the unmapped range. On a partial overlap, look up nested_start in the shadow page table with kvm_pgtable_get_leaf() and remove the tracking entry when no valid PTE or child table remains at its tracked granule size (!kvm_pte_valid(pte) && kvm_granule_size(level) >= map_size). Since kvm_pgtable_stage2_unmap() has already unlinked any empty child tables, a single lookup at nested_start is enough to tell whether the entire tracked range is empty. Fixes: 5491980c1a41 ("KVM: arm64: nv: Track guest stage-2 mapping removal") Signed-off-by: Colton Lewis --- arch/arm64/kvm/nested.c | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/arch/arm64/kvm/nested.c b/arch/arm64/kvm/nested.c index 78d5b18dbd2d6..3dcf2ea54873e 100644 --- a/arch/arm64/kvm/nested.c +++ b/arch/arm64/kvm/nested.c @@ -928,6 +928,27 @@ void kvm_record_guest_s2_mapping(struct kvm_s2_mmu *mmu, gpa_t canonical_ipa, interval_tree_insert(&mapping->canonical, &kvm->arch.mmu.guest_s2_mappings); } +/* + * Each tracked range [nested_start, nested_start + map_size - 1] is aligned + * to one host stage-2 granule. After kvm_pgtable_stage2_unmap() unlinks any + * empty child tables, an invalid leaf at or above that granule level means + * neither a block mapping nor a child table remains in the tracked range. + */ +static bool guest_s2_mapping_unmapped(struct kvm_s2_mmu *mmu, + gpa_t nested_start, size_t map_size) +{ + kvm_pte_t pte; + s8 level; + + if (!mmu->pgt) + return true; + + if (kvm_pgtable_get_leaf(mmu->pgt, nested_start, &pte, &level, 0)) + return false; + + return !kvm_pte_valid(pte) && kvm_granule_size(level) >= map_size; +} + void kvm_remove_guest_s2_mappings(struct kvm_s2_mmu *mmu, gpa_t nipa, size_t size) { @@ -944,18 +965,24 @@ void kvm_remove_guest_s2_mappings(struct kvm_s2_mmu *mmu, gpa_t nipa, node = interval_tree_iter_first(&mmu->guest_s2_mappings, nipa, nipa_end); while (node) { - unsigned long nested_start; + unsigned long nested_start, nested_end; + size_t map_size; next = interval_tree_iter_next(node, nipa, nipa_end); mapping = container_of(node, struct kvm_guest_s2_mapping, nested); /* - * Tracking must be conservative on removal, only remove - * mappings that are within the unmap range. + * Remove the tracking entry if it is fully enclosed by the + * unmap range, or if a partial unmap left no shadow mappings + * anywhere in its tracked range. */ nested_start = s2_mapping_to_nested_start(mapping); - if (nipa <= nested_start && nipa_end >= mapping->nested.last) { - interval_tree_remove(&mapping->nested, &mmu->guest_s2_mappings); + nested_end = mapping->nested.last; + map_size = nested_end - nested_start + 1; + if ((nipa <= nested_start && nipa_end >= nested_end) || + guest_s2_mapping_unmapped(mmu, nested_start, map_size)) { + interval_tree_remove(&mapping->nested, + &mmu->guest_s2_mappings); interval_tree_remove(&mapping->canonical, &kvm->arch.mmu.guest_s2_mappings); kfree(mapping); -- 2.56.1.911.g883d8bcb20-goog