sched.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  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. int nr_waiting;
  51. };
  52. static unsigned long spu_avenrun[3];
  53. static struct spu_prio_array *spu_prio;
  54. static struct task_struct *spusched_task;
  55. static struct timer_list spusched_timer;
  56. /*
  57. * Priority of a normal, non-rt, non-niced'd process (aka nice level 0).
  58. */
  59. #define NORMAL_PRIO 120
  60. /*
  61. * Frequency of the spu scheduler tick. By default we do one SPU scheduler
  62. * tick for every 10 CPU scheduler ticks.
  63. */
  64. #define SPUSCHED_TICK (10)
  65. /*
  66. * These are the 'tuning knobs' of the scheduler:
  67. *
  68. * Minimum timeslice is 5 msecs (or 1 spu scheduler tick, whichever is
  69. * larger), default timeslice is 100 msecs, maximum timeslice is 800 msecs.
  70. */
  71. #define MIN_SPU_TIMESLICE max(5 * HZ / (1000 * SPUSCHED_TICK), 1)
  72. #define DEF_SPU_TIMESLICE (100 * HZ / (1000 * SPUSCHED_TICK))
  73. #define MAX_USER_PRIO (MAX_PRIO - MAX_RT_PRIO)
  74. #define SCALE_PRIO(x, prio) \
  75. max(x * (MAX_PRIO - prio) / (MAX_USER_PRIO / 2), MIN_SPU_TIMESLICE)
  76. /*
  77. * scale user-nice values [ -20 ... 0 ... 19 ] to time slice values:
  78. * [800ms ... 100ms ... 5ms]
  79. *
  80. * The higher a thread's priority, the bigger timeslices
  81. * it gets during one round of execution. But even the lowest
  82. * priority thread gets MIN_TIMESLICE worth of execution time.
  83. */
  84. void spu_set_timeslice(struct spu_context *ctx)
  85. {
  86. if (ctx->prio < NORMAL_PRIO)
  87. ctx->time_slice = SCALE_PRIO(DEF_SPU_TIMESLICE * 4, ctx->prio);
  88. else
  89. ctx->time_slice = SCALE_PRIO(DEF_SPU_TIMESLICE, ctx->prio);
  90. }
  91. /*
  92. * Update scheduling information from the owning thread.
  93. */
  94. void __spu_update_sched_info(struct spu_context *ctx)
  95. {
  96. /*
  97. * 32-Bit assignments are atomic on powerpc, and we don't care about
  98. * memory ordering here because retrieving the controlling thread is
  99. * per definition racy.
  100. */
  101. ctx->tid = current->pid;
  102. /*
  103. * We do our own priority calculations, so we normally want
  104. * ->static_prio to start with. Unfortunately this field
  105. * contains junk for threads with a realtime scheduling
  106. * policy so we have to look at ->prio in this case.
  107. */
  108. if (rt_prio(current->prio))
  109. ctx->prio = current->prio;
  110. else
  111. ctx->prio = current->static_prio;
  112. ctx->policy = current->policy;
  113. /*
  114. * A lot of places that don't hold list_mutex poke into
  115. * cpus_allowed, including grab_runnable_context which
  116. * already holds the runq_lock. So abuse runq_lock
  117. * to protect this field as well.
  118. */
  119. spin_lock(&spu_prio->runq_lock);
  120. ctx->cpus_allowed = current->cpus_allowed;
  121. spin_unlock(&spu_prio->runq_lock);
  122. }
  123. void spu_update_sched_info(struct spu_context *ctx)
  124. {
  125. int node = ctx->spu->node;
  126. mutex_lock(&cbe_spu_info[node].list_mutex);
  127. __spu_update_sched_info(ctx);
  128. mutex_unlock(&cbe_spu_info[node].list_mutex);
  129. }
  130. static int __node_allowed(struct spu_context *ctx, int node)
  131. {
  132. if (nr_cpus_node(node)) {
  133. cpumask_t mask = node_to_cpumask(node);
  134. if (cpus_intersects(mask, ctx->cpus_allowed))
  135. return 1;
  136. }
  137. return 0;
  138. }
  139. static int node_allowed(struct spu_context *ctx, int node)
  140. {
  141. int rval;
  142. spin_lock(&spu_prio->runq_lock);
  143. rval = __node_allowed(ctx, node);
  144. spin_unlock(&spu_prio->runq_lock);
  145. return rval;
  146. }
  147. static BLOCKING_NOTIFIER_HEAD(spu_switch_notifier);
  148. void spu_switch_notify(struct spu *spu, struct spu_context *ctx)
  149. {
  150. blocking_notifier_call_chain(&spu_switch_notifier,
  151. ctx ? ctx->object_id : 0, spu);
  152. }
  153. static void notify_spus_active(void)
  154. {
  155. int node;
  156. /*
  157. * Wake up the active spu_contexts.
  158. *
  159. * When the awakened processes see their "notify_active" flag is set,
  160. * they will call spu_switch_notify().
  161. */
  162. for_each_online_node(node) {
  163. struct spu *spu;
  164. mutex_lock(&cbe_spu_info[node].list_mutex);
  165. list_for_each_entry(spu, &cbe_spu_info[node].spus, cbe_list) {
  166. if (spu->alloc_state != SPU_FREE) {
  167. struct spu_context *ctx = spu->ctx;
  168. set_bit(SPU_SCHED_NOTIFY_ACTIVE,
  169. &ctx->sched_flags);
  170. mb();
  171. wake_up_all(&ctx->stop_wq);
  172. }
  173. }
  174. mutex_unlock(&cbe_spu_info[node].list_mutex);
  175. }
  176. }
  177. int spu_switch_event_register(struct notifier_block * n)
  178. {
  179. int ret;
  180. ret = blocking_notifier_chain_register(&spu_switch_notifier, n);
  181. if (!ret)
  182. notify_spus_active();
  183. return ret;
  184. }
  185. EXPORT_SYMBOL_GPL(spu_switch_event_register);
  186. int spu_switch_event_unregister(struct notifier_block * n)
  187. {
  188. return blocking_notifier_chain_unregister(&spu_switch_notifier, n);
  189. }
  190. EXPORT_SYMBOL_GPL(spu_switch_event_unregister);
  191. /**
  192. * spu_bind_context - bind spu context to physical spu
  193. * @spu: physical spu to bind to
  194. * @ctx: context to bind
  195. */
  196. static void spu_bind_context(struct spu *spu, struct spu_context *ctx)
  197. {
  198. pr_debug("%s: pid=%d SPU=%d NODE=%d\n", __FUNCTION__, current->pid,
  199. spu->number, spu->node);
  200. spuctx_switch_state(ctx, SPU_UTIL_SYSTEM);
  201. if (ctx->flags & SPU_CREATE_NOSCHED)
  202. atomic_inc(&cbe_spu_info[spu->node].reserved_spus);
  203. ctx->stats.slb_flt_base = spu->stats.slb_flt;
  204. ctx->stats.class2_intr_base = spu->stats.class2_intr;
  205. spu->ctx = ctx;
  206. spu->flags = 0;
  207. ctx->spu = spu;
  208. ctx->ops = &spu_hw_ops;
  209. spu->pid = current->pid;
  210. spu->tgid = current->tgid;
  211. spu_associate_mm(spu, ctx->owner);
  212. spu->ibox_callback = spufs_ibox_callback;
  213. spu->wbox_callback = spufs_wbox_callback;
  214. spu->stop_callback = spufs_stop_callback;
  215. spu->mfc_callback = spufs_mfc_callback;
  216. spu->dma_callback = spufs_dma_callback;
  217. mb();
  218. spu_unmap_mappings(ctx);
  219. spu_restore(&ctx->csa, spu);
  220. spu->timestamp = jiffies;
  221. spu_cpu_affinity_set(spu, raw_smp_processor_id());
  222. spu_switch_notify(spu, ctx);
  223. ctx->state = SPU_STATE_RUNNABLE;
  224. spuctx_switch_state(ctx, SPU_UTIL_IDLE_LOADED);
  225. }
  226. /*
  227. * Must be used with the list_mutex held.
  228. */
  229. static inline int sched_spu(struct spu *spu)
  230. {
  231. BUG_ON(!mutex_is_locked(&cbe_spu_info[spu->node].list_mutex));
  232. return (!spu->ctx || !(spu->ctx->flags & SPU_CREATE_NOSCHED));
  233. }
  234. static void aff_merge_remaining_ctxs(struct spu_gang *gang)
  235. {
  236. struct spu_context *ctx;
  237. list_for_each_entry(ctx, &gang->aff_list_head, aff_list) {
  238. if (list_empty(&ctx->aff_list))
  239. list_add(&ctx->aff_list, &gang->aff_list_head);
  240. }
  241. gang->aff_flags |= AFF_MERGED;
  242. }
  243. static void aff_set_offsets(struct spu_gang *gang)
  244. {
  245. struct spu_context *ctx;
  246. int offset;
  247. offset = -1;
  248. list_for_each_entry_reverse(ctx, &gang->aff_ref_ctx->aff_list,
  249. aff_list) {
  250. if (&ctx->aff_list == &gang->aff_list_head)
  251. break;
  252. ctx->aff_offset = offset--;
  253. }
  254. offset = 0;
  255. list_for_each_entry(ctx, gang->aff_ref_ctx->aff_list.prev, aff_list) {
  256. if (&ctx->aff_list == &gang->aff_list_head)
  257. break;
  258. ctx->aff_offset = offset++;
  259. }
  260. gang->aff_flags |= AFF_OFFSETS_SET;
  261. }
  262. static struct spu *aff_ref_location(struct spu_context *ctx, int mem_aff,
  263. int group_size, int lowest_offset)
  264. {
  265. struct spu *spu;
  266. int node, n;
  267. /*
  268. * TODO: A better algorithm could be used to find a good spu to be
  269. * used as reference location for the ctxs chain.
  270. */
  271. node = cpu_to_node(raw_smp_processor_id());
  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. mutex_lock(&cbe_spu_info[node].list_mutex);
  277. list_for_each_entry(spu, &cbe_spu_info[node].spus, cbe_list) {
  278. if ((!mem_aff || spu->has_mem_affinity) &&
  279. sched_spu(spu)) {
  280. mutex_unlock(&cbe_spu_info[node].list_mutex);
  281. return spu;
  282. }
  283. }
  284. mutex_unlock(&cbe_spu_info[node].list_mutex);
  285. }
  286. return NULL;
  287. }
  288. static void aff_set_ref_point_location(struct spu_gang *gang)
  289. {
  290. int mem_aff, gs, lowest_offset;
  291. struct spu_context *ctx;
  292. struct spu *tmp;
  293. mem_aff = gang->aff_ref_ctx->flags & SPU_CREATE_AFFINITY_MEM;
  294. lowest_offset = 0;
  295. gs = 0;
  296. list_for_each_entry(tmp, &gang->aff_list_head, aff_list)
  297. gs++;
  298. list_for_each_entry_reverse(ctx, &gang->aff_ref_ctx->aff_list,
  299. aff_list) {
  300. if (&ctx->aff_list == &gang->aff_list_head)
  301. break;
  302. lowest_offset = ctx->aff_offset;
  303. }
  304. gang->aff_ref_spu = aff_ref_location(gang->aff_ref_ctx, mem_aff, gs,
  305. lowest_offset);
  306. }
  307. static struct spu *ctx_location(struct spu *ref, int offset, int node)
  308. {
  309. struct spu *spu;
  310. spu = NULL;
  311. if (offset >= 0) {
  312. list_for_each_entry(spu, ref->aff_list.prev, aff_list) {
  313. BUG_ON(spu->node != node);
  314. if (offset == 0)
  315. break;
  316. if (sched_spu(spu))
  317. offset--;
  318. }
  319. } else {
  320. list_for_each_entry_reverse(spu, ref->aff_list.next, aff_list) {
  321. BUG_ON(spu->node != node);
  322. if (offset == 0)
  323. break;
  324. if (sched_spu(spu))
  325. offset++;
  326. }
  327. }
  328. return spu;
  329. }
  330. /*
  331. * affinity_check is called each time a context is going to be scheduled.
  332. * It returns the spu ptr on which the context must run.
  333. */
  334. static int has_affinity(struct spu_context *ctx)
  335. {
  336. struct spu_gang *gang = ctx->gang;
  337. if (list_empty(&ctx->aff_list))
  338. return 0;
  339. if (!gang->aff_ref_spu) {
  340. if (!(gang->aff_flags & AFF_MERGED))
  341. aff_merge_remaining_ctxs(gang);
  342. if (!(gang->aff_flags & AFF_OFFSETS_SET))
  343. aff_set_offsets(gang);
  344. aff_set_ref_point_location(gang);
  345. }
  346. return gang->aff_ref_spu != NULL;
  347. }
  348. /**
  349. * spu_unbind_context - unbind spu context from physical spu
  350. * @spu: physical spu to unbind from
  351. * @ctx: context to unbind
  352. */
  353. static void spu_unbind_context(struct spu *spu, struct spu_context *ctx)
  354. {
  355. pr_debug("%s: unbind pid=%d SPU=%d NODE=%d\n", __FUNCTION__,
  356. spu->pid, spu->number, spu->node);
  357. spuctx_switch_state(ctx, SPU_UTIL_SYSTEM);
  358. if (spu->ctx->flags & SPU_CREATE_NOSCHED)
  359. atomic_dec(&cbe_spu_info[spu->node].reserved_spus);
  360. if (ctx->gang){
  361. mutex_lock(&ctx->gang->aff_mutex);
  362. if (has_affinity(ctx)) {
  363. if (atomic_dec_and_test(&ctx->gang->aff_sched_count))
  364. ctx->gang->aff_ref_spu = NULL;
  365. }
  366. mutex_unlock(&ctx->gang->aff_mutex);
  367. }
  368. spu_switch_notify(spu, NULL);
  369. spu_unmap_mappings(ctx);
  370. spu_save(&ctx->csa, spu);
  371. spu->timestamp = jiffies;
  372. ctx->state = SPU_STATE_SAVED;
  373. spu->ibox_callback = NULL;
  374. spu->wbox_callback = NULL;
  375. spu->stop_callback = NULL;
  376. spu->mfc_callback = NULL;
  377. spu->dma_callback = NULL;
  378. spu_associate_mm(spu, NULL);
  379. spu->pid = 0;
  380. spu->tgid = 0;
  381. ctx->ops = &spu_backing_ops;
  382. spu->flags = 0;
  383. spu->ctx = NULL;
  384. ctx->stats.slb_flt +=
  385. (spu->stats.slb_flt - ctx->stats.slb_flt_base);
  386. ctx->stats.class2_intr +=
  387. (spu->stats.class2_intr - ctx->stats.class2_intr_base);
  388. /* This maps the underlying spu state to idle */
  389. spuctx_switch_state(ctx, SPU_UTIL_IDLE_LOADED);
  390. ctx->spu = NULL;
  391. }
  392. /**
  393. * spu_add_to_rq - add a context to the runqueue
  394. * @ctx: context to add
  395. */
  396. static void __spu_add_to_rq(struct spu_context *ctx)
  397. {
  398. /*
  399. * Unfortunately this code path can be called from multiple threads
  400. * on behalf of a single context due to the way the problem state
  401. * mmap support works.
  402. *
  403. * Fortunately we need to wake up all these threads at the same time
  404. * and can simply skip the runqueue addition for every but the first
  405. * thread getting into this codepath.
  406. *
  407. * It's still quite hacky, and long-term we should proxy all other
  408. * threads through the owner thread so that spu_run is in control
  409. * of all the scheduling activity for a given context.
  410. */
  411. if (list_empty(&ctx->rq)) {
  412. list_add_tail(&ctx->rq, &spu_prio->runq[ctx->prio]);
  413. set_bit(ctx->prio, spu_prio->bitmap);
  414. if (!spu_prio->nr_waiting++)
  415. __mod_timer(&spusched_timer, jiffies + SPUSCHED_TICK);
  416. }
  417. }
  418. static void __spu_del_from_rq(struct spu_context *ctx)
  419. {
  420. int prio = ctx->prio;
  421. if (!list_empty(&ctx->rq)) {
  422. if (!--spu_prio->nr_waiting)
  423. del_timer(&spusched_timer);
  424. list_del_init(&ctx->rq);
  425. if (list_empty(&spu_prio->runq[prio]))
  426. clear_bit(prio, spu_prio->bitmap);
  427. }
  428. }
  429. static void spu_prio_wait(struct spu_context *ctx)
  430. {
  431. DEFINE_WAIT(wait);
  432. spin_lock(&spu_prio->runq_lock);
  433. prepare_to_wait_exclusive(&ctx->stop_wq, &wait, TASK_INTERRUPTIBLE);
  434. if (!signal_pending(current)) {
  435. __spu_add_to_rq(ctx);
  436. spin_unlock(&spu_prio->runq_lock);
  437. mutex_unlock(&ctx->state_mutex);
  438. schedule();
  439. mutex_lock(&ctx->state_mutex);
  440. spin_lock(&spu_prio->runq_lock);
  441. __spu_del_from_rq(ctx);
  442. }
  443. spin_unlock(&spu_prio->runq_lock);
  444. __set_current_state(TASK_RUNNING);
  445. remove_wait_queue(&ctx->stop_wq, &wait);
  446. }
  447. static struct spu *spu_get_idle(struct spu_context *ctx)
  448. {
  449. struct spu *spu, *aff_ref_spu;
  450. int node, n;
  451. if (ctx->gang) {
  452. mutex_lock(&ctx->gang->aff_mutex);
  453. if (has_affinity(ctx)) {
  454. aff_ref_spu = ctx->gang->aff_ref_spu;
  455. atomic_inc(&ctx->gang->aff_sched_count);
  456. mutex_unlock(&ctx->gang->aff_mutex);
  457. node = aff_ref_spu->node;
  458. mutex_lock(&cbe_spu_info[node].list_mutex);
  459. spu = ctx_location(aff_ref_spu, ctx->aff_offset, node);
  460. if (spu && spu->alloc_state == SPU_FREE)
  461. goto found;
  462. mutex_unlock(&cbe_spu_info[node].list_mutex);
  463. mutex_lock(&ctx->gang->aff_mutex);
  464. if (atomic_dec_and_test(&ctx->gang->aff_sched_count))
  465. ctx->gang->aff_ref_spu = NULL;
  466. mutex_unlock(&ctx->gang->aff_mutex);
  467. return NULL;
  468. }
  469. mutex_unlock(&ctx->gang->aff_mutex);
  470. }
  471. node = cpu_to_node(raw_smp_processor_id());
  472. for (n = 0; n < MAX_NUMNODES; n++, node++) {
  473. node = (node < MAX_NUMNODES) ? node : 0;
  474. if (!node_allowed(ctx, node))
  475. continue;
  476. mutex_lock(&cbe_spu_info[node].list_mutex);
  477. list_for_each_entry(spu, &cbe_spu_info[node].spus, cbe_list) {
  478. if (spu->alloc_state == SPU_FREE)
  479. goto found;
  480. }
  481. mutex_unlock(&cbe_spu_info[node].list_mutex);
  482. }
  483. return NULL;
  484. found:
  485. spu->alloc_state = SPU_USED;
  486. mutex_unlock(&cbe_spu_info[node].list_mutex);
  487. pr_debug("Got SPU %d %d\n", spu->number, spu->node);
  488. spu_init_channels(spu);
  489. return spu;
  490. }
  491. /**
  492. * find_victim - find a lower priority context to preempt
  493. * @ctx: canidate context for running
  494. *
  495. * Returns the freed physical spu to run the new context on.
  496. */
  497. static struct spu *find_victim(struct spu_context *ctx)
  498. {
  499. struct spu_context *victim = NULL;
  500. struct spu *spu;
  501. int node, n;
  502. /*
  503. * Look for a possible preemption candidate on the local node first.
  504. * If there is no candidate look at the other nodes. This isn't
  505. * exactly fair, but so far the whole spu scheduler tries to keep
  506. * a strong node affinity. We might want to fine-tune this in
  507. * the future.
  508. */
  509. restart:
  510. node = cpu_to_node(raw_smp_processor_id());
  511. for (n = 0; n < MAX_NUMNODES; n++, node++) {
  512. node = (node < MAX_NUMNODES) ? node : 0;
  513. if (!node_allowed(ctx, node))
  514. continue;
  515. mutex_lock(&cbe_spu_info[node].list_mutex);
  516. list_for_each_entry(spu, &cbe_spu_info[node].spus, cbe_list) {
  517. struct spu_context *tmp = spu->ctx;
  518. if (tmp && tmp->prio > ctx->prio &&
  519. (!victim || tmp->prio > victim->prio))
  520. victim = spu->ctx;
  521. }
  522. mutex_unlock(&cbe_spu_info[node].list_mutex);
  523. if (victim) {
  524. /*
  525. * This nests ctx->state_mutex, but we always lock
  526. * higher priority contexts before lower priority
  527. * ones, so this is safe until we introduce
  528. * priority inheritance schemes.
  529. */
  530. if (!mutex_trylock(&victim->state_mutex)) {
  531. victim = NULL;
  532. goto restart;
  533. }
  534. spu = victim->spu;
  535. if (!spu) {
  536. /*
  537. * This race can happen because we've dropped
  538. * the active list mutex. No a problem, just
  539. * restart the search.
  540. */
  541. mutex_unlock(&victim->state_mutex);
  542. victim = NULL;
  543. goto restart;
  544. }
  545. mutex_lock(&cbe_spu_info[node].list_mutex);
  546. cbe_spu_info[node].nr_active--;
  547. spu_unbind_context(spu, victim);
  548. mutex_unlock(&cbe_spu_info[node].list_mutex);
  549. victim->stats.invol_ctx_switch++;
  550. spu->stats.invol_ctx_switch++;
  551. mutex_unlock(&victim->state_mutex);
  552. /*
  553. * We need to break out of the wait loop in spu_run
  554. * manually to ensure this context gets put on the
  555. * runqueue again ASAP.
  556. */
  557. wake_up(&victim->stop_wq);
  558. return spu;
  559. }
  560. }
  561. return NULL;
  562. }
  563. /**
  564. * spu_activate - find a free spu for a context and execute it
  565. * @ctx: spu context to schedule
  566. * @flags: flags (currently ignored)
  567. *
  568. * Tries to find a free spu to run @ctx. If no free spu is available
  569. * add the context to the runqueue so it gets woken up once an spu
  570. * is available.
  571. */
  572. int spu_activate(struct spu_context *ctx, unsigned long flags)
  573. {
  574. do {
  575. struct spu *spu;
  576. /*
  577. * If there are multiple threads waiting for a single context
  578. * only one actually binds the context while the others will
  579. * only be able to acquire the state_mutex once the context
  580. * already is in runnable state.
  581. */
  582. if (ctx->spu)
  583. return 0;
  584. spu = spu_get_idle(ctx);
  585. /*
  586. * If this is a realtime thread we try to get it running by
  587. * preempting a lower priority thread.
  588. */
  589. if (!spu && rt_prio(ctx->prio))
  590. spu = find_victim(ctx);
  591. if (spu) {
  592. int node = spu->node;
  593. mutex_lock(&cbe_spu_info[node].list_mutex);
  594. spu_bind_context(spu, ctx);
  595. cbe_spu_info[node].nr_active++;
  596. mutex_unlock(&cbe_spu_info[node].list_mutex);
  597. wake_up_all(&ctx->run_wq);
  598. return 0;
  599. }
  600. spu_prio_wait(ctx);
  601. } while (!signal_pending(current));
  602. return -ERESTARTSYS;
  603. }
  604. /**
  605. * grab_runnable_context - try to find a runnable context
  606. *
  607. * Remove the highest priority context on the runqueue and return it
  608. * to the caller. Returns %NULL if no runnable context was found.
  609. */
  610. static struct spu_context *grab_runnable_context(int prio, int node)
  611. {
  612. struct spu_context *ctx;
  613. int best;
  614. spin_lock(&spu_prio->runq_lock);
  615. best = find_first_bit(spu_prio->bitmap, prio);
  616. while (best < prio) {
  617. struct list_head *rq = &spu_prio->runq[best];
  618. list_for_each_entry(ctx, rq, rq) {
  619. /* XXX(hch): check for affinity here aswell */
  620. if (__node_allowed(ctx, node)) {
  621. __spu_del_from_rq(ctx);
  622. goto found;
  623. }
  624. }
  625. best++;
  626. }
  627. ctx = NULL;
  628. found:
  629. spin_unlock(&spu_prio->runq_lock);
  630. return ctx;
  631. }
  632. static int __spu_deactivate(struct spu_context *ctx, int force, int max_prio)
  633. {
  634. struct spu *spu = ctx->spu;
  635. struct spu_context *new = NULL;
  636. if (spu) {
  637. new = grab_runnable_context(max_prio, spu->node);
  638. if (new || force) {
  639. int node = spu->node;
  640. mutex_lock(&cbe_spu_info[node].list_mutex);
  641. spu_unbind_context(spu, ctx);
  642. spu->alloc_state = SPU_FREE;
  643. cbe_spu_info[node].nr_active--;
  644. mutex_unlock(&cbe_spu_info[node].list_mutex);
  645. ctx->stats.vol_ctx_switch++;
  646. spu->stats.vol_ctx_switch++;
  647. if (new)
  648. wake_up(&new->stop_wq);
  649. }
  650. }
  651. return new != NULL;
  652. }
  653. /**
  654. * spu_deactivate - unbind a context from it's physical spu
  655. * @ctx: spu context to unbind
  656. *
  657. * Unbind @ctx from the physical spu it is running on and schedule
  658. * the highest priority context to run on the freed physical spu.
  659. */
  660. void spu_deactivate(struct spu_context *ctx)
  661. {
  662. __spu_deactivate(ctx, 1, MAX_PRIO);
  663. }
  664. /**
  665. * spu_yield - yield a physical spu if others are waiting
  666. * @ctx: spu context to yield
  667. *
  668. * Check if there is a higher priority context waiting and if yes
  669. * unbind @ctx from the physical spu and schedule the highest
  670. * priority context to run on the freed physical spu instead.
  671. */
  672. void spu_yield(struct spu_context *ctx)
  673. {
  674. if (!(ctx->flags & SPU_CREATE_NOSCHED)) {
  675. mutex_lock(&ctx->state_mutex);
  676. __spu_deactivate(ctx, 0, MAX_PRIO);
  677. mutex_unlock(&ctx->state_mutex);
  678. }
  679. }
  680. static noinline void spusched_tick(struct spu_context *ctx)
  681. {
  682. if (ctx->flags & SPU_CREATE_NOSCHED)
  683. return;
  684. if (ctx->policy == SCHED_FIFO)
  685. return;
  686. if (--ctx->time_slice)
  687. return;
  688. /*
  689. * Unfortunately list_mutex ranks outside of state_mutex, so
  690. * we have to trylock here. If we fail give the context another
  691. * tick and try again.
  692. */
  693. if (mutex_trylock(&ctx->state_mutex)) {
  694. struct spu *spu = ctx->spu;
  695. struct spu_context *new;
  696. new = grab_runnable_context(ctx->prio + 1, spu->node);
  697. if (new) {
  698. spu_unbind_context(spu, ctx);
  699. ctx->stats.invol_ctx_switch++;
  700. spu->stats.invol_ctx_switch++;
  701. spu->alloc_state = SPU_FREE;
  702. cbe_spu_info[spu->node].nr_active--;
  703. wake_up(&new->stop_wq);
  704. /*
  705. * We need to break out of the wait loop in
  706. * spu_run manually to ensure this context
  707. * gets put on the runqueue again ASAP.
  708. */
  709. wake_up(&ctx->stop_wq);
  710. }
  711. spu_set_timeslice(ctx);
  712. mutex_unlock(&ctx->state_mutex);
  713. } else {
  714. ctx->time_slice++;
  715. }
  716. }
  717. /**
  718. * count_active_contexts - count nr of active tasks
  719. *
  720. * Return the number of tasks currently running or waiting to run.
  721. *
  722. * Note that we don't take runq_lock / list_mutex here. Reading
  723. * a single 32bit value is atomic on powerpc, and we don't care
  724. * about memory ordering issues here.
  725. */
  726. static unsigned long count_active_contexts(void)
  727. {
  728. int nr_active = 0, node;
  729. for (node = 0; node < MAX_NUMNODES; node++)
  730. nr_active += cbe_spu_info[node].nr_active;
  731. nr_active += spu_prio->nr_waiting;
  732. return nr_active;
  733. }
  734. /**
  735. * spu_calc_load - given tick count, update the avenrun load estimates.
  736. * @tick: tick count
  737. *
  738. * No locking against reading these values from userspace, as for
  739. * the CPU loadavg code.
  740. */
  741. static void spu_calc_load(unsigned long ticks)
  742. {
  743. unsigned long active_tasks; /* fixed-point */
  744. static int count = LOAD_FREQ;
  745. count -= ticks;
  746. if (unlikely(count < 0)) {
  747. active_tasks = count_active_contexts() * FIXED_1;
  748. do {
  749. CALC_LOAD(spu_avenrun[0], EXP_1, active_tasks);
  750. CALC_LOAD(spu_avenrun[1], EXP_5, active_tasks);
  751. CALC_LOAD(spu_avenrun[2], EXP_15, active_tasks);
  752. count += LOAD_FREQ;
  753. } while (count < 0);
  754. }
  755. }
  756. static void spusched_wake(unsigned long data)
  757. {
  758. mod_timer(&spusched_timer, jiffies + SPUSCHED_TICK);
  759. wake_up_process(spusched_task);
  760. spu_calc_load(SPUSCHED_TICK);
  761. }
  762. static int spusched_thread(void *unused)
  763. {
  764. struct spu *spu;
  765. int node;
  766. while (!kthread_should_stop()) {
  767. set_current_state(TASK_INTERRUPTIBLE);
  768. schedule();
  769. for (node = 0; node < MAX_NUMNODES; node++) {
  770. mutex_lock(&cbe_spu_info[node].list_mutex);
  771. list_for_each_entry(spu, &cbe_spu_info[node].spus, cbe_list)
  772. if (spu->ctx)
  773. spusched_tick(spu->ctx);
  774. mutex_unlock(&cbe_spu_info[node].list_mutex);
  775. }
  776. }
  777. return 0;
  778. }
  779. void spuctx_switch_state(struct spu_context *ctx,
  780. enum spu_utilization_state new_state)
  781. {
  782. unsigned long long curtime;
  783. signed long long delta;
  784. struct timespec ts;
  785. struct spu *spu;
  786. enum spu_utilization_state old_state;
  787. ktime_get_ts(&ts);
  788. curtime = timespec_to_ns(&ts);
  789. delta = curtime - ctx->stats.tstamp;
  790. WARN_ON(!mutex_is_locked(&ctx->state_mutex));
  791. WARN_ON(delta < 0);
  792. spu = ctx->spu;
  793. old_state = ctx->stats.util_state;
  794. ctx->stats.util_state = new_state;
  795. ctx->stats.tstamp = curtime;
  796. /*
  797. * Update the physical SPU utilization statistics.
  798. */
  799. if (spu) {
  800. ctx->stats.times[old_state] += delta;
  801. spu->stats.times[old_state] += delta;
  802. spu->stats.util_state = new_state;
  803. spu->stats.tstamp = curtime;
  804. }
  805. }
  806. #define LOAD_INT(x) ((x) >> FSHIFT)
  807. #define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
  808. static int show_spu_loadavg(struct seq_file *s, void *private)
  809. {
  810. int a, b, c;
  811. a = spu_avenrun[0] + (FIXED_1/200);
  812. b = spu_avenrun[1] + (FIXED_1/200);
  813. c = spu_avenrun[2] + (FIXED_1/200);
  814. /*
  815. * Note that last_pid doesn't really make much sense for the
  816. * SPU loadavg (it even seems very odd on the CPU side...),
  817. * but we include it here to have a 100% compatible interface.
  818. */
  819. seq_printf(s, "%d.%02d %d.%02d %d.%02d %ld/%d %d\n",
  820. LOAD_INT(a), LOAD_FRAC(a),
  821. LOAD_INT(b), LOAD_FRAC(b),
  822. LOAD_INT(c), LOAD_FRAC(c),
  823. count_active_contexts(),
  824. atomic_read(&nr_spu_contexts),
  825. current->nsproxy->pid_ns->last_pid);
  826. return 0;
  827. }
  828. static int spu_loadavg_open(struct inode *inode, struct file *file)
  829. {
  830. return single_open(file, show_spu_loadavg, NULL);
  831. }
  832. static const struct file_operations spu_loadavg_fops = {
  833. .open = spu_loadavg_open,
  834. .read = seq_read,
  835. .llseek = seq_lseek,
  836. .release = single_release,
  837. };
  838. int __init spu_sched_init(void)
  839. {
  840. struct proc_dir_entry *entry;
  841. int err = -ENOMEM, i;
  842. spu_prio = kzalloc(sizeof(struct spu_prio_array), GFP_KERNEL);
  843. if (!spu_prio)
  844. goto out;
  845. for (i = 0; i < MAX_PRIO; i++) {
  846. INIT_LIST_HEAD(&spu_prio->runq[i]);
  847. __clear_bit(i, spu_prio->bitmap);
  848. }
  849. spin_lock_init(&spu_prio->runq_lock);
  850. setup_timer(&spusched_timer, spusched_wake, 0);
  851. spusched_task = kthread_run(spusched_thread, NULL, "spusched");
  852. if (IS_ERR(spusched_task)) {
  853. err = PTR_ERR(spusched_task);
  854. goto out_free_spu_prio;
  855. }
  856. entry = create_proc_entry("spu_loadavg", 0, NULL);
  857. if (!entry)
  858. goto out_stop_kthread;
  859. entry->proc_fops = &spu_loadavg_fops;
  860. pr_debug("spusched: tick: %d, min ticks: %d, default ticks: %d\n",
  861. SPUSCHED_TICK, MIN_SPU_TIMESLICE, DEF_SPU_TIMESLICE);
  862. return 0;
  863. out_stop_kthread:
  864. kthread_stop(spusched_task);
  865. out_free_spu_prio:
  866. kfree(spu_prio);
  867. out:
  868. return err;
  869. }
  870. void spu_sched_exit(void)
  871. {
  872. struct spu *spu;
  873. int node;
  874. remove_proc_entry("spu_loadavg", NULL);
  875. del_timer_sync(&spusched_timer);
  876. kthread_stop(spusched_task);
  877. for (node = 0; node < MAX_NUMNODES; node++) {
  878. mutex_lock(&cbe_spu_info[node].list_mutex);
  879. list_for_each_entry(spu, &cbe_spu_info[node].spus, cbe_list)
  880. if (spu->alloc_state != SPU_FREE)
  881. spu->alloc_state = SPU_FREE;
  882. mutex_unlock(&cbe_spu_info[node].list_mutex);
  883. }
  884. kfree(spu_prio);
  885. }