[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 13/40] vdpa: ref counting VhostVDPAShared
From: |
Si-Wei Liu |
Subject: |
[PATCH 13/40] vdpa: ref counting VhostVDPAShared |
Date: |
Thu, 7 Dec 2023 09:39:26 -0800 |
Subsequent patches attempt to release VhostVDPAShared resources,
for example iova tree to free and memory listener to unregister,
in vdpa_dev_cleanup(). Instead of checking against the vq index,
which is not always available in all of the callers, counting
the usage by reference. Then it'll be easy to free resource
upon the last deref.
Signed-off-by: Si-Wei Liu <si-wei.liu@oracle.com>
---
include/hw/virtio/vhost-vdpa.h | 2 ++
net/vhost-vdpa.c | 14 ++++++++++----
2 files changed, 12 insertions(+), 4 deletions(-)
diff --git a/include/hw/virtio/vhost-vdpa.h b/include/hw/virtio/vhost-vdpa.h
index 63493ff..7b8d3bf 100644
--- a/include/hw/virtio/vhost-vdpa.h
+++ b/include/hw/virtio/vhost-vdpa.h
@@ -70,6 +70,8 @@ typedef struct vhost_vdpa_shared {
/* Vdpa must send shadow addresses as IOTLB key for data queues, not GPA */
bool shadow_data;
+
+ unsigned refcnt;
} VhostVDPAShared;
typedef struct vhost_vdpa {
diff --git a/net/vhost-vdpa.c b/net/vhost-vdpa.c
index aebaa53..a126e5c 100644
--- a/net/vhost-vdpa.c
+++ b/net/vhost-vdpa.c
@@ -236,11 +236,11 @@ static void vhost_vdpa_cleanup(NetClientState *nc)
g_free(s->vhost_net);
s->vhost_net = NULL;
}
- if (s->vhost_vdpa.index != 0) {
- return;
+ if (--s->vhost_vdpa.shared->refcnt == 0) {
+ qemu_close(s->vhost_vdpa.shared->device_fd);
+ g_free(s->vhost_vdpa.shared);
}
- qemu_close(s->vhost_vdpa.shared->device_fd);
- g_free(s->vhost_vdpa.shared);
+ s->vhost_vdpa.shared = NULL;
}
/** Dummy SetSteeringEBPF to support RSS for vhost-vdpa backend */
@@ -1896,6 +1896,7 @@ static NetClientState *net_vhost_vdpa_init(NetClientState
*peer,
s->vhost_vdpa.shared->device_fd = vdpa_device_fd;
s->vhost_vdpa.shared->iova_range = iova_range;
s->vhost_vdpa.shared->shadow_data = svq;
+ s->vhost_vdpa.shared->refcnt++;
} else if (!is_datapath) {
s->cvq_cmd_out_buffer = mmap(NULL, vhost_vdpa_net_cvq_cmd_page_len(),
PROT_READ | PROT_WRITE,
@@ -1910,6 +1911,7 @@ static NetClientState *net_vhost_vdpa_init(NetClientState
*peer,
}
if (queue_pair_index != 0) {
s->vhost_vdpa.shared = shared;
+ s->vhost_vdpa.shared->refcnt++;
}
ret = vhost_vdpa_add(nc, (void *)&s->vhost_vdpa, queue_pair_index, nvqs);
@@ -1928,6 +1930,10 @@ static NetClientState
*net_vhost_vdpa_init(NetClientState *peer,
return nc;
err:
+ if (--s->vhost_vdpa.shared->refcnt == 0) {
+ g_free(s->vhost_vdpa.shared);
+ }
+ s->vhost_vdpa.shared = NULL;
qemu_del_net_client(nc);
return NULL;
}
--
1.8.3.1
- [PATCH 02/40] vdpa: add vhost_vdpa_get_vring_desc_group, (continued)
- [PATCH 02/40] vdpa: add vhost_vdpa_get_vring_desc_group, Si-Wei Liu, 2023/12/07
- [PATCH 06/40] vhost: make svq work with gpa without iova translation, Si-Wei Liu, 2023/12/07
- [PATCH 07/40] vdpa: move around vhost_vdpa_set_address_space_id, Si-Wei Liu, 2023/12/07
- [PATCH 04/40] vdpa: piggyback desc_group index when probing isolated cvq, Si-Wei Liu, 2023/12/07
- [PATCH 09/40] vdpa: no repeat setting shadow_data, Si-Wei Liu, 2023/12/07
- [PATCH 10/40] vdpa: assign svq descriptors a separate ASID when possible, Si-Wei Liu, 2023/12/07
- [PATCH 13/40] vdpa: ref counting VhostVDPAShared,
Si-Wei Liu <=
- [PATCH 14/40] vdpa: convert iova_tree to ref count based, Si-Wei Liu, 2023/12/07
- [PATCH 15/40] vdpa: add svq_switching and flush_map to header, Si-Wei Liu, 2023/12/07
- [PATCH 12/40] vdpa: check map_thread_enabled before join maps thread, Si-Wei Liu, 2023/12/07
- [PATCH 11/40] vdpa: factor out vhost_vdpa_last_dev, Si-Wei Liu, 2023/12/07
- [PATCH 17/40] vdpa: judge if map can be kept across reset, Si-Wei Liu, 2023/12/07
- [PATCH 18/40] vdpa: unregister listener on last dev cleanup, Si-Wei Liu, 2023/12/07