kvm.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  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. kvm_guest_cpu_init();
  360. native_smp_prepare_boot_cpu();
  361. kvm_spinlock_init();
  362. }
  363. static void kvm_guest_cpu_online(void *dummy)
  364. {
  365. kvm_guest_cpu_init();
  366. }
  367. static void kvm_guest_cpu_offline(void *dummy)
  368. {
  369. kvm_disable_steal_time();
  370. if (kvm_para_has_feature(KVM_FEATURE_PV_EOI))
  371. wrmsrl(MSR_KVM_PV_EOI_EN, 0);
  372. kvm_pv_disable_apf();
  373. apf_task_wake_all();
  374. }
  375. static int kvm_cpu_notify(struct notifier_block *self, unsigned long action,
  376. void *hcpu)
  377. {
  378. int cpu = (unsigned long)hcpu;
  379. switch (action) {
  380. case CPU_ONLINE:
  381. case CPU_DOWN_FAILED:
  382. case CPU_ONLINE_FROZEN:
  383. smp_call_function_single(cpu, kvm_guest_cpu_online, NULL, 0);
  384. break;
  385. case CPU_DOWN_PREPARE:
  386. case CPU_DOWN_PREPARE_FROZEN:
  387. smp_call_function_single(cpu, kvm_guest_cpu_offline, NULL, 1);
  388. break;
  389. default:
  390. break;
  391. }
  392. return NOTIFY_OK;
  393. }
  394. static struct notifier_block kvm_cpu_notifier = {
  395. .notifier_call = kvm_cpu_notify,
  396. };
  397. #endif
  398. static void __init kvm_apf_trap_init(void)
  399. {
  400. set_intr_gate(14, async_page_fault);
  401. }
  402. void __init kvm_guest_init(void)
  403. {
  404. int i;
  405. if (!kvm_para_available())
  406. return;
  407. paravirt_ops_setup();
  408. register_reboot_notifier(&kvm_pv_reboot_nb);
  409. for (i = 0; i < KVM_TASK_SLEEP_HASHSIZE; i++)
  410. spin_lock_init(&async_pf_sleepers[i].lock);
  411. if (kvm_para_has_feature(KVM_FEATURE_ASYNC_PF))
  412. x86_init.irqs.trap_init = kvm_apf_trap_init;
  413. if (kvm_para_has_feature(KVM_FEATURE_STEAL_TIME)) {
  414. has_steal_clock = 1;
  415. pv_time_ops.steal_clock = kvm_steal_clock;
  416. }
  417. if (kvm_para_has_feature(KVM_FEATURE_PV_EOI))
  418. apic_set_eoi_write(kvm_guest_apic_eoi_write);
  419. if (kvmclock_vsyscall)
  420. kvm_setup_vsyscall_timeinfo();
  421. #ifdef CONFIG_SMP
  422. smp_ops.smp_prepare_boot_cpu = kvm_smp_prepare_boot_cpu;
  423. register_cpu_notifier(&kvm_cpu_notifier);
  424. #else
  425. kvm_guest_cpu_init();
  426. #endif
  427. }
  428. static noinline uint32_t __kvm_cpuid_base(void)
  429. {
  430. if (boot_cpu_data.cpuid_level < 0)
  431. return 0; /* So we don't blow up on old processors */
  432. if (cpu_has_hypervisor)
  433. return hypervisor_cpuid_base("KVMKVMKVM\0\0\0", 0);
  434. return 0;
  435. }
  436. static inline uint32_t kvm_cpuid_base(void)
  437. {
  438. static int kvm_cpuid_base = -1;
  439. if (kvm_cpuid_base == -1)
  440. kvm_cpuid_base = __kvm_cpuid_base();
  441. return kvm_cpuid_base;
  442. }
  443. bool kvm_para_available(void)
  444. {
  445. return kvm_cpuid_base() != 0;
  446. }
  447. EXPORT_SYMBOL_GPL(kvm_para_available);
  448. unsigned int kvm_arch_para_features(void)
  449. {
  450. return cpuid_eax(kvm_cpuid_base() | KVM_CPUID_FEATURES);
  451. }
  452. static uint32_t __init kvm_detect(void)
  453. {
  454. return kvm_cpuid_base();
  455. }
  456. const struct hypervisor_x86 x86_hyper_kvm __refconst = {
  457. .name = "KVM",
  458. .detect = kvm_detect,
  459. .x2apic_available = kvm_para_available,
  460. };
  461. EXPORT_SYMBOL_GPL(x86_hyper_kvm);
  462. static __init int activate_jump_labels(void)
  463. {
  464. if (has_steal_clock) {
  465. static_key_slow_inc(&paravirt_steal_enabled);
  466. if (steal_acc)
  467. static_key_slow_inc(&paravirt_steal_rq_enabled);
  468. }
  469. return 0;
  470. }
  471. arch_initcall(activate_jump_labels);
  472. #ifdef CONFIG_PARAVIRT_SPINLOCKS
  473. /* Kick a cpu by its apicid. Used to wake up a halted vcpu */
  474. static void kvm_kick_cpu(int cpu)
  475. {
  476. int apicid;
  477. unsigned long flags = 0;
  478. apicid = per_cpu(x86_cpu_to_apicid, cpu);
  479. kvm_hypercall2(KVM_HC_KICK_CPU, flags, apicid);
  480. }
  481. enum kvm_contention_stat {
  482. TAKEN_SLOW,
  483. TAKEN_SLOW_PICKUP,
  484. RELEASED_SLOW,
  485. RELEASED_SLOW_KICKED,
  486. NR_CONTENTION_STATS
  487. };
  488. #ifdef CONFIG_KVM_DEBUG_FS
  489. #define HISTO_BUCKETS 30
  490. static struct kvm_spinlock_stats
  491. {
  492. u32 contention_stats[NR_CONTENTION_STATS];
  493. u32 histo_spin_blocked[HISTO_BUCKETS+1];
  494. u64 time_blocked;
  495. } spinlock_stats;
  496. static u8 zero_stats;
  497. static inline void check_zero(void)
  498. {
  499. u8 ret;
  500. u8 old;
  501. old = ACCESS_ONCE(zero_stats);
  502. if (unlikely(old)) {
  503. ret = cmpxchg(&zero_stats, old, 0);
  504. /* This ensures only one fellow resets the stat */
  505. if (ret == old)
  506. memset(&spinlock_stats, 0, sizeof(spinlock_stats));
  507. }
  508. }
  509. static inline void add_stats(enum kvm_contention_stat var, u32 val)
  510. {
  511. check_zero();
  512. spinlock_stats.contention_stats[var] += val;
  513. }
  514. static inline u64 spin_time_start(void)
  515. {
  516. return sched_clock();
  517. }
  518. static void __spin_time_accum(u64 delta, u32 *array)
  519. {
  520. unsigned index;
  521. index = ilog2(delta);
  522. check_zero();
  523. if (index < HISTO_BUCKETS)
  524. array[index]++;
  525. else
  526. array[HISTO_BUCKETS]++;
  527. }
  528. static inline void spin_time_accum_blocked(u64 start)
  529. {
  530. u32 delta;
  531. delta = sched_clock() - start;
  532. __spin_time_accum(delta, spinlock_stats.histo_spin_blocked);
  533. spinlock_stats.time_blocked += delta;
  534. }
  535. static struct dentry *d_spin_debug;
  536. static struct dentry *d_kvm_debug;
  537. struct dentry *kvm_init_debugfs(void)
  538. {
  539. d_kvm_debug = debugfs_create_dir("kvm-guest", NULL);
  540. if (!d_kvm_debug)
  541. printk(KERN_WARNING "Could not create 'kvm' debugfs directory\n");
  542. return d_kvm_debug;
  543. }
  544. static int __init kvm_spinlock_debugfs(void)
  545. {
  546. struct dentry *d_kvm;
  547. d_kvm = kvm_init_debugfs();
  548. if (d_kvm == NULL)
  549. return -ENOMEM;
  550. d_spin_debug = debugfs_create_dir("spinlocks", d_kvm);
  551. debugfs_create_u8("zero_stats", 0644, d_spin_debug, &zero_stats);
  552. debugfs_create_u32("taken_slow", 0444, d_spin_debug,
  553. &spinlock_stats.contention_stats[TAKEN_SLOW]);
  554. debugfs_create_u32("taken_slow_pickup", 0444, d_spin_debug,
  555. &spinlock_stats.contention_stats[TAKEN_SLOW_PICKUP]);
  556. debugfs_create_u32("released_slow", 0444, d_spin_debug,
  557. &spinlock_stats.contention_stats[RELEASED_SLOW]);
  558. debugfs_create_u32("released_slow_kicked", 0444, d_spin_debug,
  559. &spinlock_stats.contention_stats[RELEASED_SLOW_KICKED]);
  560. debugfs_create_u64("time_blocked", 0444, d_spin_debug,
  561. &spinlock_stats.time_blocked);
  562. debugfs_create_u32_array("histo_blocked", 0444, d_spin_debug,
  563. spinlock_stats.histo_spin_blocked, HISTO_BUCKETS + 1);
  564. return 0;
  565. }
  566. fs_initcall(kvm_spinlock_debugfs);
  567. #else /* !CONFIG_KVM_DEBUG_FS */
  568. static inline void add_stats(enum kvm_contention_stat var, u32 val)
  569. {
  570. }
  571. static inline u64 spin_time_start(void)
  572. {
  573. return 0;
  574. }
  575. static inline void spin_time_accum_blocked(u64 start)
  576. {
  577. }
  578. #endif /* CONFIG_KVM_DEBUG_FS */
  579. struct kvm_lock_waiting {
  580. struct arch_spinlock *lock;
  581. __ticket_t want;
  582. };
  583. /* cpus 'waiting' on a spinlock to become available */
  584. static cpumask_t waiting_cpus;
  585. /* Track spinlock on which a cpu is waiting */
  586. static DEFINE_PER_CPU(struct kvm_lock_waiting, klock_waiting);
  587. __visible void kvm_lock_spinning(struct arch_spinlock *lock, __ticket_t want)
  588. {
  589. struct kvm_lock_waiting *w;
  590. int cpu;
  591. u64 start;
  592. unsigned long flags;
  593. if (in_nmi())
  594. return;
  595. w = &__get_cpu_var(klock_waiting);
  596. cpu = smp_processor_id();
  597. start = spin_time_start();
  598. /*
  599. * Make sure an interrupt handler can't upset things in a
  600. * partially setup state.
  601. */
  602. local_irq_save(flags);
  603. /*
  604. * The ordering protocol on this is that the "lock" pointer
  605. * may only be set non-NULL if the "want" ticket is correct.
  606. * If we're updating "want", we must first clear "lock".
  607. */
  608. w->lock = NULL;
  609. smp_wmb();
  610. w->want = want;
  611. smp_wmb();
  612. w->lock = lock;
  613. add_stats(TAKEN_SLOW, 1);
  614. /*
  615. * This uses set_bit, which is atomic but we should not rely on its
  616. * reordering gurantees. So barrier is needed after this call.
  617. */
  618. cpumask_set_cpu(cpu, &waiting_cpus);
  619. barrier();
  620. /*
  621. * Mark entry to slowpath before doing the pickup test to make
  622. * sure we don't deadlock with an unlocker.
  623. */
  624. __ticket_enter_slowpath(lock);
  625. /*
  626. * check again make sure it didn't become free while
  627. * we weren't looking.
  628. */
  629. if (ACCESS_ONCE(lock->tickets.head) == want) {
  630. add_stats(TAKEN_SLOW_PICKUP, 1);
  631. goto out;
  632. }
  633. /*
  634. * halt until it's our turn and kicked. Note that we do safe halt
  635. * for irq enabled case to avoid hang when lock info is overwritten
  636. * in irq spinlock slowpath and no spurious interrupt occur to save us.
  637. */
  638. if (arch_irqs_disabled_flags(flags))
  639. halt();
  640. else
  641. safe_halt();
  642. out:
  643. cpumask_clear_cpu(cpu, &waiting_cpus);
  644. w->lock = NULL;
  645. local_irq_restore(flags);
  646. spin_time_accum_blocked(start);
  647. }
  648. PV_CALLEE_SAVE_REGS_THUNK(kvm_lock_spinning);
  649. /* Kick vcpu waiting on @lock->head to reach value @ticket */
  650. static void kvm_unlock_kick(struct arch_spinlock *lock, __ticket_t ticket)
  651. {
  652. int cpu;
  653. add_stats(RELEASED_SLOW, 1);
  654. for_each_cpu(cpu, &waiting_cpus) {
  655. const struct kvm_lock_waiting *w = &per_cpu(klock_waiting, cpu);
  656. if (ACCESS_ONCE(w->lock) == lock &&
  657. ACCESS_ONCE(w->want) == ticket) {
  658. add_stats(RELEASED_SLOW_KICKED, 1);
  659. kvm_kick_cpu(cpu);
  660. break;
  661. }
  662. }
  663. }
  664. /*
  665. * Setup pv_lock_ops to exploit KVM_FEATURE_PV_UNHALT if present.
  666. */
  667. void __init kvm_spinlock_init(void)
  668. {
  669. if (!kvm_para_available())
  670. return;
  671. /* Does host kernel support KVM_FEATURE_PV_UNHALT? */
  672. if (!kvm_para_has_feature(KVM_FEATURE_PV_UNHALT))
  673. return;
  674. pv_lock_ops.lock_spinning = PV_CALLEE_SAVE(kvm_lock_spinning);
  675. pv_lock_ops.unlock_kick = kvm_unlock_kick;
  676. }
  677. static __init int kvm_spinlock_init_jump(void)
  678. {
  679. if (!kvm_para_available())
  680. return 0;
  681. if (!kvm_para_has_feature(KVM_FEATURE_PV_UNHALT))
  682. return 0;
  683. static_key_slow_inc(&paravirt_ticketlocks_enabled);
  684. printk(KERN_INFO "KVM setup paravirtual spinlock\n");
  685. return 0;
  686. }
  687. early_initcall(kvm_spinlock_init_jump);
  688. #endif /* CONFIG_PARAVIRT_SPINLOCKS */