rt.c 47.1 KB
Newer Older
1 2 3 4 5
/*
 * Real-Time Scheduling Class (mapped to the SCHED_FIFO and SCHED_RR
 * policies)
 */

6 7 8 9
#include "sched.h"

#include <linux/slab.h>

10 11
int sched_rr_timeslice = RR_TIMESLICE;

12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun);

struct rt_bandwidth def_rt_bandwidth;

static enum hrtimer_restart sched_rt_period_timer(struct hrtimer *timer)
{
	struct rt_bandwidth *rt_b =
		container_of(timer, struct rt_bandwidth, rt_period_timer);
	ktime_t now;
	int overrun;
	int idle = 0;

	for (;;) {
		now = hrtimer_cb_get_time(timer);
		overrun = hrtimer_forward(timer, now, rt_b->rt_period);

		if (!overrun)
			break;

		idle = do_sched_rt_period_timer(rt_b, overrun);
	}

	return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
}

void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime)
{
	rt_b->rt_period = ns_to_ktime(period);
	rt_b->rt_runtime = runtime;

	raw_spin_lock_init(&rt_b->rt_runtime_lock);

	hrtimer_init(&rt_b->rt_period_timer,
			CLOCK_MONOTONIC, HRTIMER_MODE_REL);
	rt_b->rt_period_timer.function = sched_rt_period_timer;
}

static void start_rt_bandwidth(struct rt_bandwidth *rt_b)
{
	if (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF)
		return;

	if (hrtimer_active(&rt_b->rt_period_timer))
		return;

	raw_spin_lock(&rt_b->rt_runtime_lock);
	start_bandwidth_timer(&rt_b->rt_period_timer, rt_b->rt_period);
	raw_spin_unlock(&rt_b->rt_runtime_lock);
}

void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq)
{
	struct rt_prio_array *array;
	int i;

	array = &rt_rq->active;
	for (i = 0; i < MAX_RT_PRIO; i++) {
		INIT_LIST_HEAD(array->queue + i);
		__clear_bit(i, array->bitmap);
	}
	/* delimiter for bitsearch: */
	__set_bit(MAX_RT_PRIO, array->bitmap);

#if defined CONFIG_SMP
	rt_rq->highest_prio.curr = MAX_RT_PRIO;
	rt_rq->highest_prio.next = MAX_RT_PRIO;
	rt_rq->rt_nr_migratory = 0;
	rt_rq->overloaded = 0;
	plist_head_init(&rt_rq->pushable_tasks);
#endif

	rt_rq->rt_time = 0;
	rt_rq->rt_throttled = 0;
	rt_rq->rt_runtime = 0;
	raw_spin_lock_init(&rt_rq->rt_runtime_lock);
}

89
#ifdef CONFIG_RT_GROUP_SCHED
90 91 92 93
static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b)
{
	hrtimer_cancel(&rt_b->rt_period_timer);
}
94 95 96

#define rt_entity_is_task(rt_se) (!(rt_se)->my_q)

97 98
static inline struct task_struct *rt_task_of(struct sched_rt_entity *rt_se)
{
99 100 101
#ifdef CONFIG_SCHED_DEBUG
	WARN_ON_ONCE(!rt_entity_is_task(rt_se));
#endif
102 103 104 105 106 107 108 109 110 111 112 113 114
	return container_of(rt_se, struct task_struct, rt);
}

static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq)
{
	return rt_rq->rq;
}

static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se)
{
	return rt_se->rt_rq;
}

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
void free_rt_sched_group(struct task_group *tg)
{
	int i;

	if (tg->rt_se)
		destroy_rt_bandwidth(&tg->rt_bandwidth);

	for_each_possible_cpu(i) {
		if (tg->rt_rq)
			kfree(tg->rt_rq[i]);
		if (tg->rt_se)
			kfree(tg->rt_se[i]);
	}

	kfree(tg->rt_rq);
	kfree(tg->rt_se);
}

void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
		struct sched_rt_entity *rt_se, int cpu,
		struct sched_rt_entity *parent)
{
	struct rq *rq = cpu_rq(cpu);

	rt_rq->highest_prio.curr = MAX_RT_PRIO;
	rt_rq->rt_nr_boosted = 0;
	rt_rq->rq = rq;
	rt_rq->tg = tg;

	tg->rt_rq[cpu] = rt_rq;
	tg->rt_se[cpu] = rt_se;

	if (!rt_se)
		return;

	if (!parent)
		rt_se->rt_rq = &rq->rt;
	else
		rt_se->rt_rq = parent->my_q;

	rt_se->my_q = rt_rq;
	rt_se->parent = parent;
	INIT_LIST_HEAD(&rt_se->run_list);
}

int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
{
	struct rt_rq *rt_rq;
	struct sched_rt_entity *rt_se;
	int i;

	tg->rt_rq = kzalloc(sizeof(rt_rq) * nr_cpu_ids, GFP_KERNEL);
	if (!tg->rt_rq)
		goto err;
	tg->rt_se = kzalloc(sizeof(rt_se) * nr_cpu_ids, GFP_KERNEL);
	if (!tg->rt_se)
		goto err;

	init_rt_bandwidth(&tg->rt_bandwidth,
			ktime_to_ns(def_rt_bandwidth.rt_period), 0);

	for_each_possible_cpu(i) {
		rt_rq = kzalloc_node(sizeof(struct rt_rq),
				     GFP_KERNEL, cpu_to_node(i));
		if (!rt_rq)
			goto err;

		rt_se = kzalloc_node(sizeof(struct sched_rt_entity),
				     GFP_KERNEL, cpu_to_node(i));
		if (!rt_se)
			goto err_free_rq;

		init_rt_rq(rt_rq, cpu_rq(i));
		rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime;
		init_tg_rt_entry(tg, rt_rq, rt_se, i, parent->rt_se[i]);
	}

	return 1;

err_free_rq:
	kfree(rt_rq);
err:
	return 0;
}

200 201
#else /* CONFIG_RT_GROUP_SCHED */

202 203
#define rt_entity_is_task(rt_se) (1)

204 205 206 207 208
static inline struct task_struct *rt_task_of(struct sched_rt_entity *rt_se)
{
	return container_of(rt_se, struct task_struct, rt);
}

209 210 211 212 213 214 215 216 217 218 219 220 221
static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq)
{
	return container_of(rt_rq, struct rq, rt);
}

static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se)
{
	struct task_struct *p = rt_task_of(rt_se);
	struct rq *rq = task_rq(p);

	return &rq->rt;
}

222 223 224 225 226 227
void free_rt_sched_group(struct task_group *tg) { }

int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
{
	return 1;
}
228 229
#endif /* CONFIG_RT_GROUP_SCHED */

230
#ifdef CONFIG_SMP
231

232
static inline int rt_overloaded(struct rq *rq)
233
{
234
	return atomic_read(&rq->rd->rto_count);
235
}
236

237 238
static inline void rt_set_overload(struct rq *rq)
{
239 240 241
	if (!rq->online)
		return;

242
	cpumask_set_cpu(rq->cpu, rq->rd->rto_mask);
243 244 245 246 247 248 249 250
	/*
	 * Make sure the mask is visible before we set
	 * the overload count. That is checked to determine
	 * if we should look at the mask. It would be a shame
	 * if we looked at the mask, but the mask was not
	 * updated yet.
	 */
	wmb();
251
	atomic_inc(&rq->rd->rto_count);
252
}
253

254 255
static inline void rt_clear_overload(struct rq *rq)
{
256 257 258
	if (!rq->online)
		return;

259
	/* the order here really doesn't matter */
260
	atomic_dec(&rq->rd->rto_count);
261
	cpumask_clear_cpu(rq->cpu, rq->rd->rto_mask);
262
}
263

264
static void update_rt_migration(struct rt_rq *rt_rq)
265
{
266
	if (rt_rq->rt_nr_migratory && rt_rq->rt_nr_total > 1) {
267 268 269
		if (!rt_rq->overloaded) {
			rt_set_overload(rq_of_rt_rq(rt_rq));
			rt_rq->overloaded = 1;
270
		}
271 272 273
	} else if (rt_rq->overloaded) {
		rt_clear_overload(rq_of_rt_rq(rt_rq));
		rt_rq->overloaded = 0;
274
	}
275
}
276

277 278
static void inc_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
{
279 280
	struct task_struct *p;

281 282 283
	if (!rt_entity_is_task(rt_se))
		return;

284
	p = rt_task_of(rt_se);
285 286 287
	rt_rq = &rq_of_rt_rq(rt_rq)->rt;

	rt_rq->rt_nr_total++;
288
	if (p->nr_cpus_allowed > 1)
289 290 291 292 293 294 295
		rt_rq->rt_nr_migratory++;

	update_rt_migration(rt_rq);
}

static void dec_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
{
296 297
	struct task_struct *p;

298 299 300
	if (!rt_entity_is_task(rt_se))
		return;

301
	p = rt_task_of(rt_se);
302 303 304
	rt_rq = &rq_of_rt_rq(rt_rq)->rt;

	rt_rq->rt_nr_total--;
305
	if (p->nr_cpus_allowed > 1)
306 307 308 309 310
		rt_rq->rt_nr_migratory--;

	update_rt_migration(rt_rq);
}

311 312 313 314 315
static inline int has_pushable_tasks(struct rq *rq)
{
	return !plist_head_empty(&rq->rt.pushable_tasks);
}

316 317 318 319 320
static void enqueue_pushable_task(struct rq *rq, struct task_struct *p)
{
	plist_del(&p->pushable_tasks, &rq->rt.pushable_tasks);
	plist_node_init(&p->pushable_tasks, p->prio);
	plist_add(&p->pushable_tasks, &rq->rt.pushable_tasks);
321 322 323 324

	/* Update the highest prio pushable task */
	if (p->prio < rq->rt.highest_prio.next)
		rq->rt.highest_prio.next = p->prio;
325 326 327 328 329 330
}

static void dequeue_pushable_task(struct rq *rq, struct task_struct *p)
{
	plist_del(&p->pushable_tasks, &rq->rt.pushable_tasks);

331 332 333 334 335 336 337
	/* Update the new highest prio pushable task */
	if (has_pushable_tasks(rq)) {
		p = plist_first_entry(&rq->rt.pushable_tasks,
				      struct task_struct, pushable_tasks);
		rq->rt.highest_prio.next = p->prio;
	} else
		rq->rt.highest_prio.next = MAX_RT_PRIO;
338 339
}

340 341
#else

342
static inline void enqueue_pushable_task(struct rq *rq, struct task_struct *p)
Peter Zijlstra's avatar
Peter Zijlstra committed
343
{
Peter Zijlstra's avatar
Peter Zijlstra committed
344 345
}

346 347 348 349
static inline void dequeue_pushable_task(struct rq *rq, struct task_struct *p)
{
}

350
static inline
351 352 353 354
void inc_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
{
}

355
static inline
356 357 358
void dec_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq)
{
}
359

360 361
#endif /* CONFIG_SMP */

Peter Zijlstra's avatar
Peter Zijlstra committed
362 363 364 365 366
static inline int on_rt_rq(struct sched_rt_entity *rt_se)
{
	return !list_empty(&rt_se->run_list);
}

367
#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra's avatar
Peter Zijlstra committed
368

Peter Zijlstra's avatar
Peter Zijlstra committed
369
static inline u64 sched_rt_runtime(struct rt_rq *rt_rq)
Peter Zijlstra's avatar
Peter Zijlstra committed
370 371
{
	if (!rt_rq->tg)
Peter Zijlstra's avatar
Peter Zijlstra committed
372
		return RUNTIME_INF;
Peter Zijlstra's avatar
Peter Zijlstra committed
373

374 375 376 377 378 379
	return rt_rq->rt_runtime;
}

static inline u64 sched_rt_period(struct rt_rq *rt_rq)
{
	return ktime_to_ns(rt_rq->tg->rt_bandwidth.rt_period);
Peter Zijlstra's avatar
Peter Zijlstra committed
380 381
}

382 383
typedef struct task_group *rt_rq_iter_t;

384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
static inline struct task_group *next_task_group(struct task_group *tg)
{
	do {
		tg = list_entry_rcu(tg->list.next,
			typeof(struct task_group), list);
	} while (&tg->list != &task_groups && task_group_is_autogroup(tg));

	if (&tg->list == &task_groups)
		tg = NULL;

	return tg;
}

#define for_each_rt_rq(rt_rq, iter, rq)					\
	for (iter = container_of(&task_groups, typeof(*iter), list);	\
		(iter = next_task_group(iter)) &&			\
		(rt_rq = iter->rt_rq[cpu_of(rq)]);)
401

402 403 404 405 406 407 408 409 410 411 412
static inline void list_add_leaf_rt_rq(struct rt_rq *rt_rq)
{
	list_add_rcu(&rt_rq->leaf_rt_rq_list,
			&rq_of_rt_rq(rt_rq)->leaf_rt_rq_list);
}

static inline void list_del_leaf_rt_rq(struct rt_rq *rt_rq)
{
	list_del_rcu(&rt_rq->leaf_rt_rq_list);
}

Peter Zijlstra's avatar
Peter Zijlstra committed
413
#define for_each_leaf_rt_rq(rt_rq, rq) \
414
	list_for_each_entry_rcu(rt_rq, &rq->leaf_rt_rq_list, leaf_rt_rq_list)
Peter Zijlstra's avatar
Peter Zijlstra committed
415 416 417 418 419 420 421 422 423

#define for_each_sched_rt_entity(rt_se) \
	for (; rt_se; rt_se = rt_se->parent)

static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se)
{
	return rt_se->my_q;
}

424
static void enqueue_rt_entity(struct sched_rt_entity *rt_se, bool head);
Peter Zijlstra's avatar
Peter Zijlstra committed
425 426
static void dequeue_rt_entity(struct sched_rt_entity *rt_se);

Peter Zijlstra's avatar
Peter Zijlstra committed
427
static void sched_rt_rq_enqueue(struct rt_rq *rt_rq)
Peter Zijlstra's avatar
Peter Zijlstra committed
428
{
429
	struct task_struct *curr = rq_of_rt_rq(rt_rq)->curr;
430 431
	struct sched_rt_entity *rt_se;

432 433 434
	int cpu = cpu_of(rq_of_rt_rq(rt_rq));

	rt_se = rt_rq->tg->rt_se[cpu];
Peter Zijlstra's avatar
Peter Zijlstra committed
435

436 437
	if (rt_rq->rt_nr_running) {
		if (rt_se && !on_rt_rq(rt_se))
438
			enqueue_rt_entity(rt_se, false);
439
		if (rt_rq->highest_prio.curr < curr->prio)
440
			resched_task(curr);
Peter Zijlstra's avatar
Peter Zijlstra committed
441 442 443
	}
}

Peter Zijlstra's avatar
Peter Zijlstra committed
444
static void sched_rt_rq_dequeue(struct rt_rq *rt_rq)
Peter Zijlstra's avatar
Peter Zijlstra committed
445
{
446
	struct sched_rt_entity *rt_se;
447
	int cpu = cpu_of(rq_of_rt_rq(rt_rq));
448

449
	rt_se = rt_rq->tg->rt_se[cpu];
Peter Zijlstra's avatar
Peter Zijlstra committed
450 451 452 453 454

	if (rt_se && on_rt_rq(rt_se))
		dequeue_rt_entity(rt_se);
}

Peter Zijlstra's avatar
Peter Zijlstra committed
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
static inline int rt_rq_throttled(struct rt_rq *rt_rq)
{
	return rt_rq->rt_throttled && !rt_rq->rt_nr_boosted;
}

static int rt_se_boosted(struct sched_rt_entity *rt_se)
{
	struct rt_rq *rt_rq = group_rt_rq(rt_se);
	struct task_struct *p;

	if (rt_rq)
		return !!rt_rq->rt_nr_boosted;

	p = rt_task_of(rt_se);
	return p->prio != p->normal_prio;
}

472
#ifdef CONFIG_SMP
473
static inline const struct cpumask *sched_rt_period_mask(void)
474 475 476
{
	return cpu_rq(smp_processor_id())->rd->span;
}
Peter Zijlstra's avatar
Peter Zijlstra committed
477
#else
478
static inline const struct cpumask *sched_rt_period_mask(void)
479
{
480
	return cpu_online_mask;
481 482
}
#endif
Peter Zijlstra's avatar
Peter Zijlstra committed
483

484 485
static inline
struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu)
Peter Zijlstra's avatar
Peter Zijlstra committed
486
{
487 488
	return container_of(rt_b, struct task_group, rt_bandwidth)->rt_rq[cpu];
}
Peter Zijlstra's avatar
Peter Zijlstra committed
489

490 491 492 493 494
static inline struct rt_bandwidth *sched_rt_bandwidth(struct rt_rq *rt_rq)
{
	return &rt_rq->tg->rt_bandwidth;
}

495
#else /* !CONFIG_RT_GROUP_SCHED */
496 497 498

static inline u64 sched_rt_runtime(struct rt_rq *rt_rq)
{
499 500 501 502 503 504
	return rt_rq->rt_runtime;
}

static inline u64 sched_rt_period(struct rt_rq *rt_rq)
{
	return ktime_to_ns(def_rt_bandwidth.rt_period);
Peter Zijlstra's avatar
Peter Zijlstra committed
505 506
}

507 508 509 510 511
typedef struct rt_rq *rt_rq_iter_t;

#define for_each_rt_rq(rt_rq, iter, rq) \
	for ((void) iter, rt_rq = &rq->rt; rt_rq; rt_rq = NULL)

512 513 514 515 516 517 518 519
static inline void list_add_leaf_rt_rq(struct rt_rq *rt_rq)
{
}

static inline void list_del_leaf_rt_rq(struct rt_rq *rt_rq)
{
}

Peter Zijlstra's avatar
Peter Zijlstra committed
520 521 522 523 524 525 526 527 528 529 530
#define for_each_leaf_rt_rq(rt_rq, rq) \
	for (rt_rq = &rq->rt; rt_rq; rt_rq = NULL)

#define for_each_sched_rt_entity(rt_se) \
	for (; rt_se; rt_se = NULL)

static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se)
{
	return NULL;
}

Peter Zijlstra's avatar
Peter Zijlstra committed
531
static inline void sched_rt_rq_enqueue(struct rt_rq *rt_rq)
Peter Zijlstra's avatar
Peter Zijlstra committed
532
{
533 534
	if (rt_rq->rt_nr_running)
		resched_task(rq_of_rt_rq(rt_rq)->curr);
Peter Zijlstra's avatar
Peter Zijlstra committed
535 536
}

Peter Zijlstra's avatar
Peter Zijlstra committed
537
static inline void sched_rt_rq_dequeue(struct rt_rq *rt_rq)
Peter Zijlstra's avatar
Peter Zijlstra committed
538 539 540
{
}

Peter Zijlstra's avatar
Peter Zijlstra committed
541 542 543 544
static inline int rt_rq_throttled(struct rt_rq *rt_rq)
{
	return rt_rq->rt_throttled;
}
545

546
static inline const struct cpumask *sched_rt_period_mask(void)
547
{
548
	return cpu_online_mask;
549 550 551 552 553 554 555 556
}

static inline
struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu)
{
	return &cpu_rq(cpu)->rt;
}

557 558 559 560 561
static inline struct rt_bandwidth *sched_rt_bandwidth(struct rt_rq *rt_rq)
{
	return &def_rt_bandwidth;
}

562
#endif /* CONFIG_RT_GROUP_SCHED */
563

564
#ifdef CONFIG_SMP
565 566 567
/*
 * We ran out of runtime, see if we can borrow some from our neighbours.
 */
568
static int do_balance_runtime(struct rt_rq *rt_rq)
569 570 571 572 573 574
{
	struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
	struct root_domain *rd = cpu_rq(smp_processor_id())->rd;
	int i, weight, more = 0;
	u64 rt_period;

575
	weight = cpumask_weight(rd->span);
576

577
	raw_spin_lock(&rt_b->rt_runtime_lock);
578
	rt_period = ktime_to_ns(rt_b->rt_period);
579
	for_each_cpu(i, rd->span) {
580 581 582 583 584 585
		struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i);
		s64 diff;

		if (iter == rt_rq)
			continue;

586
		raw_spin_lock(&iter->rt_runtime_lock);
587 588 589 590 591
		/*
		 * Either all rqs have inf runtime and there's nothing to steal
		 * or __disable_runtime() below sets a specific rq to inf to
		 * indicate its been disabled and disalow stealing.
		 */
592 593 594
		if (iter->rt_runtime == RUNTIME_INF)
			goto next;

595 596 597 598
		/*
		 * From runqueues with spare time, take 1/n part of their
		 * spare time, but no more than our period.
		 */
599 600
		diff = iter->rt_runtime - iter->rt_time;
		if (diff > 0) {
601
			diff = div_u64((u64)diff, weight);
602 603 604 605 606 607
			if (rt_rq->rt_runtime + diff > rt_period)
				diff = rt_period - rt_rq->rt_runtime;
			iter->rt_runtime -= diff;
			rt_rq->rt_runtime += diff;
			more = 1;
			if (rt_rq->rt_runtime == rt_period) {
608
				raw_spin_unlock(&iter->rt_runtime_lock);
609 610 611
				break;
			}
		}
612
next:
613
		raw_spin_unlock(&iter->rt_runtime_lock);
614
	}
615
	raw_spin_unlock(&rt_b->rt_runtime_lock);
616 617 618

	return more;
}
619

620 621 622
/*
 * Ensure this RQ takes back all the runtime it lend to its neighbours.
 */
623 624 625
static void __disable_runtime(struct rq *rq)
{
	struct root_domain *rd = rq->rd;
626
	rt_rq_iter_t iter;
627 628 629 630 631
	struct rt_rq *rt_rq;

	if (unlikely(!scheduler_running))
		return;

632
	for_each_rt_rq(rt_rq, iter, rq) {
633 634 635 636
		struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);
		s64 want;
		int i;

637 638
		raw_spin_lock(&rt_b->rt_runtime_lock);
		raw_spin_lock(&rt_rq->rt_runtime_lock);
639 640 641 642 643
		/*
		 * Either we're all inf and nobody needs to borrow, or we're
		 * already disabled and thus have nothing to do, or we have
		 * exactly the right amount of runtime to take out.
		 */
644 645 646
		if (rt_rq->rt_runtime == RUNTIME_INF ||
				rt_rq->rt_runtime == rt_b->rt_runtime)
			goto balanced;
647
		raw_spin_unlock(&rt_rq->rt_runtime_lock);
648

649 650 651 652 653
		/*
		 * Calculate the difference between what we started out with
		 * and what we current have, that's the amount of runtime
		 * we lend and now have to reclaim.
		 */
654 655
		want = rt_b->rt_runtime - rt_rq->rt_runtime;

656 657 658
		/*
		 * Greedy reclaim, take back as much as we can.
		 */
659
		for_each_cpu(i, rd->span) {
660 661 662
			struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i);
			s64 diff;

663 664 665
			/*
			 * Can't reclaim from ourselves or disabled runqueues.
			 */
666
			if (iter == rt_rq || iter->rt_runtime == RUNTIME_INF)
667 668
				continue;

669
			raw_spin_lock(&iter->rt_runtime_lock);
670 671 672 673 674 675 676 677
			if (want > 0) {
				diff = min_t(s64, iter->rt_runtime, want);
				iter->rt_runtime -= diff;
				want -= diff;
			} else {
				iter->rt_runtime -= want;
				want -= want;
			}
678
			raw_spin_unlock(&iter->rt_runtime_lock);
679 680 681 682 683

			if (!want)
				break;
		}

684
		raw_spin_lock(&rt_rq->rt_runtime_lock);
685 686 687 688
		/*
		 * We cannot be left wanting - that would mean some runtime
		 * leaked out of the system.
		 */
689 690
		BUG_ON(want);
balanced:
691 692 693 694
		/*
		 * Disable all the borrow logic by pretending we have inf
		 * runtime - in which case borrowing doesn't make sense.
		 */
695
		rt_rq->rt_runtime = RUNTIME_INF;
696
		rt_rq->rt_throttled = 0;
697 698
		raw_spin_unlock(&rt_rq->rt_runtime_lock);
		raw_spin_unlock(&rt_b->rt_runtime_lock);
699 700 701 702 703 704 705
	}
}

static void disable_runtime(struct rq *rq)
{
	unsigned long flags;

706
	raw_spin_lock_irqsave(&rq->lock, flags);
707
	__disable_runtime(rq);
708
	raw_spin_unlock_irqrestore(&rq->lock, flags);
709 710 711 712
}

static void __enable_runtime(struct rq *rq)
{
713
	rt_rq_iter_t iter;
714 715 716 717 718
	struct rt_rq *rt_rq;

	if (unlikely(!scheduler_running))
		return;

719 720 721
	/*
	 * Reset each runqueue's bandwidth settings
	 */
722
	for_each_rt_rq(rt_rq, iter, rq) {
723 724
		struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);

725 726
		raw_spin_lock(&rt_b->rt_runtime_lock);
		raw_spin_lock(&rt_rq->rt_runtime_lock);
727 728
		rt_rq->rt_runtime = rt_b->rt_runtime;
		rt_rq->rt_time = 0;
729
		rt_rq->rt_throttled = 0;
730 731
		raw_spin_unlock(&rt_rq->rt_runtime_lock);
		raw_spin_unlock(&rt_b->rt_runtime_lock);
732 733 734 735 736 737 738
	}
}

static void enable_runtime(struct rq *rq)
{
	unsigned long flags;

739
	raw_spin_lock_irqsave(&rq->lock, flags);
740
	__enable_runtime(rq);
741
	raw_spin_unlock_irqrestore(&rq->lock, flags);
742 743
}

744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
int update_runtime(struct notifier_block *nfb, unsigned long action, void *hcpu)
{
	int cpu = (int)(long)hcpu;

	switch (action) {
	case CPU_DOWN_PREPARE:
	case CPU_DOWN_PREPARE_FROZEN:
		disable_runtime(cpu_rq(cpu));
		return NOTIFY_OK;

	case CPU_DOWN_FAILED:
	case CPU_DOWN_FAILED_FROZEN:
	case CPU_ONLINE:
	case CPU_ONLINE_FROZEN:
		enable_runtime(cpu_rq(cpu));
		return NOTIFY_OK;

	default:
		return NOTIFY_DONE;
	}
}

766 767 768 769
static int balance_runtime(struct rt_rq *rt_rq)
{
	int more = 0;

770 771 772
	if (!sched_feat(RT_RUNTIME_SHARE))
		return more;

773
	if (rt_rq->rt_time > rt_rq->rt_runtime) {
774
		raw_spin_unlock(&rt_rq->rt_runtime_lock);
775
		more = do_balance_runtime(rt_rq);
776
		raw_spin_lock(&rt_rq->rt_runtime_lock);
777 778 779 780
	}

	return more;
}
781
#else /* !CONFIG_SMP */
782 783 784 785
static inline int balance_runtime(struct rt_rq *rt_rq)
{
	return 0;
}
786
#endif /* CONFIG_SMP */
787

788 789
static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun)
{
790
	int i, idle = 1, throttled = 0;
791
	const struct cpumask *span;
792 793

	span = sched_rt_period_mask();
794 795 796 797 798 799 800 801 802 803 804 805 806
#ifdef CONFIG_RT_GROUP_SCHED
	/*
	 * FIXME: isolated CPUs should really leave the root task group,
	 * whether they are isolcpus or were isolated via cpusets, lest
	 * the timer run on a CPU which does not service all runqueues,
	 * potentially leaving other CPUs indefinitely throttled.  If
	 * isolation is really required, the user will turn the throttle
	 * off to kill the perturbations it causes anyway.  Meanwhile,
	 * this maintains functionality for boot and/or troubleshooting.
	 */
	if (rt_b == &root_task_group.rt_bandwidth)
		span = cpu_online_mask;
#endif
807
	for_each_cpu(i, span) {
808 809 810 811
		int enqueue = 0;
		struct rt_rq *rt_rq = sched_rt_period_rt_rq(rt_b, i);
		struct rq *rq = rq_of_rt_rq(rt_rq);

812
		raw_spin_lock(&rq->lock);
813 814 815
		if (rt_rq->rt_time) {
			u64 runtime;

816
			raw_spin_lock(&rt_rq->rt_runtime_lock);
817 818 819 820 821 822 823
			if (rt_rq->rt_throttled)
				balance_runtime(rt_rq);
			runtime = rt_rq->rt_runtime;
			rt_rq->rt_time -= min(rt_rq->rt_time, overrun*runtime);
			if (rt_rq->rt_throttled && rt_rq->rt_time < runtime) {
				rt_rq->rt_throttled = 0;
				enqueue = 1;
824 825 826 827 828 829 830

				/*
				 * Force a clock update if the CPU was idle,
				 * lest wakeup -> unthrottle time accumulate.
				 */
				if (rt_rq->rt_nr_running && rq->curr == rq->idle)
					rq->skip_clock_update = -1;
831 832 833
			}
			if (rt_rq->rt_time || rt_rq->rt_nr_running)
				idle = 0;
834
			raw_spin_unlock(&rt_rq->rt_runtime_lock);
835
		} else if (rt_rq->rt_nr_running) {
836
			idle = 0;
837 838 839
			if (!rt_rq_throttled(rt_rq))
				enqueue = 1;
		}
840 841
		if (rt_rq->rt_throttled)
			throttled = 1;
842 843 844

		if (enqueue)
			sched_rt_rq_enqueue(rt_rq);
845
		raw_spin_unlock(&rq->lock);
846 847
	}

848 849 850
	if (!throttled && (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF))
		return 1;

851 852
	return idle;
}
853

Peter Zijlstra's avatar
Peter Zijlstra committed
854 855
static inline int rt_se_prio(struct sched_rt_entity *rt_se)
{
856
#ifdef CONFIG_RT_GROUP_SCHED
Peter Zijlstra's avatar
Peter Zijlstra committed
857 858 859
	struct rt_rq *rt_rq = group_rt_rq(rt_se);

	if (rt_rq)
860
		return rt_rq->highest_prio.curr;
Peter Zijlstra's avatar
Peter Zijlstra committed
861 862 863 864 865
#endif

	return rt_task_of(rt_se)->prio;
}

Peter Zijlstra's avatar
Peter Zijlstra committed
866
static int sched_rt_runtime_exceeded(struct rt_rq *rt_rq)
Peter Zijlstra's avatar
Peter Zijlstra committed
867
{
Peter Zijlstra's avatar
Peter Zijlstra committed
868
	u64 runtime = sched_rt_runtime(rt_rq);
Peter Zijlstra's avatar
Peter Zijlstra committed
869 870

	if (rt_rq->rt_throttled)
Peter Zijlstra's avatar
Peter Zijlstra committed
871
		return rt_rq_throttled(rt_rq);
Peter Zijlstra's avatar
Peter Zijlstra committed
872

873
	if (runtime >= sched_rt_period(rt_rq))
874 875
		return 0;

876 877 878 879
	balance_runtime(rt_rq);
	runtime = sched_rt_runtime(rt_rq);
	if (runtime == RUNTIME_INF)
		return 0;
880

Peter Zijlstra's avatar
Peter Zijlstra committed
881
	if (rt_rq->rt_time > runtime) {
882 883 884 885 886 887 888
		struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq);

		/*
		 * Don't actually throttle groups that have no runtime assigned
		 * but accrue some time due to boosting.
		 */
		if (likely(rt_b->rt_runtime)) {
889 890
			static bool once = false;

891
			rt_rq->rt_throttled = 1;
892 893 894 895 896

			if (!once) {
				once = true;
				printk_sched("sched: RT throttling activated\n");
			}
897 898 899 900 901 902 903 904 905
		} else {
			/*
			 * In case we did anyway, make it go away,
			 * replenishment is a joke, since it will replenish us
			 * with exactly 0 ns.
			 */
			rt_rq->rt_time = 0;
		}

Peter Zijlstra's avatar
Peter Zijlstra committed
906
		if (rt_rq_throttled(rt_rq)) {
Peter Zijlstra's avatar
Peter Zijlstra committed
907
			sched_rt_rq_dequeue(rt_rq);
Peter Zijlstra's avatar
Peter Zijlstra committed
908 909
			return 1;
		}
Peter Zijlstra's avatar
Peter Zijlstra committed
910 911 912 913 914
	}

	return 0;
}

915 916 917 918
/*
 * Update the current task's runtime statistics. Skip current tasks that
 * are not in our scheduling class.
 */
Alexey Dobriyan's avatar
Alexey Dobriyan committed
919
static void update_curr_rt(struct rq *rq)
920 921
{
	struct task_struct *curr = rq->curr;
Peter Zijlstra's avatar
Peter Zijlstra committed
922 923
	struct sched_rt_entity *rt_se = &curr->rt;
	struct rt_rq *rt_rq = rt_rq_of_se(rt_se);
924 925
	u64 delta_exec;

Peter Zijlstra's avatar
Peter Zijlstra committed
926
	if (curr->sched_class != &rt_sched_class)
927 928
		return;

929
	delta_exec = rq->clock_task - curr->se.exec_start;
930 931
	if (unlikely((s64)delta_exec <= 0))
		return;
Ingo Molnar's avatar
Ingo Molnar committed
932

933 934
	schedstat_set(curr->se.statistics.exec_max,
		      max(curr->se.statistics.exec_max, delta_exec));
935 936

	curr->se.sum_exec_runtime += delta_exec;
937 938
	account_group_exec_runtime(curr, delta_exec);

939
	curr->se.exec_start = rq->clock_task;
940
	cpuacct_charge(curr, delta_exec);
Peter Zijlstra's avatar
Peter Zijlstra committed
941

942 943
	sched_rt_avg_update(rq, delta_exec);

944 945 946
	if (!rt_bandwidth_enabled())
		return;

Dhaval Giani's avatar
Dhaval Giani committed
947 948 949
	for_each_sched_rt_entity(rt_se) {
		rt_rq = rt_rq_of_se(rt_se);

950
		if (sched_rt_runtime(rt_rq) != RUNTIME_INF) {
951
			raw_spin_lock(&rt_rq->rt_runtime_lock);
952 953 954
			rt_rq->rt_time += delta_exec;
			if (sched_rt_runtime_exceeded(rt_rq))
				resched_task(curr);
955
			raw_spin_unlock(&rt_rq->rt_runtime_lock);
956
		}
Dhaval Giani's avatar
Dhaval Giani committed
957
	}
958 959
}

960
#if defined CONFIG_SMP
961

962 963
static void
inc_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio)
964
{
965
	struct rq *rq = rq_of_rt_rq(rt_rq);
966

967 968
	if (rq->online && prio < prev_prio)
		cpupri_set(&rq->rd->cpupri, rq->cpu, prio);
969
}
970

971 972 973 974
static void
dec_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio)
{
	struct rq *rq = rq_of_rt_rq(rt_rq);
975

976 977
	if (rq->online && rt_rq->highest_prio.curr != prev_prio)
		cpupri_set(&rq->rd->cpupri, rq->cpu, rt_rq->highest_prio.curr);
978 979
}

980 981
#else /* CONFIG_SMP */

Peter Zijlstra's avatar
Peter Zijlstra committed
982
static inline
983 984 985 986 987
void inc_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) {}
static inline
void dec_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) {}

#endif /* CONFIG_SMP */
988

989
#if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
static void
inc_rt_prio(struct rt_rq *rt_rq, int prio)
{
	int prev_prio = rt_rq->highest_prio.curr;

	if (prio < prev_prio)
		rt_rq->highest_prio.curr = prio;

	inc_rt_prio_smp(rt_rq, prio, prev_prio);
}

static void
dec_rt_prio(struct rt_rq *rt_rq, int prio)
{
	int prev_prio = rt_rq->highest_prio.curr;

Peter Zijlstra's avatar
Peter Zijlstra committed
1006
	if (rt_rq->rt_nr_running) {
1007

1008
		WARN_ON(prio < prev_prio);
1009

1010
		/*
1011 1012
		 * This may have been our highest task, and therefore
		 * we may have some recomputation to do
1013
		 */
1014
		if (prio == prev_prio) {
1015 1016 1017
			struct rt_prio_array *array = &rt_rq->active;

			rt_rq->highest_prio.curr =
1018
				sched_find_first_bit(array->bitmap);
1019 1020
		}

1021
	} else
1022
		rt_rq->highest_prio.curr = MAX_RT_PRIO;
1023

1024 1025
	dec_rt_prio_smp(rt_rq, prio, prev_prio);
}