From: Lars-Peter Clausen <lars@metafoo.de>
To: Bill Gatliff <bgat@billgatliff.com>
Cc: linux-kernel@vger.kernel.org
Subject: Re: [PWM v6 1/3] PWM: Implement a generic PWM framework
Date: Thu, 10 Mar 2011 04:13:20 +0100 [thread overview]
Message-ID: <4D7841D0.5000407@metafoo.de> (raw)
In-Reply-To: <AANLkTinWwiyWkZ3vj6fpy7oe-PR6M+aVWoqh--+95Sgq@mail.gmail.com>
On 03/09/2011 06:05 PM, Bill Gatliff wrote:
> On Sun, Mar 6, 2011 at 12:16 AM, Lars-Peter Clausen <lars@metafoo.de> wrote:
>
>>
>>> + if (IS_ERR(p->dev)) {
>>> + ret = PTR_ERR(p->dev);
>>> + goto err_device_create;
>>> + }
>> I think it would be better to embedd the device struct directly into the
>> pwm_device struct. You could also remove the data field of the pwm_device
>> struct and use dev_{get,set}_drvdata for pwm_{get,set}_drvdata.
>
> In theory I agree with you, but it would take away my ability to do
> things like device_create_vargs() and thereby make my code a bit more
> complicated and error prone. Do you think the advantages outweigh the
> disadvantages?
>
The advantages are less memory fragmentation, less cache misses and slightly
smaller generated code, but in practice this won't probably be noticeable since
there wont be vast amounts of PWM devices.
I prefer it because I think it is the nicer model.
But aside from that I've been looking into reference counting, locking and
possible races with the current framework.
So and as I see it there are two major problems:
The first problem is, that if there is no indirect refcounting on the module
which implements a device driver. For example if the parent is NULL or in a
different module, it is possible to unload the module while the driver is still
in use.
This could be fixed for example by adding a owner field to the pwm_device_ops
struct and get a reference to the owner when requesting a device.
Another option would simply be to declare it API misuse if there is not parent
or the parent has not the same owner.
The second problem is a bit serious. There is a chance of use after free.
There could still be opened sysfs files when pwm_device_unregister is called.
And thus it is possible that the opened file is read after
pwm_device_unregister has been called and the pwm_device struct might already
be freed thus causing access to already freed memory.
To fix this I suggest to move the allocation of the pwm_device to
pwm_device_register and pass in the ops instead of the device itself. Maybe the
struct could even be made opaque to the outside word in this process.
The struct would be freed from the devices 'release' callback, which is only
called after the last reference to the device has been dropped.
Related to this problem is what happens when pwm_device_unregister is called
while the device is requested. The function returns -EBUSY, but non of your
drivers currently check the return value and free the pwm_device struct anyway.
So there is going to be a use after free too.
Also the reference count of the pwm device is increased in pwm_request (by
class_find_device), but that reference is never released. A device_put should
be added to pwm_release().
- Lars
next prev parent reply other threads:[~2011-03-10 3:12 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-03-06 4:17 [PWM v6 0/3] Implement a generic PWM framework Bill Gatliff
2011-03-06 4:17 ` [PWM v6 1/3] PWM: " Bill Gatliff
2011-03-06 6:16 ` Lars-Peter Clausen
2011-03-06 6:35 ` Lars-Peter Clausen
2011-03-07 16:28 ` Bill Gatliff
2011-03-07 19:26 ` Lars-Peter Clausen
2011-03-09 16:15 ` Bill Gatliff
2011-03-07 16:26 ` Bill Gatliff
2011-03-07 19:02 ` Lars-Peter Clausen
2011-03-09 16:19 ` Bill Gatliff
2011-03-09 17:05 ` Bill Gatliff
2011-03-10 3:13 ` Lars-Peter Clausen [this message]
2011-03-06 12:16 ` Linus Walleij
2011-03-07 16:30 ` Bill Gatliff
2011-03-07 4:20 ` Jack Stone
2011-03-07 16:35 ` Bill Gatliff
2011-03-07 17:08 ` Bill Gatliff
2011-03-08 18:12 ` Jack Stone
2011-03-09 16:19 ` Bill Gatliff
2011-03-06 4:17 ` [PWM v6 2/3] PWM: GPIO+hrtimer device emulation Bill Gatliff
2011-03-06 4:17 ` [PWM v6 3/3] PWM: Atmel PWMC driver Bill Gatliff
-- strict thread matches above, loose matches on Subject: below --
2011-02-21 3:35 [PWM v6 0/3] Implement a generic PWM framework Bill Gatliff
2011-02-21 3:35 ` [PWM v6 1/3] PWM: " Bill Gatliff
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=4D7841D0.5000407@metafoo.de \
--to=lars@metafoo.de \
--cc=bgat@billgatliff.com \
--cc=linux-kernel@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.