sched.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  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 <linux/pid_namespace.h>
  38. #include <linux/proc_fs.h>
  39. #include <linux/seq_file.h>
  40. #include <asm/io.h>
  41. #include <asm/mmu_context.h>
  42. #include <asm/spu.h>
  43. #include <asm/spu_csa.h>
  44. #include <asm/spu_priv1.h>
  45. #include "spufs.h"
  46. struct spu_prio_array {
  47. DECLARE_BITMAP(bitmap, MAX_PRIO);
  48. struct list_head runq[MAX_PRIO];
  49. spinlock_t runq_lock;
  50. struct list_head active_list[MAX_NUMNODES];
  51. struct mutex active_mutex[MAX_NUMNODES];
  52. int nr_active[MAX_NUMNODES];
  53. int nr_waiting;
  54. };
  55. static unsigned long spu_avenrun[3];
  56. static struct spu_prio_array *spu_prio;
  57. static struct task_struct *spusched_task;
  58. static struct timer_list spusched_timer;
  59. /*
  60. * Priority of a normal, non-rt, non-niced'd process (aka nice level 0).
  61. */
  62. #define NORMAL_PRIO 120
  63. /*
  64. * Frequency of the spu scheduler tick. By default we do one SPU scheduler
  65. * tick for every 10 CPU scheduler ticks.
  66. */
  67. #define SPUSCHED_TICK (10)
  68. /*
  69. * These are the 'tuning knobs' of the scheduler:
  70. *
  71. * Minimum timeslice is 5 msecs (or 1 spu scheduler tick, whichever is
  72. * larger), default timeslice is 100 msecs, maximum timeslice is 800 msecs.
  73. */
  74. #define MIN_SPU_TIMESLICE max(5 * HZ / (1000 * SPUSCHED_TICK), 1)
  75. #define DEF_SPU_TIMESLICE (100 * HZ / (1000 * SPUSCHED_TICK))
  76. #define MAX_USER_PRIO (MAX_PRIO - MAX_RT_PRIO)
  77. #define SCALE_PRIO(x, prio) \
  78. max(x * (MAX_PRIO - prio) / (MAX_USER_PRIO / 2), MIN_SPU_TIMESLICE)
  79. /*
  80. * scale user-nice values [ -20 ... 0 ... 19 ] to time slice values:
  81. * [800ms ... 100ms ... 5ms]
  82. *
  83. * The higher a thread's priority, the bigger timeslices
  84. * it gets during one round of execution. But even the lowest
  85. * priority thread gets MIN_TIMESLICE worth of execution time.
  86. */
  87. void spu_set_timeslice(struct spu_context *ctx)
  88. {
  89. if (ctx->prio < NORMAL_PRIO)
  90. ctx->time_slice = SCALE_PRIO(DEF_SPU_TIMESLICE * 4, ctx->prio);
  91. else
  92. ctx->time_slice = SCALE_PRIO(DEF_SPU_TIMESLICE, ctx->prio);
  93. }
  94. /*
  95. * Update scheduling information from the owning thread.
  96. */
  97. void __spu_update_sched_info(struct spu_context *ctx)
  98. {
  99. /*
  100. * 32-Bit assignment are atomic on powerpc, and we don't care about
  101. * memory ordering here because retriving the controlling thread is
  102. * per defintion racy.
  103. */
  104. ctx->tid = current->pid;
  105. /*
  106. * We do our own priority calculations, so we normally want
  107. * ->static_prio to start with. Unfortunately thies field
  108. * contains junk for threads with a realtime scheduling
  109. * policy so we have to look at ->prio in this case.
  110. */
  111. if (rt_prio(current->prio))
  112. ctx->prio = current->prio;
  113. else
  114. ctx->prio = current->static_prio;
  115. ctx->policy = current->policy;
  116. /*
  117. * A lot of places that don't hold active_mutex poke into
  118. * cpus_allowed, including grab_runnable_context which
  119. * already holds the runq_lock. So abuse runq_lock
  120. * to protect this field aswell.
  121. */
  122. spin_lock(&spu_prio->runq_lock);
  123. ctx->cpus_allowed = current->cpus_allowed;
  124. spin_unlock(&spu_prio->runq_lock);
  125. }
  126. void spu_update_sched_info(struct spu_context *ctx)
  127. {
  128. int node = ctx->spu->node;
  129. mutex_lock(&spu_prio->active_mutex[node]);
  130. __spu_update_sched_info(ctx);
  131. mutex_unlock(&spu_prio->active_mutex[node]);
  132. }
  133. static int __node_allowed(struct spu_context *ctx, int node)
  134. {
  135. if (nr_cpus_node(node)) {
  136. cpumask_t mask = node_to_cpumask(node);
  137. if (cpus_intersects(mask, ctx->cpus_allowed))
  138. return 1;
  139. }
  140. return 0;
  141. }
  142. static int node_allowed(struct spu_context *ctx, int node)
  143. {
  144. int rval;
  145. spin_lock(&spu_prio->runq_lock);
  146. rval = __node_allowed(ctx, node);
  147. spin_unlock(&spu_prio->runq_lock);
  148. return rval;
  149. }
  150. /**
  151. * spu_add_to_active_list - add spu to active list
  152. * @spu: spu to add to the active list
  153. */
  154. static void spu_add_to_active_list(struct spu *spu)
  155. {
  156. int node = spu->node;
  157. mutex_lock(&spu_prio->active_mutex[node]);
  158. spu_prio->nr_active[node]++;
  159. list_add_tail(&spu->list, &spu_prio->active_list[node]);
  160. mutex_unlock(&spu_prio->active_mutex[node]);
  161. }
  162. static void __spu_remove_from_active_list(struct spu *spu)
  163. {
  164. list_del_init(&spu->list);
  165. spu_prio->nr_active[spu->node]--;
  166. }
  167. /**
  168. * spu_remove_from_active_list - remove spu from active list
  169. * @spu: spu to remove from the active list
  170. */
  171. static void spu_remove_from_active_list(struct spu *spu)
  172. {
  173. int node = spu->node;
  174. mutex_lock(&spu_prio->active_mutex[node]);
  175. __spu_remove_from_active_list(spu);
  176. mutex_unlock(&spu_prio->active_mutex[node]);
  177. }
  178. static BLOCKING_NOTIFIER_HEAD(spu_switch_notifier);
  179. static void spu_switch_notify(struct spu *spu, struct spu_context *ctx)
  180. {
  181. blocking_notifier_call_chain(&spu_switch_notifier,
  182. ctx ? ctx->object_id : 0, spu);
  183. }
  184. int spu_switch_event_register(struct notifier_block * n)
  185. {
  186. return blocking_notifier_chain_register(&spu_switch_notifier, n);
  187. }
  188. int spu_switch_event_unregister(struct notifier_block * n)
  189. {
  190. return blocking_notifier_chain_unregister(&spu_switch_notifier, n);
  191. }
  192. /**
  193. * spu_bind_context - bind spu context to physical spu
  194. * @spu: physical spu to bind to
  195. * @ctx: context to bind
  196. */
  197. static void spu_bind_context(struct spu *spu, struct spu_context *ctx)
  198. {
  199. pr_debug("%s: pid=%d SPU=%d NODE=%d\n", __FUNCTION__, current->pid,
  200. spu->number, spu->node);
  201. spuctx_switch_state(ctx, SPU_UTIL_SYSTEM);
  202. ctx->stats.slb_flt_base = spu->stats.slb_flt;
  203. ctx->stats.class2_intr_base = spu->stats.class2_intr;
  204. spu->ctx = ctx;
  205. spu->flags = 0;
  206. ctx->spu = spu;
  207. ctx->ops = &spu_hw_ops;
  208. spu->pid = current->pid;
  209. spu_associate_mm(spu, ctx->owner);
  210. spu->ibox_callback = spufs_ibox_callback;
  211. spu->wbox_callback = spufs_wbox_callback;
  212. spu->stop_callback = spufs_stop_callback;
  213. spu->mfc_callback = spufs_mfc_callback;
  214. spu->dma_callback = spufs_dma_callback;
  215. mb();
  216. spu_unmap_mappings(ctx);
  217. spu_restore(&ctx->csa, spu);
  218. spu->timestamp = jiffies;
  219. spu_cpu_affinity_set(spu, raw_smp_processor_id());
  220. spu_switch_notify(spu, ctx);
  221. ctx->state = SPU_STATE_RUNNABLE;
  222. spuctx_switch_state(ctx, SPU_UTIL_IDLE_LOADED);
  223. }
  224. /**
  225. * spu_unbind_context - unbind spu context from physical spu
  226. * @spu: physical spu to unbind from
  227. * @ctx: context to unbind
  228. */
  229. static void spu_unbind_context(struct spu *spu, struct spu_context *ctx)
  230. {
  231. pr_debug("%s: unbind pid=%d SPU=%d NODE=%d\n", __FUNCTION__,
  232. spu->pid, spu->number, spu->node);
  233. spuctx_switch_state(ctx, SPU_UTIL_SYSTEM);
  234. spu_switch_notify(spu, NULL);
  235. spu_unmap_mappings(ctx);
  236. spu_save(&ctx->csa, spu);
  237. spu->timestamp = jiffies;
  238. ctx->state = SPU_STATE_SAVED;
  239. spu->ibox_callback = NULL;
  240. spu->wbox_callback = NULL;
  241. spu->stop_callback = NULL;
  242. spu->mfc_callback = NULL;
  243. spu->dma_callback = NULL;
  244. spu_associate_mm(spu, NULL);
  245. spu->pid = 0;
  246. ctx->ops = &spu_backing_ops;
  247. spu->flags = 0;
  248. spu->ctx = NULL;
  249. ctx->stats.slb_flt +=
  250. (spu->stats.slb_flt - ctx->stats.slb_flt_base);
  251. ctx->stats.class2_intr +=
  252. (spu->stats.class2_intr - ctx->stats.class2_intr_base);
  253. /* This maps the underlying spu state to idle */
  254. spuctx_switch_state(ctx, SPU_UTIL_IDLE_LOADED);
  255. ctx->spu = NULL;
  256. }
  257. /**
  258. * spu_add_to_rq - add a context to the runqueue
  259. * @ctx: context to add
  260. */
  261. static void __spu_add_to_rq(struct spu_context *ctx)
  262. {
  263. /*
  264. * Unfortunately this code path can be called from multiple threads
  265. * on behalf of a single context due to the way the problem state
  266. * mmap support works.
  267. *
  268. * Fortunately we need to wake up all these threads at the same time
  269. * and can simply skip the runqueue addition for every but the first
  270. * thread getting into this codepath.
  271. *
  272. * It's still quite hacky, and long-term we should proxy all other
  273. * threads through the owner thread so that spu_run is in control
  274. * of all the scheduling activity for a given context.
  275. */
  276. if (list_empty(&ctx->rq)) {
  277. list_add_tail(&ctx->rq, &spu_prio->runq[ctx->prio]);
  278. set_bit(ctx->prio, spu_prio->bitmap);
  279. if (!spu_prio->nr_waiting++)
  280. __mod_timer(&spusched_timer, jiffies + SPUSCHED_TICK);
  281. }
  282. }
  283. static void __spu_del_from_rq(struct spu_context *ctx)
  284. {
  285. int prio = ctx->prio;
  286. if (!list_empty(&ctx->rq)) {
  287. if (!--spu_prio->nr_waiting)
  288. del_timer(&spusched_timer);
  289. list_del_init(&ctx->rq);
  290. if (list_empty(&spu_prio->runq[prio]))
  291. clear_bit(prio, spu_prio->bitmap);
  292. }
  293. }
  294. static void spu_prio_wait(struct spu_context *ctx)
  295. {
  296. DEFINE_WAIT(wait);
  297. spin_lock(&spu_prio->runq_lock);
  298. prepare_to_wait_exclusive(&ctx->stop_wq, &wait, TASK_INTERRUPTIBLE);
  299. if (!signal_pending(current)) {
  300. __spu_add_to_rq(ctx);
  301. spin_unlock(&spu_prio->runq_lock);
  302. mutex_unlock(&ctx->state_mutex);
  303. schedule();
  304. mutex_lock(&ctx->state_mutex);
  305. spin_lock(&spu_prio->runq_lock);
  306. __spu_del_from_rq(ctx);
  307. }
  308. spin_unlock(&spu_prio->runq_lock);
  309. __set_current_state(TASK_RUNNING);
  310. remove_wait_queue(&ctx->stop_wq, &wait);
  311. }
  312. static struct spu *spu_get_idle(struct spu_context *ctx)
  313. {
  314. struct spu *spu = NULL;
  315. int node = cpu_to_node(raw_smp_processor_id());
  316. int n;
  317. for (n = 0; n < MAX_NUMNODES; n++, node++) {
  318. node = (node < MAX_NUMNODES) ? node : 0;
  319. if (!node_allowed(ctx, node))
  320. continue;
  321. spu = spu_alloc_node(node);
  322. if (spu)
  323. break;
  324. }
  325. return spu;
  326. }
  327. /**
  328. * find_victim - find a lower priority context to preempt
  329. * @ctx: canidate context for running
  330. *
  331. * Returns the freed physical spu to run the new context on.
  332. */
  333. static struct spu *find_victim(struct spu_context *ctx)
  334. {
  335. struct spu_context *victim = NULL;
  336. struct spu *spu;
  337. int node, n;
  338. /*
  339. * Look for a possible preemption candidate on the local node first.
  340. * If there is no candidate look at the other nodes. This isn't
  341. * exactly fair, but so far the whole spu schedule tries to keep
  342. * a strong node affinity. We might want to fine-tune this in
  343. * the future.
  344. */
  345. restart:
  346. node = cpu_to_node(raw_smp_processor_id());
  347. for (n = 0; n < MAX_NUMNODES; n++, node++) {
  348. node = (node < MAX_NUMNODES) ? node : 0;
  349. if (!node_allowed(ctx, node))
  350. continue;
  351. mutex_lock(&spu_prio->active_mutex[node]);
  352. list_for_each_entry(spu, &spu_prio->active_list[node], list) {
  353. struct spu_context *tmp = spu->ctx;
  354. if (tmp->prio > ctx->prio &&
  355. (!victim || tmp->prio > victim->prio))
  356. victim = spu->ctx;
  357. }
  358. mutex_unlock(&spu_prio->active_mutex[node]);
  359. if (victim) {
  360. /*
  361. * This nests ctx->state_mutex, but we always lock
  362. * higher priority contexts before lower priority
  363. * ones, so this is safe until we introduce
  364. * priority inheritance schemes.
  365. */
  366. if (!mutex_trylock(&victim->state_mutex)) {
  367. victim = NULL;
  368. goto restart;
  369. }
  370. spu = victim->spu;
  371. if (!spu) {
  372. /*
  373. * This race can happen because we've dropped
  374. * the active list mutex. No a problem, just
  375. * restart the search.
  376. */
  377. mutex_unlock(&victim->state_mutex);
  378. victim = NULL;
  379. goto restart;
  380. }
  381. spu_remove_from_active_list(spu);
  382. spu_unbind_context(spu, victim);
  383. victim->stats.invol_ctx_switch++;
  384. spu->stats.invol_ctx_switch++;
  385. mutex_unlock(&victim->state_mutex);
  386. /*
  387. * We need to break out of the wait loop in spu_run
  388. * manually to ensure this context gets put on the
  389. * runqueue again ASAP.
  390. */
  391. wake_up(&victim->stop_wq);
  392. return spu;
  393. }
  394. }
  395. return NULL;
  396. }
  397. /**
  398. * spu_activate - find a free spu for a context and execute it
  399. * @ctx: spu context to schedule
  400. * @flags: flags (currently ignored)
  401. *
  402. * Tries to find a free spu to run @ctx. If no free spu is available
  403. * add the context to the runqueue so it gets woken up once an spu
  404. * is available.
  405. */
  406. int spu_activate(struct spu_context *ctx, unsigned long flags)
  407. {
  408. do {
  409. struct spu *spu;
  410. /*
  411. * If there are multiple threads waiting for a single context
  412. * only one actually binds the context while the others will
  413. * only be able to acquire the state_mutex once the context
  414. * already is in runnable state.
  415. */
  416. if (ctx->spu)
  417. return 0;
  418. spu = spu_get_idle(ctx);
  419. /*
  420. * If this is a realtime thread we try to get it running by
  421. * preempting a lower priority thread.
  422. */
  423. if (!spu && rt_prio(ctx->prio))
  424. spu = find_victim(ctx);
  425. if (spu) {
  426. spu_bind_context(spu, ctx);
  427. spu_add_to_active_list(spu);
  428. return 0;
  429. }
  430. spu_prio_wait(ctx);
  431. } while (!signal_pending(current));
  432. return -ERESTARTSYS;
  433. }
  434. /**
  435. * grab_runnable_context - try to find a runnable context
  436. *
  437. * Remove the highest priority context on the runqueue and return it
  438. * to the caller. Returns %NULL if no runnable context was found.
  439. */
  440. static struct spu_context *grab_runnable_context(int prio, int node)
  441. {
  442. struct spu_context *ctx;
  443. int best;
  444. spin_lock(&spu_prio->runq_lock);
  445. best = find_first_bit(spu_prio->bitmap, prio);
  446. while (best < prio) {
  447. struct list_head *rq = &spu_prio->runq[best];
  448. list_for_each_entry(ctx, rq, rq) {
  449. /* XXX(hch): check for affinity here aswell */
  450. if (__node_allowed(ctx, node)) {
  451. __spu_del_from_rq(ctx);
  452. goto found;
  453. }
  454. }
  455. best++;
  456. }
  457. ctx = NULL;
  458. found:
  459. spin_unlock(&spu_prio->runq_lock);
  460. return ctx;
  461. }
  462. static int __spu_deactivate(struct spu_context *ctx, int force, int max_prio)
  463. {
  464. struct spu *spu = ctx->spu;
  465. struct spu_context *new = NULL;
  466. if (spu) {
  467. new = grab_runnable_context(max_prio, spu->node);
  468. if (new || force) {
  469. spu_remove_from_active_list(spu);
  470. spu_unbind_context(spu, ctx);
  471. ctx->stats.vol_ctx_switch++;
  472. spu->stats.vol_ctx_switch++;
  473. spu_free(spu);
  474. if (new)
  475. wake_up(&new->stop_wq);
  476. }
  477. }
  478. return new != NULL;
  479. }
  480. /**
  481. * spu_deactivate - unbind a context from it's physical spu
  482. * @ctx: spu context to unbind
  483. *
  484. * Unbind @ctx from the physical spu it is running on and schedule
  485. * the highest priority context to run on the freed physical spu.
  486. */
  487. void spu_deactivate(struct spu_context *ctx)
  488. {
  489. __spu_deactivate(ctx, 1, MAX_PRIO);
  490. }
  491. /**
  492. * spu_yield - yield a physical spu if others are waiting
  493. * @ctx: spu context to yield
  494. *
  495. * Check if there is a higher priority context waiting and if yes
  496. * unbind @ctx from the physical spu and schedule the highest
  497. * priority context to run on the freed physical spu instead.
  498. */
  499. void spu_yield(struct spu_context *ctx)
  500. {
  501. if (!(ctx->flags & SPU_CREATE_NOSCHED)) {
  502. mutex_lock(&ctx->state_mutex);
  503. __spu_deactivate(ctx, 0, MAX_PRIO);
  504. mutex_unlock(&ctx->state_mutex);
  505. }
  506. }
  507. static void spusched_tick(struct spu_context *ctx)
  508. {
  509. if (ctx->flags & SPU_CREATE_NOSCHED)
  510. return;
  511. if (ctx->policy == SCHED_FIFO)
  512. return;
  513. if (--ctx->time_slice)
  514. return;
  515. /*
  516. * Unfortunately active_mutex ranks outside of state_mutex, so
  517. * we have to trylock here. If we fail give the context another
  518. * tick and try again.
  519. */
  520. if (mutex_trylock(&ctx->state_mutex)) {
  521. struct spu *spu = ctx->spu;
  522. struct spu_context *new;
  523. new = grab_runnable_context(ctx->prio + 1, spu->node);
  524. if (new) {
  525. __spu_remove_from_active_list(spu);
  526. spu_unbind_context(spu, ctx);
  527. ctx->stats.invol_ctx_switch++;
  528. spu->stats.invol_ctx_switch++;
  529. spu_free(spu);
  530. wake_up(&new->stop_wq);
  531. /*
  532. * We need to break out of the wait loop in
  533. * spu_run manually to ensure this context
  534. * gets put on the runqueue again ASAP.
  535. */
  536. wake_up(&ctx->stop_wq);
  537. }
  538. spu_set_timeslice(ctx);
  539. mutex_unlock(&ctx->state_mutex);
  540. } else {
  541. ctx->time_slice++;
  542. }
  543. }
  544. /**
  545. * count_active_contexts - count nr of active tasks
  546. *
  547. * Return the number of tasks currently running or waiting to run.
  548. *
  549. * Note that we don't take runq_lock / active_mutex here. Reading
  550. * a single 32bit value is atomic on powerpc, and we don't care
  551. * about memory ordering issues here.
  552. */
  553. static unsigned long count_active_contexts(void)
  554. {
  555. int nr_active = 0, node;
  556. for (node = 0; node < MAX_NUMNODES; node++)
  557. nr_active += spu_prio->nr_active[node];
  558. nr_active += spu_prio->nr_waiting;
  559. return nr_active;
  560. }
  561. /**
  562. * spu_calc_load - given tick count, update the avenrun load estimates.
  563. * @tick: tick count
  564. *
  565. * No locking against reading these values from userspace, as for
  566. * the CPU loadavg code.
  567. */
  568. static void spu_calc_load(unsigned long ticks)
  569. {
  570. unsigned long active_tasks; /* fixed-point */
  571. static int count = LOAD_FREQ;
  572. count -= ticks;
  573. if (unlikely(count < 0)) {
  574. active_tasks = count_active_contexts() * FIXED_1;
  575. do {
  576. CALC_LOAD(spu_avenrun[0], EXP_1, active_tasks);
  577. CALC_LOAD(spu_avenrun[1], EXP_5, active_tasks);
  578. CALC_LOAD(spu_avenrun[2], EXP_15, active_tasks);
  579. count += LOAD_FREQ;
  580. } while (count < 0);
  581. }
  582. }
  583. static void spusched_wake(unsigned long data)
  584. {
  585. mod_timer(&spusched_timer, jiffies + SPUSCHED_TICK);
  586. wake_up_process(spusched_task);
  587. spu_calc_load(SPUSCHED_TICK);
  588. }
  589. static int spusched_thread(void *unused)
  590. {
  591. struct spu *spu, *next;
  592. int node;
  593. while (!kthread_should_stop()) {
  594. set_current_state(TASK_INTERRUPTIBLE);
  595. schedule();
  596. for (node = 0; node < MAX_NUMNODES; node++) {
  597. mutex_lock(&spu_prio->active_mutex[node]);
  598. list_for_each_entry_safe(spu, next,
  599. &spu_prio->active_list[node],
  600. list)
  601. spusched_tick(spu->ctx);
  602. mutex_unlock(&spu_prio->active_mutex[node]);
  603. }
  604. }
  605. return 0;
  606. }
  607. #define LOAD_INT(x) ((x) >> FSHIFT)
  608. #define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
  609. static int show_spu_loadavg(struct seq_file *s, void *private)
  610. {
  611. int a, b, c;
  612. a = spu_avenrun[0] + (FIXED_1/200);
  613. b = spu_avenrun[1] + (FIXED_1/200);
  614. c = spu_avenrun[2] + (FIXED_1/200);
  615. /*
  616. * Note that last_pid doesn't really make much sense for the
  617. * SPU loadavg (it even seems very odd on the CPU side..),
  618. * but we include it here to have a 100% compatible interface.
  619. */
  620. seq_printf(s, "%d.%02d %d.%02d %d.%02d %ld/%d %d\n",
  621. LOAD_INT(a), LOAD_FRAC(a),
  622. LOAD_INT(b), LOAD_FRAC(b),
  623. LOAD_INT(c), LOAD_FRAC(c),
  624. count_active_contexts(),
  625. atomic_read(&nr_spu_contexts),
  626. current->nsproxy->pid_ns->last_pid);
  627. return 0;
  628. }
  629. static int spu_loadavg_open(struct inode *inode, struct file *file)
  630. {
  631. return single_open(file, show_spu_loadavg, NULL);
  632. }
  633. static const struct file_operations spu_loadavg_fops = {
  634. .open = spu_loadavg_open,
  635. .read = seq_read,
  636. .llseek = seq_lseek,
  637. .release = single_release,
  638. };
  639. int __init spu_sched_init(void)
  640. {
  641. struct proc_dir_entry *entry;
  642. int err = -ENOMEM, i;
  643. spu_prio = kzalloc(sizeof(struct spu_prio_array), GFP_KERNEL);
  644. if (!spu_prio)
  645. goto out;
  646. for (i = 0; i < MAX_PRIO; i++) {
  647. INIT_LIST_HEAD(&spu_prio->runq[i]);
  648. __clear_bit(i, spu_prio->bitmap);
  649. }
  650. for (i = 0; i < MAX_NUMNODES; i++) {
  651. mutex_init(&spu_prio->active_mutex[i]);
  652. INIT_LIST_HEAD(&spu_prio->active_list[i]);
  653. }
  654. spin_lock_init(&spu_prio->runq_lock);
  655. setup_timer(&spusched_timer, spusched_wake, 0);
  656. spusched_task = kthread_run(spusched_thread, NULL, "spusched");
  657. if (IS_ERR(spusched_task)) {
  658. err = PTR_ERR(spusched_task);
  659. goto out_free_spu_prio;
  660. }
  661. entry = create_proc_entry("spu_loadavg", 0, NULL);
  662. if (!entry)
  663. goto out_stop_kthread;
  664. entry->proc_fops = &spu_loadavg_fops;
  665. pr_debug("spusched: tick: %d, min ticks: %d, default ticks: %d\n",
  666. SPUSCHED_TICK, MIN_SPU_TIMESLICE, DEF_SPU_TIMESLICE);
  667. return 0;
  668. out_stop_kthread:
  669. kthread_stop(spusched_task);
  670. out_free_spu_prio:
  671. kfree(spu_prio);
  672. out:
  673. return err;
  674. }
  675. void spu_sched_exit(void)
  676. {
  677. struct spu *spu, *tmp;
  678. int node;
  679. remove_proc_entry("spu_loadavg", NULL);
  680. del_timer_sync(&spusched_timer);
  681. kthread_stop(spusched_task);
  682. for (node = 0; node < MAX_NUMNODES; node++) {
  683. mutex_lock(&spu_prio->active_mutex[node]);
  684. list_for_each_entry_safe(spu, tmp, &spu_prio->active_list[node],
  685. list) {
  686. list_del_init(&spu->list);
  687. spu_free(spu);
  688. }
  689. mutex_unlock(&spu_prio->active_mutex[node]);
  690. }
  691. kfree(spu_prio);
  692. }