linux-pci.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/1] PCI: fix on-stack pci_device_id's
@ 2012-09-10 19:36 Jiri Slaby
  2012-09-10 20:44 ` Bjorn Helgaas
  0 siblings, 1 reply; 4+ messages in thread
From: Jiri Slaby @ 2012-09-10 19:36 UTC (permalink / raw)
  To: bhelgaas; +Cc: linux-pci, linux-kernel, jirislaby, Feng Tang, Fengguang Wu

Commit "PCI: Use pci_device_id on stack for pci_get_subsys/class() to
avoid kmalloc" changed heap allocations to on-stack variables, but it
did not add initialization of other than set members. This causes
random failures during bootup wherever pci device is needed to be
found. Hence the boot just hangs or panics.

This patches fixes it by setting the content of pci_device_id directly
in the initializer.

Signed-off-by: Jiri Slaby <jslaby@suse.cz>
Cc: Feng Tang <feng.tang@intel.com>
Cc: Bjorn Helgaas <bhelgaas@google.com>
Cc: Fengguang Wu <fengguang.wu@intel.com>
---
 drivers/pci/search.c | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)

diff --git a/drivers/pci/search.c b/drivers/pci/search.c
index 00d486a..bf969ba 100644
--- a/drivers/pci/search.c
+++ b/drivers/pci/search.c
@@ -243,12 +243,12 @@ struct pci_dev *pci_get_subsys(unsigned int vendor, unsigned int device,
 			       unsigned int ss_vendor, unsigned int ss_device,
 			       struct pci_dev *from)
 {
-	struct pci_device_id id;
-
-	id.vendor = vendor;
-	id.device = device;
-	id.subvendor = ss_vendor;
-	id.subdevice = ss_device;
+	struct pci_device_id id = {
+		.vendor = vendor,
+		.device = device,
+		.subvendor = ss_vendor,
+		.subdevice = ss_device,
+	};
 
 	return pci_get_dev_by_id(&id, from);
 }
@@ -289,11 +289,14 @@ pci_get_device(unsigned int vendor, unsigned int device, struct pci_dev *from)
  */
 struct pci_dev *pci_get_class(unsigned int class, struct pci_dev *from)
 {
-	struct pci_device_id id;
-
-	id.vendor = id.device = id.subvendor = id.subdevice = PCI_ANY_ID;
-	id.class_mask = PCI_ANY_ID;
-	id.class = class;
+	struct pci_device_id id = {
+		.vendor = PCI_ANY_ID,
+		.device = PCI_ANY_ID,
+		.subvendor = PCI_ANY_ID,
+		.subdevice = PCI_ANY_ID,
+		.class_mask = PCI_ANY_ID,
+		.class = class,
+	};
 
 	return pci_get_dev_by_id(&id, from);
 }
-- 
1.7.11.5



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/1] PCI: fix on-stack pci_device_id's
  2012-09-10 19:36 [PATCH 1/1] PCI: fix on-stack pci_device_id's Jiri Slaby
@ 2012-09-10 20:44 ` Bjorn Helgaas
  2012-09-10 22:55   ` Fengguang Wu
  0 siblings, 1 reply; 4+ messages in thread
From: Bjorn Helgaas @ 2012-09-10 20:44 UTC (permalink / raw)
  To: Jiri Slaby; +Cc: linux-pci, linux-kernel, jirislaby, Feng Tang, Fengguang Wu

On Mon, Sep 10, 2012 at 1:36 PM, Jiri Slaby <jslaby@suse.cz> wrote:
> Commit "PCI: Use pci_device_id on stack for pci_get_subsys/class() to
> avoid kmalloc" changed heap allocations to on-stack variables, but it
> did not add initialization of other than set members. This causes
> random failures during bootup wherever pci device is needed to be
> found. Hence the boot just hangs or panics.
>
> This patches fixes it by setting the content of pci_device_id directly
> in the initializer.

Nice!  I fixed this already by adding a memset(), but I like your way
better.  I knew we could initialize members of a structure on the
stack, but I didn't know C guaranteed that uninitialized members would
be implicitly initialized also.  But apparently it does:

http://stackoverflow.com/questions/10828294/c-and-c-partial-initialization-of-automatic-structure

> Signed-off-by: Jiri Slaby <jslaby@suse.cz>
> Cc: Feng Tang <feng.tang@intel.com>
> Cc: Bjorn Helgaas <bhelgaas@google.com>
> Cc: Fengguang Wu <fengguang.wu@intel.com>
> ---
>  drivers/pci/search.c | 25 ++++++++++++++-----------
>  1 file changed, 14 insertions(+), 11 deletions(-)
>
> diff --git a/drivers/pci/search.c b/drivers/pci/search.c
> index 00d486a..bf969ba 100644
> --- a/drivers/pci/search.c
> +++ b/drivers/pci/search.c
> @@ -243,12 +243,12 @@ struct pci_dev *pci_get_subsys(unsigned int vendor, unsigned int device,
>                                unsigned int ss_vendor, unsigned int ss_device,
>                                struct pci_dev *from)
>  {
> -       struct pci_device_id id;
> -
> -       id.vendor = vendor;
> -       id.device = device;
> -       id.subvendor = ss_vendor;
> -       id.subdevice = ss_device;
> +       struct pci_device_id id = {
> +               .vendor = vendor,
> +               .device = device,
> +               .subvendor = ss_vendor,
> +               .subdevice = ss_device,
> +       };
>
>         return pci_get_dev_by_id(&id, from);
>  }
> @@ -289,11 +289,14 @@ pci_get_device(unsigned int vendor, unsigned int device, struct pci_dev *from)
>   */
>  struct pci_dev *pci_get_class(unsigned int class, struct pci_dev *from)
>  {
> -       struct pci_device_id id;
> -
> -       id.vendor = id.device = id.subvendor = id.subdevice = PCI_ANY_ID;
> -       id.class_mask = PCI_ANY_ID;
> -       id.class = class;
> +       struct pci_device_id id = {
> +               .vendor = PCI_ANY_ID,
> +               .device = PCI_ANY_ID,
> +               .subvendor = PCI_ANY_ID,
> +               .subdevice = PCI_ANY_ID,
> +               .class_mask = PCI_ANY_ID,
> +               .class = class,
> +       };
>
>         return pci_get_dev_by_id(&id, from);
>  }
> --
> 1.7.11.5
>
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/1] PCI: fix on-stack pci_device_id's
  2012-09-10 20:44 ` Bjorn Helgaas
@ 2012-09-10 22:55   ` Fengguang Wu
  2012-09-11  0:02     ` Bjorn Helgaas
  0 siblings, 1 reply; 4+ messages in thread
From: Fengguang Wu @ 2012-09-10 22:55 UTC (permalink / raw)
  To: Bjorn Helgaas; +Cc: Jiri Slaby, linux-pci, linux-kernel, jirislaby, Feng Tang

On Mon, Sep 10, 2012 at 02:44:32PM -0600, Bjorn Helgaas wrote:
> On Mon, Sep 10, 2012 at 1:36 PM, Jiri Slaby <jslaby@suse.cz> wrote:
> > Commit "PCI: Use pci_device_id on stack for pci_get_subsys/class() to
> > avoid kmalloc" changed heap allocations to on-stack variables, but it
> > did not add initialization of other than set members. This causes
> > random failures during bootup wherever pci device is needed to be
> > found. Hence the boot just hangs or panics.
> >
> > This patches fixes it by setting the content of pci_device_id directly
> > in the initializer.
> 
> Nice!  I fixed this already by adding a memset(), but I like your way
> better.  I knew we could initialize members of a structure on the
> stack, but I didn't know C guaranteed that uninitialized members would
> be implicitly initialized also.  But apparently it does:
> 
> http://stackoverflow.com/questions/10828294/c-and-c-partial-initialization-of-automatic-structure

Signed-off-by: Fengguang Wu <fengguang.wu@intel.com>

For the record, I proposed the same initializer-style fix in the very
beginning that you didn't realize its beauty until now ;-)

Thanks,
Fengguang

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/1] PCI: fix on-stack pci_device_id's
  2012-09-10 22:55   ` Fengguang Wu
@ 2012-09-11  0:02     ` Bjorn Helgaas
  0 siblings, 0 replies; 4+ messages in thread
From: Bjorn Helgaas @ 2012-09-11  0:02 UTC (permalink / raw)
  To: Fengguang Wu; +Cc: Jiri Slaby, linux-pci, linux-kernel, jirislaby, Feng Tang

On Mon, Sep 10, 2012 at 4:55 PM, Fengguang Wu <fengguang.wu@intel.com> wrote:
> On Mon, Sep 10, 2012 at 02:44:32PM -0600, Bjorn Helgaas wrote:
>> On Mon, Sep 10, 2012 at 1:36 PM, Jiri Slaby <jslaby@suse.cz> wrote:
>> > Commit "PCI: Use pci_device_id on stack for pci_get_subsys/class() to
>> > avoid kmalloc" changed heap allocations to on-stack variables, but it
>> > did not add initialization of other than set members. This causes
>> > random failures during bootup wherever pci device is needed to be
>> > found. Hence the boot just hangs or panics.
>> >
>> > This patches fixes it by setting the content of pci_device_id directly
>> > in the initializer.
>>
>> Nice!  I fixed this already by adding a memset(), but I like your way
>> better.  I knew we could initialize members of a structure on the
>> stack, but I didn't know C guaranteed that uninitialized members would
>> be implicitly initialized also.  But apparently it does:
>>
>> http://stackoverflow.com/questions/10828294/c-and-c-partial-initialization-of-automatic-structure
>
> Signed-off-by: Fengguang Wu <fengguang.wu@intel.com>
>
> For the record, I proposed the same initializer-style fix in the very
> beginning that you didn't realize its beauty until now ;-)

Yep, I credited you :)  Sorry I didn't look closely enough to begin with.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2012-09-11  0:03 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-09-10 19:36 [PATCH 1/1] PCI: fix on-stack pci_device_id's Jiri Slaby
2012-09-10 20:44 ` Bjorn Helgaas
2012-09-10 22:55   ` Fengguang Wu
2012-09-11  0:02     ` Bjorn Helgaas

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).