linux-pm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] PM / devfreq: use _visible attribute to replace create/remove_sysfs_files()
@ 2025-10-28  2:24 Pengjie Zhang
  2025-10-31 14:41 ` Jonathan Cameron
  0 siblings, 1 reply; 3+ messages in thread
From: Pengjie Zhang @ 2025-10-28  2:24 UTC (permalink / raw)
  To: myungjoo.ham, kyungmin.park, cw00.choi
  Cc: linux-pm, linux-kernel, zhanjie9, zhenglifeng1, lihuisong,
	yubowen8, linhongye, linuxarm, jonathan.cameron, zhangpengjie2

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 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.

 drivers/devfreq/devfreq.c | 140 +++++++++++++++++++++++++-------------
 1 file changed, 94 insertions(+), 46 deletions(-)

diff --git a/drivers/devfreq/devfreq.c b/drivers/devfreq/devfreq.c
index 2e8d01d47f69..32640e376124 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);
@@ -1805,19 +1801,26 @@ static struct attribute *devfreq_attrs[] = {
 	&dev_attr_min_freq.attr,
 	&dev_attr_max_freq.attr,
 	&dev_attr_trans_stat.attr,
-	NULL,
+	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)
+	mutex_lock(&devfreq_list_lock);
+	if (!df->profile || !df->governor ||
+	    !IS_SUPPORTED_ATTR(df->governor->attrs, POLLING_INTERVAL)) {
+		mutex_unlock(&devfreq_list_lock);
 		return -EINVAL;
+	}
 
-	return sprintf(buf, "%d\n", df->profile->polling_ms);
+	ret = sprintf(buf, "%d\n", df->profile->polling_ms);
+	mutex_unlock(&devfreq_list_lock);
+
+	return ret;
 }
 
 static ssize_t polling_interval_store(struct device *dev,
@@ -1828,15 +1831,22 @@ static ssize_t polling_interval_store(struct device *dev,
 	unsigned int value;
 	int ret;
 
-	if (!df->governor)
+	mutex_lock(&devfreq_list_lock);
+	if (!df->governor ||
+	    !IS_SUPPORTED_ATTR(df->governor->attrs, POLLING_INTERVAL)) {
+		mutex_unlock(&devfreq_list_lock);
 		return -EINVAL;
+	}
 
 	ret = sscanf(buf, "%u", &value);
-	if (ret != 1)
+	if (ret != 1) {
+		mutex_unlock(&devfreq_list_lock);
 		return -EINVAL;
+	}
 
 	df->governor->event_handler(df, DEVFREQ_GOV_UPDATE_INTERVAL, &value);
 	ret = count;
+	mutex_unlock(&devfreq_list_lock);
 
 	return ret;
 }
@@ -1846,11 +1856,19 @@ static ssize_t timer_show(struct device *dev,
 			     struct device_attribute *attr, char *buf)
 {
 	struct devfreq *df = to_devfreq(dev);
+	int ret;
 
-	if (!df->profile)
+	mutex_lock(&devfreq_list_lock);
+	if (!df->profile || !df->governor ||
+	    !IS_SUPPORTED_ATTR(df->governor->attrs, TIMER)) {
+		mutex_unlock(&devfreq_list_lock);
 		return -EINVAL;
+	}
+
+	ret = sprintf(buf, "%s\n", timer_name[df->profile->timer]);
+	mutex_unlock(&devfreq_list_lock);
 
-	return sprintf(buf, "%s\n", timer_name[df->profile->timer]);
+	return ret;
 }
 
 static ssize_t timer_store(struct device *dev, struct device_attribute *attr,
@@ -1861,12 +1879,18 @@ static ssize_t timer_store(struct device *dev, struct device_attribute *attr,
 	int timer = -1;
 	int ret = 0, i;
 
-	if (!df->governor || !df->profile)
-		return -EINVAL;
+	mutex_lock(&devfreq_list_lock);
+	if (!df->governor || !df->profile ||
+	    !IS_SUPPORTED_ATTR(df->governor->attrs, TIMER)) {
+		ret = -EINVAL;
+		goto out;
+	}
 
 	ret = sscanf(buf, "%16s", str_timer);
-	if (ret != 1)
-		return -EINVAL;
+	if (ret != 1) {
+		ret = -EINVAL;
+		goto out;
+	}
 
 	for (i = 0; i < DEVFREQ_TIMER_NUM; i++) {
 		if (!strncmp(timer_name[i], str_timer, DEVFREQ_NAME_LEN)) {
@@ -1901,40 +1925,64 @@ static ssize_t timer_store(struct device *dev, struct device_attribute *attr,
 		dev_warn(dev, "%s: Governor %s not started(%d)\n",
 			 __func__, df->governor->name, ret);
 out:
+	mutex_unlock(&devfreq_list_lock);
 	return ret ? ret : count;
 }
 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);
+	struct devfreq *df;
+
+	if (!dev)
+		return false;
+
+	df = to_devfreq(dev);
+	if (!df)
+		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


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] PM / devfreq: use _visible attribute to replace create/remove_sysfs_files()
  2025-10-28  2:24 [PATCH v2] PM / devfreq: use _visible attribute to replace create/remove_sysfs_files() Pengjie Zhang
@ 2025-10-31 14:41 ` Jonathan Cameron
  2025-11-03  2:29   ` zhangpengjie (A)
  0 siblings, 1 reply; 3+ messages in thread
From: Jonathan Cameron @ 2025-10-31 14:41 UTC (permalink / raw)
  To: Pengjie Zhang
  Cc: myungjoo.ham, kyungmin.park, cw00.choi, linux-pm, linux-kernel,
	zhanjie9, zhenglifeng1, lihuisong, yubowen8, linhongye, linuxarm

On Tue, 28 Oct 2025 10:24:58 +0800
Pengjie Zhang <zhangpengjie2@huawei.com> 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

Various comments inline.

Jonathan

> @@ -1805,19 +1801,26 @@ static struct attribute *devfreq_attrs[] = {
>  	&dev_attr_min_freq.attr,
>  	&dev_attr_max_freq.attr,
>  	&dev_attr_trans_stat.attr,
> -	NULL,
> +	NULL
Whilst a good change, doesn't belong in a patch doing anything more
substantial.  So not in here.

>  };
> -ATTRIBUTE_GROUPS(devfreq);

>  static ssize_t polling_interval_store(struct device *dev,
> @@ -1828,15 +1831,22 @@ static ssize_t polling_interval_store(struct device *dev,
>  	unsigned int value;
>  	int ret;
>  
> -	if (!df->governor)
> +	mutex_lock(&devfreq_list_lock);

As below I'd use guard() to simplify this.  Applies in various other places in this
patch.


> +	if (!df->governor ||
> +	    !IS_SUPPORTED_ATTR(df->governor->attrs, POLLING_INTERVAL)) {
> +		mutex_unlock(&devfreq_list_lock);
>  		return -EINVAL;
> +	}
>  
>  	ret = sscanf(buf, "%u", &value);
> -	if (ret != 1)
> +	if (ret != 1) {
> +		mutex_unlock(&devfreq_list_lock);
>  		return -EINVAL;
> +	}
>  
>  	df->governor->event_handler(df, DEVFREQ_GOV_UPDATE_INTERVAL, &value);
>  	ret = count;
> +	mutex_unlock(&devfreq_list_lock);
>  
>  	return ret;
>  }
> @@ -1846,11 +1856,19 @@ static ssize_t timer_show(struct device *dev,
>  			     struct device_attribute *attr, char *buf)
>  {
>  	struct devfreq *df = to_devfreq(dev);
> +	int ret;
>  
> -	if (!df->profile)
> +	mutex_lock(&devfreq_list_lock);

guard() would be useful here.

> +	if (!df->profile || !df->governor ||
> +	    !IS_SUPPORTED_ATTR(df->governor->attrs, TIMER)) {
> +		mutex_unlock(&devfreq_list_lock);
>  		return -EINVAL;
> +	}
> +
> +	ret = sprintf(buf, "%s\n", timer_name[df->profile->timer]);
> +	mutex_unlock(&devfreq_list_lock);
>  
> -	return sprintf(buf, "%s\n", timer_name[df->profile->timer]);
> +	return ret;
>  }
>  
>  static ssize_t timer_store(struct device *dev, struct device_attribute *attr,
> @@ -1861,12 +1879,18 @@ static ssize_t timer_store(struct device *dev, struct device_attribute *attr,
>  	int timer = -1;
>  	int ret = 0, i;
>  
> -	if (!df->governor || !df->profile)
> -		return -EINVAL;
> +	mutex_lock(&devfreq_list_lock);

Perhaps a follow up but this code would be more readable with use of guard()
and returns wherever we are done with anything other than unlocking.


> +	if (!df->governor || !df->profile ||
> +	    !IS_SUPPORTED_ATTR(df->governor->attrs, TIMER)) {
> +		ret = -EINVAL;
> +		goto out;
> +	}
>  
>  	ret = sscanf(buf, "%16s", str_timer);
> -	if (ret != 1)
> -		return -EINVAL;
> +	if (ret != 1) {
> +		ret = -EINVAL;
> +		goto out;
> +	}
>  
>  	for (i = 0; i < DEVFREQ_TIMER_NUM; i++) {
>  		if (!strncmp(timer_name[i], str_timer, DEVFREQ_NAME_LEN)) {
> @@ -1901,40 +1925,64 @@ static ssize_t timer_store(struct device *dev, struct device_attribute *attr,
>  		dev_warn(dev, "%s: Governor %s not started(%d)\n",
>  			 __func__, df->governor->name, ret);
>  out:
> +	mutex_unlock(&devfreq_list_lock);
>  	return ret ? ret : count;
>  }
>  static DEVICE_ATTR_RW(timer);


>  
> -/* 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);
> +	struct devfreq *df;
> +
> +	if (!dev)
> +		return false;
> +
> +	df = to_devfreq(dev);
> +	if (!df)
> +		return false;

Isn't to_devfreq() just a container_of() wrapper?
If that's the case it will always be there if dev is not NULL.

As such I not seeing how this group is likely to ever be
invisible.


> +
> +	return true;
>  }
> +DEFINE_SYSFS_GROUP_VISIBLE(gov);


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] PM / devfreq: use _visible attribute to replace create/remove_sysfs_files()
  2025-10-31 14:41 ` Jonathan Cameron
@ 2025-11-03  2:29   ` zhangpengjie (A)
  0 siblings, 0 replies; 3+ messages in thread
From: zhangpengjie (A) @ 2025-11-03  2:29 UTC (permalink / raw)
  To: Jonathan Cameron
  Cc: myungjoo.ham, kyungmin.park, cw00.choi, linux-pm, linux-kernel,
	zhanjie9, zhenglifeng1, lihuisong, yubowen8, linhongye, linuxarm

On 10/31/2025 10:41 PM, Jonathan Cameron wrote:
> On Tue, 28 Oct 2025 10:24:58 +0800
> Pengjie Zhang <zhangpengjie2@huawei.com> wrote:
>
>>   static ssize_t polling_interval_store(struct device *dev,
>> @@ -1828,15 +1831,22 @@ static ssize_t polling_interval_store(struct device *dev,
>>   	unsigned int value;
>>   	int ret;
>>   
>> -	if (!df->governor)
>> +	mutex_lock(&devfreq_list_lock);
> As below I'd use guard() to simplify this.  Applies in various other places in this
> patch.
>>   
>> -/* 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);
>> +	struct devfreq *df;
>> +
>> +	if (!dev)
>> +		return false;
>> +
>> +	df = to_devfreq(dev);
>> +	if (!df)
>> +		return false;
> Isn't to_devfreq() just a container_of() wrapper?
> If that's the case it will always be there if dev is not NULL.
>
> As such I not seeing how this group is likely to ever be
> invisible.
>
Thank you for your review and comments.  I will address all your feedback

in the necessary revisions and submit the third version shortly.

Best regards,

Pengjie Zhang


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-11-03  2:29 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-10-28  2:24 [PATCH v2] PM / devfreq: use _visible attribute to replace create/remove_sysfs_files() Pengjie Zhang
2025-10-31 14:41 ` Jonathan Cameron
2025-11-03  2:29   ` zhangpengjie (A)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).