On Tue, Apr 05, 2022, Peter Gonda wrote: > diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c > index 75fa6dd268f0..673e1ee2cfc9 100644 > --- a/arch/x86/kvm/svm/sev.c > +++ b/arch/x86/kvm/svm/sev.c > @@ -1591,14 +1591,21 @@ static void sev_unlock_two_vms(struct kvm *dst_kvm, struct kvm *src_kvm) > atomic_set_release(&src_sev->migration_in_progress, 0); > } > > +#define SEV_MIGRATION_SOURCE 0 > +#define SEV_MIGRATION_TARGET 1 > > -static int sev_lock_vcpus_for_migration(struct kvm *kvm) > +/* > + * To avoid lockdep warnings callers should pass @vm argument with either I think it's important to call that these are false positives, saying "avoid lockdep warnings" suggests we're intentionally not fixing bugs :-) > + * SEV_MIGRATION_SOURCE or SEV_MIGRATE_TARGET. This allows subclassing of all > + * vCPU mutex locks. > + */ If we use an enum, that'll make the param self-documenting. And we can also use that to eliminate the remaining magic number '2'. E.g. this as fixup. --- arch/x86/kvm/svm/sev.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 673e1ee2cfc9..1e07d5d3f85a 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -1591,21 +1591,27 @@ static void sev_unlock_two_vms(struct kvm *dst_kvm, struct kvm *src_kvm) atomic_set_release(&src_sev->migration_in_progress, 0); } -#define SEV_MIGRATION_SOURCE 0 -#define SEV_MIGRATION_TARGET 1 /* - * To avoid lockdep warnings callers should pass @vm argument with either - * SEV_MIGRATION_SOURCE or SEV_MIGRATE_TARGET. This allows subclassing of all - * vCPU mutex locks. + * To suppress lockdep false positives, subclass all vCPU mutex locks by + * assigning even numbers to the source vCPUs and odd numbers to destination + * vCPUs based on the vCPU's index. */ -static int sev_lock_vcpus_for_migration(struct kvm *kvm, int vm) +enum sev_migration_role { + SEV_MIGRATION_SOURCE = 0, + SEV_MIGRATION_TARGET, + SEV_NR_MIGRATION_ROLES, +}; + +static int sev_lock_vcpus_for_migration(struct kvm *kvm, + enum sev_migration_role role) { struct kvm_vcpu *vcpu; unsigned long i, j; kvm_for_each_vcpu(i, vcpu, kvm) { - if (mutex_lock_killable_nested(&vcpu->mutex, i * 2 + vm)) + if (mutex_lock_killable_nested(&vcpu->mutex, + i * SEV_NR_MIGRATION_ROLES + role)) goto out_unlock; } base-commit: 6600ddafa53b35fd5c869aff4a5efb981ed06955 --