Skip to contentSkip to conversation

PCI: Check value of resource alignment before using __ffs

PATCH In review Amey Narkhede 3 participants 6 messages Apr 22, 2021 – May 28, 2021 PCI

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 PCI: Check value of resource alignment before using __ffs

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

---
 drivers/pci/setup-bus.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/pci/setup-bus.c +5 -4
index 2ce636937c6e..ce5380bdd2fd 100644
@@ -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

#2 lore raw
Quoted 5 lines · Leon Romanovsky
Quoted 4 lines · 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.

Quoted 8 lines
Quoted 5 lines
---
 drivers/pci/setup-bus.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

Thanks,

A gentle ping :)

#3 lore raw
In reply to Amey Narkhede
Quoted 19 lines · 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.

---
 drivers/pci/setup-bus.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/pci/setup-bus.c +0 -2
index 2ce636937c6e..ce5380bdd2fd 100644
@@ -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)

drivers/pci/setup-bus.c static int pbus_size_mem(struct pci_bus *bus, unsigned long mask, · line 1054

Quoted 10 lines
drivers/pci/setup-bus.c +5 -2
-				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

#4 lore raw
Quoted 39 lines · Bjorn Helgaas
Quoted 19 lines · 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.

---
 drivers/pci/setup-bus.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/pci/setup-bus.c +0 -2
index 2ce636937c6e..ce5380bdd2fd 100644
@@ -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)

drivers/pci/setup-bus.c static int pbus_size_mem(struct pci_bus *bus, unsigned long mask, · line 1054

Quoted 10 lines
drivers/pci/setup-bus.c +5 -2
-				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.

Quoted 7 lines

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

#5 lore raw
Quoted 46 lines · Amey Narkhede
Quoted 45 lines · Bjorn Helgaas
Quoted 19 lines · 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.

---
 drivers/pci/setup-bus.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/pci/setup-bus.c +0 -2
index 2ce636937c6e..ce5380bdd2fd 100644
@@ -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)

drivers/pci/setup-bus.c static int pbus_size_mem(struct pci_bus *bus, unsigned long mask, · line 1054

Quoted 10 lines
drivers/pci/setup-bus.c +5 -2
-				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.

Quoted 4 lines
Quoted 1 line

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.

Quoted 4 lines

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