linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] PCI: Check value of resource alignment before using __ffs
@ 2021-04-22 10:55 Amey Narkhede
  2021-04-22 10:57 ` Leon Romanovsky
  2021-05-25 22:01 ` Bjorn Helgaas
  0 siblings, 2 replies; 6+ messages in thread
From: Amey Narkhede @ 2021-04-22 10:55 UTC (permalink / raw)
  To: Bjorn Helgaas; +Cc: linux-pci, linux-kernel, Leon Romanovsky, Amey Narkhede

Return value of __ffs is undefined if no set bit exists in
its argument. This indicates that the associated BAR has
invalid alignment.

Signed-off-by: Amey Narkhede <ameynarkhede03@gmail.com>
---
 drivers/pci/setup-bus.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/drivers/pci/setup-bus.c b/drivers/pci/setup-bus.c
index 2ce636937c6e..ce5380bdd2fd 100644
--- a/drivers/pci/setup-bus.c
+++ b/drivers/pci/setup-bus.c
@@ -1044,10 +1044,11 @@ static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
 			 * resources.
 			 */
 			align = pci_resource_alignment(dev, r);
-			order = __ffs(align) - 20;
-			if (order < 0)
-				order = 0;
-			if (order >= ARRAY_SIZE(aligns)) {
+			if (align) {
+				order = __ffs(align) - 20;
+				order = (order < 0) ? 0 : order;
+			}
+			if (!align || order >= ARRAY_SIZE(aligns)) {
 				pci_warn(dev, "disabling BAR %d: %pR (bad alignment %#llx)\n",
 					 i, r, (unsigned long long) align);
 				r->flags = 0;
--
2.31.1

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

* Re: [PATCH v2] PCI: Check value of resource alignment before using __ffs
  2021-04-22 10:55 [PATCH v2] PCI: Check value of resource alignment before using __ffs Amey Narkhede
@ 2021-04-22 10:57 ` Leon Romanovsky
  2021-05-24 20:32   ` Amey Narkhede
  2021-05-25 22:01 ` Bjorn Helgaas
  1 sibling, 1 reply; 6+ messages in thread
From: Leon Romanovsky @ 2021-04-22 10:57 UTC (permalink / raw)
  To: Amey Narkhede; +Cc: Bjorn Helgaas, linux-pci, linux-kernel

On Thu, Apr 22, 2021 at 04:25:38PM +0530, Amey Narkhede wrote:
> Return value of __ffs is undefined if no set bit exists in
> its argument. This indicates that the associated BAR has
> invalid alignment.
> 
> Signed-off-by: Amey Narkhede <ameynarkhede03@gmail.com>
> ---
>  drivers/pci/setup-bus.c | 9 +++++----
>  1 file changed, 5 insertions(+), 4 deletions(-)
> 

Thanks,
Reviewed-by: Leon Romanovsky <leonro@nvidia.com>

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

* Re: [PATCH v2] PCI: Check value of resource alignment before using __ffs
  2021-04-22 10:57 ` Leon Romanovsky
@ 2021-05-24 20:32   ` Amey Narkhede
  0 siblings, 0 replies; 6+ messages in thread
From: Amey Narkhede @ 2021-05-24 20:32 UTC (permalink / raw)
  To: Bjorn Helgaas; +Cc: linux-pci, linux-kernel, Leon Romanovsky

On 21/04/22 01:57PM, Leon Romanovsky wrote:
> On Thu, Apr 22, 2021 at 04:25:38PM +0530, Amey Narkhede wrote:
> > Return value of __ffs is undefined if no set bit exists in
> > its argument. This indicates that the associated BAR has
> > invalid alignment.
> >
> > Signed-off-by: Amey Narkhede <ameynarkhede03@gmail.com>
> > ---
> >  drivers/pci/setup-bus.c | 9 +++++----
> >  1 file changed, 5 insertions(+), 4 deletions(-)
> >
>
> Thanks,
> Reviewed-by: Leon Romanovsky <leonro@nvidia.com>
A gentle ping :)

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

* Re: [PATCH v2] PCI: Check value of resource alignment before using __ffs
  2021-04-22 10:55 [PATCH v2] PCI: Check value of resource alignment before using __ffs Amey Narkhede
  2021-04-22 10:57 ` Leon Romanovsky
@ 2021-05-25 22:01 ` Bjorn Helgaas
  2021-05-26  9:06   ` Amey Narkhede
  1 sibling, 1 reply; 6+ messages in thread
From: Bjorn Helgaas @ 2021-05-25 22:01 UTC (permalink / raw)
  To: Amey Narkhede; +Cc: Bjorn Helgaas, linux-pci, linux-kernel, Leon Romanovsky

On Thu, Apr 22, 2021 at 04:25:38PM +0530, Amey Narkhede wrote:
> Return value of __ffs is undefined if no set bit exists in
> its argument. This indicates that the associated BAR has
> invalid alignment.
> 
> Signed-off-by: Amey Narkhede <ameynarkhede03@gmail.com>
> ---
>  drivers/pci/setup-bus.c | 9 +++++----
>  1 file changed, 5 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/pci/setup-bus.c b/drivers/pci/setup-bus.c
> index 2ce636937c6e..ce5380bdd2fd 100644
> --- a/drivers/pci/setup-bus.c
> +++ b/drivers/pci/setup-bus.c
> @@ -1044,10 +1044,11 @@ static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
>  			 * resources.
>  			 */
>  			align = pci_resource_alignment(dev, r);
> -			order = __ffs(align) - 20;
> -			if (order < 0)
> -				order = 0;
> -			if (order >= ARRAY_SIZE(aligns)) {
> +			if (align) {
> +				order = __ffs(align) - 20;
> +				order = (order < 0) ? 0 : order;
> +			}
> +			if (!align || order >= ARRAY_SIZE(aligns)) {
>  				pci_warn(dev, "disabling BAR %d: %pR (bad alignment %#llx)\n",
>  					 i, r, (unsigned long long) align);
>  				r->flags = 0;

I know this is solving a theoretical problem.  Is it also solving a
*real* problem?

I dislike the way it complicates the code and the usage of "align" and
"order".  I know that when "!align", we don't evaluate the
"order >= ARRAY_SIZE()" (which would involve an uninitialized value),
but it just seems ugly, and I'm not sure how much we benefit.

And the "disabling BAR" part is gross.  I know you're not changing
that part, but it's just wrong.  Setting r->flags = 0 certainly does
not disable the BAR.  It might make Linux ignore it, but that doesn't
mean the hardware ignores it.  When we turn on PCI_COMMAND_MEMORY, the
BAR is enabled along with all the other memory BARs.

Bjorn

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

* Re: [PATCH v2] PCI: Check value of resource alignment before using __ffs
  2021-05-25 22:01 ` Bjorn Helgaas
@ 2021-05-26  9:06   ` Amey Narkhede
  2021-05-28  0:04     ` Bjorn Helgaas
  0 siblings, 1 reply; 6+ messages in thread
From: Amey Narkhede @ 2021-05-26  9:06 UTC (permalink / raw)
  To: Bjorn Helgaas; +Cc: linux-pci, linux-kernel, Leon Romanovsky

On 21/05/25 05:01PM, Bjorn Helgaas wrote:
> On Thu, Apr 22, 2021 at 04:25:38PM +0530, Amey Narkhede wrote:
> > Return value of __ffs is undefined if no set bit exists in
> > its argument. This indicates that the associated BAR has
> > invalid alignment.
> >
> > Signed-off-by: Amey Narkhede <ameynarkhede03@gmail.com>
> > ---
> >  drivers/pci/setup-bus.c | 9 +++++----
> >  1 file changed, 5 insertions(+), 4 deletions(-)
> >
> > diff --git a/drivers/pci/setup-bus.c b/drivers/pci/setup-bus.c
> > index 2ce636937c6e..ce5380bdd2fd 100644
> > --- a/drivers/pci/setup-bus.c
> > +++ b/drivers/pci/setup-bus.c
> > @@ -1044,10 +1044,11 @@ static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
> >  			 * resources.
> >  			 */
> >  			align = pci_resource_alignment(dev, r);
> > -			order = __ffs(align) - 20;
> > -			if (order < 0)
> > -				order = 0;
> > -			if (order >= ARRAY_SIZE(aligns)) {
> > +			if (align) {
> > +				order = __ffs(align) - 20;
> > +				order = (order < 0) ? 0 : order;
> > +			}
> > +			if (!align || order >= ARRAY_SIZE(aligns)) {
> >  				pci_warn(dev, "disabling BAR %d: %pR (bad alignment %#llx)\n",
> >  					 i, r, (unsigned long long) align);
> >  				r->flags = 0;
>
> I know this is solving a theoretical problem.  Is it also solving a
> *real* problem?
>
> I dislike the way it complicates the code and the usage of "align" and
> "order".  I know that when "!align", we don't evaluate the
> "order >= ARRAY_SIZE()" (which would involve an uninitialized value),
> but it just seems ugly, and I'm not sure how much we benefit.
>
> And the "disabling BAR" part is gross.  I know you're not changing
> that part, but it's just wrong.  Setting r->flags = 0 certainly does
> not disable the BAR.  It might make Linux ignore it, but that doesn't
> mean the hardware ignores it.  When we turn on PCI_COMMAND_MEMORY, the
> BAR is enabled along with all the other memory BARs.
>
> Bjorn

Thanks for the detailed explanation. Is there any way to properly
disable the BAR?
On the side note do you think this problem is
worth solving? I came across this during code inspection.
I mean if practically there aren't chances of
this bug occuring I'm okay with dropping this patch.

Thanks,
Amey

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

* Re: [PATCH v2] PCI: Check value of resource alignment before using __ffs
  2021-05-26  9:06   ` Amey Narkhede
@ 2021-05-28  0:04     ` Bjorn Helgaas
  0 siblings, 0 replies; 6+ messages in thread
From: Bjorn Helgaas @ 2021-05-28  0:04 UTC (permalink / raw)
  To: Amey Narkhede; +Cc: linux-pci, linux-kernel, Leon Romanovsky

On Wed, May 26, 2021 at 02:36:12PM +0530, Amey Narkhede wrote:
> On 21/05/25 05:01PM, Bjorn Helgaas wrote:
> > On Thu, Apr 22, 2021 at 04:25:38PM +0530, Amey Narkhede wrote:
> > > Return value of __ffs is undefined if no set bit exists in
> > > its argument. This indicates that the associated BAR has
> > > invalid alignment.
> > >
> > > Signed-off-by: Amey Narkhede <ameynarkhede03@gmail.com>
> > > ---
> > >  drivers/pci/setup-bus.c | 9 +++++----
> > >  1 file changed, 5 insertions(+), 4 deletions(-)
> > >
> > > diff --git a/drivers/pci/setup-bus.c b/drivers/pci/setup-bus.c
> > > index 2ce636937c6e..ce5380bdd2fd 100644
> > > --- a/drivers/pci/setup-bus.c
> > > +++ b/drivers/pci/setup-bus.c
> > > @@ -1044,10 +1044,11 @@ static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
> > >  			 * resources.
> > >  			 */
> > >  			align = pci_resource_alignment(dev, r);
> > > -			order = __ffs(align) - 20;
> > > -			if (order < 0)
> > > -				order = 0;
> > > -			if (order >= ARRAY_SIZE(aligns)) {
> > > +			if (align) {
> > > +				order = __ffs(align) - 20;
> > > +				order = (order < 0) ? 0 : order;
> > > +			}
> > > +			if (!align || order >= ARRAY_SIZE(aligns)) {
> > >  				pci_warn(dev, "disabling BAR %d: %pR (bad alignment %#llx)\n",
> > >  					 i, r, (unsigned long long) align);
> > >  				r->flags = 0;
> >
> > I know this is solving a theoretical problem.  Is it also solving a
> > *real* problem?
> >
> > I dislike the way it complicates the code and the usage of "align" and
> > "order".  I know that when "!align", we don't evaluate the
> > "order >= ARRAY_SIZE()" (which would involve an uninitialized value),
> > but it just seems ugly, and I'm not sure how much we benefit.
> >
> > And the "disabling BAR" part is gross.  I know you're not changing
> > that part, but it's just wrong.  Setting r->flags = 0 certainly does
> > not disable the BAR.  It might make Linux ignore it, but that doesn't
> > mean the hardware ignores it.  When we turn on PCI_COMMAND_MEMORY, the
> > BAR is enabled along with all the other memory BARs.
> >
> > Bjorn
> 
> Thanks for the detailed explanation. Is there any way to properly
> disable the BAR?

Unfortunately there is no way to disable an individual BAR.
PCI_COMMAND_MEMORY applied to *all* memory BARs, and the same for
PCI_COMMAND_IO.

> On the side note do you think this problem is
> worth solving? I came across this during code inspection.
> I mean if practically there aren't chances of
> this bug occuring I'm okay with dropping this patch.

I guess I would just drop it.  Yes, it's a potential problem, but I
couldn't figure out a solution that really seemed clean.

Bjorn

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

end of thread, other threads:[~2021-05-28  0:05 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-22 10:55 [PATCH v2] PCI: Check value of resource alignment before using __ffs Amey Narkhede
2021-04-22 10:57 ` Leon Romanovsky
2021-05-24 20:32   ` Amey Narkhede
2021-05-25 22:01 ` Bjorn Helgaas
2021-05-26  9:06   ` Amey Narkhede
2021-05-28  0:04     ` 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).