kvm.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  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 (__this_cpu_read(apf_reason.enabled)) {
  215. reason = __this_cpu_read(apf_reason.reason);
  216. __this_cpu_write(apf_reason.reason, 0);
  217. }
  218. return reason;
  219. }
  220. EXPORT_SYMBOL_GPL(kvm_read_and_reset_pf_reason);
  221. NOKPROBE_SYMBOL(kvm_read_and_reset_pf_reason);
  222. dotraplinkage void
  223. do_async_page_fault(struct pt_regs *regs, unsigned long error_code)
  224. {
  225. enum ctx_state prev_state;
  226. switch (kvm_read_and_reset_pf_reason()) {
  227. default:
  228. trace_do_page_fault(regs, error_code);
  229. break;
  230. case KVM_PV_REASON_PAGE_NOT_PRESENT:
  231. /* page is swapped out by the host. */
  232. prev_state = exception_enter();
  233. exit_idle();
  234. kvm_async_pf_task_wait((u32)read_cr2());
  235. exception_exit(prev_state);
  236. break;
  237. case KVM_PV_REASON_PAGE_READY:
  238. rcu_irq_enter();
  239. exit_idle();
  240. kvm_async_pf_task_wake((u32)read_cr2());
  241. rcu_irq_exit();
  242. break;
  243. }
  244. }
  245. NOKPROBE_SYMBOL(do_async_page_fault);
  246. static void __init paravirt_ops_setup(void)
  247. {
  248. pv_info.name = "KVM";
  249. pv_info.paravirt_enabled = 1;
  250. if (kvm_para_has_feature(KVM_FEATURE_NOP_IO_DELAY))
  251. pv_cpu_ops.io_delay = kvm_io_delay;
  252. #ifdef CONFIG_X86_IO_APIC
  253. no_timer_check = 1;
  254. #endif
  255. }
  256. static void kvm_register_steal_time(void)
  257. {
  258. int cpu = smp_processor_id();
  259. struct kvm_steal_time *st = &per_cpu(steal_time, cpu);
  260. if (!has_steal_clock)
  261. return;
  262. memset(st, 0, sizeof(*st));
  263. wrmsrl(MSR_KVM_STEAL_TIME, (slow_virt_to_phys(st) | KVM_MSR_ENABLED));
  264. pr_info("kvm-stealtime: cpu %d, msr %llx\n",
  265. cpu, (unsigned long long) slow_virt_to_phys(st));
  266. }
  267. static DEFINE_PER_CPU(unsigned long, kvm_apic_eoi) = KVM_PV_EOI_DISABLED;
  268. static void kvm_guest_apic_eoi_write(u32 reg, u32 val)
  269. {
  270. /**
  271. * This relies on __test_and_clear_bit to modify the memory
  272. * in a way that is atomic with respect to the local CPU.
  273. * The hypervisor only accesses this memory from the local CPU so
  274. * there's no need for lock or memory barriers.
  275. * An optimization barrier is implied in apic write.
  276. */
  277. if (__test_and_clear_bit(KVM_PV_EOI_BIT, this_cpu_ptr(&kvm_apic_eoi)))
  278. return;
  279. apic_write(APIC_EOI, APIC_EOI_ACK);
  280. }
  281. void kvm_guest_cpu_init(void)
  282. {
  283. if (!kvm_para_available())
  284. return;
  285. if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF) && kvmapf) {
  286. u64 pa = slow_virt_to_phys(this_cpu_ptr(&apf_reason));
  287. #ifdef CONFIG_PREEMPT
  288. pa |= KVM_ASYNC_PF_SEND_ALWAYS;
  289. #endif
  290. wrmsrl(MSR_KVM_ASYNC_PF_EN, pa | KVM_ASYNC_PF_ENABLED);
  291. __this_cpu_write(apf_reason.enabled, 1);
  292. printk(KERN_INFO"KVM setup async PF for cpu %d\n",
  293. smp_processor_id());
  294. }
  295. if (kvm_para_has_feature(KVM_FEATURE_PV_EOI)) {
  296. unsigned long pa;
  297. /* Size alignment is implied but just to make it explicit. */
  298. BUILD_BUG_ON(__alignof__(kvm_apic_eoi) < 4);
  299. __this_cpu_write(kvm_apic_eoi, 0);
  300. pa = slow_virt_to_phys(this_cpu_ptr(&kvm_apic_eoi))
  301. | KVM_MSR_ENABLED;
  302. wrmsrl(MSR_KVM_PV_EOI_EN, pa);
  303. }
  304. if (has_steal_clock)
  305. kvm_register_steal_time();
  306. }
  307. static void kvm_pv_disable_apf(void)
  308. {
  309. if (!__this_cpu_read(apf_reason.enabled))
  310. return;
  311. wrmsrl(MSR_KVM_ASYNC_PF_EN, 0);
  312. __this_cpu_write(apf_reason.enabled, 0);
  313. printk(KERN_INFO"Unregister pv shared memory for cpu %d\n",
  314. smp_processor_id());
  315. }
  316. static void kvm_pv_guest_cpu_reboot(void *unused)
  317. {
  318. /*
  319. * We disable PV EOI before we load a new kernel by kexec,
  320. * since MSR_KVM_PV_EOI_EN stores a pointer into old kernel's memory.
  321. * New kernel can re-enable when it boots.
  322. */
  323. if (kvm_para_has_feature(KVM_FEATURE_PV_EOI))
  324. wrmsrl(MSR_KVM_PV_EOI_EN, 0);
  325. kvm_pv_disable_apf();
  326. kvm_disable_steal_time();
  327. }
  328. static int kvm_pv_reboot_notify(struct notifier_block *nb,
  329. unsigned long code, void *unused)
  330. {
  331. if (code == SYS_RESTART)
  332. on_each_cpu(kvm_pv_guest_cpu_reboot, NULL, 1);
  333. return NOTIFY_DONE;
  334. }
  335. static struct notifier_block kvm_pv_reboot_nb = {
  336. .notifier_call = kvm_pv_reboot_notify,
  337. };
  338. static u64 kvm_steal_clock(int cpu)
  339. {
  340. u64 steal;
  341. struct kvm_steal_time *src;
  342. int version;
  343. src = &per_cpu(steal_time, cpu);
  344. do {
  345. version = src->version;
  346. rmb();
  347. steal = src->steal;
  348. rmb();
  349. } while ((version & 1) || (version != src->version));
  350. return steal;
  351. }
  352. void kvm_disable_steal_time(void)
  353. {
  354. if (!has_steal_clock)
  355. return;
  356. wrmsr(MSR_KVM_STEAL_TIME, 0, 0);
  357. }
  358. #ifdef CONFIG_SMP
  359. static void __init kvm_smp_prepare_boot_cpu(void)
  360. {
  361. kvm_guest_cpu_init();
  362. native_smp_prepare_boot_cpu();
  363. kvm_spinlock_init();
  364. }
  365. static void kvm_guest_cpu_online(void *dummy)
  366. {
  367. kvm_guest_cpu_init();
  368. }
  369. static void kvm_guest_cpu_offline(void *dummy)
  370. {
  371. kvm_disable_steal_time();
  372. if (kvm_para_has_feature(KVM_FEATURE_PV_EOI))
  373. wrmsrl(MSR_KVM_PV_EOI_EN, 0);
  374. kvm_pv_disable_apf();
  375. apf_task_wake_all();
  376. }
  377. static int kvm_cpu_notify(struct notifier_block *self, unsigned long action,
  378. void *hcpu)
  379. {
  380. int cpu = (unsigned long)hcpu;
  381. switch (action) {
  382. case CPU_ONLINE:
  383. case CPU_DOWN_FAILED:
  384. case CPU_ONLINE_FROZEN:
  385. smp_call_function_single(cpu, kvm_guest_cpu_online, NULL, 0);
  386. break;
  387. case CPU_DOWN_PREPARE:
  388. case CPU_DOWN_PREPARE_FROZEN:
  389. smp_call_function_single(cpu, kvm_guest_cpu_offline, NULL, 1);
  390. break;
  391. default:
  392. break;
  393. }
  394. return NOTIFY_OK;
  395. }
  396. static struct notifier_block kvm_cpu_notifier = {
  397. .notifier_call = kvm_cpu_notify,
  398. };
  399. #endif
  400. static void __init kvm_apf_trap_init(void)
  401. {
  402. set_intr_gate(14, async_page_fault);
  403. }
  404. void __init kvm_guest_init(void)
  405. {
  406. int i;
  407. if (!kvm_para_available())
  408. return;
  409. paravirt_ops_setup();
  410. register_reboot_notifier(&kvm_pv_reboot_nb);
  411. for (i = 0; i < KVM_TASK_SLEEP_HASHSIZE; i++)
  412. spin_lock_init(&async_pf_sleepers[i].lock);
  413. if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF))
  414. x86_init.irqs.trap_init = kvm_apf_trap_init;
  415. if (kvm_para_has_feature(KVM_FEATURE_STEAL_TIME)) {
  416. has_steal_clock = 1;
  417. pv_time_ops.steal_clock = kvm_steal_clock;
  418. }
  419. if (kvm_para_has_feature(KVM_FEATURE_PV_EOI))
  420. apic_set_eoi_write(kvm_guest_apic_eoi_write);
  421. if (kvmclock_vsyscall)
  422. kvm_setup_vsyscall_timeinfo();
  423. #ifdef CONFIG_SMP
  424. smp_ops.smp_prepare_boot_cpu = kvm_smp_prepare_boot_cpu;
  425. register_cpu_notifier(&kvm_cpu_notifier);
  426. #else
  427. kvm_guest_cpu_init();
  428. #endif
  429. }
  430. static noinline uint32_t __kvm_cpuid_base(void)
  431. {
  432. if (boot_cpu_data.cpuid_level < 0)
  433. return 0; /* So we don't blow up on old processors */
  434. if (cpu_has_hypervisor)
  435. return hypervisor_cpuid_base("KVMKVMKVM\0\0\0", 0);
  436. return 0;
  437. }
  438. static inline uint32_t kvm_cpuid_base(void)
  439. {
  440. static int kvm_cpuid_base = -1;
  441. if (kvm_cpuid_base == -1)
  442. kvm_cpuid_base = __kvm_cpuid_base();
  443. return kvm_cpuid_base;
  444. }
  445. bool kvm_para_available(void)
  446. {
  447. return kvm_cpuid_base() != 0;
  448. }
  449. EXPORT_SYMBOL_GPL(kvm_para_available);
  450. unsigned int kvm_arch_para_features(void)
  451. {
  452. return cpuid_eax(kvm_cpuid_base() | KVM_CPUID_FEATURES);
  453. }
  454. static uint32_t __init kvm_detect(void)
  455. {
  456. return kvm_cpuid_base();
  457. }
  458. const struct hypervisor_x86 x86_hyper_kvm __refconst = {
  459. .name = "KVM",
  460. .detect = kvm_detect,
  461. .x2apic_available = kvm_para_available,
  462. };
  463. EXPORT_SYMBOL_GPL(x86_hyper_kvm);
  464. static __init int activate_jump_labels(void)
  465. {
  466. if (has_steal_clock) {
  467. static_key_slow_inc(&paravirt_steal_enabled);
  468. if (steal_acc)
  469. static_key_slow_inc(&paravirt_steal_rq_enabled);
  470. }
  471. return 0;
  472. }
  473. arch_initcall(activate_jump_labels);
  474. #ifdef CONFIG_PARAVIRT_SPINLOCKS
  475. /* Kick a cpu by its apicid. Used to wake up a halted vcpu */
  476. static void kvm_kick_cpu(int cpu)
  477. {
  478. int apicid;
  479. unsigned long flags = 0;
  480. apicid = per_cpu(x86_cpu_to_apicid, cpu);
  481. kvm_hypercall2(KVM_HC_KICK_CPU, flags, apicid);
  482. }
  483. enum kvm_contention_stat {
  484. TAKEN_SLOW,
  485. TAKEN_SLOW_PICKUP,
  486. RELEASED_SLOW,
  487. RELEASED_SLOW_KICKED,
  488. NR_CONTENTION_STATS
  489. };
  490. #ifdef CONFIG_KVM_DEBUG_FS
  491. #define HISTO_BUCKETS 30
  492. static struct kvm_spinlock_stats
  493. {
  494. u32 contention_stats[NR_CONTENTION_STATS];
  495. u32 histo_spin_blocked[HISTO_BUCKETS+1];
  496. u64 time_blocked;
  497. } spinlock_stats;
  498. static u8 zero_stats;
  499. static inline void check_zero(void)
  500. {
  501. u8 ret;
  502. u8 old;
  503. old = ACCESS_ONCE(zero_stats);
  504. if (unlikely(old)) {
  505. ret = cmpxchg(&zero_stats, old, 0);
  506. /* This ensures only one fellow resets the stat */
  507. if (ret == old)
  508. memset(&spinlock_stats, 0, sizeof(spinlock_stats));
  509. }
  510. }
  511. static inline void add_stats(enum kvm_contention_stat var, u32 val)
  512. {
  513. check_zero();
  514. spinlock_stats.contention_stats[var] += val;
  515. }
  516. static inline u64 spin_time_start(void)
  517. {
  518. return sched_clock();
  519. }
  520. static void __spin_time_accum(u64 delta, u32 *array)
  521. {
  522. unsigned index;
  523. index = ilog2(delta);
  524. check_zero();
  525. if (index < HISTO_BUCKETS)
  526. array[index]++;
  527. else
  528. array[HISTO_BUCKETS]++;
  529. }
  530. static inline void spin_time_accum_blocked(u64 start)
  531. {
  532. u32 delta;
  533. delta = sched_clock() - start;
  534. __spin_time_accum(delta, spinlock_stats.histo_spin_blocked);
  535. spinlock_stats.time_blocked += delta;
  536. }
  537. static struct dentry *d_spin_debug;
  538. static struct dentry *d_kvm_debug;
  539. struct dentry *kvm_init_debugfs(void)
  540. {
  541. d_kvm_debug = debugfs_create_dir("kvm-guest", NULL);
  542. if (!d_kvm_debug)
  543. printk(KERN_WARNING "Could not create 'kvm' debugfs directory\n");
  544. return d_kvm_debug;
  545. }
  546. static int __init kvm_spinlock_debugfs(void)
  547. {
  548. struct dentry *d_kvm;
  549. d_kvm = kvm_init_debugfs();
  550. if (d_kvm == NULL)
  551. return -ENOMEM;
  552. d_spin_debug = debugfs_create_dir("spinlocks", d_kvm);
  553. debugfs_create_u8("zero_stats", 0644, d_spin_debug, &zero_stats);
  554. debugfs_create_u32("taken_slow", 0444, d_spin_debug,
  555. &spinlock_stats.contention_stats[TAKEN_SLOW]);
  556. debugfs_create_u32("taken_slow_pickup", 0444, d_spin_debug,
  557. &spinlock_stats.contention_stats[TAKEN_SLOW_PICKUP]);
  558. debugfs_create_u32("released_slow", 0444, d_spin_debug,
  559. &spinlock_stats.contention_stats[RELEASED_SLOW]);
  560. debugfs_create_u32("released_slow_kicked", 0444, d_spin_debug,
  561. &spinlock_stats.contention_stats[RELEASED_SLOW_KICKED]);
  562. debugfs_create_u64("time_blocked", 0444, d_spin_debug,
  563. &spinlock_stats.time_blocked);
  564. debugfs_create_u32_array("histo_blocked", 0444, d_spin_debug,
  565. spinlock_stats.histo_spin_blocked, HISTO_BUCKETS + 1);
  566. return 0;
  567. }
  568. fs_initcall(kvm_spinlock_debugfs);
  569. #else /* !CONFIG_KVM_DEBUG_FS */
  570. static inline void add_stats(enum kvm_contention_stat var, u32 val)
  571. {
  572. }
  573. static inline u64 spin_time_start(void)
  574. {
  575. return 0;
  576. }
  577. static inline void spin_time_accum_blocked(u64 start)
  578. {
  579. }
  580. #endif /* CONFIG_KVM_DEBUG_FS */
  581. struct kvm_lock_waiting {
  582. struct arch_spinlock *lock;
  583. __ticket_t want;
  584. };
  585. /* cpus 'waiting' on a spinlock to become available */
  586. static cpumask_t waiting_cpus;
  587. /* Track spinlock on which a cpu is waiting */
  588. static DEFINE_PER_CPU(struct kvm_lock_waiting, klock_waiting);
  589. __visible void kvm_lock_spinning(struct arch_spinlock *lock, __ticket_t want)
  590. {
  591. struct kvm_lock_waiting *w;
  592. int cpu;
  593. u64 start;
  594. unsigned long flags;
  595. if (in_nmi())
  596. return;
  597. w = this_cpu_ptr(&klock_waiting);
  598. cpu = smp_processor_id();
  599. start = spin_time_start();
  600. /*
  601. * Make sure an interrupt handler can't upset things in a
  602. * partially setup state.
  603. */
  604. local_irq_save(flags);
  605. /*
  606. * The ordering protocol on this is that the "lock" pointer
  607. * may only be set non-NULL if the "want" ticket is correct.
  608. * If we're updating "want", we must first clear "lock".
  609. */
  610. w->lock = NULL;
  611. smp_wmb();
  612. w->want = want;
  613. smp_wmb();
  614. w->lock = lock;
  615. add_stats(TAKEN_SLOW, 1);
  616. /*
  617. * This uses set_bit, which is atomic but we should not rely on its
  618. * reordering gurantees. So barrier is needed after this call.
  619. */
  620. cpumask_set_cpu(cpu, &waiting_cpus);
  621. barrier();
  622. /*
  623. * Mark entry to slowpath before doing the pickup test to make
  624. * sure we don't deadlock with an unlocker.
  625. */
  626. __ticket_enter_slowpath(lock);
  627. /*
  628. * check again make sure it didn't become free while
  629. * we weren't looking.
  630. */
  631. if (ACCESS_ONCE(lock->tickets.head) == want) {
  632. add_stats(TAKEN_SLOW_PICKUP, 1);
  633. goto out;
  634. }
  635. /*
  636. * halt until it's our turn and kicked. Note that we do safe halt
  637. * for irq enabled case to avoid hang when lock info is overwritten
  638. * in irq spinlock slowpath and no spurious interrupt occur to save us.
  639. */
  640. if (arch_irqs_disabled_flags(flags))
  641. halt();
  642. else
  643. safe_halt();
  644. out:
  645. cpumask_clear_cpu(cpu, &waiting_cpus);
  646. w->lock = NULL;
  647. local_irq_restore(flags);
  648. spin_time_accum_blocked(start);
  649. }
  650. PV_CALLEE_SAVE_REGS_THUNK(kvm_lock_spinning);
  651. /* Kick vcpu waiting on @lock->head to reach value @ticket */
  652. static void kvm_unlock_kick(struct arch_spinlock *lock, __ticket_t ticket)
  653. {
  654. int cpu;
  655. add_stats(RELEASED_SLOW, 1);
  656. for_each_cpu(cpu, &waiting_cpus) {
  657. const struct kvm_lock_waiting *w = &per_cpu(klock_waiting, cpu);
  658. if (ACCESS_ONCE(w->lock) == lock &&
  659. ACCESS_ONCE(w->want) == ticket) {
  660. add_stats(RELEASED_SLOW_KICKED, 1);
  661. kvm_kick_cpu(cpu);
  662. break;
  663. }
  664. }
  665. }
  666. /*
  667. * Setup pv_lock_ops to exploit KVM_FEATURE_PV_UNHALT if present.
  668. */
  669. void __init kvm_spinlock_init(void)
  670. {
  671. if (!kvm_para_available())
  672. return;
  673. /* Does host kernel support KVM_FEATURE_PV_UNHALT? */
  674. if (!kvm_para_has_feature(KVM_FEATURE_PV_UNHALT))
  675. return;
  676. pv_lock_ops.lock_spinning = PV_CALLEE_SAVE(kvm_lock_spinning);
  677. pv_lock_ops.unlock_kick = kvm_unlock_kick;
  678. }
  679. static __init int kvm_spinlock_init_jump(void)
  680. {
  681. if (!kvm_para_available())
  682. return 0;
  683. if (!kvm_para_has_feature(KVM_FEATURE_PV_UNHALT))
  684. return 0;
  685. static_key_slow_inc(&paravirt_ticketlocks_enabled);
  686. printk(KERN_INFO "KVM setup paravirtual spinlock\n");
  687. return 0;
  688. }
  689. early_initcall(kvm_spinlock_init_jump);
  690. #endif /* CONFIG_PARAVIRT_SPINLOCKS */