cpu_hotplug.c 2.0 KB

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