Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp5795377ybi; Tue, 28 May 2019 20:24:15 -0700 (PDT) X-Google-Smtp-Source: APXvYqyAOq4wwZckBeNNKxzhDXPZ2gnMqksRAJexTq/dDOA7fHwjg6mvzJ3BZi5PaVI69kOa2z4M X-Received: by 2002:a62:fb18:: with SMTP id x24mr81733837pfm.76.1559100255473; Tue, 28 May 2019 20:24:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1559100255; cv=none; d=google.com; s=arc-20160816; b=VroztozARCDgxEEdDsbe7sP0E8zlZXjPJkF/LEbnSaeaTgmQzob70xI6nBuUF6u9dG T8wlaK4xWqUHV9srvexlc/ufc0WH3M3zOjaU3wi9hobwi4QNGoBa9UdHMkW4uqrcrAkA wrQ0b4C9i7nrt6DY4FZ5Menksiof2ts27GrsSi5WHg4OWhkZneaE7ksBJywDclW4kbgF RDJwsC/FEy+gjHMJ7h9RuxXMpxi1FVMoAktAaDihYjHLn3+jZZt108DWdEEJCllcUVOD hyCk1l1PlAcBv/zRiQXDsI9mqCr2akOLtNzxwIHJOgmRrYrv3DniheIZkE6DCHK2ZGVB w+Vw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-language :content-transfer-encoding:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject; bh=Qbgs5GQyivhzijN5brxy2JitOFDJ7qepPtWy4eJ+WLo=; b=IgkqL5A5iGOltpET2u4ppS/BU8q06/rWOYDlsWKO0Svh4VZTBUsRSV9uR6WKRh6/B0 itoIXw+KBsaFGny/cVJXQcuI9Bl1dwV6Gy6zLnoJcSW00WdOOAjGxuKPZwOht17myqGt IAUURExnEOWYQliNtgm5inAFhYIl2CGUZ5wUdpem24CD/zSkFa1TqN90wYL8NMpX13Gk +wPn+G9r5gEtIVgzcsyCDBIbjcVHXSw7sK4+9VfLXd0YAZt309Cy/WLCPpaWCWa+J1QR MaU8vGKaITc14NOfXP/QMwwVfhgqOlftIf8KdFXr6hQWU87dojTxhyhqzBDsV5g50eiC c2Xg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id m21si12693368pgl.45.2019.05.28.20.23.58; Tue, 28 May 2019 20:24:15 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726522AbfE2DWw (ORCPT + 99 others); Tue, 28 May 2019 23:22:52 -0400 Received: from mx1.redhat.com ([209.132.183.28]:58200 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725856AbfE2DWw (ORCPT ); Tue, 28 May 2019 23:22:52 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id D2D51308425C; Wed, 29 May 2019 03:22:51 +0000 (UTC) Received: from [10.72.12.48] (ovpn-12-48.pek2.redhat.com [10.72.12.48]) by smtp.corp.redhat.com (Postfix) with ESMTP id D46731972B; Wed, 29 May 2019 03:22:42 +0000 (UTC) Subject: Re: [PATCH 3/4] vsock/virtio: fix flush of works during the .remove() To: Stefano Garzarella , netdev@vger.kernel.org Cc: linux-kernel@vger.kernel.org, virtualization@lists.linux-foundation.org, kvm@vger.kernel.org, Stefan Hajnoczi , "David S. Miller" , "Michael S . Tsirkin" References: <20190528105623.27983-1-sgarzare@redhat.com> <20190528105623.27983-4-sgarzare@redhat.com> From: Jason Wang Message-ID: <9ac9fc4b-5c39-2503-dfbb-660a7bdcfbfd@redhat.com> Date: Wed, 29 May 2019 11:22:40 +0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 MIME-Version: 1.0 In-Reply-To: <20190528105623.27983-4-sgarzare@redhat.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.40]); Wed, 29 May 2019 03:22:51 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2019/5/28 下午6:56, Stefano Garzarella wrote: > We flush all pending works before to call vdev->config->reset(vdev), > but other works can be queued before the vdev->config->del_vqs(vdev), > so we add another flush after it, to avoid use after free. > > Suggested-by: Michael S. Tsirkin > Signed-off-by: Stefano Garzarella > --- > net/vmw_vsock/virtio_transport.c | 23 +++++++++++++++++------ > 1 file changed, 17 insertions(+), 6 deletions(-) > > diff --git a/net/vmw_vsock/virtio_transport.c b/net/vmw_vsock/virtio_transport.c > index e694df10ab61..ad093ce96693 100644 > --- a/net/vmw_vsock/virtio_transport.c > +++ b/net/vmw_vsock/virtio_transport.c > @@ -660,6 +660,15 @@ static int virtio_vsock_probe(struct virtio_device *vdev) > return ret; > } > > +static void virtio_vsock_flush_works(struct virtio_vsock *vsock) > +{ > + flush_work(&vsock->loopback_work); > + flush_work(&vsock->rx_work); > + flush_work(&vsock->tx_work); > + flush_work(&vsock->event_work); > + flush_work(&vsock->send_pkt_work); > +} > + > static void virtio_vsock_remove(struct virtio_device *vdev) > { > struct virtio_vsock *vsock = vdev->priv; > @@ -668,12 +677,6 @@ static void virtio_vsock_remove(struct virtio_device *vdev) > mutex_lock(&the_virtio_vsock_mutex); > the_virtio_vsock = NULL; > > - flush_work(&vsock->loopback_work); > - flush_work(&vsock->rx_work); > - flush_work(&vsock->tx_work); > - flush_work(&vsock->event_work); > - flush_work(&vsock->send_pkt_work); > - > /* Reset all connected sockets when the device disappear */ > vsock_for_each_connected_socket(virtio_vsock_reset_sock); > > @@ -690,6 +693,9 @@ static void virtio_vsock_remove(struct virtio_device *vdev) > vsock->event_run = false; > mutex_unlock(&vsock->event_lock); > > + /* Flush all pending works */ > + virtio_vsock_flush_works(vsock); > + > /* Flush all device writes and interrupts, device will not use any > * more buffers. > */ > @@ -726,6 +732,11 @@ static void virtio_vsock_remove(struct virtio_device *vdev) > /* Delete virtqueues and flush outstanding callbacks if any */ > vdev->config->del_vqs(vdev); > > + /* Other works can be queued before 'config->del_vqs()', so we flush > + * all works before to free the vsock object to avoid use after free. > + */ > + virtio_vsock_flush_works(vsock); Some questions after a quick glance: 1) It looks to me that the work could be queued from the path of vsock_transport_cancel_pkt() . Is that synchronized here? 2) If we decide to flush after dev_vqs(), is tx_run/rx_run/event_run still needed? It looks to me we've already done except that we need flush rx_work in the end since send_pkt_work can requeue rx_work. Thanks > + > kfree(vsock); > mutex_unlock(&the_virtio_vsock_mutex); > }