Source-Changes-HG archive

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

[src/trunk]: src/sys/dev/usb Avoid race condition. Patch from bouyer@



details:   https://anonhg.NetBSD.org/src/rev/ef35538061cb
branches:  trunk
changeset: 466882:ef35538061cb
user:      mlelstv <mlelstv%NetBSD.org@localhost>
date:      Sat Jan 04 22:30:06 2020 +0000

description:
Avoid race condition. Patch from bouyer@

diffstat:

 sys/dev/usb/if_run.c    |  11 +++++++----
 sys/dev/usb/if_runvar.h |   4 ++--
 2 files changed, 9 insertions(+), 6 deletions(-)

diffs (70 lines):

diff -r 9102ac695ad2 -r ef35538061cb sys/dev/usb/if_run.c
--- a/sys/dev/usb/if_run.c      Sat Jan 04 22:28:26 2020 +0000
+++ b/sys/dev/usb/if_run.c      Sat Jan 04 22:30:06 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: if_run.c,v 1.34 2019/12/19 15:17:30 gson Exp $ */
+/*     $NetBSD: if_run.c,v 1.35 2020/01/04 22:30:06 mlelstv Exp $      */
 /*     $OpenBSD: if_run.c,v 1.90 2012/03/24 15:11:04 jsg Exp $ */
 
 /*-
@@ -23,7 +23,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_run.c,v 1.34 2019/12/19 15:17:30 gson Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_run.c,v 1.35 2020/01/04 22:30:06 mlelstv Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_usb.h"
@@ -41,6 +41,7 @@
 #include <sys/module.h>
 #include <sys/conf.h>
 #include <sys/device.h>
+#include <sys/atomic.h>
 
 #include <sys/bus.h>
 #include <machine/endian.h>
@@ -1784,10 +1785,11 @@
        while (ring->next != ring->cur) {
                cmd = &ring->cmd[ring->next];
                splx(s);
+               membar_consumer();
                /* callback */
                cmd->cb(sc, cmd->data);
                s = splusb();
-               ring->queued--;
+               atomic_dec_uint(&ring->queued);
                ring->next = (ring->next + 1) % RUN_HOST_CMD_RING_COUNT;
        }
        wakeup(ring);
@@ -1810,10 +1812,11 @@
        cmd->cb = cb;
        KASSERT(len <= sizeof(cmd->data));
        memcpy(cmd->data, arg, len);
+       membar_producer();
        ring->cur = (ring->cur + 1) % RUN_HOST_CMD_RING_COUNT;
 
        /* if there is no pending command already, schedule a task */
-       if (++ring->queued == 1)
+       if (atomic_inc_uint_nv(&ring->queued) == 1)
                usb_add_task(sc->sc_udev, &sc->sc_task, USB_TASKQ_DRIVER);
        splx(s);
 }
diff -r 9102ac695ad2 -r ef35538061cb sys/dev/usb/if_runvar.h
--- a/sys/dev/usb/if_runvar.h   Sat Jan 04 22:28:26 2020 +0000
+++ b/sys/dev/usb/if_runvar.h   Sat Jan 04 22:30:06 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: if_runvar.h,v 1.6 2019/10/08 07:30:58 mlelstv Exp $    */
+/*     $NetBSD: if_runvar.h,v 1.7 2020/01/04 22:30:06 mlelstv Exp $    */
 /*     $OpenBSD: if_runvar.h,v 1.8 2010/02/08 18:46:47 damien Exp $    */
 
 /*-
@@ -93,7 +93,7 @@
        struct run_tx_data      data[RUN_TX_RING_COUNT];
        struct usbd_pipe *      pipeh;
        int                     cur;
-       int                     queued;
+       volatile unsigned       queued;
        uint8_t                 pipe_no;
 };
 



Home | Main Index | Thread Index | Old Index