Skip to content

Commit 0377999

Browse files
bibo-maochenhuacai
authored andcommitted
LoongArch: KVM: Add PV steal time support in guest side
Per-cpu struct kvm_steal_time is added here, its size is 64 bytes and also defined as 64 bytes, so that the whole structure is in one physical page. When a VCPU is online, function pv_enable_steal_time() is called. This function will pass guest physical address of struct kvm_steal_time and tells hypervisor to enable steal time. When a vcpu is offline, physical address is set as 0 and tells hypervisor to disable steal time. Here is an output of vmstat on guest when there is workload on both host and guest. It shows steal time stat information. procs -----------memory---------- -----io---- -system-- ------cpu----- r b swpd free inact active bi bo in cs us sy id wa st 15 1 0 7583616 184112 72208 20 0 162 52 31 6 43 0 20 17 0 0 7583616 184704 72192 0 0 6318 6885 5 60 8 5 22 16 0 0 7583616 185392 72144 0 0 1766 1081 0 49 0 1 50 16 0 0 7583616 184816 72304 0 0 6300 6166 4 62 12 2 20 18 0 0 7583632 184480 72240 0 0 2814 1754 2 58 4 1 35 Signed-off-by: Bibo Mao <[email protected]> Signed-off-by: Huacai Chen <[email protected]>
1 parent b4ba157 commit 0377999

File tree

5 files changed

+166
-3
lines changed

5 files changed

+166
-3
lines changed

Documentation/admin-guide/kernel-parameters.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4073,9 +4073,9 @@
40734073
prediction) vulnerability. System may allow data
40744074
leaks with this option.
40754075

4076-
no-steal-acc [X86,PV_OPS,ARM64,PPC/PSERIES,RISCV,EARLY] Disable
4077-
paravirtualized steal time accounting. steal time is
4078-
computed, but won't influence scheduler behaviour
4076+
no-steal-acc [X86,PV_OPS,ARM64,PPC/PSERIES,RISCV,LOONGARCH,EARLY]
4077+
Disable paravirtualized steal time accounting. steal time
4078+
is computed, but won't influence scheduler behaviour
40794079

40804080
nosync [HW,M68K] Disables sync negotiation for all devices.
40814081

arch/loongarch/Kconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,17 @@ config PARAVIRT
649649
over full virtualization. However, when run without a hypervisor
650650
the kernel is theoretically slower and slightly larger.
651651

652+
config PARAVIRT_TIME_ACCOUNTING
653+
bool "Paravirtual steal time accounting"
654+
depends on PARAVIRT
655+
help
656+
Select this option to enable fine granularity task steal time
657+
accounting. Time spent executing other tasks in parallel with
658+
the current vCPU is discounted from the vCPU power. To account for
659+
that, there can be a small performance impact.
660+
661+
If in doubt, say N here.
662+
652663
endmenu
653664

654665
config ARCH_SELECT_MEMORY_MODEL

arch/loongarch/include/asm/paravirt.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ static inline u64 paravirt_steal_clock(int cpu)
1818
}
1919

2020
int __init pv_ipi_init(void);
21+
int __init pv_time_init(void);
2122

2223
#else
2324

@@ -26,5 +27,9 @@ static inline int pv_ipi_init(void)
2627
return 0;
2728
}
2829

30+
static inline int pv_time_init(void)
31+
{
32+
return 0;
33+
}
2934
#endif // CONFIG_PARAVIRT
3035
#endif

arch/loongarch/kernel/paravirt.c

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44
#include <linux/interrupt.h>
55
#include <linux/jump_label.h>
66
#include <linux/kvm_para.h>
7+
#include <linux/reboot.h>
78
#include <linux/static_call.h>
89
#include <asm/paravirt.h>
910

11+
static int has_steal_clock;
1012
struct static_key paravirt_steal_enabled;
1113
struct static_key paravirt_steal_rq_enabled;
14+
static DEFINE_PER_CPU(struct kvm_steal_time, steal_time) __aligned(64);
1215

1316
static u64 native_steal_clock(int cpu)
1417
{
@@ -17,6 +20,34 @@ static u64 native_steal_clock(int cpu)
1720

1821
DEFINE_STATIC_CALL(pv_steal_clock, native_steal_clock);
1922

23+
static bool steal_acc = true;
24+
25+
static int __init parse_no_stealacc(char *arg)
26+
{
27+
steal_acc = false;
28+
return 0;
29+
}
30+
early_param("no-steal-acc", parse_no_stealacc);
31+
32+
static u64 paravt_steal_clock(int cpu)
33+
{
34+
int version;
35+
u64 steal;
36+
struct kvm_steal_time *src;
37+
38+
src = &per_cpu(steal_time, cpu);
39+
do {
40+
41+
version = src->version;
42+
virt_rmb(); /* Make sure that the version is read before the steal */
43+
steal = src->steal;
44+
virt_rmb(); /* Make sure that the steal is read before the next version */
45+
46+
} while ((version & 1) || (version != src->version));
47+
48+
return steal;
49+
}
50+
2051
#ifdef CONFIG_SMP
2152
static void pv_send_ipi_single(int cpu, unsigned int action)
2253
{
@@ -149,3 +180,117 @@ int __init pv_ipi_init(void)
149180

150181
return 0;
151182
}
183+
184+
static int pv_enable_steal_time(void)
185+
{
186+
int cpu = smp_processor_id();
187+
unsigned long addr;
188+
struct kvm_steal_time *st;
189+
190+
if (!has_steal_clock)
191+
return -EPERM;
192+
193+
st = &per_cpu(steal_time, cpu);
194+
addr = per_cpu_ptr_to_phys(st);
195+
196+
/* The whole structure kvm_steal_time should be in one page */
197+
if (PFN_DOWN(addr) != PFN_DOWN(addr + sizeof(*st))) {
198+
pr_warn("Illegal PV steal time addr %lx\n", addr);
199+
return -EFAULT;
200+
}
201+
202+
addr |= KVM_STEAL_PHYS_VALID;
203+
kvm_hypercall2(KVM_HCALL_FUNC_NOTIFY, KVM_FEATURE_STEAL_TIME, addr);
204+
205+
return 0;
206+
}
207+
208+
static void pv_disable_steal_time(void)
209+
{
210+
if (has_steal_clock)
211+
kvm_hypercall2(KVM_HCALL_FUNC_NOTIFY, KVM_FEATURE_STEAL_TIME, 0);
212+
}
213+
214+
#ifdef CONFIG_SMP
215+
static int pv_time_cpu_online(unsigned int cpu)
216+
{
217+
unsigned long flags;
218+
219+
local_irq_save(flags);
220+
pv_enable_steal_time();
221+
local_irq_restore(flags);
222+
223+
return 0;
224+
}
225+
226+
static int pv_time_cpu_down_prepare(unsigned int cpu)
227+
{
228+
unsigned long flags;
229+
230+
local_irq_save(flags);
231+
pv_disable_steal_time();
232+
local_irq_restore(flags);
233+
234+
return 0;
235+
}
236+
#endif
237+
238+
static void pv_cpu_reboot(void *unused)
239+
{
240+
pv_disable_steal_time();
241+
}
242+
243+
static int pv_reboot_notify(struct notifier_block *nb, unsigned long code, void *unused)
244+
{
245+
on_each_cpu(pv_cpu_reboot, NULL, 1);
246+
return NOTIFY_DONE;
247+
}
248+
249+
static struct notifier_block pv_reboot_nb = {
250+
.notifier_call = pv_reboot_notify,
251+
};
252+
253+
int __init pv_time_init(void)
254+
{
255+
int r, feature;
256+
257+
if (!cpu_has_hypervisor)
258+
return 0;
259+
if (!kvm_para_available())
260+
return 0;
261+
262+
feature = read_cpucfg(CPUCFG_KVM_FEATURE);
263+
if (!(feature & KVM_FEATURE_STEAL_TIME))
264+
return 0;
265+
266+
has_steal_clock = 1;
267+
r = pv_enable_steal_time();
268+
if (r < 0) {
269+
has_steal_clock = 0;
270+
return 0;
271+
}
272+
register_reboot_notifier(&pv_reboot_nb);
273+
274+
#ifdef CONFIG_SMP
275+
r = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN,
276+
"loongarch/pv_time:online",
277+
pv_time_cpu_online, pv_time_cpu_down_prepare);
278+
if (r < 0) {
279+
has_steal_clock = 0;
280+
pr_err("Failed to install cpu hotplug callbacks\n");
281+
return r;
282+
}
283+
#endif
284+
285+
static_call_update(pv_steal_clock, paravt_steal_clock);
286+
287+
static_key_slow_inc(&paravirt_steal_enabled);
288+
#ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
289+
if (steal_acc)
290+
static_key_slow_inc(&paravirt_steal_rq_enabled);
291+
#endif
292+
293+
pr_info("Using paravirt steal-time\n");
294+
295+
return 0;
296+
}

arch/loongarch/kernel/time.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include <asm/cpu-features.h>
1717
#include <asm/loongarch.h>
18+
#include <asm/paravirt.h>
1819
#include <asm/time.h>
1920

2021
u64 cpu_clock_freq;
@@ -214,4 +215,5 @@ void __init time_init(void)
214215

215216
constant_clockevent_init();
216217
constant_clocksource_init();
218+
pv_time_init();
217219
}

0 commit comments

Comments
 (0)