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 * ideally, we'd know if a specific mgmt core is sleeping and wake it
314 * up. o/w, we could interrupt an already-running mgmt core that won't
315 * get to our new proc anytime soon. also, by poking core 0, a
316 * different mgmt core could remain idle (and this process would sleep)
317 * until its tick goes off */
318 send_ipi(0, I_POKE_CORE);
322 /* Callback to return a core to the ksched, which tracks it as idle and
323 * deallocated from p. The proclock is held (__core_req depends on that).
325 * This also is a trigger, telling us we have more cores. We could/should make
326 * a scheduling decision (or at least plan to). */
327 void __sched_put_idle_core(struct proc *p, uint32_t coreid)
329 struct sched_pcore *spc = pcoreid2spc(coreid);
330 spin_lock(&sched_lock);
331 TAILQ_INSERT_TAIL(&idlecores, spc, alloc_next);
332 __prov_track_dealloc(p, coreid);
333 spin_unlock(&sched_lock);
336 /* Helper for put_idle and core_req. Note this does not track_dealloc. When we
337 * get rid of / revise proc_preempt_all and put_idle_cores, we can get rid of
338 * this. (the ksched will never need it - only external callers). */
339 static void __put_idle_cores(struct proc *p, uint32_t *pc_arr, uint32_t num)
341 struct sched_pcore *spc_i;
342 for (int i = 0; i < num; i++) {
343 spc_i = pcoreid2spc(pc_arr[i]);
344 TAILQ_INSERT_TAIL(&idlecores, spc_i, alloc_next);
348 /* Callback, bulk interface for put_idle. Note this one also calls track_dealloc,
349 * which the internal version does not. The proclock is held for this. */
350 void __sched_put_idle_cores(struct proc *p, uint32_t *pc_arr, uint32_t num)
352 spin_lock(&sched_lock);
353 /* TODO: when we revise this func, look at __put_idle */
354 __put_idle_cores(p, pc_arr, num);
355 __prov_track_dealloc_bulk(p, pc_arr, num);
356 spin_unlock(&sched_lock);
357 /* could trigger a sched decision here */
360 /* mgmt/LL cores should call this to schedule the calling core and give it to an
361 * SCP. will also prune the dead SCPs from the list. hold the lock before
362 * calling. returns TRUE if it scheduled a proc. */
363 static bool __schedule_scp(void)
365 // TODO: sort out lock ordering (proc_run_s also locks)
367 uint32_t pcoreid = core_id();
368 struct per_cpu_info *pcpui = &per_cpu_info[pcoreid];
370 /* if there are any runnables, run them here and put any currently running
371 * SCP on the tail of the runnable queue. */
372 if ((p = TAILQ_FIRST(&runnable_scps))) {
373 /* protect owning proc, cur_ctx, etc. note this nests with the
374 * calls in proc_yield_s */
375 disable_irqsave(&state);
376 /* someone is currently running, dequeue them */
377 if (pcpui->owning_proc) {
378 spin_lock(&pcpui->owning_proc->proc_lock);
379 /* process might be dying, with a KMSG to clean it up waiting on
380 * this core. can't do much, so we'll attempt to restart */
381 if (pcpui->owning_proc->state == PROC_DYING) {
382 send_kernel_message(core_id(), __just_sched, 0, 0, 0,
384 spin_unlock(&pcpui->owning_proc->proc_lock);
385 enable_irqsave(&state);
388 printd("Descheduled %d in favor of %d\n", pcpui->owning_proc->pid,
390 __proc_set_state(pcpui->owning_proc, PROC_RUNNABLE_S);
391 /* Saving FP state aggressively. Odds are, the SCP was hit by an
392 * IRQ and has a HW ctx, in which case we must save. */
393 __proc_save_fpu_s(pcpui->owning_proc);
394 __proc_save_context_s(pcpui->owning_proc, pcpui->cur_ctx);
395 vcore_account_offline(pcpui->owning_proc, 0); /* VC# */
396 spin_unlock(&pcpui->owning_proc->proc_lock);
397 /* round-robin the SCPs (inserts at the end of the queue) */
398 switch_lists(pcpui->owning_proc, &unrunnable_scps, &runnable_scps);
399 clear_owning_proc(pcoreid);
400 /* Note we abandon core. It's not strictly necessary. If
401 * we didn't, the TLB would still be loaded with the old
402 * one, til we proc_run_s, and the various paths in
403 * proc_run_s would pick it up. This way is a bit safer for
404 * future changes, but has an extra (empty) TLB flush. */
407 /* Run the new proc */
408 switch_lists(p, &runnable_scps, &unrunnable_scps);
409 printd("PID of the SCP i'm running: %d\n", p->pid);
410 proc_run_s(p); /* gives it core we're running on */
411 enable_irqsave(&state);
417 /* Returns how many new cores p needs. This doesn't lock the proc, so your
418 * answer might be stale. */
419 static uint32_t get_cores_needed(struct proc *p)
421 uint32_t amt_wanted, amt_granted;
422 amt_wanted = p->procdata->res_req[RES_CORES].amt_wanted;
423 /* Help them out - if they ask for something impossible, give them 1 so they
424 * can make some progress. (this is racy, and unnecessary). */
425 if (amt_wanted > p->procinfo->max_vcores) {
426 printk("[kernel] proc %d wanted more than max, wanted %d\n", p->pid,
428 p->procdata->res_req[RES_CORES].amt_wanted = 1;
431 /* There are a few cases where amt_wanted is 0, but they are still RUNNABLE
432 * (involving yields, events, and preemptions). In these cases, give them
433 * at least 1, so they can make progress and yield properly. If they are
434 * not WAITING, they did not yield and may have missed a message. */
436 /* could ++, but there could be a race and we don't want to give them
437 * more than they ever asked for (in case they haven't prepped) */
438 p->procdata->res_req[RES_CORES].amt_wanted = 1;
441 /* amt_granted is racy - they could be *yielding*, but currently they can't
442 * be getting any new cores if the caller is in the mcp_ksched. this is
443 * okay - we won't accidentally give them more cores than they *ever* wanted
444 * (which could crash them), but our answer might be a little stale. */
445 amt_granted = p->procinfo->res_grant[RES_CORES];
446 /* Do not do an assert like this: it could fail (yield in progress): */
447 //assert(amt_granted == p->procinfo->num_vcores);
448 if (amt_wanted <= amt_granted)
450 return amt_wanted - amt_granted;
453 /* Actual work of the MCP kscheduler. if we were called by poke_ksched, *arg
454 * might be the process who wanted special service. this would be the case if
455 * we weren't already running the ksched. Sort of a ghetto way to "post work",
456 * such that it's an optimization. */
457 static void __run_mcp_ksched(void *arg)
459 struct proc *p, *temp;
461 struct proc_list *temp_mcp_list;
462 /* locking to protect the MCP lists' integrity and membership */
463 spin_lock(&sched_lock);
464 /* 2-pass scheme: check each proc on the primary list (FCFS). if they need
465 * nothing, put them on the secondary list. if they need something, rip
466 * them off the list, service them, and if they are still not dying, put
467 * them on the secondary list. We cull the entire primary list, so that
468 * when we start from the beginning each time, we aren't repeatedly checking
469 * procs we looked at on previous waves.
471 * TODO: we could modify this such that procs that we failed to service move
472 * to yet another list or something. We can also move the WAITINGs to
473 * another list and have wakeup move them back, etc. */
474 while (!TAILQ_EMPTY(primary_mcps)) {
475 TAILQ_FOREACH_SAFE(p, primary_mcps, ksched_data.proc_link, temp) {
476 if (p->state == PROC_WAITING) { /* unlocked peek at the state */
477 switch_lists(p, primary_mcps, secondary_mcps);
480 amt_needed = get_cores_needed(p);
482 switch_lists(p, primary_mcps, secondary_mcps);
485 /* o/w, we want to give cores to this proc */
486 remove_from_list(p, primary_mcps);
487 /* now it won't die, but it could get removed from lists and have
488 * its stuff unprov'd when we unlock */
490 /* GIANT WARNING: __core_req will unlock the sched lock for a bit.
491 * It will return with it locked still. We could unlock before we
492 * pass in, but they will relock right away. */
493 // notionally_unlock(&ksched_lock); /* for mouse-eyed viewers */
494 __core_request(p, amt_needed);
495 // notionally_lock(&ksched_lock);
496 /* Peeking at the state is okay, since we hold a ref. Once it is
497 * DYING, it'll remain DYING until we decref. And if there is a
498 * concurrent death, that will spin on the ksched lock (which we
499 * hold, and which protects the proc lists). */
500 if (p->state != PROC_DYING)
501 add_to_list(p, secondary_mcps);
502 proc_decref(p); /* fyi, this may trigger __proc_free */
503 /* need to break: the proc lists may have changed when we unlocked
504 * in core_req in ways that the FOREACH_SAFE can't handle. */
508 /* at this point, we moved all the procs over to the secondary list, and
509 * attempted to service the ones that wanted something. now just swap the
510 * lists for the next invocation of the ksched. */
511 temp_mcp_list = primary_mcps;
512 primary_mcps = secondary_mcps;
513 secondary_mcps = temp_mcp_list;
514 spin_unlock(&sched_lock);
517 /* Something has changed, and for whatever reason the scheduler should
520 * Don't call this if you are processing a syscall or otherwise care about your
521 * kthread variables, cur_proc/owning_proc, etc.
523 * Don't call this from interrupt context (grabs proclocks). */
524 void run_scheduler(void)
526 /* MCP scheduling: post work, then poke. for now, i just want the func to
527 * run again, so merely a poke is sufficient. */
528 poke(&ksched_poker, 0);
529 if (management_core()) {
530 spin_lock(&sched_lock);
532 spin_unlock(&sched_lock);
536 /* A process is asking the ksched to look at its resource desires. The
537 * scheduler is free to ignore this, for its own reasons, so long as it
538 * eventually gets around to looking at resource desires. */
539 void poke_ksched(struct proc *p, unsigned int res_type)
541 /* ignoring res_type for now. could post that if we wanted (would need some
542 * other structs/flags) */
543 if (!__proc_is_mcp(p))
545 poke(&ksched_poker, p);
548 /* The calling cpu/core has nothing to do and plans to idle/halt. This is an
549 * opportunity to pick the nature of that halting (low power state, etc), or
550 * provide some other work (_Ss on LL cores). Note that interrupts are
551 * disabled, and if you return, the core will cpu_halt(). */
554 bool new_proc = FALSE;
555 if (!management_core())
557 spin_lock(&sched_lock);
558 new_proc = __schedule_scp();
559 spin_unlock(&sched_lock);
560 /* if we just scheduled a proc, we need to manually restart it, instead of
561 * returning. if we return, the core will halt. */
566 /* Could drop into the monitor if there are no processes at all. For now,
567 * the 'call of the giraffe' suffices. */
570 /* Available resources changed (plus or minus). Some parts of the kernel may
571 * call this if a particular resource that is 'quantity-based' changes. Things
572 * like available RAM to processes, bandwidth, etc. Cores would probably be
573 * inappropriate, since we need to know which specific core is now free. */
574 void avail_res_changed(int res_type, long change)
576 printk("[kernel] ksched doesn't track any resources yet!\n");
579 /* Normally it'll be the max number of CG cores ever */
580 uint32_t max_vcores(struct proc *p)
583 #ifdef CONFIG_DISABLE_SMT
584 return num_cpus >> 1;
586 return num_cpus - 1; /* reserving core 0 */
587 #endif /* CONFIG_DISABLE_SMT */
590 /* This deals with a request for more cores. The amt of new cores needed is
591 * passed in. The ksched lock is held, but we are free to unlock if we want
592 * (and we must, if calling out of the ksched to anything high-level).
594 * Side note: if we want to warn, then we can't deal with this proc's prov'd
595 * cores until we wait til the alarm goes off. would need to put all
596 * alarmed cores on a list and wait til the alarm goes off to do the full
597 * preempt. and when those cores come in voluntarily, we'd need to know to
598 * give them to this proc. */
599 static void __core_request(struct proc *p, uint32_t amt_needed)
601 uint32_t nr_to_grant = 0;
602 uint32_t corelist[num_cpus];
603 struct sched_pcore *spc_i, *temp;
604 struct proc *proc_to_preempt;
606 /* we come in holding the ksched lock, and we hold it here to protect
607 * allocations and provisioning. */
608 /* get all available cores from their prov_not_alloc list. the list might
609 * change when we unlock (new cores added to it, or the entire list emptied,
610 * but no core allocations will happen (we hold the poke)). */
611 while (!TAILQ_EMPTY(&p->ksched_data.prov_not_alloc_me)) {
612 if (nr_to_grant == amt_needed)
614 /* picking the next victim (first on the not_alloc list) */
615 spc_i = TAILQ_FIRST(&p->ksched_data.prov_not_alloc_me);
616 /* someone else has this proc's pcore, so we need to try to preempt.
617 * after this block, the core will be tracked dealloc'd and on the idle
618 * list (regardless of whether we had to preempt or not) */
619 if (spc_i->alloc_proc) {
620 proc_to_preempt = spc_i->alloc_proc;
621 /* would break both preemption and maybe the later decref */
622 assert(proc_to_preempt != p);
623 /* need to keep a valid, external ref when we unlock */
624 proc_incref(proc_to_preempt, 1);
625 spin_unlock(&sched_lock);
626 /* sending no warning time for now - just an immediate preempt. */
627 success = proc_preempt_core(proc_to_preempt, spc2pcoreid(spc_i), 0);
628 /* reaquire locks to protect provisioning and idle lists */
629 spin_lock(&sched_lock);
631 /* we preempted it before the proc could yield or die.
632 * alloc_proc should not have changed (it'll change in death and
633 * idle CBs). the core is not on the idle core list. (if we
634 * ever have proc alloc lists, it'll still be on the old proc's
636 assert(spc_i->alloc_proc);
637 /* regardless of whether or not it is still prov to p, we need
638 * to note its dealloc. we are doing some excessive checking of
639 * p == prov_proc, but using this helper is a lot clearer. */
640 __prov_track_dealloc(proc_to_preempt, spc2pcoreid(spc_i));
641 /* here, we rely on the fact that we are the only preemptor. we
642 * assume no one else preempted it, so we know it is available*/
643 TAILQ_INSERT_TAIL(&idlecores, spc_i, alloc_next);
645 /* the preempt failed, which should only happen if the pcore was
646 * unmapped (could be dying, could be yielding, but NOT
647 * preempted). whoever unmapped it also triggered (or will soon
648 * trigger) a track_dealloc and put it on the idle list. our
649 * signal for this is spc_i->alloc_proc being 0. We need to
650 * spin and let whoever is trying to free the core grab the
651 * ksched lock. We could use an 'ignore_next_idle' flag per
652 * sched_pcore, but it's not critical anymore.
654 * Note, we're relying on us being the only preemptor - if the
655 * core was unmapped by *another* preemptor, there would be no
656 * way of knowing the core was made idle *yet* (the success
657 * branch in another thread). likewise, if there were another
658 * allocator, the pcore could have been put on the idle list and
659 * then quickly removed/allocated. */
661 while (spc_i->alloc_proc) {
662 /* this loop should be very rare */
663 spin_unlock(&sched_lock);
665 spin_lock(&sched_lock);
668 /* no longer need to keep p_to_pre alive */
669 proc_decref(proc_to_preempt);
670 /* might not be prov to p anymore (rare race). spc_i is idle - we
671 * might get it later, or maybe we'll give it to its rightful proc*/
672 if (spc_i->prov_proc != p)
675 /* at this point, the pcore is idle, regardless of how we got here
676 * (successful preempt, failed preempt, or it was idle in the first
677 * place. the core is still provisioned. lets pull from the idle list
678 * and add it to the pc_arr for p. here, we rely on the fact that we
679 * are the only allocator (spc_i is still idle, despite unlocking). */
680 TAILQ_REMOVE(&idlecores, spc_i, alloc_next);
681 /* At this point, we have the core, ready to try to give it to the proc.
682 * It is on no alloc lists, and is track_dealloc'd() (regardless of how
685 * We'll give p its cores via a bulk list, which is better for the proc
686 * mgmt code (when going from runnable to running). */
687 corelist[nr_to_grant] = spc2pcoreid(spc_i);
689 __prov_track_alloc(p, spc2pcoreid(spc_i));
691 /* Try to get cores from the idle list that aren't prov to me (FCFS) */
692 TAILQ_FOREACH_SAFE(spc_i, &idlecores, alloc_next, temp) {
693 if (nr_to_grant == amt_needed)
695 TAILQ_REMOVE(&idlecores, spc_i, alloc_next);
696 corelist[nr_to_grant] = spc2pcoreid(spc_i);
698 __prov_track_alloc(p, spc2pcoreid(spc_i));
700 /* Now, actually give them out */
702 /* Need to unlock before calling out to proc code. We are somewhat
703 * relying on being the only one allocating 'thread' here, since another
704 * allocator could have seen these cores (if they are prov to some proc)
705 * and could be trying to give them out (and assuming they are already
706 * on the idle list). */
707 spin_unlock(&sched_lock);
708 /* give them the cores. this will start up the extras if RUNNING_M. */
709 spin_lock(&p->proc_lock);
710 /* if they fail, it is because they are WAITING or DYING. we could give
711 * the cores to another proc or whatever. for the current type of
712 * ksched, we'll just put them back on the pile and return. Note, the
713 * ksched could check the states after locking, but it isn't necessary:
714 * just need to check at some point in the ksched loop. */
715 if (__proc_give_cores(p, corelist, nr_to_grant)) {
716 spin_unlock(&p->proc_lock);
717 /* we failed, put the cores and track their dealloc. lock is
718 * protecting those structures. */
719 spin_lock(&sched_lock);
720 __put_idle_cores(p, corelist, nr_to_grant);
721 __prov_track_dealloc_bulk(p, corelist, nr_to_grant);
723 /* at some point after giving cores, call proc_run_m() (harmless on
724 * RUNNING_Ms). You can give small groups of cores, then run them
725 * (which is more efficient than interleaving runs with the gives
726 * for bulk preempted processes). */
728 spin_unlock(&p->proc_lock);
729 /* main mcp_ksched wants this held (it came to __core_req held) */
730 spin_lock(&sched_lock);
733 /* note the ksched lock is still held */
736 /* TODO: need more thorough CG/LL management. For now, core0 is the only LL
737 * core. This won't play well with the ghetto shit in schedule_init() if you do
738 * anything like 'DEDICATED_MONITOR' or the ARSC server. All that needs an
740 static bool is_ll_core(uint32_t pcoreid)
747 /* Helper, makes sure the prov/alloc structures track the pcore properly when it
748 * is allocated to p. Might make this take a sched_pcore * in the future. */
749 static void __prov_track_alloc(struct proc *p, uint32_t pcoreid)
751 struct sched_pcore *spc;
752 assert(pcoreid < num_cpus); /* catch bugs */
753 spc = pcoreid2spc(pcoreid);
754 assert(spc->alloc_proc != p); /* corruption or double-alloc */
756 /* if the pcore is prov to them and now allocated, move lists */
757 if (spc->prov_proc == p) {
758 TAILQ_REMOVE(&p->ksched_data.prov_not_alloc_me, spc, prov_next);
759 TAILQ_INSERT_TAIL(&p->ksched_data.prov_alloc_me, spc, prov_next);
763 /* Helper, makes sure the prov/alloc structures track the pcore properly when it
764 * is deallocated from p. */
765 static void __prov_track_dealloc(struct proc *p, uint32_t pcoreid)
767 struct sched_pcore *spc;
768 assert(pcoreid < num_cpus); /* catch bugs */
769 spc = pcoreid2spc(pcoreid);
771 /* if the pcore is prov to them and now deallocated, move lists */
772 if (spc->prov_proc == p) {
773 TAILQ_REMOVE(&p->ksched_data.prov_alloc_me, spc, prov_next);
774 /* this is the victim list, which can be sorted so that we pick the
775 * right victim (sort by alloc_proc reverse priority, etc). In this
776 * case, the core isn't alloc'd by anyone, so it should be the first
778 TAILQ_INSERT_HEAD(&p->ksched_data.prov_not_alloc_me, spc, prov_next);
782 /* Bulk interface for __prov_track_dealloc */
783 static void __prov_track_dealloc_bulk(struct proc *p, uint32_t *pc_arr,
786 for (int i = 0; i < nr_cores; i++)
787 __prov_track_dealloc(p, pc_arr[i]);
790 /* P will get pcore if it needs more cores next time we look at it */
791 int provision_core(struct proc *p, uint32_t pcoreid)
793 struct sched_pcore *spc;
794 struct sched_pcore_tailq *prov_list;
795 /* Make sure we aren't asking for something that doesn't exist (bounds check
796 * on the pcore array) */
797 if (!(pcoreid < num_cpus)) {
801 /* Don't allow the provisioning of LL cores */
802 if (is_ll_core(pcoreid)) {
806 spc = pcoreid2spc(pcoreid);
807 /* Note the sched lock protects the spc tailqs for all procs in this code.
808 * If we need a finer grained sched lock, this is one place where we could
809 * have a different lock */
810 spin_lock(&sched_lock);
811 /* If the core is already prov to someone else, take it away. (last write
812 * wins, some other layer or new func can handle permissions). */
813 if (spc->prov_proc) {
814 /* the list the spc is on depends on whether it is alloced to the
815 * prov_proc or not */
816 prov_list = (spc->alloc_proc == spc->prov_proc ?
817 &spc->prov_proc->ksched_data.prov_alloc_me :
818 &spc->prov_proc->ksched_data.prov_not_alloc_me);
819 TAILQ_REMOVE(prov_list, spc, prov_next);
821 /* Now prov it to p. Again, the list it goes on depends on whether it is
822 * alloced to p or not. Callers can also send in 0 to de-provision. */
824 if (spc->alloc_proc == p) {
825 TAILQ_INSERT_TAIL(&p->ksched_data.prov_alloc_me, spc, prov_next);
827 /* this is be the victim list, which can be sorted so that we pick
828 * the right victim (sort by alloc_proc reverse priority, etc). */
829 TAILQ_INSERT_TAIL(&p->ksched_data.prov_not_alloc_me, spc,
834 spin_unlock(&sched_lock);
838 /************** Debugging **************/
839 void sched_diag(void)
842 spin_lock(&sched_lock);
843 TAILQ_FOREACH(p, &runnable_scps, ksched_data.proc_link)
844 printk("Runnable _S PID: %d\n", p->pid);
845 TAILQ_FOREACH(p, &unrunnable_scps, ksched_data.proc_link)
846 printk("Unrunnable _S PID: %d\n", p->pid);
847 TAILQ_FOREACH(p, primary_mcps, ksched_data.proc_link)
848 printk("Primary MCP PID: %d\n", p->pid);
849 TAILQ_FOREACH(p, secondary_mcps, ksched_data.proc_link)
850 printk("Secondary MCP PID: %d\n", p->pid);
851 spin_unlock(&sched_lock);
855 void print_idlecoremap(void)
857 struct sched_pcore *spc_i;
858 /* not locking, so we can look at this without deadlocking. */
859 printk("Idle cores (unlocked!):\n");
860 TAILQ_FOREACH(spc_i, &idlecores, alloc_next)
861 printk("Core %d, prov to %d (%p)\n", spc2pcoreid(spc_i),
862 spc_i->prov_proc ? spc_i->prov_proc->pid : 0, spc_i->prov_proc);
865 void print_resources(struct proc *p)
867 printk("--------------------\n");
868 printk("PID: %d\n", p->pid);
869 printk("--------------------\n");
870 for (int i = 0; i < MAX_NUM_RESOURCES; i++)
871 printk("Res type: %02d, amt wanted: %08d, amt granted: %08d\n", i,
872 p->procdata->res_req[i].amt_wanted, p->procinfo->res_grant[i]);
875 void print_all_resources(void)
878 void __print_resources(void *item)
880 print_resources((struct proc*)item);
882 spin_lock(&pid_hash_lock);
883 hash_for_each(pid_hash, __print_resources);
884 spin_unlock(&pid_hash_lock);
887 void print_prov_map(void)
889 struct sched_pcore *spc_i;
890 /* Doing this unlocked, which is dangerous, but won't deadlock */
891 printk("Which cores are provisioned to which procs:\n------------------\n");
892 for (int i = 0; i < num_cpus; i++) {
893 spc_i = pcoreid2spc(i);
894 printk("Core %02d, prov: %d(%p) alloc: %d(%p)\n", i,
895 spc_i->prov_proc ? spc_i->prov_proc->pid : 0, spc_i->prov_proc,
896 spc_i->alloc_proc ? spc_i->alloc_proc->pid : 0,
901 void print_proc_prov(struct proc *p)
903 struct sched_pcore *spc_i;
906 printk("Prov cores alloced to proc %d (%p)\n----------\n", p->pid, p);
907 TAILQ_FOREACH(spc_i, &p->ksched_data.prov_alloc_me, prov_next)
908 printk("Pcore %d\n", spc2pcoreid(spc_i));
909 printk("Prov cores not alloced to proc %d (%p)\n----------\n", p->pid, p);
910 TAILQ_FOREACH(spc_i, &p->ksched_data.prov_not_alloc_me, prov_next)
911 printk("Pcore %d (alloced to %d (%p))\n", spc2pcoreid(spc_i),
912 spc_i->alloc_proc ? spc_i->alloc_proc->pid : 0,
916 void next_core(uint32_t pcoreid)
918 struct sched_pcore *spc_i;
920 spin_lock(&sched_lock);
921 TAILQ_FOREACH(spc_i, &idlecores, alloc_next) {
922 if (spc2pcoreid(spc_i) == pcoreid) {
928 TAILQ_REMOVE(&idlecores, spc_i, alloc_next);
929 TAILQ_INSERT_HEAD(&idlecores, spc_i, alloc_next);
930 printk("Pcore %d will be given out next (from the idles)\n", pcoreid);
932 spin_unlock(&sched_lock);