From: Jonathan Cameron <jic23@kernel.org>
To: Vasileios Amoiridis <vassilisamir@gmail.com>
Cc: dan.carpenter@linaro.org, linux-iio@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2 2/2] iio: pressure: bmp280: Use char instead of s32 for data buffer
Date: Sun, 29 Sep 2024 18:10:03 +0100 [thread overview]
Message-ID: <20240929181003.26abf543@jic23-huawei> (raw)
In-Reply-To: <20240929112511.100292-3-vassilisamir@gmail.com>
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
next prev parent reply other threads:[~2024-09-29 17:10 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-09-29 11:25 [PATCH v2 0/2]: pressure: bmp280: Improve pushing of data to buffer Vasileios Amoiridis
2024-09-29 11:25 ` [PATCH v2 1/2] iio: pressure: bmp280: Fix type for raw values Vasileios Amoiridis
2024-09-29 17:04 ` Jonathan Cameron
2024-09-29 18:20 ` Vasileios Amoiridis
2024-09-29 11:25 ` [PATCH v2 2/2] iio: pressure: bmp280: Use char instead of s32 for data buffer Vasileios Amoiridis
2024-09-29 17:10 ` Jonathan Cameron [this message]
2024-09-29 18:26 ` Vasileios Amoiridis
2024-09-30 8:54 ` Jonathan Cameron
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240929181003.26abf543@jic23-huawei \
--to=jic23@kernel.org \
--cc=dan.carpenter@linaro.org \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=vassilisamir@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox