From: Sasha Levin <sashal@kernel.org>
To: patches@lists.linux.dev, stable@vger.kernel.org
Cc: Xianglai Li <lixianglai@loongson.cn>,
Huacai Chen <chenhuacai@loongson.cn>,
Sasha Levin <sashal@kernel.org>,
tglx@linutronix.de, peterz@infradead.org, chenhuacai@kernel.org,
jiaxun.yang@flygoat.com, kees@kernel.org,
linux-kernel@vger.kernel.org
Subject: [PATCH AUTOSEL 6.16] LoongArch: Add cpuhotplug hooks to fix high cpu usage of vCPU threads
Date: Mon, 25 Aug 2025 08:14:55 -0400 [thread overview]
Message-ID: <20250825121505.2983941-6-sashal@kernel.org> (raw)
In-Reply-To: <20250825121505.2983941-1-sashal@kernel.org>
From: Xianglai Li <lixianglai@loongson.cn>
[ Upstream commit 8ef7f3132e4005a103b382e71abea7ad01fbeb86 ]
When the CPU is offline, the timer of LoongArch is not correctly closed.
This is harmless for real machines, but resulting in an excessively high
cpu usage rate of the offline vCPU thread in the virtual machines.
To correctly close the timer, we have made the following modifications:
Register the cpu hotplug event (CPUHP_AP_LOONGARCH_ARCH_TIMER_STARTING)
for LoongArch. This event's hooks will be called to close the timer when
the CPU is offline.
Clear the timer interrupt when the timer is turned off. Since before the
timer is turned off, there may be a timer interrupt that has already 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>
Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
LLM Generated explanations, may be completely bogus:
Based on my analysis of the commit and its context, here is my
assessment:
**Backport Status: YES**
This commit should be backported to stable kernel trees for the
following reasons:
## Bug Fix for Real User Impact
1. **Fixes a concrete bug affecting virtual machines**: The commit
addresses a problem where offline vCPUs consume excessive CPU
resources in virtual machines. This is a real performance bug that
affects users running LoongArch VMs.
2. **Clear problem statement**: The commit message clearly describes the
issue - when a CPU is offlined, the LoongArch timer is not properly
disabled, leading to high CPU usage by offline vCPU threads in
virtual environments.
## Small and Contained Change
The commit is relatively small and well-contained:
- Adds ~20 lines of code for timer management hooks
- Registers CPU hotplug callbacks using existing infrastructure
(CPUHP_AP_LOONGARCH_ARCH_TIMER_STARTING)
- The changes are isolated to the LoongArch timer subsystem
## Follows Established Patterns
1. **Uses standard kernel infrastructure**: The fix properly uses the
cpuhotplug framework that other architectures already use (ARM, MIPS,
RISCV all have similar CPUHP_AP_*_TIMER_STARTING entries).
2. **Similar to previous fixes**: Commit 355170a7ecac ("LoongArch:
Implement constant timer shutdown interface") addressed a related
issue with timer shutdown, and this commit completes the proper timer
management during CPU hotplug.
## Minimal Risk of Regression
1. **Architecture-specific**: Changes are confined to LoongArch
architecture code, with no impact on other architectures.
2. **Clear timer interrupt handling**: The fix properly clears pending
timer interrupts when disabling the timer, preventing interrupt
storms.
3. **Protected by proper locking**: Uses existing state_lock for
synchronization.
## Virtual Machine Support is Important
With increasing use of virtualization, proper vCPU management is
critical for production environments. High CPU usage by offline vCPUs
can significantly impact VM performance and host resource utilization.
## Technical Correctness
The implementation correctly:
- Enables timer interrupts on CPU startup (`set_csr_ecfg(ECFGF_TIMER)`)
- Shuts down the timer on CPU dying (`constant_set_state_shutdown()`)
- Clears pending timer interrupts
(`write_csr_tintclear(CSR_TINTCLR_TI)`)
This is a straightforward bug fix that addresses a clear performance
issue in virtual machine environments without introducing new features
or architectural changes, making it an ideal candidate for stable
backport.
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..f3092f2de8b5 100644
--- a/arch/loongarch/kernel/time.c
+++ b/arch/loongarch/kernel/time.c
@@ -5,6 +5,7 @@
* Copyright (C) 2020-2022 Loongson Technology Corporation Limited
*/
#include <linux/clockchips.h>
+#include <linux/cpuhotplug.h>
#include <linux/delay.h>
#include <linux/export.h>
#include <linux/init.h>
@@ -102,6 +103,23 @@ static int constant_timer_next_event(unsigned long delta, struct clock_event_dev
return 0;
}
+static int arch_timer_starting(unsigned int cpu)
+{
+ set_csr_ecfg(ECFGF_TIMER);
+
+ return 0;
+}
+
+static int arch_timer_dying(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 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);
+
return 0;
}
diff --git a/include/linux/cpuhotplug.h b/include/linux/cpuhotplug.h
index df366ee15456..e62064cb9e08 100644
--- a/include/linux/cpuhotplug.h
+++ b/include/linux/cpuhotplug.h
@@ -169,6 +169,7 @@ enum cpuhp_state {
CPUHP_AP_QCOM_TIMER_STARTING,
CPUHP_AP_TEGRA_TIMER_STARTING,
CPUHP_AP_ARMADA_TIMER_STARTING,
+ CPUHP_AP_LOONGARCH_ARCH_TIMER_STARTING,
CPUHP_AP_MIPS_GIC_TIMER_STARTING,
CPUHP_AP_ARC_TIMER_STARTING,
CPUHP_AP_REALTEK_TIMER_STARTING,
--
2.50.1
next prev parent reply other threads:[~2025-08-25 12:15 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-25 12:14 [PATCH AUTOSEL 6.16-5.15] fs: writeback: fix use-after-free in __mark_inode_dirty() Sasha Levin
2025-08-25 12:14 ` [PATCH AUTOSEL 6.16-6.1] cdc_ncm: Flag Intel OEM version of Fibocom L850-GL as WWAN Sasha Levin
2025-08-25 12:14 ` [PATCH AUTOSEL 6.16-6.6] LoongArch: Save LBT before FPU in setup_sigcontext() Sasha Levin
2025-08-25 12:14 ` [PATCH AUTOSEL 6.16] btrfs: clear block dirty if submit_one_sector() failed Sasha Levin
2025-08-25 12:14 ` [PATCH AUTOSEL 6.16] platform/x86/amd: pmc: Drop SMU F/W match for Cezanne Sasha Levin
2025-08-25 12:14 ` Sasha Levin [this message]
2025-08-25 12:14 ` [PATCH AUTOSEL 6.16-6.12] btrfs: zoned: skip ZONE FINISH of conventional zones Sasha Levin
2025-08-25 12:14 ` [PATCH AUTOSEL 6.16-5.10] drm/amd/display: Don't warn when missing DCE encoder caps Sasha Levin
2025-08-25 12:14 ` [PATCH AUTOSEL 6.16-6.1] Bluetooth: hci_sync: Avoid adding default advertising on startup Sasha Levin
2025-08-25 12:14 ` [PATCH AUTOSEL 6.16-6.6] cpupower: Fix a bug where the -t option of the set subcommand was not working Sasha Levin
2025-08-25 12:15 ` [PATCH AUTOSEL 6.16-6.12] drm/rockchip: vop2: make vp registers nonvolatile Sasha Levin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250825121505.2983941-6-sashal@kernel.org \
--to=sashal@kernel.org \
--cc=chenhuacai@kernel.org \
--cc=chenhuacai@loongson.cn \
--cc=jiaxun.yang@flygoat.com \
--cc=kees@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lixianglai@loongson.cn \
--cc=patches@lists.linux.dev \
--cc=peterz@infradead.org \
--cc=stable@vger.kernel.org \
--cc=tglx@linutronix.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox