sched.c 26 KB

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