sched.c 29 KB

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