Skip to content

Commit f912863

Browse files
committed
Merge remote-tracking branch 'remotes/stefanha-gitlab/tags/block-pull-request' into staging
Pull request Userspace NVMe driver patches. # gpg: Signature made Tue 07 Sep 2021 09:13:57 BST # gpg: using RSA key 8695A8BFD3F97CDAAC35775A9CA4ABB381AB73C8 # gpg: Good signature from "Stefan Hajnoczi <[email protected]>" [full] # gpg: aka "Stefan Hajnoczi <[email protected]>" [full] # Primary key fingerprint: 8695 A8BF D3F9 7CDA AC35 775A 9CA4 ABB3 81AB 73C8 * remotes/stefanha-gitlab/tags/block-pull-request: block/nvme: Only report VFIO error on failed retry util/vfio-helpers: Let qemu_vfio_do_mapping() propagate Error util/vfio-helpers: Simplify qemu_vfio_dma_map() returning directly util/vfio-helpers: Use error_setg in qemu_vfio_find_[fixed/temp]_iova util/vfio-helpers: Extract qemu_vfio_water_mark_reached() util/vfio-helpers: Pass Error handle to qemu_vfio_dma_map() block/nvme: Have nvme_create_queue_pair() report errors consistently util/vfio-helpers: Remove unreachable code in qemu_vfio_dma_map() util/vfio-helpers: Replace qemu_mutex_lock() calls with QEMU_LOCK_GUARD util/vfio-helpers: Let qemu_vfio_verify_mappings() use error_report() block/nvme: Use safer trace format string Signed-off-by: Peter Maydell <[email protected]>
2 parents a61c30b + 9bd2788 commit f912863

File tree

4 files changed

+76
-56
lines changed

4 files changed

+76
-56
lines changed

block/nvme.c

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,11 @@ static bool nvme_init_queue(BDRVNVMeState *s, NVMeQueue *q,
176176
return false;
177177
}
178178
memset(q->queue, 0, bytes);
179-
r = qemu_vfio_dma_map(s->vfio, q->queue, bytes, false, &q->iova);
179+
r = qemu_vfio_dma_map(s->vfio, q->queue, bytes, false, &q->iova, errp);
180180
if (r) {
181-
error_setg(errp, "Cannot map queue");
182-
return false;
181+
error_prepend(errp, "Cannot map queue: ");
183182
}
184-
return true;
183+
return r == 0;
185184
}
186185

187186
static void nvme_free_queue_pair(NVMeQueuePair *q)
@@ -220,6 +219,7 @@ static NVMeQueuePair *nvme_create_queue_pair(BDRVNVMeState *s,
220219

221220
q = g_try_new0(NVMeQueuePair, 1);
222221
if (!q) {
222+
error_setg(errp, "Cannot allocate queue pair");
223223
return NULL;
224224
}
225225
trace_nvme_create_queue_pair(idx, q, size, aio_context,
@@ -228,6 +228,7 @@ static NVMeQueuePair *nvme_create_queue_pair(BDRVNVMeState *s,
228228
qemu_real_host_page_size);
229229
q->prp_list_pages = qemu_try_memalign(qemu_real_host_page_size, bytes);
230230
if (!q->prp_list_pages) {
231+
error_setg(errp, "Cannot allocate PRP page list");
231232
goto fail;
232233
}
233234
memset(q->prp_list_pages, 0, bytes);
@@ -237,8 +238,9 @@ static NVMeQueuePair *nvme_create_queue_pair(BDRVNVMeState *s,
237238
qemu_co_queue_init(&q->free_req_queue);
238239
q->completion_bh = aio_bh_new(aio_context, nvme_process_completion_bh, q);
239240
r = qemu_vfio_dma_map(s->vfio, q->prp_list_pages, bytes,
240-
false, &prp_list_iova);
241+
false, &prp_list_iova, errp);
241242
if (r) {
243+
error_prepend(errp, "Cannot map buffer for DMA: ");
242244
goto fail;
243245
}
244246
q->free_req_head = -1;
@@ -531,9 +533,9 @@ static bool nvme_identify(BlockDriverState *bs, int namespace, Error **errp)
531533
error_setg(errp, "Cannot allocate buffer for identify response");
532534
goto out;
533535
}
534-
r = qemu_vfio_dma_map(s->vfio, id, id_size, true, &iova);
536+
r = qemu_vfio_dma_map(s->vfio, id, id_size, true, &iova, errp);
535537
if (r) {
536-
error_setg(errp, "Cannot map buffer for DMA");
538+
error_prepend(errp, "Cannot map buffer for DMA: ");
537539
goto out;
538540
}
539541

@@ -1017,6 +1019,7 @@ static coroutine_fn int nvme_cmd_map_qiov(BlockDriverState *bs, NvmeCmd *cmd,
10171019
uint64_t *pagelist = req->prp_list_page;
10181020
int i, j, r;
10191021
int entries = 0;
1022+
Error *local_err = NULL, **errp = NULL;
10201023

10211024
assert(qiov->size);
10221025
assert(QEMU_IS_ALIGNED(qiov->size, s->page_size));
@@ -1029,7 +1032,7 @@ static coroutine_fn int nvme_cmd_map_qiov(BlockDriverState *bs, NvmeCmd *cmd,
10291032
try_map:
10301033
r = qemu_vfio_dma_map(s->vfio,
10311034
qiov->iov[i].iov_base,
1032-
len, true, &iova);
1035+
len, true, &iova, errp);
10331036
if (r == -ENOSPC) {
10341037
/*
10351038
* In addition to the -ENOMEM error, the VFIO_IOMMU_MAP_DMA
@@ -1064,6 +1067,8 @@ static coroutine_fn int nvme_cmd_map_qiov(BlockDriverState *bs, NvmeCmd *cmd,
10641067
goto fail;
10651068
}
10661069
}
1070+
errp = &local_err;
1071+
10671072
goto try_map;
10681073
}
10691074
if (r) {
@@ -1107,6 +1112,9 @@ static coroutine_fn int nvme_cmd_map_qiov(BlockDriverState *bs, NvmeCmd *cmd,
11071112
* because they are already mapped before calling this function; for
11081113
* temporary mappings, a later nvme_cmd_(un)map_qiov will reclaim by
11091114
* calling qemu_vfio_dma_reset_temporary when necessary. */
1115+
if (local_err) {
1116+
error_reportf_err(local_err, "Cannot map buffer for DMA: ");
1117+
}
11101118
return r;
11111119
}
11121120

@@ -1521,14 +1529,15 @@ static void nvme_aio_unplug(BlockDriverState *bs)
15211529
static void nvme_register_buf(BlockDriverState *bs, void *host, size_t size)
15221530
{
15231531
int ret;
1532+
Error *local_err = NULL;
15241533
BDRVNVMeState *s = bs->opaque;
15251534

1526-
ret = qemu_vfio_dma_map(s->vfio, host, size, false, NULL);
1535+
ret = qemu_vfio_dma_map(s->vfio, host, size, false, NULL, &local_err);
15271536
if (ret) {
15281537
/* FIXME: we may run out of IOVA addresses after repeated
15291538
* bdrv_register_buf/bdrv_unregister_buf, because nvme_vfio_dma_unmap
15301539
* doesn't reclaim addresses for fixed mappings. */
1531-
error_report("nvme_register_buf failed: %s", strerror(-ret));
1540+
error_reportf_err(local_err, "nvme_register_buf failed: ");
15321541
}
15331542
}
15341543

block/trace-events

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ nvme_dsm(void *s, uint64_t offset, uint64_t bytes) "s %p offset 0x%"PRIx64" byte
156156
nvme_dsm_done(void *s, uint64_t offset, uint64_t bytes, int ret) "s %p offset 0x%"PRIx64" bytes %"PRId64" ret %d"
157157
nvme_dma_map_flush(void *s) "s %p"
158158
nvme_free_req_queue_wait(void *s, unsigned q_index) "s %p q #%u"
159-
nvme_create_queue_pair(unsigned q_index, void *q, unsigned size, void *aio_context, int fd) "index %u q %p size %u aioctx %p fd %d"
159+
nvme_create_queue_pair(unsigned q_index, void *q, size_t size, void *aio_context, int fd) "index %u q %p size %zu aioctx %p fd %d"
160160
nvme_free_queue_pair(unsigned q_index, void *q) "index %u q %p"
161161
nvme_cmd_map_qiov(void *s, void *cmd, void *req, void *qiov, int entries) "s %p cmd %p req %p qiov %p entries %d"
162162
nvme_cmd_map_qiov_pages(void *s, int i, uint64_t page) "s %p page[%d] 0x%"PRIx64

include/qemu/vfio-helpers.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ typedef struct QEMUVFIOState QEMUVFIOState;
1818
QEMUVFIOState *qemu_vfio_open_pci(const char *device, Error **errp);
1919
void qemu_vfio_close(QEMUVFIOState *s);
2020
int qemu_vfio_dma_map(QEMUVFIOState *s, void *host, size_t size,
21-
bool temporary, uint64_t *iova_list);
21+
bool temporary, uint64_t *iova_list, Error **errp);
2222
int qemu_vfio_dma_reset_temporary(QEMUVFIOState *s);
2323
void qemu_vfio_dma_unmap(QEMUVFIOState *s, void *host);
2424
void *qemu_vfio_pci_map_bar(QEMUVFIOState *s, int index,

util/vfio-helpers.c

Lines changed: 55 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -463,13 +463,15 @@ static void qemu_vfio_ram_block_added(RAMBlockNotifier *n, void *host,
463463
size_t size, size_t max_size)
464464
{
465465
QEMUVFIOState *s = container_of(n, QEMUVFIOState, ram_notifier);
466+
Error *local_err = NULL;
466467
int ret;
467468

468469
trace_qemu_vfio_ram_block_added(s, host, max_size);
469-
ret = qemu_vfio_dma_map(s, host, max_size, false, NULL);
470+
ret = qemu_vfio_dma_map(s, host, max_size, false, NULL, &local_err);
470471
if (ret) {
471-
error_report("qemu_vfio_dma_map(%p, %zu) failed: %s", host, max_size,
472-
strerror(-ret));
472+
error_reportf_err(local_err,
473+
"qemu_vfio_dma_map(%p, %zu) failed: ",
474+
host, max_size);
473475
}
474476
}
475477

@@ -608,7 +610,7 @@ static IOVAMapping *qemu_vfio_add_mapping(QEMUVFIOState *s,
608610

609611
/* Do the DMA mapping with VFIO. */
610612
static int qemu_vfio_do_mapping(QEMUVFIOState *s, void *host, size_t size,
611-
uint64_t iova)
613+
uint64_t iova, Error **errp)
612614
{
613615
struct vfio_iommu_type1_dma_map dma_map = {
614616
.argsz = sizeof(dma_map),
@@ -620,7 +622,7 @@ static int qemu_vfio_do_mapping(QEMUVFIOState *s, void *host, size_t size,
620622
trace_qemu_vfio_do_mapping(s, host, iova, size);
621623

622624
if (ioctl(s->container, VFIO_IOMMU_MAP_DMA, &dma_map)) {
623-
error_report("VFIO_MAP_DMA failed: %s", strerror(errno));
625+
error_setg_errno(errp, errno, "VFIO_MAP_DMA failed");
624626
return -errno;
625627
}
626628
return 0;
@@ -660,13 +662,13 @@ static bool qemu_vfio_verify_mappings(QEMUVFIOState *s)
660662
if (QEMU_VFIO_DEBUG) {
661663
for (i = 0; i < s->nr_mappings - 1; ++i) {
662664
if (!(s->mappings[i].host < s->mappings[i + 1].host)) {
663-
fprintf(stderr, "item %d not sorted!\n", i);
665+
error_report("item %d not sorted!", i);
664666
qemu_vfio_dump_mappings(s);
665667
return false;
666668
}
667669
if (!(s->mappings[i].host + s->mappings[i].size <=
668670
s->mappings[i + 1].host)) {
669-
fprintf(stderr, "item %d overlap with next!\n", i);
671+
error_report("item %d overlap with next!", i);
670672
qemu_vfio_dump_mappings(s);
671673
return false;
672674
}
@@ -675,8 +677,8 @@ static bool qemu_vfio_verify_mappings(QEMUVFIOState *s)
675677
return true;
676678
}
677679

678-
static int
679-
qemu_vfio_find_fixed_iova(QEMUVFIOState *s, size_t size, uint64_t *iova)
680+
static bool qemu_vfio_find_fixed_iova(QEMUVFIOState *s, size_t size,
681+
uint64_t *iova, Error **errp)
680682
{
681683
int i;
682684

@@ -691,14 +693,16 @@ qemu_vfio_find_fixed_iova(QEMUVFIOState *s, size_t size, uint64_t *iova)
691693
s->usable_iova_ranges[i].end - s->low_water_mark + 1 == 0) {
692694
*iova = s->low_water_mark;
693695
s->low_water_mark += size;
694-
return 0;
696+
return true;
695697
}
696698
}
697-
return -ENOMEM;
699+
error_setg(errp, "fixed iova range not found");
700+
701+
return false;
698702
}
699703

700-
static int
701-
qemu_vfio_find_temp_iova(QEMUVFIOState *s, size_t size, uint64_t *iova)
704+
static bool qemu_vfio_find_temp_iova(QEMUVFIOState *s, size_t size,
705+
uint64_t *iova, Error **errp)
702706
{
703707
int i;
704708

@@ -713,10 +717,27 @@ qemu_vfio_find_temp_iova(QEMUVFIOState *s, size_t size, uint64_t *iova)
713717
s->high_water_mark - s->usable_iova_ranges[i].start + 1 == 0) {
714718
*iova = s->high_water_mark - size;
715719
s->high_water_mark = *iova;
716-
return 0;
720+
return true;
717721
}
718722
}
719-
return -ENOMEM;
723+
error_setg(errp, "temporary iova range not found");
724+
725+
return false;
726+
}
727+
728+
/**
729+
* qemu_vfio_water_mark_reached:
730+
*
731+
* Returns %true if high watermark has been reached, %false otherwise.
732+
*/
733+
static bool qemu_vfio_water_mark_reached(QEMUVFIOState *s, size_t size,
734+
Error **errp)
735+
{
736+
if (s->high_water_mark - s->low_water_mark + 1 < size) {
737+
error_setg(errp, "iova exhausted (water mark reached)");
738+
return true;
739+
}
740+
return false;
720741
}
721742

722743
/* Map [host, host + size) area into a contiguous IOVA address space, and store
@@ -725,61 +746,53 @@ qemu_vfio_find_temp_iova(QEMUVFIOState *s, size_t size, uint64_t *iova)
725746
* mapping status within this area is not allowed).
726747
*/
727748
int qemu_vfio_dma_map(QEMUVFIOState *s, void *host, size_t size,
728-
bool temporary, uint64_t *iova)
749+
bool temporary, uint64_t *iova, Error **errp)
729750
{
730-
int ret = 0;
731751
int index;
732752
IOVAMapping *mapping;
733753
uint64_t iova0;
734754

735755
assert(QEMU_PTR_IS_ALIGNED(host, qemu_real_host_page_size));
736756
assert(QEMU_IS_ALIGNED(size, qemu_real_host_page_size));
737757
trace_qemu_vfio_dma_map(s, host, size, temporary, iova);
738-
qemu_mutex_lock(&s->lock);
758+
QEMU_LOCK_GUARD(&s->lock);
739759
mapping = qemu_vfio_find_mapping(s, host, &index);
740760
if (mapping) {
741761
iova0 = mapping->iova + ((uint8_t *)host - (uint8_t *)mapping->host);
742762
} else {
743-
if (s->high_water_mark - s->low_water_mark + 1 < size) {
744-
ret = -ENOMEM;
745-
goto out;
763+
int ret;
764+
765+
if (qemu_vfio_water_mark_reached(s, size, errp)) {
766+
return -ENOMEM;
746767
}
747768
if (!temporary) {
748-
if (qemu_vfio_find_fixed_iova(s, size, &iova0)) {
749-
ret = -ENOMEM;
750-
goto out;
769+
if (!qemu_vfio_find_fixed_iova(s, size, &iova0, errp)) {
770+
return -ENOMEM;
751771
}
752772

753773
mapping = qemu_vfio_add_mapping(s, host, size, index + 1, iova0);
754-
if (!mapping) {
755-
ret = -ENOMEM;
756-
goto out;
757-
}
758774
assert(qemu_vfio_verify_mappings(s));
759-
ret = qemu_vfio_do_mapping(s, host, size, iova0);
760-
if (ret) {
775+
ret = qemu_vfio_do_mapping(s, host, size, iova0, errp);
776+
if (ret < 0) {
761777
qemu_vfio_undo_mapping(s, mapping, NULL);
762-
goto out;
778+
return ret;
763779
}
764780
qemu_vfio_dump_mappings(s);
765781
} else {
766-
if (qemu_vfio_find_temp_iova(s, size, &iova0)) {
767-
ret = -ENOMEM;
768-
goto out;
782+
if (!qemu_vfio_find_temp_iova(s, size, &iova0, errp)) {
783+
return -ENOMEM;
769784
}
770-
ret = qemu_vfio_do_mapping(s, host, size, iova0);
771-
if (ret) {
772-
goto out;
785+
ret = qemu_vfio_do_mapping(s, host, size, iova0, errp);
786+
if (ret < 0) {
787+
return ret;
773788
}
774789
}
775790
}
776791
trace_qemu_vfio_dma_mapped(s, host, iova0, size);
777792
if (iova) {
778793
*iova = iova0;
779794
}
780-
out:
781-
qemu_mutex_unlock(&s->lock);
782-
return ret;
795+
return 0;
783796
}
784797

785798
/* Reset the high watermark and free all "temporary" mappings. */
@@ -813,14 +826,12 @@ void qemu_vfio_dma_unmap(QEMUVFIOState *s, void *host)
813826
}
814827

815828
trace_qemu_vfio_dma_unmap(s, host);
816-
qemu_mutex_lock(&s->lock);
829+
QEMU_LOCK_GUARD(&s->lock);
817830
m = qemu_vfio_find_mapping(s, host, &index);
818831
if (!m) {
819-
goto out;
832+
return;
820833
}
821834
qemu_vfio_undo_mapping(s, m, NULL);
822-
out:
823-
qemu_mutex_unlock(&s->lock);
824835
}
825836

826837
static void qemu_vfio_reset(QEMUVFIOState *s)

0 commit comments

Comments
 (0)