All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] PCI/VGA: Serialize per-file resource counters
@ 2026-12-05 13:16 Zhao Gongyi
  2026-12-05 13:32 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Zhao Gongyi @ 2026-12-05 13:16 UTC (permalink / raw)
  To: Bjorn Helgaas; +Cc: linux-pci, linux-kernel, stable, Zhao Gongyi

Concurrent unlocks on the same file can both pass the zero check and
underflow a resource counter. On close, vga_arb_release() can then spend
a long time releasing resources with interrupts disabled.

Syzkaller reported an RCU stall during the release loop:

  __sanitizer_cov_trace_pc+0xc/0x70
  vga_arb_release+0x1a2/0x330

Serialize updates to the counters, including the unlock checks and
decrements, with the per-file lock. Drop vga_user_lock after unlinking
the file. The final release has exclusive access to its counters.

Fixes: deb2d2ecd43d ("PCI/GPU: implement VGA arbitration on Linux")
Cc: stable@vger.kernel.org
Signed-off-by: Zhao Gongyi <zhaogongyi@bytedance.com>

diff --git a/drivers/pci/vgaarb.c b/drivers/pci/vgaarb.c
index 3e58b2c05c8546cacc00ba8b881f625c195e0c83..5d30a6d0df584b61431e8d604ac18b4d4e1c94d2 100644
--- a/drivers/pci/vgaarb.c
+++ b/drivers/pci/vgaarb.c
@@ -1140,6 +1140,7 @@ static ssize_t vga_arb_write(struct file *file, const char __user *buf,
 	struct vga_arb_private *priv = file->private_data;
 	struct vga_arb_user_card *uc = NULL;
 	struct pci_dev *pdev;
+	unsigned long flags;
 
 	unsigned int io_state;
 
@@ -1185,6 +1186,7 @@ static ssize_t vga_arb_write(struct file *file, const char __user *buf,
 		}
 
 		/* Update the client's locks lists */
+		spin_lock_irqsave(&priv->lock, flags);
 		for (i = 0; i < MAX_USER_CARDS; i++) {
 			if (priv->cards[i].pdev == pdev) {
 				if (io_state & VGA_RSRC_LEGACY_IO)
@@ -1194,6 +1196,7 @@ static ssize_t vga_arb_write(struct file *file, const char __user *buf,
 				break;
 			}
 		}
+		spin_unlock_irqrestore(&priv->lock, flags);
 
 		ret_val = count;
 		goto done;
@@ -1234,22 +1237,26 @@ static ssize_t vga_arb_write(struct file *file, const char __user *buf,
 			goto done;
 		}
 
+		spin_lock_irqsave(&priv->lock, flags);
 		if (io_state & VGA_RSRC_LEGACY_IO && uc->io_cnt == 0) {
+			spin_unlock_irqrestore(&priv->lock, flags);
 			ret_val = -EINVAL;
 			goto done;
 		}
 
 		if (io_state & VGA_RSRC_LEGACY_MEM && uc->mem_cnt == 0) {
+			spin_unlock_irqrestore(&priv->lock, flags);
 			ret_val = -EINVAL;
 			goto done;
 		}
 
-		vga_put(pdev, io_state);
-
 		if (io_state & VGA_RSRC_LEGACY_IO)
 			uc->io_cnt--;
 		if (io_state & VGA_RSRC_LEGACY_MEM)
 			uc->mem_cnt--;
+		spin_unlock_irqrestore(&priv->lock, flags);
+
+		vga_put(pdev, io_state);
 
 		ret_val = count;
 		goto done;
@@ -1278,6 +1285,7 @@ static ssize_t vga_arb_write(struct file *file, const char __user *buf,
 
 		if (vga_tryget(pdev, io_state)) {
 			/* Update the client's locks lists... */
+			spin_lock_irqsave(&priv->lock, flags);
 			for (i = 0; i < MAX_USER_CARDS; i++) {
 				if (priv->cards[i].pdev == pdev) {
 					if (io_state & VGA_RSRC_LEGACY_IO)
@@ -1287,6 +1295,7 @@ static ssize_t vga_arb_write(struct file *file, const char __user *buf,
 					break;
 				}
 			}
+			spin_unlock_irqrestore(&priv->lock, flags);
 			ret_val = count;
 			goto done;
 		} else {
@@ -1432,6 +1441,8 @@ static int vga_arb_release(struct inode *inode, struct file *file)
 
 	spin_lock_irqsave(&vga_user_lock, flags);
 	list_del(&priv->list);
+	spin_unlock_irqrestore(&vga_user_lock, flags);
+
 	for (i = 0; i < MAX_USER_CARDS; i++) {
 		uc = &priv->cards[i];
 		if (uc->pdev == NULL)
@@ -1443,7 +1454,6 @@ static int vga_arb_release(struct inode *inode, struct file *file)
 		while (uc->mem_cnt--)
 			vga_put(uc->pdev, VGA_RSRC_LEGACY_MEM);
 	}
-	spin_unlock_irqrestore(&vga_user_lock, flags);
 
 	kfree(priv);
 

base-commit: 85a0cf7926b9e6883cbc515b8b5f77090d3b1889
-- 
2.39.5 (Apple Git-154)

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

* Re: [PATCH] PCI/VGA: Serialize per-file resource counters
  2026-12-05 13:16 [PATCH] PCI/VGA: Serialize per-file resource counters Zhao Gongyi
@ 2026-12-05 13:32 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-05 13:32 UTC (permalink / raw)
  To: Zhao Gongyi; +Cc: linux-pci

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] PCI/VGA: incomplete priv->lock coverage in vga_arb_write()
  target and unlock
--

commit 49358fe981ccf86702231b1775d742792b053d29
Author: Zhao Gongyi <zhaogongyi@bytedance.com>

PCI/VGA: Serialize per-file resource counters

This commit protects per-file VGA resource counter updates in
vga_arb_write() with priv->lock and drops vga_user_lock before releasing
resources in vga_arb_release().

> diff --git a/drivers/pci/vgaarb.c b/drivers/pci/vgaarb.c
> index 3e58b2c05c854..5d30a6d0df584 100644
> --- a/drivers/pci/vgaarb.c
> +++ b/drivers/pci/vgaarb.c

[ ... ]

> @@ -1234,22 +1237,26 @@ static ssize_t vga_arb_write(struct file *file, const char __user *buf,
>  			goto done;
>  		}
>  
> +		spin_lock_irqsave(&priv->lock, flags);
>  		if (io_state & VGA_RSRC_LEGACY_IO && uc->io_cnt == 0) {
> +			spin_unlock_irqrestore(&priv->lock, flags);
>  			ret_val = -EINVAL;
>  			goto done;
>  		}
>  
>  		if (io_state & VGA_RSRC_LEGACY_MEM && uc->mem_cnt == 0) {
> +			spin_unlock_irqrestore(&priv->lock, flags);
>  			ret_val = -EINVAL;
>  			goto done;
>  		}
>  
> -		vga_put(pdev, io_state);
> -
>  		if (io_state & VGA_RSRC_LEGACY_IO)
>  			uc->io_cnt--;
>  		if (io_state & VGA_RSRC_LEGACY_MEM)
>  			uc->mem_cnt--;
> +		spin_unlock_irqrestore(&priv->lock, flags);
> +
> +		vga_put(pdev, io_state);
>  
>  		ret_val = count;
>  		goto done;

[Severity: Medium]
Can a concurrent "target" write still cause uc->io_cnt or uc->mem_cnt to
underflow?

The "target" branch in vga_arb_write() still updates priv->target and
initializes priv->cards[] entries without holding priv->lock:

drivers/pci/vgaarb.c:vga_arb_write() {
    ...
		priv->target = pdev;
		for (i = 0; i < MAX_USER_CARDS; i++) {
			if (priv->cards[i].pdev == pdev)
				break;
			if (priv->cards[i].pdev == NULL) {
				priv->cards[i].pdev = pdev;
				priv->cards[i].io_cnt = 0;
				priv->cards[i].mem_cnt = 0;
				break;
			}
		}
    ...
}

CPU0                                    CPU1
vga_arb_write("target ...")
  priv->target = pdev
  priv->cards[i].pdev = pdev
                                        vga_arb_write("lock io")
                                          vga_get_uninterruptible(...)
                                          spin_lock_irqsave(&priv->lock)
                                          priv->cards[i].io_cnt++  (-> 1)
                                          spin_unlock_irqrestore(...)
                                        vga_arb_write("unlock io")
                                          uc = &priv->cards[i]
                                          spin_lock_irqsave(&priv->lock)
                                          uc->io_cnt == 0  (false)
  priv->cards[i].io_cnt = 0
                                          uc->io_cnt--  <-- underflow

When the file is closed, vga_arb_release() then loops over the wrapped
unsigned counter calling vga_put() without yielding, reproducing the RCU
stall.

A concurrent "target" can also zero a counter after "lock" increments it
(leaking the VGA lock) or collide with another "target" on the same slot.
In "unlock", scanning priv->cards[] before taking priv->lock also leaves
the uc lookup racing with those slot updates.

Holding priv->lock around the priv->cards[] scan and initialization in
"target" (updating priv->target once the slot is ready) and before scanning
priv->cards[] in "unlock" would close these races.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205131649.84007-1-zhaogongyi@bytedance.com?part=1

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

end of thread, other threads:[~2026-12-05 13:32 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-05 13:16 [PATCH] PCI/VGA: Serialize per-file resource counters Zhao Gongyi
2026-12-05 13:32 ` sashiko-bot

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.