Skip to content

Commit

Permalink
vfio: fix group descriptor check
Browse files Browse the repository at this point in the history
[ upstream commit 1f16fa9 ]

The issue is that a file descriptor at 0 is a valid one. Currently
the file not found, the return value will be set to 0. As a result,
it is impossible to distinguish between a correct descriptor and a
failed return value. Fix it to return -ENOENT instead of 0.

Fixes: b758423 ("vfio: fix race condition with sysfs")
Fixes: ff0b67d ("vfio: DMA mapping")

Signed-off-by: Yunjian Wang <wangyunjian@huawei.com>
Reviewed-by: Anatoly Burakov <anatoly.burakov@intel.com>
  • Loading branch information
wyjwang authored and kevintraynor committed Nov 5, 2020
1 parent 88cf9e5 commit 8cba350
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
23 changes: 13 additions & 10 deletions lib/librte_eal/linuxapp/eal/eal_vfio.c
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ vfio_open_group_fd(int iommu_group_num)
strerror(errno));
return -1;
}
return 0;
return -ENOENT;
}
/* noiommu group found */
}
Expand All @@ -317,12 +317,12 @@ vfio_open_group_fd(int iommu_group_num)
vfio_group_fd = mp_rep->fds[0];
} else if (p->result == SOCKET_NO_FD) {
RTE_LOG(ERR, EAL, " bad VFIO group fd\n");
vfio_group_fd = 0;
vfio_group_fd = -ENOENT;
}
}

free(mp_reply.msgs);
if (vfio_group_fd < 0)
if (vfio_group_fd < 0 && vfio_group_fd != -ENOENT)
RTE_LOG(ERR, EAL, " cannot request group fd\n");
return vfio_group_fd;
}
Expand Down Expand Up @@ -378,9 +378,9 @@ vfio_get_group_fd(struct vfio_config *vfio_cfg,
}

vfio_group_fd = vfio_open_group_fd(iommu_group_num);
if (vfio_group_fd <= 0) {
if (vfio_group_fd < 0) {
RTE_LOG(ERR, EAL, "Failed to open group %d\n", iommu_group_num);
return -1;
return vfio_group_fd;
}

cur_grp->group_num = iommu_group_num;
Expand Down Expand Up @@ -704,11 +704,14 @@ rte_vfio_setup_device(const char *sysfs_base, const char *dev_addr,

/* get the actual group fd */
vfio_group_fd = rte_vfio_get_group_fd(iommu_group_num);
if (vfio_group_fd < 0)
if (vfio_group_fd < 0 && vfio_group_fd != -ENOENT)
return -1;

/* if group_fd == 0, that means the device isn't managed by VFIO */
if (vfio_group_fd == 0) {
/*
* if vfio_group_fd == -ENOENT, that means the device
* isn't managed by VFIO
*/
if (vfio_group_fd == -ENOENT) {
RTE_LOG(WARNING, EAL, " %s not managed by VFIO driver, skipping\n",
dev_addr);
return 1;
Expand Down Expand Up @@ -928,10 +931,10 @@ rte_vfio_release_device(const char *sysfs_base, const char *dev_addr,

/* get the actual group fd */
vfio_group_fd = rte_vfio_get_group_fd(iommu_group_num);
if (vfio_group_fd <= 0) {
if (vfio_group_fd < 0) {
RTE_LOG(INFO, EAL, "rte_vfio_get_group_fd failed for %s\n",
dev_addr);
ret = -1;
ret = vfio_group_fd;
goto out;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/librte_eal/linuxapp/eal/eal_vfio_mp_sync.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ vfio_mp_primary(const struct rte_mp_msg *msg, const void *peer)
r->req = SOCKET_REQ_GROUP;
r->group_num = m->group_num;
fd = rte_vfio_get_group_fd(m->group_num);
if (fd < 0)
if (fd < 0 && fd != -ENOENT)
r->result = SOCKET_ERR;
else if (fd == 0)
else if (fd == -ENOENT)
/* if VFIO group exists but isn't bound to VFIO driver */
r->result = SOCKET_NO_FD;
else {
Expand Down

0 comments on commit 8cba350

Please sign in to comment.