From nobody Wed Apr 8 01:18:43 2026 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 30A492BEFFE; Wed, 11 Mar 2026 03:20:40 +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=1773199243; cv=none; b=agWMpo4Q/zo58TZi3PfduN49co854vDuS1LPo+k1N2zWn8iz7HTpCcm8vKF+J8V9ZDKsev5zfc96Ath5Ln2ij2WqpF+ywPBKfoG47ykoZGVCZwoQHMDijNoqOZpiRFD7DH9IbnZ0lVnug2Sy+qREYRRsRxczO8SoHD34p/BtPug= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773199243; c=relaxed/simple; bh=A0qi9Lk5pR2sBpzhCu/uXOaXgBENW3S95g4gW04dk9M=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=lvqHO2OZxslx9BFuX+JCkFDmS6ARis/FDrCLAWERCxQ5TCgenuvxNCKONy0GtS+QDZTni/Qigg+Cb4rDDpeEVlA0gJ/tBrh5R0el94XPRHhsA6ehAXRkLfu8K2vZnXPFWik3AkSKY7Pr62ifTjMpJUuRP1U1NtYkbpCfeko0a0k= 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 _____8BxksBz37BprMgZAA--.16366S3; Wed, 11 Mar 2026 11:20:19 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.213]) by front1 (Coremail) with SMTP id qMiowJCxWeAj37BpH69SAA--.26923S3; Wed, 11 Mar 2026 11:19:19 +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 1/3] LoongArch: KVM: Add kvm_request_pending checking in kvm_late_check_requests() Date: Wed, 11 Mar 2026 11:18:55 +0800 Message-Id: <20260311031857.3591470-2-maobibo@loongson.cn> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20260311031857.3591470-1-maobibo@loongson.cn> References: <20260311031857.3591470-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: qMiowJCxWeAj37BpH69SAA--.26923S3 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 09e137f2f841..6104982b53c8 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 Wed Apr 8 01:18:43 2026 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B1F152F6577; Wed, 11 Mar 2026 03:21:01 +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=1773199263; cv=none; b=LvyTo7EmG3ZZR8olvcizgug1Y5+uYBmuYDh0mZRD05Tcd9p+x+zeAz84AD2a7rGW0t20TjlOmD1ssfI0Iqa6uWK8l6A07gjuEKk0l47L6AlqGG8F2OskN+fubrf1Nvl+PS5DB5ZyowmiBm81e5VshbmaVCiCxSmStBgFE9Vg24Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773199263; c=relaxed/simple; bh=Y9kRhex0//f7baPvR8E6zVksePP0JGjPvBxZa/0Qey8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=mBz5/GWwZIcehFJ9KMnL+No/TIzxhNxJZKDNZ96DD28RLwx6u3uehLBRxEj1EuamTjHpGY2Oiq07BKu9hbW+txe01IIqpl+ZEWTmRawvL1Ql/z/6EU2VhoBoyjkRZ4DNwDaV5kJAR85ZyVXyvWLRT0MH3Vs8IOUdzc4xQaBTUiQ= 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 _____8DxMMCI37BpucgZAA--.16602S3; Wed, 11 Mar 2026 11:20:40 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.213]) by front1 (Coremail) with SMTP id qMiowJCxWeAj37BpH69SAA--.26923S4; Wed, 11 Mar 2026 11:19:39 +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 2/3] LoongArch: KVM: Move host CSR_EENTRY save in context switch Date: Wed, 11 Mar 2026 11:18:56 +0800 Message-Id: <20260311031857.3591470-3-maobibo@loongson.cn> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20260311031857.3591470-1-maobibo@loongson.cn> References: <20260311031857.3591470-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: qMiowJCxWeAj37BpH69SAA--.26923S4 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 6104982b53c8..044627c6ddc7 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 Wed Apr 8 01:18:43 2026 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1B62F2F6577; Wed, 11 Mar 2026 03:21:04 +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=1773199266; cv=none; b=hBiVcmMP+SDuzCvIIuxJnlJrJWer0bPTPHmreJdfNEHKNCjQw5Y+y1G5KUTGXtjLHczArKLltJRDP6ORFRnqvipebaldYt+eApC8SNQqKtkfwEl7MkQScWCRlLbYekqFpfLclcuJIfnqOgKP22JvN7dOy5OTs4v4oqn4+t6O+sM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773199266; c=relaxed/simple; bh=bqvzf7mRNZFCf4xu/2nV33f56CXvjRqYR5ZiY3bSIzA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=E2crnzvQxHeZI7aUcmbU5XC13vQLCI3V/rrXtD+0mzLPHToZ99hcCeUr/GWPG8aOEcSvYjby7THNytK9D3agdvA465U+HdE0LaESuzhqhDkzUAOk7f05nGbDqTeB3GJCVREdGxloiVpHWGyKsb30UIQifRPtbK9dTrBrbjMGInY= 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 _____8BxmsKc37Bp08gZAA--.10514S3; Wed, 11 Mar 2026 11:21:00 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.213]) by front1 (Coremail) with SMTP id qMiowJCxWeAj37BpH69SAA--.26923S5; Wed, 11 Mar 2026 11:20:00 +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 3/3] LoongArch: KVM: Set vcpu_is_preempted() macro rather than function Date: Wed, 11 Mar 2026 11:18:57 +0800 Message-Id: <20260311031857.3591470-4-maobibo@loongson.cn> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20260311031857.3591470-1-maobibo@loongson.cn> References: <20260311031857.3591470-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: qMiowJCxWeAj37BpH69SAA--.26923S5 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..305afd37cdcb 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 !!(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