sched.c 27 KB

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