Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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: Nuno Sa <nuno.sa@analog.com>
Link: https://lore.kernel.org/r/20240229-iio-use-cleanup-magic-v3-1-c3d34889ae3c@analog.com
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

authored by

Nuno Sa and committed by
Jonathan Cameron
42ea5992 305914d0

+13 -21
+13 -21
drivers/iio/industrialio-core.c
··· 11 11 12 12 #include <linux/anon_inodes.h> 13 13 #include <linux/cdev.h> 14 + #include <linux/cleanup.h> 14 15 #include <linux/debugfs.h> 15 16 #include <linux/device.h> 16 17 #include <linux/err.h> ··· 1811 1810 struct iio_dev *indio_dev = ib->indio_dev; 1812 1811 struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); 1813 1812 struct iio_ioctl_handler *h; 1814 - int ret = -ENODEV; 1813 + int ret; 1815 1814 1816 - mutex_lock(&iio_dev_opaque->info_exist_lock); 1817 - 1815 + guard(mutex)(&iio_dev_opaque->info_exist_lock); 1818 1816 /* 1819 1817 * The NULL check here is required to prevent crashing when a device 1820 1818 * is being removed while userspace would still have open file handles 1821 1819 * to try to access this device. 1822 1820 */ 1823 1821 if (!indio_dev->info) 1824 - goto out_unlock; 1822 + return -ENODEV; 1825 1823 1826 1824 list_for_each_entry(h, &iio_dev_opaque->ioctl_handlers, entry) { 1827 1825 ret = h->ioctl(indio_dev, filp, cmd, arg); 1828 1826 if (ret != IIO_IOCTL_UNHANDLED) 1829 - break; 1827 + return ret; 1830 1828 } 1831 1829 1832 - if (ret == IIO_IOCTL_UNHANDLED) 1833 - ret = -ENODEV; 1834 - 1835 - out_unlock: 1836 - mutex_unlock(&iio_dev_opaque->info_exist_lock); 1837 - 1838 - return ret; 1830 + return -ENODEV; 1839 1831 } 1840 1832 1841 1833 static const struct file_operations iio_buffer_fileops = { ··· 2056 2062 2057 2063 cdev_device_del(&iio_dev_opaque->chrdev, &indio_dev->dev); 2058 2064 2059 - mutex_lock(&iio_dev_opaque->info_exist_lock); 2065 + scoped_guard(mutex, &iio_dev_opaque->info_exist_lock) { 2066 + iio_device_unregister_debugfs(indio_dev); 2060 2067 2061 - iio_device_unregister_debugfs(indio_dev); 2068 + iio_disable_all_buffers(indio_dev); 2062 2069 2063 - iio_disable_all_buffers(indio_dev); 2070 + indio_dev->info = NULL; 2064 2071 2065 - indio_dev->info = NULL; 2066 - 2067 - iio_device_wakeup_eventset(indio_dev); 2068 - iio_buffer_wakeup_poll(indio_dev); 2069 - 2070 - mutex_unlock(&iio_dev_opaque->info_exist_lock); 2072 + iio_device_wakeup_eventset(indio_dev); 2073 + iio_buffer_wakeup_poll(indio_dev); 2074 + } 2071 2075 2072 2076 iio_buffers_free_sysfs_and_mask(indio_dev); 2073 2077 }