cpufreq: Fail driver register if it has adjust_perf without fast_switch
authorWyes Karny <wyes.karny@amd.com>
Mon, 29 May 2023 14:25:51 +0000 (14:25 +0000)
committerRafael J. Wysocki <rafael.j.wysocki@intel.com>
Thu, 15 Jun 2023 16:15:25 +0000 (18:15 +0200)
If fast_switch_possible flag is set by the scaling driver, the governor
is free to select fast_switch function even if adjust_perf is set.  Some
scaling drivers which use adjust_perf don't set fast_switch thinking
that the governor would never fall back to fast_switch. But the governor
can fall back to fast_switch even in runtime if frequency invariance is
disabled due to some reason. This could crash the kernel if the driver
didn't set the fast_switch function pointer.

Therefore, fail driver registration if it has adjust_perf without
fast_switch.

Suggested-by: Rafael J. Wysocki <rafael@kernel.org>
Suggested-by: Viresh Kumar <viresh.kumar@linaro.org>
Signed-off-by: Wyes Karny <wyes.karny@amd.com>
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
drivers/cpufreq/cpufreq.c
include/linux/cpufreq.h

index 6b52ebe5a890415cba039e513e04f285fea89dec..50bbc969ffe5309f429fbdc82f8e30cb68125d8e 100644 (file)
@@ -2828,7 +2828,8 @@ int cpufreq_register_driver(struct cpufreq_driver *driver_data)
             (driver_data->setpolicy && (driver_data->target_index ||
                    driver_data->target)) ||
             (!driver_data->get_intermediate != !driver_data->target_intermediate) ||
-            (!driver_data->online != !driver_data->offline))
+            (!driver_data->online != !driver_data->offline) ||
+                (driver_data->adjust_perf && !driver_data->fast_switch))
                return -EINVAL;
 
        pr_debug("trying to register driver %s\n", driver_data->name);
index 26e2eb3994841b28694640932c57b9d8e6edd1b3..172ff51c1b2a4b9f310943e78c44793ec5bd4f34 100644 (file)
@@ -340,7 +340,10 @@ struct cpufreq_driver {
        /*
         * ->fast_switch() replacement for drivers that use an internal
         * representation of performance levels and can pass hints other than
-        * the target performance level to the hardware.
+        * the target performance level to the hardware. This can only be set
+        * if ->fast_switch is set too, because in those cases (under specific
+        * conditions) scale invariance can be disabled, which causes the
+        * schedutil governor to fall back to the latter.
         */
        void            (*adjust_perf)(unsigned int cpu,
                                       unsigned long min_perf,