From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 288E43D7D74 for ; Tue, 21 Apr 2026 14:11:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.180.131 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776780703; cv=none; b=eeflvCbgJgNmSEWYyBMYc1tyJQ09wUok6jPT2tlgB8d8dYUStQXYmjXPlsz4VgZ8HXG1YNSJukdPjWD0WWc6oO4dEjX96u8kE8aOxj560EZ6MiPy0VLzPoEXfXefLv0c9PEu6Zphtt9AIt1Pq87lWLIxhgY6dYq57g4L9gCYARI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776780703; c=relaxed/simple; bh=XT8yfTFbS399P/bczJuLfIr9YjZccWUmzpfF5mpxA3M=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=hpETOiHMZ1kbJNBqHs9NhQw1XIyPst0GPyUWhh0rVKvqS3HqLLPstleNeWU/YWzyW/v2fYtfQlr3gYsfYsGzcvq742ldOvRYY/bvpX/0vNxQDOHaPhGsJRjrNX9o691kdK0pbVCQx0fs3WUxC32flf4jePjyCw13KA3zGBDHHYM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oss.qualcomm.com; spf=pass smtp.mailfrom=oss.qualcomm.com; dkim=pass (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.b=C9C1XN6/; dkim=pass (2048-bit key) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com header.b=Y0bYP2y0; arc=none smtp.client-ip=205.220.180.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.b="C9C1XN6/"; dkim=pass (2048-bit key) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com header.b="Y0bYP2y0" Received: from pps.filterd (m0279870.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 63L9XVvB3164709 for ; Tue, 21 Apr 2026 14:11:41 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qualcomm.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=qcppdkim1; bh= YMdOsIeK5M2JLQ5enQsUgM+cE8gxR0RsxwL5DeBdtDs=; b=C9C1XN6/hwj4hD84 +s2KccwH+CmktlXnRXaMdUnDjbZ3k5c/Mu9ksI7CZX7qotDHhr1AyY01xBc8bK4j 8LPVNzRR+OMIeyDv7JW42iHoXFqAhqH2VFXCpTSjeCMlKCKul8+eRQ1cw1RQgT8W VOGEUe77bB4uj8ihwMom8PYF8DBENGz6un+9RvaKCpS189Y8PAR/AxBmqoFTRUMf khRd+e9qxyAxQhCldfvw52gvt+2JPQBch8PWq65rEzZPG3l4W1DdBXjkOb57c5Jx 8hPQ+Nx2gRORep1/LUrRoP4JJ7rFhDExD1f8AjL7MPkQbSIH/uTTrjKzHPmMZmfv 5kAeng== Received: from mail-pf1-f199.google.com (mail-pf1-f199.google.com [209.85.210.199]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 4dp6q80x9m-1 (version=TLSv1.3 cipher=TLS_AES_128_GCM_SHA256 bits=128 verify=NOT) for ; Tue, 21 Apr 2026 14:11:40 +0000 (GMT) Received: by mail-pf1-f199.google.com with SMTP id d2e1a72fcca58-82f07078ff0so2925713b3a.1 for ; Tue, 21 Apr 2026 07:11:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oss.qualcomm.com; s=google; t=1776780700; x=1777385500; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=YMdOsIeK5M2JLQ5enQsUgM+cE8gxR0RsxwL5DeBdtDs=; b=Y0bYP2y0JIYCf3qp0rOnwbwl4b70fbYJuxDnCszF62fjZeOOwvGJk6L8A9npi+dfXn 8VEM6ili18hXbAm/dLjuF+U++bfEHZR0fIBe/IsmXKuB6O5qqtoOMPI5RqMuZYDtRyr1 hl4pTle3xAa1znAt8uvaSNo38vAwWnjkjEvN5cfihE2KUbrwagmThYiLPSxkTRRg9PKy sYXzV2ibOn5IPmalHCn/WzbE3bkYbVYjtciHvvgcwYS9yXCpco/95f5bGQGNyMd9U9+c 5WWgPPewwqw7Yy6hu1/BSgUIZbGebQrqQ0IS/SsvBEH4FAFTtGB52gz9X5wryUszwE/5 soNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776780700; x=1777385500; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=YMdOsIeK5M2JLQ5enQsUgM+cE8gxR0RsxwL5DeBdtDs=; b=EzlCWBtwJnbfkCAqOx6yjbCCaRX+BfX4yaM1srpnlpW+jcmxHYY9AFhRnZyEd8k2w/ xxmT3dE+Z0Suajs5x5ZUeV+qaXowK4UppDBm2OoQPEkUhktCWJxcNYRBtECkHli6ft3F tGuLhf5wZQ3OxwOfm1c9R2BBS+iwA8vDmGUFcM0tWWVw2Tz/jbfQuQyVkHkEJa1/R6hM veysuHfo0Cu/5ysG76itZVhHZ2HRMtAvac9RJ2E0MZF5rNLfHHUn8URjL+UayHfq6e67 3DWgVg8A28pMmWW0lMngA3MIZKHqIl88xVmBTg6fwHWw8ME8NTUnvi5CYBTSC80Mj33d UVnA== X-Forwarded-Encrypted: i=1; AFNElJ9NvQMNhPR4ETZlIpW7ySVuaE1OkawgulUho+v4i6ODVlwzJGkZfD/qvBDkvkBjmWnAjf7HSnZzeDdG1nc=@vger.kernel.org X-Gm-Message-State: AOJu0YzR58fqFQasAIck4IdtI4ulJBb5CV/5Sn3KvtgppyFBM0L+hSfA gMMC8nvRdf58eODC2762DXulU5fH9FXjQnNd6Tkqr/w1v8EaR0tTVMQLXsaecIjpT72ZDfyRcOD SS6lqcEuxo6AurdFoLvd058I1O/vRiyxhRV2fmdWCVLYgg56upySUPshxp7FlXJAB7zw= X-Gm-Gg: AeBDievvJCYwvhZT1NmubX2WZ2XlmjgINDN2Wkf46N0RONBnh3lLuVKu+Ckt7hViMmX 6c1wZ7NS5h4YysjrihFs/wyL9mUbj8aduuqd9w+ab0LSU3ilG4s2osojEAdc5g8VwjR/9X6/4OT daK9JYhXJdY2jzR3uxU6yeQNFQuDQzvPNmRdmVL+1ETM+6F+0tUyTZBGTjwhQ2kFcknLlTR8npc hXwHVYsD2uv4ZkzqQT1yHbgBr83JlqdcFiIdP9O5KQMrPG6ZML6wPTx02B89sP+Ink/kmQGfcS3 vxJOFqWP6PT7jIZzzpNql5WNzrlZMljDG/W2I80O4NyWizVviKDPOhKNlwmYZ4h5MlG/0aXve7/ yvfouFMqsjJEpWgI1f6b5wLA8GV5aw/HowGHfvEC40xl/TAWZw4XPxkX1 X-Received: by 2002:a05:6a00:a248:b0:82f:316:3208 with SMTP id d2e1a72fcca58-82f8c8fb879mr17264449b3a.29.1776780699658; Tue, 21 Apr 2026 07:11:39 -0700 (PDT) X-Received: by 2002:a05:6a00:a248:b0:82f:316:3208 with SMTP id d2e1a72fcca58-82f8c8fb879mr17264410b3a.29.1776780699089; Tue, 21 Apr 2026 07:11:39 -0700 (PDT) Received: from [10.253.14.183] ([114.94.8.21]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-82f8e981829sm17064601b3a.12.2026.04.21.07.11.36 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 21 Apr 2026 07:11:38 -0700 (PDT) Message-ID: <1fd9f904-19fe-4602-904e-6a30fba20061@oss.qualcomm.com> Date: Tue, 21 Apr 2026 22:11:34 +0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] virtio_console: add timeout to __send_to_port() spin loop Content-Language: en-GB To: Arnd Bergmann , Amit Shah , Greg Kroah-Hartman Cc: kernel@quicinc.com, virtualization@lists.linux.dev, linux-kernel@vger.kernel.org, kernel@oss.qualcomm.com References: <20260420-add_timeout_to___send_to_port-v1-1-6c32d33bf4f2@oss.qualcomm.com> <37b513d6-770b-4442-b3b3-93b2801f3c71@app.fastmail.com> <9e5be98b-3035-4e61-8325-52ed8fcf65fa@app.fastmail.com> From: Peng Yang In-Reply-To: <9e5be98b-3035-4e61-8325-52ed8fcf65fa@app.fastmail.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwNDIxMDE0MiBTYWx0ZWRfX5wesi/3q0Yo8 7LtrhvMmiXj5lJgfoV8pHJaHbuoX1A/DKcp5oyvD/5hjBlR/j0nhCckoilw5zBB+GJsMkcCbtAw YMjuvn2Rk8ei2MWqLTaDhgHv2z10DwhDS8jIsXhSqc3hsNrVLwVxubq2O+EEJ1rCDp+4Vfq+C/v XYhJb7ws3H0izI1/OsnGaU9vaVPPtgXTX81mzFYa3FIEbiwMUut6Vwji9rdnanig3WY+QBG8OTp JcOVD1di+c2ToFjBF8RfQgCG7xMFpyO074b915rPhB/4ba1x7cAvNLZcofuKe5mkgz4M92bKRMF DmywsZPfKIoDXLlvmk1PbfGEM6Opc4SjzswWpqgHveZTOlIwhcXOAsXvb4dHWFnxldGQpbpYZUN u7GrILJP/ykSkb3jeVraB8aKTi1n6HfzDw6sh3BmuHtHcc+c6FFXUxEKkepyBJJRbB+ihDtyQh5 qntJw70+VexDEL5UEYQ== X-Proofpoint-ORIG-GUID: ze5_v3XzBwY7cBrMIOiRlukYtEktql2g X-Authority-Analysis: v=2.4 cv=eOYjSnp1 c=1 sm=1 tr=0 ts=69e7859c cx=c_pps a=WW5sKcV1LcKqjgzy2JUPuA==:117 a=Uz3yg00KUFJ2y2WijEJ4bw==:17 a=IkcTkHD0fZMA:10 a=A5OVakUREuEA:10 a=s4-Qcg_JpJYA:10 a=VkNPw1HP01LnGYTKEx00:22 a=u7WPNUs3qKkmUXheDGA7:22 a=gowsoOTTUOVcmtlkKump:22 a=i56tckeGD7E5QaJy4WgA:9 a=3ZKOabzyN94A:10 a=QEXdDO2ut3YA:10 a=OpyuDcXvxspvyRM73sMx:22 X-Proofpoint-GUID: ze5_v3XzBwY7cBrMIOiRlukYtEktql2g X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1143,Hydra:6.1.51,FMLib:17.12.100.49 definitions=2026-04-21_02,2026-04-21_02,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 spamscore=0 phishscore=0 impostorscore=0 malwarescore=0 bulkscore=0 adultscore=0 clxscore=1015 lowpriorityscore=0 suspectscore=0 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2604070000 definitions=main-2604210142 On 4/21/2026 8:14 PM, Arnd Bergmann wrote: > On Tue, Apr 21, 2026, at 11:18, Peng Yang wrote: >> On 4/21/2026 3:38 PM, Arnd Bergmann wrote: >>> >>> Which host implementation do you use? The way the virtio_console >>> driver works really assumes that virtqueue_kick() consumes the >>> buffer synchronously. Even though that is not how virtio is >>> specified, this does tend to work. ;-) >>> >> We are using crosvm as the host VMM with its virtio-console backend, >> running on Android. The trigger is Android host reboot/shutdown: when >> Android initiates a reboot, the crosvm process exits and tears down >> the virtio-console backend. At that point, the TX virtqueue is no >> longer being drained by the host and will never be consumed again. > > I see, so the normal behavior is likely just fine, but the error > handling is what goes wrong. Maybe there is a way for the guest > to detect the device being turn down already so it does not > actually have to wait any more? > Yes, exactly. Normal operation is fine; the problem is purely in the error/teardown path. We investigated both the virtqueue_is_broken() path and the virtio config status register path. Neither works in our scenario, for the reasons explained below. >> The crash dump from the actual failure confirms the exact deadlock >> scenario: >> >> Core 3 holds outvq_lock and spins forever in virtqueue_get_buf waiting >> for the host to consume the buffer: >> >> virtqueue_get_buf >> __send_to_port >> put_chars >> hvc_push >> hvc_write >> n_tty_write >> <- writev() syscall > > This current loop here is > > while (!virtqueue_get_buf(out_vq, &len) > && !virtqueue_is_broken(out_vq)) > cpu_relax(); > > which looks like the virtqueue_is_broken() check is meant to > catch this exact case. Do you know why this does not break > out of the loop after crosvm tears down the virtio-console > device? > virtqueue_is_broken() only reads the guest-side vq->broken flag, which is set either by virtio_break_device() or by a failed virtqueue_notify() kick. Neither happens here: - When the host VMM exits, it does so as a pure userspace process termination. No PCI interrupt or notification is sent to the guest, so virtio_break_device() is never called from the guest side. - __send_to_port() runs with IRQs disabled and outvq_lock held. Even if the host were to send a config change interrupt, it cannot be delivered in this context, so the async chain virtio_config_changed() → config_intr() → virtio_break_device() is completely blocked. >> Core 0 has a watchdog bark ISR fire and attempts printk, holds the >> console lock, but spins on _raw_spin_lock_irqsave waiting to acquire >> outvq_lock: >> >> queued_spin_lock_slowpath >> _raw_spin_lock_irqsave >> __send_to_port >> put_chars >> hvc_console_print >> console_flush_all >> console_unlock >> vprintk_emit >> <- printk (watchdog bark handler) > > My first thought here was that __send_to_port() should perhaps > release the lock during the while() loop, which should avoid > blocking the other threads on the spin_lock_irqsave() but > would not avoid blocking on the loop. > Releasing the lock during the spin would unblock other CPUs waiting on outvq_lock (e.g. the watchdog bark handler trying to printk). However it does not fix the root issue — the CPU holding the lock would still spin forever. It also introduces a TOCTOU race: another thread could modify the port or queue state between the unlock and re-lock. The timeout avoids both problems by bounding the spin duration without releasing the lock. >> The 200 ms timeout is intended as a minimal, targeted workaround to prevent >> the watchdog bite in our specific scenario. We are open to suggestions on a >> better long-term approach. > > Not sure how to do it, but I think finding a way to call > virtio_break_device() at the point the host device goes away is > the best solution here. Ideally there would just be a notification > from the host, but since __send_to_port() may be called with > interrupts disabled and may be running on the only CPU, that > would still be unreliable. > > Maybe there is a way for virtio_console to read a status > register in the virtio config that tells it whether the > host has turned it off? I was thinking vdev->config->get_status(vdev) > but that seems to only get updated by the guest. > > Arnd We checked this. In our host VMM implementation, VIRTIO_CONFIG_S_NEEDS_RESET is never set on the teardown path — the device simply stops responding without updating any status register. So polling vp_modern_get_status() inside the spin loop would not help here. We agree that the ideal long-term fix is for the host to trigger virtio_break_device() via a clean PCI hot-unplug sequence, but that is not possible in a crash or forced reboot scenario. The 200ms value is chosen to be well above normal host response time (microseconds) to avoid false positives, while remaining well below the watchdog bark-to-bite window (3 seconds) to ensure all CPUs can exit the loop and complete the bark handler before a bite occurs.