• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
2 
3 #include <linux/notifier.h>
4 
5 #include <xen/xen.h>
6 #include <xen/xenbus.h>
7 
8 #include <asm/xen/hypervisor.h>
9 #include <asm/cpu.h>
10 
enable_hotplug_cpu(int cpu)11 static void enable_hotplug_cpu(int cpu)
12 {
13 	if (!cpu_present(cpu))
14 		xen_arch_register_cpu(cpu);
15 
16 	set_cpu_present(cpu, true);
17 }
18 
disable_hotplug_cpu(int cpu)19 static void disable_hotplug_cpu(int cpu)
20 {
21 	if (!cpu_is_hotpluggable(cpu))
22 		return;
23 	lock_device_hotplug();
24 	if (cpu_online(cpu))
25 		device_offline(get_cpu_device(cpu));
26 	if (!cpu_online(cpu) && cpu_present(cpu)) {
27 		xen_arch_unregister_cpu(cpu);
28 		set_cpu_present(cpu, false);
29 	}
30 	unlock_device_hotplug();
31 }
32 
vcpu_online(unsigned int cpu)33 static int vcpu_online(unsigned int cpu)
34 {
35 	int err;
36 	char dir[16], state[16];
37 
38 	sprintf(dir, "cpu/%u", cpu);
39 	err = xenbus_scanf(XBT_NIL, dir, "availability", "%15s", state);
40 	if (err != 1) {
41 		if (!xen_initial_domain())
42 			pr_err("Unable to read cpu state\n");
43 		return err;
44 	}
45 
46 	if (strcmp(state, "online") == 0)
47 		return 1;
48 	else if (strcmp(state, "offline") == 0)
49 		return 0;
50 
51 	pr_err("unknown state(%s) on CPU%d\n", state, cpu);
52 	return -EINVAL;
53 }
vcpu_hotplug(unsigned int cpu)54 static void vcpu_hotplug(unsigned int cpu)
55 {
56 	if (cpu >= nr_cpu_ids || !cpu_possible(cpu))
57 		return;
58 
59 	switch (vcpu_online(cpu)) {
60 	case 1:
61 		enable_hotplug_cpu(cpu);
62 		break;
63 	case 0:
64 		disable_hotplug_cpu(cpu);
65 		break;
66 	default:
67 		break;
68 	}
69 }
70 
handle_vcpu_hotplug_event(struct xenbus_watch * watch,const char ** vec,unsigned int len)71 static void handle_vcpu_hotplug_event(struct xenbus_watch *watch,
72 					const char **vec, unsigned int len)
73 {
74 	unsigned int cpu;
75 	char *cpustr;
76 	const char *node = vec[XS_WATCH_PATH];
77 
78 	cpustr = strstr(node, "cpu/");
79 	if (cpustr != NULL) {
80 		sscanf(cpustr, "cpu/%u", &cpu);
81 		vcpu_hotplug(cpu);
82 	}
83 }
84 
setup_cpu_watcher(struct notifier_block * notifier,unsigned long event,void * data)85 static int setup_cpu_watcher(struct notifier_block *notifier,
86 			      unsigned long event, void *data)
87 {
88 	int cpu;
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 
95 	for_each_possible_cpu(cpu) {
96 		if (vcpu_online(cpu) == 0) {
97 			(void)cpu_down(cpu);
98 			set_cpu_present(cpu, false);
99 		}
100 	}
101 
102 	return NOTIFY_DONE;
103 }
104 
setup_vcpu_hotplug_event(void)105 static int __init setup_vcpu_hotplug_event(void)
106 {
107 	static struct notifier_block xsn_cpu = {
108 		.notifier_call = setup_cpu_watcher };
109 
110 #ifdef CONFIG_X86
111 	if (!xen_pv_domain())
112 #else
113 	if (!xen_domain())
114 #endif
115 		return -ENODEV;
116 
117 	register_xenstore_notifier(&xsn_cpu);
118 
119 	return 0;
120 }
121 
122 arch_initcall(setup_vcpu_hotplug_event);
123 
124