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 bool is_ll_core(uint32_t pcoreid);
42 static void __run_mcp_ksched(void *arg); /* don't call directly */
43 static uint32_t get_cores_needed(struct proc *p);
45 /* Locks / sync tools */
47 /* poke-style ksched - ensures the MCP ksched only runs once at a time. since
48 * only one mcp ksched runs at a time, while this is set, the ksched knows no
49 * cores are being allocated by other code (though they could be dealloc, due to
52 * The main value to this sync method is to make the 'make sure the ksched runs
53 * only once at a time and that it actually runs' invariant/desire wait-free, so
54 * that it can be called anywhere (deep event code, etc).
56 * As the ksched gets smarter, we'll probably embedd this poker in a bigger
57 * struct that can handle the posting of different types of work. */
58 struct poke_tracker ksched_poker = POKE_INITIALIZER(__run_mcp_ksched);
60 /* this 'big ksched lock' protects a bunch of things, which i may make fine
62 /* - protects the integrity of proc tailqs/structures, as well as the membership
63 * of a proc on those lists. proc lifetime within the ksched but outside this
64 * lock is protected by the proc kref. */
65 //spinlock_t proclist_lock = SPINLOCK_INITIALIZER; /* subsumed by bksl */
66 /* - protects the provisioning assignment, membership of sched_pcores in
67 * provision lists, and the integrity of all prov lists (the lists of each
68 * proc). does NOT protect spc->alloc_proc. */
69 //spinlock_t prov_lock = SPINLOCK_INITIALIZER;
70 /* - protects allocation structures: spc->alloc_proc, the integrity and
71 * membership of the idelcores tailq. */
72 //spinlock_t alloc_lock = SPINLOCK_INITIALIZER;
73 spinlock_t sched_lock = SPINLOCK_INITIALIZER;
75 /* Alarm struct, for our example 'timer tick' */
76 struct alarm_waiter ksched_waiter;
78 #define TIMER_TICK_USEC 10000 /* 10msec */
80 /* Helper: Sets up a timer tick on the calling core to go off 10 msec from now.
81 * This assumes the calling core is an LL core, etc. */
82 static void set_ksched_alarm(void)
84 set_awaiter_rel(&ksched_waiter, TIMER_TICK_USEC);
85 set_alarm(&per_cpu_info[core_id()].tchain, &ksched_waiter);
88 /* Need a kmsg to just run the sched, but not to rearm */
89 static void __just_sched(uint32_t srcid, long a0, long a1, long a2)
94 /* RKM alarm, to run the scheduler tick (not in interrupt context) and reset the
95 * alarm. Note that interrupts will be disabled, but this is not the same as
96 * interrupt context. We're a routine kmsg, which means the core is in a
98 static void __ksched_tick(struct alarm_waiter *waiter)
100 /* TODO: imagine doing some accounting here */
102 /* Set our alarm to go off, incrementing from our last tick (instead of
103 * setting it relative to now, since some time has passed since the alarm
104 * first went off. Note, this may be now or in the past! */
105 set_awaiter_inc(&ksched_waiter, TIMER_TICK_USEC);
106 set_alarm(&per_cpu_info[core_id()].tchain, &ksched_waiter);
109 void schedule_init(void)
111 spin_lock(&sched_lock);
112 assert(!core_id()); /* want the alarm on core0 for now */
113 init_awaiter(&ksched_waiter, __ksched_tick);
116 spin_unlock(&sched_lock);
118 #ifdef CONFIG_ARSC_SERVER
119 int arsc_coreid = get_any_idle_core();
120 assert(arsc_coreid >= 0);
121 send_kernel_message(arsc_coreid, arsc_server, 0, 0, 0, KMSG_ROUTINE);
122 printk("Using core %d for the ARSC server\n", arsc_coreid);
123 #endif /* CONFIG_ARSC_SERVER */
126 /* Round-robins on whatever list it's on */
127 static void add_to_list(struct proc *p, struct proc_list *new)
129 assert(!(p->ksched_data.cur_list));
130 TAILQ_INSERT_TAIL(new, p, ksched_data.proc_link);
131 p->ksched_data.cur_list = new;
134 static void remove_from_list(struct proc *p, struct proc_list *old)
136 assert(p->ksched_data.cur_list == old);
137 TAILQ_REMOVE(old, p, ksched_data.proc_link);
138 p->ksched_data.cur_list = 0;
141 static void switch_lists(struct proc *p, struct proc_list *old,
142 struct proc_list *new)
144 remove_from_list(p, old);
148 /* Removes from whatever list p is on */
149 static void remove_from_any_list(struct proc *p)
151 if (p->ksched_data.cur_list) {
152 TAILQ_REMOVE(p->ksched_data.cur_list, p, ksched_data.proc_link);
153 p->ksched_data.cur_list = 0;
157 /************** Process Management Callbacks **************/
159 * - the proc lock is NOT held for any of these calls. currently, there is no
160 * lock ordering between the sched lock and the proc lock. since the proc
161 * code doesn't know what we do, it doesn't hold its lock when calling our
163 * - since the proc lock isn't held, the proc could be dying, which means we
164 * will receive a __sched_proc_destroy() either before or after some of these
165 * other CBs. the CBs related to list management need to check and abort if
167 void __sched_proc_register(struct proc *p)
169 assert(p->state != PROC_DYING); /* shouldn't be abel to happen yet */
170 /* one ref for the proc's existence, cradle-to-grave */
171 proc_incref(p, 1); /* need at least this OR the 'one for existing' */
172 spin_lock(&sched_lock);
173 coreprov_proc_init(p);
174 add_to_list(p, &unrunnable_scps);
175 spin_unlock(&sched_lock);
178 /* Returns 0 if it succeeded, an error code otherwise. */
179 void __sched_proc_change_to_m(struct proc *p)
181 spin_lock(&sched_lock);
182 /* Need to make sure they aren't dying. if so, we already dealt with their
183 * list membership, etc (or soon will). taking advantage of the 'immutable
184 * state' of dying (so long as refs are held). */
185 if (p->state == PROC_DYING) {
186 spin_unlock(&sched_lock);
189 /* Catch user bugs */
190 if (!p->procdata->res_req[RES_CORES].amt_wanted) {
191 printk("[kernel] process needs to specify amt_wanted\n");
192 p->procdata->res_req[RES_CORES].amt_wanted = 1;
194 /* For now, this should only ever be called on an unrunnable. It's
195 * probably a bug, at this stage in development, to do o/w. */
196 remove_from_list(p, &unrunnable_scps);
197 //remove_from_any_list(p); /* ^^ instead of this */
198 add_to_list(p, primary_mcps);
199 spin_unlock(&sched_lock);
200 //poke_ksched(p, RES_CORES);
203 /* Sched callback called when the proc dies. pc_arr holds the cores the proc
204 * had, if any, and nr_cores tells us how many are in the array.
206 * An external, edible ref is passed in. when we return and they decref,
207 * __proc_free will be called (when the last one is done). */
208 void __sched_proc_destroy(struct proc *p, uint32_t *pc_arr, uint32_t nr_cores)
210 spin_lock(&sched_lock);
211 /* Unprovision any cores. Note this is different than track_core_dealloc.
212 * The latter does bookkeeping when an allocation changes. This is a
213 * bulk *provisioning* change. */
214 __unprovision_all_cores(p);
215 /* Remove from whatever list we are on (if any - might not be on one if it
216 * was in the middle of __run_mcp_sched) */
217 remove_from_any_list(p);
219 __track_core_dealloc_bulk(p, pc_arr, nr_cores);
220 spin_unlock(&sched_lock);
221 /* Drop the cradle-to-the-grave reference, jet-li */
225 /* ksched callbacks. p just woke up and is UNLOCKED. */
226 void __sched_mcp_wakeup(struct proc *p)
228 spin_lock(&sched_lock);
229 if (p->state == PROC_DYING) {
230 spin_unlock(&sched_lock);
233 /* could try and prioritize p somehow (move it to the front of the list). */
234 spin_unlock(&sched_lock);
235 /* note they could be dying at this point too. */
236 poke(&ksched_poker, p);
239 /* ksched callbacks. p just woke up and is UNLOCKED. */
240 void __sched_scp_wakeup(struct proc *p)
242 spin_lock(&sched_lock);
243 if (p->state == PROC_DYING) {
244 spin_unlock(&sched_lock);
247 /* might not be on a list if it is new. o/w, it should be unrunnable */
248 remove_from_any_list(p);
249 add_to_list(p, &runnable_scps);
250 spin_unlock(&sched_lock);
251 /* we could be on a CG core, and all the mgmt cores could be halted. if we
252 * don't tell one of them about the new proc, they will sleep until the
253 * timer tick goes off. */
254 if (!management_core()) {
255 /* TODO: pick a better core and only send if halted.
257 * FYI, a POKE on x86 might lose a rare race with halt code, since the
258 * poke handler does not abort halts. if this happens, the next timer
259 * IRQ would wake up the core.
261 * ideally, we'd know if a specific mgmt core is sleeping and wake it
262 * up. o/w, we could interrupt an already-running mgmt core that won't
263 * get to our new proc anytime soon. also, by poking core 0, a
264 * different mgmt core could remain idle (and this process would sleep)
265 * until its tick goes off */
266 send_ipi(0, I_POKE_CORE);
270 /* Callback to return a core to the ksched, which tracks it as idle and
271 * deallocated from p. The proclock is held (__core_req depends on that).
273 * This also is a trigger, telling us we have more cores. We could/should make
274 * a scheduling decision (or at least plan to). */
275 void __sched_put_idle_core(struct proc *p, uint32_t coreid)
277 struct sched_pcore *spc = pcoreid2spc(coreid);
278 spin_lock(&sched_lock);
279 __track_core_dealloc(p, coreid);
280 spin_unlock(&sched_lock);
283 /* Callback, bulk interface for put_idle. The proclock is held for this. */
284 void __sched_put_idle_cores(struct proc *p, uint32_t *pc_arr, uint32_t num)
286 spin_lock(&sched_lock);
287 __track_core_dealloc_bulk(p, pc_arr, num);
288 spin_unlock(&sched_lock);
289 /* could trigger a sched decision here */
292 /* mgmt/LL cores should call this to schedule the calling core and give it to an
293 * SCP. will also prune the dead SCPs from the list. hold the lock before
294 * calling. returns TRUE if it scheduled a proc. */
295 static bool __schedule_scp(void)
297 // TODO: sort out lock ordering (proc_run_s also locks)
299 uint32_t pcoreid = core_id();
300 struct per_cpu_info *pcpui = &per_cpu_info[pcoreid];
302 /* if there are any runnables, run them here and put any currently running
303 * SCP on the tail of the runnable queue. */
304 if ((p = TAILQ_FIRST(&runnable_scps))) {
305 /* protect owning proc, cur_ctx, etc. note this nests with the
306 * calls in proc_yield_s */
307 disable_irqsave(&state);
308 /* someone is currently running, dequeue them */
309 if (pcpui->owning_proc) {
310 spin_lock(&pcpui->owning_proc->proc_lock);
311 /* process might be dying, with a KMSG to clean it up waiting on
312 * this core. can't do much, so we'll attempt to restart */
313 if (pcpui->owning_proc->state == PROC_DYING) {
314 send_kernel_message(core_id(), __just_sched, 0, 0, 0,
316 spin_unlock(&pcpui->owning_proc->proc_lock);
317 enable_irqsave(&state);
320 printd("Descheduled %d in favor of %d\n", pcpui->owning_proc->pid,
322 __proc_set_state(pcpui->owning_proc, PROC_RUNNABLE_S);
323 /* Saving FP state aggressively. Odds are, the SCP was hit by an
324 * IRQ and has a HW ctx, in which case we must save. */
325 __proc_save_fpu_s(pcpui->owning_proc);
326 __proc_save_context_s(pcpui->owning_proc, pcpui->cur_ctx);
327 vcore_account_offline(pcpui->owning_proc, 0);
328 __seq_start_write(&p->procinfo->coremap_seqctr);
330 __seq_end_write(&p->procinfo->coremap_seqctr);
331 spin_unlock(&pcpui->owning_proc->proc_lock);
332 /* round-robin the SCPs (inserts at the end of the queue) */
333 switch_lists(pcpui->owning_proc, &unrunnable_scps, &runnable_scps);
334 clear_owning_proc(pcoreid);
335 /* Note we abandon core. It's not strictly necessary. If
336 * we didn't, the TLB would still be loaded with the old
337 * one, til we proc_run_s, and the various paths in
338 * proc_run_s would pick it up. This way is a bit safer for
339 * future changes, but has an extra (empty) TLB flush. */
342 /* Run the new proc */
343 switch_lists(p, &runnable_scps, &unrunnable_scps);
344 printd("PID of the SCP i'm running: %d\n", p->pid);
345 proc_run_s(p); /* gives it core we're running on */
346 enable_irqsave(&state);
352 /* Returns how many new cores p needs. This doesn't lock the proc, so your
353 * answer might be stale. */
354 static uint32_t get_cores_needed(struct proc *p)
356 uint32_t amt_wanted, amt_granted;
357 amt_wanted = p->procdata->res_req[RES_CORES].amt_wanted;
358 /* Help them out - if they ask for something impossible, give them 1 so they
359 * can make some progress. (this is racy, and unnecessary). */
360 if (amt_wanted > p->procinfo->max_vcores) {
361 printk("[kernel] proc %d wanted more than max, wanted %d\n", p->pid,
363 p->procdata->res_req[RES_CORES].amt_wanted = 1;
366 /* There are a few cases where amt_wanted is 0, but they are still RUNNABLE
367 * (involving yields, events, and preemptions). In these cases, give them
368 * at least 1, so they can make progress and yield properly. If they are
369 * not WAITING, they did not yield and may have missed a message. */
371 /* could ++, but there could be a race and we don't want to give them
372 * more than they ever asked for (in case they haven't prepped) */
373 p->procdata->res_req[RES_CORES].amt_wanted = 1;
376 /* amt_granted is racy - they could be *yielding*, but currently they can't
377 * be getting any new cores if the caller is in the mcp_ksched. this is
378 * okay - we won't accidentally give them more cores than they *ever* wanted
379 * (which could crash them), but our answer might be a little stale. */
380 amt_granted = p->procinfo->res_grant[RES_CORES];
381 /* Do not do an assert like this: it could fail (yield in progress): */
382 //assert(amt_granted == p->procinfo->num_vcores);
383 if (amt_wanted <= amt_granted)
385 return amt_wanted - amt_granted;
388 /* Actual work of the MCP kscheduler. if we were called by poke_ksched, *arg
389 * might be the process who wanted special service. this would be the case if
390 * we weren't already running the ksched. Sort of a ghetto way to "post work",
391 * such that it's an optimization. */
392 static void __run_mcp_ksched(void *arg)
394 struct proc *p, *temp;
396 struct proc_list *temp_mcp_list;
397 /* locking to protect the MCP lists' integrity and membership */
398 spin_lock(&sched_lock);
399 /* 2-pass scheme: check each proc on the primary list (FCFS). if they need
400 * nothing, put them on the secondary list. if they need something, rip
401 * them off the list, service them, and if they are still not dying, put
402 * them on the secondary list. We cull the entire primary list, so that
403 * when we start from the beginning each time, we aren't repeatedly checking
404 * procs we looked at on previous waves.
406 * TODO: we could modify this such that procs that we failed to service move
407 * to yet another list or something. We can also move the WAITINGs to
408 * another list and have wakeup move them back, etc. */
409 while (!TAILQ_EMPTY(primary_mcps)) {
410 TAILQ_FOREACH_SAFE(p, primary_mcps, ksched_data.proc_link, temp) {
411 if (p->state == PROC_WAITING) { /* unlocked peek at the state */
412 switch_lists(p, primary_mcps, secondary_mcps);
415 amt_needed = get_cores_needed(p);
417 switch_lists(p, primary_mcps, secondary_mcps);
420 /* o/w, we want to give cores to this proc */
421 remove_from_list(p, primary_mcps);
422 /* now it won't die, but it could get removed from lists and have
423 * its stuff unprov'd when we unlock */
425 /* GIANT WARNING: __core_req will unlock the sched lock for a bit.
426 * It will return with it locked still. We could unlock before we
427 * pass in, but they will relock right away. */
428 // notionally_unlock(&ksched_lock); /* for mouse-eyed viewers */
429 __core_request(p, amt_needed);
430 // notionally_lock(&ksched_lock);
431 /* Peeking at the state is okay, since we hold a ref. Once it is
432 * DYING, it'll remain DYING until we decref. And if there is a
433 * concurrent death, that will spin on the ksched lock (which we
434 * hold, and which protects the proc lists). */
435 if (p->state != PROC_DYING)
436 add_to_list(p, secondary_mcps);
437 proc_decref(p); /* fyi, this may trigger __proc_free */
438 /* need to break: the proc lists may have changed when we unlocked
439 * in core_req in ways that the FOREACH_SAFE can't handle. */
443 /* at this point, we moved all the procs over to the secondary list, and
444 * attempted to service the ones that wanted something. now just swap the
445 * lists for the next invocation of the ksched. */
446 temp_mcp_list = primary_mcps;
447 primary_mcps = secondary_mcps;
448 secondary_mcps = temp_mcp_list;
449 spin_unlock(&sched_lock);
452 /* Something has changed, and for whatever reason the scheduler should
455 * Don't call this if you are processing a syscall or otherwise care about your
456 * kthread variables, cur_proc/owning_proc, etc.
458 * Don't call this from interrupt context (grabs proclocks). */
459 void run_scheduler(void)
461 /* MCP scheduling: post work, then poke. for now, i just want the func to
462 * run again, so merely a poke is sufficient. */
463 poke(&ksched_poker, 0);
464 if (management_core()) {
465 spin_lock(&sched_lock);
467 spin_unlock(&sched_lock);
471 /* A process is asking the ksched to look at its resource desires. The
472 * scheduler is free to ignore this, for its own reasons, so long as it
473 * eventually gets around to looking at resource desires. */
474 void poke_ksched(struct proc *p, unsigned int res_type)
476 /* ignoring res_type for now. could post that if we wanted (would need some
477 * other structs/flags) */
478 if (!__proc_is_mcp(p))
480 poke(&ksched_poker, p);
483 /* The calling cpu/core has nothing to do and plans to idle/halt. This is an
484 * opportunity to pick the nature of that halting (low power state, etc), or
485 * provide some other work (_Ss on LL cores). Note that interrupts are
486 * disabled, and if you return, the core will cpu_halt(). */
489 bool new_proc = FALSE;
490 if (!management_core())
492 spin_lock(&sched_lock);
493 new_proc = __schedule_scp();
494 spin_unlock(&sched_lock);
495 /* if we just scheduled a proc, we need to manually restart it, instead of
496 * returning. if we return, the core will halt. */
501 /* Could drop into the monitor if there are no processes at all. For now,
502 * the 'call of the giraffe' suffices. */
505 /* Available resources changed (plus or minus). Some parts of the kernel may
506 * call this if a particular resource that is 'quantity-based' changes. Things
507 * like available RAM to processes, bandwidth, etc. Cores would probably be
508 * inappropriate, since we need to know which specific core is now free. */
509 void avail_res_changed(int res_type, long change)
511 printk("[kernel] ksched doesn't track any resources yet!\n");
514 int get_any_idle_core(void)
516 spin_lock(&sched_lock);
517 int ret = __get_any_idle_core();
518 spin_unlock(&sched_lock);
522 int get_specific_idle_core(int coreid)
524 spin_lock(&sched_lock);
525 int ret = __get_specific_idle_core(coreid);
526 spin_unlock(&sched_lock);
530 /* similar to __sched_put_idle_core, but without the prov tracking */
531 void put_idle_core(int coreid)
533 spin_lock(&sched_lock);
534 __put_idle_core(coreid);
535 spin_unlock(&sched_lock);
538 /* Normally it'll be the max number of CG cores ever */
539 uint32_t max_vcores(struct proc *p)
542 #ifdef CONFIG_DISABLE_SMT
543 return num_cores >> 1;
545 return num_cores - 1; /* reserving core 0 */
546 #endif /* CONFIG_DISABLE_SMT */
549 /* This deals with a request for more cores. The amt of new cores needed is
550 * passed in. The ksched lock is held, but we are free to unlock if we want
551 * (and we must, if calling out of the ksched to anything high-level).
553 * Side note: if we want to warn, then we can't deal with this proc's prov'd
554 * cores until we wait til the alarm goes off. would need to put all
555 * alarmed cores on a list and wait til the alarm goes off to do the full
556 * preempt. and when those cores come in voluntarily, we'd need to know to
557 * give them to this proc. */
558 static void __core_request(struct proc *p, uint32_t amt_needed)
560 uint32_t nr_to_grant = 0;
561 uint32_t corelist[num_cores];
562 struct sched_pcore *spc_i, *temp;
563 struct proc *proc_to_preempt;
565 /* we come in holding the ksched lock, and we hold it here to protect
566 * allocations and provisioning. */
567 /* get all available cores from their prov_not_alloc list. the list might
568 * change when we unlock (new cores added to it, or the entire list emptied,
569 * but no core allocations will happen (we hold the poke)). */
570 while (nr_to_grant != amt_needed) {
571 /* Find the next best core to allocate to p. It may be a core
572 * provisioned to p, and it might not be. */
573 spc_i = __find_best_core_to_alloc(p);
574 /* If no core is returned, we know that there are no more cores to give
575 * out, so we exit the loop. */
578 /* If the pcore chosen currently has a proc allocated to it, we know
579 * it must be provisioned to p, but not allocated to it. We need to try
580 * to preempt. After this block, the core will be track_dealloc'd and
581 * on the idle list (regardless of whether we had to preempt or not) */
582 if (spc_i->alloc_proc) {
583 proc_to_preempt = spc_i->alloc_proc;
584 /* would break both preemption and maybe the later decref */
585 assert(proc_to_preempt != p);
586 /* need to keep a valid, external ref when we unlock */
587 proc_incref(proc_to_preempt, 1);
588 spin_unlock(&sched_lock);
589 /* sending no warning time for now - just an immediate preempt. */
590 success = proc_preempt_core(proc_to_preempt, spc2pcoreid(spc_i), 0);
591 /* reaquire locks to protect provisioning and idle lists */
592 spin_lock(&sched_lock);
594 /* we preempted it before the proc could yield or die.
595 * alloc_proc should not have changed (it'll change in death and
596 * idle CBs). the core is not on the idle core list. (if we
597 * ever have proc alloc lists, it'll still be on the old proc's
599 assert(spc_i->alloc_proc);
600 /* regardless of whether or not it is still prov to p, we need
601 * to note its dealloc. we are doing some excessive checking of
602 * p == prov_proc, but using this helper is a lot clearer. */
603 __track_core_dealloc(proc_to_preempt, spc2pcoreid(spc_i));
605 /* the preempt failed, which should only happen if the pcore was
606 * unmapped (could be dying, could be yielding, but NOT
607 * preempted). whoever unmapped it also triggered (or will
608 * soon trigger) a track_core_dealloc and put it on the idle
609 * list. Our signal for this is spc_i->alloc_proc being 0. We
610 * need to spin and let whoever is trying to free the core grab
611 * the ksched lock. We could use an 'ignore_next_idle' flag
612 * per sched_pcore, but it's not critical anymore.
614 * Note, we're relying on us being the only preemptor - if the
615 * core was unmapped by *another* preemptor, there would be no
616 * way of knowing the core was made idle *yet* (the success
617 * branch in another thread). likewise, if there were another
618 * allocator, the pcore could have been put on the idle list and
619 * then quickly removed/allocated. */
621 while (spc_i->alloc_proc) {
622 /* this loop should be very rare */
623 spin_unlock(&sched_lock);
625 spin_lock(&sched_lock);
628 /* no longer need to keep p_to_pre alive */
629 proc_decref(proc_to_preempt);
630 /* might not be prov to p anymore (rare race). spc_i is idle - we
631 * might get it later, or maybe we'll give it to its rightful proc*/
632 if (spc_i->prov_proc != p)
635 /* At this point, the pcore is idle, regardless of how we got here
636 * (successful preempt, failed preempt, or it was idle in the first
637 * place). We also know the core is still provisioned to us. Lets add
638 * it to the corelist for p (so we can give it to p in bulk later), and
639 * track its allocation with p (so our internal data structures stay in
640 * sync). We rely on the fact that we are the only allocator (spc_i is
641 * still idle, despite (potentially) unlocking during the preempt
642 * attempt above). It is guaranteed to be track_dealloc'd()
643 * (regardless of how we got here). */
644 corelist[nr_to_grant] = spc2pcoreid(spc_i);
646 __track_core_alloc(p, spc2pcoreid(spc_i));
648 /* Now, actually give them out */
650 /* Need to unlock before calling out to proc code. We are somewhat
651 * relying on being the only one allocating 'thread' here, since another
652 * allocator could have seen these cores (if they are prov to some proc)
653 * and could be trying to give them out (and assuming they are already
654 * on the idle list). */
655 spin_unlock(&sched_lock);
656 /* give them the cores. this will start up the extras if RUNNING_M. */
657 spin_lock(&p->proc_lock);
658 /* if they fail, it is because they are WAITING or DYING. we could give
659 * the cores to another proc or whatever. for the current type of
660 * ksched, we'll just put them back on the pile and return. Note, the
661 * ksched could check the states after locking, but it isn't necessary:
662 * just need to check at some point in the ksched loop. */
663 if (__proc_give_cores(p, corelist, nr_to_grant)) {
664 spin_unlock(&p->proc_lock);
665 /* we failed, put the cores and track their dealloc. lock is
666 * protecting those structures. */
667 spin_lock(&sched_lock);
668 __track_core_dealloc_bulk(p, corelist, nr_to_grant);
670 /* at some point after giving cores, call proc_run_m() (harmless on
671 * RUNNING_Ms). You can give small groups of cores, then run them
672 * (which is more efficient than interleaving runs with the gives
673 * for bulk preempted processes). */
675 spin_unlock(&p->proc_lock);
676 /* main mcp_ksched wants this held (it came to __core_req held) */
677 spin_lock(&sched_lock);
680 /* note the ksched lock is still held */
683 /* TODO: need more thorough CG/LL management. For now, core0 is the only LL
684 * core. This won't play well with the ghetto shit in schedule_init() if you do
685 * anything like 'DEDICATED_MONITOR' or the ARSC server. All that needs an
687 static bool is_ll_core(uint32_t pcoreid)
694 /* Provision a core to a process. This function wraps the primary logic
695 * implemented in __provision_core, with a lock, error checking, etc. */
696 int provision_core(struct proc *p, uint32_t pcoreid)
698 struct sched_pcore *spc;
699 /* Make sure we aren't asking for something that doesn't exist (bounds check
700 * on the pcore array) */
701 if (!(pcoreid < num_cores)) {
705 /* Don't allow the provisioning of LL cores */
706 if (is_ll_core(pcoreid)) {
710 spc = pcoreid2spc(pcoreid);
711 /* Note the sched lock protects the spc tailqs for all procs in this code.
712 * If we need a finer grained sched lock, this is one place where we could
713 * have a different lock */
714 spin_lock(&sched_lock);
715 __provision_core(p, spc);
716 spin_unlock(&sched_lock);
720 /************** Debugging **************/
721 void sched_diag(void)
724 spin_lock(&sched_lock);
725 TAILQ_FOREACH(p, &runnable_scps, ksched_data.proc_link)
726 printk("Runnable _S PID: %d\n", p->pid);
727 TAILQ_FOREACH(p, &unrunnable_scps, ksched_data.proc_link)
728 printk("Unrunnable _S PID: %d\n", p->pid);
729 TAILQ_FOREACH(p, primary_mcps, ksched_data.proc_link)
730 printk("Primary MCP PID: %d\n", p->pid);
731 TAILQ_FOREACH(p, secondary_mcps, ksched_data.proc_link)
732 printk("Secondary MCP PID: %d\n", p->pid);
733 spin_unlock(&sched_lock);
737 void print_idlecoremap(void)
739 struct sched_pcore *spc_i;
740 /* not locking, so we can look at this without deadlocking. */
741 printk("Idle cores (unlocked!):\n");
742 TAILQ_FOREACH(spc_i, &idlecores, alloc_next)
743 printk("Core %d, prov to %d (%p)\n", spc2pcoreid(spc_i),
744 spc_i->prov_proc ? spc_i->prov_proc->pid : 0, spc_i->prov_proc);
747 void print_resources(struct proc *p)
749 printk("--------------------\n");
750 printk("PID: %d\n", p->pid);
751 printk("--------------------\n");
752 for (int i = 0; i < MAX_NUM_RESOURCES; i++)
753 printk("Res type: %02d, amt wanted: %08d, amt granted: %08d\n", i,
754 p->procdata->res_req[i].amt_wanted, p->procinfo->res_grant[i]);
757 void print_all_resources(void)
760 void __print_resources(void *item, void *opaque)
762 print_resources((struct proc*)item);
764 spin_lock(&pid_hash_lock);
765 hash_for_each(pid_hash, __print_resources, NULL);
766 spin_unlock(&pid_hash_lock);
769 void next_core(uint32_t pcoreid)
771 struct sched_pcore *spc_i;
773 spin_lock(&sched_lock);
774 TAILQ_FOREACH(spc_i, &idlecores, alloc_next) {
775 if (spc2pcoreid(spc_i) == pcoreid) {
781 TAILQ_REMOVE(&idlecores, spc_i, alloc_next);
782 TAILQ_INSERT_HEAD(&idlecores, spc_i, alloc_next);
783 printk("Pcore %d will be given out next (from the idles)\n", pcoreid);
785 spin_unlock(&sched_lock);
788 void sort_idles(void)
790 struct sched_pcore *spc_i, *spc_j, *temp;
791 struct sched_pcore_tailq sorter = TAILQ_HEAD_INITIALIZER(sorter);
793 spin_lock(&sched_lock);
794 TAILQ_CONCAT(&sorter, &idlecores, alloc_next);
795 TAILQ_FOREACH_SAFE(spc_i, &sorter, alloc_next, temp) {
796 TAILQ_REMOVE(&sorter, spc_i, alloc_next);
798 /* don't need foreach_safe since we break after we muck with the list */
799 TAILQ_FOREACH(spc_j, &idlecores, alloc_next) {
801 TAILQ_INSERT_BEFORE(spc_j, spc_i, alloc_next);
807 TAILQ_INSERT_TAIL(&idlecores, spc_i, alloc_next);
809 spin_unlock(&sched_lock);