sched.c 30 KB

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