sched.c 28 KB

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