git: 04db6f654c4f - main - acpi_apple: Fix sleep with non-sleepable lock held

From: Vladimir Kondratyev <wulf_at_FreeBSD.org>
Date: Sat, 07 Nov 2026 17:21:45 UTC
The branch main has been updated by wulf:

URL: https://cgit.FreeBSD.org/src/commit/?id=04db6f654c4fb908b94265eb392db3e56cb7da8c

commit 04db6f654c4fb908b94265eb392db3e56cb7da8c
Author:     Vladimir Kondratyev <wulf@FreeBSD.org>
AuthorDate: 2026-11-07 17:20:50 +0000
Commit:     Vladimir Kondratyev <wulf@FreeBSD.org>
CommitDate: 2026-11-07 17:20:50 +0000

    acpi_apple: Fix sleep with non-sleepable lock held
    
    Apple platform attach and detach functions are serialized with ordinary
    mutex and contain code that executes malloc() that leads to INVARIANTS
    warnings. Fix it through replacement of mutex with sx lock.
    
    Fixes:  d7d237ce731f ("acpi_apple: Apple platform ACPI driver")
    Reviewed by:    seuros
    Differential Revision:  https://reviews.freebsd.org/D61295
---
 sys/dev/acpi_apple/acpi_apple.h          |  1 +
 sys/dev/acpi_apple/acpi_apple_platform.c | 14 +++++++-------
 2 files changed, 8 insertions(+), 7 deletions(-)

diff --git a/sys/dev/acpi_apple/acpi_apple.h b/sys/dev/acpi_apple/acpi_apple.h
index 4e02b43559de..790c6a544457 100644
--- a/sys/dev/acpi_apple/acpi_apple.h
+++ b/sys/dev/acpi_apple/acpi_apple.h
@@ -15,6 +15,7 @@
 #include <sys/module.h>
 #include <sys/mutex.h>
 #include <sys/sysctl.h>
+#include <sys/sx.h>
 
 #include <contrib/dev/acpica/include/acpi.h>
 
diff --git a/sys/dev/acpi_apple/acpi_apple_platform.c b/sys/dev/acpi_apple/acpi_apple_platform.c
index df690402f8f7..e993cfdbd9c7 100644
--- a/sys/dev/acpi_apple/acpi_apple_platform.c
+++ b/sys/dev/acpi_apple/acpi_apple_platform.c
@@ -22,9 +22,9 @@
  * the second instance's attach/detach doesn't reinstall or rip out the
  * first instance's hooks.
  */
-static struct mtx	acpi_apple_platform_mtx;
-MTX_SYSINIT(acpi_apple_platform_mtx_init, &acpi_apple_platform_mtx,
-    "acpi_apple_platform", MTX_DEF);
+static struct sx	acpi_apple_platform_sx;
+SX_SYSINIT(acpi_apple_platform_sx_init, &acpi_apple_platform_sx,
+    "acpi_apple_platform");
 static int		acpi_apple_platform_refs;
 static eventhandler_tag	acpi_apple_shutdown_tag;
 static eventhandler_tag	acpi_apple_lid_tag;
@@ -88,7 +88,7 @@ acpi_apple_lid_event(void *arg __unused, int lid_open)
 int
 acpi_apple_platform_attach(struct acpi_apple_softc *sc __unused)
 {
-	mtx_lock(&acpi_apple_platform_mtx);
+	sx_xlock(&acpi_apple_platform_sx);
 	if (acpi_apple_platform_refs++ == 0) {
 		acpi_apple_shutdown_tag = EVENTHANDLER_REGISTER(
 		    shutdown_final, acpi_apple_arm_wol, NULL,
@@ -96,7 +96,7 @@ acpi_apple_platform_attach(struct acpi_apple_softc *sc __unused)
 		acpi_apple_lid_tag = EVENTHANDLER_REGISTER(acpi_lid_event,
 		    acpi_apple_lid_event, NULL, EVENTHANDLER_PRI_ANY);
 	}
-	mtx_unlock(&acpi_apple_platform_mtx);
+	sx_unlock(&acpi_apple_platform_sx);
 
 	return (0);
 }
@@ -104,7 +104,7 @@ acpi_apple_platform_attach(struct acpi_apple_softc *sc __unused)
 void
 acpi_apple_platform_detach(struct acpi_apple_softc *sc __unused)
 {
-	mtx_lock(&acpi_apple_platform_mtx);
+	sx_xlock(&acpi_apple_platform_sx);
 	KASSERT(acpi_apple_platform_refs > 0,
 	    ("acpi_apple: platform refcount underflow"));
 	if (acpi_apple_platform_refs > 0 && --acpi_apple_platform_refs == 0) {
@@ -119,5 +119,5 @@ acpi_apple_platform_detach(struct acpi_apple_softc *sc __unused)
 			acpi_apple_shutdown_tag = NULL;
 		}
 	}
-	mtx_unlock(&acpi_apple_platform_mtx);
+	sx_unlock(&acpi_apple_platform_sx);
 }