Skip to contentSkip to conversation

iio: imu: st_lsm6dsx: fix possible lockup during FIFO read

PATCH Superseded Silvano Seva 2 participants 6 messages Mar 3, 2025 – Mar 10, 2025

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: imu: st_lsm6dsx: fix possible lockup during FIFO read

Prevent st_lsm6dsx_read_fifo and st_lsm6dsx_read_tagged_fifo functions from falling in an infinite loop in case pattern_len is equal to zero and the device FIFO is not empty.

---
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c +8 -0
index 0a7cd8c1aa33..7f343614f8a5 100644
@@ -395,12 +395,17 @@ int st_lsm6dsx_read_fifo(struct st_lsm6dsx_hw *hw)
 	fifo_len = (le16_to_cpu(fifo_status) & fifo_diff_mask) *
 		   ST_LSM6DSX_CHAN_SIZE;
 	fifo_len = (fifo_len / pattern_len) * pattern_len;
+	if (!fifo_len)
+		return 0;
 
 	acc_sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_ACC]);
 	gyro_sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_GYRO]);
 	if (hw->iio_devs[ST_LSM6DSX_ID_EXT0])
 		ext_sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_EXT0]);
 
+	if (!pattern_len)
+		pattern_len = ST_LSM6DSX_SAMPLE_SIZE;
+
 	for (read_len = 0; read_len < fifo_len; read_len += pattern_len) {
 		err = st_lsm6dsx_read_block(hw, ST_LSM6DSX_REG_FIFO_OUTL_ADDR,
 					    hw->buff, pattern_len,
@@ -623,6 +628,9 @@ int st_lsm6dsx_read_tagged_fifo(struct st_lsm6dsx_hw *hw)
 	if (!fifo_len)
 		return 0;
 
+	if (!pattern_len)
+		pattern_len = ST_LSM6DSX_TAGGED_SAMPLE_SIZE;
+
 	for (read_len = 0; read_len < fifo_len; read_len += pattern_len) {
 		err = st_lsm6dsx_read_block(hw,
 					    ST_LSM6DSX_REG_FIFO_OUT_TAG_ADDR,
Signature · 1 line
2.48.1
maintainer
#1 lore raw
Quoted 14 lines

Prevent st_lsm6dsx_read_fifo and st_lsm6dsx_read_tagged_fifo functions from falling in an infinite loop in case pattern_len is equal to zero and the device FIFO is not empty.

---
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c +0 -0
index 0a7cd8c1aa33..7f343614f8a5 100644

drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c int st_lsm6dsx_read_fifo(struct st_lsm6dsx_hw *hw) · line 399

Quoted 5 lines
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c +2 -0
@@ -395,12 +395,17 @@ int st_lsm6dsx_read_fifo(struct st_lsm6dsx_hw *hw)
 	fifo_len = (le16_to_cpu(fifo_status) & fifo_diff_mask) *
 		   ST_LSM6DSX_CHAN_SIZE;
 	fifo_len = (fifo_len / pattern_len) * pattern_len;
+	if (!fifo_len)
+		return 0;

I do not think this check is necessary since if fifo_len is 0 we will not run the for loop, right?

drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c int st_lsm6dsx_read_fifo(struct st_lsm6dsx_hw *hw) · line 407

Quoted 8 lines
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c +2 -0
 	acc_sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_ACC]);
 	gyro_sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_GYRO]);
 	if (hw->iio_devs[ST_LSM6DSX_ID_EXT0])
 		ext_sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_EXT0]);
 
+	if (!pattern_len)
+		pattern_len = ST_LSM6DSX_SAMPLE_SIZE;

same here, I do not think pattern_len can be 0 since hw->sip must be greater than 0 in order to enable the FIFO. Moreover, this check should be some lines above since we have already divided fifo_len by pattern_len here.

drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c int st_lsm6dsx_read_tagged_fifo(struct st_lsm6dsx_hw *hw) · line 632

Quoted 10 lines
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c +3 -0
+
 	for (read_len = 0; read_len < fifo_len; read_len += pattern_len) {
 		err = st_lsm6dsx_read_block(hw, ST_LSM6DSX_REG_FIFO_OUTL_ADDR,
 					    hw->buff, pattern_len,
@@ -623,6 +628,9 @@ int st_lsm6dsx_read_tagged_fifo(struct st_lsm6dsx_hw *hw)
 	if (!fifo_len)
 		return 0;
 
+	if (!pattern_len)
+		pattern_len = ST_LSM6DSX_TAGGED_SAMPLE_SIZE;

for the reason above, this is not necessary.

Regards,
Lorenzo

Quoted 7 lines
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c +1 -0
+
 	for (read_len = 0; read_len < fifo_len; read_len += pattern_len) {
 		err = st_lsm6dsx_read_block(hw,
 					    ST_LSM6DSX_REG_FIFO_OUT_TAG_ADDR,
Signature · 1 line
2.48.1
#2 lore raw
Quoted 10 lines · Lorenzo Bianconi
Quoted 10 lines

Prevent st_lsm6dsx_read_fifo and st_lsm6dsx_read_tagged_fifo functions from falling in an infinite loop in case pattern_len is equal to zero and the device FIFO is not empty.

---
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c | 8 ++++++++
 1 file changed, 8 insertions(+)
Quoted 13 lines

drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c int st_lsm6dsx_read_fifo(struct st_lsm6dsx_hw *hw) · line 399

Quoted 10 lines
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c +2 -0
index 0a7cd8c1aa33..7f343614f8a5 100644
@@ -395,12 +395,17 @@ int st_lsm6dsx_read_fifo(struct st_lsm6dsx_hw *hw)
      fifo_len = (le16_to_cpu(fifo_status) & fifo_diff_mask) *
                 ST_LSM6DSX_CHAN_SIZE;
      fifo_len = (fifo_len / pattern_len) * pattern_len;
+     if (!fifo_len)
+             return 0;

I do not think this check is necessary since if fifo_len is 0 we will not run the for loop, right?

This check is present in the st_lsm6dsx_read_tagged_fifo() function, i added it here for consistency. I agree with you that is not strictly necessary.

Quoted 7 lines
Quoted 7 lines
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c +0 -0
      acc_sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_ACC]);
      gyro_sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_GYRO]);
      if (hw->iio_devs[ST_LSM6DSX_ID_EXT0])
              ext_sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_EXT0]);
 
Quoted 7 lines

drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c int st_lsm6dsx_read_fifo(struct st_lsm6dsx_hw *hw) · line 407

Quoted 2 lines
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c +2 -0
+     if (!pattern_len)
+             pattern_len = ST_LSM6DSX_SAMPLE_SIZE;

same here, I do not think pattern_len can be 0 since hw->sip must be greater than 0 in order to enable the FIFO. Moreover, this check should be some lines above since we have already divided fifo_len by pattern_len here.

There is a situation causing the subsequent for loop to never terminate, hanging the kernel boot process: given a system which doesn't have an hardware reset line allowing the kernel to re-initialize the IMU hardware, in case of an hot reboot the driver probe() function attempts to flush the FIFO, which may contain some data, while the hw->sip is zero.
The complete execution path is the following:
- call of st_lsm6dsx_probe();
- allocation of the st_lsm6dsx_hw structure via the devm_kzalloc, zero-initializing the sip field;
- call of st_lsm6dsx_init_device();
- call of st_lsm6dsx_reset_device();
- call of st_lsm6dsx_flush_fifo();
- call of st_lsm6dsx_read_fifo/st_lsm6dsx_read_tagged_fifo via the fifo_ops function pointer.

An alternative solution to solve this situation is initializing the hw->sip field to a sane default value in either the probe() or init_device() function.

Quoted 23 lines

drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c int st_lsm6dsx_read_tagged_fifo(struct st_lsm6dsx_hw *hw) · line 632

Quoted 10 lines
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c +3 -0
+
      for (read_len = 0; read_len < fifo_len; read_len += pattern_len) {
              err = st_lsm6dsx_read_block(hw, ST_LSM6DSX_REG_FIFO_OUTL_ADDR,
                                          hw->buff, pattern_len,
@@ -623,6 +628,9 @@ int st_lsm6dsx_read_tagged_fifo(struct st_lsm6dsx_hw *hw)
      if (!fifo_len)
              return 0;
 
+     if (!pattern_len)
+             pattern_len = ST_LSM6DSX_TAGGED_SAMPLE_SIZE;

for the reason above, this is not necessary.

Regards,
Lorenzo

Quoted 7 lines
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c +1 -1
+
      for (read_len = 0; read_len < fifo_len; read_len += pattern_len) {
              err = st_lsm6dsx_read_block(hw,
                                          ST_LSM6DSX_REG_FIFO_OUT_TAG_ADDR,
--

2.48.1

maintainer
#3 lore raw
Quoted 44 lines · Lorenzo Bianconi
Quoted 10 lines
Quoted 10 lines

Prevent st_lsm6dsx_read_fifo and st_lsm6dsx_read_tagged_fifo functions from falling in an infinite loop in case pattern_len is equal to zero and the device FIFO is not empty.

---
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c | 8 ++++++++
 1 file changed, 8 insertions(+)
Quoted 13 lines

drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c int st_lsm6dsx_read_fifo(struct st_lsm6dsx_hw *hw) · line 399

Quoted 10 lines
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c +2 -0
index 0a7cd8c1aa33..7f343614f8a5 100644
@@ -395,12 +395,17 @@ int st_lsm6dsx_read_fifo(struct st_lsm6dsx_hw *hw)
      fifo_len = (le16_to_cpu(fifo_status) & fifo_diff_mask) *
                 ST_LSM6DSX_CHAN_SIZE;
      fifo_len = (fifo_len / pattern_len) * pattern_len;
+     if (!fifo_len)
+             return 0;

I do not think this check is necessary since if fifo_len is 0 we will not run the for loop, right?

This check is present in the st_lsm6dsx_read_tagged_fifo() function, i added it here for consistency. I agree with you that is not strictly necessary.

Quoted 7 lines
Quoted 7 lines
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c +0 -0
      acc_sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_ACC]);
      gyro_sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_GYRO]);
      if (hw->iio_devs[ST_LSM6DSX_ID_EXT0])
              ext_sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_EXT0]);
 
Quoted 7 lines

drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c int st_lsm6dsx_read_fifo(struct st_lsm6dsx_hw *hw) · line 407

Quoted 2 lines
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c +2 -0
+     if (!pattern_len)
+             pattern_len = ST_LSM6DSX_SAMPLE_SIZE;

same here, I do not think pattern_len can be 0 since hw->sip must be greater than 0 in order to enable the FIFO. Moreover, this check should be some lines above since we have already divided fifo_len by pattern_len here.

Quoted 15 lines

There is a situation causing the subsequent for loop to never terminate, hanging the kernel boot process: given a system which doesn't have an hardware reset line allowing the kernel to re-initialize the IMU hardware, in case of an hot reboot the driver probe() function attempts to flush the FIFO, which may contain some data, while the hw->sip is zero.
The complete execution path is the following:
- call of st_lsm6dsx_probe();
- allocation of the st_lsm6dsx_hw structure via the devm_kzalloc, zero-initializing the sip field;
- call of st_lsm6dsx_init_device();
- call of st_lsm6dsx_reset_device();
- call of st_lsm6dsx_flush_fifo();
- call of st_lsm6dsx_read_fifo/st_lsm6dsx_read_tagged_fifo via the fifo_ops function pointer.

ack, I can see the issue now, thx for pointing this out. I should we should set pattern_len to ST_LSM6DSX_SAMPLE_SIZE or ST_LSM6DSX_TAGGED_SAMPLE_SIZE if it is
0. Can you please move the check before updating fifo_len in st_lsm6dsx_read_fifo()?

Regards,
Lorenzo

Quoted 28 lines

An alternative solution to solve this situation is initializing the hw->sip field to a sane default value in either the probe() or init_device() function.

Quoted 23 lines

drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c int st_lsm6dsx_read_tagged_fifo(struct st_lsm6dsx_hw *hw) · line 632

Quoted 10 lines
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c +3 -0
+
      for (read_len = 0; read_len < fifo_len; read_len += pattern_len) {
              err = st_lsm6dsx_read_block(hw, ST_LSM6DSX_REG_FIFO_OUTL_ADDR,
                                          hw->buff, pattern_len,
@@ -623,6 +628,9 @@ int st_lsm6dsx_read_tagged_fifo(struct st_lsm6dsx_hw *hw)
      if (!fifo_len)
              return 0;
 
+     if (!pattern_len)
+             pattern_len = ST_LSM6DSX_TAGGED_SAMPLE_SIZE;

for the reason above, this is not necessary.

Regards,
Lorenzo

Quoted 7 lines
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c +1 -1
+
      for (read_len = 0; read_len < fifo_len; read_len += pattern_len) {
              err = st_lsm6dsx_read_block(hw,
                                          ST_LSM6DSX_REG_FIFO_OUT_TAG_ADDR,
--

2.48.1

#4 lore raw

Prevent st_lsm6dsx_read_fifo and st_lsm6dsx_read_tagged_fifo functions from falling in an infinite loop in case pattern_len is equal to zero and the device FIFO is not empty.

---
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c +8 -0
index 0a7cd8c1aa33..39a7e15ae9ae 100644
@@ -392,9 +392,14 @@ int st_lsm6dsx_read_fifo(struct st_lsm6dsx_hw *hw)
 	if (fifo_status & cpu_to_le16(ST_LSM6DSX_FIFO_EMPTY_MASK))
 		return 0;
 
+	if (!pattern_len)
+		pattern_len = ST_LSM6DSX_SAMPLE_SIZE;
+
 	fifo_len = (le16_to_cpu(fifo_status) & fifo_diff_mask) *
 		   ST_LSM6DSX_CHAN_SIZE;
 	fifo_len = (fifo_len / pattern_len) * pattern_len;
+	if (!fifo_len)
+		return 0;
 
 	acc_sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_ACC]);
 	gyro_sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_GYRO]);
@@ -623,6 +628,9 @@ int st_lsm6dsx_read_tagged_fifo(struct st_lsm6dsx_hw *hw)
 	if (!fifo_len)
 		return 0;
 
+	if (!pattern_len)
+		pattern_len = ST_LSM6DSX_TAGGED_SAMPLE_SIZE;
+
 	for (read_len = 0; read_len < fifo_len; read_len += pattern_len) {
 		err = st_lsm6dsx_read_block(hw,
 					    ST_LSM6DSX_REG_FIFO_OUT_TAG_ADDR,
Signature · 1 line
2.48.1
#5 lore raw
In reply to Lorenzo Bianconi
Quoted 61 lines · Lorenzo Bianconi
Quoted 45 lines · Lorenzo Bianconi
Quoted 10 lines
Quoted 10 lines

Prevent st_lsm6dsx_read_fifo and st_lsm6dsx_read_tagged_fifo functions from falling in an infinite loop in case pattern_len is equal to zero and the device FIFO is not empty.

---
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c | 8 ++++++++
 1 file changed, 8 insertions(+)
Quoted 13 lines

drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c int st_lsm6dsx_read_fifo(struct st_lsm6dsx_hw *hw) · line 399

Quoted 10 lines
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c +2 -0
index 0a7cd8c1aa33..7f343614f8a5 100644
@@ -395,12 +395,17 @@ int st_lsm6dsx_read_fifo(struct st_lsm6dsx_hw *hw)
      fifo_len = (le16_to_cpu(fifo_status) & fifo_diff_mask) *
                 ST_LSM6DSX_CHAN_SIZE;
      fifo_len = (fifo_len / pattern_len) * pattern_len;
+     if (!fifo_len)
+             return 0;

I do not think this check is necessary since if fifo_len is 0 we will not run the for loop, right?

This check is present in the st_lsm6dsx_read_tagged_fifo() function, i added it here for consistency. I agree with you that is not strictly necessary.

Quoted 7 lines
Quoted 7 lines
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c +0 -0
      acc_sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_ACC]);
      gyro_sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_GYRO]);
      if (hw->iio_devs[ST_LSM6DSX_ID_EXT0])
              ext_sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_EXT0]);
 
Quoted 7 lines

drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c int st_lsm6dsx_read_fifo(struct st_lsm6dsx_hw *hw) · line 407

Quoted 2 lines
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c +2 -0
+     if (!pattern_len)
+             pattern_len = ST_LSM6DSX_SAMPLE_SIZE;

same here, I do not think pattern_len can be 0 since hw->sip must be greater than 0 in order to enable the FIFO. Moreover, this check should be some lines above since we have already divided fifo_len by pattern_len here.

Quoted 15 lines

There is a situation causing the subsequent for loop to never terminate, hanging the kernel boot process: given a system which doesn't have an hardware reset line allowing the kernel to re-initialize the IMU hardware, in case of an hot reboot the driver probe() function attempts to flush the FIFO, which may contain some data, while the hw->sip is zero.
The complete execution path is the following:
- call of st_lsm6dsx_probe();
- allocation of the st_lsm6dsx_hw structure via the devm_kzalloc, zero-initializing the sip field;
- call of st_lsm6dsx_init_device();
- call of st_lsm6dsx_reset_device();
- call of st_lsm6dsx_flush_fifo();
- call of st_lsm6dsx_read_fifo/st_lsm6dsx_read_tagged_fifo via the fifo_ops function pointer.

Quoted 8 lines

ack, I can see the issue now, thx for pointing this out. I should we should set pattern_len to ST_LSM6DSX_SAMPLE_SIZE or ST_LSM6DSX_TAGGED_SAMPLE_SIZE if it is
0. Can you please move the check before updating fifo_len in st_lsm6dsx_read_fifo()?

Regards,
Lorenzo

Changes done, I sent the updated patch in a separate reply email: I hope that doing so didn't cause any trouble.

Thank you,
Silvano.