Skip to content

Commit f75b1c6

Browse files
hidavegregkh
authored andcommitted
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 <hidave.darkstar@gmail.com> Cc: Matthew Wilcox <matthew@wil.cx> Cc: Kay Sievers <kay.sievers@vrfy.org> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: James Bottomley <James.Bottomley@HansenPartnership.com> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
1 parent d2a3b91 commit f75b1c6

File tree

3 files changed

+19
-17
lines changed

3 files changed

+19
-17
lines changed

drivers/base/base.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ struct driver_private {
4444
* @class_devices - list of devices associated with this class
4545
* @class_interfaces - list of class_interfaces associated with this class
4646
* @class_dirs - "glue" directory for virtual devices associated with this class
47-
* @class_sem - semaphore to protect the children, devices, and interfaces lists.
47+
* @class_mutex - mutex to protect the children, devices, and interfaces lists.
4848
* @class - pointer back to the struct class that this structure is associated
4949
* with.
5050
*
@@ -57,7 +57,7 @@ struct class_private {
5757
struct list_head class_devices;
5858
struct list_head class_interfaces;
5959
struct kset class_dirs;
60-
struct semaphore class_sem;
60+
struct mutex class_mutex;
6161
struct class *class;
6262
};
6363
#define to_class(obj) \

drivers/base/class.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <linux/err.h>
1919
#include <linux/slab.h>
2020
#include <linux/genhd.h>
21+
#include <linux/mutex.h>
2122
#include "base.h"
2223

2324
#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)
147148
INIT_LIST_HEAD(&cp->class_devices);
148149
INIT_LIST_HEAD(&cp->class_interfaces);
149150
kset_init(&cp->class_dirs);
150-
init_MUTEX(&cp->class_sem);
151+
__mutex_init(&cp->class_mutex, "struct class mutex", key);
151152
error = kobject_set_name(&cp->class_subsys.kobj, "%s", cls->name);
152153
if (error) {
153154
kfree(cp);
@@ -281,7 +282,7 @@ char *make_class_name(const char *name, struct kobject *kobj)
281282
* We check the return of @fn each time. If it returns anything
282283
* other than 0, we break out and return that value.
283284
*
284-
* Note, we hold class->class_sem in this function, so it can not be
285+
* Note, we hold class->class_mutex in this function, so it can not be
285286
* re-acquired in @fn, otherwise it will self-deadlocking. For
286287
* example, calls to add or remove class members would be verboten.
287288
*/
@@ -293,7 +294,7 @@ int class_for_each_device(struct class *class, struct device *start,
293294

294295
if (!class)
295296
return -EINVAL;
296-
down(&class->p->class_sem);
297+
mutex_lock(&class->p->class_mutex);
297298
list_for_each_entry(dev, &class->p->class_devices, node) {
298299
if (start) {
299300
if (start == dev)
@@ -306,7 +307,7 @@ int class_for_each_device(struct class *class, struct device *start,
306307
if (error)
307308
break;
308309
}
309-
up(&class->p->class_sem);
310+
mutex_unlock(&class->p->class_mutex);
310311

311312
return error;
312313
}
@@ -329,7 +330,7 @@ EXPORT_SYMBOL_GPL(class_for_each_device);
329330
*
330331
* Note, you will need to drop the reference with put_device() after use.
331332
*
332-
* We hold class->class_sem in this function, so it can not be
333+
* We hold class->class_mutex in this function, so it can not be
333334
* re-acquired in @match, otherwise it will self-deadlocking. For
334335
* example, calls to add or remove class members would be verboten.
335336
*/
@@ -343,7 +344,7 @@ struct device *class_find_device(struct class *class, struct device *start,
343344
if (!class)
344345
return NULL;
345346

346-
down(&class->p->class_sem);
347+
mutex_lock(&class->p->class_mutex);
347348
list_for_each_entry(dev, &class->p->class_devices, node) {
348349
if (start) {
349350
if (start == dev)
@@ -357,7 +358,7 @@ struct device *class_find_device(struct class *class, struct device *start,
357358
} else
358359
put_device(dev);
359360
}
360-
up(&class->p->class_sem);
361+
mutex_unlock(&class->p->class_mutex);
361362

362363
return found ? dev : NULL;
363364
}
@@ -375,13 +376,13 @@ int class_interface_register(struct class_interface *class_intf)
375376
if (!parent)
376377
return -EINVAL;
377378

378-
down(&parent->p->class_sem);
379+
mutex_lock(&parent->p->class_mutex);
379380
list_add_tail(&class_intf->node, &parent->p->class_interfaces);
380381
if (class_intf->add_dev) {
381382
list_for_each_entry(dev, &parent->p->class_devices, node)
382383
class_intf->add_dev(dev, class_intf);
383384
}
384-
up(&parent->p->class_sem);
385+
mutex_unlock(&parent->p->class_mutex);
385386

386387
return 0;
387388
}
@@ -394,13 +395,13 @@ void class_interface_unregister(struct class_interface *class_intf)
394395
if (!parent)
395396
return;
396397

397-
down(&parent->p->class_sem);
398+
mutex_lock(&parent->p->class_mutex);
398399
list_del_init(&class_intf->node);
399400
if (class_intf->remove_dev) {
400401
list_for_each_entry(dev, &parent->p->class_devices, node)
401402
class_intf->remove_dev(dev, class_intf);
402403
}
403-
up(&parent->p->class_sem);
404+
mutex_unlock(&parent->p->class_mutex);
404405

405406
class_put(parent);
406407
}

drivers/base/core.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <linux/genhd.h>
2222
#include <linux/kallsyms.h>
2323
#include <linux/semaphore.h>
24+
#include <linux/mutex.h>
2425

2526
#include "base.h"
2627
#include "power/power.h"
@@ -907,7 +908,7 @@ int device_add(struct device *dev)
907908
klist_add_tail(&dev->knode_parent, &parent->klist_children);
908909

909910
if (dev->class) {
910-
down(&dev->class->p->class_sem);
911+
mutex_lock(&dev->class->p->class_mutex);
911912
/* tie the class to the device */
912913
list_add_tail(&dev->node, &dev->class->p->class_devices);
913914

@@ -916,7 +917,7 @@ int device_add(struct device *dev)
916917
&dev->class->p->class_interfaces, node)
917918
if (class_intf->add_dev)
918919
class_intf->add_dev(dev, class_intf);
919-
up(&dev->class->p->class_sem);
920+
mutex_unlock(&dev->class->p->class_mutex);
920921
}
921922
Done:
922923
put_device(dev);
@@ -1017,15 +1018,15 @@ void device_del(struct device *dev)
10171018
if (dev->class) {
10181019
device_remove_class_symlinks(dev);
10191020

1020-
down(&dev->class->p->class_sem);
1021+
mutex_lock(&dev->class->p->class_mutex);
10211022
/* notify any interfaces that the device is now gone */
10221023
list_for_each_entry(class_intf,
10231024
&dev->class->p->class_interfaces, node)
10241025
if (class_intf->remove_dev)
10251026
class_intf->remove_dev(dev, class_intf);
10261027
/* remove the device from the class list */
10271028
list_del_init(&dev->node);
1028-
up(&dev->class->p->class_sem);
1029+
mutex_unlock(&dev->class->p->class_mutex);
10291030
}
10301031
device_remove_file(dev, &uevent_attr);
10311032
device_remove_attrs(dev);

0 commit comments

Comments
 (0)