1 /* sched.c - SPU scheduler.
3 * Copyright (C) IBM 2005
4 * Author: Mark Nutter <mnutter@us.ibm.com>
6 * 2006-03-31 NUMA domains added.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include <linux/module.h>
26 #include <linux/errno.h>
27 #include <linux/sched.h>
28 #include <linux/kernel.h>
30 #include <linux/completion.h>
31 #include <linux/vmalloc.h>
32 #include <linux/smp.h>
33 #include <linux/stddef.h>
34 #include <linux/unistd.h>
35 #include <linux/numa.h>
36 #include <linux/mutex.h>
37 #include <linux/notifier.h>
38 #include <linux/kthread.h>
39 #include <linux/pid_namespace.h>
40 #include <linux/proc_fs.h>
41 #include <linux/seq_file.h>
44 #include <asm/mmu_context.h>
46 #include <asm/spu_csa.h>
47 #include <asm/spu_priv1.h>
50 struct spu_prio_array {
51 DECLARE_BITMAP(bitmap, MAX_PRIO);
52 struct list_head runq[MAX_PRIO];
54 struct list_head active_list[MAX_NUMNODES];
55 struct mutex active_mutex[MAX_NUMNODES];
56 int nr_active[MAX_NUMNODES];
60 static unsigned long spu_avenrun[3];
61 static struct spu_prio_array *spu_prio;
62 static struct task_struct *spusched_task;
63 static struct timer_list spusched_timer;
66 * Priority of a normal, non-rt, non-niced'd process (aka nice level 0).
68 #define NORMAL_PRIO 120
71 * Frequency of the spu scheduler tick. By default we do one SPU scheduler
72 * tick for every 10 CPU scheduler ticks.
74 #define SPUSCHED_TICK (10)
77 * These are the 'tuning knobs' of the scheduler:
79 * Minimum timeslice is 5 msecs (or 1 spu scheduler tick, whichever is
80 * larger), default timeslice is 100 msecs, maximum timeslice is 800 msecs.
82 #define MIN_SPU_TIMESLICE max(5 * HZ / (1000 * SPUSCHED_TICK), 1)
83 #define DEF_SPU_TIMESLICE (100 * HZ / (1000 * SPUSCHED_TICK))
85 #define MAX_USER_PRIO (MAX_PRIO - MAX_RT_PRIO)
86 #define SCALE_PRIO(x, prio) \
87 max(x * (MAX_PRIO - prio) / (MAX_USER_PRIO / 2), MIN_SPU_TIMESLICE)
90 * scale user-nice values [ -20 ... 0 ... 19 ] to time slice values:
91 * [800ms ... 100ms ... 5ms]
93 * The higher a thread's priority, the bigger timeslices
94 * it gets during one round of execution. But even the lowest
95 * priority thread gets MIN_TIMESLICE worth of execution time.
97 void spu_set_timeslice(struct spu_context *ctx)
99 if (ctx->prio < NORMAL_PRIO)
100 ctx->time_slice = SCALE_PRIO(DEF_SPU_TIMESLICE * 4, ctx->prio);
102 ctx->time_slice = SCALE_PRIO(DEF_SPU_TIMESLICE, ctx->prio);
106 * Update scheduling information from the owning thread.
108 void __spu_update_sched_info(struct spu_context *ctx)
111 * 32-Bit assignment are atomic on powerpc, and we don't care about
112 * memory ordering here because retriving the controlling thread is
113 * per defintion racy.
115 ctx->tid = current->pid;
118 * We do our own priority calculations, so we normally want
119 * ->static_prio to start with. Unfortunately thies field
120 * contains junk for threads with a realtime scheduling
121 * policy so we have to look at ->prio in this case.
123 if (rt_prio(current->prio))
124 ctx->prio = current->prio;
126 ctx->prio = current->static_prio;
127 ctx->policy = current->policy;
130 * A lot of places that don't hold active_mutex poke into
131 * cpus_allowed, including grab_runnable_context which
132 * already holds the runq_lock. So abuse runq_lock
133 * to protect this field aswell.
135 spin_lock(&spu_prio->runq_lock);
136 ctx->cpus_allowed = current->cpus_allowed;
137 spin_unlock(&spu_prio->runq_lock);
140 void spu_update_sched_info(struct spu_context *ctx)
142 int node = ctx->spu->node;
144 mutex_lock(&spu_prio->active_mutex[node]);
145 __spu_update_sched_info(ctx);
146 mutex_unlock(&spu_prio->active_mutex[node]);
149 static int __node_allowed(struct spu_context *ctx, int node)
151 if (nr_cpus_node(node)) {
152 cpumask_t mask = node_to_cpumask(node);
154 if (cpus_intersects(mask, ctx->cpus_allowed))
161 static int node_allowed(struct spu_context *ctx, int node)
165 spin_lock(&spu_prio->runq_lock);
166 rval = __node_allowed(ctx, node);
167 spin_unlock(&spu_prio->runq_lock);
173 * spu_add_to_active_list - add spu to active list
174 * @spu: spu to add to the active list
176 static void spu_add_to_active_list(struct spu *spu)
178 int node = spu->node;
180 mutex_lock(&spu_prio->active_mutex[node]);
181 spu_prio->nr_active[node]++;
182 list_add_tail(&spu->list, &spu_prio->active_list[node]);
183 mutex_unlock(&spu_prio->active_mutex[node]);
186 static void __spu_remove_from_active_list(struct spu *spu)
188 list_del_init(&spu->list);
189 spu_prio->nr_active[spu->node]--;
193 * spu_remove_from_active_list - remove spu from active list
194 * @spu: spu to remove from the active list
196 static void spu_remove_from_active_list(struct spu *spu)
198 int node = spu->node;
200 mutex_lock(&spu_prio->active_mutex[node]);
201 __spu_remove_from_active_list(spu);
202 mutex_unlock(&spu_prio->active_mutex[node]);
205 static BLOCKING_NOTIFIER_HEAD(spu_switch_notifier);
207 static void spu_switch_notify(struct spu *spu, struct spu_context *ctx)
209 blocking_notifier_call_chain(&spu_switch_notifier,
210 ctx ? ctx->object_id : 0, spu);
213 int spu_switch_event_register(struct notifier_block * n)
215 return blocking_notifier_chain_register(&spu_switch_notifier, n);
218 int spu_switch_event_unregister(struct notifier_block * n)
220 return blocking_notifier_chain_unregister(&spu_switch_notifier, n);
224 * spu_bind_context - bind spu context to physical spu
225 * @spu: physical spu to bind to
226 * @ctx: context to bind
228 static void spu_bind_context(struct spu *spu, struct spu_context *ctx)
230 pr_debug("%s: pid=%d SPU=%d NODE=%d\n", __FUNCTION__, current->pid,
231 spu->number, spu->node);
235 ctx->ops = &spu_hw_ops;
236 spu->pid = current->pid;
237 spu_associate_mm(spu, ctx->owner);
238 spu->ibox_callback = spufs_ibox_callback;
239 spu->wbox_callback = spufs_wbox_callback;
240 spu->stop_callback = spufs_stop_callback;
241 spu->mfc_callback = spufs_mfc_callback;
242 spu->dma_callback = spufs_dma_callback;
244 spu_unmap_mappings(ctx);
245 spu_restore(&ctx->csa, spu);
246 spu->timestamp = jiffies;
247 spu_cpu_affinity_set(spu, raw_smp_processor_id());
248 spu_switch_notify(spu, ctx);
249 ctx->state = SPU_STATE_RUNNABLE;
253 * spu_unbind_context - unbind spu context from physical spu
254 * @spu: physical spu to unbind from
255 * @ctx: context to unbind
257 static void spu_unbind_context(struct spu *spu, struct spu_context *ctx)
259 pr_debug("%s: unbind pid=%d SPU=%d NODE=%d\n", __FUNCTION__,
260 spu->pid, spu->number, spu->node);
262 spu_switch_notify(spu, NULL);
263 spu_unmap_mappings(ctx);
264 spu_save(&ctx->csa, spu);
265 spu->timestamp = jiffies;
266 ctx->state = SPU_STATE_SAVED;
267 spu->ibox_callback = NULL;
268 spu->wbox_callback = NULL;
269 spu->stop_callback = NULL;
270 spu->mfc_callback = NULL;
271 spu->dma_callback = NULL;
272 spu_associate_mm(spu, NULL);
274 ctx->ops = &spu_backing_ops;
281 * spu_add_to_rq - add a context to the runqueue
282 * @ctx: context to add
284 static void __spu_add_to_rq(struct spu_context *ctx)
286 int prio = ctx->prio;
288 spu_prio->nr_waiting++;
289 list_add_tail(&ctx->rq, &spu_prio->runq[prio]);
290 set_bit(prio, spu_prio->bitmap);
293 static void __spu_del_from_rq(struct spu_context *ctx)
295 int prio = ctx->prio;
297 if (!list_empty(&ctx->rq)) {
298 list_del_init(&ctx->rq);
299 spu_prio->nr_waiting--;
301 if (list_empty(&spu_prio->runq[prio]))
302 clear_bit(prio, spu_prio->bitmap);
305 static void spu_prio_wait(struct spu_context *ctx)
309 spin_lock(&spu_prio->runq_lock);
310 prepare_to_wait_exclusive(&ctx->stop_wq, &wait, TASK_INTERRUPTIBLE);
311 if (!signal_pending(current)) {
312 __spu_add_to_rq(ctx);
313 spin_unlock(&spu_prio->runq_lock);
314 mutex_unlock(&ctx->state_mutex);
316 mutex_lock(&ctx->state_mutex);
317 spin_lock(&spu_prio->runq_lock);
318 __spu_del_from_rq(ctx);
320 spin_unlock(&spu_prio->runq_lock);
321 __set_current_state(TASK_RUNNING);
322 remove_wait_queue(&ctx->stop_wq, &wait);
325 static struct spu *spu_get_idle(struct spu_context *ctx)
327 struct spu *spu = NULL;
328 int node = cpu_to_node(raw_smp_processor_id());
331 for (n = 0; n < MAX_NUMNODES; n++, node++) {
332 node = (node < MAX_NUMNODES) ? node : 0;
333 if (!node_allowed(ctx, node))
335 spu = spu_alloc_node(node);
343 * find_victim - find a lower priority context to preempt
344 * @ctx: canidate context for running
346 * Returns the freed physical spu to run the new context on.
348 static struct spu *find_victim(struct spu_context *ctx)
350 struct spu_context *victim = NULL;
355 * Look for a possible preemption candidate on the local node first.
356 * If there is no candidate look at the other nodes. This isn't
357 * exactly fair, but so far the whole spu schedule tries to keep
358 * a strong node affinity. We might want to fine-tune this in
362 node = cpu_to_node(raw_smp_processor_id());
363 for (n = 0; n < MAX_NUMNODES; n++, node++) {
364 node = (node < MAX_NUMNODES) ? node : 0;
365 if (!node_allowed(ctx, node))
368 mutex_lock(&spu_prio->active_mutex[node]);
369 list_for_each_entry(spu, &spu_prio->active_list[node], list) {
370 struct spu_context *tmp = spu->ctx;
372 if (tmp->prio > ctx->prio &&
373 (!victim || tmp->prio > victim->prio))
376 mutex_unlock(&spu_prio->active_mutex[node]);
380 * This nests ctx->state_mutex, but we always lock
381 * higher priority contexts before lower priority
382 * ones, so this is safe until we introduce
383 * priority inheritance schemes.
385 if (!mutex_trylock(&victim->state_mutex)) {
393 * This race can happen because we've dropped
394 * the active list mutex. No a problem, just
395 * restart the search.
397 mutex_unlock(&victim->state_mutex);
401 spu_remove_from_active_list(spu);
402 spu_unbind_context(spu, victim);
403 mutex_unlock(&victim->state_mutex);
405 * We need to break out of the wait loop in spu_run
406 * manually to ensure this context gets put on the
407 * runqueue again ASAP.
409 wake_up(&victim->stop_wq);
418 * spu_activate - find a free spu for a context and execute it
419 * @ctx: spu context to schedule
420 * @flags: flags (currently ignored)
422 * Tries to find a free spu to run @ctx. If no free spu is available
423 * add the context to the runqueue so it gets woken up once an spu
426 int spu_activate(struct spu_context *ctx, unsigned long flags)
435 spu = spu_get_idle(ctx);
437 * If this is a realtime thread we try to get it running by
438 * preempting a lower priority thread.
440 if (!spu && rt_prio(ctx->prio))
441 spu = find_victim(ctx);
443 spu_bind_context(spu, ctx);
444 spu_add_to_active_list(spu);
449 } while (!signal_pending(current));
455 * grab_runnable_context - try to find a runnable context
457 * Remove the highest priority context on the runqueue and return it
458 * to the caller. Returns %NULL if no runnable context was found.
460 static struct spu_context *grab_runnable_context(int prio, int node)
462 struct spu_context *ctx;
465 spin_lock(&spu_prio->runq_lock);
466 best = sched_find_first_bit(spu_prio->bitmap);
467 while (best < prio) {
468 struct list_head *rq = &spu_prio->runq[best];
470 list_for_each_entry(ctx, rq, rq) {
471 /* XXX(hch): check for affinity here aswell */
472 if (__node_allowed(ctx, node)) {
473 __spu_del_from_rq(ctx);
481 spin_unlock(&spu_prio->runq_lock);
485 static int __spu_deactivate(struct spu_context *ctx, int force, int max_prio)
487 struct spu *spu = ctx->spu;
488 struct spu_context *new = NULL;
491 new = grab_runnable_context(max_prio, spu->node);
493 spu_remove_from_active_list(spu);
494 spu_unbind_context(spu, ctx);
497 wake_up(&new->stop_wq);
506 * spu_deactivate - unbind a context from it's physical spu
507 * @ctx: spu context to unbind
509 * Unbind @ctx from the physical spu it is running on and schedule
510 * the highest priority context to run on the freed physical spu.
512 void spu_deactivate(struct spu_context *ctx)
515 * We must never reach this for a nosched context,
516 * but handle the case gracefull instead of panicing.
518 if (ctx->flags & SPU_CREATE_NOSCHED) {
523 __spu_deactivate(ctx, 1, MAX_PRIO);
527 * spu_yield - yield a physical spu if others are waiting
528 * @ctx: spu context to yield
530 * Check if there is a higher priority context waiting and if yes
531 * unbind @ctx from the physical spu and schedule the highest
532 * priority context to run on the freed physical spu instead.
534 void spu_yield(struct spu_context *ctx)
536 if (!(ctx->flags & SPU_CREATE_NOSCHED)) {
537 mutex_lock(&ctx->state_mutex);
538 __spu_deactivate(ctx, 0, MAX_PRIO);
539 mutex_unlock(&ctx->state_mutex);
543 static void spusched_tick(struct spu_context *ctx)
545 if (ctx->flags & SPU_CREATE_NOSCHED)
547 if (ctx->policy == SCHED_FIFO)
550 if (--ctx->time_slice)
554 * Unfortunately active_mutex ranks outside of state_mutex, so
555 * we have to trylock here. If we fail give the context another
556 * tick and try again.
558 if (mutex_trylock(&ctx->state_mutex)) {
559 struct spu *spu = ctx->spu;
560 struct spu_context *new;
562 new = grab_runnable_context(ctx->prio + 1, spu->node);
565 __spu_remove_from_active_list(spu);
566 spu_unbind_context(spu, ctx);
568 wake_up(&new->stop_wq);
570 * We need to break out of the wait loop in
571 * spu_run manually to ensure this context
572 * gets put on the runqueue again ASAP.
574 wake_up(&ctx->stop_wq);
576 spu_set_timeslice(ctx);
577 mutex_unlock(&ctx->state_mutex);
584 * count_active_contexts - count nr of active tasks
586 * Return the number of tasks currently running or waiting to run.
588 * Note that we don't take runq_lock / active_mutex here. Reading
589 * a single 32bit value is atomic on powerpc, and we don't care
590 * about memory ordering issues here.
592 static unsigned long count_active_contexts(void)
594 int nr_active = 0, node;
596 for (node = 0; node < MAX_NUMNODES; node++)
597 nr_active += spu_prio->nr_active[node];
598 nr_active += spu_prio->nr_waiting;
604 * spu_calc_load - given tick count, update the avenrun load estimates.
607 * No locking against reading these values from userspace, as for
608 * the CPU loadavg code.
610 static void spu_calc_load(unsigned long ticks)
612 unsigned long active_tasks; /* fixed-point */
613 static int count = LOAD_FREQ;
617 if (unlikely(count < 0)) {
618 active_tasks = count_active_contexts() * FIXED_1;
620 CALC_LOAD(spu_avenrun[0], EXP_1, active_tasks);
621 CALC_LOAD(spu_avenrun[1], EXP_5, active_tasks);
622 CALC_LOAD(spu_avenrun[2], EXP_15, active_tasks);
628 static void spusched_wake(unsigned long data)
630 mod_timer(&spusched_timer, jiffies + SPUSCHED_TICK);
631 wake_up_process(spusched_task);
632 spu_calc_load(SPUSCHED_TICK);
635 static int spusched_thread(void *unused)
637 struct spu *spu, *next;
640 setup_timer(&spusched_timer, spusched_wake, 0);
641 __mod_timer(&spusched_timer, jiffies + SPUSCHED_TICK);
643 while (!kthread_should_stop()) {
644 set_current_state(TASK_INTERRUPTIBLE);
646 for (node = 0; node < MAX_NUMNODES; node++) {
647 mutex_lock(&spu_prio->active_mutex[node]);
648 list_for_each_entry_safe(spu, next,
649 &spu_prio->active_list[node],
651 spusched_tick(spu->ctx);
652 mutex_unlock(&spu_prio->active_mutex[node]);
656 del_timer_sync(&spusched_timer);
660 #define LOAD_INT(x) ((x) >> FSHIFT)
661 #define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
663 static int show_spu_loadavg(struct seq_file *s, void *private)
667 a = spu_avenrun[0] + (FIXED_1/200);
668 b = spu_avenrun[1] + (FIXED_1/200);
669 c = spu_avenrun[2] + (FIXED_1/200);
672 * Note that last_pid doesn't really make much sense for the
673 * SPU loadavg (it even seems very odd on the CPU side..),
674 * but we include it here to have a 100% compatible interface.
676 seq_printf(s, "%d.%02d %d.%02d %d.%02d %ld/%d %d\n",
677 LOAD_INT(a), LOAD_FRAC(a),
678 LOAD_INT(b), LOAD_FRAC(b),
679 LOAD_INT(c), LOAD_FRAC(c),
680 count_active_contexts(),
681 atomic_read(&nr_spu_contexts),
682 current->nsproxy->pid_ns->last_pid);
686 static int spu_loadavg_open(struct inode *inode, struct file *file)
688 return single_open(file, show_spu_loadavg, NULL);
691 static const struct file_operations spu_loadavg_fops = {
692 .open = spu_loadavg_open,
695 .release = single_release,
698 int __init spu_sched_init(void)
700 struct proc_dir_entry *entry;
701 int err = -ENOMEM, i;
703 spu_prio = kzalloc(sizeof(struct spu_prio_array), GFP_KERNEL);
707 for (i = 0; i < MAX_PRIO; i++) {
708 INIT_LIST_HEAD(&spu_prio->runq[i]);
709 __clear_bit(i, spu_prio->bitmap);
711 __set_bit(MAX_PRIO, spu_prio->bitmap);
712 for (i = 0; i < MAX_NUMNODES; i++) {
713 mutex_init(&spu_prio->active_mutex[i]);
714 INIT_LIST_HEAD(&spu_prio->active_list[i]);
716 spin_lock_init(&spu_prio->runq_lock);
718 spusched_task = kthread_run(spusched_thread, NULL, "spusched");
719 if (IS_ERR(spusched_task)) {
720 err = PTR_ERR(spusched_task);
721 goto out_free_spu_prio;
724 entry = create_proc_entry("spu_loadavg", 0, NULL);
726 goto out_stop_kthread;
727 entry->proc_fops = &spu_loadavg_fops;
729 pr_debug("spusched: tick: %d, min ticks: %d, default ticks: %d\n",
730 SPUSCHED_TICK, MIN_SPU_TIMESLICE, DEF_SPU_TIMESLICE);
734 kthread_stop(spusched_task);
741 void __exit spu_sched_exit(void)
743 struct spu *spu, *tmp;
746 remove_proc_entry("spu_loadavg", NULL);
748 kthread_stop(spusched_task);
750 for (node = 0; node < MAX_NUMNODES; node++) {
751 mutex_lock(&spu_prio->active_mutex[node]);
752 list_for_each_entry_safe(spu, tmp, &spu_prio->active_list[node],
754 list_del_init(&spu->list);
757 mutex_unlock(&spu_prio->active_mutex[node]);