sched.c 27 KB

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