[27.03 RFC PATCH v1 2/8] vfio: use usage counters

Anatoly Burakov anatoly.burakov at intel.com
Fri Oct 16 16:02:09 CEST 2026


Currently, resources in VFIO (such as groups or devices) are tracked for
first/last usage due to things needing to happen on first/last reference.
Use EAL usage counters instead of manual arithmetic and checks.

Signed-off-by: Anatoly Burakov <anatoly.burakov at intel.com>
---
 lib/eal/linux/eal_vfio.c       |  7 ++-----
 lib/eal/linux/eal_vfio.h       |  5 +++--
 lib/eal/linux/eal_vfio_group.c | 11 ++++-------
 3 files changed, 9 insertions(+), 14 deletions(-)

diff --git a/lib/eal/linux/eal_vfio.c b/lib/eal/linux/eal_vfio.c
index fdf500cd5b..056795a6e4 100644
--- a/lib/eal/linux/eal_vfio.c
+++ b/lib/eal/linux/eal_vfio.c
@@ -1125,11 +1125,8 @@ dev_vfio_release_device(const char *sysfs_base __rte_unused, const char *dev_add
 		/* close device handle */
 		vfio_device_erase(cfg, dev);
 
-		/* remove device from group */
-		grp->n_devices--;
-
-		/* was this the last device? */
-		if (grp->n_devices == 0)
+		/* remove device from group, erase the group if it was the last device */
+		if (rte_usage_count_release(&grp->n_devices))
 			vfio_group_erase(cfg, grp);
 
 		/* if no more groups left, remove callback */
diff --git a/lib/eal/linux/eal_vfio.h b/lib/eal/linux/eal_vfio.h
index 980bdae0ba..8aaff4f335 100644
--- a/lib/eal/linux/eal_vfio.h
+++ b/lib/eal/linux/eal_vfio.h
@@ -7,6 +7,7 @@
 
 #include <rte_common.h>
 #include <rte_spinlock.h>
+#include <rte_usage_count.h>
 
 #include <stdint.h>
 
@@ -42,7 +43,7 @@ struct vfio_group {
 	bool active;
 	int group_num;
 	int fd;
-	int n_devices;
+	rte_usage_count_t n_devices;
 };
 
 /* device tracking (common for group and cdev modes) */
@@ -66,7 +67,7 @@ struct vfio_device {
 struct vfio_group_config {
 	bool iommu_type_set;
 	bool mem_event_clb_set;
-	size_t n_groups;
+	rte_usage_count_t n_groups;
 	struct vfio_group groups[RTE_MAX_VFIO_GROUPS];
 };
 
diff --git a/lib/eal/linux/eal_vfio_group.c b/lib/eal/linux/eal_vfio_group.c
index 7c879830ac..55ada5046b 100644
--- a/lib/eal/linux/eal_vfio_group.c
+++ b/lib/eal/linux/eal_vfio_group.c
@@ -508,7 +508,7 @@ vfio_group_create(struct vfio_container *cfg, int iommu_group)
 	VFIO_GROUP_FOREACH(cfg, grp) {
 		if (grp->active)
 			continue;
-		cfg->group_cfg.n_groups++;
+		rte_usage_count_claim(&cfg->group_cfg.n_groups);
 		grp->active = true;
 		grp->group_num = iommu_group;
 		grp->fd = -1;
@@ -527,10 +527,8 @@ vfio_group_erase(struct vfio_container *cfg, struct vfio_group *grp)
 		EAL_LOG(ERR, "Error when closing group fd %d", grp->fd);
 
 	*grp = (struct vfio_group){0};
-	group_cfg->n_groups--;
-
-	/* if this was the last group in config, erase IOMMU setup and unregister callback */
-	if (group_cfg->n_groups == 0) {
+	/* if this was the last group in config, erase IOMMU setup */
+	if (rte_usage_count_release(&group_cfg->n_groups)) {
 		cfg->dma_setup_done = false;
 		group_cfg->iommu_type_set = false;
 	}
@@ -880,8 +878,7 @@ vfio_group_setup_device_fd(const char *dev_addr, struct vfio_group *grp, struct
 	dev->fd = fd;
 	/* store backreference to group */
 	dev->group = grp->group_num;
-	/* increment number of devices in group */
-	grp->n_devices++;
+	rte_usage_count_claim(&grp->n_devices);
 	return 0;
 }
 
-- 
2.52.0



More information about the dev mailing list