linux-input.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Russell King - ARM Linux <linux@arm.linux.org.uk>
To: Hans Verkuil <hans.verkuil@cisco.com>
Cc: linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org,
	m.szyprowski@samsung.com, kyungmin.park@samsung.com,
	thomas@tommie-lie.de, sean@mess.org, dmitry.torokhov@gmail.com,
	linux-input@vger.kernel.org, linux-samsung-soc@vger.kernel.org,
	lars@opdenkamp.eu, kamil@wypas.org,
	Hans Verkuil <hansverk@cisco.com>
Subject: Re: [PATCHv8 07/15] cec: add HDMI CEC framework
Date: Tue, 18 Aug 2015 11:00:20 +0100	[thread overview]
Message-ID: <20150818100020.GM7557@n2100.arm.linux.org.uk> (raw)
In-Reply-To: <87a579ddacf90718a166fbb8a777b5d8cd05200b.1439886203.git.hans.verkuil@cisco.com>

On Tue, Aug 18, 2015 at 10:26:32AM +0200, Hans Verkuil wrote:
> +	/* Part 2: Initialize and register the character device */
> +	cdev_init(&cecdev->cdev, &cec_devnode_fops);
> +	cecdev->cdev.owner = owner;
> +
> +	ret = cdev_add(&cecdev->cdev, MKDEV(MAJOR(cec_dev_t), cecdev->minor),
> +									1);
> +	if (ret < 0) {
> +		pr_err("%s: cdev_add failed\n", __func__);
> +		goto error;
> +	}
> +
> +	/* Part 3: Register the cec device */
> +	cecdev->dev.bus = &cec_bus_type;
> +	cecdev->dev.devt = MKDEV(MAJOR(cec_dev_t), cecdev->minor);
> +	cecdev->dev.release = cec_devnode_release;
> +	if (cecdev->parent)
> +		cecdev->dev.parent = cecdev->parent;
> +	dev_set_name(&cecdev->dev, "cec%d", cecdev->minor);
> +	ret = device_register(&cecdev->dev);

It's worth pointing out that you can greatly simplify the lifetime
handling (you don't need to get and put cecdev->dev) if you make
the cdev a child of the cecdev->dev.

If you grep for kobj.parent in drivers/ you'll see many drivers are
doing this.

	cecdev->cdev.kobj.parent = &cecdev->dev.kobj;

but you will need to call device_initialize() on cecdev->dev first,
and use device_add() here.

> +	if (ret < 0) {
> +		pr_err("%s: device_register failed\n", __func__);
> +		goto error;
> +	}
> +
> +	/* Part 4: Activate this minor. The char device can now be used. */
> +	set_bit(CEC_FLAG_REGISTERED, &cecdev->flags);

Having flags to indicate whether userspace can open something is racy.
I don't see any other uses of cecdev->flags.  I think you should kill
this, and replace it with a cecdev->dead flag which indicates when the
cecdev is going away, and causes any pre-existing users to fail.


> +
> +	return 0;
> +
> +error:
> +	cdev_del(&cecdev->cdev);
> +	clear_bit(cecdev->minor, cec_devnode_nums);
> +	return ret;
> +}
> +
> +/**
> + * cec_devnode_unregister - unregister a cec device node
> + * @cecdev: the device node to unregister
> + *
> + * This unregisters the passed device. Future open calls will be met with
> + * errors.
> + *
> + * This function can safely be called if the device node has never been
> + * registered or has already been unregistered.
> + */
> +static void cec_devnode_unregister(struct cec_devnode *cecdev)
> +{
> +	/* Check if cecdev was ever registered at all */
> +	if (!cec_devnode_is_registered(cecdev))
> +		return;

Just make it a programming error if someone unregisters something that
they haven't registered... that's pretty standard kernel programming.

> +
> +	mutex_lock(&cec_devnode_lock);
> +	clear_bit(CEC_FLAG_REGISTERED, &cecdev->flags);

This should wake up the poll waitqueue so that users get to hear about
the device going away in a timely manner.

> +	mutex_unlock(&cec_devnode_lock);
> +	device_unregister(&cecdev->dev);
> +}
> +
> +int cec_create_adapter(struct cec_adapter *adap, const char *name, u32 caps,
> +		       u8 ninputs, struct module *owner, struct device *parent)
> +{
> +	int res = 0;
> +
> +	adap->owner = owner;
> +	if (WARN_ON(!owner))
> +		return -ENXIO;
> +	adap->devnode.parent = parent;
> +	if (WARN_ON(!parent))
> +		return -ENXIO;
> +	adap->name = name;
> +	adap->phys_addr = CEC_PHYS_ADDR_INVALID;
> +	adap->capabilities = caps;
> +	adap->ninputs = ninputs;
> +	adap->is_source = caps & CEC_CAP_IS_SOURCE;
> +	if (WARN_ON(!adap->ninputs && !adap->is_source))
> +		return -ENXIO;
> +	adap->cec_version = CEC_OP_CEC_VERSION_2_0;
> +	adap->vendor_id = CEC_VENDOR_ID_NONE;
> +	adap->available_log_addrs = 1;
> +	adap->sequence = 0;
> +	memset(adap->phys_addrs, 0xff, sizeof(adap->phys_addrs));
> +	mutex_init(&adap->lock);
> +	INIT_LIST_HEAD(&adap->transmit_queue);
> +	INIT_LIST_HEAD(&adap->wait_queue);
> +	adap->kthread = kthread_run(cec_thread_func, adap, "cec-%s", name);
> +	init_waitqueue_head(&adap->kthread_waitq);
> +	if (IS_ERR(adap->kthread)) {
> +		pr_err("cec-%s: kernel_thread() failed\n", name);
> +		return PTR_ERR(adap->kthread);
> +	}
> +	if (caps) {
> +		res = cec_devnode_register(&adap->devnode, adap->owner);

Okay, so adap->devnode contains a struct device.  That struct device
controls the lifetime of adap->devnode, and because adap->devnode is
part of adap, this also defines the lifetime of adap as well.  adap
must _never_ be freed until cec_devnode_release() has been called.

Looking at patch 15, the adapter structure is part of the cobalt
streams.  This makes that structure also have a lifetime controlled
by this struct device.  There is no release method implemented in
there, and indeed cec_devnode_release() shows that the release node is
optional, which suggests a misunderstanding in this area.

Far too many nested data structures are involved here.  This needs fixing
- with the code in its present form, it contains serious data structure
lifetime issues, and therefore is not ready for merging, sorry.

-- 
FTTC broadband for 0.8mile line: currently at 10.5Mbps down 400kbps up
according to speedtest.net.

  reply	other threads:[~2015-08-18 10:00 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-08-18  8:26 [PATCHv8 00/15] HDMI CEC framework Hans Verkuil
2015-08-18  8:26 ` [PATCHv8 01/15] dts: exynos4*: add HDMI CEC pin definition to pinctrl Hans Verkuil
2015-08-18  8:26 ` [PATCHv8 02/15] dts: exynos4: add node for the HDMI CEC device Hans Verkuil
2015-08-18  8:26 ` [PATCHv8 03/15] dts: exynos4412-odroid*: enable " Hans Verkuil
2015-08-18  8:26 ` [PATCHv8 04/15] input.h: add BUS_CEC type Hans Verkuil
2015-08-18  8:26 ` [PATCHv8 05/15] HID: add HDMI CEC specific keycodes Hans Verkuil
2015-08-18  8:26 ` [PATCHv8 06/15] rc: Add HDMI CEC protocol handling Hans Verkuil
2015-08-18  8:26 ` [PATCHv8 07/15] cec: add HDMI CEC framework Hans Verkuil
2015-08-18 10:00   ` Russell King - ARM Linux [this message]
2015-08-18 20:22     ` Dmitry Torokhov
2015-08-18  8:26 ` [PATCHv8 08/15] cec.txt: add CEC framework documentation Hans Verkuil
2015-08-18  8:26 ` [PATCHv8 09/15] DocBook/media: add CEC documentation Hans Verkuil
2015-08-18  8:26 ` [PATCHv8 10/15] v4l2-subdev: add HDMI CEC ops Hans Verkuil
2015-08-18  8:26 ` [PATCHv8 11/15] cec: adv7604: add cec support Hans Verkuil
2015-08-18  8:26 ` [PATCHv8 12/15] cec: adv7842: " Hans Verkuil
2015-08-18  8:26 ` [PATCHv8 13/15] cec: adv7511: " Hans Verkuil
2015-08-18  8:26 ` [PATCHv8 14/15] cec: s5p-cec: Add s5p-cec driver Hans Verkuil
2015-08-18  8:26 ` [PATCHv8 15/15] cobalt: add cec support Hans Verkuil

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=20150818100020.GM7557@n2100.arm.linux.org.uk \
    --to=linux@arm.linux.org.uk \
    --cc=dmitry.torokhov@gmail.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=hans.verkuil@cisco.com \
    --cc=hansverk@cisco.com \
    --cc=kamil@wypas.org \
    --cc=kyungmin.park@samsung.com \
    --cc=lars@opdenkamp.eu \
    --cc=linux-input@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=linux-samsung-soc@vger.kernel.org \
    --cc=m.szyprowski@samsung.com \
    --cc=sean@mess.org \
    --cc=thomas@tommie-lie.de \
    /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).