cpu_hotplug.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
  2. #include <linux/notifier.h>
  3. #include <xen/xen.h>
  4. #include <xen/xenbus.h>
  5. #include <asm/xen/hypervisor.h>
  6. #include <asm/cpu.h>
  7. static void enable_hotplug_cpu(int cpu)
  8. {
  9. if (!cpu_present(cpu))
  10. arch_register_cpu(cpu);
  11. set_cpu_present(cpu, true);
  12. }
  13. static void disable_hotplug_cpu(int cpu)
  14. {
  15. if (cpu_present(cpu))
  16. arch_unregister_cpu(cpu);
  17. set_cpu_present(cpu, false);
  18. }
  19. static int vcpu_online(unsigned int cpu)
  20. {
  21. int err;
  22. char dir[16], state[16];
  23. sprintf(dir, "cpu/%u", cpu);
  24. err = xenbus_scanf(XBT_NIL, dir, "availability", "%15s", state);
  25. if (err != 1) {
  26. if (!xen_initial_domain())
  27. pr_err("Unable to read cpu state\n");
  28. return err;
  29. }
  30. if (strcmp(state, "online") == 0)
  31. return 1;
  32. else if (strcmp(state, "offline") == 0)
  33. return 0;
  34. pr_err("unknown state(%s) on CPU%d\n", state, cpu);
  35. return -EINVAL;
  36. }
  37. static void vcpu_hotplug(unsigned int cpu)
  38. {
  39. if (!cpu_possible(cpu))
  40. return;
  41. switch (vcpu_online(cpu)) {
  42. case 1:
  43. enable_hotplug_cpu(cpu);
  44. break;
  45. case 0:
  46. (void)cpu_down(cpu);
  47. disable_hotplug_cpu(cpu);
  48. break;
  49. default:
  50. break;
  51. }
  52. }
  53. static void handle_vcpu_hotplug_event(struct xenbus_watch *watch,
  54. const char **vec, unsigned int len)
  55. {
  56. unsigned int cpu;
  57. char *cpustr;
  58. const char *node = vec[XS_WATCH_PATH];
  59. cpustr = strstr(node, "cpu/");
  60. if (cpustr != NULL) {
  61. sscanf(cpustr, "cpu/%u", &cpu);
  62. vcpu_hotplug(cpu);
  63. }
  64. }
  65. static int setup_cpu_watcher(struct notifier_block *notifier,
  66. unsigned long event, void *data)
  67. {
  68. int cpu;
  69. static struct xenbus_watch cpu_watch = {
  70. .node = "cpu",
  71. .callback = handle_vcpu_hotplug_event};
  72. (void)register_xenbus_watch(&cpu_watch);
  73. for_each_possible_cpu(cpu) {
  74. if (vcpu_online(cpu) == 0) {
  75. (void)cpu_down(cpu);
  76. set_cpu_present(cpu, false);
  77. }
  78. }
  79. return NOTIFY_DONE;
  80. }
  81. static int __init setup_vcpu_hotplug_event(void)
  82. {
  83. static struct notifier_block xsn_cpu = {
  84. .notifier_call = setup_cpu_watcher };
  85. if (!xen_pv_domain())
  86. return -ENODEV;
  87. register_xenstore_notifier(&xsn_cpu);
  88. return 0;
  89. }
  90. arch_initcall(setup_vcpu_hotplug_event);