[PATCH] iio: imu: st_lsm6dsx: Fix check for invalid samples from FIFO

Francesco Lavra posted 1 patch 2 weeks, 3 days ago
drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
[PATCH] iio: imu: st_lsm6dsx: Fix check for invalid samples from FIFO
Posted by Francesco Lavra 2 weeks, 3 days ago
The DRDY_MASK feature implemented in sensor chips marks gyroscope and
accelerometer invalid samples (i.e. samples that have been acquired during
the settling time of sensor filters) with the special values 0x7FFFh,
0x7FFE, and 0x7FFD.
The driver checks FIFO samples against these special values in order to
discard invalid samples; however, it does the check regardless of the type
of samples being processed, whereas this feature is specific to gyroscope
and accelerometer data. This could cause valid samples to be discarded.
Fix the above check so that it takes into account the type of samples being
processed.

Fixes: 960506ed2c69 ("iio: imu: st_lsm6dsx: enable drdy-mask if available")
Signed-off-by: Francesco Lavra <flavra@baylibre.com>
---
 drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c
index 5b28a3ffcc3d..4cfd5046f98e 100644
--- a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c
+++ b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_buffer.c
@@ -541,12 +541,12 @@ static int
 st_lsm6dsx_push_tagged_data(struct st_lsm6dsx_hw *hw, u8 tag,
 			    u8 *data, s64 ts)
 {
-	s16 val = le16_to_cpu(*(__le16 *)data);
 	struct st_lsm6dsx_sensor *sensor;
 	struct iio_dev *iio_dev;
 
 	/* invalid sample during bootstrap phase */
-	if (val >= ST_LSM6DSX_INVALID_SAMPLE)
+	if ((tag == ST_LSM6DSX_GYRO_TAG || tag == ST_LSM6DSX_ACC_TAG) &&
+	    (s16)le16_to_cpup((__le16 *)data) >= ST_LSM6DSX_INVALID_SAMPLE)
 		return -EINVAL;
 
 	/*
-- 
2.39.5
Re: [PATCH] iio: imu: st_lsm6dsx: Fix check for invalid samples from FIFO
Posted by Andy Shevchenko 2 weeks, 3 days ago
On Wed, Jan 21, 2026 at 12:27:57PM +0100, Francesco Lavra wrote:
> The DRDY_MASK feature implemented in sensor chips marks gyroscope and
> accelerometer invalid samples (i.e. samples that have been acquired during
> the settling time of sensor filters) with the special values 0x7FFFh,
> 0x7FFE, and 0x7FFD.
> The driver checks FIFO samples against these special values in order to
> discard invalid samples; however, it does the check regardless of the type
> of samples being processed, whereas this feature is specific to gyroscope
> and accelerometer data. This could cause valid samples to be discarded.
> Fix the above check so that it takes into account the type of samples being
> processed.

...

>  st_lsm6dsx_push_tagged_data(struct st_lsm6dsx_hw *hw, u8 tag,
>  			    u8 *data, s64 ts)
>  {
> -	s16 val = le16_to_cpu(*(__le16 *)data);
>  	struct st_lsm6dsx_sensor *sensor;
>  	struct iio_dev *iio_dev;
>  
>  	/* invalid sample during bootstrap phase */
> -	if (val >= ST_LSM6DSX_INVALID_SAMPLE)
> +	if ((tag == ST_LSM6DSX_GYRO_TAG || tag == ST_LSM6DSX_ACC_TAG) &&
> +	    (s16)le16_to_cpup((__le16 *)data) >= ST_LSM6DSX_INVALID_SAMPLE)

Since data is u8 *, it might appear on unaligned addresses and the proper
conversion here is to use get_unaligned_le16() without any of those ugly
castings.

>  		return -EINVAL;

-- 
With Best Regards,
Andy Shevchenko
Re: [PATCH] iio: imu: st_lsm6dsx: Fix check for invalid samples from FIFO
Posted by Francesco Lavra 2 weeks, 3 days ago
On Wed, 2026-01-21 at 14:47 +0200, Andy Shevchenko wrote:
> On Wed, Jan 21, 2026 at 12:27:57PM +0100, Francesco Lavra wrote:
> > The DRDY_MASK feature implemented in sensor chips marks gyroscope and
> > accelerometer invalid samples (i.e. samples that have been acquired
> > during
> > the settling time of sensor filters) with the special values 0x7FFFh,
> > 0x7FFE, and 0x7FFD.
> > The driver checks FIFO samples against these special values in order to
> > discard invalid samples; however, it does the check regardless of the
> > type
> > of samples being processed, whereas this feature is specific to
> > gyroscope
> > and accelerometer data. This could cause valid samples to be discarded.
> > Fix the above check so that it takes into account the type of samples
> > being
> > processed.
> 
> ...
> 
> >  st_lsm6dsx_push_tagged_data(struct st_lsm6dsx_hw *hw, u8 tag,
> >                             u8 *data, s64 ts)
> >  {
> > -       s16 val = le16_to_cpu(*(__le16 *)data);
> >         struct st_lsm6dsx_sensor *sensor;
> >         struct iio_dev *iio_dev;
> >  
> >         /* invalid sample during bootstrap phase */
> > -       if (val >= ST_LSM6DSX_INVALID_SAMPLE)
> > +       if ((tag == ST_LSM6DSX_GYRO_TAG || tag == ST_LSM6DSX_ACC_TAG)
> > &&
> > +           (s16)le16_to_cpup((__le16 *)data) >=
> > ST_LSM6DSX_INVALID_SAMPLE)
> 
> Since data is u8 *, it might appear on unaligned addresses and the proper
> conversion here is to use get_unaligned_le16() without any of those ugly
> castings.

data here corresponds to iio_buff in st_lsm6dsx_read_tagged_fifo(), which
is declared as __aligned(8), so it cannot appear on unaligned addresses.

> >                 return -EINVAL;
> 
Re: [PATCH] iio: imu: st_lsm6dsx: Fix check for invalid samples from FIFO
Posted by Andy Shevchenko 2 weeks, 3 days ago
On Wed, Jan 21, 2026 at 03:05:54PM +0100, Francesco Lavra wrote:
> On Wed, 2026-01-21 at 14:47 +0200, Andy Shevchenko wrote:
> > On Wed, Jan 21, 2026 at 12:27:57PM +0100, Francesco Lavra wrote:

...

> > >  st_lsm6dsx_push_tagged_data(struct st_lsm6dsx_hw *hw, u8 tag,
> > >                             u8 *data, s64 ts)
> > >  {
> > > -       s16 val = le16_to_cpu(*(__le16 *)data);
> > >         struct st_lsm6dsx_sensor *sensor;
> > >         struct iio_dev *iio_dev;
> > >  
> > >         /* invalid sample during bootstrap phase */
> > > -       if (val >= ST_LSM6DSX_INVALID_SAMPLE)
> > > +       if ((tag == ST_LSM6DSX_GYRO_TAG || tag == ST_LSM6DSX_ACC_TAG)
> > > &&
> > > +           (s16)le16_to_cpup((__le16 *)data) >=
> > > ST_LSM6DSX_INVALID_SAMPLE)
> > 
> > Since data is u8 *, it might appear on unaligned addresses and the proper
> > conversion here is to use get_unaligned_le16() without any of those ugly
> > castings.
> 
> data here corresponds to iio_buff in st_lsm6dsx_read_tagged_fifo(), which
> is declared as __aligned(8), so it cannot appear on unaligned addresses.

You know that, compiler might not. If you sure, you should change u8 *data
to be __le16 *data in the function parameter. Until it's u8 *, the
get_unaligned_le16() is the error free thing to achieve that.

> > >                 return -EINVAL;

-- 
With Best Regards,
Andy Shevchenko
Re: [PATCH] iio: imu: st_lsm6dsx: Fix check for invalid samples from FIFO
Posted by Andy Shevchenko 2 weeks, 3 days ago
On Wed, Jan 21, 2026 at 02:47:40PM +0200, Andy Shevchenko wrote:
> On Wed, Jan 21, 2026 at 12:27:57PM +0100, Francesco Lavra wrote:

...

> >  st_lsm6dsx_push_tagged_data(struct st_lsm6dsx_hw *hw, u8 tag,
> >  			    u8 *data, s64 ts)
> >  {
> > -	s16 val = le16_to_cpu(*(__le16 *)data);
> >  	struct st_lsm6dsx_sensor *sensor;
> >  	struct iio_dev *iio_dev;
> >  
> >  	/* invalid sample during bootstrap phase */
> > -	if (val >= ST_LSM6DSX_INVALID_SAMPLE)
> > +	if ((tag == ST_LSM6DSX_GYRO_TAG || tag == ST_LSM6DSX_ACC_TAG) &&
> > +	    (s16)le16_to_cpup((__le16 *)data) >= ST_LSM6DSX_INVALID_SAMPLE)
> 
> Since data is u8 *, it might appear on unaligned addresses and the proper
> conversion here is to use get_unaligned_le16() without any of those ugly
> castings.

Okay, (s16) seems needed or another way to allow 0x8000 and higher values.

> >  		return -EINVAL;

-- 
With Best Regards,
Andy Shevchenko