Linux clock framework development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Anup Patel" <anup.patel@oss.qualcomm.com>
Cc: linux-clk@vger.kernel.org
Subject: Re: [PATCH v3 4/5] mailbox: document direct synchronous transmit
Date: Fri, 04 Dec 2026 12:34:01 +0000	[thread overview]
Message-ID: <sashiko-outbox-249805@kernel.org> (raw)
In-Reply-To: <20261204111504.3362889-5-anup.patel@oss.qualcomm.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Low] mailbox: doc: spinlock_t in my_send_data_sync() violates
  PREEMPT_RT contract
- [Low] mailbox: doc: unreachable c->busy check and per-channel lock in
  example
- [Low] mailbox: doc: inaccurate deadlock and RX lock note for
  guard(irqsave)()
--

commit 7ea9fb48824dc119ecfc567b53c0fe80f89a4389
Author: Jassi Brar <jassisinghbrar@gmail.com>

mailbox: document direct synchronous transmit

This commit documents the three mailbox transmit models, controller and
client contracts for direct synchronous transmit, interaction rules with
existing mailbox APIs, and example controller implementations.

> diff --git a/Documentation/driver-api/mailbox.rst b/Documentation/driver-api/mailbox.rst
> index d46c60bd5df64..bbb800d88428a 100644
> --- a/Documentation/driver-api/mailbox.rst
> +++ b/Documentation/driver-api/mailbox.rst

[ ... ]

> +Example: atomic-safe controller
> +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> +
> +.. code-block:: c
> +
> +    struct my_chan {
> +        void __iomem *tx_reg;
> +        spinlock_t lock;	/* protects this controller's registers */
> +        bool busy;
> +    };
> +
> +    static int my_send_data_sync(struct mbox_chan *chan, void *data)
> +    {
> +        struct my_chan *c = chan->con_priv;
> +        unsigned long flags;
> +        u32 status;
> +        int ret = 0;
> +
> +        /*
> +         * The framework does not serialize direct callers and does not
> +         * disable IRQs.  This controller shares one register block across
> +         * all CPUs, so it uses its own IRQ-safe lock.
> +         */
> +        spin_lock_irqsave(&c->lock, flags);

[Severity: Low]
Should this example use raw_spinlock_t instead of spinlock_t?

send_data_sync() is documented as safe from hardirq context, but on
CONFIG_PREEMPT_RT spin_lock_irqsave() acquires a sleeping rt_spin_lock()
without disabling hardware interrupts.  A driver modeled on this example
would sleep in hardirq context and fail to block same-CPU hardirq re-entry.

Using raw_spinlock_t with raw_spin_lock_irqsave() and
raw_spin_unlock_irqrestore() (or noting that spinlock_t is not hardirq-safe
on PREEMPT_RT) would match the documented contract.

> +        if (c->busy) {
> +            ret = -EBUSY;
> +            goto out;
> +        }
> +
> +        c->busy = true;
> +        writel(*(u32 *)data, c->tx_reg);
> +        if (readl_poll_timeout_atomic(c->tx_reg + MY_STATUS, status,
> +                                      status & MY_DONE, 1, 1000))
> +            ret = -ETIMEDOUT;
> +        c->busy = false;
> +    out:
> +        spin_unlock_irqrestore(&c->lock, flags);
> +        return ret;
> +    }
> +
> +    static const struct mbox_chan_ops my_ops = {
> +        .send_data_sync = my_send_data_sync,
> +        /* .send_data may be NULL if every channel is direct-only. */
> +    };
> +
> +    static int my_probe(struct platform_device *pdev)
> +    {
> +        ...
> +        for (i = 0; i < MY_NUM_CHANS; i++)
> +            mbox->chans[i].sync_caps = MBOX_CHAN_CAP_SYNC_ATOMIC;
> +        ...
> +        return devm_mbox_controller_register(dev, &mbox->controller);
> +    }

[Severity: Low]
Can the c->busy check ever be reached here?

c->busy is only set and cleared while c->lock is unconditionally held, so
the -EBUSY path is dead code and concurrent callers spin on c->lock for up
to 1 ms instead of failing fast.  To show -EBUSY rejection, the example
would need a trylock such as raw_spin_trylock_irqsave() (or drop c->busy).

Also, the comments describe a single controller-wide register block shared
across CPUs, yet lock lives inside per-channel struct my_chan while
my_probe() registers MY_NUM_CHANS channels, leaving distinct channels with
separate locks.

> +Example: per-CPU shared-memory controller
> +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> +
> +A controller that hands a per-CPU buffer to firmware must guard the whole
> +copy/firmware-call/copy sequence, not just the copy:
> +
> +.. code-block:: c
> +
> +    static int my_send_data_sync(struct mbox_chan *chan, void *data)
> +    {
> +        struct my_msg *msg = data;
> +        int ret;
> +
> +        /*
> +         * The firmware uses per-CPU scratch memory.  A hardirq on this CPU
> +         * could otherwise re-enter and clobber it between the copy and the
> +         * firmware call.
> +         */
> +        guard(irqsave)();
> +        memcpy(this_cpu_ptr(&my_buf)->data, msg->tx, msg->len);
> +        ret = my_firmware_call(this_cpu_ptr(&my_buf)->phys, msg->len);
> +        if (!ret)
> +            memcpy(msg->rx, this_cpu_ptr(&my_buf)->data, msg->rx_len);
> +        return ret;
> +    }
> +
> +Every user of the per-CPU buffer must take the same guard.  Attribute
> +queries, notification fetches, MSI programming, and non-mailbox users that
> +share the buffer are all part of the same critical section.  Delivering RX
> +through ``mbox_chan_received_data()`` while holding that guard can deadlock
> +if the client sends from ``rx_callback``; release the guard first or use a
> +separate RX lock.

[Severity: Low]
Does calling mbox_chan_received_data() while holding guard(irqsave)()
actually deadlock?

guard(irqsave)() maps to local_irq_save(), which is re-entrant on the same
CPU.  If a client sends from rx_callback, my_send_data_sync() re-enters and
silently overwrites this_cpu_ptr(&my_buf) instead of deadlocking (a
deadlock only happens with a non-recursive lock like raw_spinlock_t).

A separate RX lock is also not a safe alternative to releasing the guard
when RX shares the per-CPU buffer, as it will not stop my_send_data_sync()
from clobbering my_buf during rx_callback.  The controller needs to copy
out the RX data and release the guard (or use a separate RX buffer) first.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204111504.3362889-1-anup.patel@oss.qualcomm.com?part=4

  reply	other threads:[~2026-12-04 12:34 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 11:14 [PATCH v3 0/5] Parallel message send using SBI MPXY Anup Patel
2026-12-04 11:15 ` [PATCH v3 1/5] RISC-V: Move common MPXY helper routines to arch/riscv Anup Patel
2026-12-04 11:47   ` sashiko-bot
2026-12-04 11:15 ` [PATCH v3 2/5] RISC-V: Prevent interrupt when accessing MPXY shared memory Anup Patel
2026-12-04 12:06   ` sashiko-bot
2026-12-04 11:15 ` [PATCH v3 3/5] mailbox: add direct synchronous transmit API Anup Patel
2026-12-04 12:26   ` sashiko-bot
2026-12-04 11:15 ` [PATCH v3 4/5] mailbox: document direct synchronous transmit Anup Patel
2026-12-04 12:34   ` sashiko-bot [this message]
2026-12-04 11:15 ` [PATCH v3 5/5] mailbox: convert RISC-V SBI MPXY to direct transmit Anup Patel
2026-12-04 13:30   ` sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-249805@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=anup.patel@oss.qualcomm.com \
    --cc=linux-clk@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox