Re: [PATCH v13 01/17] libbb: mask xvfork to xfork on MMU targets
On Mon, Nov 17, 2025 at 12:09 AM Nadav Tasher wrote: > > Using xfork() instead of xvfork() on MMU targets improves > security and stability. > Memory efficiency differences are negligable since most > kernels implement fork() with CoW. In fact, you should go into opposite direction: whenever it's not incurring difficulties, you should use vfork(). fork() is much slower. Especially if forking a process with large memory footprint. It can take something like 50-100 milliseconds even on lightly loaded machine. Yes, CoW means that the dirtied pages of the process do not need to be copied, but the whole kernel-side paging structure needs to be. If you examine in the kernel what fork() does with the paging structures, you'll realize that fork() essentially flushes the entire TLB, and the amount of newly allocated and updated pages in new paging data structures (at least a dozen pages) is large enough to flush today's CPUs L1 data cache (<=64 kbytes). In comparison, vfork() does not touch paging structures at all, it barely needs any kernel-side allocations: essentially, it only allocates new task_struct and associated small structures. ___ busybox mailing list [email protected] https://lists.busybox.net/mailman/listinfo/busybox
[PATCH v13 01/17] libbb: mask xvfork to xfork on MMU targets
Using xfork() instead of xvfork() on MMU targets improves
security and stability.
Memory efficiency differences are negligable since most
kernels implement fork() with CoW.
Signed-off-by: Nadav Tasher
---
include/libbb.h | 8 +++-
1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/include/libbb.h b/include/libbb.h
index 4d6193795..17ca36760 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -1247,6 +1247,7 @@ int BB_EXECVP(const char *file, char *const argv[])
FAST_FUNC;
#endif
void BB_EXECVP_or_die(char **argv) NORETURN FAST_FUNC;
+#if !BB_MMU
/* xvfork() can't be a _function_, return after vfork in child mangles stack
* in the parent. It must be a macro. */
#define xvfork() \
@@ -1256,8 +1257,13 @@ void BB_EXECVP_or_die(char **argv) NORETURN FAST_FUNC;
bb_simple_perror_msg_and_die("vfork"); \
bb__xvfork_pid; \
})
-#if BB_MMU
+#else
pid_t xfork(void) FAST_FUNC;
+
+/* fork() is compliant with vfork().
+ * using fork instead of vfork on MMU-enabled targets makes the entire program
+ * a little safer. */
+#define xvfork() xfork()
#endif
void xvfork_parent_waits_and_exits(void) FAST_FUNC;
--
2.43.0
___
busybox mailing list
[email protected]
https://lists.busybox.net/mailman/listinfo/busybox
