All of lore.kernel.org
 help / color / mirror / Atom feed
From: Suman Anna <s-anna@ti.com>
To: Rob Herring <robherring2@gmail.com>
Cc: Grant Likely <grant.likely@linaro.org>,
	Rob Herring <robh+dt@kernel.org>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Pawel Moll <pawel.moll@arm.com>,
	Pantelis Antoniou <pantelis.antoniou@konsulko.com>,
	Felipe Balbi <balbi@ti.com>,
	"devicetree@vger.kernel.org" <devicetree@vger.kernel.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	"linux-arm-kernel@lists.infradead.org" 
	<linux-arm-kernel@lists.infradead.org>,
	linux-omap <linux-omap@vger.kernel.org>
Subject: Re: [RFC PATCH 1/3] of/device: manage resources similar to platform_device_add
Date: Tue, 13 Jan 2015 17:04:57 -0600	[thread overview]
Message-ID: <54B5A499.7060106@ti.com> (raw)
In-Reply-To: <CAL_JsqLW9PRktHntLquohH3x_bPgJrrn2Y2_8RYA_HnKhpTDHg@mail.gmail.com>

On 01/13/2015 04:00 PM, Rob Herring wrote:
> On Tue, Jan 13, 2015 at 3:25 PM, Suman Anna <s-anna@ti.com> wrote:
>> Hi Rob,
>>
>> On 01/13/2015 02:38 PM, Rob Herring wrote:
>>> On Wed, Jan 7, 2015 at 11:30 AM, Suman Anna <s-anna@ti.com> wrote:
>>>> Drivers can use of_platform_populate() to create platform devices
>>>> for children of the device main node, and a complementary API
>>>> of_platform_depopulate() is provided to delete these child platform
>>>> devices. The of_platform_depopulate() leverages the platform API
>>>> for performing the cleanup of these devices.
>>>>
>>>> The platform device resources are managed differently between
>>>> of_device_add and platform_device_add, and this asymmetry causes
>>>> a kernel oops in platform_device_del during removal of the resources.
>>>> Manage the platform device resources similar to platform_device_add
>>>> to fix this kernel oops.
>>>
>>> This is a known issue and has been attempted to be fixed before (I
>>> believe there is a revert in mainline). The problem is there are known
>>> devicetrees which have overlapping resources and they will break with
>>> your change.
>>
>> Are you referring to 02bbde7849e6 (Revert "of: use
>> platform_device_add")?
> 
> I believe that's the one.
> 
>> That one seems to be in registration path, and
>> this crash is in the unregistration path. If so, to fix the crash,
>> should we be skipping the release_resource() for now in
>> platform_device_del for DT nodes, or replace platform_device_unregister
>> with of_device_unregister in of_platform_device_destroy()?
> 
> IIRC, the problem is inserting a resource twice on add from 2
> different nodes, not the removal path. Perhaps we could make a
> collision non-fatal for in the DT case.

We may be talking two different things here, I understand that this
patch would create an issue with inserting a resource twice in the
devicetrees with overlapping resources (just like the commit that was
reverted above), but the crash is on devices with resources whose
parent, child, sibling pointers have never been initialized (the
of_device_add path does not touch these at all), and get dereferenced in
platform_device_del()->release_resource(). See the following that has a
better explanation [1].

regards
Suman

[1]
http://lists.infradead.org/pipermail/linux-arm-kernel/2014-July/274412.html


> Grant may have some ideas on
> what's needed here.
> 
>> This is a common crash and we cannot use of_platform_depopulate() today
>> in drivers to complement of_platform_populate().
> 
> Yes, I know.
> 
>> Also, the platform_data crash is independent of this, I could reproduce
>> that one even with using of_device_unregister in a loop in driver remove.
> 
> Missed this one. I'll reply to that patch.
> 
> Rob
> 
>>
>> regards
>> Suman
>>
>>>
>>> Rob
>>>
>>>>
>>>> Signed-off-by: Suman Anna <s-anna@ti.com>
>>>> ---
>>>>  drivers/of/device.c | 38 +++++++++++++++++++++++++++++++++++++-
>>>>  1 file changed, 37 insertions(+), 1 deletion(-)
>>>>
>>>> diff --git a/drivers/of/device.c b/drivers/of/device.c
>>>> index 46d6c75c1404..fa27c1c71f29 100644
>>>> --- a/drivers/of/device.c
>>>> +++ b/drivers/of/device.c
>>>> @@ -50,6 +50,8 @@ EXPORT_SYMBOL(of_dev_put);
>>>>
>>>>  int of_device_add(struct platform_device *ofdev)
>>>>  {
>>>> +       int i, ret;
>>>> +
>>>>         BUG_ON(ofdev->dev.of_node == NULL);
>>>>
>>>>         /* name and id have to be set so that the platform bus doesn't get
>>>> @@ -63,7 +65,41 @@ int of_device_add(struct platform_device *ofdev)
>>>>         if (!ofdev->dev.parent)
>>>>                 set_dev_node(&ofdev->dev, of_node_to_nid(ofdev->dev.of_node));
>>>>
>>>> -       return device_add(&ofdev->dev);
>>>> +       for (i = 0; i < ofdev->num_resources; i++) {
>>>> +               struct resource *p, *r = &ofdev->resource[i];
>>>> +
>>>> +               if (!r->name)
>>>> +                       r->name = dev_name(&ofdev->dev);
>>>> +
>>>> +               p = r->parent;
>>>> +               if (!p) {
>>>> +                       if (resource_type(r) == IORESOURCE_MEM)
>>>> +                               p = &iomem_resource;
>>>> +                       else if (resource_type(r) == IORESOURCE_IO)
>>>> +                               p = &ioport_resource;
>>>> +               }
>>>> +
>>>> +               if (p && insert_resource(p, r)) {
>>>> +                       dev_err(&ofdev->dev, "failed to claim resource %d\n",
>>>> +                               i);
>>>> +                       ret = -EBUSY;
>>>> +                       goto failed;
>>>> +               }
>>>> +       }
>>>> +
>>>> +       ret = device_add(&ofdev->dev);
>>>> +       if (ret == 0)
>>>> +               return ret;
>>>> +
>>>> +failed:
>>>> +       while (--i >= 0) {
>>>> +               struct resource *r = &ofdev->resource[i];
>>>> +               unsigned long type = resource_type(r);
>>>> +
>>>> +               if (type == IORESOURCE_MEM || type == IORESOURCE_IO)
>>>> +                       release_resource(r);
>>>> +       }
>>>> +       return ret;
>>>>  }
>>>>
>>>>  int of_device_register(struct platform_device *pdev)
>>>> --
>>>> 2.2.1
>>>>
>>


WARNING: multiple messages have this Message-ID (diff)
From: s-anna@ti.com (Suman Anna)
To: linux-arm-kernel@lists.infradead.org
Subject: [RFC PATCH 1/3] of/device: manage resources similar to platform_device_add
Date: Tue, 13 Jan 2015 17:04:57 -0600	[thread overview]
Message-ID: <54B5A499.7060106@ti.com> (raw)
In-Reply-To: <CAL_JsqLW9PRktHntLquohH3x_bPgJrrn2Y2_8RYA_HnKhpTDHg@mail.gmail.com>

On 01/13/2015 04:00 PM, Rob Herring wrote:
> On Tue, Jan 13, 2015 at 3:25 PM, Suman Anna <s-anna@ti.com> wrote:
>> Hi Rob,
>>
>> On 01/13/2015 02:38 PM, Rob Herring wrote:
>>> On Wed, Jan 7, 2015 at 11:30 AM, Suman Anna <s-anna@ti.com> wrote:
>>>> Drivers can use of_platform_populate() to create platform devices
>>>> for children of the device main node, and a complementary API
>>>> of_platform_depopulate() is provided to delete these child platform
>>>> devices. The of_platform_depopulate() leverages the platform API
>>>> for performing the cleanup of these devices.
>>>>
>>>> The platform device resources are managed differently between
>>>> of_device_add and platform_device_add, and this asymmetry causes
>>>> a kernel oops in platform_device_del during removal of the resources.
>>>> Manage the platform device resources similar to platform_device_add
>>>> to fix this kernel oops.
>>>
>>> This is a known issue and has been attempted to be fixed before (I
>>> believe there is a revert in mainline). The problem is there are known
>>> devicetrees which have overlapping resources and they will break with
>>> your change.
>>
>> Are you referring to 02bbde7849e6 (Revert "of: use
>> platform_device_add")?
> 
> I believe that's the one.
> 
>> That one seems to be in registration path, and
>> this crash is in the unregistration path. If so, to fix the crash,
>> should we be skipping the release_resource() for now in
>> platform_device_del for DT nodes, or replace platform_device_unregister
>> with of_device_unregister in of_platform_device_destroy()?
> 
> IIRC, the problem is inserting a resource twice on add from 2
> different nodes, not the removal path. Perhaps we could make a
> collision non-fatal for in the DT case.

We may be talking two different things here, I understand that this
patch would create an issue with inserting a resource twice in the
devicetrees with overlapping resources (just like the commit that was
reverted above), but the crash is on devices with resources whose
parent, child, sibling pointers have never been initialized (the
of_device_add path does not touch these at all), and get dereferenced in
platform_device_del()->release_resource(). See the following that has a
better explanation [1].

regards
Suman

[1]
http://lists.infradead.org/pipermail/linux-arm-kernel/2014-July/274412.html


> Grant may have some ideas on
> what's needed here.
> 
>> This is a common crash and we cannot use of_platform_depopulate() today
>> in drivers to complement of_platform_populate().
> 
> Yes, I know.
> 
>> Also, the platform_data crash is independent of this, I could reproduce
>> that one even with using of_device_unregister in a loop in driver remove.
> 
> Missed this one. I'll reply to that patch.
> 
> Rob
> 
>>
>> regards
>> Suman
>>
>>>
>>> Rob
>>>
>>>>
>>>> Signed-off-by: Suman Anna <s-anna@ti.com>
>>>> ---
>>>>  drivers/of/device.c | 38 +++++++++++++++++++++++++++++++++++++-
>>>>  1 file changed, 37 insertions(+), 1 deletion(-)
>>>>
>>>> diff --git a/drivers/of/device.c b/drivers/of/device.c
>>>> index 46d6c75c1404..fa27c1c71f29 100644
>>>> --- a/drivers/of/device.c
>>>> +++ b/drivers/of/device.c
>>>> @@ -50,6 +50,8 @@ EXPORT_SYMBOL(of_dev_put);
>>>>
>>>>  int of_device_add(struct platform_device *ofdev)
>>>>  {
>>>> +       int i, ret;
>>>> +
>>>>         BUG_ON(ofdev->dev.of_node == NULL);
>>>>
>>>>         /* name and id have to be set so that the platform bus doesn't get
>>>> @@ -63,7 +65,41 @@ int of_device_add(struct platform_device *ofdev)
>>>>         if (!ofdev->dev.parent)
>>>>                 set_dev_node(&ofdev->dev, of_node_to_nid(ofdev->dev.of_node));
>>>>
>>>> -       return device_add(&ofdev->dev);
>>>> +       for (i = 0; i < ofdev->num_resources; i++) {
>>>> +               struct resource *p, *r = &ofdev->resource[i];
>>>> +
>>>> +               if (!r->name)
>>>> +                       r->name = dev_name(&ofdev->dev);
>>>> +
>>>> +               p = r->parent;
>>>> +               if (!p) {
>>>> +                       if (resource_type(r) == IORESOURCE_MEM)
>>>> +                               p = &iomem_resource;
>>>> +                       else if (resource_type(r) == IORESOURCE_IO)
>>>> +                               p = &ioport_resource;
>>>> +               }
>>>> +
>>>> +               if (p && insert_resource(p, r)) {
>>>> +                       dev_err(&ofdev->dev, "failed to claim resource %d\n",
>>>> +                               i);
>>>> +                       ret = -EBUSY;
>>>> +                       goto failed;
>>>> +               }
>>>> +       }
>>>> +
>>>> +       ret = device_add(&ofdev->dev);
>>>> +       if (ret == 0)
>>>> +               return ret;
>>>> +
>>>> +failed:
>>>> +       while (--i >= 0) {
>>>> +               struct resource *r = &ofdev->resource[i];
>>>> +               unsigned long type = resource_type(r);
>>>> +
>>>> +               if (type == IORESOURCE_MEM || type == IORESOURCE_IO)
>>>> +                       release_resource(r);
>>>> +       }
>>>> +       return ret;
>>>>  }
>>>>
>>>>  int of_device_register(struct platform_device *pdev)
>>>> --
>>>> 2.2.1
>>>>
>>

  reply	other threads:[~2015-01-13 23:05 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-01-07 17:30 [RFC PATCH 0/3] of_platform_depopulate crash fixes Suman Anna
2015-01-07 17:30 ` Suman Anna
2015-01-07 17:30 ` Suman Anna
2015-01-07 17:30 ` [RFC PATCH 1/3] of/device: manage resources similar to platform_device_add Suman Anna
2015-01-07 17:30   ` Suman Anna
2015-01-07 17:30   ` Suman Anna
2015-01-13 20:38   ` Rob Herring
2015-01-13 20:38     ` Rob Herring
2015-01-13 20:38     ` Rob Herring
2015-01-13 21:25     ` Suman Anna
2015-01-13 21:25       ` Suman Anna
2015-01-13 21:25       ` Suman Anna
2015-01-13 22:00       ` Rob Herring
2015-01-13 22:00         ` Rob Herring
2015-01-13 22:00         ` Rob Herring
2015-01-13 23:04         ` Suman Anna [this message]
2015-01-13 23:04           ` Suman Anna
2015-01-13 23:04           ` Suman Anna
2015-01-22 21:49           ` Suman Anna
2015-01-22 21:49             ` Suman Anna
2015-01-22 21:49             ` Suman Anna
2015-03-20 11:29             ` Grant Likely
2015-03-20 11:29               ` Grant Likely
2015-03-20 11:29               ` Grant Likely
2015-01-07 17:30 ` [RFC PATCH 2/3] core: platform: fix an invalid kfree during of_platform_depopulate Suman Anna
2015-01-07 17:30   ` Suman Anna
2015-01-07 17:30   ` Suman Anna
2015-01-13 22:27   ` Rob Herring
2015-01-13 22:27     ` Rob Herring
2015-01-13 22:27     ` Rob Herring
2015-01-13 22:53     ` Suman Anna
2015-01-13 22:53       ` Suman Anna
2015-01-13 22:53       ` Suman Anna
2015-01-07 17:30 ` [RFC PATCH 3/3] of/unittest: fix trailing semi-colons on conditional selftest Suman Anna
2015-01-07 17:30   ` Suman Anna
2015-01-07 17:30   ` Suman Anna

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=54B5A499.7060106@ti.com \
    --to=s-anna@ti.com \
    --cc=balbi@ti.com \
    --cc=devicetree@vger.kernel.org \
    --cc=grant.likely@linaro.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-omap@vger.kernel.org \
    --cc=pantelis.antoniou@konsulko.com \
    --cc=pawel.moll@arm.com \
    --cc=robh+dt@kernel.org \
    --cc=robherring2@gmail.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 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.