Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/dev Audit unload/unmap v.s. free against DMA buffer for ...



details:   https://anonhg.NetBSD.org/src/rev/dc90128d7704
branches:  trunk
changeset: 366500:dc90128d7704
user:      rin <rin%NetBSD.org@localhost>
date:      Sun May 29 10:43:45 2022 +0000

description:
Audit unload/unmap v.s. free against DMA buffer for sys/dev;
make sure that bus_dmamap_unload(9) [or bus_dmamap_destroy(9)] or
bus_dmamem_unmap(9) are preceding to freeing DMA buffer, if it is
loaded or mapped, respectively.

This is mandatory for some archs. See, e.g.:

http://www.nerv.org/netbsd/?q=id:20210511T013030Z.013443cc790088147e4beed43f53dedabeaf9312
http://www.nerv.org/netbsd/?q=id:20220511T172220Z.561179f0b6fcc5b9cd73e274f69d74e2ce9e4c93

For some drivers, resource leaks for error paths are fixed at
the same time.

XXX XXX XXX
Compile test only (at least one arch per driver).

diffstat:

 sys/dev/hyperv/if_hvn.c       |  11 +++++++----
 sys/dev/ic/ciss.c             |  16 ++++++++--------
 sys/dev/ic/elinkxl.c          |   8 ++++----
 sys/dev/ic/hme.c              |  22 ++++++++++++++--------
 sys/dev/ic/i82596.c           |  18 +++++++++---------
 sys/dev/isa/if_le_isa.c       |  27 ++++++++++++++++++---------
 sys/dev/isapnp/if_le_isapnp.c |  23 +++++++++++++++--------
 sys/dev/sbus/if_le.c          |   9 ++++++---
 sys/dev/sbus/if_le_ledma.c    |  22 ++++++++++++++--------
 sys/dev/tc/if_le_ioasic.c     |  16 +++++++++-------
 10 files changed, 104 insertions(+), 68 deletions(-)

diffs (truncated from 586 to 300 lines):

diff -r bc2a216b96a1 -r dc90128d7704 sys/dev/hyperv/if_hvn.c
--- a/sys/dev/hyperv/if_hvn.c   Sun May 29 10:43:19 2022 +0000
+++ b/sys/dev/hyperv/if_hvn.c   Sun May 29 10:43:45 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: if_hvn.c,v 1.22 2022/05/20 13:55:17 nonaka Exp $       */
+/*     $NetBSD: if_hvn.c,v 1.23 2022/05/29 10:43:45 rin Exp $  */
 /*     $OpenBSD: if_hvn.c,v 1.39 2018/03/11 14:31:34 mikeb Exp $       */
 
 /*-
@@ -35,7 +35,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_hvn.c,v 1.22 2022/05/20 13:55:17 nonaka Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_hvn.c,v 1.23 2022/05/29 10:43:45 rin Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_if_hvn.h"
@@ -3997,10 +3997,11 @@
                    PAGE_SIZE, NULL, BUS_DMA_WAITOK)) {
                        DPRINTF("%s: failed to load RNDIS command map\n",
                            device_xname(sc->sc_dev));
+                       bus_dmamem_unmap(sc->sc_dmat, rc->rc_req, PAGE_SIZE);
+                       rc->rc_req = NULL;
                        bus_dmamem_free(sc->sc_dmat, &rc->rc_segs,
                            rc->rc_nsegs);
                        bus_dmamap_destroy(sc->sc_dmat, rc->rc_dmap);
-                       rc->rc_req = NULL;
                        goto errout;
                }
                rc->rc_gpa = atop(rc->rc_dmap->dm_segs[0].ds_addr);
@@ -4034,8 +4035,10 @@
                        continue;
 
                TAILQ_REMOVE(&sc->sc_cntl_fq, rc, rc_entry);
+               bus_dmamap_unload(sc->sc_dmat, rc->rc_dmap);
+               bus_dmamem_unmap(sc->sc_dmat, rc->rc_req, PAGE_SIZE);
+               rc->rc_req = NULL;
                bus_dmamem_free(sc->sc_dmat, &rc->rc_segs, rc->rc_nsegs);
-               rc->rc_req = NULL;
                bus_dmamap_destroy(sc->sc_dmat, rc->rc_dmap);
                mutex_destroy(&rc->rc_lock);
                cv_destroy(&rc->rc_cv);
diff -r bc2a216b96a1 -r dc90128d7704 sys/dev/ic/ciss.c
--- a/sys/dev/ic/ciss.c Sun May 29 10:43:19 2022 +0000
+++ b/sys/dev/ic/ciss.c Sun May 29 10:43:45 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: ciss.c,v 1.53 2022/01/01 09:53:32 msaitoh Exp $        */
+/*     $NetBSD: ciss.c,v 1.54 2022/05/29 10:43:46 rin Exp $    */
 /*     $OpenBSD: ciss.c,v 1.68 2013/05/30 16:15:02 deraadt Exp $       */
 
 /*
@@ -19,7 +19,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: ciss.c,v 1.53 2022/01/01 09:53:32 msaitoh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ciss.c,v 1.54 2022/05/29 10:43:46 rin Exp $");
 
 #include "bio.h"
 
@@ -334,8 +334,8 @@
        if ((error = bus_dmamap_load(sc->sc_dmat, sc->cmdmap, sc->ccbs, total,
            NULL, BUS_DMA_NOWAIT))) {
                aprint_error(": cannot load CCBs dmamap (%d)\n", error);
+               bus_dmamap_destroy(sc->sc_dmat, sc->cmdmap);
                bus_dmamem_free(sc->sc_dmat, sc->cmdseg, 1);
-               bus_dmamap_destroy(sc->sc_dmat, sc->cmdmap);
                return -1;
        }
 
@@ -370,8 +370,8 @@
                aprint_error(": cannot create ccb#%d dmamap (%d)\n", i, error);
                if (i == 0) {
                        /* TODO leaking cmd's dmamaps and shitz */
+                       bus_dmamap_destroy(sc->sc_dmat, sc->cmdmap);
                        bus_dmamem_free(sc->sc_dmat, sc->cmdseg, 1);
-                       bus_dmamap_destroy(sc->sc_dmat, sc->cmdmap);
                        return -1;
                }
        }
@@ -395,8 +395,8 @@
        if (ciss_inq(sc, inq)) {
                aprint_error(": adapter inquiry failed\n");
                mutex_exit(&sc->sc_mutex_scratch);
+               bus_dmamap_destroy(sc->sc_dmat, sc->cmdmap);
                bus_dmamem_free(sc->sc_dmat, sc->cmdseg, 1);
-               bus_dmamap_destroy(sc->sc_dmat, sc->cmdmap);
                return -1;
        }
 
@@ -404,8 +404,8 @@
                aprint_error(": big map is not supported, flags=0x%x\n",
                    inq->flags);
                mutex_exit(&sc->sc_mutex_scratch);
+               bus_dmamap_destroy(sc->sc_dmat, sc->cmdmap);
                bus_dmamem_free(sc->sc_dmat, sc->cmdseg, 1);
-               bus_dmamap_destroy(sc->sc_dmat, sc->cmdmap);
                return -1;
        }
 
@@ -434,8 +434,8 @@
        /* map LDs */
        if (ciss_ldmap(sc)) {
                aprint_error_dev(sc->sc_dev, "adapter LD map failed\n");
+               bus_dmamap_destroy(sc->sc_dmat, sc->cmdmap);
                bus_dmamem_free(sc->sc_dmat, sc->cmdseg, 1);
-               bus_dmamap_destroy(sc->sc_dmat, sc->cmdmap);
                return -1;
        }
 
@@ -446,8 +446,8 @@
        if (!(sc->sc_sh = shutdownhook_establish(ciss_shutdown, sc))) {
                aprint_error_dev(sc->sc_dev,
                    "unable to establish shutdown hook\n");
+               bus_dmamap_destroy(sc->sc_dmat, sc->cmdmap);
                bus_dmamem_free(sc->sc_dmat, sc->cmdseg, 1);
-               bus_dmamap_destroy(sc->sc_dmat, sc->cmdmap);
                return -1;
        }
 
diff -r bc2a216b96a1 -r dc90128d7704 sys/dev/ic/elinkxl.c
--- a/sys/dev/ic/elinkxl.c      Sun May 29 10:43:19 2022 +0000
+++ b/sys/dev/ic/elinkxl.c      Sun May 29 10:43:45 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: elinkxl.c,v 1.138 2020/03/12 03:01:46 thorpej Exp $    */
+/*     $NetBSD: elinkxl.c,v 1.139 2022/05/29 10:43:46 rin Exp $        */
 
 /*-
  * Copyright (c) 1998 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: elinkxl.c,v 1.138 2020/03/12 03:01:46 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: elinkxl.c,v 1.139 2022/05/29 10:43:46 rin Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -1598,10 +1598,10 @@
        for (tx = sc->tx_head ; tx != NULL; tx = tx->tx_next) {
                if (tx->tx_mbhead == NULL)
                        continue;
+               bus_dmamap_unload(sc->sc_dmat, tx->tx_dmamap);
+               tx->tx_dpd->dpd_fsh = tx->tx_dpd->dpd_nextptr = 0;
                m_freem(tx->tx_mbhead);
                tx->tx_mbhead = NULL;
-               bus_dmamap_unload(sc->sc_dmat, tx->tx_dmamap);
-               tx->tx_dpd->dpd_fsh = tx->tx_dpd->dpd_nextptr = 0;
                bus_dmamap_sync(sc->sc_dmat, sc->sc_dpd_dmamap,
                    ((char *)tx->tx_dpd - (char *)sc->sc_dpd),
                    sizeof (struct ex_dpd),
diff -r bc2a216b96a1 -r dc90128d7704 sys/dev/ic/hme.c
--- a/sys/dev/ic/hme.c  Sun May 29 10:43:19 2022 +0000
+++ b/sys/dev/ic/hme.c  Sun May 29 10:43:45 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: hme.c,v 1.108 2020/03/12 03:01:46 thorpej Exp $        */
+/*     $NetBSD: hme.c,v 1.109 2022/05/29 10:43:46 rin Exp $    */
 
 /*-
  * Copyright (c) 1999 The NetBSD Foundation, Inc.
@@ -34,7 +34,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: hme.c,v 1.108 2020/03/12 03:01:46 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: hme.c,v 1.109 2022/05/29 10:43:46 rin Exp $");
 
 /* #define HMEDEBUG */
 
@@ -198,16 +198,14 @@
                                    BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
                aprint_error_dev(sc->sc_dev, "DMA buffer map error %d\n",
                        error);
-               bus_dmamap_unload(dmatag, sc->sc_dmamap);
-               bus_dmamem_free(dmatag, &seg, rseg);
-               return;
+               goto bad_free;
        }
 
        if ((error = bus_dmamap_create(dmatag, size, 1, size, 0,
                                    BUS_DMA_NOWAIT, &sc->sc_dmamap)) != 0) {
                aprint_error_dev(sc->sc_dev, "DMA map create error %d\n",
                        error);
-               return;
+               goto bad_unmap;
        }
 
        /* Load the buffer */
@@ -216,8 +214,7 @@
            BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
                aprint_error_dev(sc->sc_dev, "DMA buffer map load error %d\n",
                        error);
-               bus_dmamem_free(dmatag, &seg, rseg);
-               return;
+               goto bad_destroy;
        }
        sc->sc_rb.rb_dmabase = sc->sc_dmamap->dm_segs[0].ds_addr;
 
@@ -316,6 +313,15 @@
 
        callout_init(&sc->sc_tick_ch, 0);
        callout_setfunc(&sc->sc_tick_ch, hme_tick, sc);
+
+       return;
+
+ bad_destroy:
+       bus_dmamap_destroy(dmatag, sc->sc_dmamap);
+ bad_unmap:
+       bus_dmamem_unmap(dmatag, sc->sc_rb.rb_membase, size);
+ bad_free:
+       bus_dmamem_free(dmatag, &seg, rseg);
 }
 
 void
diff -r bc2a216b96a1 -r dc90128d7704 sys/dev/ic/i82596.c
--- a/sys/dev/ic/i82596.c       Sun May 29 10:43:19 2022 +0000
+++ b/sys/dev/ic/i82596.c       Sun May 29 10:43:45 2022 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: i82596.c,v 1.45 2021/11/10 17:19:30 msaitoh Exp $ */
+/* $NetBSD: i82596.c,v 1.46 2022/05/29 10:43:46 rin Exp $ */
 
 /*
  * Copyright (c) 2003 Jochen Kunz.
@@ -43,7 +43,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: i82596.c,v 1.45 2021/11/10 17:19:30 msaitoh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: i82596.c,v 1.46 2022/05/29 10:43:46 rin Exp $");
 
 /* autoconfig and device stuff */
 #include <sys/param.h>
@@ -330,10 +330,10 @@
                        if (sc->sc_next_tbd != 0) {
                                /* A TX CMD list finished, cleanup */
                                for (n = 0 ; n < sc->sc_next_cb ; n++) {
+                                       bus_dmamap_unload(sc->sc_dmat,
+                                           sc->sc_tx_map[n]);
                                        m_freem(sc->sc_tx_mbuf[n]);
                                        sc->sc_tx_mbuf[n] = NULL;
-                                       bus_dmamap_unload(sc->sc_dmat,
-                                           sc->sc_tx_map[n]);
                                        IEE_CBSYNC(sc, n,
                                            BUS_DMASYNC_POSTREAD |
                                            BUS_DMASYNC_POSTWRITE);
@@ -968,11 +968,11 @@
            IEE_SWAPA32(IEE_PHYS_SHMEM(sc->sc_rbd_off));
        if (err != 0) {
                for (n = 0 ; n < r; n++) {
-                       m_freem(sc->sc_rx_mbuf[n]);
-                       sc->sc_rx_mbuf[n] = NULL;
                        bus_dmamap_unload(sc->sc_dmat, sc->sc_rx_map[n]);
                        bus_dmamap_destroy(sc->sc_dmat, sc->sc_rx_map[n]);
                        sc->sc_rx_map[n] = NULL;
+                       m_freem(sc->sc_rx_mbuf[n]);
+                       sc->sc_rx_mbuf[n] = NULL;
                }
                for (n = 0 ; n < t ; n++) {
                        bus_dmamap_destroy(sc->sc_dmat, sc->sc_tx_map[n]);
@@ -1034,14 +1034,14 @@
                sc->sc_tx_map[n] = NULL;
        }
        for (n = 0 ; n < IEE_NRFD ; n++) {
-               if (sc->sc_rx_mbuf[n] != NULL)
-                       m_freem(sc->sc_rx_mbuf[n]);
-               sc->sc_rx_mbuf[n] = NULL;
                if (sc->sc_rx_map[n] != NULL) {
                        bus_dmamap_unload(sc->sc_dmat, sc->sc_rx_map[n]);
                        bus_dmamap_destroy(sc->sc_dmat, sc->sc_rx_map[n]);
                }
                sc->sc_rx_map[n] = NULL;
+               if (sc->sc_rx_mbuf[n] != NULL)
+                       m_freem(sc->sc_rx_mbuf[n]);
+               sc->sc_rx_mbuf[n] = NULL;
        }
 }
 
diff -r bc2a216b96a1 -r dc90128d7704 sys/dev/isa/if_le_isa.c
--- a/sys/dev/isa/if_le_isa.c   Sun May 29 10:43:19 2022 +0000
+++ b/sys/dev/isa/if_le_isa.c   Sun May 29 10:43:45 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: if_le_isa.c,v 1.52 2021/12/08 20:50:02 andvar Exp $    */
+/*     $NetBSD: if_le_isa.c,v 1.53 2022/05/29 10:43:46 rin Exp $       */
 
 /*-
  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
@@ -65,7 +65,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_le_isa.c,v 1.52 2021/12/08 20:50:02 andvar Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_le_isa.c,v 1.53 2022/05/29 10:43:46 rin Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -328,13 +328,13 @@
                             &rseg, BUS_DMA_NOWAIT)) {
                aprint_error_dev(sc->sc_dev,
                    "couldn't allocate memory for card\n");



Home | Main Index | Thread Index | Old Index