From c2fa2d50e1446128abe0b3a70fa6c468872f070c Mon Sep 17 00:00:00 2001 From: Changpeng Liu Date: Thu, 9 Dec 2021 20:04:04 +0800 Subject: [PATCH] nvmf/vfio-user: unregister the memory region whether controller is active or not The controller may be freed eailer than endpoint, so we still need to unregister the memory region from SPDK. The case can happen when removing the listener while VM is connected. Change-Id: I95d49cefdbff3e0bdea316fac824ef8b218fcd2c Signed-off-by: Changpeng Liu Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/10378 Tested-by: SPDK CI Jenkins Reviewed-by: Jim Harris Reviewed-by: Ben Walker Community-CI: Broadcom CI Community-CI: Mellanox Build Bot --- lib/nvmf/vfio_user.c | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/lib/nvmf/vfio_user.c b/lib/nvmf/vfio_user.c index 649ae55230..7dec591054 100644 --- a/lib/nvmf/vfio_user.c +++ b/lib/nvmf/vfio_user.c @@ -1617,31 +1617,30 @@ memory_region_remove_cb(vfu_ctx_t *vfu_ctx, vfu_dma_info_t *info) } assert(endpoint != NULL); - if (endpoint->ctrlr == NULL) { - return 0; - } - ctrlr = endpoint->ctrlr; + if (endpoint->ctrlr != NULL) { + ctrlr = endpoint->ctrlr; - SPDK_DEBUGLOG(nvmf_vfio, "%s: unmap IOVA %#lx-%#lx\n", ctrlr_id(ctrlr), - (uintptr_t)info->mapping.iov_base, - (uintptr_t)info->mapping.iov_base + info->mapping.iov_len); + SPDK_DEBUGLOG(nvmf_vfio, "%s: unmap IOVA %#lx-%#lx\n", ctrlr_id(ctrlr), + (uintptr_t)info->mapping.iov_base, + (uintptr_t)info->mapping.iov_base + info->mapping.iov_len); - map_start = info->mapping.iov_base; - map_end = info->mapping.iov_base + info->mapping.iov_len; + map_start = info->mapping.iov_base; + map_end = info->mapping.iov_base + info->mapping.iov_len; - pthread_mutex_lock(&endpoint->lock); - TAILQ_FOREACH(qpair, &ctrlr->connected_qps, tailq) { - if (qpair->sq.addr >= map_start && qpair->sq.addr <= map_end) { - unmap_q(ctrlr, &qpair->sq); - qpair->sq_state = VFIO_USER_SQ_INACTIVE; - } - - cqpair = ctrlr->qp[qpair->sq.cqid]; - if (cqpair->cq.addr >= map_start && cqpair->cq.addr <= map_end) { - unmap_q(ctrlr, &cqpair->cq); + pthread_mutex_lock(&endpoint->lock); + TAILQ_FOREACH(qpair, &ctrlr->connected_qps, tailq) { + if (qpair->sq.addr >= map_start && qpair->sq.addr <= map_end) { + unmap_q(ctrlr, &qpair->sq); + qpair->sq_state = VFIO_USER_SQ_INACTIVE; + } + + cqpair = ctrlr->qp[qpair->sq.cqid]; + if (cqpair->cq.addr >= map_start && cqpair->cq.addr <= map_end) { + unmap_q(ctrlr, &cqpair->cq); + } } + pthread_mutex_unlock(&endpoint->lock); } - pthread_mutex_unlock(&endpoint->lock); if (info->prot == (PROT_WRITE | PROT_READ)) { ret = spdk_mem_unregister(info->mapping.iov_base, info->mapping.iov_len);