Skip to content

Commit

Permalink
KVM: protect kvm_usage_count with its own spinlock
Browse files Browse the repository at this point in the history
The VM list need not be protected by a raw spinlock.  Separate the
two so that kvm_lock can be made non-raw.

Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Reviewed-by: Gleb Natapov <[email protected]>
Signed-off-by: Paolo Bonzini <[email protected]>
  • Loading branch information
bonzini committed Sep 30, 2013
1 parent 4fa92fb commit 4a937f9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
6 changes: 5 additions & 1 deletion Documentation/virtual/kvm/locking.txt
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,11 @@ Name: kvm_lock
Type: raw_spinlock
Arch: any
Protects: - vm_list
- hardware virtualization enable/disable

Name: kvm_count_lock
Type: raw_spinlock_t
Arch: any
Protects: - hardware virtualization enable/disable
Comment: 'raw' because hardware enabling/disabling must be atomic /wrt
migration.

Expand Down
19 changes: 10 additions & 9 deletions virt/kvm/kvm_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ MODULE_LICENSE("GPL");
*/

DEFINE_RAW_SPINLOCK(kvm_lock);
static DEFINE_RAW_SPINLOCK(kvm_count_lock);
LIST_HEAD(vm_list);

static cpumask_var_t cpus_hardware_enabled;
Expand Down Expand Up @@ -2683,10 +2684,10 @@ static void hardware_enable_nolock(void *junk)

static void hardware_enable(void)
{
raw_spin_lock(&kvm_lock);
raw_spin_lock(&kvm_count_lock);
if (kvm_usage_count)
hardware_enable_nolock(NULL);
raw_spin_unlock(&kvm_lock);
raw_spin_unlock(&kvm_count_lock);
}

static void hardware_disable_nolock(void *junk)
Expand All @@ -2701,10 +2702,10 @@ static void hardware_disable_nolock(void *junk)

static void hardware_disable(void)
{
raw_spin_lock(&kvm_lock);
raw_spin_lock(&kvm_count_lock);
if (kvm_usage_count)
hardware_disable_nolock(NULL);
raw_spin_unlock(&kvm_lock);
raw_spin_unlock(&kvm_count_lock);
}

static void hardware_disable_all_nolock(void)
Expand All @@ -2718,16 +2719,16 @@ static void hardware_disable_all_nolock(void)

static void hardware_disable_all(void)
{
raw_spin_lock(&kvm_lock);
raw_spin_lock(&kvm_count_lock);
hardware_disable_all_nolock();
raw_spin_unlock(&kvm_lock);
raw_spin_unlock(&kvm_count_lock);
}

static int hardware_enable_all(void)
{
int r = 0;

raw_spin_lock(&kvm_lock);
raw_spin_lock(&kvm_count_lock);

kvm_usage_count++;
if (kvm_usage_count == 1) {
Expand All @@ -2740,7 +2741,7 @@ static int hardware_enable_all(void)
}
}

raw_spin_unlock(&kvm_lock);
raw_spin_unlock(&kvm_count_lock);

return r;
}
Expand Down Expand Up @@ -3130,7 +3131,7 @@ static int kvm_suspend(void)
static void kvm_resume(void)
{
if (kvm_usage_count) {
WARN_ON(raw_spin_is_locked(&kvm_lock));
WARN_ON(raw_spin_is_locked(&kvm_count_lock));
hardware_enable_nolock(NULL);
}
}
Expand Down

0 comments on commit 4a937f9

Please sign in to comment.