diff mbox series

[v2,01/10] firmware: raspberrypi: Introduce rpi_firmware_put()

Message ID 20201022155858.20867-2-nsaenzjulienne@suse.de (mailing list archive)
State New, archived
Headers show
Series [v2,01/10] firmware: raspberrypi: Introduce rpi_firmware_put() | expand

Commit Message

Nicolas Saenz Julienne Oct. 22, 2020, 3:58 p.m. UTC
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 make sure they all finished
unbinding before we do.

Suggested-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
Signed-off-by: Nicolas Saenz Julienne <nsaenzjulienne@suse.de>
---

@Uwe: I didn't found it necessary to call 'try_module_get()' as the rest
      of modules depend on the 'rpi_firmware_get/put()' symbols which already
      block users from unloading the module prematurely.

 drivers/firmware/raspberrypi.c             | 30 +++++++++++++++++++++-
 include/soc/bcm2835/raspberrypi-firmware.h |  3 +++
 2 files changed, 32 insertions(+), 1 deletion(-)

Comments

Andy Shevchenko Oct. 22, 2020, 6:46 p.m. UTC | #1
On Thu, Oct 22, 2020 at 9:06 PM Nicolas Saenz Julienne
<nsaenzjulienne@suse.de> wrote:
>
> 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 make sure they all finished
> unbinding before we do.

Wait, if it's a device, why do we need all these?
get_device() / put_device() along with module_get() / module_put()
should be sufficient, no?
Nicolas Saenz Julienne Oct. 26, 2020, 4:26 p.m. UTC | #2
On Thu, 2020-10-22 at 21:46 +0300, Andy Shevchenko wrote:
> On Thu, Oct 22, 2020 at 9:06 PM Nicolas Saenz Julienne
> <nsaenzjulienne@suse.de> wrote:
> > 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 make sure they all finished
> > unbinding before we do.
> 
> Wait, if it's a device, why do we need all these?
> get_device() / put_device() along with module_get() / module_put()
> should be sufficient, no?

Could you expand here a little, I do see how I could use get_device()'s
reference count. But it seems to me I'd be digging way too deep into kobj in
order to get the functionality I need.

If you meant to say that it should be automatically taken care by the platform
bus, just FYI we're using 'simple-mfd' in DT. Where firmware supplier is the
parent and all consumers are children.

Regards,
Nicolas
diff mbox series

Patch

diff --git a/drivers/firmware/raspberrypi.c b/drivers/firmware/raspberrypi.c
index 2371d08bdd17..e5ba609e3985 100644
--- a/drivers/firmware/raspberrypi.c
+++ b/drivers/firmware/raspberrypi.c
@@ -11,7 +11,9 @@ 
 #include <linux/module.h>
 #include <linux/of_platform.h>
 #include <linux/platform_device.h>
+#include <linux/refcount.h>
 #include <linux/slab.h>
+#include <linux/wait.h>
 #include <soc/bcm2835/raspberrypi-firmware.h>
 
 #define MBOX_MSG(chan, data28)		(((data28) & ~0xf) | ((chan) & 0xf))
@@ -27,6 +29,9 @@  struct rpi_firmware {
 	struct mbox_chan *chan; /* The property channel. */
 	struct completion c;
 	u32 enabled;
+
+	refcount_t consumers;
+	wait_queue_head_t wait;
 };
 
 static DEFINE_MUTEX(transaction_lock);
@@ -247,6 +252,8 @@  static int rpi_firmware_probe(struct platform_device *pdev)
 	}
 
 	init_completion(&fw->c);
+	refcount_set(&fw->consumers, 1);
+	init_waitqueue_head(&fw->wait);
 
 	platform_set_drvdata(pdev, fw);
 
@@ -275,6 +282,8 @@  static int rpi_firmware_remove(struct platform_device *pdev)
 	rpi_hwmon = NULL;
 	platform_device_unregister(rpi_clk);
 	rpi_clk = NULL;
+
+	wait_event(fw->wait, refcount_dec_if_one(&fw->consumers));
 	mbox_free_channel(fw->chan);
 
 	return 0;
@@ -289,14 +298,33 @@  static int rpi_firmware_remove(struct platform_device *pdev)
 struct rpi_firmware *rpi_firmware_get(struct device_node *firmware_node)
 {
 	struct platform_device *pdev = of_find_device_by_node(firmware_node);
+	struct rpi_firmware *fw;
 
 	if (!pdev)
 		return NULL;
 
-	return platform_get_drvdata(pdev);
+	fw = platform_get_drvdata(pdev);
+	if (!fw)
+		return NULL;
+
+	if (!refcount_inc_not_zero(&fw->consumers))
+		return NULL;
+
+	return fw;
 }
 EXPORT_SYMBOL_GPL(rpi_firmware_get);
 
+/**
+ * rpi_firmware_put - Put pointer to rpi_firmware structure.
+ * @rpi_firmware:    Pointer to struct rpi_firmware
+ */
+void rpi_firmware_put(struct rpi_firmware *fw)
+{
+	refcount_dec(&fw->consumers);
+	wake_up(&fw->wait);
+}
+EXPORT_SYMBOL_GPL(rpi_firmware_put);
+
 static const struct of_device_id rpi_firmware_of_match[] = {
 	{ .compatible = "raspberrypi,bcm2835-firmware", },
 	{},
diff --git a/include/soc/bcm2835/raspberrypi-firmware.h b/include/soc/bcm2835/raspberrypi-firmware.h
index cc9cdbc66403..7836ea51fbdf 100644
--- a/include/soc/bcm2835/raspberrypi-firmware.h
+++ b/include/soc/bcm2835/raspberrypi-firmware.h
@@ -141,6 +141,7 @@  int rpi_firmware_property(struct rpi_firmware *fw,
 int rpi_firmware_property_list(struct rpi_firmware *fw,
 			       void *data, size_t tag_size);
 struct rpi_firmware *rpi_firmware_get(struct device_node *firmware_node);
+void rpi_firmware_put(struct rpi_firmware *fw);
 #else
 static inline int rpi_firmware_property(struct rpi_firmware *fw, u32 tag,
 					void *data, size_t len)
@@ -158,6 +159,8 @@  static inline struct rpi_firmware *rpi_firmware_get(struct device_node *firmware
 {
 	return NULL;
 }
+
+void rpi_firmware_put(struct rpi_firmware *fw) { }
 #endif
 
 #endif /* __SOC_RASPBERRY_FIRMWARE_H__ */