From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mx0a-0031df01.pphosted.com (mx0a-0031df01.pphosted.com [205.220.168.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 3522019005E for ; Tue, 21 Apr 2026 14:24:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.168.131 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776781451; cv=none; b=hCaurj6XJLh2CUCNrfDfRrij5CZUTnB0KaBUYL8CMKLcoE3fBsoUBmarhAueOl8210/dg2tVKGip5SRB4Lx09VbgQsHesRnhK12Ux4FpY/e6kruzwlzyl1M+bWFv2Rk7f88Hlsbl9HegtOzCbUf+9lUB6nLzBR8k353pKE+CNQM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776781451; c=relaxed/simple; bh=1rXHSuH/I8GRMhnd+YsBX5x/JhKjP7jhEEWF4AlQlX0=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=oW4Vnfg8WPW7EanRt0GQauX1gSmsLBBdTcLXsKo4JnKfs8iZ2BocZA2fqpVTZ/bDBv5q221e693Qf7Ko1srRiamxFlVcGYlEmGu04P4qM+LT5jR/28Mk+kQe/UT8nqhMkL8M1oXvmkhGW7sjoHam06Eq/y5DPDg8rF6xI2kjjm8= 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=mWdtrbFk; dkim=pass (2048-bit key) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com header.b=cbUtTmEc; arc=none smtp.client-ip=205.220.168.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="mWdtrbFk"; dkim=pass (2048-bit key) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com header.b="cbUtTmEc" Received: from pps.filterd (m0279865.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 63LD9aZW1538507 for ; Tue, 21 Apr 2026 14:24:09 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= 61L2ICsEUf0Di4YW/bbJVwneFaoftw7+9hq+b7ipqCk=; b=mWdtrbFkSxX8QzVG VUuusnFw+HlEV6CENPpTGY8CBox2MAkmROJgyCnPXLZUyI3FyxO1eL7yDdIcTMfe 6zgOYgwLJGqv0R62TZtlhrNTwaZMnO/jFHoiV3ZYI6V3DzqxtiT0L4gwC1cQWERu Jv2L6HpNQiWAGC5xckhzZApwzMNZDNQeFcx+lR5FeoXyV5DA74dTTOAChHAJMi2C xhHJQMzrtaMrmjPgP8x24FNZiBnNcQ541mMa5jMsra6lPMMddnPgNoLVUI32aL/P LCDUqWVM59ME8NesuilI9KTegF8eU2EwEHNw3r+SDq0QmqmX/G1ISAK+Oq+N+OS/ TQmmYg== Received: from mail-qt1-f197.google.com (mail-qt1-f197.google.com [209.85.160.197]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 4dp4hahkb1-1 (version=TLSv1.3 cipher=TLS_AES_128_GCM_SHA256 bits=128 verify=NOT) for ; Tue, 21 Apr 2026 14:24:09 +0000 (GMT) Received: by mail-qt1-f197.google.com with SMTP id d75a77b69052e-50d890580e1so23513891cf.3 for ; Tue, 21 Apr 2026 07:24:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oss.qualcomm.com; s=google; t=1776781448; x=1777386248; 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=61L2ICsEUf0Di4YW/bbJVwneFaoftw7+9hq+b7ipqCk=; b=cbUtTmEc6CbzUuXlPcuCyxcjlk1aDm+eE5blPWzNyd5hPnsCvle7oDCBjB8LKLks62 arG1pfV7/qWQe0FQ3G81SRTbSvEmAWEl2NH3hVD8ZBMviD7aXl/F+bCoT3VSnmaaiNgd 4SgrIN+7LVIqS+U2rR+rKWtr+9jBD+igrDu8yJFcNhMZpY5/sj9sRuvSfMHuvob724MG C1uRTuBe7jg62vFTOJ6MKRoxHWUzbEXq3W29FV+dpaLWlAXeGCxcu4Lr1AkAFaxHgWKW CgdjwHzgbBVIeAovNekaJ1SYQRLS2Y5XXVCzDSV435UYw8LU0BFz+i4Tu0z658b0IvAs vBhQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776781448; x=1777386248; 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=61L2ICsEUf0Di4YW/bbJVwneFaoftw7+9hq+b7ipqCk=; b=HyHXOSwQV99J9cheVxcfrfy+DSpBf7iTIWoA7bkV+Younm2Kliv5IKpJx6Y8LQPLty nfMja6OZO8/xLJ1T+RylUKFHun7Clr8jVY3OorL5LrVpOZhKhcGbY6SM1kdY/3pUCz+C BpE9dzDEjdNIq2Eh0qg8zRQIRlo+4jxupfcypB9kpbkefOX0b0YWhgGsA81c7UZPeujN IsWTkGdpXrVY8gFcQhECSAdysMJ5BapN6hjFm5Ct+VpW6novAQpYinwth/aRpihmMcJr EDlIfaija7jmRDrM0RDAKQ8TgihvbtF6XvhVoUSkPAheqheaSxnn7fmScBMAv8StnLhL ThhQ== X-Forwarded-Encrypted: i=1; AFNElJ/3iHcMRjgy6xWJFhtcxaNOJU+DDDVaevAdzEw6wS/OI/p9OnLG5BTq5QYdOYloteN2GS/as8NQ+Z2t7zM=@vger.kernel.org X-Gm-Message-State: AOJu0YyL5OAZ99RYQQPA9q2fMIK07+boKiEH27BT2UX1ZBlCsdyz4eej r0cdnDDikvgTY7LyQXkIEouk+wj44m3/zWM0bNCt/9EZuL1q52MXcgJNIlvgkhqW/B/iiDMu1I2 RD4biVij3hjlU4nHJFjAT7cjzEG2/QO+MnyGa92XbiL5we5467JUA7C8GTXHdRN5/BD8= X-Gm-Gg: AeBDiespbr65zCuJ/9CPwOjMkqm9CIhfwg75e6CUklga/0jgaSZmGGqU59S+ns9zo6c hJX4rBYGgaUjI2ZvCsLqcLUu6hwutQLyvRtD1wFO6a1VElqLaIW18lxuYRdwUNKId69YF+r1Gr6 dtNBKcrRevTiNsvqeLb3UXUizHdneGVDxV4QXGxr8amiUjpUXcm/V23+G9eOjJuu/Gh7O/RLQ5K ByY+pNmnkMbtJ+ncSVBdgDPLqoPMXYOVhMDHuyU/b5TXCujsqnmkMmQtUY7C9QyidHrK6frv0dE BOiB5j/pTvyejCKk4nUW5Qy7+MA/JgYymbMYRS/sSdBSVhXl+FbNOj+CTEUAuUbcE7NnuL0l/rB 5Ba0KecQH7pK8qaXAjmJ4EzquRdW+YhHEc3HmIXd3w7Da5C6iv9+Xzqix X-Received: by 2002:a05:622a:11ca:b0:50d:a8f5:1bef with SMTP id d75a77b69052e-50e36ec1d54mr263474541cf.46.1776781448345; Tue, 21 Apr 2026 07:24:08 -0700 (PDT) X-Received: by 2002:a05:622a:11ca:b0:50d:a8f5:1bef with SMTP id d75a77b69052e-50e36ec1d54mr263474131cf.46.1776781447775; Tue, 21 Apr 2026 07:24:07 -0700 (PDT) Received: from [10.238.138.234] ([114.94.8.21]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-50e3fbc6513sm99310751cf.2.2026.04.21.07.24.04 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 21 Apr 2026 07:24:07 -0700 (PDT) Message-ID: <2cbef155-2a84-4151-8081-b33c79ffae68@oss.qualcomm.com> Date: Tue, 21 Apr 2026 22:23:59 +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-Authority-Analysis: v=2.4 cv=bOQm5v+Z c=1 sm=1 tr=0 ts=69e78889 cx=c_pps a=EVbN6Ke/fEF3bsl7X48z0g==:117 a=Uz3yg00KUFJ2y2WijEJ4bw==:17 a=IkcTkHD0fZMA:10 a=A5OVakUREuEA:10 a=s4-Qcg_JpJYA:10 a=VkNPw1HP01LnGYTKEx00:22 a=u7WPNUs3qKkmUXheDGA7:22 a=Um2Pa8k9VHT-vaBCBUpS:22 a=i56tckeGD7E5QaJy4WgA:9 a=3ZKOabzyN94A:10 a=QEXdDO2ut3YA:10 a=a_PwQJl-kcHnX1M80qC6:22 X-Proofpoint-GUID: X9o7Sq5SXqVPshgXitRcSCFs7Wzbwf6L X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwNDIxMDE0MyBTYWx0ZWRfXzZ9LEaI+PUrQ avApyZ9pxUEoLpnqw+qoknzx6ytkuyypCMF6K39FBlb2HRHap8X9qnN385tn2w4sfYoIFkvdSQ1 8WNC8t7TUQNlq6H5LRQtaFSfUz2nJidc6fNzLe7J7aFC+7qBvEskycKcS+eOWTEJ91ouW6/qnEi S1eoe24HwdnYhvvA1XMJBSFudUtE1Ou4JzTe9RDUz8lFk9yI8/LcLclfLFUF0qEBidtxBOogmqF 00y4Dhx8tITrXybjXQeFYV+tCZ6fU+B07Em2IHPzEWAV2xvEWDlCwaMVVmnLYETVBsbongKcen8 R+gc4WZ8aGkAbqmSL3EuRyW66NOrovNdyN5MXDMSQfMerImQQsHaDND/6fpY33m7VKdTMJ8Haft xtkGhQqKMo+KYieXE94pCkzCdoPFcXIbVO/NoukFvBlQ4dpNV7DEK0627c2JvkOx7kshdGH1cFe 1m5HKIg718s1kpyeBdg== X-Proofpoint-ORIG-GUID: X9o7Sq5SXqVPshgXitRcSCFs7Wzbwf6L 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 suspectscore=0 spamscore=0 malwarescore=0 bulkscore=0 priorityscore=1501 clxscore=1015 impostorscore=0 lowpriorityscore=0 adultscore=0 phishscore=0 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2604070000 definitions=main-2604210143 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. As a result, vq->broken remains false forever and the loop never exits. >> 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.