Rafael J. Wysocki | adaf9fc | 2016-03-10 20:44:47 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Scheduler code and data structures related to cpufreq. |
| 3 | * |
| 4 | * Copyright (C) 2016, Intel Corporation |
| 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. |
| 10 | */ |
| 11 | |
| 12 | #include "sched.h" |
| 13 | |
| 14 | DEFINE_PER_CPU(struct update_util_data *, cpufreq_update_util_data); |
| 15 | |
| 16 | /** |
Rafael J. Wysocki | 0bed612 | 2016-04-02 01:08:43 +0200 | [diff] [blame] | 17 | * cpufreq_add_update_util_hook - Populate the CPU's update_util_data pointer. |
Rafael J. Wysocki | adaf9fc | 2016-03-10 20:44:47 +0100 | [diff] [blame] | 18 | * @cpu: The CPU to set the pointer for. |
| 19 | * @data: New pointer value. |
Rafael J. Wysocki | 0bed612 | 2016-04-02 01:08:43 +0200 | [diff] [blame] | 20 | * @func: Callback function to set for the CPU. |
Rafael J. Wysocki | adaf9fc | 2016-03-10 20:44:47 +0100 | [diff] [blame] | 21 | * |
Rafael J. Wysocki | 0bed612 | 2016-04-02 01:08:43 +0200 | [diff] [blame] | 22 | * Set and publish the update_util_data pointer for the given CPU. |
| 23 | * |
| 24 | * The update_util_data pointer of @cpu is set to @data and the callback |
| 25 | * function pointer in the target struct update_util_data is set to @func. |
| 26 | * That function will be called by cpufreq_update_util() from RCU-sched |
| 27 | * read-side critical sections, so it must not sleep. @data will always be |
| 28 | * passed to it as the first argument which allows the function to get to the |
| 29 | * target update_util_data structure and its container. |
| 30 | * |
| 31 | * The update_util_data pointer of @cpu must be NULL when this function is |
| 32 | * called or it will WARN() and return with no effect. |
| 33 | */ |
| 34 | void cpufreq_add_update_util_hook(int cpu, struct update_util_data *data, |
| 35 | void (*func)(struct update_util_data *data, u64 time, |
Rafael J. Wysocki | 58919e8 | 2016-08-16 22:14:55 +0200 | [diff] [blame^] | 36 | unsigned int flags)) |
Rafael J. Wysocki | 0bed612 | 2016-04-02 01:08:43 +0200 | [diff] [blame] | 37 | { |
| 38 | if (WARN_ON(!data || !func)) |
| 39 | return; |
| 40 | |
| 41 | if (WARN_ON(per_cpu(cpufreq_update_util_data, cpu))) |
| 42 | return; |
| 43 | |
| 44 | data->func = func; |
| 45 | rcu_assign_pointer(per_cpu(cpufreq_update_util_data, cpu), data); |
| 46 | } |
| 47 | EXPORT_SYMBOL_GPL(cpufreq_add_update_util_hook); |
| 48 | |
| 49 | /** |
| 50 | * cpufreq_remove_update_util_hook - Clear the CPU's update_util_data pointer. |
| 51 | * @cpu: The CPU to clear the pointer for. |
| 52 | * |
| 53 | * Clear the update_util_data pointer for the given CPU. |
Rafael J. Wysocki | adaf9fc | 2016-03-10 20:44:47 +0100 | [diff] [blame] | 54 | * |
| 55 | * Callers must use RCU-sched callbacks to free any memory that might be |
| 56 | * accessed via the old update_util_data pointer or invoke synchronize_sched() |
| 57 | * right after this function to avoid use-after-free. |
| 58 | */ |
Rafael J. Wysocki | 0bed612 | 2016-04-02 01:08:43 +0200 | [diff] [blame] | 59 | void cpufreq_remove_update_util_hook(int cpu) |
Rafael J. Wysocki | adaf9fc | 2016-03-10 20:44:47 +0100 | [diff] [blame] | 60 | { |
Rafael J. Wysocki | 0bed612 | 2016-04-02 01:08:43 +0200 | [diff] [blame] | 61 | rcu_assign_pointer(per_cpu(cpufreq_update_util_data, cpu), NULL); |
Rafael J. Wysocki | adaf9fc | 2016-03-10 20:44:47 +0100 | [diff] [blame] | 62 | } |
Rafael J. Wysocki | 0bed612 | 2016-04-02 01:08:43 +0200 | [diff] [blame] | 63 | EXPORT_SYMBOL_GPL(cpufreq_remove_update_util_hook); |