Module Name:    src
Committed By:   riastradh
Date:           Fri Dec 31 14:20:24 UTC 2021

Modified Files:
        src/sys/dev/acpi: acpi_bat.c

Log Message:
acpibat(4): Fix attach and detach ordering.

On attach: Don't register acpibat_resume handler with pmf or
acpibat_notify_handler with acpi until it is safe to call
acpibat_update_info and acpibat_update_status.
=> This requires the sc->sc_sensor array to be allocated, so allocate
   that first.
=> This requires sc->sc_sme to be created, so defer them to
   config_interrupts after sysmon_envsys_register.
=> Make sure to register the notify handler before initially polling
   the info and status.

On detach: Deregister pmf handler and acpi notifier before we destroy
anything.


To generate a diff of this commit:
cvs rdiff -u -r1.117 -r1.118 src/sys/dev/acpi/acpi_bat.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/acpi/acpi_bat.c
diff -u src/sys/dev/acpi/acpi_bat.c:1.117 src/sys/dev/acpi/acpi_bat.c:1.118
--- src/sys/dev/acpi/acpi_bat.c:1.117	Fri Jan 29 15:20:13 2021
+++ src/sys/dev/acpi/acpi_bat.c	Fri Dec 31 14:20:24 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: acpi_bat.c,v 1.117 2021/01/29 15:20:13 thorpej Exp $	*/
+/*	$NetBSD: acpi_bat.c,v 1.118 2021/12/31 14:20:24 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2003 The NetBSD Foundation, Inc.
@@ -75,7 +75,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: acpi_bat.c,v 1.117 2021/01/29 15:20:13 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: acpi_bat.c,v 1.118 2021/12/31 14:20:24 riastradh Exp $");
 
 #include <sys/param.h>
 #include <sys/condvar.h>
@@ -235,20 +235,13 @@ acpibat_attach(device_t parent, device_t
 	sc->sc_wcapacity = 0;
 
 	sc->sc_sme = NULL;
-	sc->sc_sensor = NULL;
 
 	mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_NONE);
 	cv_init(&sc->sc_condvar, device_xname(self));
 
-	(void)pmf_device_register(self, NULL, acpibat_resume);
-	(void)acpi_register_notify(sc->sc_node, acpibat_notify_handler);
-
 	sc->sc_sensor = kmem_zalloc(ACPIBAT_COUNT *
 	    sizeof(*sc->sc_sensor), KM_SLEEP);
 
-	if (sc->sc_sensor == NULL)
-		return;
-
 	config_interrupts(self, acpibat_init_envsys);
 
 	/*
@@ -270,11 +263,9 @@ acpibat_detach(device_t self, int flags)
 {
 	struct acpibat_softc *sc = device_private(self);
 
+	pmf_device_deregister(self);
 	acpi_deregister_notify(sc->sc_node);
 
-	cv_destroy(&sc->sc_condvar);
-	mutex_destroy(&sc->sc_mutex);
-
 	if (sc->sc_sme != NULL)
 		sysmon_envsys_unregister(sc->sc_sme);
 
@@ -282,7 +273,8 @@ acpibat_detach(device_t self, int flags)
 		kmem_free(sc->sc_sensor, ACPIBAT_COUNT *
 		    sizeof(*sc->sc_sensor));
 
-	pmf_device_deregister(self);
+	cv_destroy(&sc->sc_condvar);
+	mutex_destroy(&sc->sc_mutex);
 
 	return 0;
 }
@@ -774,12 +766,15 @@ acpibat_init_envsys(device_t dv)
 	sc->sc_sme->sme_flags = SME_POLL_ONLY | SME_INIT_REFRESH;
 	sc->sc_sme->sme_get_limits = acpibat_get_limits;
 
+	(void)acpi_register_notify(sc->sc_node, acpibat_notify_handler);
 	acpibat_update_info(dv);
 	acpibat_update_status(dv);
 
 	if (sysmon_envsys_register(sc->sc_sme))
 		goto fail;
 
+	(void)pmf_device_register(dv, NULL, acpibat_resume);
+
 	return;
 
 fail:

Reply via email to