From nobody Sun Jun 14 12:42:52 2026 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2A90F35F8B7 for ; Thu, 2 Apr 2026 17:07:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775149668; cv=none; b=Xm59x6jZOIIc3YPFLZ+e48vr8ui6b4XeferYDP7M8IN00Vnkj7sZKtIdZZifEb1ARn2JU+eVnlY0dZ645jmwULIxI53RwKcX3XiGmiaJyfJozgnwnp2mEv/TnSL+Gbd3VMjKcSHiOnu0fmioF8n12lln2fqG/CfG6Zrm9FrFgHg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775149668; c=relaxed/simple; bh=iboUIMrr4ZTwXaB6GUMUN1CNFh7FqDUbG1ISIooTYNY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=XMw7h5e0arF8HMnO2x+OqvGNDmct51k2elDZ/+Hsod2XAGL1CJUSMboifWmfjzMCRBFMBhysPTepa7I3m5d684++5EpdXR+KYTz4N+Kppd2yv3r7cx9/ZsVPioC8bW1z4kCCDXSr6BLcOfwBGa5uzcIG2O0/VeKy7ik5Ig8mytI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--joonwonkang.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=jPS1WL29; arc=none smtp.client-ip=209.85.215.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--joonwonkang.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="jPS1WL29" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-c6e7f45e2ddso1455639a12.1 for ; Thu, 02 Apr 2026 10:07:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1775149661; x=1775754461; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=qe/ZA3cS06JPwazJ5lT2X5nq3OydOwSen9aJipTvAo8=; b=jPS1WL29f9SHjnLowWyzlGHdFBOkTmMT5TYQQ2ay8qCl3RXGGXj7ZD2C5ihMdpdsEP rpDLedBTSkB12gZ567FyKnGS0pA/TomU8kTLOyAVWY3wxgy5uJPD8uNQiDVgrM+NOXpY puxEjssJaelwP+rtiOCvpIH5Il+rfuhAcoJ3goRIAg2DrsWOgJfD5V8j64zsod5TO7jG C3h5L23NQAPW+7FGUil0h3s0+pKLADFv6SsnPE9Gmg+00pLKmr2CJLvYCxSChzLRxhDd X+t1GnRbiea4kBpdAWtGC8wuBS64uhEm3kGABVViFYQmt1GdJZdMv+G4Rmy0kOWKSkQe /dVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775149661; x=1775754461; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=qe/ZA3cS06JPwazJ5lT2X5nq3OydOwSen9aJipTvAo8=; b=Mtopcb9hOjUzbaMgUBvGkgv7LLoWQ3h1xzHaoKEXdc3EseeRQnvKeDjuDB8ZJlqwSf dJpvq36hjOqQ2AzmL/Y3SSniRwSR+0xNWzZXLYwkmpqjH4Z76i+jblf+CjaUn0n2RuXk wlQNF9IPrxeWUIe2cSf+3qiyNzTTfV5Jh8FUDOc64gn+F0ZQKmPN6VfyyBP5yCnWj+An Fkcndg95oBU+81RB1gRfURzysC2CC7ZDwiA89aTYm1aQyAXhBTiYuOOfm+b1Nnmrsohv iR5l1l4DZ0vgITB6GBT0Ar7e11Yqxi/yoRObrfklO5Rxw0UOkB7L5gV/gbj/iLp/Z4Fz g8Zw== X-Gm-Message-State: AOJu0YxI/+zgzEwqY+pFLtT99hCQ2LaUksYPrNrwB+W2TokIY5vgmcMt v/ot+lF8kgsuItaTmc4vNOSNOVCBE0cBLTgOlZ3vO3Z5AcwBDhtmXTWcrgEICSd/13JYStlErsq deh+fI8XFhbzzOxHE7JSjFbShRw== X-Received: from pgww5.prod.google.com ([2002:a05:6a02:2c85:b0:c73:9919:c4f8]) (user=joonwonkang job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:32a0:b0:398:98ab:71a8 with SMTP id adf61e73a8af0-39ef773e0a1mr9207386637.47.1775149661098; Thu, 02 Apr 2026 10:07:41 -0700 (PDT) Date: Thu, 2 Apr 2026 17:06:40 +0000 In-Reply-To: <20260402170641.2082547-1-joonwonkang@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260402170641.2082547-1-joonwonkang@google.com> X-Mailer: git-send-email 2.53.0.1213.gd9a14994de-goog Message-ID: <20260402170641.2082547-2-joonwonkang@google.com> Subject: [PATCH v3 1/2] mailbox: Use per-thread completion to fix wrong completion order From: Joonwon Kang To: jassisinghbrar@gmail.com, matthias.bgg@gmail.com, angelogioacchino.delregno@collabora.com, thierry.reding@gmail.com, jonathanh@nvidia.com Cc: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mediatek@lists.infradead.org, linux-tegra@vger.kernel.org, Joonwon Kang , stable@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Previously, a sender thread in mbox_send_message() could be woken up at a wrong time in blocking mode. It is because there was only a single completion for a channel whereas messages from multiple threads could be sent in any order; since the shared completion could be signalled in any order, it could wake up a wrong sender thread. This commit resolves the false wake-up issue with the following changes: - Completions are created just as many as the number of concurrent sender threads - A completion is created on a sender thread's stack - Each slot of the message queue, i.e. `msg_data`, contains a pointer to its target completion - tx_tick() signals the completion of the currently active slot of the message queue Cc: stable@vger.kernel.org Link: https://lore.kernel.org/all/1490809381-28869-1-git-send-email-jaswind= er.singh@linaro.org Signed-off-by: Joonwon Kang --- drivers/mailbox/mailbox.c | 86 +++++++++++++++++++----------- drivers/mailbox/mtk-vcp-mailbox.c | 2 +- drivers/mailbox/tegra-hsp.c | 2 +- include/linux/mailbox_controller.h | 20 ++++--- 4 files changed, 72 insertions(+), 38 deletions(-) diff --git a/drivers/mailbox/mailbox.c b/drivers/mailbox/mailbox.c index 138ffbcd4fde..d63386468982 100644 --- a/drivers/mailbox/mailbox.c +++ b/drivers/mailbox/mailbox.c @@ -21,7 +21,7 @@ static LIST_HEAD(mbox_cons); static DEFINE_MUTEX(con_mutex); =20 -static int add_to_rbuf(struct mbox_chan *chan, void *mssg) +static int add_to_rbuf(struct mbox_chan *chan, void *mssg, struct completi= on *tx_complete) { int idx; =20 @@ -32,7 +32,8 @@ static int add_to_rbuf(struct mbox_chan *chan, void *mssg) return -ENOBUFS; =20 idx =3D chan->msg_free; - chan->msg_data[idx] =3D mssg; + chan->msg_data[idx].data =3D mssg; + chan->msg_data[idx].tx_complete =3D tx_complete; chan->msg_count++; =20 if (idx =3D=3D MBOX_TX_QUEUE_LEN - 1) @@ -50,24 +51,33 @@ static void msg_submit(struct mbox_chan *chan) int err =3D -EBUSY; =20 scoped_guard(spinlock_irqsave, &chan->lock) { - if (!chan->msg_count || chan->active_req !=3D MBOX_NO_MSG) + if (chan->active_req >=3D 0) break; =20 - count =3D chan->msg_count; - idx =3D chan->msg_free; - if (idx >=3D count) - idx -=3D count; - else - idx +=3D MBOX_TX_QUEUE_LEN - count; + while (chan->msg_count > 0) { + count =3D chan->msg_count; + idx =3D chan->msg_free; + if (idx >=3D count) + idx -=3D count; + else + idx +=3D MBOX_TX_QUEUE_LEN - count; =20 - data =3D chan->msg_data[idx]; + data =3D chan->msg_data[idx].data; + if (data !=3D MBOX_NO_MSG) + break; + + chan->msg_count--; + } + + if (!chan->msg_count) + break; =20 if (chan->cl->tx_prepare) chan->cl->tx_prepare(chan->cl, data); /* Try to submit a message to the MBOX controller */ err =3D chan->mbox->ops->send_data(chan, data); if (!err) { - chan->active_req =3D data; + chan->active_req =3D idx; chan->msg_count--; } } @@ -79,27 +89,35 @@ static void msg_submit(struct mbox_chan *chan) } } =20 -static void tx_tick(struct mbox_chan *chan, int r) +static void tx_tick(struct mbox_chan *chan, int r, int idx) { - void *mssg; + struct mbox_message mssg =3D {MBOX_NO_MSG, NULL}; =20 scoped_guard(spinlock_irqsave, &chan->lock) { - mssg =3D chan->active_req; - chan->active_req =3D MBOX_NO_MSG; + if (idx >=3D 0 && idx !=3D chan->active_req) { + chan->msg_data[idx].data =3D MBOX_NO_MSG; + chan->msg_data[idx].tx_complete =3D NULL; + return; + } + + if (chan->active_req >=3D 0) { + mssg =3D chan->msg_data[chan->active_req]; + chan->active_req =3D -1; + } } =20 /* Submit next message */ msg_submit(chan); =20 - if (mssg =3D=3D MBOX_NO_MSG) + if (mssg.data =3D=3D MBOX_NO_MSG) return; =20 /* Notify the client */ if (chan->cl->tx_done) - chan->cl->tx_done(chan->cl, mssg, r); + chan->cl->tx_done(chan->cl, mssg.data, r); =20 if (r !=3D -ETIME && chan->cl->tx_block) - complete(&chan->tx_complete); + complete(mssg.tx_complete); } =20 static enum hrtimer_restart txdone_hrtimer(struct hrtimer *hrtimer) @@ -112,10 +130,10 @@ static enum hrtimer_restart txdone_hrtimer(struct hrt= imer *hrtimer) for (i =3D 0; i < mbox->num_chans; i++) { struct mbox_chan *chan =3D &mbox->chans[i]; =20 - if (chan->active_req !=3D MBOX_NO_MSG && chan->cl) { + if (chan->active_req >=3D 0 && chan->cl) { txdone =3D chan->mbox->ops->last_tx_done(chan); if (txdone) - tx_tick(chan, 0); + tx_tick(chan, 0, -1); else resched =3D true; } @@ -168,7 +186,7 @@ void mbox_chan_txdone(struct mbox_chan *chan, int r) return; } =20 - tx_tick(chan, r); + tx_tick(chan, r, -1); } EXPORT_SYMBOL_GPL(mbox_chan_txdone); =20 @@ -188,7 +206,7 @@ void mbox_client_txdone(struct mbox_chan *chan, int r) return; } =20 - tx_tick(chan, r); + tx_tick(chan, r, -1); } EXPORT_SYMBOL_GPL(mbox_client_txdone); =20 @@ -266,11 +284,19 @@ EXPORT_SYMBOL_GPL(mbox_chan_tx_slots_available); int mbox_send_message(struct mbox_chan *chan, void *mssg) { int t; + int idx; + struct completion tx_complete; =20 if (!chan || !chan->cl || mssg =3D=3D MBOX_NO_MSG) return -EINVAL; =20 - t =3D add_to_rbuf(chan, mssg); + if (chan->cl->tx_block) { + init_completion(&tx_complete); + t =3D add_to_rbuf(chan, mssg, &tx_complete); + } else { + t =3D add_to_rbuf(chan, mssg, NULL); + } + if (t < 0) { dev_err(chan->mbox->dev, "Try increasing MBOX_TX_QUEUE_LEN\n"); return t; @@ -287,10 +313,11 @@ int mbox_send_message(struct mbox_chan *chan, void *m= ssg) else wait =3D msecs_to_jiffies(chan->cl->tx_tout); =20 - ret =3D wait_for_completion_timeout(&chan->tx_complete, wait); + ret =3D wait_for_completion_timeout(&tx_complete, wait); if (ret =3D=3D 0) { + idx =3D t; t =3D -ETIME; - tx_tick(chan, t); + tx_tick(chan, t, idx); } } =20 @@ -321,7 +348,7 @@ int mbox_flush(struct mbox_chan *chan, unsigned long ti= meout) =20 ret =3D chan->mbox->ops->flush(chan, timeout); if (ret < 0) - tx_tick(chan, ret); + tx_tick(chan, ret, -1); =20 return ret; } @@ -340,9 +367,8 @@ static int __mbox_bind_client(struct mbox_chan *chan, s= truct mbox_client *cl) scoped_guard(spinlock_irqsave, &chan->lock) { chan->msg_free =3D 0; chan->msg_count =3D 0; - chan->active_req =3D MBOX_NO_MSG; + chan->active_req =3D -1; chan->cl =3D cl; - init_completion(&chan->tx_complete); =20 if (chan->txdone_method =3D=3D TXDONE_BY_POLL && cl->knows_txdone) chan->txdone_method =3D TXDONE_BY_ACK; @@ -498,7 +524,7 @@ void mbox_free_channel(struct mbox_chan *chan) /* The queued TX requests are simply aborted, no callbacks are made */ scoped_guard(spinlock_irqsave, &chan->lock) { chan->cl =3D NULL; - chan->active_req =3D MBOX_NO_MSG; + chan->active_req =3D -1; if (chan->txdone_method =3D=3D TXDONE_BY_ACK) chan->txdone_method =3D TXDONE_BY_POLL; } @@ -553,7 +579,7 @@ int mbox_controller_register(struct mbox_controller *mb= ox) =20 chan->cl =3D NULL; chan->mbox =3D mbox; - chan->active_req =3D MBOX_NO_MSG; + chan->active_req =3D -1; chan->txdone_method =3D txdone; spin_lock_init(&chan->lock); } diff --git a/drivers/mailbox/mtk-vcp-mailbox.c b/drivers/mailbox/mtk-vcp-ma= ilbox.c index 1b291b8ea15a..a7bab06ac686 100644 --- a/drivers/mailbox/mtk-vcp-mailbox.c +++ b/drivers/mailbox/mtk-vcp-mailbox.c @@ -84,7 +84,7 @@ static int mtk_vcp_mbox_send_data(struct mbox_chan *chan,= void *data) =20 static bool mtk_vcp_mbox_last_tx_done(struct mbox_chan *chan) { - struct mtk_ipi_info *ipi_info =3D chan->active_req; + struct mtk_ipi_info *ipi_info =3D chan->msg_data[chan->active_req].data; struct mtk_vcp_mbox *priv =3D chan->con_priv; =20 return !(readl(priv->base + priv->cfg->set_in) & BIT(ipi_info->index)); diff --git a/drivers/mailbox/tegra-hsp.c b/drivers/mailbox/tegra-hsp.c index 7b1e1b83ea29..efe0033cb5c5 100644 --- a/drivers/mailbox/tegra-hsp.c +++ b/drivers/mailbox/tegra-hsp.c @@ -495,7 +495,7 @@ static int tegra_hsp_mailbox_flush(struct mbox_chan *ch= an, mbox_chan_txdone(chan, 0); =20 /* Wait until channel is empty */ - if (chan->active_req !=3D MBOX_NO_MSG) + if (chan->active_req >=3D 0) continue; =20 return 0; diff --git a/include/linux/mailbox_controller.h b/include/linux/mailbox_con= troller.h index e3896b08f22e..912499ad08ed 100644 --- a/include/linux/mailbox_controller.h +++ b/include/linux/mailbox_controller.h @@ -113,16 +113,25 @@ struct mbox_controller { */ #define MBOX_TX_QUEUE_LEN 20 =20 +/** + * struct mbox_message - Internal representation of a mailbox message + * @data: Data packet + * @tx_complete: Pointer to the transmission completion + */ +struct mbox_message { + void *data; + struct completion *tx_complete; +}; + /** * struct mbox_chan - s/w representation of a communication chan * @mbox: Pointer to the parent/provider of this channel * @txdone_method: Way to detect TXDone chosen by the API * @cl: Pointer to the current owner of this channel - * @tx_complete: Transmission completion - * @active_req: Currently active request hook + * @active_req: Index of the currently active slot in the queue * @msg_count: No. of mssg currently queued * @msg_free: Index of next available mssg slot - * @msg_data: Hook for data packet + * @msg_data: Queue of data packets * @lock: Serialise access to the channel * @con_priv: Hook for controller driver to attach private data */ @@ -130,10 +139,9 @@ struct mbox_chan { struct mbox_controller *mbox; unsigned txdone_method; struct mbox_client *cl; - struct completion tx_complete; - void *active_req; + int active_req; unsigned msg_count, msg_free; - void *msg_data[MBOX_TX_QUEUE_LEN]; + struct mbox_message msg_data[MBOX_TX_QUEUE_LEN]; spinlock_t lock; /* Serialise access to the channel */ void *con_priv; }; --=20 2.53.0.1185.g05d4b7b318-goog From nobody Sun Jun 14 12:42:52 2026 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A3937362130 for ; Thu, 2 Apr 2026 17:07:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775149673; cv=none; b=nY7UUdCzMonvvLbizt9xx2bNmZYqRF9MBPrfTidIT8fkClWMlbALL/l1KfaayI2aHNumEg/aMREE/xtn7xthO0CpT6NhdqyXxQ1HcNCPgsBRsOGN87tELUnl4ocgCHRg43WOaeV8Zcu9fKfECFa/db+5Jouewgj+tPbwA/7amBY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775149673; c=relaxed/simple; bh=RX9yNp5j1OE/PaC+hIPLZ/izItPwEBkRp9rXwgexlsk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=BenrlNCr5OENs1OgOCQ2gq/dEIUv19hQwYfJ/rDD55jxOCDLGxzanigyLZ3ZPxkk19Rk9p+Zs2GkrQTVSCWZlPw9uhjqrZEfgUAjH6iRruYq366WxFpBMLjxRsMfy3iU+zal/leJzPXLxqSWCyLkeLdPcxV6gaNehsO+uWJCJMQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--joonwonkang.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=REuD9243; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--joonwonkang.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="REuD9243" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-2b2489af602so10691635ad.1 for ; Thu, 02 Apr 2026 10:07:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1775149667; x=1775754467; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=xtS1OkkhTmC+kJWduMT9+PxAc4KJJ2D34QZmfUPxalo=; b=REuD9243C1mcz2hU0ovnEFQt1Ye0+vC8eA9W2m7Tn8/z5W9zo6oQRPQdcAfmkaCkCJ Wmi7Tl5kRQr1Fkj1JDfgVNmJYoWjOdl+MwgveAxlq6saEvGao27pbx4sEApuZBbrK2pc 36RtfJ10W7EusQlLIFw0fxk62uYiNEw3qEUx3SbGkB81l/KxoguSikkEDgKG0WqBgkcq qUF4/D0gL0iqQSJB4HhE5d5ryZBe6yUCiKl3FBoHqv8WyWy//h50m8qNmTi6krgUmd6k B/NbAwdHMKGf0CA5aBP7sQCY3w16tNeDgbO1Q+MQ0Vi9O63Y1HO+sx5vqjrhh7GcZUcK IbOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775149667; x=1775754467; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=xtS1OkkhTmC+kJWduMT9+PxAc4KJJ2D34QZmfUPxalo=; b=JINEX6WGcxfb+p7QcWyNSwJN2If4N0s6ioNXK3vIlaqy317wnYKNyF3eEXicehuzTd g+rRf5kuNGibINbq8xvtA12/5w1ZbWD7ukftZhWn70fNDWMRYK3zIGEf8VTvHrMxK/m1 S72j0EsGZU4tIRZw8XGlKTNql7Ik87HC1IXjZpid0OBFnXBeM1YhMcny4V6yJd37wjpk aaFBvR7mbjO9Zasf7m2Lcrsw0ksloCsdlt78oqxg/bZa8/F98LXksD+wvkuz20X6rTix oukuzW0+f+0Jd40MkgDqI2urzkgG90wFS7kp7N3p6CZJz1R7lQHg2JFJvwyNUblkmNgG zQqA== X-Gm-Message-State: AOJu0YwEOWhHEO0UYgZhai1/kc0Y5df0jWta5FCkHu7KP4ZxIxU7XPnK yDeR7uAZAxcBbVUb1P01+374Djqv96VX2pdcFoaO+2UZwHCh1QFjEjPyW2QlEaa1rk+gQQH2WbR VqsDmodZmRqJdLREglIcw0ry0aQ== X-Received: from plse12.prod.google.com ([2002:a17:902:b78c:b0:2b0:537d:70cc]) (user=joonwonkang job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:e850:b0:2b2:4f43:b49a with SMTP id d9443c01a7336-2b277e52d1dmr29085885ad.22.1775149666599; Thu, 02 Apr 2026 10:07:46 -0700 (PDT) Date: Thu, 2 Apr 2026 17:06:41 +0000 In-Reply-To: <20260402170641.2082547-1-joonwonkang@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260402170641.2082547-1-joonwonkang@google.com> X-Mailer: git-send-email 2.53.0.1213.gd9a14994de-goog Message-ID: <20260402170641.2082547-3-joonwonkang@google.com> Subject: [PATCH v3 2/2] mailbox: Make mbox_send_message() return error code when tx fails From: Joonwon Kang To: jassisinghbrar@gmail.com, matthias.bgg@gmail.com, angelogioacchino.delregno@collabora.com, thierry.reding@gmail.com, jonathanh@nvidia.com Cc: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mediatek@lists.infradead.org, linux-tegra@vger.kernel.org, Joonwon Kang , stable@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When the mailbox controller failed transmitting message, the error code was only passed to the client's tx done handler and not to mbox_send_message(). For this reason, the function could return a false success. This commit resolves the issue by introducing the tx status and checking it before mbox_send_message() returns. Cc: stable@vger.kernel.org Signed-off-by: Joonwon Kang --- drivers/mailbox/mailbox.c | 20 +++++++++++++++----- include/linux/mailbox_controller.h | 2 ++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/drivers/mailbox/mailbox.c b/drivers/mailbox/mailbox.c index d63386468982..ea9aec9dc947 100644 --- a/drivers/mailbox/mailbox.c +++ b/drivers/mailbox/mailbox.c @@ -21,7 +21,10 @@ static LIST_HEAD(mbox_cons); static DEFINE_MUTEX(con_mutex); =20 -static int add_to_rbuf(struct mbox_chan *chan, void *mssg, struct completi= on *tx_complete) +static int add_to_rbuf(struct mbox_chan *chan, + void *mssg, + struct completion *tx_complete, + int *tx_status) { int idx; =20 @@ -34,6 +37,7 @@ static int add_to_rbuf(struct mbox_chan *chan, void *mssg= , struct completion *tx idx =3D chan->msg_free; chan->msg_data[idx].data =3D mssg; chan->msg_data[idx].tx_complete =3D tx_complete; + chan->msg_data[idx].tx_status =3D tx_status; chan->msg_count++; =20 if (idx =3D=3D MBOX_TX_QUEUE_LEN - 1) @@ -91,12 +95,13 @@ static void msg_submit(struct mbox_chan *chan) =20 static void tx_tick(struct mbox_chan *chan, int r, int idx) { - struct mbox_message mssg =3D {MBOX_NO_MSG, NULL}; + struct mbox_message mssg =3D {MBOX_NO_MSG, NULL, NULL}; =20 scoped_guard(spinlock_irqsave, &chan->lock) { if (idx >=3D 0 && idx !=3D chan->active_req) { chan->msg_data[idx].data =3D MBOX_NO_MSG; chan->msg_data[idx].tx_complete =3D NULL; + chan->msg_data[idx].tx_status =3D NULL; return; } =20 @@ -116,8 +121,10 @@ static void tx_tick(struct mbox_chan *chan, int r, int= idx) if (chan->cl->tx_done) chan->cl->tx_done(chan->cl, mssg.data, r); =20 - if (r !=3D -ETIME && chan->cl->tx_block) + if (r !=3D -ETIME && chan->cl->tx_block) { + *mssg.tx_status =3D r; complete(mssg.tx_complete); + } } =20 static enum hrtimer_restart txdone_hrtimer(struct hrtimer *hrtimer) @@ -286,15 +293,16 @@ int mbox_send_message(struct mbox_chan *chan, void *m= ssg) int t; int idx; struct completion tx_complete; + int tx_status =3D 0; =20 if (!chan || !chan->cl || mssg =3D=3D MBOX_NO_MSG) return -EINVAL; =20 if (chan->cl->tx_block) { init_completion(&tx_complete); - t =3D add_to_rbuf(chan, mssg, &tx_complete); + t =3D add_to_rbuf(chan, mssg, &tx_complete, &tx_status); } else { - t =3D add_to_rbuf(chan, mssg, NULL); + t =3D add_to_rbuf(chan, mssg, NULL, NULL); } =20 if (t < 0) { @@ -318,6 +326,8 @@ int mbox_send_message(struct mbox_chan *chan, void *mss= g) idx =3D t; t =3D -ETIME; tx_tick(chan, t, idx); + } else if (tx_status < 0) { + t =3D tx_status; } } =20 diff --git a/include/linux/mailbox_controller.h b/include/linux/mailbox_con= troller.h index 912499ad08ed..890da97bcb50 100644 --- a/include/linux/mailbox_controller.h +++ b/include/linux/mailbox_controller.h @@ -117,10 +117,12 @@ struct mbox_controller { * struct mbox_message - Internal representation of a mailbox message * @data: Data packet * @tx_complete: Pointer to the transmission completion + * @tx_status: Pointer to the transmission status */ struct mbox_message { void *data; struct completion *tx_complete; + int *tx_status; }; =20 /** --=20 2.53.0.1185.g05d4b7b318-goog