sched.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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 <asm/io.h>
  37. #include <asm/mmu_context.h>
  38. #include <asm/spu.h>
  39. #include <asm/spu_csa.h>
  40. #include <asm/spu_priv1.h>
  41. #include "spufs.h"
  42. #define SPU_TIMESLICE (HZ)
  43. struct spu_prio_array {
  44. DECLARE_BITMAP(bitmap, MAX_PRIO);
  45. struct list_head runq[MAX_PRIO];
  46. spinlock_t runq_lock;
  47. struct list_head active_list[MAX_NUMNODES];
  48. struct mutex active_mutex[MAX_NUMNODES];
  49. };
  50. static struct spu_prio_array *spu_prio;
  51. static struct workqueue_struct *spu_sched_wq;
  52. static inline int node_allowed(int node)
  53. {
  54. cpumask_t mask;
  55. if (!nr_cpus_node(node))
  56. return 0;
  57. mask = node_to_cpumask(node);
  58. if (!cpus_intersects(mask, current->cpus_allowed))
  59. return 0;
  60. return 1;
  61. }
  62. void spu_start_tick(struct spu_context *ctx)
  63. {
  64. if (ctx->policy == SCHED_RR) {
  65. /*
  66. * Make sure the exiting bit is cleared.
  67. */
  68. clear_bit(SPU_SCHED_EXITING, &ctx->sched_flags);
  69. mb();
  70. queue_delayed_work(spu_sched_wq, &ctx->sched_work, SPU_TIMESLICE);
  71. }
  72. }
  73. void spu_stop_tick(struct spu_context *ctx)
  74. {
  75. if (ctx->policy == SCHED_RR) {
  76. /*
  77. * While the work can be rearming normally setting this flag
  78. * makes sure it does not rearm itself anymore.
  79. */
  80. set_bit(SPU_SCHED_EXITING, &ctx->sched_flags);
  81. mb();
  82. cancel_delayed_work(&ctx->sched_work);
  83. }
  84. }
  85. void spu_sched_tick(struct work_struct *work)
  86. {
  87. struct spu_context *ctx =
  88. container_of(work, struct spu_context, sched_work.work);
  89. struct spu *spu;
  90. int preempted = 0;
  91. /*
  92. * If this context is being stopped avoid rescheduling from the
  93. * scheduler tick because we would block on the state_mutex.
  94. * The caller will yield the spu later on anyway.
  95. */
  96. if (test_bit(SPU_SCHED_EXITING, &ctx->sched_flags))
  97. return;
  98. mutex_lock(&ctx->state_mutex);
  99. spu = ctx->spu;
  100. if (spu) {
  101. int best = sched_find_first_bit(spu_prio->bitmap);
  102. if (best <= ctx->prio) {
  103. spu_deactivate(ctx);
  104. preempted = 1;
  105. }
  106. }
  107. mutex_unlock(&ctx->state_mutex);
  108. if (preempted) {
  109. /*
  110. * We need to break out of the wait loop in spu_run manually
  111. * to ensure this context gets put on the runqueue again
  112. * ASAP.
  113. */
  114. wake_up(&ctx->stop_wq);
  115. } else
  116. spu_start_tick(ctx);
  117. }
  118. /**
  119. * spu_add_to_active_list - add spu to active list
  120. * @spu: spu to add to the active list
  121. */
  122. static void spu_add_to_active_list(struct spu *spu)
  123. {
  124. mutex_lock(&spu_prio->active_mutex[spu->node]);
  125. list_add_tail(&spu->list, &spu_prio->active_list[spu->node]);
  126. mutex_unlock(&spu_prio->active_mutex[spu->node]);
  127. }
  128. /**
  129. * spu_remove_from_active_list - remove spu from active list
  130. * @spu: spu to remove from the active list
  131. */
  132. static void spu_remove_from_active_list(struct spu *spu)
  133. {
  134. int node = spu->node;
  135. mutex_lock(&spu_prio->active_mutex[node]);
  136. list_del_init(&spu->list);
  137. mutex_unlock(&spu_prio->active_mutex[node]);
  138. }
  139. static BLOCKING_NOTIFIER_HEAD(spu_switch_notifier);
  140. static void spu_switch_notify(struct spu *spu, struct spu_context *ctx)
  141. {
  142. blocking_notifier_call_chain(&spu_switch_notifier,
  143. ctx ? ctx->object_id : 0, spu);
  144. }
  145. int spu_switch_event_register(struct notifier_block * n)
  146. {
  147. return blocking_notifier_chain_register(&spu_switch_notifier, n);
  148. }
  149. int spu_switch_event_unregister(struct notifier_block * n)
  150. {
  151. return blocking_notifier_chain_unregister(&spu_switch_notifier, n);
  152. }
  153. /**
  154. * spu_bind_context - bind spu context to physical spu
  155. * @spu: physical spu to bind to
  156. * @ctx: context to bind
  157. */
  158. static void spu_bind_context(struct spu *spu, struct spu_context *ctx)
  159. {
  160. pr_debug("%s: pid=%d SPU=%d NODE=%d\n", __FUNCTION__, current->pid,
  161. spu->number, spu->node);
  162. spu->ctx = ctx;
  163. spu->flags = 0;
  164. ctx->spu = spu;
  165. ctx->ops = &spu_hw_ops;
  166. spu->pid = current->pid;
  167. spu_associate_mm(spu, ctx->owner);
  168. spu->ibox_callback = spufs_ibox_callback;
  169. spu->wbox_callback = spufs_wbox_callback;
  170. spu->stop_callback = spufs_stop_callback;
  171. spu->mfc_callback = spufs_mfc_callback;
  172. spu->dma_callback = spufs_dma_callback;
  173. mb();
  174. spu_unmap_mappings(ctx);
  175. spu_restore(&ctx->csa, spu);
  176. spu->timestamp = jiffies;
  177. spu_cpu_affinity_set(spu, raw_smp_processor_id());
  178. spu_switch_notify(spu, ctx);
  179. spu_add_to_active_list(spu);
  180. ctx->state = SPU_STATE_RUNNABLE;
  181. }
  182. /**
  183. * spu_unbind_context - unbind spu context from physical spu
  184. * @spu: physical spu to unbind from
  185. * @ctx: context to unbind
  186. */
  187. static void spu_unbind_context(struct spu *spu, struct spu_context *ctx)
  188. {
  189. pr_debug("%s: unbind pid=%d SPU=%d NODE=%d\n", __FUNCTION__,
  190. spu->pid, spu->number, spu->node);
  191. spu_remove_from_active_list(spu);
  192. spu_switch_notify(spu, NULL);
  193. spu_unmap_mappings(ctx);
  194. spu_save(&ctx->csa, spu);
  195. spu->timestamp = jiffies;
  196. ctx->state = SPU_STATE_SAVED;
  197. spu->ibox_callback = NULL;
  198. spu->wbox_callback = NULL;
  199. spu->stop_callback = NULL;
  200. spu->mfc_callback = NULL;
  201. spu->dma_callback = NULL;
  202. spu_associate_mm(spu, NULL);
  203. spu->pid = 0;
  204. ctx->ops = &spu_backing_ops;
  205. ctx->spu = NULL;
  206. spu->flags = 0;
  207. spu->ctx = NULL;
  208. }
  209. /**
  210. * spu_add_to_rq - add a context to the runqueue
  211. * @ctx: context to add
  212. */
  213. static void __spu_add_to_rq(struct spu_context *ctx)
  214. {
  215. int prio = ctx->prio;
  216. list_add_tail(&ctx->rq, &spu_prio->runq[prio]);
  217. set_bit(prio, spu_prio->bitmap);
  218. }
  219. static void __spu_del_from_rq(struct spu_context *ctx)
  220. {
  221. int prio = ctx->prio;
  222. if (!list_empty(&ctx->rq))
  223. list_del_init(&ctx->rq);
  224. if (list_empty(&spu_prio->runq[prio]))
  225. clear_bit(prio, spu_prio->bitmap);
  226. }
  227. static void spu_prio_wait(struct spu_context *ctx)
  228. {
  229. DEFINE_WAIT(wait);
  230. spin_lock(&spu_prio->runq_lock);
  231. prepare_to_wait_exclusive(&ctx->stop_wq, &wait, TASK_INTERRUPTIBLE);
  232. if (!signal_pending(current)) {
  233. __spu_add_to_rq(ctx);
  234. spin_unlock(&spu_prio->runq_lock);
  235. mutex_unlock(&ctx->state_mutex);
  236. schedule();
  237. mutex_lock(&ctx->state_mutex);
  238. spin_lock(&spu_prio->runq_lock);
  239. __spu_del_from_rq(ctx);
  240. }
  241. spin_unlock(&spu_prio->runq_lock);
  242. __set_current_state(TASK_RUNNING);
  243. remove_wait_queue(&ctx->stop_wq, &wait);
  244. }
  245. /**
  246. * spu_reschedule - try to find a runnable context for a spu
  247. * @spu: spu available
  248. *
  249. * This function is called whenever a spu becomes idle. It looks for the
  250. * most suitable runnable spu context and schedules it for execution.
  251. */
  252. static void spu_reschedule(struct spu *spu)
  253. {
  254. int best;
  255. spu_free(spu);
  256. spin_lock(&spu_prio->runq_lock);
  257. best = sched_find_first_bit(spu_prio->bitmap);
  258. if (best < MAX_PRIO) {
  259. struct list_head *rq = &spu_prio->runq[best];
  260. struct spu_context *ctx;
  261. BUG_ON(list_empty(rq));
  262. ctx = list_entry(rq->next, struct spu_context, rq);
  263. __spu_del_from_rq(ctx);
  264. wake_up(&ctx->stop_wq);
  265. }
  266. spin_unlock(&spu_prio->runq_lock);
  267. }
  268. static struct spu *spu_get_idle(struct spu_context *ctx)
  269. {
  270. struct spu *spu = NULL;
  271. int node = cpu_to_node(raw_smp_processor_id());
  272. int n;
  273. for (n = 0; n < MAX_NUMNODES; n++, node++) {
  274. node = (node < MAX_NUMNODES) ? node : 0;
  275. if (!node_allowed(node))
  276. continue;
  277. spu = spu_alloc_node(node);
  278. if (spu)
  279. break;
  280. }
  281. return spu;
  282. }
  283. /**
  284. * find_victim - find a lower priority context to preempt
  285. * @ctx: canidate context for running
  286. *
  287. * Returns the freed physical spu to run the new context on.
  288. */
  289. static struct spu *find_victim(struct spu_context *ctx)
  290. {
  291. struct spu_context *victim = NULL;
  292. struct spu *spu;
  293. int node, n;
  294. /*
  295. * Look for a possible preemption candidate on the local node first.
  296. * If there is no candidate look at the other nodes. This isn't
  297. * exactly fair, but so far the whole spu schedule tries to keep
  298. * a strong node affinity. We might want to fine-tune this in
  299. * the future.
  300. */
  301. restart:
  302. node = cpu_to_node(raw_smp_processor_id());
  303. for (n = 0; n < MAX_NUMNODES; n++, node++) {
  304. node = (node < MAX_NUMNODES) ? node : 0;
  305. if (!node_allowed(node))
  306. continue;
  307. mutex_lock(&spu_prio->active_mutex[node]);
  308. list_for_each_entry(spu, &spu_prio->active_list[node], list) {
  309. struct spu_context *tmp = spu->ctx;
  310. if (tmp->rt_priority < ctx->rt_priority &&
  311. (!victim || tmp->rt_priority < victim->rt_priority))
  312. victim = spu->ctx;
  313. }
  314. mutex_unlock(&spu_prio->active_mutex[node]);
  315. if (victim) {
  316. /*
  317. * This nests ctx->state_mutex, but we always lock
  318. * higher priority contexts before lower priority
  319. * ones, so this is safe until we introduce
  320. * priority inheritance schemes.
  321. */
  322. if (!mutex_trylock(&victim->state_mutex)) {
  323. victim = NULL;
  324. goto restart;
  325. }
  326. spu = victim->spu;
  327. if (!spu) {
  328. /*
  329. * This race can happen because we've dropped
  330. * the active list mutex. No a problem, just
  331. * restart the search.
  332. */
  333. mutex_unlock(&victim->state_mutex);
  334. victim = NULL;
  335. goto restart;
  336. }
  337. spu_unbind_context(spu, victim);
  338. mutex_unlock(&victim->state_mutex);
  339. /*
  340. * We need to break out of the wait loop in spu_run
  341. * manually to ensure this context gets put on the
  342. * runqueue again ASAP.
  343. */
  344. wake_up(&victim->stop_wq);
  345. return spu;
  346. }
  347. }
  348. return NULL;
  349. }
  350. /**
  351. * spu_activate - find a free spu for a context and execute it
  352. * @ctx: spu context to schedule
  353. * @flags: flags (currently ignored)
  354. *
  355. * Tries to find a free spu to run @ctx. If no free spu is available
  356. * add the context to the runqueue so it gets woken up once an spu
  357. * is available.
  358. */
  359. int spu_activate(struct spu_context *ctx, unsigned long flags)
  360. {
  361. if (ctx->spu)
  362. return 0;
  363. do {
  364. struct spu *spu;
  365. spu = spu_get_idle(ctx);
  366. /*
  367. * If this is a realtime thread we try to get it running by
  368. * preempting a lower priority thread.
  369. */
  370. if (!spu && ctx->rt_priority)
  371. spu = find_victim(ctx);
  372. if (spu) {
  373. spu_bind_context(spu, ctx);
  374. return 0;
  375. }
  376. spu_prio_wait(ctx);
  377. } while (!signal_pending(current));
  378. return -ERESTARTSYS;
  379. }
  380. /**
  381. * spu_deactivate - unbind a context from it's physical spu
  382. * @ctx: spu context to unbind
  383. *
  384. * Unbind @ctx from the physical spu it is running on and schedule
  385. * the highest priority context to run on the freed physical spu.
  386. */
  387. void spu_deactivate(struct spu_context *ctx)
  388. {
  389. struct spu *spu = ctx->spu;
  390. if (spu) {
  391. spu_unbind_context(spu, ctx);
  392. spu_reschedule(spu);
  393. }
  394. }
  395. /**
  396. * spu_yield - yield a physical spu if others are waiting
  397. * @ctx: spu context to yield
  398. *
  399. * Check if there is a higher priority context waiting and if yes
  400. * unbind @ctx from the physical spu and schedule the highest
  401. * priority context to run on the freed physical spu instead.
  402. */
  403. void spu_yield(struct spu_context *ctx)
  404. {
  405. struct spu *spu;
  406. if (mutex_trylock(&ctx->state_mutex)) {
  407. if ((spu = ctx->spu) != NULL) {
  408. int best = sched_find_first_bit(spu_prio->bitmap);
  409. if (best < MAX_PRIO) {
  410. pr_debug("%s: yielding SPU %d NODE %d\n",
  411. __FUNCTION__, spu->number, spu->node);
  412. spu_deactivate(ctx);
  413. }
  414. }
  415. mutex_unlock(&ctx->state_mutex);
  416. }
  417. }
  418. int __init spu_sched_init(void)
  419. {
  420. int i;
  421. spu_sched_wq = create_singlethread_workqueue("spusched");
  422. if (!spu_sched_wq)
  423. return 1;
  424. spu_prio = kzalloc(sizeof(struct spu_prio_array), GFP_KERNEL);
  425. if (!spu_prio) {
  426. printk(KERN_WARNING "%s: Unable to allocate priority queue.\n",
  427. __FUNCTION__);
  428. destroy_workqueue(spu_sched_wq);
  429. return 1;
  430. }
  431. for (i = 0; i < MAX_PRIO; i++) {
  432. INIT_LIST_HEAD(&spu_prio->runq[i]);
  433. __clear_bit(i, spu_prio->bitmap);
  434. }
  435. __set_bit(MAX_PRIO, spu_prio->bitmap);
  436. for (i = 0; i < MAX_NUMNODES; i++) {
  437. mutex_init(&spu_prio->active_mutex[i]);
  438. INIT_LIST_HEAD(&spu_prio->active_list[i]);
  439. }
  440. spin_lock_init(&spu_prio->runq_lock);
  441. return 0;
  442. }
  443. void __exit spu_sched_exit(void)
  444. {
  445. struct spu *spu, *tmp;
  446. int node;
  447. for (node = 0; node < MAX_NUMNODES; node++) {
  448. mutex_lock(&spu_prio->active_mutex[node]);
  449. list_for_each_entry_safe(spu, tmp, &spu_prio->active_list[node],
  450. list) {
  451. list_del_init(&spu->list);
  452. spu_free(spu);
  453. }
  454. mutex_unlock(&spu_prio->active_mutex[node]);
  455. }
  456. kfree(spu_prio);
  457. destroy_workqueue(spu_sched_wq);
  458. }