2021-06-01 11:14:25

by Zenghui Yu

[permalink] [raw]
Subject: [PATCH stable-5.12.y backport 1/2] KVM: arm64: Commit pending PC adjustemnts before returning to userspace

From: Marc Zyngier <[email protected]>

commit 26778aaa134a9aefdf5dbaad904054d7be9d656d upstream.

KVM currently updates PC (and the corresponding exception state)
using a two phase approach: first by setting a set of flags,
then by converting these flags into a state update when the vcpu
is about to enter the guest.

However, this creates a disconnect with userspace if the vcpu thread
returns there with any exception/PC flag set. In this case, the exposed
context is wrong, as userspace doesn't have access to these flags
(they aren't architectural). It also means that these flags are
preserved across a reset, which isn't expected.

To solve this problem, force an explicit synchronisation of the
exception state on vcpu exit to userspace. As an optimisation
for nVHE systems, only perform this when there is something pending.

Reported-by: Zenghui Yu <[email protected]>
Reviewed-by: Alexandru Elisei <[email protected]>
Reviewed-by: Zenghui Yu <[email protected]>
Tested-by: Zenghui Yu <[email protected]>
Signed-off-by: Marc Zyngier <[email protected]>
Cc: [email protected] # 5.11
[yuz: stable-5.12.y backport: add __KVM_HOST_SMCCC_FUNC___kvm_adjust_pc
macro manually and keep it consistent with mainline]
Signed-off-by: Zenghui Yu <[email protected]>
---
arch/arm64/include/asm/kvm_asm.h | 1 +
arch/arm64/kvm/arm.c | 11 +++++++++++
arch/arm64/kvm/hyp/exception.c | 4 ++--
arch/arm64/kvm/hyp/nvhe/hyp-main.c | 8 ++++++++
4 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/arch/arm64/include/asm/kvm_asm.h b/arch/arm64/include/asm/kvm_asm.h
index a8578d650bb6..d7f769bb6c9c 100644
--- a/arch/arm64/include/asm/kvm_asm.h
+++ b/arch/arm64/include/asm/kvm_asm.h
@@ -57,6 +57,7 @@
#define __KVM_HOST_SMCCC_FUNC___kvm_get_mdcr_el2 12
#define __KVM_HOST_SMCCC_FUNC___vgic_v3_save_aprs 13
#define __KVM_HOST_SMCCC_FUNC___vgic_v3_restore_aprs 14
+#define __KVM_HOST_SMCCC_FUNC___kvm_adjust_pc 21

#ifndef __ASSEMBLY__

diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c
index 84b5f79c9eab..c18740a1e541 100644
--- a/arch/arm64/kvm/arm.c
+++ b/arch/arm64/kvm/arm.c
@@ -892,6 +892,17 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)

kvm_sigset_deactivate(vcpu);

+ /*
+ * In the unlikely event that we are returning to userspace
+ * with pending exceptions or PC adjustment, commit these
+ * adjustments in order to give userspace a consistent view of
+ * the vcpu state. Note that this relies on __kvm_adjust_pc()
+ * being preempt-safe on VHE.
+ */
+ if (unlikely(vcpu->arch.flags & (KVM_ARM64_PENDING_EXCEPTION |
+ KVM_ARM64_INCREMENT_PC)))
+ kvm_call_hyp(__kvm_adjust_pc, vcpu);
+
vcpu_put(vcpu);
return ret;
}
diff --git a/arch/arm64/kvm/hyp/exception.c b/arch/arm64/kvm/hyp/exception.c
index 0812a496725f..11541b94b328 100644
--- a/arch/arm64/kvm/hyp/exception.c
+++ b/arch/arm64/kvm/hyp/exception.c
@@ -331,8 +331,8 @@ static void kvm_inject_exception(struct kvm_vcpu *vcpu)
}

/*
- * Adjust the guest PC on entry, depending on flags provided by EL1
- * for the purpose of emulation (MMIO, sysreg) or exception injection.
+ * Adjust the guest PC (and potentially exception state) depending on
+ * flags provided by the emulation code.
*/
void __kvm_adjust_pc(struct kvm_vcpu *vcpu)
{
diff --git a/arch/arm64/kvm/hyp/nvhe/hyp-main.c b/arch/arm64/kvm/hyp/nvhe/hyp-main.c
index 936328207bde..e52582e14087 100644
--- a/arch/arm64/kvm/hyp/nvhe/hyp-main.c
+++ b/arch/arm64/kvm/hyp/nvhe/hyp-main.c
@@ -25,6 +25,13 @@ static void handle___kvm_vcpu_run(struct kvm_cpu_context *host_ctxt)
cpu_reg(host_ctxt, 1) = __kvm_vcpu_run(kern_hyp_va(vcpu));
}

+static void handle___kvm_adjust_pc(struct kvm_cpu_context *host_ctxt)
+{
+ DECLARE_REG(struct kvm_vcpu *, vcpu, host_ctxt, 1);
+
+ __kvm_adjust_pc(kern_hyp_va(vcpu));
+}
+
static void handle___kvm_flush_vm_context(struct kvm_cpu_context *host_ctxt)
{
__kvm_flush_vm_context();
@@ -112,6 +119,7 @@ typedef void (*hcall_t)(struct kvm_cpu_context *);

static const hcall_t host_hcall[] = {
HANDLE_FUNC(__kvm_vcpu_run),
+ HANDLE_FUNC(__kvm_adjust_pc),
HANDLE_FUNC(__kvm_flush_vm_context),
HANDLE_FUNC(__kvm_tlb_flush_vmid_ipa),
HANDLE_FUNC(__kvm_tlb_flush_vmid),
--
2.19.1


2021-06-01 11:46:19

by Marc Zyngier

[permalink] [raw]
Subject: Re: [PATCH stable-5.12.y backport 1/2] KVM: arm64: Commit pending PC adjustemnts before returning to userspace

Hi Zenghui,

Thanks for having a go at the backport.

On Tue, 01 Jun 2021 12:12:37 +0100,
Zenghui Yu <[email protected]> wrote:
>
> From: Marc Zyngier <[email protected]>
>
> commit 26778aaa134a9aefdf5dbaad904054d7be9d656d upstream.
>
> KVM currently updates PC (and the corresponding exception state)
> using a two phase approach: first by setting a set of flags,
> then by converting these flags into a state update when the vcpu
> is about to enter the guest.
>
> However, this creates a disconnect with userspace if the vcpu thread
> returns there with any exception/PC flag set. In this case, the exposed
> context is wrong, as userspace doesn't have access to these flags
> (they aren't architectural). It also means that these flags are
> preserved across a reset, which isn't expected.
>
> To solve this problem, force an explicit synchronisation of the
> exception state on vcpu exit to userspace. As an optimisation
> for nVHE systems, only perform this when there is something pending.
>
> Reported-by: Zenghui Yu <[email protected]>
> Reviewed-by: Alexandru Elisei <[email protected]>
> Reviewed-by: Zenghui Yu <[email protected]>
> Tested-by: Zenghui Yu <[email protected]>
> Signed-off-by: Marc Zyngier <[email protected]>
> Cc: [email protected] # 5.11
> [yuz: stable-5.12.y backport: add __KVM_HOST_SMCCC_FUNC___kvm_adjust_pc
> macro manually and keep it consistent with mainline]

I'd rather you allocated a new number here, irrespective of what
mainline has (rational below).

> Signed-off-by: Zenghui Yu <[email protected]>
> ---
> arch/arm64/include/asm/kvm_asm.h | 1 +
> arch/arm64/kvm/arm.c | 11 +++++++++++
> arch/arm64/kvm/hyp/exception.c | 4 ++--
> arch/arm64/kvm/hyp/nvhe/hyp-main.c | 8 ++++++++
> 4 files changed, 22 insertions(+), 2 deletions(-)
>
> diff --git a/arch/arm64/include/asm/kvm_asm.h b/arch/arm64/include/asm/kvm_asm.h
> index a8578d650bb6..d7f769bb6c9c 100644
> --- a/arch/arm64/include/asm/kvm_asm.h
> +++ b/arch/arm64/include/asm/kvm_asm.h
> @@ -57,6 +57,7 @@
> #define __KVM_HOST_SMCCC_FUNC___kvm_get_mdcr_el2 12
> #define __KVM_HOST_SMCCC_FUNC___vgic_v3_save_aprs 13
> #define __KVM_HOST_SMCCC_FUNC___vgic_v3_restore_aprs 14
> +#define __KVM_HOST_SMCCC_FUNC___kvm_adjust_pc 21

This is going to generate a larger than necessary host_hcall array in
hyp/nvhe/hyp-main.c, which we're trying to keep tightly packed for
obvious reasons.

With this nit fixed:

Reviewed-by: Marc Zyngier <[email protected]>

Thanks,

M.

--
Without deviation from the norm, progress is not possible.

2021-06-01 13:49:14

by Zenghui Yu

[permalink] [raw]
Subject: Re: [PATCH stable-5.12.y backport 1/2] KVM: arm64: Commit pending PC adjustemnts before returning to userspace

Hi Marc,

On 2021/6/1 19:44, Marc Zyngier wrote:
> Hi Zenghui,
>
> Thanks for having a go at the backport.
>
> On Tue, 01 Jun 2021 12:12:37 +0100,
> Zenghui Yu <[email protected]> wrote:
>>
>> From: Marc Zyngier <[email protected]>
>>
>> commit 26778aaa134a9aefdf5dbaad904054d7be9d656d upstream.
>>
>> KVM currently updates PC (and the corresponding exception state)
>> using a two phase approach: first by setting a set of flags,
>> then by converting these flags into a state update when the vcpu
>> is about to enter the guest.
>>
>> However, this creates a disconnect with userspace if the vcpu thread
>> returns there with any exception/PC flag set. In this case, the exposed
>> context is wrong, as userspace doesn't have access to these flags
>> (they aren't architectural). It also means that these flags are
>> preserved across a reset, which isn't expected.
>>
>> To solve this problem, force an explicit synchronisation of the
>> exception state on vcpu exit to userspace. As an optimisation
>> for nVHE systems, only perform this when there is something pending.
>>
>> Reported-by: Zenghui Yu <[email protected]>
>> Reviewed-by: Alexandru Elisei <[email protected]>
>> Reviewed-by: Zenghui Yu <[email protected]>
>> Tested-by: Zenghui Yu <[email protected]>
>> Signed-off-by: Marc Zyngier <[email protected]>
>> Cc: [email protected] # 5.11
>> [yuz: stable-5.12.y backport: add __KVM_HOST_SMCCC_FUNC___kvm_adjust_pc
>> macro manually and keep it consistent with mainline]
>
> I'd rather you allocated a new number here, irrespective of what
> mainline has (rational below).
>
>> Signed-off-by: Zenghui Yu <[email protected]>
>> ---
>> arch/arm64/include/asm/kvm_asm.h | 1 +
>> arch/arm64/kvm/arm.c | 11 +++++++++++
>> arch/arm64/kvm/hyp/exception.c | 4 ++--
>> arch/arm64/kvm/hyp/nvhe/hyp-main.c | 8 ++++++++
>> 4 files changed, 22 insertions(+), 2 deletions(-)
>>
>> diff --git a/arch/arm64/include/asm/kvm_asm.h b/arch/arm64/include/asm/kvm_asm.h
>> index a8578d650bb6..d7f769bb6c9c 100644
>> --- a/arch/arm64/include/asm/kvm_asm.h
>> +++ b/arch/arm64/include/asm/kvm_asm.h
>> @@ -57,6 +57,7 @@
>> #define __KVM_HOST_SMCCC_FUNC___kvm_get_mdcr_el2 12
>> #define __KVM_HOST_SMCCC_FUNC___vgic_v3_save_aprs 13
>> #define __KVM_HOST_SMCCC_FUNC___vgic_v3_restore_aprs 14
>> +#define __KVM_HOST_SMCCC_FUNC___kvm_adjust_pc 21
>
> This is going to generate a larger than necessary host_hcall array in
> hyp/nvhe/hyp-main.c, which we're trying to keep tightly packed for
> obvious reasons.

It isn't obvious to me ;-). But this creates some invalid entries
(HVC handlers) in the host_hcall array, which is not good. I'll change
__KVM_HOST_SMCCC_FUNC___kvm_adjust_pc to 15. Thanks for your reminder.

> With this nit fixed:
>
> Reviewed-by: Marc Zyngier <[email protected]>

Thanks!

Zenghui