sched.c 26 KB

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