Skip to contentSkip to conversation

cpufreq: Fix policy->freq_table is NULL in __cpufreq_driver_target()

PATCH In review Yajun Deng 3 participants 7 messages Mar 29, 2023 – Apr 4, 2023 CPU frequency scaling framework

lore
Share & tools
lore

Yajun Deng

author
#0 lore raw

PATCH 1/1 cpufreq: Fix policy->freq_table is NULL in __cpufreq_driver_target()

__resolve_freq() may be return target_freq if policy->freq_table is NULL. In this case, it should return -EINVAL before __target_index().

---
 drivers/cpufreq/cpufreq.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/cpufreq/cpufreq.c +1 -1
index c0e5be0fe2d6..308a3df1a940 100644
@@ -2299,7 +2299,7 @@ int __cpufreq_driver_target(struct cpufreq_policy *policy,
 		return cpufreq_driver->target(policy, target_freq, relation);
 	}
 
-	if (!cpufreq_driver->target_index)
+	if (!cpufreq_driver->target_index || !policy->freq_table)
 		return -EINVAL;
 
 	return __target_index(policy, policy->cached_resolved_idx);
Signature · 1 line
2.25.1
#1 lore raw
Quoted 3 lines · Yajun Deng

__resolve_freq() may be return target_freq if policy->freq_table is NULL. In this case, it should return -EINVAL before __target_index().

Even so, __target_index() itself doesn't dereference freq_table AFAICS, so arguably the driver should be prepared to deal with a NULL freq_table which comes from it after all.

Or, if you want to argue that drivers providing ->target_index() must also provide freq_table that is not NULL, a check to that effect needs to be done at the driver registration time IMO.

Quoted 19 lines
---
 drivers/cpufreq/cpufreq.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/cpufreq/cpufreq.c +1 -2
index c0e5be0fe2d6..308a3df1a940 100644
@@ -2299,7 +2299,7 @@ int __cpufreq_driver_target(struct cpufreq_policy *policy,
                return cpufreq_driver->target(policy, target_freq, relation);
        }
 
-       if (!cpufreq_driver->target_index)
+       if (!cpufreq_driver->target_index || !policy->freq_table)
                return -EINVAL;
 
        return __target_index(policy, policy->cached_resolved_idx);
--
author
#2 lore raw
Quoted 9 lines · Rafael J. Wysocki
Quoted 2 lines · Yajun Deng

__resolve_freq() may be return target_freq if policy->freq_table is NULL. In this case, it should return -EINVAL before __target_index().

Even so, __target_index() itself doesn't dereference freq_table AFAICS, so arguably the driver should be prepared to deal with a NULL freq_table which comes from it after all.

But there is a statement 'unsigned int newfreq = policy->freq_table[index].frequency;' in __target_index(), if driver doesn't provide freq_table, __target_index() will fault before the driver itself.

Quoted 23 lines

Or, if you want to argue that drivers providing ->target_index() must also provide freq_table that is not NULL, a check to that effect needs to be done at the driver registration time IMO.

Quoted 19 lines

---
drivers/cpufreq/cpufreq.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
drivers/cpufreq/cpufreq.c +1 -2
index c0e5be0fe2d6..308a3df1a940 100644
@@ -2299,7 +2299,7 @@ int __cpufreq_driver_target(struct cpufreq_policy *policy,
 return cpufreq_driver->target(policy, target_freq, relation);
 }
 
- if (!cpufreq_driver->target_index)
+ if (!cpufreq_driver->target_index || !policy->freq_table)
 return -EINVAL;
 
 return __target_index(policy, policy->cached_resolved_idx);
--
maintainer
#3 lore raw
Quoted 7 lines · Yajun Deng
Quoted 5 lines · Rafael J. Wysocki
Quoted 2 lines · Yajun Deng

__resolve_freq() may be return target_freq if policy->freq_table is NULL. In this case, it should return -EINVAL before __target_index().

Quoted 8 lines
Quoted 4 lines

Even so, __target_index() itself doesn't dereference freq_table AFAICS, so arguably the driver should be prepared to deal with a NULL freq_table which comes from it after all.

But there is a statement 'unsigned int newfreq = policy->freq_table[index].frequency;' in __target_index(), if driver doesn't provide freq_table, __target_index() will fault before the driver itself.

Driver must provide a freq table here.

Signature · 1 line
viresh
#4 lore raw
Quoted 14 lines · Viresh Kumar
Quoted 12 lines · Yajun Deng
Quoted 9 lines · Rafael J. Wysocki
Quoted 2 lines · Yajun Deng

__resolve_freq() may be return target_freq if policy->freq_table is NULL. In this case, it should return -EINVAL before __target_index().

Even so, __target_index() itself doesn't dereference freq_table AFAICS, so arguably the driver should be prepared to deal with a NULL freq_table which comes from it after all.

Quoted 5 lines
Quoted 3 lines

But there is a statement 'unsigned int newfreq = policy->freq_table[index].frequency;' in __target_index(), if driver doesn't provide freq_table, __target_index() will fault before the driver itself.

Driver must provide a freq table here.

OK, so let's do the check when the driver gets registered.

maintainer
#5 lore raw
In reply to Yajun Deng
Quoted 13 lines · Yajun Deng

__resolve_freq() may be return target_freq if policy->freq_table is NULL. In this case, it should return -EINVAL before __target_index().

---
 drivers/cpufreq/cpufreq.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/cpufreq/cpufreq.c +0 -0
index c0e5be0fe2d6..308a3df1a940 100644

drivers/cpufreq/cpufreq.c int __cpufreq_driver_target(struct cpufreq_policy *policy, · line 2303

Quoted 6 lines
drivers/cpufreq/cpufreq.c +1 -1
@@ -2299,7 +2299,7 @@ int __cpufreq_driver_target(struct cpufreq_policy *policy,
 		return cpufreq_driver->target(policy, target_freq, relation);
 	}
 
-	if (!cpufreq_driver->target_index)
+	if (!cpufreq_driver->target_index || !policy->freq_table)
 		return -EINVAL;

Hi,

I have sent an alternate patch [1] for this, please try it.

Signature · 3 lines
viresh

[1] https://lore.kernel.org/all/53d4ed4e5b18a59a48790434f8146fb207e11c49.1680494945.git.viresh.kumar@linaro.org/
author
#6 lore raw
Quoted 18 lines · Viresh Kumar
Quoted 16 lines · Yajun Deng

__resolve_freq() may be return target_freq if policy->freq_table is NULL. In this case, it should return -EINVAL before __target_index().

---
drivers/cpufreq/cpufreq.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
drivers/cpufreq/cpufreq.c +0 -0
index c0e5be0fe2d6..308a3df1a940 100644
@@ -2299,7 +2299,7 @@ int __cpufreq_driver_target(struct cpufreq_policy *policy,
 return cpufreq_driver->target(policy, target_freq, relation);
 }
 
Quoted 8 lines

drivers/cpufreq/cpufreq.c int __cpufreq_driver_target(struct cpufreq_policy *policy, · line 2303

Quoted 3 lines
drivers/cpufreq/cpufreq.c +1 -1
- if (!cpufreq_driver->target_index)
+ if (!cpufreq_driver->target_index || !policy->freq_table)
 return -EINVAL;

Hi,

I have sent an alternate patch [1] for this, please try it.

Thanks, v2 is fine.

Quoted 6 lines

--
viresh

[1]
https://lore.kernel.org/all/53d4ed4e5b18a59a48790434f8146fb207e11c49.1680494945.git.viresh.kumar@lin
ro.org