vsock: lookup and setup guest_cid inside vhost_vsock_lock
authorGao feng <omarapazanadi@gmail.com>
Wed, 14 Dec 2016 11:24:36 +0000 (19:24 +0800)
committerMichael S. Tsirkin <mst@redhat.com>
Thu, 15 Dec 2016 22:12:49 +0000 (00:12 +0200)
Multi vsocks may setup the same cid at the same time.

Signed-off-by: Gao feng <omarapazanadi@gmail.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
drivers/vhost/vsock.c

index e6b70966c19de60e2906c775d74eab8042c4fac0..bbbf588540ed71d82ed63deb355b77736a2a9628 100644 (file)
@@ -50,11 +50,10 @@ static u32 vhost_transport_get_local_cid(void)
        return VHOST_VSOCK_DEFAULT_HOST_CID;
 }
 
-static struct vhost_vsock *vhost_vsock_get(u32 guest_cid)
+static struct vhost_vsock *__vhost_vsock_get(u32 guest_cid)
 {
        struct vhost_vsock *vsock;
 
-       spin_lock_bh(&vhost_vsock_lock);
        list_for_each_entry(vsock, &vhost_vsock_list, list) {
                u32 other_cid = vsock->guest_cid;
 
@@ -63,15 +62,24 @@ static struct vhost_vsock *vhost_vsock_get(u32 guest_cid)
                        continue;
 
                if (other_cid == guest_cid) {
-                       spin_unlock_bh(&vhost_vsock_lock);
                        return vsock;
                }
        }
-       spin_unlock_bh(&vhost_vsock_lock);
 
        return NULL;
 }
 
+static struct vhost_vsock *vhost_vsock_get(u32 guest_cid)
+{
+       struct vhost_vsock *vsock;
+
+       spin_lock_bh(&vhost_vsock_lock);
+       vsock = __vhost_vsock_get(guest_cid);
+       spin_unlock_bh(&vhost_vsock_lock);
+
+       return vsock;
+}
+
 static void
 vhost_transport_do_send_pkt(struct vhost_vsock *vsock,
                            struct vhost_virtqueue *vq)
@@ -559,11 +567,12 @@ static int vhost_vsock_set_cid(struct vhost_vsock *vsock, u64 guest_cid)
                return -EINVAL;
 
        /* Refuse if CID is already in use */
-       other = vhost_vsock_get(guest_cid);
-       if (other && other != vsock)
-               return -EADDRINUSE;
-
        spin_lock_bh(&vhost_vsock_lock);
+       other = __vhost_vsock_get(guest_cid);
+       if (other && other != vsock) {
+               spin_unlock_bh(&vhost_vsock_lock);
+               return -EADDRINUSE;
+       }
        vsock->guest_cid = guest_cid;
        spin_unlock_bh(&vhost_vsock_lock);