Skip to contentSkip to conversation

eal/vfio: check if we already have the group fd open

PATCH In review Darek Stojaczyk 4 participants 6 messages Sep 17, 2018 – Oct 29, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 eal/vfio: check if we already have the group fd open

From: Dariusz Stojaczyk <[email protected]>

Always attempt to find already opened fd for an iommu group as subsequent attempts to open it will fail.

There's no public API to check if a group was already bound and has a container, so rte_vfio_container_group_bind() shouldn't fail in such case.

Fixes ea2dc1066870 ("vfio: add multi container support")
---
 lib/librte_eal/linuxapp/eal/eal_vfio.c | 5 +++++
 1 file changed, 5 insertions(+)
lib/librte_eal/linuxapp/eal/eal_vfio.c +5 -0
index c68dc38e0..bcb869be1 100644
@@ -1680,6 +1680,11 @@ rte_vfio_container_group_bind(int container_fd, int iommu_group_num)
 		return -1;
 	}
 
+	/* check if we already have the group descriptor open */
+	for (i = 0; i < VFIO_MAX_GROUPS; i++)
+		if (vfio_cfg->vfio_groups[i].group_num == iommu_group_num)
+			return vfio_cfg->vfio_groups[i].fd;
+
 	/* Check room for new group */
 	if (vfio_cfg->vfio_active_groups == VFIO_MAX_GROUPS) {
 		RTE_LOG(ERR, EAL, "Maximum number of VFIO groups reached!\n");
Signature · 1 line
2.17.1
#1 lore raw

PATCH 2/2 eal/vfio: cleanup getting group fd

From: Dariusz Stojaczyk <[email protected]>

Factor out duplicated code.

---
 lib/librte_eal/linuxapp/eal/eal_vfio.c | 119 ++++++++++---------------
 1 file changed, 45 insertions(+), 74 deletions(-)
lib/librte_eal/linuxapp/eal/eal_vfio.c +45 -74
index bcb869be1..3e93a7934 100644
@@ -345,46 +345,13 @@ get_vfio_cfg_by_group_num(int iommu_group_num)
 	return NULL;
 }
 
-static struct vfio_config *
-get_vfio_cfg_by_group_fd(int vfio_group_fd)
-{
-	struct vfio_config *vfio_cfg;
-	int i, j;
-
-	for (i = 0; i < VFIO_MAX_CONTAINERS; i++) {
-		vfio_cfg = &vfio_cfgs[i];
-		for (j = 0; j < VFIO_MAX_GROUPS; j++)
-			if (vfio_cfg->vfio_groups[j].fd == vfio_group_fd)
-				return vfio_cfg;
-	}
-
-	return NULL;
-}
-
-static struct vfio_config *
-get_vfio_cfg_by_container_fd(int container_fd)
-{
-	int i;
-
-	for (i = 0; i < VFIO_MAX_CONTAINERS; i++) {
-		if (vfio_cfgs[i].vfio_container_fd == container_fd)
-			return &vfio_cfgs[i];
-	}
-
-	return NULL;
-}
-
-int
-rte_vfio_get_group_fd(int iommu_group_num)
+static int
+vfio_get_group_fd(struct vfio_config *vfio_cfg,
+		int iommu_group_num)
 {
 	int i;
 	int vfio_group_fd;
 	struct vfio_group *cur_grp;
-	struct vfio_config *vfio_cfg;
-
-	/* get the vfio_config it belongs to */
-	vfio_cfg = get_vfio_cfg_by_group_num(iommu_group_num);
-	vfio_cfg = vfio_cfg ? vfio_cfg : default_vfio_cfg;
 
 	/* check if we already have the group descriptor open */
 	for (i = 0; i < VFIO_MAX_GROUPS; i++)
@@ -423,6 +390,47 @@ rte_vfio_get_group_fd(int iommu_group_num)
 	return vfio_group_fd;
 }
 
+static struct vfio_config *
+get_vfio_cfg_by_group_fd(int vfio_group_fd)
+{
+	struct vfio_config *vfio_cfg;
+	int i, j;
+
+	for (i = 0; i < VFIO_MAX_CONTAINERS; i++) {
+		vfio_cfg = &vfio_cfgs[i];
+		for (j = 0; j < VFIO_MAX_GROUPS; j++)
+			if (vfio_cfg->vfio_groups[j].fd == vfio_group_fd)
+				return vfio_cfg;
+	}
+
+	return NULL;
+}
+
+static struct vfio_config *
+get_vfio_cfg_by_container_fd(int container_fd)
+{
+	int i;
+
+	for (i = 0; i < VFIO_MAX_CONTAINERS; i++) {
+		if (vfio_cfgs[i].vfio_container_fd == container_fd)
+			return &vfio_cfgs[i];
+	}
+
+	return NULL;
+}
+
+int
+rte_vfio_get_group_fd(int iommu_group_num)
+{
+	struct vfio_config *vfio_cfg;
+
+	/* get the vfio_config it belongs to */
+	vfio_cfg = get_vfio_cfg_by_group_num(iommu_group_num);
+	vfio_cfg = vfio_cfg ? vfio_cfg : default_vfio_cfg;
+
+	return vfio_get_group_fd(vfio_cfg, iommu_group_num);
+}
+
 static int
 get_vfio_group_idx(int vfio_group_fd)
 {
@@ -1670,9 +1678,6 @@ int
 rte_vfio_container_group_bind(int container_fd, int iommu_group_num)
 {
 	struct vfio_config *vfio_cfg;
-	struct vfio_group *cur_grp;
-	int vfio_group_fd;
-	int i;
 
 	vfio_cfg = get_vfio_cfg_by_container_fd(container_fd);
 	if (vfio_cfg == NULL) {
@@ -1680,41 +1685,7 @@ rte_vfio_container_group_bind(int container_fd, int iommu_group_num)
 		return -1;
 	}
 
-	/* check if we already have the group descriptor open */
-	for (i = 0; i < VFIO_MAX_GROUPS; i++)
-		if (vfio_cfg->vfio_groups[i].group_num == iommu_group_num)
-			return vfio_cfg->vfio_groups[i].fd;
-
-	/* Check room for new group */
-	if (vfio_cfg->vfio_active_groups == VFIO_MAX_GROUPS) {
-		RTE_LOG(ERR, EAL, "Maximum number of VFIO groups reached!\n");
-		return -1;
-	}
-
-	/* Get an index for the new group */
-	for (i = 0; i < VFIO_MAX_GROUPS; i++)
-		if (vfio_cfg->vfio_groups[i].group_num == -1) {
-			cur_grp = &vfio_cfg->vfio_groups[i];
-			break;
-		}
-
-	/* This should not happen */
-	if (i == VFIO_MAX_GROUPS) {
-		RTE_LOG(ERR, EAL, "No VFIO group free slot found\n");
-		return -1;
-	}
-
-	vfio_group_fd = vfio_open_group_fd(iommu_group_num);
-	if (vfio_group_fd < 0) {
-		RTE_LOG(ERR, EAL, "Failed to open group %d\n", iommu_group_num);
-		return -1;
-	}
-	cur_grp->group_num = iommu_group_num;
-	cur_grp->fd = vfio_group_fd;
-	cur_grp->devices = 0;
-	vfio_cfg->vfio_active_groups++;
-
-	return vfio_group_fd;
+	return vfio_get_group_fd(vfio_cfg, iommu_group_num);
 }
 
 int
Signature · 1 line
2.17.1
Acked-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] eal/vfio: cleanup getting group fd

Quoted 6 lines · Darek Stojaczyk

From: Dariusz Stojaczyk <[email protected]>

Factor out duplicated code.

---

<snip>

lib/librte_eal/linuxapp/eal/eal_vfio.c rte_vfio_get_group_fd(int iommu_group_num) · line 429

Quoted 9 lines
lib/librte_eal/linuxapp/eal/eal_vfio.c +9 -0
+
+int
+rte_vfio_get_group_fd(int iommu_group_num)
+{
+	struct vfio_config *vfio_cfg;
+
+	/* get the vfio_config it belongs to */
+	vfio_cfg = get_vfio_cfg_by_group_num(iommu_group_num);
+	vfio_cfg = vfio_cfg ? vfio_cfg : default_vfio_cfg;

I tend to prefer being explicit rather than implicit (i.e. 'vfio_cfg == NULL' instead of 'vfio_cfg').

Otherwise,

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

Hi,

Quoted 43 lines

-----Original Message-----
From: Stojaczyk, Dariusz
Sent: Monday, September 17, 2018 9:47 PM
To: [email protected]
Cc: Alejandro Lucero <[email protected]>; Burakov, Anatoly <[email protected]>; [email protected]; Stojaczyk, Dariusz <[email protected]>; Wang, Xiao W <[email protected]>
Subject: [PATCH 1/2] eal/vfio: check if we already have the group fd open

From: Dariusz Stojaczyk <[email protected]>

Always attempt to find already opened fd for an iommu group as subsequent attempts to open it will fail.

There's no public API to check if a group was already bound and has a container, so rte_vfio_container_group_bind() shouldn't fail in such case.

Fixes ea2dc1066870 ("vfio: add multi container support")
---
 lib/librte_eal/linuxapp/eal/eal_vfio.c | 5 +++++
 1 file changed, 5 insertions(+)
lib/librte_eal/linuxapp/eal/eal_vfio.c +5 -0
index c68dc38e0..bcb869be1 100644
@@ -1680,6 +1680,11 @@ rte_vfio_container_group_bind(int container_fd,
 int iommu_group_num)
 		return -1;
 	}
 
+	/* check if we already have the group descriptor open */
+	for (i = 0; i < VFIO_MAX_GROUPS; i++)
+		if (vfio_cfg->vfio_groups[i].group_num == iommu_group_num)
+			return vfio_cfg->vfio_groups[i].fd;
+
 	/* Check room for new group */
 	if (vfio_cfg->vfio_active_groups == VFIO_MAX_GROUPS) {
Quoted 4 lines

RTE_LOG(ERR, EAL, "Maximum number of VFIO groups
reached!\n");
--
2.17.1

BRs,
Xiao

#4 lore raw

25/09/2018 09:56, Wang, Xiao W:

Quoted 11 lines

Hi,

Quoted 9 lines

From: Dariusz Stojaczyk <[email protected]>

Always attempt to find already opened fd for an iommu group as subsequent attempts to open it will fail.

There's no public API to check if a group was already bound and has a container, so rte_vfio_container_group_bind() shouldn't fail in such case.

Quoted 6 lines
Quoted 4 lines
Fixes ea2dc1066870 ("vfio: add multi container support")

+Cc: [email protected]

Series applied, thanks

Acked-by on v1 1/2
#5 lore raw
In reply to Darek Stojaczyk
Quoted 9 lines · Darek Stojaczyk

From: Dariusz Stojaczyk <[email protected]>

Always attempt to find already opened fd for an iommu group as subsequent attempts to open it will fail.

There's no public API to check if a group was already bound and has a container, so rte_vfio_container_group_bind() shouldn't fail in such case.

Quoted 5 lines
Fixes ea2dc1066870 ("vfio: add multi container support")

---

Signature · 2 lines
Thanks,
Anatoly