]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
mailbox: don't free the channel if the startup callback failed
authorWolfram Sang <wsa+renesas@sang-engineering.com>
Wed, 6 May 2026 07:09:47 +0000 (09:09 +0200)
committerJassi Brar <jassisinghbrar@gmail.com>
Mon, 18 May 2026 18:37:16 +0000 (13:37 -0500)
If the optional startup() callbacks fails, we need to clear some states.
Currently, this is done by freeing the channel. This does, however, more
than needed which creates problems. Namely, it is calling the shutdown()
callback. This is totally not intuitive. No user expects that shutdown()
is called when startup() fails, similar to remove() not being called
when probe() fails. Currently, quite some mailbox users register the IRQ
in startup() and free them in shutdown(). These drivers will get a WARN
about freeing an already free IRQ. Other subtle issues could arise from
this unexpected behaviour.

To solve this problem, introduce a helper which does the minimal cleanup
and use it in both, in free_channel() and after startup() failed.

Link: https://sashiko.dev/#/patchset/20260402112709.13002-1-wsa%2Brenesas%40sang-engineering.com
Fixes: 2b6d83e2b8b7 ("mailbox: Introduce framework for mailbox")
Signed-off-by: Wolfram Sang <wsa+renesas@sang-engineering.com>
Signed-off-by: Jassi Brar <jassisinghbrar@gmail.com>
drivers/mailbox/mailbox.c

index 066702e5a46f368fce34a6c52b95a1e9935f5361..ef88474501b9a477ca6d4c0527e1401fec28e59e 100644 (file)
@@ -335,6 +335,19 @@ int mbox_flush(struct mbox_chan *chan, unsigned long timeout)
 }
 EXPORT_SYMBOL_GPL(mbox_flush);
 
+static void mbox_clean_and_put_channel(struct mbox_chan *chan)
+{
+       /* The queued TX requests are simply aborted, no callbacks are made */
+       scoped_guard(spinlock_irqsave, &chan->lock) {
+               chan->cl = NULL;
+               chan->active_req = MBOX_NO_MSG;
+               if (chan->txdone_method == MBOX_TXDONE_BY_ACK)
+                       chan->txdone_method = MBOX_TXDONE_BY_POLL;
+       }
+
+       module_put(chan->mbox->dev->driver->owner);
+}
+
 static int __mbox_bind_client(struct mbox_chan *chan, struct mbox_client *cl)
 {
        struct device *dev = cl->dev;
@@ -358,10 +371,9 @@ static int __mbox_bind_client(struct mbox_chan *chan, struct mbox_client *cl)
 
        if (chan->mbox->ops->startup) {
                ret = chan->mbox->ops->startup(chan);
-
                if (ret) {
                        dev_err(dev, "Unable to startup the chan (%d)\n", ret);
-                       mbox_free_channel(chan);
+                       mbox_clean_and_put_channel(chan);
                        return ret;
                }
        }
@@ -503,15 +515,7 @@ void mbox_free_channel(struct mbox_chan *chan)
        if (chan->mbox->ops->shutdown)
                chan->mbox->ops->shutdown(chan);
 
-       /* The queued TX requests are simply aborted, no callbacks are made */
-       scoped_guard(spinlock_irqsave, &chan->lock) {
-               chan->cl = NULL;
-               chan->active_req = MBOX_NO_MSG;
-               if (chan->txdone_method == MBOX_TXDONE_BY_ACK)
-                       chan->txdone_method = MBOX_TXDONE_BY_POLL;
-       }
-
-       module_put(chan->mbox->dev->driver->owner);
+       mbox_clean_and_put_channel(chan);
 }
 EXPORT_SYMBOL_GPL(mbox_free_channel);