kvm.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /*
  2. * KVM paravirt_ops implementation
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. *
  18. * Copyright (C) 2007, Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
  19. * Copyright IBM Corporation, 2007
  20. * Authors: Anthony Liguori <aliguori@us.ibm.com>
  21. */
  22. #include <linux/module.h>
  23. #include <linux/kernel.h>
  24. #include <linux/kvm_para.h>
  25. #include <linux/cpu.h>
  26. #include <linux/mm.h>
  27. #include <linux/highmem.h>
  28. #include <linux/hardirq.h>
  29. #include <linux/notifier.h>
  30. #include <linux/reboot.h>
  31. #include <linux/hash.h>
  32. #include <linux/sched.h>
  33. #include <linux/slab.h>
  34. #include <linux/kprobes.h>
  35. #include <asm/timer.h>
  36. #include <asm/cpu.h>
  37. #include <asm/traps.h>
  38. #include <asm/desc.h>
  39. #include <asm/tlbflush.h>
  40. #include <asm/idle.h>
  41. static int kvmapf = 1;
  42. static int parse_no_kvmapf(char *arg)
  43. {
  44. kvmapf = 0;
  45. return 0;
  46. }
  47. early_param("no-kvmapf", parse_no_kvmapf);
  48. static int steal_acc = 1;
  49. static int parse_no_stealacc(char *arg)
  50. {
  51. steal_acc = 0;
  52. return 0;
  53. }
  54. early_param("no-steal-acc", parse_no_stealacc);
  55. static DEFINE_PER_CPU(struct kvm_vcpu_pv_apf_data, apf_reason) __aligned(64);
  56. static DEFINE_PER_CPU(struct kvm_steal_time, steal_time) __aligned(64);
  57. static int has_steal_clock = 0;
  58. /*
  59. * No need for any "IO delay" on KVM
  60. */
  61. static void kvm_io_delay(void)
  62. {
  63. }
  64. #define KVM_TASK_SLEEP_HASHBITS 8
  65. #define KVM_TASK_SLEEP_HASHSIZE (1<<KVM_TASK_SLEEP_HASHBITS)
  66. struct kvm_task_sleep_node {
  67. struct hlist_node link;
  68. wait_queue_head_t wq;
  69. u32 token;
  70. int cpu;
  71. bool halted;
  72. };
  73. static struct kvm_task_sleep_head {
  74. spinlock_t lock;
  75. struct hlist_head list;
  76. } async_pf_sleepers[KVM_TASK_SLEEP_HASHSIZE];
  77. static struct kvm_task_sleep_node *_find_apf_task(struct kvm_task_sleep_head *b,
  78. u32 token)
  79. {
  80. struct hlist_node *p;
  81. hlist_for_each(p, &b->list) {
  82. struct kvm_task_sleep_node *n =
  83. hlist_entry(p, typeof(*n), link);
  84. if (n->token == token)
  85. return n;
  86. }
  87. return NULL;
  88. }
  89. void kvm_async_pf_task_wait(u32 token)
  90. {
  91. u32 key = hash_32(token, KVM_TASK_SLEEP_HASHBITS);
  92. struct kvm_task_sleep_head *b = &async_pf_sleepers[key];
  93. struct kvm_task_sleep_node n, *e;
  94. DEFINE_WAIT(wait);
  95. int cpu, idle;
  96. cpu = get_cpu();
  97. idle = idle_cpu(cpu);
  98. put_cpu();
  99. spin_lock(&b->lock);
  100. e = _find_apf_task(b, token);
  101. if (e) {
  102. /* dummy entry exist -> wake up was delivered ahead of PF */
  103. hlist_del(&e->link);
  104. kfree(e);
  105. spin_unlock(&b->lock);
  106. return;
  107. }
  108. n.token = token;
  109. n.cpu = smp_processor_id();
  110. n.halted = idle || preempt_count() > 1;
  111. init_waitqueue_head(&n.wq);
  112. hlist_add_head(&n.link, &b->list);
  113. spin_unlock(&b->lock);
  114. for (;;) {
  115. if (!n.halted)
  116. prepare_to_wait(&n.wq, &wait, TASK_UNINTERRUPTIBLE);
  117. if (hlist_unhashed(&n.link))
  118. break;
  119. if (!n.halted) {
  120. local_irq_enable();
  121. schedule();
  122. local_irq_disable();
  123. } else {
  124. /*
  125. * We cannot reschedule. So halt.
  126. */
  127. native_safe_halt();
  128. local_irq_disable();
  129. }
  130. }
  131. if (!n.halted)
  132. finish_wait(&n.wq, &wait);
  133. return;
  134. }
  135. EXPORT_SYMBOL_GPL(kvm_async_pf_task_wait);
  136. static void apf_task_wake_one(struct kvm_task_sleep_node *n)
  137. {
  138. hlist_del_init(&n->link);
  139. if (n->halted)
  140. smp_send_reschedule(n->cpu);
  141. else if (waitqueue_active(&n->wq))
  142. wake_up(&n->wq);
  143. }
  144. static void apf_task_wake_all(void)
  145. {
  146. int i;
  147. for (i = 0; i < KVM_TASK_SLEEP_HASHSIZE; i++) {
  148. struct hlist_node *p, *next;
  149. struct kvm_task_sleep_head *b = &async_pf_sleepers[i];
  150. spin_lock(&b->lock);
  151. hlist_for_each_safe(p, next, &b->list) {
  152. struct kvm_task_sleep_node *n =
  153. hlist_entry(p, typeof(*n), link);
  154. if (n->cpu == smp_processor_id())
  155. apf_task_wake_one(n);
  156. }
  157. spin_unlock(&b->lock);
  158. }
  159. }
  160. void kvm_async_pf_task_wake(u32 token)
  161. {
  162. u32 key = hash_32(token, KVM_TASK_SLEEP_HASHBITS);
  163. struct kvm_task_sleep_head *b = &async_pf_sleepers[key];
  164. struct kvm_task_sleep_node *n;
  165. if (token == ~0) {
  166. apf_task_wake_all();
  167. return;
  168. }
  169. again:
  170. spin_lock(&b->lock);
  171. n = _find_apf_task(b, token);
  172. if (!n) {
  173. /*
  174. * async PF was not yet handled.
  175. * Add dummy entry for the token.
  176. */
  177. n = kzalloc(sizeof(*n), GFP_ATOMIC);
  178. if (!n) {
  179. /*
  180. * Allocation failed! Busy wait while other cpu
  181. * handles async PF.
  182. */
  183. spin_unlock(&b->lock);
  184. cpu_relax();
  185. goto again;
  186. }
  187. n->token = token;
  188. n->cpu = smp_processor_id();
  189. init_waitqueue_head(&n->wq);
  190. hlist_add_head(&n->link, &b->list);
  191. } else
  192. apf_task_wake_one(n);
  193. spin_unlock(&b->lock);
  194. return;
  195. }
  196. EXPORT_SYMBOL_GPL(kvm_async_pf_task_wake);
  197. u32 kvm_read_and_reset_pf_reason(void)
  198. {
  199. u32 reason = 0;
  200. if (__get_cpu_var(apf_reason).enabled) {
  201. reason = __get_cpu_var(apf_reason).reason;
  202. __get_cpu_var(apf_reason).reason = 0;
  203. }
  204. return reason;
  205. }
  206. EXPORT_SYMBOL_GPL(kvm_read_and_reset_pf_reason);
  207. dotraplinkage void __kprobes
  208. do_async_page_fault(struct pt_regs *regs, unsigned long error_code)
  209. {
  210. switch (kvm_read_and_reset_pf_reason()) {
  211. default:
  212. do_page_fault(regs, error_code);
  213. break;
  214. case KVM_PV_REASON_PAGE_NOT_PRESENT:
  215. /* page is swapped out by the host. */
  216. kvm_async_pf_task_wait((u32)read_cr2());
  217. break;
  218. case KVM_PV_REASON_PAGE_READY:
  219. rcu_irq_enter();
  220. exit_idle();
  221. kvm_async_pf_task_wake((u32)read_cr2());
  222. rcu_irq_exit();
  223. break;
  224. }
  225. }
  226. static void __init paravirt_ops_setup(void)
  227. {
  228. pv_info.name = "KVM";
  229. /*
  230. * KVM isn't paravirt in the sense of paravirt_enabled. A KVM
  231. * guest kernel works like a bare metal kernel with additional
  232. * features, and paravirt_enabled is about features that are
  233. * missing.
  234. */
  235. pv_info.paravirt_enabled = 0;
  236. if (kvm_para_has_feature(KVM_FEATURE_NOP_IO_DELAY))
  237. pv_cpu_ops.io_delay = kvm_io_delay;
  238. #ifdef CONFIG_X86_IO_APIC
  239. no_timer_check = 1;
  240. #endif
  241. }
  242. static void kvm_register_steal_time(void)
  243. {
  244. int cpu = smp_processor_id();
  245. struct kvm_steal_time *st = &per_cpu(steal_time, cpu);
  246. if (!has_steal_clock)
  247. return;
  248. memset(st, 0, sizeof(*st));
  249. wrmsrl(MSR_KVM_STEAL_TIME, (__pa(st) | KVM_MSR_ENABLED));
  250. printk(KERN_INFO "kvm-stealtime: cpu %d, msr %lx\n",
  251. cpu, __pa(st));
  252. }
  253. void __cpuinit kvm_guest_cpu_init(void)
  254. {
  255. if (!kvm_para_available())
  256. return;
  257. if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF) && kvmapf) {
  258. u64 pa = __pa(&__get_cpu_var(apf_reason));
  259. #ifdef CONFIG_PREEMPT
  260. pa |= KVM_ASYNC_PF_SEND_ALWAYS;
  261. #endif
  262. wrmsrl(MSR_KVM_ASYNC_PF_EN, pa | KVM_ASYNC_PF_ENABLED);
  263. __get_cpu_var(apf_reason).enabled = 1;
  264. printk(KERN_INFO"KVM setup async PF for cpu %d\n",
  265. smp_processor_id());
  266. }
  267. if (has_steal_clock)
  268. kvm_register_steal_time();
  269. }
  270. static void kvm_pv_disable_apf(void *unused)
  271. {
  272. if (!__get_cpu_var(apf_reason).enabled)
  273. return;
  274. wrmsrl(MSR_KVM_ASYNC_PF_EN, 0);
  275. __get_cpu_var(apf_reason).enabled = 0;
  276. printk(KERN_INFO"Unregister pv shared memory for cpu %d\n",
  277. smp_processor_id());
  278. }
  279. static int kvm_pv_reboot_notify(struct notifier_block *nb,
  280. unsigned long code, void *unused)
  281. {
  282. if (code == SYS_RESTART)
  283. on_each_cpu(kvm_pv_disable_apf, NULL, 1);
  284. return NOTIFY_DONE;
  285. }
  286. static struct notifier_block kvm_pv_reboot_nb = {
  287. .notifier_call = kvm_pv_reboot_notify,
  288. };
  289. static u64 kvm_steal_clock(int cpu)
  290. {
  291. u64 steal;
  292. struct kvm_steal_time *src;
  293. int version;
  294. src = &per_cpu(steal_time, cpu);
  295. do {
  296. version = src->version;
  297. rmb();
  298. steal = src->steal;
  299. rmb();
  300. } while ((version & 1) || (version != src->version));
  301. return steal;
  302. }
  303. void kvm_disable_steal_time(void)
  304. {
  305. if (!has_steal_clock)
  306. return;
  307. wrmsr(MSR_KVM_STEAL_TIME, 0, 0);
  308. }
  309. #ifdef CONFIG_SMP
  310. static void __init kvm_smp_prepare_boot_cpu(void)
  311. {
  312. #ifdef CONFIG_KVM_CLOCK
  313. WARN_ON(kvm_register_clock("primary cpu clock"));
  314. #endif
  315. kvm_guest_cpu_init();
  316. native_smp_prepare_boot_cpu();
  317. }
  318. static void __cpuinit kvm_guest_cpu_online(void *dummy)
  319. {
  320. kvm_guest_cpu_init();
  321. }
  322. static void kvm_guest_cpu_offline(void *dummy)
  323. {
  324. kvm_disable_steal_time();
  325. kvm_pv_disable_apf(NULL);
  326. apf_task_wake_all();
  327. }
  328. static int __cpuinit kvm_cpu_notify(struct notifier_block *self,
  329. unsigned long action, void *hcpu)
  330. {
  331. int cpu = (unsigned long)hcpu;
  332. switch (action) {
  333. case CPU_ONLINE:
  334. case CPU_DOWN_FAILED:
  335. case CPU_ONLINE_FROZEN:
  336. smp_call_function_single(cpu, kvm_guest_cpu_online, NULL, 0);
  337. break;
  338. case CPU_DOWN_PREPARE:
  339. case CPU_DOWN_PREPARE_FROZEN:
  340. smp_call_function_single(cpu, kvm_guest_cpu_offline, NULL, 1);
  341. break;
  342. default:
  343. break;
  344. }
  345. return NOTIFY_OK;
  346. }
  347. static struct notifier_block __cpuinitdata kvm_cpu_notifier = {
  348. .notifier_call = kvm_cpu_notify,
  349. };
  350. #endif
  351. static void __init kvm_apf_trap_init(void)
  352. {
  353. set_intr_gate(14, &async_page_fault);
  354. }
  355. void __init kvm_guest_init(void)
  356. {
  357. int i;
  358. if (!kvm_para_available())
  359. return;
  360. paravirt_ops_setup();
  361. register_reboot_notifier(&kvm_pv_reboot_nb);
  362. for (i = 0; i < KVM_TASK_SLEEP_HASHSIZE; i++)
  363. spin_lock_init(&async_pf_sleepers[i].lock);
  364. if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF))
  365. x86_init.irqs.trap_init = kvm_apf_trap_init;
  366. if (kvm_para_has_feature(KVM_FEATURE_STEAL_TIME)) {
  367. has_steal_clock = 1;
  368. pv_time_ops.steal_clock = kvm_steal_clock;
  369. }
  370. #ifdef CONFIG_SMP
  371. smp_ops.smp_prepare_boot_cpu = kvm_smp_prepare_boot_cpu;
  372. register_cpu_notifier(&kvm_cpu_notifier);
  373. #else
  374. kvm_guest_cpu_init();
  375. #endif
  376. }
  377. static __init int activate_jump_labels(void)
  378. {
  379. if (has_steal_clock) {
  380. static_key_slow_inc(&paravirt_steal_enabled);
  381. if (steal_acc)
  382. static_key_slow_inc(&paravirt_steal_rq_enabled);
  383. }
  384. return 0;
  385. }
  386. arch_initcall(activate_jump_labels);