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 /* Kmsg, to run the scheduler tick (not in interrupt context) and reset the
101 * alarm. Note that interrupts will be disabled, but this is not the same as
102 * interrupt context. We're a routine kmsg, which means the core is in a
103 * quiescent state. */
104 static void __ksched_tick(uint32_t srcid, long a0, long a1, long a2)
106 /* TODO: imagine doing some accounting here */
108 /* Set our alarm to go off, incrementing from our last tick (instead of
109 * setting it relative to now, since some time has passed since the alarm
110 * first went off. Note, this may be now or in the past! */
111 set_awaiter_inc(&ksched_waiter, TIMER_TICK_USEC);
112 set_alarm(&per_cpu_info[core_id()].tchain, &ksched_waiter);
115 /* Interrupt/alarm handler: tells our core to run the scheduler (out of
116 * interrupt context). */
117 static void __kalarm(struct alarm_waiter *waiter)
119 send_kernel_message(core_id(), __ksched_tick, 0, 0, 0, KMSG_ROUTINE);
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, __kalarm);
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 #ifdef __CONFIG_ARSC_SERVER__
143 struct sched_pcore *a_core = TAILQ_FIRST(&idlecores);
145 TAILQ_REMOVE(&idlecores, a_core, alloc_next);
146 send_kernel_message(spc2pcoreid(a_core), arsc_server, 0, 0, 0,
148 warn("Using core %d for the ARSCs - there are probably issues with this.",
149 spc2pcoreid(a_core));
150 #endif /* __CONFIG_ARSC_SERVER__ */
151 spin_unlock(&sched_lock);
155 static uint32_t spc2pcoreid(struct sched_pcore *spc)
157 return spc - all_pcores;
160 static struct sched_pcore *pcoreid2spc(uint32_t pcoreid)
162 return &all_pcores[pcoreid];
165 /* Round-robins on whatever list it's on */
166 static void add_to_list(struct proc *p, struct proc_list *new)
168 assert(!(p->ksched_data.cur_list));
169 TAILQ_INSERT_TAIL(new, p, ksched_data.proc_link);
170 p->ksched_data.cur_list = new;
173 static void remove_from_list(struct proc *p, struct proc_list *old)
175 assert(p->ksched_data.cur_list == old);
176 TAILQ_REMOVE(old, p, ksched_data.proc_link);
177 p->ksched_data.cur_list = 0;
180 static void switch_lists(struct proc *p, struct proc_list *old,
181 struct proc_list *new)
183 remove_from_list(p, old);
187 /* Removes from whatever list p is on */
188 static void remove_from_any_list(struct proc *p)
190 if (p->ksched_data.cur_list) {
191 TAILQ_REMOVE(p->ksched_data.cur_list, p, ksched_data.proc_link);
192 p->ksched_data.cur_list = 0;
196 /************** Process Management Callbacks **************/
198 * - the proc lock is NOT held for any of these calls. currently, there is no
199 * lock ordering between the sched lock and the proc lock. since the proc
200 * code doesn't know what we do, it doesn't hold its lock when calling our
202 * - since the proc lock isn't held, the proc could be dying, which means we
203 * will receive a __sched_proc_destroy() either before or after some of these
204 * other CBs. the CBs related to list management need to check and abort if
206 void __sched_proc_register(struct proc *p)
208 assert(p->state != PROC_DYING); /* shouldn't be abel to happen yet */
209 /* one ref for the proc's existence, cradle-to-grave */
210 proc_incref(p, 1); /* need at least this OR the 'one for existing' */
211 spin_lock(&sched_lock);
212 TAILQ_INIT(&p->ksched_data.prov_alloc_me);
213 TAILQ_INIT(&p->ksched_data.prov_not_alloc_me);
214 add_to_list(p, &unrunnable_scps);
215 spin_unlock(&sched_lock);
218 /* Returns 0 if it succeeded, an error code otherwise. */
219 void __sched_proc_change_to_m(struct proc *p)
221 spin_lock(&sched_lock);
222 /* Need to make sure they aren't dying. if so, we already dealt with their
223 * list membership, etc (or soon will). taking advantage of the 'immutable
224 * state' of dying (so long as refs are held). */
225 if (p->state == PROC_DYING) {
226 spin_unlock(&sched_lock);
229 /* Catch user bugs */
230 if (!p->procdata->res_req[RES_CORES].amt_wanted) {
231 printk("[kernel] process needs to specify amt_wanted\n");
232 p->procdata->res_req[RES_CORES].amt_wanted = 1;
234 /* For now, this should only ever be called on an unrunnable. It's
235 * probably a bug, at this stage in development, to do o/w. */
236 remove_from_list(p, &unrunnable_scps);
237 //remove_from_any_list(p); /* ^^ instead of this */
238 add_to_list(p, primary_mcps);
239 spin_unlock(&sched_lock);
240 //poke_ksched(p, RES_CORES);
243 /* Helper for the destroy CB : unprovisions any pcores for the given list */
244 static void unprov_pcore_list(struct sched_pcore_tailq *list_head)
246 struct sched_pcore *spc_i;
247 /* We can leave them connected within the tailq, since the scps don't have a
248 * default list (if they aren't on a proc's list, then we don't care about
249 * them), and since the INSERTs don't care what list you were on before
250 * (chummy with the implementation). Pretty sure this is right. If there's
251 * suspected list corruption, be safer here. */
252 TAILQ_FOREACH(spc_i, list_head, prov_next)
253 spc_i->prov_proc = 0;
254 TAILQ_INIT(list_head);
257 /* Sched callback called when the proc dies. pc_arr holds the cores the proc
258 * had, if any, and nr_cores tells us how many are in the array.
260 * An external, edible ref is passed in. when we return and they decref,
261 * __proc_free will be called (when the last one is done). */
262 void __sched_proc_destroy(struct proc *p, uint32_t *pc_arr, uint32_t nr_cores)
264 spin_lock(&sched_lock);
265 /* Unprovision any cores. Note this is different than track_dealloc.
266 * The latter does bookkeeping when an allocation changes. This is a
267 * bulk *provisioning* change. */
268 unprov_pcore_list(&p->ksched_data.prov_alloc_me);
269 unprov_pcore_list(&p->ksched_data.prov_not_alloc_me);
270 /* Remove from whatever list we are on (if any - might not be on one if it
271 * was in the middle of __run_mcp_sched) */
272 remove_from_any_list(p);
274 __put_idle_cores(p, pc_arr, nr_cores);
275 __prov_track_dealloc_bulk(p, pc_arr, nr_cores);
277 spin_unlock(&sched_lock);
278 /* Drop the cradle-to-the-grave reference, jet-li */
282 /* ksched callbacks. p just woke up and is UNLOCKED. */
283 void __sched_mcp_wakeup(struct proc *p)
285 spin_lock(&sched_lock);
286 if (p->state == PROC_DYING) {
287 spin_unlock(&sched_lock);
290 /* could try and prioritize p somehow (move it to the front of the list).
291 * for now, just help them out a bit (mild help here, can remove this) */
292 if (!p->procdata->res_req[RES_CORES].amt_wanted)
293 p->procdata->res_req[RES_CORES].amt_wanted = 1;
294 spin_unlock(&sched_lock);
295 /* note they could be dying at this point too. */
296 poke(&ksched_poker, p);
299 /* ksched callbacks. p just woke up and is UNLOCKED. */
300 void __sched_scp_wakeup(struct proc *p)
302 spin_lock(&sched_lock);
303 if (p->state == PROC_DYING) {
304 spin_unlock(&sched_lock);
307 /* might not be on a list if it is new. o/w, it should be unrunnable */
308 remove_from_any_list(p);
309 add_to_list(p, &runnable_scps);
310 spin_unlock(&sched_lock);
313 /* Callback to return a core to the ksched, which tracks it as idle and
314 * deallocated from p. The proclock is held (__core_req depends on that).
316 * This also is a trigger, telling us we have more cores. We could/should make
317 * a scheduling decision (or at least plan to). */
318 void __sched_put_idle_core(struct proc *p, uint32_t coreid)
320 struct sched_pcore *spc = pcoreid2spc(coreid);
321 spin_lock(&sched_lock);
322 TAILQ_INSERT_TAIL(&idlecores, spc, alloc_next);
323 __prov_track_dealloc(p, coreid);
324 spin_unlock(&sched_lock);
327 /* Helper for put_idle and core_req. Note this does not track_dealloc. When we
328 * get rid of / revise proc_preempt_all and put_idle_cores, we can get rid of
329 * this. (the ksched will never need it - only external callers). */
330 static void __put_idle_cores(struct proc *p, uint32_t *pc_arr, uint32_t num)
332 struct sched_pcore *spc_i;
333 for (int i = 0; i < num; i++) {
334 spc_i = pcoreid2spc(pc_arr[i]);
335 TAILQ_INSERT_TAIL(&idlecores, spc_i, alloc_next);
339 /* Callback, bulk interface for put_idle. Note this one also calls track_dealloc,
340 * which the internal version does not. The proclock is held for this. */
341 void __sched_put_idle_cores(struct proc *p, uint32_t *pc_arr, uint32_t num)
343 spin_lock(&sched_lock);
344 /* TODO: when we revise this func, look at __put_idle */
345 __put_idle_cores(p, pc_arr, num);
346 __prov_track_dealloc_bulk(p, pc_arr, num);
347 spin_unlock(&sched_lock);
348 /* could trigger a sched decision here */
351 /* mgmt/LL cores should call this to schedule the calling core and give it to an
352 * SCP. will also prune the dead SCPs from the list. hold the lock before
353 * calling. returns TRUE if it scheduled a proc. */
354 static bool __schedule_scp(void)
356 // TODO: sort out lock ordering (proc_run_s also locks)
358 uint32_t pcoreid = core_id();
359 struct per_cpu_info *pcpui = &per_cpu_info[pcoreid];
361 /* if there are any runnables, run them here and put any currently running
362 * SCP on the tail of the runnable queue. */
363 if ((p = TAILQ_FIRST(&runnable_scps))) {
364 /* protect owning proc, cur_ctx, etc. note this nests with the
365 * calls in proc_yield_s */
366 disable_irqsave(&state);
367 /* someone is currently running, dequeue them */
368 if (pcpui->owning_proc) {
369 printd("Descheduled %d in favor of %d\n", pcpui->owning_proc->pid,
371 /* locking just to be safe */
372 spin_lock(&p->proc_lock);
373 __proc_set_state(pcpui->owning_proc, PROC_RUNNABLE_S);
374 __proc_save_context_s(pcpui->owning_proc, pcpui->cur_ctx);
375 spin_unlock(&p->proc_lock);
376 /* round-robin the SCPs (inserts at the end of the queue) */
377 switch_lists(pcpui->owning_proc, &unrunnable_scps, &runnable_scps);
378 clear_owning_proc(pcoreid);
379 /* Note we abandon core. It's not strictly necessary. If
380 * we didn't, the TLB would still be loaded with the old
381 * one, til we proc_run_s, and the various paths in
382 * proc_run_s would pick it up. This way is a bit safer for
383 * future changes, but has an extra (empty) TLB flush. */
386 /* Run the new proc */
387 switch_lists(p, &runnable_scps, &unrunnable_scps);
388 printd("PID of the SCP i'm running: %d\n", p->pid);
389 proc_run_s(p); /* gives it core we're running on */
390 enable_irqsave(&state);
396 /* Returns how many new cores p needs. This doesn't lock the proc, so your
397 * answer might be stale. */
398 static uint32_t get_cores_needed(struct proc *p)
400 uint32_t amt_wanted, amt_granted;
401 amt_wanted = p->procdata->res_req[RES_CORES].amt_wanted;
402 /* Help them out - if they ask for something impossible, give them 1 so they
403 * can make some progress. (this is racy, and unnecessary). */
404 if (amt_wanted > p->procinfo->max_vcores) {
405 p->procdata->res_req[RES_CORES].amt_wanted = 1;
408 /* amt_granted is racy - they could be *yielding*, but currently they can't
409 * be getting any new cores if the caller is in the mcp_ksched. this is
410 * okay - we won't accidentally give them more cores than they *ever* wanted
411 * (which could crash them), but our answer might be a little stale. */
412 amt_granted = p->procinfo->res_grant[RES_CORES];
413 /* Do not do an assert like this: it could fail (yield in progress): */
414 //assert(amt_granted == p->procinfo->num_vcores);
415 if (amt_wanted <= amt_granted)
417 return amt_wanted - amt_granted;
420 /* Actual work of the MCP kscheduler. if we were called by poke_ksched, *arg
421 * might be the process who wanted special service. this would be the case if
422 * we weren't already running the ksched. Sort of a ghetto way to "post work",
423 * such that it's an optimization. */
424 static void __run_mcp_ksched(void *arg)
426 struct proc *p, *temp;
428 struct proc_list *temp_mcp_list;
429 /* locking to protect the MCP lists' integrity and membership */
430 spin_lock(&sched_lock);
431 /* 2-pass scheme: check each proc on the primary list (FCFS). if they need
432 * nothing, put them on the secondary list. if they need something, rip
433 * them off the list, service them, and if they are still not dying, put
434 * them on the secondary list. We cull the entire primary list, so that
435 * when we start from the beginning each time, we aren't repeatedly checking
436 * procs we looked at on previous waves.
438 * TODO: we could modify this such that procs that we failed to service move
439 * to yet another list or something. We can also move the WAITINGs to
440 * another list and have wakeup move them back, etc. */
441 while (!TAILQ_EMPTY(primary_mcps)) {
442 TAILQ_FOREACH_SAFE(p, primary_mcps, ksched_data.proc_link, temp) {
443 if (p->state == PROC_WAITING) { /* unlocked peek at the state */
444 switch_lists(p, primary_mcps, secondary_mcps);
447 amt_needed = get_cores_needed(p);
449 switch_lists(p, primary_mcps, secondary_mcps);
452 /* o/w, we want to give cores to this proc */
453 remove_from_list(p, primary_mcps);
454 /* now it won't die, but it could get removed from lists and have
455 * its stuff unprov'd when we unlock */
457 /* GIANT WARNING: __core_req will unlock the sched lock for a bit.
458 * It will return with it locked still. We could unlock before we
459 * pass in, but they will relock right away. */
460 // notionally_unlock(&ksched_lock); /* for mouse-eyed viewers */
461 __core_request(p, amt_needed);
462 // notionally_lock(&ksched_lock);
463 /* Peeking at the state is okay, since we hold a ref. Once it is
464 * DYING, it'll remain DYING until we decref. And if there is a
465 * concurrent death, that will spin on the ksched lock (which we
466 * hold, and which protects the proc lists). */
467 if (p->state != PROC_DYING)
468 add_to_list(p, secondary_mcps);
469 proc_decref(p); /* fyi, this may trigger __proc_free */
470 /* need to break: the proc lists may have changed when we unlocked
471 * in core_req in ways that the FOREACH_SAFE can't handle. */
475 /* at this point, we moved all the procs over to the secondary list, and
476 * attempted to service the ones that wanted something. now just swap the
477 * lists for the next invocation of the ksched. */
478 temp_mcp_list = primary_mcps;
479 primary_mcps = secondary_mcps;
480 secondary_mcps = temp_mcp_list;
481 spin_unlock(&sched_lock);
484 /* Something has changed, and for whatever reason the scheduler should
487 * Don't call this from interrupt context (grabs proclocks). */
490 /* MCP scheduling: post work, then poke. for now, i just want the func to
491 * run again, so merely a poke is sufficient. */
492 poke(&ksched_poker, 0);
493 if (management_core()) {
494 spin_lock(&sched_lock);
496 spin_unlock(&sched_lock);
500 /* A process is asking the ksched to look at its resource desires. The
501 * scheduler is free to ignore this, for its own reasons, so long as it
502 * eventually gets around to looking at resource desires. */
503 void poke_ksched(struct proc *p, int res_type)
505 /* ignoring res_type for now. could post that if we wanted (would need some
506 * other structs/flags) */
507 if (!__proc_is_mcp(p))
509 poke(&ksched_poker, p);
512 /* The calling cpu/core has nothing to do and plans to idle/halt. This is an
513 * opportunity to pick the nature of that halting (low power state, etc), or
514 * provide some other work (_Ss on LL cores). Note that interrupts are
515 * disabled, and if you return, the core will cpu_halt(). */
518 bool new_proc = FALSE;
519 if (!management_core())
521 spin_lock(&sched_lock);
522 new_proc = __schedule_scp();
523 spin_unlock(&sched_lock);
524 /* if we just scheduled a proc, we need to manually restart it, instead of
525 * returning. if we return, the core will halt. */
530 /* Could drop into the monitor if there are no processes at all. For now,
531 * the 'call of the giraffe' suffices. */
534 /* Available resources changed (plus or minus). Some parts of the kernel may
535 * call this if a particular resource that is 'quantity-based' changes. Things
536 * like available RAM to processes, bandwidth, etc. Cores would probably be
537 * inappropriate, since we need to know which specific core is now free. */
538 void avail_res_changed(int res_type, long change)
540 printk("[kernel] ksched doesn't track any resources yet!\n");
543 /* Normally it'll be the max number of CG cores ever */
544 uint32_t max_vcores(struct proc *p)
547 #ifdef __CONFIG_DISABLE_SMT__
548 return num_cpus >> 1;
550 return num_cpus - 1; /* reserving core 0 */
551 #endif /* __CONFIG_DISABLE_SMT__ */
554 /* This deals with a request for more cores. The amt of new cores needed is
555 * passed in. The ksched lock is held, but we are free to unlock if we want
556 * (and we must, if calling out of the ksched to anything high-level).
558 * Side note: if we want to warn, then we can't deal with this proc's prov'd
559 * cores until we wait til the alarm goes off. would need to put all
560 * alarmed cores on a list and wait til the alarm goes off to do the full
561 * preempt. and when those cores come in voluntarily, we'd need to know to
562 * give them to this proc. */
563 static void __core_request(struct proc *p, uint32_t amt_needed)
565 uint32_t nr_to_grant = 0;
566 uint32_t corelist[num_cpus];
567 struct sched_pcore *spc_i, *temp;
568 struct proc *proc_to_preempt;
570 /* we come in holding the ksched lock, and we hold it here to protect
571 * allocations and provisioning. */
572 /* get all available cores from their prov_not_alloc list. the list might
573 * change when we unlock (new cores added to it, or the entire list emptied,
574 * but no core allocations will happen (we hold the poke)). */
575 while (!TAILQ_EMPTY(&p->ksched_data.prov_not_alloc_me)) {
576 if (nr_to_grant == amt_needed)
578 /* picking the next victim (first on the not_alloc list) */
579 spc_i = TAILQ_FIRST(&p->ksched_data.prov_not_alloc_me);
580 /* someone else has this proc's pcore, so we need to try to preempt.
581 * after this block, the core will be tracked dealloc'd and on the idle
582 * list (regardless of whether we had to preempt or not) */
583 if (spc_i->alloc_proc) {
584 proc_to_preempt = spc_i->alloc_proc;
585 /* would break both preemption and maybe the later decref */
586 assert(proc_to_preempt != p);
587 /* need to keep a valid, external ref when we unlock */
588 proc_incref(proc_to_preempt, 1);
589 spin_unlock(&sched_lock);
590 /* sending no warning time for now - just an immediate preempt. */
591 success = proc_preempt_core(proc_to_preempt, spc2pcoreid(spc_i), 0);
592 /* reaquire locks to protect provisioning and idle lists */
593 spin_lock(&sched_lock);
595 /* we preempted it before the proc could yield or die.
596 * alloc_proc should not have changed (it'll change in death and
597 * idle CBs). the core is not on the idle core list. (if we
598 * ever have proc alloc lists, it'll still be on the old proc's
600 assert(spc_i->alloc_proc);
601 /* regardless of whether or not it is still prov to p, we need
602 * to note its dealloc. we are doing some excessive checking of
603 * p == prov_proc, but using this helper is a lot clearer. */
604 __prov_track_dealloc(proc_to_preempt, spc2pcoreid(spc_i));
605 /* here, we rely on the fact that we are the only preemptor. we
606 * assume no one else preempted it, so we know it is available*/
607 TAILQ_INSERT_TAIL(&idlecores, spc_i, alloc_next);
609 /* the preempt failed, which should only happen if the pcore was
610 * unmapped (could be dying, could be yielding, but NOT
611 * preempted). whoever unmapped it also triggered (or will soon
612 * trigger) a track_dealloc and put it on the idle list. our
613 * signal for this is spc_i->alloc_proc being 0. We need to
614 * spin and let whoever is trying to free the core grab the
615 * ksched lock. We could use an 'ignore_next_idle' flag per
616 * sched_pcore, but it's not critical anymore.
618 * Note, we're relying on us being the only preemptor - if the
619 * core was unmapped by *another* preemptor, there would be no
620 * way of knowing the core was made idle *yet* (the success
621 * branch in another thread). likewise, if there were another
622 * allocator, the pcore could have been put on the idle list and
623 * then quickly removed/allocated. */
625 while (spc_i->alloc_proc) {
626 /* this loop should be very rare */
627 spin_unlock(&sched_lock);
629 spin_lock(&sched_lock);
632 /* no longer need to keep p_to_pre alive */
633 proc_decref(proc_to_preempt);
634 /* might not be prov to p anymore (rare race). spc_i is idle - we
635 * might get it later, or maybe we'll give it to its rightful proc*/
636 if (spc_i->prov_proc != p)
639 /* at this point, the pcore is idle, regardless of how we got here
640 * (successful preempt, failed preempt, or it was idle in the first
641 * place. the core is still provisioned. lets pull from the idle list
642 * and add it to the pc_arr for p. here, we rely on the fact that we
643 * are the only allocator (spc_i is still idle, despite unlocking). */
644 TAILQ_REMOVE(&idlecores, spc_i, alloc_next);
645 /* At this point, we have the core, ready to try to give it to the proc.
646 * It is on no alloc lists, and is track_dealloc'd() (regardless of how
649 * We'll give p its cores via a bulk list, which is better for the proc
650 * mgmt code (when going from runnable to running). */
651 corelist[nr_to_grant] = spc2pcoreid(spc_i);
653 __prov_track_alloc(p, spc2pcoreid(spc_i));
655 /* Try to get cores from the idle list that aren't prov to me (FCFS) */
656 TAILQ_FOREACH_SAFE(spc_i, &idlecores, alloc_next, temp) {
657 if (nr_to_grant == amt_needed)
659 TAILQ_REMOVE(&idlecores, spc_i, alloc_next);
660 corelist[nr_to_grant] = spc2pcoreid(spc_i);
662 __prov_track_alloc(p, spc2pcoreid(spc_i));
664 /* Now, actually give them out */
666 /* Need to unlock before calling out to proc code. We are somewhat
667 * relying on being the only one allocating 'thread' here, since another
668 * allocator could have seen these cores (if they are prov to some proc)
669 * and could be trying to give them out (and assuming they are already
670 * on the idle list). */
671 spin_unlock(&sched_lock);
672 /* give them the cores. this will start up the extras if RUNNING_M. */
673 spin_lock(&p->proc_lock);
674 /* if they fail, it is because they are WAITING or DYING. we could give
675 * the cores to another proc or whatever. for the current type of
676 * ksched, we'll just put them back on the pile and return. Note, the
677 * ksched could check the states after locking, but it isn't necessary:
678 * just need to check at some point in the ksched loop. */
679 if (__proc_give_cores(p, corelist, nr_to_grant)) {
680 spin_unlock(&p->proc_lock);
681 /* we failed, put the cores and track their dealloc. lock is
682 * protecting those structures. */
683 spin_lock(&sched_lock);
684 __put_idle_cores(p, corelist, nr_to_grant);
685 __prov_track_dealloc_bulk(p, corelist, nr_to_grant);
687 /* at some point after giving cores, call proc_run_m() (harmless on
688 * RUNNING_Ms). You can give small groups of cores, then run them
689 * (which is more efficient than interleaving runs with the gives
690 * for bulk preempted processes). */
692 spin_unlock(&p->proc_lock);
693 /* main mcp_ksched wants this held (it came to __core_req held) */
694 spin_lock(&sched_lock);
697 /* note the ksched lock is still held */
700 /* TODO: need more thorough CG/LL management. For now, core0 is the only LL
701 * core. This won't play well with the ghetto shit in schedule_init() if you do
702 * anything like 'DEDICATED_MONITOR' or the ARSC server. All that needs an
704 static bool is_ll_core(uint32_t pcoreid)
711 /* Helper, makes sure the prov/alloc structures track the pcore properly when it
712 * is allocated to p. Might make this take a sched_pcore * in the future. */
713 static void __prov_track_alloc(struct proc *p, uint32_t pcoreid)
715 struct sched_pcore *spc;
716 assert(pcoreid < num_cpus); /* catch bugs */
717 spc = pcoreid2spc(pcoreid);
718 assert(spc->alloc_proc != p); /* corruption or double-alloc */
720 /* if the pcore is prov to them and now allocated, move lists */
721 if (spc->prov_proc == p) {
722 TAILQ_REMOVE(&p->ksched_data.prov_not_alloc_me, spc, prov_next);
723 TAILQ_INSERT_TAIL(&p->ksched_data.prov_alloc_me, spc, prov_next);
727 /* Helper, makes sure the prov/alloc structures track the pcore properly when it
728 * is deallocated from p. */
729 static void __prov_track_dealloc(struct proc *p, uint32_t pcoreid)
731 struct sched_pcore *spc;
732 assert(pcoreid < num_cpus); /* catch bugs */
733 spc = pcoreid2spc(pcoreid);
735 /* if the pcore is prov to them and now deallocated, move lists */
736 if (spc->prov_proc == p) {
737 TAILQ_REMOVE(&p->ksched_data.prov_alloc_me, spc, prov_next);
738 /* this is the victim list, which can be sorted so that we pick the
739 * right victim (sort by alloc_proc reverse priority, etc). In this
740 * case, the core isn't alloc'd by anyone, so it should be the first
742 TAILQ_INSERT_HEAD(&p->ksched_data.prov_not_alloc_me, spc, prov_next);
746 /* Bulk interface for __prov_track_dealloc */
747 static void __prov_track_dealloc_bulk(struct proc *p, uint32_t *pc_arr,
750 for (int i = 0; i < nr_cores; i++)
751 __prov_track_dealloc(p, pc_arr[i]);
754 /* P will get pcore if it needs more cores next time we look at it */
755 int provision_core(struct proc *p, uint32_t pcoreid)
757 struct sched_pcore *spc;
758 struct sched_pcore_tailq *prov_list;
759 /* Make sure we aren't asking for something that doesn't exist (bounds check
760 * on the pcore array) */
761 if (!(pcoreid < num_cpus)) {
765 /* Don't allow the provisioning of LL cores */
766 if (is_ll_core(pcoreid)) {
770 spc = pcoreid2spc(pcoreid);
771 /* Note the sched lock protects the spc tailqs for all procs in this code.
772 * If we need a finer grained sched lock, this is one place where we could
773 * have a different lock */
774 spin_lock(&sched_lock);
775 /* If the core is already prov to someone else, take it away. (last write
776 * wins, some other layer or new func can handle permissions). */
777 if (spc->prov_proc) {
778 /* the list the spc is on depends on whether it is alloced to the
779 * prov_proc or not */
780 prov_list = (spc->alloc_proc == spc->prov_proc ?
781 &spc->prov_proc->ksched_data.prov_alloc_me :
782 &spc->prov_proc->ksched_data.prov_not_alloc_me);
783 TAILQ_REMOVE(prov_list, spc, prov_next);
785 /* Now prov it to p. Again, the list it goes on depends on whether it is
786 * alloced to p or not. Callers can also send in 0 to de-provision. */
788 if (spc->alloc_proc == p) {
789 TAILQ_INSERT_TAIL(&p->ksched_data.prov_alloc_me, spc, prov_next);
791 /* this is be the victim list, which can be sorted so that we pick
792 * the right victim (sort by alloc_proc reverse priority, etc). */
793 TAILQ_INSERT_TAIL(&p->ksched_data.prov_not_alloc_me, spc,
798 spin_unlock(&sched_lock);
802 /************** Debugging **************/
803 void sched_diag(void)
806 spin_lock(&sched_lock);
807 TAILQ_FOREACH(p, &runnable_scps, ksched_data.proc_link)
808 printk("Runnable _S PID: %d\n", p->pid);
809 TAILQ_FOREACH(p, &unrunnable_scps, ksched_data.proc_link)
810 printk("Unrunnable _S PID: %d\n", p->pid);
811 TAILQ_FOREACH(p, primary_mcps, ksched_data.proc_link)
812 printk("Primary MCP PID: %d\n", p->pid);
813 TAILQ_FOREACH(p, secondary_mcps, ksched_data.proc_link)
814 printk("Secondary MCP PID: %d\n", p->pid);
815 spin_unlock(&sched_lock);
819 void print_idlecoremap(void)
821 struct sched_pcore *spc_i;
822 /* not locking, so we can look at this without deadlocking. */
823 printk("Idle cores (unlocked!):\n");
824 TAILQ_FOREACH(spc_i, &idlecores, alloc_next)
825 printk("Core %d, prov to %d (%08p)\n", spc2pcoreid(spc_i),
826 spc_i->prov_proc ? spc_i->prov_proc->pid : 0, spc_i->prov_proc);
829 void print_resources(struct proc *p)
831 printk("--------------------\n");
832 printk("PID: %d\n", p->pid);
833 printk("--------------------\n");
834 for (int i = 0; i < MAX_NUM_RESOURCES; i++)
835 printk("Res type: %02d, amt wanted: %08d, amt granted: %08d\n", i,
836 p->procdata->res_req[i].amt_wanted, p->procinfo->res_grant[i]);
839 void print_all_resources(void)
842 void __print_resources(void *item)
844 print_resources((struct proc*)item);
846 spin_lock(&pid_hash_lock);
847 hash_for_each(pid_hash, __print_resources);
848 spin_unlock(&pid_hash_lock);
851 void print_prov_map(void)
853 struct sched_pcore *spc_i;
854 /* Doing this unlocked, which is dangerous, but won't deadlock */
855 printk("Which cores are provisioned to which procs:\n------------------\n");
856 for (int i = 0; i < num_cpus; i++) {
857 spc_i = pcoreid2spc(i);
858 printk("Core %02d, prov: %d(%08p) alloc: %d(%08p)\n", i,
859 spc_i->prov_proc ? spc_i->prov_proc->pid : 0, spc_i->prov_proc,
860 spc_i->alloc_proc ? spc_i->alloc_proc->pid : 0,
865 void print_proc_prov(struct proc *p)
867 struct sched_pcore *spc_i;
870 printk("Prov cores alloced to proc %d (%08p)\n----------\n", p->pid, p);
871 TAILQ_FOREACH(spc_i, &p->ksched_data.prov_alloc_me, prov_next)
872 printk("Pcore %d\n", spc2pcoreid(spc_i));
873 printk("Prov cores not alloced to proc %d (%08p)\n----------\n", p->pid, p);
874 TAILQ_FOREACH(spc_i, &p->ksched_data.prov_not_alloc_me, prov_next)
875 printk("Pcore %d (alloced to %d (%08p))\n", spc2pcoreid(spc_i),
876 spc_i->alloc_proc ? spc_i->alloc_proc->pid : 0,
880 void next_core(uint32_t pcoreid)
882 struct sched_pcore *spc_i;
884 spin_lock(&sched_lock);
885 TAILQ_FOREACH(spc_i, &idlecores, alloc_next) {
886 if (spc2pcoreid(spc_i) == pcoreid) {
892 TAILQ_REMOVE(&idlecores, spc_i, alloc_next);
893 TAILQ_INSERT_HEAD(&idlecores, spc_i, alloc_next);
894 printk("Pcore %d will be given out next (from the idles)\n", pcoreid);
896 spin_unlock(&sched_lock);