[PATCH v3] PM / devfreq: use _visible attribute to replace create/remove_sysfs_files()

Pengjie Zhang posted 1 patch 1 month, 1 week ago
There is a newer version of this series
drivers/devfreq/devfreq.c | 108 ++++++++++++++++++++++++--------------
1 file changed, 68 insertions(+), 40 deletions(-)
[PATCH v3] PM / devfreq: use _visible attribute to replace create/remove_sysfs_files()
Posted by Pengjie Zhang 1 month, 1 week ago
Previously, non-generic attributes (polling_interval, timer) used separate 
create/delete logic, leading to race conditions during concurrent access in
creation/deletion. Multi-threaded operations also caused inconsistencies   
between governor capabilities and attribute states.                        
                                                                           
1.Use is_visible + sysfs_update_group() to unify management of these       
attributes, eliminating creation/deletion races.                           
2.Add locks and validation to these attributes, ensuring consistency       
between current governor capabilities and attribute operations in          
multi-threaded environments.                                               
                                                                           
Signed-off-by: Pengjie Zhang <zhangpengjie2@huawei.com>                    
---                                                                        
changes in v3:                                                             
- Use guard() to simplify lock acquisition and destruction.                
- Eliminate redundant checks for df.                                       
Link to v2:https://lore.kernel.org/lkml/20251028022458.2824872-1-zhangpengjie2@huawei.com/
                                                                           
Changes in v2:                                                             
- Fix one problem reported by the kernel test robot.                       
- Redirect all error paths in timer_store() to out to ensure locks are not 
 left unReleased.                                                          
Link to v1:https://lore.kernel.org/lkml/20251025135238.3576861-1-zhangpengjie2@huawei.com/

 drivers/devfreq/devfreq.c | 108 ++++++++++++++++++++++++--------------
 1 file changed, 68 insertions(+), 40 deletions(-)

diff --git a/drivers/devfreq/devfreq.c b/drivers/devfreq/devfreq.c
index 2e8d01d47f69..674b794d553d 100644
--- a/drivers/devfreq/devfreq.c
+++ b/drivers/devfreq/devfreq.c
@@ -38,6 +38,7 @@
 
 static struct class *devfreq_class;
 static struct dentry *devfreq_debugfs;
+static const struct attribute_group gov_attr_group;
 
 /*
  * devfreq core provides delayed work based load monitoring helper
@@ -785,11 +786,6 @@ static void devfreq_dev_release(struct device *dev)
 	kfree(devfreq);
 }
 
-static void create_sysfs_files(struct devfreq *devfreq,
-				const struct devfreq_governor *gov);
-static void remove_sysfs_files(struct devfreq *devfreq,
-				const struct devfreq_governor *gov);
-
 /**
  * devfreq_add_device() - Add devfreq feature to the device
  * @dev:	the device to add devfreq feature.
@@ -956,7 +952,10 @@ struct devfreq *devfreq_add_device(struct device *dev,
 			 __func__);
 		goto err_init;
 	}
-	create_sysfs_files(devfreq, devfreq->governor);
+
+	err = sysfs_update_group(&devfreq->dev.kobj, &gov_attr_group);
+	if (err)
+		goto err_init;
 
 	list_add(&devfreq->node, &devfreq_list);
 
@@ -998,9 +997,7 @@ int devfreq_remove_device(struct devfreq *devfreq)
 	if (devfreq->governor) {
 		devfreq->governor->event_handler(devfreq,
 						 DEVFREQ_GOV_STOP, NULL);
-		remove_sysfs_files(devfreq, devfreq->governor);
 	}
-
 	device_unregister(&devfreq->dev);
 
 	return 0;
@@ -1460,7 +1457,6 @@ static ssize_t governor_store(struct device *dev, struct device_attribute *attr,
 			 __func__, df->governor->name, ret);
 		goto out;
 	}
-	remove_sysfs_files(df, df->governor);
 
 	/*
 	 * Start the new governor and create the specific sysfs files
@@ -1489,7 +1485,7 @@ static ssize_t governor_store(struct device *dev, struct device_attribute *attr,
 	 * Create the sysfs files for the new governor. But if failed to start
 	 * the new governor, restore the sysfs files of previous governor.
 	 */
-	create_sysfs_files(df, df->governor);
+	ret = sysfs_update_group(&df->dev.kobj, &gov_attr_group);
 
 out:
 	mutex_unlock(&devfreq_list_lock);
@@ -1807,17 +1803,22 @@ static struct attribute *devfreq_attrs[] = {
 	&dev_attr_trans_stat.attr,
 	NULL,
 };
-ATTRIBUTE_GROUPS(devfreq);
 
 static ssize_t polling_interval_show(struct device *dev,
 				     struct device_attribute *attr, char *buf)
 {
 	struct devfreq *df = to_devfreq(dev);
+	int ret;
 
-	if (!df->profile)
+	guard(mutex)(&devfreq_list_lock);
+
+	if (!df->profile || !df->governor ||
+	    !IS_SUPPORTED_ATTR(df->governor->attrs, POLLING_INTERVAL))
 		return -EINVAL;
 
-	return sprintf(buf, "%d\n", df->profile->polling_ms);
+	ret = sprintf(buf, "%d\n", df->profile->polling_ms);
+
+	return ret;
 }
 
 static ssize_t polling_interval_store(struct device *dev,
@@ -1828,7 +1829,10 @@ static ssize_t polling_interval_store(struct device *dev,
 	unsigned int value;
 	int ret;
 
-	if (!df->governor)
+	guard(mutex)(&devfreq_list_lock);
+
+	if (!df->governor ||
+	    !IS_SUPPORTED_ATTR(df->governor->attrs, POLLING_INTERVAL))
 		return -EINVAL;
 
 	ret = sscanf(buf, "%u", &value);
@@ -1847,7 +1851,10 @@ static ssize_t timer_show(struct device *dev,
 {
 	struct devfreq *df = to_devfreq(dev);
 
-	if (!df->profile)
+	guard(mutex)(&devfreq_list_lock);
+
+	if (!df->profile || !df->governor ||
+	    !IS_SUPPORTED_ATTR(df->governor->attrs, TIMER))
 		return -EINVAL;
 
 	return sprintf(buf, "%s\n", timer_name[df->profile->timer]);
@@ -1861,7 +1868,10 @@ static ssize_t timer_store(struct device *dev, struct device_attribute *attr,
 	int timer = -1;
 	int ret = 0, i;
 
-	if (!df->governor || !df->profile)
+	guard(mutex)(&devfreq_list_lock);
+
+	if (!df->governor || !df->profile ||
+	    !IS_SUPPORTED_ATTR(df->governor->attrs, TIMER))
 		return -EINVAL;
 
 	ret = sscanf(buf, "%16s", str_timer);
@@ -1905,36 +1915,54 @@ static ssize_t timer_store(struct device *dev, struct device_attribute *attr,
 }
 static DEVICE_ATTR_RW(timer);
 
-#define CREATE_SYSFS_FILE(df, name)					\
-{									\
-	int ret;							\
-	ret = sysfs_create_file(&df->dev.kobj, &dev_attr_##name.attr);	\
-	if (ret < 0) {							\
-		dev_warn(&df->dev,					\
-			"Unable to create attr(%s)\n", "##name");	\
-	}								\
-}									\
+static struct attribute *governor_attrs[] = {
+	&dev_attr_polling_interval.attr,
+	&dev_attr_timer.attr,
+	NULL
+};
 
-/* Create the specific sysfs files which depend on each governor. */
-static void create_sysfs_files(struct devfreq *devfreq,
-				const struct devfreq_governor *gov)
+static umode_t gov_attr_visible(struct kobject *kobj,
+				struct attribute *attr, int n)
 {
-	if (IS_SUPPORTED_ATTR(gov->attrs, POLLING_INTERVAL))
-		CREATE_SYSFS_FILE(devfreq, polling_interval);
-	if (IS_SUPPORTED_ATTR(gov->attrs, TIMER))
-		CREATE_SYSFS_FILE(devfreq, timer);
+	struct device *dev = kobj_to_dev(kobj);
+	struct devfreq *df = to_devfreq(dev);
+
+	if (!df->governor || !df->governor->attrs)
+		return 0;
+
+	if (IS_SUPPORTED_ATTR(df->governor->attrs, POLLING_INTERVAL))
+		return attr->mode;
+	if (IS_SUPPORTED_ATTR(df->governor->attrs, TIMER))
+		return attr->mode;
+
+	return 0;
 }
 
-/* Remove the specific sysfs files which depend on each governor. */
-static void remove_sysfs_files(struct devfreq *devfreq,
-				const struct devfreq_governor *gov)
+static bool gov_group_visible(struct kobject *kobj)
 {
-	if (IS_SUPPORTED_ATTR(gov->attrs, POLLING_INTERVAL))
-		sysfs_remove_file(&devfreq->dev.kobj,
-				&dev_attr_polling_interval.attr);
-	if (IS_SUPPORTED_ATTR(gov->attrs, TIMER))
-		sysfs_remove_file(&devfreq->dev.kobj, &dev_attr_timer.attr);
+	struct device *dev = kobj_to_dev(kobj);
+
+	if (!dev)
+		return false;
+
+	return true;
 }
+DEFINE_SYSFS_GROUP_VISIBLE(gov);
+
+static const struct attribute_group devfreq_group = {
+	.attrs = devfreq_attrs,
+};
+
+static const struct attribute_group gov_attr_group = {
+	.attrs = governor_attrs,
+	.is_visible = SYSFS_GROUP_VISIBLE(gov),
+};
+
+static const struct attribute_group *devfreq_groups[] = {
+	&devfreq_group,
+	&gov_attr_group,
+	NULL
+};
 
 /**
  * devfreq_summary_show() - Show the summary of the devfreq devices
-- 
2.33.0
Re: [PATCH v3] PM / devfreq: use _visible attribute to replace create/remove_sysfs_files()
Posted by Jie Zhan 2 weeks, 1 day ago

On 11/7/2025 11:17 AM, Pengjie Zhang wrote:
> Previously, non-generic attributes (polling_interval, timer) used separate 
> create/delete logic, leading to race conditions during concurrent access in
> creation/deletion. Multi-threaded operations also caused inconsistencies   
> between governor capabilities and attribute states.                        
>                                                                            
> 1.Use is_visible + sysfs_update_group() to unify management of these       
> attributes, eliminating creation/deletion races.                           
> 2.Add locks and validation to these attributes, ensuring consistency       
> between current governor capabilities and attribute operations in          
> multi-threaded environments.                                               
>                                                                            
> Signed-off-by: Pengjie Zhang <zhangpengjie2@huawei.com>                    
Hi Pengjie,

The motivation looks fine but the implementation of gov_group_visible() and
gov_attr_visible() doesn't work right in my view.

See comments below.

Thanks!
Jie
> ---                                                                        
> changes in v3:                                                             
> - Use guard() to simplify lock acquisition and destruction.                
> - Eliminate redundant checks for df.                                       
> Link to v2:https://lore.kernel.org/lkml/20251028022458.2824872-1-zhangpengjie2@huawei.com/
>                                                                            
> Changes in v2:                                                             
> - Fix one problem reported by the kernel test robot.                       
> - Redirect all error paths in timer_store() to out to ensure locks are not 
>  left unReleased.                                                          
> Link to v1:https://lore.kernel.org/lkml/20251025135238.3576861-1-zhangpengjie2@huawei.com/
> 
>  drivers/devfreq/devfreq.c | 108 ++++++++++++++++++++++++--------------
>  1 file changed, 68 insertions(+), 40 deletions(-)
> 
> diff --git a/drivers/devfreq/devfreq.c b/drivers/devfreq/devfreq.c
> index 2e8d01d47f69..674b794d553d 100644
> --- a/drivers/devfreq/devfreq.c
> +++ b/drivers/devfreq/devfreq.c
> @@ -38,6 +38,7 @@
>  
>  static struct class *devfreq_class;
>  static struct dentry *devfreq_debugfs;
> +static const struct attribute_group gov_attr_group;
>  
>  /*
>   * devfreq core provides delayed work based load monitoring helper
> @@ -785,11 +786,6 @@ static void devfreq_dev_release(struct device *dev)
>  	kfree(devfreq);
>  }
>  
> -static void create_sysfs_files(struct devfreq *devfreq,
> -				const struct devfreq_governor *gov);
> -static void remove_sysfs_files(struct devfreq *devfreq,
> -				const struct devfreq_governor *gov);
> -
>  /**
>   * devfreq_add_device() - Add devfreq feature to the device
>   * @dev:	the device to add devfreq feature.
> @@ -956,7 +952,10 @@ struct devfreq *devfreq_add_device(struct device *dev,
>  			 __func__);
>  		goto err_init;
>  	}
> -	create_sysfs_files(devfreq, devfreq->governor);
> +
> +	err = sysfs_update_group(&devfreq->dev.kobj, &gov_attr_group);
> +	if (err)
> +		goto err_init;
>  
>  	list_add(&devfreq->node, &devfreq_list);
>  
> @@ -998,9 +997,7 @@ int devfreq_remove_device(struct devfreq *devfreq)
>  	if (devfreq->governor) {
>  		devfreq->governor->event_handler(devfreq,
>  						 DEVFREQ_GOV_STOP, NULL);
> -		remove_sysfs_files(devfreq, devfreq->governor);
>  	}
> -
>  	device_unregister(&devfreq->dev);
>  
>  	return 0;
> @@ -1460,7 +1457,6 @@ static ssize_t governor_store(struct device *dev, struct device_attribute *attr,
>  			 __func__, df->governor->name, ret);
>  		goto out;
>  	}
> -	remove_sysfs_files(df, df->governor);
>  
>  	/*
>  	 * Start the new governor and create the specific sysfs files
> @@ -1489,7 +1485,7 @@ static ssize_t governor_store(struct device *dev, struct device_attribute *attr,
>  	 * Create the sysfs files for the new governor. But if failed to start
>  	 * the new governor, restore the sysfs files of previous governor.
>  	 */
> -	create_sysfs_files(df, df->governor);
> +	ret = sysfs_update_group(&df->dev.kobj, &gov_attr_group);
>  
>  out:
>  	mutex_unlock(&devfreq_list_lock);
> @@ -1807,17 +1803,22 @@ static struct attribute *devfreq_attrs[] = {
>  	&dev_attr_trans_stat.attr,
>  	NULL,
>  };
> -ATTRIBUTE_GROUPS(devfreq);
>  
>  static ssize_t polling_interval_show(struct device *dev,
>  				     struct device_attribute *attr, char *buf)
>  {
>  	struct devfreq *df = to_devfreq(dev);
> +	int ret;
No need of this if restore the last line.
>  
> -	if (!df->profile)
> +	guard(mutex)(&devfreq_list_lock);
> +
> +	if (!df->profile || !df->governor ||
> +	    !IS_SUPPORTED_ATTR(df->governor->attrs, POLLING_INTERVAL))
>  		return -EINVAL;
>  
> -	return sprintf(buf, "%d\n", df->profile->polling_ms);
It's fine to keep this line.
> +	ret = sprintf(buf, "%d\n", df->profile->polling_ms);
> +
> +	return ret;
>  }
>  
>  static ssize_t polling_interval_store(struct device *dev,
> @@ -1828,7 +1829,10 @@ static ssize_t polling_interval_store(struct device *dev,
>  	unsigned int value;
>  	int ret;
>  
> -	if (!df->governor)
> +	guard(mutex)(&devfreq_list_lock);
> +
> +	if (!df->governor ||
> +	    !IS_SUPPORTED_ATTR(df->governor->attrs, POLLING_INTERVAL))
>  		return -EINVAL;
>  
>  	ret = sscanf(buf, "%u", &value);
> @@ -1847,7 +1851,10 @@ static ssize_t timer_show(struct device *dev,
>  {
>  	struct devfreq *df = to_devfreq(dev);
>  
> -	if (!df->profile)
> +	guard(mutex)(&devfreq_list_lock);
> +
> +	if (!df->profile || !df->governor ||
> +	    !IS_SUPPORTED_ATTR(df->governor->attrs, TIMER))
>  		return -EINVAL;
>  
>  	return sprintf(buf, "%s\n", timer_name[df->profile->timer]);
> @@ -1861,7 +1868,10 @@ static ssize_t timer_store(struct device *dev, struct device_attribute *attr,
>  	int timer = -1;
>  	int ret = 0, i;
>  
> -	if (!df->governor || !df->profile)
> +	guard(mutex)(&devfreq_list_lock);
> +
> +	if (!df->governor || !df->profile ||
> +	    !IS_SUPPORTED_ATTR(df->governor->attrs, TIMER))
>  		return -EINVAL;
>  
>  	ret = sscanf(buf, "%16s", str_timer);
> @@ -1905,36 +1915,54 @@ static ssize_t timer_store(struct device *dev, struct device_attribute *attr,
>  }
>  static DEVICE_ATTR_RW(timer);
>  
> -#define CREATE_SYSFS_FILE(df, name)					\
> -{									\
> -	int ret;							\
> -	ret = sysfs_create_file(&df->dev.kobj, &dev_attr_##name.attr);	\
> -	if (ret < 0) {							\
> -		dev_warn(&df->dev,					\
> -			"Unable to create attr(%s)\n", "##name");	\
> -	}								\
> -}									\
> +static struct attribute *governor_attrs[] = {
> +	&dev_attr_polling_interval.attr,
> +	&dev_attr_timer.attr,
> +	NULL
> +};
>  
> -/* Create the specific sysfs files which depend on each governor. */
> -static void create_sysfs_files(struct devfreq *devfreq,
> -				const struct devfreq_governor *gov)
> +static umode_t gov_attr_visible(struct kobject *kobj,
> +				struct attribute *attr, int n)
>  {
> -	if (IS_SUPPORTED_ATTR(gov->attrs, POLLING_INTERVAL))
> -		CREATE_SYSFS_FILE(devfreq, polling_interval);
> -	if (IS_SUPPORTED_ATTR(gov->attrs, TIMER))
> -		CREATE_SYSFS_FILE(devfreq, timer);
> +	struct device *dev = kobj_to_dev(kobj);
> +	struct devfreq *df = to_devfreq(dev);
> +
> +	if (!df->governor || !df->governor->attrs)
> +		return 0;
> +
> +	if (IS_SUPPORTED_ATTR(df->governor->attrs, POLLING_INTERVAL))
> +		return attr->mode;
> +	if (IS_SUPPORTED_ATTR(df->governor->attrs, TIMER))
> +		return attr->mode;
This would expose both 'timer' and 'polling_interval' if either of them is
supported, which is wrong.
> +
> +	return 0;
>  }
>  
> -/* Remove the specific sysfs files which depend on each governor. */
> -static void remove_sysfs_files(struct devfreq *devfreq,
> -				const struct devfreq_governor *gov)
> +static bool gov_group_visible(struct kobject *kobj)
>  {
> -	if (IS_SUPPORTED_ATTR(gov->attrs, POLLING_INTERVAL))
> -		sysfs_remove_file(&devfreq->dev.kobj,
> -				&dev_attr_polling_interval.attr);
> -	if (IS_SUPPORTED_ATTR(gov->attrs, TIMER))
> -		sysfs_remove_file(&devfreq->dev.kobj, &dev_attr_timer.attr);
> +	struct device *dev = kobj_to_dev(kobj);
> +
> +	if (!dev)
> +		return false;
kobj_to_dev(kobj) can't fail.  This check is unnecessary.
> +
> +	return true;
>  }
> +DEFINE_SYSFS_GROUP_VISIBLE(gov);
The implementation of gov_group_visible() and gov_attr_visible() doesn't
seem to comply with the design of DEFINE_SYSFS_GROUP_VISIBLE().

DEFINE_SYSFS_GROUP_VISIBLE is defined as:

#define DEFINE_SYSFS_GROUP_VISIBLE(name)                             \
        static inline umode_t sysfs_group_visible_##name(            \
                struct kobject *kobj, struct attribute *attr, int n) \
        {                                                            \
                if (n == 0 && !name##_group_visible(kobj))           \
                        return SYSFS_GROUP_INVISIBLE;                \
                return name##_attr_visible(kobj, attr, n);           \
        }

That means:
_group_visible controls whether to hide all the attrs in this group;
_attr_visible further decides whether to show each attr.

Hence,
1. gov_group_visible() should check if any attr in 'governor_attrs' should
be present, i.e. checking df->governor->attrs.
2. gov_attr_visible identifies which attr it is and checks whether the
governor supports it.

Neither is done in the above code, so this should be updated.
> +
> +static const struct attribute_group devfreq_group = {
> +	.attrs = devfreq_attrs,
> +};
> +
> +static const struct attribute_group gov_attr_group = {
> +	.attrs = governor_attrs,
> +	.is_visible = SYSFS_GROUP_VISIBLE(gov),
> +};
> +
> +static const struct attribute_group *devfreq_groups[] = {
> +	&devfreq_group,
> +	&gov_attr_group,
> +	NULL
> +};
>  
>  /**
>   * devfreq_summary_show() - Show the summary of the devfreq devices
Re: [PATCH v3] PM / devfreq: use _visible attribute to replace create/remove_sysfs_files()
Posted by zhangpengjie (A) 2 weeks, 1 day ago
hello,Jie

On 12/4/2025 3:36 PM, Jie Zhan wrote:
> On 11/7/2025 11:17 AM, Pengjie Zhang wrote:
>> Previously, non-generic attributes (polling_interval, timer) used separate
>> create/delete logic, leading to race conditions during concurrent access in
>> creation/deletion. Multi-threaded operations also caused inconsistencies
>> between governor capabilities and attribute states.
>>                                                                             
>> 1.Use is_visible + sysfs_update_group() to unify management of these
>> attributes, eliminating creation/deletion races.
>> 2.Add locks and validation to these attributes, ensuring consistency
>> between current governor capabilities and attribute operations in
>> multi-threaded environments.
>>                                                                             
>> Signed-off-by: Pengjie Zhang <zhangpengjie2@huawei.com>
> Hi Pengjie,
>
> The motivation looks fine but the implementation of gov_group_visible() and
> gov_attr_visible() doesn't work right in my view.
>
> See comments below.
>
> Thanks!
> Jie
>>   /*
>>    * devfreq core provides delayed work based load monitoring helper
>> @@ -785,11 +786,6 @@ static void devfreq_dev_release(struct device *dev)
>>   	kfree(devfreq);
>>   }
>>   
>>   
>>   static ssize_t polling_interval_show(struct device *dev,
>>   				     struct device_attribute *attr, char *buf)
>>   {
>>   	struct devfreq *df = to_devfreq(dev);
>> +	int ret;
> No need of this if restore the last line.
>>   
>> -	if (!df->profile)
>> +	guard(mutex)(&devfreq_list_lock);
>> +
>> +	if (!df->profile || !df->governor ||
>> +	    !IS_SUPPORTED_ATTR(df->governor->attrs, POLLING_INTERVAL))
>>   		return -EINVAL;
>>   
>> -	return sprintf(buf, "%d\n", df->profile->polling_ms);
> It's fine to keep this line.
>> +	ret = sprintf(buf, "%d\n", df->profile->polling_ms);
>> +
>> +	return ret;
>>   }
>>   
>>
>>   
>> -/* Create the specific sysfs files which depend on each governor. */
>> -static void create_sysfs_files(struct devfreq *devfreq,
>> -				const struct devfreq_governor *gov)
>> +static umode_t gov_attr_visible(struct kobject *kobj,
>> +				struct attribute *attr, int n)
>>   {
>> -	if (IS_SUPPORTED_ATTR(gov->attrs, POLLING_INTERVAL))
>> -		CREATE_SYSFS_FILE(devfreq, polling_interval);
>> -	if (IS_SUPPORTED_ATTR(gov->attrs, TIMER))
>> -		CREATE_SYSFS_FILE(devfreq, timer);
>> +	struct device *dev = kobj_to_dev(kobj);
>> +	struct devfreq *df = to_devfreq(dev);
>> +
>> +	if (!df->governor || !df->governor->attrs)
>> +		return 0;
>> +
>> +	if (IS_SUPPORTED_ATTR(df->governor->attrs, POLLING_INTERVAL))
>> +		return attr->mode;
>> +	if (IS_SUPPORTED_ATTR(df->governor->attrs, TIMER))
>> +		return attr->mode;
> This would expose both 'timer' and 'polling_interval' if either of them is
> supported, which is wrong.

ok,i see,may be

     if (attr == &dev_attr_polling_interval.attr) {
         if (IS_SUPPORTED_ATTR(df->governor->attrs, POLLING_INTERVAL))
             return attr->mode;
         return 0;
     }

     if (attr == &dev_attr_timer.attr) {
         if (IS_SUPPORTED_ATTR(df->governor->attrs, TIMER))
             return attr->mode;
         return 0;
     }

>> +
>> +	return 0;
>>   }
>>   
>> -/* Remove the specific sysfs files which depend on each governor. */
>> -static void remove_sysfs_files(struct devfreq *devfreq,
>> -				const struct devfreq_governor *gov)
>> +static bool gov_group_visible(struct kobject *kobj)
>>   {
>> -	if (IS_SUPPORTED_ATTR(gov->attrs, POLLING_INTERVAL))
>> -		sysfs_remove_file(&devfreq->dev.kobj,
>> -				&dev_attr_polling_interval.attr);
>> -	if (IS_SUPPORTED_ATTR(gov->attrs, TIMER))
>> -		sysfs_remove_file(&devfreq->dev.kobj, &dev_attr_timer.attr);
>> +	struct device *dev = kobj_to_dev(kobj);
>> +
>> +	if (!dev)
>> +		return false;
> kobj_to_dev(kobj) can't fail.  This check is unnecessary.
>> +
>> +	return true;
>>   }
>> +DEFINE_SYSFS_GROUP_VISIBLE(gov);
> The implementation of gov_group_visible() and gov_attr_visible() doesn't
> seem to comply with the design of DEFINE_SYSFS_GROUP_VISIBLE().
>
> DEFINE_SYSFS_GROUP_VISIBLE is defined as:
>
> #define DEFINE_SYSFS_GROUP_VISIBLE(name)                             \
>          static inline umode_t sysfs_group_visible_##name(            \
>                  struct kobject *kobj, struct attribute *attr, int n) \
>          {                                                            \
>                  if (n == 0 && !name##_group_visible(kobj))           \
>                          return SYSFS_GROUP_INVISIBLE;                \
>                  return name##_attr_visible(kobj, attr, n);           \
>          }
>
> That means:
> _group_visible controls whether to hide all the attrs in this group;
> _attr_visible further decides whether to show each attr.
>
> Hence,
> 1. gov_group_visible() should check if any attr in 'governor_attrs' should
> be present, i.e. checking df->governor->attrs.
> 2. gov_attr_visible identifies which attr it is and checks whether the
> governor supports it.
>
> Neither is done in the above code, so this should be updated.

we can move the check|if (!df->governor || !df->governor->attrs)|

from|gov_attr_visible|to|gov_group_visible|.

thanks !

Re: [PATCH v3] PM / devfreq: use _visible attribute to replace create/remove_sysfs_files()
Posted by Jie Zhan 2 weeks, 1 day ago

...

>>> +static umode_t gov_attr_visible(struct kobject *kobj,
>>> +                struct attribute *attr, int n)
>>>   {
>>> -    if (IS_SUPPORTED_ATTR(gov->attrs, POLLING_INTERVAL))
>>> -        CREATE_SYSFS_FILE(devfreq, polling_interval);
>>> -    if (IS_SUPPORTED_ATTR(gov->attrs, TIMER))
>>> -        CREATE_SYSFS_FILE(devfreq, timer);
>>> +    struct device *dev = kobj_to_dev(kobj);
>>> +    struct devfreq *df = to_devfreq(dev);
>>> +
>>> +    if (!df->governor || !df->governor->attrs)
>>> +        return 0;
>>> +
>>> +    if (IS_SUPPORTED_ATTR(df->governor->attrs, POLLING_INTERVAL))
>>> +        return attr->mode;
>>> +    if (IS_SUPPORTED_ATTR(df->governor->attrs, TIMER))
>>> +        return attr->mode;
>> This would expose both 'timer' and 'polling_interval' if either of them is
>> supported, which is wrong.
> 
> ok,i see,may be
> 
>     if (attr == &dev_attr_polling_interval.attr) {
>         if (IS_SUPPORTED_ATTR(df->governor->attrs, POLLING_INTERVAL))
>             return attr->mode;
>         return 0;
>     }
> 
>     if (attr == &dev_attr_timer.attr) {
>         if (IS_SUPPORTED_ATTR(df->governor->attrs, TIMER))
>             return attr->mode;
>         return 0;
>     }
> 
Yeah.

Could be cleaner like:
	if (attr == &dev_attr_polling_interval.attr &&
	    IS_SUPPORTED_ATTR(df->governor->attrs, POLLING_INTERVAL))
		return attr->mode;

	if (attr == &dev_attr_timer.attr &&
	    IS_SUPPORTED_ATTR(df->governor->attrs, TIMER))
		return attr->mode;

	return 0;
}
>>> +
>>> +    return 0;
>>>   }
>>>   -/* Remove the specific sysfs files which depend on each governor. */
>>> -static void remove_sysfs_files(struct devfreq *devfreq,
>>> -                const struct devfreq_governor *gov)
>>> +static bool gov_group_visible(struct kobject *kobj)
>>>   {
>>> -    if (IS_SUPPORTED_ATTR(gov->attrs, POLLING_INTERVAL))
>>> -        sysfs_remove_file(&devfreq->dev.kobj,
>>> -                &dev_attr_polling_interval.attr);
>>> -    if (IS_SUPPORTED_ATTR(gov->attrs, TIMER))
>>> -        sysfs_remove_file(&devfreq->dev.kobj, &dev_attr_timer.attr);
>>> +    struct device *dev = kobj_to_dev(kobj);
>>> +
>>> +    if (!dev)
>>> +        return false;
>> kobj_to_dev(kobj) can't fail.  This check is unnecessary.
>>> +
>>> +    return true;
>>>   }
>>> +DEFINE_SYSFS_GROUP_VISIBLE(gov);
>> The implementation of gov_group_visible() and gov_attr_visible() doesn't
>> seem to comply with the design of DEFINE_SYSFS_GROUP_VISIBLE().
>>
>> DEFINE_SYSFS_GROUP_VISIBLE is defined as:
>>
>> #define DEFINE_SYSFS_GROUP_VISIBLE(name)                             \
>>          static inline umode_t sysfs_group_visible_##name(            \
>>                  struct kobject *kobj, struct attribute *attr, int n) \
>>          {                                                            \
>>                  if (n == 0 && !name##_group_visible(kobj))           \
>>                          return SYSFS_GROUP_INVISIBLE;                \
>>                  return name##_attr_visible(kobj, attr, n);           \
>>          }
>>
>> That means:
>> _group_visible controls whether to hide all the attrs in this group;
>> _attr_visible further decides whether to show each attr.
>>
>> Hence,
>> 1. gov_group_visible() should check if any attr in 'governor_attrs' should
>> be present, i.e. checking df->governor->attrs.
>> 2. gov_attr_visible identifies which attr it is and checks whether the
>> governor supports it.
>>
>> Neither is done in the above code, so this should be updated.
> 
> we can move the check|if (!df->governor || !df->governor->attrs)|
> 
> from|gov_attr_visible|to|gov_group_visible|.
Exactly.
> 
> thanks !
> 
Re: [PATCH v3] PM / devfreq: use _visible attribute to replace create/remove_sysfs_files()
Posted by zhangpengjie (A) 4 weeks ago
Gentle ping .

Thanks!


On 11/7/2025 11:17 AM, Pengjie Zhang wrote:
> Previously, non-generic attributes (polling_interval, timer) used separate
> create/delete logic, leading to race conditions during concurrent access in
> creation/deletion. Multi-threaded operations also caused inconsistencies
> between governor capabilities and attribute states.
>                                                                             
> 1.Use is_visible + sysfs_update_group() to unify management of these
> attributes, eliminating creation/deletion races.
> 2.Add locks and validation to these attributes, ensuring consistency
> between current governor capabilities and attribute operations in
> multi-threaded environments.
>                                                                             
> Signed-off-by: Pengjie Zhang <zhangpengjie2@huawei.com>
> ---
> changes in v3:
> - Use guard() to simplify lock acquisition and destruction.
> - Eliminate redundant checks for df.
> Link to v2:https://lore.kernel.org/lkml/20251028022458.2824872-1-zhangpengjie2@huawei.com/
>                                                                             
> Changes in v2:
> - Fix one problem reported by the kernel test robot.
> - Redirect all error paths in timer_store() to out to ensure locks are not
>   left unReleased.
> Link to v1:https://lore.kernel.org/lkml/20251025135238.3576861-1-zhangpengjie2@huawei.com/
>
>   drivers/devfreq/devfreq.c | 108 ++++++++++++++++++++++++--------------
>   1 file changed, 68 insertions(+), 40 deletions(-)
>
> diff --git a/drivers/devfreq/devfreq.c b/drivers/devfreq/devfreq.c
> index 2e8d01d47f69..674b794d553d 100644
> --- a/drivers/devfreq/devfreq.c
> +++ b/drivers/devfreq/devfreq.c
> @@ -38,6 +38,7 @@
>   
>   static struct class *devfreq_class;
>   static struct dentry *devfreq_debugfs;
> +static const struct attribute_group gov_attr_group;
>   
>   /*
>    * devfreq core provides delayed work based load monitoring helper
> @@ -785,11 +786,6 @@ static void devfreq_dev_release(struct device *dev)
>   	kfree(devfreq);
>   }
>   
> -static void create_sysfs_files(struct devfreq *devfreq,
> -				const struct devfreq_governor *gov);
> -static void remove_sysfs_files(struct devfreq *devfreq,
> -				const struct devfreq_governor *gov);
> -
>   /**
>    * devfreq_add_device() - Add devfreq feature to the device
>    * @dev:	the device to add devfreq feature.
> @@ -956,7 +952,10 @@ struct devfreq *devfreq_add_device(struct device *dev,
>   			 __func__);
>   		goto err_init;
>   	}
> -	create_sysfs_files(devfreq, devfreq->governor);
> +
> +	err = sysfs_update_group(&devfreq->dev.kobj, &gov_attr_group);
> +	if (err)
> +		goto err_init;
>   
>   	list_add(&devfreq->node, &devfreq_list);
>   
> @@ -998,9 +997,7 @@ int devfreq_remove_device(struct devfreq *devfreq)
>   	if (devfreq->governor) {
>   		devfreq->governor->event_handler(devfreq,
>   						 DEVFREQ_GOV_STOP, NULL);
> -		remove_sysfs_files(devfreq, devfreq->governor);
>   	}
> -
>   	device_unregister(&devfreq->dev);
>   
>   	return 0;
> @@ -1460,7 +1457,6 @@ static ssize_t governor_store(struct device *dev, struct device_attribute *attr,
>   			 __func__, df->governor->name, ret);
>   		goto out;
>   	}
> -	remove_sysfs_files(df, df->governor);
>   
>   	/*
>   	 * Start the new governor and create the specific sysfs files
> @@ -1489,7 +1485,7 @@ static ssize_t governor_store(struct device *dev, struct device_attribute *attr,
>   	 * Create the sysfs files for the new governor. But if failed to start
>   	 * the new governor, restore the sysfs files of previous governor.
>   	 */
> -	create_sysfs_files(df, df->governor);
> +	ret = sysfs_update_group(&df->dev.kobj, &gov_attr_group);
>   
>   out:
>   	mutex_unlock(&devfreq_list_lock);
> @@ -1807,17 +1803,22 @@ static struct attribute *devfreq_attrs[] = {
>   	&dev_attr_trans_stat.attr,
>   	NULL,
>   };
> -ATTRIBUTE_GROUPS(devfreq);
>   
>   static ssize_t polling_interval_show(struct device *dev,
>   				     struct device_attribute *attr, char *buf)
>   {
>   	struct devfreq *df = to_devfreq(dev);
> +	int ret;
>   
> -	if (!df->profile)
> +	guard(mutex)(&devfreq_list_lock);
> +
> +	if (!df->profile || !df->governor ||
> +	    !IS_SUPPORTED_ATTR(df->governor->attrs, POLLING_INTERVAL))
>   		return -EINVAL;
>   
> -	return sprintf(buf, "%d\n", df->profile->polling_ms);
> +	ret = sprintf(buf, "%d\n", df->profile->polling_ms);
> +
> +	return ret;
>   }
>   
>   static ssize_t polling_interval_store(struct device *dev,
> @@ -1828,7 +1829,10 @@ static ssize_t polling_interval_store(struct device *dev,
>   	unsigned int value;
>   	int ret;
>   
> -	if (!df->governor)
> +	guard(mutex)(&devfreq_list_lock);
> +
> +	if (!df->governor ||
> +	    !IS_SUPPORTED_ATTR(df->governor->attrs, POLLING_INTERVAL))
>   		return -EINVAL;
>   
>   	ret = sscanf(buf, "%u", &value);
> @@ -1847,7 +1851,10 @@ static ssize_t timer_show(struct device *dev,
>   {
>   	struct devfreq *df = to_devfreq(dev);
>   
> -	if (!df->profile)
> +	guard(mutex)(&devfreq_list_lock);
> +
> +	if (!df->profile || !df->governor ||
> +	    !IS_SUPPORTED_ATTR(df->governor->attrs, TIMER))
>   		return -EINVAL;
>   
>   	return sprintf(buf, "%s\n", timer_name[df->profile->timer]);
> @@ -1861,7 +1868,10 @@ static ssize_t timer_store(struct device *dev, struct device_attribute *attr,
>   	int timer = -1;
>   	int ret = 0, i;
>   
> -	if (!df->governor || !df->profile)
> +	guard(mutex)(&devfreq_list_lock);
> +
> +	if (!df->governor || !df->profile ||
> +	    !IS_SUPPORTED_ATTR(df->governor->attrs, TIMER))
>   		return -EINVAL;
>   
>   	ret = sscanf(buf, "%16s", str_timer);
> @@ -1905,36 +1915,54 @@ static ssize_t timer_store(struct device *dev, struct device_attribute *attr,
>   }
>   static DEVICE_ATTR_RW(timer);
>   
> -#define CREATE_SYSFS_FILE(df, name)					\
> -{									\
> -	int ret;							\
> -	ret = sysfs_create_file(&df->dev.kobj, &dev_attr_##name.attr);	\
> -	if (ret < 0) {							\
> -		dev_warn(&df->dev,					\
> -			"Unable to create attr(%s)\n", "##name");	\
> -	}								\
> -}									\
> +static struct attribute *governor_attrs[] = {
> +	&dev_attr_polling_interval.attr,
> +	&dev_attr_timer.attr,
> +	NULL
> +};
>   
> -/* Create the specific sysfs files which depend on each governor. */
> -static void create_sysfs_files(struct devfreq *devfreq,
> -				const struct devfreq_governor *gov)
> +static umode_t gov_attr_visible(struct kobject *kobj,
> +				struct attribute *attr, int n)
>   {
> -	if (IS_SUPPORTED_ATTR(gov->attrs, POLLING_INTERVAL))
> -		CREATE_SYSFS_FILE(devfreq, polling_interval);
> -	if (IS_SUPPORTED_ATTR(gov->attrs, TIMER))
> -		CREATE_SYSFS_FILE(devfreq, timer);
> +	struct device *dev = kobj_to_dev(kobj);
> +	struct devfreq *df = to_devfreq(dev);
> +
> +	if (!df->governor || !df->governor->attrs)
> +		return 0;
> +
> +	if (IS_SUPPORTED_ATTR(df->governor->attrs, POLLING_INTERVAL))
> +		return attr->mode;
> +	if (IS_SUPPORTED_ATTR(df->governor->attrs, TIMER))
> +		return attr->mode;
> +
> +	return 0;
>   }
>   
> -/* Remove the specific sysfs files which depend on each governor. */
> -static void remove_sysfs_files(struct devfreq *devfreq,
> -				const struct devfreq_governor *gov)
> +static bool gov_group_visible(struct kobject *kobj)
>   {
> -	if (IS_SUPPORTED_ATTR(gov->attrs, POLLING_INTERVAL))
> -		sysfs_remove_file(&devfreq->dev.kobj,
> -				&dev_attr_polling_interval.attr);
> -	if (IS_SUPPORTED_ATTR(gov->attrs, TIMER))
> -		sysfs_remove_file(&devfreq->dev.kobj, &dev_attr_timer.attr);
> +	struct device *dev = kobj_to_dev(kobj);
> +
> +	if (!dev)
> +		return false;
> +
> +	return true;
>   }
> +DEFINE_SYSFS_GROUP_VISIBLE(gov);
> +
> +static const struct attribute_group devfreq_group = {
> +	.attrs = devfreq_attrs,
> +};
> +
> +static const struct attribute_group gov_attr_group = {
> +	.attrs = governor_attrs,
> +	.is_visible = SYSFS_GROUP_VISIBLE(gov),
> +};
> +
> +static const struct attribute_group *devfreq_groups[] = {
> +	&devfreq_group,
> +	&gov_attr_group,
> +	NULL
> +};
>   
>   /**
>    * devfreq_summary_show() - Show the summary of the devfreq devices