From: Sander Speetjens <sander.speetjens@gmail.com>
To: Florian Fainelli <florian.fainelli@broadcom.com>,
Broadcom internal kernel review list
<bcm-kernel-feedback-list@broadcom.com>,
Eric Anholt <eric@anholt.net>, Stefan Wahren <wahrenst@gmx.net>,
Stephen Boyd <sboyd@kernel.org>,
Nicolas Saenz Julienne <nsaenz@kernel.org>
Cc: linux-rpi-kernel@lists.infradead.org,
linux-arm-kernel@lists.infradead.org,
linux-kernel@vger.kernel.org, sander.speetjens@gmail.com
Subject: [PATCH v3 5/5] firmware: raspberrypi: Store child devices in struct rpi_firmware
Date: Tue, 10 Nov 2026 17:47:51 +0100 [thread overview]
Message-ID: <20261110-fix-raspberrypi-firmware-v3-5-e001feb542dd@gmail.com> (raw)
In-Reply-To: <20261110-fix-raspberrypi-firmware-v3-0-e001feb542dd@gmail.com>
When the raspberrypi-firmware driver is probed multiple times
(e.g. via multiple device tree nodes or dynamically loaded overlays),
rpi_firmware_probe() blindly overwrites the
global child platform device pointers rpi_hwmon and rpi_clk.
During initialization, rpi_firmware_probe() calls
rpi_register_hwmon_driver(), rpi_register_clk_driver() and
rpi_register_rtc_driver() to allocate and register new child platform
devices, storing them in global static variables. On a subsequent probe
the same platform device names are already in use, so
platform_device_register_data() fails and returns an ERR_PTR. The old
code never checked this return value and stored the error pointer
directly into the globals, overwriting the previous valid references.
When an instance is later unbound, rpi_firmware_remove() would then call
platform_device_unregister() on that error pointer (or on a pointer that
belonged to a different instance), while the previously allocated devices
were permanently leaked.
Move the hwmon, clock and rtc platform devices into struct rpi_firmware
so each instance owns its own children
and their lifetimes are correctly paired with the parent.
Also check the return value of platform_device_register_data()
so that an error pointer is never stored.
The child devices are optional; do not propagate registration
failures so the main firmware interface remains available.
Fixes: 70eea1bbb556 ("firmware: raspberrypi: Register hwmon driver")
Fixes: 91f2cf4a6b21 ("firmware: raspberrypi: register clk device")
Closes: https://sashiko.dev/#/bug/linux-667c4859-a44e-4461-aa3f-5b04056cc1d3
Signed-off-by: Sander Speetjens <sander.speetjens@gmail.com>
---
drivers/firmware/raspberrypi.c | 50 ++++++++++++++++++++++++++----------------
1 file changed, 31 insertions(+), 19 deletions(-)
diff --git a/drivers/firmware/raspberrypi.c b/drivers/firmware/raspberrypi.c
index ef3326d5cf30..7d8b89aeaf02 100644
--- a/drivers/firmware/raspberrypi.c
+++ b/drivers/firmware/raspberrypi.c
@@ -22,10 +22,6 @@
#define MBOX_DATA28(msg) ((msg) & ~0xf)
#define MBOX_CHAN_PROPERTY 8
-static struct platform_device *rpi_hwmon;
-static struct platform_device *rpi_clk;
-static struct platform_device *rpi_rtc;
-
struct rpi_firmware {
struct mbox_client cl;
struct mbox_chan *chan; /* The property channel. */
@@ -33,6 +29,10 @@ struct rpi_firmware {
u32 enabled;
struct kref consumers;
+
+ struct platform_device *hwmon_dev;
+ struct platform_device *clk_dev;
+ struct platform_device *rtc_dev;
};
static DEFINE_MUTEX(transaction_lock);
@@ -204,6 +204,7 @@ rpi_firmware_print_firmware_revision(struct rpi_firmware *fw)
static void
rpi_register_hwmon_driver(struct device *dev, struct rpi_firmware *fw)
{
+ struct platform_device *pdev;
u32 packet;
int ret = rpi_firmware_property(fw, RPI_FIRMWARE_GET_THROTTLED,
&packet, sizeof(packet));
@@ -211,13 +212,19 @@ rpi_register_hwmon_driver(struct device *dev, struct rpi_firmware *fw)
if (ret)
return;
- rpi_hwmon = platform_device_register_data(dev, "raspberrypi-hwmon",
- -1, NULL, 0);
+ pdev = platform_device_register_data(dev, "raspberrypi-hwmon",
+ -1, NULL, 0);
+
+ if (IS_ERR(pdev))
+ return;
+
+ fw->hwmon_dev = pdev;
}
-static void rpi_register_clk_driver(struct device *dev)
+static void rpi_register_clk_driver(struct device *dev, struct rpi_firmware *fw)
{
struct device_node *firmware;
+ struct platform_device *pdev;
/*
* Earlier DTs don't have a node for the firmware clocks but
@@ -231,11 +238,16 @@ static void rpi_register_clk_driver(struct device *dev)
return;
}
- rpi_clk = platform_device_register_data(dev, "raspberrypi-clk",
- -1, NULL, 0);
+ pdev = platform_device_register_data(dev, "raspberrypi-clk",
+ -1, NULL, 0);
+
+ if (IS_ERR(pdev))
+ return;
+
+ fw->clk_dev = pdev;
}
-static void rpi_register_rtc_driver(struct device *dev)
+static void rpi_register_rtc_driver(struct device *dev, struct rpi_firmware *fw)
{
struct platform_device *pdev;
@@ -252,7 +264,7 @@ static void rpi_register_rtc_driver(struct device *dev)
if (IS_ERR(pdev))
return;
- rpi_rtc = pdev;
+ fw->rtc_dev = pdev;
}
unsigned int rpi_firmware_clk_get_max_rate(struct rpi_firmware *fw, unsigned int id)
@@ -329,8 +341,8 @@ static int rpi_firmware_probe(struct platform_device *pdev)
rpi_firmware_print_firmware_revision(fw);
rpi_register_hwmon_driver(dev, fw);
- rpi_register_clk_driver(dev);
- rpi_register_rtc_driver(dev);
+ rpi_register_clk_driver(dev, fw);
+ rpi_register_rtc_driver(dev, fw);
return 0;
}
@@ -349,12 +361,12 @@ static void rpi_firmware_remove(struct platform_device *pdev)
{
struct rpi_firmware *fw = platform_get_drvdata(pdev);
- platform_device_unregister(rpi_hwmon);
- rpi_hwmon = NULL;
- platform_device_unregister(rpi_clk);
- rpi_clk = NULL;
- platform_device_unregister(rpi_rtc);
- rpi_rtc = NULL;
+ platform_device_unregister(fw->hwmon_dev);
+ fw->hwmon_dev = NULL;
+ platform_device_unregister(fw->clk_dev);
+ fw->clk_dev = NULL;
+ platform_device_unregister(fw->rtc_dev);
+ fw->rtc_dev = NULL;
rpi_firmware_put(fw);
}
--
2.56.0
prev parent reply other threads:[~2026-11-10 16:48 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-10 16:47 [PATCH v3 0/5] firmware: raspberrypi: Style cleanups and fix child device lifetime Sander Speetjens
2026-11-10 16:47 ` [PATCH v3 1/5] firmware: raspberrypi: Add blank line after declaration in response_callback Sander Speetjens
2026-11-10 16:47 ` [PATCH v3 2/5] firmware: raspberrypi: Fix indentation of return statement Sander Speetjens
2026-11-10 16:47 ` [PATCH v3 3/5] firmware: raspberrypi: Add blank line after declaration in probe error path Sander Speetjens
2026-11-10 16:47 ` [PATCH v3 4/5] firmware: raspberrypi: Document memory barriers in property list path Sander Speetjens
2026-11-10 16:47 ` Sander Speetjens [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=20261110-fix-raspberrypi-firmware-v3-5-e001feb542dd@gmail.com \
--to=sander.speetjens@gmail.com \
--cc=bcm-kernel-feedback-list@broadcom.com \
--cc=eric@anholt.net \
--cc=florian.fainelli@broadcom.com \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-rpi-kernel@lists.infradead.org \
--cc=nsaenz@kernel.org \
--cc=sboyd@kernel.org \
--cc=wahrenst@gmx.net \
/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
all inboxes | Powered by JetHome®