driver core: Expose device link details in sysfs
RFC PATCH 1/1 driver core: Expose device link details in sysfs
It's helpful to be able to look at device link details from sysfs. So, expose it in sysfs.
Say device-A is supplier of device-B. These are the additional files this patch would create:
/sys/class/devlink/device-A:device-B/
flags supplier/ -> .../device-A/ consumer/ -> .../device-B/
/sys/devices/.../device-A/
consumer:device-B/ -> /sys/class/devlink/device-A:device-B/
/sys/devices/.../device-B/
supplier:device-A/ -> /sys/class/devlink/device-A:device-B/
That way:
To get a list of all the device link in the system:
ls /sys/class/devlink/
To get the consumer names and links of a device:
ls -d /sys/devices/.../device-X/consumer:*
To get the supplier names and links of a device:
ls -d /sys/devices/.../device-X/supplier:*
For now, I'm just exporting "flags", supplier and consumer for each device link. But the goal is to expand it to "state", etc once the overall idea is accepted.
---
Greg/Rafael,
Wanted to check if both of you are okay with the overall idea of exporting device link details through sysfs and if using a "struct device" is how you'd like to do it. I think this information would be helpful in debugging all kinds of suspend/resume, probe and power issues in a production system. I didn't want to spend more time on this patch before I got your okays.
I'm not too familiar with the right way to do kobjs and symlinks in sysfs -- so apologies any crazy code. But overall, the patch does create the layout I describe above and seems to work.
I could also remove kref and switch to using link_dev to keep track of refcount and releasing stuff, but I wasn't sure if we really needed the srcu implementation or not. So didn't remove it in this series and left it as is.
Known issues:
- There seem to be some issue in the srcu clean up path where it can't find the "consumer" and "supplier" links to remove. I need to debug that. It could be one due to one of those weird dwc3 drivers. Just a FYI in case you spot something wrong with my patch. Pointers welcome. "kernfs : can not remove 'consumer', no directory"
Thanks,
Saravana
drivers/base/core.c | 129 ++++++++++++++++++++++++++++++++++++++++- include/linux/device.h | 58 +++++++++--------- 2 files changed, 156 insertions(+), 31 deletions(-)
drivers/base/core.c +126 -3
@@ -52,6 +52,7 @@ static unsigned int defer_sync_state_count = 1;static unsigned int defer_fw_devlink_count;static DEFINE_MUTEX (defer_fw_devlink_lock);static bool fw_devlink_is_permissive (void ); +static void __device_link_del (struct kref *kref);#ifdef CONFIG_SRCU static DEFINE_MUTEX (device_links_lock);
@@ -235,6 +236,108 @@ void device_pm_move_to_tail(struct device *dev)device_links_read_unlock (idx); } +#define to_devlink (dev) container_of((dev), struct device_link, link_dev) + +static ssize_t flags_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +return sprintf (buf,"%08x\n" ,to_devlink (dev)->flags); +} +static DEVICE_ATTR_RO (flags); + +static struct attribute *devlink_attrs[] = { + &dev_attr_flags.attr, +NULL , +}; +ATTRIBUTE_GROUPS (devlink); + +static struct class devlink_class = { + .name ="devlink" , + .owner =THIS_MODULE , + .dev_groups = devlink_groups, +}; + +static int __init devlink_class_init (void ) +{ +return class_register (&devlink_class); +} +postcore_initcall (devlink_class_init); + +static int devlink_add_symlinks (struct device_link *link) +{ +int ret; +size_t len; +struct device *sup = link->supplier; +struct device *con = link->consumer; +char *buf; + + len =max (strlen (dev_name (sup)),strlen (dev_name (con))); + len +=strlen ("supplier:" ) +1 ; + buf =kzalloc (len,GFP_KERNEL ); +if (!buf) +return -ENOMEM ; + + ret =sysfs_create_link (&link->link_dev.kobj, &sup->kobj,"supplier" ); +if (ret) + goto out; + + ret =sysfs_create_link (&link->link_dev.kobj, &con->kobj,"consumer" ); +if (ret) + goto err_con; + +snprintf (buf, len,"consumer:%s" ,dev_name (con)); + ret =sysfs_create_link (&sup->kobj, &link->link_dev.kobj, buf); +if (ret) + goto err_con_dev; + +snprintf (buf, len,"supplier:%s" ,dev_name (sup)); + ret =sysfs_create_link (&con->kobj, &link->link_dev.kobj, buf); +if (ret) + goto err_sup_dev; + + goto out; + +err_sup_dev: +snprintf (buf, len,"consumer:%s" ,dev_name (con)); +sysfs_remove_link (&sup->kobj, buf); +err_con_dev: +sysfs_remove_link (&link->link_dev.kobj,"consumer" ); +err_con: +sysfs_remove_link (&link->link_dev.kobj,"supplier" ); +out: +kfree (buf); +return ret; +} + +static void device_link_dev_release (struct device *dev) +{ +struct device_link *link =to_devlink (dev); +size_t len; +struct device *sup = link->supplier; +struct device *con = link->consumer; +char *buf; + +sysfs_remove_link (&link->link_dev.kobj,"consumer" ); +sysfs_remove_link (&link->link_dev.kobj,"supplier" ); + + len =max (strlen (dev_name (sup)),strlen (dev_name (con))); + len +=strlen ("supplier:" ) +1 ; + buf =kzalloc (len,GFP_KERNEL ); +if (!buf) { +WARN (1 ,"Unable to properly free device link!\n" ); +return ; + } + +snprintf (buf, len,"supplier:%s" ,dev_name (sup)); +sysfs_remove_link (&con->kobj, buf); +snprintf (buf, len,"consumer:%s" ,dev_name (con)); +sysfs_remove_link (&sup->kobj, buf); +kfree (buf); +put_device (link->consumer); +put_device (link->supplier); +kfree (link); +} +#define DL_MANAGED_LINK_FLAGS (DL_FLAG_AUTOREMOVE_CONSUMER | \ DL_FLAG_AUTOREMOVE_SUPPLIER | \ DL_FLAG_AUTOPROBE_CONSUMER | \
@@ -422,6 +525,17 @@ struct device_link *device_link_add(struct device *consumer, link->flags = flags;kref_init (&link->kref); + link->link_dev.class = &devlink_class; + link->link_dev.release = &device_link_dev_release; +device_set_pm_not_required (&link->link_dev); +dev_set_name (&link->link_dev,"%s:%s" , +dev_name (supplier),dev_name (consumer)); + +if (device_register (&link->link_dev)) + goto err_dev_reg; +if (devlink_add_symlinks (link)) + goto err_symlinks; +/* Determine the initial link state. */ if (flags &DL_FLAG_STATELESS ) link->status =DL_STATE_NONE ;
@@ -466,6 +580,14 @@ struct device_link *device_link_add(struct device *consumer,pm_runtime_put (supplier);return link; + +err_symlinks: +device_unregister (&link->link_dev); +err_dev_reg: +kref_put (&link->kref, __device_link_del); +device_pm_unlock (); +device_links_write_unlock (); +return NULL ; }EXPORT_SYMBOL_GPL (device_link_add);
@@ -542,9 +664,7 @@ static void device_link_free(struct device_link *link)while (refcount_dec_not_one (&link->rpm_active))pm_runtime_put (link->supplier); -put_device (link->consumer); -put_device (link->supplier); -kfree (link); +device_unregister (&link->link_dev); }#ifdef CONFIG_SRCU
@@ -1123,6 +1243,9 @@ static void device_links_purge(struct device *dev) {struct device_link *link, *ln; +if (dev->class == &devlink_class) +return ; +mutex_lock (&wfs_lock);list_del (&dev->links.needs_suppliers);mutex_unlock (&wfs_lock);
include/linux/device.h +30 -28
70 lines not on this page · raw message
Signature · 1 line
2.26.2.761.g0e0b3e54be-goog
Greg Kroah-Hartman
maintainerQuoted 42 lines · Saravana Kannan
It's helpful to be able to look at device link details from sysfs. So, expose it in sysfs.
Say device-A is supplier of device-B. These are the additional files this patch would create:
/sys/class/devlink/device-A:device-B/
flags supplier/ -> .../device-A/ consumer/ -> .../device-B/
/sys/devices/.../device-A/consumer:device-B/ -> /sys/class/devlink/device-A:device-B/
/sys/devices/.../device-B/supplier:device-A/ -> /sys/class/devlink/device-A:device-B/That way:
To get a list of all the device link in the system:
ls/sys/class/devlink/To get the consumer names and links of a device:
ls -d /sys/devices/.../device-X/consumer:*To get the supplier names and links of a device:
ls -d /sys/devices/.../device-X/supplier:*For now, I'm just exporting "flags", supplier and consumer for each device link. But the goal is to expand it to "state", etc once the overall idea is accepted.
Signed-off-by Saravana Kannan <[email protected]>---
Greg/Rafael,Wanted to check if both of you are okay with the overall idea of exporting device link details through sysfs and if using a "struct device" is how you'd like to do it. I think this information would be helpful in debugging all kinds of suspend/resume, probe and power issues in a production system. I didn't want to spend more time on this patch before I got your okays.
Quoted 3 lines
I'm not too familiar with the right way to do kobjs and symlinks in sysfs -- so apologies any crazy code. But overall, the patch does create the layout I describe above and seems to work.
Looks sane to me, nice work.
Quoted 4 lines
I could also remove kref and switch to using
link_devto keep track of refcount and releasing stuff, but I wasn't sure if we really needed the srcu implementation or not. So didn't remove it in this series and left it as is.
I would just leave that alone for now, as that would be a different change here, right?
thanks,
greg k-h