Skip to content

Commit 6992f53

Browse files
ebiedermgregkh
authored andcommitted
sysfs: Use one lockdep class per sysfs attribute.
Acknowledge that the logical sysfs rwsem has one instance per sysfs attribute with different locking depencencies for different attributes. There is a sysfs idiom where writing to one sysfs file causes the addition or removal of other sysfs files. Lumping all of the sysfs attributes together in one lock class causes lockdep to generate lots of false positives. This introduces the requirement that non-static sysfs attributes need to be initialized with sysfs_attr_init or sysfs_bin_attr_init. Strictly speaking this requirement only exists when lockdep is enabled, and when lockdep is enabled we get a bit fat warning if this requirement is not met. Signed-off-by: Eric W. Biederman <ebiederm@xmission.com> Acked-by: WANG Cong <xiyou.wangcong@gmail.com> Cc: Tejun Heo <tj@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
1 parent a2db684 commit 6992f53

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

‎fs/sysfs/sysfs.h‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,12 @@ static inline unsigned int sysfs_type(struct sysfs_dirent *sd)
9292
#ifdef CONFIG_DEBUG_LOCK_ALLOC
9393
#define sysfs_dirent_init_lockdep(sd) \
9494
do { \
95-
static struct lock_class_key __key; \
95+
struct attribute *attr = sd->s_attr.attr; \
96+
struct lock_class_key *key = attr->key; \
97+
if (!key) \
98+
key = &attr->skey; \
9699
\
97-
lockdep_init_map(&sd->dep_map, "s_active", &__key, 0); \
100+
lockdep_init_map(&sd->dep_map, "s_active", key, 0); \
98101
} while(0)
99102
#else
100103
#define sysfs_dirent_init_lockdep(sd) do {} while(0)

‎include/linux/sysfs.h‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <linux/compiler.h>
1616
#include <linux/errno.h>
1717
#include <linux/list.h>
18+
#include <linux/lockdep.h>
1819
#include <asm/atomic.h>
1920

2021
struct kobject;
@@ -29,8 +30,23 @@ struct attribute {
2930
const char *name;
3031
struct module *owner;
3132
mode_t mode;
33+
#ifdef CONFIG_DEBUG_LOCK_ALLOC
34+
struct lock_class_key *key;
35+
struct lock_class_key skey;
36+
#endif
3237
};
3338

39+
#ifdef CONFIG_DEBUG_LOCK_ALLOC
40+
#define sysfs_attr_init(attr) \
41+
do { \
42+
static struct lock_class_key __key; \
43+
\
44+
(attr)->key = &__key; \
45+
} while(0)
46+
#else
47+
#define sysfs_attr_init(attr) do {} while(0)
48+
#endif
49+
3450
struct attribute_group {
3551
const char *name;
3652
mode_t (*is_visible)(struct kobject *,
@@ -74,6 +90,8 @@ struct bin_attribute {
7490
struct vm_area_struct *vma);
7591
};
7692

93+
#define sysfs_bin_attr_init(bin_attr) sysfs_attr_init(&bin_attr->attr)
94+
7795
struct sysfs_ops {
7896
ssize_t (*show)(struct kobject *, struct attribute *,char *);
7997
ssize_t (*store)(struct kobject *,struct attribute *,const char *, size_t);

0 commit comments

Comments
 (0)