sched.c 24 KB

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