From nobody Sat Oct 4 14:14:57 2025 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 21285299AA0; Fri, 15 Aug 2025 02:26:28 +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=1755224793; cv=none; b=edvlA75CHq689zMfVpnp3RAkid5RT38i9xVbFq0W4S51vJeD+tXIBV3WDCncPpMLt5xyvsdurlro9OkXddpxNUpAx4+16JDgyfx8mlr/t/xnUqSYN4ydJdvLCfsFO97H8tNexPKXenW2BVeoF5pNG7fIg2cM38JHNdZ5DiQH99U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755224793; c=relaxed/simple; bh=2y3ZmYKP2DQpMTLUSrPR8DMrijt1FqrsR52egAf4L44=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ray3o3hr9Vr3sIL2kvs4JxITIYAD9Ap4YmV6Ai/nL11oZ6q53BvvQGDFAS/tgexVPbCr4Pw6pOE8+Abmh7leZ/2aUZtjpkOHC8siaapaw3D6s4IzOJiA2vGu2QKd7QO4oXsfrom1BA2oAzFHE4/CnrfViufpsI36cBXuymei6VY= 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 _____8Bx7eLTmp5oSRpAAQ--.21080S3; Fri, 15 Aug 2025 10:26:27 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.213]) by front1 (Coremail) with SMTP id qMiowJAxQMLOmp5oMPRMAA--.26771S3; Fri, 15 Aug 2025 10:26:27 +0800 (CST) From: Bibo Mao To: Tianrui Zhao , Huacai Chen , Xianglai Li Cc: kvm@vger.kernel.org, loongarch@lists.linux.dev, linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: [PATCH v3 1/4] LoongArch: KVM: Fix stack protector issue in send_ipi_data() Date: Fri, 15 Aug 2025 10:26:18 +0800 Message-Id: <20250815022621.508174-2-maobibo@loongson.cn> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20250815022621.508174-1-maobibo@loongson.cn> References: <20250815022621.508174-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: qMiowJAxQMLOmp5oMPRMAA--.26771S3 X-CM-SenderInfo: xpdruxter6z05rqj20fqof0/ X-Coremail-Antispam: 1Uk129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7KY7 ZEXasCq-sGcSsGvfJ3UbIjqfuFe4nvWSU5nxnvy29KBjDU0xBIdaVrnUUvcSsGvfC2Kfnx nUUI43ZEXa7xR_UUUUUUUUU== Content-Type: text/plain; charset="utf-8" Function kvm_io_bus_read() is called in function send_ipi_data(), buffer size of parameter *val should be at least 8 bytes. Some emulation functions like loongarch_ipi_readl() and kvm_eiointc_read() will write buffer *val with 8 bytes signed extension regardless parameter len. Otherwise there will be buffer overflow issue when CONFIG_STACKPROTECTOR is enabled. The bug report is shown as follows: Kernel panic - not syncing: stack-protector: Kernel stack is corrupted in: = send_ipi_data+0x194/0x1a0 [kvm] CPU: 11 UID: 107 PID: 2692 Comm: CPU 0/KVM Not tainted 6.17.0-rc1+ #102 PRE= EMPT(full) Stack : 9000000005901568 0000000000000000 9000000003af371c 900000013c68c000 900000013c68f850 900000013c68f858 0000000000000000 900000013c68f998 900000013c68f990 900000013c68f990 900000013c68f6c0 fffffffffffdb058 fffffffffffdb0e0 900000013c68f858 911e1d4d39cf0ec2 9000000105657a00 0000000000000001 fffffffffffffffe 0000000000000578 282049464555206e 6f73676e6f6f4c20 0000000000000001 00000000086b4000 0000000000000000 0000000000000000 0000000000000000 9000000005709968 90000000058f9000 900000013c68fa68 900000013c68fab4 90000000029279f0 900000010153f940 900000010001f360 0000000000000000 9000000003af3734 000000004390000c 00000000000000b0 0000000000000004 0000000000000000 0000000000071c1d ... Call Trace: [<9000000003af3734>] show_stack+0x5c/0x180 [<9000000003aed168>] dump_stack_lvl+0x6c/0x9c [<9000000003ad0ab0>] vpanic+0x108/0x2c4 [<9000000003ad0ca8>] panic+0x3c/0x40 [<9000000004eb0a1c>] __stack_chk_fail+0x14/0x18 [] send_ipi_data+0x190/0x1a0 [kvm] [] __kvm_io_bus_write+0xa4/0xe8 [kvm] [] kvm_io_bus_write+0x54/0x90 [kvm] [] kvm_emu_iocsr+0x180/0x310 [kvm] [] kvm_handle_gspr+0x280/0x478 [kvm] [] kvm_handle_exit+0xc0/0x130 [kvm] Fixes: daee2f9cae551 ("LoongArch: KVM: Add IPI read and write function") Cc: stable@vger.kernel.org Signed-off-by: Bibo Mao --- arch/loongarch/kvm/intc/ipi.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/arch/loongarch/kvm/intc/ipi.c b/arch/loongarch/kvm/intc/ipi.c index e658d5b37c04..7925651d2ccf 100644 --- a/arch/loongarch/kvm/intc/ipi.c +++ b/arch/loongarch/kvm/intc/ipi.c @@ -99,7 +99,7 @@ static void write_mailbox(struct kvm_vcpu *vcpu, int offs= et, uint64_t data, int static int send_ipi_data(struct kvm_vcpu *vcpu, gpa_t addr, uint64_t data) { int i, idx, ret; - uint32_t val =3D 0, mask =3D 0; + uint64_t val =3D 0, mask =3D 0; =20 /* * Bit 27-30 is mask for byte writing. @@ -108,7 +108,7 @@ static int send_ipi_data(struct kvm_vcpu *vcpu, gpa_t a= ddr, uint64_t data) if ((data >> 27) & 0xf) { /* Read the old val */ idx =3D srcu_read_lock(&vcpu->kvm->srcu); - ret =3D kvm_io_bus_read(vcpu, KVM_IOCSR_BUS, addr, sizeof(val), &val); + ret =3D kvm_io_bus_read(vcpu, KVM_IOCSR_BUS, addr, 4, &val); srcu_read_unlock(&vcpu->kvm->srcu, idx); if (unlikely(ret)) { kvm_err("%s: : read data from addr %llx failed\n", __func__, addr); @@ -124,7 +124,7 @@ static int send_ipi_data(struct kvm_vcpu *vcpu, gpa_t a= ddr, uint64_t data) } val |=3D ((uint32_t)(data >> 32) & ~mask); idx =3D srcu_read_lock(&vcpu->kvm->srcu); - ret =3D kvm_io_bus_write(vcpu, KVM_IOCSR_BUS, addr, sizeof(val), &val); + ret =3D kvm_io_bus_write(vcpu, KVM_IOCSR_BUS, addr, 4, &val); srcu_read_unlock(&vcpu->kvm->srcu, idx); if (unlikely(ret)) kvm_err("%s: : write data to addr %llx failed\n", __func__, addr); --=20 2.39.3 From nobody Sat Oct 4 14:14:57 2025 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7F03129AB1A; Fri, 15 Aug 2025 02:26:29 +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=1755224792; cv=none; b=lxSYFBi1Hm/km8iPBO1cLK9SERZiBzzVQRrwRxiajGibatrEK2CaVt7DTX881h2USUyBsJgG3uZeXDLMj1d8eM3qkb/HPPI/0N/g151EW4dbgKan7SeNH1kpsIeRxyK2HdwvLUAvf1ddIpIYivgX0Ea6b913nU2m3uSCSYi2GNk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755224792; c=relaxed/simple; bh=N+/gd/Xj0nhzvrRWHgCyeX0pWQeceiAuB6zzwJu22M8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=u9TphhkHgkehzJ3EQm0y0EZiRafjhfVJBkKKhquqmmAvGM4xw/O8C1p4/dDdW3nYLA5A5UTN9GJ2tSSfJpyaOOr+xbU3mlkGtnsju1vQR5Ou1tcvCOx6mzRKBTaD1f53Gg2ihXx5varspAVYv5MbfP7YOD33FWAUUjbxKy5kev8= 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 _____8AxaeHTmp5oTBpAAQ--.13126S3; Fri, 15 Aug 2025 10:26:27 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.213]) by front1 (Coremail) with SMTP id qMiowJAxQMLOmp5oMPRMAA--.26771S4; Fri, 15 Aug 2025 10:26:27 +0800 (CST) From: Bibo Mao To: Tianrui Zhao , Huacai Chen , Xianglai Li Cc: kvm@vger.kernel.org, loongarch@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH v3 2/4] LoongArch: KVM: Access mailbox directly in mail_send() Date: Fri, 15 Aug 2025 10:26:19 +0800 Message-Id: <20250815022621.508174-3-maobibo@loongson.cn> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20250815022621.508174-1-maobibo@loongson.cn> References: <20250815022621.508174-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: qMiowJAxQMLOmp5oMPRMAA--.26771S4 X-CM-SenderInfo: xpdruxter6z05rqj20fqof0/ X-Coremail-Antispam: 1Uk129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7KY7 ZEXasCq-sGcSsGvfJ3UbIjqfuFe4nvWSU5nxnvy29KBjDU0xBIdaVrnUUvcSsGvfC2Kfnx nUUI43ZEXa7xR_UUUUUUUUU== Content-Type: text/plain; charset="utf-8" With function mail_send(), it is to write mailbox of other VCPUs. Existing simple APIs read_mailbox/write_mailbox can be used directly rather than send command on IOCSR address. Signed-off-by: Bibo Mao --- arch/loongarch/kvm/intc/ipi.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/arch/loongarch/kvm/intc/ipi.c b/arch/loongarch/kvm/intc/ipi.c index 7925651d2ccf..f27f79a0c1e0 100644 --- a/arch/loongarch/kvm/intc/ipi.c +++ b/arch/loongarch/kvm/intc/ipi.c @@ -134,7 +134,8 @@ static int send_ipi_data(struct kvm_vcpu *vcpu, gpa_t a= ddr, uint64_t data) =20 static int mail_send(struct kvm *kvm, uint64_t data) { - int cpu, mailbox, offset; + int i, cpu, mailbox, offset; + uint32_t val =3D 0, mask =3D 0; struct kvm_vcpu *vcpu; =20 cpu =3D ((data & 0xffffffff) >> 16) & 0x3ff; @@ -144,9 +145,18 @@ static int mail_send(struct kvm *kvm, uint64_t data) return -EINVAL; } mailbox =3D ((data & 0xffffffff) >> 2) & 0x7; - offset =3D IOCSR_IPI_BASE + IOCSR_IPI_BUF_20 + mailbox * 4; + offset =3D IOCSR_IPI_BUF_20 + mailbox * 4; + if ((data >> 27) & 0xf) { + val =3D read_mailbox(vcpu, offset, 4); + for (i =3D 0; i < 4; i++) + if (data & (BIT(27 + i))) + mask |=3D (0xff << (i * 8)); + val &=3D mask; + } =20 - return send_ipi_data(vcpu, offset, data); + val |=3D ((uint32_t)(data >> 32) & ~mask); + write_mailbox(vcpu, offset, val, 4); + return 0; } =20 static int any_send(struct kvm *kvm, uint64_t data) --=20 2.39.3 From nobody Sat Oct 4 14:14:57 2025 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by smtp.subspace.kernel.org (Postfix) with ESMTP id BFE2D29AB1E; Fri, 15 Aug 2025 02:26:29 +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=1755224792; cv=none; b=JLGVhGu8+8Q4+2PjgWvZ+wPJ6dKoHRmQn2aum9X5KQzyGUqOboGQZHNMotq8NPIfdRCMxU51UukZ0Lyzl0RzUnKfwU0KPyENzkIKyJ4UH2IB2KHVGiU1KI/XtNV5WTCNtd4IuVTA1L7BlguYOA4L9LuHbVBvnidD1Gz76G+9hTs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755224792; c=relaxed/simple; bh=ZT/ffdCHsehNGkIWfnqIZ4pIKQlR3t/N3pcp5Qz9V+s=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=VFr29fB7bA7XSF1R8smGDFDo857q2lxTR3YeR5d7kx2YDx583a3C7rqBvFL7C9wgtj7TTKCi+ogwMx/xh3p0CIE2HC/Rrig6UhPXVQ+nlbzXaqti80O1681bNyCh8APZhiU4VBergCqBXmT8bSbfv1aRfXgheWfoD4SGuDjn2Bg= 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 _____8AxQK3Ump5oTxpAAQ--.39058S3; Fri, 15 Aug 2025 10:26:28 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.213]) by front1 (Coremail) with SMTP id qMiowJAxQMLOmp5oMPRMAA--.26771S5; Fri, 15 Aug 2025 10:26:27 +0800 (CST) From: Bibo Mao To: Tianrui Zhao , Huacai Chen , Xianglai Li Cc: kvm@vger.kernel.org, loongarch@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH v3 3/4] LoongArch: KVM: Add implementation with IOCSR_IPI_SET Date: Fri, 15 Aug 2025 10:26:20 +0800 Message-Id: <20250815022621.508174-4-maobibo@loongson.cn> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20250815022621.508174-1-maobibo@loongson.cn> References: <20250815022621.508174-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: qMiowJAxQMLOmp5oMPRMAA--.26771S5 X-CM-SenderInfo: xpdruxter6z05rqj20fqof0/ X-Coremail-Antispam: 1Uk129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7KY7 ZEXasCq-sGcSsGvfJ3UbIjqfuFe4nvWSU5nxnvy29KBjDU0xBIdaVrnUUvcSsGvfC2Kfnx nUUI43ZEXa7xR_UUUUUUUUU== Content-Type: text/plain; charset="utf-8" IPI IOCSR register IOCSR_IPI_SET can send ipi interrupt to other vCPUs, also it can send interrupt to vCPU itself. Instead there is such operation on Linux such as arch_irq_work_raise(), it will send ipi message to vCPU itself. Here add implementation of write operation with IOCSR_IPI_SET register. Signed-off-by: Bibo Mao --- arch/loongarch/kvm/intc/ipi.c | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/arch/loongarch/kvm/intc/ipi.c b/arch/loongarch/kvm/intc/ipi.c index f27f79a0c1e0..3fb98abd9554 100644 --- a/arch/loongarch/kvm/intc/ipi.c +++ b/arch/loongarch/kvm/intc/ipi.c @@ -7,13 +7,26 @@ #include #include =20 -static void ipi_send(struct kvm *kvm, uint64_t data) +static void ipi_set(struct kvm_vcpu *vcpu, uint32_t data) { - int cpu, action; uint32_t status; - struct kvm_vcpu *vcpu; struct kvm_interrupt irq; =20 + spin_lock(&vcpu->arch.ipi_state.lock); + status =3D vcpu->arch.ipi_state.status; + vcpu->arch.ipi_state.status |=3D data; + spin_unlock(&vcpu->arch.ipi_state.lock); + if ((status =3D=3D 0) && data) { + irq.irq =3D LARCH_INT_IPI; + kvm_vcpu_ioctl_interrupt(vcpu, &irq); + } +} + +static void ipi_send(struct kvm *kvm, uint64_t data) +{ + int cpu; + struct kvm_vcpu *vcpu; + cpu =3D ((data & 0xffffffff) >> 16) & 0x3ff; vcpu =3D kvm_get_vcpu_by_cpuid(kvm, cpu); if (unlikely(vcpu =3D=3D NULL)) { @@ -21,15 +34,7 @@ static void ipi_send(struct kvm *kvm, uint64_t data) return; } =20 - action =3D BIT(data & 0x1f); - spin_lock(&vcpu->arch.ipi_state.lock); - status =3D vcpu->arch.ipi_state.status; - vcpu->arch.ipi_state.status |=3D action; - spin_unlock(&vcpu->arch.ipi_state.lock); - if (status =3D=3D 0) { - irq.irq =3D LARCH_INT_IPI; - kvm_vcpu_ioctl_interrupt(vcpu, &irq); - } + ipi_set(vcpu, BIT(data & 0x1f)); } =20 static void ipi_clear(struct kvm_vcpu *vcpu, uint64_t data) @@ -241,7 +246,7 @@ static int loongarch_ipi_writel(struct kvm_vcpu *vcpu, = gpa_t addr, int len, cons spin_unlock(&vcpu->arch.ipi_state.lock); break; case IOCSR_IPI_SET: - ret =3D -EINVAL; + ipi_set(vcpu, data); break; case IOCSR_IPI_CLEAR: /* Just clear the status of the current vcpu */ @@ -260,10 +265,10 @@ static int loongarch_ipi_writel(struct kvm_vcpu *vcpu= , gpa_t addr, int len, cons ipi_send(vcpu->kvm, data); break; case IOCSR_MAIL_SEND: - ret =3D mail_send(vcpu->kvm, *(uint64_t *)val); + ret =3D mail_send(vcpu->kvm, data); break; case IOCSR_ANY_SEND: - ret =3D any_send(vcpu->kvm, *(uint64_t *)val); + ret =3D any_send(vcpu->kvm, data); break; default: kvm_err("%s: unknown addr: %llx\n", __func__, addr); --=20 2.39.3 From nobody Sat Oct 4 14:14:57 2025 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9B0CA29B23B; Fri, 15 Aug 2025 02:26:32 +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=1755224794; cv=none; b=WDG/69iAsjxbqdQDy719u9Gq8Lonn03JLnAZSuDwfadJXuppdQyOk5EFdQKY0kKcpovEzk93/LVS1+VW3N40+nxaMS4R+axejrw44kWCX2ZkVVIddFOCtuUvPxfF/oOEGXgc/HwsAGUtJBGBEYjzD4GTArqZzBd548rmK1D+wX4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755224794; c=relaxed/simple; bh=PesLW11khJRpGmSVJfT+LbNggq4Xf0R0wDY67UVzXGI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=clLwzIG7gWjL3+bHfYfb7oMnZPdd4gwGXNJjXUcoUXzna8lyLx5BtZbLSgzgn9b6zkSylrV/U/tHDWT/BzHiJBG4dXSHB8YBzj5Ma42H+3giY1Z28bm7u2FSAMeQ2BkCtKmYjenJrn9qduksCVeVBVoxmTxzFwbz6/Qqh9SdAKY= 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 _____8AxQK3Ump5oUhpAAQ--.39061S3; Fri, 15 Aug 2025 10:26:28 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.213]) by front1 (Coremail) with SMTP id qMiowJAxQMLOmp5oMPRMAA--.26771S6; Fri, 15 Aug 2025 10:26:28 +0800 (CST) From: Bibo Mao To: Tianrui Zhao , Huacai Chen , Xianglai Li Cc: kvm@vger.kernel.org, loongarch@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH v3 4/4] LoongArch: KVM: Make function kvm_own_lbt() robust Date: Fri, 15 Aug 2025 10:26:21 +0800 Message-Id: <20250815022621.508174-5-maobibo@loongson.cn> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20250815022621.508174-1-maobibo@loongson.cn> References: <20250815022621.508174-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: qMiowJAxQMLOmp5oMPRMAA--.26771S6 X-CM-SenderInfo: xpdruxter6z05rqj20fqof0/ X-Coremail-Antispam: 1Uk129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7KY7 ZEXasCq-sGcSsGvfJ3UbIjqfuFe4nvWSU5nxnvy29KBjDU0xBIdaVrnUUvcSsGvfC2Kfnx nUUI43ZEXa7xR_UUUUUUUUU== Content-Type: text/plain; charset="utf-8" Add flag KVM_LARCH_LBT checking in function kvm_own_lbt(), so that it can be called safely rather than duplicated enabling again. Signed-off-by: Bibo Mao --- arch/loongarch/kvm/vcpu.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/arch/loongarch/kvm/vcpu.c b/arch/loongarch/kvm/vcpu.c index d1b8c50941ca..ce478151466c 100644 --- a/arch/loongarch/kvm/vcpu.c +++ b/arch/loongarch/kvm/vcpu.c @@ -1283,9 +1283,11 @@ int kvm_own_lbt(struct kvm_vcpu *vcpu) return -EINVAL; =20 preempt_disable(); - set_csr_euen(CSR_EUEN_LBTEN); - _restore_lbt(&vcpu->arch.lbt); - vcpu->arch.aux_inuse |=3D KVM_LARCH_LBT; + if (!(vcpu->arch.aux_inuse & KVM_LARCH_LBT)) { + set_csr_euen(CSR_EUEN_LBTEN); + _restore_lbt(&vcpu->arch.lbt); + vcpu->arch.aux_inuse |=3D KVM_LARCH_LBT; + } preempt_enable(); =20 return 0; --=20 2.39.3