Re: [PATCH 1/1] vhost: Protect the virtqueue from being cleared whilst still in use

Stefano Garzarella
2022-03-02T17:11:20Z

On Wed, Mar 02, 2022 at 04:49:17PM +0000, Lee Jones wrote:
>On Wed, 02 Mar 2022, Michael S. Tsirkin wrote:
>
>> On Wed, Mar 02, 2022 at 05:28:31PM +0100, Stefano Garzarella wrote:
>> > On Wed, Mar 2, 2022 at 3:57 PM Lee Jones <lee.jones@linaro.org> wrote:
>> > >
>> > > On Wed, 02 Mar 2022, Michael S. Tsirkin wrote:
>> > >
>> > > > On Wed, Mar 02, 2022 at 01:56:35PM +0000, Lee Jones wrote:
>> > > > > On Wed, 02 Mar 2022, Michael S. Tsirkin wrote:
>> > > > >
>> > > > > > On Wed, Mar 02, 2022 at 07:54:21AM +0000, Lee Jones wrote:
>> > > > > > > vhost_vsock_handle_tx_kick() already holds the mutex during its call
>> > > > > > > to vhost_get_vq_desc(). All we have to do is take the same lock
>> > > > > > > during virtqueue clean-up and we mitigate the reported issues.
>> > > > > > >
>> > > > > > > Link: https://syzkaller.appspot.com/bug?extid=279432d30d825e63ba00
>> > > > > > >
>> > > > > > > Cc: <stable@vger.kernel.org>
>> > > > > > > Reported-by: syzbot+adc3cb32385586bec859@syzkaller.appspotmail.com
>> > > > > > > Signed-off-by: Lee Jones <lee.jones@linaro.org>
>> > > > > > > ---
>> > > > > > > drivers/vhost/vhost.c | 2 ++
>> > > > > > > 1 file changed, 2 insertions(+)
>> > > > > > >
>> > > > > > > diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c
>> > > > > > > index 59edb5a1ffe28..bbaff6a5e21b8 100644
>> > > > > > > --- a/drivers/vhost/vhost.c
>> > > > > > > +++ b/drivers/vhost/vhost.c
>> > > > > > > @@ -693,6 +693,7 @@ void vhost_dev_cleanup(struct vhost_dev *dev)
>> > > > > > > int i;
>> > > > > > >
>> > > > > > > for (i = 0; i < dev->nvqs; ++i) {
>> > > > > > > + mutex_lock(&dev->vqs[i]->mutex);
>> > > > > > > if (dev->vqs[i]->error_ctx)
>> > > > > > > eventfd_ctx_put(dev->vqs[i]->error_ctx);
>> > > > > > > if (dev->vqs[i]->kick)
>> > > > > > > @@ -700,6 +701,7 @@ void vhost_dev_cleanup(struct vhost_dev *dev)
>> > > > > > > if (dev->vqs[i]->call_ctx.ctx)
>> > > > > > > eventfd_ctx_put(dev->vqs[i]->call_ctx.ctx);
>> > > > > > > vhost_vq_reset(dev, dev->vqs[i]);
>> > > > > > > + mutex_unlock(&dev->vqs[i]->mutex);
>> > > > > > > }
>> > > > > >
>> > > > > > So this is a mitigation plan but the bug is still there though
>> > > > > > we don't know exactly what it is. I would prefer adding something like
>> > > > > > WARN_ON(mutex_is_locked(vqs[i]->mutex) here - does this make sense?
>> > > > >
>> > > > > As a rework to this, or as a subsequent patch?
>> > > >
>> > > > Can be a separate patch.
>> > > >
>> > > > > Just before the first lock I assume?
>> > > >
>> > > > I guess so, yes.
>> > >
>> > > No problem. Patch to follow.
>> > >
>> > > I'm also going to attempt to debug the root cause, but I'm new to this
>> > > subsystem to it might take a while for me to get my head around.
>> >
>> > IIUC the root cause should be the same as the one we solved here:
>> > https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=a58da53ffd70294ebea8ecd0eb45fd0d74add9f9
>> >
>> > The worker was not stopped before calling vhost_dev_cleanup(). So while
>> > the worker was still running we were going to free memory or initialize
>> > fields while it was still using virtqueue.
>>
>> Right, and I agree but it's not the root though, we do attempt to stop all workers.
>
>Exactly. This is what happens, but the question I'm going to attempt
>to answer is *why* does this happen.

IIUC the worker was still running because the /dev/vhost-vsock file was
not explicitly closed, so vhost_vsock_dev_release() was called in the
do_exit() of the process.

In that case there was the issue, because vhost_dev_check_owner()
returned false in vhost_vsock_stop() since current->mm was NULL.
So it returned earlier, without calling vhost_vq_set_backend(vq, NULL).

This did not stop the worker from continuing to run, causing the
multiple issues we are seeing.

current->mm was NULL, because in the do_exit() the address space is
cleaned in the exit_mm(), which is called before releasing the files
into the exit_task_work().

This can be seen from the logs, where we see first the warnings printed
by vhost_dev_cleanup() and then the panic in the worker (e.g. here
https://syzkaller.appspot.com/text?tag=CrashLog&x=16a61fce700000)

Mike also added a few more helpful details in this thread:
https://lore.kernel.org/virtualization/20220221100500.2x3s2sddqahgdfyt@sgarzare-redhat/T/#ree61316eac63245c9ba3050b44330e4034282cc2

Thanks,
Stefano