diff mbox series

[v2,11/12,RESEND] iio: buffer: introduce support for attaching more IIO buffers

Message ID 20210122162529.84978-12-alexandru.ardelean@analog.com (mailing list archive)
State New, archived
Headers show
Series iio: core,buffer: add support for multiple IIO buffers per IIO device | expand

Commit Message

Alexandru Ardelean Jan. 22, 2021, 4:25 p.m. UTC
With this change, calling iio_device_attach_buffer() will actually attach
more buffers.
Right now this doesn't do any validation of whether a buffer is attached
twice; maybe that can be added later (if needed). Attaching a buffer more
than once should yield noticeably bad results.

The first buffer is the legacy buffer, so a reference is kept to it.

At this point, accessing the data for the extra buffers (that are added
after the first one) isn't possible yet.

The iio_device_attach_buffer() is also changed to return an error code,
which for now is -ENOMEM if the array could not be realloc-ed for more
buffers.

Signed-off-by: Alexandru Ardelean <alexandru.ardelean@analog.com>
---
 drivers/iio/industrialio-buffer.c | 67 ++++++++++++++++++++++++-------
 include/linux/iio/buffer.h        |  4 +-
 include/linux/iio/buffer_impl.h   |  3 ++
 include/linux/iio/iio-opaque.h    |  4 ++
 4 files changed, 62 insertions(+), 16 deletions(-)

Comments

Dan Carpenter Jan. 25, 2021, 11:06 a.m. UTC | #1
Hi Alexandru,

I love your patch! Perhaps something to improve:

url:    https://github.com/0day-ci/linux/commits/Alexandru-Ardelean/iio-core-buffer-add-support-for-multiple-IIO-buffers-per-IIO-device/20210125-053419
base:   https://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio.git  togreg
config: i386-randconfig-m021-20210125 (attached as .config)
compiler: gcc-9 (Debian 9.3.0-20) 9.3.0

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>
Reported-by: Dan Carpenter <dan.carpenter@oracle.com>

smatch warnings:
drivers/iio/industrialio-buffer.c:1606 iio_buffer_alloc_sysfs_and_mask() error: uninitialized symbol 'cnt'.

vim +/cnt +1606 drivers/iio/industrialio-buffer.c

e16e0a778fec8ac1 Alexandru Ardelean 2020-09-17  1562  int iio_buffer_alloc_sysfs_and_mask(struct iio_dev *indio_dev)
d967cb6bd4e79c0c Lars-Peter Clausen 2014-11-26  1563  {
8b70c93a9d04de1e Alexandru Ardelean 2021-01-22  1564  	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
ff3f7e049aef9202 Alexandru Ardelean 2020-04-24  1565  	struct iio_buffer *buffer = indio_dev->buffer;
e16e0a778fec8ac1 Alexandru Ardelean 2020-09-17  1566  	const struct iio_chan_spec *channels;
8b70c93a9d04de1e Alexandru Ardelean 2021-01-22  1567  	int i, cnt, ret;
e16e0a778fec8ac1 Alexandru Ardelean 2020-09-17  1568  
e16e0a778fec8ac1 Alexandru Ardelean 2020-09-17  1569  	channels = indio_dev->channels;
e16e0a778fec8ac1 Alexandru Ardelean 2020-09-17  1570  	if (channels) {
e16e0a778fec8ac1 Alexandru Ardelean 2020-09-17  1571  		int ml = indio_dev->masklength;
e16e0a778fec8ac1 Alexandru Ardelean 2020-09-17  1572  
e16e0a778fec8ac1 Alexandru Ardelean 2020-09-17  1573  		for (i = 0; i < indio_dev->num_channels; i++)
e16e0a778fec8ac1 Alexandru Ardelean 2020-09-17  1574  			ml = max(ml, channels[i].scan_index + 1);
e16e0a778fec8ac1 Alexandru Ardelean 2020-09-17  1575  		indio_dev->masklength = ml;
e16e0a778fec8ac1 Alexandru Ardelean 2020-09-17  1576  	}
ff3f7e049aef9202 Alexandru Ardelean 2020-04-24  1577  
ff3f7e049aef9202 Alexandru Ardelean 2020-04-24  1578  	if (!buffer)
e16e0a778fec8ac1 Alexandru Ardelean 2020-09-17  1579  		return 0;
e16e0a778fec8ac1 Alexandru Ardelean 2020-09-17  1580  
8b70c93a9d04de1e Alexandru Ardelean 2021-01-22  1581  	for (i = 0; i < iio_dev_opaque->attached_buffers_cnt; i++) {
8b70c93a9d04de1e Alexandru Ardelean 2021-01-22  1582  		buffer = iio_dev_opaque->attached_buffers[i];
8b70c93a9d04de1e Alexandru Ardelean 2021-01-22  1583  		ret = __iio_buffer_alloc_sysfs_and_mask(buffer, indio_dev, i);
e2eb176de4748db8 Alexandru Ardelean 2021-01-22  1584  		if (ret)
8b70c93a9d04de1e Alexandru Ardelean 2021-01-22  1585  			goto error_unwind_sysfs_and_mask;

"cnt" is not set here.

8b70c93a9d04de1e Alexandru Ardelean 2021-01-22  1586  	}
d967cb6bd4e79c0c Lars-Peter Clausen 2014-11-26  1587  
e2eb176de4748db8 Alexandru Ardelean 2021-01-22  1588  	ret = sysfs_create_link(&indio_dev->dev.kobj,
e2eb176de4748db8 Alexandru Ardelean 2021-01-22  1589  				&indio_dev->buffer->buffer_dir,
e2eb176de4748db8 Alexandru Ardelean 2021-01-22  1590  				"buffer");
e2eb176de4748db8 Alexandru Ardelean 2021-01-22  1591  	if (ret)
8b70c93a9d04de1e Alexandru Ardelean 2021-01-22  1592  		goto error_unwind_sysfs_and_mask;

Oh here.

e2eb176de4748db8 Alexandru Ardelean 2021-01-22  1593  
e2eb176de4748db8 Alexandru Ardelean 2021-01-22  1594  	ret = sysfs_create_link(&indio_dev->dev.kobj,
e2eb176de4748db8 Alexandru Ardelean 2021-01-22  1595  				&indio_dev->buffer->scan_el_dir,
e2eb176de4748db8 Alexandru Ardelean 2021-01-22  1596  				"scan_elements");
e2eb176de4748db8 Alexandru Ardelean 2021-01-22  1597  	if (ret)
e2eb176de4748db8 Alexandru Ardelean 2021-01-22  1598  		goto error_remove_buffer_dir_link;
e2eb176de4748db8 Alexandru Ardelean 2021-01-22  1599  
e2eb176de4748db8 Alexandru Ardelean 2021-01-22  1600  	return 0;
e2eb176de4748db8 Alexandru Ardelean 2021-01-22  1601  
e2eb176de4748db8 Alexandru Ardelean 2021-01-22  1602  error_remove_buffer_dir_link:
e2eb176de4748db8 Alexandru Ardelean 2021-01-22  1603  	sysfs_remove_link(&indio_dev->dev.kobj, "buffer");
8b70c93a9d04de1e Alexandru Ardelean 2021-01-22  1604  	cnt = iio_dev_opaque->attached_buffers_cnt - 1;
8b70c93a9d04de1e Alexandru Ardelean 2021-01-22  1605  error_unwind_sysfs_and_mask:
8b70c93a9d04de1e Alexandru Ardelean 2021-01-22 @1606  	for (; cnt >= 0; cnt--) {
                                                               ^^^^^^^^
Uninitialized.

8b70c93a9d04de1e Alexandru Ardelean 2021-01-22  1607  		buffer = iio_dev_opaque->attached_buffers[cnt];
e2eb176de4748db8 Alexandru Ardelean 2021-01-22  1608  		__iio_buffer_free_sysfs_and_mask(buffer);
8b70c93a9d04de1e Alexandru Ardelean 2021-01-22  1609  	}
8b70c93a9d04de1e Alexandru Ardelean 2021-01-22  1610  	kfree(iio_dev_opaque->attached_buffers);
8b70c93a9d04de1e Alexandru Ardelean 2021-01-22  1611  	iio_dev_opaque->attached_buffers = NULL;
e2eb176de4748db8 Alexandru Ardelean 2021-01-22  1612  	return ret;
d967cb6bd4e79c0c Lars-Peter Clausen 2014-11-26  1613  }

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org
diff mbox series

Patch

diff --git a/drivers/iio/industrialio-buffer.c b/drivers/iio/industrialio-buffer.c
index 62cb90385246..2f429616e998 100644
--- a/drivers/iio/industrialio-buffer.c
+++ b/drivers/iio/industrialio-buffer.c
@@ -1561,9 +1561,10 @@  static void __iio_buffer_free_sysfs_and_mask(struct iio_buffer *buffer)
  */
 int iio_buffer_alloc_sysfs_and_mask(struct iio_dev *indio_dev)
 {
+	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
 	struct iio_buffer *buffer = indio_dev->buffer;
 	const struct iio_chan_spec *channels;
-	int i, ret;
+	int i, cnt, ret;
 
 	channels = indio_dev->channels;
 	if (channels) {
@@ -1577,15 +1578,18 @@  int iio_buffer_alloc_sysfs_and_mask(struct iio_dev *indio_dev)
 	if (!buffer)
 		return 0;
 
-	ret = __iio_buffer_alloc_sysfs_and_mask(buffer, indio_dev, 0);
-	if (ret)
-		return ret;
+	for (i = 0; i < iio_dev_opaque->attached_buffers_cnt; i++) {
+		buffer = iio_dev_opaque->attached_buffers[i];
+		ret = __iio_buffer_alloc_sysfs_and_mask(buffer, indio_dev, i);
+		if (ret)
+			goto error_unwind_sysfs_and_mask;
+	}
 
 	ret = sysfs_create_link(&indio_dev->dev.kobj,
 				&indio_dev->buffer->buffer_dir,
 				"buffer");
 	if (ret)
-		goto error_free_sysfs_and_mask;
+		goto error_unwind_sysfs_and_mask;
 
 	ret = sysfs_create_link(&indio_dev->dev.kobj,
 				&indio_dev->buffer->scan_el_dir,
@@ -1597,8 +1601,14 @@  int iio_buffer_alloc_sysfs_and_mask(struct iio_dev *indio_dev)
 
 error_remove_buffer_dir_link:
 	sysfs_remove_link(&indio_dev->dev.kobj, "buffer");
-error_free_sysfs_and_mask:
-	__iio_buffer_free_sysfs_and_mask(buffer);
+	cnt = iio_dev_opaque->attached_buffers_cnt - 1;
+error_unwind_sysfs_and_mask:
+	for (; cnt >= 0; cnt--) {
+		buffer = iio_dev_opaque->attached_buffers[cnt];
+		__iio_buffer_free_sysfs_and_mask(buffer);
+	}
+	kfree(iio_dev_opaque->attached_buffers);
+	iio_dev_opaque->attached_buffers = NULL;
 	return ret;
 }
 
@@ -1610,7 +1620,9 @@  int iio_buffer_alloc_sysfs_and_mask(struct iio_dev *indio_dev)
  */
 void iio_buffer_free_sysfs_and_mask(struct iio_dev *indio_dev)
 {
+	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
 	struct iio_buffer *buffer = indio_dev->buffer;
+	int i;
 
 	if (!buffer)
 		return;
@@ -1618,7 +1630,13 @@  void iio_buffer_free_sysfs_and_mask(struct iio_dev *indio_dev)
 	sysfs_remove_link(&indio_dev->dev.kobj, "scan_elements");
 	sysfs_remove_link(&indio_dev->dev.kobj, "buffer");
 
-	__iio_buffer_free_sysfs_and_mask(buffer);
+	for (i = iio_dev_opaque->attached_buffers_cnt - 1; i >= 0; i--) {
+		buffer = iio_dev_opaque->attached_buffers[i];
+		__iio_buffer_free_sysfs_and_mask(buffer);
+	}
+
+	kfree(iio_dev_opaque->attached_buffers);
+	iio_dev_opaque->attached_buffers = NULL;
 }
 
 /**
@@ -1750,15 +1768,36 @@  EXPORT_SYMBOL_GPL(iio_buffer_get_iio_dev);
  * @indio_dev: The device the buffer should be attached to
  * @buffer: The buffer to attach to the device
  *
+ * Return 0 if buffer attached, or negative if error occured.
+ *
  * This function attaches a buffer to a IIO device. The buffer stays attached to
- * the device until the device is freed. The function should only be called at
- * most once per device.
+ * the device until the device is freed. For legacy reasons, the first attached
+ * buffer will also be assigned to 'indio_dev->buffer'.
  */
-void iio_device_attach_buffer(struct iio_dev *indio_dev,
-			      struct iio_buffer *buffer)
+int iio_device_attach_buffer(struct iio_dev *indio_dev,
+			     struct iio_buffer *buffer)
 {
-	indio_dev->buffer = iio_buffer_get(buffer);
+	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
+	struct iio_buffer **new, **old = iio_dev_opaque->attached_buffers;
+	unsigned int cnt = iio_dev_opaque->attached_buffers_cnt;
+
+	cnt++;
+
+	new = krealloc(old, sizeof(*new) * cnt, GFP_KERNEL);
+	if (!new)
+		return -ENOMEM;
 
-	indio_dev->buffer->indio_dev = indio_dev;
+	iio_dev_opaque->attached_buffers = new;
+
+	/* first buffer is legacy; attach it to the IIO device directly */
+	if (!indio_dev->buffer)
+		indio_dev->buffer = iio_buffer_get(buffer);
+
+	buffer->indio_dev = indio_dev;
+
+	iio_dev_opaque->attached_buffers[cnt - 1] = buffer;
+	iio_dev_opaque->attached_buffers_cnt = cnt;
+
+	return 0;
 }
 EXPORT_SYMBOL_GPL(iio_device_attach_buffer);
diff --git a/include/linux/iio/buffer.h b/include/linux/iio/buffer.h
index b27d8c81f32c..5532eddb7808 100644
--- a/include/linux/iio/buffer.h
+++ b/include/linux/iio/buffer.h
@@ -43,7 +43,7 @@  static inline int iio_push_to_buffers_with_timestamp(struct iio_dev *indio_dev,
 bool iio_validate_scan_mask_onehot(struct iio_dev *indio_dev,
 				   const unsigned long *mask);
 
-void iio_device_attach_buffer(struct iio_dev *indio_dev,
-			      struct iio_buffer *buffer);
+int iio_device_attach_buffer(struct iio_dev *indio_dev,
+			     struct iio_buffer *buffer);
 
 #endif /* _IIO_BUFFER_GENERIC_H_ */
diff --git a/include/linux/iio/buffer_impl.h b/include/linux/iio/buffer_impl.h
index 77e169e51434..e25d26a7f601 100644
--- a/include/linux/iio/buffer_impl.h
+++ b/include/linux/iio/buffer_impl.h
@@ -124,6 +124,9 @@  struct iio_buffer {
 	/* @demux_bounce: Buffer for doing gather from incoming scan. */
 	void *demux_bounce;
 
+	/* @attached_entry: Entry in the devices list of buffers attached by the driver. */
+	struct list_head attached_entry;
+
 	/* @buffer_list: Entry in the devices list of current buffers. */
 	struct list_head buffer_list;
 
diff --git a/include/linux/iio/iio-opaque.h b/include/linux/iio/iio-opaque.h
index 07c5a8e52ca8..1db0ea09520e 100644
--- a/include/linux/iio/iio-opaque.h
+++ b/include/linux/iio/iio-opaque.h
@@ -7,6 +7,8 @@ 
  * struct iio_dev_opaque - industrial I/O device opaque information
  * @indio_dev:			public industrial I/O device information
  * @event_interface:		event chrdevs associated with interrupt lines
+ * @attached_buffers:		array of buffers statically attached by the driver
+ * @attached_buffers_cnt:	number of buffers in the array of statically attached buffers
  * @buffer_list:		list of all buffers currently attached
  * @channel_attr_list:		keep track of automatically created channel
  *				attributes
@@ -20,6 +22,8 @@ 
 struct iio_dev_opaque {
 	struct iio_dev			indio_dev;
 	struct iio_event_interface	*event_interface;
+	struct iio_buffer		**attached_buffers;
+	unsigned int			attached_buffers_cnt;
 	struct list_head		buffer_list;
 	struct list_head		channel_attr_list;
 	struct attribute_group		chan_attr_group;