Module Name:    src
Committed By:   yamaguchi
Date:           Wed Apr 19 00:38:30 UTC 2023

Modified Files:
        src/sys/dev/pci: virtio.c

Log Message:
Fix not to allocate unnecessary descriptor

fixes PR/57358


To generate a diff of this commit:
cvs rdiff -u -r1.75 -r1.76 src/sys/dev/pci/virtio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/pci/virtio.c
diff -u src/sys/dev/pci/virtio.c:1.75 src/sys/dev/pci/virtio.c:1.76
--- src/sys/dev/pci/virtio.c:1.75	Wed Apr 19 00:23:45 2023
+++ src/sys/dev/pci/virtio.c	Wed Apr 19 00:38:30 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: virtio.c,v 1.75 2023/04/19 00:23:45 yamaguchi Exp $	*/
+/*	$NetBSD: virtio.c,v 1.76 2023/04/19 00:38:30 yamaguchi Exp $	*/
 
 /*
  * Copyright (c) 2020 The NetBSD Foundation, Inc.
@@ -28,7 +28,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: virtio.c,v 1.75 2023/04/19 00:23:45 yamaguchi Exp $");
+__KERNEL_RCSID(0, "$NetBSD: virtio.c,v 1.76 2023/04/19 00:38:30 yamaguchi Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -1096,17 +1096,18 @@ virtio_enqueue_reserve(struct virtio_sof
 		}
 		vd[i].flags  = virtio_rw16(sc, 0);
 	} else {
-		uint16_t s;
+		if (nsegs > 1) {
+			uint16_t s;
 
-		s = vq_alloc_slot(sc, vq, nsegs - 1);
-		if (s == VRING_DESC_CHAIN_END) {
-			vq_free_slot(sc, vq, slot);
-			return EAGAIN;
+			s = vq_alloc_slot(sc, vq, nsegs - 1);
+			if (s == VRING_DESC_CHAIN_END) {
+				vq_free_slot(sc, vq, slot);
+				return EAGAIN;
+			}
+			vd->next = virtio_rw16(sc, s);
+			vd->flags = virtio_rw16(sc, VRING_DESC_F_NEXT);
 		}
 
-		vd->next = virtio_rw16(sc, s);
-		vd->flags = virtio_rw16(sc, VRING_DESC_F_NEXT);
-
 		vdx->desc_base = &vq->vq_desc[0];
 		vdx->desc_free_idx = slot;
 	}

Reply via email to