Skip to contentSkip to conversation

PCI: Fix BUILD_BUG_ON usage for old gcc

PATCH Superseded Alex Williamson 3 participants 4 messages Feb 10, 2025 – Feb 11, 2025 PCI

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 PCI: Fix BUILD_BUG_ON usage for old gcc

As reported in the below link, it seems older versions of gcc cannot determine that the howmany variable is known for all callers. Include a test so that newer compilers can still enforce this sanity check and older compilers can still work.

---

Verified against gcc 14.2.1 to still trigger a build error if called with a constant value greater than 6, Oleg to confirm build issue is resolved for gcc 5.3.1.

 drivers/pci/probe.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/pci/probe.c +2 -1
index b6536ed599c3..1bde89d0dc0d 100644
@@ -345,7 +345,8 @@ static void pci_read_bases(struct pci_dev *dev, unsigned int howmany, int rom)
 	unsigned int pos, reg;
 	u16 orig_cmd;
 
-	BUILD_BUG_ON(howmany > PCI_STD_NUM_BARS);
+	BUILD_BUG_ON(__builtin_constant_p(howmany) &&
+		     howmany > PCI_STD_NUM_BARS);
 
 	if (dev->non_compliant_bars)
 		return;
Signature · 1 line
2.47.1
Tested-by on v1 1/1
#1 lore raw
Quoted 10 lines · Alex Williamson

As reported in the below link, it seems older versions of gcc cannot determine that the howmany variable is known for all callers. Include a test so that newer compilers can still enforce this sanity check and older compilers can still work.

Fixes 4453f360862e ("PCI: Batch BAR sizing operations")

^^^^^^^^^^^^

Well, thanks, but I didn't ;)

drivers/pci/probe.c static void pci_read_bases(struct pci_dev *dev, unsigned int howmany, int rom) · line 349

Quoted 7 lines
drivers/pci/probe.c +2 -1
@@ -345,7 +345,8 @@ static void pci_read_bases(struct pci_dev *dev, unsigned int howmany, int rom)
 	unsigned int pos, reg;
 	u16 orig_cmd;
 
-	BUILD_BUG_ON(howmany > PCI_STD_NUM_BARS);
+	BUILD_BUG_ON(__builtin_constant_p(howmany) &&
+		     howmany > PCI_STD_NUM_BARS);

Thanks!

#2 lore raw
Quoted 7 lines · Oleg Nesterov
Quoted 5 lines · Alex Williamson
drivers/pci/probe.c +0 -0
@@ -345,7 +345,8 @@ static void pci_read_bases(struct pci_dev *dev, unsigned int howmany, int rom)
 	unsigned int pos, reg;
 	u16 orig_cmd;
 
Quoted 7 lines

drivers/pci/probe.c static void pci_read_bases(struct pci_dev *dev, unsigned int howmany, int rom) · line 349

Quoted 3 lines
drivers/pci/probe.c +2 -1
-	BUILD_BUG_ON(howmany > PCI_STD_NUM_BARS);
+	BUILD_BUG_ON(__builtin_constant_p(howmany) &&
+		     howmany > PCI_STD_NUM_BARS);

Thanks!

Just in case... I agree with David, statically_true() looks a bit better and

	BUILD_BUG_ON(statically_true(howmany > PCI_STD_NUM_BARS));

also works for me, so if you decide to update this patch feel free to keep my Tested-by.

Oleg.

#3 lore raw

On Ubuntu's (modern) GCC 13.3.0-6ubuntu2~24.04, both proposed options also build as expected for me when howmany is set correctly, and fail as expected when I force howmany = 7.

Quoted 27 lines · Oleg Nesterov
Quoted 7 lines · Oleg Nesterov
Quoted 5 lines · Alex Williamson
drivers/pci/probe.c +0 -0
@@ -345,7 +345,8 @@ static void pci_read_bases(struct pci_dev *dev, unsigned int howmany, int rom)
    unsigned int pos, reg;
    u16 orig_cmd;
 
Quoted 7 lines

drivers/pci/probe.c static void pci_read_bases(struct pci_dev *dev, unsigned int howmany, int rom) · line 349

Quoted 3 lines
drivers/pci/probe.c +2 -1
-   BUILD_BUG_ON(howmany > PCI_STD_NUM_BARS);
+   BUILD_BUG_ON(__builtin_constant_p(howmany) &&
+                howmany > PCI_STD_NUM_BARS);

Thanks!

Just in case... I agree with David, statically_true() looks a bit better and

        BUILD_BUG_ON(statically_true(howmany > PCI_STD_NUM_BARS));

also works for me, so if you decide to update this patch feel free to keep my Tested-by.

Oleg.

Signature · 2 lines
Mitchell Augustin
Software Engineer - Ubuntu Partner Engineering