PATCH(?): linux-2.4.4-pre2: fork should run child first
Subsystems (3)
I remember sometime in the late 80's a fellow at UniSoft
named Don whose last name escapes me just now told me about a paper presented at a Usenix symposium that had some measurements that purported that copy-on-write was a performance lose and better performance would be achieve by having fork() just copy all of the writable pages of the parent process.
It turned out that the particular unix-like system on which
these benchmarks were taken had a version of fork that did not run the child first. As it was explained to me then, most of the time, the child process from a fork will do just a few things and then do an exec(), releasing its copy-on-write references to the parent's pages, and that is the big win of copy-on-write for fork() in practice. This oversight was considered a big embarassment for the operating system in question, so I won't name it here.
Guess why you're seeing this email. That's right. Linux-2.4.3'sfork() does not run the child first. Consequently, the parent will probably generate unnecessary copy-on-write page copies until it burns through its remaining clock ticks (any COW's that the child causes will basically happen no matter what the order of execution is) or calls wait() (and while the wait is blocking, the parent's CPU priority will decay as the scheduler periodically recalculates process priorities, so that bit of dynamic priority has probably not been allocated where the user will be able to use it, if we want to look at "fairness" in such detail).
I suppose that running the child first also has a minor
advantage for clone() in that it should make programs that spawn lots of threads to do little bits of work behave better on machines with a small number of processors, since the threads that do so little work that they accomplish they finish within their time slice will not pile up before they have a chance to run. So, rather than give the parent's CPU priority to the child only if CLONE_VFORK is not set, I have decided to do a bit of machete surgery and have the child always inherit all of the parent's CPU priority all of the time. It simplifies the code and probably saves a few clock cycles (and before you say that this will cost a context switch, consider that the child will almost always run at least one time slice anyhow).
I have attached the patch below. I have also adjusted the
comment describing the code. Please let me know if this hand waving explanation is sufficient. I'm trying to be lazy on not do a measurement project to justify this relatively simple change. However, I do know, from a simple test program ("printf ("%d", fork());"), that this patch has the intended effect of running the child first.
Signature · 4 lines
Adam J. Richter __ ______________ 4880 Stevens Creek Blvd, Suite 104 [email protected] \ / San Jose, California 95129-1034 +1 408 261-6630 | g g d r a s i l United States of America fax +1 408 261-6631 "Free Software For The Rest Of Us."
kernel/fork.c +10 -8
@@ -666,15 +666,17 @@ p->pdeath_signal =0 ;/* -* "share" dynamic priority between parent and child, thus the -* total amount of dynamic priorities in the system doesnt change, -* more scheduling fairness. This is only important in the first -* timeslice, on the long run the scheduling behaviour is unchanged. +* Give the parent's dynamic priority entirely to the child. The +* total amount of dynamic priorities in the system doesn't change +* (more scheduling fairness), but the child will run first, which +* is especially useful in avoiding a lot of copy-on-write faults +* if the child for a fork() just wants to do a few simple things +* and then exec(). This is only important in the first timeslice. +* In the long run, the scheduling behavior is unchanged. */ - p->counter = (current->counter +1 ) >>1 ; - current->counter >>=1 ; -if (!current->counter) - current->need_resched =1 ; + p->counter = current->counter; + current->counter =0 ; + current->need_resched =1 ;/* * Ok, add it to the run-queues and make it
"Adam J. Richter" <[email protected]> said:
[...]
Quoted 4 lines
I suppose that running the child first also has a minor
advantage forclone()in that it should make programs that spawn lots of threads to do little bits of work behave better on machines with a small number of processors, since the threads that do so little work that
Quoted 8 lines
they accomplish they finish within their time slice will not pile up before they have a chance to run. So, rather than give the parent's CPU priority to the child only if
CLONE_VFORKis not set, I have decided to do a bit of machete surgery and have the child always inherit all of the parent's CPU priority all of the time. It simplifies the code and probably saves a few clock cycles (and before you say that this will cost a context switch, consider that the child will almost always run at least one time slice anyhow).
And opens the system up to DoS attacks: You can't have a process fork(2) at will and so increase its (aggregate) CPU priority.
Signature · 4 lines
Dr. Horst H. von Brand mailto:[email protected] Departamento de Informatica Fono: +56 32 654431 Universidad Tecnica Federico Santa Maria +56 32 654239 Casilla 110-V, Valparaiso, Chile Fax: +56 32 797513
Quoted 5 lines
Quoted 3 lines · Adam J. Richter
I suppose that running the child first also has a minor
advantage forclone()in that it should make programs that spawn lots of threads to do little bits of work behave better on machines with a
There is another issue with this proposition. I have begun to write (free time, slow pace) an userland sandbox which allows me to prevent a process and its childs to perform some given actions, like removing files or writing in some directories. This works by ptrace-ing the process, modifying system calls and faking return values. It also needs, obviously, to ptrace-attach childs of the sandboxed process. When the parent in a fork runs first, the sandbox program has time to ptrace-attach the child before it does any system call. Obviously, if the child runs first, this is no longer the case.
If it is decided that the child should run first in a fork, there should be a way to reliably ptrace-attach it before it can do anything.
By the way, I tried to solve this problem in my sandbox program by masqerading any fork call into a clone system call with the flag CLONE_PTRACE. I had hoped that the child would in this way start already ptraced. However, this didn't work as expected. The child did start in a ptraced state, but the owner of the trace was its parent (which issued the fork), and not my sandbox process which was ptracing this parent. I find that this behaviour is really weird and useless. I could simulate the current behaviour simply by calling ptrace(TRACE_ME,..) in the child. What is the real use of the CLONE_PTRACE flag ?
Éric Brunet
Brunet <[email protected]>:
Quoted 19 lines
Quoted 5 lines
Quoted 3 lines · Adam J. Richter
I suppose that running the child first also has a minor
advantage forclone()in that it should make programs that spawn lots of threads to do little bits of work behave better on machines with aThere is another issue with this proposition. I have begun to write (free time, slow pace) an userland sandbox which allows me to prevent a process and its childs to perform some given actions, like removing files or writing in some directories. This works by ptrace-ing the process, modifying system calls and faking return values. It also needs, obviously, to ptrace-attach childs of the sandboxed process. When the parent in a fork runs first, the sandbox program has time to ptrace-attach the child before it does any system call. Obviously, if the child runs first, this is no longer the case.
If it is decided that the child should run first in a fork, there should be a way to reliably ptrace-attach it before it can do anything.
Quoted 9 lines
By the way, I tried to solve this problem in my sandbox program by masqerading any fork call into a clone system call with the flag
CLONE_PTRACE. I had hoped that the child would in this way start already ptraced. However, this didn't work as expected. The child did start in a ptraced state, but the owner of the trace was its parent (which issued the fork), and not my sandbox process which was ptracing this parent. I find that this behaviour is really weird and useless. I could simulate the current behaviour simply by callingptrace(TRACE_ME,..)in the child. What is the real use of theCLONE_PTRACEflag ?
I believe it allows the debugger to start the process to be debugged.
-------------------------------------------------------------------------
Jesse I Pollard, II
Email: [email protected]
Any opinions expressed are solely my own.
Quoted 3 lines · you
I believe it allows the debugger to start the process to be debugged.
Well, the debugger simply needs to do something like
pid_t child = fork();
if (child == 0) {
ptrace(PTRACE_TRACEME,0,0,0);
execve(the_debugged_process,args,env);
}It is more portable, more traditionnal and works very well.
Éric Brunet
"Adam J. Richter" <[email protected]> writes:
[...]
Quoted 11 lines
It turned out that the particular unix-like system on which
these benchmarks were taken had a version of fork that did not run the child first. As it was explained to me then, most of the time, the child process from a fork will do just a few things and then do anexec(), releasing its copy-on-write references to the parent's pages, and that is the big win of copy-on-write forfork()in practice. This oversight was considered a big embarassment for the operating system in question, so I won't name it here.Guess why you're seeing this email. That's right. Linux-2.4.3's
fork()does not run the child first.
Not always, if I understand correctly. Setting to always is putting policy in kernel in a small way. If an app wants to fork and exec, it should use *vfork* and exec, which is a performance win across many OSs because the COW mappings don't even have to be set up, IIRC.
[...]
Signature · 1 line
http://www.penguinpowered.com/~vii
Quoted 4 lines · Adam J. Richter
I have attached the patch below. I have also adjusted the
comment describing the code. Please let me know if this hand waving explanation is sufficient. I'm trying to be lazy on not do a measurement project to justify this relatively simple change.
It could upset programs which use threads to handle relatively IO poor things (like, waiting on disk IO in a thread, like glibc does to fake async file IO).
Also, have you managed to find a real difference with this?
If it turns out to be beneficial to run the child first (you can measure this), why not leave everything the same as it is now but have do_fork() "switch threads" internally ?
(since everything is COW-ed, it wouldn't even need to do a real thread switch, this should be fairly easy)
regards,
Rik
--
Virtual memory is like a game you can't win; However, without VM there's truly nothing to lose...
http://www.surriel.com/
Quoted 2 lines · Rik van Riel
Also, have you managed to find a real difference with this?
It actually makes a noticeable difference on lmbench, so I think adam is 100% right.
Quoted 3 lines
If it turns out to be beneficial to run the child first (you can measure this), why not leave everything the same as it is now but have
do_fork()"switch threads" internally ?
Probably doesn't much matter. We've invalidated the TLB anyway due to the page table copy, so the cost of switching the MM is not all that noticeable.
Linus
Quoted 7 lines · Linus Torvalds
Quoted 2 lines · Rik van Riel
Also, have you managed to find a real difference with this?
It actually makes a noticeable difference on lmbench, so I think adam is 100% right.
Quoted 7 lines
Quoted 3 lines
If it turns out to be beneficial to run the child first (you can measure this), why not leave everything the same as it is now but have
do_fork()"switch threads" internally ?Probably doesn't much matter. We've invalidated the TLB anyway due to the page table copy, so the cost of switching the MM is not all that noticeable.
And we don't even have to physically switch MM, we could simply fake stuff by updating pointers in the parent MM instead of the child so by the time we exit do_fork() we're in the child...
regards,
Rik
--
Virtual memory is like a game you can't win; However, without VM there's truly nothing to lose...
http://www.surriel.com/