Commit 2a911f0b authored by Arnd Bergmann's avatar Arnd Bergmann Committed by Paul Mackerras

[PATCH] spufs: Improved SPU preemptability [part 2].

This patch reduces lock complexity of SPU scheduler, particularly
for involuntary preemptive switches.  As a result the new code
does a better job of mapping the highest priority tasks to SPUs.

Lock complexity is reduced by using the system default workqueue
to perform involuntary saves.  In this way we avoid nasty lock
ordering problems that the previous code had.  A "minimum timeslice"
for SPU contexts is also introduced.  The intent here is to avoid
thrashing.

While the new scheduler does a better job at prioritization it
still does nothing for fairness.

From: Mark Nutter <mnutter@us.ibm.com>
Signed-off-by: default avatarArnd Bergmann <arndb@de.ibm.com>
Signed-off-by: default avatarPaul Mackerras <paulus@samba.org>
parent 5110459f
...@@ -116,8 +116,10 @@ int spu_acquire_runnable(struct spu_context *ctx) ...@@ -116,8 +116,10 @@ int spu_acquire_runnable(struct spu_context *ctx)
int ret = 0; int ret = 0;
down_read(&ctx->state_sema); down_read(&ctx->state_sema);
if (ctx->state == SPU_STATE_RUNNABLE) if (ctx->state == SPU_STATE_RUNNABLE) {
ctx->spu->prio = current->prio;
return 0; return 0;
}
/* ctx is about to be freed, can't acquire any more */ /* ctx is about to be freed, can't acquire any more */
if (!ctx->owner) { if (!ctx->owner) {
ret = -EINVAL; ret = -EINVAL;
......
...@@ -45,6 +45,8 @@ ...@@ -45,6 +45,8 @@
#include <asm/spu_csa.h> #include <asm/spu_csa.h>
#include "spufs.h" #include "spufs.h"
#define SPU_MIN_TIMESLICE (100 * HZ / 1000))
#define SPU_BITMAP_SIZE (((MAX_PRIO+BITS_PER_LONG)/BITS_PER_LONG)+1) #define SPU_BITMAP_SIZE (((MAX_PRIO+BITS_PER_LONG)/BITS_PER_LONG)+1)
struct spu_prio_array { struct spu_prio_array {
atomic_t nr_blocked; atomic_t nr_blocked;
...@@ -168,6 +170,7 @@ static inline void bind_context(struct spu *spu, struct spu_context *ctx) ...@@ -168,6 +170,7 @@ static inline void bind_context(struct spu *spu, struct spu_context *ctx)
spu->number); spu->number);
spu->ctx = ctx; spu->ctx = ctx;
spu->flags = 0; spu->flags = 0;
ctx->flags = 0;
ctx->spu = spu; ctx->spu = spu;
ctx->ops = &spu_hw_ops; ctx->ops = &spu_hw_ops;
spu->pid = current->pid; spu->pid = current->pid;
...@@ -180,6 +183,7 @@ static inline void bind_context(struct spu *spu, struct spu_context *ctx) ...@@ -180,6 +183,7 @@ static inline void bind_context(struct spu *spu, struct spu_context *ctx)
mb(); mb();
spu_unmap_mappings(ctx); spu_unmap_mappings(ctx);
spu_restore(&ctx->csa, spu); spu_restore(&ctx->csa, spu);
spu->timestamp = jiffies;
} }
static inline void unbind_context(struct spu *spu, struct spu_context *ctx) static inline void unbind_context(struct spu *spu, struct spu_context *ctx)
...@@ -188,6 +192,7 @@ static inline void unbind_context(struct spu *spu, struct spu_context *ctx) ...@@ -188,6 +192,7 @@ static inline void unbind_context(struct spu *spu, struct spu_context *ctx)
spu->pid, spu->number); spu->pid, spu->number);
spu_unmap_mappings(ctx); spu_unmap_mappings(ctx);
spu_save(&ctx->csa, spu); spu_save(&ctx->csa, spu);
spu->timestamp = jiffies;
ctx->state = SPU_STATE_SAVED; ctx->state = SPU_STATE_SAVED;
spu->ibox_callback = NULL; spu->ibox_callback = NULL;
spu->wbox_callback = NULL; spu->wbox_callback = NULL;
...@@ -197,38 +202,62 @@ static inline void unbind_context(struct spu *spu, struct spu_context *ctx) ...@@ -197,38 +202,62 @@ static inline void unbind_context(struct spu *spu, struct spu_context *ctx)
spu->prio = MAX_PRIO; spu->prio = MAX_PRIO;
ctx->ops = &spu_backing_ops; ctx->ops = &spu_backing_ops;
ctx->spu = NULL; ctx->spu = NULL;
ctx->flags = 0;
spu->flags = 0;
spu->ctx = NULL; spu->ctx = NULL;
} }
static struct spu *preempt_active(struct spu_runqueue *rq) static void spu_reaper(void *data)
{
struct spu_context *ctx = data;
struct spu *spu;
down_write(&ctx->state_sema);
spu = ctx->spu;
if (spu && (ctx->flags & SPU_CONTEXT_PREEMPT)) {
if (atomic_read(&spu->rq->prio.nr_blocked)) {
pr_debug("%s: spu=%d\n", __func__, spu->number);
ctx->ops->runcntl_stop(ctx);
spu_deactivate(ctx);
wake_up_all(&ctx->stop_wq);
} else {
clear_bit(SPU_CONTEXT_PREEMPT_nr, &ctx->flags);
}
}
up_write(&ctx->state_sema);
put_spu_context(ctx);
}
static void schedule_spu_reaper(struct spu_runqueue *rq, struct spu *spu)
{
struct spu_context *ctx = get_spu_context(spu->ctx);
unsigned long now = jiffies;
unsigned long expire = spu->timestamp + SPU_MIN_TIMESLICE;
set_bit(SPU_CONTEXT_PREEMPT_nr, &ctx->flags);
INIT_WORK(&ctx->reap_work, spu_reaper, ctx);
if (time_after(now, expire))
schedule_work(&ctx->reap_work);
else
schedule_delayed_work(&ctx->reap_work, expire - now);
}
static void check_preempt_active(struct spu_runqueue *rq)
{ {
struct list_head *p; struct list_head *p;
struct spu *worst, *spu; struct spu *worst = NULL;
worst = list_entry(rq->active_list.next, struct spu, sched_list);
list_for_each(p, &rq->active_list) { list_for_each(p, &rq->active_list) {
spu = list_entry(p, struct spu, sched_list); struct spu *spu = list_entry(p, struct spu, sched_list);
if (spu->prio > worst->prio) { struct spu_context *ctx = spu->ctx;
if (!(ctx->flags & SPU_CONTEXT_PREEMPT)) {
if (!worst || (spu->prio > worst->prio)) {
worst = spu; worst = spu;
} }
} }
if (current->prio < worst->prio) {
struct spu_context *ctx = worst->ctx;
spu = worst;
if (down_write_trylock(&ctx->state_sema)) {
pr_debug("%s: booting pid=%d from SPU %d\n",
__FUNCTION__, spu->pid, spu->number);
del_active(rq, spu);
up(&rq->sem);
wake_up_all(&ctx->stop_wq);
ctx->ops->runcntl_stop(ctx);
unbind_context(spu, ctx);
up_write(&ctx->state_sema);
return spu;
}
} }
return NULL; if (worst && (current->prio < worst->prio))
schedule_spu_reaper(rq, worst);
} }
static struct spu *get_idle_spu(struct spu_context *ctx, u64 flags) static struct spu *get_idle_spu(struct spu_context *ctx, u64 flags)
...@@ -256,10 +285,7 @@ static struct spu *get_idle_spu(struct spu_context *ctx, u64 flags) ...@@ -256,10 +285,7 @@ static struct spu *get_idle_spu(struct spu_context *ctx, u64 flags)
continue; continue;
} }
} else { } else {
if (is_best_prio(rq)) { check_preempt_active(rq);
if ((spu = preempt_active(rq)) != NULL)
return spu;
}
prio_wait(rq, ctx, flags); prio_wait(rq, ctx, flags);
if (signal_pending(current)) { if (signal_pending(current)) {
prio_wakeup(rq); prio_wakeup(rq);
...@@ -361,6 +387,8 @@ void spu_yield(struct spu_context *ctx) ...@@ -361,6 +387,8 @@ void spu_yield(struct spu_context *ctx)
spu_deactivate(ctx); spu_deactivate(ctx);
ctx->state = SPU_STATE_SAVED; ctx->state = SPU_STATE_SAVED;
need_yield = 1; need_yield = 1;
} else if (spu) {
spu->prio = MAX_PRIO;
} }
up_write(&ctx->state_sema); up_write(&ctx->state_sema);
if (unlikely(need_yield)) if (unlikely(need_yield))
...@@ -399,6 +427,7 @@ int __init spu_sched_init(void) ...@@ -399,6 +427,7 @@ int __init spu_sched_init(void)
pr_debug("%s: adding SPU[%d]\n", __FUNCTION__, spu->number); pr_debug("%s: adding SPU[%d]\n", __FUNCTION__, spu->number);
add_idle(rq, spu); add_idle(rq, spu);
spu->rq = rq; spu->rq = rq;
spu->timestamp = jiffies;
} }
if (!rq->nr_idle) { if (!rq->nr_idle) {
printk(KERN_WARNING "%s: No available SPUs.\n", __FUNCTION__); printk(KERN_WARNING "%s: No available SPUs.\n", __FUNCTION__);
......
...@@ -37,6 +37,9 @@ enum { ...@@ -37,6 +37,9 @@ enum {
struct spu_context_ops; struct spu_context_ops;
#define SPU_CONTEXT_PREEMPT_nr 0UL
#define SPU_CONTEXT_PREEMPT (1UL << SPU_CONTEXT_PREEMPT_nr)
struct spu_context { struct spu_context {
struct spu *spu; /* pointer to a physical SPU */ struct spu *spu; /* pointer to a physical SPU */
struct spu_state csa; /* SPU context save area. */ struct spu_state csa; /* SPU context save area. */
...@@ -55,6 +58,8 @@ struct spu_context { ...@@ -55,6 +58,8 @@ struct spu_context {
struct fasync_struct *ibox_fasync; struct fasync_struct *ibox_fasync;
struct fasync_struct *wbox_fasync; struct fasync_struct *wbox_fasync;
struct spu_context_ops *ops; struct spu_context_ops *ops;
struct work_struct reap_work;
u64 flags;
}; };
/* SPU context query/set operations. */ /* SPU context query/set operations. */
......
...@@ -129,6 +129,7 @@ struct spu { ...@@ -129,6 +129,7 @@ struct spu {
struct mm_struct *mm; struct mm_struct *mm;
struct spu_context *ctx; struct spu_context *ctx;
struct spu_runqueue *rq; struct spu_runqueue *rq;
unsigned long long timestamp;
pid_t pid; pid_t pid;
int prio; int prio;
int class_0_pending; int class_0_pending;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment