All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v4 0/3] LoongArch: KVM: irqchip fixes
@ 2026-10-15 23:46 Tao Cui
  2026-10-15 23:46 ` [PATCH v4 1/3] LoongArch: KVM: Clear device pointer in irqchip destroy callbacks Tao Cui
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Tao Cui @ 2026-10-15 23:46 UTC (permalink / raw)
  To: chenhuacai, maobibo, zhaotianrui
  Cc: loongarch, kvm, linux-kernel, kernel, nagachaithanya9911, cui.tao,
	Tao Cui

From: Tao Cui <cuitao@kylinos.cn>

Hi,

Three fixes for the LoongArch KVM irqchip code:

- Patch 1 clears the device pointer in the destroy callbacks: when
  KVM_CREATE_DEVICE succeeds but the following fd allocation fails
  (e.g. under RLIMIT_NOFILE), ops->destroy() frees the irqchip while
  kvm->arch.* still points to it.

- Patch 2 aligns kvm_pch_pic_create() with kvm_eiointc_create() by
  propagating the real error code; the kvm_ipi_create() counterpart is
  being fixed separately (Chaithanya Lagisetty).

- Patch 3 rejects repeated PCH-PIC CTRL_INIT with -EEXIST, using the
  initialized state of the kvm_io_device as the indicator.  The check,
  the MMIO base update and the bus registration are serialized by
  slots_lock.

All patches carry Fixes tags.

Changes in v4:

- Patch 3: update pch_pic_base unconditionally, as suggested by
  Huacai.

Changes in v3:

- Drop "Load dmsintc pointer once in pch_msi_set_irq" (v2 patch 2),
  suggested by Bibo and Huacai.

- Patch 3 (v2 patch 4): reject repeated CTRL_INIT by checking the
  kvm_io_device state instead of a separate has_init flag, also
  suggested by Huacai; the check stays under slots_lock.

Changes in v2:

- Drop "Guard against NULL irqchip in irq injection" (v1 patch 2) and
  "Rebase steal time counter in vcpu context" (v1 patch 4) after
  review discussion: their trigger scenarios are not reachable
  through real VMM behaviour.

- Use the one-line destroy style in dmsintc suggested by Bibo.

- Track repeated PCH-PIC CTRL_INIT with a has_init flag and return
  -EEXIST instead of -EBUSY, also suggested by Bibo; move the check
  and base update under slots_lock so they are atomic.

- Add READ_ONCE() to the dmsintc pointer loads so the compiler
  keeps each of them a single load.

Tao Cui (3):
  LoongArch: KVM: Clear device pointer in irqchip destroy callbacks
  LoongArch: KVM: Propagate real error code in kvm_pch_pic_create
  LoongArch: KVM: Reject repeated PCH-PIC CTRL_INIT

 arch/loongarch/kvm/intc/dmsintc.c |  1 +
 arch/loongarch/kvm/intc/eiointc.c |  1 +
 arch/loongarch/kvm/intc/ipi.c     |  1 +
 arch/loongarch/kvm/intc/pch_pic.c | 14 ++++++++++----
 4 files changed, 13 insertions(+), 4 deletions(-)

-- 
2.43.0


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH v4 1/3] LoongArch: KVM: Clear device pointer in irqchip destroy callbacks
  2026-10-15 23:46 [PATCH v4 0/3] LoongArch: KVM: irqchip fixes Tao Cui
@ 2026-10-15 23:46 ` Tao Cui
  2026-10-16  0:05   ` sashiko-bot
  2026-10-16  1:03   ` Bibo Mao
  2026-10-15 23:46 ` [PATCH v4 2/3] LoongArch: KVM: Propagate real error code in kvm_pch_pic_create Tao Cui
  2026-10-15 23:46 ` [PATCH v4 3/3] LoongArch: KVM: Reject repeated PCH-PIC CTRL_INIT Tao Cui
  2 siblings, 2 replies; 9+ messages in thread
From: Tao Cui @ 2026-10-15 23:46 UTC (permalink / raw)
  To: chenhuacai, maobibo, zhaotianrui
  Cc: loongarch, kvm, linux-kernel, kernel, nagachaithanya9911, cui.tao,
	Tao Cui

From: Tao Cui <cuitao@kylinos.cn>

The destroy callbacks of the four irqchip devices free the device
structure without clearing kvm->arch.{ipi,eiointc,pch_pic,dmsintc},
leaving a dangling pointer.  ops->destroy() is not only called at VM
teardown but also when KVM_CREATE_DEVICE succeeds and the following
anon_inode_getfd() fails (e.g. under RLIMIT_NOFILE); the VM stays
alive, kvm_arch_irqchip_in_kernel() still reports true, and interrupt
injection dereferences the freed device.

Clear the pointer when destroying.

Fixes: c532de5a67a7 ("LoongArch: KVM: Add IPI device support")
Fixes: 2e8b9df82631 ("LoongArch: KVM: Add EIOINTC device support")
Fixes: e785dfacf7e7 ("LoongArch: KVM: Add PCHPIC device support")
Fixes: 229132c309d6 ("LoongArch: KVM: Add DMSINTC device support")
Signed-off-by: Tao Cui <cuitao@kylinos.cn>
---
 arch/loongarch/kvm/intc/dmsintc.c | 1 +
 arch/loongarch/kvm/intc/eiointc.c | 1 +
 arch/loongarch/kvm/intc/ipi.c     | 1 +
 arch/loongarch/kvm/intc/pch_pic.c | 1 +
 4 files changed, 4 insertions(+)

diff --git a/arch/loongarch/kvm/intc/dmsintc.c b/arch/loongarch/kvm/intc/dmsintc.c
index ff327b4c05f7..c182185a3d59 100644
--- a/arch/loongarch/kvm/intc/dmsintc.c
+++ b/arch/loongarch/kvm/intc/dmsintc.c
@@ -146,6 +146,7 @@ static void kvm_dmsintc_destroy(struct kvm_device *dev)
 		return;
 
 	kfree(dev->kvm->arch.dmsintc);
+	dev->kvm->arch.dmsintc = NULL;
 	kfree(dev);
 }
 
diff --git a/arch/loongarch/kvm/intc/eiointc.c b/arch/loongarch/kvm/intc/eiointc.c
index 80f78e07c74a..fe0a1918f26f 100644
--- a/arch/loongarch/kvm/intc/eiointc.c
+++ b/arch/loongarch/kvm/intc/eiointc.c
@@ -675,6 +675,7 @@ static void kvm_eiointc_destroy(struct kvm_device *dev)
 
 	kvm = dev->kvm;
 	eiointc = kvm->arch.eiointc;
+	kvm->arch.eiointc = NULL;
 	mutex_lock(&kvm->slots_lock);
 	kvm_io_bus_unregister_dev(kvm, KVM_IOCSR_BUS, &eiointc->device);
 	kvm_io_bus_unregister_dev(kvm, KVM_IOCSR_BUS, &eiointc->device_vext);
diff --git a/arch/loongarch/kvm/intc/ipi.c b/arch/loongarch/kvm/intc/ipi.c
index 80faa770c0cd..7a2dd72a4952 100644
--- a/arch/loongarch/kvm/intc/ipi.c
+++ b/arch/loongarch/kvm/intc/ipi.c
@@ -441,6 +441,7 @@ static void kvm_ipi_destroy(struct kvm_device *dev)
 
 	kvm = dev->kvm;
 	ipi = kvm->arch.ipi;
+	kvm->arch.ipi = NULL;
 	mutex_lock(&kvm->slots_lock);
 	kvm_io_bus_unregister_dev(kvm, KVM_IOCSR_BUS, &ipi->device);
 	mutex_unlock(&kvm->slots_lock);
diff --git a/arch/loongarch/kvm/intc/pch_pic.c b/arch/loongarch/kvm/intc/pch_pic.c
index 220fc23ffe8c..dab2d3f1b8f6 100644
--- a/arch/loongarch/kvm/intc/pch_pic.c
+++ b/arch/loongarch/kvm/intc/pch_pic.c
@@ -483,6 +483,7 @@ static void kvm_pch_pic_destroy(struct kvm_device *dev)
 
 	kvm = dev->kvm;
 	s = kvm->arch.pch_pic;
+	kvm->arch.pch_pic = NULL;
 	/* unregister pch pic device and free it's memory */
 	mutex_lock(&kvm->slots_lock);
 	kvm_io_bus_unregister_dev(kvm, KVM_MMIO_BUS, &s->device);
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH v4 2/3] LoongArch: KVM: Propagate real error code in kvm_pch_pic_create
  2026-10-15 23:46 [PATCH v4 0/3] LoongArch: KVM: irqchip fixes Tao Cui
  2026-10-15 23:46 ` [PATCH v4 1/3] LoongArch: KVM: Clear device pointer in irqchip destroy callbacks Tao Cui
@ 2026-10-15 23:46 ` Tao Cui
  2026-10-15 23:46 ` [PATCH v4 3/3] LoongArch: KVM: Reject repeated PCH-PIC CTRL_INIT Tao Cui
  2 siblings, 0 replies; 9+ messages in thread
From: Tao Cui @ 2026-10-15 23:46 UTC (permalink / raw)
  To: chenhuacai, maobibo, zhaotianrui
  Cc: loongarch, kvm, linux-kernel, kernel, nagachaithanya9911, cui.tao,
	Tao Cui

From: Tao Cui <cuitao@kylinos.cn>

kvm_pch_pic_create() replaces the real error of
kvm_setup_default_irq_routing() with a fixed -ENOMEM.  Return the
actual code, as kvm_eiointc_create() already does; the kvm_ipi_create()
case is fixed separately by "Return the actual error code in
kvm_ipi_create()" (Chaithanya Lagisetty).

Fixes: e785dfacf7e7 ("LoongArch: KVM: Add PCHPIC device support")
Signed-off-by: Tao Cui <cuitao@kylinos.cn>
Reviewed-by: Bibo Mao <maobibo@loongson.cn>
---
 arch/loongarch/kvm/intc/pch_pic.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/loongarch/kvm/intc/pch_pic.c b/arch/loongarch/kvm/intc/pch_pic.c
index dab2d3f1b8f6..51e4a6a39dc0 100644
--- a/arch/loongarch/kvm/intc/pch_pic.c
+++ b/arch/loongarch/kvm/intc/pch_pic.c
@@ -444,7 +444,7 @@ static int kvm_pch_pic_create(struct kvm_device *dev, u32 type)
 
 	ret = kvm_setup_default_irq_routing(kvm);
 	if (ret)
-		return -ENOMEM;
+		return ret;
 
 	s = kzalloc_obj(struct loongarch_pch_pic);
 	if (!s)
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH v4 3/3] LoongArch: KVM: Reject repeated PCH-PIC CTRL_INIT
  2026-10-15 23:46 [PATCH v4 0/3] LoongArch: KVM: irqchip fixes Tao Cui
  2026-10-15 23:46 ` [PATCH v4 1/3] LoongArch: KVM: Clear device pointer in irqchip destroy callbacks Tao Cui
  2026-10-15 23:46 ` [PATCH v4 2/3] LoongArch: KVM: Propagate real error code in kvm_pch_pic_create Tao Cui
@ 2026-10-15 23:46 ` Tao Cui
  2026-10-15 23:56   ` sashiko-bot
                     ` (2 more replies)
  2 siblings, 3 replies; 9+ messages in thread
From: Tao Cui @ 2026-10-15 23:46 UTC (permalink / raw)
  To: chenhuacai, maobibo, zhaotianrui
  Cc: loongarch, kvm, linux-kernel, kernel, nagachaithanya9911, cui.tao,
	Tao Cui

From: Tao Cui <cuitao@kylinos.cn>

KVM_DEV_LOONGARCH_PCH_PIC_CTRL_INIT has no guard against repeated
invocation: every call overwrites pch_pic_base and registers the same
kvm_io_device on the MMIO bus at the new address, while
kvm_pch_pic_destroy() unregisters only one bus range.  After a repeated
init, MMIO to the stale ranges computes its register offset against the
new base and silently reads 0 / drops writes, and the leftover bus
entries persist until the VM is destroyed.

Reject repeated initialization with -EEXIST, using the initialized
state of the kvm_io_device as the indicator.  The check, the MMIO base
update and the bus registration are serialized by slots_lock, and the
real registration error is propagated instead of being replaced with
-EFAULT.

Fixes: d206d9514873 ("LoongArch: KVM: Add PCHPIC user mode read and write functions")
Signed-off-by: Tao Cui <cuitao@kylinos.cn>
---
 arch/loongarch/kvm/intc/pch_pic.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/arch/loongarch/kvm/intc/pch_pic.c b/arch/loongarch/kvm/intc/pch_pic.c
index 51e4a6a39dc0..a4c35ed704be 100644
--- a/arch/loongarch/kvm/intc/pch_pic.c
+++ b/arch/loongarch/kvm/intc/pch_pic.c
@@ -281,16 +281,21 @@ static int kvm_pch_pic_init(struct kvm_device *dev, u64 addr)
 	struct kvm_io_device *device;
 	struct loongarch_pch_pic *s = dev->kvm->arch.pch_pic;
 
-	s->pch_pic_base = addr;
 	device = &s->device;
+	mutex_lock(&kvm->slots_lock);
+	if (s->device.ops) {
+		ret = -EEXIST;
+		goto out;
+	}
+	s->pch_pic_base = addr;
 	/* init device by pch pic writing and reading ops */
 	kvm_iodevice_init(device, &kvm_pch_pic_ops);
-	mutex_lock(&kvm->slots_lock);
 	/* register pch pic device */
 	ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, addr, PCH_PIC_SIZE, device);
+out:
 	mutex_unlock(&kvm->slots_lock);
 
-	return (ret < 0) ? -EFAULT : 0;
+	return ret;
 }
 
 /* used by user space to get or set pch pic registers */
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH v4 3/3] LoongArch: KVM: Reject repeated PCH-PIC CTRL_INIT
  2026-10-15 23:46 ` [PATCH v4 3/3] LoongArch: KVM: Reject repeated PCH-PIC CTRL_INIT Tao Cui
@ 2026-10-15 23:56   ` sashiko-bot
  2026-10-16  1:03   ` Bibo Mao
  2026-10-16  1:31   ` Bibo Mao
  2 siblings, 0 replies; 9+ messages in thread
From: sashiko-bot @ 2026-10-15 23:56 UTC (permalink / raw)
  To: Tao Cui; +Cc: kvm

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] kvm: loongarch: missing rollback of s->device.ops on IO bus registration failure
--

commit d13a3fd9c70a7a0b0118a0e9dbae30ac5e61efaa
Author: Tao Cui <cuitao@kylinos.cn>

LoongArch: KVM: Reject repeated PCH-PIC CTRL_INIT

This commit adds a check to prevent repeated PCH-PIC initialization. It
rejects multiple ioctl calls with -EEXIST by verifying if the device
ops are already populated.

> diff --git a/arch/loongarch/kvm/intc/pch_pic.c b/arch/loongarch/kvm/intc/pch_pic.c
> index 51e4a6a39dc06..a4c35ed704be8 100644
> --- a/arch/loongarch/kvm/intc/pch_pic.c
> +++ b/arch/loongarch/kvm/intc/pch_pic.c
> @@ -281,16 +281,21 @@ static int kvm_pch_pic_init(struct kvm_device *dev, u64 addr)
>  	struct kvm_io_device *device;
>  	struct loongarch_pch_pic *s = dev->kvm->arch.pch_pic;
>  
> -	s->pch_pic_base = addr;
>  	device = &s->device;
> +	mutex_lock(&kvm->slots_lock);
> +	if (s->device.ops) {
> +		ret = -EEXIST;
> +		goto out;
> +	}
> +	s->pch_pic_base = addr;
>  	/* init device by pch pic writing and reading ops */
>  	kvm_iodevice_init(device, &kvm_pch_pic_ops);
> -	mutex_lock(&kvm->slots_lock);
>  	/* register pch pic device */
>  	ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, addr, PCH_PIC_SIZE, device);
> +out:
>  	mutex_unlock(&kvm->slots_lock);
>  
> -	return (ret < 0) ? -EFAULT : 0;
> +	return ret;

[Severity: Low]
Does this error path leave the device partially initialized if
kvm_io_bus_register_dev() fails?

If kvm_io_bus_register_dev() returns an error (e.g. -ENOMEM), ret is set
to the error code, but s->device.ops has already been populated by
kvm_iodevice_init() earlier in kvm_pch_pic_init().

Since the function now uses s->device.ops to check for initialization,
would this prevent userspace from retrying the initialization? Subsequent
calls will see s->device.ops populated and return -EEXIST.

>  }
>  
>  /* used by user space to get or set pch pic registers */

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261015234638.1077815-1-cui.tao@linux.dev?part=3

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v4 1/3] LoongArch: KVM: Clear device pointer in irqchip destroy callbacks
  2026-10-15 23:46 ` [PATCH v4 1/3] LoongArch: KVM: Clear device pointer in irqchip destroy callbacks Tao Cui
@ 2026-10-16  0:05   ` sashiko-bot
  2026-10-16  1:03   ` Bibo Mao
  1 sibling, 0 replies; 9+ messages in thread
From: sashiko-bot @ 2026-10-16  0:05 UTC (permalink / raw)
  To: Tao Cui; +Cc: kvm

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] kvm: loongarch: UAF in irqchip teardown due to missing irq_srcu synchronization
- [High] kvm: loongarch: NULL pointer dereference in IRQ injection paths
--

commit 2f10d82f92df54fcbd750081f5d2c1a3283f66b9
Author: Tao Cui <cuitao@kylinos.cn>

LoongArch: KVM: Clear device pointer in irqchip destroy callbacks

The destroy callbacks of the four irqchip devices free the device
structure without clearing kvm->arch.{ipi,eiointc,pch_pic,dmsintc},
leaving a dangling pointer. This clears the pointer when destroying.

> diff --git a/arch/loongarch/kvm/intc/dmsintc.c b/arch/loongarch/kvm/intc/dmsintc.c
> --- a/arch/loongarch/kvm/intc/dmsintc.c
> +++ b/arch/loongarch/kvm/intc/dmsintc.c
> @@ -146,6 +146,7 @@ static void kvm_dmsintc_destroy(struct kvm_device *dev)
>  		return;
>  
>  	kfree(dev->kvm->arch.dmsintc);
> +	dev->kvm->arch.dmsintc = NULL;

[Severity: High]
Does this sequence free the structure before setting the pointer to NULL?
This could leave a globally visible dangling pointer during the race window.

If KVM_CREATE_DEVICE fails during anon_inode_getfd() (e.g., due to a process
hitting RLIMIT_NOFILE), this destroy callback executes immediately. If a
guest concurrently issues KVM_SIGNAL_MSI or KVM_IRQ_LINE ioctls, can they
race with this teardown?

Without calling synchronize_srcu(&kvm->irq_srcu) before kfree(), concurrent
IRQ injections holding the SRCU read lock might access the freed memory,
leading to a use-after-free.

[ ... ]

> diff --git a/arch/loongarch/kvm/intc/eiointc.c b/arch/loongarch/kvm/intc/eiointc.c
> --- a/arch/loongarch/kvm/intc/eiointc.c
> +++ b/arch/loongarch/kvm/intc/eiointc.c
> @@ -675,6 +675,7 @@ static void kvm_eiointc_destroy(struct kvm_device *dev)
>  
>  	kvm = dev->kvm;
>  	eiointc = kvm->arch.eiointc;
> +	kvm->arch.eiointc = NULL;
>  	mutex_lock(&kvm->slots_lock);
>  	kvm_io_bus_unregister_dev(kvm, KVM_IOCSR_BUS, &eiointc->device);
>  	kvm_io_bus_unregister_dev(kvm, KVM_IOCSR_BUS, &eiointc->device_vext);

[Severity: High]
Can this modification introduce a NULL pointer dereference in the IRQ
injection paths?

When kvm->arch.eiointc is cleared here without SRCU synchronization,
concurrent IRQ routing callbacks might see a NULL pointer. Functions like
eiointc_set_irq() or pch_msi_set_irq() currently dereference
kvm->arch.eiointc unconditionally:

arch/loongarch/kvm/intc/eiointc.c:eiointc_set_irq() {
    ...
    spin_lock_irqsave(&s->lock, flags);
    ...
}

Additionally, since eiointc is eventually freed in this function without
waiting for synchronize_srcu(&kvm->irq_srcu), could concurrent injections
also hit a use-after-free if they managed to read the pointer before it
was set to NULL?

[ ... ]

> diff --git a/arch/loongarch/kvm/intc/pch_pic.c b/arch/loongarch/kvm/intc/pch_pic.c
> --- a/arch/loongarch/kvm/intc/pch_pic.c
> +++ b/arch/loongarch/kvm/intc/pch_pic.c
> @@ -483,6 +483,7 @@ static void kvm_pch_pic_destroy(struct kvm_device *dev)
>  
>  	kvm = dev->kvm;
>  	s = kvm->arch.pch_pic;
> +	kvm->arch.pch_pic = NULL;
>  	/* unregister pch pic device and free it's memory */
>  	mutex_lock(&kvm->slots_lock);
>  	kvm_io_bus_unregister_dev(kvm, KVM_MMIO_BUS, &s->device);

[Severity: High]
Similar to eiointc, does clearing kvm->arch.pch_pic without SRCU
synchronization expose concurrent IRQ handlers to a NULL pointer dereference
or use-after-free?

If KVM_IRQ_LINE is called concurrently, kvm_set_pic_irq() reads
kvm->arch.pch_pic and passes it to pch_pic_set_irq() without validation:

arch/loongarch/kvm/irqfd.c:kvm_set_pic_irq() {
    ...
    pch_pic_set_irq(kvm->arch.pch_pic, e->irqchip.pin, level);
    ...
}

arch/loongarch/kvm/intc/pch_pic.c:pch_pic_set_irq() {
    ...
    spin_lock(&s->lock);
    ...
}

If the caller reads the pointer after it is cleared, it will dereference
NULL. If it reads the pointer before it is cleared, the subsequent kfree(s)
at the end of this destroy callback could cause a use-after-free, since
there is no synchronize_srcu(&kvm->irq_srcu) to protect the read-side.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261015234638.1077815-1-cui.tao@linux.dev?part=1

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v4 1/3] LoongArch: KVM: Clear device pointer in irqchip destroy callbacks
  2026-10-15 23:46 ` [PATCH v4 1/3] LoongArch: KVM: Clear device pointer in irqchip destroy callbacks Tao Cui
  2026-10-16  0:05   ` sashiko-bot
@ 2026-10-16  1:03   ` Bibo Mao
  1 sibling, 0 replies; 9+ messages in thread
From: Bibo Mao @ 2026-10-16  1:03 UTC (permalink / raw)
  To: Tao Cui, chenhuacai, zhaotianrui
  Cc: loongarch, kvm, linux-kernel, kernel, nagachaithanya9911, Tao Cui



On 2026/10/16 上午7:46, Tao Cui wrote:
> From: Tao Cui <cuitao@kylinos.cn>
> 
> The destroy callbacks of the four irqchip devices free the device
> structure without clearing kvm->arch.{ipi,eiointc,pch_pic,dmsintc},
> leaving a dangling pointer.  ops->destroy() is not only called at VM
> teardown but also when KVM_CREATE_DEVICE succeeds and the following
> anon_inode_getfd() fails (e.g. under RLIMIT_NOFILE); the VM stays
> alive, kvm_arch_irqchip_in_kernel() still reports true, and interrupt
> injection dereferences the freed device.
> 
> Clear the pointer when destroying.
> 
> Fixes: c532de5a67a7 ("LoongArch: KVM: Add IPI device support")
> Fixes: 2e8b9df82631 ("LoongArch: KVM: Add EIOINTC device support")
> Fixes: e785dfacf7e7 ("LoongArch: KVM: Add PCHPIC device support")
> Fixes: 229132c309d6 ("LoongArch: KVM: Add DMSINTC device support")
> Signed-off-by: Tao Cui <cuitao@kylinos.cn>
> ---
>   arch/loongarch/kvm/intc/dmsintc.c | 1 +
>   arch/loongarch/kvm/intc/eiointc.c | 1 +
>   arch/loongarch/kvm/intc/ipi.c     | 1 +
>   arch/loongarch/kvm/intc/pch_pic.c | 1 +
>   4 files changed, 4 insertions(+)
> 
> diff --git a/arch/loongarch/kvm/intc/dmsintc.c b/arch/loongarch/kvm/intc/dmsintc.c
> index ff327b4c05f7..c182185a3d59 100644
> --- a/arch/loongarch/kvm/intc/dmsintc.c
> +++ b/arch/loongarch/kvm/intc/dmsintc.c
> @@ -146,6 +146,7 @@ static void kvm_dmsintc_destroy(struct kvm_device *dev)
>   		return;
>   
>   	kfree(dev->kvm->arch.dmsintc);
> +	dev->kvm->arch.dmsintc = NULL;
>   	kfree(dev);
>   }
>   
> diff --git a/arch/loongarch/kvm/intc/eiointc.c b/arch/loongarch/kvm/intc/eiointc.c
> index 80f78e07c74a..fe0a1918f26f 100644
> --- a/arch/loongarch/kvm/intc/eiointc.c
> +++ b/arch/loongarch/kvm/intc/eiointc.c
> @@ -675,6 +675,7 @@ static void kvm_eiointc_destroy(struct kvm_device *dev)
>   
>   	kvm = dev->kvm;
>   	eiointc = kvm->arch.eiointc;
> +	kvm->arch.eiointc = NULL;
>   	mutex_lock(&kvm->slots_lock);
>   	kvm_io_bus_unregister_dev(kvm, KVM_IOCSR_BUS, &eiointc->device);
>   	kvm_io_bus_unregister_dev(kvm, KVM_IOCSR_BUS, &eiointc->device_vext);
> diff --git a/arch/loongarch/kvm/intc/ipi.c b/arch/loongarch/kvm/intc/ipi.c
> index 80faa770c0cd..7a2dd72a4952 100644
> --- a/arch/loongarch/kvm/intc/ipi.c
> +++ b/arch/loongarch/kvm/intc/ipi.c
> @@ -441,6 +441,7 @@ static void kvm_ipi_destroy(struct kvm_device *dev)
>   
>   	kvm = dev->kvm;
>   	ipi = kvm->arch.ipi;
> +	kvm->arch.ipi = NULL;
>   	mutex_lock(&kvm->slots_lock);
>   	kvm_io_bus_unregister_dev(kvm, KVM_IOCSR_BUS, &ipi->device);
>   	mutex_unlock(&kvm->slots_lock);
> diff --git a/arch/loongarch/kvm/intc/pch_pic.c b/arch/loongarch/kvm/intc/pch_pic.c
> index 220fc23ffe8c..dab2d3f1b8f6 100644
> --- a/arch/loongarch/kvm/intc/pch_pic.c
> +++ b/arch/loongarch/kvm/intc/pch_pic.c
> @@ -483,6 +483,7 @@ static void kvm_pch_pic_destroy(struct kvm_device *dev)
>   
>   	kvm = dev->kvm;
>   	s = kvm->arch.pch_pic;
> +	kvm->arch.pch_pic = NULL;
>   	/* unregister pch pic device and free it's memory */
>   	mutex_lock(&kvm->slots_lock);
>   	kvm_io_bus_unregister_dev(kvm, KVM_MMIO_BUS, &s->device);
> 
Reviewed-by: Bibo Mao <maobibo@loongson.cn>


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v4 3/3] LoongArch: KVM: Reject repeated PCH-PIC CTRL_INIT
  2026-10-15 23:46 ` [PATCH v4 3/3] LoongArch: KVM: Reject repeated PCH-PIC CTRL_INIT Tao Cui
  2026-10-15 23:56   ` sashiko-bot
@ 2026-10-16  1:03   ` Bibo Mao
  2026-10-16  1:31   ` Bibo Mao
  2 siblings, 0 replies; 9+ messages in thread
From: Bibo Mao @ 2026-10-16  1:03 UTC (permalink / raw)
  To: Tao Cui, chenhuacai, zhaotianrui
  Cc: loongarch, kvm, linux-kernel, kernel, nagachaithanya9911, Tao Cui



On 2026/10/16 上午7:46, Tao Cui wrote:
> From: Tao Cui <cuitao@kylinos.cn>
> 
> KVM_DEV_LOONGARCH_PCH_PIC_CTRL_INIT has no guard against repeated
> invocation: every call overwrites pch_pic_base and registers the same
> kvm_io_device on the MMIO bus at the new address, while
> kvm_pch_pic_destroy() unregisters only one bus range.  After a repeated
> init, MMIO to the stale ranges computes its register offset against the
> new base and silently reads 0 / drops writes, and the leftover bus
> entries persist until the VM is destroyed.
> 
> Reject repeated initialization with -EEXIST, using the initialized
> state of the kvm_io_device as the indicator.  The check, the MMIO base
> update and the bus registration are serialized by slots_lock, and the
> real registration error is propagated instead of being replaced with
> -EFAULT.
> 
> Fixes: d206d9514873 ("LoongArch: KVM: Add PCHPIC user mode read and write functions")
> Signed-off-by: Tao Cui <cuitao@kylinos.cn>
> ---
>   arch/loongarch/kvm/intc/pch_pic.c | 11 ++++++++---
>   1 file changed, 8 insertions(+), 3 deletions(-)
> 
> diff --git a/arch/loongarch/kvm/intc/pch_pic.c b/arch/loongarch/kvm/intc/pch_pic.c
> index 51e4a6a39dc0..a4c35ed704be 100644
> --- a/arch/loongarch/kvm/intc/pch_pic.c
> +++ b/arch/loongarch/kvm/intc/pch_pic.c
> @@ -281,16 +281,21 @@ static int kvm_pch_pic_init(struct kvm_device *dev, u64 addr)
>   	struct kvm_io_device *device;
>   	struct loongarch_pch_pic *s = dev->kvm->arch.pch_pic;
>   
> -	s->pch_pic_base = addr;
>   	device = &s->device;
> +	mutex_lock(&kvm->slots_lock);
> +	if (s->device.ops) {
> +		ret = -EEXIST;
> +		goto out;
> +	}
> +	s->pch_pic_base = addr;
>   	/* init device by pch pic writing and reading ops */
>   	kvm_iodevice_init(device, &kvm_pch_pic_ops);
> -	mutex_lock(&kvm->slots_lock);
>   	/* register pch pic device */
>   	ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, addr, PCH_PIC_SIZE, device);
> +out:
>   	mutex_unlock(&kvm->slots_lock);
>   
> -	return (ret < 0) ? -EFAULT : 0;
> +	return ret;
>   }
>   
>   /* used by user space to get or set pch pic registers */
> 
Reviewed-by: Bibo Mao <maobibo@loongson.cn>


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v4 3/3] LoongArch: KVM: Reject repeated PCH-PIC CTRL_INIT
  2026-10-15 23:46 ` [PATCH v4 3/3] LoongArch: KVM: Reject repeated PCH-PIC CTRL_INIT Tao Cui
  2026-10-15 23:56   ` sashiko-bot
  2026-10-16  1:03   ` Bibo Mao
@ 2026-10-16  1:31   ` Bibo Mao
  2 siblings, 0 replies; 9+ messages in thread
From: Bibo Mao @ 2026-10-16  1:31 UTC (permalink / raw)
  To: Tao Cui, chenhuacai, zhaotianrui
  Cc: loongarch, kvm, linux-kernel, kernel, nagachaithanya9911, Tao Cui



On 2026/10/16 上午7:46, Tao Cui wrote:
> From: Tao Cui <cuitao@kylinos.cn>
> 
> KVM_DEV_LOONGARCH_PCH_PIC_CTRL_INIT has no guard against repeated
> invocation: every call overwrites pch_pic_base and registers the same
> kvm_io_device on the MMIO bus at the new address, while
> kvm_pch_pic_destroy() unregisters only one bus range.  After a repeated
> init, MMIO to the stale ranges computes its register offset against the
> new base and silently reads 0 / drops writes, and the leftover bus
> entries persist until the VM is destroyed.
> 
> Reject repeated initialization with -EEXIST, using the initialized
> state of the kvm_io_device as the indicator.  The check, the MMIO base
> update and the bus registration are serialized by slots_lock, and the
> real registration error is propagated instead of being replaced with
> -EFAULT.
> 
> Fixes: d206d9514873 ("LoongArch: KVM: Add PCHPIC user mode read and write functions")
> Signed-off-by: Tao Cui <cuitao@kylinos.cn>
> ---
>   arch/loongarch/kvm/intc/pch_pic.c | 11 ++++++++---
>   1 file changed, 8 insertions(+), 3 deletions(-)
> 
> diff --git a/arch/loongarch/kvm/intc/pch_pic.c b/arch/loongarch/kvm/intc/pch_pic.c
> index 51e4a6a39dc0..a4c35ed704be 100644
> --- a/arch/loongarch/kvm/intc/pch_pic.c
> +++ b/arch/loongarch/kvm/intc/pch_pic.c
> @@ -281,16 +281,21 @@ static int kvm_pch_pic_init(struct kvm_device *dev, u64 addr)
>   	struct kvm_io_device *device;
>   	struct loongarch_pch_pic *s = dev->kvm->arch.pch_pic;
>   
> -	s->pch_pic_base = addr;
>   	device = &s->device;
> +	mutex_lock(&kvm->slots_lock);
> +	if (s->device.ops) {
> +		ret = -EEXIST;
> +		goto out;
> +	}
> +	s->pch_pic_base = addr;
>   	/* init device by pch pic writing and reading ops */
>   	kvm_iodevice_init(device, &kvm_pch_pic_ops);
> -	mutex_lock(&kvm->slots_lock);
>   	/* register pch pic device */
>   	ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, addr, PCH_PIC_SIZE, device);
> +out:
>   	mutex_unlock(&kvm->slots_lock);
one small nit, what to do if there is failure with 
kvm_io_bus_register_dev()?  clear s->pch_pic_base and  s->device.ops?

Regards
Bibo Mao
>   
> -	return (ret < 0) ? -EFAULT : 0;
> +	return ret;
>   }
>   
>   /* used by user space to get or set pch pic registers */
> 


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2026-10-16  1:30 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-10-15 23:46 [PATCH v4 0/3] LoongArch: KVM: irqchip fixes Tao Cui
2026-10-15 23:46 ` [PATCH v4 1/3] LoongArch: KVM: Clear device pointer in irqchip destroy callbacks Tao Cui
2026-10-16  0:05   ` sashiko-bot
2026-10-16  1:03   ` Bibo Mao
2026-10-15 23:46 ` [PATCH v4 2/3] LoongArch: KVM: Propagate real error code in kvm_pch_pic_create Tao Cui
2026-10-15 23:46 ` [PATCH v4 3/3] LoongArch: KVM: Reject repeated PCH-PIC CTRL_INIT Tao Cui
2026-10-15 23:56   ` sashiko-bot
2026-10-16  1:03   ` Bibo Mao
2026-10-16  1:31   ` Bibo Mao

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.