time.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Xen stolen ticks accounting.
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/kernel_stat.h>
  7. #include <linux/math64.h>
  8. #include <linux/gfp.h>
  9. #include <linux/slab.h>
  10. #include <asm/paravirt.h>
  11. #include <asm/xen/hypervisor.h>
  12. #include <asm/xen/hypercall.h>
  13. #include <xen/events.h>
  14. #include <xen/features.h>
  15. #include <xen/interface/xen.h>
  16. #include <xen/interface/vcpu.h>
  17. #include <xen/xen-ops.h>
  18. /* runstate info updated by Xen */
  19. static DEFINE_PER_CPU(struct vcpu_runstate_info, xen_runstate);
  20. static DEFINE_PER_CPU(u64[4], old_runstate_time);
  21. /* return an consistent snapshot of 64-bit time/counter value */
  22. static u64 get64(const u64 *p)
  23. {
  24. u64 ret;
  25. if (BITS_PER_LONG < 64) {
  26. u32 *p32 = (u32 *)p;
  27. u32 h, l, h2;
  28. /*
  29. * Read high then low, and then make sure high is
  30. * still the same; this will only loop if low wraps
  31. * and carries into high.
  32. * XXX some clean way to make this endian-proof?
  33. */
  34. do {
  35. h = READ_ONCE(p32[1]);
  36. l = READ_ONCE(p32[0]);
  37. h2 = READ_ONCE(p32[1]);
  38. } while(h2 != h);
  39. ret = (((u64)h) << 32) | l;
  40. } else
  41. ret = READ_ONCE(*p);
  42. return ret;
  43. }
  44. static void xen_get_runstate_snapshot_cpu_delta(
  45. struct vcpu_runstate_info *res, unsigned int cpu)
  46. {
  47. u64 state_time;
  48. struct vcpu_runstate_info *state;
  49. BUG_ON(preemptible());
  50. state = per_cpu_ptr(&xen_runstate, cpu);
  51. do {
  52. state_time = get64(&state->state_entry_time);
  53. rmb(); /* Hypervisor might update data. */
  54. *res = READ_ONCE(*state);
  55. rmb(); /* Hypervisor might update data. */
  56. } while (get64(&state->state_entry_time) != state_time ||
  57. (state_time & XEN_RUNSTATE_UPDATE));
  58. }
  59. static void xen_get_runstate_snapshot_cpu(struct vcpu_runstate_info *res,
  60. unsigned int cpu)
  61. {
  62. int i;
  63. xen_get_runstate_snapshot_cpu_delta(res, cpu);
  64. for (i = 0; i < 4; i++)
  65. res->time[i] += per_cpu(old_runstate_time, cpu)[i];
  66. }
  67. void xen_manage_runstate_time(int action)
  68. {
  69. static struct vcpu_runstate_info *runstate_delta;
  70. struct vcpu_runstate_info state;
  71. int cpu, i;
  72. switch (action) {
  73. case -1: /* backup runstate time before suspend */
  74. if (unlikely(runstate_delta))
  75. pr_warn_once("%s: memory leak as runstate_delta is not NULL\n",
  76. __func__);
  77. runstate_delta = kmalloc_array(num_possible_cpus(),
  78. sizeof(*runstate_delta),
  79. GFP_ATOMIC);
  80. if (unlikely(!runstate_delta)) {
  81. pr_warn("%s: failed to allocate runstate_delta\n",
  82. __func__);
  83. return;
  84. }
  85. for_each_possible_cpu(cpu) {
  86. xen_get_runstate_snapshot_cpu_delta(&state, cpu);
  87. memcpy(runstate_delta[cpu].time, state.time,
  88. sizeof(runstate_delta[cpu].time));
  89. }
  90. break;
  91. case 0: /* backup runstate time after resume */
  92. if (unlikely(!runstate_delta)) {
  93. pr_warn("%s: cannot accumulate runstate time as runstate_delta is NULL\n",
  94. __func__);
  95. return;
  96. }
  97. for_each_possible_cpu(cpu) {
  98. for (i = 0; i < 4; i++)
  99. per_cpu(old_runstate_time, cpu)[i] +=
  100. runstate_delta[cpu].time[i];
  101. }
  102. break;
  103. default: /* do not accumulate runstate time for checkpointing */
  104. break;
  105. }
  106. if (action != -1 && runstate_delta) {
  107. kfree(runstate_delta);
  108. runstate_delta = NULL;
  109. }
  110. }
  111. /*
  112. * Runstate accounting
  113. */
  114. void xen_get_runstate_snapshot(struct vcpu_runstate_info *res)
  115. {
  116. xen_get_runstate_snapshot_cpu(res, smp_processor_id());
  117. }
  118. /* return true when a vcpu could run but has no real cpu to run on */
  119. bool xen_vcpu_stolen(int vcpu)
  120. {
  121. return per_cpu(xen_runstate, vcpu).state == RUNSTATE_runnable;
  122. }
  123. u64 xen_steal_clock(int cpu)
  124. {
  125. struct vcpu_runstate_info state;
  126. xen_get_runstate_snapshot_cpu(&state, cpu);
  127. return state.time[RUNSTATE_runnable] + state.time[RUNSTATE_offline];
  128. }
  129. void xen_setup_runstate_info(int cpu)
  130. {
  131. struct vcpu_register_runstate_memory_area area;
  132. area.addr.v = &per_cpu(xen_runstate, cpu);
  133. if (HYPERVISOR_vcpu_op(VCPUOP_register_runstate_memory_area,
  134. xen_vcpu_nr(cpu), &area))
  135. BUG();
  136. }
  137. void __init xen_time_setup_guest(void)
  138. {
  139. bool xen_runstate_remote;
  140. xen_runstate_remote = !HYPERVISOR_vm_assist(VMASST_CMD_enable,
  141. VMASST_TYPE_runstate_update_flag);
  142. pv_time_ops.steal_clock = xen_steal_clock;
  143. static_key_slow_inc(&paravirt_steal_enabled);
  144. if (xen_runstate_remote)
  145. static_key_slow_inc(&paravirt_steal_rq_enabled);
  146. }