From nobody Mon May 25 02:41:21 2026 Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 72EEFE54B for ; Tue, 19 May 2026 16:23:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779207785; cv=none; b=BgBBLR5hVZKb8OFLS5VpuM+Eu4OA6Chi8k/Sc0ZpcCtmCVOyneyAJdse5rCIXs4DKTDXJKs8mONwKbvvLTKQmTUPakpp3Z9fzHsGmjfV4FoFeKOk2tVpuPhmFo/SGmOv6ImcXKr6Tslwe5ba0q8vfsMa/du2IBxBAJtLWKFJNTA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779207785; c=relaxed/simple; bh=w3QOsPE5ZjAYtM2vxu4mdIyDM5kHW57SYNfPY5zms/A=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=fojVHWNrFWiSxoGdZXkfLFwKq3GvWNOgLvybQ7AU+R6uDv9Wwur7sUmd9GB3ETWocNVMvZJoF62XFxXGpPxgIv+N9ijvYBMjfLkPXSOeAADDL1hHePn1ERr8++xr2ysjdY6e9OLUmuuDLVH1Xw1xwqJGU7Jb+LzbK/gCJnXUqrc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=gLD05ouM; arc=none smtp.client-ip=209.85.214.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="gLD05ouM" Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-2ba6485d219so27141905ad.3 for ; Tue, 19 May 2026 09:23:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1779207784; x=1779812584; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=51w7Om4y2lO0Q4Rbj7UCOFh1Ody3dmx8GUaGuh6fciM=; b=gLD05ouMTu7E2dnEcU7g0iP+VmU68ZSFNXUgK9U3S+7LcPlqWZiWnngDlseHbGeEhe yWeehvVqxCuTU3G74kSjWIZizSV7aEUNWQQvPFm1owl2VgdyTPj7Hf//AFboeDeqtVES jo6ZDoA4GtY3FCxSMd5pVSD7Hm22dm22tCTgD3JayqkZKtCjqIvGmyuTKj23s+MDsFba hZrck6jC4tjGM6Os0l6OVxSDcC5j7AtK63Beyj2e8akLbs7je2iAPX4I0FblnDuEDZBc Zh3ZeOFaNw+TiwV0RH/XnS9/5qY8E2llpEFdA+lYjABFGJpY4SnpoAM479x6HTUEXu2r CvkQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779207784; x=1779812584; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=51w7Om4y2lO0Q4Rbj7UCOFh1Ody3dmx8GUaGuh6fciM=; b=rGq4/Ukxu/FLONPYjLB8LXNOSQcISMtUqdHiPtqg6NDwuJMgqxNfO43IL+Ma8hYObz RMjeeeEw8bQjc5E5pYzd1Y7+TcN8ITNFLPo6DaC/efIu3zWa6yl2kdMXXXgw8MpKFHVc OVPKZFrt0iku2ThSxc2dauxKb67WM/l6c1Haqhb4sais3THSiIXFdZiNTZRrTGTsa44b kq7lMZO45VADG0N0uBa6VwEHDgdDqEW1gKpR8ShNy8xAbaznY9CsMM0MHeafGP1GHhE7 3VPJIP6Lw13avQU6rsTI93HVXBN6TF5ILlVwdWic6d/N2yrOkZis9ELCd+67y/6RYb8i Z5wg== X-Forwarded-Encrypted: i=1; AFNElJ/zDHLWLcHlZm2Xx73WGjiM9nxmzkbp4y0nq9HeU1JQCZjULdr4LhgazJMxChDuVyM1TjJZXo7hJ9zrGaA=@vger.kernel.org X-Gm-Message-State: AOJu0YwKE+kQISPeVZK4pwMJ4icsfhOjATO2vzgOqgymq7kdF+KYNVmK U3b1jEW6Egn5TN4dAslUuRs4m1yvZaj8+6YMlQUl0aLqxuuN9HLD0plU X-Gm-Gg: Acq92OHN7u3LS29be12tAnx+uIAR97spO3rnhSglmCr13IRs18pXHqvbOGEYbmfehYt HHrUPMcO86uFv2+4y6TuVXKXAtbo90niAkTTOuGCiMikWkn8gPO4grNUpblUATCf3QC6NsQdbvf 56ntYUTQ3ekBEMB38ah/Op19q8fl7YCyY5uUOLkdw+A98CxC6SlFG6CLC9TSp1NR9BR1Zb/AIDE yu0yMZjtvpc9Mxzg7WEJaNd6VLaxeeoD7xE16aIF11A4JfKEccTRGrlDeUa420jvFVDV0jbeStE 4T15TkwnEOAIo7MnuBhRlehZzOZQ7I9H27f2DANg25qUFMe/x7jzVK89JlaHGYuyB9xZHi48acG RHTm8cTB8f20jGjyu8locSDVVheaQmMwq8yDWza+a2PzD6yrEvc7vojsJaRqL7sfQH9vzkIpnz9 6m2oxxM5g3GxGItMExw00vtvzCAWm3o9LbsiThWoisEdn0ZQN37mDDe2l4NrvKfxX87e9c8h5Km kwHDFdZZVgfJfTlUg3d4SFBn8Y= X-Received: by 2002:a17:903:32c5:b0:2ba:bfc:7699 with SMTP id d9443c01a7336-2bd7e8ba6b9mr226985645ad.17.1779207783522; Tue, 19 May 2026 09:23:03 -0700 (PDT) Received: from baver-zenith.localdomain ([124.49.88.131]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2bd5bd5ef27sm245009025ad.8.2026.05.19.09.23.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 May 2026 09:23:02 -0700 (PDT) From: Sungho Bae To: amit@kernel.org, arnd@arndb.de, gregkh@linuxfoundation.org Cc: virtualization@lists.linux.dev, linux-kernel@vger.kernel.org, Sungho Bae Subject: [PATCH v2] virtio_console: fix race between hvc put_chars and virtqueue teardown on freeze Date: Wed, 20 May 2026 01:22:42 +0900 Message-Id: <20260519162242.7324-1-baver.bae@gmail.com> X-Mailer: git-send-email 2.34.1 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 Content-Type: text/plain; charset="utf-8" From: Sungho Bae With no_console_suspend enabled, hvc console output can continue while virtio_console is freezing. In that window, put_chars can still enqueue buffers to the output virtqueue while virtcons_freeze is tearing queues down, triggering a BUG_ON in virtqueue_detach_unused_buf_split: BUG_ON(vq->vq.num_free !=3D vq->split.vring.num) Add a pm_freezing flag to ports_device. Set it via smp_store_release() at the start of virtcons_freeze(); put_chars() and __send_to_port() drop output while the flag is set, checked via smp_load_acquire(). The check in __send_to_port() is placed under outvq_lock, making it atomic with remove_port_data() which also acquires outvq_lock. Once remove_port_data() returns for a given port, no concurrent __send_to_port() can add buffers before remove_vqs() tears down the vq. After setting pm_freezing, acquire and release outvq_lock for each port before calling virtio_reset_device(). A TX thread that already passed the pm_freezing check may still hold outvq_lock while spinning for host acknowledgment (the nonblock=3Dfalse hvc path); the drain loop ensures all such threads have completed before the device is reset. Clear pm_freezing in virtcons_restore() only after all port->out_vq pointers have been reassigned to the newly allocated virtqueues, preventing TX paths from dereferencing freed vqs during restore. Also fix two races in __send_to_port() uncovered by this work: load port->portdev via READ_ONCE() and check for NULL to guard against concurrent hot-unplug, and move out_vq =3D port->out_vq inside outvq_lock after the pm_freezing check to avoid a stale pointer. Link: https://sashiko.dev/#/patchset/20260515225259.1054-1-baver.bae%40gmai= l.com Signed-off-by: Sungho Bae --- drivers/char/virtio_console.c | 80 ++++++++++++++++++++++++++++++++++- 1 file changed, 78 insertions(+), 2 deletions(-) diff --git a/drivers/char/virtio_console.c b/drivers/char/virtio_console.c index 9a33217c68d9..fad673f733a8 100644 --- a/drivers/char/virtio_console.c +++ b/drivers/char/virtio_console.c @@ -157,6 +157,12 @@ struct ports_device { =20 /* Major number for this device. Ports will be created as minors. */ int chr_major; + + /* + * Set to true during PM freeze to block TX paths that may race + * with virtqueue teardown (e.g. hvc put_chars with no_console_suspend). + */ + bool pm_freezing; }; =20 struct port_stats { @@ -601,11 +607,30 @@ static ssize_t __send_to_port(struct port *port, stru= ct scatterlist *sg, int err; unsigned long flags; unsigned int len; - - out_vq =3D port->out_vq; + struct ports_device *portdev; =20 spin_lock_irqsave(&port->outvq_lock, flags); =20 + portdev =3D READ_ONCE(port->portdev); + + if (!portdev) { + in_count =3D 0; + goto done; + } + + /* + * Check freeze flag under the lock so that the flag check and + * virtqueue_add_outbuf() are atomic with respect to + * remove_port_data() which also takes outvq_lock. This + * guarantees that once remove_port_data() returns, no new + * buffers can be added before remove_vqs() tears down the vq. + * Pairs with smp_store_release() in virtcons_freeze/restore. + */ + if (smp_load_acquire(&portdev->pm_freezing)) /* pairs with freeze/restore= */ + goto done; + + out_vq =3D port->out_vq; + reclaim_consumed_buffers(port); =20 err =3D virtqueue_add_outbuf(out_vq, sg, nents, data, GFP_ATOMIC); @@ -1110,11 +1135,36 @@ static ssize_t put_chars(u32 vtermno, const u8 *buf= , size_t count) struct scatterlist sg[1]; void *data; int ret; + struct ports_device *portdev; =20 port =3D find_port_by_vtermno(vtermno); if (!port) return -EPIPE; =20 + /* + * Silently drop output in two cases, both by returning count so + * that the hvc layer does not spin-retry: + * + * 1. Device hot-unplug (!portdev): portdev was NULLed by + * unplug_port() after hvc_remove() was already called, so + * the hvc layer will stop invoking put_chars() very soon. + * Returning count avoids a pointless retry loop in the + * interim. + * + * 2. PM freeze (pm_freezing): the hvc console stays active + * under no_console_suspend but virtqueues are being torn + * down. Drop the output silently so the hvc layer does not + * stall suspend. + * + * This early check avoids a pointless GFP_ATOMIC allocation; + * __send_to_port() rechecks under outvq_lock for correctness. + * Pairs with smp_store_release() in virtcons_freeze/restore. + */ + portdev =3D READ_ONCE(port->portdev); + if (!portdev || + smp_load_acquire(&portdev->pm_freezing)) /* pairs with freeze/restore= */ + return count; + data =3D kmemdup(buf, count, GFP_ATOMIC); if (!data) return -ENOMEM; @@ -1972,6 +2022,7 @@ static int virtcons_probe(struct virtio_device *vdev) /* Attach this portdev to this virtio_device, and vice-versa. */ portdev->vdev =3D vdev; vdev->priv =3D portdev; + portdev->pm_freezing =3D false; =20 portdev->chr_major =3D register_chrdev(0, "virtio-portsdev", &portdev_fops); @@ -2092,6 +2143,24 @@ static int virtcons_freeze(struct virtio_device *vde= v) =20 portdev =3D vdev->priv; =20 + /* + * Block TX paths (put_chars, __send_to_port) before resetting the + * device and tearing down virtqueues. This prevents races with + * hvc console writes that remain active under no_console_suspend. + */ + smp_store_release(&portdev->pm_freezing, true); + + /* + * Synchronize with any concurrent __send_to_port() that may have + * passed the pm_freezing check. By acquiring and releasing the + * outvq_lock for each port, we ensure all active TX paths have + * completed before we reset the device. + */ + list_for_each_entry(port, &portdev->ports, list) { + spin_lock_irq(&port->outvq_lock); + spin_unlock_irq(&port->outvq_lock); + } + virtio_reset_device(vdev); =20 if (use_multiport(portdev)) @@ -2153,6 +2222,13 @@ static int virtcons_restore(struct virtio_device *vd= ev) if (port->guest_connected) send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1); } + + /* + * Allow TX paths only after all port->out_vq pointers have + * been reassigned to the newly allocated virtqueues. + */ + smp_store_release(&portdev->pm_freezing, false); + return 0; } #endif --=20 2.43.0