From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) (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 5FDE03DD87A for ; Wed, 3 Jun 2026 18:39:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.173 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780511954; cv=none; b=VnU2YIUnxNVTX8Bg85wIdO9aqRE4aSCEgXoj0HkvdlBwKIOYUYqmhwtT0XBuouLrDrYXv28kXm8ASdVS/uXvhEOBvt+V1vt/tXq5d2NakVr8hgBuFKps5Y6OAvDMfqo1+W6UY8eNuex0GdBZDdwPrEP/tD7JcZmBUq/IZfypcN4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780511954; c=relaxed/simple; bh=ymB6v4fxjeHGAHyyQBBzUFLJbQz9tkxCo9VxIY+IdDY=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=cjlbN/TYxueB37q+3kfvNuYXN06LjUN+UWA72pKbEUXb02W/VLqy6lnPptZIm1uq/YLhw49J89K0NJfIldpXa2YYpIzheTIDMMcrzANGOO7RhjFXeqpxcm8N/2L9teaITU9HkEAo2R3kCNmDb81ejqojkE6xitaAIcduciejZAo= 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=gs1DYdJg; arc=none smtp.client-ip=209.85.214.173 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="gs1DYdJg" Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-2bf20f6be6bso35955195ad.3 for ; Wed, 03 Jun 2026 11:39:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1780511951; x=1781116751; 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=E7ONTJkweAcZcnKMGezhHMuN65WNnCKGBpfevNj/QjE=; b=gs1DYdJgRMMuiomNuR8zGPGtjDC/oBPdVpm5rCkq787eP5YHu3Tnouf2YIwbK1OE90 GgaBOiDvFW7K1pRcA1AN5W5sDaxhJEqFvkaHYyAgcBIOoK+B45ZdCR9IPsaM4X8hOMfD Mrrb3g3NkYxuLR/kZkpOGscZRRuMGrtj+QjOnPYARmkhGkpkCh4N+xyp6osMMvw2s2m8 g7Qk1/EeW3lPDymlUkFZ97EhPShXfoYAJYBqtPi74JyT2ui50VXu3D9f8d7LV6qXYx1C 8PrErkpUbLu8t6YXrI+DTnvbipuziRDrDxXgViqPQ+7PPik6xPvvErk3XV2UEEmlRLXf dXGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780511951; x=1781116751; 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=E7ONTJkweAcZcnKMGezhHMuN65WNnCKGBpfevNj/QjE=; b=QdwScCe0T3Z0rs+vcT0T7RAYUZSoH2Oj857/fkscIHgKRWiHeAGHK67x5FfGCWVo4P LXpjg/IhEulJKF5/jzBCtwfA44mhXSscuHyqW+gxbNZOF2zjWcTUdhqd+I2ei1am3r09 CWW+0EdTT1Q09/jBuPuOoIY8BbFyT4xgPQJz0g5oymCLvu9uDu4o5bimeaRJ/M4fSK3n o56gVvgCLa8qcn/HTfoVo92MAM+If8wir2a8oZhpgqCdE8RJr4/55oXSgZ/b9c4mhrUG 7bPFukjVR+b7BOholoUuu/cWXDQ5mBu5TM3qWvCz2Wa8Me0cr71WRM8PJhbuqV/l1u8F Fq2A== X-Forwarded-Encrypted: i=1; AFNElJ98xZGSgqf+ICH9nBiL7WBGOPPWn0/OAJddptYm+RvOXR3yL5ILAIzBYb8ia78CAJY3PgUifFNzebHh974=@vger.kernel.org X-Gm-Message-State: AOJu0YwrgN3DnwpvHUC52pCRCYiRRM0OOrSbFBJpeNKkjh444kOTS/D7 NZ7oO0MqypcssWiSQU3ORjIsXWYanipkmPm5QApn6kQGprL5npzt7wsOwLOUvoFQ X-Gm-Gg: Acq92OFvkY62jnlo/zGQhx9SuKMzu6yg7U3qUGoPLwJ2wYr3J+6MJJGBa3xhgoN1uH7 FLvVYYhZyGNfP6kzHk4zI3vtbYe+P4RHzzaMmZVnO0ztnU54saTQZj2ZIxdgfQOQ5AUJ1G3arNi QushFioPWaONEsLU7uZf4tNp0iOzytThEV1KA7w0fNM51UJz4iuowXXhM4iGma/GANZj+0vZxqD 2OzDudTeZk264CBN6pGGHrlzVfDn+32kmqgcxd9jphNPamVj/eRCj2NxZoMhOcQ3nC6YeRC+2v1 pmJL/Ja3tAP0+3jhUhGyGYqjPR9J1AbKN8RyGWEl+QGnaQf7QtEPk0wTo9Upa3zBt0gzC+6vYu6 FeNfLon8mfdoAzubc79AgCbofzNImA3DrgbTemTAhCRRIGdzNnNgO+RZdjW2RvnyM5Z5QU2r2oS geh1KK3XItAoTSlZ+awbeV87cL21DTYxqa1/PE9n/+2yxW922hlSQp3/12Gat3XPW8KA81fLsT8 QjGJ4LctNl5WNiuaCqFkNjYPwRHsmlEAGtv5Q== X-Received: by 2002:a17:903:288:b0:2bf:1486:e6ca with SMTP id d9443c01a7336-2c163d61cbemr51934235ad.12.1780511950513; Wed, 03 Jun 2026 11:39:10 -0700 (PDT) Received: from baver-zenith.localdomain ([124.49.88.131]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2c16649c01asm32764915ad.74.2026.06.03.11.39.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Jun 2026 11:39:09 -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 v3 0/4] virtio_console: fix suspend/resume and hot-unplug races Date: Thu, 4 Jun 2026 03:37:53 +0900 Message-Id: <20260603183757.21587-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 Hi all, This patchset addresses several critical race conditions and memory lifecycle bugs in the virtio_console driver, primarily surrounding the TX path(`__send_to_port`, `put_chars`), device hot-unplug, and PM freeze/restore. The initial motivation for this series was to fix a race where `hvc_console` writes (which can remain active during suspend if `no_console_suspend` is enabled) could enqueue buffers while `virtcons_freeze` was tearing down virtqueues, leading to a `BUG_ON` in `virtqueue_detach_unused_buf_split()`. During the investigation and testing of the freeze race, several related vulnerabilities were uncovered in how TX paths handled buffer ownership and stale `portdev` pointers during concurrent hot-unplugs. Because these fixes structurally overlap in `__send_to_port()` and `put_chars()`, they are submitted together as a single patchset to avoid merge conflicts. Patch Summary ============= 1. virtio_console: refactor __send_to_port() buffer ownership Refactors buffer ownership in `__send_to_port()`. Previously, `put_chars()` would allocate a raw buffer, pass it down, and `kfree()` it immediately upon return. If `virtqueue_get_buf()` returned an older completed buffer from a previous non-blocking write, the newly queued buffer could be freed while the host was still DMA-ing from it. By transferring ownership of a `struct port_buffer` to `__send_to_port()`, we guarantee only the exact buffer returned by the host is freed. 2. virtio_console: fix hot-unplug races in TX paths Hardens the TX paths against concurrent hot-unplugs. It adds `READ_ONCE(port->portdev)` checks, bails out cleanly (returning `count` to prevent `hvc` infinite retries), and synchronizes `portdev = NULL` in `unplug_port()` using the `outvq_lock`. 3. virtio_console: fix control queue race during restore Fixes a race during `virtcons_restore()` where filling the control receive queue (c_ivq) immediately after `DRIVER_OK` could trigger the control workqueue before the driver finished restoring port states, leading to list corruption. 4. virtio_console: fix race between hvc put_chars and virtqueue teardown Addresses the original PM freeze race by introducing a `pm_freezing` state. TX paths now safely drop output while freezing. A synchronization loop in `virtcons_freeze()` ensures all active TX threads have drained before `virtio_reset_device()` is called. Testing ======= Runtime-tested on arm64 systems with `no_console_suspend` enabled during S4 cycles. Changes ======= v3: Split the original monolithic patch into a 4-part patchset for better logical grouping and bisectability. Sungho Bae (4): virtio_console: refactor __send_to_port() buffer ownership virtio_console: fix hot-unplug races in TX paths virtio_console: fix control queue race during restore virtio_console: fix race between hvc put_chars and virtqueue teardown on freeze drivers/char/virtio_console.c | 177 ++++++++++++++++++++++++++-------- 1 file changed, 137 insertions(+), 40 deletions(-) -- 2.34.1