i2c: qcom-geni: Add dynamic transfer timeout based on transfer length and frequency
Subsystems (4)
PATCH v3 1/1 i2c: qcom-geni: Add dynamic transfer timeout based on transfer length and frequency
The driver uses a static XFER_TIMEOUT of HZ (1 second) for all transfers regardless of message length or bus frequency, causing unnecessary delays on error paths.
Compute the timeout dynamically from message length and bus frequency with a 10x safety margin over the theoretical wire time and a 300ms floor. For GPI multi-descriptor transfers, use the maximum message length across all queued messages as the per-completion timeout.
---
Changes in v3:
- Added the extra variable to handle timeout value.
drivers/i2c/busses/i2c-qcom-geni.c | 45 +++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 10 deletions(-)
drivers/i2c/busses/i2c-qcom-geni.c +35 -10
@@ -74,9 +74,12 @@ enum geni_i2c_err_code {#define PACKING_BYTES_PW 4#define ABORT_TIMEOUT HZ -#define XFER_TIMEOUT HZ#define RST_TIMEOUT HZ +/* 9 bits per byte (8 data + 1 ACK), 10x safety margin, 300ms floor */ +#define I2C_TIMEOUT_SAFETY_COEFFICIENT 10 +#define I2C_TIMEOUT_MIN_USEC 300000 +struct geni_i2c_desc {bool no_dma_support;unsigned int tx_fifo_depth;
@@ -204,6 +207,16 @@ static int geni_i2c_clk_map_idx(struct geni_i2c_dev *gi2c)return -EINVAL ; } +static unsigned long geni_i2c_xfer_timeout (struct geni_i2c_dev *gi2c,size_t len) +{ +size_t bit_cnt = len *9 ; +size_t bit_usec = (bit_cnt *USEC_PER_SEC ) / gi2c->clk_freq_out; +size_t xfer_max_usec = (bit_usec *I2C_TIMEOUT_SAFETY_COEFFICIENT ) + +I2C_TIMEOUT_MIN_USEC ; + +return usecs_to_jiffies (xfer_max_usec); +} +static int qcom_geni_i2c_conf (struct geni_se *se,unsigned long freq) {struct geni_i2c_dev *gi2c =dev_get_drvdata (se->dev);
@@ -445,7 +458,7 @@ static int geni_i2c_rx_one_msg(struct geni_i2c_dev *gi2c, struct i2c_msg *msg, u32 m_param) {dma_addr_t rx_dma =0 ; -unsigned long time_left; +unsigned long time_left, timeout;void *dma_buf;struct geni_se *se = &gi2c->se;size_t len = msg->len;
@@ -470,8 +483,9 @@ static int geni_i2c_rx_one_msg(struct geni_i2c_dev *gi2c, struct i2c_msg *msg, gi2c->dma_buf = dma_buf; } + timeout =geni_i2c_xfer_timeout (gi2c, len); cur = gi2c->cur; - time_left =wait_for_completion_timeout (&gi2c->done,XFER_TIMEOUT ); + time_left =wait_for_completion_timeout (&gi2c->done, timeout);if (!time_left)geni_i2c_abort_xfer (gi2c);
@@ -484,7 +498,7 @@ static int geni_i2c_tx_one_msg(struct geni_i2c_dev *gi2c, struct i2c_msg *msg, u32 m_param) {dma_addr_t tx_dma =0 ; -unsigned long time_left; +unsigned long time_left, timeout;void *dma_buf;struct geni_se *se = &gi2c->se;size_t len = msg->len;
@@ -512,8 +526,9 @@ static int geni_i2c_tx_one_msg(struct geni_i2c_dev *gi2c, struct i2c_msg *msg,if (!dma_buf)/* Get FIFO IRQ */ writel_relaxed (1 , se->base +SE_GENI_TX_WATERMARK_REG ); + timeout =geni_i2c_xfer_timeout (gi2c, len); cur = gi2c->cur; - time_left =wait_for_completion_timeout (&gi2c->done,XFER_TIMEOUT ); + time_left =wait_for_completion_timeout (&gi2c->done, timeout);if (!time_left)geni_i2c_abort_xfer (gi2c);
@@ -591,7 +606,7 @@ static void geni_i2c_gpi_multi_desc_unmap(struct geni_i2c_dev *gi2c, struct i2c_* geni_i2c_gpi_multi_xfer_timeout_handler() - Handles multi message transfer timeout * @dev: Pointer to the corresponding dev node * @multi_xfer: Pointer to the geni_i2c_gpi_multi_desc_xfer -* @transfer_timeout_msecs: Timeout value in milliseconds +* @transfer_timeout_msecs: Per-message completion timeout in jiffies * @transfer_comp: Completion object of the transfer * * This function waits for the completion of each processed transfer messages
@@ -601,7 +616,7 @@ static void geni_i2c_gpi_multi_desc_unmap(struct geni_i2c_dev *gi2c, struct i2c_*/ static int geni_i2c_gpi_multi_xfer_timeout_handler (struct device *dev,struct geni_i2c_gpi_multi_desc_xfer *multi_xfer, -u32 transfer_timeout_msecs, +unsigned long timeout_jiffies,struct completion *transfer_comp) {int i;
@@ -612,7 +627,7 @@ static int geni_i2c_gpi_multi_xfer_timeout_handler(struct device *dev,if (multi_xfer->msg_idx_cnt != multi_xfer->irq_cnt) { time_left =wait_for_completion_timeout (transfer_comp, - transfer_timeout_msecs); + timeout_jiffies);if (!time_left) {dev_err (dev,"%s: Transfer timeout\n" , __func__);return -ETIMEDOUT ;
@@ -736,8 +751,16 @@ static int geni_i2c_gpi(struct geni_i2c_dev *gi2c, struct i2c_msg msgs[],dma_async_issue_pending (gi2c->tx_c);if ((msg_idx == (gi2c->num_msgs -1 )) || flags &DMA_PREP_INTERRUPT ) { +unsigned long timeout; +size_t max_len =0 ; +int j; + +for (j =0 ; j < gi2c->num_msgs; j++) + max_len =max_t (size_t, max_len, msgs[j].len); + + timeout =geni_i2c_xfer_timeout (gi2c, max_len); ret =geni_i2c_gpi_multi_xfer_timeout_handler (gi2c->se.dev, gi2c_gpi_xfer, -XFER_TIMEOUT , &gi2c->done); + timeout, &gi2c->done);if (ret) { dev_err(gi2c->se.dev, "I2C multi write msg transfer timeout: %d\n",
@@ -851,8 +874,10 @@ static int geni_i2c_gpi_xfer(struct geni_i2c_dev *gi2c, struct i2c_msg msgs[], i }if (!gi2c->is_tx_multi_desc_xfer) { +unsigned long timeout =geni_i2c_xfer_timeout (gi2c, msgs[i].len); +dma_async_issue_pending (gi2c->tx_c); - time_left =wait_for_completion_timeout (&gi2c->done,XFER_TIMEOUT ); + time_left =wait_for_completion_timeout (&gi2c->done, timeout);if (!time_left) {dev_err (gi2c->se.dev,"%s:I2C timeout\n" , __func__); gi2c->err = -ETIMEDOUT ;
Signature · 1 line
2.34.1
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
New issues:
- [High] Dynamic timeout calculation causes an integer overflow on 32-bit architectures for large transfers, leading to premature timeouts.
- [High] Multi-descriptor transfer timeout calculation uses max_len instead of total transfer length, causing premature timeouts for batches.
- [Low] Mismatch between kernel-doc comment and function signature for geni_i2c_gpi_multi_xfer_timeout_handler.
Pre-existing issues:
- [High] Data race and missed wakeup in geni_i2c_gpi_multi_xfer_timeout_handler due to lockless reinit_completion without memory barriers. --
--- Patch [1]: [PATCH v3] i2c: qcom-geni: Add dynamic transfer timeout based on transfer length and frequency --- commit d129d3fb4c6f62c77ad085c820aa98f9027086bf
Author: Aniket Randive <[email protected]>
i2c: qcom-geni: Add dynamic transfer timeout based on transfer length and frequency
This commit replaces the static 1-second transfer timeout with a dynamically computed timeout based on message length and bus frequency. It aims to reduce unnecessary delays on error paths while adding a safety margin and a 300ms floor. For multi-descriptor transfers, it computes the per-completion timeout using the maximum message length in the batch.
Quoted 4 lines
drivers/i2c/busses/i2c-qcom-geni.c +0
-0
[ ... ]
drivers/i2c/busses/i2c-qcom-geni.c static int geni_i2c_clk_map_idx(struct geni_i2c_dev *gi2c) · line 213
Quoted 8 lines
drivers/i2c/busses/i2c-qcom-geni.c +4
-0@@ -204,6 +207,16 @@ static int geni_i2c_clk_map_idx(struct geni_i2c_dev *gi2c)return -EINVAL ; } +static unsigned long geni_i2c_xfer_timeout (struct geni_i2c_dev *gi2c,size_t len) +{ +size_t bit_cnt = len *9 ; +size_t bit_usec = (bit_cnt *USEC_PER_SEC ) / gi2c->clk_freq_out;
[Severity: High]
Can this calculation overflow on 32-bit architectures? Since size_t and USEC_PER_SEC are 32-bit values on these platforms, does a transfer length of 478 bytes or more silently wrap around and result in a prematurely small timeout?
Quoted 6 lines
drivers/i2c/busses/i2c-qcom-geni.c +6
-0+size_t xfer_max_usec = (bit_usec *I2C_TIMEOUT_SAFETY_COEFFICIENT ) + +I2C_TIMEOUT_MIN_USEC ; + +return usecs_to_jiffies (xfer_max_usec); +} +
[ ... ]
drivers/i2c/busses/i2c-qcom-geni.c static void geni_i2c_gpi_multi_desc_unmap(struct geni_i2c_dev *gi2c, struct i2c_ · line 609
Quoted 6 lines
drivers/i2c/busses/i2c-qcom-geni.c +1
-1@@ -591,7 +606,7 @@ static void geni_i2c_gpi_multi_desc_unmap(struct geni_i2c_dev *gi2c, struct i2c_* geni_i2c_gpi_multi_xfer_timeout_handler() - Handles multi message transfer timeout * @dev: Pointer to the corresponding dev node * @multi_xfer: Pointer to the geni_i2c_gpi_multi_desc_xfer -* @transfer_timeout_msecs: Timeout value in milliseconds +* @transfer_timeout_msecs: Per-message completion timeout in jiffies
[Severity: Low]
Is the kernel-doc parameter name mismatched here? The function signature below updates the parameter name to timeout_jiffies.
Quoted 13 lines
drivers/i2c/busses/i2c-qcom-geni.c +1
-1* @transfer_comp: Completion object of the transfer * * This function waits for the completion of each processed transfer messages @@ -601,7 +616,7 @@ static void geni_i2c_gpi_multi_desc_unmap(struct geni_i2c_dev *gi2c, struct i2c_*/ static int geni_i2c_gpi_multi_xfer_timeout_handler (struct device *dev,struct geni_i2c_gpi_multi_desc_xfer *multi_xfer, -u32 transfer_timeout_msecs, +unsigned long timeout_jiffies,struct completion *transfer_comp) {int i;
drivers/i2c/busses/i2c-qcom-geni.c static int geni_i2c_gpi_multi_xfer_timeout_handler(struct device *dev, · line 628
Quoted 2 lines
drivers/i2c/busses/i2c-qcom-geni.c +0
-0@@ -612,7 +627,7 @@ static int geni_i2c_gpi_multi_xfer_timeout_handler(struct device *dev,if (multi_xfer->msg_idx_cnt != multi_xfer->irq_cnt) {
[Severity: High]
This is a pre-existing issue, but does this sequence lack memory barriers? Could the lockless read of multi_xfer->irq_cnt be reordered before the store to done in the preceding reinit_completion(transfer_comp) call, potentially causing a missed wakeup and subsequent hang?
Quoted 8 lines
drivers/i2c/busses/i2c-qcom-geni.c +1
-1time_left =wait_for_completion_timeout (transfer_comp, - transfer_timeout_msecs); + timeout_jiffies);if (!time_left) {dev_err (dev,"%s: Transfer timeout\n" , __func__);return -ETIMEDOUT ;@@ -736,8 +751,16 @@ static int geni_i2c_gpi(struct geni_i2c_dev *gi2c, struct i2c_msg msgs[],dma_async_issue_pending (gi2c->tx_c);
drivers/i2c/busses/i2c-qcom-geni.c static int geni_i2c_gpi(struct geni_i2c_dev *gi2c, struct i2c_msg msgs[], · line 761
Quoted 10 lines
drivers/i2c/busses/i2c-qcom-geni.c +8
-0if ((msg_idx == (gi2c->num_msgs -1 )) || flags &DMA_PREP_INTERRUPT ) { +unsigned long timeout; +size_t max_len =0 ; +int j; + +for (j =0 ; j < gi2c->num_msgs; j++) + max_len =max_t (size_t, max_len, msgs[j].len); + + timeout =geni_i2c_xfer_timeout (gi2c, max_len);
[Severity: High]
Since the hardware takes time proportional to the sum of all message lengths, does calculating the timeout based only on max_len cause premature timeouts for batches containing many messages?
Quoted 6 lines
drivers/i2c/busses/i2c-qcom-geni.c +1
-1ret =geni_i2c_gpi_multi_xfer_timeout_handler (gi2c->se.dev, gi2c_gpi_xfer, -XFER_TIMEOUT , &gi2c->done); + timeout, &gi2c->done);if (ret) { dev_err(gi2c->se.dev, "I2C multi write msg transfer timeout: %d\n",
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1