PCI: create function symlinks in /sys/bus/pci/slots/N/
PATCH v2 1/1 PCI: create function symlinks in /sys/bus/pci/slots/N/
This is a second attempt at creating some handy symlinks in /sys/bus/pci/ between slots and devices.
It addresses the following concerns from last time:
- does not create superfluous 'device' link
- correctly adds and removes links after hotplug ops
- adds a bunch of documentation ;)
It does not address Willy's concerns about not needing the functionN back-links. I kinda thought they were useful, no one else seemed to express an opinion...
This patch is against linux-next, but do note that you'll need gregkh's "pci_get_dev_by_id refleak" patch:
http://lkml.org/lkml/2008/8/21/492
for this patch to work properly.
Thanks!
/ac
From: Alex Chiang <[email protected]>
Create convenience symlinks in sysfs, linking slots to device functions, and vice versa. These links make it easier for users to figure out which devices actually live in what slots.
For example:
sapphire:/sys/bus/pci/slots # ls
1 10 2 3 4 5 6 7 8 9
sapphire:/sys/bus/pci/slots # ls -l 3
total 0
-r--r--r-- 1 root root 65536 Aug 18 14:10 address
lrwxrwxrwx 1 root root 0 Aug 18 14:10 function0 ->
../../../../devices/pci0000:23/0000:23:01.0
lrwxrwxrwx 1 root root 0 Aug 18 14:10 function1 ->
../../../../devices/pci0000:23/0000:23:01.1
sapphire:/sys/bus/pci/slots # ls -l 3/function0/slot lrwxrwxrwx 1 root root 0 Aug 18 14:13 3/function0/slot -> ../../../bus/pci/slots/3
The original form of this patch was written by Matthew Wilcox, but did not have links from the sysfs slots/ directory pointing back at the device functions.
--- Documentation/ABI/testing/sysfs-bus-pci | 39 +++++++++++++++++++++++++ drivers/pci/pci-sysfs.c | 37 +++++++++++++++++++++++ drivers/pci/slot.c | 48 +++++++++++++++++++++++++++++++ 3 files changed, 124 insertions(+), 0 deletions(-)
Documentation/ABI/testing/sysfs-bus-pci +39 -0
@@ -9,3 +9,42 @@ Description: that some devices may have malformatted data. If the underlying VPD has a writable section then the corresponding section of this file will be writable. + +What: /sys/bus/pci/slots/... +Date: April 2005 (possibly older) +KernelVersion: 2.6.12 (possibly older) +Contact: [email protected] +Description: + When the appropriate driver is loaded, it will create a + directory per claimed physical PCI slot in + /sys/bus/pci/slots/. The names of these directories are + specific to the driver, which in turn, are specific to the + platform, but in general, should match the label on the + machine's physical chassis. + + The drivers that can create slot directories include the + PCI hotplug drivers, and as of 2.6.27, the pci_slot driver. + + The slot directories contain, at a minimum, a file named + 'address' which contains the PCI bus:device:function tuple. + Other files may appear as well, but are specific to the + driver. + +What: /sys/bus/pci/slots/.../function[0-7] +Date: August 2008 +KernelVersion: 2.6.28 +Contact: [email protected] +Description: + If PCI slot directories (as described above) are created, + and the physical slot is actually populated with a device, + symbolic links in the slot directory pointing to the + device's PCI functions are created as well. + +What: /sys/bus/pci/devices/.../slot +Date: August 2008 +KernelVersion: 2.6.28 +Contact: [email protected] +Description: + If PCI slot directories (as described above) are created, + a symbolic link pointing to the slot directory will be + created as well.
drivers/pci/pci-sysfs.c +37 -0
@@ -716,6 +716,39 @@ int __attribute__ ((weak)) pcibios_add_platform_entries(struct pci_dev *dev)return 0 ; } +static void pci_remove_slot_links (struct pci_dev *dev) +{ +char func[10 ]; +struct pci_slot *slot; + +sysfs_remove_link (&dev->dev.kobj,"slot" ); +list_for_each_entry (slot, &dev->bus->slots, list) { +if (slot->number !=PCI_SLOT (dev->devfn)) +continue ; +snprintf (func,10 ,"function%d" ,PCI_FUNC (dev->devfn)); +sysfs_remove_link (&slot->kobj, func); + } +} + +static int pci_create_slot_links (struct pci_dev *dev) +{ +int result =0 ; +char func[10 ]; +struct pci_slot *slot; + +list_for_each_entry (slot, &dev->bus->slots, list) { +if (slot->number !=PCI_SLOT (dev->devfn)) +continue ; + result =sysfs_create_link (&dev->dev.kobj, &slot->kobj,"slot" ); +if (result) + goto out; +snprintf (func,10 ,"function%d" ,PCI_FUNC (dev->devfn)); + result =sysfs_create_link (&slot->kobj, &dev->dev.kobj, func); + } +out: +return result; +} +int __must_check pci_create_sysfs_dev_files (struct pci_dev *pdev) {struct bin_attribute *attr =NULL ;
@@ -779,6 +812,8 @@ int __must_check pci_create_sysfs_dev_files (struct pci_dev *pdev)pcie_aspm_create_sysfs_dev_files (pdev); +pci_create_slot_links (pdev); +return 0 ; err_rom_file:
@@ -814,6 +849,8 @@ void pci_remove_sysfs_dev_files(struct pci_dev *pdev)if (!sysfs_initialized)return ; +pci_remove_slot_links (pdev); +pcie_aspm_remove_sysfs_dev_files (pdev);if (pdev->vpd) {
drivers/pci/slot.c +48 -0
66 lines not on this page · raw message
Signature · 1 line
1.5.3.1.gbed62
Quoted 5 lines · Alex Chiang
This is a second attempt at creating some handy symlinks in
/sys/bus/pci/between slots and devices.It addresses the following concerns from last time:
Quoted 7 lines
- does not create superfluous 'device' link
- correctly adds and removes links after hotplug ops
- adds a bunch of documentation ;)It does not address Willy's concerns about not needing the functionN back-links. I kinda thought they were useful, no one else seemed to express an opinion...
I was just explaining why I didn't create them when I did my version of this patch. I don't have an objection to adding them; they make logical sense. The only concern might be the additional memory usage.
Quoted 7 lines
+++
b/drivers/pci/pci-sysfs.cdrivers/pci/pci-sysfs.c +2
-0@@ -716,6 +716,39 @@ int __attribute__ ((weak)) pcibios_add_platform_entries(struct pci_dev *dev)return 0 ; } +static void pci_remove_slot_links (struct pci_dev *dev) +{
drivers/pci/pci-sysfs.c int __attribute__ ((weak)) pcibios_add_platform_entries(struct pci_dev *dev) · line 730
Quoted 10 lines
drivers/pci/pci-sysfs.c +10
-0+char func[10 ]; +struct pci_slot *slot; + +sysfs_remove_link (&dev->dev.kobj,"slot" ); +list_for_each_entry (slot, &dev->bus->slots, list) { +if (slot->number !=PCI_SLOT (dev->devfn)) +continue ; +snprintf (func,10 ,"function%d" ,PCI_FUNC (dev->devfn)); +sysfs_remove_link (&slot->kobj, func); + }
Rather than this loop, why not not use dev->slot?
drivers/pci/pci-sysfs.c int __attribute__ ((weak)) pcibios_add_platform_entries(struct pci_dev *dev) · line 734
Quoted 2 lines
drivers/pci/pci-sysfs.c +2
-0+static int pci_create_slot_links (struct pci_dev *dev) +{
Likewise in this function.
Quoted 6 lines
drivers/pci/slot.c +6
-0+++ b/drivers/pci/slot.c +static void remove_sysfs_files (struct pci_slot *slot) +{ +char func[10 ]; +struct list_head *tmp; +
drivers/pci/slot.c static ssize_t address_read_file(struct pci_slot *slot, char *buf) · line 64
Quoted 10 lines
drivers/pci/slot.c +10
-0+list_for_each (tmp, &slot->bus->devices) { +struct pci_dev *dev =pci_dev_b (tmp); +if (PCI_SLOT (dev->devfn) != slot->number) +continue ; +sysfs_remove_link (&dev->dev.kobj,"slot" ); + +snprintf (func,10 ,"function%d" ,PCI_FUNC (dev->devfn)); +sysfs_remove_link (&slot->kobj, func); + } +}
It feels a bit strange to be doing this in two different files. I understand why -- you've got a slot to remove or you've got a device to remove, and in either case you have to get rid of the links.
Did you try putting all the logic in one of the two files and calling it from the other?
Signature · 4 lines
Matthew Wilcox Intel Open Source Technology Centre "Bill, look, we understand that you're interested in selling us this operating system, but compare it to ours. We can't possibly take such a retrograde step."
* Matthew Wilcox <[email protected]>:
Quoted 10 lines · Alex Chiang
Quoted 9 lines
This is a second attempt at creating some handy symlinks in
/sys/bus/pci/between slots and devices.It addresses the following concerns from last time:
- does not create superfluous 'device' link
- correctly adds and removes links after hotplug ops
- adds a bunch of documentation ;)
Quoted 7 lines
Quoted 3 lines
It does not address Willy's concerns about not needing the functionN back-links. I kinda thought they were useful, no one else seemed to express an opinion...
I was just explaining why I didn't create them when I did my version of this patch. I don't have an objection to adding them; they make logical sense. The only concern might be the additional memory usage.
Yes, agree about the memory usage, although I wonder what the actual overhead is.
Does anyone have numbers for how much it costs to create a new symlink? I could try and figure this out but it will take a few days (busy with other stuff).
Quoted 20 lines
Quoted 7 lines
+++
b/drivers/pci/pci-sysfs.cdrivers/pci/pci-sysfs.c +2
-0@@ -716,6 +716,39 @@ int __attribute__ ((weak)) pcibios_add_platform_entries(struct pci_dev *dev)return 0 ; } +static void pci_remove_slot_links (struct pci_dev *dev) +{drivers/pci/pci-sysfs.c int __attribute__ ((weak)) pcibios_add_platform_entries(struct pci_dev *dev) · line 730
Quoted 10 lines
drivers/pci/pci-sysfs.c +10
-0+char func[10 ]; +struct pci_slot *slot; + +sysfs_remove_link (&dev->dev.kobj,"slot" ); +list_for_each_entry (slot, &dev->bus->slots, list) { +if (slot->number !=PCI_SLOT (dev->devfn)) +continue ; +snprintf (func,10 ,"function%d" ,PCI_FUNC (dev->devfn)); +sysfs_remove_link (&slot->kobj, func); + }Rather than this loop, why not not use
dev->slot?
Quoted 4 lines
drivers/pci/pci-sysfs.c int __attribute__ ((weak)) pcibios_add_platform_entries(struct pci_dev *dev) · line 734
Quoted 2 lines
drivers/pci/pci-sysfs.c +2
-0+static int pci_create_slot_links (struct pci_dev *dev) +{Likewise in this function.
Good points, both. I'll try that for v3.
Quoted 21 lines
Quoted 6 lines
drivers/pci/slot.c +6
-0+++ b/drivers/pci/slot.c +static void remove_sysfs_files (struct pci_slot *slot) +{ +char func[10 ]; +struct list_head *tmp; +drivers/pci/slot.c static ssize_t address_read_file(struct pci_slot *slot, char *buf) · line 64
Quoted 10 lines
drivers/pci/slot.c +10
-0+list_for_each (tmp, &slot->bus->devices) { +struct pci_dev *dev =pci_dev_b (tmp); +if (PCI_SLOT (dev->devfn) != slot->number) +continue ; +sysfs_remove_link (&dev->dev.kobj,"slot" ); + +snprintf (func,10 ,"function%d" ,PCI_FUNC (dev->devfn)); +sysfs_remove_link (&slot->kobj, func); + } +}It feels a bit strange to be doing this in two different files. I understand why -- you've got a slot to remove or you've got a device to remove, and in either case you have to get rid of the links.
Quoted 2 lines
Did you try putting all the logic in one of the two files and calling it from the other?
I did actually try that at first, albeit as a single function to create links for either type of caller and another function to remove links for either type of caller.
It was possible, but the looping I had to do was ugly. I could try again, and possible be smarter about detecting what was passed in... or making the interface more complicated... or at least just patching the same file and keeping the two different flavours of add/remove links.
Thanks.
/ac
Quoted 19 lines · Alex Chiang
* Matthew Wilcox <[email protected]>:
Quoted 10 lines · Alex Chiang
Quoted 9 lines
This is a second attempt at creating some handy symlinks in
/sys/bus/pci/between slots and devices.It addresses the following concerns from last time:
- does not create superfluous 'device' link
- correctly adds and removes links after hotplug ops
- adds a bunch of documentation ;)Quoted 7 lines
Quoted 3 lines
It does not address Willy's concerns about not needing the functionN back-links. I kinda thought they were useful, no one else seemed to express an opinion...
I was just explaining why I didn't create them when I did my version of this patch. I don't have an objection to adding them; they make logical sense. The only concern might be the additional memory usage.
Quoted 6 lines
Yes, agree about the memory usage, although I wonder what the actual overhead is.
Does anyone have numbers for how much it costs to create a new symlink? I could try and figure this out but it will take a few days (busy with other stuff).
Almost nothing.
sysfs creates these things on the fly as they are accessed, and if memory pressure on the machine happens, they are freed up properly and then created again if a user asks to see them in the tree.
So don't worry about memory issues when adding new files or symlinks in sysfs, it just isn't a problem (we handle 20000 disks easily on low memory 31bit s390 systems.)
thanks,
greg k-h
* Greg KH <[email protected]>:
Quoted 8 lines · Alex Chiang
Quoted 4 lines
Does anyone have numbers for how much it costs to create a new symlink? I could try and figure this out but it will take a few days (busy with other stuff).
Almost nothing.
Quoted 7 lines
sysfs creates these things on the fly as they are accessed, and if memory pressure on the machine happens, they are freed up properly and then created again if a user asks to see them in the tree.
So don't worry about memory issues when adding new files or symlinks in sysfs, it just isn't a problem (we handle 20000 disks easily on low memory 31bit s390 systems.)
Great, thanks for the explanation. I've heard the "memory overhead" argument before for not wanting to create other sysfs files/links, so this will be good to debunk that bogeyman if it pops up again in the future.
Did you get a chance to take a look at the documentation I wrote for these new symlinks? [I also went and documented the existing slots/ directory as well...]
Was it what you had in mind?
Thanks.
/ac
Quoted 13 lines · Alex Chiang
* Greg KH <[email protected]>:
Quoted 12 lines · Alex Chiang
Quoted 4 lines
Does anyone have numbers for how much it costs to create a new symlink? I could try and figure this out but it will take a few days (busy with other stuff).
Almost nothing.
sysfs creates these things on the fly as they are accessed, and if memory pressure on the machine happens, they are freed up properly and then created again if a user asks to see them in the tree.
Quoted 8 lines
Quoted 3 lines
So don't worry about memory issues when adding new files or symlinks in sysfs, it just isn't a problem (we handle 20000 disks easily on low memory 31bit s390 systems.)
Great, thanks for the explanation. I've heard the "memory overhead" argument before for not wanting to create other sysfs files/links, so this will be good to debunk that bogeyman if it pops up again in the future.
Yes, please do.
Quoted 5 lines
Did you get a chance to take a look at the documentation I wrote for these new symlinks? [I also went and documented the existing slots/ directory as well...]
Was it what you had in mind?
Yes, it looked very good.
And thanks for the other documentation as well, if you want, you could split that out as a different patch and odds are Jesse could get that into the tree before 2.6.27 comes out :)
thanks,
greg k-h
* Greg KH <[email protected]>:
Quoted 9 lines · Alex Chiang
Quoted 6 lines
Did you get a chance to take a look at the documentation I wrote for these new symlinks? [I also went and documented the existing slots/ directory as well...]
Was it what you had in mind?
Yes, it looked very good.
Thanks.
Quoted 3 lines
And thanks for the other documentation as well, if you want, you could split that out as a different patch and odds are Jesse could get that into the tree before 2.6.27 comes out :)
Hrm, given the harder line Linus has taken against "regression only" patches lately, it can probably wait another release cycle.
After all, it's been un-documented since pre-git history anyway. ;)
Thanks.
/ac
Quoted 7 lines · Alex Chiang
* Greg KH <[email protected]>:
Quoted 3 lines
And thanks for the other documentation as well, if you want, you could split that out as a different patch and odds are Jesse could get that into the tree before 2.6.27 comes out :)
Hrm, given the harder line Linus has taken against "regression only" patches lately, it can probably wait another release cycle.
Documentation updates and new stand-alone drivers are ok at all times :)
thanks,
greg k-h
Quoted 10 lines · Greg KH
Quoted 7 lines · Alex Chiang
* Greg KH <[email protected]>:
Quoted 3 lines
And thanks for the other documentation as well, if you want, you could split that out as a different patch and odds are Jesse could get that into the tree before 2.6.27 comes out :)
Hrm, given the harder line Linus has taken against "regression only" patches lately, it can probably wait another release cycle.
Documentation updates and new stand-alone drivers are ok at all times :)
You're clearly not hanging on every word of our Dear Leader:
http://marc.info/?l=linux-kernel&m=121924636529367&w=2
Signature · 4 lines
Matthew Wilcox Intel Open Source Technology Centre "Bill, look, we understand that you're interested in selling us this operating system, but compare it to ours. We can't possibly take such a retrograde step."
Quoted 7 lines · Matthew Wilcox
Quoted 6 lines · Greg KH
Quoted 5 lines · Alex Chiang
* Greg KH <[email protected]>:
Quoted 3 lines
And thanks for the other documentation as well, if you want, you could split that out as a different patch and odds are Jesse could get that into the tree before 2.6.27 comes out :)
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
Hrm, given the harder line Linus has taken against "regression only" patches lately, it can probably wait another release cycle.
Documentation updates and new stand-alone drivers are ok at all times :)
You're clearly not hanging on every word of our Dear Leader:
I read that, and parsed:
I generally won't complain about them, but I also don't see the point.
as being up to the maintainer's judgement.
So in this case, it's up to Jesse :)
thanks,
greg k-h
Quoted 18 lines · Greg KH
Quoted 10 lines · Matthew Wilcox
Quoted 9 lines · Greg KH
Quoted 7 lines · Alex Chiang
* Greg KH <[email protected]>:
Quoted 3 lines
And thanks for the other documentation as well, if you want, you could split that out as a different patch and odds are Jesse could get that into the tree before 2.6.27 comes out :)
Hrm, given the harder line Linus has taken against "regression only" patches lately, it can probably wait another release cycle.
Quoted 6 lines
Quoted 2 lines
Documentation updates and new stand-alone drivers are ok at all times :)
You're clearly not hanging on every word of our Dear Leader:
Quoted 7 lines
I read that, and parsed:
I generally won't complain about them, but I also don't see the point.as being up to the maintainer's judgement.
So in this case, it's up to Jesse :)
Linus seems cranky lately, no need to tempt him to flame me I think (besides I don't have any other critical stuff queued up atm; I'm definitely not going to ask him to pull just a doc update this late in the cycle).
Signature · 1 line
Jesse Barnes, Intel Open Source Technology Center