sched.c 29 KB

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