Releasing finished task's stack
Releasing finished task's stack
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.
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
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.
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.
- Owen
- Member

- Posts: 1700
- Joined: Fri Jun 13, 2008 3:21 pm
- Location: Cambridge, United Kingdom
- Contact:
Re: Releasing finished task's stack
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.
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
Hmmm... Maybe having a "janitor" hanging around is not that bad idea after all...
Thanks!
Thanks!
Re: Releasing finished task's stack
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.
That is a feature can be extended with such janitor, or delayed clean up logic.
- gravaera
- Member

- Posts: 737
- Joined: Tue Jun 02, 2009 4:35 pm
- Location: Supporting the cause: Use \tabs to indent code. NOT \x20 spaces.
Re: Releasing finished task's stack
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
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
17:56 < sortie> Paging is called paging because you need to draw it on pages in your notebook to succeed at it.
Re: Releasing finished task's 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).gravaera wrote:you can switch to the CPU's sleep stack
OMG, writing a kernel is so much fun and an adventure full of open possibilities
- gravaera
- Member

- Posts: 737
- Joined: Tue Jun 02, 2009 4:35 pm
- Location: Supporting the cause: Use \tabs to indent code. NOT \x20 spaces.
Re: Releasing finished task's stack
Sorry, I haven't caught on: can you explain further?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).
17:56 < sortie> Paging is called paging because you need to draw it on pages in your notebook to succeed at it.
- xenos
- Member

- Posts: 1144
- Joined: Thu Aug 11, 2005 11:00 pm
- Libera.chat IRC: xenos1984
- Location: Tartu, Estonia
- GitHub: https://github.com/xenos1984
- Contact:
Re: Releasing finished task's stack
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
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
Consider following scenario:gravaera wrote:Sorry, I haven't caught on: can you explain further?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).
- 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.
Reading...XenOS wrote:http://forum.osdev.org/viewtopic.php?f=1&t=24716
-
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"?
- piranha
- Member

- Posts: 1391
- Joined: Thu Dec 21, 2006 7:42 pm
- Location: Unknown. Momentum is pretty certain, however.
- Contact:
Re: Releasing finished task's stack
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
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
SeaOS: Adding VT-x, networking, and ARM support
dbittman on IRC, @danielbittman on twitter
https://dbittman.github.io
dbittman on IRC, @danielbittman on twitter
https://dbittman.github.io
Re: Releasing finished task's stack
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.
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
No problembluemoon wrote:Sorry to hijack the thread but I'm curious.
-
Rudster816
- Member

- Posts: 141
- Joined: Thu Jun 17, 2010 2:36 am
Re: Releasing finished task's stack
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.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.
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.
- piranha
- Member

- Posts: 1391
- Joined: Thu Dec 21, 2006 7:42 pm
- Location: Unknown. Momentum is pretty certain, however.
- Contact:
Re: Releasing finished task's stack
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.If rely on exit() to clean stuff, to force kill other process do you just signal it and let it suicide?
-JL
SeaOS: Adding VT-x, networking, and ARM support
dbittman on IRC, @danielbittman on twitter
https://dbittman.github.io
dbittman on IRC, @danielbittman on twitter
https://dbittman.github.io
