drivers/devfreq/devfreq.c | 132 +++++++++++++++++++++++++------------- 1 file changed, 89 insertions(+), 43 deletions(-)
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>
---
drivers/devfreq/devfreq.c | 132 +++++++++++++++++++++++++-------------
1 file changed, 89 insertions(+), 43 deletions(-)
diff --git a/drivers/devfreq/devfreq.c b/drivers/devfreq/devfreq.c
index 2e8d01d47f69..f67efd37f611 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,8 +1879,12 @@ static ssize_t timer_store(struct device *dev, struct device_attribute *attr,
int timer = -1;
int ret = 0, i;
- if (!df->governor || !df->profile)
+ mutex_lock(&devfreq_list_lock);
+ if (!df->governor || !df->profile ||
+ !IS_SUPPORTED_ATTR(df->governor->attrs, TIMER)) {
+ mutex_unlock(&devfreq_list_lock);
return -EINVAL;
+ }
ret = sscanf(buf, "%16s", str_timer);
if (ret != 1)
@@ -1901,40 +1923,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
Hi Pengjie,
kernel test robot noticed the following build warnings:
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Pengjie-Zhang/PM-devfreq-use-_visible-attribute-to-replace-create-remove_sysfs_files/20251025-215510
base: https://git.kernel.org/pub/scm/linux/kernel/git/chanwoo/linux.git devfreq-testing
patch link: https://lore.kernel.org/r/20251025135238.3576861-1-zhangpengjie2%40huawei.com
patch subject: [PATCH] PM / devfreq: use _visible attribute to replace create/remove_sysfs_files()
config: loongarch-randconfig-r072-20251026 (https://download.01.org/0day-ci/archive/20251027/202510272106.x7zgW7pK-lkp@intel.com/config)
compiler: clang version 22.0.0git (https://github.com/llvm/llvm-project e1ae12640102fd2b05bc567243580f90acb1135f)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
| Closes: https://lore.kernel.org/r/202510272106.x7zgW7pK-lkp@intel.com/
smatch warnings:
drivers/devfreq/devfreq.c:1927 timer_store() warn: inconsistent returns 'global &devfreq_list_lock'.
vim +1927 drivers/devfreq/devfreq.c
4dc3bab8687f1e Chanwoo Choi 2020-07-02 1874 static ssize_t timer_store(struct device *dev, struct device_attribute *attr,
4dc3bab8687f1e Chanwoo Choi 2020-07-02 1875 const char *buf, size_t count)
4dc3bab8687f1e Chanwoo Choi 2020-07-02 1876 {
4dc3bab8687f1e Chanwoo Choi 2020-07-02 1877 struct devfreq *df = to_devfreq(dev);
4dc3bab8687f1e Chanwoo Choi 2020-07-02 1878 char str_timer[DEVFREQ_NAME_LEN + 1];
4dc3bab8687f1e Chanwoo Choi 2020-07-02 1879 int timer = -1;
4dc3bab8687f1e Chanwoo Choi 2020-07-02 1880 int ret = 0, i;
4dc3bab8687f1e Chanwoo Choi 2020-07-02 1881
88ea5f098ddcf4 Pengjie Zhang 2025-10-25 1882 mutex_lock(&devfreq_list_lock);
88ea5f098ddcf4 Pengjie Zhang 2025-10-25 1883 if (!df->governor || !df->profile ||
88ea5f098ddcf4 Pengjie Zhang 2025-10-25 1884 !IS_SUPPORTED_ATTR(df->governor->attrs, TIMER)) {
88ea5f098ddcf4 Pengjie Zhang 2025-10-25 1885 mutex_unlock(&devfreq_list_lock);
4dc3bab8687f1e Chanwoo Choi 2020-07-02 1886 return -EINVAL;
88ea5f098ddcf4 Pengjie Zhang 2025-10-25 1887 }
4dc3bab8687f1e Chanwoo Choi 2020-07-02 1888
4dc3bab8687f1e Chanwoo Choi 2020-07-02 1889 ret = sscanf(buf, "%16s", str_timer);
4dc3bab8687f1e Chanwoo Choi 2020-07-02 1890 if (ret != 1)
4dc3bab8687f1e Chanwoo Choi 2020-07-02 1891 return -EINVAL;
mutex_unlock(&devfreq_list_lock);
4dc3bab8687f1e Chanwoo Choi 2020-07-02 1892
4dc3bab8687f1e Chanwoo Choi 2020-07-02 1893 for (i = 0; i < DEVFREQ_TIMER_NUM; i++) {
4dc3bab8687f1e Chanwoo Choi 2020-07-02 1894 if (!strncmp(timer_name[i], str_timer, DEVFREQ_NAME_LEN)) {
4dc3bab8687f1e Chanwoo Choi 2020-07-02 1895 timer = i;
4dc3bab8687f1e Chanwoo Choi 2020-07-02 1896 break;
4dc3bab8687f1e Chanwoo Choi 2020-07-02 1897 }
4dc3bab8687f1e Chanwoo Choi 2020-07-02 1898 }
4dc3bab8687f1e Chanwoo Choi 2020-07-02 1899
4dc3bab8687f1e Chanwoo Choi 2020-07-02 1900 if (timer < 0) {
4dc3bab8687f1e Chanwoo Choi 2020-07-02 1901 ret = -EINVAL;
4dc3bab8687f1e Chanwoo Choi 2020-07-02 1902 goto out;
4dc3bab8687f1e Chanwoo Choi 2020-07-02 1903 }
4dc3bab8687f1e Chanwoo Choi 2020-07-02 1904
4dc3bab8687f1e Chanwoo Choi 2020-07-02 1905 if (df->profile->timer == timer) {
4dc3bab8687f1e Chanwoo Choi 2020-07-02 1906 ret = 0;
4dc3bab8687f1e Chanwoo Choi 2020-07-02 1907 goto out;
4dc3bab8687f1e Chanwoo Choi 2020-07-02 1908 }
4dc3bab8687f1e Chanwoo Choi 2020-07-02 1909
4dc3bab8687f1e Chanwoo Choi 2020-07-02 1910 mutex_lock(&df->lock);
4dc3bab8687f1e Chanwoo Choi 2020-07-02 1911 df->profile->timer = timer;
4dc3bab8687f1e Chanwoo Choi 2020-07-02 1912 mutex_unlock(&df->lock);
4dc3bab8687f1e Chanwoo Choi 2020-07-02 1913
4dc3bab8687f1e Chanwoo Choi 2020-07-02 1914 ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
4dc3bab8687f1e Chanwoo Choi 2020-07-02 1915 if (ret) {
4dc3bab8687f1e Chanwoo Choi 2020-07-02 1916 dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
4dc3bab8687f1e Chanwoo Choi 2020-07-02 1917 __func__, df->governor->name, ret);
4dc3bab8687f1e Chanwoo Choi 2020-07-02 1918 goto out;
4dc3bab8687f1e Chanwoo Choi 2020-07-02 1919 }
4dc3bab8687f1e Chanwoo Choi 2020-07-02 1920
4dc3bab8687f1e Chanwoo Choi 2020-07-02 1921 ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
4dc3bab8687f1e Chanwoo Choi 2020-07-02 1922 if (ret)
4dc3bab8687f1e Chanwoo Choi 2020-07-02 1923 dev_warn(dev, "%s: Governor %s not started(%d)\n",
4dc3bab8687f1e Chanwoo Choi 2020-07-02 1924 __func__, df->governor->name, ret);
4dc3bab8687f1e Chanwoo Choi 2020-07-02 1925 out:
88ea5f098ddcf4 Pengjie Zhang 2025-10-25 1926 mutex_unlock(&devfreq_list_lock);
4dc3bab8687f1e Chanwoo Choi 2020-07-02 @1927 return ret ? ret : count;
4dc3bab8687f1e Chanwoo Choi 2020-07-02 1928 }
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
© 2016 - 2026 Red Hat, Inc.