sched.c 13 KB

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