Re: [PATCH v2 2/2] iio: pressure: bmp280: Use char instead of s32 for data buffer

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxx> 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@xxxxxxxxx>
> 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
> 




[Index of Archives]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Input]     [Linux Kernel]     [Linux SCSI]     [X.org]

  Powered by Linux