mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Amit Shah <amit@kernel.org>
To: Lorenz Bauer <lmb@isovalent.com>, Arnd Bergmann <arnd@arndb.de>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	virtualization@lists.linux.dev,
	LKML <linux-kernel@vger.kernel.org>
Subject: Re: virtio_console: lost wakeup due to race between port_fops_poll() and vring_interrupt()
Date: Wed, 10 Dec 2025 16:58:42 +0100	[thread overview]
Message-ID: <564165392dfefdbb6e1739102ed80c3aee92881b.camel@kernel.org> (raw)
In-Reply-To: <CAN+4W8hLrR1B1pNdDXiVuzoJ1q8Ta1kjV0v8vKVEZeHdRFKGcg@mail.gmail.com>

On Fri, 2025-12-05 at 18:06 +0000, Lorenz Bauer wrote:
> Hi,
> 
> I've been chasing a bug when using virtio console to exchange data
> between guest and host. It manifests in a process inside the guest
> getting stuck while writing to the serial port:
> 
>     [<0>] wait_port_writable+0x139/0x2d0
>     [<0>] port_fops_write+0x88/0x130
>     [<0>] vfs_write+0xf3/0x450
>     [<0>] ksys_write+0x6d/0xe0
>     [<0>] do_syscall_64+0x9e/0x1a0
>     [<0>] entry_SYSCALL_64_after_hwframe+0x77/0x7
> 
> I managed to track this down to a race in virtio_console.c. The
> driver
> doesn't properly account for a port that receives blocking writes and
> is polled at the same time. I suspect that a similar problem exists
> in
> port_fops_read().
> 
> Here is how it goes. We need two threads inside the guest, A and B.
> 
> - Thread A writes to the serial port until the virtqueue fills up.
> - Thread A invokes port_fops_write() which calls
> wait_port_writable().
> There are no used buffers to reclaim and the thread is suspended,
> waiting on port->waitqueue.
> - The host side of the device makes some progress, marks some buffers
> as consumed / used and  sends an interrupt to the guest.
> - Before the guest can service the interrupt, thread B executes
> port_fops_poll(). This calls into
>   reclaim_consumed_buffers() via will_write_block(), which removes
> all
> used buffers.
> - The interrupt gets serviced, calling into vring_interrupt(). Here
> the check for more_used() returns false because the poll just went
> through all the work. The handler returns without waking
> port->waitqueue.
> - port_fops_write() never returns.

Both port_fops_write() and port_fops_poll() call will_write_block() for
checking the block condition -- which is whether port->outvq_full is
set.  Even if buffers were consumed via poll, outvq_full should just
return false, right?

Hm, does adding wake_up(port->waitqueue) in case 'ret' is false in
will_write_block() help?

> I'm not exactly sure how to best fix this. Maybe it's enough to only
> check outvq_full in poll? It'd also be nice to fix this on the read
> side as well.
> 
> I only have a reproducer which requires a Go toolchain unfortunately:
> https://github.com/lmb/vimto/issues/29
> 
> Best
> Lorenz

		Amit

  reply	other threads:[~2025-12-10 15:58 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-12-05 18:06 Lorenz Bauer
2025-12-10 15:58 ` Amit Shah [this message]
2025-12-15  9:51   ` Lorenz Bauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=564165392dfefdbb6e1739102ed80c3aee92881b.camel@kernel.org \
    --to=amit@kernel.org \
    --cc=arnd@arndb.de \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lmb@isovalent.com \
    --cc=virtualization@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®