sched.c 14 KB

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