blob: b5dcd1d83c7fada652b79576738d9542f0638d67 [file] [log] [blame]
Daniel Lezcano108c35a2018-12-03 11:29:29 +01001// SPDX-License-Identifier: GPL-2.0
Rafael J. Wysockiadaf9fc2016-03-10 20:44:47 +01002/*
3 * Scheduler code and data structures related to cpufreq.
4 *
5 * Copyright (C) 2016, Intel Corporation
6 * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Rafael J. Wysockiadaf9fc2016-03-10 20:44:47 +01007 */
Rafael J. Wysockiadaf9fc2016-03-10 20:44:47 +01008#include "sched.h"
9
Joel Fernandes (Google)b10abd02019-03-20 20:34:23 -040010DEFINE_PER_CPU(struct update_util_data __rcu *, cpufreq_update_util_data);
Rafael J. Wysockiadaf9fc2016-03-10 20:44:47 +010011
12/**
Rafael J. Wysocki0bed6122016-04-02 01:08:43 +020013 * cpufreq_add_update_util_hook - Populate the CPU's update_util_data pointer.
Rafael J. Wysockiadaf9fc2016-03-10 20:44:47 +010014 * @cpu: The CPU to set the pointer for.
15 * @data: New pointer value.
Rafael J. Wysocki0bed6122016-04-02 01:08:43 +020016 * @func: Callback function to set for the CPU.
Rafael J. Wysockiadaf9fc2016-03-10 20:44:47 +010017 *
Rafael J. Wysocki0bed6122016-04-02 01:08:43 +020018 * Set and publish the update_util_data pointer for the given CPU.
19 *
20 * The update_util_data pointer of @cpu is set to @data and the callback
21 * function pointer in the target struct update_util_data is set to @func.
22 * That function will be called by cpufreq_update_util() from RCU-sched
23 * read-side critical sections, so it must not sleep. @data will always be
24 * passed to it as the first argument which allows the function to get to the
25 * target update_util_data structure and its container.
26 *
27 * The update_util_data pointer of @cpu must be NULL when this function is
28 * called or it will WARN() and return with no effect.
29 */
30void cpufreq_add_update_util_hook(int cpu, struct update_util_data *data,
31 void (*func)(struct update_util_data *data, u64 time,
Rafael J. Wysocki58919e82016-08-16 22:14:55 +020032 unsigned int flags))
Rafael J. Wysocki0bed6122016-04-02 01:08:43 +020033{
34 if (WARN_ON(!data || !func))
35 return;
36
37 if (WARN_ON(per_cpu(cpufreq_update_util_data, cpu)))
38 return;
39
40 data->func = func;
41 rcu_assign_pointer(per_cpu(cpufreq_update_util_data, cpu), data);
42}
43EXPORT_SYMBOL_GPL(cpufreq_add_update_util_hook);
44
45/**
46 * cpufreq_remove_update_util_hook - Clear the CPU's update_util_data pointer.
47 * @cpu: The CPU to clear the pointer for.
48 *
49 * Clear the update_util_data pointer for the given CPU.
Rafael J. Wysockiadaf9fc2016-03-10 20:44:47 +010050 *
Paul E. McKenneyb290ebc2018-11-06 19:13:54 -080051 * Callers must use RCU callbacks to free any memory that might be
52 * accessed via the old update_util_data pointer or invoke synchronize_rcu()
Rafael J. Wysockiadaf9fc2016-03-10 20:44:47 +010053 * right after this function to avoid use-after-free.
54 */
Rafael J. Wysocki0bed6122016-04-02 01:08:43 +020055void cpufreq_remove_update_util_hook(int cpu)
Rafael J. Wysockiadaf9fc2016-03-10 20:44:47 +010056{
Rafael J. Wysocki0bed6122016-04-02 01:08:43 +020057 rcu_assign_pointer(per_cpu(cpufreq_update_util_data, cpu), NULL);
Rafael J. Wysockiadaf9fc2016-03-10 20:44:47 +010058}
Rafael J. Wysocki0bed6122016-04-02 01:08:43 +020059EXPORT_SYMBOL_GPL(cpufreq_remove_update_util_hook);