Skip to contentSkip to conversation

i2c: qcom-geni: Add dynamic transfer timeout based on transfer length and frequency

PATCH Superseded Aniket Randive 4 participants 5 messages Jul 10 – Jul 15

Subsystems (4)

v4 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 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. Add a 300ms floor to budget for I2C clock stretching, where a slave may hold SCL low indefinitely during internal processing. This detects real hangs 3x faster than the old 1s static timeout.

For GPI multi-descriptor transfers, use the maximum message length across all queued messages as the per-completion timeout.

---

Changes in v4:
- As per konrad suggestion used mult_frac() for bit_usec to avoid intermediate overflow on 32-bit targets.
- Updated the commit message and added a driver comment explaining the rationale for the 0.3-second minimum timeout floor value.

 drivers/i2c/busses/i2c-qcom-geni.c | 46 +++++++++++++++++++++++-------
 1 file changed, 36 insertions(+), 10 deletions(-)
drivers/i2c/busses/i2c-qcom-geni.c +36 -10
index 96dbf04138be..c5c3adc8ec77 100644
@@ -74,9 +74,13 @@ 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 */
+#define I2C_TIMEOUT_SAFETY_COEFFICIENT	10
+/* 300ms floor: budget for clock stretching; slave may hold SCL low indefinitely */
+#define I2C_TIMEOUT_MIN_USEC		300000
+
 struct geni_i2c_desc {
 	bool no_dma_support;
 	unsigned int tx_fifo_depth;
@@ -204,6 +208,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 = mult_frac(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 +459,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 +484,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 +499,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 +527,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 +607,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 +617,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 +628,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 +752,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 +875,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
#1 lore raw
Thank 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] The mult_frac() macro used to calculate bit_usec suffers from a 32-bit intermediate integer overflow on 32-bit platforms, undermining the author's intent to prevent it.
- [High] The timeout calculated for multi-descriptor (GPI) transfers incorrectly uses the maximum individual message length rather than the sum of all queued message lengths, leading to premature timeouts for batches of small messages.

Pre-existing issues:
- [High] The dynamically allocated dma_buf and dma_addr arrays for multi-descriptor transfers are leaked permanently on early error paths.
- [High] Concurrent lockless calls to reinit_completion() during multi-descriptor transfers can overwrite pending completions, leading to lost wakeups and transfer timeouts. --

--- Patch [1]: [PATCH V4] i2c: qcom-geni: Add dynamic transfer timeout based on transfer length and frequency --- commit 6638d80f15522cd0e8e676c42f903f9ce0d7a55c
Author: Aniket Randive <[email protected]>
Subject: i2c: qcom-geni: Add dynamic transfer timeout based on transfer length and frequency

This commit introduces a dynamic timeout calculation for I2C transfers based on message length and bus frequency. It replaces a static 1-second timeout with a calculated timeout that includes a safety margin and a 300ms floor. For multi-descriptor transfers, it applies the maximum message length across the queued messages.

drivers/i2c/busses/i2c-qcom-geni.c static int geni_i2c_clk_map_idx(struct geni_i2c_dev *gi2c) · line 214

Quoted 12 lines
drivers/i2c/busses/i2c-qcom-geni.c +4 -0
index 96dbf04138bee..c5c3adc8ec771 100644
@@ -204,6 +208,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 = mult_frac(bit_cnt, USEC_PER_SEC, gi2c->clk_freq_out);

[Severity: High]
Does this mult_frac() calculation suffer from an intermediate integer overflow on 32-bit platforms?

The size_t type and USEC_PER_SEC are both 32-bit on 32-bit architectures. Because mult_frac(x, n, d) internally computes (x % d) * n / d, for a large transfer the remainder term multiplied by 1,000,000 can easily exceed U32_MAX and silently wrap. This would truncate the calculated timeout to a value less than the theoretical wire time, causing valid large transfers to spuriously time out.

Quoted 5 lines
drivers/i2c/busses/i2c-qcom-geni.c +5 -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 627

Quoted 13 lines
drivers/i2c/busses/i2c-qcom-geni.c +1 -1
@@ -601,13 +617,13 @@ 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;
 	u32 time_left;
 
 	for (i = 0; i < multi_xfer->msg_idx_cnt - 1; i++) {
 		reinit_completion(transfer_comp);

[Severity: High]
This is a pre-existing issue, but can this concurrent lockless call to reinit_completion() cause lost wakeups?

During multi-descriptor transfers, reinit_completion() is called locklessly in a loop here. Concurrently, the asynchronous DMA callback i2c_gpi_cb_result() can call complete(). Since reinit_completion() happens outside the waitqueue lock, the process thread might write done = 0 and then read a stale irq_cnt, causing it to evaluate msg_idx_cnt != irq_cnt as true and block on wait_for_completion_timeout() indefinitely.

Quoted 7 lines
drivers/i2c/busses/i2c-qcom-geni.c +1 -1
 		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;

[ ... ]

drivers/i2c/busses/i2c-qcom-geni.c static int geni_i2c_gpi(struct geni_i2c_dev *gi2c, struct i2c_msg msgs[], · line 762

Quoted 12 lines
drivers/i2c/busses/i2c-qcom-geni.c +8 -0
@@ -736,8 +752,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);

[Severity: High]
Will this timeout calculation cause spurious timeouts for batches of small messages?

When a multi-descriptor transfer is submitted, the process thread blocks on the first iteration of geni_i2c_gpi_multi_xfer_timeout_handler() for the entire batch duration. However, the timeout here is calculated using only the maximum individual message length instead of the sum of all queued message lengths. If a batch contains many small messages, the actual wire time can exceed this calculated timeout, leading to premature timeouts.

drivers/i2c/busses/i2c-qcom-geni.c static int geni_i2c_gpi(struct geni_i2c_dev *gi2c, struct i2c_msg msgs[], · line 766

Quoted 5 lines
drivers/i2c/busses/i2c-qcom-geni.c +2 -1
+			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,

[ ... ]

A related pre-existing issue exists in the unmap path:

drivers/i2c/busses/i2c-qcom-geni.c:geni_i2c_gpi_multi_desc_unmap() {
    ...
    if (tx_multi_xfer->unmap_msg_cnt == gi2c->num_msgs - 1) {
        kfree(tx_multi_xfer->dma_buf);
        kfree(tx_multi_xfer->dma_addr);
        break;
    }
    ...
}

[Severity: High]
This is a pre-existing issue, but does this logic permanently leak the dynamically allocated dma_buf and dma_addr arrays on early error paths?

If an error occurs or a timeout happens before all messages are successfully processed, the loop bounds will exit before unmap_msg_cnt reaches num_msgs - 1. This causes the kfree() block to be completely skipped. Since the structure is zeroed out on subsequent runs, the pointers are permanently lost, which can lead to memory exhaustion.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#2 lore raw
In reply to Aniket Randive

Hi Aniket, Thanks for addressing previous comments.

Quoted 8 lines · Aniket Randive

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. Add a 300ms floor to budget for I2C clock stretching, where a slave may hold SCL low indefinitely during internal processing. This detects real hangs

not only internal processing but it may go bad holding SCL low indefinitely.

Quoted 1 line

3x faster than the old 1s static timeout.

Meaning, in such case/scenario, don't need to wait till fixes timeout.
I guess, 3x faster is relative to the 1 sec, but for larger data and slower frequency it may not be 3x. Hence, correct it accordingly.

Quoted 9 lines

For GPI multi-descriptor transfers, use the maximum message length across all queued messages as the per-completion timeout.

---

Changes in v4:
- As per konrad suggestion used mult_frac() for bit_usec to avoid intermediate

Do not keep space before starting, directly start with "- As per....."

Quoted 12 lines

overflow on 32-bit targets.
- Updated the commit message and added a driver comment explaining the rationale for the 0.3-second minimum timeout floor value.

  drivers/i2c/busses/i2c-qcom-geni.c | 46 +++++++++++++++++++++++-------
  1 file changed, 36 insertions(+), 10 deletions(-)
drivers/i2c/busses/i2c-qcom-geni.c +0 -0
index 96dbf04138be..c5c3adc8ec77 100644

drivers/i2c/busses/i2c-qcom-geni.c enum geni_i2c_err_code { · line 80

Quoted 8 lines
drivers/i2c/busses/i2c-qcom-geni.c +2 -1
@@ -74,9 +74,13 @@ 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 */
+#define I2C_TIMEOUT_SAFETY_COEFFICIENT	10

Add a line space to make it look clean

drivers/i2c/busses/i2c-qcom-geni.c enum geni_i2c_err_code { · line 81

Quoted 1 line
drivers/i2c/busses/i2c-qcom-geni.c +1 -0
+/* 300ms floor: budget for clock stretching; slave may hold SCL low indefinitely */

Already explained in commit log, can remove second part.

Quoted 2 lines
drivers/i2c/busses/i2c-qcom-geni.c +2 -0
+#define I2C_TIMEOUT_MIN_USEC		300000
+

[...]

#3 lore raw
Quoted 21 lines · Mukesh Savaliya

Hi Aniket, Thanks for addressing previous comments.

Quoted 8 lines · Aniket Randive

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. Add a 300ms floor to budget for I2C clock stretching, where a slave may hold SCL low indefinitely during internal processing. This detects real hangs

not only internal processing but it may go bad holding SCL low indefinitely.

Quoted 1 line

3x faster than the old 1s static timeout.

Meaning, in such case/scenario, don't need to wait till fixes timeout.
I guess, 3x faster is relative to the 1 sec, but for larger data and slower frequency it may not be 3x. Hence, correct it accordingly.

Quoted 6 lines

For GPI multi-descriptor transfers, use the maximum message length across all queued messages as the per-completion timeout.

---

[...]

Quoted 2 lines
Quoted 1 line

+/* 300ms floor: budget for clock stretching; slave may hold SCL low indefinitely */

Already explained in commit log, can remove second part.

This is very much non-obvious, please keep it

Konrad

#4 lore raw
In reply to Mukesh Savaliya
Quoted 7 lines · Mukesh Savaliya

Hi Aniket, Thanks for addressing previous comments.

Quoted 4 lines · Aniket Randive

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.

Quoted 8 lines
Quoted 4 lines

Compute the timeout dynamically from message length and bus frequency with a 10x safety margin over the theoretical wire time. Add a 300ms floor to budget for I2C clock stretching, where a slave may hold SCL low indefinitely during internal processing. This detects real hangs

not only internal processing but it may go bad holding SCL low indefinitely.

Quoted 1 line

3x faster than the old 1s static timeout.

Meaning, in such case/scenario, don't need to wait till fixes timeout.
I guess, 3x faster is relative to the 1 sec, but for larger data and slower frequency it may not be 3x. Hence, correct it accordingly.

I will update the commit message in next patch.
Thanks,
Aniket

Quoted 10 lines
Quoted 9 lines

For GPI multi-descriptor transfers, use the maximum message length across all queued messages as the per-completion timeout.

---

Changes in v4:
  - As per konrad suggestion used mult_frac() for bit_usec to avoid intermediate

Do not keep space before starting, directly start with "- As per....."

Ok. I will take care of this in next patch.
Thanks,
Aniket

Quoted 26 lines
Quoted 12 lines

   overflow on 32-bit targets.
  - Updated the commit message and added a driver comment explaining the rationale for the 0.3-second minimum timeout floor value.

  drivers/i2c/busses/i2c-qcom-geni.c | 46 +++++++++++++++++++++++-------

  1 file changed, 36 insertions(+), 10 deletions(-)
drivers/i2c/busses/i2c-qcom-geni.c +0 -0
index 96dbf04138be..c5c3adc8ec77 100644

drivers/i2c/busses/i2c-qcom-geni.c enum geni_i2c_err_code { · line 78

Quoted 6 lines
drivers/i2c/busses/i2c-qcom-geni.c +2 -1
@@ -74,9 +74,13 @@ 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 */
+#define I2C_TIMEOUT_SAFETY_COEFFICIENT    10

Add a line space to make it look clean

Quoted 2 lines
drivers/i2c/busses/i2c-qcom-geni.c +1 -0
+/* 300ms floor: budget for clock stretching; slave may hold SCL low 

indefinitely */

Already explained in commit log, can remove second part.

Quoted 2 lines
drivers/i2c/busses/i2c-qcom-geni.c +2 -0
+#define I2C_TIMEOUT_MIN_USEC        300000
+

[...]