sched.c 12 KB

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