As it was reported and discussed here [1], storing the sensor data in an
endian aware s32 buffer is not optimal. Advertising the timestamp as an
addition of 2 s32 variables which is also implied is again not the best
practice. For that reason, change the s32 sensor_data buffer to a char
buffer with an extra value for the timestamp (as it is common practice).
[1]: https://lore.kernel.org/linux-iio/73d13cc0-afb9-4306-b498-5d821728c3ba@stanley.mountain/
Signed-off-by: Vasileios Amoiridis <vassilisamir@gmail.com>
---
drivers/iio/pressure/bmp280-core.c | 78 ++++++++++++++++++------------
drivers/iio/pressure/bmp280.h | 5 +-
2 files changed, 51 insertions(+), 32 deletions(-)
diff --git a/drivers/iio/pressure/bmp280-core.c b/drivers/iio/pressure/bmp280-core.c
index 472a6696303b..2c62490a40c6 100644
--- a/drivers/iio/pressure/bmp280-core.c
+++ b/drivers/iio/pressure/bmp280-core.c
@@ -1023,9 +1023,9 @@ static irqreturn_t bmp280_trigger_handler(int irq, void *p)
struct iio_poll_func *pf = p;
struct iio_dev *indio_dev = pf->indio_dev;
struct bmp280_data *data = iio_priv(indio_dev);
- u32 adc_temp, adc_press;
- s32 t_fine;
- int ret;
+ u32 adc_temp, adc_press, comp_press;
+ s32 t_fine, comp_temp;
+ int ret, offset;
guard(mutex)(&data->lock);
@@ -1044,7 +1044,7 @@ static irqreturn_t bmp280_trigger_handler(int irq, void *p)
goto out;
}
- data->sensor_data[1] = bmp280_compensate_temp(data, adc_temp);
+ comp_temp = bmp280_compensate_temp(data, adc_temp);
/* Pressure calculations */
adc_press = FIELD_GET(BMP280_MEAS_TRIM_MASK, get_unaligned_be24(&data->buf[0]));
@@ -1054,10 +1054,13 @@ static irqreturn_t bmp280_trigger_handler(int irq, void *p)
}
t_fine = bmp280_calc_t_fine(data, adc_temp);
+ comp_press = bmp280_compensate_press(data, adc_press, t_fine);
- data->sensor_data[0] = bmp280_compensate_press(data, adc_press, t_fine);
+ memcpy(&data->buffer.buf[offset], &comp_press, sizeof(s32));
+ offset += sizeof(s32);
+ memcpy(&data->buffer.buf[offset], &comp_temp, sizeof(s32));
- iio_push_to_buffers_with_timestamp(indio_dev, &data->sensor_data,
+ iio_push_to_buffers_with_timestamp(indio_dev, &data->buffer,
iio_get_time_ns(indio_dev));
out:
@@ -1138,9 +1141,9 @@ static irqreturn_t bme280_trigger_handler(int irq, void *p)
struct iio_poll_func *pf = p;
struct iio_dev *indio_dev = pf->indio_dev;
struct bmp280_data *data = iio_priv(indio_dev);
- u32 adc_temp, adc_press, adc_humidity;
- s32 t_fine;
- int ret;
+ u32 adc_temp, adc_press, adc_humidity, comp_press, comp_humidity;
+ s32 t_fine, comp_temp;
+ int ret, offset;
guard(mutex)(&data->lock);
@@ -1159,7 +1162,7 @@ static irqreturn_t bme280_trigger_handler(int irq, void *p)
goto out;
}
- data->sensor_data[1] = bmp280_compensate_temp(data, adc_temp);
+ comp_temp = bmp280_compensate_temp(data, adc_temp);
/* Pressure calculations */
adc_press = FIELD_GET(BMP280_MEAS_TRIM_MASK, get_unaligned_be24(&data->buf[0]));
@@ -1169,8 +1172,7 @@ static irqreturn_t bme280_trigger_handler(int irq, void *p)
}
t_fine = bmp280_calc_t_fine(data, adc_temp);
-
- data->sensor_data[0] = bmp280_compensate_press(data, adc_press, t_fine);
+ comp_press = bmp280_compensate_press(data, adc_press, t_fine);
/* Humidity calculations */
adc_humidity = get_unaligned_be16(&data->buf[6]);
@@ -1179,9 +1181,16 @@ static irqreturn_t bme280_trigger_handler(int irq, void *p)
dev_err(data->dev, "reading humidity skipped\n");
goto out;
}
- data->sensor_data[2] = bme280_compensate_humidity(data, adc_humidity, t_fine);
- iio_push_to_buffers_with_timestamp(indio_dev, &data->sensor_data,
+ comp_humidity = bme280_compensate_humidity(data, adc_humidity, t_fine);
+
+ memcpy(&data->buffer.buf[offset], &comp_press, sizeof(s32));
+ offset += sizeof(s32);
+ memcpy(&data->buffer.buf[offset], &comp_temp, sizeof(s32));
+ offset += sizeof(s32);
+ memcpy(&data->buffer.buf[offset], &comp_humidity, sizeof(s32));
+
+ iio_push_to_buffers_with_timestamp(indio_dev, &data->buffer,
iio_get_time_ns(indio_dev));
out:
@@ -1618,9 +1627,9 @@ static irqreturn_t bmp380_trigger_handler(int irq, void *p)
struct iio_poll_func *pf = p;
struct iio_dev *indio_dev = pf->indio_dev;
struct bmp280_data *data = iio_priv(indio_dev);
- u32 adc_temp, adc_press;
- s32 t_fine;
- int ret;
+ u32 adc_temp, adc_press, comp_press;
+ s32 t_fine, comp_temp;
+ int ret, offset;
guard(mutex)(&data->lock);
@@ -1639,7 +1648,7 @@ static irqreturn_t bmp380_trigger_handler(int irq, void *p)
goto out;
}
- data->sensor_data[1] = bmp380_compensate_temp(data, adc_temp);
+ comp_temp = bmp380_compensate_temp(data, adc_temp);
/* Pressure calculations */
adc_press = get_unaligned_le24(&data->buf[0]);
@@ -1649,10 +1658,13 @@ static irqreturn_t bmp380_trigger_handler(int irq, void *p)
}
t_fine = bmp380_calc_t_fine(data, adc_temp);
+ comp_press = bmp380_compensate_press(data, adc_press, t_fine);
- data->sensor_data[0] = bmp380_compensate_press(data, adc_press, t_fine);
+ memcpy(&data->buffer.buf[offset], &comp_press, sizeof(s32));
+ offset += sizeof(s32);
+ memcpy(&data->buffer.buf[offset], &comp_temp, sizeof(s32));
- iio_push_to_buffers_with_timestamp(indio_dev, &data->sensor_data,
+ iio_push_to_buffers_with_timestamp(indio_dev, &data->buffer,
iio_get_time_ns(indio_dev));
out:
@@ -2199,7 +2211,7 @@ static irqreturn_t bmp580_trigger_handler(int irq, void *p)
struct iio_poll_func *pf = p;
struct iio_dev *indio_dev = pf->indio_dev;
struct bmp280_data *data = iio_priv(indio_dev);
- int ret;
+ int ret, offset;
guard(mutex)(&data->lock);
@@ -2211,13 +2223,16 @@ static irqreturn_t bmp580_trigger_handler(int irq, void *p)
goto out;
}
+ /* Pressure calculations */
+ memcpy(&data->buffer.buf[offset], &data->buf[3], 3);
+
+ offset += sizeof(s32);
+
/* Temperature calculations */
- memcpy(&data->sensor_data[1], &data->buf[0], 3);
+ memcpy(&data->buffer.buf[offset], &data->buf[0], 3);
- /* Pressure calculations */
- memcpy(&data->sensor_data[0], &data->buf[3], 3);
- iio_push_to_buffers_with_timestamp(indio_dev, &data->sensor_data,
+ iio_push_to_buffers_with_timestamp(indio_dev, &data->buffer,
iio_get_time_ns(indio_dev));
out:
@@ -2523,23 +2538,24 @@ static irqreturn_t bmp180_trigger_handler(int irq, void *p)
struct iio_poll_func *pf = p;
struct iio_dev *indio_dev = pf->indio_dev;
struct bmp280_data *data = iio_priv(indio_dev);
- int ret, chan_value;
+ int ret, comp_temp, comp_press, offset;
guard(mutex)(&data->lock);
- ret = bmp180_read_temp(data, &chan_value);
+ ret = bmp180_read_temp(data, &comp_temp);
if (ret)
goto out;
- data->sensor_data[1] = chan_value;
- ret = bmp180_read_press(data, &chan_value);
+ ret = bmp180_read_press(data, &comp_press);
if (ret)
goto out;
- data->sensor_data[0] = chan_value;
+ memcpy(&data->buffer.buf[offset], &comp_press, sizeof(s32));
+ offset += sizeof(s32);
+ memcpy(&data->buffer.buf[offset], &comp_temp, sizeof(s32));
- iio_push_to_buffers_with_timestamp(indio_dev, &data->sensor_data,
+ iio_push_to_buffers_with_timestamp(indio_dev, &data->buffer,
iio_get_time_ns(indio_dev));
out:
diff --git a/drivers/iio/pressure/bmp280.h b/drivers/iio/pressure/bmp280.h
index a9f220c1f77a..b0c26f55c6af 100644
--- a/drivers/iio/pressure/bmp280.h
+++ b/drivers/iio/pressure/bmp280.h
@@ -419,7 +419,10 @@ struct bmp280_data {
* Data to push to userspace triggered buffer. Up to 3 channels and
* s64 timestamp, aligned.
*/
- s32 sensor_data[6] __aligned(8);
+ struct {
+ u8 buf[12];
+ aligned_s64 ts;
+ } buffer;
/*
* DMA (thus cache coherency maintenance) may require the
--
2.25.1
On Sun, 29 Sep 2024 13:25:11 +0200 Vasileios Amoiridis <vassilisamir@gmail.com> wrote: > As it was reported and discussed here [1], storing the sensor data in an > endian aware s32 buffer is not optimal. Advertising the timestamp as an > addition of 2 s32 variables which is also implied is again not the best > practice. For that reason, change the s32 sensor_data buffer to a char > buffer with an extra value for the timestamp (as it is common practice). > > [1]: https://lore.kernel.org/linux-iio/73d13cc0-afb9-4306-b498-5d821728c3ba@stanley.mountain/ > > Signed-off-by: Vasileios Amoiridis <vassilisamir@gmail.com> Hi Vasileois. I missed a purely semantic issue in previous versions :( A few other places where you can achieve the same effect with less code and clear casting to correct types. Jonathan > --- > drivers/iio/pressure/bmp280-core.c | 78 ++++++++++++++++++------------ > drivers/iio/pressure/bmp280.h | 5 +- > 2 files changed, 51 insertions(+), 32 deletions(-) > > diff --git a/drivers/iio/pressure/bmp280-core.c b/drivers/iio/pressure/bmp280-core.c > index 472a6696303b..2c62490a40c6 100644 > --- a/drivers/iio/pressure/bmp280-core.c > +++ b/drivers/iio/pressure/bmp280-core.c > @@ -2523,23 +2538,24 @@ static irqreturn_t bmp180_trigger_handler(int irq, void *p) > struct iio_poll_func *pf = p; > struct iio_dev *indio_dev = pf->indio_dev; > struct bmp280_data *data = iio_priv(indio_dev); > - int ret, chan_value; > + int ret, comp_temp, comp_press, offset; > > guard(mutex)(&data->lock); > > - ret = bmp180_read_temp(data, &chan_value); > + ret = bmp180_read_temp(data, &comp_temp); > if (ret) > goto out; > > - data->sensor_data[1] = chan_value; > > - ret = bmp180_read_press(data, &chan_value); > + ret = bmp180_read_press(data, &comp_press); > if (ret) > goto out; > > - data->sensor_data[0] = chan_value; > + memcpy(&data->buffer.buf[offset], &comp_press, sizeof(s32)); > + offset += sizeof(s32); > + memcpy(&data->buffer.buf[offset], &comp_temp, sizeof(s32)); I suppose there is a consistency argument for using memcpy even for the s32 cases but you 'could' if you like do s32 *chans = (s32 *)data->buffer.buf; at top and chans[0] = comp_press; chans[1] = comp_temp; here, which is functionally equivalent, particularly as we are forcing the buffer alignment to be larger than this s32. Similar for the other simple native endian s32 cases. The memcpy is needed for the le24 one. > > - iio_push_to_buffers_with_timestamp(indio_dev, &data->sensor_data, > + iio_push_to_buffers_with_timestamp(indio_dev, &data->buffer, > iio_get_time_ns(indio_dev)); > > out: > diff --git a/drivers/iio/pressure/bmp280.h b/drivers/iio/pressure/bmp280.h > index a9f220c1f77a..b0c26f55c6af 100644 > --- a/drivers/iio/pressure/bmp280.h > +++ b/drivers/iio/pressure/bmp280.h > @@ -419,7 +419,10 @@ struct bmp280_data { > * Data to push to userspace triggered buffer. Up to 3 channels and > * s64 timestamp, aligned. > */ > - s32 sensor_data[6] __aligned(8); > + struct { > + u8 buf[12]; > + aligned_s64 ts; I'd missed that this depends on the number of channels. It makes no functional difference because the core code will happily write over the end of buf, but from a representation point of view this might be u8 buf[8]; aligned_s64 ts; or u8 buf[12]; aligned_s64 ts; So given we can't actually fix on one or the other normal convention is to just use something that forces a large enough aligned u8 buffer like u8 buf[ALIGN(sizeof(s32) * BMP280_MAX_CHANNELS, 8) + sizeof(s64)] __aligned(sizeof(s64)); Sorry for leading you astray on this! Jonathan > + } buffer; > > /* > * DMA (thus cache coherency maintenance) may require the
On Sun, Sep 29, 2024 at 06:10:03PM +0100, Jonathan Cameron wrote: > On Sun, 29 Sep 2024 13:25:11 +0200 > Vasileios Amoiridis <vassilisamir@gmail.com> wrote: > > > As it was reported and discussed here [1], storing the sensor data in an > > endian aware s32 buffer is not optimal. Advertising the timestamp as an > > addition of 2 s32 variables which is also implied is again not the best > > practice. For that reason, change the s32 sensor_data buffer to a char > > buffer with an extra value for the timestamp (as it is common practice). > > > > [1]: https://lore.kernel.org/linux-iio/73d13cc0-afb9-4306-b498-5d821728c3ba@stanley.mountain/ > > > > Signed-off-by: Vasileios Amoiridis <vassilisamir@gmail.com> > Hi Vasileois. > > I missed a purely semantic issue in previous versions :( > > A few other places where you can achieve the same effect with less code > and clear casting to correct types. > > Jonathan > > Hi Jonathan, > > --- > > drivers/iio/pressure/bmp280-core.c | 78 ++++++++++++++++++------------ > > drivers/iio/pressure/bmp280.h | 5 +- > > 2 files changed, 51 insertions(+), 32 deletions(-) > > > > diff --git a/drivers/iio/pressure/bmp280-core.c b/drivers/iio/pressure/bmp280-core.c > > index 472a6696303b..2c62490a40c6 100644 > > --- a/drivers/iio/pressure/bmp280-core.c > > +++ b/drivers/iio/pressure/bmp280-core.c > > > > @@ -2523,23 +2538,24 @@ static irqreturn_t bmp180_trigger_handler(int irq, void *p) > > struct iio_poll_func *pf = p; > > struct iio_dev *indio_dev = pf->indio_dev; > > struct bmp280_data *data = iio_priv(indio_dev); > > - int ret, chan_value; > > + int ret, comp_temp, comp_press, offset; > > > > guard(mutex)(&data->lock); > > > > - ret = bmp180_read_temp(data, &chan_value); > > + ret = bmp180_read_temp(data, &comp_temp); > > if (ret) > > goto out; > > > > - data->sensor_data[1] = chan_value; > > > > - ret = bmp180_read_press(data, &chan_value); > > + ret = bmp180_read_press(data, &comp_press); > > if (ret) > > goto out; > > > > - data->sensor_data[0] = chan_value; > > + memcpy(&data->buffer.buf[offset], &comp_press, sizeof(s32)); > > + offset += sizeof(s32); > > + memcpy(&data->buffer.buf[offset], &comp_temp, sizeof(s32)); > I suppose there is a consistency argument for using memcpy even for the s32 > cases but you 'could' if you like do > s32 *chans = (s32 *)data->buffer.buf; > at top > and > chans[0] = comp_press; > chans[1] = comp_temp; > here, which is functionally equivalent, particularly as we are forcing the > buffer alignment to be larger than this s32. > > Similar for the other simple native endian s32 cases. > > The memcpy is needed for the le24 one. > > I see what you mean, indeed I think your proposal will beautify it a lot! > > > > - iio_push_to_buffers_with_timestamp(indio_dev, &data->sensor_data, > > + iio_push_to_buffers_with_timestamp(indio_dev, &data->buffer, > > iio_get_time_ns(indio_dev)); > > > > out: > > diff --git a/drivers/iio/pressure/bmp280.h b/drivers/iio/pressure/bmp280.h > > index a9f220c1f77a..b0c26f55c6af 100644 > > --- a/drivers/iio/pressure/bmp280.h > > +++ b/drivers/iio/pressure/bmp280.h > > @@ -419,7 +419,10 @@ struct bmp280_data { > > * Data to push to userspace triggered buffer. Up to 3 channels and > > * s64 timestamp, aligned. > > */ > > - s32 sensor_data[6] __aligned(8); > > + struct { > > + u8 buf[12]; > > + aligned_s64 ts; > > I'd missed that this depends on the number of channels. It makes no functional > difference because the core code will happily write over the end of buf, but > from a representation point of view this might be > > u8 buf[8]; > aligned_s64 ts; > or > u8 buf[12]; > aligned_s64 ts; > > So given we can't actually fix on one or the other normal convention is > to just use something that forces a large enough aligned u8 buffer like > u8 buf[ALIGN(sizeof(s32) * BMP280_MAX_CHANNELS, 8) + sizeof(s64)] > __aligned(sizeof(s64)); > > Sorry for leading you astray on this! > > Jonathan > > I see your point. I can fix it in next version! This is a neat issue here that requires indeed extra attention since this type of buffers is used basically by the majority of the drivers. Some type of runtime check in those registers would have been very very helpful, but I can't see of an easy way of doing it in the core code.. Thanks for the review :) Cheers, Vasilis > > + } buffer; > > > > /* > > * DMA (thus cache coherency maintenance) may require the >
> > > - s32 sensor_data[6] __aligned(8); > > > + struct { > > > + u8 buf[12]; > > > + aligned_s64 ts; > > > > I'd missed that this depends on the number of channels. It makes no functional > > difference because the core code will happily write over the end of buf, but > > from a representation point of view this might be > > > > u8 buf[8]; > > aligned_s64 ts; > > or > > u8 buf[12]; > > aligned_s64 ts; > > > > So given we can't actually fix on one or the other normal convention is > > to just use something that forces a large enough aligned u8 buffer like > > u8 buf[ALIGN(sizeof(s32) * BMP280_MAX_CHANNELS, 8) + sizeof(s64)] > > __aligned(sizeof(s64)); > > > > Sorry for leading you astray on this! > > > > Jonathan > > > > > > I see your point. I can fix it in next version! > > This is a neat issue here that requires indeed extra attention since > this type of buffers is used basically by the majority of the drivers. > Some type of runtime check in those registers would have been very > very helpful, but I can't see of an easy way of doing it in the core > code.. Adding size description has been on my todo list for a while so as to allow the kernel to check the buffer is big enough and get rid of subtle overrun bugs due to that oddity of the buffer needing to include the timestamp space even though it's not obvious it will be written to. That would also allow us to check alignment. What we can't do is finer grained checking of these structures but arguably we don't want to as this is an elegance not correctness issue! > > Thanks for the review :) > > Cheers, > Vasilis > > > > + } buffer; > > > > > > /* > > > * DMA (thus cache coherency maintenance) may require the > >
© 2016 - 2024 Red Hat, Inc.