kvm: don't take vcpu mutex for obviously invalid vcpu ioctls
PATCH 1/1 kvm: don't take vcpu mutex for obviously invalid vcpu ioctls
vcpu ioctls can hang the calling thread if issued while a vcpu is running. If we know ioctl is going to be rejected as invalid anyway, we can fail before trying to take the vcpu mutex.
This patch does not change functionality, it just makes invalid ioctls fail faster.
--- virt/kvm/kvm_main.c | 4 ++++ 1 file changed, 4 insertions(+)
virt/kvm/kvm_main.c +4 -0
@@ -52,6 +52,7 @@#include <asm/processor.h> #include <asm/io.h> +#include <asm/ioctl.h> #include <asm/uaccess.h> #include <asm/pgtable.h>
@@ -1975,6 +1976,9 @@ static long kvm_vcpu_ioctl(struct file *filp,if (vcpu->kvm->mm != current->mm)return -EIO ; +if (unlikely (_IOC_TYPE (ioctl) !=KVMIO )) +return -EINVAL ; +#if defined(CONFIG_S390 ) || defined(CONFIG_PPC ) || defined(CONFIG_MIPS )/* * Special cases: vcpu ioctls that are asynchronous to vcpu execution,
Signature · 1 line
2.1.0.rc2.206.gedb03e5
Paolo Bonzini
maintainerIl 20/09/2014 01:03, David Matlack ha scritto:
Quoted 26 lines
vcpu ioctls can hang the calling thread if issued while a vcpu is running. If we know ioctl is going to be rejected as invalid anyway, we can fail before trying to take the vcpu mutex.
This patch does not change functionality, it just makes invalid ioctls fail faster.
Signed-off-by David Matlack <[email protected]>--- virt/kvm/kvm_main.c | 4 ++++ 1 file changed, 4 insertions(+)virt/kvm/kvm_main.c +1
-0@@ -52,6 +52,7 @@#include <asm/processor.h> #include <asm/io.h> +#include <asm/ioctl.h> #include <asm/uaccess.h> #include <asm/pgtable.h>
virt/kvm/kvm_main.c static long kvm_vcpu_ioctl(struct file *filp, · line 1984
Quoted 9 lines
virt/kvm/kvm_main.c +3
-0@@ -1975,6 +1976,9 @@ static long kvm_vcpu_ioctl(struct file *filp,if (vcpu->kvm->mm != current->mm)return -EIO ; +if (unlikely (_IOC_TYPE (ioctl) !=KVMIO )) +return -EINVAL ; +#if defined(CONFIG_S390 ) || defined(CONFIG_PPC ) || defined(CONFIG_MIPS )/* * Special cases: vcpu ioctls that are asynchronous to vcpu execution,
Thanks, applying this patch.
Paolo
Quoted 29 lines · Paolo Bonzini
Il 20/09/2014 01:03, David Matlack ha scritto:
Quoted 28 lines
vcpu ioctls can hang the calling thread if issued while a vcpu is running. If we know ioctl is going to be rejected as invalid anyway, we can fail before trying to take the vcpu mutex.
This patch does not change functionality, it just makes invalid ioctls fail faster.
Signed-off-by David Matlack <[email protected]>--- virt/kvm/kvm_main.c | 4 ++++ 1 file changed, 4 insertions(+)virt/kvm/kvm_main.c +1
-0@@ -52,6 +52,7 @@#include <asm/processor.h> #include <asm/io.h> +#include <asm/ioctl.h> #include <asm/uaccess.h> #include <asm/pgtable.h> @@ -1975,6 +1976,9 @@ static long kvm_vcpu_ioctl(struct file *filp,if (vcpu->kvm->mm != current->mm)return -EIO ;
Quoted 9 lines
virt/kvm/kvm_main.c static long kvm_vcpu_ioctl(struct file *filp, · line 1984
Quoted 7 lines
virt/kvm/kvm_main.c +3
-0+if (unlikely (_IOC_TYPE (ioctl) !=KVMIO )) +return -EINVAL ; +#if defined(CONFIG_S390 ) || defined(CONFIG_PPC ) || defined(CONFIG_MIPS )/* * Special cases: vcpu ioctls that are asynchronous to vcpu execution, Thanks, applying this patch.
Isnt that the wrong trade off?
We now have an extra condition check for every valid ioctl, to make an error case go faster. I know, the extra check is just a 1 or 2 cycles if branch prediction is right, but still.
Christian
Paolo Bonzini
maintainerIl 22/09/2014 15:45, Christian Borntraeger ha scritto:
Quoted 2 lines
We now have an extra condition check for every valid ioctl, to make an error case go faster. I know, the extra check is just a 1 or 2 cycles if branch prediction is right, but still.
I applied the patch because the delay could be substantial, depending on what the other VCPU is doing. Perhaps something like this would be better?
(Untested, but Tested-by/Reviewed-bys are welcome).
Paolo
virt/kvm/kvm_main.c +23 -7
@@ -117,12 +117,10 @@ bool kvm_is_mmio_pfn(pfn_t pfn)/* * Switches to specified vcpu, until a matching vcpu_put() */ -int vcpu_load (struct kvm_vcpu *vcpu) +static void __vcpu_load (struct kvm_vcpu *vcpu) {int cpu; -if (mutex_lock_killable (&vcpu->mutex)) -return -EINTR ;if (unlikely (vcpu->pid != current->pids[PIDTYPE_PID ].pid)) {/* The thread running this VCPU changed. */ struct pid *oldpid = vcpu->pid;
@@ -136,6 +134,14 @@ int vcpu_load(struct kvm_vcpu *vcpu)preempt_notifier_register (&vcpu->preempt_notifier);kvm_arch_vcpu_load (vcpu, cpu);put_cpu (); +} + +int vcpu_load (struct kvm_vcpu *vcpu) +{ +if (mutex_lock_killable (&vcpu->mutex)) +return -EINTR ; + +__vcpu_load (vcpu);return 0 ; }
@@ -1989,9 +1995,6 @@ static long kvm_vcpu_ioctl(struct file *filp,if (vcpu->kvm->mm != current->mm)return -EIO ; -if (unlikely (_IOC_TYPE (ioctl) !=KVMIO )) -return -EINVAL ; -#if defined(CONFIG_S390 ) || defined(CONFIG_PPC ) || defined(CONFIG_MIPS )/* * Special cases: vcpu ioctls that are asynchronous to vcpu execution,
@@ -2001,8 +2004,21 @@ static long kvm_vcpu_ioctl(struct file *filp,return kvm_arch_vcpu_ioctl (filp, ioctl, arg);#endif + if (!mutex_trylock(&vcpu->mutex))) { +/* +* Before a potentially long sleep, check if we'd exit anyway. +* The common case is for the mutex not to be contended, when +* this does not add overhead. +*/ +if (unlikely (_IOC_TYPE (ioctl) !=KVMIO )) +return -EINVAL ; + +if (mutex_lock_killable (&vcpu->mutex)) +return -EINTR ; + } + - r =vcpu_load (vcpu); + r =__vcpu_load (vcpu);if (r)return r;switch (ioctl) {
Quoted 7 lines · Paolo Bonzini
Il 22/09/2014 15:45, Christian Borntraeger ha scritto:
Quoted 2 lines
We now have an extra condition check for every valid ioctl, to make an error case go faster. I know, the extra check is just a 1 or 2 cycles if branch prediction is right, but still.
I applied the patch because the delay could be substantial, depending on what the other VCPU is doing. Perhaps something like this would be better?
I'm happy with either approach.
Quoted 2 lines
(Untested, but Tested-by/Reviewed-bys are welcome).
There were a few build bugs in your diff. Here's a working version that I tested. Feel free to add my Tested-by and Reviewed-by if you go with this.
virt/kvm/kvm_main.c +23 -6
@@ -133,12 +133,10 @@ bool kvm_is_mmio_pfn(pfn_t pfn)/* * Switches to specified vcpu, until a matching vcpu_put() */ -int vcpu_load (struct kvm_vcpu *vcpu) +static void __vcpu_load (struct kvm_vcpu *vcpu) {int cpu; -if (mutex_lock_killable (&vcpu->mutex)) -return -EINTR ;if (unlikely (vcpu->pid != current->pids[PIDTYPE_PID ].pid)) {/* The thread running this VCPU changed. */ struct pid *oldpid = vcpu->pid;
@@ -151,6 +149,14 @@ int vcpu_load(struct kvm_vcpu *vcpu)preempt_notifier_register (&vcpu->preempt_notifier);kvm_arch_vcpu_load (vcpu, cpu);put_cpu (); +} + +int vcpu_load (struct kvm_vcpu *vcpu) +{ +if (mutex_lock_killable (&vcpu->mutex)) +return -EINTR ; + +__vcpu_load (vcpu);return 0 ; }
@@ -2197,10 +2203,21 @@ static long kvm_vcpu_ioctl(struct file *filp,return kvm_arch_vcpu_ioctl (filp, ioctl, arg);#endif +if (!mutex_trylock (&vcpu->mutex)) { +/* +* Before a potentially long sleep, check if we'd exit anyway. +* The common case is for the mutex not to be contended, when +* this does not add overhead. +*/ +if (unlikely (_IOC_TYPE (ioctl) !=KVMIO )) +return -EINVAL ; + +if (mutex_lock_killable (&vcpu->mutex)) +return -EINTR ; + } + +__vcpu_load (vcpu); - r =vcpu_load (vcpu); -if (r) -return r;switch (ioctl) {case KVM_RUN : r = -EINVAL ;
Quoted 5 lines · Paolo Bonzini
Il 22/09/2014 15:45, Christian Borntraeger ha scritto:
Quoted 2 lines
We now have an extra condition check for every valid ioctl, to make an error case go faster. I know, the extra check is just a 1 or 2 cycles if branch prediction is right, but still.
I applied the patch because the delay could be substantial,
I know, but only for seriously misbehaving userspace, no? See my comment is really a minor one - lets say 2 more cycles for something that exited to userspace - nobody would even notice. I am just disturbed by the fact that we care about something that is not slow-path but broken beyond repair (why does userspace call a non-KVM ioctl on a fd of a vcpu from a different thread (otherwise the mutex would be free)?
Please, can we have an explanation, e.g. something like
"while using trinity to fuzz KVM, we noticed long stalls on invalid ioctls. Lets bail out early on invalid ioctls". or similar?
Quoted 5 lines
depending on what the other VCPU is doing.
Perhaps something like this would be
better?(Untested, but Tested-by/Reviewed-bys are welcome).
Given that it makes sense to improve a misbehaving userspace, I prefer Davids variant as the patch is smaller and easier to get right. No need to revert, but a better explanation for the use case would be appeciated.
Christian
Quoted 70 lines
Paolo
virt/kvm/kvm_main.c +23
-7@@ -117,12 +117,10 @@ bool kvm_is_mmio_pfn(pfn_t pfn)/* * Switches to specified vcpu, until a matching vcpu_put() */ -int vcpu_load (struct kvm_vcpu *vcpu) +static void __vcpu_load (struct kvm_vcpu *vcpu) {int cpu; -if (mutex_lock_killable (&vcpu->mutex)) -return -EINTR ;if (unlikely (vcpu->pid != current->pids[PIDTYPE_PID ].pid)) {/* The thread running this VCPU changed. */ struct pid *oldpid = vcpu->pid;@@ -136,6 +134,14 @@ int vcpu_load(struct kvm_vcpu *vcpu)preempt_notifier_register (&vcpu->preempt_notifier);kvm_arch_vcpu_load (vcpu, cpu);put_cpu (); +} + +int vcpu_load (struct kvm_vcpu *vcpu) +{ +if (mutex_lock_killable (&vcpu->mutex)) +return -EINTR ; + +__vcpu_load (vcpu);return 0 ; }@@ -1989,9 +1995,6 @@ static long kvm_vcpu_ioctl(struct file *filp,if (vcpu->kvm->mm != current->mm)return -EIO ; -if (unlikely (_IOC_TYPE (ioctl) !=KVMIO )) -return -EINVAL ; -#if defined(CONFIG_S390 ) || defined(CONFIG_PPC ) || defined(CONFIG_MIPS )/* * Special cases: vcpu ioctls that are asynchronous to vcpu execution, @@ -2001,8 +2004,21 @@ static long kvm_vcpu_ioctl(struct file *filp,return kvm_arch_vcpu_ioctl (filp, ioctl, arg);#endif + if (!mutex_trylock(&vcpu->mutex))) { +/* +* Before a potentially long sleep, check if we'd exit anyway. +* The common case is for the mutex not to be contended, when +* this does not add overhead. +*/ +if (unlikely (_IOC_TYPE (ioctl) !=KVMIO )) +return -EINVAL ; + +if (mutex_lock_killable (&vcpu->mutex)) +return -EINTR ; + } + - r =vcpu_load (vcpu); + r =__vcpu_load (vcpu);if (r)return r;switch (ioctl) {
Paolo Bonzini
maintainerIl 22/09/2014 21:20, Christian Borntraeger ha scritto:
Quoted 1 line
"while using trinity to fuzz KVM, we noticed long stalls on invalid ioctls. Lets bail out early on invalid ioctls". or similar?
Okay. David, can you explain how you found it so that I can make up my mind?
Gleb and Marcelo, a fourth and fifth opinion? :)
Paolo
Quoted 8 lines · Paolo Bonzini
Il 22/09/2014 21:20, Christian Borntraeger ha scritto:
Quoted 1 line
"while using trinity to fuzz KVM, we noticed long stalls on invalid ioctls. Lets bail out early on invalid ioctls". or similar?
Okay. David, can you explain how you found it so that I can make up my mind?
Gleb and Marcelo, a fourth and fifth opinion? :)
I agree with Christian that simpler fix is better here. The overhead is minimal. If we ever notice this overhead we can revert the patch all together since the problem it fixes can only be inflicted on userspace by itself and there are myriads other ways userspace can hurt itself.
--
Gleb.
Quoted 7 lines · Gleb Natapov
Quoted 6 lines · Paolo Bonzini
Il 22/09/2014 21:20, Christian Borntraeger ha scritto:
Quoted 1 line
"while using trinity to fuzz KVM, we noticed long stalls on invalid ioctls. Lets bail out early on invalid ioctls". or similar?
Okay. David, can you explain how you found it so that I can make up my mind?
Quoted 8 lines
Quoted 2 lines
Gleb and Marcelo, a fourth and fifth opinion? :)
I agree with Christian that simpler fix is better here. The overhead is minimal. If we ever notice this overhead we can revert the patch all together since the problem it fixes can only be inflicted on userspace by itself and there are myriads other ways userspace can hurt itself.
Yes. Davids explanation also makes sense as a commit message. Paolo, if you use David patch with a better description of the "why" I am fine with this patch.
Christian
Paolo Bonzini
maintainerIl 23/09/2014 10:06, Christian Borntraeger ha scritto:
Quoted 3 lines
Yes. Davids explanation also makes sense as a commit message. Paolo, if you use David patch with a better description of the "why" I am fine with this patch.
Done, thanks everybody!
Paolo
Quoted 11 lines · Christian Borntraeger
Quoted 5 lines · Paolo Bonzini
Il 22/09/2014 15:45, Christian Borntraeger ha scritto:
Quoted 2 lines
We now have an extra condition check for every valid ioctl, to make an error case go faster. I know, the extra check is just a 1 or 2 cycles if branch prediction is right, but still.
I applied the patch because the delay could be substantial,
I know, but only for seriously misbehaving userspace, no? See my comment is really a minor one - lets say 2 more cycles for something that exited to userspace - nobody would even notice. I am just disturbed by the fact that we care about something that is not slow-path but broken beyond repair (why does userspace call a non-KVM ioctl on a fd of a vcpu from a different thread (otherwise the mutex would be free)?
Please, can we have an explanation, e.g. something like
"while using trinity to fuzz KVM, we noticed long stalls on invalid ioctls. Lets bail out early on invalid ioctls". or similar?
We noticed this while using code that inspects the open file descriptors of a process. One of the things it did was check if each file descriptor was a tty (isatty() calls ioctl(TCGETS)).
Quoted 82 lines
Quoted 7 lines
depending on what the other VCPU is doing.
Perhaps something like this would be
better?(Untested, but Tested-by/Reviewed-bys are welcome).
Given that it makes sense to improve a misbehaving userspace, I prefer Davids variant as the patch is smaller and easier to get right. No need to revert, but a better explanation for the use case would be appeciated.
Christian
Quoted 70 lines
Paolo
virt/kvm/kvm_main.c +23
-7@@ -117,12 +117,10 @@ bool kvm_is_mmio_pfn(pfn_t pfn)/* * Switches to specified vcpu, until a matching vcpu_put() */ -int vcpu_load (struct kvm_vcpu *vcpu) +static void __vcpu_load (struct kvm_vcpu *vcpu) {int cpu; -if (mutex_lock_killable (&vcpu->mutex)) -return -EINTR ;if (unlikely (vcpu->pid != current->pids[PIDTYPE_PID ].pid)) {/* The thread running this VCPU changed. */ struct pid *oldpid = vcpu->pid;@@ -136,6 +134,14 @@ int vcpu_load(struct kvm_vcpu *vcpu)preempt_notifier_register (&vcpu->preempt_notifier);kvm_arch_vcpu_load (vcpu, cpu);put_cpu (); +} + +int vcpu_load (struct kvm_vcpu *vcpu) +{ +if (mutex_lock_killable (&vcpu->mutex)) +return -EINTR ; + +__vcpu_load (vcpu);return 0 ; }@@ -1989,9 +1995,6 @@ static long kvm_vcpu_ioctl(struct file *filp,if (vcpu->kvm->mm != current->mm)return -EIO ; -if (unlikely (_IOC_TYPE (ioctl) !=KVMIO )) -return -EINVAL ; -#if defined(CONFIG_S390 ) || defined(CONFIG_PPC ) || defined(CONFIG_MIPS )/* * Special cases: vcpu ioctls that are asynchronous to vcpu execution, @@ -2001,8 +2004,21 @@ static long kvm_vcpu_ioctl(struct file *filp,return kvm_arch_vcpu_ioctl (filp, ioctl, arg);#endif + if (!mutex_trylock(&vcpu->mutex))) { +/* +* Before a potentially long sleep, check if we'd exit anyway. +* The common case is for the mutex not to be contended, when +* this does not add overhead. +*/ +if (unlikely (_IOC_TYPE (ioctl) !=KVMIO )) +return -EINVAL ; + +if (mutex_lock_killable (&vcpu->mutex)) +return -EINTR ; + } + - r =vcpu_load (vcpu); + r =__vcpu_load (vcpu);if (r)return r;switch (ioctl) {
Quoted 2 lines · David Matlack
vcpu ioctls can hang the calling thread if issued while a vcpu is running.
There is a mutex per-vcpu, so thats expected, OK...
Quoted 2 lines
If we know ioctl is going to be rejected as invalid anyway, we can fail before trying to take the vcpu mutex.
Consider a valid ioctl that takes the vcpu mutex. If you need immediate access for that valid ioctl, it is necessary to interrupt thread which KVM_RUN ioctl executes.
So knowledge of whether KVM_RUN is being executed is expected in userspace (either
that or ask the KVM_RUN thread to run the ioctl for you, as qemu does).
Can't see why having different behaviour for valid/invalid ioctls is a good thing.
Quoted 2 lines
This patch does not change functionality, it just makes invalid ioctls fail faster.
Should not be executing vcpu ioctls without interrupt KVM_RUN in the first place.
Paolo Bonzini
maintainerIl 22/09/2014 22:08, Marcelo Tosatti ha scritto:
Quoted 5 lines
Quoted 2 lines
This patch does not change functionality, it just makes invalid ioctls fail faster.
Should not be executing vcpu ioctls without interrupt
KVM_RUNin the first place.
This is not entirely true, there are a couple of asynchronous ioctls though they are not used on x86 (commit 2122ff5eab8f, KVM: move vcpu locking to dispatcher for generic vcpu ioctls, 2010-05-13).
Paolo
Quoted 4 lines · Paolo Bonzini
Il 22/09/2014 22:08, Marcelo Tosatti ha scritto:
Quoted 3 lines
Quoted 2 lines
This patch does not change functionality, it just makes invalid ioctls fail faster.
Quoted 8 lines
Quoted 2 lines
Should not be executing vcpu ioctls without interrupt
KVM_RUNin the first place.This is not entirely true, there are a couple of asynchronous ioctls though they are not used on x86 (commit
2122ff5eab8f, KVM: move vcpu locking to dispatcher for generic vcpu ioctls, 2010-05-13).Paolo
Alright.
s/Should not be executing vcpu ioctls/Should not be executing vcpu ioctls which take vcpu mutex/
Quoted 17 lines · Marcelo Tosatti
Quoted 2 lines · David Matlack
vcpu ioctls can hang the calling thread if issued while a vcpu is running.
There is a mutex per-vcpu, so thats expected, OK...
Quoted 2 lines
If we know ioctl is going to be rejected as invalid anyway, we can fail before trying to take the vcpu mutex.
Consider a valid ioctl that takes the vcpu mutex. If you need immediate access for that valid ioctl, it is necessary to interrupt thread which
KVM_RUNioctl executes.So knowledge of whether
KVM_RUNis being executed is expected in userspace (either
that or ask theKVM_RUNthread to run the ioctl for you, as qemu does).
Quoted 8 lines
Can't see why having different behaviour for valid/invalid ioctls is a good thing.
Quoted 2 lines
This patch does not change functionality, it just makes invalid ioctls fail faster.
Should not be executing vcpu ioctls without interrupt
KVM_RUNin the first place.
This patch is trying to be nice to code that isn't aware it's probing kvm file descriptors. We saw long hangs with some generic process inspection code that was probing all open file descriptors. There's no reason non-kvm ioctls should have to wait for the vcpu mutex to become available just to fail.
Quoted 8 lines · David Matlack
Quoted 2 lines
Should not be executing vcpu ioctls without interrupt
KVM_RUNin the first place.This patch is trying to be nice to code that isn't aware it's probing kvm file descriptors. We saw long hangs with some generic process inspection code that was probing all open file descriptors. There's no reason non-kvm ioctls should have to wait for the vcpu mutex to become available just to fail.
OK then, please add the usecase to the changelog.