sysfs: add marks for mutable sysfs files
Subsystems (6)
RFC PATCH 1/1 sysfs: add marks for mutable sysfs files
NOTE: This patch is only a draft, not ready to be taken.
This fixes all the s_active related bogus lockdep warnings that I received, I already tested it, it works fine for cpu hotplug, I/O scheduler switch, and suspend.
This patch introduces sever sysfs/kobject interfaces to add mutable sysfs files or kobjects, those files could be removed by the kernel during some event, e.g. cpu hotplug. All of this kind of sysfs files should use these API's, to avoid the deadlock warnings.
I am still not sure if this is the best fix.
Please comment.
---
arch/x86/kernel/cpu/intel_cacheinfo.c +1 -1
@@ -914,7 +914,7 @@ static int __cpuinit cache_add_dev(struct sys_device * sys_dev)if (unlikely (retval <0 ))return retval; - retval =kobject_init_and_add (per_cpu (ici_cache_kobject, cpu), + retval =kobject_init_and_add_mutable (per_cpu (ici_cache_kobject, cpu), &ktype_percpu_entry, &sys_dev->kobj,"%s" ,"cache" );if (retval <0 ) {
arch/x86/kernel/cpu/mcheck/mce.c +1 -1
@@ -1906,7 +1906,7 @@ static __cpuinit int mce_create_device(unsigned int cpu)per_cpu (mce_dev, cpu).id = cpu;per_cpu (mce_dev, cpu).cls = &mce_sysclass; - err =sysdev_register (&per_cpu (mce_dev, cpu)); + err =sysdev_register_mutable (&per_cpu (mce_dev, cpu));if (err)return err;
block/elevator.c +1 -1
@@ -210,7 +210,7 @@ static struct elevator_queue *elevator_alloc(struct request_queue *q, eq->ops = &e->ops; eq->elevator_type = e; -kobject_init (&eq->kobj, &elv_ktype); +kobject_init_mutable (&eq->kobj, &elv_ktype);mutex_init (&eq->sysfs_lock); eq->hash = kmalloc_node(sizeof(struct hlist_head) * ELV_HASH_ENTRIES,
drivers/base/sys.c +24 -7
@@ -231,12 +231,7 @@ EXPORT_SYMBOL_GPL(sysdev_driver_unregister); -/** -* sysdev_register - add a system device to the tree -* @sysdev: device in question -* -*/ -int sysdev_register (struct sys_device *sysdev) +static int _sysdev_register (struct sys_device *sysdev,int mutable) {int error;struct sysdev_class *cls = sysdev->cls;
@@ -254,7 +249,12 @@ int sysdev_register(struct sys_device *sysdev) sysdev->kobj.kset = &cls->kset;/* Register the object */ - error =kobject_init_and_add (&sysdev->kobj, &ktype_sysdev,NULL , +if (mutable) + error =kobject_init_and_add_mutable (&sysdev->kobj, &ktype_sysdev, +NULL ,"%s%d" ,kobject_name (&cls->kset.kobj), + sysdev->id); +else + error =kobject_init_and_add (&sysdev->kobj, &ktype_sysdev,NULL ,"%s%d" ,kobject_name (&cls->kset.kobj), sysdev->id);
@@ -281,6 +281,22 @@ int sysdev_register(struct sys_device *sysdev)return error; } +/** +* sysdev_register - add a system device to the tree +* @sysdev: device in question +* +*/ + +int sysdev_register (struct sys_device *sysdev) +{ +return _sysdev_register (sysdev,0 ); +} + +int sysdev_register_mutable (struct sys_device *sysdev) +{ +return _sysdev_register (sysdev,1 ); +} +void sysdev_unregister (struct sys_device *sysdev) {struct sysdev_driver *drv;
@@ -501,6 +517,7 @@ int __init system_bus_init(void) }EXPORT_SYMBOL_GPL (sysdev_register); +EXPORT_SYMBOL_GPL (sysdev_register_mutable);EXPORT_SYMBOL_GPL (sysdev_unregister);#define to_ext_attr (x) container_of(x, struct sysdev_ext_attribute, attr)
drivers/base/topology.c +1 -1
@@ -134,7 +134,7 @@ static int __cpuinit topology_add_dev(unsigned int cpu) {struct sys_device *sys_dev =get_cpu_sysdev (cpu); -return sysfs_create_group (&sys_dev->kobj, &topology_attr_group); +return sysfs_create_group_mutable (&sys_dev->kobj, &topology_attr_group); }static void __cpuinit topology_remove_dev (unsigned int cpu)
drivers/cpufreq/cpufreq.c +1 -1
@@ -2006,7 +2006,7 @@ static int __init cpufreq_core_init(void)init_rwsem (&per_cpu (cpu_policy_rwsem, cpu)); } - cpufreq_global_kobject =kobject_create_and_add ("cpufreq" , + cpufreq_global_kobject =kobject_create_and_add_mutable ("cpufreq" , &cpu_sysdev_class.kset.kobj);BUG_ON (!cpufreq_global_kobject);
drivers/cpuidle/sysfs.c +2 -2
@@ -360,8 +360,8 @@ int cpuidle_add_sysfs(struct sys_device *sysdev)int error; dev =per_cpu (cpuidle_devices, cpu); - error =kobject_init_and_add (&dev->kobj, &ktype_cpuidle, &sysdev->kobj, -"cpuidle" ); + error =kobject_init_and_add_mutable (&dev->kobj, &ktype_cpuidle, + &sysdev->kobj,"cpuidle" );if (!error)kobject_uevent (&dev->kobj,KOBJ_ADD );return error;
fs/sysfs/dir.c +18 -5
@@ -27,6 +27,7 @@DEFINE_MUTEX (sysfs_mutex);DEFINE_SPINLOCK (sysfs_assoc_lock); +static struct lock_class_key sysfs_lock_classes[SYSFS_NR_LOCK_CLASSES ];static DEFINE_SPINLOCK (sysfs_ino_lock);static DEFINE_IDA (sysfs_ino_ida);
@@ -338,6 +339,7 @@ struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type) {char *dup_name =NULL ;struct sysfs_dirent *sd; +int class =SYSFS_LOCK_CLASS_NORMAL ;if (type &SYSFS_COPY_NAME ) { name = dup_name =kstrdup (name,GFP_KERNEL );
@@ -354,7 +356,9 @@ struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type)atomic_set (&sd->s_count,1 );atomic_set (&sd->s_active,0 ); -sysfs_dirent_init_lockdep (sd); +if (type &SYSFS_FLAG_MUTABLE ) + class =SYSFS_LOCK_CLASS_MUTABLE ; +lockdep_set_class_and_name (sd, &sysfs_lock_classes[class],"s_active" ); sd->s_name = name; sd->s_mode = mode;
@@ -608,15 +612,18 @@ struct sysfs_dirent *sysfs_get_dirent(struct sysfs_dirent *parent_sd,EXPORT_SYMBOL_GPL (sysfs_get_dirent);static int create_dir (struct kobject *kobj,struct sysfs_dirent *parent_sd, -const char *name,struct sysfs_dirent **p_sd) +const char *name,struct sysfs_dirent **p_sd,int mutable) {umode_t mode =S_IFDIR |S_IRWXU |S_IRUGO |S_IXUGO ;struct sysfs_addrm_cxt acxt;struct sysfs_dirent *sd;int rc; +int type =SYSFS_DIR ; +if (mutable || kobj->mutable || (parent_sd->s_flags &SYSFS_FLAG_MUTABLE )) + type |=SYSFS_FLAG_MUTABLE ;/* allocate */ - sd =sysfs_new_dirent (name, mode,SYSFS_DIR ); + sd =sysfs_new_dirent (name, mode, type);if (!sd)return -ENOMEM ; sd->s_dir.kobj = kobj;
@@ -637,7 +644,13 @@ static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd,int sysfs_create_subdir (struct kobject *kobj,const char *name,struct sysfs_dirent **p_sd) { -return create_dir (kobj, kobj->sd, name, p_sd); +return create_dir (kobj, kobj->sd, name, p_sd,0 ); +} + +int sysfs_create_subdir_mutable (struct kobject *kobj,const char *name, +struct sysfs_dirent **p_sd) +{ +return create_dir (kobj, kobj->sd, name, p_sd,1 ); }/**
@@ -656,7 +669,7 @@ int sysfs_create_dir(struct kobject * kobj)else parent_sd = &sysfs_root; - error =create_dir (kobj, parent_sd,kobject_name (kobj), &sd); + error =create_dir (kobj, parent_sd,kobject_name (kobj), &sd,0 );if (!error) kobj->sd = sd;return error;
fs/sysfs/file.c +7 -1
@@ -505,6 +505,9 @@ int sysfs_add_file_mode(struct sysfs_dirent *dir_sd,struct sysfs_dirent *sd;int rc; +if (dir_sd->s_flags &SYSFS_FLAG_MUTABLE ) + type |=SYSFS_FLAG_MUTABLE ; + sd =sysfs_new_dirent (attr->name, mode, type);if (!sd)return -ENOMEM ;
@@ -536,9 +539,12 @@ int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr,int sysfs_create_file (struct kobject * kobj,const struct attribute * attr) { +int type =SYSFS_KOBJ_ATTR ;BUG_ON (!kobj || !kobj->sd || !attr); -return sysfs_add_file (kobj->sd, attr,SYSFS_KOBJ_ATTR ); +if (kobj->mutable) + type |=SYSFS_FLAG_MUTABLE ; +return sysfs_add_file (kobj->sd, attr, type); }
fs/sysfs/group.c +14 -4
@@ -56,7 +56,7 @@ static int create_files(struct sysfs_dirent *dir_sd, struct kobject *kobj, } -static int internal_create_group (struct kobject *kobj,int update, +static int internal_create_group (struct kobject *kobj,int update,int mutable,const struct attribute_group *grp) {struct sysfs_dirent *sd;
@@ -69,7 +69,10 @@ static int internal_create_group(struct kobject *kobj, int update,return -EINVAL ;if (grp->name) { - error =sysfs_create_subdir (kobj, grp->name, &sd); +if (mutable) + error =sysfs_create_subdir_mutable (kobj, grp->name, &sd); +else + error =sysfs_create_subdir (kobj, grp->name, &sd);if (error)return error; }else
@@ -97,9 +100,16 @@ static int internal_create_group(struct kobject *kobj, int update,int sysfs_create_group (struct kobject *kobj,const struct attribute_group *grp) { -return internal_create_group (kobj,0 , grp); +return internal_create_group (kobj,0 ,0 , grp); } +int sysfs_create_group_mutable (struct kobject *kobj, +const struct attribute_group *grp) +{ +return internal_create_group (kobj,0 ,1 , grp); +} +EXPORT_SYMBOL_GPL (sysfs_create_group_mutable); +/** * sysfs_update_group - given a directory kobject, create an attribute group * @kobj: The kobject to create the group on
@@ -120,7 +130,7 @@ int sysfs_create_group(struct kobject *kobj,int sysfs_update_group (struct kobject *kobj,const struct attribute_group *grp) { -return internal_create_group (kobj,1 , grp); +return internal_create_group (kobj,1 ,0 , grp); }
fs/sysfs/symlink.c +2 -0
@@ -87,6 +87,8 @@ static int sysfs_do_create_link(struct kobject *kobj, struct kobject *target,int sysfs_create_link (struct kobject *kobj,struct kobject *target,const char *name) { +if (target->mutable) +kobject_set_mutable (kobj);return sysfs_do_create_link (kobj, target, name,1 ); }
fs/sysfs/sysfs.h +9 -11
@@ -71,6 +71,12 @@ struct sysfs_dirent {struct sysfs_inode_attrs *s_iattr; }; +enum sysfs_lock_classes { +SYSFS_LOCK_CLASS_NORMAL , +SYSFS_LOCK_CLASS_MUTABLE , +SYSFS_NR_LOCK_CLASSES , +}; +#define SD_DEACTIVATED_BIAS INT_MIN#define SYSFS_TYPE_MASK 0x00ff
@@ -82,23 +88,13 @@ struct sysfs_dirent {#define SYSFS_FLAG_MASK ~SYSFS_TYPE_MASK#define SYSFS_FLAG_REMOVED 0x0200 +#define SYSFS_FLAG_MUTABLE 0x0400static inline unsigned int sysfs_type (struct sysfs_dirent *sd) {return sd->s_flags &SYSFS_TYPE_MASK ; } -#ifdef CONFIG_DEBUG_LOCK_ALLOC -#define sysfs_dirent_init_lockdep (sd) \ -do { \ - static struct lock_class_key __key; \ - \ - lockdep_init_map(&sd->dep_map, "s_active", &__key, 0); \ -} while(0) -#else -#define sysfs_dirent_init_lockdep (sd) do {} while(0) -#endif -/* * Context structure to be used while adding/removing nodes. */
@@ -143,6 +139,8 @@ void release_sysfs_dirent(struct sysfs_dirent *sd);int sysfs_create_subdir (struct kobject *kobj,const char *name,struct sysfs_dirent **p_sd); +int sysfs_create_subdir_mutable (struct kobject *kobj,const char *name, +struct sysfs_dirent **p_sd);void sysfs_remove_subdir (struct sysfs_dirent *sd); int sysfs_rename(struct sysfs_dirent *sd,
include/linux/kobject.h +10 -0
@@ -69,6 +69,7 @@ struct kobject { unsigned int state_add_uevent_sent:1; unsigned int state_remove_uevent_sent:1; unsigned int uevent_suppress:1; + unsigned int mutable:1; };extern int kobject_set_name (struct kobject *kobj,const char *name, ...)
@@ -76,12 +77,15 @@ extern int kobject_set_name(struct kobject *kobj, const char *name, ...)extern int kobject_set_name_vargs (struct kobject *kobj,const char *fmt,va_list vargs); +extern void kobject_set_mutable (struct kobject *kobj); +static inline const char *kobject_name (const struct kobject *kobj) {return kobj->name; }extern void kobject_init (struct kobject *kobj,struct kobj_type *ktype); +extern void kobject_init_mutable (struct kobject *kobj,struct kobj_type *ktype);extern int __must_check kobject_add (struct kobject *kobj,struct kobject *parent,const char *fmt, ...);
@@ -89,12 +93,18 @@ extern int __must_check kobject_init_and_add(struct kobject *kobj,struct kobj_type *ktype, struct kobject *parent, const char *fmt, ...); +extern int __must_check kobject_init_and_add_mutable (struct kobject *kobj, +struct kobj_type *ktype, +struct kobject *parent, +const char *fmt, ...);extern void kobject_del (struct kobject *kobj);extern struct kobject *__must_check kobject_create (void );extern struct kobject *__must_check kobject_create_and_add (const char *name,struct kobject *parent); +extern struct kobject *__must_check kobject_create_and_add_mutable (const char *name, +struct kobject *parent);extern int __must_check kobject_rename (struct kobject *,const char *new_name);extern int __must_check kobject_move (struct kobject *,struct kobject *);
include/linux/sysdev.h +1 -0
@@ -94,6 +94,7 @@ struct sys_device { };extern int sysdev_register (struct sys_device *); +extern int sysdev_register_mutable (struct sys_device *);extern void sysdev_unregister (struct sys_device *);
include/linux/sysfs.h +2 -0
@@ -112,6 +112,8 @@ void sysfs_remove_link(struct kobject *kobj, const char *name);int __must_check sysfs_create_group (struct kobject *kobj,const struct attribute_group *grp); +int __must_check sysfs_create_group_mutable (struct kobject *kobj, +const struct attribute_group *grp);int sysfs_update_group (struct kobject *kobj,const struct attribute_group *grp); void sysfs_remove_group(struct kobject *kobj,
lib/kobject.c +61 -0
91 lines not on this page · raw message
(Adding David into Cc...)
Quoted 553 lines · Amerigo Wang
NOTE: This patch is only a draft, not ready to be taken.
This fixes all the
s_activerelated bogus lockdep warnings that I received, I already tested it, it works fine for cpu hotplug, I/O scheduler switch, and suspend.This patch introduces sever sysfs/kobject interfaces to add mutable sysfs files or kobjects, those files could be removed by the kernel during some event, e.g. cpu hotplug. All of this kind of sysfs files should use these API's, to avoid the deadlock warnings.
I am still not sure if this is the best fix.
Please comment.
Signed-off-by WANG Cong <[email protected]>---
arch/x86/kernel/cpu/intel_cacheinfo.c +1
-1@@ -914,7 +914,7 @@ static int __cpuinit cache_add_dev(struct sys_device * sys_dev)if (unlikely (retval <0 ))return retval; - retval =kobject_init_and_add (per_cpu (ici_cache_kobject, cpu), + retval =kobject_init_and_add_mutable (per_cpu (ici_cache_kobject, cpu), &ktype_percpu_entry, &sys_dev->kobj,"%s" ,"cache" );if (retval <0 ) {arch/x86/kernel/cpu/mcheck/mce.c +1
-1@@ -1906,7 +1906,7 @@ static __cpuinit int mce_create_device(unsigned int cpu)per_cpu (mce_dev, cpu).id = cpu;per_cpu (mce_dev, cpu).cls = &mce_sysclass; - err =sysdev_register (&per_cpu (mce_dev, cpu)); + err =sysdev_register_mutable (&per_cpu (mce_dev, cpu));if (err)return err;block/elevator.c +1
-1@@ -210,7 +210,7 @@ static struct elevator_queue *elevator_alloc(struct request_queue *q, eq->ops = &e->ops; eq->elevator_type = e; -kobject_init (&eq->kobj, &elv_ktype); +kobject_init_mutable (&eq->kobj, &elv_ktype);mutex_init (&eq->sysfs_lock); eq->hash = kmalloc_node(sizeof(struct hlist_head) * ELV_HASH_ENTRIES,drivers/base/sys.c +24
-7@@ -231,12 +231,7 @@ EXPORT_SYMBOL_GPL(sysdev_driver_unregister); -/** -* sysdev_register - add a system device to the tree -* @sysdev: device in question -* -*/ -int sysdev_register (struct sys_device *sysdev) +static int _sysdev_register (struct sys_device *sysdev,int mutable) {int error;struct sysdev_class *cls = sysdev->cls;@@ -254,7 +249,12 @@ int sysdev_register(struct sys_device *sysdev) sysdev->kobj.kset = &cls->kset;/* Register the object */ - error =kobject_init_and_add (&sysdev->kobj, &ktype_sysdev,NULL , +if (mutable) + error =kobject_init_and_add_mutable (&sysdev->kobj, &ktype_sysdev, +NULL ,"%s%d" ,kobject_name (&cls->kset.kobj), + sysdev->id); +else + error =kobject_init_and_add (&sysdev->kobj, &ktype_sysdev,NULL ,"%s%d" ,kobject_name (&cls->kset.kobj), sysdev->id);@@ -281,6 +281,22 @@ int sysdev_register(struct sys_device *sysdev)return error; } +/** +* sysdev_register - add a system device to the tree +* @sysdev: device in question +* +*/ + +int sysdev_register (struct sys_device *sysdev) +{ +return _sysdev_register (sysdev,0 ); +} + +int sysdev_register_mutable (struct sys_device *sysdev) +{ +return _sysdev_register (sysdev,1 ); +} +void sysdev_unregister (struct sys_device *sysdev) {struct sysdev_driver *drv;@@ -501,6 +517,7 @@ int __init system_bus_init(void) }EXPORT_SYMBOL_GPL (sysdev_register); +EXPORT_SYMBOL_GPL (sysdev_register_mutable);EXPORT_SYMBOL_GPL (sysdev_unregister);#define to_ext_attr (x) container_of(x, struct sysdev_ext_attribute, attr)drivers/base/topology.c +1
-1@@ -134,7 +134,7 @@ static int __cpuinit topology_add_dev(unsigned int cpu) {struct sys_device *sys_dev =get_cpu_sysdev (cpu); -return sysfs_create_group (&sys_dev->kobj, &topology_attr_group); +return sysfs_create_group_mutable (&sys_dev->kobj, &topology_attr_group); }static void __cpuinit topology_remove_dev (unsigned int cpu)drivers/cpufreq/cpufreq.c +1
-1@@ -2006,7 +2006,7 @@ static int __init cpufreq_core_init(void)init_rwsem (&per_cpu (cpu_policy_rwsem, cpu)); } - cpufreq_global_kobject =kobject_create_and_add ("cpufreq" , + cpufreq_global_kobject =kobject_create_and_add_mutable ("cpufreq" , &cpu_sysdev_class.kset.kobj);BUG_ON (!cpufreq_global_kobject);drivers/cpuidle/sysfs.c +2
-2@@ -360,8 +360,8 @@ int cpuidle_add_sysfs(struct sys_device *sysdev)int error; dev =per_cpu (cpuidle_devices, cpu); - error =kobject_init_and_add (&dev->kobj, &ktype_cpuidle, &sysdev->kobj, -"cpuidle" ); + error =kobject_init_and_add_mutable (&dev->kobj, &ktype_cpuidle, + &sysdev->kobj,"cpuidle" );if (!error)kobject_uevent (&dev->kobj,KOBJ_ADD );return error;fs/sysfs/dir.c +18
-5@@ -27,6 +27,7 @@DEFINE_MUTEX (sysfs_mutex);DEFINE_SPINLOCK (sysfs_assoc_lock); +static struct lock_class_key sysfs_lock_classes[SYSFS_NR_LOCK_CLASSES ];static DEFINE_SPINLOCK (sysfs_ino_lock);static DEFINE_IDA (sysfs_ino_ida);@@ -338,6 +339,7 @@ struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type) {char *dup_name =NULL ;struct sysfs_dirent *sd; +int class =SYSFS_LOCK_CLASS_NORMAL ;if (type &SYSFS_COPY_NAME ) { name = dup_name =kstrdup (name,GFP_KERNEL );@@ -354,7 +356,9 @@ struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type)atomic_set (&sd->s_count,1 );atomic_set (&sd->s_active,0 ); -sysfs_dirent_init_lockdep (sd); +if (type &SYSFS_FLAG_MUTABLE ) + class =SYSFS_LOCK_CLASS_MUTABLE ; +lockdep_set_class_and_name (sd, &sysfs_lock_classes[class],"s_active" ); sd->s_name = name; sd->s_mode = mode;@@ -608,15 +612,18 @@ struct sysfs_dirent *sysfs_get_dirent(struct sysfs_dirent *parent_sd,EXPORT_SYMBOL_GPL (sysfs_get_dirent);static int create_dir (struct kobject *kobj,struct sysfs_dirent *parent_sd, -const char *name,struct sysfs_dirent **p_sd) +const char *name,struct sysfs_dirent **p_sd,int mutable) {umode_t mode =S_IFDIR |S_IRWXU |S_IRUGO |S_IXUGO ;struct sysfs_addrm_cxt acxt;struct sysfs_dirent *sd;int rc; +int type =SYSFS_DIR ; +if (mutable || kobj->mutable || (parent_sd->s_flags &SYSFS_FLAG_MUTABLE )) + type |=SYSFS_FLAG_MUTABLE ;/* allocate */ - sd =sysfs_new_dirent (name, mode,SYSFS_DIR ); + sd =sysfs_new_dirent (name, mode, type);if (!sd)return -ENOMEM ; sd->s_dir.kobj = kobj;@@ -637,7 +644,13 @@ static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd,int sysfs_create_subdir (struct kobject *kobj,const char *name,struct sysfs_dirent **p_sd) { -return create_dir (kobj, kobj->sd, name, p_sd); +return create_dir (kobj, kobj->sd, name, p_sd,0 ); +} + +int sysfs_create_subdir_mutable (struct kobject *kobj,const char *name, +struct sysfs_dirent **p_sd) +{ +return create_dir (kobj, kobj->sd, name, p_sd,1 ); }/** @@ -656,7 +669,7 @@ int sysfs_create_dir(struct kobject * kobj)else parent_sd = &sysfs_root; - error =create_dir (kobj, parent_sd,kobject_name (kobj), &sd); + error =create_dir (kobj, parent_sd,kobject_name (kobj), &sd,0 );if (!error) kobj->sd = sd;return error;fs/sysfs/file.c +7
-1@@ -505,6 +505,9 @@ int sysfs_add_file_mode(struct sysfs_dirent *dir_sd,struct sysfs_dirent *sd;int rc; +if (dir_sd->s_flags &SYSFS_FLAG_MUTABLE ) + type |=SYSFS_FLAG_MUTABLE ; + sd =sysfs_new_dirent (attr->name, mode, type);if (!sd)return -ENOMEM ;@@ -536,9 +539,12 @@ int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr,int sysfs_create_file (struct kobject * kobj,const struct attribute * attr) { +int type =SYSFS_KOBJ_ATTR ;BUG_ON (!kobj || !kobj->sd || !attr); -return sysfs_add_file (kobj->sd, attr,SYSFS_KOBJ_ATTR ); +if (kobj->mutable) + type |=SYSFS_FLAG_MUTABLE ; +return sysfs_add_file (kobj->sd, attr, type); }fs/sysfs/group.c +14
-4@@ -56,7 +56,7 @@ static int create_files(struct sysfs_dirent *dir_sd, struct kobject *kobj, } -static int internal_create_group (struct kobject *kobj,int update, +static int internal_create_group (struct kobject *kobj,int update,int mutable,const struct attribute_group *grp) {struct sysfs_dirent *sd;@@ -69,7 +69,10 @@ static int internal_create_group(struct kobject *kobj, int update,return -EINVAL ;if (grp->name) { - error =sysfs_create_subdir (kobj, grp->name, &sd); +if (mutable) + error =sysfs_create_subdir_mutable (kobj, grp->name, &sd); +else + error =sysfs_create_subdir (kobj, grp->name, &sd);if (error)return error; }else @@ -97,9 +100,16 @@ static int internal_create_group(struct kobject *kobj, int update,int sysfs_create_group (struct kobject *kobj,const struct attribute_group *grp) { -return internal_create_group (kobj,0 , grp); +return internal_create_group (kobj,0 ,0 , grp); } +int sysfs_create_group_mutable (struct kobject *kobj, +const struct attribute_group *grp) +{ +return internal_create_group (kobj,0 ,1 , grp); +} +EXPORT_SYMBOL_GPL (sysfs_create_group_mutable); +/** * sysfs_update_group - given a directory kobject, create an attribute group * @kobj: The kobject to create the group on @@ -120,7 +130,7 @@ int sysfs_create_group(struct kobject *kobj,int sysfs_update_group (struct kobject *kobj,const struct attribute_group *grp) { -return internal_create_group (kobj,1 , grp); +return internal_create_group (kobj,1 ,0 , grp); }fs/sysfs/symlink.c +2
-0@@ -87,6 +87,8 @@ static int sysfs_do_create_link(struct kobject *kobj, struct kobject *target,int sysfs_create_link (struct kobject *kobj,struct kobject *target,const char *name) { +if (target->mutable) +kobject_set_mutable (kobj);return sysfs_do_create_link (kobj, target, name,1 ); }fs/sysfs/sysfs.h +9
-11@@ -71,6 +71,12 @@ struct sysfs_dirent {struct sysfs_inode_attrs *s_iattr; }; +enum sysfs_lock_classes { +SYSFS_LOCK_CLASS_NORMAL , +SYSFS_LOCK_CLASS_MUTABLE , +SYSFS_NR_LOCK_CLASSES , +}; +#define SD_DEACTIVATED_BIAS INT_MIN#define SYSFS_TYPE_MASK 0x00ff@@ -82,23 +88,13 @@ struct sysfs_dirent {#define SYSFS_FLAG_MASK ~SYSFS_TYPE_MASK#define SYSFS_FLAG_REMOVED 0x0200 +#define SYSFS_FLAG_MUTABLE 0x0400static inline unsigned int sysfs_type (struct sysfs_dirent *sd) {return sd->s_flags &SYSFS_TYPE_MASK ; } -#ifdef CONFIG_DEBUG_LOCK_ALLOC -#define sysfs_dirent_init_lockdep (sd) \ -do { \ - static struct lock_class_key __key; \ - \ - lockdep_init_map(&sd->dep_map, "s_active", &__key, 0); \ -} while(0) -#else -#define sysfs_dirent_init_lockdep (sd) do {} while(0) -#endif -/* * Context structure to be used while adding/removing nodes. */ @@ -143,6 +139,8 @@ void release_sysfs_dirent(struct sysfs_dirent *sd);int sysfs_create_subdir (struct kobject *kobj,const char *name,struct sysfs_dirent **p_sd); +int sysfs_create_subdir_mutable (struct kobject *kobj,const char *name, +struct sysfs_dirent **p_sd);void sysfs_remove_subdir (struct sysfs_dirent *sd); int sysfs_rename(struct sysfs_dirent *sd,include/linux/kobject.h +10
-0@@ -69,6 +69,7 @@ struct kobject { unsigned int state_add_uevent_sent:1; unsigned int state_remove_uevent_sent:1; unsigned int uevent_suppress:1; + unsigned int mutable:1; };extern int kobject_set_name (struct kobject *kobj,const char *name, ...)@@ -76,12 +77,15 @@ extern int kobject_set_name(struct kobject *kobj, const char *name, ...)extern int kobject_set_name_vargs (struct kobject *kobj,const char *fmt,va_list vargs); +extern void kobject_set_mutable (struct kobject *kobj); +static inline const char *kobject_name (const struct kobject *kobj) {return kobj->name; }extern void kobject_init (struct kobject *kobj,struct kobj_type *ktype); +extern void kobject_init_mutable (struct kobject *kobj,struct kobj_type *ktype);extern int __must_check kobject_add (struct kobject *kobj,struct kobject *parent,const char *fmt, ...);@@ -89,12 +93,18 @@ extern int __must_check kobject_init_and_add(struct kobject *kobj,struct kobj_type *ktype, struct kobject *parent, const char *fmt, ...); +extern int __must_check kobject_init_and_add_mutable (struct kobject *kobj, +struct kobj_type *ktype, +struct kobject *parent, +const char *fmt, ...);extern void kobject_del (struct kobject *kobj);extern struct kobject *__must_check kobject_create (void );extern struct kobject *__must_check kobject_create_and_add (const char *name,struct kobject *parent); +extern struct kobject *__must_check kobject_create_and_add_mutable (const char *name, +struct kobject *parent);extern int __must_check kobject_rename (struct kobject *,const char *new_name);extern int __must_check kobject_move (struct kobject *,struct kobject *);include/linux/sysdev.h +1
-0@@ -94,6 +94,7 @@ struct sys_device { };extern int sysdev_register (struct sys_device *); +extern int sysdev_register_mutable (struct sys_device *);extern void sysdev_unregister (struct sys_device *);include/linux/sysfs.h +2
-0@@ -112,6 +112,8 @@ void sysfs_remove_link(struct kobject *kobj, const char *name);int __must_check sysfs_create_group (struct kobject *kobj,const struct attribute_group *grp); +int __must_check sysfs_create_group_mutable (struct kobject *kobj, +const struct attribute_group *grp);int sysfs_update_group (struct kobject *kobj,const struct attribute_group *grp); void sysfs_remove_group(struct kobject *kobj,lib/kobject.c +61
-0@@ -152,6 +152,7 @@ static void kobject_init_internal(struct kobject *kobj) kobj->state_add_uevent_sent =0 ; kobj->state_remove_uevent_sent =0 ; kobj->state_initialized =1 ; + kobj->mutable =0 ; }@@ -256,6 +257,19 @@ int kobject_set_name(struct kobject *kobj, const char *fmt, ...)EXPORT_SYMBOL (kobject_set_name);/** +* kobject_set_mutable - Mark a kobject as mutable +* @kobj: struct kobject to mark +* +* This marks a kobject as mutable, this means this kobject +* and its children could be removed by kernel during hotplug etc.. +* Normally you don't need to set this. +*/ +void kobject_set_mutable (struct kobject *kobj) +{ + kobj->mutable =1 ; +} + +/** * kobject_init - initialize a kobject structure * @kobj: pointer to the kobject to initialize * @ktype: pointer to the ktype for this kobject. @@ -296,6 +310,13 @@ error: }EXPORT_SYMBOL (kobject_init); +void kobject_init_mutable (struct kobject *kobj,struct kobj_type *ktype) +{ +kobject_init (kobj, ktype); +kobject_set_mutable (kobj); +} +EXPORT_SYMBOL (kobject_init_mutable); +static int kobject_add_varg (struct kobject *kobj,struct kobject *parent,const char *fmt,va_list vargs) {@@ -386,6 +407,23 @@ int kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype, }EXPORT_SYMBOL_GPL (kobject_init_and_add); +int kobject_init_and_add_mutable (struct kobject *kobj,struct kobj_type *ktype, +struct kobject *parent,const char *fmt, ...) +{ +va_list args; +int retval; + +kobject_init_mutable (kobj, ktype); + +va_start (args, fmt); + retval =kobject_add_varg (kobj, parent, fmt, args); +va_end (args); + +return retval; +} +EXPORT_SYMBOL_GPL (kobject_init_and_add_mutable); + +/** * kobject_rename - change the name of an object * @kobj: object in question. @@ -664,6 +702,29 @@ struct kobject *kobject_create_and_add(const char *name, struct kobject *parent) }EXPORT_SYMBOL_GPL (kobject_create_and_add); +struct kobject *kobject_create_and_add_mutable (const char *name, +struct kobject *parent) +{ +struct kobject *kobj; +int retval; + + kobj =kobject_create (); +if (!kobj) +return NULL ; + +kobject_set_mutable (kobj); + retval =kobject_add (kobj, parent,"%s" , name); +if (retval) { +printk (KERN_WARNING "%s: kobject_add error: %d\n" , + __func__, retval); +kobject_put (kobj); + kobj =NULL ; + } +return kobj; +} +EXPORT_SYMBOL_GPL (kobject_create_and_add_mutable); + +/** * kset_init - initialize a kset for use * @k: kset
Quoted 6 lines · Amerigo Wang
NOTE: This patch is only a draft, not ready to be taken.
This fixes all the
s_activerelated bogus lockdep warnings that I received, I already tested it, it works fine for cpu hotplug, I/O scheduler switch, and suspend.
Quoted 8 lines
This patch introduces sever sysfs/kobject interfaces to add mutable sysfs files or kobjects, those files could be removed by the kernel during some event, e.g. cpu hotplug. All of this kind of sysfs files should use these API's, to avoid the deadlock warnings.
I am still not sure if this is the best fix.
Please comment.
mutable as you describe it happens to be the common case, and that class of files is not free from this class of problem.
Your patch is actively wrong if it solves the i/o scheduler issue, as those files are in fact mutable by your definition, block devices are hot pluggable.
Having a special case for permanent sysfs files, that we refuse to delete would be reasonable, but it certainly does not cover everything.
Eric
Quoted 20 lines · Eric W. Biederman
Quoted 6 lines · Amerigo Wang
NOTE: This patch is only a draft, not ready to be taken.
This fixes all the
s_activerelated bogus lockdep warnings that I received, I already tested it, it works fine for cpu hotplug, I/O scheduler switch, and suspend.Quoted 8 lines
This patch introduces sever sysfs/kobject interfaces to add mutable sysfs files or kobjects, those files could be removed by the kernel during some event, e.g. cpu hotplug. All of this kind of sysfs files should use these API's, to avoid the deadlock warnings.
I am still not sure if this is the best fix.
Please comment.
mutable as you describe it happens to be the common case, and that class of files is not free from this class of problem.
Quoted 8 lines
Your patch is actively wrong if it solves the i/o scheduler issue, as those files are in fact mutable by your definition, block devices are hot pluggable.
Having a special case for permanent sysfs files, that we refuse to delete would be reasonable, but it certainly does not cover everything.
(Sorry for the delay, we are having Chinese new year here.)
Oh, yes, I just worried that if my patch is aggressive. I saw your patch, will have a review now.
Thank you!