mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Justin He <Justin.He@arm.com>
To: Yao Yuan <yaoyuan@linux.alibaba.com>
Cc: Marc Zyngier <maz@kernel.org>, Oliver Upton <oupton@kernel.org>,
	Fuad Tabba <fuad.tabba@linux.dev>,
	Joey Gouly <Joey.Gouly@arm.com>,
	Steffen Eiden <seiden@linux.ibm.com>,
	Suzuki 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>,
	Paolo Bonzini <pbonzini@redhat.com>,
	Sean Christopherson <seanjc@google.com>,
	Shuah Khan <shuah@kernel.org>,
	Sascha Bischoff <Sascha.Bischoff@arm.com>,
	"linux-arm-kernel@lists.infradead.org"
	<linux-arm-kernel@lists.infradead.org>,
	"kvmarm@lists.linux.dev" <kvmarm@lists.linux.dev>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	"kvm@vger.kernel.org" <kvm@vger.kernel.org>,
	"linux-kselftest@vger.kernel.org"
	<linux-kselftest@vger.kernel.org>
Subject: RE: [PATCH 3/3] KVM: selftests: arm64: Test KVM_PRE_FAULT_MEMORY before first KVM_RUN
Date: Tue, 24 Nov 2026 08:12:59 +0000	[thread overview]
Message-ID: <AS2PR08MB97864B308EA8B8528C915E71F7442@AS2PR08MB9786.eurprd08.prod.outlook.com> (raw)
In-Reply-To: <6gdla4edbwa6e2s36libskrg5h3glxq3tdl7blthke42sbuqb7@tpczgelv4vud>

Hi Yao,

> -----Original Message-----
[...]
> 
> > Without EL2, cover immediate_exit on the first KVM_RUN, retrying after a
> > failed first KVM_RUN, and pre-faulting while the vCPU moves from the
> > main thread to a worker thread for its first run and back to the main
> > thread for its second run. The two threads are pinned to different CPUs
> > when at least two are available.
> 
> I suggest to list them for better readability e.g:
> 
> Test cases for w/o EL2:
> 1. Immediate_exit on the first KVM_RUN
> 2. Retrying pre-fault after a failed first KVM_RUN.
> 3. Pre-faulting while moving the vCPU between 2 threads.
> (more details of 3 can be given in code implementation there)
> 
Will do in v2.

> > With EL2, additionally cover an EL2 vCPU with ID_AA64MMFR4_EL1.NV_frac
> > cleared to 0 and HCR_EL2.NV=1 at EL1h (the NULL vncr_tlb reproducer), an
> > EL2 vCPU with a vEL1 context and asymmetric HCR_EL2.IMO/FMO staged
> before
> > the first run, and the same thread handoff. An unfixed kernel reports
> > the staged context case only as a WARN in the kernel log; the test
> > itself still passes.
> >
> > Signed-off-by: Jia He <justin.he@arm.com>
> > ---
> >  .../kvm/arm64/nv_pre_fault_memory_test.c      | 335 ++++++++++++++++--
> >  1 file changed, 298 insertions(+), 37 deletions(-)
> >
> > diff --git a/tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c
> b/tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c
> > index 09c1db303856..6dbcf3b0ef13 100644
> > --- a/tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c
> > +++ b/tools/testing/selftests/kvm/arm64/nv_pre_fault_memory_test.c
> 
> ...
> 
> > +
> > +/*
> > + * Pre-fault before the first KVM_RUN with ID_AA64MMFR4_EL1.NV_frac ==
> 0
> > + * (no vncr_tlb) and HCR_EL2.NV=1 at EL1h.
> > + */
> > +static void test_pre_first_run_no_nv2(void)
> 
> This may not necessary per Marc's comment on patch 2 ?
> 
I'd like to keep it. It is the reproducer for the NULL vncr_tlb
dereference that patch 1 fixes, not a test for patch 2. With patch 2
dropped, it becomes the regression test for patch 1. I'll add that to
the comment.

> > +{
> > +	struct kvm_vcpu *vcpu;
> > +	struct kvm_vm *vm;
> > +	u64 val;
> > +
> > +	pr_info("Testing pre-first-run pre-fault with NV_frac=0 and
> HCR_EL2.NV=1\n");
> > +	vm = setup_test_vm(true, false, guest_el1_touch_and_done, &vcpu);
> > +
> > +	/* Copy the EL2 S1 configuration into the EL1 sysregs for vEL1. */
> > +	vcpu_set_reg(vcpu, KVM_ARM64_SYS_REG(SYS_SCTLR_EL1),
> > +		     vcpu_get_reg(vcpu,
> KVM_ARM64_SYS_REG(SYS_SCTLR_EL2)));
> > +	vcpu_set_reg(vcpu, KVM_ARM64_SYS_REG(SYS_TCR_EL1),
> > +		     vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(SYS_TCR_EL2)));
> 
> ...
> 
> > +
> > +static void *first_run_worker(void *arg)
> > +{
> > +	struct handoff_args *h = arg;
> > +
> > +	maybe_pin_self_to_cpu(h->cpu);
> > +	pre_fault(h->vcpu, TEST_MEM_GPA + SZ_1M, TEST_MEM_SIZE -
> SZ_1M);
> > +
> > +	vcpu_run(h->vcpu);
> > +	expect_ucall_sync(h->vcpu, 1);
> > +	return NULL;
> > +}
> > +
> > +/*
> > + * Pre-fault in the main thread, run the vCPU for the first time in a worker
> 
> Minor: the pre-fault happens in worker thead yet, see first_run_worker().
> 
Right, both threads pre-fault. I'll fix the comment.

> > + * thread, then pre-fault and run it again in the main thread. This changes
> > + * vcpu->pid both before and after the first run. If at least two CPUs are
> > + * available, the two threads are pinned to different CPUs.
> > + */
> > +static void test_thread_handoff(bool has_el2)
> > +{
> > +	struct handoff_args h = { .cpu = -1 };
> > +	struct kvm_vcpu *vcpu;
> > +	struct kvm_vm *vm;
> > +	cpu_set_t allowed;
> > +	int cpu, cpu0 = -1;
> > +	pthread_t thr;
> > +
> > +	pr_info("Testing pre-fault across a thread handoff (%s vCPU)\n",
> > +		has_el2 ? "EL2" : "EL1");
> > +
> > +	CPU_ZERO(&allowed);
> > +	kvm_pthread_getaffinity(pthread_self(), &allowed);
> > +	for (cpu = 0; cpu < CPU_SETSIZE; cpu++) {
> > +		if (!CPU_ISSET(cpu, &allowed))
> > +			continue;
> > +		if (cpu0 < 0) {
> > +			cpu0 = cpu;
> > +		} else {
> > +			h.cpu = cpu;
> > +			break;
> > +		}
> > +	}
> > +	if (h.cpu < 0)
> > +		pr_info("Only one CPU available, not testing CPU
> migration\n");
> > +
> > +	maybe_pin_self_to_cpu(cpu0);
> > +	vm = setup_test_vm(has_el2, false,
> > +			   has_el2 ? guest_code : guest_el1_code, &vcpu);
> > +	pre_fault(vcpu, TEST_MEM_GPA, SZ_1M);
> > +
> > +	h.vcpu = vcpu;
> > +	kvm_pthread_create(&thr, NULL, first_run_worker, &h);
> > +	kvm_pthread_join(thr, NULL);
> > +
> > +	pre_fault(vcpu, TEST_MEM_GPA, TEST_MEM_SIZE);
> > +	vcpu_run(vcpu);
> > +	expect_ucall_done(vcpu);
> > +
> > +	kvm_pthread_setaffinity(pthread_self(), &allowed);
> > +
> > +	kvm_vm_free(vm);
> > +}
> > +
> > +static void test_pre_first_run_el1(void)
> > +{
> > +	int feature = KVM_ARM_VCPU_SVE;
> > +	struct kvm_vcpu *vcpu;
> > +	struct kvm_vm *vm;
> > +	int ret;
> > +
> > +	pr_info("Testing pre-first-run pre-fault with immediate_exit\n");
>                                              ^^^^
> The vcpu->run->immediate_exit = 1 happens afte the pre_fault, thus
> s/with/then/ looks more accurate here if this is the desired behavior.
> 
Yes, will fix.

Thanks,
Jia He (Justin)


      reply	other threads:[~2026-11-24  8:13 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23 15:09 [PATCH 0/3] KVM: arm64: Fix pre-first-run KVM_PRE_FAULT_MEMORY vcpu_load() issues Jia He
2026-11-23 15:09 ` [PATCH 1/3] KVM: arm64: Don't load hardware vCPU state before first run Jia He
2026-11-23 17:20   ` Marc Zyngier
2026-11-23 17:50     ` Lorenzo Stoakes (ARM)
2026-11-24  1:39     ` Justin He
2026-11-24 12:50       ` Marc Zyngier
2026-11-24 13:18         ` Justin He
2026-11-24  9:06     ` Justin He
2026-11-24 13:28       ` Marc Zyngier
2026-11-23 15:09 ` [PATCH 2/3] KVM: arm64: nv: Don't request or attempt L1 VNCR mapping without vncr_tlb Jia He
2026-11-23 17:36   ` Marc Zyngier
2026-11-24  1:42     ` Justin He
2026-11-23 15:09 ` [PATCH 3/3] KVM: selftests: arm64: Test KVM_PRE_FAULT_MEMORY before first KVM_RUN Jia He
2026-11-24  7:11   ` Yao Yuan
2026-11-24  8:12     ` Justin He [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=AS2PR08MB97864B308EA8B8528C915E71F7442@AS2PR08MB9786.eurprd08.prod.outlook.com \
    --to=justin.he@arm.com \
    --cc=Catalin.Marinas@arm.com \
    --cc=Joey.Gouly@arm.com \
    --cc=Mark.Rutland@arm.com \
    --cc=Sascha.Bischoff@arm.com \
    --cc=Suzuki.Poulose@arm.com \
    --cc=fuad.tabba@linux.dev \
    --cc=kvm@vger.kernel.org \
    --cc=kvmarm@lists.linux.dev \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=maz@kernel.org \
    --cc=oupton@kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=seanjc@google.com \
    --cc=seiden@linux.ibm.com \
    --cc=shuah@kernel.org \
    --cc=will@kernel.org \
    --cc=yaoyuan@linux.alibaba.com \
    --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®