lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date:   Thu, 20 Dec 2018 18:19:44 +0100
From:   Thierry Reding <thierry.reding@...il.com>
To:     Jassi Brar <jassisinghbrar@...il.com>
Cc:     linux-kernel@...r.kernel.org, Andy Gospodarek <gospo@...adcom.com>,
        Bjorn Andersson <bjorn.andersson@...aro.org>,
        Caesar Wang <wxt@...k-chips.com>, CK Hu <ck.hu@...iatek.com>,
        Dong Aisheng <aisheng.dong@....com>,
        Eric Anholt <eric@...olt.net>,
        Fabien Dessenne <fabien.dessenne@...com>,
        Florian Fainelli <f.fainelli@...il.com>,
        Georgi Djakov <georgi.djakov@...aro.org>,
        Houlong Wei <houlong.wei@...iatek.com>,
        HS Liao <hs.liao@...iatek.com>,
        Kaihua Zhong <zhongkaihua@...wei.com>,
        Kevin Wangtao <kevin.wangtao@...ilicon.com>,
        Lee Jones <lee.jones@...aro.org>, Leo Yan <leo.yan@...aro.org>,
        Ley Foon Tan <lftan@...era.com>,
        Ludovic Barre <ludovic.barre@...com>,
        Neil Armstrong <narmstrong@...libre.com>,
        Nishanth Menon <nm@...com>,
        Oleksij Rempel <o.rempel@...gutronix.de>,
        Ray Jui <ray.jui@...adcom.com>,
        Rob Rice <rob.rice@...adcom.com>,
        Scott Branden <scott.branden@...adcom.com>,
        Sibi Sankar <sibis@...eaurora.org>,
        Stefan Wahren <stefan.wahren@...e.com>,
        Sudeep Holla <sudeep.holla@....com>,
        Suman Anna <s-anna@...com>, Tony Lindgren <tony@...mide.com>,
        Vikram Prakash <vikram.prakash@...adcom.com>,
        Vladimir Zapolskiy <vz@...ia.com>
Subject: [PATCH v3 01/19] mailbox: Add device-managed registration functions

From: Thierry Reding <treding@...dia.com>

Add device-managed equivalents of the mbox_controller_register() and
mbox_controller_unregister() functions that can be used to have the
devres infrastructure automatically unregister mailbox controllers on
driver probe failure or driver removal. This can help remove a lot of
boiler plate code from drivers.

Reviewed-by: Bjorn Andersson <bjorn.andersson@...aro.org>
Reviewed-by: Sudeep Holla <sudeep.holla@....com>
Signed-off-by: Thierry Reding <treding@...dia.com>
---
Changes in v2:
- fix indirection in device-managed pointer (now runtime-tested)

 drivers/mailbox/mailbox.c          | 70 ++++++++++++++++++++++++++++++
 include/linux/mailbox_controller.h |  5 +++
 2 files changed, 75 insertions(+)

diff --git a/drivers/mailbox/mailbox.c b/drivers/mailbox/mailbox.c
index 674b35f402f5..08ce9a1ab53a 100644
--- a/drivers/mailbox/mailbox.c
+++ b/drivers/mailbox/mailbox.c
@@ -515,3 +515,73 @@ void mbox_controller_unregister(struct mbox_controller *mbox)
 	mutex_unlock(&con_mutex);
 }
 EXPORT_SYMBOL_GPL(mbox_controller_unregister);
+
+static void __devm_mbox_controller_unregister(struct device *dev, void *res)
+{
+	struct mbox_controller **mbox = res;
+
+	mbox_controller_unregister(*mbox);
+}
+
+static int devm_mbox_controller_match(struct device *dev, void *res, void *data)
+{
+	struct mbox_controller **mbox = res;
+
+	if (WARN_ON(!mbox || !*mbox))
+		return 0;
+
+	return *mbox == data;
+}
+
+/**
+ * devm_mbox_controller_register() - managed mbox_controller_register()
+ * @dev: device owning the mailbox controller being registered
+ * @mbox: mailbox controller being registered
+ *
+ * This function adds a device-managed resource that will make sure that the
+ * mailbox controller, which is registered using mbox_controller_register()
+ * as part of this function, will be unregistered along with the rest of
+ * device-managed resources upon driver probe failure or driver removal.
+ *
+ * Returns 0 on success or a negative error code on failure.
+ */
+int devm_mbox_controller_register(struct device *dev,
+				  struct mbox_controller *mbox)
+{
+	struct mbox_controller **ptr;
+	int err;
+
+	ptr = devres_alloc(__devm_mbox_controller_unregister, sizeof(*ptr),
+			   GFP_KERNEL);
+	if (!ptr)
+		return -ENOMEM;
+
+	err = mbox_controller_register(mbox);
+	if (err < 0) {
+		devres_free(ptr);
+		return err;
+	}
+
+	devres_add(dev, ptr);
+	*ptr = mbox;
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(devm_mbox_controller_register);
+
+/**
+ * devm_mbox_controller_unregister() - managed mbox_controller_unregister()
+ * @dev: device owning the mailbox controller being unregistered
+ * @mbox: mailbox controller being unregistered
+ *
+ * This function unregisters the mailbox controller and removes the device-
+ * managed resource that was set up to automatically unregister the mailbox
+ * controller on driver probe failure or driver removal. It's typically not
+ * necessary to call this function.
+ */
+void devm_mbox_controller_unregister(struct device *dev, struct mbox_controller *mbox)
+{
+	WARN_ON(devres_release(dev, __devm_mbox_controller_unregister,
+			       devm_mbox_controller_match, mbox));
+}
+EXPORT_SYMBOL_GPL(devm_mbox_controller_unregister);
diff --git a/include/linux/mailbox_controller.h b/include/linux/mailbox_controller.h
index 74deadb42d76..9b0b21207345 100644
--- a/include/linux/mailbox_controller.h
+++ b/include/linux/mailbox_controller.h
@@ -131,4 +131,9 @@ void mbox_controller_unregister(struct mbox_controller *mbox); /* can sleep */
 void mbox_chan_received_data(struct mbox_chan *chan, void *data); /* atomic */
 void mbox_chan_txdone(struct mbox_chan *chan, int r); /* atomic */
 
+int devm_mbox_controller_register(struct device *dev,
+				  struct mbox_controller *mbox);
+void devm_mbox_controller_unregister(struct device *dev,
+				     struct mbox_controller *mbox);
+
 #endif /* __MAILBOX_CONTROLLER_H */
-- 
2.19.1

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ