2.5 pthread support for SEM_UNDO in semop()
PATCH 1/1 2.5 pthread support for SEM_UNDO in semop()
[PATCH] 2.5 pthread support for SEM_UNDO in semop()
I've included fixes to the patch I submitted last week, from Christoph Hellwig. In particular:
1. I've removed or replaced discussions of "processes" with references to "thread groups" on the lines of code modified by this patch.
2. I've introduced the new flag CLONE_SYSVIPC into sched.h, and used this instead of the collection of flags my previous patch used to determine whether SEM_UNDO state should be shared at __clone() time.
I've noticed that linux 2.5 has already introduced a new flag, CLONE_NEWNS. I've "reserved" space in the 2.4.17 patch for the expected new flag in the 2.5 kernel. I'm not sure this was the right thing to do. The hope is that the future version of glibc will work on both 2.4 and 2.5 kernels. How is this sort of thing supposed to be handled?
This latest change as the unfortunate aspect that the glibc linuxthreads library need to be changed to pass this new thread to __clone(). The soonest this changed library can be available is in 6 months, as part of glibc verison 2.3. I'll be submitting patches to the glibc mailing list.
I've also included mail a new test program, that doesn't use pthread_create(). Instead, it calls __clone() directly. This means the kernel can be tested without a new glibc.
------------------------ Explanation of Problem-------------------------
The Linux semop() System V semaphore SEM_UNDO should perform SEM_UNDO cleanup only when the entire task group exits, not when the pthread that did the semop() exits. Following is a brief explanation of the problem, followed by the test program, followed by a patch for linux 2.4.17.
The test program is pretty simple. The application creates several threads using pthread_create(). When one thread performs a semop() with the SEM_UNDO flag set, the change in sempaphore value performed by that semop should be "undone" only when the entire application exits, NOT when that thread exits. This would be consistent with the description on the semop(2) manual page, and also with the semop() implementation on many other UNIX implementations.
However, In the current Linux implementation, the semop is "undone" when the thread that performed the semop() exits. This is too soon. This behavior has created difficulty in porting to Linux some threaded applications that use semop().
In the current Linux implementation, each thread currently maintains its own private list of semundo structures, referenced by current->semundo. The bug is that the list of sem_undo structures for a thread group should be shared among all the threads in that thread group.
This patch adds a structure (sem_undohd) that is shared by all the threads in the threaded application, and controls access to the shared list of sem_undo structures. current->semundo references are replaced with current->semundohd references. I've tried to minimize the impact on tasks that are not using SEM_UNDO semop() operations, or that are not using pthread_create().
---------------------- Test Program Output -----------------------------------
Below is a test program output. The test program's output SHOULD look like:
Waiter, pid = 11490
Poster, pid = 11490, posting
Poster posted
Poster exiting
Waiter waiting, pid = 11490
Waiter done waiting
The Incorrect output on Linux is:
Waiter, pid = 712
Poster, pid = 713, posting
Poster posted
Poster exiting
Waiter waiting, pid = 712
------------------------Test Program using __clone()-------------------------
#include <stdio.h>
#include <unistd.h>
#include <sys/sem.h>
#include <errno.h>
#define CLONE_VM 0x00000100 #define CLONE_FS 0x00000200 #define CLONE_FILES 0x00000400 #define CLONE_SIGHAND 0x00000800 #define CLONE_SYSVIPC 0x00040000
#define KEY IPC_PRIVATE
#define NUMCLONES 2
int waiter_stack[1024];
int poster_stack[1024];
void *retval[NUMCLONES];
void * waiter(void *);
void * poster(void *);
struct sembuf Psembuf = {0, -1, SEM_UNDO};
struct sembuf Vsembuf = {0, 1, SEM_UNDO};
int sem_id;
char *semgeterr = "semget failed\n";
char *clone_waiter = "clone waiter\n";
char *clone_poster = "clone poster\n";
char *waiter_cloned = "waiter cloned\n";
char *poster_cloned = "poster cloned\n";
main()
{
int i, pid, rc;
/* Create the semaphore set */
sem_id = semget(KEY, 1, 0666 | IPC_CREAT);
if (sem_id < 0)
{
printf ("semget failed, errno = %d\n", errno);
exit (1);
}
/* Create the threads */
for (i=0; i<NUMCLONES; i++)
{
if (i == 0) {
rc = __clone(waiter, (void *)&waiter_stack[1023], CLONE_SYSVIPC,
(void *)0);
} else {
rc = __clone(poster, (void *)&poster_stack[1024], CLONE_SYSVIPC,
(void *)0);
}
}
/* Sleep long enough to see that the other threads do what they are supposed to do */
sleep(20);
exit(0);
}char *waiter_enters = "Waiter enters\n"; char *waiter_waiting = "waiter_waiting\n"; char *waiter_done_waiting = "waiter done waiting\n";
/* This task sleeps 10 seconds then waits on the semaphore. As long
as someone has posted on the semaphore, and no undo has taken place, the semop should complete and we'll print "Waiter done waiting." */
void *waiter(void * foo)
{
int pid;
pid = getpid();
printf ("Waiter, pid = %d\n", pid);
sleep(10);
printf("Waiter waiting, pid = %d\n", pid);
semop(sem_id, &Psembuf, 1);
printf("Waiter done waiting\n");
exit(0);
}/* This task immediately posts on the semaphore and then immediately
exits. If the *thread* exits, the undo should not happen, and the waiter thread which will start waiting on it in 10 seconds, should
still get it. */
char *poster_enters = "poster enters\n";
char *poster_posted = "poster posted\n";
char *poster_exiting = "poster exiting\n";
void * poster(void * foo)
{
int pid;
pid = getpid();
printf ("Poster, pid = %d, posting\n", pid);
semop(sem_id, &Vsembuf, 1);
printf ("Poster posted\n");
printf ("Poster exiting\n");
exit(0);
}---------------------2.5 patch ----------------------------------------
linux/include/linux/sched.h +2 -1
@@ -42,6 +42,7 @@#define CLONE_PARENT 0x00008000/* set if we want to have the same parent as the cloner */ #define CLONE_THREAD 0x00010000/* Same thread group? */ #define CLONE_NEWNS 0x00020000/* New namespace group? */ +#define CLONE_SYSVIPC 0x00040000/* share system V IPC semantics (SEM_UNDO) */ #define CLONE_SIGNAL (CLONE_SIGHAND | CLONE_THREAD)
@@ -372,7 +373,7 @@struct tty_struct *tty;/* NULL if no tty */ unsigned int locks;/* How many file locks are being held */ /* ipc stuff */ -struct sem_undo *semundo; +struct sem_undohd *semundohd;struct sem_queue *semsleeping;/* CPU-specific state of this task */ struct thread_struct thread;
linux/include/linux/sem.h +12 -0
@@ -121,6 +121,18 @@short * semadj;/* array of adjustments, one per semaphore */ }; +/* task group that are running POSIX style threads +* must share the same semundo list, in order to support SEMUNDO +* semantics for threads. The sem_undohd controls shared access to this +* list among all the tasks (threads) in that thread group. +*/ +struct sem_undohd { +atomic_t refcnt; +spinlock_t lock; +volatile unsigned long add_count; +struct sem_undo *proc_list; +}; +asmlinkage long sys_semget (key_t key,int nsems,int semflg);asmlinkage long sys_semop (int semid,struct sembuf *sops,unsigned nsops);asmlinkage long sys_semctl (int semid,int semnum,int cmd,union semun arg);
linux/ipc/sem.c +197 -30
346 lines not on this page · raw message
linux/ipc/util.c +11 -0
@@ -340,6 +340,17 @@* Dummy functions when SYSV IPC isn't configured */ +int copy_semundo (unsigned long clone_flags,struct task_struct *tsk) +{ +return 0 ; +} + +void exit_semundo (struct task_struct *tsk) +{ +return ; +} + +void sem_exit (void ) {return ;
linux/kernel/fork.c +8 -2
@@ -27,6 +27,9 @@#include <asm/uaccess.h> #include <asm/mmu_context.h> +extern int copy_semundo (unsigned long clone_flags,struct task_struct *tsk); +extern void exit_semundo (struct task_struct *tsk); +/* The idle threads do not count.. */ int nr_threads;
@@ -662,8 +665,10 @@ retval = -ENOMEM ;/* copy all the process information */ -if (copy_files (clone_flags, p)) +if (copy_semundo (clone_flags, p)) goto bad_fork_cleanup; +if (copy_files (clone_flags, p)) + goto bad_fork_cleanup_semundo;if (copy_fs (clone_flags, p)) goto bad_fork_cleanup_files;if (copy_sighand (clone_flags, p))
@@ -675,7 +680,6 @@ retval =copy_thread (0 , clone_flags, stack_start, stack_size, p, regs);if (retval) goto bad_fork_cleanup_namespace; - p->semundo =NULL ;/* Our parent execution domain becomes current domain These must match for thread signalling to apply */
@@ -773,6 +777,8 @@exit_fs (p);/* blocking */ bad_fork_cleanup_files:exit_files (p);/* blocking */ +bad_fork_cleanup_semundo: +exit_semundo (p); bad_fork_cleanup:put_exec_domain (p->exec_domain);if (p->binfmt && p->binfmt->module)