Skip to contentSkip to conversation

x86: KVM, fix lock imbalance

PATCH In review Jiri Slaby 3 participants 7 messages Jul 7, 2010

Subsystems (3)
lore
Share & tools
lore

Jiri Slaby

author
#0 lore raw

PATCH 1/1 x86: KVM, fix lock imbalance

Stanse found that there is an omitted unlock in kvm_create_pit in one fail path. Add proper unlock there.

Cc Avi Kivity <[email protected]>
Cc Marcelo Tosatti <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Gleb Natapov <[email protected]>
Cc "Michael S. Tsirkin" <[email protected]>
Cc Gregory Haskins <[email protected]>
---
 arch/x86/kvm/i8254.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
arch/x86/kvm/i8254.c +1 -0
index 467cc47..70db4d4 100644
@@ -696,6 +696,7 @@ struct kvm_pit *kvm_create_pit(struct kvm *kvm, u32 flags)
 
 	pit->wq = create_singlethread_workqueue("kvm-pit-wq");
 	if (!pit->wq) {
+		mutex_unlock(&pit->pit_state.lock);
 		kfree(pit);
 		return NULL;
 	}
Signature · 1 line
1.7.1
#1 lore raw
Quoted 23 lines · Jiri Slaby

Stanse found that there is an omitted unlock in kvm_create_pit in one fail path. Add proper unlock there.

Cc Avi Kivity <[email protected]>
Cc Marcelo Tosatti <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Gleb Natapov <[email protected]>
Cc "Michael S. Tsirkin" <[email protected]>
Cc Gregory Haskins <[email protected]>
---
 arch/x86/kvm/i8254.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
arch/x86/kvm/i8254.c +0 -0
index 467cc47..70db4d4 100644

arch/x86/kvm/i8254.c struct kvm_pit *kvm_create_pit(struct kvm *kvm, u32 flags) · line 702

Quoted 7 lines
arch/x86/kvm/i8254.c +1 -0
@@ -696,6 +696,7 @@ struct kvm_pit *kvm_create_pit(struct kvm *kvm, u32 flags)
 
 	pit->wq = create_singlethread_workqueue("kvm-pit-wq");
 	if (!pit->wq) {
+		mutex_unlock(&pit->pit_state.lock);
 		kfree(pit);
 		return NULL;
 	}

A cleanliness comment: why is that tear-down/dealloc sequence open-coded? It should be at the end of the function, with goto labels, like we do it in similar cases.

Thanks,

Ingo

author
#2 lore raw
Quoted 27 lines · Ingo Molnar
Quoted 24 lines · Jiri Slaby

Stanse found that there is an omitted unlock in kvm_create_pit in one fail path. Add proper unlock there.

Cc Avi Kivity <[email protected]>
Cc Marcelo Tosatti <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Gleb Natapov <[email protected]>
Cc "Michael S. Tsirkin" <[email protected]>
Cc Gregory Haskins <[email protected]>
---
 arch/x86/kvm/i8254.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
arch/x86/kvm/i8254.c +0 -0
index 467cc47..70db4d4 100644
@@ -696,6 +696,7 @@ struct kvm_pit *kvm_create_pit(struct kvm *kvm, u32 flags)
 
Quoted 10 lines

arch/x86/kvm/i8254.c struct kvm_pit *kvm_create_pit(struct kvm *kvm, u32 flags) · line 702

Quoted 6 lines
arch/x86/kvm/i8254.c +1 -0
 	pit->wq = create_singlethread_workqueue("kvm-pit-wq");
 	if (!pit->wq) {
+		mutex_unlock(&pit->pit_state.lock);
 		kfree(pit);
 		return NULL;
 	}

A cleanliness comment: why is that tear-down/dealloc sequence open-coded? It should be at the end of the function, with goto labels, like we do it in similar cases.

Because the lock is around a block only. I usually don't create a goto fail-paths in these cases. Do you want one?

thanks,

Signature · 1 line
js
author
#3 lore raw
Quoted 15 lines · Jiri Slaby
Quoted 4 lines
Quoted 4 lines
arch/x86/kvm/i8254.c +0 -0
@@ -696,6 +696,7 @@ struct kvm_pit *kvm_create_pit(struct kvm *kvm, u32 flags)
 
Quoted 10 lines

arch/x86/kvm/i8254.c struct kvm_pit *kvm_create_pit(struct kvm *kvm, u32 flags) · line 702

Quoted 6 lines
arch/x86/kvm/i8254.c +1 -0
 	pit->wq = create_singlethread_workqueue("kvm-pit-wq");
 	if (!pit->wq) {
+		mutex_unlock(&pit->pit_state.lock);
 		kfree(pit);
 		return NULL;
 	}

A cleanliness comment: why is that tear-down/dealloc sequence open-coded? It should be at the end of the function, with goto labels, like we do it in similar cases.

Quoted 2 lines

Because the lock is around a block only. I usually don't create a goto fail-paths in these cases.

To be more precise what I mean by that:
if ()
return;

lock();
...
if () { [single if inside the crit section]
unlock();
return;
}
...
unlock()

...
if ()
return;
...
if ()
return;

Signature · 1 line
js
#4 lore raw
In reply to Jiri Slaby
Quoted 8 lines · Jiri Slaby
Quoted 5 lines

A cleanliness comment: why is that tear-down/dealloc sequence open-coded? It should be at the end of the function, with goto labels, like we do it in similar cases.

Because the lock is around a block only. I usually don't create a goto fail-paths in these cases. Do you want one?

In any case, the patch is a minimal fix, so I'll apply it. Any clean ups can go on top.

Signature · 2 lines
I have a truly marvellous patch that fixes the bug which this
signature is too narrow to contain.
#5 lore raw
Quoted 10 lines · Avi Kivity
Quoted 6 lines · Jiri Slaby
Quoted 4 lines

A cleanliness comment: why is that tear-down/dealloc sequence open-coded? It should be at the end of the function, with goto labels, like we do it in similar cases.

Because the lock is around a block only. I usually don't create a goto fail-paths in these cases. Do you want one?

In any case, the patch is a minimal fix, so I'll apply it. Any clean ups can go on top.

The reason the pattern caught my attention is that it is one of the typical cases where goto labels help prevent similar bugs. I.e. had it been clean to begin with the bug might not have happened.

Thanks,

Ingo

#6 lore raw
Quoted 8 lines · Ingo Molnar
Quoted 4 lines

In any case, the patch is a minimal fix, so I'll apply it. Any clean ups can go on top.

The reason the pattern caught my attention is that it is one of the typical cases where goto labels help prevent similar bugs. I.e. had it been clean to begin with the bug might not have happened.

Agreed.

Signature · 2 lines
I have a truly marvellous patch that fixes the bug which this
signature is too narrow to contain.