/arch/powerpc/platforms/cell/spufs/sched.c

http://github.com/mirrors/linux · C · 1145 lines · 745 code · 168 blank · 232 comment · 126 complexity · cc91f99f5bb5e347c457ea5ceae8b53f MD5 · raw file

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