aboutsummaryrefslogtreecommitdiff
path: root/lib/dma.c
diff options
context:
space:
mode:
authorThanos Makatos <thanos.makatos@nutanix.com>2020-11-26 05:37:02 -0500
committerThanos <tmakatos@gmail.com>2020-11-27 09:36:12 +0000
commit24cacccd92a0da049af83d4da35bd3bcebf236ae (patch)
tree9fd35a360f5ddcc08476fd8493f48d789590f43f /lib/dma.c
parent4235d834cfd090c7d33352adda8762679a132347 (diff)
downloadlibvfio-user-24cacccd92a0da049af83d4da35bd3bcebf236ae.zip
libvfio-user-24cacccd92a0da049af83d4da35bd3bcebf236ae.tar.gz
libvfio-user-24cacccd92a0da049af83d4da35bd3bcebf236ae.tar.bz2
allow DMA regions without file descriptor
Signed-off-by: Thanos Makatos <thanos.makatos@nutanix.com>
Diffstat (limited to 'lib/dma.c')
-rw-r--r--lib/dma.c46
1 files changed, 22 insertions, 24 deletions
diff --git a/lib/dma.c b/lib/dma.c
index 96b12c5..b47652d 100644
--- a/lib/dma.c
+++ b/lib/dma.c
@@ -61,9 +61,8 @@ fds_are_same_file(int fd1, int fd2)
{
struct stat st1, st2;
- if (fd1 == -1 || fd2 == -1) {
- errno = EINVAL;
- return -1;
+ if (fd1 == fd2) {
+ return true;
}
return (fstat(fd1, &st1) == 0 && fstat(fd2, &st2) == 0 &&
@@ -210,15 +209,10 @@ dma_controller_add_region(dma_controller_t *dma,
{
int idx;
dma_memory_region_t *region;
- int page_size;
+ int page_size = 0;
assert(dma != NULL);
- if (fd == -1) {
- errno = EINVAL;
- return -1;
- }
-
for (idx = 0; idx < dma->nregions; idx++) {
region = &dma->regions[idx];
@@ -267,10 +261,12 @@ dma_controller_add_region(dma_controller_t *dma,
idx = dma->nregions;
region = &dma->regions[idx];
- page_size = fd_get_blocksize(fd);
- if (page_size < 0) {
- lm_log(dma->lm_ctx, LM_ERR, "bad page size %d\n", page_size);
- goto err;
+ if (fd != -1) {
+ page_size = fd_get_blocksize(fd);
+ if (page_size < 0) {
+ lm_log(dma->lm_ctx, LM_ERR, "bad page size %d\n", page_size);
+ goto err;
+ }
}
page_size = MAX(page_size, getpagesize());
@@ -281,19 +277,21 @@ dma_controller_add_region(dma_controller_t *dma,
region->fd = fd;
region->refcnt = 0;
- region->virt_addr = dma_map_region(region, PROT_READ | PROT_WRITE,
- 0, region->size);
- if (region->virt_addr == MAP_FAILED) {
- lm_log(dma->lm_ctx, LM_ERR,
- "failed to memory map DMA region %#lx-%#lx: %s\n",
- dma_addr, dma_addr + size, strerror(errno));
- if (region->fd != -1) {
- if (close(region->fd) == -1) {
- lm_log(dma->lm_ctx, LM_DBG, "failed to close fd %d: %m\n",
- region->fd);
+ if (fd != -1) {
+ region->virt_addr = dma_map_region(region, PROT_READ | PROT_WRITE,
+ 0, region->size);
+ if (region->virt_addr == MAP_FAILED) {
+ lm_log(dma->lm_ctx, LM_ERR,
+ "failed to memory map DMA region %#lx-%#lx: %s\n",
+ dma_addr, dma_addr + size, strerror(errno));
+ if (region->fd != -1) {
+ if (close(region->fd) == -1) {
+ lm_log(dma->lm_ctx, LM_DBG, "failed to close fd %d: %m\n",
+ region->fd);
+ }
}
+ goto err;
}
- goto err;
}
dma->nregions++;