git: a2dfffb98917 - main - shsec: Allocate data blocks only for BIO_READ/WRITE requests

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Thu, 27 Jan 2022 15:15:04 UTC
The branch main has been updated by markj:

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

commit a2dfffb98917a57bfacb155b9d7d423c3e8ff792
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2022-01-27 14:53:02 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2022-01-27 14:56:07 +0000

    shsec: Allocate data blocks only for BIO_READ/WRITE requests
    
    In particular, there is no need to allocate a data block when passing
    BIO_FLUSH requests to child providers, and g_io_request() asserts that
    bp->bio_data == NULL for such requests.
    
    PR:             255131
    Reported and tested by: nvass@gmx.com
    MFC after:      2 weeks
    Sponsored by:   The FreeBSD Foundation
---
 sys/geom/shsec/g_shsec.c | 34 +++++++++++++++++++---------------
 1 file changed, 19 insertions(+), 15 deletions(-)

diff --git a/sys/geom/shsec/g_shsec.c b/sys/geom/shsec/g_shsec.c
index 65bfbc6681dd..88add59f6d26 100644
--- a/sys/geom/shsec/g_shsec.c
+++ b/sys/geom/shsec/g_shsec.c
@@ -270,8 +270,10 @@ g_shsec_done(struct bio *bp)
 			    (ssize_t)pbp->bio_length);
 		}
 	}
-	explicit_bzero(bp->bio_data, bp->bio_length);
-	uma_zfree(g_shsec_zone, bp->bio_data);
+	if (bp->bio_data != NULL) {
+		explicit_bzero(bp->bio_data, bp->bio_length);
+		uma_zfree(g_shsec_zone, bp->bio_data);
+	}
 	g_destroy_bio(bp);
 	pbp->bio_inbed++;
 	if (pbp->bio_children == pbp->bio_inbed) {
@@ -349,20 +351,22 @@ g_shsec_start(struct bio *bp)
 		 * Fill in the component buf structure.
 		 */
 		cbp->bio_done = g_shsec_done;
-		cbp->bio_data = uma_zalloc(g_shsec_zone, M_NOWAIT);
-		if (cbp->bio_data == NULL) {
-			g_shsec_alloc_failed++;
-			error = ENOMEM;
-			goto failure;
-		}
 		cbp->bio_caller2 = sc->sc_disks[no];
-		if (bp->bio_cmd == BIO_WRITE) {
-			if (no == 0) {
-				dst = (uint32_t *)cbp->bio_data;
-				bcopy(bp->bio_data, dst, len);
-			} else {
-				g_shsec_xor2((uint32_t *)cbp->bio_data, dst,
-				    len);
+		if (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE) {
+			cbp->bio_data = uma_zalloc(g_shsec_zone, M_NOWAIT);
+			if (cbp->bio_data == NULL) {
+				g_shsec_alloc_failed++;
+				error = ENOMEM;
+				goto failure;
+			}
+			if (bp->bio_cmd == BIO_WRITE) {
+				if (no == 0) {
+					dst = (uint32_t *)cbp->bio_data;
+					bcopy(bp->bio_data, dst, len);
+				} else {
+					g_shsec_xor2((uint32_t *)cbp->bio_data,
+					    dst, len);
+				}
 			}
 		}
 	}