Wed Apr 19 00:38:30 2023 UTC ()
Fix not to allocate unnecessary descriptor

fixes PR/57358


(yamaguchi)
diff -r1.75 -r1.76 src/sys/dev/pci/virtio.c

cvs diff -r1.75 -r1.76 src/sys/dev/pci/virtio.c (expand / switch to unified diff)

--- src/sys/dev/pci/virtio.c 2023/04/19 00:23:45 1.75
+++ src/sys/dev/pci/virtio.c 2023/04/19 00:38:30 1.76
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: virtio.c,v 1.75 2023/04/19 00:23:45 yamaguchi Exp $ */ 1/* $NetBSD: virtio.c,v 1.76 2023/04/19 00:38:30 yamaguchi Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2020 The NetBSD Foundation, Inc. 4 * Copyright (c) 2020 The NetBSD Foundation, Inc.
5 * Copyright (c) 2012 Stefan Fritsch, Alexander Fiveg. 5 * Copyright (c) 2012 Stefan Fritsch, Alexander Fiveg.
6 * Copyright (c) 2010 Minoura Makoto. 6 * Copyright (c) 2010 Minoura Makoto.
7 * All rights reserved. 7 * All rights reserved.
8 * 8 *
9 * Redistribution and use in source and binary forms, with or without 9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions 10 * modification, are permitted provided that the following conditions
11 * are met: 11 * are met:
12 * 1. Redistributions of source code must retain the above copyright 12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer. 13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright 14 * 2. Redistributions in binary form must reproduce the above copyright
@@ -18,27 +18,27 @@ @@ -18,27 +18,27 @@
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */ 28 */
29 29
30#include <sys/cdefs.h> 30#include <sys/cdefs.h>
31__KERNEL_RCSID(0, "$NetBSD: virtio.c,v 1.75 2023/04/19 00:23:45 yamaguchi Exp $"); 31__KERNEL_RCSID(0, "$NetBSD: virtio.c,v 1.76 2023/04/19 00:38:30 yamaguchi Exp $");
32 32
33#include <sys/param.h> 33#include <sys/param.h>
34#include <sys/systm.h> 34#include <sys/systm.h>
35#include <sys/kernel.h> 35#include <sys/kernel.h>
36#include <sys/atomic.h> 36#include <sys/atomic.h>
37#include <sys/bus.h> 37#include <sys/bus.h>
38#include <sys/device.h> 38#include <sys/device.h>
39#include <sys/kmem.h> 39#include <sys/kmem.h>
40#include <sys/module.h> 40#include <sys/module.h>
41 41
42#define VIRTIO_PRIVATE 42#define VIRTIO_PRIVATE
43 43
44#include <dev/pci/virtioreg.h> /* XXX: move to non-pci */ 44#include <dev/pci/virtioreg.h> /* XXX: move to non-pci */
@@ -1086,37 +1086,38 @@ virtio_enqueue_reserve(struct virtio_sof @@ -1086,37 +1086,38 @@ virtio_enqueue_reserve(struct virtio_sof
1086 vd->addr = virtio_rw64(sc, addr); 1086 vd->addr = virtio_rw64(sc, addr);
1087 vd->len = virtio_rw32(sc, sizeof(struct vring_desc) * nsegs); 1087 vd->len = virtio_rw32(sc, sizeof(struct vring_desc) * nsegs);
1088 vd->flags = virtio_rw16(sc, VRING_DESC_F_INDIRECT); 1088 vd->flags = virtio_rw16(sc, VRING_DESC_F_INDIRECT);
1089 1089
1090 vd = &vq->vq_indirect[vq->vq_maxnsegs * slot]; 1090 vd = &vq->vq_indirect[vq->vq_maxnsegs * slot];
1091 vdx->desc_base = vd; 1091 vdx->desc_base = vd;
1092 vdx->desc_free_idx = 0; 1092 vdx->desc_free_idx = 0;
1093 1093
1094 for (i = 0; i < nsegs - 1; i++) { 1094 for (i = 0; i < nsegs - 1; i++) {
1095 vd[i].flags = virtio_rw16(sc, VRING_DESC_F_NEXT); 1095 vd[i].flags = virtio_rw16(sc, VRING_DESC_F_NEXT);
1096 } 1096 }
1097 vd[i].flags = virtio_rw16(sc, 0); 1097 vd[i].flags = virtio_rw16(sc, 0);
1098 } else { 1098 } else {
1099 uint16_t s; 1099 if (nsegs > 1) {
 1100 uint16_t s;
1100 1101
1101 s = vq_alloc_slot(sc, vq, nsegs - 1); 1102 s = vq_alloc_slot(sc, vq, nsegs - 1);
1102 if (s == VRING_DESC_CHAIN_END) { 1103 if (s == VRING_DESC_CHAIN_END) {
1103 vq_free_slot(sc, vq, slot); 1104 vq_free_slot(sc, vq, slot);
1104 return EAGAIN; 1105 return EAGAIN;
 1106 }
 1107 vd->next = virtio_rw16(sc, s);
 1108 vd->flags = virtio_rw16(sc, VRING_DESC_F_NEXT);
1105 } 1109 }
1106 1110
1107 vd->next = virtio_rw16(sc, s); 
1108 vd->flags = virtio_rw16(sc, VRING_DESC_F_NEXT); 
1109 
1110 vdx->desc_base = &vq->vq_desc[0]; 1111 vdx->desc_base = &vq->vq_desc[0];
1111 vdx->desc_free_idx = slot; 1112 vdx->desc_free_idx = slot;
1112 } 1113 }
1113 1114
1114 return 0; 1115 return 0;
1115} 1116}
1116 1117
1117/* 1118/*
1118 * enqueue: enqueue a single dmamap. 1119 * enqueue: enqueue a single dmamap.
1119 */ 1120 */
1120int 1121int
1121virtio_enqueue(struct virtio_softc *sc, struct virtqueue *vq, int slot, 1122virtio_enqueue(struct virtio_softc *sc, struct virtqueue *vq, int slot,
1122 bus_dmamap_t dmamap, bool write) 1123 bus_dmamap_t dmamap, bool write)