1 /* Copyright (c) 2009, 2012 The Regents of the University of California
2 * Barret Rhoden <brho@cs.berkeley.edu>
3 * See LICENSE for details.
5 * Scheduling and dispatching. */
8 #include <corerequest.h>
17 #include <sys/queue.h>
18 #include <arsc_server.h>
20 /* Process Lists. 'unrunnable' is a holding list for SCPs that are running or
21 * waiting or otherwise not considered for sched decisions. */
22 struct proc_list unrunnable_scps = TAILQ_HEAD_INITIALIZER(unrunnable_scps);
23 struct proc_list runnable_scps = TAILQ_HEAD_INITIALIZER(runnable_scps);
24 /* mcp lists. we actually could get by with one list and a TAILQ_CONCAT, but
25 * I'm expecting to want the flexibility of the pointers later. */
26 struct proc_list all_mcps_1 = TAILQ_HEAD_INITIALIZER(all_mcps_1);
27 struct proc_list all_mcps_2 = TAILQ_HEAD_INITIALIZER(all_mcps_2);
28 struct proc_list *primary_mcps = &all_mcps_1;
29 struct proc_list *secondary_mcps = &all_mcps_2;
31 /* TAILQ of all unallocated, idle (CG) cores.
32 * Pulled from corealloc.c at the moment */
33 extern struct sched_pcore_tailq idlecores;
35 /* Helper, defined below */
36 static void __core_request(struct proc *p, uint32_t amt_needed);
37 static void add_to_list(struct proc *p, struct proc_list *list);
38 static void remove_from_list(struct proc *p, struct proc_list *list);
39 static void switch_lists(struct proc *p, struct proc_list *old,
40 struct proc_list *new);
41 static void __run_mcp_ksched(void *arg); /* don't call directly */
42 static uint32_t get_cores_needed(struct proc *p);
44 /* Locks / sync tools */
46 /* poke-style ksched - ensures the MCP ksched only runs once at a time. since
47 * only one mcp ksched runs at a time, while this is set, the ksched knows no
48 * cores are being allocated by other code (though they could be dealloc, due to
51 * The main value to this sync method is to make the 'make sure the ksched runs
52 * only once at a time and that it actually runs' invariant/desire wait-free, so
53 * that it can be called anywhere (deep event code, etc).
55 * As the ksched gets smarter, we'll probably embedd this poker in a bigger
56 * struct that can handle the posting of different types of work. */
57 struct poke_tracker ksched_poker = POKE_INITIALIZER(__run_mcp_ksched);
59 /* this 'big ksched lock' protects a bunch of things, which i may make fine
61 /* - protects the integrity of proc tailqs/structures, as well as the membership
62 * of a proc on those lists. proc lifetime within the ksched but outside this
63 * lock is protected by the proc kref. */
64 //spinlock_t proclist_lock = SPINLOCK_INITIALIZER; /* subsumed by bksl */
65 /* - protects the provisioning assignment, membership of sched_pcores in
66 * provision lists, and the integrity of all prov lists (the lists of each
67 * proc). does NOT protect spc->alloc_proc. */
68 //spinlock_t prov_lock = SPINLOCK_INITIALIZER;
69 /* - protects allocation structures: spc->alloc_proc, the integrity and
70 * membership of the idelcores tailq. */
71 //spinlock_t alloc_lock = SPINLOCK_INITIALIZER;
72 spinlock_t sched_lock = SPINLOCK_INITIALIZER;
74 /* Alarm struct, for our example 'timer tick' */
75 struct alarm_waiter ksched_waiter;
77 #define TIMER_TICK_USEC 10000 /* 10msec */
79 /* Helper: Sets up a timer tick on the calling core to go off 10 msec from now.
80 * This assumes the calling core is an LL core, etc. */
81 static void set_ksched_alarm(void)
83 set_awaiter_rel(&ksched_waiter, TIMER_TICK_USEC);
84 set_alarm(&per_cpu_info[core_id()].tchain, &ksched_waiter);
87 /* Need a kmsg to just run the sched, but not to rearm */
88 static void __just_sched(uint32_t srcid, long a0, long a1, long a2)
93 /* RKM alarm, to run the scheduler tick (not in interrupt context) and reset the
94 * alarm. Note that interrupts will be disabled, but this is not the same as
95 * interrupt context. We're a routine kmsg, which means the core is in a
97 static void __ksched_tick(struct alarm_waiter *waiter)
99 /* TODO: imagine doing some accounting here */
101 /* Set our alarm to go off, incrementing from our last tick (instead of
102 * setting it relative to now, since some time has passed since the alarm
103 * first went off. Note, this may be now or in the past! */
104 set_awaiter_inc(&ksched_waiter, TIMER_TICK_USEC);
105 set_alarm(&per_cpu_info[core_id()].tchain, &ksched_waiter);
108 void schedule_init(void)
110 spin_lock(&sched_lock);
111 assert(!core_id()); /* want the alarm on core0 for now */
112 init_awaiter(&ksched_waiter, __ksched_tick);
115 spin_unlock(&sched_lock);
117 #ifdef CONFIG_ARSC_SERVER
118 int arsc_coreid = get_any_idle_core();
119 assert(arsc_coreid >= 0);
120 send_kernel_message(arsc_coreid, arsc_server, 0, 0, 0, KMSG_ROUTINE);
121 printk("Using core %d for the ARSC server\n", arsc_coreid);
122 #endif /* CONFIG_ARSC_SERVER */
125 /* Round-robins on whatever list it's on */
126 static void add_to_list(struct proc *p, struct proc_list *new)
128 assert(!(p->ksched_data.cur_list));
129 TAILQ_INSERT_TAIL(new, p, ksched_data.proc_link);
130 p->ksched_data.cur_list = new;
133 static void remove_from_list(struct proc *p, struct proc_list *old)
135 assert(p->ksched_data.cur_list == old);
136 TAILQ_REMOVE(old, p, ksched_data.proc_link);
137 p->ksched_data.cur_list = 0;
140 static void switch_lists(struct proc *p, struct proc_list *old,
141 struct proc_list *new)
143 remove_from_list(p, old);
147 /* Removes from whatever list p is on */
148 static void remove_from_any_list(struct proc *p)
150 if (p->ksched_data.cur_list) {
151 TAILQ_REMOVE(p->ksched_data.cur_list, p, ksched_data.proc_link);
152 p->ksched_data.cur_list = 0;
156 /************** Process Management Callbacks **************/
158 * - the proc lock is NOT held for any of these calls. currently, there is no
159 * lock ordering between the sched lock and the proc lock. since the proc
160 * code doesn't know what we do, it doesn't hold its lock when calling our
162 * - since the proc lock isn't held, the proc could be dying, which means we
163 * will receive a __sched_proc_destroy() either before or after some of these
164 * other CBs. the CBs related to list management need to check and abort if
166 void __sched_proc_register(struct proc *p)
168 assert(p->state != PROC_DYING); /* shouldn't be abel to happen yet */
169 /* one ref for the proc's existence, cradle-to-grave */
170 proc_incref(p, 1); /* need at least this OR the 'one for existing' */
171 spin_lock(&sched_lock);
172 corealloc_proc_init(p);
173 add_to_list(p, &unrunnable_scps);
174 spin_unlock(&sched_lock);
177 /* Returns 0 if it succeeded, an error code otherwise. */
178 void __sched_proc_change_to_m(struct proc *p)
180 spin_lock(&sched_lock);
181 /* Need to make sure they aren't dying. if so, we already dealt with their
182 * list membership, etc (or soon will). taking advantage of the 'immutable
183 * state' of dying (so long as refs are held). */
184 if (p->state == PROC_DYING) {
185 spin_unlock(&sched_lock);
188 /* Catch user bugs */
189 if (!p->procdata->res_req[RES_CORES].amt_wanted) {
190 printk("[kernel] process needs to specify amt_wanted\n");
191 p->procdata->res_req[RES_CORES].amt_wanted = 1;
193 /* For now, this should only ever be called on an unrunnable. It's
194 * probably a bug, at this stage in development, to do o/w. */
195 remove_from_list(p, &unrunnable_scps);
196 //remove_from_any_list(p); /* ^^ instead of this */
197 add_to_list(p, primary_mcps);
198 spin_unlock(&sched_lock);
199 //poke_ksched(p, RES_CORES);
202 /* Sched callback called when the proc dies. pc_arr holds the cores the proc
203 * had, if any, and nr_cores tells us how many are in the array.
205 * An external, edible ref is passed in. when we return and they decref,
206 * __proc_free will be called (when the last one is done). */
207 void __sched_proc_destroy(struct proc *p, uint32_t *pc_arr, uint32_t nr_cores)
209 spin_lock(&sched_lock);
210 /* Unprovision any cores. Note this is different than track_core_dealloc.
211 * The latter does bookkeeping when an allocation changes. This is a
212 * bulk *provisioning* change. */
213 __unprovision_all_cores(p);
214 /* Remove from whatever list we are on (if any - might not be on one if it
215 * was in the middle of __run_mcp_sched) */
216 remove_from_any_list(p);
218 __track_core_dealloc_bulk(p, pc_arr, nr_cores);
219 spin_unlock(&sched_lock);
220 /* Drop the cradle-to-the-grave reference, jet-li */
224 /* ksched callbacks. p just woke up and is UNLOCKED. */
225 void __sched_mcp_wakeup(struct proc *p)
227 spin_lock(&sched_lock);
228 if (p->state == PROC_DYING) {
229 spin_unlock(&sched_lock);
232 /* could try and prioritize p somehow (move it to the front of the list). */
233 spin_unlock(&sched_lock);
234 /* note they could be dying at this point too. */
235 poke(&ksched_poker, p);
238 /* ksched callbacks. p just woke up and is UNLOCKED. */
239 void __sched_scp_wakeup(struct proc *p)
241 spin_lock(&sched_lock);
242 if (p->state == PROC_DYING) {
243 spin_unlock(&sched_lock);
246 /* might not be on a list if it is new. o/w, it should be unrunnable */
247 remove_from_any_list(p);
248 add_to_list(p, &runnable_scps);
249 spin_unlock(&sched_lock);
250 /* we could be on a CG core, and all the mgmt cores could be halted. if we
251 * don't tell one of them about the new proc, they will sleep until the
252 * timer tick goes off. */
253 if (!management_core()) {
254 /* TODO: pick a better core and only send if halted.
256 * FYI, a POKE on x86 might lose a rare race with halt code, since the
257 * poke handler does not abort halts. if this happens, the next timer
258 * IRQ would wake up the core.
260 * ideally, we'd know if a specific mgmt core is sleeping and wake it
261 * up. o/w, we could interrupt an already-running mgmt core that won't
262 * get to our new proc anytime soon. also, by poking core 0, a
263 * different mgmt core could remain idle (and this process would sleep)
264 * until its tick goes off */
265 send_ipi(0, I_POKE_CORE);
269 /* Callback to return a core to the ksched, which tracks it as idle and
270 * deallocated from p. The proclock is held (__core_req depends on that).
272 * This also is a trigger, telling us we have more cores. We could/should make
273 * a scheduling decision (or at least plan to). */
274 void __sched_put_idle_core(struct proc *p, uint32_t coreid)
276 struct sched_pcore *spc = pcoreid2spc(coreid);
277 spin_lock(&sched_lock);
278 __track_core_dealloc(p, coreid);
279 spin_unlock(&sched_lock);
282 /* Callback, bulk interface for put_idle. The proclock is held for this. */
283 void __sched_put_idle_cores(struct proc *p, uint32_t *pc_arr, uint32_t num)
285 spin_lock(&sched_lock);
286 __track_core_dealloc_bulk(p, pc_arr, num);
287 spin_unlock(&sched_lock);
288 /* could trigger a sched decision here */
291 /* mgmt/LL cores should call this to schedule the calling core and give it to an
292 * SCP. will also prune the dead SCPs from the list. hold the lock before
293 * calling. returns TRUE if it scheduled a proc. */
294 static bool __schedule_scp(void)
296 // TODO: sort out lock ordering (proc_run_s also locks)
298 uint32_t pcoreid = core_id();
299 struct per_cpu_info *pcpui = &per_cpu_info[pcoreid];
301 /* if there are any runnables, run them here and put any currently running
302 * SCP on the tail of the runnable queue. */
303 if ((p = TAILQ_FIRST(&runnable_scps))) {
304 /* protect owning proc, cur_ctx, etc. note this nests with the
305 * calls in proc_yield_s */
306 disable_irqsave(&state);
307 /* someone is currently running, dequeue them */
308 if (pcpui->owning_proc) {
309 spin_lock(&pcpui->owning_proc->proc_lock);
310 /* process might be dying, with a KMSG to clean it up waiting on
311 * this core. can't do much, so we'll attempt to restart */
312 if (pcpui->owning_proc->state == PROC_DYING) {
313 send_kernel_message(core_id(), __just_sched, 0, 0, 0,
315 spin_unlock(&pcpui->owning_proc->proc_lock);
316 enable_irqsave(&state);
319 printd("Descheduled %d in favor of %d\n", pcpui->owning_proc->pid,
321 __proc_set_state(pcpui->owning_proc, PROC_RUNNABLE_S);
322 /* Saving FP state aggressively. Odds are, the SCP was hit by an
323 * IRQ and has a HW ctx, in which case we must save. */
324 __proc_save_fpu_s(pcpui->owning_proc);
325 __proc_save_context_s(pcpui->owning_proc, pcpui->cur_ctx);
326 vcore_account_offline(pcpui->owning_proc, 0);
327 __seq_start_write(&p->procinfo->coremap_seqctr);
329 __seq_end_write(&p->procinfo->coremap_seqctr);
330 spin_unlock(&pcpui->owning_proc->proc_lock);
331 /* round-robin the SCPs (inserts at the end of the queue) */
332 switch_lists(pcpui->owning_proc, &unrunnable_scps, &runnable_scps);
333 clear_owning_proc(pcoreid);
334 /* Note we abandon core. It's not strictly necessary. If
335 * we didn't, the TLB would still be loaded with the old
336 * one, til we proc_run_s, and the various paths in
337 * proc_run_s would pick it up. This way is a bit safer for
338 * future changes, but has an extra (empty) TLB flush. */
341 /* Run the new proc */
342 switch_lists(p, &runnable_scps, &unrunnable_scps);
343 printd("PID of the SCP i'm running: %d\n", p->pid);
344 proc_run_s(p); /* gives it core we're running on */
345 enable_irqsave(&state);
351 /* Returns how many new cores p needs. This doesn't lock the proc, so your
352 * answer might be stale. */
353 static uint32_t get_cores_needed(struct proc *p)
355 uint32_t amt_wanted, amt_granted;
356 amt_wanted = p->procdata->res_req[RES_CORES].amt_wanted;
357 /* Help them out - if they ask for something impossible, give them 1 so they
358 * can make some progress. (this is racy, and unnecessary). */
359 if (amt_wanted > p->procinfo->max_vcores) {
360 printk("[kernel] proc %d wanted more than max, wanted %d\n", p->pid,
362 p->procdata->res_req[RES_CORES].amt_wanted = 1;
365 /* There are a few cases where amt_wanted is 0, but they are still RUNNABLE
366 * (involving yields, events, and preemptions). In these cases, give them
367 * at least 1, so they can make progress and yield properly. If they are
368 * not WAITING, they did not yield and may have missed a message. */
370 /* could ++, but there could be a race and we don't want to give them
371 * more than they ever asked for (in case they haven't prepped) */
372 p->procdata->res_req[RES_CORES].amt_wanted = 1;
375 /* amt_granted is racy - they could be *yielding*, but currently they can't
376 * be getting any new cores if the caller is in the mcp_ksched. this is
377 * okay - we won't accidentally give them more cores than they *ever* wanted
378 * (which could crash them), but our answer might be a little stale. */
379 amt_granted = p->procinfo->res_grant[RES_CORES];
380 /* Do not do an assert like this: it could fail (yield in progress): */
381 //assert(amt_granted == p->procinfo->num_vcores);
382 if (amt_wanted <= amt_granted)
384 return amt_wanted - amt_granted;
387 /* Actual work of the MCP kscheduler. if we were called by poke_ksched, *arg
388 * might be the process who wanted special service. this would be the case if
389 * we weren't already running the ksched. Sort of a ghetto way to "post work",
390 * such that it's an optimization. */
391 static void __run_mcp_ksched(void *arg)
393 struct proc *p, *temp;
395 struct proc_list *temp_mcp_list;
396 /* locking to protect the MCP lists' integrity and membership */
397 spin_lock(&sched_lock);
398 /* 2-pass scheme: check each proc on the primary list (FCFS). if they need
399 * nothing, put them on the secondary list. if they need something, rip
400 * them off the list, service them, and if they are still not dying, put
401 * them on the secondary list. We cull the entire primary list, so that
402 * when we start from the beginning each time, we aren't repeatedly checking
403 * procs we looked at on previous waves.
405 * TODO: we could modify this such that procs that we failed to service move
406 * to yet another list or something. We can also move the WAITINGs to
407 * another list and have wakeup move them back, etc. */
408 while (!TAILQ_EMPTY(primary_mcps)) {
409 TAILQ_FOREACH_SAFE(p, primary_mcps, ksched_data.proc_link, temp) {
410 if (p->state == PROC_WAITING) { /* unlocked peek at the state */
411 switch_lists(p, primary_mcps, secondary_mcps);
414 amt_needed = get_cores_needed(p);
416 switch_lists(p, primary_mcps, secondary_mcps);
419 /* o/w, we want to give cores to this proc */
420 remove_from_list(p, primary_mcps);
421 /* now it won't die, but it could get removed from lists and have
422 * its stuff unprov'd when we unlock */
424 /* GIANT WARNING: __core_req will unlock the sched lock for a bit.
425 * It will return with it locked still. We could unlock before we
426 * pass in, but they will relock right away. */
427 // notionally_unlock(&ksched_lock); /* for mouse-eyed viewers */
428 __core_request(p, amt_needed);
429 // notionally_lock(&ksched_lock);
430 /* Peeking at the state is okay, since we hold a ref. Once it is
431 * DYING, it'll remain DYING until we decref. And if there is a
432 * concurrent death, that will spin on the ksched lock (which we
433 * hold, and which protects the proc lists). */
434 if (p->state != PROC_DYING)
435 add_to_list(p, secondary_mcps);
436 proc_decref(p); /* fyi, this may trigger __proc_free */
437 /* need to break: the proc lists may have changed when we unlocked
438 * in core_req in ways that the FOREACH_SAFE can't handle. */
442 /* at this point, we moved all the procs over to the secondary list, and
443 * attempted to service the ones that wanted something. now just swap the
444 * lists for the next invocation of the ksched. */
445 temp_mcp_list = primary_mcps;
446 primary_mcps = secondary_mcps;
447 secondary_mcps = temp_mcp_list;
448 spin_unlock(&sched_lock);
451 /* Something has changed, and for whatever reason the scheduler should
454 * Don't call this if you are processing a syscall or otherwise care about your
455 * kthread variables, cur_proc/owning_proc, etc.
457 * Don't call this from interrupt context (grabs proclocks). */
458 void run_scheduler(void)
460 /* MCP scheduling: post work, then poke. for now, i just want the func to
461 * run again, so merely a poke is sufficient. */
462 poke(&ksched_poker, 0);
463 if (management_core()) {
464 spin_lock(&sched_lock);
466 spin_unlock(&sched_lock);
470 /* A process is asking the ksched to look at its resource desires. The
471 * scheduler is free to ignore this, for its own reasons, so long as it
472 * eventually gets around to looking at resource desires. */
473 void poke_ksched(struct proc *p, unsigned int res_type)
475 /* ignoring res_type for now. could post that if we wanted (would need some
476 * other structs/flags) */
477 if (!__proc_is_mcp(p))
479 poke(&ksched_poker, p);
482 /* The calling cpu/core has nothing to do and plans to idle/halt. This is an
483 * opportunity to pick the nature of that halting (low power state, etc), or
484 * provide some other work (_Ss on LL cores). Note that interrupts are
485 * disabled, and if you return, the core will cpu_halt(). */
488 bool new_proc = FALSE;
489 if (!management_core())
491 spin_lock(&sched_lock);
492 new_proc = __schedule_scp();
493 spin_unlock(&sched_lock);
494 /* if we just scheduled a proc, we need to manually restart it, instead of
495 * returning. if we return, the core will halt. */
500 /* Could drop into the monitor if there are no processes at all. For now,
501 * the 'call of the giraffe' suffices. */
504 /* Available resources changed (plus or minus). Some parts of the kernel may
505 * call this if a particular resource that is 'quantity-based' changes. Things
506 * like available RAM to processes, bandwidth, etc. Cores would probably be
507 * inappropriate, since we need to know which specific core is now free. */
508 void avail_res_changed(int res_type, long change)
510 printk("[kernel] ksched doesn't track any resources yet!\n");
513 int get_any_idle_core(void)
515 spin_lock(&sched_lock);
516 int ret = __get_any_idle_core();
517 spin_unlock(&sched_lock);
521 int get_specific_idle_core(int coreid)
523 spin_lock(&sched_lock);
524 int ret = __get_specific_idle_core(coreid);
525 spin_unlock(&sched_lock);
529 /* similar to __sched_put_idle_core, but without the prov tracking */
530 void put_idle_core(int coreid)
532 spin_lock(&sched_lock);
533 __put_idle_core(coreid);
534 spin_unlock(&sched_lock);
537 /* This deals with a request for more cores. The amt of new cores needed is
538 * passed in. The ksched lock is held, but we are free to unlock if we want
539 * (and we must, if calling out of the ksched to anything high-level).
541 * Side note: if we want to warn, then we can't deal with this proc's prov'd
542 * cores until we wait til the alarm goes off. would need to put all
543 * alarmed cores on a list and wait til the alarm goes off to do the full
544 * preempt. and when those cores come in voluntarily, we'd need to know to
545 * give them to this proc. */
546 static void __core_request(struct proc *p, uint32_t amt_needed)
548 uint32_t nr_to_grant = 0;
549 uint32_t corelist[num_cores];
550 struct sched_pcore *spc_i, *temp;
551 struct proc *proc_to_preempt;
553 /* we come in holding the ksched lock, and we hold it here to protect
554 * allocations and provisioning. */
555 /* get all available cores from their prov_not_alloc list. the list might
556 * change when we unlock (new cores added to it, or the entire list emptied,
557 * but no core allocations will happen (we hold the poke)). */
558 while (nr_to_grant != amt_needed) {
559 /* Find the next best core to allocate to p. It may be a core
560 * provisioned to p, and it might not be. */
561 spc_i = __find_best_core_to_alloc(p);
562 /* If no core is returned, we know that there are no more cores to give
563 * out, so we exit the loop. */
566 /* If the pcore chosen currently has a proc allocated to it, we know
567 * it must be provisioned to p, but not allocated to it. We need to try
568 * to preempt. After this block, the core will be track_dealloc'd and
569 * on the idle list (regardless of whether we had to preempt or not) */
570 if (get_alloc_proc(spc_i)) {
571 proc_to_preempt = get_alloc_proc(spc_i);
572 /* would break both preemption and maybe the later decref */
573 assert(proc_to_preempt != p);
574 /* need to keep a valid, external ref when we unlock */
575 proc_incref(proc_to_preempt, 1);
576 spin_unlock(&sched_lock);
577 /* sending no warning time for now - just an immediate preempt. */
578 success = proc_preempt_core(proc_to_preempt, spc2pcoreid(spc_i), 0);
579 /* reaquire locks to protect provisioning and idle lists */
580 spin_lock(&sched_lock);
582 /* we preempted it before the proc could yield or die.
583 * alloc_proc should not have changed (it'll change in death and
584 * idle CBs). the core is not on the idle core list. (if we
585 * ever have proc alloc lists, it'll still be on the old proc's
587 assert(get_alloc_proc(spc_i));
588 /* regardless of whether or not it is still prov to p, we need
589 * to note its dealloc. we are doing some excessive checking of
590 * p == prov_proc, but using this helper is a lot clearer. */
591 __track_core_dealloc(proc_to_preempt, spc2pcoreid(spc_i));
593 /* the preempt failed, which should only happen if the pcore was
594 * unmapped (could be dying, could be yielding, but NOT
595 * preempted). whoever unmapped it also triggered (or will soon
596 * trigger) a track_core_dealloc and put it on the idle list.
597 * Our signal for this is get_alloc_proc() being 0. We need to
598 * spin and let whoever is trying to free the core grab the
599 * ksched lock. We could use an 'ignore_next_idle' flag per
600 * sched_pcore, but it's not critical anymore.
602 * Note, we're relying on us being the only preemptor - if the
603 * core was unmapped by *another* preemptor, there would be no
604 * way of knowing the core was made idle *yet* (the success
605 * branch in another thread). likewise, if there were another
606 * allocator, the pcore could have been put on the idle list and
607 * then quickly removed/allocated. */
609 while (get_alloc_proc(spc_i)) {
610 /* this loop should be very rare */
611 spin_unlock(&sched_lock);
613 spin_lock(&sched_lock);
616 /* no longer need to keep p_to_pre alive */
617 proc_decref(proc_to_preempt);
618 /* might not be prov to p anymore (rare race). spc_i is idle - we
619 * might get it later, or maybe we'll give it to its rightful proc*/
620 if (get_prov_proc(spc_i) != p)
623 /* At this point, the pcore is idle, regardless of how we got here
624 * (successful preempt, failed preempt, or it was idle in the first
625 * place). We also know the core is still provisioned to us. Lets add
626 * it to the corelist for p (so we can give it to p in bulk later), and
627 * track its allocation with p (so our internal data structures stay in
628 * sync). We rely on the fact that we are the only allocator (spc_i is
629 * still idle, despite (potentially) unlocking during the preempt
630 * attempt above). It is guaranteed to be track_dealloc'd()
631 * (regardless of how we got here). */
632 corelist[nr_to_grant] = spc2pcoreid(spc_i);
634 __track_core_alloc(p, spc2pcoreid(spc_i));
636 /* Now, actually give them out */
638 /* Need to unlock before calling out to proc code. We are somewhat
639 * relying on being the only one allocating 'thread' here, since another
640 * allocator could have seen these cores (if they are prov to some proc)
641 * and could be trying to give them out (and assuming they are already
642 * on the idle list). */
643 spin_unlock(&sched_lock);
644 /* give them the cores. this will start up the extras if RUNNING_M. */
645 spin_lock(&p->proc_lock);
646 /* if they fail, it is because they are WAITING or DYING. we could give
647 * the cores to another proc or whatever. for the current type of
648 * ksched, we'll just put them back on the pile and return. Note, the
649 * ksched could check the states after locking, but it isn't necessary:
650 * just need to check at some point in the ksched loop. */
651 if (__proc_give_cores(p, corelist, nr_to_grant)) {
652 spin_unlock(&p->proc_lock);
653 /* we failed, put the cores and track their dealloc. lock is
654 * protecting those structures. */
655 spin_lock(&sched_lock);
656 __track_core_dealloc_bulk(p, corelist, nr_to_grant);
658 /* at some point after giving cores, call proc_run_m() (harmless on
659 * RUNNING_Ms). You can give small groups of cores, then run them
660 * (which is more efficient than interleaving runs with the gives
661 * for bulk preempted processes). */
663 spin_unlock(&p->proc_lock);
664 /* main mcp_ksched wants this held (it came to __core_req held) */
665 spin_lock(&sched_lock);
668 /* note the ksched lock is still held */
671 /* Provision a core to a process. This function wraps the primary logic
672 * implemented in __provision_core, with a lock, error checking, etc. */
673 int provision_core(struct proc *p, uint32_t pcoreid)
675 struct sched_pcore *spc;
676 /* Make sure we aren't asking for something that doesn't exist (bounds check
677 * on the pcore array) */
678 if (!(pcoreid < num_cores)) {
682 /* Don't allow the provisioning of LL cores */
683 if (is_ll_core(pcoreid)) {
687 spc = pcoreid2spc(pcoreid);
688 /* Note the sched lock protects the spc tailqs for all procs in this code.
689 * If we need a finer grained sched lock, this is one place where we could
690 * have a different lock */
691 spin_lock(&sched_lock);
692 __provision_core(p, spc);
693 spin_unlock(&sched_lock);
697 /************** Debugging **************/
698 void sched_diag(void)
701 spin_lock(&sched_lock);
702 TAILQ_FOREACH(p, &runnable_scps, ksched_data.proc_link)
703 printk("Runnable _S PID: %d\n", p->pid);
704 TAILQ_FOREACH(p, &unrunnable_scps, ksched_data.proc_link)
705 printk("Unrunnable _S PID: %d\n", p->pid);
706 TAILQ_FOREACH(p, primary_mcps, ksched_data.proc_link)
707 printk("Primary MCP PID: %d\n", p->pid);
708 TAILQ_FOREACH(p, secondary_mcps, ksched_data.proc_link)
709 printk("Secondary MCP PID: %d\n", p->pid);
710 spin_unlock(&sched_lock);
714 void print_resources(struct proc *p)
716 printk("--------------------\n");
717 printk("PID: %d\n", p->pid);
718 printk("--------------------\n");
719 for (int i = 0; i < MAX_NUM_RESOURCES; i++)
720 printk("Res type: %02d, amt wanted: %08d, amt granted: %08d\n", i,
721 p->procdata->res_req[i].amt_wanted, p->procinfo->res_grant[i]);
724 void print_all_resources(void)
727 void __print_resources(void *item, void *opaque)
729 print_resources((struct proc*)item);
731 spin_lock(&pid_hash_lock);
732 hash_for_each(pid_hash, __print_resources, NULL);
733 spin_unlock(&pid_hash_lock);
736 void next_core_to_alloc(uint32_t pcoreid)
738 spin_lock(&sched_lock);
739 __next_core_to_alloc(pcoreid);
740 spin_unlock(&sched_lock);
743 void sort_idle_cores(void)
745 spin_lock(&sched_lock);
747 spin_unlock(&sched_lock);