From nobody Mon Apr 6 23:37:15 2026 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 46A6235F161; Tue, 17 Mar 2026 09:29:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=114.242.206.163 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773739765; cv=none; b=bzP7ilIdlaD82r0MZxhj0JgLitE2UNn5NxaNFHQ2ormYCQOx41kEQIfpSkq2qRjGvlLnecl1RQKSrgT1If4cztoJ10kvVyf1YGKJsWusabuUo6StCscxT5760TfaDoTqYLHv5NWrg4AMDm849Ypo9KKqRAe9DQLtc5DG3hajvkk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773739765; c=relaxed/simple; bh=omcBfFSmTGtKmubn8NNmh/ELlSQUrctCVpJbJ/JNpas=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=hqG9V/ytbGt3SqHqOd+gBshsY2cfiLbOj5DfC+1zZOq96AkagKYQGvas9yzrZs/7FYjNvkrNyltykg9gq9kEpULpEU/EJBaofACvtlOArTzMssGE945VmF6m80M9ovZjw3rfXEsZcnp2oCyQKNuruQXCrpoB3QPW/A5LGfO8N9k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=loongson.cn; spf=pass smtp.mailfrom=loongson.cn; arc=none smtp.client-ip=114.242.206.163 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=loongson.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=loongson.cn Received: from loongson.cn (unknown [10.2.5.213]) by gateway (Coremail) with SMTP id _____8Axz6vrHrlpROQbAA--.21767S3; Tue, 17 Mar 2026 17:29:15 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.213]) by front1 (Coremail) with SMTP id qMiowJCxHOHpHrlp3PlWAA--.35253S3; Tue, 17 Mar 2026 17:29:14 +0800 (CST) From: Bibo Mao To: Tianrui Zhao , Huacai Chen Cc: kernel@xen0n.name, kvm@vger.kernel.org, loongarch@lists.linux.dev, linux-kernel@vger.kernel.org, Juergen Gross Subject: [PATCH v2 1/3] LoongArch: KVM: Add kvm_request_pending checking in kvm_late_check_requests() Date: Tue, 17 Mar 2026 17:29:09 +0800 Message-Id: <20260317092911.1400248-2-maobibo@loongson.cn> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20260317092911.1400248-1-maobibo@loongson.cn> References: <20260317092911.1400248-1-maobibo@loongson.cn> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: qMiowJCxHOHpHrlp3PlWAA--.35253S3 X-CM-SenderInfo: xpdruxter6z05rqj20fqof0/ X-Coremail-Antispam: 1Uk129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7KY7 ZEXasCq-sGcSsGvfJ3UbIjqfuFe4nvWSU5nxnvy29KBjDU0xBIdaVrnUUvcSsGvfC2Kfnx nUUI43ZEXa7xR_UUUUUUUUU== Content-Type: text/plain; charset="utf-8" Add kvm_request_pending() checking firstly in kvm_late_check_requests(), at most time there is no pending request, the following pending bit checking can be skipped then. Also move function kvm_check_pmu() in kvm_late_check_requests(), and put it after kvm_request_pending() checking. Signed-off-by: Bibo Mao --- arch/loongarch/kvm/vcpu.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/arch/loongarch/kvm/vcpu.c b/arch/loongarch/kvm/vcpu.c index 8ffd50a470e6..028eb3d5a33b 100644 --- a/arch/loongarch/kvm/vcpu.c +++ b/arch/loongarch/kvm/vcpu.c @@ -149,14 +149,6 @@ static void kvm_lose_pmu(struct kvm_vcpu *vcpu) kvm_restore_host_pmu(vcpu); } =20 -static void kvm_check_pmu(struct kvm_vcpu *vcpu) -{ - if (kvm_check_request(KVM_REQ_PMU, vcpu)) { - kvm_own_pmu(vcpu); - vcpu->arch.aux_inuse |=3D KVM_LARCH_PMU; - } -} - static void kvm_update_stolen_time(struct kvm_vcpu *vcpu) { u32 version; @@ -232,6 +224,14 @@ static int kvm_check_requests(struct kvm_vcpu *vcpu) static void kvm_late_check_requests(struct kvm_vcpu *vcpu) { lockdep_assert_irqs_disabled(); + if (!kvm_request_pending(vcpu)) + return; + + if (kvm_check_request(KVM_REQ_PMU, vcpu)) { + kvm_own_pmu(vcpu); + vcpu->arch.aux_inuse |=3D KVM_LARCH_PMU; + } + if (kvm_check_request(KVM_REQ_TLB_FLUSH_GPA, vcpu)) if (vcpu->arch.flush_gpa !=3D INVALID_GPA) { kvm_flush_tlb_gpa(vcpu, vcpu->arch.flush_gpa); @@ -312,7 +312,6 @@ static int kvm_pre_enter_guest(struct kvm_vcpu *vcpu) /* Make sure the vcpu mode has been written */ smp_store_mb(vcpu->mode, IN_GUEST_MODE); kvm_check_vpid(vcpu); - kvm_check_pmu(vcpu); =20 /* * Called after function kvm_check_vpid() --=20 2.39.3 From nobody Mon Apr 6 23:37:15 2026 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 377FD35DA6B; Tue, 17 Mar 2026 09:29:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=114.242.206.163 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773739766; cv=none; b=rDuM0stCt7kpo7Q3UMxdsgxWNu6vPFU69xySx/g+lhIPvkCUkYf4JSsO+G9e6yRGOMeQ/W7YDrRNcTOyVD8RrmNJojc4X1EX4Vuzvv9Ydbc5NwbdwdY7g6gNfAJ4jOI7lNoqSX6tvCrXxhFkQkAtUbt4ZI9C7eLjya5Xg83fO/M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773739766; c=relaxed/simple; bh=CYX3G3ifnOGdRTffmuHm0LU74ney0de1dwmKeDVPgFY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=jR0LXoOmNkZgAe37FbHIRM5UBE+nCpiXtejVa508kT4ARcFRWWzuTUOIZE2e7fy2nL5bG+MFvxKNPMfiqrccyY1V2Mh7MDLb84SfzjBIaTdNrqnKDTwVOwBofEEYNv34dLRmg/hksFf4OZkqH4lmScVqDjEz11VmgNp5g0M/oNw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=loongson.cn; spf=pass smtp.mailfrom=loongson.cn; arc=none smtp.client-ip=114.242.206.163 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=loongson.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=loongson.cn Received: from loongson.cn (unknown [10.2.5.213]) by gateway (Coremail) with SMTP id _____8CxqsLsHrlpTOQbAA--.14701S3; Tue, 17 Mar 2026 17:29:16 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.213]) by front1 (Coremail) with SMTP id qMiowJCxHOHpHrlp3PlWAA--.35253S4; Tue, 17 Mar 2026 17:29:15 +0800 (CST) From: Bibo Mao To: Tianrui Zhao , Huacai Chen Cc: kernel@xen0n.name, kvm@vger.kernel.org, loongarch@lists.linux.dev, linux-kernel@vger.kernel.org, Juergen Gross Subject: [PATCH v2 2/3] LoongArch: KVM: Move host CSR_EENTRY save in context switch Date: Tue, 17 Mar 2026 17:29:10 +0800 Message-Id: <20260317092911.1400248-3-maobibo@loongson.cn> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20260317092911.1400248-1-maobibo@loongson.cn> References: <20260317092911.1400248-1-maobibo@loongson.cn> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: qMiowJCxHOHpHrlp3PlWAA--.35253S4 X-CM-SenderInfo: xpdruxter6z05rqj20fqof0/ X-Coremail-Antispam: 1Uk129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7KY7 ZEXasCq-sGcSsGvfJ3UbIjqfuFe4nvWSU5nxnvy29KBjDU0xBIdaVrnUUvcSsGvfC2Kfnx nUUI43ZEXa7xR_UUUUUUUUU== Content-Type: text/plain; charset="utf-8" CSR register LOONGARCH_CSR_EENTRY is shared between host cpu and guest vCPU, KVM need save and restore LOONGARCH_CSR_EENTRY register. Here move CSR register LOONGARCH_CSR_EENTRY saving in context switch function, rather than VM enter entry. At most time VM enter/exit is much more frequent than vCPU thread context switch. Signed-off-by: Bibo Mao --- arch/loongarch/kvm/vcpu.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/arch/loongarch/kvm/vcpu.c b/arch/loongarch/kvm/vcpu.c index 028eb3d5a33b..e82b2c84ce17 100644 --- a/arch/loongarch/kvm/vcpu.c +++ b/arch/loongarch/kvm/vcpu.c @@ -319,7 +319,6 @@ static int kvm_pre_enter_guest(struct kvm_vcpu *vcpu) * and it may also clear KVM_REQ_TLB_FLUSH_GPA pending bit */ kvm_late_check_requests(vcpu); - vcpu->arch.host_eentry =3D csr_read64(LOONGARCH_CSR_EENTRY); /* Clear KVM_LARCH_SWCSR_LATEST as CSR will change when enter guest */ vcpu->arch.aux_inuse &=3D ~KVM_LARCH_SWCSR_LATEST; =20 @@ -1624,9 +1623,11 @@ static int _kvm_vcpu_load(struct kvm_vcpu *vcpu, int= cpu) * If not, any old guest state from this vCPU will have been clobbered. */ context =3D per_cpu_ptr(vcpu->kvm->arch.vmcs, cpu); - if (migrated || (context->last_vcpu !=3D vcpu)) + if (migrated || (context->last_vcpu !=3D vcpu)) { vcpu->arch.aux_inuse &=3D ~KVM_LARCH_HWCSR_USABLE; - context->last_vcpu =3D vcpu; + context->last_vcpu =3D vcpu; + vcpu->arch.host_eentry =3D csr_read64(LOONGARCH_CSR_EENTRY); + } =20 /* Restore timer state regardless */ kvm_restore_timer(vcpu); --=20 2.39.3 From nobody Mon Apr 6 23:37:15 2026 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AC93D363088; Tue, 17 Mar 2026 09:29:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=114.242.206.163 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773739766; cv=none; b=LLbHHSbf8iDrHMkFfSW2jvljBgovkfXDWHJXvwVHn5bZ3K4lEIqbcFpHdOEGk/ar7hvz3Btwp66rSZUP82/ZUzntC1kmvbhmLAbvvcRh4lEow7bW2KDfM38s2s1iDY5J8OhZQnTh92NxtXjGTAjT9agqbK1nnj7sZ/D8+59bX6M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773739766; c=relaxed/simple; bh=W9/CppDDE8uS3FzshUKFhphbwsJQer6EAjKZ/ReuUWw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=fVVldFT6+LwsBI0XFa5fVcgpSdXaYyRvmKmpFrsbXveEPBCXI7alz3ftKM1uT8wmfaWh5LXDf9QOL2t/G131fZquax91owcvPK2gRuxMrlKU8/Gz4GFC2Wvm1zvS1W1BvdLZdzqBGLUrck6tg1ovbeXO39i14fDvBDx4sAbp1UM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=loongson.cn; spf=pass smtp.mailfrom=loongson.cn; arc=none smtp.client-ip=114.242.206.163 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=loongson.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=loongson.cn Received: from loongson.cn (unknown [10.2.5.213]) by gateway (Coremail) with SMTP id _____8BxUcDsHrlpTeQbAA--.21718S3; Tue, 17 Mar 2026 17:29:16 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.213]) by front1 (Coremail) with SMTP id qMiowJCxHOHpHrlp3PlWAA--.35253S5; Tue, 17 Mar 2026 17:29:15 +0800 (CST) From: Bibo Mao To: Tianrui Zhao , Huacai Chen Cc: kernel@xen0n.name, kvm@vger.kernel.org, loongarch@lists.linux.dev, linux-kernel@vger.kernel.org, Juergen Gross Subject: [PATCH v2 3/3] LoongArch: KVM: Set vcpu_is_preempted() macro rather than function Date: Tue, 17 Mar 2026 17:29:11 +0800 Message-Id: <20260317092911.1400248-4-maobibo@loongson.cn> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20260317092911.1400248-1-maobibo@loongson.cn> References: <20260317092911.1400248-1-maobibo@loongson.cn> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: qMiowJCxHOHpHrlp3PlWAA--.35253S5 X-CM-SenderInfo: xpdruxter6z05rqj20fqof0/ X-Coremail-Antispam: 1Uk129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7KY7 ZEXasCq-sGcSsGvfJ3UbIjqfuFe4nvWSU5nxnvy29KBjDU0xBIdaVrnUUvcSsGvfC2Kfnx nUUI43ZEXa7xR_UUUUUUUUU== Content-Type: text/plain; charset="utf-8" vcpu_is_preempted() is performance sensitive called in function osq_lock(), here set it as macro. So that parameter is not parsed at most time, it can avoid cache line thrashing across numa node. Here is part of unixbench result on 3C5000 DualWay machine with 32 Cores and 2 Numa node. origin with patch CONFIG_PARAVIRT disabled execl 6871.9 7134.2 7190.8 fstime 425.5 959.9 956.1 From the test result, with macro method it is almost the same with CONFIG_PARAVIRT disabled, and there is some improvment compared with function method. Signed-off-by: Bibo Mao --- arch/loongarch/include/asm/qspinlock.h | 27 +++++++++++++++++++++----- arch/loongarch/kernel/paravirt.c | 15 ++------------ 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/arch/loongarch/include/asm/qspinlock.h b/arch/loongarch/includ= e/asm/qspinlock.h index 66244801db67..b5d7a038faf1 100644 --- a/arch/loongarch/include/asm/qspinlock.h +++ b/arch/loongarch/include/asm/qspinlock.h @@ -5,8 +5,10 @@ #include =20 #ifdef CONFIG_PARAVIRT - +#include DECLARE_STATIC_KEY_FALSE(virt_spin_lock_key); +DECLARE_STATIC_KEY_FALSE(virt_preempt_key); +DECLARE_PER_CPU(struct kvm_steal_time, steal_time); =20 #define virt_spin_lock virt_spin_lock =20 @@ -34,10 +36,25 @@ static inline bool virt_spin_lock(struct qspinlock *loc= k) return true; } =20 -#define vcpu_is_preempted vcpu_is_preempted - -bool vcpu_is_preempted(int cpu); - +/* + * Macro is better than inline function here + * With inline function, parameter cpu is parsed even though it is not use= d. + * This may cause cache line thrashing across NUMA node. + * With macro method, parameter cpu is parsed only when it is used. + */ +#define vcpu_is_preempted(cpu) \ +({ \ + bool __val; \ + \ + if (!static_branch_unlikely(&virt_preempt_key)) \ + __val =3D false; \ + else { \ + struct kvm_steal_time *src; \ + src =3D &per_cpu(steal_time, cpu); \ + __val =3D !!(READ_ONCE(src->preempted) & KVM_VCPU_PREEMPTED); \ + } \ + __val; \ +}) #endif /* CONFIG_PARAVIRT */ =20 #include diff --git a/arch/loongarch/kernel/paravirt.c b/arch/loongarch/kernel/parav= irt.c index b74fe6db49ab..2d1206e486e2 100644 --- a/arch/loongarch/kernel/paravirt.c +++ b/arch/loongarch/kernel/paravirt.c @@ -10,8 +10,8 @@ #include =20 static int has_steal_clock; -static DEFINE_PER_CPU(struct kvm_steal_time, steal_time) __aligned(64); -static DEFINE_STATIC_KEY_FALSE(virt_preempt_key); +DEFINE_PER_CPU(struct kvm_steal_time, steal_time) __aligned(64); +DEFINE_STATIC_KEY_FALSE(virt_preempt_key); DEFINE_STATIC_KEY_FALSE(virt_spin_lock_key); =20 static bool steal_acc =3D true; @@ -261,17 +261,6 @@ static int pv_time_cpu_down_prepare(unsigned int cpu) return 0; } =20 -bool vcpu_is_preempted(int cpu) -{ - struct kvm_steal_time *src; - - if (!static_branch_unlikely(&virt_preempt_key)) - return false; - - src =3D &per_cpu(steal_time, cpu); - return !!(src->preempted & KVM_VCPU_PREEMPTED); -} -EXPORT_SYMBOL(vcpu_is_preempted); #endif =20 static void pv_cpu_reboot(void *unused) --=20 2.39.3