kvm.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. struct mm_struct *mm;
  73. };
  74. static struct kvm_task_sleep_head {
  75. spinlock_t lock;
  76. struct hlist_head list;
  77. } async_pf_sleepers[KVM_TASK_SLEEP_HASHSIZE];
  78. static struct kvm_task_sleep_node *_find_apf_task(struct kvm_task_sleep_head *b,
  79. u32 token)
  80. {
  81. struct hlist_node *p;
  82. hlist_for_each(p, &b->list) {
  83. struct kvm_task_sleep_node *n =
  84. hlist_entry(p, typeof(*n), link);
  85. if (n->token == token)
  86. return n;
  87. }
  88. return NULL;
  89. }
  90. void kvm_async_pf_task_wait(u32 token)
  91. {
  92. u32 key = hash_32(token, KVM_TASK_SLEEP_HASHBITS);
  93. struct kvm_task_sleep_head *b = &async_pf_sleepers[key];
  94. struct kvm_task_sleep_node n, *e;
  95. DEFINE_WAIT(wait);
  96. int cpu, idle;
  97. cpu = get_cpu();
  98. idle = idle_cpu(cpu);
  99. put_cpu();
  100. spin_lock(&b->lock);
  101. e = _find_apf_task(b, token);
  102. if (e) {
  103. /* dummy entry exist -> wake up was delivered ahead of PF */
  104. hlist_del(&e->link);
  105. kfree(e);
  106. spin_unlock(&b->lock);
  107. return;
  108. }
  109. n.token = token;
  110. n.cpu = smp_processor_id();
  111. n.mm = current->active_mm;
  112. n.halted = idle || preempt_count() > 1;
  113. atomic_inc(&n.mm->mm_count);
  114. init_waitqueue_head(&n.wq);
  115. hlist_add_head(&n.link, &b->list);
  116. spin_unlock(&b->lock);
  117. for (;;) {
  118. if (!n.halted)
  119. prepare_to_wait(&n.wq, &wait, TASK_UNINTERRUPTIBLE);
  120. if (hlist_unhashed(&n.link))
  121. break;
  122. if (!n.halted) {
  123. local_irq_enable();
  124. schedule();
  125. local_irq_disable();
  126. } else {
  127. /*
  128. * We cannot reschedule. So halt.
  129. */
  130. native_safe_halt();
  131. local_irq_disable();
  132. }
  133. }
  134. if (!n.halted)
  135. finish_wait(&n.wq, &wait);
  136. return;
  137. }
  138. EXPORT_SYMBOL_GPL(kvm_async_pf_task_wait);
  139. static void apf_task_wake_one(struct kvm_task_sleep_node *n)
  140. {
  141. hlist_del_init(&n->link);
  142. if (!n->mm)
  143. return;
  144. mmdrop(n->mm);
  145. if (n->halted)
  146. smp_send_reschedule(n->cpu);
  147. else if (waitqueue_active(&n->wq))
  148. wake_up(&n->wq);
  149. }
  150. static void apf_task_wake_all(void)
  151. {
  152. int i;
  153. for (i = 0; i < KVM_TASK_SLEEP_HASHSIZE; i++) {
  154. struct hlist_node *p, *next;
  155. struct kvm_task_sleep_head *b = &async_pf_sleepers[i];
  156. spin_lock(&b->lock);
  157. hlist_for_each_safe(p, next, &b->list) {
  158. struct kvm_task_sleep_node *n =
  159. hlist_entry(p, typeof(*n), link);
  160. if (n->cpu == smp_processor_id())
  161. apf_task_wake_one(n);
  162. }
  163. spin_unlock(&b->lock);
  164. }
  165. }
  166. void kvm_async_pf_task_wake(u32 token)
  167. {
  168. u32 key = hash_32(token, KVM_TASK_SLEEP_HASHBITS);
  169. struct kvm_task_sleep_head *b = &async_pf_sleepers[key];
  170. struct kvm_task_sleep_node *n;
  171. if (token == ~0) {
  172. apf_task_wake_all();
  173. return;
  174. }
  175. again:
  176. spin_lock(&b->lock);
  177. n = _find_apf_task(b, token);
  178. if (!n) {
  179. /*
  180. * async PF was not yet handled.
  181. * Add dummy entry for the token.
  182. */
  183. n = kmalloc(sizeof(*n), GFP_ATOMIC);
  184. if (!n) {
  185. /*
  186. * Allocation failed! Busy wait while other cpu
  187. * handles async PF.
  188. */
  189. spin_unlock(&b->lock);
  190. cpu_relax();
  191. goto again;
  192. }
  193. n->token = token;
  194. n->cpu = smp_processor_id();
  195. n->mm = NULL;
  196. init_waitqueue_head(&n->wq);
  197. hlist_add_head(&n->link, &b->list);
  198. } else
  199. apf_task_wake_one(n);
  200. spin_unlock(&b->lock);
  201. return;
  202. }
  203. EXPORT_SYMBOL_GPL(kvm_async_pf_task_wake);
  204. u32 kvm_read_and_reset_pf_reason(void)
  205. {
  206. u32 reason = 0;
  207. if (__get_cpu_var(apf_reason).enabled) {
  208. reason = __get_cpu_var(apf_reason).reason;
  209. __get_cpu_var(apf_reason).reason = 0;
  210. }
  211. return reason;
  212. }
  213. EXPORT_SYMBOL_GPL(kvm_read_and_reset_pf_reason);
  214. dotraplinkage void __kprobes
  215. do_async_page_fault(struct pt_regs *regs, unsigned long error_code)
  216. {
  217. switch (kvm_read_and_reset_pf_reason()) {
  218. default:
  219. do_page_fault(regs, error_code);
  220. break;
  221. case KVM_PV_REASON_PAGE_NOT_PRESENT:
  222. /* page is swapped out by the host. */
  223. kvm_async_pf_task_wait((u32)read_cr2());
  224. break;
  225. case KVM_PV_REASON_PAGE_READY:
  226. rcu_irq_enter();
  227. exit_idle();
  228. kvm_async_pf_task_wake((u32)read_cr2());
  229. rcu_irq_exit();
  230. break;
  231. }
  232. }
  233. static void __init paravirt_ops_setup(void)
  234. {
  235. pv_info.name = "KVM";
  236. pv_info.paravirt_enabled = 1;
  237. if (kvm_para_has_feature(KVM_FEATURE_NOP_IO_DELAY))
  238. pv_cpu_ops.io_delay = kvm_io_delay;
  239. #ifdef CONFIG_X86_IO_APIC
  240. no_timer_check = 1;
  241. #endif
  242. }
  243. static void kvm_register_steal_time(void)
  244. {
  245. int cpu = smp_processor_id();
  246. struct kvm_steal_time *st = &per_cpu(steal_time, cpu);
  247. if (!has_steal_clock)
  248. return;
  249. memset(st, 0, sizeof(*st));
  250. wrmsrl(MSR_KVM_STEAL_TIME, (__pa(st) | KVM_MSR_ENABLED));
  251. printk(KERN_INFO "kvm-stealtime: cpu %d, msr %lx\n",
  252. cpu, __pa(st));
  253. }
  254. void __cpuinit kvm_guest_cpu_init(void)
  255. {
  256. if (!kvm_para_available())
  257. return;
  258. if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF) && kvmapf) {
  259. u64 pa = __pa(&__get_cpu_var(apf_reason));
  260. #ifdef CONFIG_PREEMPT
  261. pa |= KVM_ASYNC_PF_SEND_ALWAYS;
  262. #endif
  263. wrmsrl(MSR_KVM_ASYNC_PF_EN, pa | KVM_ASYNC_PF_ENABLED);
  264. __get_cpu_var(apf_reason).enabled = 1;
  265. printk(KERN_INFO"KVM setup async PF for cpu %d\n",
  266. smp_processor_id());
  267. }
  268. if (has_steal_clock)
  269. kvm_register_steal_time();
  270. }
  271. static void kvm_pv_disable_apf(void *unused)
  272. {
  273. if (!__get_cpu_var(apf_reason).enabled)
  274. return;
  275. wrmsrl(MSR_KVM_ASYNC_PF_EN, 0);
  276. __get_cpu_var(apf_reason).enabled = 0;
  277. printk(KERN_INFO"Unregister pv shared memory for cpu %d\n",
  278. smp_processor_id());
  279. }
  280. static int kvm_pv_reboot_notify(struct notifier_block *nb,
  281. unsigned long code, void *unused)
  282. {
  283. if (code == SYS_RESTART)
  284. on_each_cpu(kvm_pv_disable_apf, NULL, 1);
  285. return NOTIFY_DONE;
  286. }
  287. static struct notifier_block kvm_pv_reboot_nb = {
  288. .notifier_call = kvm_pv_reboot_notify,
  289. };
  290. static u64 kvm_steal_clock(int cpu)
  291. {
  292. u64 steal;
  293. struct kvm_steal_time *src;
  294. int version;
  295. src = &per_cpu(steal_time, cpu);
  296. do {
  297. version = src->version;
  298. rmb();
  299. steal = src->steal;
  300. rmb();
  301. } while ((version & 1) || (version != src->version));
  302. return steal;
  303. }
  304. void kvm_disable_steal_time(void)
  305. {
  306. if (!has_steal_clock)
  307. return;
  308. wrmsr(MSR_KVM_STEAL_TIME, 0, 0);
  309. }
  310. #ifdef CONFIG_SMP
  311. static void __init kvm_smp_prepare_boot_cpu(void)
  312. {
  313. #ifdef CONFIG_KVM_CLOCK
  314. WARN_ON(kvm_register_clock("primary cpu clock"));
  315. #endif
  316. kvm_guest_cpu_init();
  317. native_smp_prepare_boot_cpu();
  318. }
  319. static void __cpuinit kvm_guest_cpu_online(void *dummy)
  320. {
  321. kvm_guest_cpu_init();
  322. }
  323. static void kvm_guest_cpu_offline(void *dummy)
  324. {
  325. kvm_disable_steal_time();
  326. kvm_pv_disable_apf(NULL);
  327. apf_task_wake_all();
  328. }
  329. static int __cpuinit kvm_cpu_notify(struct notifier_block *self,
  330. unsigned long action, void *hcpu)
  331. {
  332. int cpu = (unsigned long)hcpu;
  333. switch (action) {
  334. case CPU_ONLINE:
  335. case CPU_DOWN_FAILED:
  336. case CPU_ONLINE_FROZEN:
  337. smp_call_function_single(cpu, kvm_guest_cpu_online, NULL, 0);
  338. break;
  339. case CPU_DOWN_PREPARE:
  340. case CPU_DOWN_PREPARE_FROZEN:
  341. smp_call_function_single(cpu, kvm_guest_cpu_offline, NULL, 1);
  342. break;
  343. default:
  344. break;
  345. }
  346. return NOTIFY_OK;
  347. }
  348. static struct notifier_block __cpuinitdata kvm_cpu_notifier = {
  349. .notifier_call = kvm_cpu_notify,
  350. };
  351. #endif
  352. static void __init kvm_apf_trap_init(void)
  353. {
  354. set_intr_gate(14, &async_page_fault);
  355. }
  356. void __init kvm_guest_init(void)
  357. {
  358. int i;
  359. if (!kvm_para_available())
  360. return;
  361. paravirt_ops_setup();
  362. register_reboot_notifier(&kvm_pv_reboot_nb);
  363. for (i = 0; i < KVM_TASK_SLEEP_HASHSIZE; i++)
  364. spin_lock_init(&async_pf_sleepers[i].lock);
  365. if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF))
  366. x86_init.irqs.trap_init = kvm_apf_trap_init;
  367. if (kvm_para_has_feature(KVM_FEATURE_STEAL_TIME)) {
  368. has_steal_clock = 1;
  369. pv_time_ops.steal_clock = kvm_steal_clock;
  370. }
  371. #ifdef CONFIG_SMP
  372. smp_ops.smp_prepare_boot_cpu = kvm_smp_prepare_boot_cpu;
  373. register_cpu_notifier(&kvm_cpu_notifier);
  374. #else
  375. kvm_guest_cpu_init();
  376. #endif
  377. }
  378. static __init int activate_jump_labels(void)
  379. {
  380. if (has_steal_clock) {
  381. static_key_slow_inc(&paravirt_steal_enabled);
  382. if (steal_acc)
  383. static_key_slow_inc(&paravirt_steal_rq_enabled);
  384. }
  385. return 0;
  386. }
  387. arch_initcall(activate_jump_labels);