time.c 4.1 KB

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