Skip to content

Commit 8a4a891

Browse files
committed
Merge branch 'sched-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
* 'sched-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: (46 commits) llist: Add back llist_add_batch() and llist_del_first() prototypes sched: Don't use tasklist_lock for debug prints sched: Warn on rt throttling sched: Unify the ->cpus_allowed mask copy sched: Wrap scheduler p->cpus_allowed access sched: Request for idle balance during nohz idle load balance sched: Use resched IPI to kick off the nohz idle balance sched: Fix idle_cpu() llist: Remove cpu_relax() usage in cmpxchg loops sched: Convert to struct llist llist: Add llist_next() irq_work: Use llist in the struct irq_work logic llist: Return whether list is empty before adding in llist_add() llist: Move cpu_relax() to after the cmpxchg() llist: Remove the platform-dependent NMI checks llist: Make some llist functions inline sched, tracing: Show PREEMPT_ACTIVE state in trace_sched_switch sched: Remove redundant test in check_preempt_tick() sched: Add documentation for bandwidth control sched: Return unused runtime on group dequeue ...
2 parents 8686a0e + 540f41e commit 8a4a891

20 files changed

Lines changed: 1646 additions & 410 deletions

File tree

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
CFS Bandwidth Control
2+
=====================
3+
4+
[ This document only discusses CPU bandwidth control for SCHED_NORMAL.
5+
The SCHED_RT case is covered in Documentation/scheduler/sched-rt-group.txt ]
6+
7+
CFS bandwidth control is a CONFIG_FAIR_GROUP_SCHED extension which allows the
8+
specification of the maximum CPU bandwidth available to a group or hierarchy.
9+
10+
The bandwidth allowed for a group is specified using a quota and period. Within
11+
each given "period" (microseconds), a group is allowed to consume only up to
12+
"quota" microseconds of CPU time. When the CPU bandwidth consumption of a
13+
group exceeds this limit (for that period), the tasks belonging to its
14+
hierarchy will be throttled and are not allowed to run again until the next
15+
period.
16+
17+
A group's unused runtime is globally tracked, being refreshed with quota units
18+
above at each period boundary. As threads consume this bandwidth it is
19+
transferred to cpu-local "silos" on a demand basis. The amount transferred
20+
within each of these updates is tunable and described as the "slice".
21+
22+
Management
23+
----------
24+
Quota and period are managed within the cpu subsystem via cgroupfs.
25+
26+
cpu.cfs_quota_us: the total available run-time within a period (in microseconds)
27+
cpu.cfs_period_us: the length of a period (in microseconds)
28+
cpu.stat: exports throttling statistics [explained further below]
29+
30+
The default values are:
31+
cpu.cfs_period_us=100ms
32+
cpu.cfs_quota=-1
33+
34+
A value of -1 for cpu.cfs_quota_us indicates that the group does not have any
35+
bandwidth restriction in place, such a group is described as an unconstrained
36+
bandwidth group. This represents the traditional work-conserving behavior for
37+
CFS.
38+
39+
Writing any (valid) positive value(s) will enact the specified bandwidth limit.
40+
The minimum quota allowed for the quota or period is 1ms. There is also an
41+
upper bound on the period length of 1s. Additional restrictions exist when
42+
bandwidth limits are used in a hierarchical fashion, these are explained in
43+
more detail below.
44+
45+
Writing any negative value to cpu.cfs_quota_us will remove the bandwidth limit
46+
and return the group to an unconstrained state once more.
47+
48+
Any updates to a group's bandwidth specification will result in it becoming
49+
unthrottled if it is in a constrained state.
50+
51+
System wide settings
52+
--------------------
53+
For efficiency run-time is transferred between the global pool and CPU local
54+
"silos" in a batch fashion. This greatly reduces global accounting pressure
55+
on large systems. The amount transferred each time such an update is required
56+
is described as the "slice".
57+
58+
This is tunable via procfs:
59+
/proc/sys/kernel/sched_cfs_bandwidth_slice_us (default=5ms)
60+
61+
Larger slice values will reduce transfer overheads, while smaller values allow
62+
for more fine-grained consumption.
63+
64+
Statistics
65+
----------
66+
A group's bandwidth statistics are exported via 3 fields in cpu.stat.
67+
68+
cpu.stat:
69+
- nr_periods: Number of enforcement intervals that have elapsed.
70+
- nr_throttled: Number of times the group has been throttled/limited.
71+
- throttled_time: The total time duration (in nanoseconds) for which entities
72+
of the group have been throttled.
73+
74+
This interface is read-only.
75+
76+
Hierarchical considerations
77+
---------------------------
78+
The interface enforces that an individual entity's bandwidth is always
79+
attainable, that is: max(c_i) <= C. However, over-subscription in the
80+
aggregate case is explicitly allowed to enable work-conserving semantics
81+
within a hierarchy.
82+
e.g. \Sum (c_i) may exceed C
83+
[ Where C is the parent's bandwidth, and c_i its children ]
84+
85+
86+
There are two ways in which a group may become throttled:
87+
a. it fully consumes its own quota within a period
88+
b. a parent's quota is fully consumed within its period
89+
90+
In case b) above, even though the child may have runtime remaining it will not
91+
be allowed to until the parent's runtime is refreshed.
92+
93+
Examples
94+
--------
95+
1. Limit a group to 1 CPU worth of runtime.
96+
97+
If period is 250ms and quota is also 250ms, the group will get
98+
1 CPU worth of runtime every 250ms.
99+
100+
# echo 250000 > cpu.cfs_quota_us /* quota = 250ms */
101+
# echo 250000 > cpu.cfs_period_us /* period = 250ms */
102+
103+
2. Limit a group to 2 CPUs worth of runtime on a multi-CPU machine.
104+
105+
With 500ms period and 1000ms quota, the group can get 2 CPUs worth of
106+
runtime every 500ms.
107+
108+
# echo 1000000 > cpu.cfs_quota_us /* quota = 1000ms */
109+
# echo 500000 > cpu.cfs_period_us /* period = 500ms */
110+
111+
The larger period here allows for increased burst capacity.
112+
113+
3. Limit a group to 20% of 1 CPU.
114+
115+
With 50ms period, 10ms quota will be equivalent to 20% of 1 CPU.
116+
117+
# echo 10000 > cpu.cfs_quota_us /* quota = 10ms */
118+
# echo 50000 > cpu.cfs_period_us /* period = 50ms */
119+
120+
By using a small period here we are ensuring a consistent latency
121+
response at the expense of burst capacity.
122+

‎drivers/acpi/apei/Kconfig‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ config ACPI_APEI_GHES
1414
depends on ACPI_APEI && X86
1515
select ACPI_HED
1616
select IRQ_WORK
17-
select LLIST
1817
select GENERIC_ALLOCATOR
1918
help
2019
Generic Hardware Error Source provides a way to report

‎include/linux/irq_work.h‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
#ifndef _LINUX_IRQ_WORK_H
22
#define _LINUX_IRQ_WORK_H
33

4+
#include <linux/llist.h>
5+
46
struct irq_work {
5-
struct irq_work *next;
7+
unsigned long flags;
8+
struct llist_node llnode;
69
void (*func)(struct irq_work *);
710
};
811

912
static inline
10-
void init_irq_work(struct irq_work *entry, void (*func)(struct irq_work *))
13+
void init_irq_work(struct irq_work *work, void (*func)(struct irq_work *))
1114
{
12-
entry->next = NULL;
13-
entry->func = func;
15+
work->flags = 0;
16+
work->func = func;
1417
}
1518

16-
bool irq_work_queue(struct irq_work *entry);
19+
bool irq_work_queue(struct irq_work *work);
1720
void irq_work_run(void);
18-
void irq_work_sync(struct irq_work *entry);
21+
void irq_work_sync(struct irq_work *work);
1922

2023
#endif /* _LINUX_IRQ_WORK_H */

‎include/linux/llist.h‎

Lines changed: 69 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,30 @@
3535
*
3636
* The basic atomic operation of this list is cmpxchg on long. On
3737
* architectures that don't have NMI-safe cmpxchg implementation, the
38-
* list can NOT be used in NMI handler. So code uses the list in NMI
39-
* handler should depend on CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG.
38+
* list can NOT be used in NMI handlers. So code that uses the list in
39+
* an NMI handler should depend on CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG.
40+
*
41+
* Copyright 2010,2011 Intel Corp.
42+
* Author: Huang Ying <ying.huang@intel.com>
43+
*
44+
* This program is free software; you can redistribute it and/or
45+
* modify it under the terms of the GNU General Public License version
46+
* 2 as published by the Free Software Foundation;
47+
*
48+
* This program is distributed in the hope that it will be useful,
49+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
50+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
51+
* GNU General Public License for more details.
52+
*
53+
* You should have received a copy of the GNU General Public License
54+
* along with this program; if not, write to the Free Software
55+
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
4056
*/
4157

58+
#include <linux/kernel.h>
59+
#include <asm/system.h>
60+
#include <asm/processor.h>
61+
4262
struct llist_head {
4363
struct llist_node *first;
4464
};
@@ -113,14 +133,55 @@ static inline void init_llist_head(struct llist_head *list)
113133
* test whether the list is empty without deleting something from the
114134
* list.
115135
*/
116-
static inline int llist_empty(const struct llist_head *head)
136+
static inline bool llist_empty(const struct llist_head *head)
117137
{
118138
return ACCESS_ONCE(head->first) == NULL;
119139
}
120140

121-
void llist_add(struct llist_node *new, struct llist_head *head);
122-
void llist_add_batch(struct llist_node *new_first, struct llist_node *new_last,
123-
struct llist_head *head);
124-
struct llist_node *llist_del_first(struct llist_head *head);
125-
struct llist_node *llist_del_all(struct llist_head *head);
141+
static inline struct llist_node *llist_next(struct llist_node *node)
142+
{
143+
return node->next;
144+
}
145+
146+
/**
147+
* llist_add - add a new entry
148+
* @new: new entry to be added
149+
* @head: the head for your lock-less list
150+
*
151+
* Return whether list is empty before adding.
152+
*/
153+
static inline bool llist_add(struct llist_node *new, struct llist_head *head)
154+
{
155+
struct llist_node *entry, *old_entry;
156+
157+
entry = head->first;
158+
for (;;) {
159+
old_entry = entry;
160+
new->next = entry;
161+
entry = cmpxchg(&head->first, old_entry, new);
162+
if (entry == old_entry)
163+
break;
164+
}
165+
166+
return old_entry == NULL;
167+
}
168+
169+
/**
170+
* llist_del_all - delete all entries from lock-less list
171+
* @head: the head of lock-less list to delete all entries
172+
*
173+
* If list is empty, return NULL, otherwise, delete all entries and
174+
* return the pointer to the first entry. The order of entries
175+
* deleted is from the newest to the oldest added one.
176+
*/
177+
static inline struct llist_node *llist_del_all(struct llist_head *head)
178+
{
179+
return xchg(&head->first, NULL);
180+
}
181+
182+
extern bool llist_add_batch(struct llist_node *new_first,
183+
struct llist_node *new_last,
184+
struct llist_head *head);
185+
extern struct llist_node *llist_del_first(struct llist_head *head);
186+
126187
#endif /* LLIST_H */

‎include/linux/sched.h‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ struct sched_param {
9090
#include <linux/task_io_accounting.h>
9191
#include <linux/latencytop.h>
9292
#include <linux/cred.h>
93+
#include <linux/llist.h>
9394

9495
#include <asm/processor.h>
9596

@@ -1224,7 +1225,7 @@ struct task_struct {
12241225
unsigned int ptrace;
12251226

12261227
#ifdef CONFIG_SMP
1227-
struct task_struct *wake_entry;
1228+
struct llist_node wake_entry;
12281229
int on_cpu;
12291230
#endif
12301231
int on_rq;
@@ -2035,6 +2036,10 @@ static inline void sched_autogroup_fork(struct signal_struct *sig) { }
20352036
static inline void sched_autogroup_exit(struct signal_struct *sig) { }
20362037
#endif
20372038

2039+
#ifdef CONFIG_CFS_BANDWIDTH
2040+
extern unsigned int sysctl_sched_cfs_bandwidth_slice;
2041+
#endif
2042+
20382043
#ifdef CONFIG_RT_MUTEXES
20392044
extern int rt_mutex_getprio(struct task_struct *p);
20402045
extern void rt_mutex_setprio(struct task_struct *p, int prio);

‎include/trace/events/sched.h‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ static inline long __trace_sched_switch_state(struct task_struct *p)
100100
* For all intents and purposes a preempted task is a running task.
101101
*/
102102
if (task_thread_info(p)->preempt_count & PREEMPT_ACTIVE)
103-
state = TASK_RUNNING;
103+
state = TASK_RUNNING | TASK_STATE_MAX;
104104
#endif
105105

106106
return state;
@@ -137,13 +137,14 @@ TRACE_EVENT(sched_switch,
137137
__entry->next_prio = next->prio;
138138
),
139139

140-
TP_printk("prev_comm=%s prev_pid=%d prev_prio=%d prev_state=%s ==> next_comm=%s next_pid=%d next_prio=%d",
140+
TP_printk("prev_comm=%s prev_pid=%d prev_prio=%d prev_state=%s%s ==> next_comm=%s next_pid=%d next_prio=%d",
141141
__entry->prev_comm, __entry->prev_pid, __entry->prev_prio,
142-
__entry->prev_state ?
143-
__print_flags(__entry->prev_state, "|",
142+
__entry->prev_state & (TASK_STATE_MAX-1) ?
143+
__print_flags(__entry->prev_state & (TASK_STATE_MAX-1), "|",
144144
{ 1, "S"} , { 2, "D" }, { 4, "T" }, { 8, "t" },
145145
{ 16, "Z" }, { 32, "X" }, { 64, "x" },
146146
{ 128, "W" }) : "R",
147+
__entry->prev_state & TASK_STATE_MAX ? "+" : "",
147148
__entry->next_comm, __entry->next_pid, __entry->next_prio)
148149
);
149150

‎init/Kconfig‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,18 @@ config FAIR_GROUP_SCHED
715715
depends on CGROUP_SCHED
716716
default CGROUP_SCHED
717717

718+
config CFS_BANDWIDTH
719+
bool "CPU bandwidth provisioning for FAIR_GROUP_SCHED"
720+
depends on EXPERIMENTAL
721+
depends on FAIR_GROUP_SCHED
722+
default n
723+
help
724+
This option allows users to define CPU bandwidth rates (limits) for
725+
tasks running within the fair group scheduler. Groups with no limit
726+
set are considered to be unconstrained and will run with no
727+
restriction.
728+
See tip/Documentation/scheduler/sched-bwc.txt for more information.
729+
718730
config RT_GROUP_SCHED
719731
bool "Group scheduling for SCHED_RR/FIFO"
720732
depends on EXPERIMENTAL

0 commit comments

Comments
 (0)