sched.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  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 1 spu scheduler tick, whichever is
  66. * larger), default timeslice is 100 msecs, maximum timeslice is 800 msecs.
  67. */
  68. #define MIN_SPU_TIMESLICE max(5 * HZ / (1000 * SPUSCHED_TICK), 1)
  69. #define DEF_SPU_TIMESLICE (100 * HZ / (1000 * SPUSCHED_TICK))
  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. /*
  89. * Update scheduling information from the owning thread.
  90. */
  91. void __spu_update_sched_info(struct spu_context *ctx)
  92. {
  93. /*
  94. * We do our own priority calculations, so we normally want
  95. * ->static_prio to start with. Unfortunately thies field
  96. * contains junk for threads with a realtime scheduling
  97. * policy so we have to look at ->prio in this case.
  98. */
  99. if (rt_prio(current->prio))
  100. ctx->prio = current->prio;
  101. else
  102. ctx->prio = current->static_prio;
  103. ctx->policy = current->policy;
  104. /*
  105. * A lot of places that don't hold active_mutex poke into
  106. * cpus_allowed, including grab_runnable_context which
  107. * already holds the runq_lock. So abuse runq_lock
  108. * to protect this field aswell.
  109. */
  110. spin_lock(&spu_prio->runq_lock);
  111. ctx->cpus_allowed = current->cpus_allowed;
  112. spin_unlock(&spu_prio->runq_lock);
  113. }
  114. void spu_update_sched_info(struct spu_context *ctx)
  115. {
  116. int node = ctx->spu->node;
  117. mutex_lock(&spu_prio->active_mutex[node]);
  118. __spu_update_sched_info(ctx);
  119. mutex_unlock(&spu_prio->active_mutex[node]);
  120. }
  121. static int __node_allowed(struct spu_context *ctx, int node)
  122. {
  123. if (nr_cpus_node(node)) {
  124. cpumask_t mask = node_to_cpumask(node);
  125. if (cpus_intersects(mask, ctx->cpus_allowed))
  126. return 1;
  127. }
  128. return 0;
  129. }
  130. static int node_allowed(struct spu_context *ctx, int node)
  131. {
  132. int rval;
  133. spin_lock(&spu_prio->runq_lock);
  134. rval = __node_allowed(ctx, node);
  135. spin_unlock(&spu_prio->runq_lock);
  136. return rval;
  137. }
  138. /**
  139. * spu_add_to_active_list - add spu to active list
  140. * @spu: spu to add to the active list
  141. */
  142. static void spu_add_to_active_list(struct spu *spu)
  143. {
  144. mutex_lock(&spu_prio->active_mutex[spu->node]);
  145. list_add_tail(&spu->list, &spu_prio->active_list[spu->node]);
  146. mutex_unlock(&spu_prio->active_mutex[spu->node]);
  147. }
  148. static void __spu_remove_from_active_list(struct spu *spu)
  149. {
  150. list_del_init(&spu->list);
  151. }
  152. /**
  153. * spu_remove_from_active_list - remove spu from active list
  154. * @spu: spu to remove from the active list
  155. */
  156. static void spu_remove_from_active_list(struct spu *spu)
  157. {
  158. int node = spu->node;
  159. mutex_lock(&spu_prio->active_mutex[node]);
  160. __spu_remove_from_active_list(spu);
  161. mutex_unlock(&spu_prio->active_mutex[node]);
  162. }
  163. static BLOCKING_NOTIFIER_HEAD(spu_switch_notifier);
  164. static void spu_switch_notify(struct spu *spu, struct spu_context *ctx)
  165. {
  166. blocking_notifier_call_chain(&spu_switch_notifier,
  167. ctx ? ctx->object_id : 0, spu);
  168. }
  169. int spu_switch_event_register(struct notifier_block * n)
  170. {
  171. return blocking_notifier_chain_register(&spu_switch_notifier, n);
  172. }
  173. int spu_switch_event_unregister(struct notifier_block * n)
  174. {
  175. return blocking_notifier_chain_unregister(&spu_switch_notifier, n);
  176. }
  177. /**
  178. * spu_bind_context - bind spu context to physical spu
  179. * @spu: physical spu to bind to
  180. * @ctx: context to bind
  181. */
  182. static void spu_bind_context(struct spu *spu, struct spu_context *ctx)
  183. {
  184. pr_debug("%s: pid=%d SPU=%d NODE=%d\n", __FUNCTION__, current->pid,
  185. spu->number, spu->node);
  186. spu->ctx = ctx;
  187. spu->flags = 0;
  188. ctx->spu = spu;
  189. ctx->ops = &spu_hw_ops;
  190. spu->pid = current->pid;
  191. spu_associate_mm(spu, ctx->owner);
  192. spu->ibox_callback = spufs_ibox_callback;
  193. spu->wbox_callback = spufs_wbox_callback;
  194. spu->stop_callback = spufs_stop_callback;
  195. spu->mfc_callback = spufs_mfc_callback;
  196. spu->dma_callback = spufs_dma_callback;
  197. mb();
  198. spu_unmap_mappings(ctx);
  199. spu_restore(&ctx->csa, spu);
  200. spu->timestamp = jiffies;
  201. spu_cpu_affinity_set(spu, raw_smp_processor_id());
  202. spu_switch_notify(spu, ctx);
  203. ctx->state = SPU_STATE_RUNNABLE;
  204. }
  205. /**
  206. * spu_unbind_context - unbind spu context from physical spu
  207. * @spu: physical spu to unbind from
  208. * @ctx: context to unbind
  209. */
  210. static void spu_unbind_context(struct spu *spu, struct spu_context *ctx)
  211. {
  212. pr_debug("%s: unbind pid=%d SPU=%d NODE=%d\n", __FUNCTION__,
  213. spu->pid, spu->number, spu->node);
  214. spu_switch_notify(spu, NULL);
  215. spu_unmap_mappings(ctx);
  216. spu_save(&ctx->csa, spu);
  217. spu->timestamp = jiffies;
  218. ctx->state = SPU_STATE_SAVED;
  219. spu->ibox_callback = NULL;
  220. spu->wbox_callback = NULL;
  221. spu->stop_callback = NULL;
  222. spu->mfc_callback = NULL;
  223. spu->dma_callback = NULL;
  224. spu_associate_mm(spu, NULL);
  225. spu->pid = 0;
  226. ctx->ops = &spu_backing_ops;
  227. ctx->spu = NULL;
  228. spu->flags = 0;
  229. spu->ctx = NULL;
  230. }
  231. /**
  232. * spu_add_to_rq - add a context to the runqueue
  233. * @ctx: context to add
  234. */
  235. static void __spu_add_to_rq(struct spu_context *ctx)
  236. {
  237. int prio = ctx->prio;
  238. list_add_tail(&ctx->rq, &spu_prio->runq[prio]);
  239. set_bit(prio, spu_prio->bitmap);
  240. }
  241. static void __spu_del_from_rq(struct spu_context *ctx)
  242. {
  243. int prio = ctx->prio;
  244. if (!list_empty(&ctx->rq))
  245. list_del_init(&ctx->rq);
  246. if (list_empty(&spu_prio->runq[prio]))
  247. clear_bit(prio, spu_prio->bitmap);
  248. }
  249. static void spu_prio_wait(struct spu_context *ctx)
  250. {
  251. DEFINE_WAIT(wait);
  252. spin_lock(&spu_prio->runq_lock);
  253. prepare_to_wait_exclusive(&ctx->stop_wq, &wait, TASK_INTERRUPTIBLE);
  254. if (!signal_pending(current)) {
  255. __spu_add_to_rq(ctx);
  256. spin_unlock(&spu_prio->runq_lock);
  257. mutex_unlock(&ctx->state_mutex);
  258. schedule();
  259. mutex_lock(&ctx->state_mutex);
  260. spin_lock(&spu_prio->runq_lock);
  261. __spu_del_from_rq(ctx);
  262. }
  263. spin_unlock(&spu_prio->runq_lock);
  264. __set_current_state(TASK_RUNNING);
  265. remove_wait_queue(&ctx->stop_wq, &wait);
  266. }
  267. static struct spu *spu_get_idle(struct spu_context *ctx)
  268. {
  269. struct spu *spu = NULL;
  270. int node = cpu_to_node(raw_smp_processor_id());
  271. int n;
  272. for (n = 0; n < MAX_NUMNODES; n++, node++) {
  273. node = (node < MAX_NUMNODES) ? node : 0;
  274. if (!node_allowed(ctx, node))
  275. continue;
  276. spu = spu_alloc_node(node);
  277. if (spu)
  278. break;
  279. }
  280. return spu;
  281. }
  282. /**
  283. * find_victim - find a lower priority context to preempt
  284. * @ctx: canidate context for running
  285. *
  286. * Returns the freed physical spu to run the new context on.
  287. */
  288. static struct spu *find_victim(struct spu_context *ctx)
  289. {
  290. struct spu_context *victim = NULL;
  291. struct spu *spu;
  292. int node, n;
  293. /*
  294. * Look for a possible preemption candidate on the local node first.
  295. * If there is no candidate look at the other nodes. This isn't
  296. * exactly fair, but so far the whole spu schedule tries to keep
  297. * a strong node affinity. We might want to fine-tune this in
  298. * the future.
  299. */
  300. restart:
  301. node = cpu_to_node(raw_smp_processor_id());
  302. for (n = 0; n < MAX_NUMNODES; n++, node++) {
  303. node = (node < MAX_NUMNODES) ? node : 0;
  304. if (!node_allowed(ctx, node))
  305. continue;
  306. mutex_lock(&spu_prio->active_mutex[node]);
  307. list_for_each_entry(spu, &spu_prio->active_list[node], list) {
  308. struct spu_context *tmp = spu->ctx;
  309. if (tmp->prio > ctx->prio &&
  310. (!victim || tmp->prio > victim->prio))
  311. victim = spu->ctx;
  312. }
  313. mutex_unlock(&spu_prio->active_mutex[node]);
  314. if (victim) {
  315. /*
  316. * This nests ctx->state_mutex, but we always lock
  317. * higher priority contexts before lower priority
  318. * ones, so this is safe until we introduce
  319. * priority inheritance schemes.
  320. */
  321. if (!mutex_trylock(&victim->state_mutex)) {
  322. victim = NULL;
  323. goto restart;
  324. }
  325. spu = victim->spu;
  326. if (!spu) {
  327. /*
  328. * This race can happen because we've dropped
  329. * the active list mutex. No a problem, just
  330. * restart the search.
  331. */
  332. mutex_unlock(&victim->state_mutex);
  333. victim = NULL;
  334. goto restart;
  335. }
  336. spu_remove_from_active_list(spu);
  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 && rt_prio(ctx->prio))
  371. spu = find_victim(ctx);
  372. if (spu) {
  373. spu_bind_context(spu, ctx);
  374. spu_add_to_active_list(spu);
  375. return 0;
  376. }
  377. spu_prio_wait(ctx);
  378. } while (!signal_pending(current));
  379. return -ERESTARTSYS;
  380. }
  381. /**
  382. * grab_runnable_context - try to find a runnable context
  383. *
  384. * Remove the highest priority context on the runqueue and return it
  385. * to the caller. Returns %NULL if no runnable context was found.
  386. */
  387. static struct spu_context *grab_runnable_context(int prio, int node)
  388. {
  389. struct spu_context *ctx;
  390. int best;
  391. spin_lock(&spu_prio->runq_lock);
  392. best = sched_find_first_bit(spu_prio->bitmap);
  393. while (best < prio) {
  394. struct list_head *rq = &spu_prio->runq[best];
  395. list_for_each_entry(ctx, rq, rq) {
  396. /* XXX(hch): check for affinity here aswell */
  397. if (__node_allowed(ctx, node)) {
  398. __spu_del_from_rq(ctx);
  399. goto found;
  400. }
  401. }
  402. best++;
  403. }
  404. ctx = NULL;
  405. found:
  406. spin_unlock(&spu_prio->runq_lock);
  407. return ctx;
  408. }
  409. static int __spu_deactivate(struct spu_context *ctx, int force, int max_prio)
  410. {
  411. struct spu *spu = ctx->spu;
  412. struct spu_context *new = NULL;
  413. if (spu) {
  414. new = grab_runnable_context(max_prio, spu->node);
  415. if (new || force) {
  416. spu_remove_from_active_list(spu);
  417. spu_unbind_context(spu, ctx);
  418. spu_free(spu);
  419. if (new)
  420. wake_up(&new->stop_wq);
  421. }
  422. }
  423. return new != NULL;
  424. }
  425. /**
  426. * spu_deactivate - unbind a context from it's physical spu
  427. * @ctx: spu context to unbind
  428. *
  429. * Unbind @ctx from the physical spu it is running on and schedule
  430. * the highest priority context to run on the freed physical spu.
  431. */
  432. void spu_deactivate(struct spu_context *ctx)
  433. {
  434. /*
  435. * We must never reach this for a nosched context,
  436. * but handle the case gracefull instead of panicing.
  437. */
  438. if (ctx->flags & SPU_CREATE_NOSCHED) {
  439. WARN_ON(1);
  440. return;
  441. }
  442. __spu_deactivate(ctx, 1, MAX_PRIO);
  443. }
  444. /**
  445. * spu_yield - yield a physical spu if others are waiting
  446. * @ctx: spu context to yield
  447. *
  448. * Check if there is a higher priority context waiting and if yes
  449. * unbind @ctx from the physical spu and schedule the highest
  450. * priority context to run on the freed physical spu instead.
  451. */
  452. void spu_yield(struct spu_context *ctx)
  453. {
  454. if (!(ctx->flags & SPU_CREATE_NOSCHED)) {
  455. mutex_lock(&ctx->state_mutex);
  456. __spu_deactivate(ctx, 0, MAX_PRIO);
  457. mutex_unlock(&ctx->state_mutex);
  458. }
  459. }
  460. static void spusched_tick(struct spu_context *ctx)
  461. {
  462. if (ctx->policy == SCHED_FIFO || --ctx->time_slice)
  463. return;
  464. /*
  465. * Unfortunately active_mutex ranks outside of state_mutex, so
  466. * we have to trylock here. If we fail give the context another
  467. * tick and try again.
  468. */
  469. if (mutex_trylock(&ctx->state_mutex)) {
  470. struct spu *spu = ctx->spu;
  471. struct spu_context *new;
  472. new = grab_runnable_context(ctx->prio + 1, spu->node);
  473. if (new) {
  474. __spu_remove_from_active_list(spu);
  475. spu_unbind_context(spu, ctx);
  476. spu_free(spu);
  477. wake_up(&new->stop_wq);
  478. /*
  479. * We need to break out of the wait loop in
  480. * spu_run manually to ensure this context
  481. * gets put on the runqueue again ASAP.
  482. */
  483. wake_up(&ctx->stop_wq);
  484. }
  485. spu_set_timeslice(ctx);
  486. mutex_unlock(&ctx->state_mutex);
  487. } else {
  488. ctx->time_slice++;
  489. }
  490. }
  491. static void spusched_wake(unsigned long data)
  492. {
  493. mod_timer(&spusched_timer, jiffies + SPUSCHED_TICK);
  494. wake_up_process(spusched_task);
  495. }
  496. static int spusched_thread(void *unused)
  497. {
  498. struct spu *spu, *next;
  499. int node;
  500. setup_timer(&spusched_timer, spusched_wake, 0);
  501. __mod_timer(&spusched_timer, jiffies + SPUSCHED_TICK);
  502. while (!kthread_should_stop()) {
  503. set_current_state(TASK_INTERRUPTIBLE);
  504. schedule();
  505. for (node = 0; node < MAX_NUMNODES; node++) {
  506. mutex_lock(&spu_prio->active_mutex[node]);
  507. list_for_each_entry_safe(spu, next,
  508. &spu_prio->active_list[node],
  509. list)
  510. spusched_tick(spu->ctx);
  511. mutex_unlock(&spu_prio->active_mutex[node]);
  512. }
  513. }
  514. del_timer_sync(&spusched_timer);
  515. return 0;
  516. }
  517. int __init spu_sched_init(void)
  518. {
  519. int i;
  520. spu_prio = kzalloc(sizeof(struct spu_prio_array), GFP_KERNEL);
  521. if (!spu_prio)
  522. return -ENOMEM;
  523. for (i = 0; i < MAX_PRIO; i++) {
  524. INIT_LIST_HEAD(&spu_prio->runq[i]);
  525. __clear_bit(i, spu_prio->bitmap);
  526. }
  527. __set_bit(MAX_PRIO, spu_prio->bitmap);
  528. for (i = 0; i < MAX_NUMNODES; i++) {
  529. mutex_init(&spu_prio->active_mutex[i]);
  530. INIT_LIST_HEAD(&spu_prio->active_list[i]);
  531. }
  532. spin_lock_init(&spu_prio->runq_lock);
  533. spusched_task = kthread_run(spusched_thread, NULL, "spusched");
  534. if (IS_ERR(spusched_task)) {
  535. kfree(spu_prio);
  536. return PTR_ERR(spusched_task);
  537. }
  538. pr_debug("spusched: tick: %d, min ticks: %d, default ticks: %d\n",
  539. SPUSCHED_TICK, MIN_SPU_TIMESLICE, DEF_SPU_TIMESLICE);
  540. return 0;
  541. }
  542. void __exit spu_sched_exit(void)
  543. {
  544. struct spu *spu, *tmp;
  545. int node;
  546. kthread_stop(spusched_task);
  547. for (node = 0; node < MAX_NUMNODES; node++) {
  548. mutex_lock(&spu_prio->active_mutex[node]);
  549. list_for_each_entry_safe(spu, tmp, &spu_prio->active_list[node],
  550. list) {
  551. list_del_init(&spu->list);
  552. spu_free(spu);
  553. }
  554. mutex_unlock(&spu_prio->active_mutex[node]);
  555. }
  556. kfree(spu_prio);
  557. }