Commit 42ea5992 authored by Nuno Sa's avatar Nuno Sa Committed by Jonathan Cameron

iio: core: move to cleanup.h magic

Use the new cleanup magic for handling mutexes in IIO. This allows us to
greatly simplify some code paths.

Note that we keep the plain mutex calls in the
iio_device_release|acquire() APIs since in there the macros would likely
not help much (as we want to keep the lock acquired when he leave the
APIs).
Signed-off-by: default avatarNuno Sa <nuno.sa@analog.com>
Link: https://lore.kernel.org/r/20240229-iio-use-cleanup-magic-v3-1-c3d34889ae3c@analog.comSigned-off-by: default avatarJonathan Cameron <Jonathan.Cameron@huawei.com>
parent 305914d0
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include <linux/anon_inodes.h> #include <linux/anon_inodes.h>
#include <linux/cdev.h> #include <linux/cdev.h>
#include <linux/cleanup.h>
#include <linux/debugfs.h> #include <linux/debugfs.h>
#include <linux/device.h> #include <linux/device.h>
#include <linux/err.h> #include <linux/err.h>
...@@ -1810,31 +1811,24 @@ static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) ...@@ -1810,31 +1811,24 @@ static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
struct iio_dev *indio_dev = ib->indio_dev; struct iio_dev *indio_dev = ib->indio_dev;
struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
struct iio_ioctl_handler *h; struct iio_ioctl_handler *h;
int ret = -ENODEV; int ret;
mutex_lock(&iio_dev_opaque->info_exist_lock);
guard(mutex)(&iio_dev_opaque->info_exist_lock);
/* /*
* The NULL check here is required to prevent crashing when a device * The NULL check here is required to prevent crashing when a device
* is being removed while userspace would still have open file handles * is being removed while userspace would still have open file handles
* to try to access this device. * to try to access this device.
*/ */
if (!indio_dev->info) if (!indio_dev->info)
goto out_unlock; return -ENODEV;
list_for_each_entry(h, &iio_dev_opaque->ioctl_handlers, entry) { list_for_each_entry(h, &iio_dev_opaque->ioctl_handlers, entry) {
ret = h->ioctl(indio_dev, filp, cmd, arg); ret = h->ioctl(indio_dev, filp, cmd, arg);
if (ret != IIO_IOCTL_UNHANDLED) if (ret != IIO_IOCTL_UNHANDLED)
break; return ret;
} }
if (ret == IIO_IOCTL_UNHANDLED) return -ENODEV;
ret = -ENODEV;
out_unlock:
mutex_unlock(&iio_dev_opaque->info_exist_lock);
return ret;
} }
static const struct file_operations iio_buffer_fileops = { static const struct file_operations iio_buffer_fileops = {
...@@ -2062,8 +2056,7 @@ void iio_device_unregister(struct iio_dev *indio_dev) ...@@ -2062,8 +2056,7 @@ void iio_device_unregister(struct iio_dev *indio_dev)
cdev_device_del(&iio_dev_opaque->chrdev, &indio_dev->dev); cdev_device_del(&iio_dev_opaque->chrdev, &indio_dev->dev);
mutex_lock(&iio_dev_opaque->info_exist_lock); scoped_guard(mutex, &iio_dev_opaque->info_exist_lock) {
iio_device_unregister_debugfs(indio_dev); iio_device_unregister_debugfs(indio_dev);
iio_disable_all_buffers(indio_dev); iio_disable_all_buffers(indio_dev);
...@@ -2072,8 +2065,7 @@ void iio_device_unregister(struct iio_dev *indio_dev) ...@@ -2072,8 +2065,7 @@ void iio_device_unregister(struct iio_dev *indio_dev)
iio_device_wakeup_eventset(indio_dev); iio_device_wakeup_eventset(indio_dev);
iio_buffer_wakeup_poll(indio_dev); iio_buffer_wakeup_poll(indio_dev);
}
mutex_unlock(&iio_dev_opaque->info_exist_lock);
iio_buffers_free_sysfs_and_mask(indio_dev); iio_buffers_free_sysfs_and_mask(indio_dev);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment