Merge tag 'for-6.4-rc1-tag' of git://git.kernel.org/pub/scm/linux/kernel/git/kdave...
[linux-block.git] / drivers / xen / cpu_hotplug.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
283c0972
JP
2#define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
3
d68d82af
AN
4#include <linux/notifier.h>
5
1ccbf534 6#include <xen/xen.h>
d68d82af
AN
7#include <xen/xenbus.h>
8
bb898558 9#include <asm/xen/hypervisor.h>
d68d82af
AN
10#include <asm/cpu.h>
11
12static void enable_hotplug_cpu(int cpu)
13{
14 if (!cpu_present(cpu))
a314e3eb 15 xen_arch_register_cpu(cpu);
d68d82af 16
d680eb8b 17 set_cpu_present(cpu, true);
d68d82af
AN
18}
19
20static void disable_hotplug_cpu(int cpu)
21{
3366cdb6
OH
22 if (!cpu_is_hotpluggable(cpu))
23 return;
24 lock_device_hotplug();
25 if (cpu_online(cpu))
1c7a6213 26 device_offline(get_cpu_device(cpu));
3366cdb6 27 if (!cpu_online(cpu) && cpu_present(cpu)) {
a314e3eb 28 xen_arch_unregister_cpu(cpu);
3366cdb6
OH
29 set_cpu_present(cpu, false);
30 }
31 unlock_device_hotplug();
d68d82af
AN
32}
33
d745562c 34static int vcpu_online(unsigned int cpu)
d68d82af
AN
35{
36 int err;
e5c702d3 37 char dir[16], state[16];
d68d82af 38
d68d82af 39 sprintf(dir, "cpu/%u", cpu);
e5c702d3 40 err = xenbus_scanf(XBT_NIL, dir, "availability", "%15s", state);
d68d82af 41 if (err != 1) {
5b02aa1e 42 if (!xen_initial_domain())
283c0972 43 pr_err("Unable to read cpu state\n");
d745562c 44 return err;
d68d82af
AN
45 }
46
d745562c
IC
47 if (strcmp(state, "online") == 0)
48 return 1;
49 else if (strcmp(state, "offline") == 0)
50 return 0;
51
283c0972 52 pr_err("unknown state(%s) on CPU%d\n", state, cpu);
d745562c
IC
53 return -EINVAL;
54}
55static void vcpu_hotplug(unsigned int cpu)
56{
20167609 57 if (cpu >= nr_cpu_ids || !cpu_possible(cpu))
d745562c
IC
58 return;
59
60 switch (vcpu_online(cpu)) {
61 case 1:
d68d82af 62 enable_hotplug_cpu(cpu);
d745562c
IC
63 break;
64 case 0:
d68d82af 65 disable_hotplug_cpu(cpu);
d745562c
IC
66 break;
67 default:
68 break;
d68d82af
AN
69 }
70}
71
72static void handle_vcpu_hotplug_event(struct xenbus_watch *watch,
5584ea25 73 const char *path, const char *token)
d68d82af
AN
74{
75 unsigned int cpu;
76 char *cpustr;
d68d82af 77
5584ea25 78 cpustr = strstr(path, "cpu/");
d68d82af
AN
79 if (cpustr != NULL) {
80 sscanf(cpustr, "cpu/%u", &cpu);
81 vcpu_hotplug(cpu);
82 }
83}
84
85static int setup_cpu_watcher(struct notifier_block *notifier,
86 unsigned long event, void *data)
87{
d745562c 88 int cpu;
d68d82af
AN
89 static struct xenbus_watch cpu_watch = {
90 .node = "cpu",
91 .callback = handle_vcpu_hotplug_event};
92
93 (void)register_xenbus_watch(&cpu_watch);
94
d745562c 95 for_each_possible_cpu(cpu) {
c54b071c
BO
96 if (vcpu_online(cpu) == 0)
97 disable_hotplug_cpu(cpu);
d745562c
IC
98 }
99
d68d82af
AN
100 return NOTIFY_DONE;
101}
102
103static int __init setup_vcpu_hotplug_event(void)
104{
105 static struct notifier_block xsn_cpu = {
106 .notifier_call = setup_cpu_watcher };
107
a314e3eb 108#ifdef CONFIG_X86
2a7197f0 109 if (!xen_pv_domain() && !xen_pvh_domain())
a314e3eb
SS
110#else
111 if (!xen_domain())
112#endif
d68d82af
AN
113 return -ENODEV;
114
115 register_xenstore_notifier(&xsn_cpu);
116
117 return 0;
118}
119
c54b071c 120late_initcall(setup_vcpu_hotplug_event);
d68d82af 121