Source-Changes-HG archive

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

[src/trunk]: src/sys/dev/usb xhci(4): Avoid holding bus lock across usb_delay...



details:   https://anonhg.NetBSD.org/src/rev/171ae8c845c5
branches:  trunk
changeset: 363399:171ae8c845c5
user:      riastradh <riastradh%NetBSD.org@localhost>
date:      Wed Mar 09 22:19:07 2022 +0000

description:
xhci(4): Avoid holding bus lock across usb_delay_ms.

We may still need a mechanism to serialize access to the portsc
registers between xhci_roothub_ctrl and xhci_suspend/resume, but the
bus lock is no longer that, and holding the bus lock across
usb_delay_ms may lead to deadlock by blocking the softints that wake
usb_delay_ms.

diffstat:

 sys/dev/usb/xhci.c |  20 +++++++++++---------
 1 files changed, 11 insertions(+), 9 deletions(-)

diffs (85 lines):

diff -r 024c870abe6c -r 171ae8c845c5 sys/dev/usb/xhci.c
--- a/sys/dev/usb/xhci.c        Wed Mar 09 22:18:54 2022 +0000
+++ b/sys/dev/usb/xhci.c        Wed Mar 09 22:19:07 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: xhci.c,v 1.159 2022/03/09 22:17:41 riastradh Exp $     */
+/*     $NetBSD: xhci.c,v 1.160 2022/03/09 22:19:07 riastradh Exp $     */
 
 /*
  * Copyright (c) 2013 Jonathan A. Kollasch
@@ -34,7 +34,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: xhci.c,v 1.159 2022/03/09 22:17:41 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: xhci.c,v 1.160 2022/03/09 22:19:07 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_usb.h"
@@ -696,16 +696,16 @@
 
        XHCIHIST_FUNC(); XHCIHIST_CALLED();
 
-       mutex_enter(&sc->sc_lock);
-
        /*
         * Block issuance of new commands, and wait for all pending
         * commands to complete.
         */
+       mutex_enter(&sc->sc_lock);
        KASSERT(sc->sc_suspender == NULL);
        sc->sc_suspender = curlwp;
        while (sc->sc_command_addr != 0)
                cv_wait(&sc->sc_cmdbusy_cv, &sc->sc_lock);
+       mutex_exit(&sc->sc_lock);
 
        /*
         * xHCI Requirements Specification 1.2, May 2019, Sec. 4.23.2:
@@ -739,8 +739,10 @@
                                continue;
 
                        /* Stop endpoint.  */
+                       mutex_enter(&sc->sc_lock);
                        err = xhci_stop_endpoint_cmd(sc, xs, dci,
                            XHCI_TRB_3_SUSP_EP_BIT);
+                       mutex_exit(&sc->sc_lock);
                        if (err) {
                                device_printf(self, "failed to stop endpoint"
                                    " slot %zu dci %zu err %d\n",
@@ -886,8 +888,7 @@
        /* Success!  */
        ok = true;
 
-out:   mutex_exit(&sc->sc_lock);
-       return ok;
+out:   return ok;
 }
 
 bool
@@ -901,7 +902,6 @@
 
        XHCIHIST_FUNC(); XHCIHIST_CALLED();
 
-       mutex_enter(&sc->sc_lock);
        KASSERT(sc->sc_suspender);
 
        /*
@@ -1079,14 +1079,16 @@
        }
 
        /* Resume command issuance.  */
+       mutex_enter(&sc->sc_lock);
+       KASSERT(sc->sc_suspender);
        sc->sc_suspender = NULL;
        cv_broadcast(&sc->sc_cmdbusy_cv);
+       mutex_exit(&sc->sc_lock);
 
        /* Success!  */
        ok = true;
 
-out:   mutex_exit(&sc->sc_lock);
-       return ok;
+out:   return ok;
 }
 
 bool



Home | Main Index | Thread Index | Old Index