Page 1 of 2
Releasing finished task's stack
Posted: Fri Mar 30, 2012 7:47 am
by invalid
Hi, here's another newbie question.
I have multitasking implemented by timer interrupt calling task_switch, which basically switches to next task's stack.
When a task is launched, I allocate some memory for it's stack and fill it with values to be popped by popad/iret.
When a task finishes, it's resources are released, and the task is marked "finished" (not to be reentered). However, I don't know how to release the stack space - the timer interrupt still needs to push return address somewhere!
So I ended up with "Hades" task, which does the job of releasing those dead stacks. But I consider this solution to be a funny workaround.
Can you give me a hint about what I can do smarter, or point me to some further readings?
Thanks in advance.
Re: Releasing finished task's stack
Posted: Fri Mar 30, 2012 7:55 am
by bluemoon
I do it this way:
When terminating a process:
- mark zombie flag
- yield (give up remaining time slice and reschedule)
In the scheduler, when switch out from a process,
- if the zombie flag is set, the process is put into zombie list
- if sleep flag is set, it's put into slept list
- put back into active process list waiting next schedule or preempt
In kernel's maintenance thread, which run in lowest priority, it pull out any zombie and do clean up in that thread's time-slice and process space.
The advantage is the system don't lag when a process ends, and the cleanup logic can be a lot easier to write.
Re: Releasing finished task's stack
Posted: Fri Mar 30, 2012 9:20 am
by Owen
On most operating systems, you need some kind of handle to a task to hang around after it ceases to exist. For example, on POSIX a process will hang around until someone executes the wait() call and reaps it by receiving its termination notification.
The exceptions are detached threads; for that case, I would probably engage in some assembly trickery whereby I would inject the thread final shutdown code to be run inside the next thread that is to be run. Alternatively, stick with your current approach, where the kernel has one or more threads which exists just to reap dead threads.
Re: Releasing finished task's stack
Posted: Fri Mar 30, 2012 10:41 am
by invalid
Hmmm... Maybe having a "janitor" hanging around is not that bad idea after all...
Thanks!
Re: Releasing finished task's stack
Posted: Fri Mar 30, 2012 10:48 am
by bluemoon
As a side note, some system (eg android) do not clean some resources after all, process are suspended and just get swapped later naturally, or if the user decided to run the application again quick enough, some resources can be retained again and thus less things to clean(ie. revive a zombie).
That is a feature can be extended with such janitor, or delayed clean up logic.
Re: Releasing finished task's stack
Posted: Fri Mar 30, 2012 11:14 am
by gravaera
Yo:
All of my CPUs have a small "sleep stack" which is used for power management operations such as placing CPUs into low-power states, and waking them up from such states, waking CPUs up from a powered-down state, and for executing code when the CPU has no access to a stack.
Since the CPU will not be making long chains of calls from within any code that would require it to use a sleep stack, the stack push-depth need not be anything over 256, or 384 to be safe. Naturally, these are estimates. In your case, for something as trivial as switching to a new task while being unable to depend on the presence of a usable thread-kernel-stack, you can switch to the CPU's sleep stack and use that to execute the switching code.
--Peace out,
gravaera
Re: Releasing finished task's stack
Posted: Fri Mar 30, 2012 11:48 am
by invalid
gravaera wrote:you can switch to the CPU's sleep stack
Yes, I've considered switching to some temporary stack, but then it would be problematic to have more than one task finishing at the same time (especially that I plan to put some mutexes on memory operations).
OMG, writing a kernel is so much fun and an adventure full of open possibilities

Re: Releasing finished task's stack
Posted: Fri Mar 30, 2012 11:59 am
by gravaera
ydoom wrote:...it would be problematic to have more than one task finishing at the same time (especially that I plan to put some mutexes on memory operations).
Sorry, I haven't caught on: can you explain further?
Re: Releasing finished task's stack
Posted: Fri Mar 30, 2012 12:11 pm
by xenos
In my kernel a terminated process is simply marked as "finished" and the scheduler picks a new process. It then performs the actual task switch (which is actually a stack switch). After the stack switch the scheduler checks the status of the previous process. When it turns out to be finished, the process is deleted. At this time its stack is already out of use.
This is a typical problem of the "one kernel stack per process" approach. There are other approaches like "one kernel thread per CPU" where there is no need to delete (kernel) stacks at all. We once had a nice discussion on these approaches:
http://forum.osdev.org/viewtopic.php?f=1&t=24716
Re: Releasing finished task's stack
Posted: Fri Mar 30, 2012 2:01 pm
by invalid
gravaera wrote:ydoom wrote:...it would be problematic to have more than one task finishing at the same time (especially that I plan to put some mutexes on memory operations).
Sorry, I haven't caught on: can you explain further?
Consider following scenario:
- task1 ended, and is about to release the stack
- esp is set to some temporary stack
- attempt to release original stack results in waiting (due to mutex)
- task2 ends etc.
Temporary stack then gets corrupted. Ok, it could be avoided by making memory operations running exclusively, but that's even worse IMHO.
XenOS wrote:http://forum.osdev.org/viewtopic.php?f=1&t=24716
Reading...
-
So it appears that it's hard to avoid some "inertia" - the data does not fully reflect the real state at all times until "someone flips that switch later". What would be other things adding the "inertia"?
Re: Releasing finished task's stack
Posted: Fri Mar 30, 2012 2:58 pm
by piranha
I handle it fairly simply (global stack per processor for releasing tasks sounds like a bad solution to me).
1) Task calls exit(), exit() frees up as many resources as the task can possibly free (this is the entire address space [the task is running in kernel mode, and so is using a kernel mode stack], file handles, other stuff). Task then sets a flag to be reaped.
2) The kernel task gets scheduled, comes around, sees that this task needs to be deleted and does so. It also frees that tasks kernel mode stack and task struct.
Simple.
-JL
Re: Releasing finished task's stack
Posted: Fri Mar 30, 2012 4:07 pm
by bluemoon
Sorry to hijack the thread but I'm curious.
If rely on exit() to clean stuff, to force kill other process do you just signal it and let it suicide?
Would it be problem if the signal queue got DoS or application has dead-locked in the sig_handler?
Linux seems suffer from this signal-suicide problem as sometime force kill may fail.
Re: Releasing finished task's stack
Posted: Fri Mar 30, 2012 5:46 pm
by invalid
bluemoon wrote:Sorry to hijack the thread but I'm curious.
No problem

Thanks for all suggestions, every bit of information around the concept fits somewhere sooner or later!
Re: Releasing finished task's stack
Posted: Sat Mar 31, 2012 1:36 am
by Rudster816
ydoom wrote:Hi, here's another newbie question.
I have multitasking implemented by timer interrupt calling task_switch, which basically switches to next task's stack.
When a task is launched, I allocate some memory for it's stack and fill it with values to be popped by popad/iret.
When a task finishes, it's resources are released, and the task is marked "finished" (not to be reentered). However, I don't know how to release the stack space - the timer interrupt still needs to push return address somewhere!
So I ended up with "Hades" task, which does the job of releasing those dead stacks. But I consider this solution to be a funny workaround.
Can you give me a hint about what I can do smarter, or point me to some further readings?
Thanks in advance.
You don't have to use the call\ret instructions. You could easily store the return address in a register that your calling convention requires be preserved by the callee (e.g. rbx) then just jump to that register to return after releasing the stack.
That being said, I think it's much better to lazily release resources. When a process\thread needs to die, just add it to a list that the kernel will use to release all it's resources in a task that runs a lower priority than any user task. Since the vast majority of the time these resources won't be needed before they are free'd with spare CPU time, you'll speed things up by being able to reschedule that timeslice immediately after adding it to the list, without actually having to go trough with the process of actually releasing everything. If you ever run out of resources, you can just force the reaper to run for a timeslice to attempt to free some up. You could also dynamically raise the reapers priority above that of all user tasks if it contains more than X amount of resources. It could then move itself back down after dropping below that threshold.
Re: Releasing finished task's stack
Posted: Sat Mar 31, 2012 2:12 am
by piranha
If rely on exit() to clean stuff, to force kill other process do you just signal it and let it suicide?
I handle SIGKILL in a special way in relation to other signals, if a task receives it (and it was sent in a legal way), then the kernel just calls kill_task immediately. That way the state is irrelevant.
-JL