git: 8edcb37dd075 - main - efirt(9): carefully destroy efi_lock

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Sat, 20 Jun 2026 18:16:54 UTC
The branch main has been updated by kib:

URL: https://cgit.FreeBSD.org/src/commit/?id=8edcb37dd0753dc7e50044d9ccf0e991392a3d84

commit 8edcb37dd0753dc7e50044d9ccf0e991392a3d84
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2026-06-20 12:20:38 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2026-06-20 18:16:37 +0000

    efirt(9): carefully destroy efi_lock
    
    efi_init() might return error after initializing the mutex, in which
    case MOD_UNLOAD() is not processed, and the mutex is not destroyed.
    Similarly, efi_uninit() skips any processing if efi_runtime was left as
    NULL, leaving mutex not destroyed.
    
    Initialize the mutex in MOD_LOAD case, and destroy in MOD_UNLOAD, also
    handling errors.
    
    Reviewed by:    imp
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week
    Differential revision:  https://reviews.freebsd.org/D57704
---
 sys/dev/efidev/efirt.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/sys/dev/efidev/efirt.c b/sys/dev/efidev/efirt.c
index 2e9a917de1f0..590d98030741 100644
--- a/sys/dev/efidev/efirt.c
+++ b/sys/dev/efidev/efirt.c
@@ -29,7 +29,6 @@
  * SUCH DAMAGE.
  */
 
-#include <sys/cdefs.h>
 #include "opt_acpi.h"
 
 #include <sys/param.h>
@@ -184,7 +183,6 @@ efi_init(void)
 	TUNABLE_INT_FETCH("efi.rt.disabled", &rt_disabled);
 	if (rt_disabled == 1)
 		return (0);
-	mtx_init(&efi_lock, "efi", NULL, MTX_DEF);
 
 	if (efi_systbl_phys == 0) {
 		if (bootverbose)
@@ -285,8 +283,6 @@ efi_uninit(void)
 	efi_systbl = NULL;
 	efi_cfgtbl = NULL;
 	efi_runtime = NULL;
-
-	mtx_destroy(&efi_lock);
 }
 
 static int
@@ -858,13 +854,19 @@ const struct efi_ops *active_efi_ops = &efi_ops;
 static int
 efirt_modevents(module_t m, int event, void *arg __unused)
 {
+	int error;
 
 	switch (event) {
 	case MOD_LOAD:
-		return (efi_init());
+		mtx_init(&efi_lock, "efi", NULL, MTX_DEF);
+		error = efi_init();
+		if (error != 0)
+			mtx_destroy(&efi_lock);
+		return (error);
 
 	case MOD_UNLOAD:
 		efi_uninit();
+		mtx_destroy(&efi_lock);
 		return (0);
 
 	case MOD_SHUTDOWN: