public inbox for linux-i2c@vger.kernel.org
 help / color / mirror / Atom feed
* [1/1] i2c: designware: Ensure runtime suspend is invoked during rapid slave unregistration and registration
@ 2025-04-12  2:33 ende.tan
  2025-04-15 13:57 ` Jarkko Nikula
  0 siblings, 1 reply; 8+ messages in thread
From: ende.tan @ 2025-04-12  2:33 UTC (permalink / raw)
  To: linux-i2c
  Cc: jarkko.nikula, andriy.shevchenko, mika.westerberg, jsd,
	andi.shyti, linux-kernel, leyfoon.tan, endeneer, Tan En De

From: Tan En De <ende.tan@starfivetech.com>

Replaced pm_runtime_put() with pm_runtime_put_sync_suspend() to ensure
the runtime suspend is invoked immediately when unregistering a slave.
This prevents a race condition where suspend was skipped when
unregistering and registering slave in quick succession.

Signed-off-by: Tan En De <ende.tan@starfivetech.com>
---
 drivers/i2c/busses/i2c-designware-slave.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/i2c/busses/i2c-designware-slave.c b/drivers/i2c/busses/i2c-designware-slave.c
index 5cd4a5f7a472..b936a240db0a 100644
--- a/drivers/i2c/busses/i2c-designware-slave.c
+++ b/drivers/i2c/busses/i2c-designware-slave.c
@@ -96,7 +96,7 @@ static int i2c_dw_unreg_slave(struct i2c_client *slave)
 	i2c_dw_disable(dev);
 	synchronize_irq(dev->irq);
 	dev->slave = NULL;
-	pm_runtime_put(dev->dev);
+	pm_runtime_put_sync_suspend(dev->dev);
 
 	return 0;
 }
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [1/1] i2c: designware: Ensure runtime suspend is invoked during rapid slave unregistration and registration
  2025-04-12  2:33 [1/1] i2c: designware: Ensure runtime suspend is invoked during rapid slave unregistration and registration ende.tan
@ 2025-04-15 13:57 ` Jarkko Nikula
  2025-04-20  3:31   ` EnDe Tan
  0 siblings, 1 reply; 8+ messages in thread
From: Jarkko Nikula @ 2025-04-15 13:57 UTC (permalink / raw)
  To: ende.tan, linux-i2c
  Cc: andriy.shevchenko, mika.westerberg, jsd, andi.shyti, linux-kernel,
	leyfoon.tan, endeneer

Hi

On 4/12/25 5:33 AM, ende.tan@starfivetech.com wrote:
> From: Tan En De <ende.tan@starfivetech.com>
> 
> Replaced pm_runtime_put() with pm_runtime_put_sync_suspend() to ensure
> the runtime suspend is invoked immediately when unregistering a slave.
> This prevents a race condition where suspend was skipped when
> unregistering and registering slave in quick succession.
> 
> Signed-off-by: Tan En De <ende.tan@starfivetech.com>
> ---
>   drivers/i2c/busses/i2c-designware-slave.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/i2c/busses/i2c-designware-slave.c b/drivers/i2c/busses/i2c-designware-slave.c
> index 5cd4a5f7a472..b936a240db0a 100644
> --- a/drivers/i2c/busses/i2c-designware-slave.c
> +++ b/drivers/i2c/busses/i2c-designware-slave.c
> @@ -96,7 +96,7 @@ static int i2c_dw_unreg_slave(struct i2c_client *slave)
>   	i2c_dw_disable(dev);
>   	synchronize_irq(dev->irq);
>   	dev->slave = NULL;
> -	pm_runtime_put(dev->dev);
> +	pm_runtime_put_sync_suspend(dev->dev);
>   
>   	return 0;
>   }

What kind of issue you are seeing?

I tried to test with no delay and 1 second delays between registering 
and unregistering. Power state was changing between D0 and D3 if there 
was delay between unregister and register but I could not see any issue.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* RE: [1/1] i2c: designware: Ensure runtime suspend is invoked during rapid slave unregistration and registration
  2025-04-15 13:57 ` Jarkko Nikula
@ 2025-04-20  3:31   ` EnDe Tan
  2025-05-02 11:03     ` Jarkko Nikula
  0 siblings, 1 reply; 8+ messages in thread
From: EnDe Tan @ 2025-04-20  3:31 UTC (permalink / raw)
  To: Jarkko Nikula, linux-i2c@vger.kernel.org
  Cc: andriy.shevchenko@linux.intel.com,
	mika.westerberg@linux.intel.com, jsd@semihalf.com,
	andi.shyti@kernel.org, linux-kernel@vger.kernel.org, Leyfoon Tan,
	endeneer@gmail.com

It appears that when performing a rapid sequence of `delete_device -> new_device -> delete_device -> new_device`, the `dw_i2c_plat_runtime_suspend` is not invoked for the second `delete_device`.

This seems to happen because when `i2c_dw_unreg_slave` is about to trigger suspend during the second `delete_device`, the second `new_device` operation cancels the suspend. As a result, `dw_i2c_plat_runtime_resume` is not called (since there was no suspend), which means `i_dev->init` (i.e., `i2c_dw_init_slave`) is skipped.

Because `i2c_dw_init_slave` is skipped, `i2c_dw_configure_fifo_slave` is not invoked, which leaves `DW_IC_INTR_MASK` unconfigured.
If we inspect the interrupt mask register using devmem, it will show as zero.

Here's an example shell script to reproduce the issue:
```
#!/bin/sh

SLAVE_LADDR=0x1010
SLAVE_BUS=13
NEW_DEVICE=/sys/bus/i2c/devices/i2c-$SLAVE_BUS/new_device
DELETE_DEVICE=/sys/bus/i2c/devices/i2c-$SLAVE_BUS/delete_device

# Create initial device
echo slave-24c02 $SLAVE_LADDR > $NEW_DEVICE
sleep 2

# Rapid sequence of delete_device -> new_device -> delete_device -> new_device
echo $SLAVE_LADDR > $DELETE_DEVICE
echo slave-24c02 $SLAVE_LADDR > $NEW_DEVICE
echo $SLAVE_LADDR > $DELETE_DEVICE
echo slave-24c02 $SLAVE_LADDR > $NEW_DEVICE

# If we use devmem to inspect IC_INTR_MASK, it will show as zero
```


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [1/1] i2c: designware: Ensure runtime suspend is invoked during rapid slave unregistration and registration
  2025-04-20  3:31   ` EnDe Tan
@ 2025-05-02 11:03     ` Jarkko Nikula
  2025-05-05 21:48       ` Andi Shyti
  0 siblings, 1 reply; 8+ messages in thread
From: Jarkko Nikula @ 2025-05-02 11:03 UTC (permalink / raw)
  To: EnDe Tan, linux-i2c@vger.kernel.org
  Cc: andriy.shevchenko@linux.intel.com,
	mika.westerberg@linux.intel.com, jsd@semihalf.com,
	andi.shyti@kernel.org, linux-kernel@vger.kernel.org, Leyfoon Tan,
	endeneer@gmail.com

Hi

Sorry the delay. Comment below.

On 4/20/25 6:31 AM, EnDe Tan wrote:
> It appears that when performing a rapid sequence of `delete_device -> new_device -> delete_device -> new_device`, the `dw_i2c_plat_runtime_suspend` is not invoked for the second `delete_device`.
> 
> This seems to happen because when `i2c_dw_unreg_slave` is about to trigger suspend during the second `delete_device`, the second `new_device` operation cancels the suspend. As a result, `dw_i2c_plat_runtime_resume` is not called (since there was no suspend), which means `i_dev->init` (i.e., `i2c_dw_init_slave`) is skipped.
> 
> Because `i2c_dw_init_slave` is skipped, `i2c_dw_configure_fifo_slave` is not invoked, which leaves `DW_IC_INTR_MASK` unconfigured.
> If we inspect the interrupt mask register using devmem, it will show as zero.
> 
> Here's an example shell script to reproduce the issue:
> ```
> #!/bin/sh
> 
> SLAVE_LADDR=0x1010
> SLAVE_BUS=13
> NEW_DEVICE=/sys/bus/i2c/devices/i2c-$SLAVE_BUS/new_device
> DELETE_DEVICE=/sys/bus/i2c/devices/i2c-$SLAVE_BUS/delete_device
> 
> # Create initial device
> echo slave-24c02 $SLAVE_LADDR > $NEW_DEVICE
> sleep 2
> 
> # Rapid sequence of delete_device -> new_device -> delete_device -> new_device
> echo $SLAVE_LADDR > $DELETE_DEVICE
> echo slave-24c02 $SLAVE_LADDR > $NEW_DEVICE
> echo $SLAVE_LADDR > $DELETE_DEVICE
> echo slave-24c02 $SLAVE_LADDR > $NEW_DEVICE
> 
> # If we use devmem to inspect IC_INTR_MASK, it will show as zero
> ```
> 
Good explanation and could you add it the commit log together with the 
example?

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [1/1] i2c: designware: Ensure runtime suspend is invoked during rapid slave unregistration and registration
  2025-05-02 11:03     ` Jarkko Nikula
@ 2025-05-05 21:48       ` Andi Shyti
  2025-05-08  8:30         ` EnDe Tan
  0 siblings, 1 reply; 8+ messages in thread
From: Andi Shyti @ 2025-05-05 21:48 UTC (permalink / raw)
  To: Jarkko Nikula
  Cc: EnDe Tan, linux-i2c@vger.kernel.org,
	andriy.shevchenko@linux.intel.com,
	mika.westerberg@linux.intel.com, jsd@semihalf.com,
	linux-kernel@vger.kernel.org, Leyfoon Tan, endeneer@gmail.com

Hi EnDe,

On Fri, May 02, 2025 at 02:03:25PM +0300, Jarkko Nikula wrote:
> On 4/20/25 6:31 AM, EnDe Tan wrote:
> > It appears that when performing a rapid sequence of `delete_device -> new_device -> delete_device -> new_device`, the `dw_i2c_plat_runtime_suspend` is not invoked for the second `delete_device`.
> > 
> > This seems to happen because when `i2c_dw_unreg_slave` is about to trigger suspend during the second `delete_device`, the second `new_device` operation cancels the suspend. As a result, `dw_i2c_plat_runtime_resume` is not called (since there was no suspend), which means `i_dev->init` (i.e., `i2c_dw_init_slave`) is skipped.
> > 
> > Because `i2c_dw_init_slave` is skipped, `i2c_dw_configure_fifo_slave` is not invoked, which leaves `DW_IC_INTR_MASK` unconfigured.
> > If we inspect the interrupt mask register using devmem, it will show as zero.
> > 
> > Here's an example shell script to reproduce the issue:
> > ```
> > #!/bin/sh
> > 
> > SLAVE_LADDR=0x1010
> > SLAVE_BUS=13
> > NEW_DEVICE=/sys/bus/i2c/devices/i2c-$SLAVE_BUS/new_device
> > DELETE_DEVICE=/sys/bus/i2c/devices/i2c-$SLAVE_BUS/delete_device
> > 
> > # Create initial device
> > echo slave-24c02 $SLAVE_LADDR > $NEW_DEVICE
> > sleep 2
> > 
> > # Rapid sequence of delete_device -> new_device -> delete_device -> new_device
> > echo $SLAVE_LADDR > $DELETE_DEVICE
> > echo slave-24c02 $SLAVE_LADDR > $NEW_DEVICE
> > echo $SLAVE_LADDR > $DELETE_DEVICE
> > echo slave-24c02 $SLAVE_LADDR > $NEW_DEVICE
> > 
> > # If we use devmem to inspect IC_INTR_MASK, it will show as zero
> > ```

Please, don't remove the interesting parts of the original email
from your reply, otherwise it wouldn't be easy to follow the
discussion. Please refer to the email netiquette[*].

> Good explanation and could you add it the commit log together with the
> example?

If you want you can paste the new commit log as reply to this
e-mail.

Thanks,
Andi

[*] https://www.ietf.org/rfc/rfc1855.txt

^ permalink raw reply	[flat|nested] 8+ messages in thread

* RE: [1/1] i2c: designware: Ensure runtime suspend is invoked during rapid slave unregistration and registration
  2025-05-05 21:48       ` Andi Shyti
@ 2025-05-08  8:30         ` EnDe Tan
  2025-05-08 11:55           ` Jarkko Nikula
  2025-05-12 23:29           ` Andi Shyti
  0 siblings, 2 replies; 8+ messages in thread
From: EnDe Tan @ 2025-05-08  8:30 UTC (permalink / raw)
  To: Andi Shyti, Jarkko Nikula
  Cc: linux-i2c@vger.kernel.org, andriy.shevchenko@linux.intel.com,
	mika.westerberg@linux.intel.com, jsd@semihalf.com,
	linux-kernel@vger.kernel.org, Leyfoon Tan, endeneer@gmail.com

Hi Andi and Jarkko, thank you for the feedback.

> -----Original Message-----
> From: Andi Shyti <andi.shyti@kernel.org>
> Sent: Tuesday, 6 May, 2025 5:49 AM
> To: Jarkko Nikula <jarkko.nikula@linux.intel.com>
> Cc: EnDe Tan <ende.tan@starfivetech.com>; linux-i2c@vger.kernel.org;
...
> > Good explanation and could you add it the commit log together with the
> > example?
> 
> If you want you can paste the new commit log as reply to this e-mail.

Here is the new commit log, feel free to let me know if further changes are required:

Replaced pm_runtime_put() with pm_runtime_put_sync_suspend() to ensure
the runtime suspend is invoked immediately when unregistering a slave.
This prevents a race condition where suspend was skipped when
unregistering and registering slave in quick succession.

For example, consider the rapid sequence of
`delete_device -> new_device -> delete_device -> new_device`.
In this sequence, it is observed that the dw_i2c_plat_runtime_suspend() might
not be invoked after `delete_device` operation.

This is because after `delete_device` operation, when the
pm_runtime_put() is about to trigger suspend, the following `new_device`
operation might race and cancel the suspend.

If that happens, during the `new_device` operation,
dw_i2c_plat_runtime_resume() is skipped (since there was no suspend), which
means `i_dev->init()`, i.e. i2c_dw_init_slave(), is skipped.
Since i2c_dw_init_slave() is skipped, i2c_dw_configure_fifo_slave() is
skipped too, which leaves `DW_IC_INTR_MASK` unconfigured. If we inspect
the interrupt mask register using devmem, it will show as zero.

Example shell script to reproduce the issue:
```
  #!/bin/sh

  SLAVE_LADDR=0x1010
  SLAVE_BUS=13
  NEW_DEVICE=/sys/bus/i2c/devices/i2c-$SLAVE_BUS/new_device
  DELETE_DEVICE=/sys/bus/i2c/devices/i2c-$SLAVE_BUS/delete_device

  # Create initial device
  echo slave-24c02 $SLAVE_LADDR > $NEW_DEVICE
  sleep 2

  # Rapid sequence of
  # delete_device -> new_device -> delete_device -> new_device
  echo $SLAVE_LADDR > $DELETE_DEVICE
  echo slave-24c02 $SLAVE_LADDR > $NEW_DEVICE
  echo $SLAVE_LADDR > $DELETE_DEVICE
  echo slave-24c02 $SLAVE_LADDR > $NEW_DEVICE

  # Using devmem to inspect IC_INTR_MASK will show as zero
```

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [1/1] i2c: designware: Ensure runtime suspend is invoked during rapid slave unregistration and registration
  2025-05-08  8:30         ` EnDe Tan
@ 2025-05-08 11:55           ` Jarkko Nikula
  2025-05-12 23:29           ` Andi Shyti
  1 sibling, 0 replies; 8+ messages in thread
From: Jarkko Nikula @ 2025-05-08 11:55 UTC (permalink / raw)
  To: EnDe Tan, Andi Shyti
  Cc: linux-i2c@vger.kernel.org, andriy.shevchenko@linux.intel.com,
	mika.westerberg@linux.intel.com, jsd@semihalf.com,
	linux-kernel@vger.kernel.org, Leyfoon Tan, endeneer@gmail.com

On 5/8/25 11:30 AM, EnDe Tan wrote:
> Hi Andi and Jarkko, thank you for the feedback.
> 
>> -----Original Message-----
>> From: Andi Shyti <andi.shyti@kernel.org>
>> Sent: Tuesday, 6 May, 2025 5:49 AM
>> To: Jarkko Nikula <jarkko.nikula@linux.intel.com>
>> Cc: EnDe Tan <ende.tan@starfivetech.com>; linux-i2c@vger.kernel.org;
> ...
>>> Good explanation and could you add it the commit log together with the
>>> example?
>>
>> If you want you can paste the new commit log as reply to this e-mail.
> 
> Here is the new commit log, feel free to let me know if further changes are required:
> 
> Replaced pm_runtime_put() with pm_runtime_put_sync_suspend() to ensure
> the runtime suspend is invoked immediately when unregistering a slave.
> This prevents a race condition where suspend was skipped when
> unregistering and registering slave in quick succession.
> 
> For example, consider the rapid sequence of
> `delete_device -> new_device -> delete_device -> new_device`.
> In this sequence, it is observed that the dw_i2c_plat_runtime_suspend() might
> not be invoked after `delete_device` operation.
> 
> This is because after `delete_device` operation, when the
> pm_runtime_put() is about to trigger suspend, the following `new_device`
> operation might race and cancel the suspend.
> 
> If that happens, during the `new_device` operation,
> dw_i2c_plat_runtime_resume() is skipped (since there was no suspend), which
> means `i_dev->init()`, i.e. i2c_dw_init_slave(), is skipped.
> Since i2c_dw_init_slave() is skipped, i2c_dw_configure_fifo_slave() is
> skipped too, which leaves `DW_IC_INTR_MASK` unconfigured. If we inspect
> the interrupt mask register using devmem, it will show as zero.
> 
> Example shell script to reproduce the issue:
> ```
>    #!/bin/sh
> 
>    SLAVE_LADDR=0x1010
>    SLAVE_BUS=13
>    NEW_DEVICE=/sys/bus/i2c/devices/i2c-$SLAVE_BUS/new_device
>    DELETE_DEVICE=/sys/bus/i2c/devices/i2c-$SLAVE_BUS/delete_device
> 
>    # Create initial device
>    echo slave-24c02 $SLAVE_LADDR > $NEW_DEVICE
>    sleep 2
> 
>    # Rapid sequence of
>    # delete_device -> new_device -> delete_device -> new_device
>    echo $SLAVE_LADDR > $DELETE_DEVICE
>    echo slave-24c02 $SLAVE_LADDR > $NEW_DEVICE
>    echo $SLAVE_LADDR > $DELETE_DEVICE
>    echo slave-24c02 $SLAVE_LADDR > $NEW_DEVICE
> 
>    # Using devmem to inspect IC_INTR_MASK will show as zero
> ```
Acked-by: Jarkko Nikula <jarkko.nikula@linux.intel.com>

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [1/1] i2c: designware: Ensure runtime suspend is invoked during rapid slave unregistration and registration
  2025-05-08  8:30         ` EnDe Tan
  2025-05-08 11:55           ` Jarkko Nikula
@ 2025-05-12 23:29           ` Andi Shyti
  1 sibling, 0 replies; 8+ messages in thread
From: Andi Shyti @ 2025-05-12 23:29 UTC (permalink / raw)
  To: EnDe Tan
  Cc: Jarkko Nikula, linux-i2c@vger.kernel.org,
	andriy.shevchenko@linux.intel.com,
	mika.westerberg@linux.intel.com, jsd@semihalf.com,
	linux-kernel@vger.kernel.org, Leyfoon Tan, endeneer@gmail.com

Hi EnDe,

On Thu, May 08, 2025 at 08:30:27AM +0000, EnDe Tan wrote:
> Hi Andi and Jarkko, thank you for the feedback.
> 
> > -----Original Message-----
> > From: Andi Shyti <andi.shyti@kernel.org>
> > Sent: Tuesday, 6 May, 2025 5:49 AM
> > To: Jarkko Nikula <jarkko.nikula@linux.intel.com>
> > Cc: EnDe Tan <ende.tan@starfivetech.com>; linux-i2c@vger.kernel.org;
> ...
> > > Good explanation and could you add it the commit log together with the
> > > example?
> > 
> > If you want you can paste the new commit log as reply to this e-mail.
> 
> Here is the new commit log, feel free to let me know if further changes are required:
> 
> Replaced pm_runtime_put() with pm_runtime_put_sync_suspend() to ensure
> the runtime suspend is invoked immediately when unregistering a slave.
> This prevents a race condition where suspend was skipped when
> unregistering and registering slave in quick succession.
> 
> For example, consider the rapid sequence of
> `delete_device -> new_device -> delete_device -> new_device`.
> In this sequence, it is observed that the dw_i2c_plat_runtime_suspend() might
> not be invoked after `delete_device` operation.
> 
> This is because after `delete_device` operation, when the
> pm_runtime_put() is about to trigger suspend, the following `new_device`
> operation might race and cancel the suspend.
> 
> If that happens, during the `new_device` operation,
> dw_i2c_plat_runtime_resume() is skipped (since there was no suspend), which
> means `i_dev->init()`, i.e. i2c_dw_init_slave(), is skipped.
> Since i2c_dw_init_slave() is skipped, i2c_dw_configure_fifo_slave() is
> skipped too, which leaves `DW_IC_INTR_MASK` unconfigured. If we inspect
> the interrupt mask register using devmem, it will show as zero.
> 
> Example shell script to reproduce the issue:
> ```
>   #!/bin/sh
> 
>   SLAVE_LADDR=0x1010
>   SLAVE_BUS=13
>   NEW_DEVICE=/sys/bus/i2c/devices/i2c-$SLAVE_BUS/new_device
>   DELETE_DEVICE=/sys/bus/i2c/devices/i2c-$SLAVE_BUS/delete_device
> 
>   # Create initial device
>   echo slave-24c02 $SLAVE_LADDR > $NEW_DEVICE
>   sleep 2
> 
>   # Rapid sequence of
>   # delete_device -> new_device -> delete_device -> new_device
>   echo $SLAVE_LADDR > $DELETE_DEVICE
>   echo slave-24c02 $SLAVE_LADDR > $NEW_DEVICE
>   echo $SLAVE_LADDR > $DELETE_DEVICE
>   echo slave-24c02 $SLAVE_LADDR > $NEW_DEVICE
> 
>   # Using devmem to inspect IC_INTR_MASK will show as zero
> ```

Thanks, merged to i2c/i2c-host.

I just reworded the title to:

i2c: designware: Invoke runtime suspend on quick slave re-registration

to keep it under 75 characters.

Thanks,
Andi

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2025-05-12 23:29 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-04-12  2:33 [1/1] i2c: designware: Ensure runtime suspend is invoked during rapid slave unregistration and registration ende.tan
2025-04-15 13:57 ` Jarkko Nikula
2025-04-20  3:31   ` EnDe Tan
2025-05-02 11:03     ` Jarkko Nikula
2025-05-05 21:48       ` Andi Shyti
2025-05-08  8:30         ` EnDe Tan
2025-05-08 11:55           ` Jarkko Nikula
2025-05-12 23:29           ` Andi Shyti

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox