i2c: tegra: fix error handling in tegra_i2c_xfer()
PATCH 1/1 i2c: tegra: fix error handling in tegra_i2c_xfer()
Fix two bugs in the SW mutex path introduced by commit 6077cfd716fb ("i2c: tegra: Add support for SW mutex register"):
1. If tegra_i2c_mutex_lock() fails, the function returns without calling pm_runtime_put(), leaking the runtime PM reference acquired by the preceding pm_runtime_get_sync(). Add the missing pm_runtime_put() before returning.
2. tegra_i2c_mutex_unlock() unconditionally overwrites ret, which may already hold a transfer error from tegra_i2c_xfer_msg(). If the transfer failed but the unlock succeeds, the error is silently lost and the function incorrectly reports success. Use a separate variable for the unlock return value and preserve error priority:
transfer error > unlock error > message count.
--- drivers/i2c/busses/i2c-tegra.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
drivers/i2c/busses/i2c-tegra.c +6 -4
@@ -1656,7 +1656,7 @@ static int tegra_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num) {struct tegra_i2c_dev *i2c_dev =i2c_get_adapdata (adap); -int i, ret; +int i, ret, ret2; ret =pm_runtime_get_sync (i2c_dev->dev);if (ret <0 ) {
@@ -1666,8 +1666,10 @@ static int tegra_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], } ret =tegra_i2c_mutex_lock (i2c_dev); -if (ret) +if (ret) { +pm_runtime_put (i2c_dev->dev);return ret; + }for (i =0 ; i < num; i++) {enum msg_end_type end_type =MSG_END_STOP ;
@@ -1698,10 +1700,10 @@ static int tegra_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],break ; } - ret =tegra_i2c_mutex_unlock (i2c_dev); + ret2 =tegra_i2c_mutex_unlock (i2c_dev);pm_runtime_put (i2c_dev->dev); -return ret ?: i; +return ret ?: ret2 ?: i; } static int tegra_i2c_xfer_atomic(struct i2c_adapter *adap,
Signature · 1 line
2.47.3
Jon Hunter
maintainerQuoted 8 lines · Saurav Sachidanand
Fix two bugs in the SW mutex path introduced by commit
6077cfd716fb("i2c: tegra: Add support for SW mutex register"):1. If
tegra_i2c_mutex_lock()fails, the function returns without callingpm_runtime_put(), leaking the runtime PM reference acquired by the precedingpm_runtime_get_sync(). Add the missingpm_runtime_put()before returning.
Quoted 8 lines
2.
tegra_i2c_mutex_unlock()unconditionally overwrites ret, which may already hold a transfer error fromtegra_i2c_xfer_msg(). If the transfer failed but the unlock succeeds, the error is silently lost and the function incorrectly reports success. Use a separate variable for the unlock return value and preserve error priority:
transfer error > unlock error > message count.Fixes 6077cfd716fb ("i2c: tegra: Add support for SW mutex register")
Although this is fixing issues associated with one patch, this is fixing two different issues and so I think that this should be split into 2 patches.
Quoted 31 lines
Signed-off-by Saurav Sachidanand <[email protected]>--- drivers/i2c/busses/i2c-tegra.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)drivers/i2c/busses/i2c-tegra.c +4
-2@@ -1656,7 +1656,7 @@ static int tegra_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num) {struct tegra_i2c_dev *i2c_dev =i2c_get_adapdata (adap); -int i, ret; +int i, ret, ret2; ret =pm_runtime_get_sync (i2c_dev->dev);if (ret <0 ) {@@ -1666,8 +1666,10 @@ static int tegra_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], } ret =tegra_i2c_mutex_lock (i2c_dev); -if (ret) +if (ret) { +pm_runtime_put (i2c_dev->dev);return ret; + }for (i =0 ; i < num; i++) {enum msg_end_type end_type =MSG_END_STOP ;
drivers/i2c/busses/i2c-tegra.c static int tegra_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], · line 1706
Quoted 9 lines
drivers/i2c/busses/i2c-tegra.c +2
-2@@ -1698,10 +1700,10 @@ static int tegra_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],break ; } - ret =tegra_i2c_mutex_unlock (i2c_dev); + ret2 =tegra_i2c_mutex_unlock (i2c_dev);pm_runtime_put (i2c_dev->dev); -return ret ?: i; +return ret ?: ret2 ?: i;
I can't say I am a fan of this. I wonder if we would be better off removing the return value from tegra_i2c_mutex_unlock() and just WARN if this ever happens? If the unlock did fail, the actual I2C message may still have been sent and the next time we try to send a message I assume that the lock would fail anyway and we would not be able to send further messages.
Jon
Signature · 1 line
nvpublic
v2 cover i2c: tegra: fix error handling in tegra_i2c_xfer()
In reply to Saurav SachidanandFix two bugs in the SW mutex path introduced by commit 6077cfd716fb ("i2c: tegra: Add support for SW mutex register"):
1/2: Fix pm_runtime reference leak when tegra_i2c_mutex_lock() fails. 2/2: Make tegra_i2c_mutex_unlock() return void with WARN to avoid
silently losing transfer errors.
Changes since v1:
- Split into two patches (Jon)
- Make tegra_i2c_mutex_unlock() return void with WARN instead of propagating the error value (Jon)
v1: https://lore.kernel.org/all/[email protected]/
Saurav Sachidanand (2):
i2c: tegra: fix pm_runtime leak on mutex_lock failure
i2c: tegra: make tegra_i2c_mutex_unlock() return void
drivers/i2c/busses/i2c-tegra.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-)
Signature · 1 line
2.47.3
PATCH v2 1/2 i2c: tegra: fix pm_runtime leak on mutex_lock failure
If tegra_i2c_mutex_lock() fails, the function returns without calling pm_runtime_put(), leaking the runtime PM reference acquired by the preceding pm_runtime_get_sync(). This prevents the device from ever entering runtime suspend.
Add the missing pm_runtime_put() before returning on lock failure.
--- drivers/i2c/busses/i2c-tegra.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/i2c/busses/i2c-tegra.c +3 -1
@@ -1666,8 +1666,10 @@ static int tegra_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], } ret =tegra_i2c_mutex_lock (i2c_dev); -if (ret) +if (ret) { +pm_runtime_put (i2c_dev->dev);return ret; + }for (i =0 ; i < num; i++) {enum msg_end_type end_type =MSG_END_STOP ;
Signature · 1 line
2.47.3
Re: [PATCH v2 1/2] i2c: tegra: fix pm_runtime leak on mutex_lock failure
Quoted 5 lines · Saurav Sachidanand
If
tegra_i2c_mutex_lock()fails, the function returns without callingpm_runtime_put(), leaking the runtime PM reference acquired by the precedingpm_runtime_get_sync(). This prevents the device from ever entering runtime suspend.
Quoted 7 lines
Add the missing
pm_runtime_put()before returning on lock failure.Fixes 6077cfd716fb ("i2c: tegra: Add support for SW mutex register")Signed-off-by Saurav Sachidanand <[email protected]>--- drivers/i2c/busses/i2c-tegra.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
Re: [PATCH v2 1/2] i2c: tegra: fix pm_runtime leak on mutex_lock failure
Quoted 8 lines · Thierry Reding
Quoted 7 lines · Saurav Sachidanand
If
tegra_i2c_mutex_lock()fails, the function returns without callingpm_runtime_put(), leaking the runtime PM reference acquired by the precedingpm_runtime_get_sync(). This prevents the device from ever entering runtime suspend.Add the missing
pm_runtime_put()before returning on lock failure.
Quoted 7 lines
Quoted 5 lines
Fixes 6077cfd716fb ("i2c: tegra: Add support for SW mutex register")Signed-off-by Saurav Sachidanand <[email protected]>--- drivers/i2c/busses/i2c-tegra.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)Acked-by Thierry Reding <[email protected]>
Thanks
Jon
Signature · 1 line
nvpublic
PATCH v2 2/2 i2c: tegra: make tegra_i2c_mutex_unlock() return void
In reply to Saurav Sachidanandtegra_i2c_mutex_unlock() returning an error that overwrites the transfer result causes silent loss of I2C transfer errors. If the transfer failed but the unlock succeeded, the error was lost and the function incorrectly reported success.
Rather than propagating the unlock error (which is not actionable by the caller - the I2C message may have been sent regardless), convert the function to return void and WARN on the unexpected condition. If the unlock fails, subsequent lock attempts will fail anyway, making the error visible on the next transfer.
--- drivers/i2c/busses/i2c-tegra.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-)
drivers/i2c/busses/i2c-tegra.c +6 -9
@@ -589,25 +589,22 @@ static int tegra_i2c_mutex_lock(struct tegra_i2c_dev *i2c_dev)return ret; } -static int tegra_i2c_mutex_unlock (struct tegra_i2c_dev *i2c_dev) +static void tegra_i2c_mutex_unlock (struct tegra_i2c_dev *i2c_dev) {unsigned int reg = i2c_dev->hw->regs->sw_mutex;u32 val, id;if (!i2c_dev->hw->has_mutex) -return 0 ; +return ; val =readl (i2c_dev->base + reg); id =FIELD_GET (I2C_SW_MUTEX_GRANT , val); -if (id && id !=I2C_SW_MUTEX_ID_CCPLEX ) { -dev_warn (i2c_dev->dev,"unable to unlock mutex, mutex is owned by: %u\n" , id); -return -EPERM ; - } +if (WARN (id && id !=I2C_SW_MUTEX_ID_CCPLEX , +"unable to unlock mutex, mutex is owned by: %u\n" , id)) +return ;writel (0 , i2c_dev->base + reg); - -return 0 ; }static void tegra_i2c_mask_irq (struct tegra_i2c_dev *i2c_dev,u32 mask)
@@ -1700,7 +1697,7 @@ static int tegra_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],break ; } - ret =tegra_i2c_mutex_unlock (i2c_dev); +tegra_i2c_mutex_unlock (i2c_dev);pm_runtime_put (i2c_dev->dev);return ret ?: i;
Signature · 1 line
2.47.3
Re: [PATCH v2 2/2] i2c: tegra: make tegra_i2c_mutex_unlock() return void
Quoted 10 lines · Saurav Sachidanand
tegra_i2c_mutex_unlock()returning an error that overwrites the transfer result causes silent loss of I2C transfer errors. If the transfer failed but the unlock succeeded, the error was lost and the function incorrectly reported success.Rather than propagating the unlock error (which is not actionable by the caller - the I2C message may have been sent regardless), convert the function to return void and WARN on the unexpected condition. If the unlock fails, subsequent lock attempts will fail anyway, making the error visible on the next transfer.
Technically I don't think it's guaranteed that a subsequent lock attempt will fail. For example, if the SW mutex was somehow held by some other owner while trying to unlock, by the time we try to lock later on that owner might have released the SW mutex again.
Obviously if we've managed to lock the SW mutex but fail to unlock because somebody else was holding it, it means that the other party did not respect the SW mutex protocol, in which case anything goes.
Anyway, this looks good to me, so:
Re: [PATCH v2 2/2] i2c: tegra: make tegra_i2c_mutex_unlock() return void
Quoted 17 lines · Thierry Reding
Quoted 10 lines · Saurav Sachidanand
tegra_i2c_mutex_unlock()returning an error that overwrites the transfer result causes silent loss of I2C transfer errors. If the transfer failed but the unlock succeeded, the error was lost and the function incorrectly reported success.Rather than propagating the unlock error (which is not actionable by the caller - the I2C message may have been sent regardless), convert the function to return void and WARN on the unexpected condition. If the unlock fails, subsequent lock attempts will fail anyway, making the error visible on the next transfer.
Technically I don't think it's guaranteed that a subsequent lock attempt will fail. For example, if the SW mutex was somehow held by some other owner while trying to unlock, by the time we try to lock later on that owner might have released the SW mutex again.
Quoted 7 lines
Obviously if we've managed to lock the SW mutex but fail to unlock because somebody else was holding it, it means that the other party did not respect the SW mutex protocol, in which case anything goes.
Anyway, this looks good to me, so:
Acked-by Thierry Reding <[email protected]>
Thanks
Jon
Signature · 1 line
nvpublic
Hi Saurav,
Quoted 3 lines
Saurav Sachidanand (2):
i2c: tegra: fixpm_runtimeleak onmutex_lockfailure
i2c: tegra: maketegra_i2c_mutex_unlock()return void
merged to i2c/i2c-host-fixes.
Next time, please:
- don't forget to add me in Cc
- to not nest in the same thread different versions. It would have been clearer if v2 was in its own thread.
Thanks,
Andi