From: Jonathan Cameron <jic23@kernel.org>
To: Lars-Peter Clausen <lars@metafoo.de>
Cc: linux-iio@vger.kernel.org
Subject: Re: [PATCH 03/10] iio: Set the IIO device as the parent for the character device
Date: Sat, 21 Sep 2013 12:52:15 +0100 [thread overview]
Message-ID: <523D886F.1070209@kernel.org> (raw)
In-Reply-To: <1379534574-11213-3-git-send-email-lars@metafoo.de>
On 09/18/13 21:02, Lars-Peter Clausen wrote:
> We need to make sure that the IIO device is not freed while the character device
> exists, otherwise the freeing of the IIO device might race against the file open
> callback. Do this by setting the character device's parent to the IIO device,
> this will cause the character device to grab a reference to the IIO device and
> only release it once the character device itself has been removed.
>
> Also move the registration of the character device before the registration of
> the IIO device to avoid the (rather theoretical case) that the IIO device is
> already freed again before we can add the character device and grab a reference
> to the IIO device.
>
> We also need to move the call to cdev_del() from iio_dev_release() to
> iio_device_unregister() (where it should have been in the first place anyway) to
> avoid a reference cycle. As iio_dev_release() is only called once all reference
> are dropped, but the character device holds a reference to the IIO device.
>
> Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
Applied to the fixes to greg branch of iio.git
Have changed the patch title to make it more apparent that this is a fix.
Now called:
iio: Prevent race between IIO chardev opening and IIO device free
with the original title as the first line of the description.
Good fix but you need to be sensational in the patch titles!
> ---
> drivers/iio/industrialio-core.c | 21 ++++++++++++---------
> 1 file changed, 12 insertions(+), 9 deletions(-)
>
> diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
> index 5ea741c..863aa01 100644
> --- a/drivers/iio/industrialio-core.c
> +++ b/drivers/iio/industrialio-core.c
> @@ -890,8 +890,6 @@ static void iio_device_unregister_sysfs(struct iio_dev *indio_dev)
> static void iio_dev_release(struct device *device)
> {
> struct iio_dev *indio_dev = dev_to_iio_dev(device);
> - if (indio_dev->chrdev.dev)
> - cdev_del(&indio_dev->chrdev);
> if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
> iio_device_unregister_trigger_consumer(indio_dev);
> iio_device_unregister_eventset(indio_dev);
> @@ -1098,18 +1096,20 @@ int iio_device_register(struct iio_dev *indio_dev)
> indio_dev->setup_ops == NULL)
> indio_dev->setup_ops = &noop_ring_setup_ops;
>
> - ret = device_add(&indio_dev->dev);
> - if (ret < 0)
> - goto error_unreg_eventset;
> cdev_init(&indio_dev->chrdev, &iio_buffer_fileops);
> indio_dev->chrdev.owner = indio_dev->info->driver_module;
> + indio_dev->chrdev.kobj.parent = &indio_dev->dev.kobj;
> ret = cdev_add(&indio_dev->chrdev, indio_dev->dev.devt, 1);
> if (ret < 0)
> - goto error_del_device;
> - return 0;
> + goto error_unreg_eventset;
>
> -error_del_device:
> - device_del(&indio_dev->dev);
> + ret = device_add(&indio_dev->dev);
> + if (ret < 0)
> + goto error_cdev_del;
> +
> + return 0;
> +error_cdev_del:
> + cdev_del(&indio_dev->chrdev);
> error_unreg_eventset:
> iio_device_unregister_eventset(indio_dev);
> error_free_sysfs:
> @@ -1127,6 +1127,9 @@ void iio_device_unregister(struct iio_dev *indio_dev)
>
> device_del(&indio_dev->dev);
>
> + if (indio_dev->chrdev.dev)
> + cdev_del(&indio_dev->chrdev);
> +
> iio_disable_all_buffers(indio_dev);
>
> indio_dev->info = NULL;
>
next prev parent reply other threads:[~2013-09-21 10:51 UTC|newest]
Thread overview: 29+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-09-18 20:02 [PATCH 01/10] iio: Stop sampling when the device is removed Lars-Peter Clausen
2013-09-18 20:02 ` [PATCH 02/10] iio: Keep a reference to the IIO device for open file descriptors Lars-Peter Clausen
2013-09-21 11:44 ` Jonathan Cameron
2013-09-21 11:48 ` Jonathan Cameron
2013-09-18 20:02 ` [PATCH 03/10] iio: Set the IIO device as the parent for the character device Lars-Peter Clausen
2013-09-21 11:52 ` Jonathan Cameron [this message]
2013-09-18 20:02 ` [PATCH 04/10] iio:buffer_cb: Add missing iio_buffer_init() Lars-Peter Clausen
2013-09-21 11:53 ` Jonathan Cameron
2013-09-18 20:02 ` [PATCH 05/10] iio: Add reference counting for buffers Lars-Peter Clausen
2013-09-18 20:02 ` [PATCH 06/10] iio: Remove debugfs entries in iio_device_unregister() Lars-Peter Clausen
2013-09-18 20:02 ` [PATCH 07/10] iio: Return -ENODEV for file operations if the device has been unregistered Lars-Peter Clausen
2013-09-18 20:02 ` [PATCH 08/10] iio: Wakeup poll and blocking reads when the device is unregistered Lars-Peter Clausen
2013-09-21 11:56 ` Jonathan Cameron
2013-09-21 11:43 ` Lars-Peter Clausen
2013-09-21 12:45 ` Jonathan Cameron
2013-09-21 15:16 ` Lars-Peter Clausen
2013-09-21 18:18 ` Jonathan Cameron
2013-09-18 20:02 ` [PATCH 09/10] iio:buffer: Add proper locking for iio_update_buffers() Lars-Peter Clausen
2013-09-18 20:27 ` Lars-Peter Clausen
2013-09-21 11:59 ` Jonathan Cameron
2013-09-21 11:05 ` Lars-Peter Clausen
2013-09-21 12:09 ` Jonathan Cameron
2013-09-18 20:02 ` [PATCH 10/10] iio:buffer: Ignore noop requests " Lars-Peter Clausen
2013-09-21 11:57 ` Jonathan Cameron
2013-09-18 22:00 ` [PATCH 01/10] iio: Stop sampling when the device is removed Jonathan Cameron
2013-09-18 21:08 ` Lars-Peter Clausen
2013-09-18 22:11 ` Jonathan Cameron
2013-09-21 11:36 ` Jonathan Cameron
2013-09-21 10:45 ` Lars-Peter Clausen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=523D886F.1070209@kernel.org \
--to=jic23@kernel.org \
--cc=lars@metafoo.de \
--cc=linux-iio@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).