Re: [PATCH v8 16/43] arm64: RME: Handle realm enter/exit

From: Suzuki K Poulose
Date: Mon Jun 02 2025 - 11:21:11 EST


On 02/06/2025 16:14, Steven Price wrote:
On 29/05/2025 05:52, Emi Kisanuki (Fujitsu) wrote:
Hello, I comment below.

Hi Emi,

Subject: [PATCH v8 16/43] arm64: RME: Handle realm enter/exit

Entering a realm is done using a SMC call to the RMM. On exit the exit-codes
need to be handled slightly differently to the normal KVM path so define our own
functions for realm enter/exit and hook them in if the guest is a realm guest.

Signed-off-by: Steven Price <steven.price@xxxxxxx>
---

[..]

+/*
+ * Return > 0 to return to guest, < 0 on error, 0 (and set exit_reason)
+on
+ * proper exit to userspace.
+ */
+int handle_rec_exit(struct kvm_vcpu *vcpu, int rec_run_ret) {
+ struct realm_rec *rec = &vcpu->arch.rec;
+ u8 esr_ec = ESR_ELx_EC(rec->run->exit.esr);
+ unsigned long status, index;
+
+ status = RMI_RETURN_STATUS(rec_run_ret);
+ index = RMI_RETURN_INDEX(rec_run_ret);
+
+ /*
+ * If a PSCI_SYSTEM_OFF request raced with a vcpu executing, we
might
+ * see the following status code and index indicating an attempt to run
+ * a REC when the RD state is SYSTEM_OFF. In this case, we just need
to
+ * return to user space which can deal with the system event or will try
+ * to run the KVM VCPU again, at which point we will no longer attempt
+ * to enter the Realm because we will have a sleep request pending on
+ * the VCPU as a result of KVM's PSCI handling.
+ */
+ if (status == RMI_ERROR_REALM && index == 1) {
+ vcpu->run->exit_reason = KVM_EXIT_UNKNOWN;
+ return 0;
+ }
Running kvm-unit-tests-cca selftest(smp) test in quick succession may trigger these conditions, resulting in the following error logs.
Error: KVM exit reason: 0 ("KVM_EXIT_UNKNOWN")

Since KVM_EXIT_UNKNOWN is used when no specific exit reason applies, I think it would be better to make it identifiable as an error.
How about adding and setting a new ARM64 exit_reason value to indicate that the PSCI_SYSTEM_OFF request is conflicting with a running vcpu?

Aneesh pointed this out to me off-list. We agreed that KVM_EXIT_SHUTDOWN
was more appropriate here. I'll make the change for v9.

Sounds good to me

Cheers
Suzuki