sched.c 27 KB

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