Source-Changes-HG archive

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

[src/trunk]: src/sys/dev/usb ualea(4): Simplify xfer error branches.



details:   https://anonhg.NetBSD.org/src/rev/7c52775f374a
branches:  trunk
changeset: 364383:7c52775f374a
user:      riastradh <riastradh%NetBSD.org@localhost>
date:      Sun Mar 20 13:13:10 2022 +0000

description:
ualea(4): Simplify xfer error branches.

- Avoid going into a loop in case the transfer fails repeatedly --
  just give up immediately if it fails.

- Assert result size is reasonable; no need to assume usbdi(9) is
  malicious.  If it can return ux_actlen > ux_length, that's a bug in
  usbdi(9) that we should fix.

diffstat:

 sys/dev/usb/ualea.c |  40 +++++++++++++++++++---------------------
 1 files changed, 19 insertions(+), 21 deletions(-)

diffs (75 lines):

diff -r 8c96023f95a2 -r 7c52775f374a sys/dev/usb/ualea.c
--- a/sys/dev/usb/ualea.c       Sun Mar 20 00:41:01 2022 +0000
+++ b/sys/dev/usb/ualea.c       Sun Mar 20 13:13:10 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: ualea.c,v 1.17 2022/03/20 00:41:01 riastradh Exp $     */
+/*     $NetBSD: ualea.c,v 1.18 2022/03/20 13:13:10 riastradh Exp $     */
 
 /*-
  * Copyright (c) 2017 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: ualea.c,v 1.17 2022/03/20 00:41:01 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ualea.c,v 1.18 2022/03/20 13:13:10 riastradh Exp $");
 
 #include <sys/types.h>
 #include <sys/atomic.h>
@@ -230,39 +230,37 @@
        void *pkt;
        uint32_t pktsize;
 
-       /* Check the transfer status.  */
+       /*
+        * If the transfer failed, give up -- forget what we need and
+        * don't reschedule ourselves.
+        */
        if (status) {
                device_printf(sc->sc_dev, "xfer failed: %s\n",
                    usbd_errstr(status));
-               pktsize = 0;
-               goto out;
+               mutex_enter(&sc->sc_lock);
+               sc->sc_needed = 0;
+               sc->sc_inflight = false;
+               mutex_exit(&sc->sc_lock);
+               return;
        }
 
-       /* Get and sanity-check the transferred size.  */
+       /* Get the transferred size.  */
        usbd_get_xfer_status(xfer, NULL, &pkt, &pktsize, NULL);
-       if (pktsize > sc->sc_maxpktsize) {
-               device_printf(sc->sc_dev,
-                   "bogus packet size: %"PRIu32" > %"PRIu16" (max), ignoring"
-                   "\n",
-                   pktsize, sc->sc_maxpktsize);
-               goto out;
-       }
+       KASSERTMSG(pktsize <= sc->sc_maxpktsize,
+           "pktsize %"PRIu32" > %"PRIu16" (max)",
+           pktsize, sc->sc_maxpktsize);
 
        /* Add the data to the pool.  */
        rnd_add_data(&sc->sc_rnd, pkt, pktsize, NBBY*pktsize);
 
-out:
+       /*
+        * Debit what we contributed from what we need, mark the xfer
+        * as done, and reschedule the xfer if we still need more.
+        */
        mutex_enter(&sc->sc_lock);
-
-       /* Debit what we contributed from what we need.  */
        sc->sc_needed -= MIN(sc->sc_needed, pktsize);
-
-       /* Mark xfer done.  */
        sc->sc_inflight = false;
-
-       /* Reissue xfer if we still need more.  */
        ualea_xfer(sc);
-
        mutex_exit(&sc->sc_lock);
 }
 



Home | Main Index | Thread Index | Old Index