Merge branch 'upstream-16.11-stable' into 16.11.x
[deb_dpdk.git] / lib / librte_vhost / socket.c
index 84e0595..805b2e5 100644 (file)
@@ -166,6 +166,11 @@ send_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
                msgh.msg_control = control;
                msgh.msg_controllen = sizeof(control);
                cmsg = CMSG_FIRSTHDR(&msgh);
+               if (cmsg == NULL) {
+                       RTE_LOG(ERR, VHOST_CONFIG, "cmsg == NULL\n");
+                       errno = EINVAL;
+                       return -1;
+               }
                cmsg->cmsg_len = CMSG_LEN(fdsize);
                cmsg->cmsg_level = SOL_SOCKET;
                cmsg->cmsg_type = SCM_RIGHTS;
@@ -438,7 +443,7 @@ vhost_user_reconnect_init(void)
 
        ret = pthread_create(&reconn_tid, NULL,
                             vhost_user_client_reconnect, NULL);
-       if (ret < 0)
+       if (ret != 0)
                RTE_LOG(ERR, VHOST_CONFIG, "failed to create reconnect thread");
 
        return ret;
@@ -525,7 +530,7 @@ rte_vhost_driver_register(const char *path, uint64_t flags)
        if ((flags & RTE_VHOST_USER_CLIENT) != 0) {
                vsocket->reconnect = !(flags & RTE_VHOST_USER_NO_RECONNECT);
                if (vsocket->reconnect && reconn_tid == 0) {
-                       if (vhost_user_reconnect_init() < 0) {
+                       if (vhost_user_reconnect_init() != 0) {
                                free(vsocket->path);
                                free(vsocket);
                                goto out;
@@ -598,13 +603,25 @@ rte_vhost_driver_unregister(const char *path)
                                vhost_user_remove_reconnect(vsocket);
                        }
 
+again:
                        pthread_mutex_lock(&vsocket->conn_mutex);
                        for (conn = TAILQ_FIRST(&vsocket->conn_list);
                             conn != NULL;
                             conn = next) {
                                next = TAILQ_NEXT(conn, next);
 
-                               fdset_del(&vhost_user.fdset, conn->connfd);
+                               /*
+                                * If r/wcb is executing, release the
+                                * conn_mutex lock, and try again since
+                                * the r/wcb may use the conn_mutex lock.
+                                */
+                               if (fdset_try_del(&vhost_user.fdset,
+                                                 conn->connfd) == -1) {
+                                       pthread_mutex_unlock(
+                                                       &vsocket->conn_mutex);
+                                       goto again;
+                               }
+
                                RTE_LOG(INFO, VHOST_CONFIG,
                                        "free connfd = %d for device '%s'\n",
                                        conn->connfd, path);