kvm->lock vs. SRCU sync optimizations
v2 cover kvm->lock vs. SRCU sync optimizations
This series is mostly about unlocking kvm->lock before synchronizing SRCU. Discussed at https://lore.kernel.org/kvm/[email protected]/ .
I'm mentioning the fact it's an optimization (not a bugfix; at least under the assumption that Xen does not break the lock order anymore) meant to reduce the time spent under the mutex. Sean, would that suffice?
Along the way simplification and cleanups are smuggled.
And, by the way, what about other sync-under-lock cases such as
kvm_arch_vm_ioctl KVM_CREATE_PIT
mutex_lock(&kvm->lock)
kvm_create_pit()
kvm_io_bus_register_dev()
synchronize_srcu_expedited(&kvm->srcu)Does this qualify for some form of refactoring?
Michal Luczaj (6):
KVM: x86: Optimize kvm->lock and SRCU interaction
(KVM_SET_PMU_EVENT_FILTER)
KVM: x86: Optimize kvm->lock and SRCU interaction
(KVM_X86_SET_MSR_FILTER)
KVM: x86: Simplify msr_filter update
KVM: x86: Explicitly state lockdep condition of msr_filter update
KVM: x86: Remove unnecessary initialization in
kvm_vm_ioctl_set_msr_filter()
KVM: x86: Simplify msr_io()
arch/x86/kvm/pmu.c | 3 +-- arch/x86/kvm/x86.c | 22 +++++++--------------- 2 files changed, 8 insertions(+), 17 deletions(-)
Signature · 1 line
2.39.0
PATCH v2 1/6 KVM: x86: Optimize kvm->lock and SRCU interaction (KVM_SET_PMU_EVENT_FILTER)
Reduce time spent holding kvm->lock: unlock mutex before calling synchronize_srcu_expedited().
--- arch/x86/kvm/pmu.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
arch/x86/kvm/pmu.c +1 -2
@@ -633,6 +633,7 @@ int kvm_vm_ioctl_set_pmu_event_filter(struct kvm *kvm, void __user *argp)mutex_lock (&kvm->lock); filter =rcu_replace_pointer (kvm->arch.pmu_event_filter, filter,mutex_is_locked (&kvm->lock)); +mutex_unlock (&kvm->lock);synchronize_srcu_expedited (&kvm->srcu); BUILD_BUG_ON(sizeof(((struct kvm_pmu *)0)->reprogram_pmi) >
@@ -643,8 +644,6 @@ int kvm_vm_ioctl_set_pmu_event_filter(struct kvm *kvm, void __user *argp)kvm_make_all_cpus_request (kvm,KVM_REQ_PMU ); -mutex_unlock (&kvm->lock); - r =0 ; cleanup:kfree (filter);
Signature · 1 line
2.39.0
PATCH v2 2/6 KVM: x86: Optimize kvm->lock and SRCU interaction (KVM_X86_SET_MSR_FILTER)
In reply to Michal LuczajReduce time spent holding kvm->lock: unlock mutex before calling synchronize_srcu().
--- arch/x86/kvm/x86.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/x86.c +1 -1
@@ -6491,12 +6491,12 @@ static int kvm_vm_ioctl_set_msr_filter(struct kvm *kvm, old_filter =srcu_dereference_check (kvm->arch.msr_filter, &kvm->srcu,1 );rcu_assign_pointer (kvm->arch.msr_filter, new_filter); +mutex_unlock (&kvm->lock);synchronize_srcu (&kvm->srcu);kvm_free_msr_filter (old_filter);kvm_make_all_cpus_request (kvm,KVM_REQ_MSR_FILTER_CHANGED ); -mutex_unlock (&kvm->lock);return 0 ; }
Signature · 1 line
2.39.0
PATCH v2 3/6 KVM: x86: Simplify msr_filter update
In reply to Michal LuczajReplace srcu_dereference()+rcu_assign_pointer() sequence with a single rcu_replace_pointer().
--- arch/x86/kvm/x86.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
arch/x86/kvm/x86.c +1 -4
@@ -6486,11 +6486,8 @@ static int kvm_vm_ioctl_set_msr_filter(struct kvm *kvm, }mutex_lock (&kvm->lock); -/* The per-VM filter is protected by kvm->lock... */ - old_filter =srcu_dereference_check (kvm->arch.msr_filter, &kvm->srcu,1 ); - -rcu_assign_pointer (kvm->arch.msr_filter, new_filter); + old_filter =rcu_replace_pointer (kvm->arch.msr_filter, new_filter,1 );mutex_unlock (&kvm->lock);synchronize_srcu (&kvm->srcu);
Signature · 1 line
2.39.0
PATCH v2 4/6 KVM: x86: Explicitly state lockdep condition of msr_filter update
In reply to Michal LuczajReplace 1 with the actual mutex_is_locked() check.
--- arch/x86/kvm/x86.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
arch/x86/kvm/x86.c +2 -1
@@ -6487,7 +6487,8 @@ static int kvm_vm_ioctl_set_msr_filter(struct kvm *kvm,mutex_lock (&kvm->lock);/* The per-VM filter is protected by kvm->lock... */ - old_filter =rcu_replace_pointer (kvm->arch.msr_filter, new_filter,1 ); + old_filter =rcu_replace_pointer (kvm->arch.msr_filter, new_filter, +mutex_is_locked (&kvm->lock));mutex_unlock (&kvm->lock);synchronize_srcu (&kvm->srcu);
Signature · 1 line
2.39.0
PATCH v2 5/6 KVM: x86: Remove unnecessary initialization in kvm_vm_ioctl_set_msr_filter()
In reply to Michal LuczajDo not initialize the value of r, as it will be overwritten.
--- arch/x86/kvm/x86.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/x86.c +1 -1
@@ -6460,7 +6460,7 @@ static int kvm_vm_ioctl_set_msr_filter(struct kvm *kvm,struct kvm_x86_msr_filter *new_filter, *old_filter;bool default_allow;bool empty = true; -int r =0 ; +int r;u32 i;if (filter->flags & ~KVM_MSR_FILTER_VALID_MASK )
Signature · 1 line
2.39.0
PATCH v2 6/6 KVM: x86: Simplify msr_io()
In reply to Michal Luczaj__msr_io() never returns a negative value. Remove unnecessary checks.
--- arch/x86/kvm/x86.c | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-)
arch/x86/kvm/x86.c +3 -9
@@ -4285,8 +4285,8 @@ static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs, {struct kvm_msrs msrs;struct kvm_msr_entry *entries; -int r, n;unsigned size; +int r; r = -EFAULT ;if (copy_from_user (&msrs, user_msrs,sizeof (msrs)))
@@ -4303,17 +4303,11 @@ static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs, goto out; } - r = n =__msr_io (vcpu, &msrs, entries, do_msr); -if (r <0 ) - goto out_free; + r =__msr_io (vcpu, &msrs, entries, do_msr); - r = -EFAULT ;if (writeback &©_to_user (user_msrs->entries, entries, size)) - goto out_free; - - r = n; + r = -EFAULT ; -out_free:kfree (entries); out:return r;
Signature · 1 line
2.39.0
Quoted 8 lines · Michal Luczaj
This series is mostly about unlocking
kvm->lockbefore synchronizing SRCU. Discussed at https://lore.kernel.org/kvm/[email protected]/ .I'm mentioning the fact it's an optimization (not a bugfix; at least under the assumption that Xen does not break the lock order anymore) meant to reduce the time spent under the mutex. Sean, would that suffice?
[...]
Applied to kvm-x86 misc, thanks!
Note, I massaged a few changelogs to provide more context and justification, but didn't see a need to respond to individual patches.
[1/6] KVM: x86: Optimize kvm->lock and SRCU interaction (KVM_SET_PMU_EVENT_FILTER)
https://github.com/kvm-x86/linux/commit/95744a90db18 [2/6] KVM: x86: Optimize kvm->lock and SRCU interaction (KVM_X86_SET_MSR_FILTER) https://github.com/kvm-x86/linux/commit/708f799d22fe [3/6] KVM: x86: Simplify msr_filter update https://github.com/kvm-x86/linux/commit/4d85cfcaa82f [4/6] KVM: x86: Explicitly state lockdep condition of msr_filter update https://github.com/kvm-x86/linux/commit/1fdefb8bd862 [5/6] KVM: x86: Remove unnecessary initialization in kvm_vm_ioctl_set_msr_filter() https://github.com/kvm-x86/linux/commit/4559e6cf45b5 [6/6] KVM: x86: Simplify msr_io() https://github.com/kvm-x86/linux/commit/e73ba25fdc24
--
https://github.com/kvm-x86/linux/tree/next
https://github.com/kvm-x86/linux/tree/fixes
Quoted 8 lines · Sean Christopherson
Quoted 7 lines · Michal Luczaj
This series is mostly about unlocking
kvm->lockbefore synchronizing SRCU. Discussed at https://lore.kernel.org/kvm/[email protected]/ .I'm mentioning the fact it's an optimization (not a bugfix; at least under the assumption that Xen does not break the lock order anymore) meant to reduce the time spent under the mutex. Sean, would that suffice?
Quoted 6 lines
Quoted 1 line
[...]
Applied to kvm-x86 misc, thanks!
Note, I massaged a few changelogs to provide more context and justification, but didn't see a need to respond to individual patches.
Sure, thanks.
Michal