[PATCH 3/4] KVM: kvm_main.c: Remove a global variable, hardware_enable_failed

From: isaku . yamahata
Date: Wed Nov 02 2022 - 02:24:37 EST


From: Isaku Yamahata <isaku.yamahata@xxxxxxxxx>

A global variable hardware_enable_failed in kvm_main.c is used only by
hardware_enable_all() and hardware_enable_nolock(). It doesn't have to be
a global variable. Make it function local.

Signed-off-by: Isaku Yamahata <isaku.yamahata@xxxxxxxxx>
---
virt/kvm/kvm_main.c | 29 +++++++++++++++++------------
1 file changed, 17 insertions(+), 12 deletions(-)

diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 1a21b21ba326..ac24fef2c818 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -105,7 +105,6 @@ LIST_HEAD(vm_list);

static cpumask_t cpus_hardware_enabled = CPU_MASK_NONE;
static int kvm_usage_count;
-static atomic_t hardware_enable_failed;

static struct kmem_cache *kvm_vcpu_cache;

@@ -5007,30 +5006,36 @@ static struct miscdevice kvm_dev = {
&kvm_chardev_ops,
};

-static void hardware_enable_nolock(void *junk)
+static int __hardware_enable_nolock(void)
{
int cpu = raw_smp_processor_id();
int r;

- if (cpumask_test_cpu(cpu, &cpus_hardware_enabled))
- return;
-
- cpumask_set_cpu(cpu, &cpus_hardware_enabled);
+ if (cpumask_test_and_set_cpu(cpu, &cpus_hardware_enabled))
+ return 0;

r = kvm_arch_hardware_enable();

if (r) {
cpumask_clear_cpu(cpu, &cpus_hardware_enabled);
- atomic_inc(&hardware_enable_failed);
pr_info("kvm: enabling virtualization on CPU%d failed\n", cpu);
}
+ return r;
+}
+
+static void hardware_enable_nolock(void *arg)
+{
+ atomic_t *failed = arg;
+
+ if (__hardware_enable_nolock())
+ atomic_inc(failed);
}

static int kvm_starting_cpu(unsigned int cpu)
{
raw_spin_lock(&kvm_count_lock);
if (kvm_usage_count)
- hardware_enable_nolock(NULL);
+ (void)__hardware_enable_nolock();
raw_spin_unlock(&kvm_count_lock);
return 0;
}
@@ -5072,16 +5077,16 @@ static void hardware_disable_all(void)

static int hardware_enable_all(void)
{
+ atomic_t failed = ATOMIC_INIT(0);
int r = 0;

raw_spin_lock(&kvm_count_lock);

kvm_usage_count++;
if (kvm_usage_count == 1) {
- atomic_set(&hardware_enable_failed, 0);
- on_each_cpu(hardware_enable_nolock, NULL, 1);
+ on_each_cpu(hardware_enable_nolock, &failed, 1);

- if (atomic_read(&hardware_enable_failed)) {
+ if (atomic_read(&failed)) {
hardware_disable_all_nolock();
r = -EBUSY;
}
@@ -5702,7 +5707,7 @@ static void kvm_resume(void)
{
if (kvm_usage_count) {
lockdep_assert_not_held(&kvm_count_lock);
- hardware_enable_nolock(NULL);
+ (void)__hardware_enable_nolock();
}
}

--
2.25.1