linux-i2c.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jean Delvare <khali-PUYAD+kWke1g9hUCZPvPmw@public.gmane.org>
To: Hans Verkuil <hverkuil-qWit8jRvyhVmR6Xm/wNWPw@public.gmane.org>
Cc: Mark Brown
	<broonie-yzvPICuk2AATkU/dhu1WVueM+bqZidxxQQ4Iyu8u01E@public.gmane.org>,
	Wolfram Sang <w.sang-bIcnvbaLZ9MEGnE8C9+IrQ@public.gmane.org>,
	kernel-janitors-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
	linux-i2c-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
	Mauro Carvalho Chehab
	<mchehab-wEGCiKHe2LqWVfeAwA7xHQ@public.gmane.org>,
	linux-media-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
Subject: Re: [PATCH 12/24] media/video: fix dangling pointers
Date: Mon, 22 Mar 2010 21:33:58 +0100	[thread overview]
Message-ID: <20100322213358.31e50b3c@hyperion.delvare> (raw)
In-Reply-To: <201003211709.56319.hverkuil-qWit8jRvyhVmR6Xm/wNWPw@public.gmane.org>

Hi Hans, Mark, Wolfram,

On Sun, 21 Mar 2010 17:09:56 +0100, Hans Verkuil wrote:
> On Sunday 21 March 2010 15:14:17 Mark Brown wrote:
> > On Sun, Mar 21, 2010 at 02:46:55PM +0100, Jean Delvare wrote:
> > > On Sat, 20 Mar 2010 23:02:49 +0100, Hans Verkuil wrote:
> > 
> > > > I feel I am missing something here. Why does clientdata have to be set to
> > > > NULL when we are tearing down the device anyway?
> > 
> > > We're not tearing down the device, that's the point. We are only
> > > unbinding it from its driver. The device itself survives the operation.
> > 
> > That's the subsystem point of view, not the driver point of view.  As
> > far as the driver is concerned the device appears when probe() is called
> > and vanishes after remove() has completed, any management the subsystem
> > does in between is up to it.
> 
> Right. And from the point of view of the driver I see no reason why I would
> have to zero the client data pointer when I know nobody will ever access it
> again. If there is a good reason, then that is (again, from the PoV of the
> driver) completely unexpected and it is guaranteed that driver writers will
> continue to make that mistake in the future.

I can't disagree. Given that there is no way to enforce the rule, it is
likely not everybody will follow it.

> > > 1* It is good practice to have memory freed not too far from where it
> > > was allocated, otherwise there is always a risk of unmatched pairs. In
> > > this regard, it seems preferable to let each i2c driver kfree the
> > > device memory it kalloc'd, be it in probe() or remove().
> > 
> > I agree with this.  There are also some use cases where the device data
> > is actually static (eg, a generic description of the device or a
> > reference to some other shared resource rather than per device allocated
> > data).

>From a technical perspective, there is little rationale to have the
client data pointed to static data. If you could reach it from probe(),
it has to be a global, and if it is a global, you can reach it again
directly from the rest of your code.

That being said, that doesn't mean drivers aren't actually doing it.

> Definitely. Freeing should be done in the i2c drivers.
>  
> > > 2* References to allocated memory should be dropped before that memory
> > > is freed. This means that we want to call i2c_set_clientdata(c, NULL)
> > > before kfree(d). As a corollary, we can't do the former in i2c-core and
> > > the later in device drivers.
> > 
> > This is where the mismatch between the subsystem view of the device
> > lifetime and the driver view of the device lifetime comes into play.
> > For the driver once the device is unregistered the device no longer
> > exists - if the driver tries to work with the device it's buggy.  This
> > means that to the driver returning from the remove() function is
> > dropping the reference to the data and there's no reason for the driver
> > to take any other action.
> > 
> > The device may hang around after the remove() has happened, but if the
> > device driver knows or cares about it then it's doing something wrong.
> > Similarly on probe() we can't assme anything about the pointer since
> > even if we saw the device before we can't guarantee that some other
> > driver didn't do so as well.  The situation is similar to that with
> > kfree() - we don't memset() data we're freeing with that, even though it
> > might contain pointers to other things.

I'm not sure if this comparison really holds. When you free memory,
nobody is supposed to use that memory afterwards, so whether it contains
pointers to other memory locations is irrelevant. Unbinding a device
doesn't make it disappear, it might still get access, now or later, so
the value of struct device members is more relevant. But of course we
can discuss the guarantees made on these struct members over the
device's lifetime. I don't this is documented anywhere.

> Indeed. The client data is for the client. Once the client is gone (unbound)
> the client data can safely be set back to NULL.
>  
> > > So we are in the difficult situation where we can't do both in i2c-core
> > > because that violates point 1 above, we can't do half in i2c-core and
> > > half in device drivers because this violates point 2 above, so we fall
> > > back to doing both in device drivers, which doesn't violate any point
> > > but duplicates the code all around.
> > 
> > Personally I'd much rather just not bother setting the driver data in
> > the removal path, it seems unneeded.  I had assumed that the subsystem
> > code cared for some reason when I saw the patch series.
> 
> Anyway, should this really be necessary, then for the media drivers this
> should be done in v4l2_device_unregister_subdev() and not in every driver.
> 
> But this just feels like an i2c core thing to me. After remove() is called
> the core should just set the client data to NULL. If there are drivers that
> rely on the current behavior, then those drivers should be reviewed first as
> to the reason why they need it.

I tend to agree, now.

Wolfram, how do you feel about this? I feel a little sorry that I more
or less encouraged you to submit this patch series, and now I get to
agree with the objections which were raised against it.

My key motivation was that I wanted i2c_set_clientdata() to be called
before kfree(). Now that everybody seems to agree that the latter
belongs to the drivers while the former belongs to lower layers
(i2c-core or even driver core), this is not going to happen. So I guess
we want to remove calls to i2c_set_clientdata(NULL) from all drivers
and have only one in i2c-core for now?

-- 
Jean Delvare

  parent reply	other threads:[~2010-03-22 20:33 UTC|newest]

Thread overview: 67+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-03-20 14:12 i2c-drivers: Remove dangling pointers Wolfram Sang
2010-03-20 14:12 ` [PATCH 04/24] gpio: fix " Wolfram Sang
     [not found]   ` <1269094385-16114-5-git-send-email-w.sang-bIcnvbaLZ9MEGnE8C9+IrQ@public.gmane.org>
2010-03-30 12:31     ` Wolfram Sang
2010-03-20 14:12 ` [PATCH 06/24] input/keyboard: " Wolfram Sang
2010-03-20 19:20   ` Dmitry Torokhov
     [not found]     ` <20100320192007.GA28402-WlK9ik9hQGAhIp7JRqBPierSzoNAToWh@public.gmane.org>
2010-03-21  1:59       ` Wolfram Sang
2010-03-30 12:35       ` Wolfram Sang
2010-03-20 14:12 ` [PATCH 08/24] leds: " Wolfram Sang
     [not found]   ` <1269094385-16114-9-git-send-email-w.sang-bIcnvbaLZ9MEGnE8C9+IrQ@public.gmane.org>
2010-03-30 12:36     ` Wolfram Sang
2010-03-20 14:12 ` [PATCH 09/24] macintosh: " Wolfram Sang
2010-03-20 14:12 ` [PATCH 10/24] media/radio: " Wolfram Sang
2010-03-20 14:12 ` [PATCH 12/24] media/video: " Wolfram Sang
2010-03-20 22:02   ` Hans Verkuil
     [not found]     ` <201003202302.49526.hverkuil-qWit8jRvyhVmR6Xm/wNWPw@public.gmane.org>
2010-03-21 11:31       ` Wolfram Sang
2010-03-21 13:46       ` Jean Delvare
     [not found]         ` <20100321144655.4747fd2a-ig7AzVSIIG7kN2dkZ6Wm7A@public.gmane.org>
2010-03-21 14:14           ` Mark Brown
2010-03-21 16:09             ` Hans Verkuil
     [not found]               ` <201003211709.56319.hverkuil-qWit8jRvyhVmR6Xm/wNWPw@public.gmane.org>
2010-03-22 20:33                 ` Jean Delvare [this message]
2010-03-22 21:51                   ` Mark Brown
2010-03-23  0:35                   ` Wolfram Sang
2010-03-30 12:39                 ` Wolfram Sang
2010-04-01  8:32                   ` Hans Verkuil
2010-03-22 20:36           ` Jean Delvare
2010-03-20 14:12 ` [PATCH 13/24] mfd: " Wolfram Sang
2010-03-20 17:22   ` Mark Brown
     [not found]     ` <20100320172241.GD1549-yzvPICuk2AATkU/dhu1WVueM+bqZidxxQQ4Iyu8u01E@public.gmane.org>
2010-03-20 20:25       ` Jean Delvare
2010-03-21  2:09     ` Wolfram Sang
     [not found]   ` <1269094385-16114-14-git-send-email-w.sang-bIcnvbaLZ9MEGnE8C9+IrQ@public.gmane.org>
2010-03-25 10:41     ` Samuel Ortiz
     [not found]       ` <20100325104137.GA3720-jcdQHdrhKHMdnm+yROfE0A@public.gmane.org>
2010-03-30 12:41         ` Wolfram Sang
2010-03-20 14:12 ` [PATCH 14/24] misc: " Wolfram Sang
     [not found]   ` <1269094385-16114-15-git-send-email-w.sang-bIcnvbaLZ9MEGnE8C9+IrQ@public.gmane.org>
2010-03-30 12:44     ` Wolfram Sang
2010-03-20 14:12 ` [PATCH 15/24] misc/eeprom: " Wolfram Sang
2010-03-20 14:12 ` [PATCH 18/24] rtc: " Wolfram Sang
     [not found]   ` <1269094385-16114-19-git-send-email-w.sang-bIcnvbaLZ9MEGnE8C9+IrQ@public.gmane.org>
2010-03-30 12:49     ` Wolfram Sang
2010-03-20 14:13 ` [PATCH 20/24] staging/go7007: " Wolfram Sang
2010-03-20 14:13 ` [PATCH 21/24] staging/iio/adc: " Wolfram Sang
     [not found]   ` <1269094385-16114-22-git-send-email-w.sang-bIcnvbaLZ9MEGnE8C9+IrQ@public.gmane.org>
2010-03-22 12:41     ` Jonathan Cameron
2010-03-20 14:13 ` [PATCH 23/24] video/matrox: " Wolfram Sang
     [not found]   ` <1269094385-16114-24-git-send-email-w.sang-bIcnvbaLZ9MEGnE8C9+IrQ@public.gmane.org>
2010-04-05 14:04     ` James Simmons
2010-03-20 14:13 ` [PATCH 24/24] w1/masters: " Wolfram Sang
     [not found] ` <1269094385-16114-1-git-send-email-w.sang-bIcnvbaLZ9MEGnE8C9+IrQ@public.gmane.org>
2010-03-20 14:12   ` [PATCH 01/24] mtd/maps/pismo: remove dangling pointer and a leak Wolfram Sang
     [not found]     ` <1269094385-16114-2-git-send-email-w.sang-bIcnvbaLZ9MEGnE8C9+IrQ@public.gmane.org>
2010-03-20 14:57       ` Russell King
     [not found]         ` <20100320145729.GA5399-f404yB8NqCZvn6HldHNs0ANdhmdF6hFW@public.gmane.org>
2010-03-20 15:31           ` Wolfram Sang
     [not found]             ` <20100320153146.GB5515-bIcnvbaLZ9MEGnE8C9+IrQ@public.gmane.org>
2010-03-20 15:41               ` Russell King
     [not found]                 ` <20100320154113.GB14470-f404yB8NqCZvn6HldHNs0ANdhmdF6hFW@public.gmane.org>
2010-03-21  8:30                   ` David Woodhouse
2010-03-30 12:26                     ` Wolfram Sang
2010-03-20 14:12   ` [PATCH 02/24] power/ds2782: really clear i2c_clientdata on exit Wolfram Sang
     [not found]     ` <1269094385-16114-3-git-send-email-w.sang-bIcnvbaLZ9MEGnE8C9+IrQ@public.gmane.org>
2010-03-20 21:53       ` Ryan Mallon
2010-03-22 17:08       ` Anton Vorontsov
     [not found]         ` <20100322170810.GA30325-wnGakbxT3iijyJ0x5qLZdcN33GVbZNy3@public.gmane.org>
2010-03-30 12:28           ` Wolfram Sang
2010-03-20 14:12   ` [PATCH 03/24] usb/otg/isp1301_omap: Fix dangling pointer Wolfram Sang
     [not found]     ` <1269094385-16114-4-git-send-email-w.sang-bIcnvbaLZ9MEGnE8C9+IrQ@public.gmane.org>
2010-03-20 20:09       ` Felipe Balbi
2010-03-30 12:30         ` Wolfram Sang
2010-03-20 14:12   ` [PATCH 05/24] hwmon: fix dangling pointers Wolfram Sang
     [not found]     ` <1269094385-16114-6-git-send-email-w.sang-bIcnvbaLZ9MEGnE8C9+IrQ@public.gmane.org>
2010-03-29 14:30       ` Jean Delvare
2010-03-20 14:12   ` [PATCH 07/24] input/touchscreen: " Wolfram Sang
2010-03-20 14:12   ` [PATCH 11/24] media/radio/si470x: " Wolfram Sang
2010-03-20 14:12   ` [PATCH 16/24] power: " Wolfram Sang
2010-03-20 14:12   ` [PATCH 17/24] regulator: " Wolfram Sang
     [not found]     ` <1269094385-16114-18-git-send-email-w.sang-bIcnvbaLZ9MEGnE8C9+IrQ@public.gmane.org>
2010-03-20 17:23       ` Mark Brown
2010-03-22 19:49     ` Liam Girdwood
2010-03-30 12:47       ` Wolfram Sang
2010-03-20 14:13   ` [PATCH 19/24] staging/dream: " Wolfram Sang
2010-03-20 14:13   ` [PATCH 22/24] staging/iio/light: " Wolfram Sang
2010-03-22 12:41     ` Jonathan Cameron
2010-04-05 22:59   ` i2c-drivers: Remove " Ben Dooks
     [not found]     ` <20100405225925.GC32401-elnMNo+KYs3pIgCt6eIbzw@public.gmane.org>
2010-04-06  0:06       ` Wolfram Sang

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=20100322213358.31e50b3c@hyperion.delvare \
    --to=khali-puyad+kwke1g9huczpvpmw@public.gmane.org \
    --cc=broonie-yzvPICuk2AATkU/dhu1WVueM+bqZidxxQQ4Iyu8u01E@public.gmane.org \
    --cc=hverkuil-qWit8jRvyhVmR6Xm/wNWPw@public.gmane.org \
    --cc=kernel-janitors-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=linux-i2c-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=linux-media-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=mchehab-wEGCiKHe2LqWVfeAwA7xHQ@public.gmane.org \
    --cc=w.sang-bIcnvbaLZ9MEGnE8C9+IrQ@public.gmane.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).