All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: Liem <liem16213@gmail.com>
Cc: imx@lists.linux.dev, Frank.Li@kernel.org
Subject: Re: [PATCH v4 2/2] i2c: imx: Cancel hrtimer before clearing slave pointer
Date: Mon, 29 Jun 2026 02:48:16 +0000	[thread overview]
Message-ID: <20260629024844.206F91F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260629023829.152651-3-liem16213@gmail.com>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Pre-existing issues:
- [High] The patch fails to completely resolve the NULL pointer dereference and timer re-arming race because it does not synchronize with the hardware interrupt handler (`i2c_imx_isr`).
- [High] Unsafe register access in shared interrupt handler while device is runtime suspended.
--

commit c2a34f9b292f8911be5d2996abfed1cdbdad49e7
Author: Liem <liem16213@gmail.com>

i2c: imx: Cancel hrtimer before clearing slave pointer

This commit attempts to fix a use-after-free and NULL pointer dereference
by canceling the slave hrtimer before setting the slave pointer to NULL in
i2c_imx_unreg_slave(). The goal is to prevent the timer callback from firing
and dereferencing a cleared pointer.

> diff --git a/drivers/i2c/busses/i2c-imx.c b/drivers/i2c/busses/i2c-imx.c
> index 136e179d18b2d..4a6348f03fc5d 100644
> --- a/drivers/i2c/busses/i2c-imx.c
> +++ b/drivers/i2c/busses/i2c-imx.c
> @@ -895,6 +895,7 @@ static int i2c_imx_unreg_slave(struct i2c_client *client)
>  
>  	i2c_imx_reset_regs(i2c_imx);
>  
> +	hrtimer_cancel(&i2c_imx->slave_timer);

[Severity: High]
This is a pre-existing issue, but does this patch fully close the race
condition it attempts to fix?

Since teardown happens without holding slave_lock or calling
synchronize_irq(), the hardware interrupt handler (i2c_imx_isr) can
execute concurrently.

If the ISR evaluates i2c_imx->slave before it is set to NULL here, it will
proceed to i2c_imx_slave_handle() and unconditionally re-arm the timer:

drivers/i2c/busses/i2c-imx.c:i2c_imx_slave_handle() {
    ...
    hrtimer_restart(&i2c_imx->slave_timer);
    ...
}

Because i2c_imx_isr() is a hardware interrupt, hrtimer_cancel() here will
return immediately without waiting for it. The concurrent ISR will re-arm
the timer, which can then fire after i2c_imx->slave is set to NULL, still
causing the NULL pointer dereference.

Should synchronize_irq() be called before clearing the pointer and
canceling the timer, or should the slave_lock be held?

>  	i2c_imx->slave = NULL;
>  
>  	/* Suspend */

[Severity: High]
This is a pre-existing issue, but can the shared interrupt handler crash the
system while the device is suspended?

The interrupt is requested as IRQF_SHARED in i2c_imx_probe():

    ret = request_threaded_irq(irq, i2c_imx_isr, NULL, IRQF_SHARED,
                               pdev->name, i2c_imx);

When the device is suspended, i2c_imx_runtime_suspend() disables the clock
via clk_disable(i2c_imx->clk) without masking the shared IRQ line.

If another device on the same IRQ line triggers an interrupt while the
controller is suspended, i2c_imx_isr() will execute and unconditionally
read hardware registers:

drivers/i2c/busses/i2c-imx.c:i2c_imx_isr() {
    ...
    status = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2SR);
    ...
}

On i.MX hardware, accessing registers of an unclocked peripheral results
in a synchronous bus fault. Does the ISR need a check to ensure the device
is not runtime-suspended before reading registers?

[ ... ]

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260629023829.152651-1-liem16213@gmail.com?part=2

  reply	other threads:[~2026-06-29  2:48 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-25  7:11 [PATCH] i2c: imx: Fix slave registration error path and missing NULL check Liem
2026-06-25  7:26 ` sashiko-bot
2026-06-25 11:17 ` Carlos Song (OSS)
2026-06-25 16:02 ` [PATCH v2] i2c: imx: Fix slave registration error path and missing timer cleanup Liem
2026-06-25 16:15   ` sashiko-bot
2026-06-25 16:16   ` Frank Li
2026-06-26  1:55     ` liem
2026-06-26  2:58     ` [PATCH v3 0/2] Fix slave mode corner issues Liem
2026-06-26  2:58       ` [PATCH v3 1/2] i2c: imx: Clear slave pointer on registration error Liem
2026-06-26  6:23         ` Carlos Song (OSS)
2026-06-26  8:30           ` liem
2026-06-29  2:38           ` [PATCH v4 0/2] i2c: imx: Fix slave mode corner issues Liem
2026-06-29  2:38             ` [PATCH v4 1/2] i2c: imx: Fix slave registration race and error handling Liem
2026-06-29  2:55               ` sashiko-bot
2026-06-29  9:30               ` Carlos Song
2026-06-29 14:13               ` Frank Li
2026-06-30  6:49                 ` liem
2026-06-29  2:38             ` [PATCH v4 2/2] i2c: imx: Cancel hrtimer before clearing slave pointer Liem
2026-06-29  2:48               ` sashiko-bot [this message]
2026-06-29 14:14               ` Frank Li
2026-06-26  2:58       ` [PATCH v3 " Liem
2026-06-26  6:26         ` Carlos Song (OSS)

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=20260629024844.206F91F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=Frank.Li@kernel.org \
    --cc=imx@lists.linux.dev \
    --cc=liem16213@gmail.com \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.