Skip to content

Commit 466c190

Browse files
committed
Merge branch 'sched-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/linux-2.6-tip
* 'sched-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/linux-2.6-tip: sched, cgroup: Use exit hook to avoid use-after-free crash sched: Fix signed unsigned comparison in check_preempt_tick() sched: Replace rq->bkl_count with rq->rq_sched_info.bkl_count sched, autogroup: Fix CONFIG_RT_GROUP_SCHED sched_setscheduler() failure sched: Display autogroup names in /proc/sched_debug sched: Reinstate group names in /proc/sched_debug sched: Update effective_load() to use global share weights
2 parents 67290f4 + 068c5cc commit 466c190

5 files changed

Lines changed: 117 additions & 22 deletions

File tree

‎kernel/sched.c‎

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -553,9 +553,6 @@ struct rq {
553553
/* try_to_wake_up() stats */
554554
unsigned int ttwu_count;
555555
unsigned int ttwu_local;
556-
557-
/* BKL stats */
558-
unsigned int bkl_count;
559556
#endif
560557
};
561558

@@ -609,6 +606,9 @@ static inline struct task_group *task_group(struct task_struct *p)
609606
struct task_group *tg;
610607
struct cgroup_subsys_state *css;
611608

609+
if (p->flags & PF_EXITING)
610+
return &root_task_group;
611+
612612
css = task_subsys_state_check(p, cpu_cgroup_subsys_id,
613613
lockdep_is_held(&task_rq(p)->lock));
614614
tg = container_of(css, struct task_group, css);
@@ -3887,7 +3887,7 @@ static inline void schedule_debug(struct task_struct *prev)
38873887
schedstat_inc(this_rq(), sched_count);
38883888
#ifdef CONFIG_SCHEDSTATS
38893889
if (unlikely(prev->lock_depth >= 0)) {
3890-
schedstat_inc(this_rq(), bkl_count);
3890+
schedstat_inc(this_rq(), rq_sched_info.bkl_count);
38913891
schedstat_inc(prev, sched_info.bkl_count);
38923892
}
38933893
#endif
@@ -4871,7 +4871,8 @@ static int __sched_setscheduler(struct task_struct *p, int policy,
48714871
* assigned.
48724872
*/
48734873
if (rt_bandwidth_enabled() && rt_policy(policy) &&
4874-
task_group(p)->rt_bandwidth.rt_runtime == 0) {
4874+
task_group(p)->rt_bandwidth.rt_runtime == 0 &&
4875+
!task_group_is_autogroup(task_group(p))) {
48754876
__task_rq_unlock(rq);
48764877
raw_spin_unlock_irqrestore(&p->pi_lock, flags);
48774878
return -EPERM;
@@ -8882,6 +8883,20 @@ cpu_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
88828883
}
88838884
}
88848885

8886+
static void
8887+
cpu_cgroup_exit(struct cgroup_subsys *ss, struct task_struct *task)
8888+
{
8889+
/*
8890+
* cgroup_exit() is called in the copy_process() failure path.
8891+
* Ignore this case since the task hasn't ran yet, this avoids
8892+
* trying to poke a half freed task state from generic code.
8893+
*/
8894+
if (!(task->flags & PF_EXITING))
8895+
return;
8896+
8897+
sched_move_task(task);
8898+
}
8899+
88858900
#ifdef CONFIG_FAIR_GROUP_SCHED
88868901
static int cpu_shares_write_u64(struct cgroup *cgrp, struct cftype *cftype,
88878902
u64 shareval)
@@ -8954,6 +8969,7 @@ struct cgroup_subsys cpu_cgroup_subsys = {
89548969
.destroy = cpu_cgroup_destroy,
89558970
.can_attach = cpu_cgroup_can_attach,
89568971
.attach = cpu_cgroup_attach,
8972+
.exit = cpu_cgroup_exit,
89578973
.populate = cpu_cgroup_populate,
89588974
.subsys_id = cpu_cgroup_subsys_id,
89598975
.early_init = 1,

‎kernel/sched_autogroup.c‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ static inline void autogroup_destroy(struct kref *kref)
2727
{
2828
struct autogroup *ag = container_of(kref, struct autogroup, kref);
2929

30+
#ifdef CONFIG_RT_GROUP_SCHED
31+
/* We've redirected RT tasks to the root task group... */
32+
ag->tg->rt_se = NULL;
33+
ag->tg->rt_rq = NULL;
34+
#endif
3035
sched_destroy_group(ag->tg);
3136
}
3237

@@ -55,6 +60,10 @@ static inline struct autogroup *autogroup_task_get(struct task_struct *p)
5560
return ag;
5661
}
5762

63+
#ifdef CONFIG_RT_GROUP_SCHED
64+
static void free_rt_sched_group(struct task_group *tg);
65+
#endif
66+
5867
static inline struct autogroup *autogroup_create(void)
5968
{
6069
struct autogroup *ag = kzalloc(sizeof(*ag), GFP_KERNEL);
@@ -72,6 +81,19 @@ static inline struct autogroup *autogroup_create(void)
7281
init_rwsem(&ag->lock);
7382
ag->id = atomic_inc_return(&autogroup_seq_nr);
7483
ag->tg = tg;
84+
#ifdef CONFIG_RT_GROUP_SCHED
85+
/*
86+
* Autogroup RT tasks are redirected to the root task group
87+
* so we don't have to move tasks around upon policy change,
88+
* or flail around trying to allocate bandwidth on the fly.
89+
* A bandwidth exception in __sched_setscheduler() allows
90+
* the policy change to proceed. Thereafter, task_group()
91+
* returns &root_task_group, so zero bandwidth is required.
92+
*/
93+
free_rt_sched_group(tg);
94+
tg->rt_se = root_task_group.rt_se;
95+
tg->rt_rq = root_task_group.rt_rq;
96+
#endif
7597
tg->autogroup = ag;
7698

7799
return ag;
@@ -106,6 +128,11 @@ task_wants_autogroup(struct task_struct *p, struct task_group *tg)
106128
return true;
107129
}
108130

131+
static inline bool task_group_is_autogroup(struct task_group *tg)
132+
{
133+
return tg != &root_task_group && tg->autogroup;
134+
}
135+
109136
static inline struct task_group *
110137
autogroup_task_group(struct task_struct *p, struct task_group *tg)
111138
{
@@ -231,6 +258,11 @@ void proc_sched_autogroup_show_task(struct task_struct *p, struct seq_file *m)
231258
#ifdef CONFIG_SCHED_DEBUG
232259
static inline int autogroup_path(struct task_group *tg, char *buf, int buflen)
233260
{
261+
int enabled = ACCESS_ONCE(sysctl_sched_autogroup_enabled);
262+
263+
if (!enabled || !tg->autogroup)
264+
return 0;
265+
234266
return snprintf(buf, buflen, "%s-%ld", "/autogroup", tg->autogroup->id);
235267
}
236268
#endif /* CONFIG_SCHED_DEBUG */

‎kernel/sched_autogroup.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ autogroup_task_group(struct task_struct *p, struct task_group *tg);
1515

1616
static inline void autogroup_init(struct task_struct *init_task) { }
1717
static inline void autogroup_free(struct task_group *tg) { }
18+
static inline bool task_group_is_autogroup(struct task_group *tg)
19+
{
20+
return 0;
21+
}
1822

1923
static inline struct task_group *
2024
autogroup_task_group(struct task_struct *p, struct task_group *tg)

‎kernel/sched_debug.c‎

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#include <linux/kallsyms.h>
1717
#include <linux/utsname.h>
1818

19+
static DEFINE_SPINLOCK(sched_debug_lock);
20+
1921
/*
2022
* This allows printing both to /proc/sched_debug and
2123
* to the console
@@ -86,6 +88,26 @@ static void print_cfs_group_stats(struct seq_file *m, int cpu, struct task_group
8688
}
8789
#endif
8890

91+
#ifdef CONFIG_CGROUP_SCHED
92+
static char group_path[PATH_MAX];
93+
94+
static char *task_group_path(struct task_group *tg)
95+
{
96+
if (autogroup_path(tg, group_path, PATH_MAX))
97+
return group_path;
98+
99+
/*
100+
* May be NULL if the underlying cgroup isn't fully-created yet
101+
*/
102+
if (!tg->css.cgroup) {
103+
group_path[0] = '\0';
104+
return group_path;
105+
}
106+
cgroup_path(tg->css.cgroup, group_path, PATH_MAX);
107+
return group_path;
108+
}
109+
#endif
110+
89111
static void
90112
print_task(struct seq_file *m, struct rq *rq, struct task_struct *p)
91113
{
@@ -108,6 +130,9 @@ print_task(struct seq_file *m, struct rq *rq, struct task_struct *p)
108130
SEQ_printf(m, "%15Ld %15Ld %15Ld.%06ld %15Ld.%06ld %15Ld.%06ld",
109131
0LL, 0LL, 0LL, 0L, 0LL, 0L, 0LL, 0L);
110132
#endif
133+
#ifdef CONFIG_CGROUP_SCHED
134+
SEQ_printf(m, " %s", task_group_path(task_group(p)));
135+
#endif
111136

112137
SEQ_printf(m, "\n");
113138
}
@@ -144,7 +169,11 @@ void print_cfs_rq(struct seq_file *m, int cpu, struct cfs_rq *cfs_rq)
144169
struct sched_entity *last;
145170
unsigned long flags;
146171

172+
#ifdef CONFIG_FAIR_GROUP_SCHED
173+
SEQ_printf(m, "\ncfs_rq[%d]:%s\n", cpu, task_group_path(cfs_rq->tg));
174+
#else
147175
SEQ_printf(m, "\ncfs_rq[%d]:\n", cpu);
176+
#endif
148177
SEQ_printf(m, " .%-30s: %Ld.%06ld\n", "exec_clock",
149178
SPLIT_NS(cfs_rq->exec_clock));
150179

@@ -191,7 +220,11 @@ void print_cfs_rq(struct seq_file *m, int cpu, struct cfs_rq *cfs_rq)
191220

192221
void print_rt_rq(struct seq_file *m, int cpu, struct rt_rq *rt_rq)
193222
{
223+
#ifdef CONFIG_RT_GROUP_SCHED
224+
SEQ_printf(m, "\nrt_rq[%d]:%s\n", cpu, task_group_path(rt_rq->tg));
225+
#else
194226
SEQ_printf(m, "\nrt_rq[%d]:\n", cpu);
227+
#endif
195228

196229
#define P(x) \
197230
SEQ_printf(m, " .%-30s: %Ld\n", #x, (long long)(rt_rq->x))
@@ -212,6 +245,7 @@ extern __read_mostly int sched_clock_running;
212245
static void print_cpu(struct seq_file *m, int cpu)
213246
{
214247
struct rq *rq = cpu_rq(cpu);
248+
unsigned long flags;
215249

216250
#ifdef CONFIG_X86
217251
{
@@ -262,14 +296,20 @@ static void print_cpu(struct seq_file *m, int cpu)
262296
P(ttwu_count);
263297
P(ttwu_local);
264298

265-
P(bkl_count);
299+
SEQ_printf(m, " .%-30s: %d\n", "bkl_count",
300+
rq->rq_sched_info.bkl_count);
266301

267302
#undef P
303+
#undef P64
268304
#endif
305+
spin_lock_irqsave(&sched_debug_lock, flags);
269306
print_cfs_stats(m, cpu);
270307
print_rt_stats(m, cpu);
271308

309+
rcu_read_lock();
272310
print_rq(m, rq, cpu);
311+
rcu_read_unlock();
312+
spin_unlock_irqrestore(&sched_debug_lock, flags);
273313
}
274314

275315
static const char *sched_tunable_scaling_names[] = {

‎kernel/sched_fair.c‎

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,6 +1062,9 @@ check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
10621062
struct sched_entity *se = __pick_next_entity(cfs_rq);
10631063
s64 delta = curr->vruntime - se->vruntime;
10641064

1065+
if (delta < 0)
1066+
return;
1067+
10651068
if (delta > ideal_runtime)
10661069
resched_task(rq_of(cfs_rq)->curr);
10671070
}
@@ -1362,27 +1365,27 @@ static long effective_load(struct task_group *tg, int cpu, long wl, long wg)
13621365
return wl;
13631366

13641367
for_each_sched_entity(se) {
1365-
long S, rw, s, a, b;
1368+
long lw, w;
13661369

1367-
S = se->my_q->tg->shares;
1368-
s = se->load.weight;
1369-
rw = se->my_q->load.weight;
1370+
tg = se->my_q->tg;
1371+
w = se->my_q->load.weight;
13701372

1371-
a = S*(rw + wl);
1372-
b = S*rw + s*wg;
1373+
/* use this cpu's instantaneous contribution */
1374+
lw = atomic_read(&tg->load_weight);
1375+
lw -= se->my_q->load_contribution;
1376+
lw += w + wg;
13731377

1374-
wl = s*(a-b);
1378+
wl += w;
13751379

1376-
if (likely(b))
1377-
wl /= b;
1380+
if (lw > 0 && wl < lw)
1381+
wl = (wl * tg->shares) / lw;
1382+
else
1383+
wl = tg->shares;
13781384

1379-
/*
1380-
* Assume the group is already running and will
1381-
* thus already be accounted for in the weight.
1382-
*
1383-
* That is, moving shares between CPUs, does not
1384-
* alter the group weight.
1385-
*/
1385+
/* zero point is MIN_SHARES */
1386+
if (wl < MIN_SHARES)
1387+
wl = MIN_SHARES;
1388+
wl -= se->load.weight;
13861389
wg = 0;
13871390
}
13881391

0 commit comments

Comments
 (0)