[dpdk-dev,v2] vfio: use right index when tracking devices in a vfio group

Message ID 1494406463-24517-1-git-send-email-alejandro.lucero@netronome.com (mailing list archive)
State Accepted, archived
Headers

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/Intel-compilation success Compilation OK

Commit Message

Alejandro Lucero May 10, 2017, 8:54 a.m. UTC
  Previous fix for properly handling devices from the same VFIO group
introduced another bug where the file descriptor of a kernel vfio
group is used as the index for tracking number of devices of a vfio
group struct handled by dpdk vfio code. Instead of the file
descriptor itself, the vfio group object that file descriptor is
registered with has to be used.

This patch introduces specific functions for incrementing or
decrementing the device counter for a specific vfio group using the
vfio file descriptor as a parameter. Note the code is not optimized
as the vfio group is found sequentially going through the vfio group
array but this should not be a problem as this is not related to
packet handling at all.

Fixes: a9c349e3a100 ("vfio: fix device unplug when several devices per group")

Signed-off-by: Alejandro Lucero <alejandro.lucero@netronome.com>
---
 lib/librte_eal/linuxapp/eal/eal_vfio.c | 71 ++++++++++++++++++++++++++++------
 1 file changed, 60 insertions(+), 11 deletions(-)
  

Comments

Anatoly Burakov May 10, 2017, 9:11 a.m. UTC | #1
> From: Alejandro Lucero [mailto:alejandro.lucero@netronome.com]
> Sent: Wednesday, May 10, 2017 9:54 AM
> To: dev@dpdk.org
> Cc: Burakov, Anatoly <anatoly.burakov@intel.com>;
> jerin.jacob@caviumnetworks.com; thomas@monjalon.net
> Subject: [PATCH v2] vfio: use right index when tracking devices in a vfio
> group
> 
> Previous fix for properly handling devices from the same VFIO group
> introduced another bug where the file descriptor of a kernel vfio group is
> used as the index for tracking number of devices of a vfio group struct
> handled by dpdk vfio code. Instead of the file descriptor itself, the vfio group
> object that file descriptor is registered with has to be used.
> 
> This patch introduces specific functions for incrementing or decrementing
> the device counter for a specific vfio group using the vfio file descriptor as a
> parameter. Note the code is not optimized as the vfio group is found
> sequentially going through the vfio group array but this should not be a
> problem as this is not related to packet handling at all.
> 
> Fixes: a9c349e3a100 ("vfio: fix device unplug when several devices per
> group")
> 
> Signed-off-by: Alejandro Lucero <alejandro.lucero@netronome.com>
> ---


Acked-by: Anatoly  Burakov <anatoly.burakov@intel.com>
  
Thomas Monjalon May 10, 2017, 1:31 p.m. UTC | #2
10/05/2017 11:11, Burakov, Anatoly:
> From: Alejandro Lucero [mailto:alejandro.lucero@netronome.com]
> > 
> > Previous fix for properly handling devices from the same VFIO group
> > introduced another bug where the file descriptor of a kernel vfio group is
> > used as the index for tracking number of devices of a vfio group struct
> > handled by dpdk vfio code. Instead of the file descriptor itself, the vfio group
> > object that file descriptor is registered with has to be used.
> > 
> > This patch introduces specific functions for incrementing or decrementing
> > the device counter for a specific vfio group using the vfio file descriptor as a
> > parameter. Note the code is not optimized as the vfio group is found
> > sequentially going through the vfio group array but this should not be a
> > problem as this is not related to packet handling at all.
> > 
> > Fixes: a9c349e3a100 ("vfio: fix device unplug when several devices per
> > group")
> > 
> > Signed-off-by: Alejandro Lucero <alejandro.lucero@netronome.com>
> 
> Acked-by: Anatoly  Burakov <anatoly.burakov@intel.com>

Applied, thanks
  

Patch

diff --git a/lib/librte_eal/linuxapp/eal/eal_vfio.c b/lib/librte_eal/linuxapp/eal/eal_vfio.c
index 6e24273..5486dca 100644
--- a/lib/librte_eal/linuxapp/eal/eal_vfio.c
+++ b/lib/librte_eal/linuxapp/eal/eal_vfio.c
@@ -172,6 +172,55 @@ 
 	return -1;
 }
 
+
+static int
+get_vfio_group_idx(int vfio_group_fd)
+{
+	int i;
+	for (i = 0; i < VFIO_MAX_GROUPS; i++)
+		if (vfio_cfg.vfio_groups[i].fd == vfio_group_fd)
+			return i;
+	return -1;
+}
+
+static void
+vfio_group_device_get(int vfio_group_fd)
+{
+	int i;
+
+	i = get_vfio_group_idx(vfio_group_fd);
+	if (i < 0 || i > VFIO_MAX_GROUPS)
+		RTE_LOG(ERR, EAL, "  wrong vfio_group index (%d)\n", i);
+	else
+		vfio_cfg.vfio_groups[i].devices++;
+}
+
+static void
+vfio_group_device_put(int vfio_group_fd)
+{
+	int i;
+
+	i = get_vfio_group_idx(vfio_group_fd);
+	if (i < 0 || i > VFIO_MAX_GROUPS)
+		RTE_LOG(ERR, EAL, "  wrong vfio_group index (%d)\n", i);
+	else
+		vfio_cfg.vfio_groups[i].devices--;
+}
+
+static int
+vfio_group_device_count(int vfio_group_fd)
+{
+	int i;
+
+	i = get_vfio_group_idx(vfio_group_fd);
+	if (i < 0 || i > VFIO_MAX_GROUPS) {
+		RTE_LOG(ERR, EAL, "  wrong vfio_group index (%d)\n", i);
+		return -1;
+	}
+
+	return vfio_cfg.vfio_groups[i].devices;
+}
+
 int
 clear_group(int vfio_group_fd)
 {
@@ -180,15 +229,14 @@ 
 
 	if (internal_config.process_type == RTE_PROC_PRIMARY) {
 
-		for (i = 0; i < VFIO_MAX_GROUPS; i++)
-			if (vfio_cfg.vfio_groups[i].fd == vfio_group_fd) {
-				vfio_cfg.vfio_groups[i].group_no = -1;
-				vfio_cfg.vfio_groups[i].fd = -1;
-				vfio_cfg.vfio_groups[i].devices = 0;
-				vfio_cfg.vfio_active_groups--;
-				return 0;
-			}
-		return -1;
+		i = get_vfio_group_idx(vfio_group_fd);
+		if (i < 0)
+			return -1;
+		vfio_cfg.vfio_groups[i].group_no = -1;
+		vfio_cfg.vfio_groups[i].fd = -1;
+		vfio_cfg.vfio_groups[i].devices = 0;
+		vfio_cfg.vfio_active_groups--;
+		return 0;
 	}
 
 	/* This is just for SECONDARY processes */
@@ -354,7 +402,7 @@ 
 		clear_group(vfio_group_fd);
 		return -1;
 	}
-	vfio_cfg.vfio_groups[vfio_group_fd].devices++;
+	vfio_group_device_get(vfio_group_fd);
 
 	return 0;
 }
@@ -402,7 +450,8 @@ 
 	/* An VFIO group can have several devices attached. Just when there is
 	 * no devices remaining should the group be closed.
 	 */
-	if (--vfio_cfg.vfio_groups[vfio_group_fd].devices == 0) {
+	vfio_group_device_put(vfio_group_fd);
+	if (!vfio_group_device_count(vfio_group_fd)) {
 
 		if (close(vfio_group_fd) < 0) {
 			RTE_LOG(INFO, EAL, "Error when closing vfio_group_fd for %s\n",