kvm.c 20 KB

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