mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [patch] exit_free(), 2.5.31-A0
@ 2002-08-13 15:25 Ingo Molnar
  2002-08-13 15:36 ` Linus Torvalds
  0 siblings, 1 reply; 74+ messages in thread
From: Ingo Molnar @ 2002-08-13 15:25 UTC (permalink / raw)
  To: Linus Torvalds; +Cc: linux-kernel


the attached patch implements a new syscall, exit_free():

	exit_free(error_code, addr, val);

this syscall is used as a performance optimization in glibc's threading
library.

upon exiting of child threads there is an ugly race condition that must be
solved: the freeing of the child stack. Old pthreads used to set up a
trampoline, jump to it, munmap() the child stack and call sys_exit(). It
does not have to be detailed how much this hurts performance: the
munmap(), besides being slow for such a lightweight thing as thread-exit,
also flushes the TLB, possibly across CPUs. Also, locality of reference of
thread stacks is lost as well.

the new thread library solves this performance by introducing a 'thread
stack cache' - a simple list of thread stacks. (with some more details to
handle different stack sizes.) The problem with the stack cache is that
there's a race condition: who releases it? We must not free the thread
stack up until the very last instruction the current thread executes,
because a signal handler might arrive and might use an alrady freed stack.  
Other threads might pick this stack and overwrite it ... Disabling signals
upon thread-exit adds a syscall overhead, but it still doesnt solve the
fundamental problem of 'who frees the stack'. A global semaphore for a
trampoline stack would have to be introduced to be able to free the stack
safely - a messy, slow and unscalable solution. Or queueing the stack to a
helper thread is equally messy.

again the kernel can give the threading library a helping hand to solve
this catch-22 problem, surprisingly easily in this case as well.  
exit_free() simply writes a user-provided word back to userspace. At that
point the user stack is not used anymore (nor will it ever be - sys_exit()
cannot fail), so freeing it is appropriate. The actual way glibc utilizes
exit_free() is that there's a "is this stack free" flag in the thread
stack control structure, and exit_free() sets this to '1'. So upon
thread-exit the thread frees the stack and puts it into the stack cache -
but other threads will skip over it because the 'free' flag is still 0.

with this syscall it was possible to implement single-syscall thread exit
in glibc. (well, actually not yet, the next patch i send will enable this
fully by solving the "who does the waitpid()?" problem.)

	Ingo

--- linux/arch/i386/kernel/entry.S.orig	Tue Aug 13 17:13:30 2002
+++ linux/arch/i386/kernel/entry.S	Tue Aug 13 17:13:42 2002
@@ -755,6 +755,7 @@
 	.long sys_set_thread_area
 	.long sys_get_thread_area
 	.long sys_clone_startup	/* 245 */
+	.long sys_exit_free
 
 	.rept NR_syscalls-(.-sys_call_table)/4
 		.long sys_ni_syscall
--- linux/include/asm-i386/unistd.h.orig	Tue Aug 13 17:13:00 2002
+++ linux/include/asm-i386/unistd.h	Tue Aug 13 17:13:17 2002
@@ -250,6 +250,7 @@
 #define __NR_set_thread_area	243
 #define __NR_get_thread_area	244
 #define __NR_clone_startup	245
+#define __NR_exit_free		246
 
 /* user-visible error numbers are in the range -1 - -124: see <asm-i386/errno.h> */
 
--- linux/kernel/exit.c.orig	Tue Aug 13 17:12:06 2002
+++ linux/kernel/exit.c	Tue Aug 13 17:12:45 2002
@@ -586,6 +586,12 @@
 	do_exit((error_code&0xff)<<8);
 }
 
+asmlinkage long sys_exit_free(int error_code, unsigned long *addr, unsigned long val)
+{
+	put_user(val, addr);
+	do_exit((error_code&0xff)<<8);
+}
+
 asmlinkage long sys_wait4(pid_t pid,unsigned int * stat_addr, int options, struct rusage * ru)
 {
 	int flag, retval;


^ permalink raw reply	[flat|nested] 74+ messages in thread

end of thread, other threads:[~2002-08-26 19:26 UTC | newest]

Thread overview: 74+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-08-13 15:25 [patch] exit_free(), 2.5.31-A0 Ingo Molnar
2002-08-13 15:36 ` Linus Torvalds
2002-08-13 17:50   ` Ingo Molnar
2002-08-13 18:00     ` Linus Torvalds
2002-08-13 18:03       ` Ingo Molnar
2002-08-13 18:17         ` Linus Torvalds
2002-08-13 18:22           ` Ingo Molnar
2002-08-15 20:51           ` mgross
2002-08-13 18:08       ` Ingo Molnar
2002-08-13 18:23         ` Linus Torvalds
2002-08-13 18:52           ` Linus Torvalds
2002-08-13 19:16             ` Ingo Molnar
2002-08-13 19:43               ` Linus Torvalds
2002-08-13 19:55                 ` Ingo Molnar
2002-08-13 20:06                   ` Ingo Molnar
2002-08-13 20:42                     ` Linus Torvalds
2002-08-13 21:17                       ` [patch] clone-detached-2.5.31-B0 Ingo Molnar
2002-08-13 21:23                         ` [patch] user-vm-unlock-2.5.31-A2 Ingo Molnar
2002-08-15  4:03                           ` Jamie Lokier
2002-08-15  6:37                             ` Ingo Molnar
2002-08-15 10:38                               ` Ingo Molnar
2002-08-15 13:24                                 ` Ingo Molnar
2002-08-15 18:02                                 ` Linus Torvalds
2002-08-15 22:26                                   ` Ingo Molnar
2002-08-15 23:01                                     ` Ingo Molnar
2002-08-15 23:45                                     ` Linus Torvalds
2002-08-15 23:46                                       ` Ingo Molnar
2002-08-15 23:58                                         ` Linus Torvalds
2002-08-16  0:00                                           ` Ingo Molnar
2002-08-15 23:47                                       ` Ingo Molnar
2002-08-15 23:53                                         ` Linus Torvalds
2002-08-15 23:58                                           ` Ingo Molnar
2002-08-16  0:06                                             ` Linus Torvalds
2002-08-16  0:11                                               ` Ingo Molnar
2002-08-16  0:14                                                 ` Ingo Molnar
2002-08-16  1:06                                                 ` Linus Torvalds
2002-08-16  1:14                                                   ` Linus Torvalds
2002-08-16  9:51                                                     ` Ingo Molnar
2002-08-16  9:49                                                   ` Ingo Molnar
2002-08-16 16:54                                                     ` Linus Torvalds
2002-08-15  6:45                             ` Ingo Molnar
2002-08-15 10:31                               ` Jamie Lokier
2002-08-15 11:30                                 ` Alex Riesen
2002-08-15 13:03                                 ` Ingo Molnar
2002-08-15 21:27                                   ` Jamie Lokier
2002-08-15 22:02                                     ` Ingo Molnar
2002-08-16  3:09                                       ` Jamie Lokier
2002-08-16 10:02                                         ` Ingo Molnar
2002-08-16 12:34                                           ` Jamie Lokier
2002-08-16 13:18                                             ` Ingo Molnar
2002-08-16 14:19                                               ` CLONE_DETACHED and exit notification (was user-vm-unlock-2.5.31-A2) Jamie Lokier
2002-08-16 14:48                                                 ` Ingo Molnar
2002-08-16 16:30                                                   ` Jamie Lokier
2002-08-16 17:12                                                     ` Ingo Molnar
2002-08-16 17:22                                                     ` Linus Torvalds
2002-08-16 17:28                                                       ` Ingo Molnar
2002-08-16 17:34                                                         ` Linus Torvalds
2002-08-16 17:38                                                           ` Ingo Molnar
2002-08-16 17:47                                                           ` Ingo Molnar
2002-08-17 18:48                                                           ` Ingo Molnar
2002-08-17 19:06                                                             ` Christoph Hellwig
2002-08-17 22:18                                                             ` Jamie Lokier
2002-08-15 23:29                                     ` [patch] user-vm-unlock-2.5.31-A2 Jamie Lokier
2002-08-15  6:49                             ` Ingo Molnar
2002-08-15 15:16                         ` [patch] clone-detached-2.5.31-B0 Ingo Molnar
2002-08-26 12:35                           ` Pavel Machek
2002-08-26 19:28                             ` Robert Love
2002-08-26 19:33                               ` Ingo Molnar
2002-08-13 19:31             ` [patch] exit_free(), 2.5.31-A0 Ingo Molnar
2002-08-13 19:50               ` Linus Torvalds
2002-08-13 20:47                 ` [patch] user-vm-unlock-2.5.31-A1 Ingo Molnar
2002-08-13 20:53                   ` Ingo Molnar
2002-08-13 18:14   ` [patch] exit_free(), 2.5.31-A0 Ingo Molnar
2002-08-13 18:24     ` Linus Torvalds

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®