Skip to content

Commit adaf9fc

Browse files
committed
cpufreq: Move scheduler-related code to the sched directory
Create cpufreq.c under kernel/sched/ and move the cpufreq code related to the scheduler to that file and to sched.h. Redefine cpufreq_update_util() as a static inline function to avoid function calls at its call sites in the scheduler code (as suggested by Peter Zijlstra). Also move the definition of struct update_util_data and declaration of cpufreq_set_update_util_data() from include/linux/cpufreq.h to include/linux/sched.h. Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com> Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
1 parent 08f511f commit adaf9fc

7 files changed

Lines changed: 96 additions & 88 deletions

File tree

‎drivers/cpufreq/cpufreq.c‎

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -103,59 +103,6 @@ static struct cpufreq_driver *cpufreq_driver;
103103
static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
104104
static DEFINE_RWLOCK(cpufreq_driver_lock);
105105

106-
static DEFINE_PER_CPU(struct update_util_data *, cpufreq_update_util_data);
107-
108-
/**
109-
* cpufreq_set_update_util_data - Populate the CPU's update_util_data pointer.
110-
* @cpu: The CPU to set the pointer for.
111-
* @data: New pointer value.
112-
*
113-
* Set and publish the update_util_data pointer for the given CPU. That pointer
114-
* points to a struct update_util_data object containing a callback function
115-
* to call from cpufreq_update_util(). That function will be called from an RCU
116-
* read-side critical section, so it must not sleep.
117-
*
118-
* Callers must use RCU-sched callbacks to free any memory that might be
119-
* accessed via the old update_util_data pointer or invoke synchronize_sched()
120-
* right after this function to avoid use-after-free.
121-
*/
122-
void cpufreq_set_update_util_data(int cpu, struct update_util_data *data)
123-
{
124-
if (WARN_ON(data && !data->func))
125-
return;
126-
127-
rcu_assign_pointer(per_cpu(cpufreq_update_util_data, cpu), data);
128-
}
129-
EXPORT_SYMBOL_GPL(cpufreq_set_update_util_data);
130-
131-
/**
132-
* cpufreq_update_util - Take a note about CPU utilization changes.
133-
* @time: Current time.
134-
* @util: Current utilization.
135-
* @max: Utilization ceiling.
136-
*
137-
* This function is called by the scheduler on every invocation of
138-
* update_load_avg() on the CPU whose utilization is being updated.
139-
*
140-
* It can only be called from RCU-sched read-side critical sections.
141-
*/
142-
void cpufreq_update_util(u64 time, unsigned long util, unsigned long max)
143-
{
144-
struct update_util_data *data;
145-
146-
#ifdef CONFIG_LOCKDEP
147-
WARN_ON(debug_locks && !rcu_read_lock_sched_held());
148-
#endif
149-
150-
data = rcu_dereference_sched(*this_cpu_ptr(&cpufreq_update_util_data));
151-
/*
152-
* If this isn't inside of an RCU-sched read-side critical section, data
153-
* may become NULL after the check below.
154-
*/
155-
if (data)
156-
data->func(data, time, util, max);
157-
}
158-
159106
/* Flag to suspend/resume CPUFreq governors */
160107
static bool cpufreq_suspended;
161108

‎drivers/cpufreq/cpufreq_governor.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include <linux/export.h>
2020
#include <linux/kernel_stat.h>
21+
#include <linux/sched.h>
2122
#include <linux/slab.h>
2223

2324
#include "cpufreq_governor.h"

‎include/linux/cpufreq.h‎

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -146,36 +146,6 @@ static inline bool policy_is_shared(struct cpufreq_policy *policy)
146146
extern struct kobject *cpufreq_global_kobject;
147147

148148
#ifdef CONFIG_CPU_FREQ
149-
void cpufreq_update_util(u64 time, unsigned long util, unsigned long max);
150-
151-
/**
152-
* cpufreq_trigger_update - Trigger CPU performance state evaluation if needed.
153-
* @time: Current time.
154-
*
155-
* The way cpufreq is currently arranged requires it to evaluate the CPU
156-
* performance state (frequency/voltage) on a regular basis to prevent it from
157-
* being stuck in a completely inadequate performance level for too long.
158-
* That is not guaranteed to happen if the updates are only triggered from CFS,
159-
* though, because they may not be coming in if RT or deadline tasks are active
160-
* all the time (or there are RT and DL tasks only).
161-
*
162-
* As a workaround for that issue, this function is called by the RT and DL
163-
* sched classes to trigger extra cpufreq updates to prevent it from stalling,
164-
* but that really is a band-aid. Going forward it should be replaced with
165-
* solutions targeted more specifically at RT and DL tasks.
166-
*/
167-
static inline void cpufreq_trigger_update(u64 time)
168-
{
169-
cpufreq_update_util(time, ULONG_MAX, 0);
170-
}
171-
172-
struct update_util_data {
173-
void (*func)(struct update_util_data *data,
174-
u64 time, unsigned long util, unsigned long max);
175-
};
176-
177-
void cpufreq_set_update_util_data(int cpu, struct update_util_data *data);
178-
179149
unsigned int cpufreq_get(unsigned int cpu);
180150
unsigned int cpufreq_quick_get(unsigned int cpu);
181151
unsigned int cpufreq_quick_get_max(unsigned int cpu);
@@ -187,10 +157,6 @@ int cpufreq_update_policy(unsigned int cpu);
187157
bool have_governor_per_policy(void);
188158
struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy);
189159
#else
190-
static inline void cpufreq_update_util(u64 time, unsigned long util,
191-
unsigned long max) {}
192-
static inline void cpufreq_trigger_update(u64 time) {}
193-
194160
static inline unsigned int cpufreq_get(unsigned int cpu)
195161
{
196162
return 0;

‎include/linux/sched.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3207,4 +3207,13 @@ static inline unsigned long rlimit_max(unsigned int limit)
32073207
return task_rlimit_max(current, limit);
32083208
}
32093209

3210+
#ifdef CONFIG_CPU_FREQ
3211+
struct update_util_data {
3212+
void (*func)(struct update_util_data *data,
3213+
u64 time, unsigned long util, unsigned long max);
3214+
};
3215+
3216+
void cpufreq_set_update_util_data(int cpu, struct update_util_data *data);
3217+
#endif /* CONFIG_CPU_FREQ */
3218+
32103219
#endif

‎kernel/sched/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ obj-$(CONFIG_SCHED_AUTOGROUP) += auto_group.o
1919
obj-$(CONFIG_SCHEDSTATS) += stats.o
2020
obj-$(CONFIG_SCHED_DEBUG) += debug.o
2121
obj-$(CONFIG_CGROUP_CPUACCT) += cpuacct.o
22+
obj-$(CONFIG_CPU_FREQ) += cpufreq.o

‎kernel/sched/cpufreq.c‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
/**
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+
*/
30+
void 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+
}
37+
EXPORT_SYMBOL_GPL(cpufreq_set_update_util_data);

‎kernel/sched/sched.h‎

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include <linux/irq_work.h>
1010
#include <linux/tick.h>
1111
#include <linux/slab.h>
12-
#include <linux/cpufreq.h>
1312

1413
#include "cpupri.h"
1514
#include "cpudeadline.h"
@@ -1739,3 +1738,51 @@ static inline u64 irq_time_read(int cpu)
17391738
}
17401739
#endif /* CONFIG_64BIT */
17411740
#endif /* CONFIG_IRQ_TIME_ACCOUNTING */
1741+
1742+
#ifdef CONFIG_CPU_FREQ
1743+
DECLARE_PER_CPU(struct update_util_data *, cpufreq_update_util_data);
1744+
1745+
/**
1746+
* cpufreq_update_util - Take a note about CPU utilization changes.
1747+
* @time: Current time.
1748+
* @util: Current utilization.
1749+
* @max: Utilization ceiling.
1750+
*
1751+
* This function is called by the scheduler on every invocation of
1752+
* update_load_avg() on the CPU whose utilization is being updated.
1753+
*
1754+
* It can only be called from RCU-sched read-side critical sections.
1755+
*/
1756+
static inline void cpufreq_update_util(u64 time, unsigned long util, unsigned long max)
1757+
{
1758+
struct update_util_data *data;
1759+
1760+
data = rcu_dereference_sched(*this_cpu_ptr(&cpufreq_update_util_data));
1761+
if (data)
1762+
data->func(data, time, util, max);
1763+
}
1764+
1765+
/**
1766+
* cpufreq_trigger_update - Trigger CPU performance state evaluation if needed.
1767+
* @time: Current time.
1768+
*
1769+
* The way cpufreq is currently arranged requires it to evaluate the CPU
1770+
* performance state (frequency/voltage) on a regular basis to prevent it from
1771+
* being stuck in a completely inadequate performance level for too long.
1772+
* That is not guaranteed to happen if the updates are only triggered from CFS,
1773+
* though, because they may not be coming in if RT or deadline tasks are active
1774+
* all the time (or there are RT and DL tasks only).
1775+
*
1776+
* As a workaround for that issue, this function is called by the RT and DL
1777+
* sched classes to trigger extra cpufreq updates to prevent it from stalling,
1778+
* but that really is a band-aid. Going forward it should be replaced with
1779+
* solutions targeted more specifically at RT and DL tasks.
1780+
*/
1781+
static inline void cpufreq_trigger_update(u64 time)
1782+
{
1783+
cpufreq_update_util(time, ULONG_MAX, 0);
1784+
}
1785+
#else
1786+
static inline void cpufreq_update_util(u64 time, unsigned long util, unsigned long max) {}
1787+
static inline void cpufreq_trigger_update(u64 time) {}
1788+
#endif /* CONFIG_CPU_FREQ */

0 commit comments

Comments
 (0)