cpu_hotplug.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. if (!xen_initial_domain())
  26. printk(KERN_ERR "XENBUS: Unable to read cpu state\n");
  27. return err;
  28. }
  29. if (strcmp(state, "online") == 0)
  30. return 1;
  31. else if (strcmp(state, "offline") == 0)
  32. return 0;
  33. printk(KERN_ERR "XENBUS: unknown state(%s) on CPU%d\n", state, cpu);
  34. return -EINVAL;
  35. }
  36. static void vcpu_hotplug(unsigned int cpu)
  37. {
  38. if (!cpu_possible(cpu))
  39. return;
  40. switch (vcpu_online(cpu)) {
  41. case 1:
  42. enable_hotplug_cpu(cpu);
  43. break;
  44. case 0:
  45. (void)cpu_down(cpu);
  46. disable_hotplug_cpu(cpu);
  47. break;
  48. default:
  49. break;
  50. }
  51. }
  52. static void handle_vcpu_hotplug_event(struct xenbus_watch *watch,
  53. const char **vec, unsigned int len)
  54. {
  55. unsigned int cpu;
  56. char *cpustr;
  57. const char *node = vec[XS_WATCH_PATH];
  58. cpustr = strstr(node, "cpu/");
  59. if (cpustr != NULL) {
  60. sscanf(cpustr, "cpu/%u", &cpu);
  61. vcpu_hotplug(cpu);
  62. }
  63. }
  64. static int setup_cpu_watcher(struct notifier_block *notifier,
  65. unsigned long event, void *data)
  66. {
  67. int cpu;
  68. static struct xenbus_watch cpu_watch = {
  69. .node = "cpu",
  70. .callback = handle_vcpu_hotplug_event};
  71. (void)register_xenbus_watch(&cpu_watch);
  72. for_each_possible_cpu(cpu) {
  73. if (vcpu_online(cpu) == 0) {
  74. (void)cpu_down(cpu);
  75. set_cpu_present(cpu, false);
  76. }
  77. }
  78. return NOTIFY_DONE;
  79. }
  80. static int __init setup_vcpu_hotplug_event(void)
  81. {
  82. static struct notifier_block xsn_cpu = {
  83. .notifier_call = setup_cpu_watcher };
  84. if (!xen_pv_domain())
  85. return -ENODEV;
  86. register_xenstore_notifier(&xsn_cpu);
  87. return 0;
  88. }
  89. arch_initcall(setup_vcpu_hotplug_event);