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>
22 #include <arsc_server.h>
24 /* Process Lists. 'unrunnable' is a holding list for SCPs that are running or
25 * waiting or otherwise not considered for sched decisions. */
26 struct proc_list unrunnable_scps = TAILQ_HEAD_INITIALIZER(unrunnable_scps);
27 struct proc_list runnable_scps = TAILQ_HEAD_INITIALIZER(runnable_scps);
28 /* mcp lists. we actually could get by with one list and a TAILQ_CONCAT, but
29 * I'm expecting to want the flexibility of the pointers later. */
30 struct proc_list all_mcps_1 = TAILQ_HEAD_INITIALIZER(all_mcps_1);
31 struct proc_list all_mcps_2 = TAILQ_HEAD_INITIALIZER(all_mcps_2);
32 struct proc_list *primary_mcps = &all_mcps_1;
33 struct proc_list *secondary_mcps = &all_mcps_2;
35 /* The pcores in the system. (array gets alloced in init()). */
36 struct sched_pcore *all_pcores;
38 /* TAILQ of all unallocated, idle (CG) cores */
39 struct sched_pcore_tailq idlecores = TAILQ_HEAD_INITIALIZER(idlecores);
41 /* Helper, defined below */
42 static void __core_request(struct proc *p, uint32_t amt_needed);
43 static void __put_idle_cores(struct proc *p, uint32_t *pc_arr, uint32_t num);
44 static void add_to_list(struct proc *p, struct proc_list *list);
45 static void remove_from_list(struct proc *p, struct proc_list *list);
46 static void switch_lists(struct proc *p, struct proc_list *old,
47 struct proc_list *new);
48 static uint32_t spc2pcoreid(struct sched_pcore *spc);
49 static struct sched_pcore *pcoreid2spc(uint32_t pcoreid);
50 static bool is_ll_core(uint32_t pcoreid);
51 static void __prov_track_alloc(struct proc *p, uint32_t pcoreid);
52 static void __prov_track_dealloc(struct proc *p, uint32_t pcoreid);
53 static void __prov_track_dealloc_bulk(struct proc *p, uint32_t *pc_arr,
55 static void __run_mcp_ksched(void *arg); /* don't call directly */
56 static uint32_t get_cores_needed(struct proc *p);
58 /* Locks / sync tools */
60 /* poke-style ksched - ensures the MCP ksched only runs once at a time. since
61 * only one mcp ksched runs at a time, while this is set, the ksched knows no
62 * cores are being allocated by other code (though they could be dealloc, due to
65 * The main value to this sync method is to make the 'make sure the ksched runs
66 * only once at a time and that it actually runs' invariant/desire wait-free, so
67 * that it can be called anywhere (deep event code, etc).
69 * As the ksched gets smarter, we'll probably embedd this poker in a bigger
70 * struct that can handle the posting of different types of work. */
71 struct poke_tracker ksched_poker = POKE_INITIALIZER(__run_mcp_ksched);
73 /* this 'big ksched lock' protects a bunch of things, which i may make fine
75 /* - protects the integrity of proc tailqs/structures, as well as the membership
76 * of a proc on those lists. proc lifetime within the ksched but outside this
77 * lock is protected by the proc kref. */
78 //spinlock_t proclist_lock = SPINLOCK_INITIALIZER; /* subsumed by bksl */
79 /* - protects the provisioning assignment, membership of sched_pcores in
80 * provision lists, and the integrity of all prov lists (the lists of each
81 * proc). does NOT protect spc->alloc_proc. */
82 //spinlock_t prov_lock = SPINLOCK_INITIALIZER;
83 /* - protects allocation structures: spc->alloc_proc, the integrity and
84 * membership of the idelcores tailq. */
85 //spinlock_t alloc_lock = SPINLOCK_INITIALIZER;
86 spinlock_t sched_lock = SPINLOCK_INITIALIZER;
88 /* Alarm struct, for our example 'timer tick' */
89 struct alarm_waiter ksched_waiter;
91 #define TIMER_TICK_USEC 10000 /* 10msec */
93 /* Helper: Sets up a timer tick on the calling core to go off 10 msec from now.
94 * This assumes the calling core is an LL core, etc. */
95 static void set_ksched_alarm(void)
97 set_awaiter_rel(&ksched_waiter, TIMER_TICK_USEC);
98 set_alarm(&per_cpu_info[core_id()].tchain, &ksched_waiter);
101 /* Need a kmsg to just run the sched, but not to rearm */
102 static void __just_sched(uint32_t srcid, long a0, long a1, long a2)
107 /* RKM alarm, to run the scheduler tick (not in interrupt context) and reset the
108 * alarm. Note that interrupts will be disabled, but this is not the same as
109 * interrupt context. We're a routine kmsg, which means the core is in a
110 * quiescent state. */
111 static void __ksched_tick(struct alarm_waiter *waiter)
113 /* TODO: imagine doing some accounting here */
115 /* Set our alarm to go off, incrementing from our last tick (instead of
116 * setting it relative to now, since some time has passed since the alarm
117 * first went off. Note, this may be now or in the past! */
118 set_awaiter_inc(&ksched_waiter, TIMER_TICK_USEC);
119 set_alarm(&per_cpu_info[core_id()].tchain, &ksched_waiter);
122 void schedule_init(void)
124 spin_lock(&sched_lock);
125 /* init provisioning stuff */
126 all_pcores = kmalloc(sizeof(struct sched_pcore) * num_cpus, 0);
127 memset(all_pcores, 0, sizeof(struct sched_pcore) * num_cpus);
128 assert(!core_id()); /* want the alarm on core0 for now */
129 init_awaiter(&ksched_waiter, __ksched_tick);
131 /* init the idlecore list. if they turned off hyperthreading, give them the
132 * odds from 1..max-1. otherwise, give them everything by 0 (default mgmt
133 * core). TODO: (CG/LL) better LL/CG mgmt */
134 #ifndef CONFIG_DISABLE_SMT
135 for (int i = 1; i < num_cpus; i++)
136 TAILQ_INSERT_TAIL(&idlecores, pcoreid2spc(i), alloc_next);
138 assert(!(num_cpus % 2));
139 for (int i = 1; i < num_cpus; i += 2)
140 TAILQ_INSERT_TAIL(&idlecores, pcoreid2spc(i), alloc_next);
141 #endif /* CONFIG_DISABLE_SMT */
142 spin_unlock(&sched_lock);
144 #ifdef CONFIG_ARSC_SERVER
145 int arsc_coreid = get_any_idle_core();
146 assert(arsc_coreid >= 0);
147 send_kernel_message(arsc_coreid, arsc_server, 0, 0, 0, KMSG_ROUTINE);
148 printk("Using core %d for the ARSC server\n", arsc_coreid);
149 #endif /* CONFIG_ARSC_SERVER */
152 static uint32_t spc2pcoreid(struct sched_pcore *spc)
154 return spc - all_pcores;
157 static struct sched_pcore *pcoreid2spc(uint32_t pcoreid)
159 return &all_pcores[pcoreid];
162 /* Round-robins on whatever list it's on */
163 static void add_to_list(struct proc *p, struct proc_list *new)
165 assert(!(p->ksched_data.cur_list));
166 TAILQ_INSERT_TAIL(new, p, ksched_data.proc_link);
167 p->ksched_data.cur_list = new;
170 static void remove_from_list(struct proc *p, struct proc_list *old)
172 assert(p->ksched_data.cur_list == old);
173 TAILQ_REMOVE(old, p, ksched_data.proc_link);
174 p->ksched_data.cur_list = 0;
177 static void switch_lists(struct proc *p, struct proc_list *old,
178 struct proc_list *new)
180 remove_from_list(p, old);
184 /* Removes from whatever list p is on */
185 static void remove_from_any_list(struct proc *p)
187 if (p->ksched_data.cur_list) {
188 TAILQ_REMOVE(p->ksched_data.cur_list, p, ksched_data.proc_link);
189 p->ksched_data.cur_list = 0;
193 /************** Process Management Callbacks **************/
195 * - the proc lock is NOT held for any of these calls. currently, there is no
196 * lock ordering between the sched lock and the proc lock. since the proc
197 * code doesn't know what we do, it doesn't hold its lock when calling our
199 * - since the proc lock isn't held, the proc could be dying, which means we
200 * will receive a __sched_proc_destroy() either before or after some of these
201 * other CBs. the CBs related to list management need to check and abort if
203 void __sched_proc_register(struct proc *p)
205 assert(p->state != PROC_DYING); /* shouldn't be abel to happen yet */
206 /* one ref for the proc's existence, cradle-to-grave */
207 proc_incref(p, 1); /* need at least this OR the 'one for existing' */
208 spin_lock(&sched_lock);
209 TAILQ_INIT(&p->ksched_data.prov_alloc_me);
210 TAILQ_INIT(&p->ksched_data.prov_not_alloc_me);
211 add_to_list(p, &unrunnable_scps);
212 spin_unlock(&sched_lock);
215 /* Returns 0 if it succeeded, an error code otherwise. */
216 void __sched_proc_change_to_m(struct proc *p)
218 spin_lock(&sched_lock);
219 /* Need to make sure they aren't dying. if so, we already dealt with their
220 * list membership, etc (or soon will). taking advantage of the 'immutable
221 * state' of dying (so long as refs are held). */
222 if (p->state == PROC_DYING) {
223 spin_unlock(&sched_lock);
226 /* Catch user bugs */
227 if (!p->procdata->res_req[RES_CORES].amt_wanted) {
228 printk("[kernel] process needs to specify amt_wanted\n");
229 p->procdata->res_req[RES_CORES].amt_wanted = 1;
231 /* For now, this should only ever be called on an unrunnable. It's
232 * probably a bug, at this stage in development, to do o/w. */
233 remove_from_list(p, &unrunnable_scps);
234 //remove_from_any_list(p); /* ^^ instead of this */
235 add_to_list(p, primary_mcps);
236 spin_unlock(&sched_lock);
237 //poke_ksched(p, RES_CORES);
240 /* Helper for the destroy CB : unprovisions any pcores for the given list */
241 static void unprov_pcore_list(struct sched_pcore_tailq *list_head)
243 struct sched_pcore *spc_i;
244 /* We can leave them connected within the tailq, since the scps don't have a
245 * default list (if they aren't on a proc's list, then we don't care about
246 * them), and since the INSERTs don't care what list you were on before
247 * (chummy with the implementation). Pretty sure this is right. If there's
248 * suspected list corruption, be safer here. */
249 TAILQ_FOREACH(spc_i, list_head, prov_next)
250 spc_i->prov_proc = 0;
251 TAILQ_INIT(list_head);
254 /* Sched callback called when the proc dies. pc_arr holds the cores the proc
255 * had, if any, and nr_cores tells us how many are in the array.
257 * An external, edible ref is passed in. when we return and they decref,
258 * __proc_free will be called (when the last one is done). */
259 void __sched_proc_destroy(struct proc *p, uint32_t *pc_arr, uint32_t nr_cores)
261 spin_lock(&sched_lock);
262 /* Unprovision any cores. Note this is different than track_dealloc.
263 * The latter does bookkeeping when an allocation changes. This is a
264 * bulk *provisioning* change. */
265 unprov_pcore_list(&p->ksched_data.prov_alloc_me);
266 unprov_pcore_list(&p->ksched_data.prov_not_alloc_me);
267 /* Remove from whatever list we are on (if any - might not be on one if it
268 * was in the middle of __run_mcp_sched) */
269 remove_from_any_list(p);
271 __put_idle_cores(p, pc_arr, nr_cores);
272 __prov_track_dealloc_bulk(p, pc_arr, nr_cores);
274 spin_unlock(&sched_lock);
275 /* Drop the cradle-to-the-grave reference, jet-li */
279 /* ksched callbacks. p just woke up and is UNLOCKED. */
280 void __sched_mcp_wakeup(struct proc *p)
282 spin_lock(&sched_lock);
283 if (p->state == PROC_DYING) {
284 spin_unlock(&sched_lock);
287 /* could try and prioritize p somehow (move it to the front of the list). */
288 spin_unlock(&sched_lock);
289 /* note they could be dying at this point too. */
290 poke(&ksched_poker, p);
293 /* ksched callbacks. p just woke up and is UNLOCKED. */
294 void __sched_scp_wakeup(struct proc *p)
296 spin_lock(&sched_lock);
297 if (p->state == PROC_DYING) {
298 spin_unlock(&sched_lock);
301 /* might not be on a list if it is new. o/w, it should be unrunnable */
302 remove_from_any_list(p);
303 add_to_list(p, &runnable_scps);
304 spin_unlock(&sched_lock);
305 /* we could be on a CG core, and all the mgmt cores could be halted. if we
306 * don't tell one of them about the new proc, they will sleep until the
307 * timer tick goes off. */
308 if (!management_core()) {
309 /* TODO: pick a better core and only send if halted.
311 * FYI, a POKE on x86 might lose a rare race with halt code, since the
312 * poke handler does not abort halts. if this happens, the next timer
313 * IRQ would wake up the core.
315 * ideally, we'd know if a specific mgmt core is sleeping and wake it
316 * up. o/w, we could interrupt an already-running mgmt core that won't
317 * get to our new proc anytime soon. also, by poking core 0, a
318 * different mgmt core could remain idle (and this process would sleep)
319 * until its tick goes off */
320 send_ipi(0, I_POKE_CORE);
324 /* Callback to return a core to the ksched, which tracks it as idle and
325 * deallocated from p. The proclock is held (__core_req depends on that).
327 * This also is a trigger, telling us we have more cores. We could/should make
328 * a scheduling decision (or at least plan to). */
329 void __sched_put_idle_core(struct proc *p, uint32_t coreid)
331 struct sched_pcore *spc = pcoreid2spc(coreid);
332 spin_lock(&sched_lock);
333 TAILQ_INSERT_TAIL(&idlecores, spc, alloc_next);
334 __prov_track_dealloc(p, coreid);
335 spin_unlock(&sched_lock);
338 /* Helper for put_idle and core_req. Note this does not track_dealloc. When we
339 * get rid of / revise proc_preempt_all and put_idle_cores, we can get rid of
340 * this. (the ksched will never need it - only external callers). */
341 static void __put_idle_cores(struct proc *p, uint32_t *pc_arr, uint32_t num)
343 struct sched_pcore *spc_i;
344 for (int i = 0; i < num; i++) {
345 spc_i = pcoreid2spc(pc_arr[i]);
346 TAILQ_INSERT_TAIL(&idlecores, spc_i, alloc_next);
350 /* Callback, bulk interface for put_idle. Note this one also calls track_dealloc,
351 * which the internal version does not. The proclock is held for this. */
352 void __sched_put_idle_cores(struct proc *p, uint32_t *pc_arr, uint32_t num)
354 spin_lock(&sched_lock);
355 /* TODO: when we revise this func, look at __put_idle */
356 __put_idle_cores(p, pc_arr, num);
357 __prov_track_dealloc_bulk(p, pc_arr, num);
358 spin_unlock(&sched_lock);
359 /* could trigger a sched decision here */
362 /* mgmt/LL cores should call this to schedule the calling core and give it to an
363 * SCP. will also prune the dead SCPs from the list. hold the lock before
364 * calling. returns TRUE if it scheduled a proc. */
365 static bool __schedule_scp(void)
367 // TODO: sort out lock ordering (proc_run_s also locks)
369 uint32_t pcoreid = core_id();
370 struct per_cpu_info *pcpui = &per_cpu_info[pcoreid];
372 /* if there are any runnables, run them here and put any currently running
373 * SCP on the tail of the runnable queue. */
374 if ((p = TAILQ_FIRST(&runnable_scps))) {
375 /* protect owning proc, cur_ctx, etc. note this nests with the
376 * calls in proc_yield_s */
377 disable_irqsave(&state);
378 /* someone is currently running, dequeue them */
379 if (pcpui->owning_proc) {
380 spin_lock(&pcpui->owning_proc->proc_lock);
381 /* process might be dying, with a KMSG to clean it up waiting on
382 * this core. can't do much, so we'll attempt to restart */
383 if (pcpui->owning_proc->state == PROC_DYING) {
384 send_kernel_message(core_id(), __just_sched, 0, 0, 0,
386 spin_unlock(&pcpui->owning_proc->proc_lock);
387 enable_irqsave(&state);
390 printd("Descheduled %d in favor of %d\n", pcpui->owning_proc->pid,
392 __proc_set_state(pcpui->owning_proc, PROC_RUNNABLE_S);
393 /* Saving FP state aggressively. Odds are, the SCP was hit by an
394 * IRQ and has a HW ctx, in which case we must save. */
395 __proc_save_fpu_s(pcpui->owning_proc);
396 __proc_save_context_s(pcpui->owning_proc, pcpui->cur_ctx);
397 vcore_account_offline(pcpui->owning_proc, 0); /* VC# */
398 spin_unlock(&pcpui->owning_proc->proc_lock);
399 /* round-robin the SCPs (inserts at the end of the queue) */
400 switch_lists(pcpui->owning_proc, &unrunnable_scps, &runnable_scps);
401 clear_owning_proc(pcoreid);
402 /* Note we abandon core. It's not strictly necessary. If
403 * we didn't, the TLB would still be loaded with the old
404 * one, til we proc_run_s, and the various paths in
405 * proc_run_s would pick it up. This way is a bit safer for
406 * future changes, but has an extra (empty) TLB flush. */
409 /* Run the new proc */
410 switch_lists(p, &runnable_scps, &unrunnable_scps);
411 printd("PID of the SCP i'm running: %d\n", p->pid);
412 proc_run_s(p); /* gives it core we're running on */
413 enable_irqsave(&state);
419 /* Returns how many new cores p needs. This doesn't lock the proc, so your
420 * answer might be stale. */
421 static uint32_t get_cores_needed(struct proc *p)
423 uint32_t amt_wanted, amt_granted;
424 amt_wanted = p->procdata->res_req[RES_CORES].amt_wanted;
425 /* Help them out - if they ask for something impossible, give them 1 so they
426 * can make some progress. (this is racy, and unnecessary). */
427 if (amt_wanted > p->procinfo->max_vcores) {
428 printk("[kernel] proc %d wanted more than max, wanted %d\n", p->pid,
430 p->procdata->res_req[RES_CORES].amt_wanted = 1;
433 /* There are a few cases where amt_wanted is 0, but they are still RUNNABLE
434 * (involving yields, events, and preemptions). In these cases, give them
435 * at least 1, so they can make progress and yield properly. If they are
436 * not WAITING, they did not yield and may have missed a message. */
438 /* could ++, but there could be a race and we don't want to give them
439 * more than they ever asked for (in case they haven't prepped) */
440 p->procdata->res_req[RES_CORES].amt_wanted = 1;
443 /* amt_granted is racy - they could be *yielding*, but currently they can't
444 * be getting any new cores if the caller is in the mcp_ksched. this is
445 * okay - we won't accidentally give them more cores than they *ever* wanted
446 * (which could crash them), but our answer might be a little stale. */
447 amt_granted = p->procinfo->res_grant[RES_CORES];
448 /* Do not do an assert like this: it could fail (yield in progress): */
449 //assert(amt_granted == p->procinfo->num_vcores);
450 if (amt_wanted <= amt_granted)
452 return amt_wanted - amt_granted;
455 /* Actual work of the MCP kscheduler. if we were called by poke_ksched, *arg
456 * might be the process who wanted special service. this would be the case if
457 * we weren't already running the ksched. Sort of a ghetto way to "post work",
458 * such that it's an optimization. */
459 static void __run_mcp_ksched(void *arg)
461 struct proc *p, *temp;
463 struct proc_list *temp_mcp_list;
464 /* locking to protect the MCP lists' integrity and membership */
465 spin_lock(&sched_lock);
466 /* 2-pass scheme: check each proc on the primary list (FCFS). if they need
467 * nothing, put them on the secondary list. if they need something, rip
468 * them off the list, service them, and if they are still not dying, put
469 * them on the secondary list. We cull the entire primary list, so that
470 * when we start from the beginning each time, we aren't repeatedly checking
471 * procs we looked at on previous waves.
473 * TODO: we could modify this such that procs that we failed to service move
474 * to yet another list or something. We can also move the WAITINGs to
475 * another list and have wakeup move them back, etc. */
476 while (!TAILQ_EMPTY(primary_mcps)) {
477 TAILQ_FOREACH_SAFE(p, primary_mcps, ksched_data.proc_link, temp) {
478 if (p->state == PROC_WAITING) { /* unlocked peek at the state */
479 switch_lists(p, primary_mcps, secondary_mcps);
482 amt_needed = get_cores_needed(p);
484 switch_lists(p, primary_mcps, secondary_mcps);
487 /* o/w, we want to give cores to this proc */
488 remove_from_list(p, primary_mcps);
489 /* now it won't die, but it could get removed from lists and have
490 * its stuff unprov'd when we unlock */
492 /* GIANT WARNING: __core_req will unlock the sched lock for a bit.
493 * It will return with it locked still. We could unlock before we
494 * pass in, but they will relock right away. */
495 // notionally_unlock(&ksched_lock); /* for mouse-eyed viewers */
496 __core_request(p, amt_needed);
497 // notionally_lock(&ksched_lock);
498 /* Peeking at the state is okay, since we hold a ref. Once it is
499 * DYING, it'll remain DYING until we decref. And if there is a
500 * concurrent death, that will spin on the ksched lock (which we
501 * hold, and which protects the proc lists). */
502 if (p->state != PROC_DYING)
503 add_to_list(p, secondary_mcps);
504 proc_decref(p); /* fyi, this may trigger __proc_free */
505 /* need to break: the proc lists may have changed when we unlocked
506 * in core_req in ways that the FOREACH_SAFE can't handle. */
510 /* at this point, we moved all the procs over to the secondary list, and
511 * attempted to service the ones that wanted something. now just swap the
512 * lists for the next invocation of the ksched. */
513 temp_mcp_list = primary_mcps;
514 primary_mcps = secondary_mcps;
515 secondary_mcps = temp_mcp_list;
516 spin_unlock(&sched_lock);
519 /* Something has changed, and for whatever reason the scheduler should
522 * Don't call this if you are processing a syscall or otherwise care about your
523 * kthread variables, cur_proc/owning_proc, etc.
525 * Don't call this from interrupt context (grabs proclocks). */
526 void run_scheduler(void)
528 /* MCP scheduling: post work, then poke. for now, i just want the func to
529 * run again, so merely a poke is sufficient. */
530 poke(&ksched_poker, 0);
531 if (management_core()) {
532 spin_lock(&sched_lock);
534 spin_unlock(&sched_lock);
538 /* A process is asking the ksched to look at its resource desires. The
539 * scheduler is free to ignore this, for its own reasons, so long as it
540 * eventually gets around to looking at resource desires. */
541 void poke_ksched(struct proc *p, unsigned int res_type)
543 /* ignoring res_type for now. could post that if we wanted (would need some
544 * other structs/flags) */
545 if (!__proc_is_mcp(p))
547 poke(&ksched_poker, p);
550 /* The calling cpu/core has nothing to do and plans to idle/halt. This is an
551 * opportunity to pick the nature of that halting (low power state, etc), or
552 * provide some other work (_Ss on LL cores). Note that interrupts are
553 * disabled, and if you return, the core will cpu_halt(). */
556 bool new_proc = FALSE;
557 if (!management_core())
559 spin_lock(&sched_lock);
560 new_proc = __schedule_scp();
561 spin_unlock(&sched_lock);
562 /* if we just scheduled a proc, we need to manually restart it, instead of
563 * returning. if we return, the core will halt. */
568 /* Could drop into the monitor if there are no processes at all. For now,
569 * the 'call of the giraffe' suffices. */
572 /* Available resources changed (plus or minus). Some parts of the kernel may
573 * call this if a particular resource that is 'quantity-based' changes. Things
574 * like available RAM to processes, bandwidth, etc. Cores would probably be
575 * inappropriate, since we need to know which specific core is now free. */
576 void avail_res_changed(int res_type, long change)
578 printk("[kernel] ksched doesn't track any resources yet!\n");
581 int get_any_idle_core(void)
583 struct sched_pcore *spc;
585 spin_lock(&sched_lock);
586 while ((spc = TAILQ_FIRST(&idlecores))) {
587 /* Don't take cores that are provisioned to a process */
590 assert(!spc->alloc_proc);
591 TAILQ_REMOVE(&idlecores, spc, alloc_next);
592 ret = spc2pcoreid(spc);
595 spin_unlock(&sched_lock);
599 /* TODO: if we end up using this a lot, track CG-idleness as a property of the
600 * SPC instead of doing a linear search. */
601 static bool __spc_is_idle(struct sched_pcore *spc)
603 struct sched_pcore *i;
604 TAILQ_FOREACH(i, &idlecores, alloc_next) {
611 int get_this_idle_core(int coreid)
613 struct sched_pcore *spc = pcoreid2spc(coreid);
615 assert((0 <= coreid) && (coreid < num_cpus));
616 spin_lock(&sched_lock);
617 if (__spc_is_idle(pcoreid2spc(coreid)) && !spc->prov_proc) {
618 assert(!spc->alloc_proc);
619 TAILQ_REMOVE(&idlecores, spc, alloc_next);
622 spin_unlock(&sched_lock);
626 /* similar to __sched_put_idle_core, but without the prov tracking */
627 void put_idle_core(int coreid)
629 struct sched_pcore *spc = pcoreid2spc(coreid);
630 assert((0 <= coreid) && (coreid < num_cpus));
631 spin_lock(&sched_lock);
632 TAILQ_INSERT_TAIL(&idlecores, spc, alloc_next);
633 spin_unlock(&sched_lock);
636 /* Normally it'll be the max number of CG cores ever */
637 uint32_t max_vcores(struct proc *p)
640 #ifdef CONFIG_DISABLE_SMT
641 return num_cpus >> 1;
643 return num_cpus - 1; /* reserving core 0 */
644 #endif /* CONFIG_DISABLE_SMT */
647 /* This deals with a request for more cores. The amt of new cores needed is
648 * passed in. The ksched lock is held, but we are free to unlock if we want
649 * (and we must, if calling out of the ksched to anything high-level).
651 * Side note: if we want to warn, then we can't deal with this proc's prov'd
652 * cores until we wait til the alarm goes off. would need to put all
653 * alarmed cores on a list and wait til the alarm goes off to do the full
654 * preempt. and when those cores come in voluntarily, we'd need to know to
655 * give them to this proc. */
656 static void __core_request(struct proc *p, uint32_t amt_needed)
658 uint32_t nr_to_grant = 0;
659 uint32_t corelist[num_cpus];
660 struct sched_pcore *spc_i, *temp;
661 struct proc *proc_to_preempt;
663 /* we come in holding the ksched lock, and we hold it here to protect
664 * allocations and provisioning. */
665 /* get all available cores from their prov_not_alloc list. the list might
666 * change when we unlock (new cores added to it, or the entire list emptied,
667 * but no core allocations will happen (we hold the poke)). */
668 while (!TAILQ_EMPTY(&p->ksched_data.prov_not_alloc_me)) {
669 if (nr_to_grant == amt_needed)
671 /* picking the next victim (first on the not_alloc list) */
672 spc_i = TAILQ_FIRST(&p->ksched_data.prov_not_alloc_me);
673 /* someone else has this proc's pcore, so we need to try to preempt.
674 * after this block, the core will be tracked dealloc'd and on the idle
675 * list (regardless of whether we had to preempt or not) */
676 if (spc_i->alloc_proc) {
677 proc_to_preempt = spc_i->alloc_proc;
678 /* would break both preemption and maybe the later decref */
679 assert(proc_to_preempt != p);
680 /* need to keep a valid, external ref when we unlock */
681 proc_incref(proc_to_preempt, 1);
682 spin_unlock(&sched_lock);
683 /* sending no warning time for now - just an immediate preempt. */
684 success = proc_preempt_core(proc_to_preempt, spc2pcoreid(spc_i), 0);
685 /* reaquire locks to protect provisioning and idle lists */
686 spin_lock(&sched_lock);
688 /* we preempted it before the proc could yield or die.
689 * alloc_proc should not have changed (it'll change in death and
690 * idle CBs). the core is not on the idle core list. (if we
691 * ever have proc alloc lists, it'll still be on the old proc's
693 assert(spc_i->alloc_proc);
694 /* regardless of whether or not it is still prov to p, we need
695 * to note its dealloc. we are doing some excessive checking of
696 * p == prov_proc, but using this helper is a lot clearer. */
697 __prov_track_dealloc(proc_to_preempt, spc2pcoreid(spc_i));
698 /* here, we rely on the fact that we are the only preemptor. we
699 * assume no one else preempted it, so we know it is available*/
700 TAILQ_INSERT_TAIL(&idlecores, spc_i, alloc_next);
702 /* the preempt failed, which should only happen if the pcore was
703 * unmapped (could be dying, could be yielding, but NOT
704 * preempted). whoever unmapped it also triggered (or will soon
705 * trigger) a track_dealloc and put it on the idle list. our
706 * signal for this is spc_i->alloc_proc being 0. We need to
707 * spin and let whoever is trying to free the core grab the
708 * ksched lock. We could use an 'ignore_next_idle' flag per
709 * sched_pcore, but it's not critical anymore.
711 * Note, we're relying on us being the only preemptor - if the
712 * core was unmapped by *another* preemptor, there would be no
713 * way of knowing the core was made idle *yet* (the success
714 * branch in another thread). likewise, if there were another
715 * allocator, the pcore could have been put on the idle list and
716 * then quickly removed/allocated. */
718 while (spc_i->alloc_proc) {
719 /* this loop should be very rare */
720 spin_unlock(&sched_lock);
722 spin_lock(&sched_lock);
725 /* no longer need to keep p_to_pre alive */
726 proc_decref(proc_to_preempt);
727 /* might not be prov to p anymore (rare race). spc_i is idle - we
728 * might get it later, or maybe we'll give it to its rightful proc*/
729 if (spc_i->prov_proc != p)
732 /* at this point, the pcore is idle, regardless of how we got here
733 * (successful preempt, failed preempt, or it was idle in the first
734 * place. the core is still provisioned. lets pull from the idle list
735 * and add it to the pc_arr for p. here, we rely on the fact that we
736 * are the only allocator (spc_i is still idle, despite unlocking). */
737 TAILQ_REMOVE(&idlecores, spc_i, alloc_next);
738 /* At this point, we have the core, ready to try to give it to the proc.
739 * It is on no alloc lists, and is track_dealloc'd() (regardless of how
742 * We'll give p its cores via a bulk list, which is better for the proc
743 * mgmt code (when going from runnable to running). */
744 corelist[nr_to_grant] = spc2pcoreid(spc_i);
746 __prov_track_alloc(p, spc2pcoreid(spc_i));
748 /* Try to get cores from the idle list that aren't prov to me (FCFS) */
749 TAILQ_FOREACH_SAFE(spc_i, &idlecores, alloc_next, temp) {
750 if (nr_to_grant == amt_needed)
752 TAILQ_REMOVE(&idlecores, spc_i, alloc_next);
753 corelist[nr_to_grant] = spc2pcoreid(spc_i);
755 __prov_track_alloc(p, spc2pcoreid(spc_i));
757 /* Now, actually give them out */
759 /* Need to unlock before calling out to proc code. We are somewhat
760 * relying on being the only one allocating 'thread' here, since another
761 * allocator could have seen these cores (if they are prov to some proc)
762 * and could be trying to give them out (and assuming they are already
763 * on the idle list). */
764 spin_unlock(&sched_lock);
765 /* give them the cores. this will start up the extras if RUNNING_M. */
766 spin_lock(&p->proc_lock);
767 /* if they fail, it is because they are WAITING or DYING. we could give
768 * the cores to another proc or whatever. for the current type of
769 * ksched, we'll just put them back on the pile and return. Note, the
770 * ksched could check the states after locking, but it isn't necessary:
771 * just need to check at some point in the ksched loop. */
772 if (__proc_give_cores(p, corelist, nr_to_grant)) {
773 spin_unlock(&p->proc_lock);
774 /* we failed, put the cores and track their dealloc. lock is
775 * protecting those structures. */
776 spin_lock(&sched_lock);
777 __put_idle_cores(p, corelist, nr_to_grant);
778 __prov_track_dealloc_bulk(p, corelist, nr_to_grant);
780 /* at some point after giving cores, call proc_run_m() (harmless on
781 * RUNNING_Ms). You can give small groups of cores, then run them
782 * (which is more efficient than interleaving runs with the gives
783 * for bulk preempted processes). */
785 spin_unlock(&p->proc_lock);
786 /* main mcp_ksched wants this held (it came to __core_req held) */
787 spin_lock(&sched_lock);
790 /* note the ksched lock is still held */
793 /* TODO: need more thorough CG/LL management. For now, core0 is the only LL
794 * core. This won't play well with the ghetto shit in schedule_init() if you do
795 * anything like 'DEDICATED_MONITOR' or the ARSC server. All that needs an
797 static bool is_ll_core(uint32_t pcoreid)
804 /* Helper, makes sure the prov/alloc structures track the pcore properly when it
805 * is allocated to p. Might make this take a sched_pcore * in the future. */
806 static void __prov_track_alloc(struct proc *p, uint32_t pcoreid)
808 struct sched_pcore *spc;
809 assert(pcoreid < num_cpus); /* catch bugs */
810 spc = pcoreid2spc(pcoreid);
811 assert(spc->alloc_proc != p); /* corruption or double-alloc */
813 /* if the pcore is prov to them and now allocated, move lists */
814 if (spc->prov_proc == p) {
815 TAILQ_REMOVE(&p->ksched_data.prov_not_alloc_me, spc, prov_next);
816 TAILQ_INSERT_TAIL(&p->ksched_data.prov_alloc_me, spc, prov_next);
820 /* Helper, makes sure the prov/alloc structures track the pcore properly when it
821 * is deallocated from p. */
822 static void __prov_track_dealloc(struct proc *p, uint32_t pcoreid)
824 struct sched_pcore *spc;
825 assert(pcoreid < num_cpus); /* catch bugs */
826 spc = pcoreid2spc(pcoreid);
828 /* if the pcore is prov to them and now deallocated, move lists */
829 if (spc->prov_proc == p) {
830 TAILQ_REMOVE(&p->ksched_data.prov_alloc_me, spc, prov_next);
831 /* this is the victim list, which can be sorted so that we pick the
832 * right victim (sort by alloc_proc reverse priority, etc). In this
833 * case, the core isn't alloc'd by anyone, so it should be the first
835 TAILQ_INSERT_HEAD(&p->ksched_data.prov_not_alloc_me, spc, prov_next);
839 /* Bulk interface for __prov_track_dealloc */
840 static void __prov_track_dealloc_bulk(struct proc *p, uint32_t *pc_arr,
843 for (int i = 0; i < nr_cores; i++)
844 __prov_track_dealloc(p, pc_arr[i]);
847 /* P will get pcore if it needs more cores next time we look at it */
848 int provision_core(struct proc *p, uint32_t pcoreid)
850 struct sched_pcore *spc;
851 struct sched_pcore_tailq *prov_list;
852 /* Make sure we aren't asking for something that doesn't exist (bounds check
853 * on the pcore array) */
854 if (!(pcoreid < num_cpus)) {
858 /* Don't allow the provisioning of LL cores */
859 if (is_ll_core(pcoreid)) {
863 spc = pcoreid2spc(pcoreid);
864 /* Note the sched lock protects the spc tailqs for all procs in this code.
865 * If we need a finer grained sched lock, this is one place where we could
866 * have a different lock */
867 spin_lock(&sched_lock);
868 /* If the core is already prov to someone else, take it away. (last write
869 * wins, some other layer or new func can handle permissions). */
870 if (spc->prov_proc) {
871 /* the list the spc is on depends on whether it is alloced to the
872 * prov_proc or not */
873 prov_list = (spc->alloc_proc == spc->prov_proc ?
874 &spc->prov_proc->ksched_data.prov_alloc_me :
875 &spc->prov_proc->ksched_data.prov_not_alloc_me);
876 TAILQ_REMOVE(prov_list, spc, prov_next);
878 /* Now prov it to p. Again, the list it goes on depends on whether it is
879 * alloced to p or not. Callers can also send in 0 to de-provision. */
881 if (spc->alloc_proc == p) {
882 TAILQ_INSERT_TAIL(&p->ksched_data.prov_alloc_me, spc, prov_next);
884 /* this is be the victim list, which can be sorted so that we pick
885 * the right victim (sort by alloc_proc reverse priority, etc). */
886 TAILQ_INSERT_TAIL(&p->ksched_data.prov_not_alloc_me, spc,
891 spin_unlock(&sched_lock);
895 /************** Debugging **************/
896 void sched_diag(void)
899 spin_lock(&sched_lock);
900 TAILQ_FOREACH(p, &runnable_scps, ksched_data.proc_link)
901 printk("Runnable _S PID: %d\n", p->pid);
902 TAILQ_FOREACH(p, &unrunnable_scps, ksched_data.proc_link)
903 printk("Unrunnable _S PID: %d\n", p->pid);
904 TAILQ_FOREACH(p, primary_mcps, ksched_data.proc_link)
905 printk("Primary MCP PID: %d\n", p->pid);
906 TAILQ_FOREACH(p, secondary_mcps, ksched_data.proc_link)
907 printk("Secondary MCP PID: %d\n", p->pid);
908 spin_unlock(&sched_lock);
912 void print_idlecoremap(void)
914 struct sched_pcore *spc_i;
915 /* not locking, so we can look at this without deadlocking. */
916 printk("Idle cores (unlocked!):\n");
917 TAILQ_FOREACH(spc_i, &idlecores, alloc_next)
918 printk("Core %d, prov to %d (%p)\n", spc2pcoreid(spc_i),
919 spc_i->prov_proc ? spc_i->prov_proc->pid : 0, spc_i->prov_proc);
922 void print_resources(struct proc *p)
924 printk("--------------------\n");
925 printk("PID: %d\n", p->pid);
926 printk("--------------------\n");
927 for (int i = 0; i < MAX_NUM_RESOURCES; i++)
928 printk("Res type: %02d, amt wanted: %08d, amt granted: %08d\n", i,
929 p->procdata->res_req[i].amt_wanted, p->procinfo->res_grant[i]);
932 void print_all_resources(void)
935 void __print_resources(void *item)
937 print_resources((struct proc*)item);
939 spin_lock(&pid_hash_lock);
940 hash_for_each(pid_hash, __print_resources);
941 spin_unlock(&pid_hash_lock);
944 void print_prov_map(void)
946 struct sched_pcore *spc_i;
947 /* Doing this unlocked, which is dangerous, but won't deadlock */
948 printk("Which cores are provisioned to which procs:\n------------------\n");
949 for (int i = 0; i < num_cpus; i++) {
950 spc_i = pcoreid2spc(i);
951 printk("Core %02d, prov: %d(%p) alloc: %d(%p)\n", i,
952 spc_i->prov_proc ? spc_i->prov_proc->pid : 0, spc_i->prov_proc,
953 spc_i->alloc_proc ? spc_i->alloc_proc->pid : 0,
958 void print_proc_prov(struct proc *p)
960 struct sched_pcore *spc_i;
963 printk("Prov cores alloced to proc %d (%p)\n----------\n", p->pid, p);
964 TAILQ_FOREACH(spc_i, &p->ksched_data.prov_alloc_me, prov_next)
965 printk("Pcore %d\n", spc2pcoreid(spc_i));
966 printk("Prov cores not alloced to proc %d (%p)\n----------\n", p->pid, p);
967 TAILQ_FOREACH(spc_i, &p->ksched_data.prov_not_alloc_me, prov_next)
968 printk("Pcore %d (alloced to %d (%p))\n", spc2pcoreid(spc_i),
969 spc_i->alloc_proc ? spc_i->alloc_proc->pid : 0,
973 void next_core(uint32_t pcoreid)
975 struct sched_pcore *spc_i;
977 spin_lock(&sched_lock);
978 TAILQ_FOREACH(spc_i, &idlecores, alloc_next) {
979 if (spc2pcoreid(spc_i) == pcoreid) {
985 TAILQ_REMOVE(&idlecores, spc_i, alloc_next);
986 TAILQ_INSERT_HEAD(&idlecores, spc_i, alloc_next);
987 printk("Pcore %d will be given out next (from the idles)\n", pcoreid);
989 spin_unlock(&sched_lock);
992 void sort_idles(void)
994 struct sched_pcore *spc_i, *spc_j, *temp;
995 struct sched_pcore_tailq sorter = TAILQ_HEAD_INITIALIZER(sorter);
997 spin_lock(&sched_lock);
998 TAILQ_CONCAT(&sorter, &idlecores, alloc_next);
999 TAILQ_FOREACH_SAFE(spc_i, &sorter, alloc_next, temp) {
1000 TAILQ_REMOVE(&sorter, spc_i, alloc_next);
1002 /* don't need foreach_safe since we break after we muck with the list */
1003 TAILQ_FOREACH(spc_j, &idlecores, alloc_next) {
1004 if (spc_i < spc_j) {
1005 TAILQ_INSERT_BEFORE(spc_j, spc_i, alloc_next);
1011 TAILQ_INSERT_TAIL(&idlecores, spc_i, alloc_next);
1013 spin_unlock(&sched_lock);