vhost: fix race condition when adding fd in the fdset
authorMatthias Gatto <matthias.gatto@outscale.com>
Thu, 6 Dec 2018 16:00:07 +0000 (16:00 +0000)
committerFerruh Yigit <ferruh.yigit@intel.com>
Fri, 21 Dec 2018 15:22:40 +0000 (16:22 +0100)
fdset_add can call fdset_shrink_nolock which call fdset_move
concurrently to poll that is call in fdset_event_dispatch.

This patch add a mutex to protect poll from been call at the same time
fdset_add call fdset_shrink_nolock.

Fixes: 1b815b89599c ("vhost: try to shrink pfdset when fdset_add fails")
Cc: stable@dpdk.org
Signed-off-by: Matthias Gatto <matthias.gatto@outscale.com>
Reviewed-by: Maxime Coquelin <maxime.coquelin@redhat.com>
lib/librte_vhost/fd_man.c
lib/librte_vhost/fd_man.h
lib/librte_vhost/socket.c

index 38347ab..55d4856 100644 (file)
@@ -129,7 +129,9 @@ fdset_add(struct fdset *pfdset, int fd, fd_cb rcb, fd_cb wcb, void *dat)
        pthread_mutex_lock(&pfdset->fd_mutex);
        i = pfdset->num < MAX_FDS ? pfdset->num++ : -1;
        if (i == -1) {
+               pthread_mutex_lock(&pfdset->fd_pooling_mutex);
                fdset_shrink_nolock(pfdset);
+               pthread_mutex_unlock(&pfdset->fd_pooling_mutex);
                i = pfdset->num < MAX_FDS ? pfdset->num++ : -1;
                if (i == -1) {
                        pthread_mutex_unlock(&pfdset->fd_mutex);
@@ -246,7 +248,9 @@ fdset_event_dispatch(void *arg)
                numfds = pfdset->num;
                pthread_mutex_unlock(&pfdset->fd_mutex);
 
+               pthread_mutex_lock(&pfdset->fd_pooling_mutex);
                val = poll(pfdset->rwfds, numfds, 1000 /* millisecs */);
+               pthread_mutex_unlock(&pfdset->fd_pooling_mutex);
                if (val < 0)
                        continue;
 
index 3331bcd..3ab5cfd 100644 (file)
@@ -24,6 +24,7 @@ struct fdset {
        struct pollfd rwfds[MAX_FDS];
        struct fdentry fd[MAX_FDS];
        pthread_mutex_t fd_mutex;
+       pthread_mutex_t fd_pooling_mutex;
        int num;        /* current fd number of this fdset */
 
        union pipefds {
index 01b60ff..9cf34ad 100644 (file)
@@ -90,6 +90,7 @@ static struct vhost_user vhost_user = {
        .fdset = {
                .fd = { [0 ... MAX_FDS - 1] = {-1, NULL, NULL, NULL, 0} },
                .fd_mutex = PTHREAD_MUTEX_INITIALIZER,
+               .fd_pooling_mutex = PTHREAD_MUTEX_INITIALIZER,
                .num = 0
        },
        .vsocket_cnt = 0,