1 /* Copyright (c) 2009, 2012 The Regents of the University of California
2 * Barret Rhoden <brho@cs.berkeley.edu>
3 * See LICENSE for details.
5 * Scheduling and dispatching. */
8 #include <corerequest.h>
17 #include <sys/queue.h>
19 #include <arsc_server.h>
21 /* Process Lists. 'unrunnable' is a holding list for SCPs that are running or
22 * waiting or otherwise not considered for sched decisions. */
23 struct proc_list unrunnable_scps = TAILQ_HEAD_INITIALIZER(unrunnable_scps);
24 struct proc_list runnable_scps = TAILQ_HEAD_INITIALIZER(runnable_scps);
25 /* mcp lists. we actually could get by with one list and a TAILQ_CONCAT, but
26 * I'm expecting to want the flexibility of the pointers later. */
27 struct proc_list all_mcps_1 = TAILQ_HEAD_INITIALIZER(all_mcps_1);
28 struct proc_list all_mcps_2 = TAILQ_HEAD_INITIALIZER(all_mcps_2);
29 struct proc_list *primary_mcps = &all_mcps_1;
30 struct proc_list *secondary_mcps = &all_mcps_2;
32 /* The pcores in the system. (array gets alloced in init()). */
33 struct sched_pcore *all_pcores;
35 /* TAILQ of all unallocated, idle (CG) cores */
36 struct sched_pcore_tailq idlecores = TAILQ_HEAD_INITIALIZER(idlecores);
38 /* Helper, defined below */
39 static void __core_request(struct proc *p, uint32_t amt_needed);
40 static void add_to_list(struct proc *p, struct proc_list *list);
41 static void remove_from_list(struct proc *p, struct proc_list *list);
42 static void switch_lists(struct proc *p, struct proc_list *old,
43 struct proc_list *new);
44 static uint32_t spc2pcoreid(struct sched_pcore *spc);
45 static struct sched_pcore *pcoreid2spc(uint32_t pcoreid);
46 static bool is_ll_core(uint32_t pcoreid);
47 static void __prov_track_alloc(struct proc *p, uint32_t pcoreid);
48 static void __prov_track_dealloc(struct proc *p, uint32_t pcoreid);
49 static void __prov_track_dealloc_bulk(struct proc *p, uint32_t *pc_arr,
51 static void __run_mcp_ksched(void *arg); /* don't call directly */
52 static uint32_t get_cores_needed(struct proc *p);
54 /* Locks / sync tools */
56 /* poke-style ksched - ensures the MCP ksched only runs once at a time. since
57 * only one mcp ksched runs at a time, while this is set, the ksched knows no
58 * cores are being allocated by other code (though they could be dealloc, due to
61 * The main value to this sync method is to make the 'make sure the ksched runs
62 * only once at a time and that it actually runs' invariant/desire wait-free, so
63 * that it can be called anywhere (deep event code, etc).
65 * As the ksched gets smarter, we'll probably embedd this poker in a bigger
66 * struct that can handle the posting of different types of work. */
67 struct poke_tracker ksched_poker = POKE_INITIALIZER(__run_mcp_ksched);
69 /* this 'big ksched lock' protects a bunch of things, which i may make fine
71 /* - protects the integrity of proc tailqs/structures, as well as the membership
72 * of a proc on those lists. proc lifetime within the ksched but outside this
73 * lock is protected by the proc kref. */
74 //spinlock_t proclist_lock = SPINLOCK_INITIALIZER; /* subsumed by bksl */
75 /* - protects the provisioning assignment, membership of sched_pcores in
76 * provision lists, and the integrity of all prov lists (the lists of each
77 * proc). does NOT protect spc->alloc_proc. */
78 //spinlock_t prov_lock = SPINLOCK_INITIALIZER;
79 /* - protects allocation structures: spc->alloc_proc, the integrity and
80 * membership of the idelcores tailq. */
81 //spinlock_t alloc_lock = SPINLOCK_INITIALIZER;
82 spinlock_t sched_lock = SPINLOCK_INITIALIZER;
84 /* Alarm struct, for our example 'timer tick' */
85 struct alarm_waiter ksched_waiter;
87 #define TIMER_TICK_USEC 10000 /* 10msec */
89 /* Helper: Sets up a timer tick on the calling core to go off 10 msec from now.
90 * This assumes the calling core is an LL core, etc. */
91 static void set_ksched_alarm(void)
93 set_awaiter_rel(&ksched_waiter, TIMER_TICK_USEC);
94 set_alarm(&per_cpu_info[core_id()].tchain, &ksched_waiter);
97 /* Need a kmsg to just run the sched, but not to rearm */
98 static void __just_sched(uint32_t srcid, long a0, long a1, long a2)
103 /* RKM alarm, to run the scheduler tick (not in interrupt context) and reset the
104 * alarm. Note that interrupts will be disabled, but this is not the same as
105 * interrupt context. We're a routine kmsg, which means the core is in a
106 * quiescent state. */
107 static void __ksched_tick(struct alarm_waiter *waiter)
109 /* TODO: imagine doing some accounting here */
111 /* Set our alarm to go off, incrementing from our last tick (instead of
112 * setting it relative to now, since some time has passed since the alarm
113 * first went off. Note, this may be now or in the past! */
114 set_awaiter_inc(&ksched_waiter, TIMER_TICK_USEC);
115 set_alarm(&per_cpu_info[core_id()].tchain, &ksched_waiter);
118 void schedule_init(void)
120 spin_lock(&sched_lock);
121 /* init provisioning stuff */
122 all_pcores = kmalloc(sizeof(struct sched_pcore) * num_cores, 0);
123 memset(all_pcores, 0, sizeof(struct sched_pcore) * num_cores);
124 assert(!core_id()); /* want the alarm on core0 for now */
125 init_awaiter(&ksched_waiter, __ksched_tick);
127 /* init the idlecore list. if they turned off hyperthreading, give them the
128 * odds from 1..max-1. otherwise, give them everything by 0 (default mgmt
129 * core). TODO: (CG/LL) better LL/CG mgmt */
130 #ifndef CONFIG_DISABLE_SMT
131 for (int i = 1; i < num_cores; i++)
132 TAILQ_INSERT_TAIL(&idlecores, pcoreid2spc(i), alloc_next);
134 assert(!(num_cores % 2));
135 for (int i = 1; i < num_cores; i += 2)
136 TAILQ_INSERT_TAIL(&idlecores, pcoreid2spc(i), alloc_next);
137 #endif /* CONFIG_DISABLE_SMT */
138 spin_unlock(&sched_lock);
140 #ifdef CONFIG_ARSC_SERVER
141 int arsc_coreid = get_any_idle_core();
142 assert(arsc_coreid >= 0);
143 send_kernel_message(arsc_coreid, arsc_server, 0, 0, 0, KMSG_ROUTINE);
144 printk("Using core %d for the ARSC server\n", arsc_coreid);
145 #endif /* CONFIG_ARSC_SERVER */
148 static uint32_t spc2pcoreid(struct sched_pcore *spc)
150 return spc - all_pcores;
153 static struct sched_pcore *pcoreid2spc(uint32_t pcoreid)
155 return &all_pcores[pcoreid];
158 /* Round-robins on whatever list it's on */
159 static void add_to_list(struct proc *p, struct proc_list *new)
161 assert(!(p->ksched_data.cur_list));
162 TAILQ_INSERT_TAIL(new, p, ksched_data.proc_link);
163 p->ksched_data.cur_list = new;
166 static void remove_from_list(struct proc *p, struct proc_list *old)
168 assert(p->ksched_data.cur_list == old);
169 TAILQ_REMOVE(old, p, ksched_data.proc_link);
170 p->ksched_data.cur_list = 0;
173 static void switch_lists(struct proc *p, struct proc_list *old,
174 struct proc_list *new)
176 remove_from_list(p, old);
180 /* Removes from whatever list p is on */
181 static void remove_from_any_list(struct proc *p)
183 if (p->ksched_data.cur_list) {
184 TAILQ_REMOVE(p->ksched_data.cur_list, p, ksched_data.proc_link);
185 p->ksched_data.cur_list = 0;
189 /************** Process Management Callbacks **************/
191 * - the proc lock is NOT held for any of these calls. currently, there is no
192 * lock ordering between the sched lock and the proc lock. since the proc
193 * code doesn't know what we do, it doesn't hold its lock when calling our
195 * - since the proc lock isn't held, the proc could be dying, which means we
196 * will receive a __sched_proc_destroy() either before or after some of these
197 * other CBs. the CBs related to list management need to check and abort if
199 void __sched_proc_register(struct proc *p)
201 assert(p->state != PROC_DYING); /* shouldn't be abel to happen yet */
202 /* one ref for the proc's existence, cradle-to-grave */
203 proc_incref(p, 1); /* need at least this OR the 'one for existing' */
204 spin_lock(&sched_lock);
205 TAILQ_INIT(&p->ksched_data.crd.prov_alloc_me);
206 TAILQ_INIT(&p->ksched_data.crd.prov_not_alloc_me);
207 add_to_list(p, &unrunnable_scps);
208 spin_unlock(&sched_lock);
211 /* Returns 0 if it succeeded, an error code otherwise. */
212 void __sched_proc_change_to_m(struct proc *p)
214 spin_lock(&sched_lock);
215 /* Need to make sure they aren't dying. if so, we already dealt with their
216 * list membership, etc (or soon will). taking advantage of the 'immutable
217 * state' of dying (so long as refs are held). */
218 if (p->state == PROC_DYING) {
219 spin_unlock(&sched_lock);
222 /* Catch user bugs */
223 if (!p->procdata->res_req[RES_CORES].amt_wanted) {
224 printk("[kernel] process needs to specify amt_wanted\n");
225 p->procdata->res_req[RES_CORES].amt_wanted = 1;
227 /* For now, this should only ever be called on an unrunnable. It's
228 * probably a bug, at this stage in development, to do o/w. */
229 remove_from_list(p, &unrunnable_scps);
230 //remove_from_any_list(p); /* ^^ instead of this */
231 add_to_list(p, primary_mcps);
232 spin_unlock(&sched_lock);
233 //poke_ksched(p, RES_CORES);
236 /* Helper for the destroy CB : unprovisions any pcores for the given list */
237 static void unprov_pcore_list(struct sched_pcore_tailq *list_head)
239 struct sched_pcore *spc_i;
240 /* We can leave them connected within the tailq, since the scps don't have a
241 * default list (if they aren't on a proc's list, then we don't care about
242 * them), and since the INSERTs don't care what list you were on before
243 * (chummy with the implementation). Pretty sure this is right. If there's
244 * suspected list corruption, be safer here. */
245 TAILQ_FOREACH(spc_i, list_head, prov_next)
246 spc_i->prov_proc = 0;
247 TAILQ_INIT(list_head);
250 /* Sched callback called when the proc dies. pc_arr holds the cores the proc
251 * had, if any, and nr_cores tells us how many are in the array.
253 * An external, edible ref is passed in. when we return and they decref,
254 * __proc_free will be called (when the last one is done). */
255 void __sched_proc_destroy(struct proc *p, uint32_t *pc_arr, uint32_t nr_cores)
257 spin_lock(&sched_lock);
258 /* Unprovision any cores. Note this is different than track_dealloc.
259 * The latter does bookkeeping when an allocation changes. This is a
260 * bulk *provisioning* change. */
261 unprov_pcore_list(&p->ksched_data.crd.prov_alloc_me);
262 unprov_pcore_list(&p->ksched_data.crd.prov_not_alloc_me);
263 /* Remove from whatever list we are on (if any - might not be on one if it
264 * was in the middle of __run_mcp_sched) */
265 remove_from_any_list(p);
267 __prov_track_dealloc_bulk(p, pc_arr, nr_cores);
268 spin_unlock(&sched_lock);
269 /* Drop the cradle-to-the-grave reference, jet-li */
273 /* ksched callbacks. p just woke up and is UNLOCKED. */
274 void __sched_mcp_wakeup(struct proc *p)
276 spin_lock(&sched_lock);
277 if (p->state == PROC_DYING) {
278 spin_unlock(&sched_lock);
281 /* could try and prioritize p somehow (move it to the front of the list). */
282 spin_unlock(&sched_lock);
283 /* note they could be dying at this point too. */
284 poke(&ksched_poker, p);
287 /* ksched callbacks. p just woke up and is UNLOCKED. */
288 void __sched_scp_wakeup(struct proc *p)
290 spin_lock(&sched_lock);
291 if (p->state == PROC_DYING) {
292 spin_unlock(&sched_lock);
295 /* might not be on a list if it is new. o/w, it should be unrunnable */
296 remove_from_any_list(p);
297 add_to_list(p, &runnable_scps);
298 spin_unlock(&sched_lock);
299 /* we could be on a CG core, and all the mgmt cores could be halted. if we
300 * don't tell one of them about the new proc, they will sleep until the
301 * timer tick goes off. */
302 if (!management_core()) {
303 /* TODO: pick a better core and only send if halted.
305 * FYI, a POKE on x86 might lose a rare race with halt code, since the
306 * poke handler does not abort halts. if this happens, the next timer
307 * IRQ would wake up the core.
309 * ideally, we'd know if a specific mgmt core is sleeping and wake it
310 * up. o/w, we could interrupt an already-running mgmt core that won't
311 * get to our new proc anytime soon. also, by poking core 0, a
312 * different mgmt core could remain idle (and this process would sleep)
313 * until its tick goes off */
314 send_ipi(0, I_POKE_CORE);
318 /* Callback to return a core to the ksched, which tracks it as idle and
319 * deallocated from p. The proclock is held (__core_req depends on that).
321 * This also is a trigger, telling us we have more cores. We could/should make
322 * a scheduling decision (or at least plan to). */
323 void __sched_put_idle_core(struct proc *p, uint32_t coreid)
325 struct sched_pcore *spc = pcoreid2spc(coreid);
326 spin_lock(&sched_lock);
327 __prov_track_dealloc(p, coreid);
328 spin_unlock(&sched_lock);
331 /* Callback, bulk interface for put_idle. The proclock is held for this. */
332 void __sched_put_idle_cores(struct proc *p, uint32_t *pc_arr, uint32_t num)
334 spin_lock(&sched_lock);
335 __prov_track_dealloc_bulk(p, pc_arr, num);
336 spin_unlock(&sched_lock);
337 /* could trigger a sched decision here */
340 /* mgmt/LL cores should call this to schedule the calling core and give it to an
341 * SCP. will also prune the dead SCPs from the list. hold the lock before
342 * calling. returns TRUE if it scheduled a proc. */
343 static bool __schedule_scp(void)
345 // TODO: sort out lock ordering (proc_run_s also locks)
347 uint32_t pcoreid = core_id();
348 struct per_cpu_info *pcpui = &per_cpu_info[pcoreid];
350 /* if there are any runnables, run them here and put any currently running
351 * SCP on the tail of the runnable queue. */
352 if ((p = TAILQ_FIRST(&runnable_scps))) {
353 /* protect owning proc, cur_ctx, etc. note this nests with the
354 * calls in proc_yield_s */
355 disable_irqsave(&state);
356 /* someone is currently running, dequeue them */
357 if (pcpui->owning_proc) {
358 spin_lock(&pcpui->owning_proc->proc_lock);
359 /* process might be dying, with a KMSG to clean it up waiting on
360 * this core. can't do much, so we'll attempt to restart */
361 if (pcpui->owning_proc->state == PROC_DYING) {
362 send_kernel_message(core_id(), __just_sched, 0, 0, 0,
364 spin_unlock(&pcpui->owning_proc->proc_lock);
365 enable_irqsave(&state);
368 printd("Descheduled %d in favor of %d\n", pcpui->owning_proc->pid,
370 __proc_set_state(pcpui->owning_proc, PROC_RUNNABLE_S);
371 /* Saving FP state aggressively. Odds are, the SCP was hit by an
372 * IRQ and has a HW ctx, in which case we must save. */
373 __proc_save_fpu_s(pcpui->owning_proc);
374 __proc_save_context_s(pcpui->owning_proc, pcpui->cur_ctx);
375 vcore_account_offline(pcpui->owning_proc, 0);
376 __seq_start_write(&p->procinfo->coremap_seqctr);
378 __seq_end_write(&p->procinfo->coremap_seqctr);
379 spin_unlock(&pcpui->owning_proc->proc_lock);
380 /* round-robin the SCPs (inserts at the end of the queue) */
381 switch_lists(pcpui->owning_proc, &unrunnable_scps, &runnable_scps);
382 clear_owning_proc(pcoreid);
383 /* Note we abandon core. It's not strictly necessary. If
384 * we didn't, the TLB would still be loaded with the old
385 * one, til we proc_run_s, and the various paths in
386 * proc_run_s would pick it up. This way is a bit safer for
387 * future changes, but has an extra (empty) TLB flush. */
390 /* Run the new proc */
391 switch_lists(p, &runnable_scps, &unrunnable_scps);
392 printd("PID of the SCP i'm running: %d\n", p->pid);
393 proc_run_s(p); /* gives it core we're running on */
394 enable_irqsave(&state);
400 /* Returns how many new cores p needs. This doesn't lock the proc, so your
401 * answer might be stale. */
402 static uint32_t get_cores_needed(struct proc *p)
404 uint32_t amt_wanted, amt_granted;
405 amt_wanted = p->procdata->res_req[RES_CORES].amt_wanted;
406 /* Help them out - if they ask for something impossible, give them 1 so they
407 * can make some progress. (this is racy, and unnecessary). */
408 if (amt_wanted > p->procinfo->max_vcores) {
409 printk("[kernel] proc %d wanted more than max, wanted %d\n", p->pid,
411 p->procdata->res_req[RES_CORES].amt_wanted = 1;
414 /* There are a few cases where amt_wanted is 0, but they are still RUNNABLE
415 * (involving yields, events, and preemptions). In these cases, give them
416 * at least 1, so they can make progress and yield properly. If they are
417 * not WAITING, they did not yield and may have missed a message. */
419 /* could ++, but there could be a race and we don't want to give them
420 * more than they ever asked for (in case they haven't prepped) */
421 p->procdata->res_req[RES_CORES].amt_wanted = 1;
424 /* amt_granted is racy - they could be *yielding*, but currently they can't
425 * be getting any new cores if the caller is in the mcp_ksched. this is
426 * okay - we won't accidentally give them more cores than they *ever* wanted
427 * (which could crash them), but our answer might be a little stale. */
428 amt_granted = p->procinfo->res_grant[RES_CORES];
429 /* Do not do an assert like this: it could fail (yield in progress): */
430 //assert(amt_granted == p->procinfo->num_vcores);
431 if (amt_wanted <= amt_granted)
433 return amt_wanted - amt_granted;
436 /* Actual work of the MCP kscheduler. if we were called by poke_ksched, *arg
437 * might be the process who wanted special service. this would be the case if
438 * we weren't already running the ksched. Sort of a ghetto way to "post work",
439 * such that it's an optimization. */
440 static void __run_mcp_ksched(void *arg)
442 struct proc *p, *temp;
444 struct proc_list *temp_mcp_list;
445 /* locking to protect the MCP lists' integrity and membership */
446 spin_lock(&sched_lock);
447 /* 2-pass scheme: check each proc on the primary list (FCFS). if they need
448 * nothing, put them on the secondary list. if they need something, rip
449 * them off the list, service them, and if they are still not dying, put
450 * them on the secondary list. We cull the entire primary list, so that
451 * when we start from the beginning each time, we aren't repeatedly checking
452 * procs we looked at on previous waves.
454 * TODO: we could modify this such that procs that we failed to service move
455 * to yet another list or something. We can also move the WAITINGs to
456 * another list and have wakeup move them back, etc. */
457 while (!TAILQ_EMPTY(primary_mcps)) {
458 TAILQ_FOREACH_SAFE(p, primary_mcps, ksched_data.proc_link, temp) {
459 if (p->state == PROC_WAITING) { /* unlocked peek at the state */
460 switch_lists(p, primary_mcps, secondary_mcps);
463 amt_needed = get_cores_needed(p);
465 switch_lists(p, primary_mcps, secondary_mcps);
468 /* o/w, we want to give cores to this proc */
469 remove_from_list(p, primary_mcps);
470 /* now it won't die, but it could get removed from lists and have
471 * its stuff unprov'd when we unlock */
473 /* GIANT WARNING: __core_req will unlock the sched lock for a bit.
474 * It will return with it locked still. We could unlock before we
475 * pass in, but they will relock right away. */
476 // notionally_unlock(&ksched_lock); /* for mouse-eyed viewers */
477 __core_request(p, amt_needed);
478 // notionally_lock(&ksched_lock);
479 /* Peeking at the state is okay, since we hold a ref. Once it is
480 * DYING, it'll remain DYING until we decref. And if there is a
481 * concurrent death, that will spin on the ksched lock (which we
482 * hold, and which protects the proc lists). */
483 if (p->state != PROC_DYING)
484 add_to_list(p, secondary_mcps);
485 proc_decref(p); /* fyi, this may trigger __proc_free */
486 /* need to break: the proc lists may have changed when we unlocked
487 * in core_req in ways that the FOREACH_SAFE can't handle. */
491 /* at this point, we moved all the procs over to the secondary list, and
492 * attempted to service the ones that wanted something. now just swap the
493 * lists for the next invocation of the ksched. */
494 temp_mcp_list = primary_mcps;
495 primary_mcps = secondary_mcps;
496 secondary_mcps = temp_mcp_list;
497 spin_unlock(&sched_lock);
500 /* Something has changed, and for whatever reason the scheduler should
503 * Don't call this if you are processing a syscall or otherwise care about your
504 * kthread variables, cur_proc/owning_proc, etc.
506 * Don't call this from interrupt context (grabs proclocks). */
507 void run_scheduler(void)
509 /* MCP scheduling: post work, then poke. for now, i just want the func to
510 * run again, so merely a poke is sufficient. */
511 poke(&ksched_poker, 0);
512 if (management_core()) {
513 spin_lock(&sched_lock);
515 spin_unlock(&sched_lock);
519 /* A process is asking the ksched to look at its resource desires. The
520 * scheduler is free to ignore this, for its own reasons, so long as it
521 * eventually gets around to looking at resource desires. */
522 void poke_ksched(struct proc *p, unsigned int res_type)
524 /* ignoring res_type for now. could post that if we wanted (would need some
525 * other structs/flags) */
526 if (!__proc_is_mcp(p))
528 poke(&ksched_poker, p);
531 /* The calling cpu/core has nothing to do and plans to idle/halt. This is an
532 * opportunity to pick the nature of that halting (low power state, etc), or
533 * provide some other work (_Ss on LL cores). Note that interrupts are
534 * disabled, and if you return, the core will cpu_halt(). */
537 bool new_proc = FALSE;
538 if (!management_core())
540 spin_lock(&sched_lock);
541 new_proc = __schedule_scp();
542 spin_unlock(&sched_lock);
543 /* if we just scheduled a proc, we need to manually restart it, instead of
544 * returning. if we return, the core will halt. */
549 /* Could drop into the monitor if there are no processes at all. For now,
550 * the 'call of the giraffe' suffices. */
553 /* Available resources changed (plus or minus). Some parts of the kernel may
554 * call this if a particular resource that is 'quantity-based' changes. Things
555 * like available RAM to processes, bandwidth, etc. Cores would probably be
556 * inappropriate, since we need to know which specific core is now free. */
557 void avail_res_changed(int res_type, long change)
559 printk("[kernel] ksched doesn't track any resources yet!\n");
562 int get_any_idle_core(void)
564 struct sched_pcore *spc;
566 spin_lock(&sched_lock);
567 while ((spc = TAILQ_FIRST(&idlecores))) {
568 /* Don't take cores that are provisioned to a process */
571 assert(!spc->alloc_proc);
572 TAILQ_REMOVE(&idlecores, spc, alloc_next);
573 ret = spc2pcoreid(spc);
576 spin_unlock(&sched_lock);
580 /* TODO: if we end up using this a lot, track CG-idleness as a property of the
581 * SPC instead of doing a linear search. */
582 static bool __spc_is_idle(struct sched_pcore *spc)
584 struct sched_pcore *i;
585 TAILQ_FOREACH(i, &idlecores, alloc_next) {
592 int get_specific_idle_core(int coreid)
594 struct sched_pcore *spc = pcoreid2spc(coreid);
596 assert((0 <= coreid) && (coreid < num_cores));
597 spin_lock(&sched_lock);
598 if (__spc_is_idle(pcoreid2spc(coreid)) && !spc->prov_proc) {
599 assert(!spc->alloc_proc);
600 TAILQ_REMOVE(&idlecores, spc, alloc_next);
603 spin_unlock(&sched_lock);
607 /* similar to __sched_put_idle_core, but without the prov tracking */
608 void put_idle_core(int coreid)
610 struct sched_pcore *spc = pcoreid2spc(coreid);
611 assert((0 <= coreid) && (coreid < num_cores));
612 spin_lock(&sched_lock);
613 TAILQ_INSERT_TAIL(&idlecores, spc, alloc_next);
614 spin_unlock(&sched_lock);
617 /* Normally it'll be the max number of CG cores ever */
618 uint32_t max_vcores(struct proc *p)
621 #ifdef CONFIG_DISABLE_SMT
622 return num_cores >> 1;
624 return num_cores - 1; /* reserving core 0 */
625 #endif /* CONFIG_DISABLE_SMT */
628 /* Find the best core to give to p. First check p's list of cores
629 * provisioned to it, but not yet allocated. If no cores are found, try and
630 * pull from the idle list. If no cores found on either list, return NULL.
632 struct sched_pcore *find_best_core(struct proc *p)
634 struct sched_pcore *spc_i = NULL;
635 spc_i = TAILQ_FIRST(&p->ksched_data.crd.prov_not_alloc_me);
637 spc_i = TAILQ_FIRST(&idlecores);
641 /* This deals with a request for more cores. The amt of new cores needed is
642 * passed in. The ksched lock is held, but we are free to unlock if we want
643 * (and we must, if calling out of the ksched to anything high-level).
645 * Side note: if we want to warn, then we can't deal with this proc's prov'd
646 * cores until we wait til the alarm goes off. would need to put all
647 * alarmed cores on a list and wait til the alarm goes off to do the full
648 * preempt. and when those cores come in voluntarily, we'd need to know to
649 * give them to this proc. */
650 static void __core_request(struct proc *p, uint32_t amt_needed)
652 uint32_t nr_to_grant = 0;
653 uint32_t corelist[num_cores];
654 struct sched_pcore *spc_i, *temp;
655 struct proc *proc_to_preempt;
657 /* we come in holding the ksched lock, and we hold it here to protect
658 * allocations and provisioning. */
659 /* get all available cores from their prov_not_alloc list. the list might
660 * change when we unlock (new cores added to it, or the entire list emptied,
661 * but no core allocations will happen (we hold the poke)). */
662 while (nr_to_grant != amt_needed) {
663 /* Find the next best core to allocate to p. It may be a core
664 * provisioned to p, and it might not be. */
665 spc_i = find_best_core(p);
666 /* If no core is returned, we know that there are no more cores to give
667 * out, so we exit the loop. */
670 /* If the pcore chosen currently has a proc allocated to it, we know
671 * it must be provisioned to p, but not allocated to it. We need to try
672 * to preempt. After this block, the core will be track_dealloc'd and
673 * on the idle list (regardless of whether we had to preempt or not) */
674 if (spc_i->alloc_proc) {
675 proc_to_preempt = spc_i->alloc_proc;
676 /* would break both preemption and maybe the later decref */
677 assert(proc_to_preempt != p);
678 /* need to keep a valid, external ref when we unlock */
679 proc_incref(proc_to_preempt, 1);
680 spin_unlock(&sched_lock);
681 /* sending no warning time for now - just an immediate preempt. */
682 success = proc_preempt_core(proc_to_preempt, spc2pcoreid(spc_i), 0);
683 /* reaquire locks to protect provisioning and idle lists */
684 spin_lock(&sched_lock);
686 /* we preempted it before the proc could yield or die.
687 * alloc_proc should not have changed (it'll change in death and
688 * idle CBs). the core is not on the idle core list. (if we
689 * ever have proc alloc lists, it'll still be on the old proc's
691 assert(spc_i->alloc_proc);
692 /* regardless of whether or not it is still prov to p, we need
693 * to note its dealloc. we are doing some excessive checking of
694 * p == prov_proc, but using this helper is a lot clearer. */
695 __prov_track_dealloc(proc_to_preempt, spc2pcoreid(spc_i));
697 /* the preempt failed, which should only happen if the pcore was
698 * unmapped (could be dying, could be yielding, but NOT
699 * preempted). whoever unmapped it also triggered (or will soon
700 * trigger) a track_dealloc and put it on the idle list. our
701 * signal for this is spc_i->alloc_proc being 0. We need to
702 * spin and let whoever is trying to free the core grab the
703 * ksched lock. We could use an 'ignore_next_idle' flag per
704 * sched_pcore, but it's not critical anymore.
706 * Note, we're relying on us being the only preemptor - if the
707 * core was unmapped by *another* preemptor, there would be no
708 * way of knowing the core was made idle *yet* (the success
709 * branch in another thread). likewise, if there were another
710 * allocator, the pcore could have been put on the idle list and
711 * then quickly removed/allocated. */
713 while (spc_i->alloc_proc) {
714 /* this loop should be very rare */
715 spin_unlock(&sched_lock);
717 spin_lock(&sched_lock);
720 /* no longer need to keep p_to_pre alive */
721 proc_decref(proc_to_preempt);
722 /* might not be prov to p anymore (rare race). spc_i is idle - we
723 * might get it later, or maybe we'll give it to its rightful proc*/
724 if (spc_i->prov_proc != p)
727 /* At this point, the pcore is idle, regardless of how we got here
728 * (successful preempt, failed preempt, or it was idle in the first
729 * place). We also know the core is still provisioned to us. Lets add
730 * it to the corelist for p (so we can give it to p in bulk later), and
731 * track its allocation with p (so our internal data structures stay in
732 * sync). We rely on the fact that we are the only allocator (spc_i is
733 * still idle, despite (potentially) unlocking during the preempt
734 * attempt above). It is guaranteed to be track_dealloc'd()
735 * (regardless of how we got here). */
736 corelist[nr_to_grant] = spc2pcoreid(spc_i);
738 __prov_track_alloc(p, spc2pcoreid(spc_i));
740 /* Now, actually give them out */
742 /* Need to unlock before calling out to proc code. We are somewhat
743 * relying on being the only one allocating 'thread' here, since another
744 * allocator could have seen these cores (if they are prov to some proc)
745 * and could be trying to give them out (and assuming they are already
746 * on the idle list). */
747 spin_unlock(&sched_lock);
748 /* give them the cores. this will start up the extras if RUNNING_M. */
749 spin_lock(&p->proc_lock);
750 /* if they fail, it is because they are WAITING or DYING. we could give
751 * the cores to another proc or whatever. for the current type of
752 * ksched, we'll just put them back on the pile and return. Note, the
753 * ksched could check the states after locking, but it isn't necessary:
754 * just need to check at some point in the ksched loop. */
755 if (__proc_give_cores(p, corelist, nr_to_grant)) {
756 spin_unlock(&p->proc_lock);
757 /* we failed, put the cores and track their dealloc. lock is
758 * protecting those structures. */
759 spin_lock(&sched_lock);
760 __prov_track_dealloc_bulk(p, corelist, nr_to_grant);
762 /* at some point after giving cores, call proc_run_m() (harmless on
763 * RUNNING_Ms). You can give small groups of cores, then run them
764 * (which is more efficient than interleaving runs with the gives
765 * for bulk preempted processes). */
767 spin_unlock(&p->proc_lock);
768 /* main mcp_ksched wants this held (it came to __core_req held) */
769 spin_lock(&sched_lock);
772 /* note the ksched lock is still held */
775 /* TODO: need more thorough CG/LL management. For now, core0 is the only LL
776 * core. This won't play well with the ghetto shit in schedule_init() if you do
777 * anything like 'DEDICATED_MONITOR' or the ARSC server. All that needs an
779 static bool is_ll_core(uint32_t pcoreid)
786 /* Helper, makes sure the prov/alloc structures track the pcore properly when it
787 * is allocated to p. Might make this take a sched_pcore * in the future. */
788 static void __prov_track_alloc(struct proc *p, uint32_t pcoreid)
790 struct sched_pcore *spc;
791 assert(pcoreid < num_cores); /* catch bugs */
792 spc = pcoreid2spc(pcoreid);
793 assert(spc->alloc_proc != p); /* corruption or double-alloc */
795 /* if the pcore is prov to them and now allocated, move lists */
796 if (spc->prov_proc == p) {
797 TAILQ_REMOVE(&p->ksched_data.crd.prov_not_alloc_me, spc, prov_next);
798 TAILQ_INSERT_TAIL(&p->ksched_data.crd.prov_alloc_me, spc, prov_next);
800 /* Actually allocate the core, removing it from the idle core list. */
801 TAILQ_REMOVE(&idlecores, spc, alloc_next);
804 /* Helper, makes sure the prov/alloc structures track the pcore properly when it
805 * is deallocated from p. */
806 static void __prov_track_dealloc(struct proc *p, uint32_t pcoreid)
808 struct sched_pcore *spc;
809 assert(pcoreid < num_cores); /* catch bugs */
810 spc = pcoreid2spc(pcoreid);
812 /* if the pcore is prov to them and now deallocated, move lists */
813 if (spc->prov_proc == p) {
814 TAILQ_REMOVE(&p->ksched_data.crd.prov_alloc_me, spc, prov_next);
815 /* this is the victim list, which can be sorted so that we pick the
816 * right victim (sort by alloc_proc reverse priority, etc). In this
817 * case, the core isn't alloc'd by anyone, so it should be the first
819 TAILQ_INSERT_HEAD(&p->ksched_data.crd.prov_not_alloc_me, spc,
822 /* Actually dealloc the core, putting it back on the idle core list. */
823 TAILQ_INSERT_TAIL(&idlecores, spc, alloc_next);
826 /* Bulk interface for __prov_track_dealloc */
827 static void __prov_track_dealloc_bulk(struct proc *p, uint32_t *pc_arr,
830 for (int i = 0; i < nr_cores; i++)
831 __prov_track_dealloc(p, pc_arr[i]);
834 /* P will get pcore if it needs more cores next time we look at it */
835 int provision_core(struct proc *p, uint32_t pcoreid)
837 struct sched_pcore *spc;
838 struct sched_pcore_tailq *prov_list;
839 /* Make sure we aren't asking for something that doesn't exist (bounds check
840 * on the pcore array) */
841 if (!(pcoreid < num_cores)) {
845 /* Don't allow the provisioning of LL cores */
846 if (is_ll_core(pcoreid)) {
850 spc = pcoreid2spc(pcoreid);
851 /* Note the sched lock protects the spc tailqs for all procs in this code.
852 * If we need a finer grained sched lock, this is one place where we could
853 * have a different lock */
854 spin_lock(&sched_lock);
855 /* If the core is already prov to someone else, take it away. (last write
856 * wins, some other layer or new func can handle permissions). */
857 if (spc->prov_proc) {
858 /* the list the spc is on depends on whether it is alloced to the
859 * prov_proc or not */
860 prov_list = (spc->alloc_proc == spc->prov_proc ?
861 &spc->prov_proc->ksched_data.crd.prov_alloc_me :
862 &spc->prov_proc->ksched_data.crd.prov_not_alloc_me);
863 TAILQ_REMOVE(prov_list, spc, prov_next);
865 /* Now prov it to p. Again, the list it goes on depends on whether it is
866 * alloced to p or not. Callers can also send in 0 to de-provision. */
868 if (spc->alloc_proc == p) {
869 TAILQ_INSERT_TAIL(&p->ksched_data.crd.prov_alloc_me, spc,
872 /* this is be the victim list, which can be sorted so that we pick
873 * the right victim (sort by alloc_proc reverse priority, etc). */
874 TAILQ_INSERT_TAIL(&p->ksched_data.crd.prov_not_alloc_me, spc,
879 spin_unlock(&sched_lock);
883 /************** Debugging **************/
884 void sched_diag(void)
887 spin_lock(&sched_lock);
888 TAILQ_FOREACH(p, &runnable_scps, ksched_data.proc_link)
889 printk("Runnable _S PID: %d\n", p->pid);
890 TAILQ_FOREACH(p, &unrunnable_scps, ksched_data.proc_link)
891 printk("Unrunnable _S PID: %d\n", p->pid);
892 TAILQ_FOREACH(p, primary_mcps, ksched_data.proc_link)
893 printk("Primary MCP PID: %d\n", p->pid);
894 TAILQ_FOREACH(p, secondary_mcps, ksched_data.proc_link)
895 printk("Secondary MCP PID: %d\n", p->pid);
896 spin_unlock(&sched_lock);
900 void print_idlecoremap(void)
902 struct sched_pcore *spc_i;
903 /* not locking, so we can look at this without deadlocking. */
904 printk("Idle cores (unlocked!):\n");
905 TAILQ_FOREACH(spc_i, &idlecores, alloc_next)
906 printk("Core %d, prov to %d (%p)\n", spc2pcoreid(spc_i),
907 spc_i->prov_proc ? spc_i->prov_proc->pid : 0, spc_i->prov_proc);
910 void print_resources(struct proc *p)
912 printk("--------------------\n");
913 printk("PID: %d\n", p->pid);
914 printk("--------------------\n");
915 for (int i = 0; i < MAX_NUM_RESOURCES; i++)
916 printk("Res type: %02d, amt wanted: %08d, amt granted: %08d\n", i,
917 p->procdata->res_req[i].amt_wanted, p->procinfo->res_grant[i]);
920 void print_all_resources(void)
923 void __print_resources(void *item, void *opaque)
925 print_resources((struct proc*)item);
927 spin_lock(&pid_hash_lock);
928 hash_for_each(pid_hash, __print_resources, NULL);
929 spin_unlock(&pid_hash_lock);
932 void print_prov_map(void)
934 struct sched_pcore *spc_i;
935 /* Doing this unlocked, which is dangerous, but won't deadlock */
936 printk("Which cores are provisioned to which procs:\n------------------\n");
937 for (int i = 0; i < num_cores; i++) {
938 spc_i = pcoreid2spc(i);
939 printk("Core %02d, prov: %d(%p) alloc: %d(%p)\n", i,
940 spc_i->prov_proc ? spc_i->prov_proc->pid : 0, spc_i->prov_proc,
941 spc_i->alloc_proc ? spc_i->alloc_proc->pid : 0,
946 void print_proc_prov(struct proc *p)
948 struct sched_pcore *spc_i;
951 printk("Prov cores alloced to proc %d (%p)\n----------\n", p->pid, p);
952 TAILQ_FOREACH(spc_i, &p->ksched_data.crd.prov_alloc_me, prov_next)
953 printk("Pcore %d\n", spc2pcoreid(spc_i));
954 printk("Prov cores not alloced to proc %d (%p)\n----------\n", p->pid, p);
955 TAILQ_FOREACH(spc_i, &p->ksched_data.crd.prov_not_alloc_me, prov_next)
956 printk("Pcore %d (alloced to %d (%p))\n", spc2pcoreid(spc_i),
957 spc_i->alloc_proc ? spc_i->alloc_proc->pid : 0,
961 void next_core(uint32_t pcoreid)
963 struct sched_pcore *spc_i;
965 spin_lock(&sched_lock);
966 TAILQ_FOREACH(spc_i, &idlecores, alloc_next) {
967 if (spc2pcoreid(spc_i) == pcoreid) {
973 TAILQ_REMOVE(&idlecores, spc_i, alloc_next);
974 TAILQ_INSERT_HEAD(&idlecores, spc_i, alloc_next);
975 printk("Pcore %d will be given out next (from the idles)\n", pcoreid);
977 spin_unlock(&sched_lock);
980 void sort_idles(void)
982 struct sched_pcore *spc_i, *spc_j, *temp;
983 struct sched_pcore_tailq sorter = TAILQ_HEAD_INITIALIZER(sorter);
985 spin_lock(&sched_lock);
986 TAILQ_CONCAT(&sorter, &idlecores, alloc_next);
987 TAILQ_FOREACH_SAFE(spc_i, &sorter, alloc_next, temp) {
988 TAILQ_REMOVE(&sorter, spc_i, alloc_next);
990 /* don't need foreach_safe since we break after we muck with the list */
991 TAILQ_FOREACH(spc_j, &idlecores, alloc_next) {
993 TAILQ_INSERT_BEFORE(spc_j, spc_i, alloc_next);
999 TAILQ_INSERT_TAIL(&idlecores, spc_i, alloc_next);
1001 spin_unlock(&sched_lock);