git: 6f5141456ed0 - main - vtblk: secondary fix for dumping

From: Mitchell Horne <mhorne_at_FreeBSD.org>
Date: Mon, 23 Jan 2023 19:21:43 UTC
The branch main has been updated by mhorne:

URL: https://cgit.FreeBSD.org/src/commit/?id=6f5141456ed07a24097c2cf3d6c389bf971d78d2

commit 6f5141456ed07a24097c2cf3d6c389bf971d78d2
Author:     Mitchell Horne <mhorne@FreeBSD.org>
AuthorDate: 2023-01-09 17:14:19 +0000
Commit:     Mitchell Horne <mhorne@FreeBSD.org>
CommitDate: 2023-01-23 19:21:36 +0000

    vtblk: secondary fix for dumping
    
    The code paths while dumping do not got through busdma. As such,
    safeguard against calling bus_dmamap_sync() with a NULL map. The x86
    implementation tolerates this but others do not, resulting in a
    NULL dereference panic when dumping to a vtblk device on arm64, riscv,
    etc.
    
    Fixes:          782105f7c898 ("vtblk: Use busdma")
    Sponsored by:   The FreeBSD Foundation
    Differential Revision:  https://reviews.freebsd.org/D37990
---
 sys/dev/virtio/block/virtio_blk.c | 46 ++++++++++++++++++++++-----------------
 1 file changed, 26 insertions(+), 20 deletions(-)

diff --git a/sys/dev/virtio/block/virtio_blk.c b/sys/dev/virtio/block/virtio_blk.c
index 052017251c22..0e66cd3c89db 100644
--- a/sys/dev/virtio/block/virtio_blk.c
+++ b/sys/dev/virtio/block/virtio_blk.c
@@ -1126,15 +1126,17 @@ vtblk_request_execute_cb(void * callback_arg, bus_dma_segment_t * segs,
 	sglist_append(sg, &req->vbr_ack, sizeof(uint8_t));
 	readable = sg->sg_nseg - writable;
 
-	switch (bp->bio_cmd) {
-	case BIO_READ:
-		bus_dmamap_sync(sc->vtblk_dmat, req->vbr_mapp,
-		    BUS_DMASYNC_PREREAD);
-		break;
-	case BIO_WRITE:
-		bus_dmamap_sync(sc->vtblk_dmat, req->vbr_mapp,
-		    BUS_DMASYNC_PREWRITE);
-		break;
+	if (req->vbr_mapp != NULL) {
+		switch (bp->bio_cmd) {
+		case BIO_READ:
+			bus_dmamap_sync(sc->vtblk_dmat, req->vbr_mapp,
+			    BUS_DMASYNC_PREREAD);
+			break;
+		case BIO_WRITE:
+			bus_dmamap_sync(sc->vtblk_dmat, req->vbr_mapp,
+			    BUS_DMASYNC_PREWRITE);
+			break;
+		}
 	}
 
 	error = virtqueue_enqueue(vq, req, sg, readable, writable);
@@ -1191,17 +1193,21 @@ vtblk_queue_completed(struct vtblk_softc *sc, struct bio_queue *queue)
 		}
 
 		bp = req->vbr_bp;
-		switch (bp->bio_cmd) {
-		case BIO_READ:
-			bus_dmamap_sync(sc->vtblk_dmat, req->vbr_mapp,
-			    BUS_DMASYNC_POSTREAD);
-			bus_dmamap_unload(sc->vtblk_dmat, req->vbr_mapp);
-			break;
-		case BIO_WRITE:
-			bus_dmamap_sync(sc->vtblk_dmat, req->vbr_mapp,
-			    BUS_DMASYNC_POSTWRITE);
-			bus_dmamap_unload(sc->vtblk_dmat, req->vbr_mapp);
-			break;
+		if (req->vbr_mapp != NULL) {
+			switch (bp->bio_cmd) {
+			case BIO_READ:
+				bus_dmamap_sync(sc->vtblk_dmat, req->vbr_mapp,
+				    BUS_DMASYNC_POSTREAD);
+				bus_dmamap_unload(sc->vtblk_dmat,
+				    req->vbr_mapp);
+				break;
+			case BIO_WRITE:
+				bus_dmamap_sync(sc->vtblk_dmat, req->vbr_mapp,
+				    BUS_DMASYNC_POSTWRITE);
+				bus_dmamap_unload(sc->vtblk_dmat,
+				    req->vbr_mapp);
+				break;
+			}
 		}
 		bp->bio_error = vtblk_request_error(req);
 		TAILQ_INSERT_TAIL(queue, bp, bio_queue);