Skip to contentSkip to conversation

PCI: Check value of resource alignment before using __ffs

PATCH Superseded Amey Narkhede 2 participants 5 messages Apr 21, 2021 – Apr 22, 2021 PCI

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 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 | 5 +++++
 1 file changed, 5 insertions(+)
drivers/pci/setup-bus.c +5 -0
index 2ce636937c6e..44e8449418ae 100644
@@ -1044,6 +1044,11 @@ static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
 			 * resources.
 			 */
 			align = pci_resource_alignment(dev, r);
+			if (!align) {
+				pci_warn(dev, "BAR %d: %pR has bogus alignment\n",
+					 i, r);
+				continue;
+			}
 			order = __ffs(align) - 20;
 			if (order < 0)
 				order = 0;

--
2.31.1

#1 lore raw
Quoted 14 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 | 5 +++++
 1 file changed, 5 insertions(+)
drivers/pci/setup-bus.c +0 -0
index 2ce636937c6e..44e8449418ae 100644

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

Quoted 8 lines
drivers/pci/setup-bus.c +5 -0
@@ -1044,6 +1044,11 @@ static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
 			 * resources.
 			 */
 			align = pci_resource_alignment(dev, r);
+			if (!align) {
+				pci_warn(dev, "BAR %d: %pR has bogus alignment\n",
+					 i, r);
+				continue;
+			}

I see that you copied it from pdev_sort_resources(), but it is incorrect change, see how negative order is handled and later ARRAY_SIZE() check.

Thanks

Quoted 5 lines
drivers/pci/setup-bus.c +0 -1
 			order = __ffs(align) - 20;
 			if (order < 0)
 				order = 0;
--

2.31.1

#2 lore raw
Quoted 28 lines · Leon Romanovsky
Quoted 14 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 | 5 +++++
 1 file changed, 5 insertions(+)
drivers/pci/setup-bus.c +0 -0
index 2ce636937c6e..44e8449418ae 100644

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

Quoted 8 lines
drivers/pci/setup-bus.c +5 -0
@@ -1044,6 +1044,11 @@ static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
 			 * resources.
 			 */
 			align = pci_resource_alignment(dev, r);
+			if (!align) {
+				pci_warn(dev, "BAR %d: %pR has bogus alignment\n",
+					 i, r);
+				continue;
+			}

I see that you copied it from pdev_sort_resources(), but it is incorrect change, see how negative order is handled and later ARRAY_SIZE() check.

Quoted 2 lines

Thanks

Is it guaranteed that it will return value which will result in negative value or >= ARRAY_SIZE? Comment on __ffs says value is undefined for 0 that means it could be anything or am I missing something?

Thanks,
Amey

#3 lore raw
Quoted 29 lines · Amey Narkhede
Quoted 28 lines · Leon Romanovsky
Quoted 14 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 | 5 +++++
 1 file changed, 5 insertions(+)
drivers/pci/setup-bus.c +0 -0
index 2ce636937c6e..44e8449418ae 100644

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

Quoted 8 lines
drivers/pci/setup-bus.c +5 -0
@@ -1044,6 +1044,11 @@ static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
 			 * resources.
 			 */
 			align = pci_resource_alignment(dev, r);
+			if (!align) {
+				pci_warn(dev, "BAR %d: %pR has bogus alignment\n",
+					 i, r);
+				continue;
+			}

I see that you copied it from pdev_sort_resources(), but it is incorrect change, see how negative order is handled and later ARRAY_SIZE() check.

Quoted 6 lines
Quoted 2 lines

Thanks

Is it guaranteed that it will return value which will result in negative value or >= ARRAY_SIZE? Comment on __ffs says value is undefined for 0 that means it could be anything or am I missing something?

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;
Quoted 3 lines

Thanks,
Amey

#4 lore raw
Quoted 49 lines · Leon Romanovsky
Quoted 35 lines · Amey Narkhede
Quoted 28 lines · Leon Romanovsky
Quoted 14 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 | 5 +++++
 1 file changed, 5 insertions(+)
drivers/pci/setup-bus.c +0 -0
index 2ce636937c6e..44e8449418ae 100644

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

Quoted 8 lines
drivers/pci/setup-bus.c +5 -0
@@ -1044,6 +1044,11 @@ static int pbus_size_mem(struct pci_bus *bus, unsigned long mask,
 			 * resources.
 			 */
 			align = pci_resource_alignment(dev, r);
+			if (!align) {
+				pci_warn(dev, "BAR %d: %pR has bogus alignment\n",
+					 i, r);
+				continue;
+			}

I see that you copied it from pdev_sort_resources(), but it is incorrect change, see how negative order is handled and later ARRAY_SIZE() check.

Quoted 2 lines

Thanks

Is it guaranteed that it will return value which will result in negative value or >= ARRAY_SIZE? Comment on __ffs says value is undefined for 0 that means it could be anything or am I missing something?

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;

Oh I see. Thanks. I'll correct this in v2.

Thanks,
Amey