Skip to contentSkip to conversation

Add PCI_DEVICE_TABLE macro

PATCH In review Jonas Bonn 4 participants 9 messages Feb 17, 2008 – Feb 22, 2008

Subsystems (2)
lore
Share & tools
lore

Jonas Bonn

author
#0 lore raw

PATCH 1/1 Add PCI_DEVICE_TABLE macro

The definitions of struct pci_device_id arrays should generally follow the same pattern across the entire kernel. This macro defines this array as static const and puts it into the __devinitconst section.

---
 include/linux/pci.h |    8 ++++++++
 1 files changed, 8 insertions(+), 0 deletions(-)
include/linux/pci.h +8 -0
index 87195b6..487d31c 100644
@@ -389,6 +389,14 @@ struct pci_driver {
 #define	to_pci_driver(drv) container_of(drv, struct pci_driver, driver)
 
 /**
+ * PCI_DEVICE_TABLE - macro used to describe a pci device table
+ * 
+ * This macro is used to create a struct pci_device_id array in a generic
+ * manner.
+ */
+#define PCI_DEVICE_TABLE(_table) static const struct pci_device_id _table[] __devinitconst
+
+/**
  * PCI_DEVICE - macro used to describe a specific pci device
  * @vend: the 16 bit PCI Vendor ID
  * @dev: the 16 bit PCI Device ID
Signature · 1 line
1.5.3.8
author
#1 lore raw

I'm a bit uncertain whether the definition really should include the 'static' modifier... for most definitions of these tables this is ok, but there are a couple of cases where it should not be static, so the line would need to be open-coded again...

/Jonas

Quoted 28 lines · Jonas Bonn

The definitions of struct pci_device_id arrays should generally follow the same pattern across the entire kernel. This macro defines this array as static const and puts it into the __devinitconst section.

---
 include/linux/pci.h |    8 ++++++++
 1 files changed, 8 insertions(+), 0 deletions(-)
include/linux/pci.h +8 -0
index 87195b6..487d31c 100644
@@ -389,6 +389,14 @@ struct pci_driver {
 #define	to_pci_driver(drv) container_of(drv, struct pci_driver, driver)
 
 /**
+ * PCI_DEVICE_TABLE - macro used to describe a pci device table
+ * 
+ * This macro is used to create a struct pci_device_id array in a generic
+ * manner.
+ */
+#define PCI_DEVICE_TABLE(_table) static const struct pci_device_id _table[] __devinitconst
+
+/**
  * PCI_DEVICE - macro used to describe a specific pci device
  * @vend: the 16 bit PCI Vendor ID
  * @dev: the 16 bit PCI Device ID
#2 lore raw
Quoted 4 lines · Jonas Bonn

I'm a bit uncertain whether the definition really should include the 'static' modifier... for most definitions of these tables this is ok, but there are a couple of cases where it should not be static, so the line would need to be open-coded again...

Yes, please don't make it static.

And again, what does this buy us?

thanks,

greg k-h

author
#3 lore raw
Quoted 3 lines

And again, what does this buy us?

Clarity and simplicity, I hope... there are a bunch of definitions scattered about the kernel that omit the __devinitdata modifier despite the documentation stating that it should always be there. The definition really should have been const, which wasn't possible before but has become so with the addition of the __devinitconst attribute.

Furthermore, there are definitions that use "const" and __devinitdata, which is explicitly wrong but the compiler doesn't catch section mismatches if there's only one such one case in the module (which is often the case).

Adding the __devinitconst modifier where there was nothing before buys us memory. Adding the const modifier gives the compiler a chance to do its thing. Changing __devinitdata to __devinitconst where it was wrong actually fixes some compiler errors in older (mid-release) kernels that were patched over by "removing" the section attribute altogether (which wastes memory).

Adding the macro (Olof's idea, not mine) makes it pretty difficult to get this definition wrong... I'll do the rest of the cleanup, but I need to know whether it's better to use a macro like this, or to open code the definitions. I prefer the macro approach...

Hope this makes some sense...

/Jonas

#4 lore raw
Quoted 19 lines · Jonas Bonn
Quoted 1 line

And again, what does this buy us?

Clarity and simplicity, I hope... there are a bunch of definitions scattered about the kernel that omit the __devinitdata modifier despite the documentation stating that it should always be there. The definition really should have been const, which wasn't possible before but has become so with the addition of the __devinitconst attribute.

Furthermore, there are definitions that use "const" and __devinitdata, which is explicitly wrong but the compiler doesn't catch section mismatches if there's only one such one case in the module (which is often the case).

Adding the __devinitconst modifier where there was nothing before buys us memory. Adding the const modifier gives the compiler a chance to do its thing. Changing __devinitdata to __devinitconst where it was wrong actually fixes some compiler errors in older (mid-release) kernels that were patched over by "removing" the section attribute altogether (which wastes memory).

Quoted 6 lines

Adding the macro (Olof's idea, not mine) makes it pretty difficult to get this definition wrong... I'll do the rest of the cleanup, but I need to know whether it's better to use a macro like this, or to open code the definitions. I prefer the macro approach...

Hope this makes some sense...

Ok, yes it does, thanks for the explaination.

Please, can you add this very good text to the changelog entry for the addition of the macro, and to the documentation somewhere? I'd be glad to take the patch if that was done.

thanks,

greg k-h

#5 lore raw
Quoted 10 lines · Greg KH
Quoted 9 lines · Jonas Bonn
Quoted 1 line

And again, what does this buy us?

Clarity and simplicity, I hope... there are a bunch of definitions scattered about the kernel that omit the __devinitdata modifier despite the documentation stating that it should always be there. The definition really should have been const, which wasn't possible before but has become so with the addition of the __devinitconst attribute.

Furthermore, there are definitions that use "const" and __devinitdata, which is explicitly wrong but the compiler doesn't catch section mismatches if there's only one such one case in the module (which is often the case).

Adding the __devinitconst modifier where there was nothing before buys us memory. Adding the const modifier gives the compiler a chance to do its thing. Changing __devinitdata to __devinitconst where it was wrong actually fixes some compiler errors in older (mid-release) kernels that were patched over by "removing" the section attribute altogether (which wastes memory).

Adding the macro (Olof's idea, not mine) makes it pretty difficult to get this definition wrong... I'll do the rest of the cleanup, but I need to know whether it's better to use a macro like this, or to open code the definitions. I prefer the macro approach...

Quoted 7 lines
Quoted 1 line

Hope this makes some sense...

Ok, yes it does, thanks for the explaination.

Please, can you add this very good text to the changelog entry for the addition of the macro, and to the documentation somewhere? I'd be glad to take the patch if that was done.

I would suggest having a DECLARE_ prefix in there, like other subsystems do...

Jeff

author
#6 lore raw

PATCH 1/1 Add DECLARE_PCI_DEVICE_TABLE macro

The definitions of struct pci_device_id arrays should generally follow the same pattern across the entire kernel. This macro defines this array as const and puts it into the __devinitconst section.

There are currently many definitions scattered about the kernel that omit the __devinitdata modifier despite the documentation stating that it should always be there. These definitions really also should have been const, which wasn't possible before but has become so with the addition of the __devinitconst attribute.

Furthermore, there are definitions that use "const" and __devinitdata, which is explicitly wrong but the compiler doesn't catch section mismatches if there's only one such one case in the module (which is often the case).

Adding the __devinitconst modifier where there was nothing before buys us memory. Adding the const modifier gives the compiler a chance to do its thing. Changing __devinitdata to __devinitconst where it was wrong actually fixes some compiler errors in older (mid-release) kernels that were patched over by "removing" the section attribute altogether (which wastes memory).

This macro makes it pretty difficult to get this definition wrong in the future...

---
 Documentation/pci.txt |    6 ++++--
 include/linux/pci.h   |    9 +++++++++
 2 files changed, 13 insertions(+), 2 deletions(-)
Documentation/pci.txt +4 -2
index 72b20c6..bb7bd27 100644
@@ -123,7 +123,8 @@ initialization with a pointer to a structure describing the driver
 
 
 The ID table is an array of struct pci_device_id entries ending with an
-all-zero entry.  Each entry consists of:
+all-zero entry; use of the macro DECLARE_PCI_DEVICE_TABLE is the preferred
+method of declaring the table.  Each entry consists of:
 
 	vendor,device	Vendor and device ID to match (or PCI_ANY_ID)
 
@@ -191,7 +192,8 @@ Tips on when/where to use the above attributes:
 
 	o Do not mark the struct pci_driver.
 
-	o The ID table array should be marked __devinitdata.
+	o The ID table array should be marked __devinitconst; this is done
+	  automatically if the table is declared with DECLARE_PCI_DEVICE_TABLE().
 
 	o The probe() and remove() functions should be marked __devinit
 	  and __devexit respectively.  All initialization functions
include/linux/pci.h +9 -0
index 87195b6..a41a484 100644
@@ -389,6 +389,15 @@ struct pci_driver {
 #define	to_pci_driver(drv) container_of(drv, struct pci_driver, driver)
 
 /**
+ * DECLARE_PCI_DEVICE_TABLE - macro used to describe a pci device table
+ * @_table: device table name
+ *
+ * This macro is used to create a struct pci_device_id array (a device table) 
+ * in a generic manner.
+ */
+#define DECLARE_PCI_DEVICE_TABLE(_table) const struct pci_device_id _table[] __devinitconst
+
+/**
  * PCI_DEVICE - macro used to describe a specific pci device
  * @vend: the 16 bit PCI Vendor ID
  * @dev: the 16 bit PCI Device ID
Signature · 1 line
1.5.3.8
#7 lore raw
In reply to Jonas Bonn
Quoted 19 lines · On Sun, 17 Feb 2008 13:10:05 +0100 Jonas Bonn wrote:

The definitions of struct pci_device_id arrays should generally follow the same pattern across the entire kernel. This macro defines this array as static const and puts it into the __devinitconst section.

---
 include/linux/pci.h |    8 ++++++++
 1 files changed, 8 insertions(+), 0 deletions(-)
include/linux/pci.h +1 -0
index 87195b6..487d31c 100644
@@ -389,6 +389,14 @@ struct pci_driver {
 #define	to_pci_driver(drv) container_of(drv, struct pci_driver, driver)
 
 /**
+ * PCI_DEVICE_TABLE - macro used to describe a pci device table

include/linux/pci.h struct pci_driver { · line 402

Quoted 10 lines
include/linux/pci.h +7 -0
+ * 
+ * This macro is used to create a struct pci_device_id array in a generic
+ * manner.
+ */
+#define PCI_DEVICE_TABLE(_table) static const struct pci_device_id _table[] __devinitconst
+
+/**
  * PCI_DEVICE - macro used to describe a specific pci device
  * @vend: the 16 bit PCI Vendor ID
  * @dev: the 16 bit PCI Device ID

Missing kernel-doc notation for the _table parameter. See the parameters for PCI_DEVICE() below for an example.

---
~Randy

author
#8 lore raw

PATCH 1/1 Add PCI_DEVICE_TABLE macro

The definitions of struct pci_device_id arrays should generally follow the same pattern across the entire kernel. This macro defines this array as const and puts it into the __devinitconst section.

---
 include/linux/pci.h |    9 +++++++++
 1 files changed, 9 insertions(+), 0 deletions(-)
include/linux/pci.h +9 -0
index 87195b6..c7a91b1 100644
@@ -389,6 +389,15 @@ struct pci_driver {
 #define	to_pci_driver(drv) container_of(drv, struct pci_driver, driver)
 
 /**
+ * PCI_DEVICE_TABLE - macro used to describe a pci device table
+ * @_table: device table name
+ *
+ * This macro is used to create a struct pci_device_id array (a device table) 
+ * in a generic manner.
+ */
+#define PCI_DEVICE_TABLE(_table) const struct pci_device_id _table[] __devinitconst
+
+/**
  * PCI_DEVICE - macro used to describe a specific pci device
  * @vend: the 16 bit PCI Vendor ID
  * @dev: the 16 bit PCI Device ID
Signature · 1 line
1.5.3.8