Signed-off-by: Chen Fan <chen.fan.f...@cn.fujitsu.com> --- hw/vfio/common.c | 14 +++++++++++++- include/hw/vfio/vfio-common.h | 1 + 2 files changed, 14 insertions(+), 1 deletion(-)
diff --git a/hw/vfio/common.c b/hw/vfio/common.c index b1045da..67881f7 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -795,6 +795,7 @@ VFIOGroup *vfio_get_group(int groupid, AddressSpace *as) if (group->groupid == groupid) { /* Found it. Now is it already in the right context? */ if (group->container->space->as == as) { + group->ref++; return group; } else { error_report("vfio: group %d used in multiple address spaces", @@ -825,6 +826,7 @@ VFIOGroup *vfio_get_group(int groupid, AddressSpace *as) goto close_fd_exit; } + group->ref = 1; group->groupid = groupid; QLIST_INIT(&group->device_list); @@ -854,7 +856,17 @@ free_group_exit: void vfio_put_group(VFIOGroup *group) { - if (!group || !QLIST_EMPTY(&group->device_list)) { + if (!group) { + return; + } + + group->ref--; + assert(group->ref >= 0); + if (!QLIST_EMPTY(&group->device_list)) { + return; + } + + if (group->ref) { return; } diff --git a/include/hw/vfio/vfio-common.h b/include/hw/vfio/vfio-common.h index 0d1fb80..3eb042e 100644 --- a/include/hw/vfio/vfio-common.h +++ b/include/hw/vfio/vfio-common.h @@ -117,6 +117,7 @@ struct VFIODeviceOps { typedef struct VFIOGroup { int fd; int groupid; + int ref; VFIOContainer *container; QLIST_HEAD(, VFIODevice) device_list; QLIST_ENTRY(VFIOGroup) next; -- 1.9.3