time.c 4.2 KB

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