Skip to contentSkip to conversation

PCI: Optimize pci_resource_len() to reduce the binary size of kernel

PATCH Applied Zhen Lei 2 participants 2 messages Jul 13, 2021 – Aug 20, 2021 PCI

lore
Share & tools
lore

Zhen Lei

author
#0 lore raw

PATCH 1/1 PCI: Optimize pci_resource_len() to reduce the binary size of kernel

pci_resource_end() can be 0 only when pci_resource_start() is 0. Otherwise, it is definitely an error. In this case, pci_resource_len() should be regarded as 0. Therefore, determining whether pci_resource_start() and pci_resource_end() are both 0 can be reduced to determining only whether pci_resource_end() is 0.

Although only one condition judgment is reduced, the macro function pci_resource_len() is widely referenced in the kernel. I used defconfig to compile the latest kernel on X86, and its binary code size was reduced by about 3KB.

Before:
[ 2] .rela.text RELA 0000000000000000 093bfcb0

      0000000001a67168  0000000000000018   I      68     1     8

After:
[ 2] .rela.text RELA 0000000000000000 093bfcb0

      0000000001a66598  0000000000000018   I      68     1     8
---
 include/linux/pci.h | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
include/linux/pci.h +1 -3
index 540b377ca8f6..23ef1a15eb5d 100644
@@ -1881,9 +1881,7 @@ int pci_iobar_pfn(struct pci_dev *pdev, int bar, struct vm_area_struct *vma);
 #define pci_resource_end(dev, bar)	((dev)->resource[(bar)].end)
 #define pci_resource_flags(dev, bar)	((dev)->resource[(bar)].flags)
 #define pci_resource_len(dev,bar) \
-	((pci_resource_start((dev), (bar)) == 0 &&	\
-	  pci_resource_end((dev), (bar)) ==		\
-	  pci_resource_start((dev), (bar))) ? 0 :	\
+	((pci_resource_end((dev), (bar)) == 0) ? 0 :	\
 							\
 	 (pci_resource_end((dev), (bar)) -		\
 	  pci_resource_start((dev), (bar)) + 1))
Signature · 1 line
2.25.1
#1 lore raw
Quoted 13 lines · Zhen Lei

pci_resource_end() can be 0 only when pci_resource_start() is 0. Otherwise, it is definitely an error. In this case, pci_resource_len() should be regarded as 0. Therefore, determining whether pci_resource_start() and pci_resource_end() are both 0 can be reduced to determining only whether pci_resource_end() is 0.

Although only one condition judgment is reduced, the macro function pci_resource_len() is widely referenced in the kernel. I used defconfig to compile the latest kernel on X86, and its binary code size was reduced by about 3KB.

Before:
[ 2] .rela.text RELA 0000000000000000 093bfcb0

Quoted 7 lines
      0000000001a67168  0000000000000018   I      68     1     8

After:
[ 2] .rela.text RELA 0000000000000000 093bfcb0

      0000000001a66598  0000000000000018   I      68     1     8

Applied to pci/resource for v5.15, thanks!

Quoted 22 lines
---
 include/linux/pci.h | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
include/linux/pci.h +1 -3
index 540b377ca8f6..23ef1a15eb5d 100644
@@ -1881,9 +1881,7 @@ int pci_iobar_pfn(struct pci_dev *pdev, int bar, struct vm_area_struct *vma);
 #define pci_resource_end(dev, bar)	((dev)->resource[(bar)].end)
 #define pci_resource_flags(dev, bar)	((dev)->resource[(bar)].flags)
 #define pci_resource_len(dev,bar) \
-	((pci_resource_start((dev), (bar)) == 0 &&	\
-	  pci_resource_end((dev), (bar)) ==		\
-	  pci_resource_start((dev), (bar))) ? 0 :	\
+	((pci_resource_end((dev), (bar)) == 0) ? 0 :	\
 							\
 	 (pci_resource_end((dev), (bar)) -		\
 	  pci_resource_start((dev), (bar)) + 1))
Signature · 1 line
2.25.1