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