Skip to contentSkip to conversation

mailbox: Provide devm variants for mbox_request_channel{,_byname}()

PATCH In review Uwe Kleine-König 2 participants 7 messages Aug 12 – Oct 11

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover mailbox: Provide devm variants for mbox_request_channel{,_byname}()

Hello,

the first patch introduces devm variants of mbox request functions. The second patch is just a minor cleanup preparing the third patch. The third patch demos how the simplification looks like for a driver using mbox channels.

This series is mostly about the first patch, I'll resend the serial patches separately once the first patch is in. (If the serial maintainers are enthusiastic about the 2nd patch: There is no dependency on the first and it can go in already now; the third patch has to wait however.)

Best regards
Uwe

Uwe Kleine-König (3):
mailbox: Provide devm variants for mbox_request_channel{,_byname}()
serial: tegra-tcu: Make use of dev_err_probe() in .probe()
serial: tegra-tcu: Make use of devm_mbox_request_channel_byname()

 drivers/mailbox/mailbox.c      | 44 ++++++++++++++++++++++++++++++++++
 drivers/tty/serial/tegra-tcu.c | 32 ++++++++++---------------
 include/linux/mailbox_client.h |  5 ++++
 3 files changed, 62 insertions(+), 19 deletions(-)

base-commit: dc59e4fea9d83f03bad6bddf3fa2e52491777482

Signature · 1 line
2.47.3
#1 lore raw

PATCH 1/3 mailbox: Provide devm variants for mbox_request_channel{,_byname}()

The new functions devm_mbox_request_channel() and devm_mbox_request_channel_byname() allow to simplify resource management for callers in their .probe() and .remove() functions.

---
 drivers/mailbox/mailbox.c      | 44 ++++++++++++++++++++++++++++++++++
 include/linux/mailbox_client.h |  5 ++++
 2 files changed, 49 insertions(+)
drivers/mailbox/mailbox.c +44 -0
index efacd24a085d..f51b4ca02041 100644
@@ -490,6 +490,31 @@ struct mbox_chan *mbox_request_channel(struct mbox_client *cl, int index)
 }
 EXPORT_SYMBOL_GPL(mbox_request_channel);
 
+static void devm_mbox_free_channel(void *data)
+{
+	struct mbox_chan *chan = data;
+
+	mbox_free_channel(chan);
+}
+
+struct mbox_chan *devm_mbox_request_channel(struct device *dev,
+					    struct mbox_client *cl, int index)
+{
+	struct mbox_chan *chan;
+	int ret;
+
+	chan = mbox_request_channel(cl, index);
+	if (IS_ERR(chan))
+		return chan;
+
+	ret = devm_add_action_or_reset(dev, devm_mbox_free_channel, chan);
+	if (ret)
+		return ERR_PTR(ret);
+
+	return chan;
+}
+EXPORT_SYMBOL_GPL(devm_mbox_request_channel);
+
 struct mbox_chan *mbox_request_channel_byname(struct mbox_client *cl,
 					      const char *name)
 {
@@ -504,6 +529,25 @@ struct mbox_chan *mbox_request_channel_byname(struct mbox_client *cl,
 }
 EXPORT_SYMBOL_GPL(mbox_request_channel_byname);
 
+struct mbox_chan *devm_mbox_request_channel_byname(struct device *dev,
+						   struct mbox_client *cl,
+						   const char *name)
+{
+	struct mbox_chan *chan;
+	int ret;
+
+	chan = mbox_request_channel_byname(cl, name);
+	if (IS_ERR(chan))
+		return chan;
+
+	ret = devm_add_action_or_reset(dev, devm_mbox_free_channel, chan);
+	if (ret)
+		return ERR_PTR(ret);
+
+	return chan;
+}
+EXPORT_SYMBOL_GPL(devm_mbox_request_channel_byname);
+
 /**
  * mbox_free_channel - The client relinquishes control of a mailbox
  *			channel by this call.
include/linux/mailbox_client.h +5 -0
index e5997120f45c..ce550a93bf61 100644
@@ -40,7 +40,12 @@ struct mbox_client {
 int mbox_bind_client(struct mbox_chan *chan, struct mbox_client *cl);
 struct mbox_chan *mbox_request_channel_byname(struct mbox_client *cl,
 					      const char *name);
+struct mbox_chan *devm_mbox_request_channel_byname(struct device *dev,
+						   struct mbox_client *cl,
+						   const char *name);
 struct mbox_chan *mbox_request_channel(struct mbox_client *cl, int index);
+struct mbox_chan *devm_mbox_request_channel(struct device *dev,
+					    struct mbox_client *cl, int index);
 int mbox_send_message(struct mbox_chan *chan, void *mssg);
 int mbox_flush(struct mbox_chan *chan, unsigned long timeout);
 void mbox_client_txdone(struct mbox_chan *chan, int r); /* atomic */
Signature · 1 line
2.47.3
Reviewed-by on v1 1/3
#2 lore raw
Quoted 5 lines · Uwe Kleine-König

The new functions devm_mbox_request_channel() and devm_mbox_request_channel_byname() allow to simplify resource management for callers in their .probe() and .remove() functions.

I planned to add similar API, and find there is already one in list :) Thanks Uwe.

LGTM:

BTW: will this be picked up for 7.4?

Thanks
Peng

#3 lore raw

PATCH 2/3 serial: tegra-tcu: Make use of dev_err_probe() in .probe()

In reply to Uwe Kleine-König

Usage of dev_err_probe() is (sometimes) a bit more compact than dev_err(), it properly handles ENOMEM and EPROBE_DEFER and emits the error code symbolically.

Also introduce a variable to hold &pdev->dev to reduce line length a bit.

---
 drivers/tty/serial/tegra-tcu.c | 22 ++++++++++------------
 1 file changed, 10 insertions(+), 12 deletions(-)
drivers/tty/serial/tegra-tcu.c +10 -12
index 7033dbfe8ba1..077023ce1842 100644
@@ -179,9 +179,10 @@ static int tegra_tcu_probe(struct platform_device *pdev)
 {
 	struct uart_port *port;
 	struct tegra_tcu *tcu;
+	struct device *dev = &pdev->dev;
 	int err;
 
-	tcu = devm_kzalloc(&pdev->dev, sizeof(*tcu), GFP_KERNEL);
+	tcu = devm_kzalloc(dev, sizeof(*tcu), GFP_KERNEL);
 	if (!tcu)
 		return -ENOMEM;
 
@@ -190,11 +191,9 @@ static int tegra_tcu_probe(struct platform_device *pdev)
 	tcu->rx_client.rx_callback = tegra_tcu_receive;
 
 	tcu->tx = mbox_request_channel_byname(&tcu->tx_client, "tx");
-	if (IS_ERR(tcu->tx)) {
-		err = PTR_ERR(tcu->tx);
-		dev_err(&pdev->dev, "failed to get tx mailbox: %d\n", err);
-		return err;
-	}
+	if (IS_ERR(tcu->tx))
+		return dev_err_probe(dev, PTR_ERR(tcu->tx),
+				     "failed to get tx mailbox\n");
 
 #if IS_ENABLED(CONFIG_SERIAL_TEGRA_TCU_CONSOLE)
 	/* setup the console */
@@ -218,15 +217,14 @@ static int tegra_tcu_probe(struct platform_device *pdev)
 
 	err = uart_register_driver(&tcu->driver);
 	if (err) {
-		dev_err(&pdev->dev, "failed to register UART driver: %d\n",
-			err);
+		dev_err_probe(dev, err, "failed to register UART driver\n");
 		goto free_tx;
 	}
 
 	/* setup the port */
 	port = &tcu->port;
 	spin_lock_init(&port->lock);
-	port->dev = &pdev->dev;
+	port->dev = dev;
 	port->type = PORT_TEGRA_TCU;
 	port->ops = &tegra_tcu_uart_ops;
 	port->fifosize = 1;
@@ -236,7 +234,7 @@ static int tegra_tcu_probe(struct platform_device *pdev)
 
 	err = uart_add_one_port(&tcu->driver, port);
 	if (err) {
-		dev_err(&pdev->dev, "failed to add UART port: %d\n", err);
+		dev_err_probe(dev, err, "failed to add UART port\n");
 		goto unregister_uart;
 	}
 
@@ -246,8 +244,8 @@ static int tegra_tcu_probe(struct platform_device *pdev)
 	 */
 	tcu->rx = mbox_request_channel_byname(&tcu->rx_client, "rx");
 	if (IS_ERR(tcu->rx)) {
-		err = PTR_ERR(tcu->rx);
-		dev_err(&pdev->dev, "failed to get rx mailbox: %d\n", err);
+		err = dev_err_probe(dev, PTR_ERR(tcu->rx),
+				    "failed to get rx mailbox\n");
 		goto remove_uart_port;
 	}
 
Signature · 1 line
2.47.3
Reviewed-by on v1 2/3
#4 lore raw

Re: [PATCH v1 2/3] serial: tegra-tcu: Make use of dev_err_probe() in .probe()

Quoted 8 lines · Uwe Kleine-König

Usage of dev_err_probe() is (sometimes) a bit more compact than dev_err(), it properly handles ENOMEM and EPROBE_DEFER and emits the error code symbolically.

Also introduce a variable to hold &pdev->dev to reduce line length a bit.

#5 lore raw

PATCH 3/3 serial: tegra-tcu: Make use of devm_mbox_request_channel_byname()

In reply to Uwe Kleine-König

Simplify tegra_tcu_probe() a bit by using the devm managed variant of mbox_request_channel_byname(). For the rx channel the function cannot be used without confusing the order of resource freeing.

---
 drivers/tty/serial/tegra-tcu.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
drivers/tty/serial/tegra-tcu.c +4 -8
index 077023ce1842..a3596ec24d34 100644
@@ -190,7 +190,7 @@ static int tegra_tcu_probe(struct platform_device *pdev)
 	tcu->rx_client.dev = &pdev->dev;
 	tcu->rx_client.rx_callback = tegra_tcu_receive;
 
-	tcu->tx = mbox_request_channel_byname(&tcu->tx_client, "tx");
+	tcu->tx = devm_mbox_request_channel_byname(dev, &tcu->tx_client, "tx");
 	if (IS_ERR(tcu->tx))
 		return dev_err_probe(dev, PTR_ERR(tcu->tx),
 				     "failed to get tx mailbox\n");
@@ -216,10 +216,9 @@ static int tegra_tcu_probe(struct platform_device *pdev)
 	tcu->driver.nr = 1;
 
 	err = uart_register_driver(&tcu->driver);
-	if (err) {
-		dev_err_probe(dev, err, "failed to register UART driver\n");
-		goto free_tx;
-	}
+	if (err)
+		return dev_err_probe(dev, err,
+				     "failed to register UART driver\n");
 
 	/* setup the port */
 	port = &tcu->port;
@@ -260,8 +259,6 @@ static int tegra_tcu_probe(struct platform_device *pdev)
 	uart_remove_one_port(&tcu->driver, &tcu->port);
 unregister_uart:
 	uart_unregister_driver(&tcu->driver);
-free_tx:
-	mbox_free_channel(tcu->tx);
 
 	return err;
 }
@@ -276,7 +273,6 @@ static void tegra_tcu_remove(struct platform_device *pdev)
 	mbox_free_channel(tcu->rx);
 	uart_remove_one_port(&tcu->driver, &tcu->port);
 	uart_unregister_driver(&tcu->driver);
-	mbox_free_channel(tcu->tx);
 }
 
 static const struct of_device_id tegra_tcu_match[] = {
Signature · 1 line
2.47.3
Reviewed-by on v1 3/3
#6 lore raw

Re: [PATCH v1 3/3] serial: tegra-tcu: Make use of devm_mbox_request_channel_byname()

Quoted 5 lines · Uwe Kleine-König

Simplify tegra_tcu_probe() a bit by using the devm managed variant of mbox_request_channel_byname(). For the rx channel the function cannot be used without confusing the order of resource freeing.