i2c: designware: Implement atomic transfer suppot
Subsystems (4)
cover i2c: designware: Implement atomic transfer suppot
Implement atomic transfer support for designware i2c. This increases complexity but is necessary for atomic I2C transfers required by some hardware configurations, e.g., to trigger reboots on an external PMIC chip.
patch1 is bug fix
patch2 is the atomic transfer implementation patch.
Jisheng Zhang (2):
i2c: designware: Avoid taking clk_prepare mutex in PM callbacks
i2c: designware: Implement atomic transfer suppot
drivers/i2c/busses/i2c-designware-common.c | 47 +++++++++---- drivers/i2c/busses/i2c-designware-core.h | 5 +- drivers/i2c/busses/i2c-designware-master.c | 80 +++++++++++++++++++--- 3 files changed, 106 insertions(+), 26 deletions(-)
Signature · 1 line
2.50.1
PATCH 1/2 i2c: designware: Avoid taking clk_prepare mutex in PM callbacks
This is unsafe, as the runtime PM callbacks are called from the PM workqueue, so this may deadlock when handling an i2c attached clock, which may already hold the clk_prepare mutex from another context.
--- drivers/i2c/busses/i2c-designware-common.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/i2c/busses/i2c-designware-common.c +6 -3
@@ -776,7 +776,8 @@ static int i2c_dw_runtime_suspend(struct device *device)return 0 ;i2c_dw_disable (dev); -i2c_dw_prepare_clk (dev, false); +clk_disable (dev->clk); +clk_disable (dev->pclk);return 0 ; }
@@ -794,8 +795,10 @@ static int i2c_dw_runtime_resume(struct device *device) {struct dw_i2c_dev *dev =dev_get_drvdata (device); -if (!dev->shared_with_punit) -i2c_dw_prepare_clk (dev, true); +if (!dev->shared_with_punit) { +clk_enable (dev->pclk); +clk_enable (dev->clk); + } dev->init (dev);
Signature · 1 line
2.50.1
PATCH 2/2 i2c: designware: Implement atomic transfer suppot
In reply to Jisheng ZhangRework the read and write code paths in the driver to support operation in atomic contexts. To achieve this, the driver must not rely on IRQs or perform any scheduling, e.g., via a sleep or schedule routine.
Implement atomic, sleep-free, and IRQ-less operation. This increases complexity but is necessary for atomic I2C transfers required by some hardware configurations, e.g., to trigger reboots on an external PMIC chip.
--- drivers/i2c/busses/i2c-designware-common.c | 38 +++++++--- drivers/i2c/busses/i2c-designware-core.h | 5 +- drivers/i2c/busses/i2c-designware-master.c | 80 +++++++++++++++++++--- 3 files changed, 100 insertions(+), 23 deletions(-)
drivers/i2c/busses/i2c-designware-common.c +28 -10
@@ -532,15 +532,23 @@ void __i2c_dw_disable(struct dw_i2c_dev *dev)* 25us) to ensure the I2C ENABLE bit is already set * as described in the DesignWare I2C databook. */ -fsleep (DIV_ROUND_CLOSEST_ULL (10 *MICRO , t->bus_freq_hz)); +if (dev->atomic) +udelay (DIV_ROUND_CLOSEST_ULL (10 *MICRO , t->bus_freq_hz)); +else +fsleep (DIV_ROUND_CLOSEST_ULL (10 *MICRO , t->bus_freq_hz));/* Set ENABLE bit before setting ABORT */ enable |=DW_IC_ENABLE_ENABLE ; }regmap_write (dev->map,DW_IC_ENABLE , enable |DW_IC_ENABLE_ABORT ); - ret =regmap_read_poll_timeout (dev->map,DW_IC_ENABLE , enable, - !(enable &DW_IC_ENABLE_ABORT ),10 , -100 ); +if (dev->atomic) + ret =regmap_read_poll_timeout_atomic (dev->map,DW_IC_ENABLE , enable, + !(enable &DW_IC_ENABLE_ABORT ),10 , +100 ); +else + ret =regmap_read_poll_timeout (dev->map,DW_IC_ENABLE , enable, + !(enable &DW_IC_ENABLE_ABORT ),10 , +100 );if (ret)dev_err (dev->dev,"timeout while trying to abort current transfer\n" ); }
@@ -560,7 +568,10 @@ void __i2c_dw_disable(struct dw_i2c_dev *dev)* transfer supported by the driver (for 400kHz this is * 25us) as described in the DesignWare I2C databook. */ -usleep_range (25 ,250 ); +if (dev->atomic) +udelay (25 ); +else +usleep_range (25 ,250 ); }while (timeout--);dev_warn (dev->dev,"timeout in disabling adapter\n" );
@@ -607,7 +618,7 @@ int i2c_dw_acquire_lock(struct dw_i2c_dev *dev) {int ret; -if (!dev->acquire_lock) +if (dev->atomic || !dev->acquire_lock)return 0 ; ret = dev->acquire_lock ();
@@ -621,7 +632,7 @@ int i2c_dw_acquire_lock(struct dw_i2c_dev *dev)void i2c_dw_release_lock (struct dw_i2c_dev *dev) { -if (dev->release_lock) +if (!dev->atomic && dev->release_lock) dev->release_lock (); }
@@ -633,11 +644,18 @@ int i2c_dw_wait_bus_not_busy(struct dw_i2c_dev *dev)unsigned int status;int ret; - ret =regmap_read_poll_timeout (dev->map,DW_IC_STATUS , status, - !(status &DW_IC_STATUS_ACTIVITY ), -1100 ,20000 ); +if (dev->atomic) + ret =regmap_read_poll_timeout_atomic (dev->map,DW_IC_STATUS , status, + !(status &DW_IC_STATUS_ACTIVITY ), +1100 ,20000 ); +else + ret =regmap_read_poll_timeout (dev->map,DW_IC_STATUS , status, + !(status &DW_IC_STATUS_ACTIVITY ), +1100 ,20000 );if (ret) {dev_warn (dev->dev,"timeout waiting for bus ready\n" ); +if (dev->atomic) +return ret;i2c_recover_bus (&dev->adapter);
drivers/i2c/busses/i2c-designware-core.h +3 -2
@@ -299,6 +299,7 @@ struct dw_i2c_dev {void (*release_lock)(void );int semaphore_idx;bool shared_with_punit; +bool atomic;int (*init)(struct dw_i2c_dev *dev);int (*set_sda_hold_time)(struct dw_i2c_dev *dev);int mode;
@@ -365,7 +366,7 @@ static inline void __i2c_dw_disable_nowait(struct dw_i2c_dev *dev)static inline void __i2c_dw_write_intr_mask (struct dw_i2c_dev *dev,unsigned int intr_mask) { -unsigned int val = dev->flags &ACCESS_POLLING ?0 : intr_mask; +unsigned int val = (dev->atomic || dev->flags &ACCESS_POLLING ) ?0 : intr_mask;regmap_write (dev->map,DW_IC_INTR_MASK , val); dev->sw_mask = intr_mask;
@@ -374,7 +375,7 @@ static inline void __i2c_dw_write_intr_mask(struct dw_i2c_dev *dev,static inline void __i2c_dw_read_intr_mask (struct dw_i2c_dev *dev,unsigned int *intr_mask) { -if (!(dev->flags &ACCESS_POLLING )) +if (!(dev->flags &ACCESS_POLLING ) && !dev->atomic)regmap_read (dev->map,DW_IC_INTR_MASK , intr_mask);else *intr_mask = dev->sw_mask;
drivers/i2c/busses/i2c-designware-master.c +69 -11
134 lines not on this page · raw message
Signature · 1 line
2.50.1
Andy Shevchenko
reviewerRe: [PATCH 1/2] i2c: designware: Avoid taking clk_prepare mutex in PM callbacks
In reply to Jisheng ZhangQuoted 3 lines · Jisheng Zhang
This is unsafe, as the runtime PM callbacks are called from the PM workqueue, so this may deadlock when handling an i2c attached clock, which may already hold the
clk_preparemutex from another context.
Can you be more specific? What is the actual issue in practice? Do you have traces and lockdep warnings?
AFAICS it seems related to the bus recovery mechanism. Is this what you have in mind?
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH 1/2] i2c: designware: Avoid taking clk_prepare mutex in PM callbacks
Quoted 7 lines · Andy Shevchenko
Quoted 3 lines · Jisheng Zhang
This is unsafe, as the runtime PM callbacks are called from the PM workqueue, so this may deadlock when handling an i2c attached clock, which may already hold the
clk_preparemutex from another context.Can you be more specific? What is the actual issue in practice? Do you have traces and lockdep warnings?
Assume we use i2c designware to control any i2c based clks, e.g the clk-si5351.c driver. In its .clk_prepare, we'll get the prepare_lock mutex, then we call i2c adapter to operate the regs, to runtime resume the i2c adapter, we call clk_prepare_enable() which will try to get the prepare_lock mutex again.
Regards.
Quoted 9 lines
AFAICS it seems related to the bus recovery mechanism. Is this what you have in mind?
Signature · 2 lines
With Best Regards, Andy Shevchenko
Andy Shevchenko
reviewerQuoted 7 lines · Jisheng Zhang
Rework the read and write code paths in the driver to support operation in atomic contexts. To achieve this, the driver must not rely on IRQs or perform any scheduling, e.g., via a sleep or schedule routine.
Implement atomic, sleep-free, and IRQ-less operation. This increases complexity but is necessary for atomic I2C transfers required by some hardware configurations, e.g., to trigger reboots on an external PMIC chip.
...
drivers/i2c/busses/i2c-designware-common.c void __i2c_dw_disable(struct dw_i2c_dev *dev) · line 574
Quoted 5 lines
drivers/i2c/busses/i2c-designware-common.c +4
-1-usleep_range (25 ,250 ); +if (dev->atomic) +udelay (25 ); +else +usleep_range (25 ,250 );
Wondering why this delay is not being properly calculated. Why in atomic case is okay to use the shortest one?
...
drivers/i2c/busses/i2c-designware-common.c int i2c_dw_acquire_lock(struct dw_i2c_dev *dev) · line 621
Quoted 2 lines
drivers/i2c/busses/i2c-designware-common.c +1
-1-if (!dev->acquire_lock) +if (dev->atomic || !dev->acquire_lock)
I think basically we should no allow atomic transfers at all when the lock is in use. Otherwise it will be interesting case if HW (FW) wants to have an exclusive access while OS wants to perform an atomic transfer.
...
drivers/i2c/busses/i2c-designware-master.c static bool i2c_dw_is_controller_active(struct dw_i2c_dev *dev) · line 322
Quoted 8 lines
drivers/i2c/busses/i2c-designware-master.c +8
-0+if (dev->atomic) +return regmap_read_poll_timeout_atomic (dev->map,DW_IC_STATUS , status, + !(status &DW_IC_STATUS_MASTER_ACTIVITY ), +1100 ,20000 ) !=0 ; +else +return regmap_read_poll_timeout (dev->map,DW_IC_STATUS , status, + !(status &DW_IC_STATUS_MASTER_ACTIVITY ), +1100 ,20000 ) !=0 ;
Please, drop ' != 0' parts at the same time, they are redundant.
...
Quoted 6 lines
drivers/i2c/busses/i2c-designware-master.c +6
-0+static int i2c_dw_wait_transfer_atomic (struct dw_i2c_dev *dev) +{ +ktime_t timeout =ktime_add_us (ktime_get (),jiffies_to_usecs (dev->adapter.timeout)); +unsigned int stat; +int ret; +
drivers/i2c/busses/i2c-designware-master.c static int i2c_dw_wait_transfer(struct dw_i2c_dev *dev) · line 824
Quoted 10 lines
drivers/i2c/busses/i2c-designware-master.c +10
-0+ do { + ret = try_wait_for_completion(&dev->cmd_complete); + if (ret) + break; + + stat = i2c_dw_read_clear_intrbits(dev); + if (stat) + i2c_dw_process_transfer(dev, stat); + else + udelay(15);
No explanation about this value.
drivers/i2c/busses/i2c-designware-master.c static int i2c_dw_wait_transfer(struct dw_i2c_dev *dev) · line 825
Quoted 1 line
drivers/i2c/busses/i2c-designware-master.c +1
-0+ }while (ktime_compare (ktime_get (), timeout) <0 );
Whe have _before() and _after() APIs, use them.
Quoted 2 lines
drivers/i2c/busses/i2c-designware-master.c +2
-0+return ret ?0 : -ETIMEDOUT ; +}
...
drivers/i2c/busses/i2c-designware-master.c i2c_dw_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num) · line 848
Quoted 6 lines
drivers/i2c/busses/i2c-designware-master.c +4
-0switch (dev->flags &MODEL_MASK ) {case MODEL_AMD_NAVI_GPU : +if (dev->atomic) { + ret = -EOPNOTSUPP ; + goto done_nolock; + }
Why only AMD case?
Quoted 5 lines
drivers/i2c/busses/i2c-designware-master.c +0
-0ret =amd_i2c_dw_xfer_quirk (adap, msgs, num); goto done_nolock;default :break ; }
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH 1/2] i2c: designware: Avoid taking clk_prepare mutex in PM callbacks
In reply to Jisheng ZhangQuoted 6 lines · Jisheng Zhang
Quoted 5 lines · Andy Shevchenko
Quoted 3 lines · Jisheng Zhang
This is unsafe, as the runtime PM callbacks are called from the PM workqueue, so this may deadlock when handling an i2c attached clock, which may already hold the
clk_preparemutex from another context.
Quoted 9 lines
Quoted 2 lines
Can you be more specific? What is the actual issue in practice? Do you have traces and lockdep warnings?
Assume we use i2c designware to control any i2c based clks, e.g the
clk-si5351.cdriver. In its .clk_prepare, we'll get theprepare_lockmutex, then we call i2c adapter to operate the regs, to runtime resume the i2c adapter, we callclk_prepare_enable()which will try to get theprepare_lockmutex again.
I'd also like to see the issue here. I'm blind to see what's the relation between the clocks managed by the clk-si5351.c and clocks to the i2c-designware IP.
Andy Shevchenko
reviewerRe: [PATCH 1/2] i2c: designware: Avoid taking clk_prepare mutex in PM callbacks
Quoted 16 lines · Jarkko Nikula
Quoted 6 lines · Jisheng Zhang
Quoted 5 lines · Andy Shevchenko
Quoted 3 lines · Jisheng Zhang
This is unsafe, as the runtime PM callbacks are called from the PM workqueue, so this may deadlock when handling an i2c attached clock, which may already hold the
clk_preparemutex from another context.Quoted 9 lines
Quoted 2 lines
Can you be more specific? What is the actual issue in practice? Do you have traces and lockdep warnings?
Assume we use i2c designware to control any i2c based clks, e.g the
clk-si5351.cdriver. In its .clk_prepare, we'll get theprepare_lockmutex, then we call i2c adapter to operate the regs, to runtime resume the i2c adapter, we callclk_prepare_enable()which will try to get theprepare_lockmutex again.
Quoted 3 lines
I'd also like to see the issue here. I'm blind to see what's the relation between the clocks managed by the
clk-si5351.cand clocks to the i2c-designware IP.
I believe they try to make an example when clk-si5351 is the provider of the clock to I²C host controller (DesignWare).
But I'm still not sure about the issues here... Without (even simulated with specific delay injections) lockdep warnings it would be rather theoretical.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH 1/2] i2c: designware: Avoid taking clk_prepare mutex in PM callbacks
Quoted 17 lines · Andy Shevchenko
Quoted 16 lines · Jarkko Nikula
Quoted 15 lines · Jisheng Zhang
Quoted 7 lines · Andy Shevchenko
Quoted 3 lines · Jisheng Zhang
This is unsafe, as the runtime PM callbacks are called from the PM workqueue, so this may deadlock when handling an i2c attached clock, which may already hold the
clk_preparemutex from another context.Can you be more specific? What is the actual issue in practice? Do you have traces and lockdep warnings?
Assume we use i2c designware to control any i2c based clks, e.g the
clk-si5351.cdriver. In its .clk_prepare, we'll get theprepare_lockmutex, then we call i2c adapter to operate the regs, to runtime resume the i2c adapter, we callclk_prepare_enable()which will try to get theprepare_lockmutex again.
Quoted 3 lines
Quoted 3 lines
I'd also like to see the issue here. I'm blind to see what's the relation between the clocks managed by the
clk-si5351.cand clocks to the i2c-designware IP.
The key here is: all clks in the system share the same prepare_lock mutex, so the global prepare_lock mutex is locked by clk-si5351 .prepare(), then in this exact .prepare(), the i2c-designware's runtime resume will try to lock the same prepare_lock again due to clk_prepare_enable()
can you plz check clk_prepare_lock() in drivers/clk/clk.c?
And if we take a look at other i2c adapters' drivers, we'll see some of them have ever met this issue and already fixed it, such as
i2c-exynos5, by commit 10ff4c5239a1 ("i2c: exynos5: Fix possible ABBA deadlock by keeping I2C clock prepared")
i2c-imx, by commit d9a22d713acb ("i2c: imx: avoid taking clk_prepare mutex in PM callbacks")
Quoted 3 lines
I believe they try to make an example when clk-si5351 is the provider of the clock to I²C host controller (DesignWare).
Nope, the example case is using i2c host controller to operate the clk-si5351
Quoted 3 lines
But I'm still not sure about the issues here... Without (even simulated with specific delay injections) lockdep warnings it would be rather theoretical.
No, it happened in real world.
Quoted 6 lines
Signature · 3 lines
-- With Best Regards, Andy Shevchenko
Andy Shevchenko
reviewerRe: [PATCH 1/2] i2c: designware: Avoid taking clk_prepare mutex in PM callbacks
Quoted 33 lines · Jisheng Zhang
Quoted 17 lines · Andy Shevchenko
Quoted 16 lines · Jarkko Nikula
Quoted 15 lines · Jisheng Zhang
Quoted 7 lines · Andy Shevchenko
Quoted 3 lines · Jisheng Zhang
This is unsafe, as the runtime PM callbacks are called from the PM workqueue, so this may deadlock when handling an i2c attached clock, which may already hold the
clk_preparemutex from another context.Can you be more specific? What is the actual issue in practice? Do you have traces and lockdep warnings?
Assume we use i2c designware to control any i2c based clks, e.g the
clk-si5351.cdriver. In its .clk_prepare, we'll get theprepare_lockmutex, then we call i2c adapter to operate the regs, to runtime resume the i2c adapter, we callclk_prepare_enable()which will try to get theprepare_lockmutex again.Quoted 3 lines
Quoted 3 lines
I'd also like to see the issue here. I'm blind to see what's the relation between the clocks managed by the
clk-si5351.cand clocks to the i2c-designware IP.The key here is: all clks in the system share the same
prepare_lockmutex, so the globalprepare_lockmutex is locked by clk-si5351 .prepare(), then in this exact .prepare(), the i2c-designware's runtime resume will try to lock the sameprepare_lockagain due toclk_prepare_enable()
can you plz checkclk_prepare_lock()indrivers/clk/clk.c?And if we take a look at other i2c adapters' drivers, we'll see some of them have ever met this issue and already fixed it, such as
Quoted 5 lines
i2c-exynos5, by commit
10ff4c5239a1("i2c: exynos5: Fix possible ABBA deadlock by keeping I2C clock prepared")i2c-imx, by commit
d9a22d713acb("i2c: imx: avoid takingclk_preparemutex in PM callbacks")
Why is this an I²C driver problem?
Quoted 4 lines
Quoted 2 lines
I believe they try to make an example when clk-si5351 is the provider of the clock to I²C host controller (DesignWare).
Nope, the example case is using i2c host controller to operate the clk-si5351
Okay, so that chip is controlled over I²C, but how their clocks even related to the I²C host controller clock?! I am sorry, I am lost here.
Quoted 4 lines
Quoted 2 lines
But I'm still not sure about the issues here... Without (even simulated with specific delay injections) lockdep warnings it would be rather theoretical.
No, it happened in real world.
Can you provide the asked traces and lockdep warnigns and/or other stuff to see what's going on there?
Signature · 2 lines
With Best Regards, Andy Shevchenko
Andy Shevchenko
reviewerRe: [PATCH 1/2] i2c: designware: Avoid taking clk_prepare mutex in PM callbacks
Quoted 34 lines · Andy Shevchenko
Quoted 33 lines · Jisheng Zhang
Quoted 17 lines · Andy Shevchenko
Quoted 16 lines · Jarkko Nikula
Quoted 15 lines · Jisheng Zhang
Quoted 7 lines · Andy Shevchenko
Quoted 3 lines · Jisheng Zhang
This is unsafe, as the runtime PM callbacks are called from the PM workqueue, so this may deadlock when handling an i2c attached clock, which may already hold the
clk_preparemutex from another context.Can you be more specific? What is the actual issue in practice? Do you have traces and lockdep warnings?
Assume we use i2c designware to control any i2c based clks, e.g the
clk-si5351.cdriver. In its .clk_prepare, we'll get theprepare_lockmutex, then we call i2c adapter to operate the regs, to runtime resume the i2c adapter, we callclk_prepare_enable()which will try to get theprepare_lockmutex again.Quoted 3 lines
Quoted 3 lines
I'd also like to see the issue here. I'm blind to see what's the relation between the clocks managed by the
clk-si5351.cand clocks to the i2c-designware IP.The key here is: all clks in the system share the same
prepare_lockmutex, so the globalprepare_lockmutex is locked by clk-si5351 .prepare(), then in this exact .prepare(), the i2c-designware's runtime resume will try to lock the sameprepare_lockagain due toclk_prepare_enable()
can you plz checkclk_prepare_lock()indrivers/clk/clk.c?And if we take a look at other i2c adapters' drivers, we'll see some of them have ever met this issue and already fixed it, such as
Quoted 7 lines
Quoted 5 lines
i2c-exynos5, by commit
10ff4c5239a1("i2c: exynos5: Fix possible ABBA deadlock by keeping I2C clock prepared")i2c-imx, by commit
d9a22d713acb("i2c: imx: avoid takingclk_preparemutex in PM callbacks")Why is this an I²C driver problem?
I just read these two and one more referenced from one of the changes.
I do not think this is a correct fix. Seems to me like papering over a special (corner case). I would agree on this change if and only if the CLK maintainers tell us that there is no other way.
My understanding is that the I²C clock and client's clocks (when it's a clock provider) are independent. There should not be such a clash to begin with. The clock framework should operate on a clock subtrees and not having yet another Global Kernel Lock.
That said, I think this is a design issue in CLK framework, we should not go and "fix" all the drivers. Today it's I²C, tomorrow SPI and I³C and so on... This is not a scalable solution.
Here is formal NAK until it will be worked with CLK maintainers to provide an agreed roadmap for this(ese) issue(s).
Quoted 15 lines
Quoted 4 lines
Quoted 2 lines
I believe they try to make an example when clk-si5351 is the provider of the clock to I²C host controller (DesignWare).
Nope, the example case is using i2c host controller to operate the clk-si5351
Okay, so that chip is controlled over I²C, but how their clocks even related to the I²C host controller clock?! I am sorry, I am lost here.
Quoted 4 lines
Quoted 2 lines
But I'm still not sure about the issues here... Without (even simulated with specific delay injections) lockdep warnings it would be rather theoretical.
No, it happened in real world.
Can you provide the asked traces and lockdep warnigns and/or other stuff to see what's going on there?
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH 1/2] i2c: designware: Avoid taking clk_prepare mutex in PM callbacks
Quoted 49 lines · Andy Shevchenko
Quoted 34 lines · Andy Shevchenko
Quoted 33 lines · Jisheng Zhang
Quoted 17 lines · Andy Shevchenko
Quoted 16 lines · Jarkko Nikula
Quoted 15 lines · Jisheng Zhang
Quoted 7 lines · Andy Shevchenko
Quoted 3 lines · Jisheng Zhang
This is unsafe, as the runtime PM callbacks are called from the PM workqueue, so this may deadlock when handling an i2c attached clock, which may already hold the
clk_preparemutex from another context.Can you be more specific? What is the actual issue in practice? Do you have traces and lockdep warnings?
Assume we use i2c designware to control any i2c based clks, e.g the
clk-si5351.cdriver. In its .clk_prepare, we'll get theprepare_lockmutex, then we call i2c adapter to operate the regs, to runtime resume the i2c adapter, we callclk_prepare_enable()which will try to get theprepare_lockmutex again.Quoted 3 lines
Quoted 3 lines
I'd also like to see the issue here. I'm blind to see what's the relation between the clocks managed by the
clk-si5351.cand clocks to the i2c-designware IP.The key here is: all clks in the system share the same
prepare_lockmutex, so the globalprepare_lockmutex is locked by clk-si5351 .prepare(), then in this exact .prepare(), the i2c-designware's runtime resume will try to lock the sameprepare_lockagain due toclk_prepare_enable()
can you plz checkclk_prepare_lock()indrivers/clk/clk.c?And if we take a look at other i2c adapters' drivers, we'll see some of them have ever met this issue and already fixed it, such as
Quoted 7 lines
Quoted 5 lines
i2c-exynos5, by commit
10ff4c5239a1("i2c: exynos5: Fix possible ABBA deadlock by keeping I2C clock prepared")i2c-imx, by commit
d9a22d713acb("i2c: imx: avoid takingclk_preparemutex in PM callbacks")Why is this an I²C driver problem?
I just read these two and one more referenced from one of the changes.
I do not think this is a correct fix. Seems to me like papering over a special (corner case). I would agree on this change if and only if the CLK maintainers tell us that there is no other way.
Quoted 8 lines
My understanding is that the I²C clock and client's clocks (when it's a clock provider) are independent. There should not be such a clash to begin with. The clock framework should operate on a clock subtrees and not having yet another Global Kernel Lock.
That said, I think this is a design issue in CLK framework, we should not go and "fix" all the drivers. Today it's I²C, tomorrow SPI and I³C and so on... This is not a scalable solution.
The fact is there's no SPI attached clks. Current all clks are either mmio based or i2c attached clks. And yep you're right, I3C has the same problem if we use i3c controller to operate those i2c attached clks.
Quoted 3 lines
Here is formal NAK until it will be worked with CLK maintainers to provide an agreed roadmap for this(ese) issue(s).
+ clk maintainers
Hi Michael and Stephen,
If we use i2c designware adapter to operate the i2c attached clks such as clk-si5351, then there's a deadlock issue -- This issue is well explained by commit d9a22d713acb ("i2c: imx: avoid taking clk_prepare mutex in PM callbacks")
I propsed a similar fix for the i2c-designware adapter, but Andy disagree with this fix. Instead, he thinks the issue is from the clk framework. Could you please comment?
Thanks
Quoted 22 lines
Quoted 16 lines
Quoted 5 lines
Quoted 3 lines
I believe they try to make an example when clk-si5351 is the provider of the clock to I²C host controller (DesignWare).
Nope, the example case is using i2c host controller to operate the clk-si5351
Okay, so that chip is controlled over I²C, but how their clocks even related to the I²C host controller clock?! I am sorry, I am lost here.
Quoted 4 lines
Quoted 2 lines
But I'm still not sure about the issues here... Without (even simulated with specific delay injections) lockdep warnings it would be rather theoretical.
No, it happened in real world.
Can you provide the asked traces and lockdep warnigns and/or other stuff to see what's going on there?
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH 1/2] i2c: designware: Avoid taking clk_prepare mutex in PM callbacks
In reply to Andy ShevchenkoQuoted 49 lines · Andy Shevchenko
Quoted 34 lines · Andy Shevchenko
Quoted 33 lines · Jisheng Zhang
Quoted 17 lines · Andy Shevchenko
Quoted 16 lines · Jarkko Nikula
Quoted 15 lines · Jisheng Zhang
Quoted 7 lines · Andy Shevchenko
Quoted 3 lines · Jisheng Zhang
This is unsafe, as the runtime PM callbacks are called from the PM workqueue, so this may deadlock when handling an i2c attached clock, which may already hold the
clk_preparemutex from another context.Can you be more specific? What is the actual issue in practice? Do you have traces and lockdep warnings?
Assume we use i2c designware to control any i2c based clks, e.g the
clk-si5351.cdriver. In its .clk_prepare, we'll get theprepare_lockmutex, then we call i2c adapter to operate the regs, to runtime resume the i2c adapter, we callclk_prepare_enable()which will try to get theprepare_lockmutex again.Quoted 3 lines
Quoted 3 lines
I'd also like to see the issue here. I'm blind to see what's the relation between the clocks managed by the
clk-si5351.cand clocks to the i2c-designware IP.The key here is: all clks in the system share the same
prepare_lockmutex, so the globalprepare_lockmutex is locked by clk-si5351 .prepare(), then in this exact .prepare(), the i2c-designware's runtime resume will try to lock the sameprepare_lockagain due toclk_prepare_enable()
can you plz checkclk_prepare_lock()indrivers/clk/clk.c?And if we take a look at other i2c adapters' drivers, we'll see some of them have ever met this issue and already fixed it, such as
Quoted 7 lines
Quoted 5 lines
i2c-exynos5, by commit
10ff4c5239a1("i2c: exynos5: Fix possible ABBA deadlock by keeping I2C clock prepared")i2c-imx, by commit
d9a22d713acb("i2c: imx: avoid takingclk_preparemutex in PM callbacks")Why is this an I²C driver problem?
I just read these two and one more referenced from one of the changes.
I do not think this is a correct fix. Seems to me like papering over a special (corner case). I would agree on this change if and only if the CLK maintainers tell us that there is no other way.
Quoted 6 lines
My understanding is that the I²C clock and client's clocks (when it's a clock provider) are independent. There should not be such a clash to begin with. The clock framework should operate on a clock subtrees and not having yet another Global Kernel Lock.
That said, I think this is a design issue in CLK framework, we should not go and
After some thoughts, let me show you another case where this patch is needed and not related with CLK framework at all: As can be seen in patch2, atomic transfer support is added, but if IIRC, the clk_prepare_enable() can't be used in atomic context. Then how to support atomic transfer if patch1 is NAKed? Any comment is appreciated.
Thanks
Quoted 27 lines
"fix" all the drivers. Today it's I²C, tomorrow SPI and I³C and so on... This is not a scalable solution.
Here is formal NAK until it will be worked with CLK maintainers to provide an agreed roadmap for this(ese) issue(s).
Quoted 15 lines
Quoted 4 lines
Quoted 2 lines
I believe they try to make an example when clk-si5351 is the provider of the clock to I²C host controller (DesignWare).
Nope, the example case is using i2c host controller to operate the clk-si5351
Okay, so that chip is controlled over I²C, but how their clocks even related to the I²C host controller clock?! I am sorry, I am lost here.
Quoted 4 lines
Quoted 2 lines
But I'm still not sure about the issues here... Without (even simulated with specific delay injections) lockdep warnings it would be rather theoretical.
No, it happened in real world.
Can you provide the asked traces and lockdep warnigns and/or other stuff to see what's going on there?
Signature · 2 lines
With Best Regards, Andy Shevchenko