KVM: arm64: Fix vcpu->mutex v. kvm->lock inversion
v2 cover KVM: arm64: Fix vcpu->mutex v. kvm->lock inversion
As it so happens, lock ordering in KVM/arm64 is completely backwards. There's a significant amount of VM-wide state that needs to be accessed from the context of a vCPU. Until now, this was accomplished by acquiring the kvm->lock, but that cannot be nested within vcpu->mutex.
This series fixes the issue with some fine-grained locking for MP state and a new, dedicated mutex that can nest with both kvm->lock and vcpu->mutex.
Tested with kvmtool and QEMU scaled up to 64 vCPUs on a kernel w/ lockdep enabled. Applies to kvmarm/next.
v1: http://lore.kernel.org/kvmarm/[email protected]
v1 -> v2:
- Add a dedicated lock for serializing writes to MP state
- Inform lockdep of acquisition order at time of VM/vCPU creation
- Plug a race with GIC creation (Sean)
- Use the config_lock in GIC ITS flows as well. There is now a single (valid) use of kvm->lock when enabling MTE.
Oliver Upton (4):
KVM: arm64: Avoid vcpu->mutex v. kvm->lock inversion in CPU_ON
KVM: arm64: Avoid lock inversion when setting the VM register width
KVM: arm64: Use config_lock to protect data ordered against KVM_RUN
KVM: arm64: Use config_lock to protect vgic state
arch/arm64/include/asm/kvm_host.h | 4 ++ arch/arm64/kvm/arm.c | 45 +++++++++++++++++++---- arch/arm64/kvm/guest.c | 2 + arch/arm64/kvm/hypercalls.c | 4 +- arch/arm64/kvm/pmu-emul.c | 23 +++--------- arch/arm64/kvm/psci.c | 19 +++++----- arch/arm64/kvm/reset.c | 16 ++++---- arch/arm64/kvm/vgic/vgic-debug.c | 8 ++-- arch/arm64/kvm/vgic/vgic-init.c | 33 ++++++++++------- arch/arm64/kvm/vgic/vgic-its.c | 29 ++++++--------- arch/arm64/kvm/vgic/vgic-kvm-device.c | 53 ++++++++++++--------------- arch/arm64/kvm/vgic/vgic-mmio-v3.c | 4 +- arch/arm64/kvm/vgic/vgic-mmio.c | 12 +++--- arch/arm64/kvm/vgic/vgic-v4.c | 11 +++--- arch/arm64/kvm/vgic/vgic.c | 2 +- 15 files changed, 146 insertions(+), 119 deletions(-)
Signature · 1 line
2.40.0.rc1.284.g88254d51c5-goog
PATCH v2 1/4 KVM: arm64: Avoid vcpu->mutex v. kvm->lock inversion in CPU_ON
KVM/arm64 had the lock ordering backwards on vcpu->mutex and kvm->lock from the very beginning. One such example is the way vCPU resets are
handled: the kvm->lock is acquired while handling a guest CPU_ON PSCI call.
Add a dedicated lock to serialize writes to kvm_vcpu_arch::mp_state. Hold the lock in kvm_psci_vcpu_on() to protect against changes while the reset state is being configured. Ensure that readers read mp_state exactly once. While at it, plug yet another race by taking the mp_state_lock in the KVM_SET_MP_STATE ioctl handler.
As changes to MP state are now guarded with a dedicated lock, drop the kvm->lock acquisition from the PSCI CPU_ON path. Similarly, move the reader of reset_state outside of the kvm->lock and insert a barrier to ensure reset_state is read before dropping the pending reset state.
While the kvm->lock inversion still exists in kvm_reset_vcpu(), at least now PSCI CPU_ON no longer depends on it for serializing vCPU reset.
--- arch/arm64/include/asm/kvm_host.h | 1 + arch/arm64/kvm/arm.c | 23 ++++++++++++++++++----- arch/arm64/kvm/psci.c | 19 ++++++++++--------- arch/arm64/kvm/reset.c | 10 ++++++---- 4 files changed, 35 insertions(+), 18 deletions(-)
arch/arm64/include/asm/kvm_host.h +1 -0
@@ -522,6 +522,7 @@ struct kvm_vcpu_arch {/* vcpu power state */ struct kvm_mp_state mp_state; +spinlock_t mp_state_lock;/* Cache some mmu pages needed inside spinlock regions */ struct kvm_mmu_memory_cache mmu_page_cache;
arch/arm64/kvm/arm.c +18 -5
@@ -326,6 +326,8 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) {int err; +spin_lock_init (&vcpu->arch.mp_state_lock); +/* Force users to call KVM_ARM_VCPU_INIT */ vcpu->arch.target =-1 ;bitmap_zero (vcpu->arch.features,KVM_VCPU_MAX_FEATURES );
@@ -443,16 +445,23 @@ void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) vcpu->cpu =-1 ; } -void kvm_arm_vcpu_power_off (struct kvm_vcpu *vcpu) +static void __kvm_arm_vcpu_power_off (struct kvm_vcpu *vcpu) { vcpu->arch.mp_state.mp_state =KVM_MP_STATE_STOPPED ;kvm_make_request (KVM_REQ_SLEEP , vcpu);kvm_vcpu_kick (vcpu); } +void kvm_arm_vcpu_power_off (struct kvm_vcpu *vcpu) +{ +spin_lock (&vcpu->arch.mp_state_lock); +__kvm_arm_vcpu_power_off (vcpu); +spin_unlock (&vcpu->arch.mp_state_lock); +} +bool kvm_arm_vcpu_stopped (struct kvm_vcpu *vcpu) { -return vcpu->arch.mp_state.mp_state ==KVM_MP_STATE_STOPPED ; +return READ_ONCE (vcpu->arch.mp_state.mp_state) ==KVM_MP_STATE_STOPPED ; }static void kvm_arm_vcpu_suspend (struct kvm_vcpu *vcpu)
@@ -464,13 +473,13 @@ static void kvm_arm_vcpu_suspend(struct kvm_vcpu *vcpu)static bool kvm_arm_vcpu_suspended (struct kvm_vcpu *vcpu) { -return vcpu->arch.mp_state.mp_state ==KVM_MP_STATE_SUSPENDED ; +return READ_ONCE (vcpu->arch.mp_state.mp_state) ==KVM_MP_STATE_SUSPENDED ; }int kvm_arch_vcpu_ioctl_get_mpstate (struct kvm_vcpu *vcpu,struct kvm_mp_state *mp_state) { - *mp_state = vcpu->arch.mp_state; + *mp_state =READ_ONCE (vcpu->arch.mp_state);return 0 ; }
@@ -480,12 +489,14 @@ int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, {int ret =0 ; +spin_lock (&vcpu->arch.mp_state_lock); +switch (mp_state->mp_state) {case KVM_MP_STATE_RUNNABLE : vcpu->arch.mp_state = *mp_state;break ;case KVM_MP_STATE_STOPPED : -kvm_arm_vcpu_power_off (vcpu); +__kvm_arm_vcpu_power_off (vcpu);break ;case KVM_MP_STATE_SUSPENDED :kvm_arm_vcpu_suspend (vcpu);
@@ -494,6 +505,8 @@ int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, ret = -EINVAL ; } +spin_unlock (&vcpu->arch.mp_state_lock); +return ret; }
arch/arm64/kvm/psci.c +10 -9
@@ -62,6 +62,7 @@ static unsigned long kvm_psci_vcpu_on(struct kvm_vcpu *source_vcpu)struct vcpu_reset_state *reset_state;struct kvm *kvm = source_vcpu->kvm;struct kvm_vcpu *vcpu =NULL ; +int ret =PSCI_RET_SUCCESS ;unsigned long cpu_id; cpu_id =smccc_get_arg1 (source_vcpu);
@@ -76,11 +77,15 @@ static unsigned long kvm_psci_vcpu_on(struct kvm_vcpu *source_vcpu)*/ if (!vcpu)return PSCI_RET_INVALID_PARAMS ; + +spin_lock (&vcpu->arch.mp_state_lock);if (!kvm_arm_vcpu_stopped (vcpu)) {if (kvm_psci_version (source_vcpu) !=KVM_ARM_PSCI_0_1 ) -return PSCI_RET_ALREADY_ON ; + ret =PSCI_RET_ALREADY_ON ;else -return PSCI_RET_INVALID_PARAMS ; + ret =PSCI_RET_INVALID_PARAMS ; + + goto out_unlock; } reset_state = &vcpu->arch.reset_state;
@@ -108,7 +113,9 @@ static unsigned long kvm_psci_vcpu_on(struct kvm_vcpu *source_vcpu) vcpu->arch.mp_state.mp_state =KVM_MP_STATE_RUNNABLE ;kvm_vcpu_wake_up (vcpu); -return PSCI_RET_SUCCESS ; +out_unlock: +spin_unlock (&vcpu->arch.mp_state_lock); +return ret; }static unsigned long kvm_psci_vcpu_affinity_info (struct kvm_vcpu *vcpu)
@@ -229,7 +236,6 @@ static unsigned long kvm_psci_check_allowed_function(struct kvm_vcpu *vcpu, u32static int kvm_psci_0_2_call (struct kvm_vcpu *vcpu) { -struct kvm *kvm = vcpu->kvm;u32 psci_fn =smccc_get_function (vcpu);unsigned long val;int ret =1 ;
@@ -254,9 +260,7 @@ static int kvm_psci_0_2_call(struct kvm_vcpu *vcpu)kvm_psci_narrow_to_32bit (vcpu); fallthrough;case PSCI_0_2_FN64_CPU_ON : -mutex_lock (&kvm->lock); val =kvm_psci_vcpu_on (vcpu); -mutex_unlock (&kvm->lock);break ;case PSCI_0_2_FN_AFFINITY_INFO :kvm_psci_narrow_to_32bit (vcpu);
@@ -395,7 +399,6 @@ static int kvm_psci_1_x_call(struct kvm_vcpu *vcpu, u32 minor)static int kvm_psci_0_1_call (struct kvm_vcpu *vcpu) { -struct kvm *kvm = vcpu->kvm;u32 psci_fn =smccc_get_function (vcpu);unsigned long val;
@@ -405,9 +408,7 @@ static int kvm_psci_0_1_call(struct kvm_vcpu *vcpu) val =PSCI_RET_SUCCESS ;break ;case KVM_PSCI_FN_CPU_ON : -mutex_lock (&kvm->lock); val =kvm_psci_vcpu_on (vcpu); -mutex_unlock (&kvm->lock);break ;default : val =PSCI_RET_NOT_SUPPORTED ;
arch/arm64/kvm/reset.c +6 -4
@@ -264,15 +264,17 @@ int kvm_reset_vcpu(struct kvm_vcpu *vcpu)mutex_lock (&vcpu->kvm->lock); ret =kvm_set_vm_width (vcpu); -if (!ret) { - reset_state = vcpu->arch.reset_state; -WRITE_ONCE (vcpu->arch.reset_state.reset, false); - }mutex_unlock (&vcpu->kvm->lock);if (ret)return ret; + reset_state = vcpu->arch.reset_state; + +/* Ensure reset_state is read before clearing the pending state */ +smp_rmb (); +WRITE_ONCE (vcpu->arch.reset_state.reset, false); +/* Reset PMU outside of the non-preemptible section */ kvm_pmu_vcpu_reset (vcpu);
Signature · 1 line
2.40.0.rc1.284.g88254d51c5-goog
PATCH v2 2/4 KVM: arm64: Avoid lock inversion when setting the VM register width
In reply to Oliver Uptonkvm->lock must be taken outside of the vcpu->mutex. Of course, the locking documentation for KVM makes this abundantly clear. Nonetheless, the locking order in KVM/arm64 has been wrong for quite a while; we acquire the kvm->lock while holding the vcpu->mutex all over the shop.
All was seemingly fine until commit 42a90008f890 ("KVM: Ensure lockdep knows about kvm->lock vs. vcpu->mutex ordering rule") caught us with our pants down, leading to lockdep barfing:
======================================================
WARNING: possible circular locking dependency detected 6.2.0-rc7+ #19 Not tainted
------------------------------------------------------
qemu-system-aar/859 is trying to acquire lock:
ffff5aa69269eba0 (&host_kvm->lock){+.+.}-{3:3}, at: kvm_reset_vcpu+0x34/0x274
but task is already holding lock:
ffff5aa68768c0b8 (&vcpu->mutex){+.+.}-{3:3}, at: kvm_vcpu_ioctl+0x8c/0xba0
which lock already depends on the new lock.
Add a dedicated lock to serialize writes to VM-scoped configuration from the context of a vCPU. Protect the register width flags with the new lock, thus avoiding the need to grab the kvm->lock while holding vcpu->mutex in kvm_reset_vcpu().
--- arch/arm64/include/asm/kvm_host.h | 3 +++ arch/arm64/kvm/arm.c | 18 ++++++++++++++++++ arch/arm64/kvm/reset.c | 6 +++--- 3 files changed, 24 insertions(+), 3 deletions(-)
arch/arm64/include/asm/kvm_host.h +3 -0
@@ -185,6 +185,9 @@ struct kvm_protected_vm { };struct kvm_arch { +/* Protects VM-scoped configuration data */ +struct mutex config_lock; +struct kvm_s2_mmu mmu;/* VTCR_EL2 value for this VM */
arch/arm64/kvm/arm.c +18 -0
@@ -128,6 +128,16 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) {int ret; +mutex_init (&kvm->arch.config_lock); + +#ifdef CONFIG_LOCKDEP +/* Clue in lockdep that the config_lock must be taken inside kvm->lock */ +mutex_lock (&kvm->lock); +mutex_lock (&kvm->arch.config_lock); +mutex_unlock (&kvm->arch.config_lock); +mutex_unlock (&kvm->lock); +#endif + ret =kvm_share_hyp (kvm, kvm +1 );if (ret)return ret;
@@ -328,6 +338,14 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)spin_lock_init (&vcpu->arch.mp_state_lock); +#ifdef CONFIG_LOCKDEP +/* Inform lockdep that the config_lock is acquired after vcpu->mutex */ +mutex_lock (&vcpu->mutex); +mutex_lock (&vcpu->kvm->arch.config_lock); +mutex_unlock (&vcpu->kvm->arch.config_lock); +mutex_unlock (&vcpu->mutex); +#endif +/* Force users to call KVM_ARM_VCPU_INIT */ vcpu->arch.target =-1 ;bitmap_zero (vcpu->arch.features,KVM_VCPU_MAX_FEATURES );
arch/arm64/kvm/reset.c +3 -3
@@ -205,7 +205,7 @@ static int kvm_set_vm_width(struct kvm_vcpu *vcpu) is32bit =vcpu_has_feature (vcpu,KVM_ARM_VCPU_EL1_32BIT ); -lockdep_assert_held (&kvm->lock); +lockdep_assert_held (&kvm->arch.config_lock);if (test_bit (KVM_ARCH_FLAG_REG_WIDTH_CONFIGURED , &kvm->arch.flags)) {/*
@@ -262,9 +262,9 @@ int kvm_reset_vcpu(struct kvm_vcpu *vcpu)bool loaded;u32 pstate; -mutex_lock (&vcpu->kvm->lock); +mutex_lock (&vcpu->kvm->arch.config_lock); ret =kvm_set_vm_width (vcpu); -mutex_unlock (&vcpu->kvm->lock); +mutex_unlock (&vcpu->kvm->arch.config_lock);if (ret)return ret;
Signature · 1 line
2.40.0.rc1.284.g88254d51c5-goog
PATCH v2 3/4 KVM: arm64: Use config_lock to protect data ordered against KVM_RUN
In reply to Oliver UptonThere are various bits of VM-scoped data that can only be configured before the first call to KVM_RUN, such as the hypercall bitmaps and the PMU. As these fields are protected by the kvm->lock and accessed while holding vcpu->mutex, this is yet another example of lock inversion.
Change out the kvm->lock for kvm->arch.config_lock in all of these instances. Opportunistically simplify the locking mechanics of the PMU configuration by holding the config_lock for the entirety of kvm_arm_pmu_v3_set_attr().
Note that this also addresses a couple of bugs. There is an unguarded read of the PMU version in KVM_ARM_VCPU_PMU_V3_FILTER which could race with KVM_ARM_VCPU_PMU_V3_SET_PMU. Additionally, until now writes to the per-vCPU vPMU irq were not serialized VM-wide, meaning concurrent calls to KVM_ARM_VCPU_PMU_V3_IRQ could lead to a false positive in pmu_irq_is_valid().
--- arch/arm64/kvm/arm.c | 4 ++-- arch/arm64/kvm/guest.c | 2 ++ arch/arm64/kvm/hypercalls.c | 4 ++-- arch/arm64/kvm/pmu-emul.c | 23 ++++++----------------- 4 files changed, 12 insertions(+), 21 deletions(-)
arch/arm64/kvm/arm.c +2 -2
@@ -624,9 +624,9 @@ int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu)if (kvm_vm_is_protected (kvm))kvm_call_hyp_nvhe (__pkvm_vcpu_init_traps, vcpu); -mutex_lock (&kvm->lock); +mutex_lock (&kvm->arch.config_lock);set_bit (KVM_ARCH_FLAG_HAS_RAN_ONCE , &kvm->arch.flags); -mutex_unlock (&kvm->lock); +mutex_unlock (&kvm->arch.config_lock);return ret; }
arch/arm64/kvm/guest.c +2 -0
@@ -957,7 +957,9 @@ int kvm_arm_vcpu_arch_set_attr(struct kvm_vcpu *vcpu,switch (attr->group) {case KVM_ARM_VCPU_PMU_V3_CTRL : +mutex_lock (&vcpu->kvm->arch.config_lock); ret =kvm_arm_pmu_v3_set_attr (vcpu, attr); +mutex_unlock (&vcpu->kvm->arch.config_lock);break ;case KVM_ARM_VCPU_TIMER_CTRL : ret =kvm_arm_timer_set_attr (vcpu, attr);
arch/arm64/kvm/hypercalls.c +2 -2
@@ -377,7 +377,7 @@ static int kvm_arm_set_fw_reg_bmap(struct kvm_vcpu *vcpu, u64 reg_id, u64 val)if (val & ~fw_reg_features)return -EINVAL ; -mutex_lock (&kvm->lock); +mutex_lock (&kvm->arch.config_lock);if (test_bit (KVM_ARCH_FLAG_HAS_RAN_ONCE , &kvm->arch.flags) && val != *fw_reg_bmap) {
@@ -387,7 +387,7 @@ static int kvm_arm_set_fw_reg_bmap(struct kvm_vcpu *vcpu, u64 reg_id, u64 val)WRITE_ONCE (*fw_reg_bmap, val); out: -mutex_unlock (&kvm->lock); +mutex_unlock (&kvm->arch.config_lock);return ret; }
arch/arm64/kvm/pmu-emul.c +6 -17
@@ -875,7 +875,7 @@ static int kvm_arm_pmu_v3_set_pmu(struct kvm_vcpu *vcpu, int pmu_id)struct arm_pmu *arm_pmu;int ret = -ENXIO ; -mutex_lock (&kvm->lock); +lockdep_assert_held (&kvm->arch.config_lock);mutex_lock (&arm_pmus_lock);list_for_each_entry (entry, &arm_pmus, entry) {
@@ -895,7 +895,6 @@ static int kvm_arm_pmu_v3_set_pmu(struct kvm_vcpu *vcpu, int pmu_id) }mutex_unlock (&arm_pmus_lock); -mutex_unlock (&kvm->lock);return ret; }
@@ -903,22 +902,20 @@ int kvm_arm_pmu_v3_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr) {struct kvm *kvm = vcpu->kvm; +lockdep_assert_held (&kvm->arch.config_lock); +if (!kvm_vcpu_has_pmu (vcpu))return -ENODEV ;if (vcpu->arch.pmu.created)return -EBUSY ; -mutex_lock (&kvm->lock);if (!kvm->arch.arm_pmu) {/* No PMU set, get the default one */ kvm->arch.arm_pmu =kvm_pmu_probe_armpmu (); -if (!kvm->arch.arm_pmu) { -mutex_unlock (&kvm->lock); +if (!kvm->arch.arm_pmu)return -ENODEV ; - } } -mutex_unlock (&kvm->lock);switch (attr->attr) {case KVM_ARM_VCPU_PMU_V3_IRQ : {
@@ -962,19 +959,13 @@ int kvm_arm_pmu_v3_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr) filter.action != KVM_PMU_EVENT_DENY))return -EINVAL ; -mutex_lock (&kvm->lock); - -if (test_bit (KVM_ARCH_FLAG_HAS_RAN_ONCE , &kvm->arch.flags)) { -mutex_unlock (&kvm->lock); +if (test_bit (KVM_ARCH_FLAG_HAS_RAN_ONCE , &kvm->arch.flags))return -EBUSY ; - }if (!kvm->arch.pmu_filter) { kvm->arch.pmu_filter =bitmap_alloc (nr_events,GFP_KERNEL_ACCOUNT ); -if (!kvm->arch.pmu_filter) { -mutex_unlock (&kvm->lock); +if (!kvm->arch.pmu_filter)return -ENOMEM ; - }/* * The default depends on the first applied filter.
@@ -993,8 +984,6 @@ int kvm_arm_pmu_v3_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)else bitmap_clear (kvm->arch.pmu_filter, filter.base_event, filter.nevents); -mutex_unlock (&kvm->lock); -return 0 ; }case KVM_ARM_VCPU_PMU_V3_SET_PMU : {
Signature · 1 line
2.40.0.rc1.284.g88254d51c5-goog
PATCH v2 4/4 KVM: arm64: Use config_lock to protect vgic state
In reply to Oliver UptonAlmost all of the vgic state is VM-scoped but accessed from the context of a vCPU. These accesses were serialized on the kvm->lock which cannot be nested within a vcpu->mutex critical section.
Move over the vgic state to using the config_lock. Tweak the lock ordering where necessary to ensure that the config_lock is acquired after the vcpu->mutex. Acquire the config_lock in kvm_vgic_create() to avoid a race between the converted flows and GIC creation.
--- arch/arm64/kvm/vgic/vgic-debug.c | 8 ++-- arch/arm64/kvm/vgic/vgic-init.c | 33 ++++++++++------- arch/arm64/kvm/vgic/vgic-its.c | 29 ++++++--------- arch/arm64/kvm/vgic/vgic-kvm-device.c | 53 ++++++++++++--------------- arch/arm64/kvm/vgic/vgic-mmio-v3.c | 4 +- arch/arm64/kvm/vgic/vgic-mmio.c | 12 +++--- arch/arm64/kvm/vgic/vgic-v4.c | 11 +++--- arch/arm64/kvm/vgic/vgic.c | 2 +- 8 files changed, 75 insertions(+), 77 deletions(-)
arch/arm64/kvm/vgic/vgic-debug.c +4 -4
@@ -85,7 +85,7 @@ static void *vgic_debug_start(struct seq_file *s, loff_t *pos)struct kvm *kvm = s->private;struct vgic_state_iter *iter; -mutex_lock (&kvm->lock); +mutex_lock (&kvm->arch.config_lock); iter = kvm->arch.vgic.iter;if (iter) { iter =ERR_PTR (-EBUSY );
@@ -104,7 +104,7 @@ static void *vgic_debug_start(struct seq_file *s, loff_t *pos)if (end_of_vgic (iter)) iter =NULL ; out: -mutex_unlock (&kvm->lock); +mutex_unlock (&kvm->arch.config_lock);return iter; }
@@ -132,12 +132,12 @@ static void vgic_debug_stop(struct seq_file *s, void *v)if (IS_ERR (v))return ; -mutex_lock (&kvm->lock); +mutex_lock (&kvm->arch.config_lock); iter = kvm->arch.vgic.iter;kfree (iter->lpi_array);kfree (iter); kvm->arch.vgic.iter =NULL ; -mutex_unlock (&kvm->lock); +mutex_unlock (&kvm->arch.config_lock); }static void print_dist_state (struct seq_file *s,struct vgic_dist *dist)
arch/arm64/kvm/vgic/vgic-init.c +20 -13
@@ -74,9 +74,6 @@ int kvm_vgic_create(struct kvm *kvm, u32 type)unsigned long i;int ret; -if (irqchip_in_kernel (kvm)) -return -EEXIST ; -/* * This function is also called by the KVM_CREATE_IRQCHIP handler, * which had no chance yet to check the availability of the GICv2
@@ -91,6 +88,13 @@ int kvm_vgic_create(struct kvm *kvm, u32 type)if (!lock_all_vcpus (kvm))return ret; +mutex_lock (&kvm->arch.config_lock); + +if (irqchip_in_kernel (kvm)) { + ret = -EEXIST ; + goto out_unlock; + } +kvm_for_each_vcpu (i, vcpu, kvm) {if (vcpu_has_run_once (vcpu)) goto out_unlock;
@@ -118,6 +122,7 @@ int kvm_vgic_create(struct kvm *kvm, u32 type)INIT_LIST_HEAD (&kvm->arch.vgic.rd_regions); out_unlock: +mutex_unlock (&kvm->arch.config_lock);unlock_all_vcpus (kvm);return ret; }
@@ -227,9 +232,9 @@ int kvm_vgic_vcpu_init(struct kvm_vcpu *vcpu)* KVM io device for the redistributor that belongs to this VCPU. */ if (dist->vgic_model ==KVM_DEV_TYPE_ARM_VGIC_V3 ) { -mutex_lock (&vcpu->kvm->lock); +mutex_lock (&vcpu->kvm->arch.config_lock); ret =vgic_register_redist_iodev (vcpu); -mutex_unlock (&vcpu->kvm->lock); +mutex_unlock (&vcpu->kvm->arch.config_lock); }return ret; }
@@ -250,7 +255,6 @@ static void kvm_vgic_vcpu_enable(struct kvm_vcpu *vcpu)* The function is generally called when nr_spis has been explicitly set * by the guest through the KVM DEVICE API. If not nr_spis is set to 256. * vgic_initialized() returns true when this function has succeeded. -* Must be called with kvm->lock held! */ int vgic_init (struct kvm *kvm) {
@@ -259,6 +263,8 @@ int vgic_init(struct kvm *kvm)int ret =0 , i;unsigned long idx; +lockdep_assert_held (&kvm->arch.config_lock); +if (vgic_initialized (kvm))return 0 ;
@@ -373,12 +379,13 @@ void kvm_vgic_vcpu_destroy(struct kvm_vcpu *vcpu) vgic_cpu->rd_iodev.base_addr =VGIC_ADDR_UNDEF ; } -/* To be called with kvm->lock held */ static void __kvm_vgic_destroy (struct kvm *kvm) {struct kvm_vcpu *vcpu;unsigned long i; +lockdep_assert_held (&kvm->arch.config_lock); +vgic_debug_destroy (kvm);kvm_for_each_vcpu (i, vcpu, kvm)
@@ -389,9 +396,9 @@ static void __kvm_vgic_destroy(struct kvm *kvm)void kvm_vgic_destroy (struct kvm *kvm) { -mutex_lock (&kvm->lock); +mutex_lock (&kvm->arch.config_lock);__kvm_vgic_destroy (kvm); -mutex_unlock (&kvm->lock); +mutex_unlock (&kvm->arch.config_lock); }/**
@@ -414,9 +421,9 @@ int vgic_lazy_init(struct kvm *kvm)if (kvm->arch.vgic.vgic_model !=KVM_DEV_TYPE_ARM_VGIC_V2 )return -EBUSY ; -mutex_lock (&kvm->lock); +mutex_lock (&kvm->arch.config_lock); ret =vgic_init (kvm); -mutex_unlock (&kvm->lock); +mutex_unlock (&kvm->arch.config_lock); }return ret;
@@ -441,7 +448,7 @@ int kvm_vgic_map_resources(struct kvm *kvm)if (likely (vgic_ready (kvm)))return 0 ; -mutex_lock (&kvm->lock); +mutex_lock (&kvm->arch.config_lock);if (vgic_ready (kvm)) goto out;
@@ -459,7 +466,7 @@ int kvm_vgic_map_resources(struct kvm *kvm) dist->ready = true; out: -mutex_unlock (&kvm->lock); +mutex_unlock (&kvm->arch.config_lock);return ret; }
arch/arm64/kvm/vgic/vgic-its.c +12 -17
@@ -2043,7 +2043,10 @@ static int vgic_its_attr_regs_access(struct kvm_device *dev,if (offset & align)return -EINVAL ; -mutex_lock (&dev->kvm->lock); +if (!lock_all_vcpus (dev->kvm)) +return -EBUSY ; + +mutex_lock (&dev->kvm->arch.config_lock);if (IS_VGIC_ADDR_UNDEF (its->vgic_its_base)) { ret = -ENXIO ;
@@ -2058,11 +2061,6 @@ static int vgic_its_attr_regs_access(struct kvm_device *dev, goto out; } -if (!lock_all_vcpus (dev->kvm)) { - ret = -EBUSY ; - goto out; - } - addr = its->vgic_its_base + offset; len = region->access_flags & VGIC_ACCESS_64bit ?8 :4 ;
@@ -2076,9 +2074,9 @@ static int vgic_its_attr_regs_access(struct kvm_device *dev, }else { *reg = region->its_read (dev->kvm, its, addr, len); } -unlock_all_vcpus (dev->kvm); out: -mutex_unlock (&dev->kvm->lock); +mutex_unlock (&dev->kvm->arch.config_lock); +unlock_all_vcpus (dev->kvm);return ret; }
@@ -2748,14 +2746,11 @@ static int vgic_its_ctrl(struct kvm *kvm, struct vgic_its *its, u64 attr)if (attr ==KVM_DEV_ARM_VGIC_CTRL_INIT )/* Nothing to do */ return 0 ; -mutex_lock (&kvm->lock); -mutex_lock (&its->its_lock); - -if (!lock_all_vcpus (kvm)) { -mutex_unlock (&its->its_lock); -mutex_unlock (&kvm->lock); +if (!lock_all_vcpus (kvm))return -EBUSY ; - } + +mutex_lock (&kvm->arch.config_lock); +mutex_lock (&its->its_lock);switch (attr) {case KVM_DEV_ARM_ITS_CTRL_RESET :
@@ -2769,9 +2764,9 @@ static int vgic_its_ctrl(struct kvm *kvm, struct vgic_its *its, u64 attr)break ; } -unlock_all_vcpus (kvm);mutex_unlock (&its->its_lock); -mutex_unlock (&kvm->lock); +mutex_unlock (&kvm->arch.config_lock); +unlock_all_vcpus (kvm);return ret; }
arch/arm64/kvm/vgic/vgic-kvm-device.c +24 -29
139 lines not on this page · raw message
arch/arm64/kvm/vgic/vgic-mmio-v3.c +2 -2
@@ -111,7 +111,7 @@ static void vgic_mmio_write_v3_misc(struct kvm_vcpu *vcpu,case GICD_CTLR : {bool was_enabled, is_hwsgi; -mutex_lock (&vcpu->kvm->lock); +mutex_lock (&vcpu->kvm->arch.config_lock); was_enabled = dist->enabled; is_hwsgi = dist->nassgireq;
@@ -139,7 +139,7 @@ static void vgic_mmio_write_v3_misc(struct kvm_vcpu *vcpu,else if (!was_enabled && dist->enabled)vgic_kick_vcpus (vcpu->kvm); -mutex_unlock (&vcpu->kvm->lock); +mutex_unlock (&vcpu->kvm->arch.config_lock);break ; }case GICD_TYPER :
arch/arm64/kvm/vgic/vgic-mmio.c +6 -6
@@ -530,13 +530,13 @@ unsigned long vgic_mmio_read_active(struct kvm_vcpu *vcpu,u32 intid =VGIC_ADDR_TO_INTID (addr,1 );u32 val; -mutex_lock (&vcpu->kvm->lock); +mutex_lock (&vcpu->kvm->arch.config_lock);vgic_access_active_prepare (vcpu, intid); val =__vgic_mmio_read_active (vcpu, addr, len);vgic_access_active_finish (vcpu, intid); -mutex_unlock (&vcpu->kvm->lock); +mutex_unlock (&vcpu->kvm->arch.config_lock);return val; }
@@ -625,13 +625,13 @@ void vgic_mmio_write_cactive(struct kvm_vcpu *vcpu, {u32 intid =VGIC_ADDR_TO_INTID (addr,1 ); -mutex_lock (&vcpu->kvm->lock); +mutex_lock (&vcpu->kvm->arch.config_lock);vgic_access_active_prepare (vcpu, intid);__vgic_mmio_write_cactive (vcpu, addr, len, val);vgic_access_active_finish (vcpu, intid); -mutex_unlock (&vcpu->kvm->lock); +mutex_unlock (&vcpu->kvm->arch.config_lock); } int vgic_mmio_uaccess_write_cactive(struct kvm_vcpu *vcpu,
@@ -662,13 +662,13 @@ void vgic_mmio_write_sactive(struct kvm_vcpu *vcpu, {u32 intid =VGIC_ADDR_TO_INTID (addr,1 ); -mutex_lock (&vcpu->kvm->lock); +mutex_lock (&vcpu->kvm->arch.config_lock);vgic_access_active_prepare (vcpu, intid);__vgic_mmio_write_sactive (vcpu, addr, len, val);vgic_access_active_finish (vcpu, intid); -mutex_unlock (&vcpu->kvm->lock); +mutex_unlock (&vcpu->kvm->arch.config_lock); } int vgic_mmio_uaccess_write_sactive(struct kvm_vcpu *vcpu,
arch/arm64/kvm/vgic/vgic-v4.c +6 -5
@@ -232,9 +232,8 @@ int vgic_v4_request_vpe_irq(struct kvm_vcpu *vcpu, int irq)* @kvm: Pointer to the VM being initialized * * We may be called each time a vITS is created, or when the -* vgic is initialized. This relies on kvm->lock to be -* held. In both cases, the number of vcpus should now be -* fixed. +* vgic is initialized. In both cases, the number of vcpus +* should now be fixed. */ int vgic_v4_init (struct kvm *kvm) {
@@ -243,6 +242,8 @@ int vgic_v4_init(struct kvm *kvm)int nr_vcpus, ret;unsigned long i; +lockdep_assert_held (&kvm->arch.config_lock); +if (!kvm_vgic_global_state.has_gicv4)return 0 ;/* Nothing to see here... move along. */
@@ -309,14 +310,14 @@ int vgic_v4_init(struct kvm *kvm)/** * vgic_v4_teardown - Free the GICv4 data structures * @kvm: Pointer to the VM being destroyed -* -* Relies on kvm->lock to be held. */ void vgic_v4_teardown (struct kvm *kvm) {struct its_vm *its_vm = &kvm->arch.vgic.its_vm;int i; +lockdep_assert_held (&kvm->arch.config_lock); +if (!its_vm->vpes)return ;
arch/arm64/kvm/vgic/vgic.c +1 -1
@@ -23,7 +23,7 @@ struct vgic_global kvm_vgic_global_state __ro_after_init = {/* * Locking order is always: -* kvm->lock (mutex) +* kvm->arch.config_lock (mutex) * its->cmd_lock (mutex) * its->its_lock (mutex) * vgic_cpu->ap_list_lock must be taken with IRQs disabled
Signature · 1 line
2.40.0.rc1.284.g88254d51c5-goog
Marc Zyngier
maintainerRe: [PATCH v2 4/4] KVM: arm64: Use config_lock to protect vgic state
Quoted 186 lines · Oliver Upton
Almost all of the vgic state is VM-scoped but accessed from the context of a vCPU. These accesses were serialized on the
kvm->lockwhich cannot be nested within avcpu->mutexcritical section.Move over the vgic state to using the
config_lock. Tweak the lock ordering where necessary to ensure that theconfig_lockis acquired after thevcpu->mutex. Acquire theconfig_lockinkvm_vgic_create()to avoid a race between the converted flows and GIC creation.Signed-off-by Oliver Upton <[email protected]>--- arch/arm64/kvm/vgic/vgic-debug.c | 8 ++-- arch/arm64/kvm/vgic/vgic-init.c | 33 ++++++++++------- arch/arm64/kvm/vgic/vgic-its.c | 29 ++++++--------- arch/arm64/kvm/vgic/vgic-kvm-device.c | 53 ++++++++++++--------------- arch/arm64/kvm/vgic/vgic-mmio-v3.c | 4 +- arch/arm64/kvm/vgic/vgic-mmio.c | 12 +++--- arch/arm64/kvm/vgic/vgic-v4.c | 11 +++--- arch/arm64/kvm/vgic/vgic.c | 2 +- 8 files changed, 75 insertions(+), 77 deletions(-)arch/arm64/kvm/vgic/vgic-debug.c +4
-4@@ -85,7 +85,7 @@ static void *vgic_debug_start(struct seq_file *s, loff_t *pos)struct kvm *kvm = s->private;struct vgic_state_iter *iter; -mutex_lock (&kvm->lock); +mutex_lock (&kvm->arch.config_lock); iter = kvm->arch.vgic.iter;if (iter) { iter =ERR_PTR (-EBUSY );@@ -104,7 +104,7 @@ static void *vgic_debug_start(struct seq_file *s, loff_t *pos)if (end_of_vgic (iter)) iter =NULL ; out: -mutex_unlock (&kvm->lock); +mutex_unlock (&kvm->arch.config_lock);return iter; }@@ -132,12 +132,12 @@ static void vgic_debug_stop(struct seq_file *s, void *v)if (IS_ERR (v))return ; -mutex_lock (&kvm->lock); +mutex_lock (&kvm->arch.config_lock); iter = kvm->arch.vgic.iter;kfree (iter->lpi_array);kfree (iter); kvm->arch.vgic.iter =NULL ; -mutex_unlock (&kvm->lock); +mutex_unlock (&kvm->arch.config_lock); }static void print_dist_state (struct seq_file *s,struct vgic_dist *dist)arch/arm64/kvm/vgic/vgic-init.c +20
-13@@ -74,9 +74,6 @@ int kvm_vgic_create(struct kvm *kvm, u32 type)unsigned long i;int ret; -if (irqchip_in_kernel (kvm)) -return -EEXIST ; -/* * This function is also called by the KVM_CREATE_IRQCHIP handler, * which had no chance yet to check the availability of the GICv2 @@ -91,6 +88,13 @@ int kvm_vgic_create(struct kvm *kvm, u32 type)if (!lock_all_vcpus (kvm))return ret; +mutex_lock (&kvm->arch.config_lock); + +if (irqchip_in_kernel (kvm)) { + ret = -EEXIST ; + goto out_unlock; + } +kvm_for_each_vcpu (i, vcpu, kvm) {if (vcpu_has_run_once (vcpu)) goto out_unlock;@@ -118,6 +122,7 @@ int kvm_vgic_create(struct kvm *kvm, u32 type)INIT_LIST_HEAD (&kvm->arch.vgic.rd_regions); out_unlock: +mutex_unlock (&kvm->arch.config_lock);unlock_all_vcpus (kvm);return ret; }@@ -227,9 +232,9 @@ int kvm_vgic_vcpu_init(struct kvm_vcpu *vcpu)* KVM io device for the redistributor that belongs to this VCPU. */ if (dist->vgic_model ==KVM_DEV_TYPE_ARM_VGIC_V3 ) { -mutex_lock (&vcpu->kvm->lock); +mutex_lock (&vcpu->kvm->arch.config_lock); ret =vgic_register_redist_iodev (vcpu); -mutex_unlock (&vcpu->kvm->lock); +mutex_unlock (&vcpu->kvm->arch.config_lock); }return ret; }@@ -250,7 +255,6 @@ static void kvm_vgic_vcpu_enable(struct kvm_vcpu *vcpu)* The function is generally called when nr_spis has been explicitly set * by the guest through the KVM DEVICE API. If not nr_spis is set to 256. * vgic_initialized() returns true when this function has succeeded. -* Must be called with kvm->lock held! */ int vgic_init (struct kvm *kvm) {@@ -259,6 +263,8 @@ int vgic_init(struct kvm *kvm)int ret =0 , i;unsigned long idx; +lockdep_assert_held (&kvm->arch.config_lock); +if (vgic_initialized (kvm))return 0 ;@@ -373,12 +379,13 @@ void kvm_vgic_vcpu_destroy(struct kvm_vcpu *vcpu) vgic_cpu->rd_iodev.base_addr =VGIC_ADDR_UNDEF ; } -/* To be called with kvm->lock held */ static void __kvm_vgic_destroy (struct kvm *kvm) {struct kvm_vcpu *vcpu;unsigned long i; +lockdep_assert_held (&kvm->arch.config_lock); +vgic_debug_destroy (kvm);kvm_for_each_vcpu (i, vcpu, kvm)@@ -389,9 +396,9 @@ static void __kvm_vgic_destroy(struct kvm *kvm)void kvm_vgic_destroy (struct kvm *kvm) { -mutex_lock (&kvm->lock); +mutex_lock (&kvm->arch.config_lock);__kvm_vgic_destroy (kvm); -mutex_unlock (&kvm->lock); +mutex_unlock (&kvm->arch.config_lock); }/** @@ -414,9 +421,9 @@ int vgic_lazy_init(struct kvm *kvm)if (kvm->arch.vgic.vgic_model !=KVM_DEV_TYPE_ARM_VGIC_V2 )return -EBUSY ; -mutex_lock (&kvm->lock); +mutex_lock (&kvm->arch.config_lock); ret =vgic_init (kvm); -mutex_unlock (&kvm->lock); +mutex_unlock (&kvm->arch.config_lock); }return ret;@@ -441,7 +448,7 @@ int kvm_vgic_map_resources(struct kvm *kvm)if (likely (vgic_ready (kvm)))return 0 ; -mutex_lock (&kvm->lock); +mutex_lock (&kvm->arch.config_lock);if (vgic_ready (kvm)) goto out;@@ -459,7 +466,7 @@ int kvm_vgic_map_resources(struct kvm *kvm) dist->ready = true; out: -mutex_unlock (&kvm->lock); +mutex_unlock (&kvm->arch.config_lock);return ret; }arch/arm64/kvm/vgic/vgic-its.c +0
-0
arch/arm64/kvm/vgic/vgic-its.c static int vgic_its_attr_regs_access(struct kvm_device *dev, · line 2049
Quoted 8 lines
arch/arm64/kvm/vgic/vgic-its.c +4
-1@@ -2043,7 +2043,10 @@ static int vgic_its_attr_regs_access(struct kvm_device *dev,if (offset & align)return -EINVAL ; -mutex_lock (&dev->kvm->lock); +if (!lock_all_vcpus (dev->kvm)) +return -EBUSY ; + +mutex_lock (&dev->kvm->arch.config_lock);
Huh, that's fishy. The whole "lock the VM and the lock the individual vcpus" is there to prevent a concurrent creation of a vcpu while we're doing stuff that affects them all. Allowing a new vcpu to come online while this sequence is happening is ... unexpected.
Why do we need to drop this initial lock? I'd expect them to be completely cumulative.
Thanks,
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Marc Zyngier
maintainerRe: [PATCH v2 1/4] KVM: arm64: Avoid vcpu->mutex v. kvm->lock inversion in CPU_ON
In reply to Oliver UptonQuoted 96 lines · Oliver Upton
KVM/arm64 had the lock ordering backwards on
vcpu->mutexandkvm->lockfrom the very beginning. One such example is the way vCPU resets are
handled: thekvm->lockis acquired while handling a guestCPU_ONPSCI call.Add a dedicated lock to serialize writes to
kvm_vcpu_arch::mp_state. Hold the lock inkvm_psci_vcpu_on()to protect against changes while the reset state is being configured. Ensure that readers readmp_stateexactly once. While at it, plug yet another race by taking themp_state_lockin theKVM_SET_MP_STATEioctl handler.As changes to MP state are now guarded with a dedicated lock, drop the
kvm->lockacquisition from the PSCICPU_ONpath. Similarly, move the reader ofreset_stateoutside of thekvm->lockand insert a barrier to ensurereset_stateis read before dropping the pending reset state.While the
kvm->lockinversion still exists inkvm_reset_vcpu(), at least now PSCICPU_ONno longer depends on it for serializing vCPU reset.Signed-off-by Oliver Upton <[email protected]>--- arch/arm64/include/asm/kvm_host.h | 1 + arch/arm64/kvm/arm.c | 23 ++++++++++++++++++----- arch/arm64/kvm/psci.c | 19 ++++++++++--------- arch/arm64/kvm/reset.c | 10 ++++++---- 4 files changed, 35 insertions(+), 18 deletions(-)arch/arm64/include/asm/kvm_host.h +1
-0@@ -522,6 +522,7 @@ struct kvm_vcpu_arch {/* vcpu power state */ struct kvm_mp_state mp_state; +spinlock_t mp_state_lock;/* Cache some mmu pages needed inside spinlock regions */ struct kvm_mmu_memory_cache mmu_page_cache;arch/arm64/kvm/arm.c +13
-4@@ -326,6 +326,8 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) {int err; +spin_lock_init (&vcpu->arch.mp_state_lock); +/* Force users to call KVM_ARM_VCPU_INIT */ vcpu->arch.target =-1 ;bitmap_zero (vcpu->arch.features,KVM_VCPU_MAX_FEATURES );@@ -443,16 +445,23 @@ void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) vcpu->cpu =-1 ; } -void kvm_arm_vcpu_power_off (struct kvm_vcpu *vcpu) +static void __kvm_arm_vcpu_power_off (struct kvm_vcpu *vcpu) { vcpu->arch.mp_state.mp_state =KVM_MP_STATE_STOPPED ;kvm_make_request (KVM_REQ_SLEEP , vcpu);kvm_vcpu_kick (vcpu); } +void kvm_arm_vcpu_power_off (struct kvm_vcpu *vcpu) +{ +spin_lock (&vcpu->arch.mp_state_lock); +__kvm_arm_vcpu_power_off (vcpu); +spin_unlock (&vcpu->arch.mp_state_lock); +} +bool kvm_arm_vcpu_stopped (struct kvm_vcpu *vcpu) { -return vcpu->arch.mp_state.mp_state ==KVM_MP_STATE_STOPPED ; +return READ_ONCE (vcpu->arch.mp_state.mp_state) ==KVM_MP_STATE_STOPPED ; }static void kvm_arm_vcpu_suspend (struct kvm_vcpu *vcpu)@@ -464,13 +473,13 @@ static void kvm_arm_vcpu_suspend(struct kvm_vcpu *vcpu)static bool kvm_arm_vcpu_suspended (struct kvm_vcpu *vcpu) { -return vcpu->arch.mp_state.mp_state ==KVM_MP_STATE_SUSPENDED ; +return READ_ONCE (vcpu->arch.mp_state.mp_state) ==KVM_MP_STATE_SUSPENDED ; }int kvm_arch_vcpu_ioctl_get_mpstate (struct kvm_vcpu *vcpu,struct kvm_mp_state *mp_state) { - *mp_state = vcpu->arch.mp_state; + *mp_state =READ_ONCE (vcpu->arch.mp_state);return 0 ; }
arch/arm64/kvm/arm.c int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, · line 496
Quoted 8 lines
arch/arm64/kvm/arm.c +2
-0@@ -480,12 +489,14 @@ int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, {int ret =0 ; +spin_lock (&vcpu->arch.mp_state_lock); +switch (mp_state->mp_state) {case KVM_MP_STATE_RUNNABLE : vcpu->arch.mp_state = *mp_state;
Given that the above helpers snapshot mp_state without holding the lock, it'd be better to at least turn this into a WRITE_ONCE().
Quoted 105 lines
arch/arm64/kvm/arm.c +3
-1break ;case KVM_MP_STATE_STOPPED : -kvm_arm_vcpu_power_off (vcpu); +__kvm_arm_vcpu_power_off (vcpu);break ;case KVM_MP_STATE_SUSPENDED :kvm_arm_vcpu_suspend (vcpu);@@ -494,6 +505,8 @@ int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, ret = -EINVAL ; } +spin_unlock (&vcpu->arch.mp_state_lock); +return ret; }arch/arm64/kvm/psci.c +10
-9@@ -62,6 +62,7 @@ static unsigned long kvm_psci_vcpu_on(struct kvm_vcpu *source_vcpu)struct vcpu_reset_state *reset_state;struct kvm *kvm = source_vcpu->kvm;struct kvm_vcpu *vcpu =NULL ; +int ret =PSCI_RET_SUCCESS ;unsigned long cpu_id; cpu_id =smccc_get_arg1 (source_vcpu);@@ -76,11 +77,15 @@ static unsigned long kvm_psci_vcpu_on(struct kvm_vcpu *source_vcpu)*/ if (!vcpu)return PSCI_RET_INVALID_PARAMS ; + +spin_lock (&vcpu->arch.mp_state_lock);if (!kvm_arm_vcpu_stopped (vcpu)) {if (kvm_psci_version (source_vcpu) !=KVM_ARM_PSCI_0_1 ) -return PSCI_RET_ALREADY_ON ; + ret =PSCI_RET_ALREADY_ON ;else -return PSCI_RET_INVALID_PARAMS ; + ret =PSCI_RET_INVALID_PARAMS ; + + goto out_unlock; } reset_state = &vcpu->arch.reset_state;@@ -108,7 +113,9 @@ static unsigned long kvm_psci_vcpu_on(struct kvm_vcpu *source_vcpu) vcpu->arch.mp_state.mp_state =KVM_MP_STATE_RUNNABLE ;kvm_vcpu_wake_up (vcpu); -return PSCI_RET_SUCCESS ; +out_unlock: +spin_unlock (&vcpu->arch.mp_state_lock); +return ret; }static unsigned long kvm_psci_vcpu_affinity_info (struct kvm_vcpu *vcpu)@@ -229,7 +236,6 @@ static unsigned long kvm_psci_check_allowed_function(struct kvm_vcpu *vcpu, u32static int kvm_psci_0_2_call (struct kvm_vcpu *vcpu) { -struct kvm *kvm = vcpu->kvm;u32 psci_fn =smccc_get_function (vcpu);unsigned long val;int ret =1 ;@@ -254,9 +260,7 @@ static int kvm_psci_0_2_call(struct kvm_vcpu *vcpu)kvm_psci_narrow_to_32bit (vcpu); fallthrough;case PSCI_0_2_FN64_CPU_ON : -mutex_lock (&kvm->lock); val =kvm_psci_vcpu_on (vcpu); -mutex_unlock (&kvm->lock);break ;case PSCI_0_2_FN_AFFINITY_INFO :kvm_psci_narrow_to_32bit (vcpu);@@ -395,7 +399,6 @@ static int kvm_psci_1_x_call(struct kvm_vcpu *vcpu, u32 minor)static int kvm_psci_0_1_call (struct kvm_vcpu *vcpu) { -struct kvm *kvm = vcpu->kvm;u32 psci_fn =smccc_get_function (vcpu);unsigned long val;@@ -405,9 +408,7 @@ static int kvm_psci_0_1_call(struct kvm_vcpu *vcpu) val =PSCI_RET_SUCCESS ;break ;case KVM_PSCI_FN_CPU_ON : -mutex_lock (&kvm->lock); val =kvm_psci_vcpu_on (vcpu); -mutex_unlock (&kvm->lock);break ;default : val =PSCI_RET_NOT_SUPPORTED ;arch/arm64/kvm/reset.c +0
-4@@ -264,15 +264,17 @@ int kvm_reset_vcpu(struct kvm_vcpu *vcpu)mutex_lock (&vcpu->kvm->lock); ret =kvm_set_vm_width (vcpu); -if (!ret) { - reset_state = vcpu->arch.reset_state; -WRITE_ONCE (vcpu->arch.reset_state.reset, false); - }
arch/arm64/kvm/reset.c int kvm_reset_vcpu(struct kvm_vcpu *vcpu) · line 276
Quoted 10 lines
arch/arm64/kvm/reset.c +5
-0mutex_unlock (&vcpu->kvm->lock);if (ret)return ret; + reset_state = vcpu->arch.reset_state; + +/* Ensure reset_state is read before clearing the pending state */ +smp_rmb (); +WRITE_ONCE (vcpu->arch.reset_state.reset, false);
What prevents a concurrent PSCI call from messing with this state? I'd have expected this to be done while holding the mp_state lock... There must be something, somewhere, but I fail to spot it right now.
Thanks,
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Marc Zyngier
maintainerRe: [PATCH v2 2/4] KVM: arm64: Avoid lock inversion when setting the VM register width
In reply to Oliver UptonQuoted 41 lines · Oliver Upton
kvm->lockmust be taken outside of thevcpu->mutex. Of course, the locking documentation for KVM makes this abundantly clear. Nonetheless, the locking order in KVM/arm64 has been wrong for quite a while; we acquire thekvm->lockwhile holding thevcpu->mutexall over the shop.All was seemingly fine until commit
42a90008f890("KVM: Ensure lockdep knows aboutkvm->lockvs.vcpu->mutexordering rule") caught us with our pants down, leading to lockdep barfing:======================================================
WARNING: possible circular locking dependency detected 6.2.0-rc7+ #19 Not tainted
------------------------------------------------------
qemu-system-aar/859 is trying to acquire lock:
ffff5aa69269eba0(&host_kvm->lock){+.+.}-{3:3}, at:kvm_reset_vcpu+0x34/0x274but task is already holding lock:
ffff5aa68768c0b8(&vcpu->mutex){+.+.}-{3:3}, at:kvm_vcpu_ioctl+0x8c/0xba0which lock already depends on the new lock.
Add a dedicated lock to serialize writes to VM-scoped configuration from the context of a vCPU. Protect the register width flags with the new lock, thus avoiding the need to grab the
kvm->lockwhile holdingvcpu->mutexinkvm_reset_vcpu().Reported-by Jeremy Linton <[email protected]>Signed-off-by Oliver Upton <[email protected]>--- arch/arm64/include/asm/kvm_host.h | 3 +++ arch/arm64/kvm/arm.c | 18 ++++++++++++++++++ arch/arm64/kvm/reset.c | 6 +++--- 3 files changed, 24 insertions(+), 3 deletions(-)arch/arm64/include/asm/kvm_host.h +0
-0
arch/arm64/include/asm/kvm_host.h struct kvm_protected_vm { · line 190
Quoted 6 lines
arch/arm64/include/asm/kvm_host.h +3
-0@@ -185,6 +185,9 @@ struct kvm_protected_vm { };struct kvm_arch { +/* Protects VM-scoped configuration data */ +struct mutex config_lock; +
nit: can we move this down into the structure and keep the MM stuff on its own at the top? Placing it next to the flags would make some sense, as these flags are definitely related to configuration matters.
Quoted 25 lines
arch/arm64/include/asm/kvm_host.h +0
-0struct kvm_s2_mmu mmu;/* VTCR_EL2 value for this VM */ arch/arm64/kvm/arm.c +10
-0@@ -128,6 +128,16 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) {int ret; +mutex_init (&kvm->arch.config_lock); + +#ifdef CONFIG_LOCKDEP +/* Clue in lockdep that the config_lock must be taken inside kvm->lock */ +mutex_lock (&kvm->lock); +mutex_lock (&kvm->arch.config_lock); +mutex_unlock (&kvm->arch.config_lock); +mutex_unlock (&kvm->lock); +#endif + ret =kvm_share_hyp (kvm, kvm +1 );if (ret)return ret;
arch/arm64/kvm/arm.c int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) · line 347
Quoted 10 lines
arch/arm64/kvm/arm.c +7
-0@@ -328,6 +338,14 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)spin_lock_init (&vcpu->arch.mp_state_lock); +#ifdef CONFIG_LOCKDEP +/* Inform lockdep that the config_lock is acquired after vcpu->mutex */ +mutex_lock (&vcpu->mutex); +mutex_lock (&vcpu->kvm->arch.config_lock); +mutex_unlock (&vcpu->kvm->arch.config_lock); +mutex_unlock (&vcpu->mutex); +#endif
Shouldn't this hunk be moved to the previous patch?
Quoted 19 lines
arch/arm64/kvm/arm.c +1
-0+/* Force users to call KVM_ARM_VCPU_INIT */ vcpu->arch.target =-1 ;bitmap_zero (vcpu->arch.features,KVM_VCPU_MAX_FEATURES );arch/arm64/kvm/reset.c +1
-1@@ -205,7 +205,7 @@ static int kvm_set_vm_width(struct kvm_vcpu *vcpu) is32bit =vcpu_has_feature (vcpu,KVM_ARM_VCPU_EL1_32BIT ); -lockdep_assert_held (&kvm->lock); +lockdep_assert_held (&kvm->arch.config_lock);if (test_bit (KVM_ARCH_FLAG_REG_WIDTH_CONFIGURED , &kvm->arch.flags)) {/* @@ -262,9 +262,9 @@ int kvm_reset_vcpu(struct kvm_vcpu *vcpu)bool loaded;
arch/arm64/kvm/reset.c int kvm_reset_vcpu(struct kvm_vcpu *vcpu) · line 270
Quoted 10 lines
arch/arm64/kvm/reset.c +2
-2u32 pstate; -mutex_lock (&vcpu->kvm->lock); +mutex_lock (&vcpu->kvm->arch.config_lock); ret =kvm_set_vm_width (vcpu); -mutex_unlock (&vcpu->kvm->lock); +mutex_unlock (&vcpu->kvm->arch.config_lock);if (ret)return ret;
Thanks,
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Re: [PATCH v2 4/4] KVM: arm64: Use config_lock to protect vgic state
In reply to Marc ZyngierQuoted 2 lines · Marc Zyngier
On Thu, 16 Mar 2023 21:14:12 +0000,
Oliver Upton <[email protected]> wrote:
[...]
Quoted 17 lines
arch/arm64/kvm/vgic/vgic-its.c static int vgic_its_attr_regs_access(struct kvm_device *dev, · line 2049
Quoted 11 lines
arch/arm64/kvm/vgic/vgic-its.c +4
-1@@ -2043,7 +2043,10 @@ static int vgic_its_attr_regs_access(struct kvm_device *dev,if (offset & align)return -EINVAL ; -mutex_lock (&dev->kvm->lock); +if (!lock_all_vcpus (dev->kvm)) +return -EBUSY ; + +mutex_lock (&dev->kvm->arch.config_lock);Huh, that's fishy. The whole "lock the VM and the lock the individual vcpus" is there to prevent a concurrent creation of a vcpu while we're doing stuff that affects them all. Allowing a new vcpu to come online while this sequence is happening is ... unexpected.
Quoted 2 lines
Why do we need to drop this initial lock? I'd expect them to be completely cumulative.
Urgh.. Yes, you're right. I'll go with kvm->lock -> lock_all_vcpus() -> kvm->config_lock in the next spin to guard against the vCPU creation race.
Signature · 2 lines
Thanks, Oliver
Re: [PATCH v2 2/4] KVM: arm64: Avoid lock inversion when setting the VM register width
In reply to Marc ZyngierQuoted 45 lines · Marc Zyngier
Quoted 43 lines · Oliver Upton
kvm->lockmust be taken outside of thevcpu->mutex. Of course, the locking documentation for KVM makes this abundantly clear. Nonetheless, the locking order in KVM/arm64 has been wrong for quite a while; we acquire thekvm->lockwhile holding thevcpu->mutexall over the shop.All was seemingly fine until commit
42a90008f890("KVM: Ensure lockdep knows aboutkvm->lockvs.vcpu->mutexordering rule") caught us with our pants down, leading to lockdep barfing:======================================================
WARNING: possible circular locking dependency detected 6.2.0-rc7+ #19 Not tainted
------------------------------------------------------
qemu-system-aar/859 is trying to acquire lock:
ffff5aa69269eba0(&host_kvm->lock){+.+.}-{3:3}, at:kvm_reset_vcpu+0x34/0x274but task is already holding lock:
ffff5aa68768c0b8(&vcpu->mutex){+.+.}-{3:3}, at:kvm_vcpu_ioctl+0x8c/0xba0which lock already depends on the new lock.
Add a dedicated lock to serialize writes to VM-scoped configuration from the context of a vCPU. Protect the register width flags with the new lock, thus avoiding the need to grab the
kvm->lockwhile holdingvcpu->mutexinkvm_reset_vcpu().Reported-by Jeremy Linton <[email protected]>Signed-off-by Oliver Upton <[email protected]>--- arch/arm64/include/asm/kvm_host.h | 3 +++ arch/arm64/kvm/arm.c | 18 ++++++++++++++++++ arch/arm64/kvm/reset.c | 6 +++--- 3 files changed, 24 insertions(+), 3 deletions(-)arch/arm64/include/asm/kvm_host.h +0
-0@@ -185,6 +185,9 @@ struct kvm_protected_vm { };
Quoted 8 lines
arch/arm64/include/asm/kvm_host.h struct kvm_protected_vm { · line 190
Quoted 4 lines
arch/arm64/include/asm/kvm_host.h +3
-0struct kvm_arch { +/* Protects VM-scoped configuration data */ +struct mutex config_lock; +nit: can we move this down into the structure and keep the MM stuff on its own at the top? Placing it next to the flags would make some sense, as these flags are definitely related to configuration matters.
Sure thing!
Quoted 28 lines
Quoted 28 lines
arch/arm64/include/asm/kvm_host.h +0
-0struct kvm_s2_mmu mmu;/* VTCR_EL2 value for this VM */ arch/arm64/kvm/arm.c +10
-0@@ -128,6 +128,16 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) {int ret; +mutex_init (&kvm->arch.config_lock); + +#ifdef CONFIG_LOCKDEP +/* Clue in lockdep that the config_lock must be taken inside kvm->lock */ +mutex_lock (&kvm->lock); +mutex_lock (&kvm->arch.config_lock); +mutex_unlock (&kvm->arch.config_lock); +mutex_unlock (&kvm->lock); +#endif + ret =kvm_share_hyp (kvm, kvm +1 );if (ret)return ret;@@ -328,6 +338,14 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)spin_lock_init (&vcpu->arch.mp_state_lock);
Quoted 9 lines
arch/arm64/kvm/arm.c int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) · line 347
Quoted 7 lines
arch/arm64/kvm/arm.c +7
-0+#ifdef CONFIG_LOCKDEP +/* Inform lockdep that the config_lock is acquired after vcpu->mutex */ +mutex_lock (&vcpu->mutex); +mutex_lock (&vcpu->kvm->arch.config_lock); +mutex_unlock (&vcpu->kvm->arch.config_lock); +mutex_unlock (&vcpu->mutex); +#endif Shouldn't this hunk be moved to the previous patch?
Uh, I don't believe so since this is the patch that actually introduces kvm_arch::config_lock. The last patch was aimed at a separate lock for mp state.
Signature · 2 lines
Thanks, Oliver
Marc Zyngier
maintainerRe: [PATCH v2 2/4] KVM: arm64: Avoid lock inversion when setting the VM register width
Quoted 95 lines · Oliver Upton
Quoted 45 lines · Marc Zyngier
Quoted 43 lines · Oliver Upton
kvm->lockmust be taken outside of thevcpu->mutex. Of course, the locking documentation for KVM makes this abundantly clear. Nonetheless, the locking order in KVM/arm64 has been wrong for quite a while; we acquire thekvm->lockwhile holding thevcpu->mutexall over the shop.All was seemingly fine until commit
42a90008f890("KVM: Ensure lockdep knows aboutkvm->lockvs.vcpu->mutexordering rule") caught us with our pants down, leading to lockdep barfing:======================================================
WARNING: possible circular locking dependency detected 6.2.0-rc7+ #19 Not tainted
------------------------------------------------------
qemu-system-aar/859 is trying to acquire lock:
ffff5aa69269eba0(&host_kvm->lock){+.+.}-{3:3}, at:kvm_reset_vcpu+0x34/0x274but task is already holding lock:
ffff5aa68768c0b8(&vcpu->mutex){+.+.}-{3:3}, at:kvm_vcpu_ioctl+0x8c/0xba0which lock already depends on the new lock.
Add a dedicated lock to serialize writes to VM-scoped configuration from the context of a vCPU. Protect the register width flags with the new lock, thus avoiding the need to grab the
kvm->lockwhile holdingvcpu->mutexinkvm_reset_vcpu().Reported-by Jeremy Linton <[email protected]>Signed-off-by Oliver Upton <[email protected]>--- arch/arm64/include/asm/kvm_host.h | 3 +++ arch/arm64/kvm/arm.c | 18 ++++++++++++++++++ arch/arm64/kvm/reset.c | 6 +++--- 3 files changed, 24 insertions(+), 3 deletions(-)arch/arm64/include/asm/kvm_host.h +0
-0@@ -185,6 +185,9 @@ struct kvm_protected_vm { };Quoted 8 lines
arch/arm64/include/asm/kvm_host.h struct kvm_protected_vm { · line 190
Quoted 4 lines
arch/arm64/include/asm/kvm_host.h +3
-0struct kvm_arch { +/* Protects VM-scoped configuration data */ +struct mutex config_lock; +nit: can we move this down into the structure and keep the MM stuff on its own at the top? Placing it next to the flags would make some sense, as these flags are definitely related to configuration matters.
Sure thing!
Quoted 28 lines
Quoted 28 lines
arch/arm64/include/asm/kvm_host.h +0
-0struct kvm_s2_mmu mmu;/* VTCR_EL2 value for this VM */ arch/arm64/kvm/arm.c +10
-0@@ -128,6 +128,16 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) {int ret; +mutex_init (&kvm->arch.config_lock); + +#ifdef CONFIG_LOCKDEP +/* Clue in lockdep that the config_lock must be taken inside kvm->lock */ +mutex_lock (&kvm->lock); +mutex_lock (&kvm->arch.config_lock); +mutex_unlock (&kvm->arch.config_lock); +mutex_unlock (&kvm->lock); +#endif + ret =kvm_share_hyp (kvm, kvm +1 );if (ret)return ret;@@ -328,6 +338,14 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)spin_lock_init (&vcpu->arch.mp_state_lock);Quoted 9 lines
arch/arm64/kvm/arm.c int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) · line 347
Quoted 7 lines
arch/arm64/kvm/arm.c +7
-0+#ifdef CONFIG_LOCKDEP +/* Inform lockdep that the config_lock is acquired after vcpu->mutex */ +mutex_lock (&vcpu->mutex); +mutex_lock (&vcpu->kvm->arch.config_lock); +mutex_unlock (&vcpu->kvm->arch.config_lock); +mutex_unlock (&vcpu->mutex); +#endif Shouldn't this hunk be moved to the previous patch?
Quoted 3 lines
Uh, I don't believe so since this is the patch that actually introduces
kvm_arch::config_lock. The last patch was aimed at a separate lock for mp state.
Nah, you're obviously right. I reviewed this at 4am being jet-lagged. Not the brightest comment... :-/ Sorry for the noise.
M.
Signature · 1 line
Jazz is not dead. It just smells funny...
Re: [PATCH v2 1/4] KVM: arm64: Avoid vcpu->mutex v. kvm->lock inversion in CPU_ON
In reply to Marc ZyngierQuoted 101 lines · Marc Zyngier
Quoted 99 lines · Oliver Upton
KVM/arm64 had the lock ordering backwards on
vcpu->mutexandkvm->lockfrom the very beginning. One such example is the way vCPU resets are
handled: thekvm->lockis acquired while handling a guestCPU_ONPSCI call.Add a dedicated lock to serialize writes to
kvm_vcpu_arch::mp_state. Hold the lock inkvm_psci_vcpu_on()to protect against changes while the reset state is being configured. Ensure that readers readmp_stateexactly once. While at it, plug yet another race by taking themp_state_lockin theKVM_SET_MP_STATEioctl handler.As changes to MP state are now guarded with a dedicated lock, drop the
kvm->lockacquisition from the PSCICPU_ONpath. Similarly, move the reader ofreset_stateoutside of thekvm->lockand insert a barrier to ensurereset_stateis read before dropping the pending reset state.While the
kvm->lockinversion still exists inkvm_reset_vcpu(), at least now PSCICPU_ONno longer depends on it for serializing vCPU reset.Signed-off-by Oliver Upton <[email protected]>--- arch/arm64/include/asm/kvm_host.h | 1 + arch/arm64/kvm/arm.c | 23 ++++++++++++++++++----- arch/arm64/kvm/psci.c | 19 ++++++++++--------- arch/arm64/kvm/reset.c | 10 ++++++---- 4 files changed, 35 insertions(+), 18 deletions(-)arch/arm64/include/asm/kvm_host.h +1
-0@@ -522,6 +522,7 @@ struct kvm_vcpu_arch {/* vcpu power state */ struct kvm_mp_state mp_state; +spinlock_t mp_state_lock;/* Cache some mmu pages needed inside spinlock regions */ struct kvm_mmu_memory_cache mmu_page_cache;arch/arm64/kvm/arm.c +13
-4@@ -326,6 +326,8 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) {int err; +spin_lock_init (&vcpu->arch.mp_state_lock); +/* Force users to call KVM_ARM_VCPU_INIT */ vcpu->arch.target =-1 ;bitmap_zero (vcpu->arch.features,KVM_VCPU_MAX_FEATURES );@@ -443,16 +445,23 @@ void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) vcpu->cpu =-1 ; } -void kvm_arm_vcpu_power_off (struct kvm_vcpu *vcpu) +static void __kvm_arm_vcpu_power_off (struct kvm_vcpu *vcpu) { vcpu->arch.mp_state.mp_state =KVM_MP_STATE_STOPPED ;kvm_make_request (KVM_REQ_SLEEP , vcpu);kvm_vcpu_kick (vcpu); } +void kvm_arm_vcpu_power_off (struct kvm_vcpu *vcpu) +{ +spin_lock (&vcpu->arch.mp_state_lock); +__kvm_arm_vcpu_power_off (vcpu); +spin_unlock (&vcpu->arch.mp_state_lock); +} +bool kvm_arm_vcpu_stopped (struct kvm_vcpu *vcpu) { -return vcpu->arch.mp_state.mp_state ==KVM_MP_STATE_STOPPED ; +return READ_ONCE (vcpu->arch.mp_state.mp_state) ==KVM_MP_STATE_STOPPED ; }static void kvm_arm_vcpu_suspend (struct kvm_vcpu *vcpu)@@ -464,13 +473,13 @@ static void kvm_arm_vcpu_suspend(struct kvm_vcpu *vcpu)static bool kvm_arm_vcpu_suspended (struct kvm_vcpu *vcpu) { -return vcpu->arch.mp_state.mp_state ==KVM_MP_STATE_SUSPENDED ; +return READ_ONCE (vcpu->arch.mp_state.mp_state) ==KVM_MP_STATE_SUSPENDED ; }int kvm_arch_vcpu_ioctl_get_mpstate (struct kvm_vcpu *vcpu,struct kvm_mp_state *mp_state) { - *mp_state = vcpu->arch.mp_state; + *mp_state =READ_ONCE (vcpu->arch.mp_state);return 0 ; }@@ -480,12 +489,14 @@ int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, {int ret =0 ;
Quoted 8 lines
arch/arm64/kvm/arm.c int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, · line 496
Quoted 5 lines
arch/arm64/kvm/arm.c +2
-0+spin_lock (&vcpu->arch.mp_state_lock); +switch (mp_state->mp_state) {case KVM_MP_STATE_RUNNABLE : vcpu->arch.mp_state = *mp_state;Given that the above helpers snapshot
mp_statewithout holding the lock, it'd be better to at least turn this into aWRITE_ONCE().
Yeah, definitely need that.
Quoted 110 lines
Quoted 110 lines
arch/arm64/kvm/arm.c +3
-1break ;case KVM_MP_STATE_STOPPED : -kvm_arm_vcpu_power_off (vcpu); +__kvm_arm_vcpu_power_off (vcpu);break ;case KVM_MP_STATE_SUSPENDED :kvm_arm_vcpu_suspend (vcpu);@@ -494,6 +505,8 @@ int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, ret = -EINVAL ; } +spin_unlock (&vcpu->arch.mp_state_lock); +return ret; }arch/arm64/kvm/psci.c +10
-9@@ -62,6 +62,7 @@ static unsigned long kvm_psci_vcpu_on(struct kvm_vcpu *source_vcpu)struct vcpu_reset_state *reset_state;struct kvm *kvm = source_vcpu->kvm;struct kvm_vcpu *vcpu =NULL ; +int ret =PSCI_RET_SUCCESS ;unsigned long cpu_id; cpu_id =smccc_get_arg1 (source_vcpu);@@ -76,11 +77,15 @@ static unsigned long kvm_psci_vcpu_on(struct kvm_vcpu *source_vcpu)*/ if (!vcpu)return PSCI_RET_INVALID_PARAMS ; + +spin_lock (&vcpu->arch.mp_state_lock);if (!kvm_arm_vcpu_stopped (vcpu)) {if (kvm_psci_version (source_vcpu) !=KVM_ARM_PSCI_0_1 ) -return PSCI_RET_ALREADY_ON ; + ret =PSCI_RET_ALREADY_ON ;else -return PSCI_RET_INVALID_PARAMS ; + ret =PSCI_RET_INVALID_PARAMS ; + + goto out_unlock; } reset_state = &vcpu->arch.reset_state;@@ -108,7 +113,9 @@ static unsigned long kvm_psci_vcpu_on(struct kvm_vcpu *source_vcpu) vcpu->arch.mp_state.mp_state =KVM_MP_STATE_RUNNABLE ;kvm_vcpu_wake_up (vcpu); -return PSCI_RET_SUCCESS ; +out_unlock: +spin_unlock (&vcpu->arch.mp_state_lock); +return ret; }static unsigned long kvm_psci_vcpu_affinity_info (struct kvm_vcpu *vcpu)@@ -229,7 +236,6 @@ static unsigned long kvm_psci_check_allowed_function(struct kvm_vcpu *vcpu, u32static int kvm_psci_0_2_call (struct kvm_vcpu *vcpu) { -struct kvm *kvm = vcpu->kvm;u32 psci_fn =smccc_get_function (vcpu);unsigned long val;int ret =1 ;@@ -254,9 +260,7 @@ static int kvm_psci_0_2_call(struct kvm_vcpu *vcpu)kvm_psci_narrow_to_32bit (vcpu); fallthrough;case PSCI_0_2_FN64_CPU_ON : -mutex_lock (&kvm->lock); val =kvm_psci_vcpu_on (vcpu); -mutex_unlock (&kvm->lock);break ;case PSCI_0_2_FN_AFFINITY_INFO :kvm_psci_narrow_to_32bit (vcpu);@@ -395,7 +399,6 @@ static int kvm_psci_1_x_call(struct kvm_vcpu *vcpu, u32 minor)static int kvm_psci_0_1_call (struct kvm_vcpu *vcpu) { -struct kvm *kvm = vcpu->kvm;u32 psci_fn =smccc_get_function (vcpu);unsigned long val;@@ -405,9 +408,7 @@ static int kvm_psci_0_1_call(struct kvm_vcpu *vcpu) val =PSCI_RET_SUCCESS ;break ;case KVM_PSCI_FN_CPU_ON : -mutex_lock (&kvm->lock); val =kvm_psci_vcpu_on (vcpu); -mutex_unlock (&kvm->lock);break ;default : val =PSCI_RET_NOT_SUPPORTED ;arch/arm64/kvm/reset.c +0
-4@@ -264,15 +264,17 @@ int kvm_reset_vcpu(struct kvm_vcpu *vcpu)mutex_lock (&vcpu->kvm->lock); ret =kvm_set_vm_width (vcpu); -if (!ret) { - reset_state = vcpu->arch.reset_state; -WRITE_ONCE (vcpu->arch.reset_state.reset, false); - }mutex_unlock (&vcpu->kvm->lock);if (ret)return ret;
Quoted 9 lines
arch/arm64/kvm/reset.c int kvm_reset_vcpu(struct kvm_vcpu *vcpu) · line 276
Quoted 5 lines
arch/arm64/kvm/reset.c +5
-0+ reset_state = vcpu->arch.reset_state; + +/* Ensure reset_state is read before clearing the pending state */ +smp_rmb (); +WRITE_ONCE (vcpu->arch.reset_state.reset, false);What prevents a concurrent PSCI call from messing with this state? I'd have expected this to be done while holding the
mp_statelock... There must be something, somewhere, but I fail to spot it right now.
It is a bit shaky, but my expectation was that the vCPU couldn't be transitioned from RUNNABLE -> STOPPED w/o holding the vcpu->mutex, thus any PSCI CPU_ON calls would fail as the vCPU is marked as RUNNABLE until the reset completes.
However, looking at this with fresh eyes, the kvm_prepare_system_event() flow breaks my expectation and does an unguarded transition to STOPPED state.
So, in the interest of making the whole dance correct I'll take the mp_state_lock here and in kvm_prepare_system_event(). Thanks for spotting this.
--
Thanks,
Oliver
Re: [PATCH v2 2/4] KVM: arm64: Avoid lock inversion when setting the VM register width
In reply to Marc ZyngierQuoted 3 lines · Marc Zyngier
Quoted 2 lines · Oliver Upton
Quoted 1 line · Marc Zyngier
On Thu, 16 Mar 2023 21:14:10 +0000, Oliver Upton <[email protected]> wrote:
[...]
Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
arch/arm64/kvm/arm.c int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) · line 347
Quoted 7 lines
arch/arm64/kvm/arm.c +7
-0+#ifdef CONFIG_LOCKDEP +/* Inform lockdep that the config_lock is acquired after vcpu->mutex */ +mutex_lock (&vcpu->mutex); +mutex_lock (&vcpu->kvm->arch.config_lock); +mutex_unlock (&vcpu->kvm->arch.config_lock); +mutex_unlock (&vcpu->mutex); +#endif Shouldn't this hunk be moved to the previous patch?
Quoted 6 lines
Quoted 3 lines
Uh, I don't believe so since this is the patch that actually introduces
kvm_arch::config_lock. The last patch was aimed at a separate lock for mp state.Nah, you're obviously right. I reviewed this at 4am being jet-lagged. Not the brightest comment... :-/ Sorry for the noise.
No problem at all! I wish I had as good of an excuse as you for the times I gave far worse feedback...
Signature · 2 lines
Thanks, Oliver
Re: [PATCH v2 2/4] KVM: arm64: Avoid lock inversion when setting the VM register width
In reply to Oliver UptonHi,
Quoted 8 lines · Oliver Upton
kvm->lockmust be taken outside of thevcpu->mutex. Of course, the locking documentation for KVM makes this abundantly clear. Nonetheless, the locking order in KVM/arm64 has been wrong for quite a while; we acquire thekvm->lockwhile holding thevcpu->mutexall over the shop.All was seemingly fine until commit
42a90008f890("KVM: Ensure lockdep knows aboutkvm->lockvs.vcpu->mutexordering rule") caught us with our pants down, leading to lockdep barfing:
Thanks for looking at this! It had a bit of fuzz applying to -rc3, did I miss a required patch?
This patch makes the lockdep warnings I was seeing go away but I'm seeing similar lockdep problem while running the kvm kselftests. In particular I think it was "selftests: kvm: debug-exceptions" which threw the warning.
So, i'm not sure its completely fixed. I ran the previous one a couple weeks back before you respun it, and IIRC didn't see any errors.
Quoted 102 lines
======================================================
WARNING: possible circular locking dependency detected 6.2.0-rc7+ #19 Not tainted
------------------------------------------------------
qemu-system-aar/859 is trying to acquire lock:
ffff5aa69269eba0(&host_kvm->lock){+.+.}-{3:3}, at:kvm_reset_vcpu+0x34/0x274but task is already holding lock:
ffff5aa68768c0b8(&vcpu->mutex){+.+.}-{3:3}, at:kvm_vcpu_ioctl+0x8c/0xba0which lock already depends on the new lock.
Add a dedicated lock to serialize writes to VM-scoped configuration from the context of a vCPU. Protect the register width flags with the new lock, thus avoiding the need to grab the
kvm->lockwhile holdingvcpu->mutexinkvm_reset_vcpu().Reported-by Jeremy Linton <[email protected]>Signed-off-by Oliver Upton <[email protected]>--- arch/arm64/include/asm/kvm_host.h | 3 +++ arch/arm64/kvm/arm.c | 18 ++++++++++++++++++ arch/arm64/kvm/reset.c | 6 +++--- 3 files changed, 24 insertions(+), 3 deletions(-)arch/arm64/include/asm/kvm_host.h +3
-0@@ -185,6 +185,9 @@ struct kvm_protected_vm { };struct kvm_arch { +/* Protects VM-scoped configuration data */ +struct mutex config_lock; +struct kvm_s2_mmu mmu;/* VTCR_EL2 value for this VM */ arch/arm64/kvm/arm.c +18
-0@@ -128,6 +128,16 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) {int ret; +mutex_init (&kvm->arch.config_lock); + +#ifdef CONFIG_LOCKDEP +/* Clue in lockdep that the config_lock must be taken inside kvm->lock */ +mutex_lock (&kvm->lock); +mutex_lock (&kvm->arch.config_lock); +mutex_unlock (&kvm->arch.config_lock); +mutex_unlock (&kvm->lock); +#endif + ret =kvm_share_hyp (kvm, kvm +1 );if (ret)return ret;@@ -328,6 +338,14 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu)spin_lock_init (&vcpu->arch.mp_state_lock); +#ifdef CONFIG_LOCKDEP +/* Inform lockdep that the config_lock is acquired after vcpu->mutex */ +mutex_lock (&vcpu->mutex); +mutex_lock (&vcpu->kvm->arch.config_lock); +mutex_unlock (&vcpu->kvm->arch.config_lock); +mutex_unlock (&vcpu->mutex); +#endif +/* Force users to call KVM_ARM_VCPU_INIT */ vcpu->arch.target =-1 ;bitmap_zero (vcpu->arch.features,KVM_VCPU_MAX_FEATURES );arch/arm64/kvm/reset.c +3
-3@@ -205,7 +205,7 @@ static int kvm_set_vm_width(struct kvm_vcpu *vcpu) is32bit =vcpu_has_feature (vcpu,KVM_ARM_VCPU_EL1_32BIT ); -lockdep_assert_held (&kvm->lock); +lockdep_assert_held (&kvm->arch.config_lock);if (test_bit (KVM_ARCH_FLAG_REG_WIDTH_CONFIGURED , &kvm->arch.flags)) {/* @@ -262,9 +262,9 @@ int kvm_reset_vcpu(struct kvm_vcpu *vcpu)bool loaded;u32 pstate; -mutex_lock (&vcpu->kvm->lock); +mutex_lock (&vcpu->kvm->arch.config_lock); ret =kvm_set_vm_width (vcpu); -mutex_unlock (&vcpu->kvm->lock); +mutex_unlock (&vcpu->kvm->arch.config_lock);if (ret)return ret;
Re: [PATCH v2 2/4] KVM: arm64: Avoid lock inversion when setting the VM register width
Jeremy,
Quoted 12 lines · Jeremy Linton
Hi,
Quoted 8 lines · Oliver Upton
kvm->lockmust be taken outside of thevcpu->mutex. Of course, the locking documentation for KVM makes this abundantly clear. Nonetheless, the locking order in KVM/arm64 has been wrong for quite a while; we acquire thekvm->lockwhile holding thevcpu->mutexall over the shop.All was seemingly fine until commit
42a90008f890("KVM: Ensure lockdep knows aboutkvm->lockvs.vcpu->mutexordering rule") caught us with our pants down, leading to lockdep barfing:
Quoted 6 lines
Thanks for looking at this! It had a bit of fuzz applying to -rc3, did I miss a required patch?
This patch makes the lockdep warnings I was seeing go away but I'm seeing similar lockdep problem while running the kvm kselftests. In particular I think it was "selftests: kvm: debug-exceptions" which threw the warning.
Hmm, that's odd. IIRC the only test that exploded for me w/o the commit below was arch_timer.
Quoted 2 lines
So, i'm not sure its completely fixed. I ran the previous one a couple weeks back before you respun it, and IIRC didn't see any errors.
Thanks for taking this for a spin! This series depends on commit 47053904e182 ("KVM: arm64: timers: Convert per-vcpu virtual offset to a global value") which is only present in kvmarm/fixes at the moment. I had sent out a PR for this last week but Paolo has yet to pull.
With both Marc's patch and this series I'm unable to reproduce lockdep warnings w/ selftests or kvmtool. If it isn't too much trouble can you give kvmarm/fixes plus this series a whirl and see if everything is addressed?
Otherwise you may have stumbled into some more crud we'll need to address :)
Signature · 2 lines
Thanks, Oliver
Re: [PATCH v2 2/4] KVM: arm64: Avoid lock inversion when setting the VM register width
Hi,
Quoted 33 lines · Oliver Upton
Jeremy,
Quoted 12 lines · Jeremy Linton
Hi,
Quoted 8 lines · Oliver Upton
kvm->lockmust be taken outside of thevcpu->mutex. Of course, the locking documentation for KVM makes this abundantly clear. Nonetheless, the locking order in KVM/arm64 has been wrong for quite a while; we acquire thekvm->lockwhile holding thevcpu->mutexall over the shop.All was seemingly fine until commit
42a90008f890("KVM: Ensure lockdep knows aboutkvm->lockvs.vcpu->mutexordering rule") caught us with our pants down, leading to lockdep barfing:Quoted 6 lines
Thanks for looking at this! It had a bit of fuzz applying to -rc3, did I miss a required patch?
This patch makes the lockdep warnings I was seeing go away but I'm seeing similar lockdep problem while running the kvm kselftests. In particular I think it was "selftests: kvm: debug-exceptions" which threw the warning.
Hmm, that's odd. IIRC the only test that exploded for me w/o the commit below was
arch_timer.Quoted 2 lines
So, i'm not sure its completely fixed. I ran the previous one a couple weeks back before you respun it, and IIRC didn't see any errors.
Thanks for taking this for a spin! This series depends on commit
47053904e182("KVM: arm64: timers: Convert per-vcpu virtual offset to a global value") which is only present in kvmarm/fixes at the moment. I had sent out a PR for this last week but Paolo has yet to pull.
Quoted 7 lines
With both Marc's patch and this series I'm unable to reproduce lockdep warnings w/ selftests or kvmtool. If it isn't too much trouble can you give kvmarm/fixes plus this series a whirl and see if everything is addressed?
Otherwise you may have stumbled into some more crud we'll need to address :)
Ok, I reran everything, including with the latest arm/kvm patches and it seems I created the problem. Which I realized almost immediately after sending the original comment, as is too frequently the case.
I will put the tested by on the main set, although it should be noted the selftest doesn't complete cleanly on armv8.0 machines because of the:
GUEST_ASSERT(guest_check_lse()) line in guest_cas().
So, this error was largely noise.
Thanks,
Hi,
Quoted 11 lines · Oliver Upton
As it so happens, lock ordering in KVM/arm64 is completely backwards. There's a significant amount of VM-wide state that needs to be accessed from the context of a vCPU. Until now, this was accomplished by acquiring the
kvm->lock, but that cannot be nested withinvcpu->mutex.This series fixes the issue with some fine-grained locking for MP state and a new, dedicated mutex that can nest with both
kvm->lockandvcpu->mutex.Tested with kvmtool and QEMU scaled up to 64 vCPUs on a kernel w/ lockdep enabled. Applies to kvmarm/next.
This set makes the reported lockdep error go away for me, it also appears to complete some basic kernel/kvm testing without error as well. So,
Thanks,
Quoted 33 lines
v1: http://lore.kernel.org/kvmarm/[email protected]
v1 -> v2:
- Add a dedicated lock for serializing writes to MP state
- Inform lockdep of acquisition order at time of VM/vCPU creation
- Plug a race with GIC creation (Sean)
- Use theconfig_lockin GIC ITS flows as well. There is now a single(valid) use of kvm->lock when enabling MTE.Oliver Upton (4):
KVM: arm64: Avoidvcpu->mutexv.kvm->lockinversion inCPU_ON
KVM: arm64: Avoid lock inversion when setting the VM register width
KVM: arm64: Useconfig_lockto protect data ordered againstKVM_RUN
KVM: arm64: Useconfig_lockto protect vgic statearch/arm64/include/asm/kvm_host.h | 4 ++ arch/arm64/kvm/arm.c | 45 +++++++++++++++++++---- arch/arm64/kvm/guest.c | 2 + arch/arm64/kvm/hypercalls.c | 4 +- arch/arm64/kvm/pmu-emul.c | 23 +++--------- arch/arm64/kvm/psci.c | 19 +++++----- arch/arm64/kvm/reset.c | 16 ++++---- arch/arm64/kvm/vgic/vgic-debug.c | 8 ++-- arch/arm64/kvm/vgic/vgic-init.c | 33 ++++++++++------- arch/arm64/kvm/vgic/vgic-its.c | 29 ++++++--------- arch/arm64/kvm/vgic/vgic-kvm-device.c | 53 ++++++++++++--------------- arch/arm64/kvm/vgic/vgic-mmio-v3.c | 4 +- arch/arm64/kvm/vgic/vgic-mmio.c | 12 +++--- arch/arm64/kvm/vgic/vgic-v4.c | 11 +++--- arch/arm64/kvm/vgic/vgic.c | 2 +- 15 files changed, 146 insertions(+), 119 deletions(-)