Skip to content

Commit cadc212

Browse files
larsclausenjic23
authored andcommitted
iio: fix: Keep a reference to the IIO device for open file descriptors
Make sure that the IIO device is not freed while we still have file descriptors for it. Signed-off-by: Lars-Peter Clausen <lars@metafoo.de> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
1 parent a87c82e commit cadc212

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

drivers/iio/industrialio-core.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -970,6 +970,8 @@ static int iio_chrdev_open(struct inode *inode, struct file *filp)
970970
if (test_and_set_bit(IIO_BUSY_BIT_POS, &indio_dev->flags))
971971
return -EBUSY;
972972

973+
iio_device_get(indio_dev);
974+
973975
filp->private_data = indio_dev;
974976

975977
return 0;
@@ -983,6 +985,8 @@ static int iio_chrdev_release(struct inode *inode, struct file *filp)
983985
struct iio_dev *indio_dev = container_of(inode->i_cdev,
984986
struct iio_dev, chrdev);
985987
clear_bit(IIO_BUSY_BIT_POS, &indio_dev->flags);
988+
iio_device_put(indio_dev);
989+
986990
return 0;
987991
}
988992

drivers/iio/industrialio-event.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ EXPORT_SYMBOL(iio_push_event);
7272
static unsigned int iio_event_poll(struct file *filep,
7373
struct poll_table_struct *wait)
7474
{
75-
struct iio_event_interface *ev_int = filep->private_data;
75+
struct iio_dev *indio_dev = filep->private_data;
76+
struct iio_event_interface *ev_int = indio_dev->event_interface;
7677
unsigned int events = 0;
7778

7879
poll_wait(filep, &ev_int->wait, wait);
@@ -90,7 +91,8 @@ static ssize_t iio_event_chrdev_read(struct file *filep,
9091
size_t count,
9192
loff_t *f_ps)
9293
{
93-
struct iio_event_interface *ev_int = filep->private_data;
94+
struct iio_dev *indio_dev = filep->private_data;
95+
struct iio_event_interface *ev_int = indio_dev->event_interface;
9496
unsigned int copied;
9597
int ret;
9698

@@ -121,7 +123,8 @@ static ssize_t iio_event_chrdev_read(struct file *filep,
121123

122124
static int iio_event_chrdev_release(struct inode *inode, struct file *filep)
123125
{
124-
struct iio_event_interface *ev_int = filep->private_data;
126+
struct iio_dev *indio_dev = filep->private_data;
127+
struct iio_event_interface *ev_int = indio_dev->event_interface;
125128

126129
spin_lock_irq(&ev_int->wait.lock);
127130
__clear_bit(IIO_BUSY_BIT_POS, &ev_int->flags);
@@ -133,6 +136,8 @@ static int iio_event_chrdev_release(struct inode *inode, struct file *filep)
133136
kfifo_reset_out(&ev_int->det_events);
134137
spin_unlock_irq(&ev_int->wait.lock);
135138

139+
iio_device_put(indio_dev);
140+
136141
return 0;
137142
}
138143

@@ -158,12 +163,15 @@ int iio_event_getfd(struct iio_dev *indio_dev)
158163
return -EBUSY;
159164
}
160165
spin_unlock_irq(&ev_int->wait.lock);
161-
fd = anon_inode_getfd("iio:event",
162-
&iio_event_chrdev_fileops, ev_int, O_RDONLY);
166+
iio_device_get(indio_dev);
167+
168+
fd = anon_inode_getfd("iio:event", &iio_event_chrdev_fileops,
169+
indio_dev, O_RDONLY);
163170
if (fd < 0) {
164171
spin_lock_irq(&ev_int->wait.lock);
165172
__clear_bit(IIO_BUSY_BIT_POS, &ev_int->flags);
166173
spin_unlock_irq(&ev_int->wait.lock);
174+
iio_device_put(indio_dev);
167175
}
168176
return fd;
169177
}

0 commit comments

Comments
 (0)