[PATCH v2] KVM: x86: Check irqchip mode before create PIT

Tengfei Yu posted 1 patch 1 year, 11 months ago
arch/x86/kvm/x86.c | 3 +++
1 file changed, 3 insertions(+)
[PATCH v2] KVM: x86: Check irqchip mode before create PIT
Posted by Tengfei Yu 1 year, 11 months ago
As the kvm api(https://docs.kernel.org/virt/kvm/api.html) reads,
KVM_CREATE_PIT2 call is only valid after enabling in-kernel irqchip
support via KVM_CREATE_IRQCHIP.

Without this check, I can create PIT first and enable irqchip-split
then, which may cause the PIT invalid because of lacking of in-kernel
PIC to inject the interrupt.

Signed-off-by: Tengfei Yu <moehanabichan@gmail.com>
---
v1 -> v2: Change errno from -EEXIST to -ENOENT.
v1 link: https://lore.kernel.org/lkml/ZbGkZlFmi1war6vq@google.com/

 arch/x86/kvm/x86.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 27e23714e960..c1e3aecd627f 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -7016,6 +7016,9 @@ int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
 		r = -EEXIST;
 		if (kvm->arch.vpit)
 			goto create_pit_unlock;
+		r = -ENOENT;
+		if (!pic_in_kernel(kvm))
+			goto create_pit_unlock;
 		r = -ENOMEM;
 		kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags);
 		if (kvm->arch.vpit)
-- 
2.39.3
Re: [PATCH v2] KVM: x86: Check irqchip mode before create PIT
Posted by Paolo Bonzini 1 year, 11 months ago
On Thu, Jan 25, 2024 at 6:09 AM Tengfei Yu <moehanabichan@gmail.com> wrote:
>
> As the kvm api(https://docs.kernel.org/virt/kvm/api.html) reads,
> KVM_CREATE_PIT2 call is only valid after enabling in-kernel irqchip
> support via KVM_CREATE_IRQCHIP.
>
> Without this check, I can create PIT first and enable irqchip-split
> then, which may cause the PIT invalid because of lacking of in-kernel
> PIC to inject the interrupt.
>
> Signed-off-by: Tengfei Yu <moehanabichan@gmail.com>

Queued, thanks.

Paolo

> ---
> v1 -> v2: Change errno from -EEXIST to -ENOENT.
> v1 link: https://lore.kernel.org/lkml/ZbGkZlFmi1war6vq@google.com/
>
>  arch/x86/kvm/x86.c | 3 +++
>  1 file changed, 3 insertions(+)
>
> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> index 27e23714e960..c1e3aecd627f 100644
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c
> @@ -7016,6 +7016,9 @@ int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
>                 r = -EEXIST;
>                 if (kvm->arch.vpit)
>                         goto create_pit_unlock;
> +               r = -ENOENT;
> +               if (!pic_in_kernel(kvm))
> +                       goto create_pit_unlock;
>                 r = -ENOMEM;
>                 kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags);
>                 if (kvm->arch.vpit)
> --
> 2.39.3
>