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 5/5] efi: pstore: Convert into a platform driver for 'efivars'
Date: Tue, 24 Nov 2026 18:28:15 +0100 [thread overview]
Message-ID: <20261124172811.2580784-12-ardb+git@google.com> (raw)
In-Reply-To: <20261124172811.2580784-7-ardb+git@google.com>
From: Ard Biesheuvel <ardb@kernel.org>
efi-pstore already carries MODULE_ALIAS("platform:efivars") to match the
"efivars" platform device, but never registered an actual platform_driver
for it. As a result, it stays registered with pstore even after the
"efivars" device goes away, e.g., when tee_stmm_efi replaces the generic
efivars ops.
Convert efi-pstore into a proper platform_driver, so that its pstore
backend lifetime follows that of the "efivars" device. The EFI core
registers that device only while the generic, EFI runtime services based
efivars ops are in use and support SetVariable().
efi_pstore_info is a singleton, so only bind to the device named
"efivars" that carries struct efivars_pdev_data. This rejects devices
whose driver_override has been set to "efi-pstore" via sysfs: binding to
one of those would race with the "efivars" device under a different
device_lock and clobber efi_pstore_info.buf while it is registered.
When pstore_disable is set (via CONFIG_EFI_VARS_PSTORE_DEFAULT_DISABLE=y
or efi_pstore.pstore_disable=Y), refuse the probes that the driver core
performs automatically during platform_driver_register() and during
registration of the "efivars" device, while keeping the driver
registered. Each window is tracked by a flag: a module-local one for
driver registration, and efivars_pdev_data::registering, which the EFI
core sets while registering the device. With PROBE_FORCE_SYNCHRONOUS,
the automatic probes complete within these windows. This lets userspace
enable or disable efi-pstore at runtime without rebooting, via the
standard driver model sysfs interface:
echo efivars.0 > /sys/bus/platform/drivers/efi-pstore/bind
echo efivars.0 > /sys/bus/platform/drivers/efi-pstore/unbind
An explicit bind that races with either registration window is refused.
A manual binding does not carry over if the "efivars" device is
re-registered, e.g., when tee_stmm_efi is unbound and the generic ops are
restored; the new device is again subject to pstore_disable.
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
---
drivers/firmware/efi/efi-pstore.c | 87 +++++++++++++++++---
drivers/firmware/efi/efi.c | 25 ++++--
include/linux/efi.h | 11 +++
3 files changed, 107 insertions(+), 16 deletions(-)
diff --git a/drivers/firmware/efi/efi-pstore.c b/drivers/firmware/efi/efi-pstore.c
index 19a7e4fe5b52..93c5e18f33b5 100644
--- a/drivers/firmware/efi/efi-pstore.c
+++ b/drivers/firmware/efi/efi-pstore.c
@@ -2,8 +2,10 @@
#include <linux/efi.h>
#include <linux/module.h>
+#include <linux/platform_device.h>
#include <linux/pstore.h>
#include <linux/slab.h>
+#include <linux/string.h>
#include <linux/ucs2_string.h>
MODULE_IMPORT_NS("EFIVAR");
@@ -21,6 +23,7 @@ MODULE_PARM_DESC(record_size, "size of each pstore UEFI var (in bytes, min/defau
static bool efivars_pstore_disable =
IS_ENABLED(CONFIG_EFI_VARS_PSTORE_DEFAULT_DISABLE);
+static bool efi_pstore_driver_registering;
module_param_named(pstore_disable, efivars_pstore_disable, bool, 0444);
@@ -228,13 +231,42 @@ static struct pstore_info efi_pstore_info = {
.erase = efi_pstore_erase,
};
-static __init int efivars_pstore_init(void)
+static int efi_pstore_probe(struct platform_device *pdev)
{
+ const struct efivars_pdev_data *pdata = dev_get_platdata(&pdev->dev);
+ int ret;
+
+ /*
+ * efi_pstore_info is a singleton, so only bind to the "efivars" device
+ * registered by the EFI core, of which at most one exists at any given
+ * time, and whose probe and remove are serialized by its device_lock.
+ * This also rejects other devices whose driver_override was set to
+ * "efi-pstore" via sysfs, which platform_match() honours before
+ * consulting the id table.
+ */
+ if (!pdata || strcmp(pdev->name, EFIVARS_PDEV_NAME))
+ return -ENODEV;
+
if (!efivar_supports_writes())
- return 0;
+ return -ENODEV;
- if (efivars_pstore_disable)
- return 0;
+ /*
+ * If efi-pstore is disabled, only permit binding it explicitly via
+ * sysfs, by refusing the probes that the driver core performs
+ * automatically when the driver or the "efivars" device is
+ * registered. As the driver uses PROBE_FORCE_SYNCHRONOUS, such probes
+ * always complete before platform_driver_register() or
+ * platform_device_register_data() return, and therefore before the
+ * respective flag is cleared again. (This also means that an explicit
+ * bind that races with either registration is refused.)
+ *
+ * The flags may be cleared concurrently by another CPU, without
+ * holding this device's device_lock, hence the READ_ONCE()s.
+ */
+ if (efivars_pstore_disable &&
+ (READ_ONCE(efi_pstore_driver_registering) ||
+ READ_ONCE(pdata->registering)))
+ return -ENODEV;
/*
* Notice that 1024 is the minimum here to prevent issues with
@@ -251,29 +283,62 @@ static __init int efivars_pstore_init(void)
efi_pstore_info.bufsize = record_size;
- if (pstore_register(&efi_pstore_info)) {
+ ret = pstore_register(&efi_pstore_info);
+ if (ret) {
kfree(efi_pstore_info.buf);
efi_pstore_info.buf = NULL;
efi_pstore_info.bufsize = 0;
}
- return 0;
+ return ret;
}
-static __exit void efivars_pstore_exit(void)
+static void efi_pstore_remove(struct platform_device *pdev)
{
- if (!efi_pstore_info.bufsize)
- return;
-
pstore_unregister(&efi_pstore_info);
kfree(efi_pstore_info.buf);
efi_pstore_info.buf = NULL;
efi_pstore_info.bufsize = 0;
}
+static const struct platform_device_id efi_pstore_ids[] = {
+ { .name = EFIVARS_PDEV_NAME, },
+ { }
+};
+MODULE_DEVICE_TABLE(platform, efi_pstore_ids);
+
+static struct platform_driver efi_pstore_driver = {
+ .probe = efi_pstore_probe,
+ .remove = efi_pstore_remove,
+ .id_table = efi_pstore_ids,
+ .driver = {
+ .name = "efi-pstore",
+ .probe_type = PROBE_FORCE_SYNCHRONOUS,
+ },
+};
+
+static __init int efivars_pstore_init(void)
+{
+ int ret;
+
+ /*
+ * efi_pstore_driver becomes visible on the platform bus and in sysfs
+ * before platform_driver_register() returns, so a concurrent probe on
+ * another CPU may read the flag while it is being cleared.
+ */
+ WRITE_ONCE(efi_pstore_driver_registering, true);
+ ret = platform_driver_register(&efi_pstore_driver);
+ WRITE_ONCE(efi_pstore_driver_registering, false);
+ return ret;
+}
+
+static __exit void efivars_pstore_exit(void)
+{
+ platform_driver_unregister(&efi_pstore_driver);
+}
+
module_init(efivars_pstore_init);
module_exit(efivars_pstore_exit);
MODULE_DESCRIPTION("EFI variable backend for pstore");
MODULE_LICENSE("GPL");
-MODULE_ALIAS("platform:efivars");
diff --git a/drivers/firmware/efi/efi.c b/drivers/firmware/efi/efi.c
index a9508595740b..5b47e3073256 100644
--- a/drivers/firmware/efi/efi.c
+++ b/drivers/firmware/efi/efi.c
@@ -226,6 +226,8 @@ static bool generic_ops_supported(void)
static int generic_ops_register(void)
{
+ struct efivars_pdev_data pdata = { .registering = true };
+ struct efivars_pdev_data *pdev_data;
struct platform_device *pdev;
int ret;
@@ -251,14 +253,27 @@ static int generic_ops_register(void)
/*
* 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.
+ * platform_device_register_data() can take efivars_lock.
*/
- pdev = platform_device_register_simple("efivars", 0, NULL, 0);
- if (IS_ERR(pdev))
+ pdev = platform_device_register_data(NULL, EFIVARS_PDEV_NAME, 0,
+ &pdata, sizeof(pdata));
+ if (IS_ERR(pdev)) {
pr_warn("efi: failed to register efivars platform device: %pe\n",
pdev);
- else
- efivars_pdev = pdev;
+ return 0;
+ }
+
+ /*
+ * Any probe performed automatically by the driver core as part of
+ * registering the device has completed by now (for drivers that
+ * probe synchronously). Clear the flag so that consumers can tell
+ * such probes apart from later ones, e.g., explicit binds via sysfs.
+ * The device is already visible in sysfs, so a concurrent probe on
+ * another CPU may read the flag without serialization.
+ */
+ pdev_data = dev_get_platdata(&pdev->dev);
+ WRITE_ONCE(pdev_data->registering, false);
+ efivars_pdev = pdev;
return 0;
}
diff --git a/include/linux/efi.h b/include/linux/efi.h
index cc1df1577d5f..c58ff1c1e206 100644
--- a/include/linux/efi.h
+++ b/include/linux/efi.h
@@ -1069,6 +1069,17 @@ struct efivars {
const struct efivar_operations *ops;
};
+/*
+ * The "efivars" platform device is registered while the generic, EFI runtime
+ * services based efivars ops are registered and support SetVariable().
+ */
+#define EFIVARS_PDEV_NAME "efivars"
+
+struct efivars_pdev_data {
+ /* true only while platform_device_register_data() is in progress */
+ bool registering;
+};
+
#ifdef CONFIG_X86
u64 __attribute_const__ efivar_reserved_space(void);
#else
--
2.56.1.831.g720f3f5937-goog
prev parent reply other threads:[~2026-11-24 17:38 UTC|newest]
Thread overview: 6+ 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 ` [PATCH v2 4/5] efi: Tie 'efivars' pdev lifecycle to generic efivars Ard Biesheuvel
2026-11-24 17:28 ` Ard Biesheuvel [this message]
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-12-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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox