Skip to contentSkip to conversation

drivers/pci: Fix FIELD_PREP compilation error with gcc-8

PATCH In review Anders Roxell 3 participants 4 messages Aug 28, 2025 – Sep 6, 2025 PCI

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drivers/pci: Fix FIELD_PREP compilation error with gcc-8

Commit cbc654d18d37 ("bitops: Add __attribute_const__ to generic ffs()-family implementations") causes a compilation failure on ARM footbridge_defconfig with gcc-8:

FIELD_PREP: value too large for the field

The error occurs in pcie_set_readrq() at:
v = FIELD_PREP(PCI_EXP_DEVCTL_READRQ, ffs(rq) - 8);

With __attribute_const__, gcc-8 now performs wrong compile-time validation in FIELD_PREP and cannot guarantee that ffs(rq) - 8 will always produce values that fit in the 3-bit PCI_EXP_DEVCTL_READRQ field.

Avoid FIELD_PREP entirely by using direct bit manipulation. Replace FIELD_PREP(PCI_EXP_DEVCTL_READRQ, ffs(rq) - 8) with the equivalent manual bit operations: ((ffs(rq) - 8) << 12) & PCI_EXP_DEVCTL_READRQ.

This bypasses the compile-time validation while maintaining identical runtime behavior and functionality.

---
 drivers/pci/pci.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/pci/pci.c +2 -1
index e698278229f2..9f9607bd9f51 100644
@@ -5893,7 +5893,8 @@ int pcie_set_readrq(struct pci_dev *dev, int rq)
 			rq = mps;
 	}
 
-	v = FIELD_PREP(PCI_EXP_DEVCTL_READRQ, ffs(rq) - 8);
+	/* Ideally we would used FIELD_PREP() but this is a work around for gcc-8 */
+	v = ((ffs(rq) - 8) << 12) & PCI_EXP_DEVCTL_READRQ;
 
 	if (bridge->no_inc_mrrs) {
 		int max_mrrs = pcie_get_readrq(dev);
Signature · 1 line
2.50.1
#1 lore raw
Quoted 4 lines · Anders Roxell

Commit cbc654d18d37 ("bitops: Add __attribute_const__ to generic ffs()-family implementations") causes a compilation failure on ARM footbridge_defconfig with gcc-8:

Quoted 8 lines

FIELD_PREP: value too large for the field

The error occurs in pcie_set_readrq() at:
v = FIELD_PREP(PCI_EXP_DEVCTL_READRQ, ffs(rq) - 8);

With __attribute_const__, gcc-8 now performs wrong compile-time validation in FIELD_PREP and cannot guarantee that ffs(rq) - 8 will always produce values that fit in the 3-bit PCI_EXP_DEVCTL_READRQ field.

Thanks for examining this! It seems rather alarming -- why did it work before?

Quoted 6 lines

Avoid FIELD_PREP entirely by using direct bit manipulation. Replace FIELD_PREP(PCI_EXP_DEVCTL_READRQ, ffs(rq) - 8) with the equivalent manual bit operations: ((ffs(rq) - 8) << 12) & PCI_EXP_DEVCTL_READRQ.

This bypasses the compile-time validation while maintaining identical runtime behavior and functionality.

Did you dig into why this happened? It seems like a fragile situation, so I'm worried we'll see more of these pop up.

Quoted 13 lines
---
 drivers/pci/pci.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/pci/pci.c +0 -0
index e698278229f2..9f9607bd9f51 100644

drivers/pci/pci.c int pcie_set_readrq(struct pci_dev *dev, int rq) · line 5900

Quoted 9 lines
drivers/pci/pci.c +2 -1
@@ -5893,7 +5893,8 @@ int pcie_set_readrq(struct pci_dev *dev, int rq)
 			rq = mps;
 	}
 
-	v = FIELD_PREP(PCI_EXP_DEVCTL_READRQ, ffs(rq) - 8);
+	/* Ideally we would used FIELD_PREP() but this is a work around for gcc-8 */
+	v = ((ffs(rq) - 8) << 12) & PCI_EXP_DEVCTL_READRQ;
 
 	if (bridge->no_inc_mrrs) {
 		int max_mrrs = pcie_get_readrq(dev);

If you're sure this is okay, I'll take it with the series, but I feel like we should justify it better. :)

-Kees

Signature · 1 line
Kees Cook
#3 lore raw
In reply to Anders Roxell
Quoted 4 lines · On Thu, 28 Aug 2025 12:12:37 +0200 Anders Roxell <[email protected]>…

Commit cbc654d18d37 ("bitops: Add __attribute_const__ to generic ffs()-family implementations") causes a compilation failure on ARM footbridge_defconfig with gcc-8:

Quoted 8 lines

FIELD_PREP: value too large for the field

The error occurs in pcie_set_readrq() at:
v = FIELD_PREP(PCI_EXP_DEVCTL_READRQ, ffs(rq) - 8);

With __attribute_const__, gcc-8 now performs wrong compile-time validation in FIELD_PREP and cannot guarantee that ffs(rq) - 8 will always produce values that fit in the 3-bit PCI_EXP_DEVCTL_READRQ field.

Which is actually quite correct - in principle pcie_get_mps() could return a small value.

What is probably happening is that two copies of the FIELD_PREP() are being generated for ffs(rq) and ffs(mps).
The latter might be (mps ? asm_fun(mps) + 1 : 0) leading to an extra copy for ffs(0) - which will cause the warning in FIELD_PREP.

An alternate fix you be to move the validation of rq below the 'performance' clamp.

David

Quoted 30 lines

Avoid FIELD_PREP entirely by using direct bit manipulation. Replace FIELD_PREP(PCI_EXP_DEVCTL_READRQ, ffs(rq) - 8) with the equivalent manual bit operations: ((ffs(rq) - 8) << 12) & PCI_EXP_DEVCTL_READRQ.

This bypasses the compile-time validation while maintaining identical runtime behavior and functionality.

---
 drivers/pci/pci.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/pci/pci.c +2 -1
index e698278229f2..9f9607bd9f51 100644
@@ -5893,7 +5893,8 @@ int pcie_set_readrq(struct pci_dev *dev, int rq)
 			rq = mps;
 	}
 
-	v = FIELD_PREP(PCI_EXP_DEVCTL_READRQ, ffs(rq) - 8);
+	/* Ideally we would used FIELD_PREP() but this is a work around for gcc-8 */
+	v = ((ffs(rq) - 8) << 12) & PCI_EXP_DEVCTL_READRQ;
 
 	if (bridge->no_inc_mrrs) {
 		int max_mrrs = pcie_get_readrq(dev);