kvm.c 9.6 KB

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