1 // SPDX-License-Identifier: GPL-2.0 << 2 /* 1 /* 3 * Scheduler code and data structures related 2 * Scheduler code and data structures related to cpufreq. 4 * 3 * 5 * Copyright (C) 2016, Intel Corporation 4 * Copyright (C) 2016, Intel Corporation 6 * Author: Rafael J. Wysocki <rafael.j.wysocki 5 * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com> >> 6 * >> 7 * This program is free software; you can redistribute it and/or modify >> 8 * it under the terms of the GNU General Public License version 2 as >> 9 * published by the Free Software Foundation. 7 */ 10 */ >> 11 #include <linux/cpufreq.h> >> 12 >> 13 #include "sched.h" 8 14 9 DEFINE_PER_CPU(struct update_util_data __rcu * !! 15 DEFINE_PER_CPU(struct update_util_data *, cpufreq_update_util_data); 10 16 11 /** 17 /** 12 * cpufreq_add_update_util_hook - Populate the 18 * cpufreq_add_update_util_hook - Populate the CPU's update_util_data pointer. 13 * @cpu: The CPU to set the pointer for. 19 * @cpu: The CPU to set the pointer for. 14 * @data: New pointer value. 20 * @data: New pointer value. 15 * @func: Callback function to set for the CPU 21 * @func: Callback function to set for the CPU. 16 * 22 * 17 * Set and publish the update_util_data pointe 23 * Set and publish the update_util_data pointer for the given CPU. 18 * 24 * 19 * The update_util_data pointer of @cpu is set 25 * The update_util_data pointer of @cpu is set to @data and the callback 20 * function pointer in the target struct updat 26 * function pointer in the target struct update_util_data is set to @func. 21 * That function will be called by cpufreq_upd 27 * That function will be called by cpufreq_update_util() from RCU-sched 22 * read-side critical sections, so it must not 28 * read-side critical sections, so it must not sleep. @data will always be 23 * passed to it as the first argument which al 29 * passed to it as the first argument which allows the function to get to the 24 * target update_util_data structure and its c 30 * target update_util_data structure and its container. 25 * 31 * 26 * The update_util_data pointer of @cpu must b 32 * The update_util_data pointer of @cpu must be NULL when this function is 27 * called or it will WARN() and return with no 33 * called or it will WARN() and return with no effect. 28 */ 34 */ 29 void cpufreq_add_update_util_hook(int cpu, str 35 void cpufreq_add_update_util_hook(int cpu, struct update_util_data *data, 30 void (*func)(struct up 36 void (*func)(struct update_util_data *data, u64 time, 31 unsigned 37 unsigned int flags)) 32 { 38 { 33 if (WARN_ON(!data || !func)) 39 if (WARN_ON(!data || !func)) 34 return; 40 return; 35 41 36 if (WARN_ON(per_cpu(cpufreq_update_uti 42 if (WARN_ON(per_cpu(cpufreq_update_util_data, cpu))) 37 return; 43 return; 38 44 39 data->func = func; 45 data->func = func; 40 rcu_assign_pointer(per_cpu(cpufreq_upd 46 rcu_assign_pointer(per_cpu(cpufreq_update_util_data, cpu), data); 41 } 47 } 42 EXPORT_SYMBOL_GPL(cpufreq_add_update_util_hook 48 EXPORT_SYMBOL_GPL(cpufreq_add_update_util_hook); 43 49 44 /** 50 /** 45 * cpufreq_remove_update_util_hook - Clear the 51 * cpufreq_remove_update_util_hook - Clear the CPU's update_util_data pointer. 46 * @cpu: The CPU to clear the pointer for. 52 * @cpu: The CPU to clear the pointer for. 47 * 53 * 48 * Clear the update_util_data pointer for the 54 * Clear the update_util_data pointer for the given CPU. 49 * 55 * 50 * Callers must use RCU callbacks to free any !! 56 * Callers must use RCU-sched callbacks to free any memory that might be 51 * accessed via the old update_util_data point !! 57 * accessed via the old update_util_data pointer or invoke synchronize_sched() 52 * right after this function to avoid use-afte 58 * right after this function to avoid use-after-free. 53 */ 59 */ 54 void cpufreq_remove_update_util_hook(int cpu) 60 void cpufreq_remove_update_util_hook(int cpu) 55 { 61 { 56 rcu_assign_pointer(per_cpu(cpufreq_upd 62 rcu_assign_pointer(per_cpu(cpufreq_update_util_data, cpu), NULL); 57 } 63 } 58 EXPORT_SYMBOL_GPL(cpufreq_remove_update_util_h 64 EXPORT_SYMBOL_GPL(cpufreq_remove_update_util_hook); 59 65 60 /** 66 /** 61 * cpufreq_this_cpu_can_update - Check if cpuf 67 * cpufreq_this_cpu_can_update - Check if cpufreq policy can be updated. 62 * @policy: cpufreq policy to check. 68 * @policy: cpufreq policy to check. 63 * 69 * 64 * Return 'true' if: 70 * Return 'true' if: 65 * - the local and remote CPUs share @policy, 71 * - the local and remote CPUs share @policy, 66 * - dvfs_possible_from_any_cpu is set in @pol 72 * - dvfs_possible_from_any_cpu is set in @policy and the local CPU is not going 67 * offline (in which case it is not expected 73 * offline (in which case it is not expected to run cpufreq updates any more). 68 */ 74 */ 69 bool cpufreq_this_cpu_can_update(struct cpufre 75 bool cpufreq_this_cpu_can_update(struct cpufreq_policy *policy) 70 { 76 { 71 return cpumask_test_cpu(smp_processor_ 77 return cpumask_test_cpu(smp_processor_id(), policy->cpus) || 72 (policy->dvfs_possible_from_an 78 (policy->dvfs_possible_from_any_cpu && 73 rcu_dereference_sched(*this_c 79 rcu_dereference_sched(*this_cpu_ptr(&cpufreq_update_util_data))); 74 } 80 } 75 81
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.