kvm.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  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/context_tracking.h>
  23. #include <linux/module.h>
  24. #include <linux/kernel.h>
  25. #include <linux/kvm_para.h>
  26. #include <linux/cpu.h>
  27. #include <linux/mm.h>
  28. #include <linux/highmem.h>
  29. #include <linux/hardirq.h>
  30. #include <linux/notifier.h>
  31. #include <linux/reboot.h>
  32. #include <linux/hash.h>
  33. #include <linux/sched.h>
  34. #include <linux/slab.h>
  35. #include <linux/kprobes.h>
  36. #include <linux/debugfs.h>
  37. #include <asm/timer.h>
  38. #include <asm/cpu.h>
  39. #include <asm/traps.h>
  40. #include <asm/desc.h>
  41. #include <asm/tlbflush.h>
  42. #include <asm/idle.h>
  43. #include <asm/apic.h>
  44. #include <asm/apicdef.h>
  45. #include <asm/hypervisor.h>
  46. #include <asm/kvm_guest.h>
  47. static int kvmapf = 1;
  48. static int parse_no_kvmapf(char *arg)
  49. {
  50. kvmapf = 0;
  51. return 0;
  52. }
  53. early_param("no-kvmapf", parse_no_kvmapf);
  54. static int steal_acc = 1;
  55. static int parse_no_stealacc(char *arg)
  56. {
  57. steal_acc = 0;
  58. return 0;
  59. }
  60. early_param("no-steal-acc", parse_no_stealacc);
  61. static int kvmclock_vsyscall = 1;
  62. static int parse_no_kvmclock_vsyscall(char *arg)
  63. {
  64. kvmclock_vsyscall = 0;
  65. return 0;
  66. }
  67. early_param("no-kvmclock-vsyscall", parse_no_kvmclock_vsyscall);
  68. static DEFINE_PER_CPU(struct kvm_vcpu_pv_apf_data, apf_reason) __aligned(64);
  69. static DEFINE_PER_CPU(struct kvm_steal_time, steal_time) __aligned(64);
  70. static int has_steal_clock = 0;
  71. /*
  72. * No need for any "IO delay" on KVM
  73. */
  74. static void kvm_io_delay(void)
  75. {
  76. }
  77. #define KVM_TASK_SLEEP_HASHBITS 8
  78. #define KVM_TASK_SLEEP_HASHSIZE (1<<KVM_TASK_SLEEP_HASHBITS)
  79. struct kvm_task_sleep_node {
  80. struct hlist_node link;
  81. wait_queue_head_t wq;
  82. u32 token;
  83. int cpu;
  84. bool halted;
  85. };
  86. static struct kvm_task_sleep_head {
  87. spinlock_t lock;
  88. struct hlist_head list;
  89. } async_pf_sleepers[KVM_TASK_SLEEP_HASHSIZE];
  90. static struct kvm_task_sleep_node *_find_apf_task(struct kvm_task_sleep_head *b,
  91. u32 token)
  92. {
  93. struct hlist_node *p;
  94. hlist_for_each(p, &b->list) {
  95. struct kvm_task_sleep_node *n =
  96. hlist_entry(p, typeof(*n), link);
  97. if (n->token == token)
  98. return n;
  99. }
  100. return NULL;
  101. }
  102. void kvm_async_pf_task_wait(u32 token)
  103. {
  104. u32 key = hash_32(token, KVM_TASK_SLEEP_HASHBITS);
  105. struct kvm_task_sleep_head *b = &async_pf_sleepers[key];
  106. struct kvm_task_sleep_node n, *e;
  107. DEFINE_WAIT(wait);
  108. rcu_irq_enter();
  109. spin_lock(&b->lock);
  110. e = _find_apf_task(b, token);
  111. if (e) {
  112. /* dummy entry exist -> wake up was delivered ahead of PF */
  113. hlist_del(&e->link);
  114. kfree(e);
  115. spin_unlock(&b->lock);
  116. rcu_irq_exit();
  117. return;
  118. }
  119. n.token = token;
  120. n.cpu = smp_processor_id();
  121. n.halted = is_idle_task(current) || preempt_count() > 1;
  122. init_waitqueue_head(&n.wq);
  123. hlist_add_head(&n.link, &b->list);
  124. spin_unlock(&b->lock);
  125. for (;;) {
  126. if (!n.halted)
  127. prepare_to_wait(&n.wq, &wait, TASK_UNINTERRUPTIBLE);
  128. if (hlist_unhashed(&n.link))
  129. break;
  130. if (!n.halted) {
  131. local_irq_enable();
  132. schedule();
  133. local_irq_disable();
  134. } else {
  135. /*
  136. * We cannot reschedule. So halt.
  137. */
  138. rcu_irq_exit();
  139. native_safe_halt();
  140. rcu_irq_enter();
  141. local_irq_disable();
  142. }
  143. }
  144. if (!n.halted)
  145. finish_wait(&n.wq, &wait);
  146. rcu_irq_exit();
  147. return;
  148. }
  149. EXPORT_SYMBOL_GPL(kvm_async_pf_task_wait);
  150. static void apf_task_wake_one(struct kvm_task_sleep_node *n)
  151. {
  152. hlist_del_init(&n->link);
  153. if (n->halted)
  154. smp_send_reschedule(n->cpu);
  155. else if (waitqueue_active(&n->wq))
  156. wake_up(&n->wq);
  157. }
  158. static void apf_task_wake_all(void)
  159. {
  160. int i;
  161. for (i = 0; i < KVM_TASK_SLEEP_HASHSIZE; i++) {
  162. struct hlist_node *p, *next;
  163. struct kvm_task_sleep_head *b = &async_pf_sleepers[i];
  164. spin_lock(&b->lock);
  165. hlist_for_each_safe(p, next, &b->list) {
  166. struct kvm_task_sleep_node *n =
  167. hlist_entry(p, typeof(*n), link);
  168. if (n->cpu == smp_processor_id())
  169. apf_task_wake_one(n);
  170. }
  171. spin_unlock(&b->lock);
  172. }
  173. }
  174. void kvm_async_pf_task_wake(u32 token)
  175. {
  176. u32 key = hash_32(token, KVM_TASK_SLEEP_HASHBITS);
  177. struct kvm_task_sleep_head *b = &async_pf_sleepers[key];
  178. struct kvm_task_sleep_node *n;
  179. if (token == ~0) {
  180. apf_task_wake_all();
  181. return;
  182. }
  183. again:
  184. spin_lock(&b->lock);
  185. n = _find_apf_task(b, token);
  186. if (!n) {
  187. /*
  188. * async PF was not yet handled.
  189. * Add dummy entry for the token.
  190. */
  191. n = kzalloc(sizeof(*n), GFP_ATOMIC);
  192. if (!n) {
  193. /*
  194. * Allocation failed! Busy wait while other cpu
  195. * handles async PF.
  196. */
  197. spin_unlock(&b->lock);
  198. cpu_relax();
  199. goto again;
  200. }
  201. n->token = token;
  202. n->cpu = smp_processor_id();
  203. init_waitqueue_head(&n->wq);
  204. hlist_add_head(&n->link, &b->list);
  205. } else
  206. apf_task_wake_one(n);
  207. spin_unlock(&b->lock);
  208. return;
  209. }
  210. EXPORT_SYMBOL_GPL(kvm_async_pf_task_wake);
  211. u32 kvm_read_and_reset_pf_reason(void)
  212. {
  213. u32 reason = 0;
  214. if (__get_cpu_var(apf_reason).enabled) {
  215. reason = __get_cpu_var(apf_reason).reason;
  216. __get_cpu_var(apf_reason).reason = 0;
  217. }
  218. return reason;
  219. }
  220. EXPORT_SYMBOL_GPL(kvm_read_and_reset_pf_reason);
  221. dotraplinkage void __kprobes
  222. do_async_page_fault(struct pt_regs *regs, unsigned long error_code)
  223. {
  224. enum ctx_state prev_state;
  225. switch (kvm_read_and_reset_pf_reason()) {
  226. default:
  227. do_page_fault(regs, error_code);
  228. break;
  229. case KVM_PV_REASON_PAGE_NOT_PRESENT:
  230. /* page is swapped out by the host. */
  231. prev_state = exception_enter();
  232. exit_idle();
  233. kvm_async_pf_task_wait((u32)read_cr2());
  234. exception_exit(prev_state);
  235. break;
  236. case KVM_PV_REASON_PAGE_READY:
  237. rcu_irq_enter();
  238. exit_idle();
  239. kvm_async_pf_task_wake((u32)read_cr2());
  240. rcu_irq_exit();
  241. break;
  242. }
  243. }
  244. static void __init paravirt_ops_setup(void)
  245. {
  246. pv_info.name = "KVM";
  247. pv_info.paravirt_enabled = 1;
  248. if (kvm_para_has_feature(KVM_FEATURE_NOP_IO_DELAY))
  249. pv_cpu_ops.io_delay = kvm_io_delay;
  250. #ifdef CONFIG_X86_IO_APIC
  251. no_timer_check = 1;
  252. #endif
  253. }
  254. static void kvm_register_steal_time(void)
  255. {
  256. int cpu = smp_processor_id();
  257. struct kvm_steal_time *st = &per_cpu(steal_time, cpu);
  258. if (!has_steal_clock)
  259. return;
  260. memset(st, 0, sizeof(*st));
  261. wrmsrl(MSR_KVM_STEAL_TIME, (slow_virt_to_phys(st) | KVM_MSR_ENABLED));
  262. pr_info("kvm-stealtime: cpu %d, msr %llx\n",
  263. cpu, (unsigned long long) slow_virt_to_phys(st));
  264. }
  265. static DEFINE_PER_CPU(unsigned long, kvm_apic_eoi) = KVM_PV_EOI_DISABLED;
  266. static void kvm_guest_apic_eoi_write(u32 reg, u32 val)
  267. {
  268. /**
  269. * This relies on __test_and_clear_bit to modify the memory
  270. * in a way that is atomic with respect to the local CPU.
  271. * The hypervisor only accesses this memory from the local CPU so
  272. * there's no need for lock or memory barriers.
  273. * An optimization barrier is implied in apic write.
  274. */
  275. if (__test_and_clear_bit(KVM_PV_EOI_BIT, &__get_cpu_var(kvm_apic_eoi)))
  276. return;
  277. apic_write(APIC_EOI, APIC_EOI_ACK);
  278. }
  279. void kvm_guest_cpu_init(void)
  280. {
  281. if (!kvm_para_available())
  282. return;
  283. if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF) && kvmapf) {
  284. u64 pa = slow_virt_to_phys(&__get_cpu_var(apf_reason));
  285. #ifdef CONFIG_PREEMPT
  286. pa |= KVM_ASYNC_PF_SEND_ALWAYS;
  287. #endif
  288. wrmsrl(MSR_KVM_ASYNC_PF_EN, pa | KVM_ASYNC_PF_ENABLED);
  289. __get_cpu_var(apf_reason).enabled = 1;
  290. printk(KERN_INFO"KVM setup async PF for cpu %d\n",
  291. smp_processor_id());
  292. }
  293. if (kvm_para_has_feature(KVM_FEATURE_PV_EOI)) {
  294. unsigned long pa;
  295. /* Size alignment is implied but just to make it explicit. */
  296. BUILD_BUG_ON(__alignof__(kvm_apic_eoi) < 4);
  297. __get_cpu_var(kvm_apic_eoi) = 0;
  298. pa = slow_virt_to_phys(&__get_cpu_var(kvm_apic_eoi))
  299. | KVM_MSR_ENABLED;
  300. wrmsrl(MSR_KVM_PV_EOI_EN, pa);
  301. }
  302. if (has_steal_clock)
  303. kvm_register_steal_time();
  304. }
  305. static void kvm_pv_disable_apf(void)
  306. {
  307. if (!__get_cpu_var(apf_reason).enabled)
  308. return;
  309. wrmsrl(MSR_KVM_ASYNC_PF_EN, 0);
  310. __get_cpu_var(apf_reason).enabled = 0;
  311. printk(KERN_INFO"Unregister pv shared memory for cpu %d\n",
  312. smp_processor_id());
  313. }
  314. static void kvm_pv_guest_cpu_reboot(void *unused)
  315. {
  316. /*
  317. * We disable PV EOI before we load a new kernel by kexec,
  318. * since MSR_KVM_PV_EOI_EN stores a pointer into old kernel's memory.
  319. * New kernel can re-enable when it boots.
  320. */
  321. if (kvm_para_has_feature(KVM_FEATURE_PV_EOI))
  322. wrmsrl(MSR_KVM_PV_EOI_EN, 0);
  323. kvm_pv_disable_apf();
  324. kvm_disable_steal_time();
  325. }
  326. static int kvm_pv_reboot_notify(struct notifier_block *nb,
  327. unsigned long code, void *unused)
  328. {
  329. if (code == SYS_RESTART)
  330. on_each_cpu(kvm_pv_guest_cpu_reboot, NULL, 1);
  331. return NOTIFY_DONE;
  332. }
  333. static struct notifier_block kvm_pv_reboot_nb = {
  334. .notifier_call = kvm_pv_reboot_notify,
  335. };
  336. static u64 kvm_steal_clock(int cpu)
  337. {
  338. u64 steal;
  339. struct kvm_steal_time *src;
  340. int version;
  341. src = &per_cpu(steal_time, cpu);
  342. do {
  343. version = src->version;
  344. rmb();
  345. steal = src->steal;
  346. rmb();
  347. } while ((version & 1) || (version != src->version));
  348. return steal;
  349. }
  350. void kvm_disable_steal_time(void)
  351. {
  352. if (!has_steal_clock)
  353. return;
  354. wrmsr(MSR_KVM_STEAL_TIME, 0, 0);
  355. }
  356. #ifdef CONFIG_SMP
  357. static void __init kvm_smp_prepare_boot_cpu(void)
  358. {
  359. WARN_ON(kvm_register_clock("primary cpu clock"));
  360. kvm_guest_cpu_init();
  361. native_smp_prepare_boot_cpu();
  362. kvm_spinlock_init();
  363. }
  364. static void kvm_guest_cpu_online(void *dummy)
  365. {
  366. kvm_guest_cpu_init();
  367. }
  368. static void kvm_guest_cpu_offline(void *dummy)
  369. {
  370. kvm_disable_steal_time();
  371. if (kvm_para_has_feature(KVM_FEATURE_PV_EOI))
  372. wrmsrl(MSR_KVM_PV_EOI_EN, 0);
  373. kvm_pv_disable_apf();
  374. apf_task_wake_all();
  375. }
  376. static int kvm_cpu_notify(struct notifier_block *self, unsigned long action,
  377. void *hcpu)
  378. {
  379. int cpu = (unsigned long)hcpu;
  380. switch (action) {
  381. case CPU_ONLINE:
  382. case CPU_DOWN_FAILED:
  383. case CPU_ONLINE_FROZEN:
  384. smp_call_function_single(cpu, kvm_guest_cpu_online, NULL, 0);
  385. break;
  386. case CPU_DOWN_PREPARE:
  387. case CPU_DOWN_PREPARE_FROZEN:
  388. smp_call_function_single(cpu, kvm_guest_cpu_offline, NULL, 1);
  389. break;
  390. default:
  391. break;
  392. }
  393. return NOTIFY_OK;
  394. }
  395. static struct notifier_block kvm_cpu_notifier = {
  396. .notifier_call = kvm_cpu_notify,
  397. };
  398. #endif
  399. static void __init kvm_apf_trap_init(void)
  400. {
  401. set_intr_gate(14, &async_page_fault);
  402. }
  403. void __init kvm_guest_init(void)
  404. {
  405. int i;
  406. if (!kvm_para_available())
  407. return;
  408. paravirt_ops_setup();
  409. register_reboot_notifier(&kvm_pv_reboot_nb);
  410. for (i = 0; i < KVM_TASK_SLEEP_HASHSIZE; i++)
  411. spin_lock_init(&async_pf_sleepers[i].lock);
  412. if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF))
  413. x86_init.irqs.trap_init = kvm_apf_trap_init;
  414. if (kvm_para_has_feature(KVM_FEATURE_STEAL_TIME)) {
  415. has_steal_clock = 1;
  416. pv_time_ops.steal_clock = kvm_steal_clock;
  417. }
  418. if (kvm_para_has_feature(KVM_FEATURE_PV_EOI))
  419. apic_set_eoi_write(kvm_guest_apic_eoi_write);
  420. if (kvmclock_vsyscall)
  421. kvm_setup_vsyscall_timeinfo();
  422. #ifdef CONFIG_SMP
  423. smp_ops.smp_prepare_boot_cpu = kvm_smp_prepare_boot_cpu;
  424. register_cpu_notifier(&kvm_cpu_notifier);
  425. #else
  426. kvm_guest_cpu_init();
  427. #endif
  428. }
  429. static uint32_t __init kvm_detect(void)
  430. {
  431. return kvm_cpuid_base();
  432. }
  433. const struct hypervisor_x86 x86_hyper_kvm __refconst = {
  434. .name = "KVM",
  435. .detect = kvm_detect,
  436. .x2apic_available = kvm_para_available,
  437. };
  438. EXPORT_SYMBOL_GPL(x86_hyper_kvm);
  439. static __init int activate_jump_labels(void)
  440. {
  441. if (has_steal_clock) {
  442. static_key_slow_inc(&paravirt_steal_enabled);
  443. if (steal_acc)
  444. static_key_slow_inc(&paravirt_steal_rq_enabled);
  445. }
  446. return 0;
  447. }
  448. arch_initcall(activate_jump_labels);
  449. #ifdef CONFIG_PARAVIRT_SPINLOCKS
  450. /* Kick a cpu by its apicid. Used to wake up a halted vcpu */
  451. static void kvm_kick_cpu(int cpu)
  452. {
  453. int apicid;
  454. unsigned long flags = 0;
  455. apicid = per_cpu(x86_cpu_to_apicid, cpu);
  456. kvm_hypercall2(KVM_HC_KICK_CPU, flags, apicid);
  457. }
  458. enum kvm_contention_stat {
  459. TAKEN_SLOW,
  460. TAKEN_SLOW_PICKUP,
  461. RELEASED_SLOW,
  462. RELEASED_SLOW_KICKED,
  463. NR_CONTENTION_STATS
  464. };
  465. #ifdef CONFIG_KVM_DEBUG_FS
  466. #define HISTO_BUCKETS 30
  467. static struct kvm_spinlock_stats
  468. {
  469. u32 contention_stats[NR_CONTENTION_STATS];
  470. u32 histo_spin_blocked[HISTO_BUCKETS+1];
  471. u64 time_blocked;
  472. } spinlock_stats;
  473. static u8 zero_stats;
  474. static inline void check_zero(void)
  475. {
  476. u8 ret;
  477. u8 old;
  478. old = ACCESS_ONCE(zero_stats);
  479. if (unlikely(old)) {
  480. ret = cmpxchg(&zero_stats, old, 0);
  481. /* This ensures only one fellow resets the stat */
  482. if (ret == old)
  483. memset(&spinlock_stats, 0, sizeof(spinlock_stats));
  484. }
  485. }
  486. static inline void add_stats(enum kvm_contention_stat var, u32 val)
  487. {
  488. check_zero();
  489. spinlock_stats.contention_stats[var] += val;
  490. }
  491. static inline u64 spin_time_start(void)
  492. {
  493. return sched_clock();
  494. }
  495. static void __spin_time_accum(u64 delta, u32 *array)
  496. {
  497. unsigned index;
  498. index = ilog2(delta);
  499. check_zero();
  500. if (index < HISTO_BUCKETS)
  501. array[index]++;
  502. else
  503. array[HISTO_BUCKETS]++;
  504. }
  505. static inline void spin_time_accum_blocked(u64 start)
  506. {
  507. u32 delta;
  508. delta = sched_clock() - start;
  509. __spin_time_accum(delta, spinlock_stats.histo_spin_blocked);
  510. spinlock_stats.time_blocked += delta;
  511. }
  512. static struct dentry *d_spin_debug;
  513. static struct dentry *d_kvm_debug;
  514. struct dentry *kvm_init_debugfs(void)
  515. {
  516. d_kvm_debug = debugfs_create_dir("kvm", NULL);
  517. if (!d_kvm_debug)
  518. printk(KERN_WARNING "Could not create 'kvm' debugfs directory\n");
  519. return d_kvm_debug;
  520. }
  521. static int __init kvm_spinlock_debugfs(void)
  522. {
  523. struct dentry *d_kvm;
  524. d_kvm = kvm_init_debugfs();
  525. if (d_kvm == NULL)
  526. return -ENOMEM;
  527. d_spin_debug = debugfs_create_dir("spinlocks", d_kvm);
  528. debugfs_create_u8("zero_stats", 0644, d_spin_debug, &zero_stats);
  529. debugfs_create_u32("taken_slow", 0444, d_spin_debug,
  530. &spinlock_stats.contention_stats[TAKEN_SLOW]);
  531. debugfs_create_u32("taken_slow_pickup", 0444, d_spin_debug,
  532. &spinlock_stats.contention_stats[TAKEN_SLOW_PICKUP]);
  533. debugfs_create_u32("released_slow", 0444, d_spin_debug,
  534. &spinlock_stats.contention_stats[RELEASED_SLOW]);
  535. debugfs_create_u32("released_slow_kicked", 0444, d_spin_debug,
  536. &spinlock_stats.contention_stats[RELEASED_SLOW_KICKED]);
  537. debugfs_create_u64("time_blocked", 0444, d_spin_debug,
  538. &spinlock_stats.time_blocked);
  539. debugfs_create_u32_array("histo_blocked", 0444, d_spin_debug,
  540. spinlock_stats.histo_spin_blocked, HISTO_BUCKETS + 1);
  541. return 0;
  542. }
  543. fs_initcall(kvm_spinlock_debugfs);
  544. #else /* !CONFIG_KVM_DEBUG_FS */
  545. static inline void add_stats(enum kvm_contention_stat var, u32 val)
  546. {
  547. }
  548. static inline u64 spin_time_start(void)
  549. {
  550. return 0;
  551. }
  552. static inline void spin_time_accum_blocked(u64 start)
  553. {
  554. }
  555. #endif /* CONFIG_KVM_DEBUG_FS */
  556. struct kvm_lock_waiting {
  557. struct arch_spinlock *lock;
  558. __ticket_t want;
  559. };
  560. /* cpus 'waiting' on a spinlock to become available */
  561. static cpumask_t waiting_cpus;
  562. /* Track spinlock on which a cpu is waiting */
  563. static DEFINE_PER_CPU(struct kvm_lock_waiting, klock_waiting);
  564. static void kvm_lock_spinning(struct arch_spinlock *lock, __ticket_t want)
  565. {
  566. struct kvm_lock_waiting *w;
  567. int cpu;
  568. u64 start;
  569. unsigned long flags;
  570. if (in_nmi())
  571. return;
  572. w = &__get_cpu_var(klock_waiting);
  573. cpu = smp_processor_id();
  574. start = spin_time_start();
  575. /*
  576. * Make sure an interrupt handler can't upset things in a
  577. * partially setup state.
  578. */
  579. local_irq_save(flags);
  580. /*
  581. * The ordering protocol on this is that the "lock" pointer
  582. * may only be set non-NULL if the "want" ticket is correct.
  583. * If we're updating "want", we must first clear "lock".
  584. */
  585. w->lock = NULL;
  586. smp_wmb();
  587. w->want = want;
  588. smp_wmb();
  589. w->lock = lock;
  590. add_stats(TAKEN_SLOW, 1);
  591. /*
  592. * This uses set_bit, which is atomic but we should not rely on its
  593. * reordering gurantees. So barrier is needed after this call.
  594. */
  595. cpumask_set_cpu(cpu, &waiting_cpus);
  596. barrier();
  597. /*
  598. * Mark entry to slowpath before doing the pickup test to make
  599. * sure we don't deadlock with an unlocker.
  600. */
  601. __ticket_enter_slowpath(lock);
  602. /*
  603. * check again make sure it didn't become free while
  604. * we weren't looking.
  605. */
  606. if (ACCESS_ONCE(lock->tickets.head) == want) {
  607. add_stats(TAKEN_SLOW_PICKUP, 1);
  608. goto out;
  609. }
  610. /*
  611. * halt until it's our turn and kicked. Note that we do safe halt
  612. * for irq enabled case to avoid hang when lock info is overwritten
  613. * in irq spinlock slowpath and no spurious interrupt occur to save us.
  614. */
  615. if (arch_irqs_disabled_flags(flags))
  616. halt();
  617. else
  618. safe_halt();
  619. out:
  620. cpumask_clear_cpu(cpu, &waiting_cpus);
  621. w->lock = NULL;
  622. local_irq_restore(flags);
  623. spin_time_accum_blocked(start);
  624. }
  625. PV_CALLEE_SAVE_REGS_THUNK(kvm_lock_spinning);
  626. /* Kick vcpu waiting on @lock->head to reach value @ticket */
  627. static void kvm_unlock_kick(struct arch_spinlock *lock, __ticket_t ticket)
  628. {
  629. int cpu;
  630. add_stats(RELEASED_SLOW, 1);
  631. for_each_cpu(cpu, &waiting_cpus) {
  632. const struct kvm_lock_waiting *w = &per_cpu(klock_waiting, cpu);
  633. if (ACCESS_ONCE(w->lock) == lock &&
  634. ACCESS_ONCE(w->want) == ticket) {
  635. add_stats(RELEASED_SLOW_KICKED, 1);
  636. kvm_kick_cpu(cpu);
  637. break;
  638. }
  639. }
  640. }
  641. /*
  642. * Setup pv_lock_ops to exploit KVM_FEATURE_PV_UNHALT if present.
  643. */
  644. void __init kvm_spinlock_init(void)
  645. {
  646. if (!kvm_para_available())
  647. return;
  648. /* Does host kernel support KVM_FEATURE_PV_UNHALT? */
  649. if (!kvm_para_has_feature(KVM_FEATURE_PV_UNHALT))
  650. return;
  651. printk(KERN_INFO "KVM setup paravirtual spinlock\n");
  652. static_key_slow_inc(&paravirt_ticketlocks_enabled);
  653. pv_lock_ops.lock_spinning = PV_CALLEE_SAVE(kvm_lock_spinning);
  654. pv_lock_ops.unlock_kick = kvm_unlock_kick;
  655. }
  656. #endif /* CONFIG_PARAVIRT_SPINLOCKS */