Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp4868618ybi; Tue, 28 May 2019 03:59:22 -0700 (PDT) X-Google-Smtp-Source: APXvYqwdeMUmJQQfs/RNrMolNV46meeH11UfAyOG5a4KMaoSEU3pQxEiE8TlMITePs0TaDoOWwQi X-Received: by 2002:a63:c750:: with SMTP id v16mr130713788pgg.409.1559041162526; Tue, 28 May 2019 03:59:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1559041162; cv=none; d=google.com; s=arc-20160816; b=x+XUJ2ENRHG679qnrHeAhY9j5IlIGvVlMBfE4xaAqleu3WbdNOMJV2BMpsVJgpmFz/ dZoiv49ZE4b1uovoPKwkU504OTuQW3jCPecUd6YWJvLNkLWjU4PMO4ypxe+wsVrfuD6C vbMXvGiZ6h4uhDPj8fnQV7athYt/OPKSDKvXWi/bSWHlErfB+O+yaot3CD1KVJXI/gkt bt7rgiHtMY+XfOiRHo+Es2AWD0+mR70X3uZZGYBMlkvw4NBWb2EpvsTe/kJwnEfeHL6I yp6hyEA0f8HowvOe1PjLYQe6OANe0EzHkkzplGyDjvzj4OZYH1pFGPDhj8a3wvYH848v 3vpA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=YinJ5w60OK7XtrNB/LWIY5V9lJrksG8I13BdkymZ+2M=; b=RoAAVGM2h7DaMY0EbDAAcibx/h56Zlmuu134Vcq9fQWQOUR3MGAYwm9vFWsUMmO+VU X0QYvJF7zx8+127u6qFLKeeRY7iZGJ5jz1lKQspbTzGgoLLUTNRfvxbIwor/cEfhHbOa 2zWKtNgctMM+kpslFHr3kuYiQsEUratS20Uk1+RmGdKuBFX3zoaL7iGERSToS46DQrNC dULTVULIbDdc1hWYNlPC+/288dS9yE2VmDuQRTJa2g/rtFPzkIK2/ZkMqcFK5tYl56Lj 1JkhKD7kK5R40B9Rt6ZgwaqPe18WEykq0xMLoxacbbvDsZmXtGbSeeSEO0QK4NddXOiY KTsw== 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 m3si14734600pld.40.2019.05.28.03.59.07; Tue, 28 May 2019 03:59:22 -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 S1726812AbfE1K4k (ORCPT + 99 others); Tue, 28 May 2019 06:56:40 -0400 Received: from mx1.redhat.com ([209.132.183.28]:60092 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726282AbfE1K4j (ORCPT ); Tue, 28 May 2019 06:56:39 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id E2B8B3079B8F; Tue, 28 May 2019 10:56:38 +0000 (UTC) Received: from steredhat.redhat.com (ovpn-117-13.ams2.redhat.com [10.36.117.13]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2E37560BE2; Tue, 28 May 2019 10:56:36 +0000 (UTC) From: Stefano Garzarella To: 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" Subject: [PATCH 1/4] vsock/virtio: fix locking around 'the_virtio_vsock' Date: Tue, 28 May 2019 12:56:20 +0200 Message-Id: <20190528105623.27983-2-sgarzare@redhat.com> In-Reply-To: <20190528105623.27983-1-sgarzare@redhat.com> References: <20190528105623.27983-1-sgarzare@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.41]); Tue, 28 May 2019 10:56:39 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch protects the reading of 'the_virtio_vsock' taking the mutex used when it is set. We also move the 'the_virtio_vsock' assignment at the end of the .probe(), when we finished all the initialization, and at the beginning of .remove(), before to release resources, taking the lock until the end of the function. Signed-off-by: Stefano Garzarella --- net/vmw_vsock/virtio_transport.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/net/vmw_vsock/virtio_transport.c b/net/vmw_vsock/virtio_transport.c index 96ab344f17bb..d3ba7747aa73 100644 --- a/net/vmw_vsock/virtio_transport.c +++ b/net/vmw_vsock/virtio_transport.c @@ -68,7 +68,13 @@ struct virtio_vsock { static struct virtio_vsock *virtio_vsock_get(void) { - return the_virtio_vsock; + struct virtio_vsock *vsock; + + mutex_lock(&the_virtio_vsock_mutex); + vsock = the_virtio_vsock; + mutex_unlock(&the_virtio_vsock_mutex); + + return vsock; } static u32 virtio_transport_get_local_cid(void) @@ -592,7 +598,6 @@ static int virtio_vsock_probe(struct virtio_device *vdev) atomic_set(&vsock->queued_replies, 0); vdev->priv = vsock; - the_virtio_vsock = vsock; mutex_init(&vsock->tx_lock); mutex_init(&vsock->rx_lock); mutex_init(&vsock->event_lock); @@ -614,6 +619,8 @@ static int virtio_vsock_probe(struct virtio_device *vdev) virtio_vsock_event_fill(vsock); mutex_unlock(&vsock->event_lock); + the_virtio_vsock = vsock; + mutex_unlock(&the_virtio_vsock_mutex); return 0; @@ -628,6 +635,9 @@ static void virtio_vsock_remove(struct virtio_device *vdev) struct virtio_vsock *vsock = vdev->priv; struct virtio_vsock_pkt *pkt; + 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); @@ -667,13 +677,10 @@ static void virtio_vsock_remove(struct virtio_device *vdev) } spin_unlock_bh(&vsock->loopback_list_lock); - mutex_lock(&the_virtio_vsock_mutex); - the_virtio_vsock = NULL; - mutex_unlock(&the_virtio_vsock_mutex); - vdev->config->del_vqs(vdev); kfree(vsock); + mutex_unlock(&the_virtio_vsock_mutex); } static struct virtio_device_id id_table[] = { -- 2.20.1