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