All of lore.kernel.org
 help / color / mirror / Atom feed
From: Ard Biesheuvel <ardb+git@google.com>
To: linux-efi@vger.kernel.org
Cc: "Ard Biesheuvel" <ardb@kernel.org>, "Kees Cook" <kees@kernel.org>,
	"Guilherme G. Piccoli" <gpiccoli@igalia.com>,
	"Uwe Kleine-König" <u.kleine-koenig@baylibre.com>
Subject: [PATCH v2 4/5] efi: Tie 'efivars' pdev lifecycle to generic efivars
Date: Tue, 24 Nov 2026 18:28:14 +0100	[thread overview]
Message-ID: <20261124172811.2580784-11-ardb+git@google.com> (raw)
In-Reply-To: <20261124172811.2580784-7-ardb+git@google.com>

From: Ard Biesheuvel <ardb@kernel.org>

Currently, efisubsys_init() registers the "efivars" platform device,
which efi-pstore matches via MODULE_ALIAS("platform:efivars"), whenever
EFI_BOOT is set and GetVariable/GetNextVariableName are marked supported
in efi.runtime_supported_mask. The device is never unregistered.

That makes the device's existence unrelated to what efi-pstore actually
needs:

 - it is registered even when generic_ops_supported() returns false
   (leaving no efivars implementation registered) or when SetVariable()
   is unsupported;
 - it remains registered after tee_stmm_efi_probe() replaces the generic
   ops with the OP-TEE StandaloneMM implementation, which does not
   support non-blocking SetVariable() calls from the pstore dump path.

Other efivars implementations (Coreboot GSMI, Qualcomm UEFI Secure App)
should not be exposed to efi-pstore at all. qcuefi_set_variable() takes
a mutex and allocates memory, and efivar_set_variable_locked() falls
back to it from pstore_dump(), which runs with a raw spinlock held and
interrupts disabled. On GSMI systems, efi-pstore would also compete with
ramoops for pstore's single backend slot.

Instead, register the "efivars" platform device from
generic_ops_register() once efivars_register() has succeeded with
SetVariable() support, and unregister it from generic_ops_unregister()
before efivars_unregister(). Consumers are then always detached while
the ops they use are still in place.

Serialize both paths with a new generic_ops_lock. They can also be
reached at runtime via efivars_generic_ops_[un]register() from
tee_stmm_efi. The device is registered and unregistered outside
efivars_lock, so a consumer can take efivars_lock from its probe or
remove path.

Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
---
 drivers/firmware/efi/efi.c | 52 +++++++++++++++++++-
 1 file changed, 50 insertions(+), 2 deletions(-)

diff --git a/drivers/firmware/efi/efi.c b/drivers/firmware/efi/efi.c
index 98f6365c1a8c..a9508595740b 100644
--- a/drivers/firmware/efi/efi.c
+++ b/drivers/firmware/efi/efi.c
@@ -31,6 +31,7 @@
 #include <linux/acpi.h>
 #include <linux/ucs2_string.h>
 #include <linux/memblock.h>
+#include <linux/mutex.h>
 #include <linux/security.h>
 #include <linux/notifier.h>
 
@@ -190,6 +191,21 @@ EXPORT_SYMBOL_GPL(efivar_ops_nh);
 static struct efivars generic_efivars;
 static struct efivar_operations generic_ops;
 
+/*
+ * Serializes generic_ops_register() and generic_ops_unregister(), which may
+ * also be called at runtime via efivars_generic_ops_[un]register(), and
+ * protects efivars_pdev.
+ */
+static DEFINE_MUTEX(generic_ops_lock);
+
+/*
+ * The "efivars" platform device that efi-pstore binds to. It only exists
+ * while the generic, EFI runtime services based efivars ops are registered
+ * and support SetVariable(), as other efivars implementations may not
+ * provide a SetVariable() that is safe to call from the pstore dump path.
+ */
+static struct platform_device *efivars_pdev;
+
 static bool generic_ops_supported(void)
 {
 	unsigned long name_size;
@@ -210,6 +226,11 @@ static bool generic_ops_supported(void)
 
 static int generic_ops_register(void)
 {
+	struct platform_device *pdev;
+	int ret;
+
+	guard(mutex)(&generic_ops_lock);
+
 	if (!generic_ops_supported())
 		return 0;
 
@@ -222,14 +243,42 @@ static int generic_ops_register(void)
 		generic_ops.set_variable = efi.set_variable;
 		generic_ops.set_variable_nonblocking = efi.set_variable_nonblocking;
 	}
-	return efivars_register(&generic_efivars, &generic_ops);
+
+	ret = efivars_register(&generic_efivars, &generic_ops);
+	if (ret || !generic_ops.set_variable)
+		return ret;
+
+	/*
+	 * Register the platform device only after efivars_register() has
+	 * returned, so that a consumer probing synchronously from
+	 * platform_device_register_simple() can take efivars_lock.
+	 */
+	pdev = platform_device_register_simple("efivars", 0, NULL, 0);
+	if (IS_ERR(pdev))
+		pr_warn("efi: failed to register efivars platform device: %pe\n",
+			pdev);
+	else
+		efivars_pdev = pdev;
+
+	return 0;
 }
 
 static void generic_ops_unregister(void)
 {
+	guard(mutex)(&generic_ops_lock);
+
 	if (!generic_ops.get_variable)
 		return;
 
+	/*
+	 * Unregister the platform device first, so that consumers are
+	 * detached while the efivars ops they rely on are still registered.
+	 */
+	if (efivars_pdev) {
+		platform_device_unregister(efivars_pdev);
+		efivars_pdev = NULL;
+	}
+
 	efivars_unregister(&generic_efivars);
 }
 
@@ -482,7 +531,6 @@ static int __init efisubsys_init(void)
 		error = efivar_ssdt_load();
 		if (error)
 			pr_err("efi: failed to load SSDT, error %d.\n", error);
-		platform_device_register_simple("efivars", 0, NULL, 0);
 	}
 
 	error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group);
-- 
2.56.1.831.g720f3f5937-goog


  parent reply	other threads:[~2026-11-24 17:38 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24 17:28 [PATCH v2 0/5] efi: Fix pstore runtime en/disable Ard Biesheuvel
2026-11-24 17:28 ` [PATCH v2 1/5] efi: pstore: Make pstore_disable parameter read-only again Ard Biesheuvel
2026-11-24 17:28 ` [PATCH v2 2/5] efi: efivars: Don't dereference NULL __efivars in efivar_lock() Ard Biesheuvel
2026-11-24 17:28 ` [PATCH v2 3/5] efi: Statically initialize efivar_ops_nh Ard Biesheuvel
2026-11-24 17:28 ` Ard Biesheuvel [this message]
2026-11-24 17:28 ` [PATCH v2 5/5] efi: pstore: Convert into a platform driver for 'efivars' Ard Biesheuvel
2026-11-27 22:08 ` [PATCH v2 0/5] efi: Fix pstore runtime en/disable Guilherme G. Piccoli
2026-11-27 23:00   ` Ard Biesheuvel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261124172811.2580784-11-ardb+git@google.com \
    --to=ardb+git@google.com \
    --cc=ardb@kernel.org \
    --cc=gpiccoli@igalia.com \
    --cc=kees@kernel.org \
    --cc=linux-efi@vger.kernel.org \
    --cc=u.kleine-koenig@baylibre.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.