2009-12-06 09:16:11

by Julia Lawall

[permalink] [raw]
Subject: [PATCH 3/5] drivers/infiniband: correct size computation

From: Julia Lawall <[email protected]>

The size argument to ioremap_nocache should be the size of desired
information, not the pointer to it.

The semantic patch that makes this change is as follows:
(http://coccinelle.lip6.fr/)

// <smpl>
@expression@
expression *x;
@@

x =
<+...
-sizeof(x)
+sizeof(*x)
...+>// </smpl>

Signed-off-by: Julia Lawall <[email protected]>

---
drivers/infiniband/hw/nes/nes.c | 3 ++-
1 files changed, 2 insertions(+), 1 deletions(-)

diff --git a/drivers/infiniband/hw/nes/nes.c b/drivers/infiniband/hw/nes/nes.c
index cbde0cf..b8c5372 100644
--- a/drivers/infiniband/hw/nes/nes.c
+++ b/drivers/infiniband/hw/nes/nes.c
@@ -521,7 +521,8 @@ static int __devinit nes_probe(struct pci_dev *pcidev, const struct pci_device_i
spin_lock_init(&nesdev->indexed_regs_lock);

/* Remap the PCI registers in adapter BAR0 to kernel VA space */
- mmio_regs = ioremap_nocache(pci_resource_start(pcidev, BAR_0), sizeof(mmio_regs));
+ mmio_regs = ioremap_nocache(pci_resource_start(pcidev, BAR_0),
+ sizeof(*mmio_regs));
if (mmio_regs == NULL) {
printk(KERN_ERR PFX "Unable to remap BAR0\n");
ret = -EIO;


2009-12-07 16:17:13

by Chien Tin Tung

[permalink] [raw]
Subject: RE: [PATCH 3/5] drivers/infiniband: correct size computation

Thanks for pointing out the bug.

> /* Remap the PCI registers in adapter BAR0 to kernel VA space */
>- mmio_regs = ioremap_nocache(pci_resource_start(pcidev, BAR_0), sizeof(mmio_regs));
>+ mmio_regs = ioremap_nocache(pci_resource_start(pcidev, BAR_0),
>+ sizeof(*mmio_regs));
> if (mmio_regs == NULL) {
> printk(KERN_ERR PFX "Unable to remap BAR0\n");
> ret = -EIO;

mmio_regs is initialized to NULL at the top of the function so *mmio_regs wouldn't be
a good idea. Instead of sizeof(*mmio_regs) use pci_resource_len(pcidev, BAR_0). If
you can recreate the patch with this change I will ack it.

Thanks,


Chien

2009-12-07 16:22:14

by Julia Lawall

[permalink] [raw]
Subject: RE: [PATCH 3/5] drivers/infiniband: correct size computation

On Mon, 7 Dec 2009, Tung, Chien Tin wrote:

> Thanks for pointing out the bug.
>
> > /* Remap the PCI registers in adapter BAR0 to kernel VA space */
> >- mmio_regs = ioremap_nocache(pci_resource_start(pcidev, BAR_0), sizeof(mmio_regs));
> >+ mmio_regs = ioremap_nocache(pci_resource_start(pcidev, BAR_0),
> >+ sizeof(*mmio_regs));
> > if (mmio_regs == NULL) {
> > printk(KERN_ERR PFX "Unable to remap BAR0\n");
> > ret = -EIO;
>
> mmio_regs is initialized to NULL at the top of the function so
> *mmio_regs wouldn't be a good idea. Instead of sizeof(*mmio_regs) use
> pci_resource_len(pcidev, BAR_0). If you can recreate the patch with
> this change I will ack it.

When you say that it isn't a good idea, do you mean that the result is
wrong, or that it looks odd? I didn't think sizeof looked at the value,
but only at the type?

julia

2009-12-07 16:38:54

by Chien Tin Tung

[permalink] [raw]
Subject: RE: [PATCH 3/5] drivers/infiniband: correct size computation

>> > /* Remap the PCI registers in adapter BAR0 to kernel VA space */
>> >- mmio_regs = ioremap_nocache(pci_resource_start(pcidev, BAR_0), sizeof(mmio_regs));
>> >+ mmio_regs = ioremap_nocache(pci_resource_start(pcidev, BAR_0),
>> >+ sizeof(*mmio_regs));
>> > if (mmio_regs == NULL) {
>> > printk(KERN_ERR PFX "Unable to remap BAR0\n");
>> > ret = -EIO;
>>
>> mmio_regs is initialized to NULL at the top of the function so
>> *mmio_regs wouldn't be a good idea. Instead of sizeof(*mmio_regs) use
>> pci_resource_len(pcidev, BAR_0). If you can recreate the patch with
>> this change I will ack it.
>
>When you say that it isn't a good idea, do you mean that the result is
>wrong, or that it looks odd? I didn't think sizeof looked at the value,
>but only at the type?


I misspoke on the problem. Mmio_regs is declared as a void pointer:

void __iomem *mmio_regs = NULL;

Thus pci_resource_len(pcidev, BAR_0) is the correct fix.

Chien

2009-12-07 16:42:06

by Julia Lawall

[permalink] [raw]
Subject: RE: [PATCH 3/5] drivers/infiniband: correct size computation

On Mon, 7 Dec 2009, Tung, Chien Tin wrote:

> >> > /* Remap the PCI registers in adapter BAR0 to kernel VA space */
> >> >- mmio_regs = ioremap_nocache(pci_resource_start(pcidev, BAR_0), sizeof(mmio_regs));
> >> >+ mmio_regs = ioremap_nocache(pci_resource_start(pcidev, BAR_0),
> >> >+ sizeof(*mmio_regs));
> >> > if (mmio_regs == NULL) {
> >> > printk(KERN_ERR PFX "Unable to remap BAR0\n");
> >> > ret = -EIO;
> >>
> >> mmio_regs is initialized to NULL at the top of the function so
> >> *mmio_regs wouldn't be a good idea. Instead of sizeof(*mmio_regs) use
> >> pci_resource_len(pcidev, BAR_0). If you can recreate the patch with
> >> this change I will ack it.
> >
> >When you say that it isn't a good idea, do you mean that the result is
> >wrong, or that it looks odd? I didn't think sizeof looked at the value,
> >but only at the type?
>
>
> I misspoke on the problem. Mmio_regs is declared as a void pointer:
>
> void __iomem *mmio_regs = NULL;
>
> Thus pci_resource_len(pcidev, BAR_0) is the correct fix.

OK, thanks for the clarification. I will make another patch later today.

julia

2009-12-07 19:57:05

by Julia Lawall

[permalink] [raw]
Subject: RE: [PATCH 3/5] drivers/infiniband: correct size computation

From: Julia Lawall <[email protected]>

The size argument to ioremap_nocache should be the size of desired
information, not the pointer to it.

The semantic match that finds this problem is as follows:
(http://coccinelle.lip6.fr/)

// <smpl>
@expression@
expression *x;
@@

x =
<+...
*sizeof(x)
...+>// </smpl>

Signed-off-by: Julia Lawall <[email protected]>

---
drivers/infiniband/hw/nes/nes.c | 3 ++-
1 files changed, 2 insertions(+), 1 deletions(-)

diff --git a/drivers/infiniband/hw/nes/nes.c b/drivers/infiniband/hw/nes/nes.c
index cbde0cf..95db98f 100644
--- a/drivers/infiniband/hw/nes/nes.c
+++ b/drivers/infiniband/hw/nes/nes.c
@@ -521,7 +521,8 @@ static int __devinit nes_probe(struct pci_dev *pcidev, const struct pci_device_i
spin_lock_init(&nesdev->indexed_regs_lock);

/* Remap the PCI registers in adapter BAR0 to kernel VA space */
- mmio_regs = ioremap_nocache(pci_resource_start(pcidev, BAR_0), sizeof(mmio_regs));
+ mmio_regs = ioremap_nocache(pci_resource_start(pcidev, BAR_0),
+ pci_resource_len(pcidev, BAR_0));
if (mmio_regs == NULL) {
printk(KERN_ERR PFX "Unable to remap BAR0\n");
ret = -EIO;

2009-12-07 20:09:32

by Chien Tin Tung

[permalink] [raw]
Subject: RE: [PATCH 3/5] drivers/infiniband: correct size computation

>From: Julia Lawall <[email protected]>
>
>The size argument to ioremap_nocache should be the size of desired
>information, not the pointer to it.
>
>The semantic match that finds this problem is as follows:
>(http://coccinelle.lip6.fr/)
>
>// <smpl>
>@expression@
>expression *x;
>@@
>
>x =
> <+...
>*sizeof(x)
>...+>// </smpl>
>
>Signed-off-by: Julia Lawall <[email protected]>
>
>---
> drivers/infiniband/hw/nes/nes.c | 3 ++-
> 1 files changed, 2 insertions(+), 1 deletions(-)
>
>diff --git a/drivers/infiniband/hw/nes/nes.c b/drivers/infiniband/hw/nes/nes.c
>index cbde0cf..95db98f 100644
>--- a/drivers/infiniband/hw/nes/nes.c
>+++ b/drivers/infiniband/hw/nes/nes.c
>@@ -521,7 +521,8 @@ static int __devinit nes_probe(struct pci_dev *pcidev, const struct pci_device_i
> spin_lock_init(&nesdev->indexed_regs_lock);
>
> /* Remap the PCI registers in adapter BAR0 to kernel VA space */
>- mmio_regs = ioremap_nocache(pci_resource_start(pcidev, BAR_0), sizeof(mmio_regs));
>+ mmio_regs = ioremap_nocache(pci_resource_start(pcidev, BAR_0),
>+ pci_resource_len(pcidev, BAR_0));
> if (mmio_regs == NULL) {
> printk(KERN_ERR PFX "Unable to remap BAR0\n");
> ret = -EIO;

Acked-by: Chien Tung <[email protected]>

2009-12-09 22:32:51

by Roland Dreier

[permalink] [raw]