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