cpu_hotplug.c 2.0 KB

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