From e105b8bfc769b0545b6f0f395179d1e43cbee822 Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Mon, 21 Apr 2008 10:51:07 -0700 Subject: sysfs: add /sys/dev/{char,block} to lookup sysfs path by major:minor Why?: There are occasions where userspace would like to access sysfs attributes for a device but it may not know how sysfs has named the device or the path. For example what is the sysfs path for /dev/disk/by-id/ata-ST3160827AS_5MT004CK? With this change a call to stat(2) returns the major:minor then userspace can see that /sys/dev/block/8:32 links to /sys/block/sdc. What are the alternatives?: 1/ Add an ioctl to return the path: Doable, but sysfs is meant to reduce the need to proliferate ioctl interfaces into the kernel, so this seems counter productive. 2/ Use udev to create these symlinks: Also doable, but it adds a udev dependency to utilities that might be running in a limited environment like an initramfs. 3/ Do a full-tree search of sysfs. [kay.sievers@vrfy.org: fix duplicate registrations] [kay.sievers@vrfy.org: cleanup suggestions] Cc: Neil Brown Cc: Tejun Heo Acked-by: Kay Sievers Reviewed-by: SL Baur Acked-by: Kay Sievers Acked-by: Mark Lord Acked-by: H. Peter Anvin Signed-off-by: Dan Williams Signed-off-by: Greg Kroah-Hartman --- drivers/base/class.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'drivers/base/class.c') diff --git a/drivers/base/class.c b/drivers/base/class.c index e085af0ff94..71ce3ff6bdf 100644 --- a/drivers/base/class.c +++ b/drivers/base/class.c @@ -148,6 +148,10 @@ int class_register(struct class *cls) if (error) return error; + /* set the default /sys/dev directory for devices of this class */ + if (!cls->dev_kobj) + cls->dev_kobj = sysfs_dev_char_kobj; + #if defined(CONFIG_SYSFS_DEPRECATED) && defined(CONFIG_BLOCK) /* let the block class directory show up in the root of sysfs */ if (cls != &block_class) -- cgit v1.2.3 From 93562b537659fc0f63920fd4d9d24f54e434f4c4 Mon Sep 17 00:00:00 2001 From: Greg Kroah-Hartman Date: Thu, 22 May 2008 17:21:08 -0400 Subject: Driver Core: add ability for class_for_each_device to start in middle of list This mirrors the functionality that driver_for_each_device has as well. We add a start variable, and all callers of the function are fixed up at the same time. The block layer will be using this new functionality in a follow-on patch. Cc: Kay Sievers Signed-off-by: Greg Kroah-Hartman --- drivers/base/class.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) (limited to 'drivers/base/class.c') diff --git a/drivers/base/class.c b/drivers/base/class.c index 71ce3ff6bdf..2eb7048003a 100644 --- a/drivers/base/class.c +++ b/drivers/base/class.c @@ -256,11 +256,14 @@ char *make_class_name(const char *name, struct kobject *kobj) /** * class_for_each_device - device iterator * @class: the class we're iterating + * @start: the device to start with in the list, if any. * @data: data for the callback * @fn: function to be called for each device * * Iterate over @class's list of devices, and call @fn for each, - * passing it @data. + * passing it @data. If @start is set, the list iteration will start + * there, otherwise if it is NULL, the iteration starts at the + * beginning of the list. * * We check the return of @fn each time. If it returns anything * other than 0, we break out and return that value. @@ -269,8 +272,8 @@ char *make_class_name(const char *name, struct kobject *kobj) * re-acquired in @fn, otherwise it will self-deadlocking. For * example, calls to add or remove class members would be verboten. */ -int class_for_each_device(struct class *class, void *data, - int (*fn)(struct device *, void *)) +int class_for_each_device(struct class *class, struct device *start, + void *data, int (*fn)(struct device *, void *)) { struct device *dev; int error = 0; @@ -279,12 +282,14 @@ int class_for_each_device(struct class *class, void *data, return -EINVAL; down(&class->sem); list_for_each_entry(dev, &class->devices, node) { + if (start) { + if (start == dev) + start = NULL; + continue; + } dev = get_device(dev); - if (dev) { - error = fn(dev, data); - put_device(dev); - } else - error = -ENODEV; + error = fn(dev, data); + put_device(dev); if (error) break; } -- cgit v1.2.3 From 695794ae0c5bdd9bd06e35b118801e2e9be04f9e Mon Sep 17 00:00:00 2001 From: Greg Kroah-Hartman Date: Thu, 22 May 2008 17:21:08 -0400 Subject: Driver Core: add ability for class_find_device to start in middle of list This mirrors the functionality that driver_find_device has as well. We add a start variable, and all callers of the function are fixed up at the same time. The block layer will be using this new functionality in a follow-on patch. Cc: Kay Sievers Signed-off-by: Greg Kroah-Hartman --- drivers/base/class.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) (limited to 'drivers/base/class.c') diff --git a/drivers/base/class.c b/drivers/base/class.c index 2eb7048003a..3918d0e432d 100644 --- a/drivers/base/class.c +++ b/drivers/base/class.c @@ -302,6 +302,7 @@ EXPORT_SYMBOL_GPL(class_for_each_device); /** * class_find_device - device iterator for locating a particular device * @class: the class we're iterating + * @start: Device to begin with * @data: data for the match function * @match: function to check device * @@ -319,8 +320,9 @@ EXPORT_SYMBOL_GPL(class_for_each_device); * re-acquired in @match, otherwise it will self-deadlocking. For * example, calls to add or remove class members would be verboten. */ -struct device *class_find_device(struct class *class, void *data, - int (*match)(struct device *, void *)) +struct device *class_find_device(struct class *class, struct device *start, + void *data, + int (*match)(struct device *, void *)) { struct device *dev; int found = 0; @@ -330,15 +332,17 @@ struct device *class_find_device(struct class *class, void *data, down(&class->sem); list_for_each_entry(dev, &class->devices, node) { + if (start) { + if (start == dev) + start = NULL; + continue; + } dev = get_device(dev); - if (dev) { - if (match(dev, data)) { - found = 1; - break; - } else - put_device(dev); - } else + if (match(dev, data)) { + found = 1; break; + } else + put_device(dev); } up(&class->sem); -- cgit v1.2.3 From 7c71448b8aa80123fc521563d5f7c63a099d97ab Mon Sep 17 00:00:00 2001 From: Greg Kroah-Hartman Date: Tue, 22 Jan 2008 18:17:41 -0500 Subject: class: move driver core specific parts to a private structure This moves the portions of struct class that are dynamic (kobject and lock and lists) out of the main structure and into a dynamic, private, structure. Cc: Kay Sievers Signed-off-by: Greg Kroah-Hartman --- drivers/base/class.c | 87 +++++++++++++++++++++++++++++----------------------- 1 file changed, 48 insertions(+), 39 deletions(-) (limited to 'drivers/base/class.c') diff --git a/drivers/base/class.c b/drivers/base/class.c index 3918d0e432d..06f09c929a9 100644 --- a/drivers/base/class.c +++ b/drivers/base/class.c @@ -21,17 +21,16 @@ #include "base.h" #define to_class_attr(_attr) container_of(_attr, struct class_attribute, attr) -#define to_class(obj) container_of(obj, struct class, subsys.kobj) static ssize_t class_attr_show(struct kobject *kobj, struct attribute *attr, char *buf) { struct class_attribute *class_attr = to_class_attr(attr); - struct class *dc = to_class(kobj); + struct class_private *cp = to_class(kobj); ssize_t ret = -EIO; if (class_attr->show) - ret = class_attr->show(dc, buf); + ret = class_attr->show(cp->class, buf); return ret; } @@ -39,17 +38,18 @@ static ssize_t class_attr_store(struct kobject *kobj, struct attribute *attr, const char *buf, size_t count) { struct class_attribute *class_attr = to_class_attr(attr); - struct class *dc = to_class(kobj); + struct class_private *cp = to_class(kobj); ssize_t ret = -EIO; if (class_attr->store) - ret = class_attr->store(dc, buf, count); + ret = class_attr->store(cp->class, buf, count); return ret; } static void class_release(struct kobject *kobj) { - struct class *class = to_class(kobj); + struct class_private *cp = to_class(kobj); + struct class *class = cp->class; pr_debug("class '%s': release.\n", class->name); @@ -78,7 +78,7 @@ int class_create_file(struct class *cls, const struct class_attribute *attr) { int error; if (cls) - error = sysfs_create_file(&cls->subsys.kobj, &attr->attr); + error = sysfs_create_file(&cls->p->subsys.kobj, &attr->attr); else error = -EINVAL; return error; @@ -87,21 +87,20 @@ int class_create_file(struct class *cls, const struct class_attribute *attr) void class_remove_file(struct class *cls, const struct class_attribute *attr) { if (cls) - sysfs_remove_file(&cls->subsys.kobj, &attr->attr); + sysfs_remove_file(&cls->p->subsys.kobj, &attr->attr); } static struct class *class_get(struct class *cls) { if (cls) - return container_of(kset_get(&cls->subsys), - struct class, subsys); - return NULL; + kset_get(&cls->p->subsys); + return cls; } static void class_put(struct class *cls) { if (cls) - kset_put(&cls->subsys); + kset_put(&cls->p->subsys); } static int add_class_attrs(struct class *cls) @@ -136,17 +135,23 @@ static void remove_class_attrs(struct class *cls) int class_register(struct class *cls) { + struct class_private *cp; int error; pr_debug("device class '%s': registering\n", cls->name); - INIT_LIST_HEAD(&cls->devices); - INIT_LIST_HEAD(&cls->interfaces); - kset_init(&cls->class_dirs); - init_MUTEX(&cls->sem); - error = kobject_set_name(&cls->subsys.kobj, "%s", cls->name); - if (error) + cp = kzalloc(sizeof(*cp), GFP_KERNEL); + if (!cp) + return -ENOMEM; + INIT_LIST_HEAD(&cp->devices); + INIT_LIST_HEAD(&cp->interfaces); + kset_init(&cp->class_dirs); + init_MUTEX(&cp->sem); + error = kobject_set_name(&cp->subsys.kobj, "%s", cls->name); + if (error) { + kfree(cp); return error; + } /* set the default /sys/dev directory for devices of this class */ if (!cls->dev_kobj) @@ -155,17 +160,21 @@ int class_register(struct class *cls) #if defined(CONFIG_SYSFS_DEPRECATED) && defined(CONFIG_BLOCK) /* let the block class directory show up in the root of sysfs */ if (cls != &block_class) - cls->subsys.kobj.kset = class_kset; + cp->subsys.kobj.kset = class_kset; #else - cls->subsys.kobj.kset = class_kset; + cp->subsys.kobj.kset = class_kset; #endif - cls->subsys.kobj.ktype = &class_ktype; + cp->subsys.kobj.ktype = &class_ktype; + cp->class = cls; + cls->p = cp; - error = kset_register(&cls->subsys); - if (!error) { - error = add_class_attrs(class_get(cls)); - class_put(cls); + error = kset_register(&cp->subsys); + if (error) { + kfree(cp); + return error; } + error = add_class_attrs(class_get(cls)); + class_put(cls); return error; } @@ -173,7 +182,7 @@ void class_unregister(struct class *cls) { pr_debug("device class '%s': unregistering\n", cls->name); remove_class_attrs(cls); - kset_unregister(&cls->subsys); + kset_unregister(&cls->p->subsys); } static void class_create_release(struct class *cls) @@ -280,8 +289,8 @@ int class_for_each_device(struct class *class, struct device *start, if (!class) return -EINVAL; - down(&class->sem); - list_for_each_entry(dev, &class->devices, node) { + down(&class->p->sem); + list_for_each_entry(dev, &class->p->devices, node) { if (start) { if (start == dev) start = NULL; @@ -293,7 +302,7 @@ int class_for_each_device(struct class *class, struct device *start, if (error) break; } - up(&class->sem); + up(&class->p->sem); return error; } @@ -330,8 +339,8 @@ struct device *class_find_device(struct class *class, struct device *start, if (!class) return NULL; - down(&class->sem); - list_for_each_entry(dev, &class->devices, node) { + down(&class->p->sem); + list_for_each_entry(dev, &class->p->devices, node) { if (start) { if (start == dev) start = NULL; @@ -344,7 +353,7 @@ struct device *class_find_device(struct class *class, struct device *start, } else put_device(dev); } - up(&class->sem); + up(&class->p->sem); return found ? dev : NULL; } @@ -362,13 +371,13 @@ int class_interface_register(struct class_interface *class_intf) if (!parent) return -EINVAL; - down(&parent->sem); - list_add_tail(&class_intf->node, &parent->interfaces); + down(&parent->p->sem); + list_add_tail(&class_intf->node, &parent->p->interfaces); if (class_intf->add_dev) { - list_for_each_entry(dev, &parent->devices, node) + list_for_each_entry(dev, &parent->p->devices, node) class_intf->add_dev(dev, class_intf); } - up(&parent->sem); + up(&parent->p->sem); return 0; } @@ -381,13 +390,13 @@ void class_interface_unregister(struct class_interface *class_intf) if (!parent) return; - down(&parent->sem); + down(&parent->p->sem); list_del_init(&class_intf->node); if (class_intf->remove_dev) { - list_for_each_entry(dev, &parent->devices, node) + list_for_each_entry(dev, &parent->p->devices, node) class_intf->remove_dev(dev, class_intf); } - up(&parent->sem); + up(&parent->p->sem); class_put(parent); } -- cgit v1.2.3 From 97ae69fdbaa71a8f7dbc20bf10fb349d1759152f Mon Sep 17 00:00:00 2001 From: Greg Kroah-Hartman Date: Wed, 28 May 2008 09:28:39 -0700 Subject: class: rename "devices" to "class_devices" in internal class structure This renames the struct class "devices" field to be "class_devices" to make things easier when struct bus_type and struct class merge in the future. It also makes grepping for fields easier as well. Based on an idea from Kay. Cc: Kay Sievers Signed-off-by: Greg Kroah-Hartman --- drivers/base/class.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'drivers/base/class.c') diff --git a/drivers/base/class.c b/drivers/base/class.c index 06f09c929a9..9947560def6 100644 --- a/drivers/base/class.c +++ b/drivers/base/class.c @@ -143,7 +143,7 @@ int class_register(struct class *cls) cp = kzalloc(sizeof(*cp), GFP_KERNEL); if (!cp) return -ENOMEM; - INIT_LIST_HEAD(&cp->devices); + INIT_LIST_HEAD(&cp->class_devices); INIT_LIST_HEAD(&cp->interfaces); kset_init(&cp->class_dirs); init_MUTEX(&cp->sem); @@ -290,7 +290,7 @@ int class_for_each_device(struct class *class, struct device *start, if (!class) return -EINVAL; down(&class->p->sem); - list_for_each_entry(dev, &class->p->devices, node) { + list_for_each_entry(dev, &class->p->class_devices, node) { if (start) { if (start == dev) start = NULL; @@ -340,7 +340,7 @@ struct device *class_find_device(struct class *class, struct device *start, return NULL; down(&class->p->sem); - list_for_each_entry(dev, &class->p->devices, node) { + list_for_each_entry(dev, &class->p->class_devices, node) { if (start) { if (start == dev) start = NULL; @@ -374,7 +374,7 @@ int class_interface_register(struct class_interface *class_intf) down(&parent->p->sem); list_add_tail(&class_intf->node, &parent->p->interfaces); if (class_intf->add_dev) { - list_for_each_entry(dev, &parent->p->devices, node) + list_for_each_entry(dev, &parent->p->class_devices, node) class_intf->add_dev(dev, class_intf); } up(&parent->p->sem); @@ -393,7 +393,7 @@ void class_interface_unregister(struct class_interface *class_intf) down(&parent->p->sem); list_del_init(&class_intf->node); if (class_intf->remove_dev) { - list_for_each_entry(dev, &parent->p->devices, node) + list_for_each_entry(dev, &parent->p->class_devices, node) class_intf->remove_dev(dev, class_intf); } up(&parent->p->sem); -- cgit v1.2.3 From 184f1f779d5a2e62de4a0b34842ddf8546beca8f Mon Sep 17 00:00:00 2001 From: Greg Kroah-Hartman Date: Wed, 28 May 2008 09:28:39 -0700 Subject: class: rename "interfaces" to "class_interfaces" in internal class structure This renames the struct class "interfaces" field to be "class_interfaces" to make things easier when struct bus_type and struct class merge in the future. It also makes grepping for fields easier as well. Based on an idea from Kay. Cc: Kay Sievers Signed-off-by: Greg Kroah-Hartman --- drivers/base/class.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'drivers/base/class.c') diff --git a/drivers/base/class.c b/drivers/base/class.c index 9947560def6..48b518e66bf 100644 --- a/drivers/base/class.c +++ b/drivers/base/class.c @@ -144,7 +144,7 @@ int class_register(struct class *cls) if (!cp) return -ENOMEM; INIT_LIST_HEAD(&cp->class_devices); - INIT_LIST_HEAD(&cp->interfaces); + INIT_LIST_HEAD(&cp->class_interfaces); kset_init(&cp->class_dirs); init_MUTEX(&cp->sem); error = kobject_set_name(&cp->subsys.kobj, "%s", cls->name); @@ -372,7 +372,7 @@ int class_interface_register(struct class_interface *class_intf) return -EINVAL; down(&parent->p->sem); - list_add_tail(&class_intf->node, &parent->p->interfaces); + list_add_tail(&class_intf->node, &parent->p->class_interfaces); if (class_intf->add_dev) { list_for_each_entry(dev, &parent->p->class_devices, node) class_intf->add_dev(dev, class_intf); -- cgit v1.2.3 From 1fbfee6c6dc0f4a4c587b6b163ee79643fc9aaa7 Mon Sep 17 00:00:00 2001 From: Greg Kroah-Hartman Date: Wed, 28 May 2008 09:28:39 -0700 Subject: class: rename "subsys" to "class_subsys" in internal class structure This renames the struct class "subsys" field to be "class_subsys" to make things easier when struct bus_type and struct class merge in the future. It also makes grepping for fields easier as well. Based on an idea from Kay. Cc: Kay Sievers Signed-off-by: Greg Kroah-Hartman --- drivers/base/class.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) (limited to 'drivers/base/class.c') diff --git a/drivers/base/class.c b/drivers/base/class.c index 48b518e66bf..86778b86496 100644 --- a/drivers/base/class.c +++ b/drivers/base/class.c @@ -70,7 +70,7 @@ static struct kobj_type class_ktype = { .release = class_release, }; -/* Hotplug events for classes go to the class_obj subsys */ +/* Hotplug events for classes go to the class class_subsys */ static struct kset *class_kset; @@ -78,7 +78,8 @@ int class_create_file(struct class *cls, const struct class_attribute *attr) { int error; if (cls) - error = sysfs_create_file(&cls->p->subsys.kobj, &attr->attr); + error = sysfs_create_file(&cls->p->class_subsys.kobj, + &attr->attr); else error = -EINVAL; return error; @@ -87,20 +88,20 @@ int class_create_file(struct class *cls, const struct class_attribute *attr) void class_remove_file(struct class *cls, const struct class_attribute *attr) { if (cls) - sysfs_remove_file(&cls->p->subsys.kobj, &attr->attr); + sysfs_remove_file(&cls->p->class_subsys.kobj, &attr->attr); } static struct class *class_get(struct class *cls) { if (cls) - kset_get(&cls->p->subsys); + kset_get(&cls->p->class_subsys); return cls; } static void class_put(struct class *cls) { if (cls) - kset_put(&cls->p->subsys); + kset_put(&cls->p->class_subsys); } static int add_class_attrs(struct class *cls) @@ -147,7 +148,7 @@ int class_register(struct class *cls) INIT_LIST_HEAD(&cp->class_interfaces); kset_init(&cp->class_dirs); init_MUTEX(&cp->sem); - error = kobject_set_name(&cp->subsys.kobj, "%s", cls->name); + error = kobject_set_name(&cp->class_subsys.kobj, "%s", cls->name); if (error) { kfree(cp); return error; @@ -160,15 +161,15 @@ int class_register(struct class *cls) #if defined(CONFIG_SYSFS_DEPRECATED) && defined(CONFIG_BLOCK) /* let the block class directory show up in the root of sysfs */ if (cls != &block_class) - cp->subsys.kobj.kset = class_kset; + cp->class_subsys.kobj.kset = class_kset; #else - cp->subsys.kobj.kset = class_kset; + cp->class_subsys.kobj.kset = class_kset; #endif - cp->subsys.kobj.ktype = &class_ktype; + cp->class_subsys.kobj.ktype = &class_ktype; cp->class = cls; cls->p = cp; - error = kset_register(&cp->subsys); + error = kset_register(&cp->class_subsys); if (error) { kfree(cp); return error; @@ -182,7 +183,7 @@ void class_unregister(struct class *cls) { pr_debug("device class '%s': unregistering\n", cls->name); remove_class_attrs(cls); - kset_unregister(&cls->p->subsys); + kset_unregister(&cls->p->class_subsys); } static void class_create_release(struct class *cls) -- cgit v1.2.3 From d9a0157328507c5f563e16a583cd0a063854aebb Mon Sep 17 00:00:00 2001 From: Greg Kroah-Hartman Date: Wed, 28 May 2008 09:28:39 -0700 Subject: class: rename "sem" to "class_sem" in internal class structure This renames the struct class "sem" field to be "class_sem" to make things easier when struct bus_type and struct class merge in the future. It also makes grepping for fields easier as well. Based on an idea from Kay. Cc: Kay Sievers Signed-off-by: Greg Kroah-Hartman --- drivers/base/class.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'drivers/base/class.c') diff --git a/drivers/base/class.c b/drivers/base/class.c index 86778b86496..d24d21114cc 100644 --- a/drivers/base/class.c +++ b/drivers/base/class.c @@ -147,7 +147,7 @@ int class_register(struct class *cls) INIT_LIST_HEAD(&cp->class_devices); INIT_LIST_HEAD(&cp->class_interfaces); kset_init(&cp->class_dirs); - init_MUTEX(&cp->sem); + init_MUTEX(&cp->class_sem); error = kobject_set_name(&cp->class_subsys.kobj, "%s", cls->name); if (error) { kfree(cp); @@ -278,7 +278,7 @@ char *make_class_name(const char *name, struct kobject *kobj) * We check the return of @fn each time. If it returns anything * other than 0, we break out and return that value. * - * Note, we hold class->sem in this function, so it can not be + * Note, we hold class->class_sem in this function, so it can not be * re-acquired in @fn, otherwise it will self-deadlocking. For * example, calls to add or remove class members would be verboten. */ @@ -290,7 +290,7 @@ int class_for_each_device(struct class *class, struct device *start, if (!class) return -EINVAL; - down(&class->p->sem); + down(&class->p->class_sem); list_for_each_entry(dev, &class->p->class_devices, node) { if (start) { if (start == dev) @@ -303,7 +303,7 @@ int class_for_each_device(struct class *class, struct device *start, if (error) break; } - up(&class->p->sem); + up(&class->p->class_sem); return error; } @@ -326,7 +326,7 @@ EXPORT_SYMBOL_GPL(class_for_each_device); * * Note, you will need to drop the reference with put_device() after use. * - * We hold class->sem in this function, so it can not be + * We hold class->class_sem in this function, so it can not be * re-acquired in @match, otherwise it will self-deadlocking. For * example, calls to add or remove class members would be verboten. */ @@ -340,7 +340,7 @@ struct device *class_find_device(struct class *class, struct device *start, if (!class) return NULL; - down(&class->p->sem); + down(&class->p->class_sem); list_for_each_entry(dev, &class->p->class_devices, node) { if (start) { if (start == dev) @@ -354,7 +354,7 @@ struct device *class_find_device(struct class *class, struct device *start, } else put_device(dev); } - up(&class->p->sem); + up(&class->p->class_sem); return found ? dev : NULL; } @@ -372,13 +372,13 @@ int class_interface_register(struct class_interface *class_intf) if (!parent) return -EINVAL; - down(&parent->p->sem); + down(&parent->p->class_sem); list_add_tail(&class_intf->node, &parent->p->class_interfaces); if (class_intf->add_dev) { list_for_each_entry(dev, &parent->p->class_devices, node) class_intf->add_dev(dev, class_intf); } - up(&parent->p->sem); + up(&parent->p->class_sem); return 0; } @@ -391,13 +391,13 @@ void class_interface_unregister(struct class_interface *class_intf) if (!parent) return; - down(&parent->p->sem); + down(&parent->p->class_sem); list_del_init(&class_intf->node); if (class_intf->remove_dev) { list_for_each_entry(dev, &parent->p->class_devices, node) class_intf->remove_dev(dev, class_intf); } - up(&parent->p->sem); + up(&parent->p->class_sem); class_put(parent); } -- cgit v1.2.3 From d2a3b9146e4f40c2e872d7567c996ef95083d802 Mon Sep 17 00:00:00 2001 From: Matthew Wilcox Date: Wed, 28 May 2008 09:28:39 -0700 Subject: class: add lockdep infrastructure This adds the infrastructure to properly handle lockdep issues when the internal class semaphore is changed to a mutex. Matthew wrote the original patch, and Greg fixed it up to work properly with the class_create() function. From: Matthew Wilcox Cc: Kay Sievers Cc: Dave Young Cc: Andrew Morton Cc: James Bottomley Cc: Peter Zijlstra Cc: Ingo Molnar Signed-off-by: Greg Kroah-Hartman --- drivers/base/class.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'drivers/base/class.c') diff --git a/drivers/base/class.c b/drivers/base/class.c index d24d21114cc..89000566690 100644 --- a/drivers/base/class.c +++ b/drivers/base/class.c @@ -134,7 +134,7 @@ static void remove_class_attrs(struct class *cls) } } -int class_register(struct class *cls) +int __class_register(struct class *cls, struct lock_class_key *key) { struct class_private *cp; int error; @@ -178,6 +178,7 @@ int class_register(struct class *cls) class_put(cls); return error; } +EXPORT_SYMBOL_GPL(__class_register); void class_unregister(struct class *cls) { @@ -203,7 +204,8 @@ static void class_create_release(struct class *cls) * Note, the pointer created here is to be destroyed when finished by * making a call to class_destroy(). */ -struct class *class_create(struct module *owner, const char *name) +struct class *__class_create(struct module *owner, const char *name, + struct lock_class_key *key) { struct class *cls; int retval; @@ -218,7 +220,7 @@ struct class *class_create(struct module *owner, const char *name) cls->owner = owner; cls->class_release = class_create_release; - retval = class_register(cls); + retval = __class_register(cls, key); if (retval) goto error; @@ -228,6 +230,7 @@ error: kfree(cls); return ERR_PTR(retval); } +EXPORT_SYMBOL_GPL(__class_create); /** * class_destroy - destroys a struct class structure @@ -412,9 +415,7 @@ int __init classes_init(void) EXPORT_SYMBOL_GPL(class_create_file); EXPORT_SYMBOL_GPL(class_remove_file); -EXPORT_SYMBOL_GPL(class_register); EXPORT_SYMBOL_GPL(class_unregister); -EXPORT_SYMBOL_GPL(class_create); EXPORT_SYMBOL_GPL(class_destroy); EXPORT_SYMBOL_GPL(class_interface_register); -- cgit v1.2.3 From f75b1c60fc1e53c4004a79ea0be071aa3318cdcc Mon Sep 17 00:00:00 2001 From: Dave Young Date: Wed, 28 May 2008 09:28:39 -0700 Subject: class: change internal semaphore to a mutex Now that the lockdep infrastructure in the class core is in place, we should be able to properly change the internal class semaphore to be a mutex. David wrote the original patch, and Greg fixed it up to apply properly due to all of the recent changes in this area. From: Dave Young Cc: Matthew Wilcox Cc: Kay Sievers Cc: Andrew Morton Cc: James Bottomley Signed-off-by: Greg Kroah-Hartman --- drivers/base/class.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) (limited to 'drivers/base/class.c') diff --git a/drivers/base/class.c b/drivers/base/class.c index 89000566690..839d27cecb3 100644 --- a/drivers/base/class.c +++ b/drivers/base/class.c @@ -18,6 +18,7 @@ #include #include #include +#include #include "base.h" #define to_class_attr(_attr) container_of(_attr, struct class_attribute, attr) @@ -147,7 +148,7 @@ int __class_register(struct class *cls, struct lock_class_key *key) INIT_LIST_HEAD(&cp->class_devices); INIT_LIST_HEAD(&cp->class_interfaces); kset_init(&cp->class_dirs); - init_MUTEX(&cp->class_sem); + __mutex_init(&cp->class_mutex, "struct class mutex", key); error = kobject_set_name(&cp->class_subsys.kobj, "%s", cls->name); if (error) { kfree(cp); @@ -281,7 +282,7 @@ char *make_class_name(const char *name, struct kobject *kobj) * We check the return of @fn each time. If it returns anything * other than 0, we break out and return that value. * - * Note, we hold class->class_sem in this function, so it can not be + * Note, we hold class->class_mutex in this function, so it can not be * re-acquired in @fn, otherwise it will self-deadlocking. For * example, calls to add or remove class members would be verboten. */ @@ -293,7 +294,7 @@ int class_for_each_device(struct class *class, struct device *start, if (!class) return -EINVAL; - down(&class->p->class_sem); + mutex_lock(&class->p->class_mutex); list_for_each_entry(dev, &class->p->class_devices, node) { if (start) { if (start == dev) @@ -306,7 +307,7 @@ int class_for_each_device(struct class *class, struct device *start, if (error) break; } - up(&class->p->class_sem); + mutex_unlock(&class->p->class_mutex); return error; } @@ -329,7 +330,7 @@ EXPORT_SYMBOL_GPL(class_for_each_device); * * Note, you will need to drop the reference with put_device() after use. * - * We hold class->class_sem in this function, so it can not be + * We hold class->class_mutex in this function, so it can not be * re-acquired in @match, otherwise it will self-deadlocking. For * example, calls to add or remove class members would be verboten. */ @@ -343,7 +344,7 @@ struct device *class_find_device(struct class *class, struct device *start, if (!class) return NULL; - down(&class->p->class_sem); + mutex_lock(&class->p->class_mutex); list_for_each_entry(dev, &class->p->class_devices, node) { if (start) { if (start == dev) @@ -357,7 +358,7 @@ struct device *class_find_device(struct class *class, struct device *start, } else put_device(dev); } - up(&class->p->class_sem); + mutex_unlock(&class->p->class_mutex); return found ? dev : NULL; } @@ -375,13 +376,13 @@ int class_interface_register(struct class_interface *class_intf) if (!parent) return -EINVAL; - down(&parent->p->class_sem); + mutex_lock(&parent->p->class_mutex); list_add_tail(&class_intf->node, &parent->p->class_interfaces); if (class_intf->add_dev) { list_for_each_entry(dev, &parent->p->class_devices, node) class_intf->add_dev(dev, class_intf); } - up(&parent->p->class_sem); + mutex_unlock(&parent->p->class_mutex); return 0; } @@ -394,13 +395,13 @@ void class_interface_unregister(struct class_interface *class_intf) if (!parent) return; - down(&parent->p->class_sem); + mutex_lock(&parent->p->class_mutex); list_del_init(&class_intf->node); if (class_intf->remove_dev) { list_for_each_entry(dev, &parent->p->class_devices, node) class_intf->remove_dev(dev, class_intf); } - up(&parent->p->class_sem); + mutex_unlock(&parent->p->class_mutex); class_put(parent); } -- cgit v1.2.3