| Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1 | /* | 
 | 2 |  * Real-Time Scheduling Class (mapped to the SCHED_FIFO and SCHED_RR | 
 | 3 |  * policies) | 
 | 4 |  */ | 
 | 5 |  | 
| Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 6 | #include "sched.h" | 
 | 7 |  | 
 | 8 | #include <linux/slab.h> | 
 | 9 |  | 
 | 10 | static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun); | 
 | 11 |  | 
 | 12 | struct rt_bandwidth def_rt_bandwidth; | 
 | 13 |  | 
 | 14 | static enum hrtimer_restart sched_rt_period_timer(struct hrtimer *timer) | 
 | 15 | { | 
 | 16 | 	struct rt_bandwidth *rt_b = | 
 | 17 | 		container_of(timer, struct rt_bandwidth, rt_period_timer); | 
 | 18 | 	ktime_t now; | 
 | 19 | 	int overrun; | 
 | 20 | 	int idle = 0; | 
 | 21 |  | 
 | 22 | 	for (;;) { | 
 | 23 | 		now = hrtimer_cb_get_time(timer); | 
 | 24 | 		overrun = hrtimer_forward(timer, now, rt_b->rt_period); | 
 | 25 |  | 
 | 26 | 		if (!overrun) | 
 | 27 | 			break; | 
 | 28 |  | 
 | 29 | 		idle = do_sched_rt_period_timer(rt_b, overrun); | 
 | 30 | 	} | 
 | 31 |  | 
 | 32 | 	return idle ? HRTIMER_NORESTART : HRTIMER_RESTART; | 
 | 33 | } | 
 | 34 |  | 
 | 35 | void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime) | 
 | 36 | { | 
 | 37 | 	rt_b->rt_period = ns_to_ktime(period); | 
 | 38 | 	rt_b->rt_runtime = runtime; | 
 | 39 |  | 
 | 40 | 	raw_spin_lock_init(&rt_b->rt_runtime_lock); | 
 | 41 |  | 
 | 42 | 	hrtimer_init(&rt_b->rt_period_timer, | 
 | 43 | 			CLOCK_MONOTONIC, HRTIMER_MODE_REL); | 
 | 44 | 	rt_b->rt_period_timer.function = sched_rt_period_timer; | 
 | 45 | } | 
 | 46 |  | 
 | 47 | static void start_rt_bandwidth(struct rt_bandwidth *rt_b) | 
 | 48 | { | 
 | 49 | 	if (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF) | 
 | 50 | 		return; | 
 | 51 |  | 
 | 52 | 	if (hrtimer_active(&rt_b->rt_period_timer)) | 
 | 53 | 		return; | 
 | 54 |  | 
 | 55 | 	raw_spin_lock(&rt_b->rt_runtime_lock); | 
 | 56 | 	start_bandwidth_timer(&rt_b->rt_period_timer, rt_b->rt_period); | 
 | 57 | 	raw_spin_unlock(&rt_b->rt_runtime_lock); | 
 | 58 | } | 
 | 59 |  | 
 | 60 | void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq) | 
 | 61 | { | 
 | 62 | 	struct rt_prio_array *array; | 
 | 63 | 	int i; | 
 | 64 |  | 
 | 65 | 	array = &rt_rq->active; | 
 | 66 | 	for (i = 0; i < MAX_RT_PRIO; i++) { | 
 | 67 | 		INIT_LIST_HEAD(array->queue + i); | 
 | 68 | 		__clear_bit(i, array->bitmap); | 
 | 69 | 	} | 
 | 70 | 	/* delimiter for bitsearch: */ | 
 | 71 | 	__set_bit(MAX_RT_PRIO, array->bitmap); | 
 | 72 |  | 
 | 73 | #if defined CONFIG_SMP | 
 | 74 | 	rt_rq->highest_prio.curr = MAX_RT_PRIO; | 
 | 75 | 	rt_rq->highest_prio.next = MAX_RT_PRIO; | 
 | 76 | 	rt_rq->rt_nr_migratory = 0; | 
 | 77 | 	rt_rq->overloaded = 0; | 
 | 78 | 	plist_head_init(&rt_rq->pushable_tasks); | 
 | 79 | #endif | 
 | 80 |  | 
 | 81 | 	rt_rq->rt_time = 0; | 
 | 82 | 	rt_rq->rt_throttled = 0; | 
 | 83 | 	rt_rq->rt_runtime = 0; | 
 | 84 | 	raw_spin_lock_init(&rt_rq->rt_runtime_lock); | 
 | 85 | } | 
 | 86 |  | 
| Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 87 | #ifdef CONFIG_RT_GROUP_SCHED | 
| Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 88 | static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b) | 
 | 89 | { | 
 | 90 | 	hrtimer_cancel(&rt_b->rt_period_timer); | 
 | 91 | } | 
| Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 92 |  | 
| Peter Zijlstra | a1ba4d8 | 2009-04-01 18:40:15 +0200 | [diff] [blame] | 93 | #define rt_entity_is_task(rt_se) (!(rt_se)->my_q) | 
 | 94 |  | 
| Peter Zijlstra | 8f48894 | 2009-07-24 12:25:30 +0200 | [diff] [blame] | 95 | static inline struct task_struct *rt_task_of(struct sched_rt_entity *rt_se) | 
 | 96 | { | 
 | 97 | #ifdef CONFIG_SCHED_DEBUG | 
 | 98 | 	WARN_ON_ONCE(!rt_entity_is_task(rt_se)); | 
 | 99 | #endif | 
 | 100 | 	return container_of(rt_se, struct task_struct, rt); | 
 | 101 | } | 
 | 102 |  | 
| Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 103 | static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq) | 
 | 104 | { | 
 | 105 | 	return rt_rq->rq; | 
 | 106 | } | 
 | 107 |  | 
 | 108 | static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se) | 
 | 109 | { | 
 | 110 | 	return rt_se->rt_rq; | 
 | 111 | } | 
 | 112 |  | 
| Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 113 | void free_rt_sched_group(struct task_group *tg) | 
 | 114 | { | 
 | 115 | 	int i; | 
 | 116 |  | 
 | 117 | 	if (tg->rt_se) | 
 | 118 | 		destroy_rt_bandwidth(&tg->rt_bandwidth); | 
 | 119 |  | 
 | 120 | 	for_each_possible_cpu(i) { | 
 | 121 | 		if (tg->rt_rq) | 
 | 122 | 			kfree(tg->rt_rq[i]); | 
 | 123 | 		if (tg->rt_se) | 
 | 124 | 			kfree(tg->rt_se[i]); | 
 | 125 | 	} | 
 | 126 |  | 
 | 127 | 	kfree(tg->rt_rq); | 
 | 128 | 	kfree(tg->rt_se); | 
 | 129 | } | 
 | 130 |  | 
 | 131 | void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq, | 
 | 132 | 		struct sched_rt_entity *rt_se, int cpu, | 
 | 133 | 		struct sched_rt_entity *parent) | 
 | 134 | { | 
 | 135 | 	struct rq *rq = cpu_rq(cpu); | 
 | 136 |  | 
 | 137 | 	rt_rq->highest_prio.curr = MAX_RT_PRIO; | 
 | 138 | 	rt_rq->rt_nr_boosted = 0; | 
 | 139 | 	rt_rq->rq = rq; | 
 | 140 | 	rt_rq->tg = tg; | 
 | 141 |  | 
 | 142 | 	tg->rt_rq[cpu] = rt_rq; | 
 | 143 | 	tg->rt_se[cpu] = rt_se; | 
 | 144 |  | 
 | 145 | 	if (!rt_se) | 
 | 146 | 		return; | 
 | 147 |  | 
 | 148 | 	if (!parent) | 
 | 149 | 		rt_se->rt_rq = &rq->rt; | 
 | 150 | 	else | 
 | 151 | 		rt_se->rt_rq = parent->my_q; | 
 | 152 |  | 
 | 153 | 	rt_se->my_q = rt_rq; | 
 | 154 | 	rt_se->parent = parent; | 
 | 155 | 	INIT_LIST_HEAD(&rt_se->run_list); | 
 | 156 | } | 
 | 157 |  | 
 | 158 | int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent) | 
 | 159 | { | 
 | 160 | 	struct rt_rq *rt_rq; | 
 | 161 | 	struct sched_rt_entity *rt_se; | 
 | 162 | 	int i; | 
 | 163 |  | 
 | 164 | 	tg->rt_rq = kzalloc(sizeof(rt_rq) * nr_cpu_ids, GFP_KERNEL); | 
 | 165 | 	if (!tg->rt_rq) | 
 | 166 | 		goto err; | 
 | 167 | 	tg->rt_se = kzalloc(sizeof(rt_se) * nr_cpu_ids, GFP_KERNEL); | 
 | 168 | 	if (!tg->rt_se) | 
 | 169 | 		goto err; | 
 | 170 |  | 
 | 171 | 	init_rt_bandwidth(&tg->rt_bandwidth, | 
 | 172 | 			ktime_to_ns(def_rt_bandwidth.rt_period), 0); | 
 | 173 |  | 
 | 174 | 	for_each_possible_cpu(i) { | 
 | 175 | 		rt_rq = kzalloc_node(sizeof(struct rt_rq), | 
 | 176 | 				     GFP_KERNEL, cpu_to_node(i)); | 
 | 177 | 		if (!rt_rq) | 
 | 178 | 			goto err; | 
 | 179 |  | 
 | 180 | 		rt_se = kzalloc_node(sizeof(struct sched_rt_entity), | 
 | 181 | 				     GFP_KERNEL, cpu_to_node(i)); | 
 | 182 | 		if (!rt_se) | 
 | 183 | 			goto err_free_rq; | 
 | 184 |  | 
 | 185 | 		init_rt_rq(rt_rq, cpu_rq(i)); | 
 | 186 | 		rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime; | 
 | 187 | 		init_tg_rt_entry(tg, rt_rq, rt_se, i, parent->rt_se[i]); | 
 | 188 | 	} | 
 | 189 |  | 
 | 190 | 	return 1; | 
 | 191 |  | 
 | 192 | err_free_rq: | 
 | 193 | 	kfree(rt_rq); | 
 | 194 | err: | 
 | 195 | 	return 0; | 
 | 196 | } | 
 | 197 |  | 
| Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 198 | #else /* CONFIG_RT_GROUP_SCHED */ | 
 | 199 |  | 
| Peter Zijlstra | a1ba4d8 | 2009-04-01 18:40:15 +0200 | [diff] [blame] | 200 | #define rt_entity_is_task(rt_se) (1) | 
 | 201 |  | 
| Peter Zijlstra | 8f48894 | 2009-07-24 12:25:30 +0200 | [diff] [blame] | 202 | static inline struct task_struct *rt_task_of(struct sched_rt_entity *rt_se) | 
 | 203 | { | 
 | 204 | 	return container_of(rt_se, struct task_struct, rt); | 
 | 205 | } | 
 | 206 |  | 
| Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 207 | static inline struct rq *rq_of_rt_rq(struct rt_rq *rt_rq) | 
 | 208 | { | 
 | 209 | 	return container_of(rt_rq, struct rq, rt); | 
 | 210 | } | 
 | 211 |  | 
 | 212 | static inline struct rt_rq *rt_rq_of_se(struct sched_rt_entity *rt_se) | 
 | 213 | { | 
 | 214 | 	struct task_struct *p = rt_task_of(rt_se); | 
 | 215 | 	struct rq *rq = task_rq(p); | 
 | 216 |  | 
 | 217 | 	return &rq->rt; | 
 | 218 | } | 
 | 219 |  | 
| Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 220 | void free_rt_sched_group(struct task_group *tg) { } | 
 | 221 |  | 
 | 222 | int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent) | 
 | 223 | { | 
 | 224 | 	return 1; | 
 | 225 | } | 
| Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 226 | #endif /* CONFIG_RT_GROUP_SCHED */ | 
 | 227 |  | 
| Steven Rostedt | 4fd2917 | 2008-01-25 21:08:06 +0100 | [diff] [blame] | 228 | #ifdef CONFIG_SMP | 
| Ingo Molnar | 84de427 | 2008-01-25 21:08:15 +0100 | [diff] [blame] | 229 |  | 
| Gregory Haskins | 637f508 | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 230 | static inline int rt_overloaded(struct rq *rq) | 
| Steven Rostedt | 4fd2917 | 2008-01-25 21:08:06 +0100 | [diff] [blame] | 231 | { | 
| Gregory Haskins | 637f508 | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 232 | 	return atomic_read(&rq->rd->rto_count); | 
| Steven Rostedt | 4fd2917 | 2008-01-25 21:08:06 +0100 | [diff] [blame] | 233 | } | 
| Ingo Molnar | 84de427 | 2008-01-25 21:08:15 +0100 | [diff] [blame] | 234 |  | 
| Steven Rostedt | 4fd2917 | 2008-01-25 21:08:06 +0100 | [diff] [blame] | 235 | static inline void rt_set_overload(struct rq *rq) | 
 | 236 | { | 
| Gregory Haskins | 1f11eb6 | 2008-06-04 15:04:05 -0400 | [diff] [blame] | 237 | 	if (!rq->online) | 
 | 238 | 		return; | 
 | 239 |  | 
| Rusty Russell | c6c4927 | 2008-11-25 02:35:05 +1030 | [diff] [blame] | 240 | 	cpumask_set_cpu(rq->cpu, rq->rd->rto_mask); | 
| Steven Rostedt | 4fd2917 | 2008-01-25 21:08:06 +0100 | [diff] [blame] | 241 | 	/* | 
 | 242 | 	 * Make sure the mask is visible before we set | 
 | 243 | 	 * the overload count. That is checked to determine | 
 | 244 | 	 * if we should look at the mask. It would be a shame | 
 | 245 | 	 * if we looked at the mask, but the mask was not | 
 | 246 | 	 * updated yet. | 
 | 247 | 	 */ | 
 | 248 | 	wmb(); | 
| Gregory Haskins | 637f508 | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 249 | 	atomic_inc(&rq->rd->rto_count); | 
| Steven Rostedt | 4fd2917 | 2008-01-25 21:08:06 +0100 | [diff] [blame] | 250 | } | 
| Ingo Molnar | 84de427 | 2008-01-25 21:08:15 +0100 | [diff] [blame] | 251 |  | 
| Steven Rostedt | 4fd2917 | 2008-01-25 21:08:06 +0100 | [diff] [blame] | 252 | static inline void rt_clear_overload(struct rq *rq) | 
 | 253 | { | 
| Gregory Haskins | 1f11eb6 | 2008-06-04 15:04:05 -0400 | [diff] [blame] | 254 | 	if (!rq->online) | 
 | 255 | 		return; | 
 | 256 |  | 
| Steven Rostedt | 4fd2917 | 2008-01-25 21:08:06 +0100 | [diff] [blame] | 257 | 	/* the order here really doesn't matter */ | 
| Gregory Haskins | 637f508 | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 258 | 	atomic_dec(&rq->rd->rto_count); | 
| Rusty Russell | c6c4927 | 2008-11-25 02:35:05 +1030 | [diff] [blame] | 259 | 	cpumask_clear_cpu(rq->cpu, rq->rd->rto_mask); | 
| Steven Rostedt | 4fd2917 | 2008-01-25 21:08:06 +0100 | [diff] [blame] | 260 | } | 
| Gregory Haskins | 73fe6aa | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 261 |  | 
| Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 262 | static void update_rt_migration(struct rt_rq *rt_rq) | 
| Gregory Haskins | 73fe6aa | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 263 | { | 
| Peter Zijlstra | a1ba4d8 | 2009-04-01 18:40:15 +0200 | [diff] [blame] | 264 | 	if (rt_rq->rt_nr_migratory && rt_rq->rt_nr_total > 1) { | 
| Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 265 | 		if (!rt_rq->overloaded) { | 
 | 266 | 			rt_set_overload(rq_of_rt_rq(rt_rq)); | 
 | 267 | 			rt_rq->overloaded = 1; | 
| Gregory Haskins | cdc8eb9 | 2008-01-25 21:08:23 +0100 | [diff] [blame] | 268 | 		} | 
| Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 269 | 	} else if (rt_rq->overloaded) { | 
 | 270 | 		rt_clear_overload(rq_of_rt_rq(rt_rq)); | 
 | 271 | 		rt_rq->overloaded = 0; | 
| Gregory Haskins | 637f508 | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 272 | 	} | 
| Gregory Haskins | 73fe6aa | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 273 | } | 
| Steven Rostedt | 4fd2917 | 2008-01-25 21:08:06 +0100 | [diff] [blame] | 274 |  | 
| Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 275 | static void inc_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) | 
| Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 276 | { | 
| Peter Zijlstra | a1ba4d8 | 2009-04-01 18:40:15 +0200 | [diff] [blame] | 277 | 	if (!rt_entity_is_task(rt_se)) | 
 | 278 | 		return; | 
 | 279 |  | 
 | 280 | 	rt_rq = &rq_of_rt_rq(rt_rq)->rt; | 
 | 281 |  | 
 | 282 | 	rt_rq->rt_nr_total++; | 
| Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 283 | 	if (rt_se->nr_cpus_allowed > 1) | 
 | 284 | 		rt_rq->rt_nr_migratory++; | 
 | 285 |  | 
 | 286 | 	update_rt_migration(rt_rq); | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 287 | } | 
 | 288 |  | 
| Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 289 | static void dec_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) | 
 | 290 | { | 
| Peter Zijlstra | a1ba4d8 | 2009-04-01 18:40:15 +0200 | [diff] [blame] | 291 | 	if (!rt_entity_is_task(rt_se)) | 
 | 292 | 		return; | 
 | 293 |  | 
 | 294 | 	rt_rq = &rq_of_rt_rq(rt_rq)->rt; | 
 | 295 |  | 
 | 296 | 	rt_rq->rt_nr_total--; | 
| Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 297 | 	if (rt_se->nr_cpus_allowed > 1) | 
 | 298 | 		rt_rq->rt_nr_migratory--; | 
 | 299 |  | 
 | 300 | 	update_rt_migration(rt_rq); | 
 | 301 | } | 
 | 302 |  | 
| Steven Rostedt | 5181f4a4 | 2011-06-16 21:55:23 -0400 | [diff] [blame] | 303 | static inline int has_pushable_tasks(struct rq *rq) | 
 | 304 | { | 
 | 305 | 	return !plist_head_empty(&rq->rt.pushable_tasks); | 
 | 306 | } | 
 | 307 |  | 
| Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 308 | static void enqueue_pushable_task(struct rq *rq, struct task_struct *p) | 
 | 309 | { | 
 | 310 | 	plist_del(&p->pushable_tasks, &rq->rt.pushable_tasks); | 
 | 311 | 	plist_node_init(&p->pushable_tasks, p->prio); | 
 | 312 | 	plist_add(&p->pushable_tasks, &rq->rt.pushable_tasks); | 
| Steven Rostedt | 5181f4a4 | 2011-06-16 21:55:23 -0400 | [diff] [blame] | 313 |  | 
 | 314 | 	/* Update the highest prio pushable task */ | 
 | 315 | 	if (p->prio < rq->rt.highest_prio.next) | 
 | 316 | 		rq->rt.highest_prio.next = p->prio; | 
| Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 317 | } | 
 | 318 |  | 
 | 319 | static void dequeue_pushable_task(struct rq *rq, struct task_struct *p) | 
 | 320 | { | 
 | 321 | 	plist_del(&p->pushable_tasks, &rq->rt.pushable_tasks); | 
| Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 322 |  | 
| Steven Rostedt | 5181f4a4 | 2011-06-16 21:55:23 -0400 | [diff] [blame] | 323 | 	/* Update the new highest prio pushable task */ | 
 | 324 | 	if (has_pushable_tasks(rq)) { | 
 | 325 | 		p = plist_first_entry(&rq->rt.pushable_tasks, | 
 | 326 | 				      struct task_struct, pushable_tasks); | 
 | 327 | 		rq->rt.highest_prio.next = p->prio; | 
 | 328 | 	} else | 
 | 329 | 		rq->rt.highest_prio.next = MAX_RT_PRIO; | 
| Ingo Molnar | bcf08df | 2008-04-19 12:11:10 +0200 | [diff] [blame] | 330 | } | 
 | 331 |  | 
| Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 332 | #else | 
 | 333 |  | 
| Peter Zijlstra | ceacc2c | 2009-01-16 14:46:40 +0100 | [diff] [blame] | 334 | static inline void enqueue_pushable_task(struct rq *rq, struct task_struct *p) | 
 | 335 | { | 
 | 336 | } | 
 | 337 |  | 
 | 338 | static inline void dequeue_pushable_task(struct rq *rq, struct task_struct *p) | 
 | 339 | { | 
 | 340 | } | 
 | 341 |  | 
| Gregory Haskins | b07430a | 2009-01-14 08:55:39 -0500 | [diff] [blame] | 342 | static inline | 
| Peter Zijlstra | ceacc2c | 2009-01-16 14:46:40 +0100 | [diff] [blame] | 343 | void inc_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) | 
 | 344 | { | 
 | 345 | } | 
 | 346 |  | 
| Gregory Haskins | b07430a | 2009-01-14 08:55:39 -0500 | [diff] [blame] | 347 | static inline | 
| Peter Zijlstra | ceacc2c | 2009-01-16 14:46:40 +0100 | [diff] [blame] | 348 | void dec_rt_migration(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) | 
 | 349 | { | 
 | 350 | } | 
| Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 351 |  | 
| Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 352 | #endif /* CONFIG_SMP */ | 
 | 353 |  | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 354 | static inline int on_rt_rq(struct sched_rt_entity *rt_se) | 
 | 355 | { | 
 | 356 | 	return !list_empty(&rt_se->run_list); | 
 | 357 | } | 
 | 358 |  | 
| Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 359 | #ifdef CONFIG_RT_GROUP_SCHED | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 360 |  | 
| Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 361 | static inline u64 sched_rt_runtime(struct rt_rq *rt_rq) | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 362 | { | 
 | 363 | 	if (!rt_rq->tg) | 
| Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 364 | 		return RUNTIME_INF; | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 365 |  | 
| Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 366 | 	return rt_rq->rt_runtime; | 
 | 367 | } | 
 | 368 |  | 
 | 369 | static inline u64 sched_rt_period(struct rt_rq *rt_rq) | 
 | 370 | { | 
 | 371 | 	return ktime_to_ns(rt_rq->tg->rt_bandwidth.rt_period); | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 372 | } | 
 | 373 |  | 
| Cheng Xu | ec514c4 | 2011-05-14 14:20:02 +0800 | [diff] [blame] | 374 | typedef struct task_group *rt_rq_iter_t; | 
 | 375 |  | 
| Yong Zhang | 1c09ab0 | 2011-06-28 10:51:31 +0800 | [diff] [blame] | 376 | static inline struct task_group *next_task_group(struct task_group *tg) | 
 | 377 | { | 
 | 378 | 	do { | 
 | 379 | 		tg = list_entry_rcu(tg->list.next, | 
 | 380 | 			typeof(struct task_group), list); | 
 | 381 | 	} while (&tg->list != &task_groups && task_group_is_autogroup(tg)); | 
 | 382 |  | 
 | 383 | 	if (&tg->list == &task_groups) | 
 | 384 | 		tg = NULL; | 
 | 385 |  | 
 | 386 | 	return tg; | 
 | 387 | } | 
 | 388 |  | 
 | 389 | #define for_each_rt_rq(rt_rq, iter, rq)					\ | 
 | 390 | 	for (iter = container_of(&task_groups, typeof(*iter), list);	\ | 
 | 391 | 		(iter = next_task_group(iter)) &&			\ | 
 | 392 | 		(rt_rq = iter->rt_rq[cpu_of(rq)]);) | 
| Cheng Xu | ec514c4 | 2011-05-14 14:20:02 +0800 | [diff] [blame] | 393 |  | 
| Peter Zijlstra | 3d4b47b | 2010-11-15 15:47:01 -0800 | [diff] [blame] | 394 | static inline void list_add_leaf_rt_rq(struct rt_rq *rt_rq) | 
 | 395 | { | 
 | 396 | 	list_add_rcu(&rt_rq->leaf_rt_rq_list, | 
 | 397 | 			&rq_of_rt_rq(rt_rq)->leaf_rt_rq_list); | 
 | 398 | } | 
 | 399 |  | 
 | 400 | static inline void list_del_leaf_rt_rq(struct rt_rq *rt_rq) | 
 | 401 | { | 
 | 402 | 	list_del_rcu(&rt_rq->leaf_rt_rq_list); | 
 | 403 | } | 
 | 404 |  | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 405 | #define for_each_leaf_rt_rq(rt_rq, rq) \ | 
| Bharata B Rao | 80f40ee | 2008-12-15 11:56:48 +0530 | [diff] [blame] | 406 | 	list_for_each_entry_rcu(rt_rq, &rq->leaf_rt_rq_list, leaf_rt_rq_list) | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 407 |  | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 408 | #define for_each_sched_rt_entity(rt_se) \ | 
 | 409 | 	for (; rt_se; rt_se = rt_se->parent) | 
 | 410 |  | 
 | 411 | static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se) | 
 | 412 | { | 
 | 413 | 	return rt_se->my_q; | 
 | 414 | } | 
 | 415 |  | 
| Thomas Gleixner | 37dad3f | 2010-01-20 20:59:01 +0000 | [diff] [blame] | 416 | static void enqueue_rt_entity(struct sched_rt_entity *rt_se, bool head); | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 417 | static void dequeue_rt_entity(struct sched_rt_entity *rt_se); | 
 | 418 |  | 
| Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 419 | static void sched_rt_rq_enqueue(struct rt_rq *rt_rq) | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 420 | { | 
| Dario Faggioli | f6121f4 | 2008-10-03 17:40:46 +0200 | [diff] [blame] | 421 | 	struct task_struct *curr = rq_of_rt_rq(rt_rq)->curr; | 
| Yong Zhang | 74b7eb5 | 2010-01-29 14:57:52 +0800 | [diff] [blame] | 422 | 	struct sched_rt_entity *rt_se; | 
 | 423 |  | 
| Balbir Singh | 0c3b916 | 2011-03-03 17:04:35 +0530 | [diff] [blame] | 424 | 	int cpu = cpu_of(rq_of_rt_rq(rt_rq)); | 
 | 425 |  | 
 | 426 | 	rt_se = rt_rq->tg->rt_se[cpu]; | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 427 |  | 
| Dario Faggioli | f6121f4 | 2008-10-03 17:40:46 +0200 | [diff] [blame] | 428 | 	if (rt_rq->rt_nr_running) { | 
 | 429 | 		if (rt_se && !on_rt_rq(rt_se)) | 
| Thomas Gleixner | 37dad3f | 2010-01-20 20:59:01 +0000 | [diff] [blame] | 430 | 			enqueue_rt_entity(rt_se, false); | 
| Gregory Haskins | e864c49 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 431 | 		if (rt_rq->highest_prio.curr < curr->prio) | 
| Peter Zijlstra | 1020387 | 2008-01-25 21:08:32 +0100 | [diff] [blame] | 432 | 			resched_task(curr); | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 433 | 	} | 
 | 434 | } | 
 | 435 |  | 
| Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 436 | static void sched_rt_rq_dequeue(struct rt_rq *rt_rq) | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 437 | { | 
| Yong Zhang | 74b7eb5 | 2010-01-29 14:57:52 +0800 | [diff] [blame] | 438 | 	struct sched_rt_entity *rt_se; | 
| Balbir Singh | 0c3b916 | 2011-03-03 17:04:35 +0530 | [diff] [blame] | 439 | 	int cpu = cpu_of(rq_of_rt_rq(rt_rq)); | 
| Yong Zhang | 74b7eb5 | 2010-01-29 14:57:52 +0800 | [diff] [blame] | 440 |  | 
| Balbir Singh | 0c3b916 | 2011-03-03 17:04:35 +0530 | [diff] [blame] | 441 | 	rt_se = rt_rq->tg->rt_se[cpu]; | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 442 |  | 
 | 443 | 	if (rt_se && on_rt_rq(rt_se)) | 
 | 444 | 		dequeue_rt_entity(rt_se); | 
 | 445 | } | 
 | 446 |  | 
| Peter Zijlstra | 23b0fdf | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 447 | static inline int rt_rq_throttled(struct rt_rq *rt_rq) | 
 | 448 | { | 
 | 449 | 	return rt_rq->rt_throttled && !rt_rq->rt_nr_boosted; | 
 | 450 | } | 
 | 451 |  | 
 | 452 | static int rt_se_boosted(struct sched_rt_entity *rt_se) | 
 | 453 | { | 
 | 454 | 	struct rt_rq *rt_rq = group_rt_rq(rt_se); | 
 | 455 | 	struct task_struct *p; | 
 | 456 |  | 
 | 457 | 	if (rt_rq) | 
 | 458 | 		return !!rt_rq->rt_nr_boosted; | 
 | 459 |  | 
 | 460 | 	p = rt_task_of(rt_se); | 
 | 461 | 	return p->prio != p->normal_prio; | 
 | 462 | } | 
 | 463 |  | 
| Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 464 | #ifdef CONFIG_SMP | 
| Rusty Russell | c6c4927 | 2008-11-25 02:35:05 +1030 | [diff] [blame] | 465 | static inline const struct cpumask *sched_rt_period_mask(void) | 
| Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 466 | { | 
 | 467 | 	return cpu_rq(smp_processor_id())->rd->span; | 
 | 468 | } | 
 | 469 | #else | 
| Rusty Russell | c6c4927 | 2008-11-25 02:35:05 +1030 | [diff] [blame] | 470 | static inline const struct cpumask *sched_rt_period_mask(void) | 
| Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 471 | { | 
| Rusty Russell | c6c4927 | 2008-11-25 02:35:05 +1030 | [diff] [blame] | 472 | 	return cpu_online_mask; | 
| Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 473 | } | 
 | 474 | #endif | 
 | 475 |  | 
 | 476 | static inline | 
 | 477 | struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu) | 
 | 478 | { | 
 | 479 | 	return container_of(rt_b, struct task_group, rt_bandwidth)->rt_rq[cpu]; | 
 | 480 | } | 
 | 481 |  | 
| Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 482 | static inline struct rt_bandwidth *sched_rt_bandwidth(struct rt_rq *rt_rq) | 
 | 483 | { | 
 | 484 | 	return &rt_rq->tg->rt_bandwidth; | 
 | 485 | } | 
 | 486 |  | 
| Dhaval Giani | 55e12e5 | 2008-06-24 23:39:43 +0530 | [diff] [blame] | 487 | #else /* !CONFIG_RT_GROUP_SCHED */ | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 488 |  | 
| Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 489 | static inline u64 sched_rt_runtime(struct rt_rq *rt_rq) | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 490 | { | 
| Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 491 | 	return rt_rq->rt_runtime; | 
 | 492 | } | 
 | 493 |  | 
 | 494 | static inline u64 sched_rt_period(struct rt_rq *rt_rq) | 
 | 495 | { | 
 | 496 | 	return ktime_to_ns(def_rt_bandwidth.rt_period); | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 497 | } | 
 | 498 |  | 
| Cheng Xu | ec514c4 | 2011-05-14 14:20:02 +0800 | [diff] [blame] | 499 | typedef struct rt_rq *rt_rq_iter_t; | 
 | 500 |  | 
 | 501 | #define for_each_rt_rq(rt_rq, iter, rq) \ | 
 | 502 | 	for ((void) iter, rt_rq = &rq->rt; rt_rq; rt_rq = NULL) | 
 | 503 |  | 
| Peter Zijlstra | 3d4b47b | 2010-11-15 15:47:01 -0800 | [diff] [blame] | 504 | static inline void list_add_leaf_rt_rq(struct rt_rq *rt_rq) | 
 | 505 | { | 
 | 506 | } | 
 | 507 |  | 
 | 508 | static inline void list_del_leaf_rt_rq(struct rt_rq *rt_rq) | 
 | 509 | { | 
 | 510 | } | 
 | 511 |  | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 512 | #define for_each_leaf_rt_rq(rt_rq, rq) \ | 
 | 513 | 	for (rt_rq = &rq->rt; rt_rq; rt_rq = NULL) | 
 | 514 |  | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 515 | #define for_each_sched_rt_entity(rt_se) \ | 
 | 516 | 	for (; rt_se; rt_se = NULL) | 
 | 517 |  | 
 | 518 | static inline struct rt_rq *group_rt_rq(struct sched_rt_entity *rt_se) | 
 | 519 | { | 
 | 520 | 	return NULL; | 
 | 521 | } | 
 | 522 |  | 
| Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 523 | static inline void sched_rt_rq_enqueue(struct rt_rq *rt_rq) | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 524 | { | 
| John Blackwood | f3ade83 | 2008-08-26 15:09:43 -0400 | [diff] [blame] | 525 | 	if (rt_rq->rt_nr_running) | 
 | 526 | 		resched_task(rq_of_rt_rq(rt_rq)->curr); | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 527 | } | 
 | 528 |  | 
| Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 529 | static inline void sched_rt_rq_dequeue(struct rt_rq *rt_rq) | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 530 | { | 
 | 531 | } | 
 | 532 |  | 
| Peter Zijlstra | 23b0fdf | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 533 | static inline int rt_rq_throttled(struct rt_rq *rt_rq) | 
 | 534 | { | 
 | 535 | 	return rt_rq->rt_throttled; | 
 | 536 | } | 
| Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 537 |  | 
| Rusty Russell | c6c4927 | 2008-11-25 02:35:05 +1030 | [diff] [blame] | 538 | static inline const struct cpumask *sched_rt_period_mask(void) | 
| Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 539 | { | 
| Rusty Russell | c6c4927 | 2008-11-25 02:35:05 +1030 | [diff] [blame] | 540 | 	return cpu_online_mask; | 
| Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 541 | } | 
 | 542 |  | 
 | 543 | static inline | 
 | 544 | struct rt_rq *sched_rt_period_rt_rq(struct rt_bandwidth *rt_b, int cpu) | 
 | 545 | { | 
 | 546 | 	return &cpu_rq(cpu)->rt; | 
 | 547 | } | 
 | 548 |  | 
| Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 549 | static inline struct rt_bandwidth *sched_rt_bandwidth(struct rt_rq *rt_rq) | 
 | 550 | { | 
 | 551 | 	return &def_rt_bandwidth; | 
 | 552 | } | 
 | 553 |  | 
| Dhaval Giani | 55e12e5 | 2008-06-24 23:39:43 +0530 | [diff] [blame] | 554 | #endif /* CONFIG_RT_GROUP_SCHED */ | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 555 |  | 
| Peter Zijlstra | b79f383 | 2008-06-19 14:22:25 +0200 | [diff] [blame] | 556 | #ifdef CONFIG_SMP | 
| Peter Zijlstra | 78333cd | 2008-09-23 15:33:43 +0200 | [diff] [blame] | 557 | /* | 
 | 558 |  * We ran out of runtime, see if we can borrow some from our neighbours. | 
 | 559 |  */ | 
| Peter Zijlstra | b79f383 | 2008-06-19 14:22:25 +0200 | [diff] [blame] | 560 | static int do_balance_runtime(struct rt_rq *rt_rq) | 
| Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 561 | { | 
 | 562 | 	struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq); | 
 | 563 | 	struct root_domain *rd = cpu_rq(smp_processor_id())->rd; | 
 | 564 | 	int i, weight, more = 0; | 
 | 565 | 	u64 rt_period; | 
 | 566 |  | 
| Rusty Russell | c6c4927 | 2008-11-25 02:35:05 +1030 | [diff] [blame] | 567 | 	weight = cpumask_weight(rd->span); | 
| Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 568 |  | 
| Thomas Gleixner | 0986b11 | 2009-11-17 15:32:06 +0100 | [diff] [blame] | 569 | 	raw_spin_lock(&rt_b->rt_runtime_lock); | 
| Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 570 | 	rt_period = ktime_to_ns(rt_b->rt_period); | 
| Rusty Russell | c6c4927 | 2008-11-25 02:35:05 +1030 | [diff] [blame] | 571 | 	for_each_cpu(i, rd->span) { | 
| Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 572 | 		struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i); | 
 | 573 | 		s64 diff; | 
 | 574 |  | 
 | 575 | 		if (iter == rt_rq) | 
 | 576 | 			continue; | 
 | 577 |  | 
| Thomas Gleixner | 0986b11 | 2009-11-17 15:32:06 +0100 | [diff] [blame] | 578 | 		raw_spin_lock(&iter->rt_runtime_lock); | 
| Peter Zijlstra | 78333cd | 2008-09-23 15:33:43 +0200 | [diff] [blame] | 579 | 		/* | 
 | 580 | 		 * Either all rqs have inf runtime and there's nothing to steal | 
 | 581 | 		 * or __disable_runtime() below sets a specific rq to inf to | 
 | 582 | 		 * indicate its been disabled and disalow stealing. | 
 | 583 | 		 */ | 
| Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 584 | 		if (iter->rt_runtime == RUNTIME_INF) | 
 | 585 | 			goto next; | 
 | 586 |  | 
| Peter Zijlstra | 78333cd | 2008-09-23 15:33:43 +0200 | [diff] [blame] | 587 | 		/* | 
 | 588 | 		 * From runqueues with spare time, take 1/n part of their | 
 | 589 | 		 * spare time, but no more than our period. | 
 | 590 | 		 */ | 
| Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 591 | 		diff = iter->rt_runtime - iter->rt_time; | 
 | 592 | 		if (diff > 0) { | 
| Peter Zijlstra | 58838cf | 2008-07-24 12:43:13 +0200 | [diff] [blame] | 593 | 			diff = div_u64((u64)diff, weight); | 
| Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 594 | 			if (rt_rq->rt_runtime + diff > rt_period) | 
 | 595 | 				diff = rt_period - rt_rq->rt_runtime; | 
 | 596 | 			iter->rt_runtime -= diff; | 
 | 597 | 			rt_rq->rt_runtime += diff; | 
 | 598 | 			more = 1; | 
 | 599 | 			if (rt_rq->rt_runtime == rt_period) { | 
| Thomas Gleixner | 0986b11 | 2009-11-17 15:32:06 +0100 | [diff] [blame] | 600 | 				raw_spin_unlock(&iter->rt_runtime_lock); | 
| Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 601 | 				break; | 
 | 602 | 			} | 
 | 603 | 		} | 
| Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 604 | next: | 
| Thomas Gleixner | 0986b11 | 2009-11-17 15:32:06 +0100 | [diff] [blame] | 605 | 		raw_spin_unlock(&iter->rt_runtime_lock); | 
| Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 606 | 	} | 
| Thomas Gleixner | 0986b11 | 2009-11-17 15:32:06 +0100 | [diff] [blame] | 607 | 	raw_spin_unlock(&rt_b->rt_runtime_lock); | 
| Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 608 |  | 
 | 609 | 	return more; | 
 | 610 | } | 
| Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 611 |  | 
| Peter Zijlstra | 78333cd | 2008-09-23 15:33:43 +0200 | [diff] [blame] | 612 | /* | 
 | 613 |  * Ensure this RQ takes back all the runtime it lend to its neighbours. | 
 | 614 |  */ | 
| Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 615 | static void __disable_runtime(struct rq *rq) | 
 | 616 | { | 
 | 617 | 	struct root_domain *rd = rq->rd; | 
| Cheng Xu | ec514c4 | 2011-05-14 14:20:02 +0800 | [diff] [blame] | 618 | 	rt_rq_iter_t iter; | 
| Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 619 | 	struct rt_rq *rt_rq; | 
 | 620 |  | 
 | 621 | 	if (unlikely(!scheduler_running)) | 
 | 622 | 		return; | 
 | 623 |  | 
| Cheng Xu | ec514c4 | 2011-05-14 14:20:02 +0800 | [diff] [blame] | 624 | 	for_each_rt_rq(rt_rq, iter, rq) { | 
| Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 625 | 		struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq); | 
 | 626 | 		s64 want; | 
 | 627 | 		int i; | 
 | 628 |  | 
| Thomas Gleixner | 0986b11 | 2009-11-17 15:32:06 +0100 | [diff] [blame] | 629 | 		raw_spin_lock(&rt_b->rt_runtime_lock); | 
 | 630 | 		raw_spin_lock(&rt_rq->rt_runtime_lock); | 
| Peter Zijlstra | 78333cd | 2008-09-23 15:33:43 +0200 | [diff] [blame] | 631 | 		/* | 
 | 632 | 		 * Either we're all inf and nobody needs to borrow, or we're | 
 | 633 | 		 * already disabled and thus have nothing to do, or we have | 
 | 634 | 		 * exactly the right amount of runtime to take out. | 
 | 635 | 		 */ | 
| Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 636 | 		if (rt_rq->rt_runtime == RUNTIME_INF || | 
 | 637 | 				rt_rq->rt_runtime == rt_b->rt_runtime) | 
 | 638 | 			goto balanced; | 
| Thomas Gleixner | 0986b11 | 2009-11-17 15:32:06 +0100 | [diff] [blame] | 639 | 		raw_spin_unlock(&rt_rq->rt_runtime_lock); | 
| Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 640 |  | 
| Peter Zijlstra | 78333cd | 2008-09-23 15:33:43 +0200 | [diff] [blame] | 641 | 		/* | 
 | 642 | 		 * Calculate the difference between what we started out with | 
 | 643 | 		 * and what we current have, that's the amount of runtime | 
 | 644 | 		 * we lend and now have to reclaim. | 
 | 645 | 		 */ | 
| Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 646 | 		want = rt_b->rt_runtime - rt_rq->rt_runtime; | 
 | 647 |  | 
| Peter Zijlstra | 78333cd | 2008-09-23 15:33:43 +0200 | [diff] [blame] | 648 | 		/* | 
 | 649 | 		 * Greedy reclaim, take back as much as we can. | 
 | 650 | 		 */ | 
| Rusty Russell | c6c4927 | 2008-11-25 02:35:05 +1030 | [diff] [blame] | 651 | 		for_each_cpu(i, rd->span) { | 
| Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 652 | 			struct rt_rq *iter = sched_rt_period_rt_rq(rt_b, i); | 
 | 653 | 			s64 diff; | 
 | 654 |  | 
| Peter Zijlstra | 78333cd | 2008-09-23 15:33:43 +0200 | [diff] [blame] | 655 | 			/* | 
 | 656 | 			 * Can't reclaim from ourselves or disabled runqueues. | 
 | 657 | 			 */ | 
| Peter Zijlstra | f1679d0 | 2008-08-14 15:49:00 +0200 | [diff] [blame] | 658 | 			if (iter == rt_rq || iter->rt_runtime == RUNTIME_INF) | 
| Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 659 | 				continue; | 
 | 660 |  | 
| Thomas Gleixner | 0986b11 | 2009-11-17 15:32:06 +0100 | [diff] [blame] | 661 | 			raw_spin_lock(&iter->rt_runtime_lock); | 
| Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 662 | 			if (want > 0) { | 
 | 663 | 				diff = min_t(s64, iter->rt_runtime, want); | 
 | 664 | 				iter->rt_runtime -= diff; | 
 | 665 | 				want -= diff; | 
 | 666 | 			} else { | 
 | 667 | 				iter->rt_runtime -= want; | 
 | 668 | 				want -= want; | 
 | 669 | 			} | 
| Thomas Gleixner | 0986b11 | 2009-11-17 15:32:06 +0100 | [diff] [blame] | 670 | 			raw_spin_unlock(&iter->rt_runtime_lock); | 
| Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 671 |  | 
 | 672 | 			if (!want) | 
 | 673 | 				break; | 
 | 674 | 		} | 
 | 675 |  | 
| Thomas Gleixner | 0986b11 | 2009-11-17 15:32:06 +0100 | [diff] [blame] | 676 | 		raw_spin_lock(&rt_rq->rt_runtime_lock); | 
| Peter Zijlstra | 78333cd | 2008-09-23 15:33:43 +0200 | [diff] [blame] | 677 | 		/* | 
 | 678 | 		 * We cannot be left wanting - that would mean some runtime | 
 | 679 | 		 * leaked out of the system. | 
 | 680 | 		 */ | 
| Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 681 | 		BUG_ON(want); | 
 | 682 | balanced: | 
| Peter Zijlstra | 78333cd | 2008-09-23 15:33:43 +0200 | [diff] [blame] | 683 | 		/* | 
 | 684 | 		 * Disable all the borrow logic by pretending we have inf | 
 | 685 | 		 * runtime - in which case borrowing doesn't make sense. | 
 | 686 | 		 */ | 
| Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 687 | 		rt_rq->rt_runtime = RUNTIME_INF; | 
| Thomas Gleixner | 0986b11 | 2009-11-17 15:32:06 +0100 | [diff] [blame] | 688 | 		raw_spin_unlock(&rt_rq->rt_runtime_lock); | 
 | 689 | 		raw_spin_unlock(&rt_b->rt_runtime_lock); | 
| Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 690 | 	} | 
 | 691 | } | 
 | 692 |  | 
 | 693 | static void disable_runtime(struct rq *rq) | 
 | 694 | { | 
 | 695 | 	unsigned long flags; | 
 | 696 |  | 
| Thomas Gleixner | 05fa785 | 2009-11-17 14:28:38 +0100 | [diff] [blame] | 697 | 	raw_spin_lock_irqsave(&rq->lock, flags); | 
| Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 698 | 	__disable_runtime(rq); | 
| Thomas Gleixner | 05fa785 | 2009-11-17 14:28:38 +0100 | [diff] [blame] | 699 | 	raw_spin_unlock_irqrestore(&rq->lock, flags); | 
| Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 700 | } | 
 | 701 |  | 
 | 702 | static void __enable_runtime(struct rq *rq) | 
 | 703 | { | 
| Cheng Xu | ec514c4 | 2011-05-14 14:20:02 +0800 | [diff] [blame] | 704 | 	rt_rq_iter_t iter; | 
| Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 705 | 	struct rt_rq *rt_rq; | 
 | 706 |  | 
 | 707 | 	if (unlikely(!scheduler_running)) | 
 | 708 | 		return; | 
 | 709 |  | 
| Peter Zijlstra | 78333cd | 2008-09-23 15:33:43 +0200 | [diff] [blame] | 710 | 	/* | 
 | 711 | 	 * Reset each runqueue's bandwidth settings | 
 | 712 | 	 */ | 
| Cheng Xu | ec514c4 | 2011-05-14 14:20:02 +0800 | [diff] [blame] | 713 | 	for_each_rt_rq(rt_rq, iter, rq) { | 
| Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 714 | 		struct rt_bandwidth *rt_b = sched_rt_bandwidth(rt_rq); | 
 | 715 |  | 
| Thomas Gleixner | 0986b11 | 2009-11-17 15:32:06 +0100 | [diff] [blame] | 716 | 		raw_spin_lock(&rt_b->rt_runtime_lock); | 
 | 717 | 		raw_spin_lock(&rt_rq->rt_runtime_lock); | 
| Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 718 | 		rt_rq->rt_runtime = rt_b->rt_runtime; | 
 | 719 | 		rt_rq->rt_time = 0; | 
| Zhang, Yanmin | baf2573 | 2008-09-09 11:26:33 +0800 | [diff] [blame] | 720 | 		rt_rq->rt_throttled = 0; | 
| Thomas Gleixner | 0986b11 | 2009-11-17 15:32:06 +0100 | [diff] [blame] | 721 | 		raw_spin_unlock(&rt_rq->rt_runtime_lock); | 
 | 722 | 		raw_spin_unlock(&rt_b->rt_runtime_lock); | 
| Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 723 | 	} | 
 | 724 | } | 
 | 725 |  | 
 | 726 | static void enable_runtime(struct rq *rq) | 
 | 727 | { | 
 | 728 | 	unsigned long flags; | 
 | 729 |  | 
| Thomas Gleixner | 05fa785 | 2009-11-17 14:28:38 +0100 | [diff] [blame] | 730 | 	raw_spin_lock_irqsave(&rq->lock, flags); | 
| Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 731 | 	__enable_runtime(rq); | 
| Thomas Gleixner | 05fa785 | 2009-11-17 14:28:38 +0100 | [diff] [blame] | 732 | 	raw_spin_unlock_irqrestore(&rq->lock, flags); | 
| Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 733 | } | 
 | 734 |  | 
| Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 735 | int update_runtime(struct notifier_block *nfb, unsigned long action, void *hcpu) | 
 | 736 | { | 
 | 737 | 	int cpu = (int)(long)hcpu; | 
 | 738 |  | 
 | 739 | 	switch (action) { | 
 | 740 | 	case CPU_DOWN_PREPARE: | 
 | 741 | 	case CPU_DOWN_PREPARE_FROZEN: | 
 | 742 | 		disable_runtime(cpu_rq(cpu)); | 
 | 743 | 		return NOTIFY_OK; | 
 | 744 |  | 
 | 745 | 	case CPU_DOWN_FAILED: | 
 | 746 | 	case CPU_DOWN_FAILED_FROZEN: | 
 | 747 | 	case CPU_ONLINE: | 
 | 748 | 	case CPU_ONLINE_FROZEN: | 
 | 749 | 		enable_runtime(cpu_rq(cpu)); | 
 | 750 | 		return NOTIFY_OK; | 
 | 751 |  | 
 | 752 | 	default: | 
 | 753 | 		return NOTIFY_DONE; | 
 | 754 | 	} | 
 | 755 | } | 
 | 756 |  | 
| Peter Zijlstra | eff6549 | 2008-06-19 14:22:26 +0200 | [diff] [blame] | 757 | static int balance_runtime(struct rt_rq *rt_rq) | 
 | 758 | { | 
 | 759 | 	int more = 0; | 
 | 760 |  | 
| Peter Zijlstra | 4a6184c | 2011-10-06 22:39:14 +0200 | [diff] [blame] | 761 | 	if (!sched_feat(RT_RUNTIME_SHARE)) | 
 | 762 | 		return more; | 
 | 763 |  | 
| Peter Zijlstra | eff6549 | 2008-06-19 14:22:26 +0200 | [diff] [blame] | 764 | 	if (rt_rq->rt_time > rt_rq->rt_runtime) { | 
| Thomas Gleixner | 0986b11 | 2009-11-17 15:32:06 +0100 | [diff] [blame] | 765 | 		raw_spin_unlock(&rt_rq->rt_runtime_lock); | 
| Peter Zijlstra | eff6549 | 2008-06-19 14:22:26 +0200 | [diff] [blame] | 766 | 		more = do_balance_runtime(rt_rq); | 
| Thomas Gleixner | 0986b11 | 2009-11-17 15:32:06 +0100 | [diff] [blame] | 767 | 		raw_spin_lock(&rt_rq->rt_runtime_lock); | 
| Peter Zijlstra | eff6549 | 2008-06-19 14:22:26 +0200 | [diff] [blame] | 768 | 	} | 
 | 769 |  | 
 | 770 | 	return more; | 
 | 771 | } | 
| Dhaval Giani | 55e12e5 | 2008-06-24 23:39:43 +0530 | [diff] [blame] | 772 | #else /* !CONFIG_SMP */ | 
| Peter Zijlstra | eff6549 | 2008-06-19 14:22:26 +0200 | [diff] [blame] | 773 | static inline int balance_runtime(struct rt_rq *rt_rq) | 
 | 774 | { | 
 | 775 | 	return 0; | 
 | 776 | } | 
| Dhaval Giani | 55e12e5 | 2008-06-24 23:39:43 +0530 | [diff] [blame] | 777 | #endif /* CONFIG_SMP */ | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 778 |  | 
 | 779 | static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun) | 
 | 780 | { | 
 | 781 | 	int i, idle = 1; | 
| Rusty Russell | c6c4927 | 2008-11-25 02:35:05 +1030 | [diff] [blame] | 782 | 	const struct cpumask *span; | 
| Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 783 |  | 
| Peter Zijlstra | 0b148fa | 2008-08-19 12:33:04 +0200 | [diff] [blame] | 784 | 	if (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF) | 
| Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 785 | 		return 1; | 
 | 786 |  | 
 | 787 | 	span = sched_rt_period_mask(); | 
| Rusty Russell | c6c4927 | 2008-11-25 02:35:05 +1030 | [diff] [blame] | 788 | 	for_each_cpu(i, span) { | 
| Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 789 | 		int enqueue = 0; | 
 | 790 | 		struct rt_rq *rt_rq = sched_rt_period_rt_rq(rt_b, i); | 
 | 791 | 		struct rq *rq = rq_of_rt_rq(rt_rq); | 
 | 792 |  | 
| Thomas Gleixner | 05fa785 | 2009-11-17 14:28:38 +0100 | [diff] [blame] | 793 | 		raw_spin_lock(&rq->lock); | 
| Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 794 | 		if (rt_rq->rt_time) { | 
 | 795 | 			u64 runtime; | 
 | 796 |  | 
| Thomas Gleixner | 0986b11 | 2009-11-17 15:32:06 +0100 | [diff] [blame] | 797 | 			raw_spin_lock(&rt_rq->rt_runtime_lock); | 
| Peter Zijlstra | eff6549 | 2008-06-19 14:22:26 +0200 | [diff] [blame] | 798 | 			if (rt_rq->rt_throttled) | 
 | 799 | 				balance_runtime(rt_rq); | 
| Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 800 | 			runtime = rt_rq->rt_runtime; | 
 | 801 | 			rt_rq->rt_time -= min(rt_rq->rt_time, overrun*runtime); | 
 | 802 | 			if (rt_rq->rt_throttled && rt_rq->rt_time < runtime) { | 
 | 803 | 				rt_rq->rt_throttled = 0; | 
 | 804 | 				enqueue = 1; | 
| Mike Galbraith | 61eadef | 2011-04-29 08:36:50 +0200 | [diff] [blame] | 805 |  | 
 | 806 | 				/* | 
 | 807 | 				 * Force a clock update if the CPU was idle, | 
 | 808 | 				 * lest wakeup -> unthrottle time accumulate. | 
 | 809 | 				 */ | 
 | 810 | 				if (rt_rq->rt_nr_running && rq->curr == rq->idle) | 
 | 811 | 					rq->skip_clock_update = -1; | 
| Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 812 | 			} | 
 | 813 | 			if (rt_rq->rt_time || rt_rq->rt_nr_running) | 
 | 814 | 				idle = 0; | 
| Thomas Gleixner | 0986b11 | 2009-11-17 15:32:06 +0100 | [diff] [blame] | 815 | 			raw_spin_unlock(&rt_rq->rt_runtime_lock); | 
| Balbir Singh | 0c3b916 | 2011-03-03 17:04:35 +0530 | [diff] [blame] | 816 | 		} else if (rt_rq->rt_nr_running) { | 
| Peter Zijlstra | 8a8cde1 | 2008-06-19 14:22:28 +0200 | [diff] [blame] | 817 | 			idle = 0; | 
| Balbir Singh | 0c3b916 | 2011-03-03 17:04:35 +0530 | [diff] [blame] | 818 | 			if (!rt_rq_throttled(rt_rq)) | 
 | 819 | 				enqueue = 1; | 
 | 820 | 		} | 
| Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 821 |  | 
 | 822 | 		if (enqueue) | 
 | 823 | 			sched_rt_rq_enqueue(rt_rq); | 
| Thomas Gleixner | 05fa785 | 2009-11-17 14:28:38 +0100 | [diff] [blame] | 824 | 		raw_spin_unlock(&rq->lock); | 
| Peter Zijlstra | d0b27fa | 2008-04-19 19:44:57 +0200 | [diff] [blame] | 825 | 	} | 
 | 826 |  | 
 | 827 | 	return idle; | 
 | 828 | } | 
 | 829 |  | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 830 | static inline int rt_se_prio(struct sched_rt_entity *rt_se) | 
 | 831 | { | 
| Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 832 | #ifdef CONFIG_RT_GROUP_SCHED | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 833 | 	struct rt_rq *rt_rq = group_rt_rq(rt_se); | 
 | 834 |  | 
 | 835 | 	if (rt_rq) | 
| Gregory Haskins | e864c49 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 836 | 		return rt_rq->highest_prio.curr; | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 837 | #endif | 
 | 838 |  | 
 | 839 | 	return rt_task_of(rt_se)->prio; | 
 | 840 | } | 
 | 841 |  | 
| Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 842 | static int sched_rt_runtime_exceeded(struct rt_rq *rt_rq) | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 843 | { | 
| Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 844 | 	u64 runtime = sched_rt_runtime(rt_rq); | 
| Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 845 |  | 
| Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 846 | 	if (rt_rq->rt_throttled) | 
| Peter Zijlstra | 23b0fdf | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 847 | 		return rt_rq_throttled(rt_rq); | 
| Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 848 |  | 
| Shan Hai | 5b680fd | 2011-11-29 11:03:56 +0800 | [diff] [blame] | 849 | 	if (runtime >= sched_rt_period(rt_rq)) | 
| Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 850 | 		return 0; | 
 | 851 |  | 
| Peter Zijlstra | b79f383 | 2008-06-19 14:22:25 +0200 | [diff] [blame] | 852 | 	balance_runtime(rt_rq); | 
 | 853 | 	runtime = sched_rt_runtime(rt_rq); | 
 | 854 | 	if (runtime == RUNTIME_INF) | 
 | 855 | 		return 0; | 
| Peter Zijlstra | ac086bc | 2008-04-19 19:44:58 +0200 | [diff] [blame] | 856 |  | 
| Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 857 | 	if (rt_rq->rt_time > runtime) { | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 858 | 		rt_rq->rt_throttled = 1; | 
| Thomas Gleixner | 1c83437 | 2011-10-05 13:32:34 +0200 | [diff] [blame] | 859 | 		printk_once(KERN_WARNING "sched: RT throttling activated\n"); | 
| Peter Zijlstra | 23b0fdf | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 860 | 		if (rt_rq_throttled(rt_rq)) { | 
| Peter Zijlstra | 9f0c1e5 | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 861 | 			sched_rt_rq_dequeue(rt_rq); | 
| Peter Zijlstra | 23b0fdf | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 862 | 			return 1; | 
 | 863 | 		} | 
| Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 864 | 	} | 
 | 865 |  | 
 | 866 | 	return 0; | 
 | 867 | } | 
 | 868 |  | 
| Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 869 | /* | 
 | 870 |  * Update the current task's runtime statistics. Skip current tasks that | 
 | 871 |  * are not in our scheduling class. | 
 | 872 |  */ | 
| Alexey Dobriyan | a995744 | 2007-10-15 17:00:13 +0200 | [diff] [blame] | 873 | static void update_curr_rt(struct rq *rq) | 
| Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 874 | { | 
 | 875 | 	struct task_struct *curr = rq->curr; | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 876 | 	struct sched_rt_entity *rt_se = &curr->rt; | 
 | 877 | 	struct rt_rq *rt_rq = rt_rq_of_se(rt_se); | 
| Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 878 | 	u64 delta_exec; | 
 | 879 |  | 
| Peter Zijlstra | 06c3bc6 | 2011-02-02 13:19:48 +0100 | [diff] [blame] | 880 | 	if (curr->sched_class != &rt_sched_class) | 
| Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 881 | 		return; | 
 | 882 |  | 
| Venkatesh Pallipadi | 305e683 | 2010-10-04 17:03:21 -0700 | [diff] [blame] | 883 | 	delta_exec = rq->clock_task - curr->se.exec_start; | 
| Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 884 | 	if (unlikely((s64)delta_exec < 0)) | 
 | 885 | 		delta_exec = 0; | 
| Ingo Molnar | 6cfb0d5 | 2007-08-02 17:41:40 +0200 | [diff] [blame] | 886 |  | 
| Lucas De Marchi | 41acab8 | 2010-03-10 23:37:45 -0300 | [diff] [blame] | 887 | 	schedstat_set(curr->se.statistics.exec_max, max(curr->se.statistics.exec_max, delta_exec)); | 
| Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 888 |  | 
 | 889 | 	curr->se.sum_exec_runtime += delta_exec; | 
| Frank Mayhar | f06febc | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 890 | 	account_group_exec_runtime(curr, delta_exec); | 
 | 891 |  | 
| Venkatesh Pallipadi | 305e683 | 2010-10-04 17:03:21 -0700 | [diff] [blame] | 892 | 	curr->se.exec_start = rq->clock_task; | 
| Srivatsa Vaddagiri | d842de8 | 2007-12-02 20:04:49 +0100 | [diff] [blame] | 893 | 	cpuacct_charge(curr, delta_exec); | 
| Peter Zijlstra | fa85ae2 | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 894 |  | 
| Peter Zijlstra | e9e9250 | 2009-09-01 10:34:37 +0200 | [diff] [blame] | 895 | 	sched_rt_avg_update(rq, delta_exec); | 
 | 896 |  | 
| Peter Zijlstra | 0b148fa | 2008-08-19 12:33:04 +0200 | [diff] [blame] | 897 | 	if (!rt_bandwidth_enabled()) | 
 | 898 | 		return; | 
 | 899 |  | 
| Dhaval Giani | 354d60c | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 900 | 	for_each_sched_rt_entity(rt_se) { | 
 | 901 | 		rt_rq = rt_rq_of_se(rt_se); | 
 | 902 |  | 
| Peter Zijlstra | cc2991c | 2008-08-19 12:33:03 +0200 | [diff] [blame] | 903 | 		if (sched_rt_runtime(rt_rq) != RUNTIME_INF) { | 
| Thomas Gleixner | 0986b11 | 2009-11-17 15:32:06 +0100 | [diff] [blame] | 904 | 			raw_spin_lock(&rt_rq->rt_runtime_lock); | 
| Peter Zijlstra | cc2991c | 2008-08-19 12:33:03 +0200 | [diff] [blame] | 905 | 			rt_rq->rt_time += delta_exec; | 
 | 906 | 			if (sched_rt_runtime_exceeded(rt_rq)) | 
 | 907 | 				resched_task(curr); | 
| Thomas Gleixner | 0986b11 | 2009-11-17 15:32:06 +0100 | [diff] [blame] | 908 | 			raw_spin_unlock(&rt_rq->rt_runtime_lock); | 
| Peter Zijlstra | cc2991c | 2008-08-19 12:33:03 +0200 | [diff] [blame] | 909 | 		} | 
| Dhaval Giani | 354d60c | 2008-04-19 19:44:59 +0200 | [diff] [blame] | 910 | 	} | 
| Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 911 | } | 
 | 912 |  | 
| Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 913 | #if defined CONFIG_SMP | 
| Gregory Haskins | e864c49 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 914 |  | 
| Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 915 | static void | 
 | 916 | inc_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) | 
| Steven Rostedt | 63489e4 | 2008-01-25 21:08:03 +0100 | [diff] [blame] | 917 | { | 
| Gregory Haskins | 4d98427 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 918 | 	struct rq *rq = rq_of_rt_rq(rt_rq); | 
| Gregory Haskins | 4d98427 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 919 |  | 
| Steven Rostedt | 5181f4a4 | 2011-06-16 21:55:23 -0400 | [diff] [blame] | 920 | 	if (rq->online && prio < prev_prio) | 
 | 921 | 		cpupri_set(&rq->rd->cpupri, rq->cpu, prio); | 
| Steven Rostedt | 63489e4 | 2008-01-25 21:08:03 +0100 | [diff] [blame] | 922 | } | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 923 |  | 
| Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 924 | static void | 
 | 925 | dec_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) | 
| Steven Rostedt | 63489e4 | 2008-01-25 21:08:03 +0100 | [diff] [blame] | 926 | { | 
| Gregory Haskins | 4d98427 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 927 | 	struct rq *rq = rq_of_rt_rq(rt_rq); | 
| Gregory Haskins | 6e0534f | 2008-05-12 21:21:01 +0200 | [diff] [blame] | 928 |  | 
| Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 929 | 	if (rq->online && rt_rq->highest_prio.curr != prev_prio) | 
 | 930 | 		cpupri_set(&rq->rd->cpupri, rq->cpu, rt_rq->highest_prio.curr); | 
 | 931 | } | 
 | 932 |  | 
 | 933 | #else /* CONFIG_SMP */ | 
 | 934 |  | 
 | 935 | static inline | 
 | 936 | void inc_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) {} | 
 | 937 | static inline | 
 | 938 | void dec_rt_prio_smp(struct rt_rq *rt_rq, int prio, int prev_prio) {} | 
 | 939 |  | 
 | 940 | #endif /* CONFIG_SMP */ | 
 | 941 |  | 
| Steven Rostedt | 63489e4 | 2008-01-25 21:08:03 +0100 | [diff] [blame] | 942 | #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED | 
| Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 943 | static void | 
 | 944 | inc_rt_prio(struct rt_rq *rt_rq, int prio) | 
 | 945 | { | 
 | 946 | 	int prev_prio = rt_rq->highest_prio.curr; | 
| Steven Rostedt | 63489e4 | 2008-01-25 21:08:03 +0100 | [diff] [blame] | 947 |  | 
| Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 948 | 	if (prio < prev_prio) | 
 | 949 | 		rt_rq->highest_prio.curr = prio; | 
 | 950 |  | 
 | 951 | 	inc_rt_prio_smp(rt_rq, prio, prev_prio); | 
 | 952 | } | 
 | 953 |  | 
 | 954 | static void | 
 | 955 | dec_rt_prio(struct rt_rq *rt_rq, int prio) | 
 | 956 | { | 
 | 957 | 	int prev_prio = rt_rq->highest_prio.curr; | 
 | 958 |  | 
 | 959 | 	if (rt_rq->rt_nr_running) { | 
 | 960 |  | 
 | 961 | 		WARN_ON(prio < prev_prio); | 
| Gregory Haskins | e864c49 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 962 |  | 
 | 963 | 		/* | 
| Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 964 | 		 * This may have been our highest task, and therefore | 
 | 965 | 		 * we may have some recomputation to do | 
| Gregory Haskins | e864c49 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 966 | 		 */ | 
| Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 967 | 		if (prio == prev_prio) { | 
| Gregory Haskins | e864c49 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 968 | 			struct rt_prio_array *array = &rt_rq->active; | 
 | 969 |  | 
 | 970 | 			rt_rq->highest_prio.curr = | 
| Steven Rostedt | 764a9d6 | 2008-01-25 21:08:04 +0100 | [diff] [blame] | 971 | 				sched_find_first_bit(array->bitmap); | 
| Gregory Haskins | e864c49 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 972 | 		} | 
 | 973 |  | 
| Steven Rostedt | 764a9d6 | 2008-01-25 21:08:04 +0100 | [diff] [blame] | 974 | 	} else | 
| Gregory Haskins | e864c49 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 975 | 		rt_rq->highest_prio.curr = MAX_RT_PRIO; | 
| Gregory Haskins | 73fe6aa | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 976 |  | 
| Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 977 | 	dec_rt_prio_smp(rt_rq, prio, prev_prio); | 
 | 978 | } | 
| Gregory Haskins | 1f11eb6 | 2008-06-04 15:04:05 -0400 | [diff] [blame] | 979 |  | 
| Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 980 | #else | 
 | 981 |  | 
 | 982 | static inline void inc_rt_prio(struct rt_rq *rt_rq, int prio) {} | 
 | 983 | static inline void dec_rt_prio(struct rt_rq *rt_rq, int prio) {} | 
 | 984 |  | 
 | 985 | #endif /* CONFIG_SMP || CONFIG_RT_GROUP_SCHED */ | 
 | 986 |  | 
| Gregory Haskins | 73fe6aa | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 987 | #ifdef CONFIG_RT_GROUP_SCHED | 
| Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 988 |  | 
 | 989 | static void | 
 | 990 | inc_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) | 
 | 991 | { | 
| Gregory Haskins | 73fe6aa | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 992 | 	if (rt_se_boosted(rt_se)) | 
| Steven Rostedt | 764a9d6 | 2008-01-25 21:08:04 +0100 | [diff] [blame] | 993 | 		rt_rq->rt_nr_boosted++; | 
| Peter Zijlstra | 052f1dc | 2008-02-13 15:45:40 +0100 | [diff] [blame] | 994 |  | 
| Peter Zijlstra | 23b0fdf | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 995 | 	if (rt_rq->tg) | 
 | 996 | 		start_rt_bandwidth(&rt_rq->tg->rt_bandwidth); | 
| Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 997 | } | 
 | 998 |  | 
 | 999 | static void | 
 | 1000 | dec_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) | 
 | 1001 | { | 
| Peter Zijlstra | 23b0fdf | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 1002 | 	if (rt_se_boosted(rt_se)) | 
 | 1003 | 		rt_rq->rt_nr_boosted--; | 
 | 1004 |  | 
 | 1005 | 	WARN_ON(!rt_rq->rt_nr_running && rt_rq->rt_nr_boosted); | 
| Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 1006 | } | 
 | 1007 |  | 
 | 1008 | #else /* CONFIG_RT_GROUP_SCHED */ | 
 | 1009 |  | 
 | 1010 | static void | 
 | 1011 | inc_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) | 
 | 1012 | { | 
| Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1013 | 	start_rt_bandwidth(&def_rt_bandwidth); | 
| Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 1014 | } | 
 | 1015 |  | 
 | 1016 | static inline | 
 | 1017 | void dec_rt_group(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) {} | 
 | 1018 |  | 
 | 1019 | #endif /* CONFIG_RT_GROUP_SCHED */ | 
 | 1020 |  | 
 | 1021 | static inline | 
 | 1022 | void inc_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) | 
 | 1023 | { | 
 | 1024 | 	int prio = rt_se_prio(rt_se); | 
 | 1025 |  | 
 | 1026 | 	WARN_ON(!rt_prio(prio)); | 
 | 1027 | 	rt_rq->rt_nr_running++; | 
 | 1028 |  | 
 | 1029 | 	inc_rt_prio(rt_rq, prio); | 
 | 1030 | 	inc_rt_migration(rt_se, rt_rq); | 
 | 1031 | 	inc_rt_group(rt_se, rt_rq); | 
| Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1032 | } | 
 | 1033 |  | 
| Peter Zijlstra | 23b0fdf | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 1034 | static inline | 
 | 1035 | void dec_rt_tasks(struct sched_rt_entity *rt_se, struct rt_rq *rt_rq) | 
 | 1036 | { | 
| Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1037 | 	WARN_ON(!rt_prio(rt_se_prio(rt_se))); | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1038 | 	WARN_ON(!rt_rq->rt_nr_running); | 
| Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1039 | 	rt_rq->rt_nr_running--; | 
| Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1040 |  | 
| Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 1041 | 	dec_rt_prio(rt_rq, rt_se_prio(rt_se)); | 
 | 1042 | 	dec_rt_migration(rt_se, rt_rq); | 
 | 1043 | 	dec_rt_group(rt_se, rt_rq); | 
| Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1044 | } | 
 | 1045 |  | 
| Thomas Gleixner | 37dad3f | 2010-01-20 20:59:01 +0000 | [diff] [blame] | 1046 | static void __enqueue_rt_entity(struct sched_rt_entity *rt_se, bool head) | 
| Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1047 | { | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1048 | 	struct rt_rq *rt_rq = rt_rq_of_se(rt_se); | 
 | 1049 | 	struct rt_prio_array *array = &rt_rq->active; | 
 | 1050 | 	struct rt_rq *group_rq = group_rt_rq(rt_se); | 
| Dmitry Adamushko | 20b6331 | 2008-06-11 00:58:30 +0200 | [diff] [blame] | 1051 | 	struct list_head *queue = array->queue + rt_se_prio(rt_se); | 
| Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1052 |  | 
| Peter Zijlstra | ad2a3f1 | 2008-06-19 09:06:57 +0200 | [diff] [blame] | 1053 | 	/* | 
 | 1054 | 	 * Don't enqueue the group if its throttled, or when empty. | 
 | 1055 | 	 * The latter is a consequence of the former when a child group | 
 | 1056 | 	 * get throttled and the current group doesn't have any other | 
 | 1057 | 	 * active members. | 
 | 1058 | 	 */ | 
 | 1059 | 	if (group_rq && (rt_rq_throttled(group_rq) || !group_rq->rt_nr_running)) | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1060 | 		return; | 
| Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1061 |  | 
| Peter Zijlstra | 3d4b47b | 2010-11-15 15:47:01 -0800 | [diff] [blame] | 1062 | 	if (!rt_rq->rt_nr_running) | 
 | 1063 | 		list_add_leaf_rt_rq(rt_rq); | 
 | 1064 |  | 
| Thomas Gleixner | 37dad3f | 2010-01-20 20:59:01 +0000 | [diff] [blame] | 1065 | 	if (head) | 
 | 1066 | 		list_add(&rt_se->run_list, queue); | 
 | 1067 | 	else | 
 | 1068 | 		list_add_tail(&rt_se->run_list, queue); | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1069 | 	__set_bit(rt_se_prio(rt_se), array->bitmap); | 
| Peter Zijlstra | 78f2c7d | 2008-01-25 21:08:27 +0100 | [diff] [blame] | 1070 |  | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1071 | 	inc_rt_tasks(rt_se, rt_rq); | 
 | 1072 | } | 
 | 1073 |  | 
| Peter Zijlstra | ad2a3f1 | 2008-06-19 09:06:57 +0200 | [diff] [blame] | 1074 | static void __dequeue_rt_entity(struct sched_rt_entity *rt_se) | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1075 | { | 
 | 1076 | 	struct rt_rq *rt_rq = rt_rq_of_se(rt_se); | 
 | 1077 | 	struct rt_prio_array *array = &rt_rq->active; | 
 | 1078 |  | 
 | 1079 | 	list_del_init(&rt_se->run_list); | 
 | 1080 | 	if (list_empty(array->queue + rt_se_prio(rt_se))) | 
 | 1081 | 		__clear_bit(rt_se_prio(rt_se), array->bitmap); | 
 | 1082 |  | 
 | 1083 | 	dec_rt_tasks(rt_se, rt_rq); | 
| Peter Zijlstra | 3d4b47b | 2010-11-15 15:47:01 -0800 | [diff] [blame] | 1084 | 	if (!rt_rq->rt_nr_running) | 
 | 1085 | 		list_del_leaf_rt_rq(rt_rq); | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1086 | } | 
 | 1087 |  | 
 | 1088 | /* | 
 | 1089 |  * Because the prio of an upper entry depends on the lower | 
 | 1090 |  * entries, we must remove entries top - down. | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1091 |  */ | 
| Peter Zijlstra | ad2a3f1 | 2008-06-19 09:06:57 +0200 | [diff] [blame] | 1092 | static void dequeue_rt_stack(struct sched_rt_entity *rt_se) | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1093 | { | 
| Peter Zijlstra | ad2a3f1 | 2008-06-19 09:06:57 +0200 | [diff] [blame] | 1094 | 	struct sched_rt_entity *back = NULL; | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1095 |  | 
| Peter Zijlstra | 58d6c2d | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 1096 | 	for_each_sched_rt_entity(rt_se) { | 
 | 1097 | 		rt_se->back = back; | 
 | 1098 | 		back = rt_se; | 
 | 1099 | 	} | 
 | 1100 |  | 
 | 1101 | 	for (rt_se = back; rt_se; rt_se = rt_se->back) { | 
 | 1102 | 		if (on_rt_rq(rt_se)) | 
| Peter Zijlstra | ad2a3f1 | 2008-06-19 09:06:57 +0200 | [diff] [blame] | 1103 | 			__dequeue_rt_entity(rt_se); | 
 | 1104 | 	} | 
 | 1105 | } | 
 | 1106 |  | 
| Thomas Gleixner | 37dad3f | 2010-01-20 20:59:01 +0000 | [diff] [blame] | 1107 | static void enqueue_rt_entity(struct sched_rt_entity *rt_se, bool head) | 
| Peter Zijlstra | ad2a3f1 | 2008-06-19 09:06:57 +0200 | [diff] [blame] | 1108 | { | 
 | 1109 | 	dequeue_rt_stack(rt_se); | 
 | 1110 | 	for_each_sched_rt_entity(rt_se) | 
| Thomas Gleixner | 37dad3f | 2010-01-20 20:59:01 +0000 | [diff] [blame] | 1111 | 		__enqueue_rt_entity(rt_se, head); | 
| Peter Zijlstra | ad2a3f1 | 2008-06-19 09:06:57 +0200 | [diff] [blame] | 1112 | } | 
 | 1113 |  | 
 | 1114 | static void dequeue_rt_entity(struct sched_rt_entity *rt_se) | 
 | 1115 | { | 
 | 1116 | 	dequeue_rt_stack(rt_se); | 
 | 1117 |  | 
 | 1118 | 	for_each_sched_rt_entity(rt_se) { | 
 | 1119 | 		struct rt_rq *rt_rq = group_rt_rq(rt_se); | 
 | 1120 |  | 
 | 1121 | 		if (rt_rq && rt_rq->rt_nr_running) | 
| Thomas Gleixner | 37dad3f | 2010-01-20 20:59:01 +0000 | [diff] [blame] | 1122 | 			__enqueue_rt_entity(rt_se, false); | 
| Peter Zijlstra | 58d6c2d | 2008-04-19 19:45:00 +0200 | [diff] [blame] | 1123 | 	} | 
| Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1124 | } | 
 | 1125 |  | 
 | 1126 | /* | 
 | 1127 |  * Adding/removing a task to/from a priority array: | 
 | 1128 |  */ | 
| Thomas Gleixner | ea87bb7 | 2010-01-20 20:58:57 +0000 | [diff] [blame] | 1129 | static void | 
| Peter Zijlstra | 371fd7e | 2010-03-24 16:38:48 +0100 | [diff] [blame] | 1130 | enqueue_task_rt(struct rq *rq, struct task_struct *p, int flags) | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1131 | { | 
 | 1132 | 	struct sched_rt_entity *rt_se = &p->rt; | 
 | 1133 |  | 
| Peter Zijlstra | 371fd7e | 2010-03-24 16:38:48 +0100 | [diff] [blame] | 1134 | 	if (flags & ENQUEUE_WAKEUP) | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1135 | 		rt_se->timeout = 0; | 
 | 1136 |  | 
| Peter Zijlstra | 371fd7e | 2010-03-24 16:38:48 +0100 | [diff] [blame] | 1137 | 	enqueue_rt_entity(rt_se, flags & ENQUEUE_HEAD); | 
| Peter Zijlstra | c09595f | 2008-06-27 13:41:14 +0200 | [diff] [blame] | 1138 |  | 
| Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1139 | 	if (!task_current(rq, p) && p->rt.nr_cpus_allowed > 1) | 
 | 1140 | 		enqueue_pushable_task(rq, p); | 
| Paul Turner | 953bfcd | 2011-07-21 09:43:27 -0700 | [diff] [blame] | 1141 |  | 
 | 1142 | 	inc_nr_running(rq); | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1143 | } | 
 | 1144 |  | 
| Peter Zijlstra | 371fd7e | 2010-03-24 16:38:48 +0100 | [diff] [blame] | 1145 | static void dequeue_task_rt(struct rq *rq, struct task_struct *p, int flags) | 
| Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1146 | { | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1147 | 	struct sched_rt_entity *rt_se = &p->rt; | 
| Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1148 |  | 
 | 1149 | 	update_curr_rt(rq); | 
| Peter Zijlstra | ad2a3f1 | 2008-06-19 09:06:57 +0200 | [diff] [blame] | 1150 | 	dequeue_rt_entity(rt_se); | 
| Peter Zijlstra | c09595f | 2008-06-27 13:41:14 +0200 | [diff] [blame] | 1151 |  | 
| Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1152 | 	dequeue_pushable_task(rq, p); | 
| Paul Turner | 953bfcd | 2011-07-21 09:43:27 -0700 | [diff] [blame] | 1153 |  | 
 | 1154 | 	dec_nr_running(rq); | 
| Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1155 | } | 
 | 1156 |  | 
 | 1157 | /* | 
| Richard Weinberger | 6068631 | 2011-11-12 18:07:57 +0100 | [diff] [blame] | 1158 |  * Put task to the head or the end of the run list without the overhead of | 
 | 1159 |  * dequeue followed by enqueue. | 
| Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1160 |  */ | 
| Dmitry Adamushko | 7ebefa8 | 2008-07-01 23:32:15 +0200 | [diff] [blame] | 1161 | static void | 
 | 1162 | requeue_rt_entity(struct rt_rq *rt_rq, struct sched_rt_entity *rt_se, int head) | 
| Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1163 | { | 
| Ingo Molnar | 1cdad71 | 2008-06-19 09:09:15 +0200 | [diff] [blame] | 1164 | 	if (on_rt_rq(rt_se)) { | 
| Dmitry Adamushko | 7ebefa8 | 2008-07-01 23:32:15 +0200 | [diff] [blame] | 1165 | 		struct rt_prio_array *array = &rt_rq->active; | 
 | 1166 | 		struct list_head *queue = array->queue + rt_se_prio(rt_se); | 
 | 1167 |  | 
 | 1168 | 		if (head) | 
 | 1169 | 			list_move(&rt_se->run_list, queue); | 
 | 1170 | 		else | 
 | 1171 | 			list_move_tail(&rt_se->run_list, queue); | 
| Ingo Molnar | 1cdad71 | 2008-06-19 09:09:15 +0200 | [diff] [blame] | 1172 | 	} | 
| Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1173 | } | 
 | 1174 |  | 
| Dmitry Adamushko | 7ebefa8 | 2008-07-01 23:32:15 +0200 | [diff] [blame] | 1175 | static void requeue_task_rt(struct rq *rq, struct task_struct *p, int head) | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1176 | { | 
 | 1177 | 	struct sched_rt_entity *rt_se = &p->rt; | 
 | 1178 | 	struct rt_rq *rt_rq; | 
 | 1179 |  | 
 | 1180 | 	for_each_sched_rt_entity(rt_se) { | 
 | 1181 | 		rt_rq = rt_rq_of_se(rt_se); | 
| Dmitry Adamushko | 7ebefa8 | 2008-07-01 23:32:15 +0200 | [diff] [blame] | 1182 | 		requeue_rt_entity(rt_rq, rt_se, head); | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1183 | 	} | 
 | 1184 | } | 
 | 1185 |  | 
 | 1186 | static void yield_task_rt(struct rq *rq) | 
| Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1187 | { | 
| Dmitry Adamushko | 7ebefa8 | 2008-07-01 23:32:15 +0200 | [diff] [blame] | 1188 | 	requeue_task_rt(rq, rq->curr, 0); | 
| Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1189 | } | 
 | 1190 |  | 
| Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 1191 | #ifdef CONFIG_SMP | 
| Gregory Haskins | 318e089 | 2008-01-25 21:08:10 +0100 | [diff] [blame] | 1192 | static int find_lowest_rq(struct task_struct *task); | 
 | 1193 |  | 
| Peter Zijlstra | 0017d73 | 2010-03-24 18:34:10 +0100 | [diff] [blame] | 1194 | static int | 
| Peter Zijlstra | 7608dec | 2011-04-05 17:23:46 +0200 | [diff] [blame] | 1195 | select_task_rq_rt(struct task_struct *p, int sd_flag, int flags) | 
| Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 1196 | { | 
| Peter Zijlstra | 7608dec | 2011-04-05 17:23:46 +0200 | [diff] [blame] | 1197 | 	struct task_struct *curr; | 
 | 1198 | 	struct rq *rq; | 
 | 1199 | 	int cpu; | 
 | 1200 |  | 
| Peter Zijlstra | 7608dec | 2011-04-05 17:23:46 +0200 | [diff] [blame] | 1201 | 	cpu = task_cpu(p); | 
| Steven Rostedt | c37495f | 2011-06-16 21:55:22 -0400 | [diff] [blame] | 1202 |  | 
| Mike Galbraith | 76854c7 | 2011-11-22 15:18:24 +0100 | [diff] [blame] | 1203 | 	if (p->rt.nr_cpus_allowed == 1) | 
 | 1204 | 		goto out; | 
 | 1205 |  | 
| Steven Rostedt | c37495f | 2011-06-16 21:55:22 -0400 | [diff] [blame] | 1206 | 	/* For anything but wake ups, just return the task_cpu */ | 
 | 1207 | 	if (sd_flag != SD_BALANCE_WAKE && sd_flag != SD_BALANCE_FORK) | 
 | 1208 | 		goto out; | 
 | 1209 |  | 
| Peter Zijlstra | 7608dec | 2011-04-05 17:23:46 +0200 | [diff] [blame] | 1210 | 	rq = cpu_rq(cpu); | 
 | 1211 |  | 
 | 1212 | 	rcu_read_lock(); | 
 | 1213 | 	curr = ACCESS_ONCE(rq->curr); /* unlocked access */ | 
 | 1214 |  | 
| Gregory Haskins | 318e089 | 2008-01-25 21:08:10 +0100 | [diff] [blame] | 1215 | 	/* | 
| Peter Zijlstra | 7608dec | 2011-04-05 17:23:46 +0200 | [diff] [blame] | 1216 | 	 * If the current task on @p's runqueue is an RT task, then | 
| Steven Rostedt | e1f47d8 | 2008-01-25 21:08:12 +0100 | [diff] [blame] | 1217 | 	 * try to see if we can wake this RT task up on another | 
 | 1218 | 	 * runqueue. Otherwise simply start this RT task | 
 | 1219 | 	 * on its current runqueue. | 
 | 1220 | 	 * | 
| Steven Rostedt | 43fa546 | 2010-09-20 22:40:03 -0400 | [diff] [blame] | 1221 | 	 * We want to avoid overloading runqueues. If the woken | 
 | 1222 | 	 * task is a higher priority, then it will stay on this CPU | 
 | 1223 | 	 * and the lower prio task should be moved to another CPU. | 
 | 1224 | 	 * Even though this will probably make the lower prio task | 
 | 1225 | 	 * lose its cache, we do not want to bounce a higher task | 
 | 1226 | 	 * around just because it gave up its CPU, perhaps for a | 
 | 1227 | 	 * lock? | 
 | 1228 | 	 * | 
 | 1229 | 	 * For equal prio tasks, we just let the scheduler sort it out. | 
| Peter Zijlstra | 7608dec | 2011-04-05 17:23:46 +0200 | [diff] [blame] | 1230 | 	 * | 
| Gregory Haskins | 318e089 | 2008-01-25 21:08:10 +0100 | [diff] [blame] | 1231 | 	 * Otherwise, just let it ride on the affined RQ and the | 
 | 1232 | 	 * post-schedule router will push the preempted task away | 
| Peter Zijlstra | 7608dec | 2011-04-05 17:23:46 +0200 | [diff] [blame] | 1233 | 	 * | 
 | 1234 | 	 * This test is optimistic, if we get it wrong the load-balancer | 
 | 1235 | 	 * will have to sort it out. | 
| Gregory Haskins | 318e089 | 2008-01-25 21:08:10 +0100 | [diff] [blame] | 1236 | 	 */ | 
| Peter Zijlstra | 7608dec | 2011-04-05 17:23:46 +0200 | [diff] [blame] | 1237 | 	if (curr && unlikely(rt_task(curr)) && | 
 | 1238 | 	    (curr->rt.nr_cpus_allowed < 2 || | 
| Shawn Bohrer | 3be209a | 2011-09-12 09:28:04 -0500 | [diff] [blame] | 1239 | 	     curr->prio <= p->prio) && | 
| Peter Zijlstra | 7608dec | 2011-04-05 17:23:46 +0200 | [diff] [blame] | 1240 | 	    (p->rt.nr_cpus_allowed > 1)) { | 
 | 1241 | 		int target = find_lowest_rq(p); | 
 | 1242 |  | 
 | 1243 | 		if (target != -1) | 
 | 1244 | 			cpu = target; | 
 | 1245 | 	} | 
 | 1246 | 	rcu_read_unlock(); | 
 | 1247 |  | 
| Steven Rostedt | c37495f | 2011-06-16 21:55:22 -0400 | [diff] [blame] | 1248 | out: | 
| Peter Zijlstra | 7608dec | 2011-04-05 17:23:46 +0200 | [diff] [blame] | 1249 | 	return cpu; | 
| Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 1250 | } | 
| Dmitry Adamushko | 7ebefa8 | 2008-07-01 23:32:15 +0200 | [diff] [blame] | 1251 |  | 
 | 1252 | static void check_preempt_equal_prio(struct rq *rq, struct task_struct *p) | 
 | 1253 | { | 
| Dmitry Adamushko | 7ebefa8 | 2008-07-01 23:32:15 +0200 | [diff] [blame] | 1254 | 	if (rq->curr->rt.nr_cpus_allowed == 1) | 
 | 1255 | 		return; | 
 | 1256 |  | 
| Rusty Russell | 13b8bd0 | 2009-03-25 15:01:22 +1030 | [diff] [blame] | 1257 | 	if (p->rt.nr_cpus_allowed != 1 | 
 | 1258 | 	    && cpupri_find(&rq->rd->cpupri, p, NULL)) | 
| Dmitry Adamushko | 7ebefa8 | 2008-07-01 23:32:15 +0200 | [diff] [blame] | 1259 | 		return; | 
 | 1260 |  | 
| Rusty Russell | 13b8bd0 | 2009-03-25 15:01:22 +1030 | [diff] [blame] | 1261 | 	if (!cpupri_find(&rq->rd->cpupri, rq->curr, NULL)) | 
 | 1262 | 		return; | 
| Dmitry Adamushko | 7ebefa8 | 2008-07-01 23:32:15 +0200 | [diff] [blame] | 1263 |  | 
 | 1264 | 	/* | 
 | 1265 | 	 * There appears to be other cpus that can accept | 
 | 1266 | 	 * current and none to run 'p', so lets reschedule | 
 | 1267 | 	 * to try and push current away: | 
 | 1268 | 	 */ | 
 | 1269 | 	requeue_task_rt(rq, p, 1); | 
 | 1270 | 	resched_task(rq->curr); | 
 | 1271 | } | 
 | 1272 |  | 
| Gregory Haskins | e7693a3 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 1273 | #endif /* CONFIG_SMP */ | 
 | 1274 |  | 
| Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1275 | /* | 
 | 1276 |  * Preempt the current task with a newly woken task if needed: | 
 | 1277 |  */ | 
| Peter Zijlstra | 7d47872 | 2009-09-14 19:55:44 +0200 | [diff] [blame] | 1278 | static void check_preempt_curr_rt(struct rq *rq, struct task_struct *p, int flags) | 
| Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1279 | { | 
| Gregory Haskins | 45c01e8 | 2008-05-12 21:20:41 +0200 | [diff] [blame] | 1280 | 	if (p->prio < rq->curr->prio) { | 
| Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1281 | 		resched_task(rq->curr); | 
| Gregory Haskins | 45c01e8 | 2008-05-12 21:20:41 +0200 | [diff] [blame] | 1282 | 		return; | 
 | 1283 | 	} | 
 | 1284 |  | 
 | 1285 | #ifdef CONFIG_SMP | 
 | 1286 | 	/* | 
 | 1287 | 	 * If: | 
 | 1288 | 	 * | 
 | 1289 | 	 * - the newly woken task is of equal priority to the current task | 
 | 1290 | 	 * - the newly woken task is non-migratable while current is migratable | 
 | 1291 | 	 * - current will be preempted on the next reschedule | 
 | 1292 | 	 * | 
 | 1293 | 	 * we should check to see if current can readily move to a different | 
 | 1294 | 	 * cpu.  If so, we will reschedule to allow the push logic to try | 
 | 1295 | 	 * to move current somewhere else, making room for our non-migratable | 
 | 1296 | 	 * task. | 
 | 1297 | 	 */ | 
| Hillf Danton | 8dd0de8 | 2011-06-14 18:36:24 -0400 | [diff] [blame] | 1298 | 	if (p->prio == rq->curr->prio && !test_tsk_need_resched(rq->curr)) | 
| Dmitry Adamushko | 7ebefa8 | 2008-07-01 23:32:15 +0200 | [diff] [blame] | 1299 | 		check_preempt_equal_prio(rq, p); | 
| Gregory Haskins | 45c01e8 | 2008-05-12 21:20:41 +0200 | [diff] [blame] | 1300 | #endif | 
| Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1301 | } | 
 | 1302 |  | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1303 | static struct sched_rt_entity *pick_next_rt_entity(struct rq *rq, | 
 | 1304 | 						   struct rt_rq *rt_rq) | 
| Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1305 | { | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1306 | 	struct rt_prio_array *array = &rt_rq->active; | 
 | 1307 | 	struct sched_rt_entity *next = NULL; | 
| Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1308 | 	struct list_head *queue; | 
 | 1309 | 	int idx; | 
 | 1310 |  | 
 | 1311 | 	idx = sched_find_first_bit(array->bitmap); | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1312 | 	BUG_ON(idx >= MAX_RT_PRIO); | 
| Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1313 |  | 
 | 1314 | 	queue = array->queue + idx; | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1315 | 	next = list_entry(queue->next, struct sched_rt_entity, run_list); | 
| Dmitry Adamushko | 326587b | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 1316 |  | 
| Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1317 | 	return next; | 
 | 1318 | } | 
 | 1319 |  | 
| Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1320 | static struct task_struct *_pick_next_task_rt(struct rq *rq) | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1321 | { | 
 | 1322 | 	struct sched_rt_entity *rt_se; | 
 | 1323 | 	struct task_struct *p; | 
 | 1324 | 	struct rt_rq *rt_rq; | 
 | 1325 |  | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1326 | 	rt_rq = &rq->rt; | 
 | 1327 |  | 
| Steven Rostedt | 8e54a2c | 2010-12-06 11:28:30 -0500 | [diff] [blame] | 1328 | 	if (!rt_rq->rt_nr_running) | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1329 | 		return NULL; | 
 | 1330 |  | 
| Peter Zijlstra | 23b0fdf | 2008-02-13 15:45:39 +0100 | [diff] [blame] | 1331 | 	if (rt_rq_throttled(rt_rq)) | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1332 | 		return NULL; | 
 | 1333 |  | 
 | 1334 | 	do { | 
 | 1335 | 		rt_se = pick_next_rt_entity(rq, rt_rq); | 
| Dmitry Adamushko | 326587b | 2008-01-25 21:08:34 +0100 | [diff] [blame] | 1336 | 		BUG_ON(!rt_se); | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1337 | 		rt_rq = group_rt_rq(rt_se); | 
 | 1338 | 	} while (rt_rq); | 
 | 1339 |  | 
 | 1340 | 	p = rt_task_of(rt_se); | 
| Venkatesh Pallipadi | 305e683 | 2010-10-04 17:03:21 -0700 | [diff] [blame] | 1341 | 	p->se.exec_start = rq->clock_task; | 
| Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1342 |  | 
 | 1343 | 	return p; | 
 | 1344 | } | 
 | 1345 |  | 
 | 1346 | static struct task_struct *pick_next_task_rt(struct rq *rq) | 
 | 1347 | { | 
 | 1348 | 	struct task_struct *p = _pick_next_task_rt(rq); | 
 | 1349 |  | 
 | 1350 | 	/* The running task is never eligible for pushing */ | 
 | 1351 | 	if (p) | 
 | 1352 | 		dequeue_pushable_task(rq, p); | 
 | 1353 |  | 
| Ingo Molnar | bcf08df | 2008-04-19 12:11:10 +0200 | [diff] [blame] | 1354 | #ifdef CONFIG_SMP | 
| Gregory Haskins | 3f029d3 | 2009-07-29 11:08:47 -0400 | [diff] [blame] | 1355 | 	/* | 
 | 1356 | 	 * We detect this state here so that we can avoid taking the RQ | 
 | 1357 | 	 * lock again later if there is no need to push | 
 | 1358 | 	 */ | 
 | 1359 | 	rq->post_schedule = has_pushable_tasks(rq); | 
| Ingo Molnar | bcf08df | 2008-04-19 12:11:10 +0200 | [diff] [blame] | 1360 | #endif | 
| Gregory Haskins | 3f029d3 | 2009-07-29 11:08:47 -0400 | [diff] [blame] | 1361 |  | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1362 | 	return p; | 
 | 1363 | } | 
 | 1364 |  | 
| Ingo Molnar | 31ee529 | 2007-08-09 11:16:49 +0200 | [diff] [blame] | 1365 | static void put_prev_task_rt(struct rq *rq, struct task_struct *p) | 
| Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1366 | { | 
| Ingo Molnar | f1e14ef | 2007-08-09 11:16:48 +0200 | [diff] [blame] | 1367 | 	update_curr_rt(rq); | 
| Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1368 |  | 
 | 1369 | 	/* | 
 | 1370 | 	 * The previous task needs to be made eligible for pushing | 
 | 1371 | 	 * if it is still active | 
 | 1372 | 	 */ | 
| Peter Zijlstra | fd2f441 | 2011-04-05 17:23:44 +0200 | [diff] [blame] | 1373 | 	if (on_rt_rq(&p->rt) && p->rt.nr_cpus_allowed > 1) | 
| Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1374 | 		enqueue_pushable_task(rq, p); | 
| Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1375 | } | 
 | 1376 |  | 
| Peter Williams | 681f3e6 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 1377 | #ifdef CONFIG_SMP | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1378 |  | 
| Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1379 | /* Only try algorithms three times */ | 
 | 1380 | #define RT_MAX_TRIES 3 | 
 | 1381 |  | 
| Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1382 | static int pick_rt_task(struct rq *rq, struct task_struct *p, int cpu) | 
 | 1383 | { | 
 | 1384 | 	if (!task_running(rq, p) && | 
| Peter Zijlstra | fa17b50 | 2011-06-16 12:23:22 +0200 | [diff] [blame] | 1385 | 	    (cpu < 0 || cpumask_test_cpu(cpu, tsk_cpus_allowed(p))) && | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1386 | 	    (p->rt.nr_cpus_allowed > 1)) | 
| Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1387 | 		return 1; | 
 | 1388 | 	return 0; | 
 | 1389 | } | 
 | 1390 |  | 
| Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1391 | /* Return the second highest RT task, NULL otherwise */ | 
| Ingo Molnar | 79064fb | 2008-01-25 21:08:14 +0100 | [diff] [blame] | 1392 | static struct task_struct *pick_next_highest_task_rt(struct rq *rq, int cpu) | 
| Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1393 | { | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1394 | 	struct task_struct *next = NULL; | 
 | 1395 | 	struct sched_rt_entity *rt_se; | 
 | 1396 | 	struct rt_prio_array *array; | 
 | 1397 | 	struct rt_rq *rt_rq; | 
| Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1398 | 	int idx; | 
 | 1399 |  | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1400 | 	for_each_leaf_rt_rq(rt_rq, rq) { | 
 | 1401 | 		array = &rt_rq->active; | 
 | 1402 | 		idx = sched_find_first_bit(array->bitmap); | 
| Peter Zijlstra | 4924627 | 2010-10-17 21:46:10 +0200 | [diff] [blame] | 1403 | next_idx: | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1404 | 		if (idx >= MAX_RT_PRIO) | 
 | 1405 | 			continue; | 
 | 1406 | 		if (next && next->prio < idx) | 
 | 1407 | 			continue; | 
 | 1408 | 		list_for_each_entry(rt_se, array->queue + idx, run_list) { | 
| Peter Zijlstra | 3d07467 | 2010-03-10 17:07:24 +0100 | [diff] [blame] | 1409 | 			struct task_struct *p; | 
 | 1410 |  | 
 | 1411 | 			if (!rt_entity_is_task(rt_se)) | 
 | 1412 | 				continue; | 
 | 1413 |  | 
 | 1414 | 			p = rt_task_of(rt_se); | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1415 | 			if (pick_rt_task(rq, p, cpu)) { | 
 | 1416 | 				next = p; | 
 | 1417 | 				break; | 
 | 1418 | 			} | 
 | 1419 | 		} | 
 | 1420 | 		if (!next) { | 
 | 1421 | 			idx = find_next_bit(array->bitmap, MAX_RT_PRIO, idx+1); | 
 | 1422 | 			goto next_idx; | 
 | 1423 | 		} | 
| Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1424 | 	} | 
 | 1425 |  | 
| Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1426 | 	return next; | 
 | 1427 | } | 
 | 1428 |  | 
| Rusty Russell | 0e3900e | 2008-11-25 02:35:13 +1030 | [diff] [blame] | 1429 | static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask); | 
| Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1430 |  | 
| Gregory Haskins | 6e1254d | 2008-01-25 21:08:11 +0100 | [diff] [blame] | 1431 | static int find_lowest_rq(struct task_struct *task) | 
 | 1432 | { | 
 | 1433 | 	struct sched_domain *sd; | 
| Rusty Russell | 96f874e | 2008-11-25 02:35:14 +1030 | [diff] [blame] | 1434 | 	struct cpumask *lowest_mask = __get_cpu_var(local_cpu_mask); | 
| Gregory Haskins | 6e1254d | 2008-01-25 21:08:11 +0100 | [diff] [blame] | 1435 | 	int this_cpu = smp_processor_id(); | 
 | 1436 | 	int cpu      = task_cpu(task); | 
 | 1437 |  | 
| Steven Rostedt | 0da938c | 2011-06-14 18:36:25 -0400 | [diff] [blame] | 1438 | 	/* Make sure the mask is initialized first */ | 
 | 1439 | 	if (unlikely(!lowest_mask)) | 
 | 1440 | 		return -1; | 
 | 1441 |  | 
| Gregory Haskins | 6e0534f | 2008-05-12 21:21:01 +0200 | [diff] [blame] | 1442 | 	if (task->rt.nr_cpus_allowed == 1) | 
 | 1443 | 		return -1; /* No other targets possible */ | 
 | 1444 |  | 
 | 1445 | 	if (!cpupri_find(&task_rq(task)->rd->cpupri, task, lowest_mask)) | 
| Gregory Haskins | 06f90db | 2008-01-25 21:08:13 +0100 | [diff] [blame] | 1446 | 		return -1; /* No targets found */ | 
 | 1447 |  | 
 | 1448 | 	/* | 
| Gregory Haskins | 6e1254d | 2008-01-25 21:08:11 +0100 | [diff] [blame] | 1449 | 	 * At this point we have built a mask of cpus representing the | 
 | 1450 | 	 * lowest priority tasks in the system.  Now we want to elect | 
 | 1451 | 	 * the best one based on our affinity and topology. | 
 | 1452 | 	 * | 
 | 1453 | 	 * We prioritize the last cpu that the task executed on since | 
 | 1454 | 	 * it is most likely cache-hot in that location. | 
 | 1455 | 	 */ | 
| Rusty Russell | 96f874e | 2008-11-25 02:35:14 +1030 | [diff] [blame] | 1456 | 	if (cpumask_test_cpu(cpu, lowest_mask)) | 
| Gregory Haskins | 6e1254d | 2008-01-25 21:08:11 +0100 | [diff] [blame] | 1457 | 		return cpu; | 
 | 1458 |  | 
 | 1459 | 	/* | 
 | 1460 | 	 * Otherwise, we consult the sched_domains span maps to figure | 
 | 1461 | 	 * out which cpu is logically closest to our hot cache data. | 
 | 1462 | 	 */ | 
| Rusty Russell | e2c8806 | 2009-11-03 14:53:15 +1030 | [diff] [blame] | 1463 | 	if (!cpumask_test_cpu(this_cpu, lowest_mask)) | 
 | 1464 | 		this_cpu = -1; /* Skip this_cpu opt if not among lowest */ | 
| Gregory Haskins | 6e1254d | 2008-01-25 21:08:11 +0100 | [diff] [blame] | 1465 |  | 
| Xiaotian Feng | cd4ae6a | 2011-04-22 18:53:54 +0800 | [diff] [blame] | 1466 | 	rcu_read_lock(); | 
| Rusty Russell | e2c8806 | 2009-11-03 14:53:15 +1030 | [diff] [blame] | 1467 | 	for_each_domain(cpu, sd) { | 
 | 1468 | 		if (sd->flags & SD_WAKE_AFFINE) { | 
 | 1469 | 			int best_cpu; | 
| Gregory Haskins | 6e1254d | 2008-01-25 21:08:11 +0100 | [diff] [blame] | 1470 |  | 
| Rusty Russell | e2c8806 | 2009-11-03 14:53:15 +1030 | [diff] [blame] | 1471 | 			/* | 
 | 1472 | 			 * "this_cpu" is cheaper to preempt than a | 
 | 1473 | 			 * remote processor. | 
 | 1474 | 			 */ | 
 | 1475 | 			if (this_cpu != -1 && | 
| Xiaotian Feng | cd4ae6a | 2011-04-22 18:53:54 +0800 | [diff] [blame] | 1476 | 			    cpumask_test_cpu(this_cpu, sched_domain_span(sd))) { | 
 | 1477 | 				rcu_read_unlock(); | 
| Rusty Russell | e2c8806 | 2009-11-03 14:53:15 +1030 | [diff] [blame] | 1478 | 				return this_cpu; | 
| Xiaotian Feng | cd4ae6a | 2011-04-22 18:53:54 +0800 | [diff] [blame] | 1479 | 			} | 
| Gregory Haskins | 6e1254d | 2008-01-25 21:08:11 +0100 | [diff] [blame] | 1480 |  | 
| Rusty Russell | e2c8806 | 2009-11-03 14:53:15 +1030 | [diff] [blame] | 1481 | 			best_cpu = cpumask_first_and(lowest_mask, | 
 | 1482 | 						     sched_domain_span(sd)); | 
| Xiaotian Feng | cd4ae6a | 2011-04-22 18:53:54 +0800 | [diff] [blame] | 1483 | 			if (best_cpu < nr_cpu_ids) { | 
 | 1484 | 				rcu_read_unlock(); | 
| Rusty Russell | e2c8806 | 2009-11-03 14:53:15 +1030 | [diff] [blame] | 1485 | 				return best_cpu; | 
| Xiaotian Feng | cd4ae6a | 2011-04-22 18:53:54 +0800 | [diff] [blame] | 1486 | 			} | 
| Gregory Haskins | 6e1254d | 2008-01-25 21:08:11 +0100 | [diff] [blame] | 1487 | 		} | 
 | 1488 | 	} | 
| Xiaotian Feng | cd4ae6a | 2011-04-22 18:53:54 +0800 | [diff] [blame] | 1489 | 	rcu_read_unlock(); | 
| Gregory Haskins | 6e1254d | 2008-01-25 21:08:11 +0100 | [diff] [blame] | 1490 |  | 
 | 1491 | 	/* | 
 | 1492 | 	 * And finally, if there were no matches within the domains | 
 | 1493 | 	 * just give the caller *something* to work with from the compatible | 
 | 1494 | 	 * locations. | 
 | 1495 | 	 */ | 
| Rusty Russell | e2c8806 | 2009-11-03 14:53:15 +1030 | [diff] [blame] | 1496 | 	if (this_cpu != -1) | 
 | 1497 | 		return this_cpu; | 
 | 1498 |  | 
 | 1499 | 	cpu = cpumask_any(lowest_mask); | 
 | 1500 | 	if (cpu < nr_cpu_ids) | 
 | 1501 | 		return cpu; | 
 | 1502 | 	return -1; | 
| Gregory Haskins | 07b4032 | 2008-01-25 21:08:10 +0100 | [diff] [blame] | 1503 | } | 
 | 1504 |  | 
| Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1505 | /* Will lock the rq it finds */ | 
| Ingo Molnar | 4df64c0 | 2008-01-25 21:08:15 +0100 | [diff] [blame] | 1506 | static struct rq *find_lock_lowest_rq(struct task_struct *task, struct rq *rq) | 
| Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1507 | { | 
 | 1508 | 	struct rq *lowest_rq = NULL; | 
| Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1509 | 	int tries; | 
| Ingo Molnar | 4df64c0 | 2008-01-25 21:08:15 +0100 | [diff] [blame] | 1510 | 	int cpu; | 
| Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1511 |  | 
 | 1512 | 	for (tries = 0; tries < RT_MAX_TRIES; tries++) { | 
| Gregory Haskins | 07b4032 | 2008-01-25 21:08:10 +0100 | [diff] [blame] | 1513 | 		cpu = find_lowest_rq(task); | 
| Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1514 |  | 
| Gregory Haskins | 2de0b46 | 2008-01-25 21:08:10 +0100 | [diff] [blame] | 1515 | 		if ((cpu == -1) || (cpu == rq->cpu)) | 
| Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1516 | 			break; | 
 | 1517 |  | 
| Gregory Haskins | 07b4032 | 2008-01-25 21:08:10 +0100 | [diff] [blame] | 1518 | 		lowest_rq = cpu_rq(cpu); | 
 | 1519 |  | 
| Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1520 | 		/* if the prio of this runqueue changed, try again */ | 
| Gregory Haskins | 07b4032 | 2008-01-25 21:08:10 +0100 | [diff] [blame] | 1521 | 		if (double_lock_balance(rq, lowest_rq)) { | 
| Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1522 | 			/* | 
 | 1523 | 			 * We had to unlock the run queue. In | 
 | 1524 | 			 * the mean time, task could have | 
 | 1525 | 			 * migrated already or had its affinity changed. | 
 | 1526 | 			 * Also make sure that it wasn't scheduled on its rq. | 
 | 1527 | 			 */ | 
| Gregory Haskins | 07b4032 | 2008-01-25 21:08:10 +0100 | [diff] [blame] | 1528 | 			if (unlikely(task_rq(task) != rq || | 
| Rusty Russell | 96f874e | 2008-11-25 02:35:14 +1030 | [diff] [blame] | 1529 | 				     !cpumask_test_cpu(lowest_rq->cpu, | 
| Peter Zijlstra | fa17b50 | 2011-06-16 12:23:22 +0200 | [diff] [blame] | 1530 | 						       tsk_cpus_allowed(task)) || | 
| Gregory Haskins | 07b4032 | 2008-01-25 21:08:10 +0100 | [diff] [blame] | 1531 | 				     task_running(rq, task) || | 
| Peter Zijlstra | fd2f441 | 2011-04-05 17:23:44 +0200 | [diff] [blame] | 1532 | 				     !task->on_rq)) { | 
| Ingo Molnar | 4df64c0 | 2008-01-25 21:08:15 +0100 | [diff] [blame] | 1533 |  | 
| Thomas Gleixner | 05fa785 | 2009-11-17 14:28:38 +0100 | [diff] [blame] | 1534 | 				raw_spin_unlock(&lowest_rq->lock); | 
| Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1535 | 				lowest_rq = NULL; | 
 | 1536 | 				break; | 
 | 1537 | 			} | 
 | 1538 | 		} | 
 | 1539 |  | 
 | 1540 | 		/* If this rq is still suitable use it. */ | 
| Gregory Haskins | e864c49 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 1541 | 		if (lowest_rq->rt.highest_prio.curr > task->prio) | 
| Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1542 | 			break; | 
 | 1543 |  | 
 | 1544 | 		/* try again */ | 
| Peter Zijlstra | 1b12bbc | 2008-08-11 09:30:22 +0200 | [diff] [blame] | 1545 | 		double_unlock_balance(rq, lowest_rq); | 
| Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1546 | 		lowest_rq = NULL; | 
 | 1547 | 	} | 
 | 1548 |  | 
 | 1549 | 	return lowest_rq; | 
 | 1550 | } | 
 | 1551 |  | 
| Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1552 | static struct task_struct *pick_next_pushable_task(struct rq *rq) | 
 | 1553 | { | 
 | 1554 | 	struct task_struct *p; | 
 | 1555 |  | 
 | 1556 | 	if (!has_pushable_tasks(rq)) | 
 | 1557 | 		return NULL; | 
 | 1558 |  | 
 | 1559 | 	p = plist_first_entry(&rq->rt.pushable_tasks, | 
 | 1560 | 			      struct task_struct, pushable_tasks); | 
 | 1561 |  | 
 | 1562 | 	BUG_ON(rq->cpu != task_cpu(p)); | 
 | 1563 | 	BUG_ON(task_current(rq, p)); | 
 | 1564 | 	BUG_ON(p->rt.nr_cpus_allowed <= 1); | 
 | 1565 |  | 
| Peter Zijlstra | fd2f441 | 2011-04-05 17:23:44 +0200 | [diff] [blame] | 1566 | 	BUG_ON(!p->on_rq); | 
| Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1567 | 	BUG_ON(!rt_task(p)); | 
 | 1568 |  | 
 | 1569 | 	return p; | 
 | 1570 | } | 
 | 1571 |  | 
| Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1572 | /* | 
 | 1573 |  * If the current CPU has more than one RT task, see if the non | 
 | 1574 |  * running task can migrate over to a CPU that is running a task | 
 | 1575 |  * of lesser priority. | 
 | 1576 |  */ | 
| Gregory Haskins | 697f0a4 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 1577 | static int push_rt_task(struct rq *rq) | 
| Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1578 | { | 
 | 1579 | 	struct task_struct *next_task; | 
 | 1580 | 	struct rq *lowest_rq; | 
| Hillf Danton | 311e800 | 2011-06-16 21:55:20 -0400 | [diff] [blame] | 1581 | 	int ret = 0; | 
| Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1582 |  | 
| Gregory Haskins | a22d7fc | 2008-01-25 21:08:12 +0100 | [diff] [blame] | 1583 | 	if (!rq->rt.overloaded) | 
 | 1584 | 		return 0; | 
 | 1585 |  | 
| Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1586 | 	next_task = pick_next_pushable_task(rq); | 
| Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1587 | 	if (!next_task) | 
 | 1588 | 		return 0; | 
 | 1589 |  | 
| Chanho Min | cb297a3 | 2012-01-05 20:00:19 +0900 | [diff] [blame^] | 1590 | #ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW | 
 | 1591 |        if (unlikely(task_running(rq, next_task))) | 
 | 1592 |                return 0; | 
 | 1593 | #endif | 
 | 1594 |  | 
| Peter Zijlstra | 4924627 | 2010-10-17 21:46:10 +0200 | [diff] [blame] | 1595 | retry: | 
| Gregory Haskins | 697f0a4 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 1596 | 	if (unlikely(next_task == rq->curr)) { | 
| Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1597 | 		WARN_ON(1); | 
| Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1598 | 		return 0; | 
| Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1599 | 	} | 
| Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1600 |  | 
 | 1601 | 	/* | 
 | 1602 | 	 * It's possible that the next_task slipped in of | 
 | 1603 | 	 * higher priority than current. If that's the case | 
 | 1604 | 	 * just reschedule current. | 
 | 1605 | 	 */ | 
| Gregory Haskins | 697f0a4 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 1606 | 	if (unlikely(next_task->prio < rq->curr->prio)) { | 
 | 1607 | 		resched_task(rq->curr); | 
| Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1608 | 		return 0; | 
 | 1609 | 	} | 
 | 1610 |  | 
| Gregory Haskins | 697f0a4 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 1611 | 	/* We might release rq lock */ | 
| Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1612 | 	get_task_struct(next_task); | 
 | 1613 |  | 
 | 1614 | 	/* find_lock_lowest_rq locks the rq if found */ | 
| Gregory Haskins | 697f0a4 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 1615 | 	lowest_rq = find_lock_lowest_rq(next_task, rq); | 
| Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1616 | 	if (!lowest_rq) { | 
 | 1617 | 		struct task_struct *task; | 
 | 1618 | 		/* | 
| Hillf Danton | 311e800 | 2011-06-16 21:55:20 -0400 | [diff] [blame] | 1619 | 		 * find_lock_lowest_rq releases rq->lock | 
| Gregory Haskins | 1563513 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1620 | 		 * so it is possible that next_task has migrated. | 
 | 1621 | 		 * | 
 | 1622 | 		 * We need to make sure that the task is still on the same | 
 | 1623 | 		 * run-queue and is also still the next task eligible for | 
 | 1624 | 		 * pushing. | 
| Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1625 | 		 */ | 
| Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1626 | 		task = pick_next_pushable_task(rq); | 
| Gregory Haskins | 1563513 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1627 | 		if (task_cpu(next_task) == rq->cpu && task == next_task) { | 
 | 1628 | 			/* | 
| Hillf Danton | 311e800 | 2011-06-16 21:55:20 -0400 | [diff] [blame] | 1629 | 			 * The task hasn't migrated, and is still the next | 
 | 1630 | 			 * eligible task, but we failed to find a run-queue | 
 | 1631 | 			 * to push it to.  Do not retry in this case, since | 
 | 1632 | 			 * other cpus will pull from us when ready. | 
| Gregory Haskins | 1563513 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1633 | 			 */ | 
| Gregory Haskins | 1563513 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1634 | 			goto out; | 
| Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1635 | 		} | 
| Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1636 |  | 
| Gregory Haskins | 1563513 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1637 | 		if (!task) | 
 | 1638 | 			/* No more tasks, just exit */ | 
 | 1639 | 			goto out; | 
 | 1640 |  | 
| Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1641 | 		/* | 
| Gregory Haskins | 1563513 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1642 | 		 * Something has shifted, try again. | 
| Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1643 | 		 */ | 
| Gregory Haskins | 1563513 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1644 | 		put_task_struct(next_task); | 
 | 1645 | 		next_task = task; | 
 | 1646 | 		goto retry; | 
| Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1647 | 	} | 
 | 1648 |  | 
| Gregory Haskins | 697f0a4 | 2008-01-25 21:08:09 +0100 | [diff] [blame] | 1649 | 	deactivate_task(rq, next_task, 0); | 
| Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1650 | 	set_task_cpu(next_task, lowest_rq->cpu); | 
 | 1651 | 	activate_task(lowest_rq, next_task, 0); | 
| Hillf Danton | 311e800 | 2011-06-16 21:55:20 -0400 | [diff] [blame] | 1652 | 	ret = 1; | 
| Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1653 |  | 
 | 1654 | 	resched_task(lowest_rq->curr); | 
 | 1655 |  | 
| Peter Zijlstra | 1b12bbc | 2008-08-11 09:30:22 +0200 | [diff] [blame] | 1656 | 	double_unlock_balance(rq, lowest_rq); | 
| Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1657 |  | 
| Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1658 | out: | 
 | 1659 | 	put_task_struct(next_task); | 
 | 1660 |  | 
| Hillf Danton | 311e800 | 2011-06-16 21:55:20 -0400 | [diff] [blame] | 1661 | 	return ret; | 
| Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1662 | } | 
 | 1663 |  | 
| Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1664 | static void push_rt_tasks(struct rq *rq) | 
 | 1665 | { | 
 | 1666 | 	/* push_rt_task will return true if it moved an RT */ | 
 | 1667 | 	while (push_rt_task(rq)) | 
 | 1668 | 		; | 
 | 1669 | } | 
 | 1670 |  | 
| Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1671 | static int pull_rt_task(struct rq *this_rq) | 
 | 1672 | { | 
| Ingo Molnar | 80bf317 | 2008-01-25 21:08:17 +0100 | [diff] [blame] | 1673 | 	int this_cpu = this_rq->cpu, ret = 0, cpu; | 
| Gregory Haskins | a872894 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 1674 | 	struct task_struct *p; | 
| Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1675 | 	struct rq *src_rq; | 
| Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1676 |  | 
| Gregory Haskins | 637f508 | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 1677 | 	if (likely(!rt_overloaded(this_rq))) | 
| Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1678 | 		return 0; | 
 | 1679 |  | 
| Rusty Russell | c6c4927 | 2008-11-25 02:35:05 +1030 | [diff] [blame] | 1680 | 	for_each_cpu(cpu, this_rq->rd->rto_mask) { | 
| Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1681 | 		if (this_cpu == cpu) | 
 | 1682 | 			continue; | 
 | 1683 |  | 
 | 1684 | 		src_rq = cpu_rq(cpu); | 
| Gregory Haskins | 74ab8e4 | 2008-12-29 09:39:50 -0500 | [diff] [blame] | 1685 |  | 
 | 1686 | 		/* | 
 | 1687 | 		 * Don't bother taking the src_rq->lock if the next highest | 
 | 1688 | 		 * task is known to be lower-priority than our current task. | 
 | 1689 | 		 * This may look racy, but if this value is about to go | 
 | 1690 | 		 * logically higher, the src_rq will push this task away. | 
 | 1691 | 		 * And if its going logically lower, we do not care | 
 | 1692 | 		 */ | 
 | 1693 | 		if (src_rq->rt.highest_prio.next >= | 
 | 1694 | 		    this_rq->rt.highest_prio.curr) | 
 | 1695 | 			continue; | 
 | 1696 |  | 
| Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1697 | 		/* | 
 | 1698 | 		 * We can potentially drop this_rq's lock in | 
 | 1699 | 		 * double_lock_balance, and another CPU could | 
| Gregory Haskins | a872894 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 1700 | 		 * alter this_rq | 
| Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1701 | 		 */ | 
| Gregory Haskins | a872894 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 1702 | 		double_lock_balance(this_rq, src_rq); | 
| Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1703 |  | 
 | 1704 | 		/* | 
 | 1705 | 		 * Are there still pullable RT tasks? | 
 | 1706 | 		 */ | 
| Mike Galbraith | 614ee1f | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1707 | 		if (src_rq->rt.rt_nr_running <= 1) | 
 | 1708 | 			goto skip; | 
| Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1709 |  | 
| Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1710 | 		p = pick_next_highest_task_rt(src_rq, this_cpu); | 
 | 1711 |  | 
 | 1712 | 		/* | 
 | 1713 | 		 * Do we have an RT task that preempts | 
 | 1714 | 		 * the to-be-scheduled task? | 
 | 1715 | 		 */ | 
| Gregory Haskins | a872894 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 1716 | 		if (p && (p->prio < this_rq->rt.highest_prio.curr)) { | 
| Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1717 | 			WARN_ON(p == src_rq->curr); | 
| Peter Zijlstra | fd2f441 | 2011-04-05 17:23:44 +0200 | [diff] [blame] | 1718 | 			WARN_ON(!p->on_rq); | 
| Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1719 |  | 
 | 1720 | 			/* | 
 | 1721 | 			 * There's a chance that p is higher in priority | 
 | 1722 | 			 * than what's currently running on its cpu. | 
 | 1723 | 			 * This is just that p is wakeing up and hasn't | 
 | 1724 | 			 * had a chance to schedule. We only pull | 
 | 1725 | 			 * p if it is lower in priority than the | 
| Gregory Haskins | a872894 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 1726 | 			 * current task on the run queue | 
| Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1727 | 			 */ | 
| Gregory Haskins | a872894 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 1728 | 			if (p->prio < src_rq->curr->prio) | 
| Mike Galbraith | 614ee1f | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1729 | 				goto skip; | 
| Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1730 |  | 
 | 1731 | 			ret = 1; | 
 | 1732 |  | 
 | 1733 | 			deactivate_task(src_rq, p, 0); | 
 | 1734 | 			set_task_cpu(p, this_cpu); | 
 | 1735 | 			activate_task(this_rq, p, 0); | 
 | 1736 | 			/* | 
 | 1737 | 			 * We continue with the search, just in | 
 | 1738 | 			 * case there's an even higher prio task | 
| Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1739 | 			 * in another runqueue. (low likelihood | 
| Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1740 | 			 * but possible) | 
| Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1741 | 			 */ | 
| Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1742 | 		} | 
| Peter Zijlstra | 4924627 | 2010-10-17 21:46:10 +0200 | [diff] [blame] | 1743 | skip: | 
| Peter Zijlstra | 1b12bbc | 2008-08-11 09:30:22 +0200 | [diff] [blame] | 1744 | 		double_unlock_balance(this_rq, src_rq); | 
| Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1745 | 	} | 
 | 1746 |  | 
 | 1747 | 	return ret; | 
 | 1748 | } | 
 | 1749 |  | 
| Steven Rostedt | 9a897c5 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 1750 | static void pre_schedule_rt(struct rq *rq, struct task_struct *prev) | 
| Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1751 | { | 
 | 1752 | 	/* Try to pull RT tasks here if we lower this rq's prio */ | 
| Yong Zhang | 33c3d6c | 2010-02-09 14:43:59 -0500 | [diff] [blame] | 1753 | 	if (rq->rt.highest_prio.curr > prev->prio) | 
| Steven Rostedt | f65eda4 | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1754 | 		pull_rt_task(rq); | 
 | 1755 | } | 
 | 1756 |  | 
| Steven Rostedt | 9a897c5 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 1757 | static void post_schedule_rt(struct rq *rq) | 
| Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1758 | { | 
| Gregory Haskins | 967fc04 | 2008-12-29 09:39:52 -0500 | [diff] [blame] | 1759 | 	push_rt_tasks(rq); | 
| Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1760 | } | 
 | 1761 |  | 
| Gregory Haskins | 8ae121a | 2008-04-23 07:13:29 -0400 | [diff] [blame] | 1762 | /* | 
 | 1763 |  * If we are not running and we are not going to reschedule soon, we should | 
 | 1764 |  * try to push tasks away now | 
 | 1765 |  */ | 
| Peter Zijlstra | efbbd05 | 2009-12-16 18:04:40 +0100 | [diff] [blame] | 1766 | static void task_woken_rt(struct rq *rq, struct task_struct *p) | 
| Steven Rostedt | 4642daf | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1767 | { | 
| Steven Rostedt | 9a897c5 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 1768 | 	if (!task_running(rq, p) && | 
| Gregory Haskins | 8ae121a | 2008-04-23 07:13:29 -0400 | [diff] [blame] | 1769 | 	    !test_tsk_need_resched(rq->curr) && | 
| Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1770 | 	    has_pushable_tasks(rq) && | 
| Steven Rostedt | b3bc211 | 2010-09-20 22:40:04 -0400 | [diff] [blame] | 1771 | 	    p->rt.nr_cpus_allowed > 1 && | 
| Steven Rostedt | 43fa546 | 2010-09-20 22:40:03 -0400 | [diff] [blame] | 1772 | 	    rt_task(rq->curr) && | 
| Steven Rostedt | b3bc211 | 2010-09-20 22:40:04 -0400 | [diff] [blame] | 1773 | 	    (rq->curr->rt.nr_cpus_allowed < 2 || | 
| Shawn Bohrer | 3be209a | 2011-09-12 09:28:04 -0500 | [diff] [blame] | 1774 | 	     rq->curr->prio <= p->prio)) | 
| Steven Rostedt | 4642daf | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1775 | 		push_rt_tasks(rq); | 
 | 1776 | } | 
 | 1777 |  | 
| Mike Travis | cd8ba7c | 2008-03-26 14:23:49 -0700 | [diff] [blame] | 1778 | static void set_cpus_allowed_rt(struct task_struct *p, | 
| Rusty Russell | 96f874e | 2008-11-25 02:35:14 +1030 | [diff] [blame] | 1779 | 				const struct cpumask *new_mask) | 
| Gregory Haskins | 73fe6aa | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1780 | { | 
| Rusty Russell | 96f874e | 2008-11-25 02:35:14 +1030 | [diff] [blame] | 1781 | 	int weight = cpumask_weight(new_mask); | 
| Gregory Haskins | 73fe6aa | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1782 |  | 
 | 1783 | 	BUG_ON(!rt_task(p)); | 
 | 1784 |  | 
 | 1785 | 	/* | 
 | 1786 | 	 * Update the migration status of the RQ if we have an RT task | 
 | 1787 | 	 * which is running AND changing its weight value. | 
 | 1788 | 	 */ | 
| Peter Zijlstra | fd2f441 | 2011-04-05 17:23:44 +0200 | [diff] [blame] | 1789 | 	if (p->on_rq && (weight != p->rt.nr_cpus_allowed)) { | 
| Gregory Haskins | 73fe6aa | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1790 | 		struct rq *rq = task_rq(p); | 
 | 1791 |  | 
| Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1792 | 		if (!task_current(rq, p)) { | 
 | 1793 | 			/* | 
 | 1794 | 			 * Make sure we dequeue this task from the pushable list | 
 | 1795 | 			 * before going further.  It will either remain off of | 
 | 1796 | 			 * the list because we are no longer pushable, or it | 
 | 1797 | 			 * will be requeued. | 
 | 1798 | 			 */ | 
 | 1799 | 			if (p->rt.nr_cpus_allowed > 1) | 
 | 1800 | 				dequeue_pushable_task(rq, p); | 
 | 1801 |  | 
 | 1802 | 			/* | 
 | 1803 | 			 * Requeue if our weight is changing and still > 1 | 
 | 1804 | 			 */ | 
 | 1805 | 			if (weight > 1) | 
 | 1806 | 				enqueue_pushable_task(rq, p); | 
 | 1807 |  | 
 | 1808 | 		} | 
 | 1809 |  | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1810 | 		if ((p->rt.nr_cpus_allowed <= 1) && (weight > 1)) { | 
| Gregory Haskins | 73fe6aa | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1811 | 			rq->rt.rt_nr_migratory++; | 
| Peter Zijlstra | 6f505b1 | 2008-01-25 21:08:30 +0100 | [diff] [blame] | 1812 | 		} else if ((p->rt.nr_cpus_allowed > 1) && (weight <= 1)) { | 
| Gregory Haskins | 73fe6aa | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1813 | 			BUG_ON(!rq->rt.rt_nr_migratory); | 
 | 1814 | 			rq->rt.rt_nr_migratory--; | 
 | 1815 | 		} | 
 | 1816 |  | 
| Gregory Haskins | 398a153 | 2009-01-14 09:10:04 -0500 | [diff] [blame] | 1817 | 		update_rt_migration(&rq->rt); | 
| Gregory Haskins | 73fe6aa | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1818 | 	} | 
| Gregory Haskins | 73fe6aa | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 1819 | } | 
| Ingo Molnar | deeeccd | 2008-01-25 21:08:15 +0100 | [diff] [blame] | 1820 |  | 
| Ingo Molnar | bdd7c81 | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 1821 | /* Assumes rq->lock is held */ | 
| Gregory Haskins | 1f11eb6 | 2008-06-04 15:04:05 -0400 | [diff] [blame] | 1822 | static void rq_online_rt(struct rq *rq) | 
| Ingo Molnar | bdd7c81 | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 1823 | { | 
 | 1824 | 	if (rq->rt.overloaded) | 
 | 1825 | 		rt_set_overload(rq); | 
| Gregory Haskins | 6e0534f | 2008-05-12 21:21:01 +0200 | [diff] [blame] | 1826 |  | 
| Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 1827 | 	__enable_runtime(rq); | 
 | 1828 |  | 
| Gregory Haskins | e864c49 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 1829 | 	cpupri_set(&rq->rd->cpupri, rq->cpu, rq->rt.highest_prio.curr); | 
| Ingo Molnar | bdd7c81 | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 1830 | } | 
 | 1831 |  | 
 | 1832 | /* Assumes rq->lock is held */ | 
| Gregory Haskins | 1f11eb6 | 2008-06-04 15:04:05 -0400 | [diff] [blame] | 1833 | static void rq_offline_rt(struct rq *rq) | 
| Ingo Molnar | bdd7c81 | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 1834 | { | 
 | 1835 | 	if (rq->rt.overloaded) | 
 | 1836 | 		rt_clear_overload(rq); | 
| Gregory Haskins | 6e0534f | 2008-05-12 21:21:01 +0200 | [diff] [blame] | 1837 |  | 
| Peter Zijlstra | 7def2be | 2008-06-05 14:49:58 +0200 | [diff] [blame] | 1838 | 	__disable_runtime(rq); | 
 | 1839 |  | 
| Gregory Haskins | 6e0534f | 2008-05-12 21:21:01 +0200 | [diff] [blame] | 1840 | 	cpupri_set(&rq->rd->cpupri, rq->cpu, CPUPRI_INVALID); | 
| Ingo Molnar | bdd7c81 | 2008-01-25 21:08:18 +0100 | [diff] [blame] | 1841 | } | 
| Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 1842 |  | 
 | 1843 | /* | 
 | 1844 |  * When switch from the rt queue, we bring ourselves to a position | 
 | 1845 |  * that we might want to pull RT tasks from other runqueues. | 
 | 1846 |  */ | 
| Peter Zijlstra | da7a735 | 2011-01-17 17:03:27 +0100 | [diff] [blame] | 1847 | static void switched_from_rt(struct rq *rq, struct task_struct *p) | 
| Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 1848 | { | 
 | 1849 | 	/* | 
 | 1850 | 	 * If there are other RT tasks then we will reschedule | 
 | 1851 | 	 * and the scheduling of the other RT tasks will handle | 
 | 1852 | 	 * the balancing. But if we are the last RT task | 
 | 1853 | 	 * we may need to handle the pulling of RT tasks | 
 | 1854 | 	 * now. | 
 | 1855 | 	 */ | 
| Peter Zijlstra | fd2f441 | 2011-04-05 17:23:44 +0200 | [diff] [blame] | 1856 | 	if (p->on_rq && !rq->rt.rt_nr_running) | 
| Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 1857 | 		pull_rt_task(rq); | 
 | 1858 | } | 
| Rusty Russell | 3d8cbdf | 2008-11-25 09:58:41 +1030 | [diff] [blame] | 1859 |  | 
| Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1860 | void init_sched_rt_class(void) | 
| Rusty Russell | 3d8cbdf | 2008-11-25 09:58:41 +1030 | [diff] [blame] | 1861 | { | 
 | 1862 | 	unsigned int i; | 
 | 1863 |  | 
| Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1864 | 	for_each_possible_cpu(i) { | 
| Yinghai Lu | eaa9584 | 2009-06-06 14:51:36 -0700 | [diff] [blame] | 1865 | 		zalloc_cpumask_var_node(&per_cpu(local_cpu_mask, i), | 
| Mike Travis | 6ca09df | 2008-12-31 18:08:45 -0800 | [diff] [blame] | 1866 | 					GFP_KERNEL, cpu_to_node(i)); | 
| Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 1867 | 	} | 
| Rusty Russell | 3d8cbdf | 2008-11-25 09:58:41 +1030 | [diff] [blame] | 1868 | } | 
| Steven Rostedt | e8fa136 | 2008-01-25 21:08:05 +0100 | [diff] [blame] | 1869 | #endif /* CONFIG_SMP */ | 
| Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1870 |  | 
| Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 1871 | /* | 
 | 1872 |  * When switching a task to RT, we may overload the runqueue | 
 | 1873 |  * with RT tasks. In this case we try to push them off to | 
 | 1874 |  * other runqueues. | 
 | 1875 |  */ | 
| Peter Zijlstra | da7a735 | 2011-01-17 17:03:27 +0100 | [diff] [blame] | 1876 | static void switched_to_rt(struct rq *rq, struct task_struct *p) | 
| Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 1877 | { | 
 | 1878 | 	int check_resched = 1; | 
 | 1879 |  | 
 | 1880 | 	/* | 
 | 1881 | 	 * If we are already running, then there's nothing | 
 | 1882 | 	 * that needs to be done. But if we are not running | 
 | 1883 | 	 * we may need to preempt the current running task. | 
 | 1884 | 	 * If that current running task is also an RT task | 
 | 1885 | 	 * then see if we can move to another run queue. | 
 | 1886 | 	 */ | 
| Peter Zijlstra | fd2f441 | 2011-04-05 17:23:44 +0200 | [diff] [blame] | 1887 | 	if (p->on_rq && rq->curr != p) { | 
| Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 1888 | #ifdef CONFIG_SMP | 
 | 1889 | 		if (rq->rt.overloaded && push_rt_task(rq) && | 
 | 1890 | 		    /* Don't resched if we changed runqueues */ | 
 | 1891 | 		    rq != task_rq(p)) | 
 | 1892 | 			check_resched = 0; | 
 | 1893 | #endif /* CONFIG_SMP */ | 
 | 1894 | 		if (check_resched && p->prio < rq->curr->prio) | 
 | 1895 | 			resched_task(rq->curr); | 
 | 1896 | 	} | 
 | 1897 | } | 
 | 1898 |  | 
 | 1899 | /* | 
 | 1900 |  * Priority of the task has changed. This may cause | 
 | 1901 |  * us to initiate a push or pull. | 
 | 1902 |  */ | 
| Peter Zijlstra | da7a735 | 2011-01-17 17:03:27 +0100 | [diff] [blame] | 1903 | static void | 
 | 1904 | prio_changed_rt(struct rq *rq, struct task_struct *p, int oldprio) | 
| Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 1905 | { | 
| Peter Zijlstra | fd2f441 | 2011-04-05 17:23:44 +0200 | [diff] [blame] | 1906 | 	if (!p->on_rq) | 
| Peter Zijlstra | da7a735 | 2011-01-17 17:03:27 +0100 | [diff] [blame] | 1907 | 		return; | 
 | 1908 |  | 
 | 1909 | 	if (rq->curr == p) { | 
| Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 1910 | #ifdef CONFIG_SMP | 
 | 1911 | 		/* | 
 | 1912 | 		 * If our priority decreases while running, we | 
 | 1913 | 		 * may need to pull tasks to this runqueue. | 
 | 1914 | 		 */ | 
 | 1915 | 		if (oldprio < p->prio) | 
 | 1916 | 			pull_rt_task(rq); | 
 | 1917 | 		/* | 
 | 1918 | 		 * If there's a higher priority task waiting to run | 
| Steven Rostedt | 6fa46fa | 2008-03-05 10:00:12 -0500 | [diff] [blame] | 1919 | 		 * then reschedule. Note, the above pull_rt_task | 
 | 1920 | 		 * can release the rq lock and p could migrate. | 
 | 1921 | 		 * Only reschedule if p is still on the same runqueue. | 
| Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 1922 | 		 */ | 
| Gregory Haskins | e864c49 | 2008-12-29 09:39:49 -0500 | [diff] [blame] | 1923 | 		if (p->prio > rq->rt.highest_prio.curr && rq->curr == p) | 
| Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 1924 | 			resched_task(p); | 
 | 1925 | #else | 
 | 1926 | 		/* For UP simply resched on drop of prio */ | 
 | 1927 | 		if (oldprio < p->prio) | 
 | 1928 | 			resched_task(p); | 
 | 1929 | #endif /* CONFIG_SMP */ | 
 | 1930 | 	} else { | 
 | 1931 | 		/* | 
 | 1932 | 		 * This task is not running, but if it is | 
 | 1933 | 		 * greater than the current running task | 
 | 1934 | 		 * then reschedule. | 
 | 1935 | 		 */ | 
 | 1936 | 		if (p->prio < rq->curr->prio) | 
 | 1937 | 			resched_task(rq->curr); | 
 | 1938 | 	} | 
 | 1939 | } | 
 | 1940 |  | 
| Peter Zijlstra | 78f2c7d | 2008-01-25 21:08:27 +0100 | [diff] [blame] | 1941 | static void watchdog(struct rq *rq, struct task_struct *p) | 
 | 1942 | { | 
 | 1943 | 	unsigned long soft, hard; | 
 | 1944 |  | 
| Jiri Slaby | 78d7d40 | 2010-03-05 13:42:54 -0800 | [diff] [blame] | 1945 | 	/* max may change after cur was read, this will be fixed next tick */ | 
 | 1946 | 	soft = task_rlimit(p, RLIMIT_RTTIME); | 
 | 1947 | 	hard = task_rlimit_max(p, RLIMIT_RTTIME); | 
| Peter Zijlstra | 78f2c7d | 2008-01-25 21:08:27 +0100 | [diff] [blame] | 1948 |  | 
 | 1949 | 	if (soft != RLIM_INFINITY) { | 
 | 1950 | 		unsigned long next; | 
 | 1951 |  | 
 | 1952 | 		p->rt.timeout++; | 
 | 1953 | 		next = DIV_ROUND_UP(min(soft, hard), USEC_PER_SEC/HZ); | 
| Peter Zijlstra | 5a52dd5 | 2008-01-25 21:08:32 +0100 | [diff] [blame] | 1954 | 		if (p->rt.timeout > next) | 
| Frank Mayhar | f06febc | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 1955 | 			p->cputime_expires.sched_exp = p->se.sum_exec_runtime; | 
| Peter Zijlstra | 78f2c7d | 2008-01-25 21:08:27 +0100 | [diff] [blame] | 1956 | 	} | 
 | 1957 | } | 
| Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 1958 |  | 
| Peter Zijlstra | 8f4d37e | 2008-01-25 21:08:29 +0100 | [diff] [blame] | 1959 | static void task_tick_rt(struct rq *rq, struct task_struct *p, int queued) | 
| Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1960 | { | 
| Peter Zijlstra | 67e2be0 | 2007-12-20 15:01:17 +0100 | [diff] [blame] | 1961 | 	update_curr_rt(rq); | 
 | 1962 |  | 
| Peter Zijlstra | 78f2c7d | 2008-01-25 21:08:27 +0100 | [diff] [blame] | 1963 | 	watchdog(rq, p); | 
 | 1964 |  | 
| Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1965 | 	/* | 
 | 1966 | 	 * RR tasks need a special form of timeslice management. | 
 | 1967 | 	 * FIFO tasks have no timeslices. | 
 | 1968 | 	 */ | 
 | 1969 | 	if (p->policy != SCHED_RR) | 
 | 1970 | 		return; | 
 | 1971 |  | 
| Peter Zijlstra | fa71706 | 2008-01-25 21:08:27 +0100 | [diff] [blame] | 1972 | 	if (--p->rt.time_slice) | 
| Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1973 | 		return; | 
 | 1974 |  | 
| Peter Zijlstra | fa71706 | 2008-01-25 21:08:27 +0100 | [diff] [blame] | 1975 | 	p->rt.time_slice = DEF_TIMESLICE; | 
| Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1976 |  | 
| Dmitry Adamushko | 98fbc79 | 2007-08-24 20:39:10 +0200 | [diff] [blame] | 1977 | 	/* | 
 | 1978 | 	 * Requeue to the end of queue if we are not the only element | 
 | 1979 | 	 * on the queue: | 
 | 1980 | 	 */ | 
| Peter Zijlstra | fa71706 | 2008-01-25 21:08:27 +0100 | [diff] [blame] | 1981 | 	if (p->rt.run_list.prev != p->rt.run_list.next) { | 
| Dmitry Adamushko | 7ebefa8 | 2008-07-01 23:32:15 +0200 | [diff] [blame] | 1982 | 		requeue_task_rt(rq, p, 0); | 
| Dmitry Adamushko | 98fbc79 | 2007-08-24 20:39:10 +0200 | [diff] [blame] | 1983 | 		set_tsk_need_resched(p); | 
 | 1984 | 	} | 
| Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 1985 | } | 
 | 1986 |  | 
| Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 1987 | static void set_curr_task_rt(struct rq *rq) | 
 | 1988 | { | 
 | 1989 | 	struct task_struct *p = rq->curr; | 
 | 1990 |  | 
| Venkatesh Pallipadi | 305e683 | 2010-10-04 17:03:21 -0700 | [diff] [blame] | 1991 | 	p->se.exec_start = rq->clock_task; | 
| Gregory Haskins | 917b627 | 2008-12-29 09:39:53 -0500 | [diff] [blame] | 1992 |  | 
 | 1993 | 	/* The running task is never eligible for pushing */ | 
 | 1994 | 	dequeue_pushable_task(rq, p); | 
| Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 1995 | } | 
 | 1996 |  | 
| H Hartley Sweeten | 6d686f4 | 2010-01-13 20:21:52 -0700 | [diff] [blame] | 1997 | static unsigned int get_rr_interval_rt(struct rq *rq, struct task_struct *task) | 
| Peter Williams | 0d721ce | 2009-09-21 01:31:53 +0000 | [diff] [blame] | 1998 | { | 
 | 1999 | 	/* | 
 | 2000 | 	 * Time slice is 0 for SCHED_FIFO tasks | 
 | 2001 | 	 */ | 
 | 2002 | 	if (task->policy == SCHED_RR) | 
 | 2003 | 		return DEF_TIMESLICE; | 
 | 2004 | 	else | 
 | 2005 | 		return 0; | 
 | 2006 | } | 
 | 2007 |  | 
| Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 2008 | const struct sched_class rt_sched_class = { | 
| Ingo Molnar | 5522d5d | 2007-10-15 17:00:12 +0200 | [diff] [blame] | 2009 | 	.next			= &fair_sched_class, | 
| Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2010 | 	.enqueue_task		= enqueue_task_rt, | 
 | 2011 | 	.dequeue_task		= dequeue_task_rt, | 
 | 2012 | 	.yield_task		= yield_task_rt, | 
 | 2013 |  | 
 | 2014 | 	.check_preempt_curr	= check_preempt_curr_rt, | 
 | 2015 |  | 
 | 2016 | 	.pick_next_task		= pick_next_task_rt, | 
 | 2017 | 	.put_prev_task		= put_prev_task_rt, | 
 | 2018 |  | 
| Peter Williams | 681f3e6 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 2019 | #ifdef CONFIG_SMP | 
| Li Zefan | 4ce72a2 | 2008-10-22 15:25:26 +0800 | [diff] [blame] | 2020 | 	.select_task_rq		= select_task_rq_rt, | 
 | 2021 |  | 
| Gregory Haskins | 73fe6aa | 2008-01-25 21:08:07 +0100 | [diff] [blame] | 2022 | 	.set_cpus_allowed       = set_cpus_allowed_rt, | 
| Gregory Haskins | 1f11eb6 | 2008-06-04 15:04:05 -0400 | [diff] [blame] | 2023 | 	.rq_online              = rq_online_rt, | 
 | 2024 | 	.rq_offline             = rq_offline_rt, | 
| Steven Rostedt | 9a897c5 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 2025 | 	.pre_schedule		= pre_schedule_rt, | 
 | 2026 | 	.post_schedule		= post_schedule_rt, | 
| Peter Zijlstra | efbbd05 | 2009-12-16 18:04:40 +0100 | [diff] [blame] | 2027 | 	.task_woken		= task_woken_rt, | 
| Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 2028 | 	.switched_from		= switched_from_rt, | 
| Peter Williams | 681f3e6 | 2007-10-24 18:23:51 +0200 | [diff] [blame] | 2029 | #endif | 
| Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2030 |  | 
| Srivatsa Vaddagiri | 83b699e | 2007-10-15 17:00:08 +0200 | [diff] [blame] | 2031 | 	.set_curr_task          = set_curr_task_rt, | 
| Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2032 | 	.task_tick		= task_tick_rt, | 
| Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 2033 |  | 
| Peter Williams | 0d721ce | 2009-09-21 01:31:53 +0000 | [diff] [blame] | 2034 | 	.get_rr_interval	= get_rr_interval_rt, | 
 | 2035 |  | 
| Steven Rostedt | cb46984 | 2008-01-25 21:08:22 +0100 | [diff] [blame] | 2036 | 	.prio_changed		= prio_changed_rt, | 
 | 2037 | 	.switched_to		= switched_to_rt, | 
| Ingo Molnar | bb44e5d | 2007-07-09 18:51:58 +0200 | [diff] [blame] | 2038 | }; | 
| Peter Zijlstra | ada18de | 2008-06-19 14:22:24 +0200 | [diff] [blame] | 2039 |  | 
 | 2040 | #ifdef CONFIG_SCHED_DEBUG | 
 | 2041 | extern void print_rt_rq(struct seq_file *m, int cpu, struct rt_rq *rt_rq); | 
 | 2042 |  | 
| Peter Zijlstra | 029632f | 2011-10-25 10:00:11 +0200 | [diff] [blame] | 2043 | void print_rt_stats(struct seq_file *m, int cpu) | 
| Peter Zijlstra | ada18de | 2008-06-19 14:22:24 +0200 | [diff] [blame] | 2044 | { | 
| Cheng Xu | ec514c4 | 2011-05-14 14:20:02 +0800 | [diff] [blame] | 2045 | 	rt_rq_iter_t iter; | 
| Peter Zijlstra | ada18de | 2008-06-19 14:22:24 +0200 | [diff] [blame] | 2046 | 	struct rt_rq *rt_rq; | 
 | 2047 |  | 
 | 2048 | 	rcu_read_lock(); | 
| Cheng Xu | ec514c4 | 2011-05-14 14:20:02 +0800 | [diff] [blame] | 2049 | 	for_each_rt_rq(rt_rq, iter, cpu_rq(cpu)) | 
| Peter Zijlstra | ada18de | 2008-06-19 14:22:24 +0200 | [diff] [blame] | 2050 | 		print_rt_rq(m, cpu, rt_rq); | 
 | 2051 | 	rcu_read_unlock(); | 
 | 2052 | } | 
| Dhaval Giani | 55e12e5 | 2008-06-24 23:39:43 +0530 | [diff] [blame] | 2053 | #endif /* CONFIG_SCHED_DEBUG */ |