From 9e69c935fad9fd5f0550c51e3bd251cd30033136 Mon Sep 17 00:00:00 2001 From: Lars-Peter Clausen Date: Fri, 4 Oct 2013 12:06:00 +0100 Subject: iio: Add reference counting for buffers Since the buffer is accessed by userspace we can not just free the buffers memory once we are done with it in kernel space. There might still be open file descriptors and userspace still might be accessing the buffer. This patch adds support for reference counting to the IIO buffers. When a buffer is created and initialized its initial reference count is set to 1. Instead of freeing the memory of the buffer the buffer's _free() function will drop that reference again. But only after the last reference to the buffer has been dropped the buffer the buffer's memory will be freed. The IIO device will take a reference to its primary buffer. The patch adds a small helper function for this called iio_device_attach_buffer() which will get a reference to the buffer and assign the buffer to the IIO device. This function must be used instead of assigning the buffer to the device by hand. The reference is only dropped once the IIO device is freed and we can be sure that there are no more open file handles. A reference to a buffer will also be taken whenever the buffer is active to avoid the buffer being freed while data is still being send to it. Signed-off-by: Lars-Peter Clausen Signed-off-by: Jonathan Cameron --- drivers/iio/industrialio-triggered-buffer.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'drivers/iio/industrialio-triggered-buffer.c') diff --git a/drivers/iio/industrialio-triggered-buffer.c b/drivers/iio/industrialio-triggered-buffer.c index 46c619b0d8c5..c1cb1f94fe2e 100644 --- a/drivers/iio/industrialio-triggered-buffer.c +++ b/drivers/iio/industrialio-triggered-buffer.c @@ -47,14 +47,17 @@ int iio_triggered_buffer_setup(struct iio_dev *indio_dev, irqreturn_t (*pollfunc_th)(int irq, void *p), const struct iio_buffer_setup_ops *setup_ops) { + struct iio_buffer *buffer; int ret; - indio_dev->buffer = iio_kfifo_allocate(indio_dev); - if (!indio_dev->buffer) { + buffer = iio_kfifo_allocate(indio_dev); + if (!buffer) { ret = -ENOMEM; goto error_ret; } + iio_device_attach_buffer(indio_dev, buffer); + indio_dev->pollfunc = iio_alloc_pollfunc(pollfunc_bh, pollfunc_th, IRQF_ONESHOT, -- cgit v1.2.3 From c80b8ea9b4ef11fa99e7c3bfa90836971ec1e291 Mon Sep 17 00:00:00 2001 From: Lars-Peter Clausen Date: Mon, 14 Oct 2013 17:49:00 +0100 Subject: iio:triggered-buffer: Remove redundant call to iio_sw_buffer_preenable(). The equivalent of iio_sw_buffer_preenable() is now done in the IIO buffer core, so there is no need to do this from the driver anymore. Signed-off-by: Lars-Peter Clausen Signed-off-by: Jonathan Cameron --- drivers/iio/industrialio-triggered-buffer.c | 1 - 1 file changed, 1 deletion(-) (limited to 'drivers/iio/industrialio-triggered-buffer.c') diff --git a/drivers/iio/industrialio-triggered-buffer.c b/drivers/iio/industrialio-triggered-buffer.c index c1cb1f94fe2e..d6f54930b34a 100644 --- a/drivers/iio/industrialio-triggered-buffer.c +++ b/drivers/iio/industrialio-triggered-buffer.c @@ -17,7 +17,6 @@ #include static const struct iio_buffer_setup_ops iio_triggered_buffer_setup_ops = { - .preenable = &iio_sw_buffer_preenable, .postenable = &iio_triggered_buffer_postenable, .predisable = &iio_triggered_buffer_predisable, }; -- cgit v1.2.3