Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
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>
7 */
8#include "sched.h"
9
10DEFINE_PER_CPU(struct update_util_data __rcu *, cpufreq_update_util_data);
11
12/**
13 * cpufreq_add_update_util_hook - Populate the CPU's update_util_data pointer.
14 * @cpu: The CPU to set the pointer for.
15 * @data: New pointer value.
16 * @func: Callback function to set for the CPU.
17 *
18 * 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,
32 unsigned int flags))
33{
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.
50 *
51 * 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()
53 * right after this function to avoid use-after-free.
54 */
55void cpufreq_remove_update_util_hook(int cpu)
56{
57 rcu_assign_pointer(per_cpu(cpufreq_update_util_data, cpu), NULL);
58}
59EXPORT_SYMBOL_GPL(cpufreq_remove_update_util_hook);
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
14DEFINE_PER_CPU(struct update_util_data *, cpufreq_update_util_data);
15
16/**
17 * cpufreq_set_update_util_data - Populate the CPU's update_util_data pointer.
18 * @cpu: The CPU to set the pointer for.
19 * @data: New pointer value.
20 *
21 * Set and publish the update_util_data pointer for the given CPU. That pointer
22 * points to a struct update_util_data object containing a callback function
23 * to call from cpufreq_update_util(). That function will be called from an RCU
24 * read-side critical section, so it must not sleep.
25 *
26 * Callers must use RCU-sched callbacks to free any memory that might be
27 * accessed via the old update_util_data pointer or invoke synchronize_sched()
28 * right after this function to avoid use-after-free.
29 */
30void cpufreq_set_update_util_data(int cpu, struct update_util_data *data)
31{
32 if (WARN_ON(data && !data->func))
33 return;
34
35 rcu_assign_pointer(per_cpu(cpufreq_update_util_data, cpu), data);
36}
37EXPORT_SYMBOL_GPL(cpufreq_set_update_util_data);