Skip to content

Commit 60831f5

Browse files
Nicolas Saenz Juliennegregkh
authored andcommitted
firmware: raspberrypi: Keep count of all consumers
[ Upstream commit 1e7c573 ] When unbinding the firmware device we need to make sure it has no consumers left. Otherwise we'd leave them with a firmware handle pointing at freed memory. Keep a reference count of all consumers and introduce rpi_firmware_put() which will permit automatically decrease the reference count upon unbinding consumer drivers. Suggested-by: Uwe Kleine-König <[email protected]> Signed-off-by: Nicolas Saenz Julienne <[email protected]> Reviewed-by: Florian Fainelli <[email protected]> Reviewed-by: Stephen Boyd <[email protected]> Reviewed-by: Bartosz Golaszewski <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent 5c68b77 commit 60831f5

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

drivers/firmware/raspberrypi.c

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
#include <linux/dma-mapping.h>
10+
#include <linux/kref.h>
1011
#include <linux/mailbox_client.h>
1112
#include <linux/module.h>
1213
#include <linux/of_platform.h>
@@ -27,6 +28,8 @@ struct rpi_firmware {
2728
struct mbox_chan *chan; /* The property channel. */
2829
struct completion c;
2930
u32 enabled;
31+
32+
struct kref consumers;
3033
};
3134

3235
static DEFINE_MUTEX(transaction_lock);
@@ -225,12 +228,31 @@ static void rpi_register_clk_driver(struct device *dev)
225228
-1, NULL, 0);
226229
}
227230

231+
static void rpi_firmware_delete(struct kref *kref)
232+
{
233+
struct rpi_firmware *fw = container_of(kref, struct rpi_firmware,
234+
consumers);
235+
236+
mbox_free_channel(fw->chan);
237+
kfree(fw);
238+
}
239+
240+
void rpi_firmware_put(struct rpi_firmware *fw)
241+
{
242+
kref_put(&fw->consumers, rpi_firmware_delete);
243+
}
244+
EXPORT_SYMBOL_GPL(rpi_firmware_put);
245+
228246
static int rpi_firmware_probe(struct platform_device *pdev)
229247
{
230248
struct device *dev = &pdev->dev;
231249
struct rpi_firmware *fw;
232250

233-
fw = devm_kzalloc(dev, sizeof(*fw), GFP_KERNEL);
251+
/*
252+
* Memory will be freed by rpi_firmware_delete() once all users have
253+
* released their firmware handles. Don't use devm_kzalloc() here.
254+
*/
255+
fw = kzalloc(sizeof(*fw), GFP_KERNEL);
234256
if (!fw)
235257
return -ENOMEM;
236258

@@ -247,6 +269,7 @@ static int rpi_firmware_probe(struct platform_device *pdev)
247269
}
248270

249271
init_completion(&fw->c);
272+
kref_init(&fw->consumers);
250273

251274
platform_set_drvdata(pdev, fw);
252275

@@ -275,7 +298,8 @@ static int rpi_firmware_remove(struct platform_device *pdev)
275298
rpi_hwmon = NULL;
276299
platform_device_unregister(rpi_clk);
277300
rpi_clk = NULL;
278-
mbox_free_channel(fw->chan);
301+
302+
rpi_firmware_put(fw);
279303

280304
return 0;
281305
}
@@ -284,16 +308,26 @@ static int rpi_firmware_remove(struct platform_device *pdev)
284308
* rpi_firmware_get - Get pointer to rpi_firmware structure.
285309
* @firmware_node: Pointer to the firmware Device Tree node.
286310
*
311+
* The reference to rpi_firmware has to be released with rpi_firmware_put().
312+
*
287313
* Returns NULL is the firmware device is not ready.
288314
*/
289315
struct rpi_firmware *rpi_firmware_get(struct device_node *firmware_node)
290316
{
291317
struct platform_device *pdev = of_find_device_by_node(firmware_node);
318+
struct rpi_firmware *fw;
292319

293320
if (!pdev)
294321
return NULL;
295322

296-
return platform_get_drvdata(pdev);
323+
fw = platform_get_drvdata(pdev);
324+
if (!fw)
325+
return NULL;
326+
327+
if (!kref_get_unless_zero(&fw->consumers))
328+
return NULL;
329+
330+
return fw;
297331
}
298332
EXPORT_SYMBOL_GPL(rpi_firmware_get);
299333

include/soc/bcm2835/raspberrypi-firmware.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ int rpi_firmware_property(struct rpi_firmware *fw,
140140
u32 tag, void *data, size_t len);
141141
int rpi_firmware_property_list(struct rpi_firmware *fw,
142142
void *data, size_t tag_size);
143+
void rpi_firmware_put(struct rpi_firmware *fw);
143144
struct rpi_firmware *rpi_firmware_get(struct device_node *firmware_node);
144145
#else
145146
static inline int rpi_firmware_property(struct rpi_firmware *fw, u32 tag,
@@ -154,6 +155,7 @@ static inline int rpi_firmware_property_list(struct rpi_firmware *fw,
154155
return -ENOSYS;
155156
}
156157

158+
static inline void rpi_firmware_put(struct rpi_firmware *fw) { }
157159
static inline struct rpi_firmware *rpi_firmware_get(struct device_node *firmware_node)
158160
{
159161
return NULL;

0 commit comments

Comments
 (0)