Source-Changes-HG archive

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

[src/trunk]: src/sys/dev/acpi acpiec(4): Fix cv_timedwait abuse in acpiec_rea...



details:   https://anonhg.NetBSD.org/src/rev/14fefbe8514d
branches:  trunk
changeset: 377587:14fefbe8514d
user:      riastradh <riastradh%NetBSD.org@localhost>
date:      Tue Jul 18 10:04:50 2023 +0000

description:
acpiec(4): Fix cv_timedwait abuse in acpiec_read/write.

diffstat:

 sys/dev/acpi/acpi_ec.c |  56 ++++++++++++++++++++++++++++---------------------
 1 files changed, 32 insertions(+), 24 deletions(-)

diffs (84 lines):

diff -r 8ff5557710b9 -r 14fefbe8514d sys/dev/acpi/acpi_ec.c
--- a/sys/dev/acpi/acpi_ec.c    Tue Jul 18 10:04:40 2023 +0000
+++ b/sys/dev/acpi/acpi_ec.c    Tue Jul 18 10:04:50 2023 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: acpi_ec.c,v 1.94 2023/07/18 10:04:40 riastradh Exp $   */
+/*     $NetBSD: acpi_ec.c,v 1.95 2023/07/18 10:04:50 riastradh Exp $   */
 
 /*-
  * Copyright (c) 2007 Joerg Sonnenberger <joerg%NetBSD.org@localhost>.
@@ -57,7 +57,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: acpi_ec.c,v 1.94 2023/07/18 10:04:40 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: acpi_ec.c,v 1.95 2023/07/18 10:04:50 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_acpi_ec.h"
@@ -695,17 +695,21 @@ acpiec_read(device_t dv, uint8_t addr, u
                            sc->sc_state);
                        return AE_ERROR;
                }
-       } else if (cv_timedwait(&sc->sc_cv, &sc->sc_mtx, EC_CMD_TIMEOUT * hz)) {
-               /*
-                * XXX while (sc->sc_state != EC_STATE_FREE)
-                *      cv_timedwait(...),
-                * plus deadline
-                */
-               mutex_exit(&sc->sc_mtx);
-               acpiec_unlock(dv);
-               aprint_error_dev(dv,
-                   "command takes over %d sec...\n", EC_CMD_TIMEOUT);
-               return AE_ERROR;
+       } else {
+               const unsigned deadline = getticks() + EC_CMD_TIMEOUT*hz;
+               unsigned delta;
+
+               while (sc->sc_state != EC_STATE_FREE &&
+                   (delta = deadline - getticks()) < INT_MAX)
+                       (void)cv_timedwait(&sc->sc_cv, &sc->sc_mtx, delta);
+               if (sc->sc_state != EC_STATE_FREE) {
+                       mutex_exit(&sc->sc_mtx);
+                       acpiec_unlock(dv);
+                       aprint_error_dev(dv,
+                           "command takes over %d sec...\n",
+                           EC_CMD_TIMEOUT);
+                       return AE_ERROR;
+               }
        }
 
 done:
@@ -764,17 +768,21 @@ acpiec_write(device_t dv, uint8_t addr, 
                            sc->sc_state);
                        return AE_ERROR;
                }
-       } else if (cv_timedwait(&sc->sc_cv, &sc->sc_mtx, EC_CMD_TIMEOUT * hz)) {
-               /*
-                * XXX while (sc->sc_state != EC_STATE_FREE)
-                *      cv_timedwait(...),
-                * plus deadline
-                */
-               mutex_exit(&sc->sc_mtx);
-               acpiec_unlock(dv);
-               aprint_error_dev(dv,
-                   "command takes over %d sec...\n", EC_CMD_TIMEOUT);
-               return AE_ERROR;
+       } else {
+               const unsigned deadline = getticks() + EC_CMD_TIMEOUT*hz;
+               unsigned delta;
+
+               while (sc->sc_state != EC_STATE_FREE &&
+                   (delta = deadline - getticks()) < INT_MAX)
+                       (void)cv_timedwait(&sc->sc_cv, &sc->sc_mtx, delta);
+               if (sc->sc_state != EC_STATE_FREE) {
+                       mutex_exit(&sc->sc_mtx);
+                       acpiec_unlock(dv);
+                       aprint_error_dev(dv,
+                           "command takes over %d sec...\n",
+                           EC_CMD_TIMEOUT);
+                       return AE_ERROR;
+               }
        }
 
 done:



Home | Main Index | Thread Index | Old Index