* [PATCH v4 0/4] KVM: arm64: nv: VNCR page mapping fixes
@ 2026-12-03 10:58 Jia He
2026-12-03 10:58 ` [PATCH v4 1/4] KVM: arm64: nv: Locate the VNCR page in the host page from its PA Jia He
` (3 more replies)
0 siblings, 4 replies; 7+ messages in thread
From: Jia He @ 2026-12-03 10:58 UTC (permalink / raw)
To: Marc Zyngier, Oliver Upton
Cc: Jia He, Fuad Tabba, Joey Gouly, Steffen Eiden, Suzuki K Poulose,
Zenghui Yu, Catalin Marinas, Will Deacon, Mark Rutland,
Paolo Bonzini, Sean Christopherson, Shuah Khan, linux-arm-kernel,
kvmarm, kvm, linux-kselftest, linux-kernel
Fix the VNCR fixmap offset on hosts with 16K or 64K pages and inject
the fault recorded by the stage-1 walker when an AF update fails.
Add a selftest for both fixes. The last patch allows cacheable PFNMAP
memory to back the VNCR page.
Testing for patch 4:
On an FGT-capable host with L1's RAM backed by cacheable PFNMAP, the L2
hypervisor repeatedly took an SEA before the patch and booted with it.
On Neoverse-N2, an NV2 VTTBR_EL2 read from a cacheable PFNMAP VNCR page
in a read-only memslot took an SEA with gfn_to_hva_memslot() and read
the expected value with gfn_to_hva_memslot_prot().
On a Neoverse-N2 host with 4K pages, both writable and read-only
memslots passed the cacheable PFNMAP read, non-cacheable PFNMAP and
PROT_NONE rejection, and concurrent PFNMAP/shmem replacement tests.
Both 5-second remap runs completed without a spurious SEA or bad read;
an instrumented run confirmed that both the fault-in error and
cacheability failure paths hit concurrent invalidations and retried.
nv_vncr_abort passed 3/3.
Changes since v3:
- Rebase onto v7.4-rc4.
- Patch 1: use the cached HPA in __compute_hcr(), align the address
passed to __set_fixmap(), and clarify the granule requirements (Marc).
- Patch 3: document the 16K and 64K test results and that the test runs
with nested stage-2 disabled (Marc).
- Patch 4: shorten the cacheability helper comment (Marc), move the
test descriptions to the cover letter (Marc), and defer the SEA
injection for PFN fault-in errors and non-cacheable results until
after the existing mmu_invalidate_retry() check under mmu_lock.
Changes since v2:
- New patch 1 (Sashiko).
- Patch 3: size the read-only memslot to whole host pages, and map the
VNCR page at a VA whose bits [15:12] differ from those of its PA
(Sashiko).
Changes since v1:
- New patches 2 and 3 (Sashiko).
- Patch 4: get the HVA with gfn_to_hva_memslot_prot() so that a VNCR
page in a read-only memslot is not rejected (Sashiko).
- Patch 4: limit the stage-2 comparison in the commit message to
VM_PFNMAP and VM_MIXEDMAP VMAs.
v3: https://lore.kernel.org/r/20261128011447.3588988-1-justin.he@arm.com
v2: https://lore.kernel.org/r/20261125024426.3464050-1-justin.he@arm.com
v1: https://lore.kernel.org/r/20261124025336.4107575-1-justin.he@arm.com
Jia He (4):
KVM: arm64: nv: Locate the VNCR page in the host page from its PA
KVM: arm64: nv: Inject an SEA when the VNCR walk cannot set the AF
KVM: arm64: selftests: Add a test for VNCR_EL2 translation faults
KVM: arm64: nv: Allow the L1 VNCR page in cacheable PFNMAP memory
arch/arm64/include/asm/kvm_mmu.h | 2 +
arch/arm64/include/asm/kvm_nested.h | 20 ++
arch/arm64/kvm/hyp/vhe/switch.c | 7 +-
arch/arm64/kvm/mmu.c | 2 +-
arch/arm64/kvm/nested.c | 82 ++++---
tools/testing/selftests/kvm/Makefile.kvm | 1 +
.../selftests/kvm/arm64/nv_vncr_abort.c | 221 ++++++++++++++++++
7 files changed, 296 insertions(+), 39 deletions(-)
create mode 100644 tools/testing/selftests/kvm/arm64/nv_vncr_abort.c
base-commit: b6a9fa34ce4187cb69d3c588a4bbffc31ce5db8f
--
2.34.1
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v4 1/4] KVM: arm64: nv: Locate the VNCR page in the host page from its PA
2026-12-03 10:58 [PATCH v4 0/4] KVM: arm64: nv: VNCR page mapping fixes Jia He
@ 2026-12-03 10:58 ` Jia He
2026-12-03 10:58 ` [PATCH v4 2/4] KVM: arm64: nv: Inject an SEA when the VNCR walk cannot set the AF Jia He
` (2 subsequent siblings)
3 siblings, 0 replies; 7+ messages in thread
From: Jia He @ 2026-12-03 10:58 UTC (permalink / raw)
To: Marc Zyngier, Oliver Upton
Cc: Jia He, Fuad Tabba, Joey Gouly, Steffen Eiden, Suzuki K Poulose,
Zenghui Yu, Catalin Marinas, Will Deacon, Mark Rutland,
Paolo Bonzini, Sean Christopherson, Shuah Khan, linux-arm-kernel,
kvmarm, kvm, linux-kselftest, linux-kernel, Sashiko
The VNCR area is 4K aligned. On a host with 16K or 64K pages, KVM
maps the host page containing the VNCR area at a fixmap address.
__compute_hcr() currently takes the offset within that page from the
guest's VNCR_EL2, which holds a VA.
The guest hypervisor can use a smaller EL2 stage-1 granule than the
host page size. This is possible with nested stage-2 disabled
(HCR_EL2.VM clear), or with a nested stage-2 granule at least as large
as the host page. The VA and PA offsets can then differ, and VNCR
accesses land in the wrong 4K area.
Keep the translated offset in the cached HPA. Align the HPA when
installing the fixmap and use its offset when programming VNCR_EL2.
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://lore.kernel.org/all/sashiko-outbox-232440@kernel.org/
Fixes: 73e1b621b25d ("KVM: arm64: nv: Program host's VNCR_EL2 to the fixmap address")
Signed-off-by: Jia He <justin.he@arm.com>
---
arch/arm64/include/asm/kvm_nested.h | 20 ++++++++++++++++++++
arch/arm64/kvm/hyp/vhe/switch.c | 7 +++++--
arch/arm64/kvm/nested.c | 24 +++---------------------
3 files changed, 28 insertions(+), 23 deletions(-)
diff --git a/arch/arm64/include/asm/kvm_nested.h b/arch/arm64/include/asm/kvm_nested.h
index 0f899addafa8..4b0057413229 100644
--- a/arch/arm64/include/asm/kvm_nested.h
+++ b/arch/arm64/include/asm/kvm_nested.h
@@ -430,6 +430,26 @@ int __kvm_find_s1_desc_level(struct kvm_vcpu *vcpu, u64 va, u64 ipa,
int *level);
/* VNCR management */
+struct vncr_tlb {
+ /* The guest's VNCR_EL2 */
+ u64 gva;
+ struct s1_walk_info wi;
+ struct s1_walk_result wr;
+
+ /* Host physical address of the 4K VNCR page. */
+ u64 hpa;
+ bool hpa_writable;
+
+ /* -1 when not mapped on a CPU */
+ atomic_t cpu;
+
+ /*
+ * true if the TLB is valid. Can only be changed with the
+ * mmu_lock held.
+ */
+ bool valid;
+};
+
int kvm_vcpu_allocate_vncr_tlb(struct kvm_vcpu *vcpu);
int kvm_handle_vncr_abort(struct kvm_vcpu *vcpu);
void kvm_handle_s1e2_tlbi(struct kvm_vcpu *vcpu, u32 inst, u64 val);
diff --git a/arch/arm64/kvm/hyp/vhe/switch.c b/arch/arm64/kvm/hyp/vhe/switch.c
index 3f2390a13173..c6aab5940376 100644
--- a/arch/arm64/kvm/hyp/vhe/switch.c
+++ b/arch/arm64/kvm/hyp/vhe/switch.c
@@ -94,8 +94,11 @@ static u64 __compute_hcr(struct kvm_vcpu *vcpu)
if (guest_hcr & HCR_EL2_NV) {
u64 va = __fix_to_virt(vncr_fixmap(smp_processor_id()));
- /* Inherit the low bits from the actual register */
- va |= __vcpu_sys_reg(vcpu, VNCR_EL2) & GENMASK(PAGE_SHIFT - 1, 0);
+ /*
+ * Inherit the offset within the fixmap page from the PA
+ * obtained after translating the guest's VA.
+ */
+ va |= offset_in_page(vcpu->arch.vncr_tlb->hpa);
write_sysreg_s(va, SYS_VNCR_EL2);
/* Force NV2 in case the guest is forgetful... */
diff --git a/arch/arm64/kvm/nested.c b/arch/arm64/kvm/nested.c
index 4d83281a7a28..ff78d2784db5 100644
--- a/arch/arm64/kvm/nested.c
+++ b/arch/arm64/kvm/nested.c
@@ -19,25 +19,6 @@
#include "sys_regs.h"
#include "vgic/vgic.h"
-struct vncr_tlb {
- /* The guest's VNCR_EL2 */
- u64 gva;
- struct s1_walk_info wi;
- struct s1_walk_result wr;
-
- u64 hpa;
- bool hpa_writable;
-
- /* -1 when not mapped on a CPU */
- atomic_t cpu;
-
- /*
- * true if the TLB is valid. Can only be changed with the
- * mmu_lock held.
- */
- bool valid;
-};
-
/*
* Ratio of live shadow S2 MMU per vcpu. This is a trade-off between
* memory usage and potential number of different sets of S2 PTs in
@@ -1643,7 +1624,7 @@ static int kvm_translate_vncr(struct kvm_vcpu *vcpu, bool *is_gmem)
}
vt->gva = va;
- vt->hpa = pfn << PAGE_SHIFT;
+ vt->hpa = (pfn << PAGE_SHIFT) | offset_in_page(vt->wr.pa);
vt->hpa_writable = writable;
vt->valid = true;
atomic_set(&vt->cpu, -1);
@@ -1805,7 +1786,8 @@ static void kvm_map_l1_vncr(struct kvm_vcpu *vcpu)
*/
if (pgprot_val(prot) != pgprot_val(PAGE_NONE)) {
atomic_set(&vt->cpu, smp_processor_id());
- __set_fixmap(vncr_fixmap(atomic_read(&vt->cpu)), vt->hpa, prot);
+ __set_fixmap(vncr_fixmap(atomic_read(&vt->cpu)),
+ vt->hpa & PAGE_MASK, prot);
host_data_set_flag(L1_VNCR_MAPPED);
}
}
--
2.34.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v4 2/4] KVM: arm64: nv: Inject an SEA when the VNCR walk cannot set the AF
2026-12-03 10:58 [PATCH v4 0/4] KVM: arm64: nv: VNCR page mapping fixes Jia He
2026-12-03 10:58 ` [PATCH v4 1/4] KVM: arm64: nv: Locate the VNCR page in the host page from its PA Jia He
@ 2026-12-03 10:58 ` Jia He
2026-12-03 10:58 ` [PATCH v4 3/4] KVM: arm64: selftests: Add a test for VNCR_EL2 translation faults Jia He
2026-12-03 10:58 ` [PATCH v4 4/4] KVM: arm64: nv: Allow the L1 VNCR page in cacheable PFNMAP memory Jia He
3 siblings, 0 replies; 7+ messages in thread
From: Jia He @ 2026-12-03 10:58 UTC (permalink / raw)
To: Marc Zyngier, Oliver Upton
Cc: Jia He, Fuad Tabba, Joey Gouly, Steffen Eiden, Suzuki K Poulose,
Zenghui Yu, Catalin Marinas, Will Deacon, Mark Rutland,
Paolo Bonzini, Sean Christopherson, Shuah Khan, linux-arm-kernel,
kvmarm, kvm, linux-kselftest, linux-kernel, Sashiko, Yuan Yao
With TCR_EL2.HA set and AF clear, the VNCR stage-1 walk tries to
update the leaf descriptor. If the table is in a read-only memslot,
__kvm_at_swap_desc() returns -EPERM and walk_s1() records an SEA on
the table walk. The VNCR walk uses the EL2&0 regime, so nested stage-2
permissions do not enter into this case.
kvm_handle_vncr_abort() ignores -EPERM and re-enters the guest, which
takes the same abort again. Handle -EPERM and inject the recorded SEA.
On Neoverse-N2 the unpatched case took about 1.3 million exits in five
seconds. With the fix the guest received a level-3 table-walk SEA.
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://lore.kernel.org/all/sashiko-outbox-230439@kernel.org/
Fixes: bff8aa213dee ("KVM: arm64: Implement HW access flag management in stage-1 SW PTW")
Reviewed-by: Yuan Yao <yaoyuan@linux.alibaba.com>
Signed-off-by: Jia He <justin.he@arm.com>
---
arch/arm64/kvm/nested.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/arch/arm64/kvm/nested.c b/arch/arm64/kvm/nested.c
index ff78d2784db5..867bcdd69382 100644
--- a/arch/arm64/kvm/nested.c
+++ b/arch/arm64/kvm/nested.c
@@ -1718,6 +1718,7 @@ int kvm_handle_vncr_abort(struct kvm_vcpu *vcpu)
case -EINVAL:
case -ENOENT:
case -EACCES:
+ case -EPERM:
/*
* Translation failed, inject the corresponding
* exception back to EL2.
--
2.34.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v4 3/4] KVM: arm64: selftests: Add a test for VNCR_EL2 translation faults
2026-12-03 10:58 [PATCH v4 0/4] KVM: arm64: nv: VNCR page mapping fixes Jia He
2026-12-03 10:58 ` [PATCH v4 1/4] KVM: arm64: nv: Locate the VNCR page in the host page from its PA Jia He
2026-12-03 10:58 ` [PATCH v4 2/4] KVM: arm64: nv: Inject an SEA when the VNCR walk cannot set the AF Jia He
@ 2026-12-03 10:58 ` Jia He
2026-12-03 11:16 ` sashiko-bot
2026-12-03 10:58 ` [PATCH v4 4/4] KVM: arm64: nv: Allow the L1 VNCR page in cacheable PFNMAP memory Jia He
3 siblings, 1 reply; 7+ messages in thread
From: Jia He @ 2026-12-03 10:58 UTC (permalink / raw)
To: Marc Zyngier, Oliver Upton
Cc: Jia He, Fuad Tabba, Joey Gouly, Steffen Eiden, Suzuki K Poulose,
Zenghui Yu, Catalin Marinas, Will Deacon, Mark Rutland,
Paolo Bonzini, Sean Christopherson, Shuah Khan, linux-arm-kernel,
kvmarm, kvm, linux-kselftest, linux-kernel
Read VTTBR_EL2 from a nested context with HCR_EL2.{NV,NV2} set, so
that the read goes through the VNCR page. With TCR_EL2.HA set, check
that the read succeeds with a writable table and AF clear, or with a
read-only table and AF set. A read-only table with AF clear must
instead produce a level-3 table-walk SEA.
Use a 4K guest EL2 stage-1 granule and leave HCR_EL2.VM clear. Pick
a VNCR VA whose bits [15:12] differ from those of its PA. On Neoverse-N2
hosts with 16K and 64K pages, the read returned zero with the VA
offset and the expected value with the PA offset.
Signed-off-by: Jia He <justin.he@arm.com>
---
tools/testing/selftests/kvm/Makefile.kvm | 1 +
.../selftests/kvm/arm64/nv_vncr_abort.c | 221 ++++++++++++++++++
2 files changed, 222 insertions(+)
create mode 100644 tools/testing/selftests/kvm/arm64/nv_vncr_abort.c
diff --git a/tools/testing/selftests/kvm/Makefile.kvm b/tools/testing/selftests/kvm/Makefile.kvm
index 17a4256e9081..2cf70b585c03 100644
--- a/tools/testing/selftests/kvm/Makefile.kvm
+++ b/tools/testing/selftests/kvm/Makefile.kvm
@@ -185,6 +185,7 @@ TEST_GEN_PROGS_arm64 += arm64/hello_el2
TEST_GEN_PROGS_arm64 += arm64/host_sve
TEST_GEN_PROGS_arm64 += arm64/hypercalls
TEST_GEN_PROGS_arm64 += arm64/nv_pre_fault_memory_test
+TEST_GEN_PROGS_arm64 += arm64/nv_vncr_abort
TEST_GEN_PROGS_arm64 += arm64/external_aborts
TEST_GEN_PROGS_arm64 += arm64/mmio_sign_ext
TEST_GEN_PROGS_arm64 += arm64/page_fault_test
diff --git a/tools/testing/selftests/kvm/arm64/nv_vncr_abort.c b/tools/testing/selftests/kvm/arm64/nv_vncr_abort.c
new file mode 100644
index 000000000000..0279a9990d28
--- /dev/null
+++ b/tools/testing/selftests/kvm/arm64/nv_vncr_abort.c
@@ -0,0 +1,221 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * nv_vncr_abort - Test KVM's handling of VNCR_EL2 translation faults.
+ *
+ * The guest hypervisor enters a nested context with HCR_EL2.{NV,NV2} set
+ * and reads VTTBR_EL2, which FEAT_NV2 turns into a load from the VNCR page.
+ * KVM translates VNCR_EL2 with its software stage-1 walker. The last-level
+ * table for the VNCR page is placed in a read-only memslot, optionally with
+ * the access flag clear. With TCR_EL2.HA set, KVM then cannot update the
+ * descriptor and must inject an SEA on translation table walk into the
+ * guest hypervisor.
+ *
+ * The VNCR page is mapped at a VA whose bits [15:12] differ from those of
+ * its PA, so that on a host with 16K or 64K pages KVM has to locate the
+ * VNCR page in the host page from its PA.
+ */
+#include <signal.h>
+
+#include "kvm_util.h"
+#include "processor.h"
+#include "test_util.h"
+#include "ucall.h"
+
+#include <asm/sysreg.h>
+
+#define VNCR_BASE_VA 0x80000000UL
+#define RO_PT_GPA 0x100000000UL
+#define RO_PT_SLOT 10
+#define VNCR_VTTBR_EL2 0x20
+#define VNCR_MAGIC 0x5a5a1234a5a5beefUL
+#define TIMEOUT_SEC 5
+
+enum test_mode {
+ RW_PT_AF_CLEAR, /* AF set by KVM, VNCR page mapped */
+ RO_PT_AF_SET, /* nothing to update, VNCR page mapped */
+ RO_PT_AF_CLEAR, /* AF update fails, SEA expected */
+};
+
+static const char * const mode_name[] = {
+ [RW_PT_AF_CLEAR] = "read-write table, AF clear",
+ [RO_PT_AF_SET] = "read-only table, AF set",
+ [RO_PT_AF_CLEAR] = "read-only table, AF clear",
+};
+
+static bool expect_sea;
+static u64 vncr_va;
+
+#define copy_el2_to_el1(reg) \
+ write_sysreg_s(read_sysreg_s(SYS_##reg##_EL1), SYS_##reg##_EL12)
+
+/*
+ * Runs in the nested context. No stack: SP_EL1 is not set up. The read of
+ * VTTBR_EL2 is redirected to the VNCR page by FEAT_NV2, and its value is
+ * handed back to the guest hypervisor through HVC.
+ */
+asm(".pushsection .text, \"ax\"\n"
+ ".global l2_code\n"
+ "l2_code:\n"
+ " mrs x0, S3_4_C2_C1_0\n" /* VTTBR_EL2 */
+ " hvc #0\n"
+ " b .\n"
+ ".popsection\n");
+extern char l2_code[];
+
+static void hvc_handler(struct ex_regs *regs)
+{
+ __GUEST_ASSERT(!expect_sea, "expected SEA, VNCR read returned %lx",
+ regs->regs[0]);
+ GUEST_ASSERT_EQ(regs->regs[0], VNCR_MAGIC);
+ GUEST_DONE();
+}
+
+static void dabt_handler(struct ex_regs *regs)
+{
+ u64 esr = read_sysreg(esr_el1);
+
+ __GUEST_ASSERT(expect_sea, "unexpected data abort, esr %lx", esr);
+ __GUEST_ASSERT(esr & ESR_ELx_VNCR, "not a VNCR abort, esr %lx", esr);
+ __GUEST_ASSERT((esr & ESR_ELx_FSC) == ESR_ELx_FSC_SEA_TTW(3),
+ "unexpected FSC, esr %lx", esr);
+ GUEST_DONE();
+}
+
+static void guest_code(void)
+{
+ sysreg_clear_set(tcr_el1, 0, TCR_HA);
+ isb();
+
+ /* Reuse the stage-1 MMU context from EL2 in the nested context */
+ copy_el2_to_el1(SCTLR);
+ copy_el2_to_el1(MAIR);
+ copy_el2_to_el1(TCR);
+ copy_el2_to_el1(TTBR0);
+ copy_el2_to_el1(TTBR1);
+
+ write_sysreg_s(vncr_va, SYS_VNCR_EL2);
+ write_sysreg(0, vtcr_el2);
+ write_sysreg(0, vttbr_el2);
+ sysreg_clear_set(hcr_el2, HCR_EL2_TGE | HCR_EL2_VM,
+ HCR_EL2_NV | HCR_EL2_NV2);
+
+ write_sysreg((u64)l2_code, elr_el2);
+ write_sysreg(PSR_MODE_EL1h | PSR_D_BIT | PSR_A_BIT | PSR_I_BIT | PSR_F_BIT,
+ spsr_el2);
+ isb();
+ asm volatile("eret");
+
+ GUEST_FAIL("ERET to the nested context returned");
+}
+
+/*
+ * Map the VNCR page and move its last-level table to a read-only memslot.
+ * The guest never accesses vncr_va itself, so only KVM walks that table.
+ */
+static void setup_vncr_page(struct kvm_vm *vm, enum test_mode mode)
+{
+ u64 oa_mask = GENMASK_ULL(47, 12);
+ gpa_t data_gpa, table_gpa;
+ u64 *l2e, *table;
+ int idx;
+
+ data_gpa = vm_phy_page_alloc(vm, MEM_REGION_DATA);
+ vncr_va = VNCR_BASE_VA | ((data_gpa + 0x1000) & GENMASK_ULL(15, 12));
+ idx = (vncr_va >> 12) & 511;
+ virt_pg_map(vm, vncr_va, data_gpa);
+ *(u64 *)(addr_gpa2hva(vm, data_gpa) + VNCR_VTTBR_EL2) = VNCR_MAGIC;
+
+ l2e = virt_get_pte_hva_at_level(vm, vncr_va, 2);
+ TEST_ASSERT((*l2e & 3) == 3, "level 2 entry is not a table: %lx", *l2e);
+ table_gpa = *l2e & oa_mask;
+ table = addr_gpa2hva(vm, table_gpa);
+
+ if (mode == RW_PT_AF_CLEAR) {
+ table[idx] &= ~PTE_AF;
+ return;
+ }
+
+ /* A memslot must cover whole host pages */
+ vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, RO_PT_GPA, RO_PT_SLOT,
+ vm_adjust_num_guest_pages(vm->mode, 1),
+ KVM_MEM_READONLY);
+ memcpy(addr_gpa2hva(vm, RO_PT_GPA), table, vm->page_size);
+ if (mode == RO_PT_AF_CLEAR)
+ ((u64 *)addr_gpa2hva(vm, RO_PT_GPA))[idx] &= ~PTE_AF;
+
+ *l2e = (*l2e & ~oa_mask) | RO_PT_GPA;
+}
+
+static void sigalrm_handler(int sig)
+{
+}
+
+static void run_test(enum test_mode mode)
+{
+ struct kvm_vcpu_init init;
+ struct kvm_vcpu *vcpu;
+ struct kvm_vm *vm;
+ struct ucall uc;
+ u64 mmfr1;
+ int ret;
+
+ vm = vm_create(1);
+ kvm_get_default_vcpu_target(vm, &init);
+ init.features[0] |= BIT(KVM_ARM_VCPU_HAS_EL2);
+ vcpu = aarch64_vcpu_add(vm, 0, &init, guest_code);
+ kvm_arch_vm_finalize_vcpus(vm);
+
+ mmfr1 = vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(SYS_ID_AA64MMFR1_EL1));
+ __TEST_REQUIRE(SYS_FIELD_GET(ID_AA64MMFR1_EL1, HAFDBS, mmfr1),
+ "Hardware AF management not supported");
+ TEST_REQUIRE(vm->page_size == 4096);
+
+ vm_init_descriptor_tables(vm);
+ vcpu_init_descriptor_tables(vcpu);
+ vm_install_sync_handler(vm, VECTOR_SYNC_LOWER_64, ESR_ELx_EC_HVC64,
+ hvc_handler);
+ vm_install_sync_handler(vm, VECTOR_SYNC_LOWER_64, ESR_ELx_EC_DABT_LOW,
+ dabt_handler);
+ vm_install_sync_handler(vm, VECTOR_SYNC_LOWER_64, ESR_ELx_EC_DABT_CUR,
+ dabt_handler);
+
+ expect_sea = mode == RO_PT_AF_CLEAR;
+ sync_global_to_guest(vm, expect_sea);
+ setup_vncr_page(vm, mode);
+ sync_global_to_guest(vm, vncr_va);
+
+ alarm(TIMEOUT_SEC);
+ ret = __vcpu_run(vcpu);
+ alarm(0);
+ TEST_ASSERT(!ret || errno != EINTR,
+ "%s: vCPU made no progress in %ds (%lu exits)",
+ mode_name[mode], TIMEOUT_SEC, vcpu_get_stat(vcpu, exits));
+ TEST_ASSERT(!ret, KVM_IOCTL_ERROR(KVM_RUN, ret));
+
+ switch (get_ucall(vcpu, &uc)) {
+ case UCALL_DONE:
+ pr_info("%s: ok\n", mode_name[mode]);
+ break;
+ case UCALL_ABORT:
+ REPORT_GUEST_ASSERT(uc);
+ break;
+ default:
+ TEST_FAIL("%s: unexpected ucall %lu", mode_name[mode], uc.cmd);
+ }
+
+ kvm_vm_free(vm);
+}
+
+int main(void)
+{
+ struct sigaction sa = { .sa_handler = sigalrm_handler };
+
+ TEST_REQUIRE(kvm_check_cap(KVM_CAP_ARM_EL2));
+ TEST_ASSERT(!sigaction(SIGALRM, &sa, NULL), "sigaction failed");
+
+ run_test(RW_PT_AF_CLEAR);
+ run_test(RO_PT_AF_SET);
+ run_test(RO_PT_AF_CLEAR);
+
+ return 0;
+}
--
2.34.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v4 4/4] KVM: arm64: nv: Allow the L1 VNCR page in cacheable PFNMAP memory
2026-12-03 10:58 [PATCH v4 0/4] KVM: arm64: nv: VNCR page mapping fixes Jia He
` (2 preceding siblings ...)
2026-12-03 10:58 ` [PATCH v4 3/4] KVM: arm64: selftests: Add a test for VNCR_EL2 translation faults Jia He
@ 2026-12-03 10:58 ` Jia He
2026-12-03 11:16 ` sashiko-bot
3 siblings, 1 reply; 7+ messages in thread
From: Jia He @ 2026-12-03 10:58 UTC (permalink / raw)
To: Marc Zyngier, Oliver Upton
Cc: Jia He, Fuad Tabba, Joey Gouly, Steffen Eiden, Suzuki K Poulose,
Zenghui Yu, Catalin Marinas, Will Deacon, Mark Rutland,
Paolo Bonzini, Sean Christopherson, Shuah Khan, linux-arm-kernel,
kvmarm, kvm, linux-kselftest, linux-kernel
An L1 hypervisor can use cacheable PFNMAP memory as RAM and put its
VNCR page there. KVM currently rejects the PFN if it lacks a linear
mapping and injects an SEA on the first VNCR access.
For PFNs outside the linear map, use the stage-2 VMA cacheability
check for VM_PFNMAP and VM_MIXEDMAP mappings that KVM can fault in.
Require FWB and DIC. Use gfn_to_hva_memslot_prot() to look up the
VMA even for a read-only memslot.
During a concurrent MAP_FIXED replacement, GUP can fail on the old
PFNMAP VMA and the fallback can find the new shmem VMA. The existing
fault-in error path injects an SEA before checking for invalidation.
The cacheability lookup can also see a replacement VMA. Check the
invalidation sequence before injecting an SEA for either failure.
Signed-off-by: Jia He <justin.he@arm.com>
---
arch/arm64/include/asm/kvm_mmu.h | 2 ++
arch/arm64/kvm/mmu.c | 2 +-
arch/arm64/kvm/nested.c | 57 +++++++++++++++++++++++---------
3 files changed, 45 insertions(+), 16 deletions(-)
diff --git a/arch/arm64/include/asm/kvm_mmu.h b/arch/arm64/include/asm/kvm_mmu.h
index 32392abf2e21..e59c6603b137 100644
--- a/arch/arm64/include/asm/kvm_mmu.h
+++ b/arch/arm64/include/asm/kvm_mmu.h
@@ -385,6 +385,8 @@ static inline void kvm_fault_unlock(struct kvm *kvm)
read_unlock(&kvm->mmu_lock);
}
+bool kvm_vma_is_cacheable(struct vm_area_struct *vma);
+
/*
* ARM64 KVM relies on a simple conversion from physaddr to a kernel
* virtual address (KVA) when it does cache maintenance as the CMO
diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c
index 1db10d8d6db7..cbd563ba4dd9 100644
--- a/arch/arm64/kvm/mmu.c
+++ b/arch/arm64/kvm/mmu.c
@@ -1639,7 +1639,7 @@ static bool kvm_vma_mte_allowed(struct vm_area_struct *vma)
return vma->vm_flags & VM_MTE_ALLOWED;
}
-static bool kvm_vma_is_cacheable(struct vm_area_struct *vma)
+bool kvm_vma_is_cacheable(struct vm_area_struct *vma)
{
switch (FIELD_GET(PTE_ATTRINDX_MASK, pgprot_val(vma->vm_page_prot))) {
case MT_NORMAL_NC:
diff --git a/arch/arm64/kvm/nested.c b/arch/arm64/kvm/nested.c
index 867bcdd69382..e29fb1a8a9ba 100644
--- a/arch/arm64/kvm/nested.c
+++ b/arch/arm64/kvm/nested.c
@@ -1534,10 +1534,38 @@ static u64 read_vncr_el2(struct kvm_vcpu *vcpu)
return (u64)sign_extend64(__vcpu_sys_reg(vcpu, VNCR_EL2), 48);
}
+/*
+ * Allow a non-memory PFN only if it comes from a cacheable VM_PFNMAP or
+ * VM_MIXEDMAP VMA and the system supports cacheable PFNMAP at stage-2.
+ */
+static bool vncr_pfn_is_cacheable(struct kvm_memory_slot *memslot, gfn_t gfn,
+ kvm_pfn_t pfn)
+{
+ struct vm_area_struct *vma;
+ unsigned long hva;
+ bool ret = false;
+
+ if (pfn_is_map_memory(pfn))
+ return true;
+
+ if (!kvm_supports_cacheable_pfnmap())
+ return false;
+
+ hva = gfn_to_hva_memslot_prot(memslot, gfn, NULL);
+
+ mmap_read_lock(current->mm);
+ vma = vma_lookup(current->mm, hva);
+ if (vma && (vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP)))
+ ret = kvm_vma_is_cacheable(vma);
+ mmap_read_unlock(current->mm);
+
+ return ret;
+}
+
static int kvm_translate_vncr(struct kvm_vcpu *vcpu, bool *is_gmem)
{
struct kvm_memory_slot *memslot;
- bool write_fault, writable;
+ bool write_fault, writable, cacheable;
unsigned long mmu_seq;
struct vncr_tlb *vt;
struct page *page = NULL;
@@ -1591,10 +1619,6 @@ static int kvm_translate_vncr(struct kvm_vcpu *vcpu, bool *is_gmem)
if (!*is_gmem) {
pfn = __kvm_faultin_pfn(memslot, gfn, write_fault ? FOLL_WRITE : 0,
&writable, &page);
- if (is_error_noslot_pfn(pfn)) {
- fail_s1_walk(&vt->wr, ESR_ELx_FSC_EXTABT, false);
- return -EFAULT;
- }
} else {
ret = kvm_gmem_get_pfn(vcpu->kvm, memslot, gfn, &pfn, NULL);
if (ret) {
@@ -1606,16 +1630,8 @@ static int kvm_translate_vncr(struct kvm_vcpu *vcpu, bool *is_gmem)
writable = !(memslot->flags & KVM_MEM_READONLY);
}
- /*
- * FIXME: This check is too restrictive as KVM allows cacheable memory
- * attributes for PFNMAP VMAs that have cacheable attributes in host
- * stage-1.
- */
- if (!pfn_is_map_memory(pfn)) {
- kvm_release_faultin_page(vcpu->kvm, page, true, false);
- fail_s1_walk(&vt->wr, ESR_ELx_FSC_EXTABT, false);
- return -EINVAL;
- }
+ cacheable = !is_error_noslot_pfn(pfn) &&
+ vncr_pfn_is_cacheable(memslot, gfn, pfn);
scoped_guard(write_lock, &vcpu->kvm->mmu_lock) {
if (mmu_invalidate_retry(vcpu->kvm, mmu_seq)) {
@@ -1623,6 +1639,17 @@ static int kvm_translate_vncr(struct kvm_vcpu *vcpu, bool *is_gmem)
return -EAGAIN;
}
+ if (is_error_noslot_pfn(pfn)) {
+ fail_s1_walk(&vt->wr, ESR_ELx_FSC_EXTABT, false);
+ return -EFAULT;
+ }
+
+ if (!cacheable) {
+ kvm_release_faultin_page(vcpu->kvm, page, true, false);
+ fail_s1_walk(&vt->wr, ESR_ELx_FSC_EXTABT, false);
+ return -EINVAL;
+ }
+
vt->gva = va;
vt->hpa = (pfn << PAGE_SHIFT) | offset_in_page(vt->wr.pa);
vt->hpa_writable = writable;
--
2.34.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH v4 4/4] KVM: arm64: nv: Allow the L1 VNCR page in cacheable PFNMAP memory
2026-12-03 10:58 ` [PATCH v4 4/4] KVM: arm64: nv: Allow the L1 VNCR page in cacheable PFNMAP memory Jia He
@ 2026-12-03 11:16 ` sashiko-bot
0 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-12-03 11:16 UTC (permalink / raw)
To: Jia He; +Cc: kvmarm, kvm, Oliver Upton, Marc Zyngier
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] KVM: arm64: vncr_pfn_is_cacheable() checks VMA prot instead of
PTE prot
--
commit 08fdf40109728fb074e9e46b68fb679e51daf3c0
Author: Jia He <justin.he@arm.com>
KVM: arm64: nv: Allow the L1 VNCR page in cacheable PFNMAP memory
This commit allows the L1 VNCR page to reside in cacheable VM_PFNMAP or
VM_MIXEDMAP memory outside the linear map when the host supports FWB and
DIC, and checks the MMU invalidation sequence before injecting an SEA on
fault-in or cacheability failures.
> diff --git a/arch/arm64/kvm/nested.c b/arch/arm64/kvm/nested.c
> index 867bcdd693828..e29fb1a8a9ba3 100644
> --- a/arch/arm64/kvm/nested.c
> +++ b/arch/arm64/kvm/nested.c
> @@ -1534,10 +1534,38 @@ static u64 read_vncr_el2(struct kvm_vcpu *vcpu)
> return (u64)sign_extend64(__vcpu_sys_reg(vcpu, VNCR_EL2), 48);
> }
>
> +/*
> + * Allow a non-memory PFN only if it comes from a cacheable VM_PFNMAP or
> + * VM_MIXEDMAP VMA and the system supports cacheable PFNMAP at stage-2.
> + */
> +static bool vncr_pfn_is_cacheable(struct kvm_memory_slot *memslot, gfn_t gfn,
> + kvm_pfn_t pfn)
> +{
> + struct vm_area_struct *vma;
> + unsigned long hva;
> + bool ret = false;
> +
> + if (pfn_is_map_memory(pfn))
> + return true;
> +
> + if (!kvm_supports_cacheable_pfnmap())
> + return false;
> +
> + hva = gfn_to_hva_memslot_prot(memslot, gfn, NULL);
> +
> + mmap_read_lock(current->mm);
> + vma = vma_lookup(current->mm, hva);
> + if (vma && (vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP)))
> + ret = kvm_vma_is_cacheable(vma);
> + mmap_read_unlock(current->mm);
> +
> + return ret;
> +}
[Severity: High]
Can kvm_vma_is_cacheable() misclassify device MMIO or non-cacheable PFNs
as cacheable when the backing driver uses vmf_insert_pfn_prot()?
kvm_vma_is_cacheable() only inspects vma->vm_page_prot, whereas drivers
using vmf_insert_pfn_prot() can leave vma->vm_page_prot cacheable and
set non-cacheable PTE attributes on fault. For example,
panthor_mmio_vm_fault() maps GPU MMIO with pgprot_noncached().
If userspace backs a memslot with such a VMA and points VNCR_EL2 at it,
kvm_map_l1_vncr() will map the device MMIO page into the host EL2 fixmap
with PAGE_KERNEL or PAGE_KERNEL_RO attributes. Cacheable accesses or
writebacks through the fixmap can then trigger a host EL2 SError.
Since follow_pfnmap_start() already resolves the actual PTE pgprot during
__kvm_faultin_pfn(), checking the PTE attributes instead of
vma->vm_page_prot would avoid this mismatch.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203105805.165749-1-justin.he@arm.com?part=4
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v4 3/4] KVM: arm64: selftests: Add a test for VNCR_EL2 translation faults
2026-12-03 10:58 ` [PATCH v4 3/4] KVM: arm64: selftests: Add a test for VNCR_EL2 translation faults Jia He
@ 2026-12-03 11:16 ` sashiko-bot
0 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-12-03 11:16 UTC (permalink / raw)
To: Jia He; +Cc: kvmarm, kvm, Oliver Upton, Marc Zyngier
Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Medium] KVM: arm64: selftests: missing vGICv3 and NV=0 check via
test_supports_el2()
- [Low] KVM: arm64: selftests: vcpu_get_stat() in TEST_ASSERT() can
clobber errno
- [Low] KVM: arm64: selftests: nv_vncr_abort omits kselftest TAP
reporting wrappers
- [Low] KVM: arm64: selftests: empty sigalrm_handler() mishandles EINTR
and timeout
--
commit 417636aa268598b9f55088e6899197d6effa3e67
Author: Jia He <justin.he@arm.com>
KVM: arm64: selftests: Add a test for VNCR_EL2 translation faults
This commit adds an arm64 KVM selftest that enters a nested EL2 context
with HCR_EL2.{NV,NV2} and TCR_EL2.HA set, reads VTTBR_EL2 through the VNCR
page, and verifies KVM's handling of writable and read-only translation
tables with the access flag set or clear.
> diff --git a/tools/testing/selftests/kvm/arm64/nv_vncr_abort.c b/tools/testing/selftests/kvm/arm64/nv_vncr_abort.c
> new file mode 100644
> index 0000000000000..0279a9990d28a
> --- /dev/null
> +++ b/tools/testing/selftests/kvm/arm64/nv_vncr_abort.c
[ ... ]
> +static void sigalrm_handler(int sig)
> +{
> +}
[ ... ]
> + alarm(TIMEOUT_SEC);
> + ret = __vcpu_run(vcpu);
> + alarm(0);
[Severity: Low]
Can an unrelated signal cause a false timeout here, or can a lost SIGALRM
leave __vcpu_run() hung indefinitely?
sigalrm_handler() does not record SIGALRM delivery, and __vcpu_run() is not
retried on EINTR, so any unrelated signal triggers a false timeout failure.
Conversely, if the one-shot alarm fires before KVM_RUN entry, the empty
handler consumes it and __vcpu_run() can block with no timer armed.
Recording SIGALRM in a volatile sig_atomic_t flag, retrying __vcpu_run()
while that flag is clear, and checking it around entry (or using a periodic
ITIMER_REAL) would handle both cases.
> + TEST_ASSERT(!ret || errno != EINTR,
> + "%s: vCPU made no progress in %ds (%lu exits)",
> + mode_name[mode], TIMEOUT_SEC, vcpu_get_stat(vcpu, exits));
> + TEST_ASSERT(!ret, KVM_IOCTL_ERROR(KVM_RUN, ret));
[Severity: Low]
Could vcpu_get_stat(vcpu, exits) clobber errno before it is checked here?
TEST_ASSERT() expands to a function call, so vcpu_get_stat() runs on every
call in unspecified order relative to errno != EINTR, and before the next
TEST_ASSERT() reads errno. On first use, kvm_get_stat() calls calloc() and
pread(), which may overwrite errno and corrupt the failure diagnostic.
Saving errno immediately after __vcpu_run() and only calling vcpu_get_stat()
in the timeout failure branch avoids this and skips the stats lookup on
passing runs.
[ ... ]
> + TEST_REQUIRE(kvm_check_cap(KVM_CAP_ARM_EL2));
[Severity: Medium]
Should this use test_supports_el2() instead of checking KVM_CAP_ARM_EL2
directly?
KVM_CAP_ARM_EL2 does not check for vGICv3 support, so on hosts without
vGICv3 kvm_arch_vm_post_create() leaves vm->arch.has_gic false and the first
KVM_RUN fails with -EAGAIN in kvm_vcpu_first_run_init() ->
kvm_vgic_vcpu_nv_init() -> kvm_vgic_set_owner() instead of skipping.
Using test_supports_el2() also respects the NV=0 environment override used
by other arm64 nested virtualization selftests.
> + TEST_ASSERT(!sigaction(SIGALRM, &sa, NULL), "sigaction failed");
> +
> + run_test(RW_PT_AF_CLEAR);
> + run_test(RO_PT_AF_SET);
> + run_test(RO_PT_AF_CLEAR);
> +
> + return 0;
> +}
[Severity: Low]
This isn't a bug, but the test prints pass lines with pr_info() in
run_test() and returns 0 directly from main() instead of using the kselftest
TAP wrappers.
Checking prerequisites before calling ksft_print_header() and
ksft_set_plan(), then reporting each case with ksft_test_result_pass() and
ending with ksft_finished(), would match kselftest conventions and sibling
arm64 KVM selftests.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203105805.165749-1-justin.he@arm.com?part=3
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-12-03 11:16 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 10:58 [PATCH v4 0/4] KVM: arm64: nv: VNCR page mapping fixes Jia He
2026-12-03 10:58 ` [PATCH v4 1/4] KVM: arm64: nv: Locate the VNCR page in the host page from its PA Jia He
2026-12-03 10:58 ` [PATCH v4 2/4] KVM: arm64: nv: Inject an SEA when the VNCR walk cannot set the AF Jia He
2026-12-03 10:58 ` [PATCH v4 3/4] KVM: arm64: selftests: Add a test for VNCR_EL2 translation faults Jia He
2026-12-03 11:16 ` sashiko-bot
2026-12-03 10:58 ` [PATCH v4 4/4] KVM: arm64: nv: Allow the L1 VNCR page in cacheable PFNMAP memory Jia He
2026-12-03 11:16 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox