* [PATCH] KVM: x86: Check irqchip mode before create PIT @ 2024-01-24 16:02 Brilliant Hanabi 2024-01-24 16:32 ` Sean Christopherson 0 siblings, 1 reply; 9+ messages in thread From: Brilliant Hanabi @ 2024-01-24 16:02 UTC (permalink / raw) To: Sean Christopherson, Paolo Bonzini, Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86, H. Peter Anvin Cc: Brilliant Hanabi, kvm, linux-kernel 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: Brilliant Hanabi <moehanabichan@gmail.com> --- arch/x86/kvm/x86.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 27e23714e960..3edc8478310f 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -7016,6 +7016,8 @@ int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) r = -EEXIST; if (kvm->arch.vpit) goto create_pit_unlock; + 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 ^ permalink raw reply related [flat|nested] 9+ messages in thread
* Re: [PATCH] KVM: x86: Check irqchip mode before create PIT 2024-01-24 16:02 [PATCH] KVM: x86: Check irqchip mode before create PIT Brilliant Hanabi @ 2024-01-24 16:32 ` Sean Christopherson 2024-01-24 17:02 ` moehanabi 0 siblings, 1 reply; 9+ messages in thread From: Sean Christopherson @ 2024-01-24 16:32 UTC (permalink / raw) To: Brilliant Hanabi Cc: Paolo Bonzini, Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86, H. Peter Anvin, kvm, linux-kernel On Thu, Jan 25, 2024, Brilliant Hanabi 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. Does this cause actual problems beyond the PIT not working for the guest? E.g. does it put the host kernel at risk? If the only problem is that the PIT doesn't work as expected, I'm tempted to tweak the docs to say that KVM's PIT emulation won't work without an in-kernel I/O APIC. Rejecting the ioctl could theoertically break misconfigured setups that happen to work, e.g. because the guest never uses the PIT. > Signed-off-by: Brilliant Hanabi <moehanabichan@gmail.com> > --- > arch/x86/kvm/x86.c | 2 ++ > 1 file changed, 2 insertions(+) > > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c > index 27e23714e960..3edc8478310f 100644 > --- a/arch/x86/kvm/x86.c > +++ b/arch/x86/kvm/x86.c > @@ -7016,6 +7016,8 @@ int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) > r = -EEXIST; > if (kvm->arch.vpit) > goto create_pit_unlock; > + if (!pic_in_kernel(kvm)) > + goto create_pit_unlock; -EEXIST is not an appropriate errno. > r = -ENOMEM; > kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags); > if (kvm->arch.vpit) > -- > 2.39.3 > ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Re: [PATCH] KVM: x86: Check irqchip mode before create PIT 2024-01-24 16:32 ` Sean Christopherson @ 2024-01-24 17:02 ` moehanabi 2024-01-24 17:43 ` Sean Christopherson 0 siblings, 1 reply; 9+ messages in thread From: moehanabi @ 2024-01-24 17:02 UTC (permalink / raw) To: seanjc Cc: bp, dave.hansen, hpa, kvm, linux-kernel, mingo, moehanabichan, pbonzini, tglx, x86 > On Thu, Jan 25, 2024, Brilliant Hanabi 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. > > Does this cause actual problems beyond the PIT not working for the guest? E.g. > does it put the host kernel at risk? If the only problem is that the PIT doesn't > work as expected, I'm tempted to tweak the docs to say that KVM's PIT emulation > won't work without an in-kernel I/O APIC. Rejecting the ioctl could theoertically > break misconfigured setups that happen to work, e.g. because the guest never uses > the PIT. I don't think it will put the host kernel at risk. But that's exactly what kvmtool does: it creates in-kernel PIT first and set KVM_CREATE_IRQCHIP then. I found this problem because I was working on implementing a userspace PIC and PIT in kvmtool. As I planned, I'm going to commit a related patch to kvmtool if this patch will be applied. > > Signed-off-by: Brilliant Hanabi <moehanabichan@gmail.com> > > --- > > arch/x86/kvm/x86.c | 2 ++ > > 1 file changed, 2 insertions(+) > > > > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c > > index 27e23714e960..3edc8478310f 100644 > > --- a/arch/x86/kvm/x86.c > > +++ b/arch/x86/kvm/x86.c > > @@ -7016,6 +7016,8 @@ int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) > > r = -EEXIST; > > if (kvm->arch.vpit) > > goto create_pit_unlock; > > + if (!pic_in_kernel(kvm)) > > + goto create_pit_unlock; > > -EEXIST is not an appropriate errno. Which errno do you think is better? > > r = -ENOMEM; > > kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags); > > if (kvm->arch.vpit) > > -- > > 2.39.3 > > ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Re: [PATCH] KVM: x86: Check irqchip mode before create PIT 2024-01-24 17:02 ` moehanabi @ 2024-01-24 17:43 ` Sean Christopherson 2024-01-24 19:01 ` Brilliant Hanabi 0 siblings, 1 reply; 9+ messages in thread From: Sean Christopherson @ 2024-01-24 17:43 UTC (permalink / raw) To: moehanabi Cc: bp, dave.hansen, hpa, kvm, linux-kernel, mingo, pbonzini, tglx, x86 On Thu, Jan 25, 2024, moehanabi wrote: > > On Thu, Jan 25, 2024, Brilliant Hanabi 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. > > > > Does this cause actual problems beyond the PIT not working for the guest? E.g. > > does it put the host kernel at risk? If the only problem is that the PIT doesn't > > work as expected, I'm tempted to tweak the docs to say that KVM's PIT emulation > > won't work without an in-kernel I/O APIC. Rejecting the ioctl could theoertically > > break misconfigured setups that happen to work, e.g. because the guest never uses > > the PIT. > > I don't think it will put the host kernel at risk. But that's exactly what > kvmtool does: it creates in-kernel PIT first and set KVM_CREATE_IRQCHIP then. Right. My concern, which could be unfounded paranoia, is that rejecting an ioctl() that used to succeed could break existing setups. E.g. if a userspace VMM creates a PIT and checks the ioctl() result, but its guest(s) never actually use the PIT and so don't care that the PIT is busted. > I found this problem because I was working on implementing a userspace PIC > and PIT in kvmtool. As I planned, I'm going to commit a related patch to > kvmtool if this patch will be applied. > > > > Signed-off-by: Brilliant Hanabi <moehanabichan@gmail.com> > > > --- > > > arch/x86/kvm/x86.c | 2 ++ > > > 1 file changed, 2 insertions(+) > > > > > > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c > > > index 27e23714e960..3edc8478310f 100644 > > > --- a/arch/x86/kvm/x86.c > > > +++ b/arch/x86/kvm/x86.c > > > @@ -7016,6 +7016,8 @@ int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) > > > r = -EEXIST; > > > if (kvm->arch.vpit) > > > goto create_pit_unlock; > > > + if (!pic_in_kernel(kvm)) > > > + goto create_pit_unlock; > > > > -EEXIST is not an appropriate errno. > > Which errno do you think is better? Maybe ENOENT? ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Re: Re: [PATCH] KVM: x86: Check irqchip mode before create PIT 2024-01-24 17:43 ` Sean Christopherson @ 2024-01-24 19:01 ` Brilliant Hanabi 2024-01-24 23:59 ` Sean Christopherson 0 siblings, 1 reply; 9+ messages in thread From: Brilliant Hanabi @ 2024-01-24 19:01 UTC (permalink / raw) To: seanjc Cc: bp, dave.hansen, hpa, kvm, linux-kernel, mingo, moehanabichan, pbonzini, tglx, x86 > On Thu, Jan 25, 2024, moehanabi wrote: > > > On Thu, Jan 25, 2024, Brilliant Hanabi 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. > > > > > > Does this cause actual problems beyond the PIT not working for the guest? E.g. > > > does it put the host kernel at risk? If the only problem is that the PIT doesn't > > > work as expected, I'm tempted to tweak the docs to say that KVM's PIT emulation > > > won't work without an in-kernel I/O APIC. Rejecting the ioctl could theoertically > > > break misconfigured setups that happen to work, e.g. because the guest never uses > > > the PIT. > > > > I don't think it will put the host kernel at risk. But that's exactly what > > kvmtool does: it creates in-kernel PIT first and set KVM_CREATE_IRQCHIP then. > > Right. My concern, which could be unfounded paranoia, is that rejecting an ioctl() > that used to succeed could break existing setups. E.g. if a userspace VMM creates > a PIT and checks the ioctl() result, but its guest(s) never actually use the PIT > and so don't care that the PIT is busted. Thanks for your review. In my opinion, it is better to avoid potential bugs which is difficult to detect, as long as you can return errors to let developers know about them in advance, although the kernel is not to blame for this bug. > > I found this problem because I was working on implementing a userspace PIC > > and PIT in kvmtool. As I planned, I'm going to commit a related patch to > > kvmtool if this patch will be applied. > > > > > > Signed-off-by: Brilliant Hanabi <moehanabichan@gmail.com> > > > > --- > > > > arch/x86/kvm/x86.c | 2 ++ > > > > 1 file changed, 2 insertions(+) > > > > > > > > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c > > > > index 27e23714e960..3edc8478310f 100644 > > > > --- a/arch/x86/kvm/x86.c > > > > +++ b/arch/x86/kvm/x86.c > > > > @@ -7016,6 +7016,8 @@ int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) > > > > r = -EEXIST; > > > > if (kvm->arch.vpit) > > > > goto create_pit_unlock; > > > > + if (!pic_in_kernel(kvm)) > > > > + goto create_pit_unlock; > > > > > > -EEXIST is not an appropriate errno. > > > > Which errno do you think is better? > > Maybe ENOENT? > I'm glad to send a new version patch if you're willing to accept the patch. ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Re: Re: [PATCH] KVM: x86: Check irqchip mode before create PIT 2024-01-24 19:01 ` Brilliant Hanabi @ 2024-01-24 23:59 ` Sean Christopherson 2024-01-25 5:08 ` [PATCH v2] " Tengfei Yu 2024-01-26 18:11 ` Re: Re: [PATCH] " Paolo Bonzini 0 siblings, 2 replies; 9+ messages in thread From: Sean Christopherson @ 2024-01-24 23:59 UTC (permalink / raw) To: Brilliant Hanabi Cc: bp, dave.hansen, hpa, kvm, linux-kernel, mingo, pbonzini, tglx, x86 On Thu, Jan 25, 2024, Brilliant Hanabi wrote: > > On Thu, Jan 25, 2024, moehanabi wrote: > > > > On Thu, Jan 25, 2024, Brilliant Hanabi 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. > > > > > > > > Does this cause actual problems beyond the PIT not working for the guest? E.g. > > > > does it put the host kernel at risk? If the only problem is that the PIT doesn't > > > > work as expected, I'm tempted to tweak the docs to say that KVM's PIT emulation > > > > won't work without an in-kernel I/O APIC. Rejecting the ioctl could theoertically > > > > break misconfigured setups that happen to work, e.g. because the guest never uses > > > > the PIT. > > > > > > I don't think it will put the host kernel at risk. But that's exactly what > > > kvmtool does: it creates in-kernel PIT first and set KVM_CREATE_IRQCHIP then. > > > > Right. My concern, which could be unfounded paranoia, is that rejecting an ioctl() > > that used to succeed could break existing setups. E.g. if a userspace VMM creates > > a PIT and checks the ioctl() result, but its guest(s) never actually use the PIT > > and so don't care that the PIT is busted. > > Thanks for your review. In my opinion, it is better to avoid potential bugs > which is difficult to detect, as long as you can return errors to let > developers know about them in advance, although the kernel is not to blame > for this bug. Oh, I completely agree that explict errors are far better. My only concern is that there's a teeny tiny chance that rejecting an ioctl() that used to work could break userspace. > > > I found this problem because I was working on implementing a userspace PIC > > > and PIT in kvmtool. As I planned, I'm going to commit a related patch to > > > kvmtool if this patch will be applied. > > > > > > > > Signed-off-by: Brilliant Hanabi <moehanabichan@gmail.com> > > > > > --- > > > > > arch/x86/kvm/x86.c | 2 ++ > > > > > 1 file changed, 2 insertions(+) > > > > > > > > > > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c > > > > > index 27e23714e960..3edc8478310f 100644 > > > > > --- a/arch/x86/kvm/x86.c > > > > > +++ b/arch/x86/kvm/x86.c > > > > > @@ -7016,6 +7016,8 @@ int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) > > > > > r = -EEXIST; > > > > > if (kvm->arch.vpit) > > > > > goto create_pit_unlock; > > > > > + if (!pic_in_kernel(kvm)) > > > > > + goto create_pit_unlock; > > > > > > > > -EEXIST is not an appropriate errno. > > > > > > Which errno do you think is better? > > > > Maybe ENOENT? > > > > I'm glad to send a new version patch if you're willing to accept the > patch. Go ahead and send v2. I'll get Paolo's thoughts on whether or not this is likely to break userspace and we can go from there. ^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH v2] KVM: x86: Check irqchip mode before create PIT 2024-01-24 23:59 ` Sean Christopherson @ 2024-01-25 5:08 ` Tengfei Yu 2024-01-26 18:12 ` Paolo Bonzini 2024-01-26 18:11 ` Re: Re: [PATCH] " Paolo Bonzini 1 sibling, 1 reply; 9+ messages in thread From: Tengfei Yu @ 2024-01-25 5:08 UTC (permalink / raw) To: Sean Christopherson, Paolo Bonzini, Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86, H. Peter Anvin Cc: Tengfei Yu, kvm, linux-kernel 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 ^ permalink raw reply related [flat|nested] 9+ messages in thread
* Re: [PATCH v2] KVM: x86: Check irqchip mode before create PIT 2024-01-25 5:08 ` [PATCH v2] " Tengfei Yu @ 2024-01-26 18:12 ` Paolo Bonzini 0 siblings, 0 replies; 9+ messages in thread From: Paolo Bonzini @ 2024-01-26 18:12 UTC (permalink / raw) To: Tengfei Yu Cc: Sean Christopherson, Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86, H. Peter Anvin, kvm, linux-kernel 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 > ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: Re: Re: [PATCH] KVM: x86: Check irqchip mode before create PIT 2024-01-24 23:59 ` Sean Christopherson 2024-01-25 5:08 ` [PATCH v2] " Tengfei Yu @ 2024-01-26 18:11 ` Paolo Bonzini 1 sibling, 0 replies; 9+ messages in thread From: Paolo Bonzini @ 2024-01-26 18:11 UTC (permalink / raw) To: Sean Christopherson Cc: Brilliant Hanabi, bp, dave.hansen, hpa, kvm, linux-kernel, mingo, tglx, x86 On Thu, Jan 25, 2024 at 12:59 AM Sean Christopherson <seanjc@google.com> wrote: > On Thu, Jan 25, 2024, Brilliant Hanabi wrote: > > Thanks for your review. In my opinion, it is better to avoid potential bugs > > which is difficult to detect, as long as you can return errors to let > > developers know about them in advance, although the kernel is not to blame > > for this bug. > > Oh, I completely agree that explict errors are far better. My only concern is > that there's a teeny tiny chance that rejecting an ioctl() that used to work > could break userspace. > > Go ahead and send v2. I'll get Paolo's thoughts on whether or not this is likely > to break userspace and we can go from there. I share the same worry but I agree it's quite unlikely. Let's just do it, and if someone complains we'll revert it. Paolo ^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2024-01-26 18:12 UTC | newest] Thread overview: 9+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2024-01-24 16:02 [PATCH] KVM: x86: Check irqchip mode before create PIT Brilliant Hanabi 2024-01-24 16:32 ` Sean Christopherson 2024-01-24 17:02 ` moehanabi 2024-01-24 17:43 ` Sean Christopherson 2024-01-24 19:01 ` Brilliant Hanabi 2024-01-24 23:59 ` Sean Christopherson 2024-01-25 5:08 ` [PATCH v2] " Tengfei Yu 2024-01-26 18:12 ` Paolo Bonzini 2024-01-26 18:11 ` Re: Re: [PATCH] " Paolo Bonzini
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).