sched.c 30 KB

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