Skip to content

Commit ef7e7d6

Browse files
guohanjunrafaeljw
authored andcommitted
cpuidle: sysfs: Accept governor name with 15 characters
CPUIDLE_NAME_LEN is 16, so it's possible to accept governor name with 15 characters, but now store_current_governor() rejects governor name with 15 characters as it returns -EINVAL if count equals CPUIDLE_NAME_LEN. Refactor the code to accept such case and simplify the code. Signed-off-by: Hanjun Guo <[email protected]> Reviewed-by: Doug Smythies <[email protected]> Tested-by: Doug Smythies <[email protected]> Signed-off-by: Rafael J. Wysocki <[email protected]>
1 parent 3f9f8da commit ef7e7d6

File tree

1 file changed

+7
-16
lines changed

1 file changed

+7
-16
lines changed

drivers/cpuidle/sysfs.c

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -85,34 +85,25 @@ static ssize_t store_current_governor(struct device *dev,
8585
struct device_attribute *attr,
8686
const char *buf, size_t count)
8787
{
88-
char gov_name[CPUIDLE_NAME_LEN];
89-
int ret = -EINVAL;
90-
size_t len = count;
88+
char gov_name[CPUIDLE_NAME_LEN + 1];
89+
int ret;
9190
struct cpuidle_governor *gov;
9291

93-
if (!len || len >= sizeof(gov_name))
92+
ret = sscanf(buf, "%" __stringify(CPUIDLE_NAME_LEN) "s", gov_name);
93+
if (ret != 1)
9494
return -EINVAL;
9595

96-
memcpy(gov_name, buf, len);
97-
gov_name[len] = '\0';
98-
if (gov_name[len - 1] == '\n')
99-
gov_name[--len] = '\0';
100-
10196
mutex_lock(&cpuidle_lock);
102-
97+
ret = -EINVAL;
10398
list_for_each_entry(gov, &cpuidle_governors, governor_list) {
104-
if (strlen(gov->name) == len && !strcmp(gov->name, gov_name)) {
99+
if (!strncmp(gov->name, gov_name, CPUIDLE_NAME_LEN)) {
105100
ret = cpuidle_switch_governor(gov);
106101
break;
107102
}
108103
}
109-
110104
mutex_unlock(&cpuidle_lock);
111105

112-
if (ret)
113-
return ret;
114-
else
115-
return count;
106+
return ret ? ret : count;
116107
}
117108

118109
static DEVICE_ATTR(current_driver, 0444, show_current_driver, NULL);

0 commit comments

Comments
 (0)