Objlab
← News
Linguaggi e compilatori

Andare oltre fork() + exec()

Sintesi redazionale: URL dell'articolo: https://lwn.net/SubscriberLink/1076018/16f01bbbb8e0d1f0/ URL dei commenti: https://news.ycombinator.com/item?id=48425528 Punti: 68 # Commenti: 47. Fonte originale: https://lwn.net/SubscriberLink/1076018/16f01bbbb8e0d1f0/

<p># Moving beyond fork() + exec()</p><p>Since the earliest days of Unix, two of the core process-oriented system calls have beenThis article brought to you by LWN subscribersSubscribers to LWN.net made this article — and everything that surrounds it — possible. If you appreciate our content, please buy a subscription and make the next set of articles possible.</p><p>`fork()`, which creates a child process as a copy of the parent, and</p><p>`exec()`, which runs a new program in the place of the current one. In Linux kernels, those system calls are better known as</p><p>`clone()`and</p><p>`execve()`, but the core functionality remains the same. While there is elegance to this process-creation model, there are shortcomings as well. A recent proposal from Li Chen to add &quot;spawn templates&quot; to the kernel will not be accepted in its current form, but it may point the way toward a new process-creation primitive in the future.</p><p>`fork()` is a relatively expensive system call; it must copy the<br>entire process state (including memory) for the child process. Many<br>optimizations have been made over the years, but a fork is still a<br>fundamentally costly operation. To make things worse, a `fork()`<br>call is often immediately followed by an `exec()`, which will<br>discard all of that memory that was so carefully copied for the child.<br>Attempts (such as `vfork()`)<br>have been made over the years to optimize for this case, but the pattern<br>still is more expensive than it could be.</p><p>#### Spawn templates</p><p>Chen&#x27;s patch set takes an interesting approach to optimize the<br>`fork()` and `exec()` pattern. It is focused on applications<br>that repeatedly launch processes running the same executable; imagine, for<br>example, a program that must run Git repeatedly to obtain information about<br>the contents of a repository. In such cases, the program could establish a<br>template to accelerate those invocations, spreading the setup cost across<br>multiple operations. This template would be created with the<br>`spawn_template_create()` system call:</p><p>struct spawn_template_create_args { __aligned_u64 flags; __s32 execfd; __u32 exec_flags; __aligned_u64 filename; /* Some fields elided */ }; int spawn_template_create(struct spawn_template_create_args *args, size_t args_size);</p><p>This call will return a file descriptor representing a template for the<br>executable file, which can be specified as either a file descriptor<br>(`execfd`) or an absolute path (`filename`), but not both.<br>To create the template, the kernel will open the indicated file and cache a<br>bunch of information that will allow a process to run that file more<br>quickly in the future.</p><p>The application in question may run a given executable many times, but each invocation is different in a number of ways. The details of a specific invocation must be placed into an instance of this structure:</p><p>struct spawn_template_spawn_args { __aligned_u64 flags; __aligned_u64 pidfd; __aligned_u64 argv; __aligned_u64 envp; __aligned_u64 actions; __aligned_u64 actions_len; __aligned_u64 reserved[4]; };</p><p>The `argv` field is a pointer to the argument list to be passed to<br>the program, while `envp` points to its environment. Changes to<br>file descriptors and signal handling, instead, are passed through<br>`actions`, which is a pointer to an array of:</p><p>struct spawn_template_action { __u32 type; __u32 flags; __s32 fd; __s32 newfd; __aligned_u64 arg; };</p><p>If, for example, file descriptor four should be closed in the child, the<br>associated `spawn_template_action` structure would have<br>`type` set to `SPAWN_TEMPLATE_ACTION_CLOSE` and `fd`<br>set to four. Other actions exist for duplicating file descriptors, opening<br>files, changing the working directory, and changing signal handling.</p><p>Once the `spawn_template_spawn_args` structure has been filled in,<br>the new process can be run with:</p><p>int spawn_template_spawn(int template_fd, struct spawn_template_spawn_args *args, int args_size);</p><p>Internally, this system call follows something close to the normal<br>`fork()`/`exec()` path. Chen is careful to point out that<br>all of the normal checks applied when executing a new file remain in place.<br>But the cached information in the template makes the whole process faster<br>than it was before.<br>How much faster? Benchmark results provided in the cover letter show an<br>improvement of about 2%, which may not seem like a lot, but it may<br>make a difference for applications that fit the expected pattern.</p><p>#### Toward `posix_spawn()`</p><p>The most detailed review of this work was posted<br>by Mateusz Guzik, who said: &quot;This problem is dear to my heart and I<br>have been pondering it on and off for some time now. The entire fork + exec<br>idiom is terrible and needs to be retired</p><p>&quot;. He pointed out that the<br>focus of the patch set was a bit strange in that it left the<br>`fork()` part of the problem untouched. That is where most of the<br>cost lies, he said, so optimization efforts should seek to remove it from<br>the picture. Rather than copying the current process, &quot;creating a<br>pristine process is the way to go</p><p>&quot;.</p><p>Christian Brauner was<br>favorable toward the goal, saying: &quot;The idea of having a builder api<br>for exec isn&#x27;t all that crazy</p><p>&quot;. His suggestion, though, was that a new<br>API should be built on top of the existing pidfd abstraction. Without getting into any<br>degree of detail, he said that the right approach would be to create an<br>option to `pidfd_open()`<br>to create an empty process. A series of calls to a new<br>`pidfd_config()` system call would then configure this new process<br>as desired, setting up its environment, image to execute, and more.<br>`pidfd_config()` would thus be analogous to `fsconfig()`.</p><p>An important objective for a new interface, Brauner said, would be the<br>ability to support an implementation of `posix_spawn()`<br>in user space. `posix_spawn()` is well suited as a replacement for<br>the `fork()`/`exec()` pattern; developers would likely<br>welcome a native implementation that isn&#x27;t (unlike the current<br>implementation) hiding `fork()` and `exec()` under the covers.<br>Chen agreed<br>that the API as broadly sketched out by Brauner seemed better, and said<br>that future work would be in that direction. So there will be no spawn<br>templates in the Linux kernel but, if Chen&#x27;s future work comes to fruition,<br>Linux may finally gain a proper `posix_spawn()` implementation instead.</p><p>| Index entries for this article | |<br>|---|---|<br>| Kernel | System calls/clone() |<br>| Kernel | System calls/execve() |</p>