From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) (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 08A063F5BE3 for ; Fri, 15 May 2026 22:56:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.177 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778885809; cv=none; b=Mnxx97W3uMh0np+VzPgzkqmA5Ycn1k1IbaSsfEUfZi7M7QhswSJB1riAZpJNNhviG3SYtGGxQyyepMcFzdKeTveFab7rnc4EULZetG0gpcnc4Jp245iIi7ovYSBHD0CqH4bC5zXcAKXS4E8FTiZ72HJKW6qD1IVVOiEPTEkhmZs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778885809; c=relaxed/simple; bh=rXwApuFN1SdxpBDQ2BXUqq0bmhkFOjR/Es1q6yrCnu8=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=Vkbh8TIvVpQCSEejUaZsT2zNTwtLrhNl2TiOXT08/SefJSYTHdU2pJscvhK0TJ41aEYasFNn+VfTlV/muP580spjYIwiLd1gyNxKAT8QkUnK7BO2ndYLm7giU7sf7cUFkZIMCd9qBBWnuG28rkLRqg/pDhHSk2n1Er78DvnBdcQ= 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=iSYLXqZg; arc=none smtp.client-ip=209.85.214.177 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="iSYLXqZg" Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-2ba856db1c0so3361855ad.3 for ; Fri, 15 May 2026 15:56:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1778885807; x=1779490607; 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=c8et1in+2rcZs0uws1Ukv+gI0DMN5vcCabd9NrHMTYY=; b=iSYLXqZgoQS9SjSIIVPFMjcMJWi/HdfNtdhzQmFnMW31K9LDuC8JctnYQMZyoKboSG yHE3L0K0rmAEv9zU2z5RshcrQQKG5bugu3puCOBjTp7sZTn8jtukfitInlYPd3KCsYb8 26QLT9USnA4zDP4C+6DYFRFYscOIrgWgvvv8syymDo44n0QsWk5s7RLH+4V7s7t1y/Wp xV1laz0T2lN28a6y4GfIip7QELk6PJkoZ/jW+ktyFwPgPPWXhllmZctcFsdQyb1D7b65 TA/FrdUyYK4DOYtP0/NSYju1NCAaISIcOUO2HTILM0SE7n9fXpSNm5T/vwXntMv4vGVf D3YQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778885807; x=1779490607; 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=c8et1in+2rcZs0uws1Ukv+gI0DMN5vcCabd9NrHMTYY=; b=PAZ9fO6Yl/pXizoit3TsKiQCPI0CfwlLRT7XS07hiuZ/d5iQyErClrSgsEGoMY21Lo OzcAIFVj21qdd7R73AzgpQQ9jSP+sldELQQ4WxNu3IXwycVqsrgOC9kw0a8PhX3JE/ZF H7XfpTA1ZoOo2545M4iQgi7gXzESRUs4ms8F8v3vcaX1jhQ35aVhk3anJxtCI3OFqdb7 g2/EjWHd99tLxdIVTbQizYCgIKowwTksmkBkjvUqnATVI2vDSQCkydy2I/mElzmw3ikY VSXWVRdRQxJZsyHNqxf3oXTcXhH+SfOT31khb8e/gLRuCucfwxYeVCi3X/zUQnk0AQrg J+5Q== X-Forwarded-Encrypted: i=1; AFNElJ80f3Ahx0H4TueGLuOc117wqxuDj3u7rDWWuMofYut7JhL7lc4lZLKxujlhCV77oAs+ll1fnrD4JBHmGNA=@vger.kernel.org X-Gm-Message-State: AOJu0YyiGcB4WY29oASb12VChQIVnO1R36Q7lUBv+izD1vAirebqjtoX esJ9WfadnrGnZ59GTefYFY6S7wdrHFOvvcmmvrhnafdvm2gfCPlg5QSE X-Gm-Gg: Acq92OEOLpVSmWUOm4y6rQ7rbSIEdfqcukW9V4FeAnhH3jeJEBN+I2hp7HbxPBzRDMH xIa6KpndTgc6G1WCrgw95lzrTLAyeirE6Xbe9ZBnyzduWEmSrkyYhMpnWlil+G9k+/Ou/FIN7qU BSCaQk99jwTPZSisaP15IvxcuUX1blEnDLZ0J4rTG4F1imDr48sU8pmdl+lr+vsd1uzbsdUxL9z ypnE3e6NqjSf1z8Nd87586aLF+05nIwQRaHzY9mz4z/q8+KgSeDCKJ6OFZl83tV/oB8pv9elfvz YtqRU1M0hvgWEZSrgFBZvjAsDG1zd/VbJYbiBoTG2an16iqC1NCgOc9BvYgjV2WERb3rkcdPY/J lSFjam0rgFd5Fd21TDgTY9K/ry5pdtoFAcKTSbNVflPflcy8w3kH6OEmwn6GgJxa2HxhMlNqGfJ V01slebkRu92u+ucNH7b024Z2ONzH9zat4YSybQjkPBWugrrKqKMTpFEp5+dw9en3l4eOM0mMX5 +k9DuyJf+7UYiMXoNY4TLM3RmE1iZr0YwRT1A== X-Received: by 2002:a17:903:983:b0:2bd:8395:fed8 with SMTP id d9443c01a7336-2bd83960609mr58212275ad.27.1778885807082; Fri, 15 May 2026 15:56:47 -0700 (PDT) Received: from baver-zenith.localdomain ([124.49.88.131]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2bd5d0fcb10sm70438295ad.60.2026.05.15.15.56.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 May 2026 15:56:46 -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] virtio_console: fix race between hvc put_chars and virtqueue teardown on freeze Date: Sat, 16 May 2026 07:52:59 +0900 Message-Id: <20260515225259.1054-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: 8bit 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. This races with virtqueue_detach_unused_buf_split, which expects all descriptors to be reclaimed during teardown and can hit: BUG_ON(vq->vq.num_free != vq->split.vring.num) Root cause is concurrent virtqueue_add_outbuf activity from the console TX path during freeze. Add a pm_freezing flag in ports_device and set it at the beginning of virtcons_freeze. Check the flag in put_chars and __send_to_port to drop output while freeze is in progress, preventing new buffer enqueue before vq teardown. Clear the flag on restore only after all port->out_vq pointers have been reassigned to the newly allocated virtqueues, which prevents a use-after-free window where TX paths could dereference already-freed virtqueues. Place the pm_freezing check inside outvq_lock in __send_to_port so that it serializes with remove_port_data, which also takes outvq_lock during freeze. This provides a formal guarantee: once remove_port_data returns for a given port, no concurrent __send_to_port call can still be adding buffers to the vq, eliminating the race window with remove_vqs. Use smp_store_release/smp_load_acquire for the flag to guarantee correct cross-CPU visibility on weakly ordered architectures. Signed-off-by: Sungho Bae --- drivers/char/virtio_console.c | 46 +++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/drivers/char/virtio_console.c b/drivers/char/virtio_console.c index 9a33217c68d9..0fbe40a7f1e7 100644 --- a/drivers/char/virtio_console.c +++ b/drivers/char/virtio_console.c @@ -157,6 +157,12 @@ struct ports_device { /* 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; }; struct port_stats { @@ -606,6 +612,20 @@ static ssize_t __send_to_port(struct port *port, struct scatterlist *sg, spin_lock_irqsave(&port->outvq_lock, flags); + /* + * 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 (!port->portdev || + smp_load_acquire(&port->portdev->pm_freezing)) { /* pairs with freeze/restore */ + in_count = 0; + goto done; + } + reclaim_consumed_buffers(port); err = virtqueue_add_outbuf(out_vq, sg, nents, data, GFP_ATOMIC); @@ -1115,6 +1135,17 @@ static ssize_t put_chars(u32 vtermno, const u8 *buf, size_t count) if (!port) return -EPIPE; + /* + * Silently drop output during freeze: the console is still active + * with no_console_suspend but the virtqueues are being torn down. + * 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. + */ + if (!port->portdev || + smp_load_acquire(&port->portdev->pm_freezing)) /* pairs with freeze/restore */ + return count; + data = kmemdup(buf, count, GFP_ATOMIC); if (!data) return -ENOMEM; @@ -1972,6 +2003,7 @@ static int virtcons_probe(struct virtio_device *vdev) /* Attach this portdev to this virtio_device, and vice-versa. */ portdev->vdev = vdev; vdev->priv = portdev; + portdev->pm_freezing = false; portdev->chr_major = register_chrdev(0, "virtio-portsdev", &portdev_fops); @@ -2092,6 +2124,13 @@ static int virtcons_freeze(struct virtio_device *vdev) portdev = vdev->priv; + /* + * 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); + virtio_reset_device(vdev); if (use_multiport(portdev)) @@ -2153,6 +2192,13 @@ static int virtcons_restore(struct virtio_device *vdev) 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 -- 2.34.1