linux-media.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Hans Verkuil <hverkuil@xs4all.nl>
To: Andrzej Hajda <a.hajda@samsung.com>, linux-media@vger.kernel.org
Cc: linux-samsung-soc@vger.kernel.org,
	Russell King <linux@armlinux.org.uk>,
	dri-devel@lists.freedesktop.org,
	Javier Martinez Canillas <javier@osg.samsung.com>,
	Hans Verkuil <hans.verkuil@cisco.com>,
	Krzysztof Kozlowski <krzk@kernel.org>,
	Marek Szyprowski <m.szyprowski@samsung.com>
Subject: Re: [PATCHv2 1/4] video: add hotplug detect notifier support
Date: Tue, 3 Jan 2017 08:54:52 +0100	[thread overview]
Message-ID: <ad4af428-fb4a-6b81-4c3a-9d525d6aa19d@xs4all.nl> (raw)
In-Reply-To: <ddb0ba80-c2a4-1024-8e9c-4ba74882a282@samsung.com>

On 01/03/2017 08:50 AM, Andrzej Hajda wrote:
> Hi Hans,
> 
> On 02.01.2017 15:19, Hans Verkuil wrote:
>> From: Hans Verkuil <hans.verkuil@cisco.com>
>>
>> Add support for video hotplug detect and EDID/ELD notifiers, which is used
>> to convey information from video drivers to their CEC and audio counterparts.
>>
>> Based on an earlier version from Russell King:
>>
>> https://patchwork.kernel.org/patch/9277043/
>>
>> The hpd_notifier is a reference counted object containing the HPD/EDID/ELD state
>> of a video device.
>>
>> When a new notifier is registered the current state will be reported to
>> that notifier at registration time.
>>
>> Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
>> Tested-by: Marek Szyprowski <m.szyprowski@samsung.com>
>> ---
>>  drivers/video/Kconfig        |   3 +
>>  drivers/video/Makefile       |   1 +
>>  drivers/video/hpd-notifier.c | 134 +++++++++++++++++++++++++++++++++++++++++++
>>  include/linux/hpd-notifier.h | 109 +++++++++++++++++++++++++++++++++++
>>  4 files changed, 247 insertions(+)
>>  create mode 100644 drivers/video/hpd-notifier.c
>>  create mode 100644 include/linux/hpd-notifier.h
>>
>> diff --git a/drivers/video/Kconfig b/drivers/video/Kconfig
>> index 3c20af9..cddc860 100644
>> --- a/drivers/video/Kconfig
>> +++ b/drivers/video/Kconfig
>> @@ -36,6 +36,9 @@ config VIDEOMODE_HELPERS
>>  config HDMI
>>  	bool
>>  
>> +config HPD_NOTIFIERS
>> +	bool
>> +
>>  if VT
>>  	source "drivers/video/console/Kconfig"
>>  endif
>> diff --git a/drivers/video/Makefile b/drivers/video/Makefile
>> index 9ad3c17..424698b 100644
>> --- a/drivers/video/Makefile
>> +++ b/drivers/video/Makefile
>> @@ -1,5 +1,6 @@
>>  obj-$(CONFIG_VGASTATE)            += vgastate.o
>>  obj-$(CONFIG_HDMI)                += hdmi.o
>> +obj-$(CONFIG_HPD_NOTIFIERS)       += hpd-notifier.o
>>  
>>  obj-$(CONFIG_VT)		  += console/
>>  obj-$(CONFIG_LOGO)		  += logo/
>> diff --git a/drivers/video/hpd-notifier.c b/drivers/video/hpd-notifier.c
>> new file mode 100644
>> index 0000000..54f7a6b
>> --- /dev/null
>> +++ b/drivers/video/hpd-notifier.c
>> @@ -0,0 +1,134 @@
>> +#include <linux/export.h>
>> +#include <linux/hpd-notifier.h>
>> +#include <linux/string.h>
>> +#include <linux/slab.h>
>> +#include <linux/list.h>
>> +
>> +static LIST_HEAD(hpd_notifiers);
>> +static DEFINE_MUTEX(hpd_notifiers_lock);
>> +
>> +struct hpd_notifier *hpd_notifier_get(struct device *dev)
>> +{
>> +	struct hpd_notifier *n;
>> +
>> +	mutex_lock(&hpd_notifiers_lock);
>> +	list_for_each_entry(n, &hpd_notifiers, head) {
>> +		if (n->dev == dev) {
>> +			mutex_unlock(&hpd_notifiers_lock);
> 
> I think this place is racy, we have pointer to unprotected area
> (n->kref), so if concurrent thread calls hpd_notifier_put in this moment
> &n->kref could be freed and kref_get in the next line will operate on
> dangling pointer. Am I right?

You're right. I took the hpd_notifiers_lock in hpd_notifier_release,
but I should take it in hpd_notifier_put.

Thanks! I'll fix that.

Regards,

	Hans

> 
> Regards
> Andrzej
> 
>> +			kref_get(&n->kref);
>> +			return n;
>> +		}
>> +	}
>> +	n = kzalloc(sizeof(*n), GFP_KERNEL);
>> +	if (!n)
>> +		goto unlock;
>> +	n->dev = dev;
>> +	mutex_init(&n->lock);
>> +	BLOCKING_INIT_NOTIFIER_HEAD(&n->notifiers);
>> +	kref_init(&n->kref);
>> +	list_add_tail(&n->head, &hpd_notifiers);
>> +unlock:
>> +	mutex_unlock(&hpd_notifiers_lock);
>> +	return n;
>> +}
>> +EXPORT_SYMBOL_GPL(hpd_notifier_get);
>> +
>> +static void hpd_notifier_release(struct kref *kref)
>> +{
>> +	struct hpd_notifier *n =
>> +		container_of(kref, struct hpd_notifier, kref);
>> +
>> +	mutex_lock(&hpd_notifiers_lock);
>> +	list_del(&n->head);
>> +	mutex_unlock(&hpd_notifiers_lock);
>> +	kfree(n->edid);
>> +	kfree(n);
>> +}
>> +
>> +void hpd_notifier_put(struct hpd_notifier *n)
>> +{
>> +	kref_put(&n->kref, hpd_notifier_release);
>> +}
>> +EXPORT_SYMBOL_GPL(hpd_notifier_put);
>> +
>> +int hpd_notifier_register(struct hpd_notifier *n, struct notifier_block *nb)
>> +{
>> +	int ret = blocking_notifier_chain_register(&n->notifiers, nb);
>> +
>> +	if (ret)
>> +		return ret;
>> +	kref_get(&n->kref);
>> +	mutex_lock(&n->lock);
>> +	if (n->connected) {
>> +		blocking_notifier_call_chain(&n->notifiers, HPD_CONNECTED, n);
>> +		if (n->edid_size)
>> +			blocking_notifier_call_chain(&n->notifiers, HPD_NEW_EDID, n);
>> +		if (n->has_eld)
>> +			blocking_notifier_call_chain(&n->notifiers, HPD_NEW_ELD, n);
>> +	}
>> +	mutex_unlock(&n->lock);
>> +	return 0;
>> +}
>> +EXPORT_SYMBOL_GPL(hpd_notifier_register);
>> +
>> +int hpd_notifier_unregister(struct hpd_notifier *n, struct notifier_block *nb)
>> +{
>> +	int ret = blocking_notifier_chain_unregister(&n->notifiers, nb);
>> +
>> +	if (ret == 0)
>> +		hpd_notifier_put(n);
>> +	return ret;
>> +}
>> +EXPORT_SYMBOL_GPL(hpd_notifier_unregister);
> (...)
> 
> --
> To unsubscribe from this list: send the line "unsubscribe linux-media" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> 


  reply	other threads:[~2017-01-03  7:54 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-01-02 14:19 [PATCHv2 0/4] video/exynos/cec: add HPD state notifier & use in s5p-cec Hans Verkuil
2017-01-02 14:19 ` [PATCHv2 1/4] video: add hotplug detect notifier support Hans Verkuil
2017-01-03  7:50   ` Andrzej Hajda
2017-01-03  7:54     ` Hans Verkuil [this message]
2017-03-20 14:26   ` Russell King - ARM Linux
2017-03-20 14:27     ` Russell King - ARM Linux
2017-03-20 14:41       ` Hans Verkuil
2017-01-02 14:19 ` [PATCHv2 2/4] exynos_hdmi: add HPD " Hans Verkuil
2017-01-03  7:55   ` Andrzej Hajda
2017-01-03  8:25     ` Hans Verkuil
2017-01-02 14:19 ` [PATCHv2 3/4] cec: integrate " Hans Verkuil
2017-01-02 14:19 ` [PATCHv2 4/4] s5p-cec: add hpd-notifier support, move out of staging Hans Verkuil
2017-01-02 17:50   ` Krzysztof Kozlowski
2017-01-03  8:00   ` Andrzej Hajda
2017-01-03  8:11     ` Hans Verkuil
2017-01-04  8:44       ` Andrzej Hajda
2017-01-23 10:14         ` 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=ad4af428-fb4a-6b81-4c3a-9d525d6aa19d@xs4all.nl \
    --to=hverkuil@xs4all.nl \
    --cc=a.hajda@samsung.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=hans.verkuil@cisco.com \
    --cc=javier@osg.samsung.com \
    --cc=krzk@kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=linux-samsung-soc@vger.kernel.org \
    --cc=linux@armlinux.org.uk \
    --cc=m.szyprowski@samsung.com \
    /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).