Skip to contentSkip to conversation

IIO: Alignment fixes part 4 - bounce buffers for the hard cases.

RFC PATCH Superseded Jonathan Cameron 4 participants 18 messages May 1, 2021 – May 5, 2021

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jonathan Cameron

author maintainer
#0 lore raw

cover IIO: Alignment fixes part 4 - bounce buffers for the hard cases.

From: Jonathan Cameron <[email protected]>

I finally got around to do a manual audit of all the calls to iio_push_to_buffers_with_timestamp() which has the somewhat odd requirements of:
1. 8 byte alignment of the provided buffer.
2. space for an 8 byte naturally aligned timestamp to be inserted at the end.

Unfortunately there were rather a lot of these left, but time to bite the bullet and clean them up.

As discussed previous in
https://lore.kernel.org/linux-iio/[email protected]/
it is not easy to fix the alignment issue without requiring a bounce buffer. This final part of the 4 sets of fixes is concerned with the cases where bounce buffers are the proposed solutions.

In these cases we have hardware that reads a prefix that we wish to drop. That makes it hard to directly read the data into the correct location.

Rather than implementing bouce buffers in each case, this set provides some magic in the core to handle them via a new function. iio_push_to_buffers_with_ts_na() - non aligned

Note this is totally untested as I don't have suitable hardware or emulation. I can fake something up in the dummy driver or via QEMU but I definitely want both eyes and testing on this series!

Cc Jan Kiszka <[email protected]>
Cc Linus Walleij <[email protected]>
Cc Nuno Sa <[email protected]>

Jonathan Cameron (4):
iio: core: Introduce iio_push_to_buffers_with_ts_na() for non aligned
case.
iio: adc: ti-adc108s102: Fix alignment of buffer pushed to iio
buffers.
iio: gyro: mpu3050: Fix alignment and size issues with buffers.
iio: imu: adis16400: Fix buffer alignment requirements.

 drivers/iio/adc/ti-adc108s102.c   | 11 +++++-----
 drivers/iio/gyro/mpu3050-core.c   | 24 ++++++++++-----------
 drivers/iio/imu/adis16400.c       | 20 ++++++++++++-----
 drivers/iio/industrialio-buffer.c | 36 +++++++++++++++++++++++++++++++
 include/linux/iio/buffer.h        |  4 ++++
 include/linux/iio/iio-opaque.h    |  4 ++++
 6 files changed, 76 insertions(+), 23 deletions(-)
Signature · 1 line
2.31.1
author maintainer
#1 lore raw

RFC PATCH 1/4 iio: core: Introduce iio_push_to_buffers_with_ts_na() for non aligned case.

From: Jonathan Cameron <[email protected]>

Whilst it is almost always possible to arrange for scan data to be read directly into a buffer that is suitable for passing to iio_push_to_buffers_with_timestamp(), there are a few places where leading data needs to be skipped over.

For these cases introduce a function that will allocate an appropriate sized and aligned bounce buffer (if not already allocated) and copy the unaligned data into that before calling iio_push_to_buffers_with_timestamp() on the bounce buffer. We tie the lifespace of this buffer to that of the iio_dev.dev which should ensure no memory leaks occur.

---
 drivers/iio/industrialio-buffer.c | 36 +++++++++++++++++++++++++++++++
 include/linux/iio/buffer.h        |  4 ++++
 include/linux/iio/iio-opaque.h    |  4 ++++
 3 files changed, 44 insertions(+)
drivers/iio/industrialio-buffer.c +36 -0
index 9a8e16c7e9af..818dfaa73665 100644
@@ -1727,6 +1727,42 @@ int iio_push_to_buffers(struct iio_dev *indio_dev, const void *data)
 }
 EXPORT_SYMBOL_GPL(iio_push_to_buffers);
 
+/**
+ * iio_push_to_buffers_with_ts_na() - push to registered buffer,
+ *    no alignment or space requirements.
+ * @indio_dev:		iio_dev structure for device.
+ * @data:		channel data excluding the timestamp.
+ * @data_sz:		size of data.
+ * @timestamp:		timestamp for the sample data.
+ *
+ * This special variant of iio_push_to_buffers_with_timetamp() does
+ * not require space for the timestamp, or 8 byte alignment of data.
+ * It does however require an allocation on first call and additional
+ * coppies on all calls, so should be avoided if possible
+ */
+int iio_push_to_buffers_with_ts_na(struct iio_dev *indio_dev,
+				   const void *data,
+				   size_t data_sz,
+				   int64_t timestamp)
+{
+	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
+
+	data_sz = min_t(size_t, indio_dev->scan_bytes, data_sz);
+	if (iio_dev_opaque->bounce_buffer_size !=  indio_dev->scan_bytes) {
+		iio_dev_opaque->bounce_buffer =
+			devm_krealloc(&indio_dev->dev,
+				      iio_dev_opaque->bounce_buffer,
+				      indio_dev->scan_bytes, GFP_KERNEL);
+		if (!iio_dev_opaque)
+			return -ENOMEM;
+	}
+	memcpy(iio_dev_opaque->bounce_buffer, data, data_sz);
+	return iio_push_to_buffers_with_timestamp(indio_dev,
+						  iio_dev_opaque->bounce_buffer,
+						  timestamp);
+}
+EXPORT_SYMBOL_GPL(iio_push_to_buffers_with_ts_na);
+
 /**
  * iio_buffer_release() - Free a buffer's resources
  * @ref: Pointer to the kref embedded in the iio_buffer struct
include/linux/iio/buffer.h +4 -0
index b6928ac5c63d..4ea9027ad2bf 100644
@@ -38,6 +38,10 @@ static inline int iio_push_to_buffers_with_timestamp(struct iio_dev *indio_dev,
 	return iio_push_to_buffers(indio_dev, data);
 }
 
+int iio_push_to_buffers_with_ts_na(struct iio_dev *indio_dev,
+				   const void *data, size_t data_sz,
+				   int64_t timestamp);
+
 bool iio_validate_scan_mask_onehot(struct iio_dev *indio_dev,
 				   const unsigned long *mask);
 
include/linux/iio/iio-opaque.h +4 -0
index 32addd5e790e..70ca58e66c0a 100644
@@ -19,6 +19,8 @@
  * @groupcounter:		index of next attribute group
  * @legacy_scan_el_group:	attribute group for legacy scan elements attribute group
  * @legacy_buffer_group:	attribute group for legacy buffer attributes group
+ * @bounce_buffer:		for devices that call iio_push_to_buffers_with_timestamp_na
+ * @bounce_buffer_size:		size of currently allocate bounce buffer
  * @debugfs_dentry:		device specific debugfs dentry
  * @cached_reg_addr:		cached register address for debugfs reads
  * @read_buf:			read buffer to be used for the initial reg read
@@ -38,6 +40,8 @@ struct iio_dev_opaque {
 	int				groupcounter;
 	struct attribute_group		legacy_scan_el_group;
 	struct attribute_group		legacy_buffer_group;
+	void				*bounce_buffer;
+	size_t				bounce_buffer_size;
 #if defined(CONFIG_DEBUG_FS)
 	struct dentry			*debugfs_dentry;
 	unsigned			cached_reg_addr;
Signature · 1 line
2.31.1
#2 lore raw

Re: [RFC PATCH 1/4] iio: core: Introduce iio_push_to_buffers_with_ts_na() for non aligned case.

Quoted 14 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Whilst it is almost always possible to arrange for scan data to be read directly into a buffer that is suitable for passing to iio_push_to_buffers_with_timestamp(), there are a few places where leading data needs to be skipped over.

For these cases introduce a function that will allocate an appropriate sized and aligned bounce buffer (if not already allocated) and copy the unaligned data into that before calling iio_push_to_buffers_with_timestamp() on the bounce buffer. We tie the lifespace of this buffer to that of the iio_dev.dev which should ensure no memory leaks occur.

...

Quoted 7 lines

+/**
+ * iio_push_to_buffers_with_ts_na() - push to registered buffer,
+ * no alignment or space requirements.

+ * @indio_dev:         iio_dev structure for device.
+ * @data:              channel data excluding the timestamp.
+ * @data_sz:           size of data.
+ * @timestamp:         timestamp for the sample data.
Quoted 5 lines

+ *
+ * This special variant of iio_push_to_buffers_with_timetamp() does
+ * not require space for the timestamp, or 8 byte alignment of data.
+ * It does however require an allocation on first call and additional
+ * coppies on all calls, so should be avoided if possible

copies

Quoted 1 line

+ */

I do not like the _na part in the name (My first impression was with a Timestamp that was not available, what?!). Can we spell it better?

drivers/iio/industrialio-buffer.c int iio_push_to_buffers(struct iio_dev *indio_dev, const void *data) · line 1754

Quoted 13 lines
drivers/iio/industrialio-buffer.c +12 -0
+int iio_push_to_buffers_with_ts_na(struct iio_dev *indio_dev,
+                                  const void *data,
+                                  size_t data_sz,
+                                  int64_t timestamp)
+{
+       struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
+
+       data_sz = min_t(size_t, indio_dev->scan_bytes, data_sz);
+       if (iio_dev_opaque->bounce_buffer_size !=  indio_dev->scan_bytes) {
 
+               iio_dev_opaque->bounce_buffer =
+                       devm_krealloc(&indio_dev->dev,
+                                     iio_dev_opaque->bounce_buffer,

Oh la la, foo = realloc(foo, ...) is 101 type of mistakes. Please, don't do this.

Quoted 9 lines
drivers/iio/industrialio-buffer.c +9 -0
+                                     indio_dev->scan_bytes, GFP_KERNEL);
+               if (!iio_dev_opaque)
+                       return -ENOMEM;
+       }
+       memcpy(iio_dev_opaque->bounce_buffer, data, data_sz);
+       return iio_push_to_buffers_with_timestamp(indio_dev,
+                                                 iio_dev_opaque->bounce_buffer,
+                                                 timestamp);
+}
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw

Re: [RFC PATCH 1/4] iio: core: Introduce iio_push_to_buffers_with_ts_na() for non aligned case.

Quoted 1 line · Andy Shevchenko

On Sat, May 1, 2021 at 8:28 PM Jonathan Cameron <[email protected]> wrote:

...

Quoted 9 lines

drivers/iio/industrialio-buffer.c int iio_push_to_buffers(struct iio_dev *indio_dev, const void *data) · line 1754

Quoted 3 lines
drivers/iio/industrialio-buffer.c +3 -0
+               iio_dev_opaque->bounce_buffer =
+                       devm_krealloc(&indio_dev->dev,
+                                     iio_dev_opaque->bounce_buffer,

Oh la la, foo = realloc(foo, ...) is 101 type of mistakes. Please, don't do this.

Quoted 2 lines
drivers/iio/industrialio-buffer.c +2 -0
+                                     indio_dev->scan_bytes, GFP_KERNEL);
+               if (!iio_dev_opaque)

And this check...

Quoted 2 lines
Quoted 2 lines
(quoted diff) +2 -0
+                       return -ENOMEM;
+       }
Signature · 2 lines
With Best Regards,
Andy Shevchenko
author maintainer
#4 lore raw

Re: [RFC PATCH 1/4] iio: core: Introduce iio_push_to_buffers_with_ts_na() for non aligned case.

Quoted 10 lines · On Sat, 1 May 2021 22:27:22 +0300 Andy Shevchenko <[email protected]>…
Quoted 1 line · Andy Shevchenko

On Sat, May 1, 2021 at 8:28 PM Jonathan Cameron <[email protected]> wrote:

...

Quoted 4 lines
Quoted 3 lines
drivers/iio/industrialio-buffer.c +3 -0
+               iio_dev_opaque->bounce_buffer =
+                       devm_krealloc(&indio_dev->dev,
+                                     iio_dev_opaque->bounce_buffer,  
Quoted 7 lines
Quoted 5 lines

Oh la la, foo = realloc(foo, ...) is 101 type of mistakes. Please, don't do this.

Quoted 2 lines
drivers/iio/industrialio-buffer.c +2 -0
+                                     indio_dev->scan_bytes, GFP_KERNEL);
+               if (!iio_dev_opaque)  

And this check...

Yup. Thoroughly broken. Hohum, but basic idea conveyed however bad the rfc was :(

Quoted 4 lines
Quoted 3 lines
Quoted 3 lines
(quoted diff) +2 -0
+                       return -ENOMEM;
+       }  
author maintainer
#5 lore raw

Re: [RFC PATCH 1/4] iio: core: Introduce iio_push_to_buffers_with_ts_na() for non aligned case.

In reply to Andy Shevchenko
Quoted 31 lines · On Sat, 1 May 2021 22:25:55 +0300 Andy Shevchenko <[email protected]>…
Quoted 14 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Whilst it is almost always possible to arrange for scan data to be read directly into a buffer that is suitable for passing to iio_push_to_buffers_with_timestamp(), there are a few places where leading data needs to be skipped over.

For these cases introduce a function that will allocate an appropriate sized and aligned bounce buffer (if not already allocated) and copy the unaligned data into that before calling iio_push_to_buffers_with_timestamp() on the bounce buffer. We tie the lifespace of this buffer to that of the iio_dev.dev which should ensure no memory leaks occur.

...

Quoted 7 lines

+/**
+ * iio_push_to_buffers_with_ts_na() - push to registered buffer,
+ * no alignment or space requirements.

+ * @indio_dev:         iio_dev structure for device.
+ * @data:              channel data excluding the timestamp.
+ * @data_sz:           size of data.
+ * @timestamp:         timestamp for the sample data.
Quoted 5 lines

+ *
+ * This special variant of iio_push_to_buffers_with_timetamp() does
+ * not require space for the timestamp, or 8 byte alignment of data.
+ * It does however require an allocation on first call and additional
+ * coppies on all calls, so should be avoided if possible

Quoted 1 line

copies

One day I'll remember to actually spell check *sigh*

Quoted 5 lines
Quoted 2 lines

+ */

I do not like the _na part in the name (My first impression was with a Timestamp that was not available, what?!). Can we spell it better?

I struggled with the naming. Ideally we'd have started with this as the iio_push_to_buffers_with_timestamp() and had
an _aligned version for the existing case.

Perhaps spend the characters and just make it
_with_ts_unaligned()

Quoted 11 lines
Quoted 10 lines
drivers/iio/industrialio-buffer.c +9 -0
+int iio_push_to_buffers_with_ts_na(struct iio_dev *indio_dev,
+                                  const void *data,
+                                  size_t data_sz,
+                                  int64_t timestamp)
+{
+       struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
+
+       data_sz = min_t(size_t, indio_dev->scan_bytes, data_sz);
+       if (iio_dev_opaque->bounce_buffer_size !=  indio_dev->scan_bytes) {  
Quoted 6 lines

drivers/iio/industrialio-buffer.c int iio_push_to_buffers(struct iio_dev *indio_dev, const void *data) · line 1754

Quoted 3 lines
drivers/iio/industrialio-buffer.c +3 -0
+               iio_dev_opaque->bounce_buffer =
+                       devm_krealloc(&indio_dev->dev,
+                                     iio_dev_opaque->bounce_buffer,  

Oh la la, foo = realloc(foo, ...) is 101 type of mistakes. Please, don't do this.

For realloc I'd agree because if the new allocation fails we'd just have lost the pointer, but with a managed case, I think we'll leave the original pointer alone from the point of view of the devm_ cleanup.

The only exit paths of interest in devm_krealloc() are the ones where we are trying to allocate a new larger object (otherwise it either does nothing or it is just a call to devm_kmalloc().

The one on failure to find the original managed resource, so can't loose it because it wasn't there.

Am I missing something?

Quoted 4 lines

drivers/iio/industrialio-buffer.c int iio_push_to_buffers(struct iio_dev *indio_dev, const void *data) · line 1757

Quoted 4 lines
drivers/iio/industrialio-buffer.c +3 -0
+                                     indio_dev->scan_bytes, GFP_KERNEL);
+               if (!iio_dev_opaque)
+                       return -ENOMEM;

As you observed this is clearly garbage. I should have sat on this patch for a day and at least reread it or ideally done some testing.

Failing to set the bounce_buffer_size() definitely doesn't help either..

Quoted 7 lines
Quoted 6 lines
drivers/iio/industrialio-buffer.c +6 -0
+       }
+       memcpy(iio_dev_opaque->bounce_buffer, data, data_sz);
+       return iio_push_to_buffers_with_timestamp(indio_dev,
+                                                 iio_dev_opaque->bounce_buffer,
+                                                 timestamp);
+}  
maintainer
#6 lore raw

RE: [RFC PATCH 1/4] iio: core: Introduce iio_push_to_buffers_with_ts_na() for non aligned case.

In reply to Jonathan Cameron
Quoted 51 lines

From: Jonathan Cameron <[email protected]>
Sent: Saturday, May 1, 2021 7:25 PM
To: [email protected]
Cc: Jonathan Cameron <[email protected]>
Subject: [RFC PATCH 1/4] iio: core: Introduce iio_push_to_buffers_with_ts_na() for non aligned case.

From: Jonathan Cameron <[email protected]>

Whilst it is almost always possible to arrange for scan data to be read directly into a buffer that is suitable for passing to iio_push_to_buffers_with_timestamp(), there are a few places where leading data needs to be skipped over.

For these cases introduce a function that will allocate an appropriate sized and aligned bounce buffer (if not already allocated) and copy the unaligned data into that before calling iio_push_to_buffers_with_timestamp() on the bounce buffer. We tie the lifespace of this buffer to that of the iio_dev.dev which should ensure no memory leaks occur.

---
 drivers/iio/industrialio-buffer.c | 36
+++++++++++++++++++++++++++++++
 include/linux/iio/buffer.h        |  4 ++++
 include/linux/iio/iio-opaque.h    |  4 ++++
 3 files changed, 44 insertions(+)
drivers/iio/industrialio- renamed from drivers/iio/industrialio-buffer.c +0 -0

buffer.c
index 9a8e16c7e9af..818dfaa73665 100644

drivers/iio/industrialio-buffer.c +12 -0
@@ -1727,6 +1727,42 @@ int iio_push_to_buffers(struct iio_dev
 *indio_dev, const void *data)
 }
 EXPORT_SYMBOL_GPL(iio_push_to_buffers);
 
+/**
+ * iio_push_to_buffers_with_ts_na() - push to registered buffer,
+ *    no alignment or space requirements.
+ * @indio_dev:		iio_dev structure for device.
+ * @data:		channel data excluding the timestamp.
+ * @data_sz:		size of data.
+ * @timestamp:		timestamp for the sample data.
+ *
+ * This special variant of iio_push_to_buffers_with_timetamp() does
+ * not require space for the timestamp, or 8 byte alignment of data.
+ * It does however require an allocation on first call and additional
+ * coppies on all calls, so should be avoided if possible

drivers/iio/industrialio-buffer.c int iio_push_to_buffers(struct iio_dev · line 1752

Quoted 10 lines
drivers/iio/industrialio-buffer.c +9 -0
+ */
+int iio_push_to_buffers_with_ts_na(struct iio_dev *indio_dev,
+				   const void *data,
+				   size_t data_sz,
+				   int64_t timestamp)
+{
+	struct iio_dev_opaque *iio_dev_opaque =
 to_iio_dev_opaque(indio_dev);
+
+	data_sz = min_t(size_t, indio_dev->scan_bytes, data_sz);

I'm not really sure about this. Is it really a good idea to silently truncate the data if some erroneous driver calls this with data_sz > scan_bytes? (I don't think it's ever valid for data_sz to be bigger than scan_bytes) We might be discarding data that userland actually requested. So, I'm not sure if it would not be better to be strict here and return error, so that driver developers could catch this early on...

data_sz < scan_bytes is also probably not valid as that might happen if some drivers call this without the scan elements properly aligned which will definitely be problematic... Naturally in this case we would have to take into account timestamp possible alignment fill bytes as it could be possible (and likely) that data_sz won't include those bits...

Alternatively, we could be consistent with 'iio_push_to_buffers_with_timestamp()' and assume drivers play along in terms of alignment and size
(so, removing the data_sz parameter)...

My point is, if we are taking the data_sz parameter, maybe we should be pedantic about it...

drivers/iio/industrialio-buffer.c int iio_push_to_buffers(struct iio_dev · line 1754

Quoted 2 lines
drivers/iio/industrialio-buffer.c +1 -0
+	if (iio_dev_opaque->bounce_buffer_size !=  indio_dev-
 >scan_bytes) {

Where do we set ' bounce_buffer_size '? I guess we do not want to get here all the time...

Quoted 5 lines
drivers/iio/industrialio-buffer.c +4 -0
+		iio_dev_opaque->bounce_buffer =
+			devm_krealloc(&indio_dev->dev,
+				      iio_dev_opaque->bounce_buffer,
+				      indio_dev->scan_bytes,

GFP_KERNEL);

I'm also not a big fan of realloc so I prefer to avoid it unless really needed... Here, maybe we could just allocate this once during device/buffer initialization and allocate it for the maximum possible size, i.e all scan elements enabled... That could also make this API simpler to the point of actually be inlined...

- Nuno Sá

author maintainer
#7 lore raw

Re: [RFC PATCH 1/4] iio: core: Introduce iio_push_to_buffers_with_ts_na() for non aligned case.

Quoted 56 lines · Nuno
Quoted 56 lines

From: Jonathan Cameron <[email protected]>
Sent: Saturday, May 1, 2021 7:25 PM
To: [email protected]
Cc: Jonathan Cameron <[email protected]>
Subject: [RFC PATCH 1/4] iio: core: Introduce iio_push_to_buffers_with_ts_na() for non aligned case.

From: Jonathan Cameron <[email protected]>

Whilst it is almost always possible to arrange for scan data to be read directly into a buffer that is suitable for passing to iio_push_to_buffers_with_timestamp(), there are a few places where leading data needs to be skipped over.

For these cases introduce a function that will allocate an appropriate sized and aligned bounce buffer (if not already allocated) and copy the unaligned data into that before calling iio_push_to_buffers_with_timestamp() on the bounce buffer. We tie the lifespace of this buffer to that of the iio_dev.dev which should ensure no memory leaks occur.

---
 drivers/iio/industrialio-buffer.c | 36
+++++++++++++++++++++++++++++++
 include/linux/iio/buffer.h        |  4 ++++
 include/linux/iio/iio-opaque.h    |  4 ++++
 3 files changed, 44 insertions(+)
drivers/iio/industrialio- renamed from drivers/iio/industrialio-buffer.c +0 -0

buffer.c
index 9a8e16c7e9af..818dfaa73665 100644

drivers/iio/industrialio-buffer.c +17 -0
@@ -1727,6 +1727,42 @@ int iio_push_to_buffers(struct iio_dev
 *indio_dev, const void *data)
 }
 EXPORT_SYMBOL_GPL(iio_push_to_buffers);
 
+/**
+ * iio_push_to_buffers_with_ts_na() - push to registered buffer,
+ *    no alignment or space requirements.
+ * @indio_dev:		iio_dev structure for device.
+ * @data:		channel data excluding the timestamp.
+ * @data_sz:		size of data.
+ * @timestamp:		timestamp for the sample data.
+ *
+ * This special variant of iio_push_to_buffers_with_timetamp() does
+ * not require space for the timestamp, or 8 byte alignment of data.
+ * It does however require an allocation on first call and additional
+ * coppies on all calls, so should be avoided if possible
+ */
+int iio_push_to_buffers_with_ts_na(struct iio_dev *indio_dev,
+				   const void *data,
+				   size_t data_sz,
+				   int64_t timestamp)
Quoted 8 lines

drivers/iio/industrialio-buffer.c int iio_push_to_buffers(struct iio_dev *indio_dev, const void *data) · line 1750

Quoted 5 lines
drivers/iio/industrialio-buffer.c +4 -0
+{
+	struct iio_dev_opaque *iio_dev_opaque =
 to_iio_dev_opaque(indio_dev);
+
+	data_sz = min_t(size_t, indio_dev->scan_bytes, data_sz);  

I'm not really sure about this. Is it really a good idea to silently truncate the data if some erroneous driver calls this with data_sz > scan_bytes? (I don't think it's ever valid for data_sz to be bigger than scan_bytes)

It's not probably not. This is just a fairly nasty way of papering over anyone doing that.

Quoted 3 lines

We might be discarding data that userland actually requested. So, I'm not sure if it would not be better to be strict here and return error, so that driver developers could catch this early on...

I'm fairly sure we'll never be discarding data userspace requested because scan_bytes will be the maximum possible and is the size of the kfifo element. Any more data that comes in is going to be dropped anyway assuming it ends up in a kfifo_put()

I wondered if we wanted to enforce this? It would not seem totally silly for a driver that wasn't bothering to track the size of it's enabled channels to provide data_sz == maximum size it ever needs.
Docs should definitely say if that's valid however, and it's definitely less than optimal because we may copy garbage that will then be overwritten by the timestamp.

Quoted 6 lines

data_sz < scan_bytes is also probably not valid as that might happen if some drivers call this without the scan elements properly aligned which will definitely be problematic... Naturally in this case we would have to take into account timestamp possible alignment fill bytes as it could be possible (and likely) that data_sz won't include those bits...

data_sz < scan_bytes is intentionally valid (and indeed I made one of the drivers do this). Given we are bouncing anyway, why insist the driver provides space for padding + timestamp? That can be done for the bounce buffer.

Quoted 5 lines

Alternatively, we could be consistent with 'iio_push_to_buffers_with_timestamp()' and assume drivers play along in terms of alignment and size
(so, removing the data_sz parameter)...

Quoted 7 lines

My point is, if we are taking the data_sz parameter, maybe we should be pedantic about it...

drivers/iio/industrialio-buffer.c int iio_push_to_buffers(struct iio_dev · line 1754

Quoted 2 lines
drivers/iio/industrialio-buffer.c +1 -0
+	if (iio_dev_opaque->bounce_buffer_size !=  indio_dev-  
 >scan_bytes) {  

Where do we set ' bounce_buffer_size '? I guess we do not want to get here all the time...

:( oops

Quoted 9 lines
Quoted 5 lines
drivers/iio/industrialio-buffer.c +4 -0
+		iio_dev_opaque->bounce_buffer =
+			devm_krealloc(&indio_dev->dev,
+				      iio_dev_opaque->bounce_buffer,
+				      indio_dev->scan_bytes,

GFP_KERNEL);

I'm also not a big fan of realloc so I prefer to avoid it unless really needed... Here, maybe we could just allocate this once during device/buffer initialization and allocate it for the maximum possible size, i.e all scan elements enabled...

We can but to do that requires more invasive changes. Perhaps not horrible ones, but we'd need to add stuff to caller drivers to configure the bounce buffer, or some flag to see they needed to enable support for this. Only 3 drivers out of all those in IIO are currently using this code (might be a few others that could take advantage but not many - so need to keep it localized).

I suppose we could have an
iio_unaligned_push_to_buffers_prepare() to be called from the update_scan_mode() callback (typed to be useable as that callback if nothing else to be done in the relevant drivers.

Quoted 1 line

That could also make this API simpler to the point of actually be inlined...

Can't make it inline without making the bounce buffer visible to the driver and I don't want the driver to be able to see it as it adds another place for things to be done wrongly in drivers.
Any driver doing this is having to deal with a bounce buffer anyway, so the indirection doesn't worry me too much.

Quoted 2 lines

- Nuno Sá

Thanks,

Jonathan

maintainer
#8 lore raw

RE: [RFC PATCH 1/4] iio: core: Introduce iio_push_to_buffers_with_ts_na() for non aligned case.

Quoted 94 lines

-----Original Message-----
From: Jonathan Cameron <[email protected]>
Sent: Sunday, May 2, 2021 6:09 PM
To: Sa, Nuno <[email protected]>
Cc: [email protected]; Jonathan Cameron <[email protected]>
Subject: Re: [RFC PATCH 1/4] iio: core: Introduce iio_push_to_buffers_with_ts_na() for non aligned case.

Quoted 7 lines · Nuno
Quoted 7 lines

From: Jonathan Cameron <[email protected]>
Sent: Saturday, May 1, 2021 7:25 PM
To: [email protected]
Cc: Jonathan Cameron <[email protected]>
Subject: [RFC PATCH 1/4] iio: core: Introduce iio_push_to_buffers_with_ts_na() for non aligned case.

Quoted 5 lines
Quoted 5 lines

From: Jonathan Cameron <[email protected]>

Whilst it is almost always possible to arrange for scan data to be read directly into a buffer that is suitable for passing to iio_push_to_buffers_with_timestamp(), there are a few places

where

Quoted 3 lines
Quoted 3 lines

leading data needs to be skipped over.

For these cases introduce a function that will allocate an

appropriate

Quoted 7 lines
Quoted 7 lines

sized and aligned bounce buffer (if not already allocated) and copy the unaligned data into that before calling iio_push_to_buffers_with_timestamp() on the bounce buffer. We tie the lifespace of this buffer to that of the iio_dev.dev which should ensure no memory leaks occur.

Signed-off-by Jonathan Cameron

<[email protected]>

Quoted 8 lines

Review

Quoted 8 lines
---
 drivers/iio/industrialio-buffer.c | 36
+++++++++++++++++++++++++++++++
 include/linux/iio/buffer.h        |  4 ++++
 include/linux/iio/iio-opaque.h    |  4 ++++
 3 files changed, 44 insertions(+)
(quoted diff) +0 -0

b/drivers/iio/industrialio-

Quoted 9 lines
Quoted 9 lines

buffer.c
index 9a8e16c7e9af..818dfaa73665 100644

drivers/iio/industrialio-buffer.c +0 -0
@@ -1727,6 +1727,42 @@ int iio_push_to_buffers(struct iio_dev
 *indio_dev, const void *data)
 }
 EXPORT_SYMBOL_GPL(iio_push_to_buffers);
 
Quoted 9 lines

drivers/iio/industrialio-buffer.c int iio_push_to_buffers(struct iio_dev *indio_dev, const void *data)

Quoted 9 lines
drivers/iio/industrialio-buffer.c +9 -0
+/**
+ * iio_push_to_buffers_with_ts_na() - push to registered buffer,
+ *    no alignment or space requirements.
+ * @indio_dev:		iio_dev structure for device.
+ * @data:		channel data excluding the timestamp.
+ * @data_sz:		size of data.
+ * @timestamp:		timestamp for the sample data.
+ *
+ * This special variant of iio_push_to_buffers_with_timetamp()

does

Quoted 1 line

drivers/iio/industrialio-buffer.c int iio_push_to_buffers(struct iio_dev · line 1740

Quoted 1 line
drivers/iio/industrialio-buffer.c +1 -0
+ * not require space for the timestamp, or 8 byte alignment of

data.

Quoted 5 lines
Quoted 5 lines
drivers/iio/industrialio-buffer.c +5 -0
+ * It does however require an allocation on first call and additional
+ * coppies on all calls, so should be avoided if possible
+ */
+int iio_push_to_buffers_with_ts_na(struct iio_dev *indio_dev,
+				   const void *data,
Quoted 8 lines

drivers/iio/industrialio-buffer.c int iio_push_to_buffers(struct iio_dev *indio_dev, const void *data) · line 1750

Quoted 7 lines
drivers/iio/industrialio-buffer.c +6 -0
+				   size_t data_sz,
+				   int64_t timestamp)
+{
+	struct iio_dev_opaque *iio_dev_opaque =
 to_iio_dev_opaque(indio_dev);
+
+	data_sz = min_t(size_t, indio_dev->scan_bytes, data_sz);

I'm not really sure about this. Is it really a good idea to silently

truncate

Quoted 1 line

the data if some erroneous driver calls this with data_sz >

scan_bytes?

Quoted 1 line

(I don't think it's ever valid for data_sz to be bigger than scan_bytes)

It's not probably not. This is just a fairly nasty way of papering over anyone doing that.

Quoted 2 lines

We might be discarding data that userland actually requested. So, I'm not sure if it would not be better to be strict here and return error,

so that

Quoted 8 lines
Quoted 1 line

driver developers could catch this early on...

I'm fairly sure we'll never be discarding data userspace requested because scan_bytes will be the maximum possible and is the size of the
kfifo element. Any more data that comes in is going to be dropped anyway
assuming it ends up in a kfifo_put()

I think it depends. Think on a totally erroneous driver that does not track enabled elements and just pushes all elements to IIO buffers. If userspace just requested, let's say, the last 2 elements, I believe those would be definitely discarded by this... More subtle things could also happen I guess... In this case, we can arguably say that the driver is completely broken and while I'm fairly sure such a scenario would never pass through revision, I think that if we make things more strict, we might minimize the chances of such patches to appear in the first place (unless someone is sending patches without testing them at all).

Quoted 15 lines

I wondered if we wanted to enforce this? It would not seem totally silly for a driver that wasn't bothering to track the size of it's enabled channels to provide data_sz == maximum size it ever needs.
Docs should definitely say if that's valid however, and it's definitely less than optimal because we may copy garbage that will then be overwritten by
the timestamp.

Quoted 6 lines

data_sz < scan_bytes is also probably not valid as that might happen if some drivers call this without the scan elements properly aligned which will definitely be problematic... Naturally in this case we would have to take into account timestamp possible alignment fill bytes as it could be possible (and likely) that data_sz won't include those bits...

Quoted 4 lines

data_sz < scan_bytes is intentionally valid (and indeed I made one of the drivers do this). Given we are bouncing anyway, why insist the driver provides space for padding + timestamp? That can be done for the bounce buffer.

What I meant was that if data_sz < scan_bytes - timestamp - padding, then the driver is most likely not guaranteeing properly aligned elements. I was not that clear :)

Anyways, I still feel that if we are taking the data_sz parameter on the API, we should handle it more strictly. But I do not feel too strong about this (as you said, It's not expected to exist too much users of this) and in the end of the day it still behaves the same way as 'iio_push_to_buffers_with_timestamp()'...

Quoted 30 lines
Quoted 2 lines

Alternatively, we could be consistent with

'iio_push_to_buffers_with_timestamp()'

Quoted 4 lines

and assume drivers play along in terms of alignment and size (so, removing the data_sz parameter)...

My point is, if we are taking the data_sz parameter, maybe we

should be pedantic

Quoted 6 lines

about it...

drivers/iio/industrialio-buffer.c int iio_push_to_buffers(struct iio_dev · line 1754

Quoted 2 lines
drivers/iio/industrialio-buffer.c +1 -0
+	if (iio_dev_opaque->bounce_buffer_size !=  indio_dev-
 >scan_bytes) {

Where do we set ' bounce_buffer_size '? I guess we do not want to get here all the time...

:( oops

Quoted 7 lines
Quoted 5 lines
drivers/iio/industrialio-buffer.c +4 -0
+		iio_dev_opaque->bounce_buffer =
+			devm_krealloc(&indio_dev->dev,
+				      iio_dev_opaque->bounce_buffer,
+				      indio_dev->scan_bytes,

GFP_KERNEL);

I'm also not a big fan of realloc so I prefer to avoid it unless really

needed...

Quoted 1 line

Here, maybe we could just allocate this once during device/buffer

initialization

Quoted 1 line

and allocate it for the maximum possible size, i.e all scan elements

enabled...

Quoted 9 lines

We can but to do that requires more invasive changes. Perhaps not horrible ones,
but we'd need to add stuff to caller drivers to configure the bounce buffer, or
some flag to see they needed to enable support for this. Only 3 drivers out of
all those in IIO are currently using this code (might be a few others that could
take advantage but not many - so need to keep it localized).

I was not thinking that way. I was just thinking in always allocating the bounce buffer. But I do agree that it is less than ideal to allocate something that will almost never be used...

Quoted 6 lines

I suppose we could have an iio_unaligned_push_to_buffers_prepare() to be called from the update_scan_mode()
callback (typed to be useable as that callback if nothing else to be done in
the relevant drivers.

It could also be an option... If we want to not use realloc, we would have to manually control the lifespan of the buffer and free it in the disable path. But I guess that might feel like too much handling...

Anyways, maybe sticking with realloc, we could just use a more normal pattern like:

tmp = devm_realloc(bounce_fuffer, ...);
if (!tmp)

    error;

bounce_buffer = tmp;

- Nuno Sá

maintainer
#9 lore raw

RE: [RFC PATCH 1/4] iio: core: Introduce iio_push_to_buffers_with_ts_na() for non aligned case.

Quoted 240 lines

-----Original Message-----
From: Sa, Nuno <[email protected]>
Sent: Monday, May 3, 2021 9:15 AM
To: Jonathan Cameron <[email protected]>
Cc: [email protected]; Jonathan Cameron <[email protected]>
Subject: RE: [RFC PATCH 1/4] iio: core: Introduce iio_push_to_buffers_with_ts_na() for non aligned case.

Quoted 22 lines

-----Original Message-----
From: Jonathan Cameron <[email protected]>
Sent: Sunday, May 2, 2021 6:09 PM
To: Sa, Nuno <[email protected]>
Cc: [email protected]; Jonathan Cameron <[email protected]>
Subject: Re: [RFC PATCH 1/4] iio: core: Introduce iio_push_to_buffers_with_ts_na() for non aligned case.

Quoted 9 lines · Nuno
Quoted 9 lines

From: Jonathan Cameron <[email protected]>
Sent: Saturday, May 1, 2021 7:25 PM
To: [email protected]
Cc: Jonathan Cameron <[email protected]>
Subject: [RFC PATCH 1/4] iio: core: Introduce iio_push_to_buffers_with_ts_na() for non aligned case.

From: Jonathan Cameron <[email protected]>

Quoted 9 lines
Quoted 3 lines
Quoted 3 lines

Whilst it is almost always possible to arrange for scan data to be read directly into a buffer that is suitable for passing to iio_push_to_buffers_with_timestamp(), there are a few places

where

Quoted 3 lines
Quoted 3 lines

leading data needs to be skipped over.

For these cases introduce a function that will allocate an

appropriate

Quoted 1 line
Quoted 1 line

sized and aligned bounce buffer (if not already allocated) and

copy

Quoted 25 lines
Quoted 6 lines
Quoted 6 lines

the unaligned data into that before calling iio_push_to_buffers_with_timestamp() on the bounce buffer. We tie the lifespace of this buffer to that of the iio_dev.dev which should ensure no memory leaks occur.

Signed-off-by Jonathan Cameron

<[email protected]>

Quoted 8 lines

Review

Quoted 8 lines
---
 drivers/iio/industrialio-buffer.c | 36
+++++++++++++++++++++++++++++++
 include/linux/iio/buffer.h        |  4 ++++
 include/linux/iio/iio-opaque.h    |  4 ++++
 3 files changed, 44 insertions(+)
(quoted diff) +0 -0

b/drivers/iio/industrialio-

Quoted 9 lines
Quoted 9 lines

buffer.c
index 9a8e16c7e9af..818dfaa73665 100644

drivers/iio/industrialio-buffer.c +0 -0
@@ -1727,6 +1727,42 @@ int iio_push_to_buffers(struct iio_dev
 *indio_dev, const void *data)
 }
 EXPORT_SYMBOL_GPL(iio_push_to_buffers);
 
Quoted 2 lines
Quoted 2 lines

drivers/iio/industrialio-buffer.c int iio_push_to_buffers(struct iio_dev · line 1732

Quoted 2 lines
drivers/iio/industrialio-buffer.c +2 -0
+/**
+ * iio_push_to_buffers_with_ts_na() - push to registered

buffer,

Quoted 11 lines
Quoted 7 lines

drivers/iio/industrialio-buffer.c int iio_push_to_buffers(struct iio_dev *indio_dev, const void *data)

Quoted 7 lines
drivers/iio/industrialio-buffer.c +7 -0
+ *    no alignment or space requirements.
+ * @indio_dev:		iio_dev structure for device.
+ * @data:		channel data excluding the timestamp.
+ * @data_sz:		size of data.
+ * @timestamp:		timestamp for the sample data.
+ *
+ * This special variant of iio_push_to_buffers_with_timetamp()

does

Quoted 1 line

drivers/iio/industrialio-buffer.c int iio_push_to_buffers(struct iio_dev · line 1740

Quoted 1 line
drivers/iio/industrialio-buffer.c +1 -0
+ * not require space for the timestamp, or 8 byte alignment of

data.

Quoted 1 line
Quoted 1 line
drivers/iio/industrialio-buffer.c +1 -0
+ * It does however require an allocation on first call and

additional

Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
drivers/iio/industrialio-buffer.c +4 -0
+ * coppies on all calls, so should be avoided if possible
+ */
+int iio_push_to_buffers_with_ts_na(struct iio_dev *indio_dev,
+				   const void *data,
Quoted 8 lines

drivers/iio/industrialio-buffer.c int iio_push_to_buffers(struct iio_dev *indio_dev, const void *data) · line 1750

Quoted 7 lines
drivers/iio/industrialio-buffer.c +6 -0
+				   size_t data_sz,
+				   int64_t timestamp)
+{
+	struct iio_dev_opaque *iio_dev_opaque =
 to_iio_dev_opaque(indio_dev);
+
+	data_sz = min_t(size_t, indio_dev->scan_bytes, data_sz);

I'm not really sure about this. Is it really a good idea to silently

Quoted 4 lines

truncate

Quoted 1 line

the data if some erroneous driver calls this with data_sz >

scan_bytes?

Quoted 1 line

(I don't think it's ever valid for data_sz to be bigger than

scan_bytes)

Quoted 5 lines

It's not probably not. This is just a fairly nasty way of papering over anyone doing that.

Quoted 1 line

We might be discarding data that userland actually requested. So,

I'm

Quoted 10 lines
Quoted 1 line

not sure if it would not be better to be strict here and return error,

so that

Quoted 1 line

driver developers could catch this early on...

I'm fairly sure we'll never be discarding data userspace requested because scan_bytes will be the maximum possible and is the size of the
kfifo element. Any more data that comes in is going to be dropped anyway
assuming it ends up in a kfifo_put()

I think it depends. Think on a totally erroneous driver that does not track
enabled elements and just pushes all elements to IIO buffers. If userspace
just requested, let's say, the last 2 elements, I believe those would be definitely discarded by this... More subtle things could also happen I guess...
In this case, we can arguably say that the driver is completely broken and while
I'm fairly sure such a scenario would never pass through revision, I think that
if we make things more strict, we might minimize the chances of such patches
to appear in the first place (unless someone is sending patches without testing them at all).

Quoted 4 lines

I wondered if we wanted to enforce this? It would not seem totally silly for a driver that wasn't bothering to track the size of it's enabled channels to provide data_sz == maximum size it ever needs. Docs should definitely say if that's valid however, and it's definitely

less

Quoted 6 lines

than optimal because we may copy garbage that will then be overwritten by
the timestamp.

Quoted 2 lines

data_sz < scan_bytes is also probably not valid as that might

happen

Quoted 2 lines
Quoted 2 lines

if some drivers call this without the scan elements properly aligned which will definitely be problematic... Naturally in this case we

would

Quoted 1 line
Quoted 1 line

have to take into account timestamp possible alignment fill bytes as

it

Quoted 1 line
Quoted 1 line

could be possible (and likely) that data_sz won't include those

bits...

Quoted 3 lines

data_sz < scan_bytes is intentionally valid (and indeed I made one of the drivers do this). Given we are bouncing anyway, why insist

the

Quoted 1 line

driver provides space for padding + timestamp? That can be done

for

Quoted 1 line

the bounce buffer.

What I meant was that if data_sz < scan_bytes - timestamp - padding, then
the driver is most likely not guaranteeing properly aligned elements. I was
not that clear :)

Anyways, I still feel that if we are taking the data_sz parameter on the API,
we should handle it more strictly. But I do not feel too strong about this (as
you said, It's not expected to exist too much users of this) and in the end
of the day it still behaves the same way as 'iio_push_to_buffers_with_timestamp()'...

Quoted 13 lines
Quoted 2 lines

Alternatively, we could be consistent with

'iio_push_to_buffers_with_timestamp()'

Quoted 4 lines

and assume drivers play along in terms of alignment and size (so, removing the data_sz parameter)...

My point is, if we are taking the data_sz parameter, maybe we

should be pedantic

Quoted 5 lines

about it...

drivers/iio/industrialio-buffer.c int iio_push_to_buffers(struct iio_dev · line 1754

Quoted 2 lines
drivers/iio/industrialio-buffer.c +1 -0
+	if (iio_dev_opaque->bounce_buffer_size !=  indio_dev-
 >scan_bytes) {

Where do we set ' bounce_buffer_size '? I guess we do not want

to

Quoted 17 lines
Quoted 1 line

get here all the time...

:( oops

Quoted 7 lines
Quoted 5 lines
drivers/iio/industrialio-buffer.c +4 -0
+		iio_dev_opaque->bounce_buffer =
+			devm_krealloc(&indio_dev->dev,
+				      iio_dev_opaque->bounce_buffer,
+				      indio_dev->scan_bytes,

GFP_KERNEL);

I'm also not a big fan of realloc so I prefer to avoid it unless really

needed...

Quoted 1 line

Here, maybe we could just allocate this once during device/buffer

initialization

Quoted 1 line

and allocate it for the maximum possible size, i.e all scan elements

enabled...

Quoted 7 lines

We can but to do that requires more invasive changes. Perhaps not horrible ones,
but we'd need to add stuff to caller drivers to configure the bounce buffer, or
some flag to see they needed to enable support for this. Only 3 drivers out of
all those in IIO are currently using this code (might be a few others

that

Quoted 2 lines

could
take advantage but not many - so need to keep it localized).

I was not thinking that way. I was just thinking in always allocating the bounce
buffer. But I do agree that it is less than ideal to allocate something that will
almost never be used...

Quoted 4 lines

I suppose we could have an iio_unaligned_push_to_buffers_prepare() to be called from the update_scan_mode()
callback (typed to be useable as that callback if nothing else to be

done

Quoted 2 lines

in
the relevant drivers.

It could also be an option... If we want to not use realloc, we would have to manually
control the lifespan of the buffer and free it in the disable path. But I guess that might feel
like too much handling...

Quoted 9 lines

Anyways, maybe sticking with realloc, we could just use a more normal pattern like:

tmp = devm_realloc(bounce_fuffer, ...);
if (!tmp)

    error;

bounce_buffer = tmp;

I just read your reply to Andy about this and I think you are right... If the new allocation fails, the older pointer is still preserved. However, I find the above form a bit more readable and straight :)

- Nuno Sá

#10 lore raw

Re: [RFC PATCH 1/4] iio: core: Introduce iio_push_to_buffers_with_ts_na() for non aligned case.

Quoted 2 lines · Nuno
Quoted 2 lines

From: Sa, Nuno <[email protected]>
Sent: Monday, May 3, 2021 9:15 AM

...

Quoted 7 lines
Quoted 7 lines

Anyways, maybe sticking with realloc, we could just use a more normal pattern like:

tmp = devm_realloc(bounce_fuffer, ...);
if (!tmp)

    error;
Quoted 6 lines
Quoted 2 lines

bounce_buffer = tmp;

I just read your reply to Andy about this and I think you are right... If the new allocation fails, the older pointer is still preserved. However, I find the above form a bit more readable and straight :)

You beat me up to it.

While devm_realloc() may actually be safer, the pattern is that: we should not rely on specifics of the custom implementation, it will really confuse new contributors or people who may take this as a "good' (which is not) pattern.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author maintainer
#11 lore raw

Re: [RFC PATCH 1/4] iio: core: Introduce iio_push_to_buffers_with_ts_na() for non aligned case.

In reply to Sa, Nuno
Quoted 118 lines · Nuno
Quoted 118 lines

-----Original Message-----
From: Sa, Nuno <[email protected]>
Sent: Monday, May 3, 2021 9:15 AM
To: Jonathan Cameron <[email protected]>
Cc: [email protected]; Jonathan Cameron <[email protected]>
Subject: RE: [RFC PATCH 1/4] iio: core: Introduce iio_push_to_buffers_with_ts_na() for non aligned case.

Quoted 22 lines

-----Original Message-----
From: Jonathan Cameron <[email protected]>
Sent: Sunday, May 2, 2021 6:09 PM
To: Sa, Nuno <[email protected]>
Cc: [email protected]; Jonathan Cameron <[email protected]>
Subject: Re: [RFC PATCH 1/4] iio: core: Introduce iio_push_to_buffers_with_ts_na() for non aligned case.

Quoted 9 lines · Nuno
Quoted 9 lines

From: Jonathan Cameron <[email protected]>
Sent: Saturday, May 1, 2021 7:25 PM
To: [email protected]
Cc: Jonathan Cameron <[email protected]>
Subject: [RFC PATCH 1/4] iio: core: Introduce iio_push_to_buffers_with_ts_na() for non aligned case.

From: Jonathan Cameron <[email protected]>

Quoted 9 lines
Quoted 3 lines
Quoted 3 lines

Whilst it is almost always possible to arrange for scan data to be read directly into a buffer that is suitable for passing to iio_push_to_buffers_with_timestamp(), there are a few places

where

Quoted 3 lines
Quoted 3 lines

leading data needs to be skipped over.

For these cases introduce a function that will allocate an

appropriate

Quoted 1 line
Quoted 1 line

sized and aligned bounce buffer (if not already allocated) and

copy

Quoted 25 lines
Quoted 6 lines
Quoted 6 lines

the unaligned data into that before calling iio_push_to_buffers_with_timestamp() on the bounce buffer. We tie the lifespace of this buffer to that of the iio_dev.dev which should ensure no memory leaks occur.

Signed-off-by Jonathan Cameron

<[email protected]>

Quoted 8 lines

Review

Quoted 8 lines
---
 drivers/iio/industrialio-buffer.c | 36
+++++++++++++++++++++++++++++++
 include/linux/iio/buffer.h        |  4 ++++
 include/linux/iio/iio-opaque.h    |  4 ++++
 3 files changed, 44 insertions(+)
(quoted diff) +0 -0

b/drivers/iio/industrialio-

Quoted 9 lines
Quoted 9 lines

buffer.c
index 9a8e16c7e9af..818dfaa73665 100644

drivers/iio/industrialio-buffer.c +0 -0
@@ -1727,6 +1727,42 @@ int iio_push_to_buffers(struct iio_dev
 *indio_dev, const void *data)
 }
 EXPORT_SYMBOL_GPL(iio_push_to_buffers);
 
Quoted 2 lines
Quoted 2 lines

drivers/iio/industrialio-buffer.c int iio_push_to_buffers(struct iio_dev · line 1732

Quoted 2 lines
drivers/iio/industrialio-buffer.c +2 -0
+/**
+ * iio_push_to_buffers_with_ts_na() - push to registered  

buffer,

Quoted 11 lines
Quoted 7 lines

drivers/iio/industrialio-buffer.c int iio_push_to_buffers(struct iio_dev *indio_dev, const void *data)

Quoted 7 lines
drivers/iio/industrialio-buffer.c +7 -0
+ *    no alignment or space requirements.
+ * @indio_dev:		iio_dev structure for device.
+ * @data:		channel data excluding the timestamp.
+ * @data_sz:		size of data.
+ * @timestamp:		timestamp for the sample data.
+ *
+ * This special variant of iio_push_to_buffers_with_timetamp()  

does

Quoted 1 line

drivers/iio/industrialio-buffer.c int iio_push_to_buffers(struct iio_dev · line 1740

Quoted 1 line
drivers/iio/industrialio-buffer.c +1 -0
+ * not require space for the timestamp, or 8 byte alignment of  

data.

Quoted 1 line
Quoted 1 line
drivers/iio/industrialio-buffer.c +1 -0
+ * It does however require an allocation on first call and  

additional

Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
drivers/iio/industrialio-buffer.c +4 -0
+ * coppies on all calls, so should be avoided if possible
+ */
+int iio_push_to_buffers_with_ts_na(struct iio_dev *indio_dev,
+				   const void *data,
Quoted 8 lines

drivers/iio/industrialio-buffer.c int iio_push_to_buffers(struct iio_dev *indio_dev, const void *data) · line 1750

Quoted 7 lines
drivers/iio/industrialio-buffer.c +6 -0
+				   size_t data_sz,
+				   int64_t timestamp)
+{
+	struct iio_dev_opaque *iio_dev_opaque =
 to_iio_dev_opaque(indio_dev);
+
+	data_sz = min_t(size_t, indio_dev->scan_bytes, data_sz);  

I'm not really sure about this. Is it really a good idea to silently

Quoted 4 lines

truncate

Quoted 1 line

the data if some erroneous driver calls this with data_sz >

scan_bytes?

Quoted 1 line

(I don't think it's ever valid for data_sz to be bigger than

scan_bytes)

Quoted 5 lines

It's not probably not. This is just a fairly nasty way of papering over anyone doing that.

Quoted 1 line

We might be discarding data that userland actually requested. So,

I'm

Quoted 10 lines
Quoted 1 line

not sure if it would not be better to be strict here and return error,

so that

Quoted 1 line

driver developers could catch this early on...

I'm fairly sure we'll never be discarding data userspace requested because scan_bytes will be the maximum possible and is the size of the
kfifo element. Any more data that comes in is going to be dropped anyway
assuming it ends up in a kfifo_put()

Quoted 15 lines
Quoted 7 lines

I think it depends. Think on a totally erroneous driver that does not track
enabled elements and just pushes all elements to IIO buffers. If userspace
just requested, let's say, the last 2 elements, I believe those would be definitely discarded by this... More subtle things could also happen I guess...

Quoted 8 lines

In this case, we can arguably say that the driver is completely broken and while
I'm fairly sure such a scenario would never pass through revision, I think that
if we make things more strict, we might minimize the chances of such patches
to appear in the first place (unless someone is sending patches without testing them at all).

Like me ;) *laughs*

Agreed there is nothing stopping at least adding a warning print here and it might help weed out gross bugs even if it is imprecise.

Quoted 25 lines
Quoted 25 lines
Quoted 5 lines

I wondered if we wanted to enforce this? It would not seem totally silly for a driver that wasn't bothering to track the size of it's enabled channels to provide data_sz == maximum size it ever needs. Docs should definitely say if that's valid however, and it's definitely

less

Quoted 6 lines

than optimal because we may copy garbage that will then be overwritten by
the timestamp.

Quoted 2 lines

data_sz < scan_bytes is also probably not valid as that might

happen

Quoted 2 lines
Quoted 2 lines

if some drivers call this without the scan elements properly aligned which will definitely be problematic... Naturally in this case we

would

Quoted 1 line
Quoted 1 line

have to take into account timestamp possible alignment fill bytes as

it

Quoted 1 line
Quoted 1 line

could be possible (and likely) that data_sz won't include those

bits...

Quoted 3 lines

data_sz < scan_bytes is intentionally valid (and indeed I made one of the drivers do this). Given we are bouncing anyway, why insist

the

Quoted 1 line

driver provides space for padding + timestamp? That can be done

Quoted 8 lines
Quoted 8 lines

for

Quoted 1 line

the bounce buffer.

What I meant was that if data_sz < scan_bytes - timestamp - padding, then
the driver is most likely not guaranteeing properly aligned elements. I was
not that clear :)

If you hit that condition I think it would be a bug as not enough data provided. We could easily enforce data_sz < scan_bytes - timstamp, but the padding would need to be stashed somewhere. + I've been lazy in one of the drivers because I couldn't figure it out in a trivial fashion - perhaps I should take another look at that one and see if I can make the check accurate rather than a worst case bound.

Quoted 49 lines
Quoted 49 lines

Anyways, I still feel that if we are taking the data_sz parameter on the API,
we should handle it more strictly. But I do not feel too strong about this (as
you said, It's not expected to exist too much users of this) and in the end
of the day it still behaves the same way as 'iio_push_to_buffers_with_timestamp()'...

Quoted 13 lines
Quoted 2 lines

Alternatively, we could be consistent with

'iio_push_to_buffers_with_timestamp()'

Quoted 4 lines

and assume drivers play along in terms of alignment and size (so, removing the data_sz parameter)...

My point is, if we are taking the data_sz parameter, maybe we

should be pedantic

Quoted 5 lines

about it...

drivers/iio/industrialio-buffer.c int iio_push_to_buffers(struct iio_dev · line 1754

Quoted 2 lines
drivers/iio/industrialio-buffer.c +1 -0
+	if (iio_dev_opaque->bounce_buffer_size !=  indio_dev-  
 >scan_bytes) {  

Where do we set ' bounce_buffer_size '? I guess we do not want

to

Quoted 17 lines
Quoted 1 line

get here all the time...

:( oops

Quoted 7 lines
Quoted 5 lines
drivers/iio/industrialio-buffer.c +4 -0
+		iio_dev_opaque->bounce_buffer =
+			devm_krealloc(&indio_dev->dev,
+				      iio_dev_opaque->bounce_buffer,
+				      indio_dev->scan_bytes,

GFP_KERNEL);

I'm also not a big fan of realloc so I prefer to avoid it unless really

needed...

Quoted 1 line

Here, maybe we could just allocate this once during device/buffer

initialization

Quoted 1 line

and allocate it for the maximum possible size, i.e all scan elements

enabled...

Quoted 7 lines

We can but to do that requires more invasive changes. Perhaps not horrible ones,
but we'd need to add stuff to caller drivers to configure the bounce buffer, or
some flag to see they needed to enable support for this. Only 3 drivers out of
all those in IIO are currently using this code (might be a few others

that

Quoted 8 lines
Quoted 8 lines
Quoted 2 lines

could
take advantage but not many - so need to keep it localized).

I was not thinking that way. I was just thinking in always allocating the bounce
buffer. But I do agree that it is less than ideal to allocate something that will
almost never be used...

Ah got you. We could do that, but I think it wants to be opt in just because I'd feel bad adding an extra allocation to every driver.

Quoted 22 lines
Quoted 22 lines
Quoted 5 lines

I suppose we could have an iio_unaligned_push_to_buffers_prepare() to be called from the update_scan_mode()
callback (typed to be useable as that callback if nothing else to be

done

Quoted 2 lines

in
the relevant drivers.

It could also be an option... If we want to not use realloc, we would have to manually
control the lifespan of the buffer and free it in the disable path. But I guess that might feel
like too much handling...

Anyways, maybe sticking with realloc, we could just use a more normal pattern like:

tmp = devm_realloc(bounce_fuffer, ...);
if (!tmp)

    error;
Quoted 7 lines
Quoted 2 lines

bounce_buffer = tmp;

I just read your reply to Andy about this and I think you are right... If the new allocation fails, the older pointer is still preserved. However, I find the above form a bit more readable and straight :)

Understood. Given it's causing confusion (and I had to think about it in the first place), simply have the more standard pattern is probably wise even if strictly speaking unnecessary! Give people what they expect to see rather than being 'clever' :)

Quoted 1 line

- Nuno Sá

maintainer
#12 lore raw

RE: [RFC PATCH 1/4] iio: core: Introduce iio_push_to_buffers_with_ts_na() for non aligned case.

Quoted 161 lines

-----Original Message-----
From: Jonathan Cameron <[email protected]>
Sent: Monday, May 3, 2021 12:40 PM
To: Sa, Nuno <[email protected]>
Cc: [email protected]; Jonathan Cameron <[email protected]>
Subject: Re: [RFC PATCH 1/4] iio: core: Introduce iio_push_to_buffers_with_ts_na() for non aligned case.

Quoted 32 lines · Nuno
Quoted 32 lines

-----Original Message-----
From: Sa, Nuno <[email protected]>
Sent: Monday, May 3, 2021 9:15 AM
To: Jonathan Cameron <[email protected]>
Cc: [email protected]; Jonathan Cameron <[email protected]>
Subject: RE: [RFC PATCH 1/4] iio: core: Introduce iio_push_to_buffers_with_ts_na() for non aligned case.

Quoted 20 lines

-----Original Message-----
From: Jonathan Cameron <[email protected]>
Sent: Sunday, May 2, 2021 6:09 PM
To: Sa, Nuno <[email protected]>
Cc: [email protected]; Jonathan Cameron <[email protected]>
Subject: Re: [RFC PATCH 1/4] iio: core: Introduce iio_push_to_buffers_with_ts_na() for non aligned case.

Quoted 7 lines · Nuno
Quoted 7 lines

From: Jonathan Cameron <[email protected]>
Sent: Saturday, May 1, 2021 7:25 PM
To: [email protected]
Cc: Jonathan Cameron <[email protected]>
Subject: [RFC PATCH 1/4] iio: core: Introduce iio_push_to_buffers_with_ts_na() for non aligned case.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

From: Jonathan Cameron <[email protected]>

Whilst it is almost always possible to arrange for scan data to

be

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

read directly into a buffer that is suitable for passing to iio_push_to_buffers_with_timestamp(), there are a few

places

Quoted 9 lines
Quoted 9 lines
Quoted 6 lines

where

Quoted 3 lines
Quoted 3 lines

leading data needs to be skipped over.

For these cases introduce a function that will allocate an

appropriate

Quoted 1 line
Quoted 1 line

sized and aligned bounce buffer (if not already allocated) and

copy

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

the unaligned data into that before calling iio_push_to_buffers_with_timestamp() on the bounce

buffer.

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 4 lines
Quoted 4 lines

We tie the lifespace of this buffer to that of the iio_dev.dev which should ensure no memory leaks occur.

Signed-off-by Jonathan Cameron

<[email protected]>

Quoted 7 lines
Quoted 7 lines
---
 drivers/iio/industrialio-buffer.c | 36
+++++++++++++++++++++++++++++++
 include/linux/iio/buffer.h        |  4 ++++
 include/linux/iio/iio-opaque.h    |  4 ++++
 3 files changed, 44 insertions(+)
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 1 line

Review

Quoted 1 line
(quoted diff) +0 -0

b/drivers/iio/industrialio-

Quoted 5 lines

drivers/iio/industrialio-buffer.c int iio_push_to_buffers(struct

Quoted 5 lines

buffer.c
index 9a8e16c7e9af..818dfaa73665 100644

drivers/iio/industrialio-buffer.c +0 -0
@@ -1727,6 +1727,42 @@ int iio_push_to_buffers(struct

iio_dev

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
drivers/iio/industrialio-buffer.c +2 -0
 *indio_dev, const void *data)
 }
 EXPORT_SYMBOL_GPL(iio_push_to_buffers);
 
+/**
+ * iio_push_to_buffers_with_ts_na() - push to registered
Quoted 8 lines
Quoted 8 lines

buffer,

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
drivers/iio/industrialio-buffer.c +7 -0
+ *    no alignment or space requirements.
+ * @indio_dev:		iio_dev structure for device.
+ * @data:		channel data excluding the timestamp.
+ * @data_sz:		size of data.
+ * @timestamp:		timestamp for the sample data.
+ *
+ * This special variant of

iio_push_to_buffers_with_timetamp()

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

does

Quoted 1 line
Quoted 1 line
drivers/iio/industrialio-buffer.c +1 -0
+ * not require space for the timestamp, or 8 byte alignment

of

Quoted 6 lines
Quoted 6 lines
Quoted 2 lines

data.

Quoted 1 line
Quoted 1 line
drivers/iio/industrialio-buffer.c +1 -0
+ * It does however require an allocation on first call and

additional

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
drivers/iio/industrialio-buffer.c +3 -0
+ * coppies on all calls, so should be avoided if possible
+ */
+int iio_push_to_buffers_with_ts_na(struct iio_dev

*indio_dev,

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

drivers/iio/industrialio-buffer.c int iio_push_to_buffers(struct iio_dev *indio_dev, const void *data)

Quoted 8 lines
drivers/iio/industrialio-buffer.c +7 -0
+				   const void *data,
+				   size_t data_sz,
+				   int64_t timestamp)
+{
+	struct iio_dev_opaque *iio_dev_opaque =
 to_iio_dev_opaque(indio_dev);
+
+	data_sz = min_t(size_t, indio_dev->scan_bytes,

data_sz);

Quoted 8 lines
Quoted 8 lines
Quoted 5 lines
Quoted 1 line

I'm not really sure about this. Is it really a good idea to silently

truncate

Quoted 1 line

the data if some erroneous driver calls this with data_sz >

scan_bytes?

Quoted 1 line

(I don't think it's ever valid for data_sz to be bigger than

scan_bytes)

Quoted 2 lines

It's not probably not. This is just a fairly nasty way of papering

over

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

anyone doing that.

Quoted 1 line

We might be discarding data that userland actually requested.

So,

Quoted 2 lines
Quoted 2 lines

I'm

Quoted 1 line
Quoted 1 line

not sure if it would not be better to be strict here and return

error,

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

so that

Quoted 1 line

driver developers could catch this early on...

I'm fairly sure we'll never be discarding data userspace requested because scan_bytes will be the maximum possible and is the size

of

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

the
kfifo element. Any more data that comes in is going to be

dropped

Quoted 8 lines
Quoted 8 lines
Quoted 2 lines

anyway
assuming it ends up in a kfifo_put()

I think it depends. Think on a totally erroneous driver that does not track
enabled elements and just pushes all elements to IIO buffers. If userspace
just requested, let's say, the last 2 elements, I believe those would

be

Quoted 1 line
Quoted 1 line

definitely discarded by this... More subtle things could also happen

I

Quoted 2 lines
Quoted 2 lines

guess...
In this case, we can arguably say that the driver is completely

broken

Quoted 4 lines
Quoted 4 lines

and while
I'm fairly sure such a scenario would never pass through revision, I think that
if we make things more strict, we might minimize the chances of

Quoted 7 lines

such

Quoted 2 lines
Quoted 2 lines

patches
to appear in the first place (unless someone is sending patches

without

Quoted 1 line
Quoted 1 line

testing them at all).

Like me ;) *laughs*

Well, at least you warned everyone about it :)

Quoted 46 lines

Agreed there is nothing stopping at least adding a warning print here and it might help weed out gross bugs even if it is imprecise.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

I wondered if we wanted to enforce this? It would not seem

totally

Quoted 1 line
Quoted 1 line
Quoted 1 line

silly for a driver that wasn't bothering to track the size of it's

enabled

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

channels to provide data_sz == maximum size it ever needs. Docs should definitely say if that's valid however, and it's

definitely

Quoted 9 lines
Quoted 9 lines

less

Quoted 6 lines

than optimal because we may copy garbage that will then be overwritten by
the timestamp.

Quoted 2 lines

data_sz < scan_bytes is also probably not valid as that might

happen

Quoted 1 line
Quoted 1 line

if some drivers call this without the scan elements properly

aligned

Quoted 3 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line

which will definitely be problematic... Naturally in this case we

would

Quoted 1 line
Quoted 1 line

have to take into account timestamp possible alignment fill

bytes as

Quoted 5 lines
Quoted 5 lines

it

Quoted 1 line
Quoted 1 line

could be possible (and likely) that data_sz won't include those

bits...

Quoted 2 lines

data_sz < scan_bytes is intentionally valid (and indeed I made

one

Quoted 3 lines
Quoted 3 lines
Quoted 1 line

of the drivers do this). Given we are bouncing anyway, why insist

the

Quoted 1 line

driver provides space for padding + timestamp? That can be

done

Quoted 4 lines
Quoted 4 lines

for

Quoted 1 line

the bounce buffer.

What I meant was that if data_sz < scan_bytes - timestamp -

padding,

Quoted 2 lines
Quoted 2 lines

then
the driver is most likely not guaranteeing properly aligned

elements. I

Quoted 2 lines
Quoted 2 lines

was
not that clear :)

Quoted 6 lines

If you hit that condition I think it would be a bug as not enough data provided.
We could easily enforce data_sz < scan_bytes - timstamp, but the padding would
need to be stashed somewhere. + I've been lazy in one of the drivers because

I think with data_sz we can easily calculate the needed padding for the timestamp... Naturally we would need to clearly state in the docs (probably done already) that data_sz should not contain any bits related with the timestamp element...

Quoted 70 lines

I couldn't figure it out in a trivial fashion - perhaps I should take another look at that one and see if I can make the check accurate rather than a worst
case bound.

Quoted 2 lines
Quoted 2 lines

Anyways, I still feel that if we are taking the data_sz parameter on

the

Quoted 4 lines
Quoted 4 lines

API,
we should handle it more strictly. But I do not feel too strong about this (as
you said, It's not expected to exist too much users of this) and in

the

Quoted 6 lines
Quoted 6 lines

end
of the day it still behaves the same way as
'iio_push_to_buffers_with_timestamp()'...

Quoted 2 lines
Quoted 2 lines

Alternatively, we could be consistent with

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

'iio_push_to_buffers_with_timestamp()'

Quoted 4 lines

and assume drivers play along in terms of alignment and size (so, removing the data_sz parameter)...

My point is, if we are taking the data_sz parameter, maybe we

should be pedantic

Quoted 2 lines

about it...

drivers/iio/industrialio-buffer.c int iio_push_to_buffers(struct · line 1753

Quoted 1 line
drivers/iio/industrialio-buffer.c +1 -0
+	if (iio_dev_opaque->bounce_buffer_size !=

indio_dev-

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

drivers/iio/industrialio-buffer.c int iio_push_to_buffers(struct · line 1754

Quoted 1 line
drivers/iio/industrialio-buffer.c +0 -0
 >scan_bytes) {

Where do we set ' bounce_buffer_size '? I guess we do not

want

Quoted 13 lines
Quoted 8 lines

to

Quoted 7 lines
Quoted 1 line

get here all the time...

:( oops

Quoted 3 lines

drivers/iio/industrialio-buffer.c int iio_push_to_buffers(struct iio_dev *indio_dev, const void *data)

Quoted 3 lines
drivers/iio/industrialio-buffer.c +3 -0
+		iio_dev_opaque->bounce_buffer =
+			devm_krealloc(&indio_dev->dev,
+				      iio_dev_opaque-

bounce_buffer,

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 2 lines
drivers/iio/industrialio-buffer.c +1 -0
+				      indio_dev->scan_bytes,

GFP_KERNEL);

I'm also not a big fan of realloc so I prefer to avoid it unless

really

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

needed...

Quoted 1 line

Here, maybe we could just allocate this once during

device/buffer

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

initialization

Quoted 1 line

and allocate it for the maximum possible size, i.e all scan

elements

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

enabled...

We can but to do that requires more invasive changes. Perhaps

not

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

horrible ones,
but we'd need to add stuff to caller drivers to configure the

bounce

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

buffer, or
some flag to see they needed to enable support for this. Only 3 drivers out of
all those in IIO are currently using this code (might be a few

others

Quoted 5 lines
Quoted 5 lines

that

Quoted 2 lines

could
take advantage but not many - so need to keep it localized).

I was not thinking that way. I was just thinking in always allocating

the

Quoted 8 lines
Quoted 4 lines
Quoted 4 lines

bounce
buffer. But I do agree that it is less than ideal to allocate something that will
almost never be used...

Ah got you. We could do that, but I think it wants to be opt in just because
I'd feel bad adding an extra allocation to every driver.

Agreed...

- Nuno Sá

author maintainer
#13 lore raw

RFC PATCH 2/4 iio: adc: ti-adc108s102: Fix alignment of buffer pushed to iio buffers.

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Use the newly introduce iio_push_to_buffers_with_ts_na() function to ensure a bounce buffer is used to provide the required alignment and space padding needed by the IIO core which requires the timestamp is naturally aligned. There will be a performance cost to this change but it will ensure the driver works on platforms that do not support unaligned 8 byte assignements, and with consumer drivers that may assume natural alignment of the timestamp.

Issue found as part of an audit of all calls to
iio_push_to_buffers_with_timestamp()

Fixes 7e87d11c9bda ("iio: adc: Add support for TI ADC108S102 and ADC128S102")
Cc Jan Kiszka <[email protected]>
---
 drivers/iio/adc/ti-adc108s102.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
drivers/iio/adc/ti-adc108s102.c +6 -5
index 183b2245e89b..7aeed3503829 100644
@@ -75,9 +75,9 @@ struct adc108s102_state {
 	 *  rx_buf: |XX|R0|R1|R2|R3|R4|R5|R6|R7|tt|tt|tt|tt|
 	 *
 	 *  tx_buf: 8 channel read commands, plus 1 dummy command
-	 *  rx_buf: 1 dummy response, 8 channel responses, plus 64-bit timestamp
+	 *  rx_buf: 1 dummy response, 8 channel responses
 	 */
-	__be16				rx_buf[13] ____cacheline_aligned;
+	__be16				rx_buf[9] ____cacheline_aligned;
 	__be16				tx_buf[9] ____cacheline_aligned;
 };
 
@@ -149,9 +149,10 @@ static irqreturn_t adc108s102_trigger_handler(int irq, void *p)
 		goto out_notify;
 
 	/* Skip the dummy response in the first slot */
-	iio_push_to_buffers_with_timestamp(indio_dev,
-					   (u8 *)&st->rx_buf[1],
-					   iio_get_time_ns(indio_dev));
+	iio_push_to_buffers_with_ts_na(indio_dev,
+				       &st->rx_buf[1],
+				       st->ring_xfer.len - sizeof(st->rx_buf[1]),
+				       iio_get_time_ns(indio_dev));
 
 out_notify:
 	iio_trigger_notify_done(indio_dev->trig);
Signature · 1 line
2.31.1
author maintainer
#14 lore raw

RFC PATCH 3/4 iio: gyro: mpu3050: Fix alignment and size issues with buffers.

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Fix a set of closely related issues.
1. When using fifo_values() there was not enough space for the timestamp to be inserted by iio_push_to_buffers_with_timestamp()
2. fifo_values() did not meet the alignment requirement of iio_push_to_buffers_with_timestamp()
3. hw_values did not meet the alignment requirement either.

1 and 2 fixed by using new iio_push_to_buffers_with_ts_na() which has no alignment or space padding requirements.
3 fixed by introducing a structure that makes the space and alignment requirements explicit.

Fixes 3904b28efb2c ("iio: gyro: Add driver for the MPU-3050 gyroscope")
Cc Linus Walleij <[email protected]>
---
 drivers/iio/gyro/mpu3050-core.c | 24 +++++++++++-------------
 1 file changed, 11 insertions(+), 13 deletions(-)
drivers/iio/gyro/mpu3050-core.c +11 -13
index ac90be03332a..a7fdfe811258 100644
@@ -462,13 +462,10 @@ static irqreturn_t mpu3050_trigger_handler(int irq, void *p)
 	struct iio_dev *indio_dev = pf->indio_dev;
 	struct mpu3050 *mpu3050 = iio_priv(indio_dev);
 	int ret;
-	/*
-	 * Temperature 1*16 bits
-	 * Three axes 3*16 bits
-	 * Timestamp 64 bits (4*16 bits)
-	 * Sum total 8*16 bits
-	 */
-	__be16 hw_values[8];
+	struct {
+		__be16 chans[4];
+		s64 timestamp __aligned(8);
+	} scan;
 	s64 timestamp;
 	unsigned int datums_from_fifo = 0;
 
@@ -563,9 +560,10 @@ static irqreturn_t mpu3050_trigger_handler(int irq, void *p)
 				fifo_values[4]);
 
 			/* Index past the footer (fifo_values[0]) and push */
-			iio_push_to_buffers_with_timestamp(indio_dev,
-							   &fifo_values[1],
-							   timestamp);
+			iio_push_to_buffers_with_ts_na(indio_dev,
+						       &fifo_values[1],
+						       sizeof(__be16) * 4,
+						       timestamp);
 
 			fifocnt -= toread;
 			datums_from_fifo++;
@@ -623,15 +621,15 @@ static irqreturn_t mpu3050_trigger_handler(int irq, void *p)
 		goto out_trigger_unlock;
 	}
 
-	ret = regmap_bulk_read(mpu3050->map, MPU3050_TEMP_H, &hw_values,
-			       sizeof(hw_values));
+	ret = regmap_bulk_read(mpu3050->map, MPU3050_TEMP_H, scan.chans,
+			       sizeof(scan.chans));
 	if (ret) {
 		dev_err(mpu3050->dev,
 			"error reading axis data\n");
 		goto out_trigger_unlock;
 	}
 
-	iio_push_to_buffers_with_timestamp(indio_dev, hw_values, timestamp);
+	iio_push_to_buffers_with_timestamp(indio_dev, &scan, timestamp);
 
 out_trigger_unlock:
 	mutex_unlock(&mpu3050->lock);
Signature · 1 line
2.31.1
Reviewed-by on v1 3/4
#15 lore raw

Re: [RFC PATCH 3/4] iio: gyro: mpu3050: Fix alignment and size issues with buffers.

Quoted 9 lines · Jonathan Cameron

From: Jonathan Cameron <[email protected]>

Fix a set of closely related issues.
1. When using fifo_values() there was not enough space for the timestamp to be inserted by iio_push_to_buffers_with_timestamp()
2. fifo_values() did not meet the alignment requirement of iio_push_to_buffers_with_timestamp()
3. hw_values did not meet the alignment requirement either.

Quoted 8 lines

1 and 2 fixed by using new iio_push_to_buffers_with_ts_na() which has no alignment or space padding requirements.
3 fixed by introducing a structure that makes the space and alignment requirements explicit.

Fixes 3904b28efb2c ("iio: gyro: Add driver for the MPU-3050 gyroscope")
Cc Linus Walleij <[email protected]>

Again a very nice fix:

Again I wonder how we can help driver authors to always get this right.

Yours,
Linus Walleij

author maintainer
#16 lore raw

RFC PATCH 4/4 iio: imu: adis16400: Fix buffer alignment requirements.

In reply to Jonathan Cameron

From: Jonathan Cameron <[email protected]>

iio_push_to_buffers_with_timestamp() requires that the buffer is 8 byte alignment to ensure an inserted timestamp is naturally aligned.

This requirement was not met here when burst mode is in use beause of a leading u16. Use the new iio_push_to_buffers_with_ts_na() function that has more relaxed requirements.

It is somewhat complex to access that actual data length, but a safe bound can be found by using scan_bytes - sizeof(timestamp) so that is used in this path.

More efficient approaches exist, but this ensure correctness at the cost of using a bounce buffer.

Fixes 5075e0720d93 ("iio: imu: adis: generalize burst mode support")
Cc Nuno Sa <[email protected]>
---
 drivers/iio/imu/adis16400.c | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)
drivers/iio/imu/adis16400.c +15 -5
index 768aa493a1a6..c6d03a37373b 100644
@@ -663,13 +663,23 @@ static irqreturn_t adis16400_trigger_handler(int irq, void *p)
 		spi_setup(st->adis.spi);
 	}
 
-	if (st->variant->flags & ADIS16400_BURST_DIAG_STAT)
+	if (st->variant->flags & ADIS16400_BURST_DIAG_STAT) {
 		buffer = adis->buffer + sizeof(u16);
-	else
-		buffer = adis->buffer;
+		/*
+		 * The size here is always larger than, or equal to the true
+		 * size of the channel data. This may result in a larger copy
+		 * than necessary, but as the target buffer will be
+		 * buffer->scan_bytes this will be safe.
+		 */
+		iio_push_to_buffers_with_ts_na(indio_dev, buffer,
+					       indio_dev->scan_bytes - sizeof(pf->timestamp),
+					       pf->timestamp);
+	} else {
+		iio_push_to_buffers_with_timestamp(indio_dev,
+						   adis->buffer,
+						   pf->timestamp);
+	}
 
-	iio_push_to_buffers_with_timestamp(indio_dev, buffer,
-		pf->timestamp);
 
 	iio_trigger_notify_done(indio_dev->trig);
 
Signature · 1 line
2.31.1
maintainer Reviewed-by on v1 4/4
#17 lore raw

RE: [RFC PATCH 4/4] iio: imu: adis16400: Fix buffer alignment requirements.

Quoted 61 lines

From: Jonathan Cameron <[email protected]>
Sent: Saturday, May 1, 2021 7:25 PM
To: [email protected]
Cc: Jonathan Cameron <[email protected]>; Sa, Nuno <[email protected]>
Subject: [RFC PATCH 4/4] iio: imu: adis16400: Fix buffer alignment requirements.

From: Jonathan Cameron <[email protected]>

iio_push_to_buffers_with_timestamp() requires that the buffer is 8 byte alignment to ensure an inserted timestamp is naturally aligned.

This requirement was not met here when burst mode is in use beause of a leading u16. Use the new iio_push_to_buffers_with_ts_na() function
that has more relaxed requirements.

It is somewhat complex to access that actual data length, but a safe bound can be found by using scan_bytes - sizeof(timestamp) so that
is used in this path.

More efficient approaches exist, but this ensure correctness at the cost of using a bounce buffer.

Fixes 5075e0720d93 ("iio: imu: adis: generalize burst mode support")
Cc Nuno Sa <[email protected]>
---
 drivers/iio/imu/adis16400.c | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)
drivers/iio/imu/adis16400.c +3 -3
index 768aa493a1a6..c6d03a37373b 100644
@@ -663,13 +663,23 @@ static irqreturn_t
 adis16400_trigger_handler(int irq, void *p)
 		spi_setup(st->adis.spi);
 	}
 
-	if (st->variant->flags & ADIS16400_BURST_DIAG_STAT)
+	if (st->variant->flags & ADIS16400_BURST_DIAG_STAT) {
 		buffer = adis->buffer + sizeof(u16);
-	else
-		buffer = adis->buffer;
+		/*
+		 * The size here is always larger than, or equal to the

true

drivers/iio/imu/adis16400.c +1 -0
+		 * size of the channel data. This may result in a larger

copy

drivers/iio/imu/adis16400.c +5 -0
+		 * than necessary, but as the target buffer will be
+		 * buffer->scan_bytes this will be safe.
+		 */
+		iio_push_to_buffers_with_ts_na(indio_dev, buffer,
+					       indio_dev->scan_bytes -

sizeof(pf->timestamp),

drivers/iio/imu/adis16400.c static irqreturn_t adis16400_trigger_handler(int irq, void *p) · line 681

Quoted 6 lines
drivers/iio/imu/adis16400.c +6 -0
+					       pf->timestamp);
+	} else {
+		iio_push_to_buffers_with_timestamp(indio_dev,
+						   adis->buffer,
+						   pf->timestamp);
+	}

Hi Jonathan,

This looks good to me although I think there's some stuff to care in ' iio_push_to_buffers_with_ts_na()'. However, for this patch alone:

I will also check if I find any HW to test this...

- Nuno Sá