Skip to contentSkip to conversation

eal/vfio: share the default container in multi process

PATCH Applied Darek Stojaczyk 4 participants 7 messages Oct 3, 2018 – Oct 29, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 eal/vfio: share the default container in multi process

So far each process in MP used to have a separate container and relied on the primary process to register all memsegs.

Mapping external memory via rte_vfio_container_dma_map() in secondary processes was broken, because the default (process-local) container had no groups bound. There was even no way to bind any groups to it, because the container fd was deeply encapsulated within EAL.

This patch introduces a new SOCKET_REQ_DEFAULT_CONTAINER message type for MP synchronization, makes all processes within a MP party use a single default container, and hence fixes rte_vfio_container_dma_map() for secondary processes.

Quoted 1 line

From what I checked this behavior was always the same, but

started to be invalid/insufficient once mapping external memory was allowed.

Fixes 73a639085938 ("vfio: allow to map other memory regions")

While here, fix up the comment on rte_vfio_get_container_fd(). This function always opens a new container, never reuses an old one.

---
 lib/librte_eal/common/include/rte_vfio.h      |  2 +-
 lib/librte_eal/linuxapp/eal/eal_vfio.c        | 49 ++++++++++++++++++-
 lib/librte_eal/linuxapp/eal/eal_vfio.h        |  4 ++
 .../linuxapp/eal/eal_vfio_mp_sync.c           | 11 +++++
 4 files changed, 64 insertions(+), 2 deletions(-)
lib/librte_eal/common/include/rte_vfio.h +1 -1
index 5ca13fcce..1064426eb 100644
@@ -227,7 +227,7 @@ rte_vfio_get_group_num(const char *sysfs_base,
 		      const char *dev_addr, int *iommu_group_num);
 
 /**
- * Open VFIO container fd or get an existing one
+ * Open a new VFIO container fd
  *
  * This function is only relevant to linux and will return
  * an error on BSD.
lib/librte_eal/linuxapp/eal/eal_vfio.c +48 -1

61 lines not on this page · raw message

lib/librte_eal/linuxapp/eal/eal_vfio.h +4 -0
index 68d4750a5..63ae115c3 100644
@@ -115,6 +115,9 @@ struct vfio_iommu_type {
 	vfio_dma_func_t dma_map_func;
 };
 
+/* get the vfio container that devices are bound to by default */
+int vfio_get_default_container_fd(void);
+
 /* pick IOMMU type. returns a pointer to vfio_iommu_type or NULL for error */
 const struct vfio_iommu_type *
 vfio_set_iommu_type(int vfio_container_fd);
@@ -129,6 +132,7 @@ int vfio_mp_sync_setup(void);
 
 #define SOCKET_REQ_CONTAINER 0x100
 #define SOCKET_REQ_GROUP 0x200
+#define SOCKET_REQ_DEFAULT_CONTAINER 0x400
 #define SOCKET_OK 0x0
 #define SOCKET_NO_FD 0x1
 #define SOCKET_ERR 0xFF
lib/librte_eal/linuxapp/eal/eal_vfio_mp_sync.c +11 -0
index 680a24aae..a1e8c834f 100644
@@ -66,6 +66,17 @@ vfio_mp_primary(const struct rte_mp_msg *msg, const void *peer)
 			reply.fds[0] = fd;
 		}
 		break;
+	case SOCKET_REQ_DEFAULT_CONTAINER:
+		r->req = SOCKET_REQ_DEFAULT_CONTAINER;
+		fd = vfio_get_default_container_fd();
+		if (fd < 0)
+			r->result = SOCKET_ERR;
+		else {
+			r->result = SOCKET_OK;
+			reply.num_fds = 1;
+			reply.fds[0] = fd;
+		}
+		break;
 	default:
 		RTE_LOG(ERR, EAL, "vfio received invalid message!\n");
 		return -1;
Signature · 1 line
2.17.1
#1 lore raw
Quoted 24 lines · Darek Stojaczyk

So far each process in MP used to have a separate container and relied on the primary process to register all memsegs.

Mapping external memory via rte_vfio_container_dma_map() in secondary processes was broken, because the default (process-local) container had no groups bound. There was even no way to bind any groups to it, because the container fd was deeply encapsulated within EAL.

This patch introduces a new SOCKET_REQ_DEFAULT_CONTAINER message type for MP synchronization, makes all processes within a MP party use a single default container, and hence fixes rte_vfio_container_dma_map() for secondary processes.

Quoted 1 line

From what I checked this behavior was always the same, but

started to be invalid/insufficient once mapping external memory was allowed.

Fixes 73a639085938 ("vfio: allow to map other memory regions")
Quoted 6 lines

While here, fix up the comment on rte_vfio_get_container_fd(). This function always opens a new container, never reuses an old one.

---

This makes things a bit funky when we consider external memory support. The same address space may or may not be mapped into the process, so we may or may not need to map things for DMA depending on whether we're mapping shared memory for DMA, or not.

The previous behavior may be counter-intuitive, but it's IMO the correct one - each process has its own container and performs its own DMA mappings.

Signature · 2 lines
Thanks,
Anatoly
#2 lore raw
Quoted 29 lines · Anatoly
Quoted 28 lines · Darek Stojaczyk

So far each process in MP used to have a separate container and relied on the primary process to register all memsegs.

Mapping external memory via rte_vfio_container_dma_map() in secondary processes was broken, because the default (process-local) container had no groups bound. There was even no way to bind any groups to it, because the container fd was deeply encapsulated within EAL.

This patch introduces a new SOCKET_REQ_DEFAULT_CONTAINER message type for MP synchronization, makes all processes within a MP party use a single default container, and hence fixes rte_vfio_container_dma_map() for secondary processes.

Quoted 1 line

From what I checked this behavior was always the same, but

started to be invalid/insufficient once mapping external memory was allowed.

Fixes 73a639085938 ("vfio: allow to map other memory regions")

While here, fix up the comment on rte_vfio_get_container_fd(). This function always opens a new container, never reuses an old one.

Quoted 7 lines
Quoted 2 lines

---

This makes things a bit funky when we consider external memory support. The same address space may or may not be mapped into the process, so we may or may not need to map things for DMA depending on whether we're mapping shared memory for DMA, or not.

The previous behavior may be counter-intuitive, but it's IMO the correct one - each process has its own container and performs its own DMA mappings.

Re-reading the commit message, i can see that i've misinterpreted the commit a bit (or rather i missed the fact that we can't even map anything for DMA in the secondaries due to container being not the same container we use devices for).

However, my above comment regarding external memory still applies - if we share DMA mappings between processes, it will be possible for secondary to map something else into space occupied by external memory, and potentially overwrite the DMA mappings.

Unfortunately, we cannot do anything about it, as that memory isn't under our control. I will have to update my external memory patchset to account for this and map memory for DMA only once, in whatever process that creates the memory in the first place.

So, on the concept of this i have no objections here, as i think this fix is necessary. I'll have to review the code some more to make sure i'm not missing anything.

Signature · 2 lines
Thanks,
Anatoly
Reviewed-by on v1 1/1
#3 lore raw
In reply to Darek Stojaczyk
Quoted 30 lines · Darek Stojaczyk

So far each process in MP used to have a separate container and relied on the primary process to register all memsegs.

Mapping external memory via rte_vfio_container_dma_map() in secondary processes was broken, because the default (process-local) container had no groups bound. There was even no way to bind any groups to it, because the container fd was deeply encapsulated within EAL.

This patch introduces a new SOCKET_REQ_DEFAULT_CONTAINER message type for MP synchronization, makes all processes within a MP party use a single default container, and hence fixes rte_vfio_container_dma_map() for secondary processes.

Quoted 1 line

From what I checked this behavior was always the same, but

started to be invalid/insufficient once mapping external memory was allowed.

Fixes 73a639085938 ("vfio: allow to map other memory regions")

While here, fix up the comment on rte_vfio_get_container_fd(). This function always opens a new container, never reuses an old one.

---

<snip>

lib/librte_eal/linuxapp/eal/eal_vfio.c rte_vfio_is_enabled(const char *modname) · line 947

Quoted 10 lines
lib/librte_eal/linuxapp/eal/eal_vfio.c +10 -0
+	if (internal_config.process_type == RTE_PROC_PRIMARY) {
+		/* if we were secondary process we would try requesting
+		 * container fd from the primary, but we're the primary
+		 * process so just exit here
+		 */
+		return -1;
+	}
+
+	p->req = SOCKET_REQ_DEFAULT_CONTAINER;
+	strcpy(mp_req.name, EAL_VFIO_MP);

strlcpy?

Otherwise,

Signature · 2 lines
Thanks,
Anatoly
#4 lore raw
Quoted 4 lines

-----Original Message-----
From: Burakov, Anatoly
Sent: Friday, October 5, 2018 10:58 AM

<snip>

Quoted 13 lines

lib/librte_eal/linuxapp/eal/eal_vfio.c rte_vfio_is_enabled(const char *modname) · line 947

Quoted 11 lines
lib/librte_eal/linuxapp/eal/eal_vfio.c +10 -0
+	if (internal_config.process_type == RTE_PROC_PRIMARY) {
+		/* if we were secondary process we would try requesting
+		 * container fd from the primary, but we're the primary
+		 * process so just exit here
+		 */
+		return -1;
+	}
+
+	p->req = SOCKET_REQ_DEFAULT_CONTAINER;
+	strcpy(mp_req.name, EAL_VFIO_MP);

strlcpy?

Probably, although I admit I only copied this code from SOCKET_REQ_CONTAINER handling in this file. Eventually it should be fixed in all places with a separate patch.

Thanks,
D.

Quoted 7 lines

Otherwise,

--
Thanks,
Anatoly

#5 lore raw
In reply to Burakov, Anatoly
Quoted 34 lines · Anatoly
Quoted 30 lines · Darek Stojaczyk

So far each process in MP used to have a separate container and relied on the primary process to register all memsegs.

Mapping external memory via rte_vfio_container_dma_map() in secondary processes was broken, because the default (process-local) container had no groups bound. There was even no way to bind any groups to it, because the container fd was deeply encapsulated within EAL.

This patch introduces a new SOCKET_REQ_DEFAULT_CONTAINER message type for MP synchronization, makes all processes within a MP party use a single default container, and hence fixes rte_vfio_container_dma_map() for secondary processes.

Quoted 1 line

From what I checked this behavior was always the same, but

started to be invalid/insufficient once mapping external memory was allowed.

Fixes 73a639085938 ("vfio: allow to map other memory regions")

While here, fix up the comment on rte_vfio_get_container_fd(). This function always opens a new container, never reuses an old one.

---

<snip>

Quoted 12 lines

lib/librte_eal/linuxapp/eal/eal_vfio.c rte_vfio_is_enabled(const char *modname) · line 947

Quoted 10 lines
lib/librte_eal/linuxapp/eal/eal_vfio.c +10 -0
+	if (internal_config.process_type == RTE_PROC_PRIMARY) {
+		/* if we were secondary process we would try requesting
+		 * container fd from the primary, but we're the primary
+		 * process so just exit here
+		 */
+		return -1;
+	}
+
+	p->req = SOCKET_REQ_DEFAULT_CONTAINER;
+	strcpy(mp_req.name, EAL_VFIO_MP);

strlcpy?

even better, rte_strscpy?

Quoted 4 lines

Otherwise,

#6 lore raw
In reply to Burakov, Anatoly

05/10/2018 10:57, Burakov, Anatoly:

Quoted 47 lines · Darek Stojaczyk
Quoted 30 lines

So far each process in MP used to have a separate container and relied on the primary process to register all memsegs.

Mapping external memory via rte_vfio_container_dma_map() in secondary processes was broken, because the default (process-local) container had no groups bound. There was even no way to bind any groups to it, because the container fd was deeply encapsulated within EAL.

This patch introduces a new SOCKET_REQ_DEFAULT_CONTAINER message type for MP synchronization, makes all processes within a MP party use a single default container, and hence fixes rte_vfio_container_dma_map() for secondary processes.

Quoted 1 line

From what I checked this behavior was always the same, but

started to be invalid/insufficient once mapping external memory was allowed.

Fixes 73a639085938 ("vfio: allow to map other memory regions")

While here, fix up the comment on rte_vfio_get_container_fd(). This function always opens a new container, never reuses an old one.

---

<snip>

lib/librte_eal/linuxapp/eal/eal_vfio.c rte_vfio_is_enabled(const char *modname) · line 947

Quoted 10 lines
lib/librte_eal/linuxapp/eal/eal_vfio.c +10 -0
+	if (internal_config.process_type == RTE_PROC_PRIMARY) {
+		/* if we were secondary process we would try requesting
+		 * container fd from the primary, but we're the primary
+		 * process so just exit here
+		 */
+		return -1;
+	}
+
+	p->req = SOCKET_REQ_DEFAULT_CONTAINER;
+	strcpy(mp_req.name, EAL_VFIO_MP);

strlcpy?

Quoted 2 lines

Otherwise,

Applied, thanks