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. */
20 #include <sys/queue.h>
23 /* Process Lists. 'unrunnable' is a holding list for SCPs that are running or
24 * waiting or otherwise not considered for sched decisions. */
25 struct proc_list unrunnable_scps = TAILQ_HEAD_INITIALIZER(unrunnable_scps);
26 struct proc_list runnable_scps = TAILQ_HEAD_INITIALIZER(runnable_scps);
27 /* mcp lists. we actually could get by with one list and a TAILQ_CONCAT, but
28 * I'm expecting to want the flexibility of the pointers later. */
29 struct proc_list all_mcps_1 = TAILQ_HEAD_INITIALIZER(all_mcps_1);
30 struct proc_list all_mcps_2 = TAILQ_HEAD_INITIALIZER(all_mcps_2);
31 struct proc_list *primary_mcps = &all_mcps_1;
32 struct proc_list *secondary_mcps = &all_mcps_2;
34 /* The pcores in the system. (array gets alloced in init()). */
35 struct sched_pcore *all_pcores;
37 /* TAILQ of all unallocated, idle (CG) cores */
38 struct sched_pcore_tailq idlecores = TAILQ_HEAD_INITIALIZER(idlecores);
40 /* Helper, defined below */
41 static void __core_request(struct proc *p, uint32_t amt_needed);
42 static void __put_idle_cores(struct proc *p, uint32_t *pc_arr, uint32_t num);
43 static void add_to_list(struct proc *p, struct proc_list *list);
44 static void remove_from_list(struct proc *p, struct proc_list *list);
45 static void switch_lists(struct proc *p, struct proc_list *old,
46 struct proc_list *new);
47 static uint32_t spc2pcoreid(struct sched_pcore *spc);
48 static struct sched_pcore *pcoreid2spc(uint32_t pcoreid);
49 static bool is_ll_core(uint32_t pcoreid);
50 static void __prov_track_alloc(struct proc *p, uint32_t pcoreid);
51 static void __prov_track_dealloc(struct proc *p, uint32_t pcoreid);
52 static void __prov_track_dealloc_bulk(struct proc *p, uint32_t *pc_arr,
54 static void __run_mcp_ksched(void *arg); /* don't call directly */
55 static uint32_t get_cores_needed(struct proc *p);
57 /* Locks / sync tools */
59 /* poke-style ksched - ensures the MCP ksched only runs once at a time. since
60 * only one mcp ksched runs at a time, while this is set, the ksched knows no
61 * cores are being allocated by other code (though they could be dealloc, due to
64 * The main value to this sync method is to make the 'make sure the ksched runs
65 * only once at a time and that it actually runs' invariant/desire wait-free, so
66 * that it can be called anywhere (deep event code, etc).
68 * As the ksched gets smarter, we'll probably embedd this poker in a bigger
69 * struct that can handle the posting of different types of work. */
70 struct poke_tracker ksched_poker = {0, 0, __run_mcp_ksched};
72 /* this 'big ksched lock' protects a bunch of things, which i may make fine
74 /* - protects the integrity of proc tailqs/structures, as well as the membership
75 * of a proc on those lists. proc lifetime within the ksched but outside this
76 * lock is protected by the proc kref. */
77 //spinlock_t proclist_lock = SPINLOCK_INITIALIZER; /* subsumed by bksl */
78 /* - protects the provisioning assignment, membership of sched_pcores in
79 * provision lists, and the integrity of all prov lists (the lists of each
80 * proc). does NOT protect spc->alloc_proc. */
81 //spinlock_t prov_lock = SPINLOCK_INITIALIZER;
82 /* - protects allocation structures: spc->alloc_proc, the integrity and
83 * membership of the idelcores tailq. */
84 //spinlock_t alloc_lock = SPINLOCK_INITIALIZER;
85 spinlock_t sched_lock = SPINLOCK_INITIALIZER;
87 /* Alarm struct, for our example 'timer tick' */
88 struct alarm_waiter ksched_waiter;
90 #define TIMER_TICK_USEC 10000 /* 10msec */
92 /* Helper: Sets up a timer tick on the calling core to go off 10 msec from now.
93 * This assumes the calling core is an LL core, etc. */
94 static void set_ksched_alarm(void)
96 set_awaiter_rel(&ksched_waiter, TIMER_TICK_USEC);
97 set_alarm(&per_cpu_info[core_id()].tchain, &ksched_waiter);
100 /* Need a kmsg to just run the sched, but not to rearm */
101 static void __just_sched(uint32_t srcid, long a0, long a1, long a2)
106 /* RKM alarm, to run the scheduler tick (not in interrupt context) and reset the
107 * alarm. Note that interrupts will be disabled, but this is not the same as
108 * interrupt context. We're a routine kmsg, which means the core is in a
109 * quiescent state. */
110 static void __ksched_tick(struct alarm_waiter *waiter)
112 /* TODO: imagine doing some accounting here */
114 /* Set our alarm to go off, incrementing from our last tick (instead of
115 * setting it relative to now, since some time has passed since the alarm
116 * first went off. Note, this may be now or in the past! */
117 set_awaiter_inc(&ksched_waiter, TIMER_TICK_USEC);
118 set_alarm(&per_cpu_info[core_id()].tchain, &ksched_waiter);
121 void schedule_init(void)
123 spin_lock(&sched_lock);
124 /* init provisioning stuff */
125 all_pcores = kmalloc(sizeof(struct sched_pcore) * num_cpus, 0);
126 memset(all_pcores, 0, sizeof(struct sched_pcore) * num_cpus);
127 assert(!core_id()); /* want the alarm on core0 for now */
128 init_awaiter(&ksched_waiter, __ksched_tick);
130 /* init the idlecore list. if they turned off hyperthreading, give them the
131 * odds from 1..max-1. otherwise, give them everything by 0 (default mgmt
132 * core). TODO: (CG/LL) better LL/CG mgmt */
133 #ifndef CONFIG_DISABLE_SMT
134 for (int i = 1; i < num_cpus; i++)
135 TAILQ_INSERT_TAIL(&idlecores, pcoreid2spc(i), alloc_next);
137 assert(!(num_cpus % 2));
138 for (int i = 1; i < num_cpus; i += 2)
139 TAILQ_INSERT_TAIL(&idlecores, pcoreid2spc(i), alloc_next);
140 #endif /* CONFIG_DISABLE_SMT */
141 #ifdef CONFIG_ARSC_SERVER
142 struct sched_pcore *a_core = TAILQ_FIRST(&idlecores);
144 TAILQ_REMOVE(&idlecores, a_core, alloc_next);
145 send_kernel_message(spc2pcoreid(a_core), arsc_server, 0, 0, 0,
147 warn("Using core %d for the ARSCs - there are probably issues with this.",
148 spc2pcoreid(a_core));
149 #endif /* CONFIG_ARSC_SERVER */
150 spin_unlock(&sched_lock);
154 static uint32_t spc2pcoreid(struct sched_pcore *spc)
156 return spc - all_pcores;
159 static struct sched_pcore *pcoreid2spc(uint32_t pcoreid)
161 return &all_pcores[pcoreid];
164 /* Round-robins on whatever list it's on */
165 static void add_to_list(struct proc *p, struct proc_list *new)
167 assert(!(p->ksched_data.cur_list));
168 TAILQ_INSERT_TAIL(new, p, ksched_data.proc_link);
169 p->ksched_data.cur_list = new;
172 static void remove_from_list(struct proc *p, struct proc_list *old)
174 assert(p->ksched_data.cur_list == old);
175 TAILQ_REMOVE(old, p, ksched_data.proc_link);
176 p->ksched_data.cur_list = 0;
179 static void switch_lists(struct proc *p, struct proc_list *old,
180 struct proc_list *new)
182 remove_from_list(p, old);
186 /* Removes from whatever list p is on */
187 static void remove_from_any_list(struct proc *p)
189 if (p->ksched_data.cur_list) {
190 TAILQ_REMOVE(p->ksched_data.cur_list, p, ksched_data.proc_link);
191 p->ksched_data.cur_list = 0;
195 /************** Process Management Callbacks **************/
197 * - the proc lock is NOT held for any of these calls. currently, there is no
198 * lock ordering between the sched lock and the proc lock. since the proc
199 * code doesn't know what we do, it doesn't hold its lock when calling our
201 * - since the proc lock isn't held, the proc could be dying, which means we
202 * will receive a __sched_proc_destroy() either before or after some of these
203 * other CBs. the CBs related to list management need to check and abort if
205 void __sched_proc_register(struct proc *p)
207 assert(p->state != PROC_DYING); /* shouldn't be abel to happen yet */
208 /* one ref for the proc's existence, cradle-to-grave */
209 proc_incref(p, 1); /* need at least this OR the 'one for existing' */
210 spin_lock(&sched_lock);
211 TAILQ_INIT(&p->ksched_data.prov_alloc_me);
212 TAILQ_INIT(&p->ksched_data.prov_not_alloc_me);
213 add_to_list(p, &unrunnable_scps);
214 spin_unlock(&sched_lock);
217 /* Returns 0 if it succeeded, an error code otherwise. */
218 void __sched_proc_change_to_m(struct proc *p)
220 spin_lock(&sched_lock);
221 /* Need to make sure they aren't dying. if so, we already dealt with their
222 * list membership, etc (or soon will). taking advantage of the 'immutable
223 * state' of dying (so long as refs are held). */
224 if (p->state == PROC_DYING) {
225 spin_unlock(&sched_lock);
228 /* Catch user bugs */
229 if (!p->procdata->res_req[RES_CORES].amt_wanted) {
230 printk("[kernel] process needs to specify amt_wanted\n");
231 p->procdata->res_req[RES_CORES].amt_wanted = 1;
233 /* For now, this should only ever be called on an unrunnable. It's
234 * probably a bug, at this stage in development, to do o/w. */
235 remove_from_list(p, &unrunnable_scps);
236 //remove_from_any_list(p); /* ^^ instead of this */
237 add_to_list(p, primary_mcps);
238 spin_unlock(&sched_lock);
239 //poke_ksched(p, RES_CORES);
242 /* Helper for the destroy CB : unprovisions any pcores for the given list */
243 static void unprov_pcore_list(struct sched_pcore_tailq *list_head)
245 struct sched_pcore *spc_i;
246 /* We can leave them connected within the tailq, since the scps don't have a
247 * default list (if they aren't on a proc's list, then we don't care about
248 * them), and since the INSERTs don't care what list you were on before
249 * (chummy with the implementation). Pretty sure this is right. If there's
250 * suspected list corruption, be safer here. */
251 TAILQ_FOREACH(spc_i, list_head, prov_next)
252 spc_i->prov_proc = 0;
253 TAILQ_INIT(list_head);
256 /* Sched callback called when the proc dies. pc_arr holds the cores the proc
257 * had, if any, and nr_cores tells us how many are in the array.
259 * An external, edible ref is passed in. when we return and they decref,
260 * __proc_free will be called (when the last one is done). */
261 void __sched_proc_destroy(struct proc *p, uint32_t *pc_arr, uint32_t nr_cores)
263 spin_lock(&sched_lock);
264 /* Unprovision any cores. Note this is different than track_dealloc.
265 * The latter does bookkeeping when an allocation changes. This is a
266 * bulk *provisioning* change. */
267 unprov_pcore_list(&p->ksched_data.prov_alloc_me);
268 unprov_pcore_list(&p->ksched_data.prov_not_alloc_me);
269 /* Remove from whatever list we are on (if any - might not be on one if it
270 * was in the middle of __run_mcp_sched) */
271 remove_from_any_list(p);
273 __put_idle_cores(p, pc_arr, nr_cores);
274 __prov_track_dealloc_bulk(p, pc_arr, nr_cores);
276 spin_unlock(&sched_lock);
277 /* Drop the cradle-to-the-grave reference, jet-li */
281 /* ksched callbacks. p just woke up and is UNLOCKED. */
282 void __sched_mcp_wakeup(struct proc *p)
284 spin_lock(&sched_lock);
285 if (p->state == PROC_DYING) {
286 spin_unlock(&sched_lock);
289 /* could try and prioritize p somehow (move it to the front of the list). */
290 spin_unlock(&sched_lock);
291 /* note they could be dying at this point too. */
292 poke(&ksched_poker, p);
295 /* ksched callbacks. p just woke up and is UNLOCKED. */
296 void __sched_scp_wakeup(struct proc *p)
298 spin_lock(&sched_lock);
299 if (p->state == PROC_DYING) {
300 spin_unlock(&sched_lock);
303 /* might not be on a list if it is new. o/w, it should be unrunnable */
304 remove_from_any_list(p);
305 add_to_list(p, &runnable_scps);
306 spin_unlock(&sched_lock);
307 /* we could be on a CG core, and all the mgmt cores could be halted. if we
308 * don't tell one of them about the new proc, they will sleep until the
309 * timer tick goes off. */
310 if (!management_core()) {
311 /* TODO: pick a better core and only send if halted.
313 * FYI, a POKE on x86 might lose a rare race with halt code, since the
314 * poke handler does not abort halts. if this happens, the next timer
315 * IRQ would wake up the core.
317 * ideally, we'd know if a specific mgmt core is sleeping and wake it
318 * up. o/w, we could interrupt an already-running mgmt core that won't
319 * get to our new proc anytime soon. also, by poking core 0, a
320 * different mgmt core could remain idle (and this process would sleep)
321 * until its tick goes off */
322 send_ipi(0, I_POKE_CORE);
326 /* Callback to return a core to the ksched, which tracks it as idle and
327 * deallocated from p. The proclock is held (__core_req depends on that).
329 * This also is a trigger, telling us we have more cores. We could/should make
330 * a scheduling decision (or at least plan to). */
331 void __sched_put_idle_core(struct proc *p, uint32_t coreid)
333 struct sched_pcore *spc = pcoreid2spc(coreid);
334 spin_lock(&sched_lock);
335 TAILQ_INSERT_TAIL(&idlecores, spc, alloc_next);
336 __prov_track_dealloc(p, coreid);
337 spin_unlock(&sched_lock);
340 /* Helper for put_idle and core_req. Note this does not track_dealloc. When we
341 * get rid of / revise proc_preempt_all and put_idle_cores, we can get rid of
342 * this. (the ksched will never need it - only external callers). */
343 static void __put_idle_cores(struct proc *p, uint32_t *pc_arr, uint32_t num)
345 struct sched_pcore *spc_i;
346 for (int i = 0; i < num; i++) {
347 spc_i = pcoreid2spc(pc_arr[i]);
348 TAILQ_INSERT_TAIL(&idlecores, spc_i, alloc_next);
352 /* Callback, bulk interface for put_idle. Note this one also calls track_dealloc,
353 * which the internal version does not. The proclock is held for this. */
354 void __sched_put_idle_cores(struct proc *p, uint32_t *pc_arr, uint32_t num)
356 spin_lock(&sched_lock);
357 /* TODO: when we revise this func, look at __put_idle */
358 __put_idle_cores(p, pc_arr, num);
359 __prov_track_dealloc_bulk(p, pc_arr, num);
360 spin_unlock(&sched_lock);
361 /* could trigger a sched decision here */
364 /* mgmt/LL cores should call this to schedule the calling core and give it to an
365 * SCP. will also prune the dead SCPs from the list. hold the lock before
366 * calling. returns TRUE if it scheduled a proc. */
367 static bool __schedule_scp(void)
369 // TODO: sort out lock ordering (proc_run_s also locks)
371 uint32_t pcoreid = core_id();
372 struct per_cpu_info *pcpui = &per_cpu_info[pcoreid];
374 /* if there are any runnables, run them here and put any currently running
375 * SCP on the tail of the runnable queue. */
376 if ((p = TAILQ_FIRST(&runnable_scps))) {
377 /* protect owning proc, cur_ctx, etc. note this nests with the
378 * calls in proc_yield_s */
379 disable_irqsave(&state);
380 /* someone is currently running, dequeue them */
381 if (pcpui->owning_proc) {
382 spin_lock(&pcpui->owning_proc->proc_lock);
383 /* process might be dying, with a KMSG to clean it up waiting on
384 * this core. can't do much, so we'll attempt to restart */
385 if (pcpui->owning_proc->state == PROC_DYING) {
386 send_kernel_message(core_id(), __just_sched, 0, 0, 0,
388 spin_unlock(&pcpui->owning_proc->proc_lock);
389 enable_irqsave(&state);
392 printd("Descheduled %d in favor of %d\n", pcpui->owning_proc->pid,
394 __proc_set_state(pcpui->owning_proc, PROC_RUNNABLE_S);
395 /* Saving FP state aggressively. Odds are, the SCP was hit by an
396 * IRQ and has a HW ctx, in which case we must save. */
397 __proc_save_fpu_s(pcpui->owning_proc);
398 __proc_save_context_s(pcpui->owning_proc, pcpui->cur_ctx);
399 vcore_account_offline(pcpui->owning_proc, 0); /* VC# */
400 spin_unlock(&pcpui->owning_proc->proc_lock);
401 /* round-robin the SCPs (inserts at the end of the queue) */
402 switch_lists(pcpui->owning_proc, &unrunnable_scps, &runnable_scps);
403 clear_owning_proc(pcoreid);
404 /* Note we abandon core. It's not strictly necessary. If
405 * we didn't, the TLB would still be loaded with the old
406 * one, til we proc_run_s, and the various paths in
407 * proc_run_s would pick it up. This way is a bit safer for
408 * future changes, but has an extra (empty) TLB flush. */
411 /* Run the new proc */
412 switch_lists(p, &runnable_scps, &unrunnable_scps);
413 printd("PID of the SCP i'm running: %d\n", p->pid);
414 proc_run_s(p); /* gives it core we're running on */
415 enable_irqsave(&state);
421 /* Returns how many new cores p needs. This doesn't lock the proc, so your
422 * answer might be stale. */
423 static uint32_t get_cores_needed(struct proc *p)
425 uint32_t amt_wanted, amt_granted;
426 amt_wanted = p->procdata->res_req[RES_CORES].amt_wanted;
427 /* Help them out - if they ask for something impossible, give them 1 so they
428 * can make some progress. (this is racy, and unnecessary). */
429 if (amt_wanted > p->procinfo->max_vcores) {
430 printk("[kernel] proc %d wanted more than max, wanted %d\n", p->pid,
432 p->procdata->res_req[RES_CORES].amt_wanted = 1;
435 /* There are a few cases where amt_wanted is 0, but they are still RUNNABLE
436 * (involving yields, events, and preemptions). In these cases, give them
437 * at least 1, so they can make progress and yield properly. If they are
438 * not WAITING, they did not yield and may have missed a message. */
440 /* could ++, but there could be a race and we don't want to give them
441 * more than they ever asked for (in case they haven't prepped) */
442 p->procdata->res_req[RES_CORES].amt_wanted = 1;
445 /* amt_granted is racy - they could be *yielding*, but currently they can't
446 * be getting any new cores if the caller is in the mcp_ksched. this is
447 * okay - we won't accidentally give them more cores than they *ever* wanted
448 * (which could crash them), but our answer might be a little stale. */
449 amt_granted = p->procinfo->res_grant[RES_CORES];
450 /* Do not do an assert like this: it could fail (yield in progress): */
451 //assert(amt_granted == p->procinfo->num_vcores);
452 if (amt_wanted <= amt_granted)
454 return amt_wanted - amt_granted;
457 /* Actual work of the MCP kscheduler. if we were called by poke_ksched, *arg
458 * might be the process who wanted special service. this would be the case if
459 * we weren't already running the ksched. Sort of a ghetto way to "post work",
460 * such that it's an optimization. */
461 static void __run_mcp_ksched(void *arg)
463 struct proc *p, *temp;
465 struct proc_list *temp_mcp_list;
466 /* locking to protect the MCP lists' integrity and membership */
467 spin_lock(&sched_lock);
468 /* 2-pass scheme: check each proc on the primary list (FCFS). if they need
469 * nothing, put them on the secondary list. if they need something, rip
470 * them off the list, service them, and if they are still not dying, put
471 * them on the secondary list. We cull the entire primary list, so that
472 * when we start from the beginning each time, we aren't repeatedly checking
473 * procs we looked at on previous waves.
475 * TODO: we could modify this such that procs that we failed to service move
476 * to yet another list or something. We can also move the WAITINGs to
477 * another list and have wakeup move them back, etc. */
478 while (!TAILQ_EMPTY(primary_mcps)) {
479 TAILQ_FOREACH_SAFE(p, primary_mcps, ksched_data.proc_link, temp) {
480 if (p->state == PROC_WAITING) { /* unlocked peek at the state */
481 switch_lists(p, primary_mcps, secondary_mcps);
484 amt_needed = get_cores_needed(p);
486 switch_lists(p, primary_mcps, secondary_mcps);
489 /* o/w, we want to give cores to this proc */
490 remove_from_list(p, primary_mcps);
491 /* now it won't die, but it could get removed from lists and have
492 * its stuff unprov'd when we unlock */
494 /* GIANT WARNING: __core_req will unlock the sched lock for a bit.
495 * It will return with it locked still. We could unlock before we
496 * pass in, but they will relock right away. */
497 // notionally_unlock(&ksched_lock); /* for mouse-eyed viewers */
498 __core_request(p, amt_needed);
499 // notionally_lock(&ksched_lock);
500 /* Peeking at the state is okay, since we hold a ref. Once it is
501 * DYING, it'll remain DYING until we decref. And if there is a
502 * concurrent death, that will spin on the ksched lock (which we
503 * hold, and which protects the proc lists). */
504 if (p->state != PROC_DYING)
505 add_to_list(p, secondary_mcps);
506 proc_decref(p); /* fyi, this may trigger __proc_free */
507 /* need to break: the proc lists may have changed when we unlocked
508 * in core_req in ways that the FOREACH_SAFE can't handle. */
512 /* at this point, we moved all the procs over to the secondary list, and
513 * attempted to service the ones that wanted something. now just swap the
514 * lists for the next invocation of the ksched. */
515 temp_mcp_list = primary_mcps;
516 primary_mcps = secondary_mcps;
517 secondary_mcps = temp_mcp_list;
518 spin_unlock(&sched_lock);
521 /* Something has changed, and for whatever reason the scheduler should
524 * Don't call this if you are processing a syscall or otherwise care about your
525 * kthread variables, cur_proc/owning_proc, etc.
527 * Don't call this from interrupt context (grabs proclocks). */
528 void run_scheduler(void)
530 /* MCP scheduling: post work, then poke. for now, i just want the func to
531 * run again, so merely a poke is sufficient. */
532 poke(&ksched_poker, 0);
533 if (management_core()) {
534 spin_lock(&sched_lock);
536 spin_unlock(&sched_lock);
540 /* A process is asking the ksched to look at its resource desires. The
541 * scheduler is free to ignore this, for its own reasons, so long as it
542 * eventually gets around to looking at resource desires. */
543 void poke_ksched(struct proc *p, unsigned int res_type)
545 /* ignoring res_type for now. could post that if we wanted (would need some
546 * other structs/flags) */
547 if (!__proc_is_mcp(p))
549 poke(&ksched_poker, p);
552 /* The calling cpu/core has nothing to do and plans to idle/halt. This is an
553 * opportunity to pick the nature of that halting (low power state, etc), or
554 * provide some other work (_Ss on LL cores). Note that interrupts are
555 * disabled, and if you return, the core will cpu_halt(). */
558 bool new_proc = FALSE;
559 if (!management_core())
561 spin_lock(&sched_lock);
562 new_proc = __schedule_scp();
563 spin_unlock(&sched_lock);
564 /* if we just scheduled a proc, we need to manually restart it, instead of
565 * returning. if we return, the core will halt. */
570 /* Could drop into the monitor if there are no processes at all. For now,
571 * the 'call of the giraffe' suffices. */
574 /* Available resources changed (plus or minus). Some parts of the kernel may
575 * call this if a particular resource that is 'quantity-based' changes. Things
576 * like available RAM to processes, bandwidth, etc. Cores would probably be
577 * inappropriate, since we need to know which specific core is now free. */
578 void avail_res_changed(int res_type, long change)
580 printk("[kernel] ksched doesn't track any resources yet!\n");
583 /* Normally it'll be the max number of CG cores ever */
584 uint32_t max_vcores(struct proc *p)
587 #ifdef CONFIG_DISABLE_SMT
588 return num_cpus >> 1;
590 return num_cpus - 1; /* reserving core 0 */
591 #endif /* CONFIG_DISABLE_SMT */
594 /* This deals with a request for more cores. The amt of new cores needed is
595 * passed in. The ksched lock is held, but we are free to unlock if we want
596 * (and we must, if calling out of the ksched to anything high-level).
598 * Side note: if we want to warn, then we can't deal with this proc's prov'd
599 * cores until we wait til the alarm goes off. would need to put all
600 * alarmed cores on a list and wait til the alarm goes off to do the full
601 * preempt. and when those cores come in voluntarily, we'd need to know to
602 * give them to this proc. */
603 static void __core_request(struct proc *p, uint32_t amt_needed)
605 uint32_t nr_to_grant = 0;
606 uint32_t corelist[num_cpus];
607 struct sched_pcore *spc_i, *temp;
608 struct proc *proc_to_preempt;
610 /* we come in holding the ksched lock, and we hold it here to protect
611 * allocations and provisioning. */
612 /* get all available cores from their prov_not_alloc list. the list might
613 * change when we unlock (new cores added to it, or the entire list emptied,
614 * but no core allocations will happen (we hold the poke)). */
615 while (!TAILQ_EMPTY(&p->ksched_data.prov_not_alloc_me)) {
616 if (nr_to_grant == amt_needed)
618 /* picking the next victim (first on the not_alloc list) */
619 spc_i = TAILQ_FIRST(&p->ksched_data.prov_not_alloc_me);
620 /* someone else has this proc's pcore, so we need to try to preempt.
621 * after this block, the core will be tracked dealloc'd and on the idle
622 * list (regardless of whether we had to preempt or not) */
623 if (spc_i->alloc_proc) {
624 proc_to_preempt = spc_i->alloc_proc;
625 /* would break both preemption and maybe the later decref */
626 assert(proc_to_preempt != p);
627 /* need to keep a valid, external ref when we unlock */
628 proc_incref(proc_to_preempt, 1);
629 spin_unlock(&sched_lock);
630 /* sending no warning time for now - just an immediate preempt. */
631 success = proc_preempt_core(proc_to_preempt, spc2pcoreid(spc_i), 0);
632 /* reaquire locks to protect provisioning and idle lists */
633 spin_lock(&sched_lock);
635 /* we preempted it before the proc could yield or die.
636 * alloc_proc should not have changed (it'll change in death and
637 * idle CBs). the core is not on the idle core list. (if we
638 * ever have proc alloc lists, it'll still be on the old proc's
640 assert(spc_i->alloc_proc);
641 /* regardless of whether or not it is still prov to p, we need
642 * to note its dealloc. we are doing some excessive checking of
643 * p == prov_proc, but using this helper is a lot clearer. */
644 __prov_track_dealloc(proc_to_preempt, spc2pcoreid(spc_i));
645 /* here, we rely on the fact that we are the only preemptor. we
646 * assume no one else preempted it, so we know it is available*/
647 TAILQ_INSERT_TAIL(&idlecores, spc_i, alloc_next);
649 /* the preempt failed, which should only happen if the pcore was
650 * unmapped (could be dying, could be yielding, but NOT
651 * preempted). whoever unmapped it also triggered (or will soon
652 * trigger) a track_dealloc and put it on the idle list. our
653 * signal for this is spc_i->alloc_proc being 0. We need to
654 * spin and let whoever is trying to free the core grab the
655 * ksched lock. We could use an 'ignore_next_idle' flag per
656 * sched_pcore, but it's not critical anymore.
658 * Note, we're relying on us being the only preemptor - if the
659 * core was unmapped by *another* preemptor, there would be no
660 * way of knowing the core was made idle *yet* (the success
661 * branch in another thread). likewise, if there were another
662 * allocator, the pcore could have been put on the idle list and
663 * then quickly removed/allocated. */
665 while (spc_i->alloc_proc) {
666 /* this loop should be very rare */
667 spin_unlock(&sched_lock);
669 spin_lock(&sched_lock);
672 /* no longer need to keep p_to_pre alive */
673 proc_decref(proc_to_preempt);
674 /* might not be prov to p anymore (rare race). spc_i is idle - we
675 * might get it later, or maybe we'll give it to its rightful proc*/
676 if (spc_i->prov_proc != p)
679 /* at this point, the pcore is idle, regardless of how we got here
680 * (successful preempt, failed preempt, or it was idle in the first
681 * place. the core is still provisioned. lets pull from the idle list
682 * and add it to the pc_arr for p. here, we rely on the fact that we
683 * are the only allocator (spc_i is still idle, despite unlocking). */
684 TAILQ_REMOVE(&idlecores, spc_i, alloc_next);
685 /* At this point, we have the core, ready to try to give it to the proc.
686 * It is on no alloc lists, and is track_dealloc'd() (regardless of how
689 * We'll give p its cores via a bulk list, which is better for the proc
690 * mgmt code (when going from runnable to running). */
691 corelist[nr_to_grant] = spc2pcoreid(spc_i);
693 __prov_track_alloc(p, spc2pcoreid(spc_i));
695 /* Try to get cores from the idle list that aren't prov to me (FCFS) */
696 TAILQ_FOREACH_SAFE(spc_i, &idlecores, alloc_next, temp) {
697 if (nr_to_grant == amt_needed)
699 TAILQ_REMOVE(&idlecores, spc_i, alloc_next);
700 corelist[nr_to_grant] = spc2pcoreid(spc_i);
702 __prov_track_alloc(p, spc2pcoreid(spc_i));
704 /* Now, actually give them out */
706 /* Need to unlock before calling out to proc code. We are somewhat
707 * relying on being the only one allocating 'thread' here, since another
708 * allocator could have seen these cores (if they are prov to some proc)
709 * and could be trying to give them out (and assuming they are already
710 * on the idle list). */
711 spin_unlock(&sched_lock);
712 /* give them the cores. this will start up the extras if RUNNING_M. */
713 spin_lock(&p->proc_lock);
714 /* if they fail, it is because they are WAITING or DYING. we could give
715 * the cores to another proc or whatever. for the current type of
716 * ksched, we'll just put them back on the pile and return. Note, the
717 * ksched could check the states after locking, but it isn't necessary:
718 * just need to check at some point in the ksched loop. */
719 if (__proc_give_cores(p, corelist, nr_to_grant)) {
720 spin_unlock(&p->proc_lock);
721 /* we failed, put the cores and track their dealloc. lock is
722 * protecting those structures. */
723 spin_lock(&sched_lock);
724 __put_idle_cores(p, corelist, nr_to_grant);
725 __prov_track_dealloc_bulk(p, corelist, nr_to_grant);
727 /* at some point after giving cores, call proc_run_m() (harmless on
728 * RUNNING_Ms). You can give small groups of cores, then run them
729 * (which is more efficient than interleaving runs with the gives
730 * for bulk preempted processes). */
732 spin_unlock(&p->proc_lock);
733 /* main mcp_ksched wants this held (it came to __core_req held) */
734 spin_lock(&sched_lock);
737 /* note the ksched lock is still held */
740 /* TODO: need more thorough CG/LL management. For now, core0 is the only LL
741 * core. This won't play well with the ghetto shit in schedule_init() if you do
742 * anything like 'DEDICATED_MONITOR' or the ARSC server. All that needs an
744 static bool is_ll_core(uint32_t pcoreid)
751 /* Helper, makes sure the prov/alloc structures track the pcore properly when it
752 * is allocated to p. Might make this take a sched_pcore * in the future. */
753 static void __prov_track_alloc(struct proc *p, uint32_t pcoreid)
755 struct sched_pcore *spc;
756 assert(pcoreid < num_cpus); /* catch bugs */
757 spc = pcoreid2spc(pcoreid);
758 assert(spc->alloc_proc != p); /* corruption or double-alloc */
760 /* if the pcore is prov to them and now allocated, move lists */
761 if (spc->prov_proc == p) {
762 TAILQ_REMOVE(&p->ksched_data.prov_not_alloc_me, spc, prov_next);
763 TAILQ_INSERT_TAIL(&p->ksched_data.prov_alloc_me, spc, prov_next);
767 /* Helper, makes sure the prov/alloc structures track the pcore properly when it
768 * is deallocated from p. */
769 static void __prov_track_dealloc(struct proc *p, uint32_t pcoreid)
771 struct sched_pcore *spc;
772 assert(pcoreid < num_cpus); /* catch bugs */
773 spc = pcoreid2spc(pcoreid);
775 /* if the pcore is prov to them and now deallocated, move lists */
776 if (spc->prov_proc == p) {
777 TAILQ_REMOVE(&p->ksched_data.prov_alloc_me, spc, prov_next);
778 /* this is the victim list, which can be sorted so that we pick the
779 * right victim (sort by alloc_proc reverse priority, etc). In this
780 * case, the core isn't alloc'd by anyone, so it should be the first
782 TAILQ_INSERT_HEAD(&p->ksched_data.prov_not_alloc_me, spc, prov_next);
786 /* Bulk interface for __prov_track_dealloc */
787 static void __prov_track_dealloc_bulk(struct proc *p, uint32_t *pc_arr,
790 for (int i = 0; i < nr_cores; i++)
791 __prov_track_dealloc(p, pc_arr[i]);
794 /* P will get pcore if it needs more cores next time we look at it */
795 int provision_core(struct proc *p, uint32_t pcoreid)
797 struct sched_pcore *spc;
798 struct sched_pcore_tailq *prov_list;
799 /* Make sure we aren't asking for something that doesn't exist (bounds check
800 * on the pcore array) */
801 if (!(pcoreid < num_cpus)) {
805 /* Don't allow the provisioning of LL cores */
806 if (is_ll_core(pcoreid)) {
810 spc = pcoreid2spc(pcoreid);
811 /* Note the sched lock protects the spc tailqs for all procs in this code.
812 * If we need a finer grained sched lock, this is one place where we could
813 * have a different lock */
814 spin_lock(&sched_lock);
815 /* If the core is already prov to someone else, take it away. (last write
816 * wins, some other layer or new func can handle permissions). */
817 if (spc->prov_proc) {
818 /* the list the spc is on depends on whether it is alloced to the
819 * prov_proc or not */
820 prov_list = (spc->alloc_proc == spc->prov_proc ?
821 &spc->prov_proc->ksched_data.prov_alloc_me :
822 &spc->prov_proc->ksched_data.prov_not_alloc_me);
823 TAILQ_REMOVE(prov_list, spc, prov_next);
825 /* Now prov it to p. Again, the list it goes on depends on whether it is
826 * alloced to p or not. Callers can also send in 0 to de-provision. */
828 if (spc->alloc_proc == p) {
829 TAILQ_INSERT_TAIL(&p->ksched_data.prov_alloc_me, spc, prov_next);
831 /* this is be the victim list, which can be sorted so that we pick
832 * the right victim (sort by alloc_proc reverse priority, etc). */
833 TAILQ_INSERT_TAIL(&p->ksched_data.prov_not_alloc_me, spc,
838 spin_unlock(&sched_lock);
842 /************** Debugging **************/
843 void sched_diag(void)
846 spin_lock(&sched_lock);
847 TAILQ_FOREACH(p, &runnable_scps, ksched_data.proc_link)
848 printk("Runnable _S PID: %d\n", p->pid);
849 TAILQ_FOREACH(p, &unrunnable_scps, ksched_data.proc_link)
850 printk("Unrunnable _S PID: %d\n", p->pid);
851 TAILQ_FOREACH(p, primary_mcps, ksched_data.proc_link)
852 printk("Primary MCP PID: %d\n", p->pid);
853 TAILQ_FOREACH(p, secondary_mcps, ksched_data.proc_link)
854 printk("Secondary MCP PID: %d\n", p->pid);
855 spin_unlock(&sched_lock);
859 void print_idlecoremap(void)
861 struct sched_pcore *spc_i;
862 /* not locking, so we can look at this without deadlocking. */
863 printk("Idle cores (unlocked!):\n");
864 TAILQ_FOREACH(spc_i, &idlecores, alloc_next)
865 printk("Core %d, prov to %d (%p)\n", spc2pcoreid(spc_i),
866 spc_i->prov_proc ? spc_i->prov_proc->pid : 0, spc_i->prov_proc);
869 void print_resources(struct proc *p)
871 printk("--------------------\n");
872 printk("PID: %d\n", p->pid);
873 printk("--------------------\n");
874 for (int i = 0; i < MAX_NUM_RESOURCES; i++)
875 printk("Res type: %02d, amt wanted: %08d, amt granted: %08d\n", i,
876 p->procdata->res_req[i].amt_wanted, p->procinfo->res_grant[i]);
879 void print_all_resources(void)
882 void __print_resources(void *item)
884 print_resources((struct proc*)item);
886 spin_lock(&pid_hash_lock);
887 hash_for_each(pid_hash, __print_resources);
888 spin_unlock(&pid_hash_lock);
891 void print_prov_map(void)
893 struct sched_pcore *spc_i;
894 /* Doing this unlocked, which is dangerous, but won't deadlock */
895 printk("Which cores are provisioned to which procs:\n------------------\n");
896 for (int i = 0; i < num_cpus; i++) {
897 spc_i = pcoreid2spc(i);
898 printk("Core %02d, prov: %d(%p) alloc: %d(%p)\n", i,
899 spc_i->prov_proc ? spc_i->prov_proc->pid : 0, spc_i->prov_proc,
900 spc_i->alloc_proc ? spc_i->alloc_proc->pid : 0,
905 void print_proc_prov(struct proc *p)
907 struct sched_pcore *spc_i;
910 printk("Prov cores alloced to proc %d (%p)\n----------\n", p->pid, p);
911 TAILQ_FOREACH(spc_i, &p->ksched_data.prov_alloc_me, prov_next)
912 printk("Pcore %d\n", spc2pcoreid(spc_i));
913 printk("Prov cores not alloced to proc %d (%p)\n----------\n", p->pid, p);
914 TAILQ_FOREACH(spc_i, &p->ksched_data.prov_not_alloc_me, prov_next)
915 printk("Pcore %d (alloced to %d (%p))\n", spc2pcoreid(spc_i),
916 spc_i->alloc_proc ? spc_i->alloc_proc->pid : 0,
920 void next_core(uint32_t pcoreid)
922 struct sched_pcore *spc_i;
924 spin_lock(&sched_lock);
925 TAILQ_FOREACH(spc_i, &idlecores, alloc_next) {
926 if (spc2pcoreid(spc_i) == pcoreid) {
932 TAILQ_REMOVE(&idlecores, spc_i, alloc_next);
933 TAILQ_INSERT_HEAD(&idlecores, spc_i, alloc_next);
934 printk("Pcore %d will be given out next (from the idles)\n", pcoreid);
936 spin_unlock(&sched_lock);
939 void sort_idles(void)
941 struct sched_pcore *spc_i, *spc_j, *temp;
942 struct sched_pcore_tailq sorter = TAILQ_HEAD_INITIALIZER(sorter);
944 spin_lock(&sched_lock);
945 TAILQ_CONCAT(&sorter, &idlecores, alloc_next);
946 TAILQ_FOREACH_SAFE(spc_i, &sorter, alloc_next, temp) {
947 TAILQ_REMOVE(&sorter, spc_i, alloc_next);
949 /* don't need foreach_safe since we break after we muck with the list */
950 TAILQ_FOREACH(spc_j, &idlecores, alloc_next) {
952 TAILQ_INSERT_BEFORE(spc_j, spc_i, alloc_next);
958 TAILQ_INSERT_TAIL(&idlecores, spc_i, alloc_next);
960 spin_unlock(&sched_lock);