Skip to content

Commit ab05d97

Browse files
Yue Hurafaeljw
authored andcommitted
cpufreq: Don't find governor for setpolicy drivers in cpufreq_init_policy()
In cpufreq_init_policy() we will check if there's last_governor for target and setpolicy type. However last_governor is set only if has_target() is true in cpufreq_offline(). That means find last_governor for setpolicy type is pointless. Also new_policy.governor will not be used if ->setpolicy callback is set in cpufreq_set_policy(). Moreover, there's duplicate ->setpolicy check in using default policy path. Let's add a new helper function to avoid it. Also update comments. Signed-off-by: Yue Hu <[email protected]> Acked-by: Viresh Kumar <[email protected]> Signed-off-by: Rafael J. Wysocki <[email protected]>
1 parent 2acb9bd commit ab05d97

File tree

1 file changed

+65
-51
lines changed

1 file changed

+65
-51
lines changed

drivers/cpufreq/cpufreq.c

Lines changed: 65 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -619,50 +619,52 @@ static struct cpufreq_governor *find_governor(const char *str_governor)
619619
return NULL;
620620
}
621621

622+
static int cpufreq_parse_policy(char *str_governor,
623+
struct cpufreq_policy *policy)
624+
{
625+
if (!strncasecmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
626+
policy->policy = CPUFREQ_POLICY_PERFORMANCE;
627+
return 0;
628+
}
629+
if (!strncasecmp(str_governor, "powersave", CPUFREQ_NAME_LEN)) {
630+
policy->policy = CPUFREQ_POLICY_POWERSAVE;
631+
return 0;
632+
}
633+
return -EINVAL;
634+
}
635+
622636
/**
623-
* cpufreq_parse_governor - parse a governor string
637+
* cpufreq_parse_governor - parse a governor string only for !setpolicy
624638
*/
625639
static int cpufreq_parse_governor(char *str_governor,
626640
struct cpufreq_policy *policy)
627641
{
628-
if (cpufreq_driver->setpolicy) {
629-
if (!strncasecmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
630-
policy->policy = CPUFREQ_POLICY_PERFORMANCE;
631-
return 0;
632-
}
633-
634-
if (!strncasecmp(str_governor, "powersave", CPUFREQ_NAME_LEN)) {
635-
policy->policy = CPUFREQ_POLICY_POWERSAVE;
636-
return 0;
637-
}
638-
} else {
639-
struct cpufreq_governor *t;
642+
struct cpufreq_governor *t;
640643

641-
mutex_lock(&cpufreq_governor_mutex);
644+
mutex_lock(&cpufreq_governor_mutex);
642645

643-
t = find_governor(str_governor);
644-
if (!t) {
645-
int ret;
646+
t = find_governor(str_governor);
647+
if (!t) {
648+
int ret;
646649

647-
mutex_unlock(&cpufreq_governor_mutex);
650+
mutex_unlock(&cpufreq_governor_mutex);
648651

649-
ret = request_module("cpufreq_%s", str_governor);
650-
if (ret)
651-
return -EINVAL;
652+
ret = request_module("cpufreq_%s", str_governor);
653+
if (ret)
654+
return -EINVAL;
652655

653-
mutex_lock(&cpufreq_governor_mutex);
656+
mutex_lock(&cpufreq_governor_mutex);
654657

655-
t = find_governor(str_governor);
656-
}
657-
if (t && !try_module_get(t->owner))
658-
t = NULL;
658+
t = find_governor(str_governor);
659+
}
660+
if (t && !try_module_get(t->owner))
661+
t = NULL;
659662

660-
mutex_unlock(&cpufreq_governor_mutex);
663+
mutex_unlock(&cpufreq_governor_mutex);
661664

662-
if (t) {
663-
policy->governor = t;
664-
return 0;
665-
}
665+
if (t) {
666+
policy->governor = t;
667+
return 0;
666668
}
667669

668670
return -EINVAL;
@@ -784,8 +786,13 @@ static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
784786
if (ret != 1)
785787
return -EINVAL;
786788

787-
if (cpufreq_parse_governor(str_governor, &new_policy))
788-
return -EINVAL;
789+
if (cpufreq_driver->setpolicy) {
790+
if (cpufreq_parse_policy(str_governor, &new_policy))
791+
return -EINVAL;
792+
} else {
793+
if (cpufreq_parse_governor(str_governor, &new_policy))
794+
return -EINVAL;
795+
}
789796

790797
ret = cpufreq_set_policy(policy, &new_policy);
791798

@@ -1051,32 +1058,39 @@ __weak struct cpufreq_governor *cpufreq_default_governor(void)
10511058

10521059
static int cpufreq_init_policy(struct cpufreq_policy *policy)
10531060
{
1054-
struct cpufreq_governor *gov = NULL;
1061+
struct cpufreq_governor *gov = NULL, *def_gov = NULL;
10551062
struct cpufreq_policy new_policy;
10561063

10571064
memcpy(&new_policy, policy, sizeof(*policy));
10581065

1059-
/* Update governor of new_policy to the governor used before hotplug */
1060-
gov = find_governor(policy->last_governor);
1061-
if (gov) {
1062-
pr_debug("Restoring governor %s for cpu %d\n",
1066+
def_gov = cpufreq_default_governor();
1067+
1068+
if (has_target()) {
1069+
/*
1070+
* Update governor of new_policy to the governor used before
1071+
* hotplug
1072+
*/
1073+
gov = find_governor(policy->last_governor);
1074+
if (gov) {
1075+
pr_debug("Restoring governor %s for cpu %d\n",
10631076
policy->governor->name, policy->cpu);
1077+
} else {
1078+
if (!def_gov)
1079+
return -ENODATA;
1080+
gov = def_gov;
1081+
}
1082+
new_policy.governor = gov;
10641083
} else {
1065-
gov = cpufreq_default_governor();
1066-
if (!gov)
1067-
return -ENODATA;
1068-
}
1069-
1070-
new_policy.governor = gov;
1071-
1072-
/* Use the default policy if there is no last_policy. */
1073-
if (cpufreq_driver->setpolicy) {
1074-
if (policy->last_policy)
1084+
/* Use the default policy if there is no last_policy. */
1085+
if (policy->last_policy) {
10751086
new_policy.policy = policy->last_policy;
1076-
else
1077-
cpufreq_parse_governor(gov->name, &new_policy);
1087+
} else {
1088+
if (!def_gov)
1089+
return -ENODATA;
1090+
cpufreq_parse_policy(def_gov->name, &new_policy);
1091+
}
10781092
}
1079-
/* set default policy */
1093+
10801094
return cpufreq_set_policy(policy, &new_policy);
10811095
}
10821096

0 commit comments

Comments
 (0)