sched.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  1. /* sched.c - SPU scheduler.
  2. *
  3. * Copyright (C) IBM 2005
  4. * Author: Mark Nutter <mnutter@us.ibm.com>
  5. *
  6. * 2006-03-31 NUMA domains added.
  7. *
  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)
  11. * any later version.
  12. *
  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.
  17. *
  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.
  21. */
  22. #undef DEBUG
  23. #include <linux/module.h>
  24. #include <linux/errno.h>
  25. #include <linux/sched.h>
  26. #include <linux/kernel.h>
  27. #include <linux/mm.h>
  28. #include <linux/completion.h>
  29. #include <linux/vmalloc.h>
  30. #include <linux/smp.h>
  31. #include <linux/stddef.h>
  32. #include <linux/unistd.h>
  33. #include <linux/numa.h>
  34. #include <linux/mutex.h>
  35. #include <linux/notifier.h>
  36. #include <linux/kthread.h>
  37. #include <linux/pid_namespace.h>
  38. #include <linux/proc_fs.h>
  39. #include <linux/seq_file.h>
  40. #include <asm/io.h>
  41. #include <asm/mmu_context.h>
  42. #include <asm/spu.h>
  43. #include <asm/spu_csa.h>
  44. #include <asm/spu_priv1.h>
  45. #include "spufs.h"
  46. struct spu_prio_array {
  47. DECLARE_BITMAP(bitmap, MAX_PRIO);
  48. struct list_head runq[MAX_PRIO];
  49. spinlock_t runq_lock;
  50. int nr_waiting;
  51. };
  52. static unsigned long spu_avenrun[3];
  53. static struct spu_prio_array *spu_prio;
  54. static struct task_struct *spusched_task;
  55. static struct timer_list spusched_timer;
  56. /*
  57. * Priority of a normal, non-rt, non-niced'd process (aka nice level 0).
  58. */
  59. #define NORMAL_PRIO 120
  60. /*
  61. * Frequency of the spu scheduler tick. By default we do one SPU scheduler
  62. * tick for every 10 CPU scheduler ticks.
  63. */
  64. #define SPUSCHED_TICK (10)
  65. /*
  66. * These are the 'tuning knobs' of the scheduler:
  67. *
  68. * Minimum timeslice is 5 msecs (or 1 spu scheduler tick, whichever is
  69. * larger), default timeslice is 100 msecs, maximum timeslice is 800 msecs.
  70. */
  71. #define MIN_SPU_TIMESLICE max(5 * HZ / (1000 * SPUSCHED_TICK), 1)
  72. #define DEF_SPU_TIMESLICE (100 * HZ / (1000 * SPUSCHED_TICK))
  73. #define MAX_USER_PRIO (MAX_PRIO - MAX_RT_PRIO)
  74. #define SCALE_PRIO(x, prio) \
  75. max(x * (MAX_PRIO - prio) / (MAX_USER_PRIO / 2), MIN_SPU_TIMESLICE)
  76. /*
  77. * scale user-nice values [ -20 ... 0 ... 19 ] to time slice values:
  78. * [800ms ... 100ms ... 5ms]
  79. *
  80. * The higher a thread's priority, the bigger timeslices
  81. * it gets during one round of execution. But even the lowest
  82. * priority thread gets MIN_TIMESLICE worth of execution time.
  83. */
  84. void spu_set_timeslice(struct spu_context *ctx)
  85. {
  86. if (ctx->prio < NORMAL_PRIO)
  87. ctx->time_slice = SCALE_PRIO(DEF_SPU_TIMESLICE * 4, ctx->prio);
  88. else
  89. ctx->time_slice = SCALE_PRIO(DEF_SPU_TIMESLICE, ctx->prio);
  90. }
  91. /*
  92. * Update scheduling information from the owning thread.
  93. */
  94. void __spu_update_sched_info(struct spu_context *ctx)
  95. {
  96. /*
  97. * 32-Bit assignment are atomic on powerpc, and we don't care about
  98. * memory ordering here because retriving the controlling thread is
  99. * per defintion racy.
  100. */
  101. ctx->tid = current->pid;
  102. /*
  103. * We do our own priority calculations, so we normally want
  104. * ->static_prio to start with. Unfortunately thies field
  105. * contains junk for threads with a realtime scheduling
  106. * policy so we have to look at ->prio in this case.
  107. */
  108. if (rt_prio(current->prio))
  109. ctx->prio = current->prio;
  110. else
  111. ctx->prio = current->static_prio;
  112. ctx->policy = current->policy;
  113. /*
  114. * A lot of places that don't hold list_mutex poke into
  115. * cpus_allowed, including grab_runnable_context which
  116. * already holds the runq_lock. So abuse runq_lock
  117. * to protect this field aswell.
  118. */
  119. spin_lock(&spu_prio->runq_lock);
  120. ctx->cpus_allowed = current->cpus_allowed;
  121. spin_unlock(&spu_prio->runq_lock);
  122. }
  123. void spu_update_sched_info(struct spu_context *ctx)
  124. {
  125. int node = ctx->spu->node;
  126. mutex_lock(&cbe_spu_info[node].list_mutex);
  127. __spu_update_sched_info(ctx);
  128. mutex_unlock(&cbe_spu_info[node].list_mutex);
  129. }
  130. static int __node_allowed(struct spu_context *ctx, int node)
  131. {
  132. if (nr_cpus_node(node)) {
  133. cpumask_t mask = node_to_cpumask(node);
  134. if (cpus_intersects(mask, ctx->cpus_allowed))
  135. return 1;
  136. }
  137. return 0;
  138. }
  139. static int node_allowed(struct spu_context *ctx, int node)
  140. {
  141. int rval;
  142. spin_lock(&spu_prio->runq_lock);
  143. rval = __node_allowed(ctx, node);
  144. spin_unlock(&spu_prio->runq_lock);
  145. return rval;
  146. }
  147. void do_notify_spus_active(void)
  148. {
  149. int node;
  150. /*
  151. * Wake up the active spu_contexts.
  152. *
  153. * When the awakened processes see their "notify_active" flag is set,
  154. * they will call spu_switch_notify();
  155. */
  156. for_each_online_node(node) {
  157. struct spu *spu;
  158. mutex_lock(&cbe_spu_info[node].list_mutex);
  159. list_for_each_entry(spu, &cbe_spu_info[node].spus, cbe_list) {
  160. if (spu->alloc_state != SPU_FREE) {
  161. struct spu_context *ctx = spu->ctx;
  162. set_bit(SPU_SCHED_NOTIFY_ACTIVE,
  163. &ctx->sched_flags);
  164. mb();
  165. wake_up_all(&ctx->stop_wq);
  166. }
  167. }
  168. mutex_unlock(&cbe_spu_info[node].list_mutex);
  169. }
  170. }
  171. /**
  172. * spu_bind_context - bind spu context to physical spu
  173. * @spu: physical spu to bind to
  174. * @ctx: context to bind
  175. */
  176. static void spu_bind_context(struct spu *spu, struct spu_context *ctx)
  177. {
  178. pr_debug("%s: pid=%d SPU=%d NODE=%d\n", __FUNCTION__, current->pid,
  179. spu->number, spu->node);
  180. spuctx_switch_state(ctx, SPU_UTIL_SYSTEM);
  181. if (ctx->flags & SPU_CREATE_NOSCHED)
  182. atomic_inc(&cbe_spu_info[spu->node].reserved_spus);
  183. ctx->stats.slb_flt_base = spu->stats.slb_flt;
  184. ctx->stats.class2_intr_base = spu->stats.class2_intr;
  185. spu->ctx = ctx;
  186. spu->flags = 0;
  187. ctx->spu = spu;
  188. ctx->ops = &spu_hw_ops;
  189. spu->pid = current->pid;
  190. spu->tgid = current->tgid;
  191. spu_associate_mm(spu, ctx->owner);
  192. spu->ibox_callback = spufs_ibox_callback;
  193. spu->wbox_callback = spufs_wbox_callback;
  194. spu->stop_callback = spufs_stop_callback;
  195. spu->mfc_callback = spufs_mfc_callback;
  196. spu->dma_callback = spufs_dma_callback;
  197. mb();
  198. spu_unmap_mappings(ctx);
  199. spu_restore(&ctx->csa, spu);
  200. spu->timestamp = jiffies;
  201. spu_cpu_affinity_set(spu, raw_smp_processor_id());
  202. spu_switch_notify(spu, ctx);
  203. ctx->state = SPU_STATE_RUNNABLE;
  204. spuctx_switch_state(ctx, SPU_UTIL_IDLE_LOADED);
  205. }
  206. /*
  207. * Must be used with the list_mutex held.
  208. */
  209. static inline int sched_spu(struct spu *spu)
  210. {
  211. BUG_ON(!mutex_is_locked(&cbe_spu_info[spu->node].list_mutex));
  212. return (!spu->ctx || !(spu->ctx->flags & SPU_CREATE_NOSCHED));
  213. }
  214. static void aff_merge_remaining_ctxs(struct spu_gang *gang)
  215. {
  216. struct spu_context *ctx;
  217. list_for_each_entry(ctx, &gang->aff_list_head, aff_list) {
  218. if (list_empty(&ctx->aff_list))
  219. list_add(&ctx->aff_list, &gang->aff_list_head);
  220. }
  221. gang->aff_flags |= AFF_MERGED;
  222. }
  223. static void aff_set_offsets(struct spu_gang *gang)
  224. {
  225. struct spu_context *ctx;
  226. int offset;
  227. offset = -1;
  228. list_for_each_entry_reverse(ctx, &gang->aff_ref_ctx->aff_list,
  229. aff_list) {
  230. if (&ctx->aff_list == &gang->aff_list_head)
  231. break;
  232. ctx->aff_offset = offset--;
  233. }
  234. offset = 0;
  235. list_for_each_entry(ctx, gang->aff_ref_ctx->aff_list.prev, aff_list) {
  236. if (&ctx->aff_list == &gang->aff_list_head)
  237. break;
  238. ctx->aff_offset = offset++;
  239. }
  240. gang->aff_flags |= AFF_OFFSETS_SET;
  241. }
  242. static struct spu *aff_ref_location(struct spu_context *ctx, int mem_aff,
  243. int group_size, int lowest_offset)
  244. {
  245. struct spu *spu;
  246. int node, n;
  247. /*
  248. * TODO: A better algorithm could be used to find a good spu to be
  249. * used as reference location for the ctxs chain.
  250. */
  251. node = cpu_to_node(raw_smp_processor_id());
  252. for (n = 0; n < MAX_NUMNODES; n++, node++) {
  253. node = (node < MAX_NUMNODES) ? node : 0;
  254. if (!node_allowed(ctx, node))
  255. continue;
  256. mutex_lock(&cbe_spu_info[node].list_mutex);
  257. list_for_each_entry(spu, &cbe_spu_info[node].spus, cbe_list) {
  258. if ((!mem_aff || spu->has_mem_affinity) &&
  259. sched_spu(spu)) {
  260. mutex_unlock(&cbe_spu_info[node].list_mutex);
  261. return spu;
  262. }
  263. }
  264. mutex_unlock(&cbe_spu_info[node].list_mutex);
  265. }
  266. return NULL;
  267. }
  268. static void aff_set_ref_point_location(struct spu_gang *gang)
  269. {
  270. int mem_aff, gs, lowest_offset;
  271. struct spu_context *ctx;
  272. struct spu *tmp;
  273. mem_aff = gang->aff_ref_ctx->flags & SPU_CREATE_AFFINITY_MEM;
  274. lowest_offset = 0;
  275. gs = 0;
  276. list_for_each_entry(tmp, &gang->aff_list_head, aff_list)
  277. gs++;
  278. list_for_each_entry_reverse(ctx, &gang->aff_ref_ctx->aff_list,
  279. aff_list) {
  280. if (&ctx->aff_list == &gang->aff_list_head)
  281. break;
  282. lowest_offset = ctx->aff_offset;
  283. }
  284. gang->aff_ref_spu = aff_ref_location(gang->aff_ref_ctx, mem_aff, gs,
  285. lowest_offset);
  286. }
  287. static struct spu *ctx_location(struct spu *ref, int offset, int node)
  288. {
  289. struct spu *spu;
  290. spu = NULL;
  291. if (offset >= 0) {
  292. list_for_each_entry(spu, ref->aff_list.prev, aff_list) {
  293. BUG_ON(spu->node != node);
  294. if (offset == 0)
  295. break;
  296. if (sched_spu(spu))
  297. offset--;
  298. }
  299. } else {
  300. list_for_each_entry_reverse(spu, ref->aff_list.next, aff_list) {
  301. BUG_ON(spu->node != node);
  302. if (offset == 0)
  303. break;
  304. if (sched_spu(spu))
  305. offset++;
  306. }
  307. }
  308. return spu;
  309. }
  310. /*
  311. * affinity_check is called each time a context is going to be scheduled.
  312. * It returns the spu ptr on which the context must run.
  313. */
  314. static int has_affinity(struct spu_context *ctx)
  315. {
  316. struct spu_gang *gang = ctx->gang;
  317. if (list_empty(&ctx->aff_list))
  318. return 0;
  319. if (!gang->aff_ref_spu) {
  320. if (!(gang->aff_flags & AFF_MERGED))
  321. aff_merge_remaining_ctxs(gang);
  322. if (!(gang->aff_flags & AFF_OFFSETS_SET))
  323. aff_set_offsets(gang);
  324. aff_set_ref_point_location(gang);
  325. }
  326. return gang->aff_ref_spu != NULL;
  327. }
  328. /**
  329. * spu_unbind_context - unbind spu context from physical spu
  330. * @spu: physical spu to unbind from
  331. * @ctx: context to unbind
  332. */
  333. static void spu_unbind_context(struct spu *spu, struct spu_context *ctx)
  334. {
  335. pr_debug("%s: unbind pid=%d SPU=%d NODE=%d\n", __FUNCTION__,
  336. spu->pid, spu->number, spu->node);
  337. spuctx_switch_state(ctx, SPU_UTIL_SYSTEM);
  338. if (spu->ctx->flags & SPU_CREATE_NOSCHED)
  339. atomic_dec(&cbe_spu_info[spu->node].reserved_spus);
  340. if (ctx->gang){
  341. mutex_lock(&ctx->gang->aff_mutex);
  342. if (has_affinity(ctx)) {
  343. if (atomic_dec_and_test(&ctx->gang->aff_sched_count))
  344. ctx->gang->aff_ref_spu = NULL;
  345. }
  346. mutex_unlock(&ctx->gang->aff_mutex);
  347. }
  348. spu_switch_notify(spu, NULL);
  349. spu_unmap_mappings(ctx);
  350. spu_save(&ctx->csa, spu);
  351. spu->timestamp = jiffies;
  352. ctx->state = SPU_STATE_SAVED;
  353. spu->ibox_callback = NULL;
  354. spu->wbox_callback = NULL;
  355. spu->stop_callback = NULL;
  356. spu->mfc_callback = NULL;
  357. spu->dma_callback = NULL;
  358. spu_associate_mm(spu, NULL);
  359. spu->pid = 0;
  360. spu->tgid = 0;
  361. ctx->ops = &spu_backing_ops;
  362. spu->flags = 0;
  363. spu->ctx = NULL;
  364. ctx->stats.slb_flt +=
  365. (spu->stats.slb_flt - ctx->stats.slb_flt_base);
  366. ctx->stats.class2_intr +=
  367. (spu->stats.class2_intr - ctx->stats.class2_intr_base);
  368. /* This maps the underlying spu state to idle */
  369. spuctx_switch_state(ctx, SPU_UTIL_IDLE_LOADED);
  370. ctx->spu = NULL;
  371. }
  372. /**
  373. * spu_add_to_rq - add a context to the runqueue
  374. * @ctx: context to add
  375. */
  376. static void __spu_add_to_rq(struct spu_context *ctx)
  377. {
  378. /*
  379. * Unfortunately this code path can be called from multiple threads
  380. * on behalf of a single context due to the way the problem state
  381. * mmap support works.
  382. *
  383. * Fortunately we need to wake up all these threads at the same time
  384. * and can simply skip the runqueue addition for every but the first
  385. * thread getting into this codepath.
  386. *
  387. * It's still quite hacky, and long-term we should proxy all other
  388. * threads through the owner thread so that spu_run is in control
  389. * of all the scheduling activity for a given context.
  390. */
  391. if (list_empty(&ctx->rq)) {
  392. list_add_tail(&ctx->rq, &spu_prio->runq[ctx->prio]);
  393. set_bit(ctx->prio, spu_prio->bitmap);
  394. if (!spu_prio->nr_waiting++)
  395. __mod_timer(&spusched_timer, jiffies + SPUSCHED_TICK);
  396. }
  397. }
  398. static void __spu_del_from_rq(struct spu_context *ctx)
  399. {
  400. int prio = ctx->prio;
  401. if (!list_empty(&ctx->rq)) {
  402. if (!--spu_prio->nr_waiting)
  403. del_timer(&spusched_timer);
  404. list_del_init(&ctx->rq);
  405. if (list_empty(&spu_prio->runq[prio]))
  406. clear_bit(prio, spu_prio->bitmap);
  407. }
  408. }
  409. static void spu_prio_wait(struct spu_context *ctx)
  410. {
  411. DEFINE_WAIT(wait);
  412. spin_lock(&spu_prio->runq_lock);
  413. prepare_to_wait_exclusive(&ctx->stop_wq, &wait, TASK_INTERRUPTIBLE);
  414. if (!signal_pending(current)) {
  415. __spu_add_to_rq(ctx);
  416. spin_unlock(&spu_prio->runq_lock);
  417. mutex_unlock(&ctx->state_mutex);
  418. schedule();
  419. mutex_lock(&ctx->state_mutex);
  420. spin_lock(&spu_prio->runq_lock);
  421. __spu_del_from_rq(ctx);
  422. }
  423. spin_unlock(&spu_prio->runq_lock);
  424. __set_current_state(TASK_RUNNING);
  425. remove_wait_queue(&ctx->stop_wq, &wait);
  426. }
  427. static struct spu *spu_get_idle(struct spu_context *ctx)
  428. {
  429. struct spu *spu, *aff_ref_spu;
  430. int node, n;
  431. if (ctx->gang) {
  432. mutex_lock(&ctx->gang->aff_mutex);
  433. if (has_affinity(ctx)) {
  434. aff_ref_spu = ctx->gang->aff_ref_spu;
  435. atomic_inc(&ctx->gang->aff_sched_count);
  436. mutex_unlock(&ctx->gang->aff_mutex);
  437. node = aff_ref_spu->node;
  438. mutex_lock(&cbe_spu_info[node].list_mutex);
  439. spu = ctx_location(aff_ref_spu, ctx->aff_offset, node);
  440. if (spu && spu->alloc_state == SPU_FREE)
  441. goto found;
  442. mutex_unlock(&cbe_spu_info[node].list_mutex);
  443. mutex_lock(&ctx->gang->aff_mutex);
  444. if (atomic_dec_and_test(&ctx->gang->aff_sched_count))
  445. ctx->gang->aff_ref_spu = NULL;
  446. mutex_unlock(&ctx->gang->aff_mutex);
  447. return NULL;
  448. }
  449. mutex_unlock(&ctx->gang->aff_mutex);
  450. }
  451. node = cpu_to_node(raw_smp_processor_id());
  452. for (n = 0; n < MAX_NUMNODES; n++, node++) {
  453. node = (node < MAX_NUMNODES) ? node : 0;
  454. if (!node_allowed(ctx, node))
  455. continue;
  456. mutex_lock(&cbe_spu_info[node].list_mutex);
  457. list_for_each_entry(spu, &cbe_spu_info[node].spus, cbe_list) {
  458. if (spu->alloc_state == SPU_FREE)
  459. goto found;
  460. }
  461. mutex_unlock(&cbe_spu_info[node].list_mutex);
  462. }
  463. return NULL;
  464. found:
  465. spu->alloc_state = SPU_USED;
  466. mutex_unlock(&cbe_spu_info[node].list_mutex);
  467. pr_debug("Got SPU %d %d\n", spu->number, spu->node);
  468. spu_init_channels(spu);
  469. return spu;
  470. }
  471. /**
  472. * find_victim - find a lower priority context to preempt
  473. * @ctx: canidate context for running
  474. *
  475. * Returns the freed physical spu to run the new context on.
  476. */
  477. static struct spu *find_victim(struct spu_context *ctx)
  478. {
  479. struct spu_context *victim = NULL;
  480. struct spu *spu;
  481. int node, n;
  482. /*
  483. * Look for a possible preemption candidate on the local node first.
  484. * If there is no candidate look at the other nodes. This isn't
  485. * exactly fair, but so far the whole spu schedule tries to keep
  486. * a strong node affinity. We might want to fine-tune this in
  487. * the future.
  488. */
  489. restart:
  490. node = cpu_to_node(raw_smp_processor_id());
  491. for (n = 0; n < MAX_NUMNODES; n++, node++) {
  492. node = (node < MAX_NUMNODES) ? node : 0;
  493. if (!node_allowed(ctx, node))
  494. continue;
  495. mutex_lock(&cbe_spu_info[node].list_mutex);
  496. list_for_each_entry(spu, &cbe_spu_info[node].spus, cbe_list) {
  497. struct spu_context *tmp = spu->ctx;
  498. if (tmp && tmp->prio > ctx->prio &&
  499. (!victim || tmp->prio > victim->prio))
  500. victim = spu->ctx;
  501. }
  502. mutex_unlock(&cbe_spu_info[node].list_mutex);
  503. if (victim) {
  504. /*
  505. * This nests ctx->state_mutex, but we always lock
  506. * higher priority contexts before lower priority
  507. * ones, so this is safe until we introduce
  508. * priority inheritance schemes.
  509. */
  510. if (!mutex_trylock(&victim->state_mutex)) {
  511. victim = NULL;
  512. goto restart;
  513. }
  514. spu = victim->spu;
  515. if (!spu) {
  516. /*
  517. * This race can happen because we've dropped
  518. * the active list mutex. No a problem, just
  519. * restart the search.
  520. */
  521. mutex_unlock(&victim->state_mutex);
  522. victim = NULL;
  523. goto restart;
  524. }
  525. mutex_lock(&cbe_spu_info[node].list_mutex);
  526. cbe_spu_info[node].nr_active--;
  527. spu_unbind_context(spu, victim);
  528. mutex_unlock(&cbe_spu_info[node].list_mutex);
  529. victim->stats.invol_ctx_switch++;
  530. spu->stats.invol_ctx_switch++;
  531. mutex_unlock(&victim->state_mutex);
  532. /*
  533. * We need to break out of the wait loop in spu_run
  534. * manually to ensure this context gets put on the
  535. * runqueue again ASAP.
  536. */
  537. wake_up(&victim->stop_wq);
  538. return spu;
  539. }
  540. }
  541. return NULL;
  542. }
  543. /**
  544. * spu_activate - find a free spu for a context and execute it
  545. * @ctx: spu context to schedule
  546. * @flags: flags (currently ignored)
  547. *
  548. * Tries to find a free spu to run @ctx. If no free spu is available
  549. * add the context to the runqueue so it gets woken up once an spu
  550. * is available.
  551. */
  552. int spu_activate(struct spu_context *ctx, unsigned long flags)
  553. {
  554. do {
  555. struct spu *spu;
  556. /*
  557. * If there are multiple threads waiting for a single context
  558. * only one actually binds the context while the others will
  559. * only be able to acquire the state_mutex once the context
  560. * already is in runnable state.
  561. */
  562. if (ctx->spu)
  563. return 0;
  564. spu = spu_get_idle(ctx);
  565. /*
  566. * If this is a realtime thread we try to get it running by
  567. * preempting a lower priority thread.
  568. */
  569. if (!spu && rt_prio(ctx->prio))
  570. spu = find_victim(ctx);
  571. if (spu) {
  572. int node = spu->node;
  573. mutex_lock(&cbe_spu_info[node].list_mutex);
  574. spu_bind_context(spu, ctx);
  575. cbe_spu_info[node].nr_active++;
  576. mutex_unlock(&cbe_spu_info[node].list_mutex);
  577. return 0;
  578. }
  579. spu_prio_wait(ctx);
  580. } while (!signal_pending(current));
  581. return -ERESTARTSYS;
  582. }
  583. /**
  584. * grab_runnable_context - try to find a runnable context
  585. *
  586. * Remove the highest priority context on the runqueue and return it
  587. * to the caller. Returns %NULL if no runnable context was found.
  588. */
  589. static struct spu_context *grab_runnable_context(int prio, int node)
  590. {
  591. struct spu_context *ctx;
  592. int best;
  593. spin_lock(&spu_prio->runq_lock);
  594. best = find_first_bit(spu_prio->bitmap, prio);
  595. while (best < prio) {
  596. struct list_head *rq = &spu_prio->runq[best];
  597. list_for_each_entry(ctx, rq, rq) {
  598. /* XXX(hch): check for affinity here aswell */
  599. if (__node_allowed(ctx, node)) {
  600. __spu_del_from_rq(ctx);
  601. goto found;
  602. }
  603. }
  604. best++;
  605. }
  606. ctx = NULL;
  607. found:
  608. spin_unlock(&spu_prio->runq_lock);
  609. return ctx;
  610. }
  611. static int __spu_deactivate(struct spu_context *ctx, int force, int max_prio)
  612. {
  613. struct spu *spu = ctx->spu;
  614. struct spu_context *new = NULL;
  615. if (spu) {
  616. new = grab_runnable_context(max_prio, spu->node);
  617. if (new || force) {
  618. int node = spu->node;
  619. mutex_lock(&cbe_spu_info[node].list_mutex);
  620. spu_unbind_context(spu, ctx);
  621. spu->alloc_state = SPU_FREE;
  622. cbe_spu_info[node].nr_active--;
  623. mutex_unlock(&cbe_spu_info[node].list_mutex);
  624. ctx->stats.vol_ctx_switch++;
  625. spu->stats.vol_ctx_switch++;
  626. if (new)
  627. wake_up(&new->stop_wq);
  628. }
  629. }
  630. return new != NULL;
  631. }
  632. /**
  633. * spu_deactivate - unbind a context from it's physical spu
  634. * @ctx: spu context to unbind
  635. *
  636. * Unbind @ctx from the physical spu it is running on and schedule
  637. * the highest priority context to run on the freed physical spu.
  638. */
  639. void spu_deactivate(struct spu_context *ctx)
  640. {
  641. __spu_deactivate(ctx, 1, MAX_PRIO);
  642. }
  643. /**
  644. * spu_yield - yield a physical spu if others are waiting
  645. * @ctx: spu context to yield
  646. *
  647. * Check if there is a higher priority context waiting and if yes
  648. * unbind @ctx from the physical spu and schedule the highest
  649. * priority context to run on the freed physical spu instead.
  650. */
  651. void spu_yield(struct spu_context *ctx)
  652. {
  653. if (!(ctx->flags & SPU_CREATE_NOSCHED)) {
  654. mutex_lock(&ctx->state_mutex);
  655. __spu_deactivate(ctx, 0, MAX_PRIO);
  656. mutex_unlock(&ctx->state_mutex);
  657. }
  658. }
  659. static noinline void spusched_tick(struct spu_context *ctx)
  660. {
  661. if (ctx->flags & SPU_CREATE_NOSCHED)
  662. return;
  663. if (ctx->policy == SCHED_FIFO)
  664. return;
  665. if (--ctx->time_slice)
  666. return;
  667. /*
  668. * Unfortunately list_mutex ranks outside of state_mutex, so
  669. * we have to trylock here. If we fail give the context another
  670. * tick and try again.
  671. */
  672. if (mutex_trylock(&ctx->state_mutex)) {
  673. struct spu *spu = ctx->spu;
  674. struct spu_context *new;
  675. new = grab_runnable_context(ctx->prio + 1, spu->node);
  676. if (new) {
  677. spu_unbind_context(spu, ctx);
  678. ctx->stats.invol_ctx_switch++;
  679. spu->stats.invol_ctx_switch++;
  680. spu->alloc_state = SPU_FREE;
  681. cbe_spu_info[spu->node].nr_active--;
  682. wake_up(&new->stop_wq);
  683. /*
  684. * We need to break out of the wait loop in
  685. * spu_run manually to ensure this context
  686. * gets put on the runqueue again ASAP.
  687. */
  688. wake_up(&ctx->stop_wq);
  689. }
  690. spu_set_timeslice(ctx);
  691. mutex_unlock(&ctx->state_mutex);
  692. } else {
  693. ctx->time_slice++;
  694. }
  695. }
  696. /**
  697. * count_active_contexts - count nr of active tasks
  698. *
  699. * Return the number of tasks currently running or waiting to run.
  700. *
  701. * Note that we don't take runq_lock / list_mutex here. Reading
  702. * a single 32bit value is atomic on powerpc, and we don't care
  703. * about memory ordering issues here.
  704. */
  705. static unsigned long count_active_contexts(void)
  706. {
  707. int nr_active = 0, node;
  708. for (node = 0; node < MAX_NUMNODES; node++)
  709. nr_active += cbe_spu_info[node].nr_active;
  710. nr_active += spu_prio->nr_waiting;
  711. return nr_active;
  712. }
  713. /**
  714. * spu_calc_load - given tick count, update the avenrun load estimates.
  715. * @tick: tick count
  716. *
  717. * No locking against reading these values from userspace, as for
  718. * the CPU loadavg code.
  719. */
  720. static void spu_calc_load(unsigned long ticks)
  721. {
  722. unsigned long active_tasks; /* fixed-point */
  723. static int count = LOAD_FREQ;
  724. count -= ticks;
  725. if (unlikely(count < 0)) {
  726. active_tasks = count_active_contexts() * FIXED_1;
  727. do {
  728. CALC_LOAD(spu_avenrun[0], EXP_1, active_tasks);
  729. CALC_LOAD(spu_avenrun[1], EXP_5, active_tasks);
  730. CALC_LOAD(spu_avenrun[2], EXP_15, active_tasks);
  731. count += LOAD_FREQ;
  732. } while (count < 0);
  733. }
  734. }
  735. static void spusched_wake(unsigned long data)
  736. {
  737. mod_timer(&spusched_timer, jiffies + SPUSCHED_TICK);
  738. wake_up_process(spusched_task);
  739. spu_calc_load(SPUSCHED_TICK);
  740. }
  741. static int spusched_thread(void *unused)
  742. {
  743. struct spu *spu;
  744. int node;
  745. while (!kthread_should_stop()) {
  746. set_current_state(TASK_INTERRUPTIBLE);
  747. schedule();
  748. for (node = 0; node < MAX_NUMNODES; node++) {
  749. mutex_lock(&cbe_spu_info[node].list_mutex);
  750. list_for_each_entry(spu, &cbe_spu_info[node].spus, cbe_list)
  751. if (spu->ctx)
  752. spusched_tick(spu->ctx);
  753. mutex_unlock(&cbe_spu_info[node].list_mutex);
  754. }
  755. }
  756. return 0;
  757. }
  758. #define LOAD_INT(x) ((x) >> FSHIFT)
  759. #define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
  760. static int show_spu_loadavg(struct seq_file *s, void *private)
  761. {
  762. int a, b, c;
  763. a = spu_avenrun[0] + (FIXED_1/200);
  764. b = spu_avenrun[1] + (FIXED_1/200);
  765. c = spu_avenrun[2] + (FIXED_1/200);
  766. /*
  767. * Note that last_pid doesn't really make much sense for the
  768. * SPU loadavg (it even seems very odd on the CPU side..),
  769. * but we include it here to have a 100% compatible interface.
  770. */
  771. seq_printf(s, "%d.%02d %d.%02d %d.%02d %ld/%d %d\n",
  772. LOAD_INT(a), LOAD_FRAC(a),
  773. LOAD_INT(b), LOAD_FRAC(b),
  774. LOAD_INT(c), LOAD_FRAC(c),
  775. count_active_contexts(),
  776. atomic_read(&nr_spu_contexts),
  777. current->nsproxy->pid_ns->last_pid);
  778. return 0;
  779. }
  780. static int spu_loadavg_open(struct inode *inode, struct file *file)
  781. {
  782. return single_open(file, show_spu_loadavg, NULL);
  783. }
  784. static const struct file_operations spu_loadavg_fops = {
  785. .open = spu_loadavg_open,
  786. .read = seq_read,
  787. .llseek = seq_lseek,
  788. .release = single_release,
  789. };
  790. int __init spu_sched_init(void)
  791. {
  792. struct proc_dir_entry *entry;
  793. int err = -ENOMEM, i;
  794. spu_prio = kzalloc(sizeof(struct spu_prio_array), GFP_KERNEL);
  795. if (!spu_prio)
  796. goto out;
  797. for (i = 0; i < MAX_PRIO; i++) {
  798. INIT_LIST_HEAD(&spu_prio->runq[i]);
  799. __clear_bit(i, spu_prio->bitmap);
  800. }
  801. spin_lock_init(&spu_prio->runq_lock);
  802. setup_timer(&spusched_timer, spusched_wake, 0);
  803. spusched_task = kthread_run(spusched_thread, NULL, "spusched");
  804. if (IS_ERR(spusched_task)) {
  805. err = PTR_ERR(spusched_task);
  806. goto out_free_spu_prio;
  807. }
  808. entry = create_proc_entry("spu_loadavg", 0, NULL);
  809. if (!entry)
  810. goto out_stop_kthread;
  811. entry->proc_fops = &spu_loadavg_fops;
  812. pr_debug("spusched: tick: %d, min ticks: %d, default ticks: %d\n",
  813. SPUSCHED_TICK, MIN_SPU_TIMESLICE, DEF_SPU_TIMESLICE);
  814. return 0;
  815. out_stop_kthread:
  816. kthread_stop(spusched_task);
  817. out_free_spu_prio:
  818. kfree(spu_prio);
  819. out:
  820. return err;
  821. }
  822. void spu_sched_exit(void)
  823. {
  824. struct spu *spu;
  825. int node;
  826. remove_proc_entry("spu_loadavg", NULL);
  827. del_timer_sync(&spusched_timer);
  828. kthread_stop(spusched_task);
  829. for (node = 0; node < MAX_NUMNODES; node++) {
  830. mutex_lock(&cbe_spu_info[node].list_mutex);
  831. list_for_each_entry(spu, &cbe_spu_info[node].spus, cbe_list)
  832. if (spu->alloc_state != SPU_FREE)
  833. spu->alloc_state = SPU_FREE;
  834. mutex_unlock(&cbe_spu_info[node].list_mutex);
  835. }
  836. kfree(spu_prio);
  837. }