Skip to content

Commit

Permalink
vdpa/virtio: fix container recovery
Browse files Browse the repository at this point in the history
During recovery, devices could be removed. So we need to make sure
container will not be destroyed when there are VFs that are to be
recovered belonging to the same container.

Signed-off-by: Chenbo Xia <chenbox@nvidia.com>
  • Loading branch information
Ch3n60x committed Jun 24, 2024
1 parent 4a04d8e commit 2654e92
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 22 deletions.
18 changes: 11 additions & 7 deletions app/vfe-vdpa/vdpa_ha.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ virtio_ha_client_dev_restore_pf(int *total_vf)
goto err;
}

ret = virtio_ha_pf_ctx_set(pf_list + i, &pf_ctx, 0);
ret = virtio_ha_pf_ctx_set(pf_list + i, &pf_ctx, NULL);
if (ret < 0) {
RTE_LOG(ERR, HA, "Failed to set pf ctx in pf driver\n");
ret = -1;
Expand Down Expand Up @@ -177,7 +177,8 @@ virtio_ha_client_dev_restore_pf(int *total_vf)
memset(vf_dev, 0, sizeof(struct virtio_ha_vf_to_restore));
memcpy(&vf_dev->vf_devargs, vf_list + j, sizeof(struct vdpa_vf_with_devargs));
memcpy(&vf_dev->pf_name, pf_list + i, sizeof(struct virtio_dev_name));
vf_dev->vf_cnt_vm = 1;
vf_dev->vm_ctx.vm_vf = 1;
vf_dev->vm_ctx.vm_tbl_vf = 1;
TAILQ_INSERT_TAIL(&rq.non_prio_q, vf_dev, next);
}

Expand All @@ -189,10 +190,13 @@ virtio_ha_client_dev_restore_pf(int *total_vf)
tmp = TAILQ_NEXT(v1, next);
while (tmp != NULL) {
v2 = tmp;
if (strncmp(v1->vf_devargs.vm_uuid, v2->vf_devargs.vm_uuid, RTE_UUID_STRLEN) == 0 &&
v1->vf_devargs.mem_tbl_set && v2->vf_devargs.mem_tbl_set) {
v1->vf_cnt_vm++;
v2->vf_cnt_vm++;
if (strncmp(v1->vf_devargs.vm_uuid, v2->vf_devargs.vm_uuid, RTE_UUID_STRLEN) == 0) {
v1->vm_ctx.vm_vf++;
v2->vm_ctx.vm_vf++;
if (v1->vf_devargs.mem_tbl_set && v2->vf_devargs.mem_tbl_set) {
v1->vm_ctx.vm_tbl_vf++;
v2->vm_ctx.vm_tbl_vf++;
}
}
tmp = TAILQ_NEXT(tmp, next);
}
Expand Down Expand Up @@ -245,7 +249,7 @@ virtio_ha_client_dev_restore_vf(int total_vf)
goto err;
}

ret = virtio_ha_vf_ctx_set(&vf_dev->vf_devargs.vf_name, vf_ctx, vf_dev->vf_cnt_vm);
ret = virtio_ha_vf_ctx_set(&vf_dev->vf_devargs.vf_name, vf_ctx, &vf_dev->vm_ctx);
if (ret < 0) {
RTE_LOG(ERR, HA, "Failed to set vf ctx in vf driver\n");
pthread_mutex_unlock(&vf_restore_lock);
Expand Down
8 changes: 4 additions & 4 deletions drivers/common/virtio_ha/virtio_ha.c
Original file line number Diff line number Diff line change
Expand Up @@ -919,12 +919,12 @@ virtio_ha_vf_ctx_query(struct virtio_dev_name *vf,
}

int
virtio_ha_pf_ctx_set(const struct virtio_dev_name *pf, const struct virtio_pf_ctx *ctx, int num)
virtio_ha_pf_ctx_set(const struct virtio_dev_name *pf, const struct virtio_pf_ctx *ctx, struct virtio_ha_vm_dev_ctx *vm_ctx)
{
if (!pf_ctx_cb || !pf_ctx_cb->set)
return -1;

pf_ctx_cb->set(pf, ctx, num);
pf_ctx_cb->set(pf, ctx, vm_ctx);

return 0;
}
Expand All @@ -941,12 +941,12 @@ virtio_ha_pf_ctx_unset(const struct virtio_dev_name *pf)
}

int
virtio_ha_vf_ctx_set(const struct virtio_dev_name *vf, const struct vdpa_vf_ctx *ctx, int vf_cnt_vm)
virtio_ha_vf_ctx_set(const struct virtio_dev_name *vf, const struct vdpa_vf_ctx *ctx, struct virtio_ha_vm_dev_ctx *vm_ctx)
{
if (!vf_ctx_cb || !vf_ctx_cb->set)
return -1;

vf_ctx_cb->set(vf, ctx, vf_cnt_vm);
vf_ctx_cb->set(vf, ctx, vm_ctx);

return 0;
}
Expand Down
14 changes: 10 additions & 4 deletions drivers/common/virtio_ha/virtio_ha.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
#define VIRTIO_HA_TIME_SIZE 32

struct virtio_dev_name;
struct virtio_ha_vm_dev_ctx;

typedef void (*ctx_set_cb)(const struct virtio_dev_name *dev, const void *ctx, int vf_num_vm);
typedef void (*ctx_set_cb)(const struct virtio_dev_name *dev, const void *ctx, struct virtio_ha_vm_dev_ctx *vm_ctx);
typedef void (*ctx_unset_cb)(const struct virtio_dev_name *dev);
typedef void (*fd_cb)(int fd, void *data);
typedef void (*ver_time_set)(char *version, char *buildtime);
Expand Down Expand Up @@ -141,6 +142,11 @@ struct virtio_ha_device_list {
pthread_t prio_thread;
};

struct virtio_ha_vm_dev_ctx {
int vm_tbl_vf; /* Number of VFs belong to the same VM and use the DMA tbl */
int vm_vf; /* Number of VFs belong to the same VM and shares the same VFIO container */
};

struct virtio_ha_dev_ctx_cb {
ctx_set_cb set;
ctx_unset_cb unset;
Expand All @@ -150,7 +156,7 @@ struct virtio_ha_vf_to_restore {
TAILQ_ENTRY(virtio_ha_vf_to_restore) next;
struct vdpa_vf_with_devargs vf_devargs;
struct virtio_dev_name pf_name;
int vf_cnt_vm; /* VF number in the same VM */
struct virtio_ha_vm_dev_ctx vm_ctx;
};

TAILQ_HEAD(virtio_ha_vf_restore_list, virtio_ha_vf_to_restore);
Expand Down Expand Up @@ -211,13 +217,13 @@ int virtio_ha_vf_ctx_query(struct virtio_dev_name *vf,
const struct virtio_dev_name *pf, struct vdpa_vf_ctx **ctx);

/* App set PF context to PF driver */
int virtio_ha_pf_ctx_set(const struct virtio_dev_name *pf, const struct virtio_pf_ctx *ctx, int num);
int virtio_ha_pf_ctx_set(const struct virtio_dev_name *pf, const struct virtio_pf_ctx *ctx, struct virtio_ha_vm_dev_ctx *vm_ctx);

/* App unset PF context from PF driver */
int virtio_ha_pf_ctx_unset(const struct virtio_dev_name *pf);

/* App set VF context to VF driver */
int virtio_ha_vf_ctx_set(const struct virtio_dev_name *vf, const struct vdpa_vf_ctx *ctx, int vf_cnt_vm);
int virtio_ha_vf_ctx_set(const struct virtio_dev_name *vf, const struct vdpa_vf_ctx *ctx, struct virtio_ha_vm_dev_ctx *vm_ctx);

/* App unset VF context from VF driver */
int virtio_ha_vf_ctx_unset(const struct virtio_dev_name *vf);
Expand Down
2 changes: 1 addition & 1 deletion drivers/common/virtio_mi/lm.c
Original file line number Diff line number Diff line change
Expand Up @@ -1216,7 +1216,7 @@ virtio_vdpa_mi_dev_remove(struct rte_pci_device *pci_dev)
}

static void
virtio_ha_pf_drv_ctx_set(const struct virtio_dev_name *pf, const void *ctx, __rte_unused int num)
virtio_ha_pf_drv_ctx_set(const struct virtio_dev_name *pf, const void *ctx, __rte_unused struct virtio_ha_vm_dev_ctx *vm_ctx)
{
const struct virtio_pf_ctx *pf_ctx = (const struct virtio_pf_ctx *)ctx;

Expand Down
15 changes: 9 additions & 6 deletions drivers/vdpa/virtio/virtio_vdpa.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ static int stage1 = 0;
struct virtio_ha_vf_drv_ctx {
struct virtio_dev_name vf_name;
const struct vdpa_vf_ctx *ctx;
int vf_num_vm;
struct virtio_ha_vm_dev_ctx *vm_ctx;
};

extern struct virtio_vdpa_device_callback virtio_vdpa_blk_callback;
Expand All @@ -62,13 +62,13 @@ static pthread_mutex_t iommu_domain_locks[VIRTIO_VDPA_MAX_IOMMU_DOMAIN];
static struct virtio_ha_vf_drv_ctx cached_ctx;

static void
virtio_ha_vf_drv_ctx_set(const struct virtio_dev_name *vf, const void *ctx, int vf_num_vm)
virtio_ha_vf_drv_ctx_set(const struct virtio_dev_name *vf, const void *ctx, struct virtio_ha_vm_dev_ctx *vm_ctx)
{
const struct vdpa_vf_ctx *vf_ctx = (const struct vdpa_vf_ctx *)ctx;

memcpy(&cached_ctx.vf_name, vf, sizeof(struct virtio_dev_name));
cached_ctx.ctx = vf_ctx;
cached_ctx.vf_num_vm = vf_num_vm;
cached_ctx.vm_ctx = vm_ctx;
}

static void
Expand All @@ -78,7 +78,7 @@ virtio_ha_vf_drv_ctx_unset(const struct virtio_dev_name *vf)
return;
memset(&cached_ctx.vf_name, 0, sizeof(struct virtio_dev_name));
cached_ctx.ctx = NULL;
cached_ctx.vf_num_vm = 0;
cached_ctx.vm_ctx = NULL;
}

static struct virtio_vdpa_priv *
Expand Down Expand Up @@ -139,6 +139,7 @@ alloc_iommu_domain(void)
iommu_domain->container_ref_cnt = 0;
iommu_domain->mem_tbl_ref_cnt = 0;
iommu_domain->tbl_recover_cnt = 0;
iommu_domain->cont_recover_cnt = 0;
virtio_iommu_domains[i] = iommu_domain;

return i;
Expand Down Expand Up @@ -2176,7 +2177,7 @@ virtio_vdpa_dev_do_remove(struct rte_pci_device *pci_dev, struct virtio_vdpa_pri
iommu_domain = virtio_iommu_domains[priv->iommu_idx];
if (iommu_domain) {
iommu_domain->container_ref_cnt--;
if (iommu_domain->container_ref_cnt == 0) {
if (iommu_domain->container_ref_cnt == 0 && iommu_domain->cont_recover_cnt == 0) {
if (priv->vfio_container_fd >= 0) {
rte_vfio_container_destroy(priv->vfio_container_fd);
priv->vfio_container_fd = -1;
Expand Down Expand Up @@ -2361,8 +2362,10 @@ virtio_vdpa_dev_probe(struct rte_pci_driver *pci_drv __rte_unused,
iommu_domain->mem.regions[i].size = mem->regions[i].size;
}
iommu_domain->mem.nregions = mem->nregions;
iommu_domain->tbl_recover_cnt = cached_ctx.vf_num_vm;
iommu_domain->tbl_recover_cnt = cached_ctx.vm_ctx->vm_tbl_vf;
iommu_domain->cont_recover_cnt = cached_ctx.vm_ctx->vm_vf;
}
iommu_domain->cont_recover_cnt--;
pthread_mutex_unlock(&iommu_domain_locks[iommu_idx]);
if (cached_ctx.ctx->ctt.mem.nregions != 0)
priv->tbl_recovering = true;
Expand Down
1 change: 1 addition & 0 deletions drivers/vdpa/virtio/virtio_vdpa.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ struct virtio_vdpa_iommu_domain {
int container_ref_cnt;
int mem_tbl_ref_cnt;
int tbl_recover_cnt;
int cont_recover_cnt;
};

struct virtio_vdpa_vring_info {
Expand Down

0 comments on commit 2654e92

Please sign in to comment.