From: sashiko-bot@kernel.org
To: "Sasha Finkelstein" <k@chaosmail.tech>
Cc: conor+dt@kernel.org, robh@kernel.org, devicetree@vger.kernel.org
Subject: Re: [PATCH v2 4/7] spmi: apple: Implement remaining commands
Date: Tue, 28 Jul 2026 09:46:06 +0000 [thread overview]
Message-ID: <20260728094607.141E41F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260728-t603x-spmi-v2-4-f43e5f10e583@chaosmail.tech>
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
New issues:
- [High] Missing bounds validation allows unmasked data in SPMI_CMD_ZERO_WRITE to corrupt the command opcode.
- [Medium] Missing NACK check on read commands triggers a false 'controller stuck' error and incorrect error handling.
Pre-existing issues:
- [High] Missing locking around shared hardware FIFO registers causes race conditions and bus state corruption.
- [High] Unhandled residual data in the RX FIFO leads to permanent transaction desynchronization.
--
--- Patch [4]: [PATCH v2 4/7] spmi: apple: Implement remaining commands ---
commit aad16a3367909482f17e1e9e1da0123341931c90
Author: Alba Mendez <me@alba.sh>
spmi: apple: Implement remaining commands
This commit adds support for zero write and power management commands to the
Apple SPMI driver. It restructures the internal command functions to handle
raw inputs and acknowledgements.
> diff --git a/drivers/spmi/spmi-apple-controller.c b/drivers/spmi/spmi-apple-controller.c
> --- a/drivers/spmi/spmi-apple-controller.c
> +++ b/drivers/spmi/spmi-apple-controller.c
[ ... ]
> -static int spmi_read_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid,
> - u16 saddr, u8 *buf, size_t len)
> +static int spmi_raw_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid,
> + u16 param, const u8 *buf, size_t len, u8 *ibuf, size_t ilen)
> {
> struct apple_spmi *spmi = spmi_controller_get_drvdata(ctrl);
> - u32 spmi_cmd = apple_spmi_pack_cmd(opc, sid, saddr, len);
> + u32 spmi_cmd = apple_spmi_pack_cmd(opc, sid, param);
> u32 reply, rsp;
> size_t len_read = 0;
> - u8 i;
> + size_t i = 0, j;
> int ret;
>
> writel(spmi_cmd, spmi->regs + SPMI_CMD_REG);
[Severity: High]
This is a pre-existing issue, but does spmi_raw_cmd() need locking to serialize
access to the shared hardware FIFO registers? Since the SPMI framework does
not serialize transactions, if two threads perform concurrent commands, could
their reads and writes to SPMI_CMD_REG and SPMI_RSP_REG interleave, resulting
in hardware FIFO corruption or reading another transaction's response?
> +
> + while (i < len) {
> + j = 0;
> + spmi_cmd = 0;
> + while ((j < 4) & (i < len))
> + spmi_cmd |= buf[i++] << (j++ * 8);
> +
> + writel(spmi_cmd, spmi->regs + SPMI_CMD_REG);
> + }
> +
> ret = apple_spmi_wait_rx_not_empty(ctrl);
> if (ret)
> return ret;
> @@ -82,15 +91,15 @@ static int spmi_read_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid,
> reply = readl(spmi->regs + SPMI_RSP_REG);
>
> /* Read SPMI data reply */
> - while (len_read < len) {
> + while (len_read < ilen) {
> if (readl(spmi->regs + SPMI_STATUS_REG) & SPMI_RX_FIFO_EMPTY) {
> dev_err(&ctrl->dev, "FIFO lacks reply data, controller stuck?\n");
> return -EIO;
> }
[Severity: Medium]
Will this error path incorrectly trigger for unacknowledged read commands?
Because the NACK check below is restricted to commands with no input payload
(!ilen), if a read command is NACKed (e.g., when probing an empty bus address),
it bypasses the NACK check and enters this loop. Could this result in hitting
the empty FIFO check and raising a misleading "controller stuck?" error instead
of gracefully handling the normal NACK condition?
> rsp = readl(spmi->regs + SPMI_RSP_REG);
> i = 0;
> - while ((len_read < len) && (i < 4)) {
> - buf[len_read++] = ((0xff << (8 * i)) & rsp) >> (8 * i);
> + while ((len_read < ilen) && (i < 4)) {
> + ibuf[len_read++] = ((0xffU << (8 * i)) & rsp) >> (8 * i);
> i += 1;
> }
> }
> @@ -98,47 +107,67 @@ static int spmi_read_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid,
> if (!(readl(spmi->regs + SPMI_STATUS_REG) & SPMI_RX_FIFO_EMPTY))
> dev_warn(&ctrl->dev, "FIFO has extra data\n");
[Severity: High]
This is a pre-existing issue, but does the RX FIFO need to be drained when
residual data is detected? If the PMIC pushes more frames than expected or the
transaction returns early, leaving this extra data could cause subsequent
transactions to read the stale residual words, permanently desynchronizing the
bus.
> - if ((~reply >> SPMI_REPLY_FRAME_PARITY_OFFSET) & ((1 << len) - 1)) {
> + if (!ilen && !(reply & SPMI_REPLY_ACK)) {
> + dev_err(&ctrl->dev, "command not acknowledged\n");
> + return -EIO;
> + }
[ ... ]
> +static int spmi_write_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid,
> + u16 saddr, const u8 *buf, size_t len)
> +{
> + switch (opc) {
> + case SPMI_CMD_WRITE:
> + return spmi_raw_cmd_ack(ctrl, opc | saddr, sid, buf[0] << 8 | saddr, NULL, 0);
> + case SPMI_CMD_ZERO_WRITE:
> + return spmi_raw_cmd_ack(ctrl, opc | buf[0], sid, buf[0] << 8 | saddr, NULL, 0);
[Severity: High]
Does this code allow unmasked user data to corrupt the command opcode? If a
caller provides a data payload for SPMI_CMD_ZERO_WRITE where bits 4-7 are set,
bitwise ORing it directly with the opcode (opc | buf[0]) without masking
(e.g., buf[0] & 0x0F) could overwrite the upper opcode bits, potentially
executing unintended commands on the PMIC.
> + case SPMI_CMD_EXT_WRITE:
> + case SPMI_CMD_EXT_WRITEL:
> + return spmi_raw_cmd_ack(ctrl, opc | (len - 1), sid, saddr, buf, len);
> }
> - return 0;
> + return -EINVAL;
> +}
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260728-t603x-spmi-v2-0-f43e5f10e583@chaosmail.tech?part=4
next prev parent reply other threads:[~2026-07-28 9:46 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-28 9:28 [PATCH v2 0/7] spmi: apple: Additional commands and interrupt support Sasha Finkelstein
2026-07-28 9:28 ` [PATCH v2 1/7] dt-bindings: spmi: apple,spmi: Add t603x Sasha Finkelstein
2026-07-28 9:33 ` sashiko-bot
2026-07-28 9:28 ` [PATCH v2 2/7] spmi: apple: Validate FIFO state Sasha Finkelstein
2026-07-28 9:41 ` sashiko-bot
2026-07-28 9:28 ` [PATCH v2 3/7] spmi: apple: check transaction status Sasha Finkelstein
2026-07-28 9:41 ` sashiko-bot
2026-07-28 9:28 ` [PATCH v2 4/7] spmi: apple: Implement remaining commands Sasha Finkelstein
2026-07-28 9:46 ` sashiko-bot [this message]
2026-07-28 9:28 ` [PATCH v2 5/7] spmi: apple: lock around FIFOs Sasha Finkelstein
2026-07-28 9:37 ` sashiko-bot
2026-07-28 9:28 ` [PATCH v2 6/7] spmi: apple: use IRQ for RX FIFO if possible Sasha Finkelstein
2026-07-28 9:42 ` sashiko-bot
2026-07-28 9:28 ` [PATCH v2 7/7] spmi: apple: interrupt controller functionality Sasha Finkelstein
2026-07-28 9:40 ` 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=20260728094607.141E41F000E9@smtp.kernel.org \
--to=sashiko-bot@kernel.org \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=k@chaosmail.tech \
--cc=robh@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