* [PATCH V2] LoongArch: time: Fix the issue of high cpu usage of vcpu threads in virtual machines
@ 2025-08-20 2:15 Xianglai Li
2025-08-21 9:36 ` Huacai Chen
0 siblings, 1 reply; 2+ messages in thread
From: Xianglai Li @ 2025-08-20 2:15 UTC (permalink / raw)
To: Huacai Chen, WANG Xuerui, Thomas Gleixner, Peter Zijlstra,
Bibo Mao, Song Gao, Tianrui Zhao
Cc: loongarch, linux-kernel
When the cpu is offline, the timer under loongarch is not correctly closed,
resulting in an excessively high cpu usage rate of the offline vcpu thread
in the virtual machine.
To correctly close the timer, we have made the following modifications:
Register the cpu hotplug timer start event for loongarch.This event will
be called to close the timer when the cpu is offline.
Clear the timer interrupt when the timer is turned off, Because before the
timer is turned off, there may be a timer interrupt that has been in the
pending state due to the interruption of the disabled, which also affects
the halt state of the offline vcpu.
Signed-off-by: Xianglai Li <lixianglai@loongson.cn>
---
V1->V2:
1.Add the implementation of the function arch_timer_starting
2.Add more descriptions about clear timer interrupts in the commit message
3.Delete useless prints
4.Adjust the calling position of the function cpuhp_setup_state
5.Adjust the timer interrupt clear position
arch/loongarch/kernel/time.c | 22 ++++++++++++++++++++++
include/linux/cpuhotplug.h | 1 +
2 files changed, 23 insertions(+)
diff --git a/arch/loongarch/kernel/time.c b/arch/loongarch/kernel/time.c
index 367906b10f81..09b95f28bdcc 100644
--- a/arch/loongarch/kernel/time.c
+++ b/arch/loongarch/kernel/time.c
@@ -12,6 +12,7 @@
#include <linux/kernel.h>
#include <linux/sched_clock.h>
#include <linux/spinlock.h>
+#include <linux/cpu.h>
#include <asm/cpu-features.h>
#include <asm/loongarch.h>
@@ -102,6 +103,23 @@ static int constant_timer_next_event(unsigned long delta, struct clock_event_dev
return 0;
}
+static int arch_timer_dying_cpu(unsigned int cpu)
+{
+ constant_set_state_shutdown(this_cpu_ptr(&constant_clockevent_device));
+
+ /* Clear Timer Interrupt */
+ write_csr_tintclear(CSR_TINTCLR_TI);
+
+ return 0;
+}
+
+static int arch_timer_starting(unsigned int cpu)
+{
+ set_csr_ecfg(ECFGF_TIMER);
+
+ return 0;
+}
+
static unsigned long get_loops_per_jiffy(void)
{
unsigned long lpj = (unsigned long)const_clock_freq;
@@ -172,6 +190,10 @@ int constant_clockevent_init(void)
lpj_fine = get_loops_per_jiffy();
pr_info("Constant clock event device register\n");
+ cpuhp_setup_state(CPUHP_AP_LOONGARCH_ARCH_TIMER_STARTING,
+ "clockevents/loongarch/timer:starting",
+ arch_timer_starting, arch_timer_dying_cpu);
+
return 0;
}
diff --git a/include/linux/cpuhotplug.h b/include/linux/cpuhotplug.h
index edfa61d80702..6606c1546afc 100644
--- a/include/linux/cpuhotplug.h
+++ b/include/linux/cpuhotplug.h
@@ -159,6 +159,7 @@ enum cpuhp_state {
CPUHP_AP_PERF_ARM_STARTING,
CPUHP_AP_PERF_RISCV_STARTING,
CPUHP_AP_ARM_L2X0_STARTING,
+ CPUHP_AP_LOONGARCH_ARCH_TIMER_STARTING,
CPUHP_AP_EXYNOS4_MCT_TIMER_STARTING,
CPUHP_AP_ARM_ARCH_TIMER_STARTING,
CPUHP_AP_ARM_ARCH_TIMER_EVTSTRM_STARTING,
--
2.39.1
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH V2] LoongArch: time: Fix the issue of high cpu usage of vcpu threads in virtual machines
2025-08-20 2:15 [PATCH V2] LoongArch: time: Fix the issue of high cpu usage of vcpu threads in virtual machines Xianglai Li
@ 2025-08-21 9:36 ` Huacai Chen
0 siblings, 0 replies; 2+ messages in thread
From: Huacai Chen @ 2025-08-21 9:36 UTC (permalink / raw)
To: Xianglai Li
Cc: WANG Xuerui, Thomas Gleixner, Peter Zijlstra, Bibo Mao, Song Gao,
Tianrui Zhao, loongarch, linux-kernel
Applied, thanks.
Huacai
On Wed, Aug 20, 2025 at 10:39 AM Xianglai Li <lixianglai@loongson.cn> wrote:
>
> When the cpu is offline, the timer under loongarch is not correctly closed,
> resulting in an excessively high cpu usage rate of the offline vcpu thread
> in the virtual machine.
>
> To correctly close the timer, we have made the following modifications:
>
> Register the cpu hotplug timer start event for loongarch.This event will
> be called to close the timer when the cpu is offline.
>
> Clear the timer interrupt when the timer is turned off, Because before the
> timer is turned off, there may be a timer interrupt that has been in the
> pending state due to the interruption of the disabled, which also affects
> the halt state of the offline vcpu.
>
> Signed-off-by: Xianglai Li <lixianglai@loongson.cn>
> ---
> V1->V2:
> 1.Add the implementation of the function arch_timer_starting
> 2.Add more descriptions about clear timer interrupts in the commit message
> 3.Delete useless prints
> 4.Adjust the calling position of the function cpuhp_setup_state
> 5.Adjust the timer interrupt clear position
>
> arch/loongarch/kernel/time.c | 22 ++++++++++++++++++++++
> include/linux/cpuhotplug.h | 1 +
> 2 files changed, 23 insertions(+)
>
> diff --git a/arch/loongarch/kernel/time.c b/arch/loongarch/kernel/time.c
> index 367906b10f81..09b95f28bdcc 100644
> --- a/arch/loongarch/kernel/time.c
> +++ b/arch/loongarch/kernel/time.c
> @@ -12,6 +12,7 @@
> #include <linux/kernel.h>
> #include <linux/sched_clock.h>
> #include <linux/spinlock.h>
> +#include <linux/cpu.h>
>
> #include <asm/cpu-features.h>
> #include <asm/loongarch.h>
> @@ -102,6 +103,23 @@ static int constant_timer_next_event(unsigned long delta, struct clock_event_dev
> return 0;
> }
>
> +static int arch_timer_dying_cpu(unsigned int cpu)
> +{
> + constant_set_state_shutdown(this_cpu_ptr(&constant_clockevent_device));
> +
> + /* Clear Timer Interrupt */
> + write_csr_tintclear(CSR_TINTCLR_TI);
> +
> + return 0;
> +}
> +
> +static int arch_timer_starting(unsigned int cpu)
> +{
> + set_csr_ecfg(ECFGF_TIMER);
> +
> + return 0;
> +}
> +
> static unsigned long get_loops_per_jiffy(void)
> {
> unsigned long lpj = (unsigned long)const_clock_freq;
> @@ -172,6 +190,10 @@ int constant_clockevent_init(void)
> lpj_fine = get_loops_per_jiffy();
> pr_info("Constant clock event device register\n");
>
> + cpuhp_setup_state(CPUHP_AP_LOONGARCH_ARCH_TIMER_STARTING,
> + "clockevents/loongarch/timer:starting",
> + arch_timer_starting, arch_timer_dying_cpu);
> +
> return 0;
> }
>
> diff --git a/include/linux/cpuhotplug.h b/include/linux/cpuhotplug.h
> index edfa61d80702..6606c1546afc 100644
> --- a/include/linux/cpuhotplug.h
> +++ b/include/linux/cpuhotplug.h
> @@ -159,6 +159,7 @@ enum cpuhp_state {
> CPUHP_AP_PERF_ARM_STARTING,
> CPUHP_AP_PERF_RISCV_STARTING,
> CPUHP_AP_ARM_L2X0_STARTING,
> + CPUHP_AP_LOONGARCH_ARCH_TIMER_STARTING,
> CPUHP_AP_EXYNOS4_MCT_TIMER_STARTING,
> CPUHP_AP_ARM_ARCH_TIMER_STARTING,
> CPUHP_AP_ARM_ARCH_TIMER_EVTSTRM_STARTING,
> --
> 2.39.1
>
>
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2025-08-21 9:36 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-08-20 2:15 [PATCH V2] LoongArch: time: Fix the issue of high cpu usage of vcpu threads in virtual machines Xianglai Li
2025-08-21 9:36 ` Huacai Chen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).