Re: [PATCH 3/4] KVM: Register cpuhp and syscore callbacks when enabling hardware

Chao Gao
2024-04-26T08:32:48Z

>+static int hardware_enable_all(void)
>+{
>+ int r;
>+
>+ guard(mutex)(&kvm_lock);
>+
>+ if (kvm_usage_count++)
>+ return 0;
>+
>+ r = cpuhp_setup_state(CPUHP_AP_KVM_ONLINE, "kvm/cpu:online",
>+ kvm_online_cpu, kvm_offline_cpu);

A subtle change is: cpuhp_setup_state() calls kvm_online_cpu() serially
on all CPUs. Previously, hardware enabling is done with on_each_cpu().
I assume performance isn't a concern here. Right?

>+ if (r)
>+ return r;

decrease kvm_usage_count on error?

>+
>+ register_syscore_ops(&kvm_syscore_ops);
>+
>+ /*
>+ * Undo virtualization enabling and bail if the system is going down.
>+ * If userspace initiated a forced reboot, e.g. reboot -f, then it's
>+ * possible for an in-flight operation to enable virtualization after
>+ * syscore_shutdown() is called, i.e. without kvm_shutdown() being
>+ * invoked. Note, this relies on system_state being set _before_
>+ * kvm_shutdown(), e.g. to ensure either kvm_shutdown() is invoked
>+ * or this CPU observes the impending shutdown. Which is why KVM uses
>+ * a syscore ops hook instead of registering a dedicated reboot
>+ * notifier (the latter runs before system_state is updated).
>+ */
>+ if (system_state == SYSTEM_HALT || system_state == SYSTEM_POWER_OFF ||
>+ system_state == SYSTEM_RESTART) {
>+ unregister_syscore_ops(&kvm_syscore_ops);
>+ cpuhp_remove_state(CPUHP_AP_KVM_ONLINE);
>+ return -EBUSY;

ditto

>+ }
>+
>+ return 0;
>+}