Skip to contentSkip to conversation

heavy handed exit() in latest BK

DISCUSSION Ingo Molnar 4 participants 8 messages Feb 9, 2003 – Feb 10, 2003 1 message not in the archive Scheduler

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

Roland,

there are two bugs in the signal code still:

- a read_lock(&tasklist_lock) is missing around the group_send_sig_info() in send_sig_info().

- session-IDs and group-IDs are set outside the tasklist lock. This causes breakage in the USB code. The correct fix is to do this:

void __set_special_pids(pid_t session, pid_t pgrp)

{
        struct task_struct *curr = current;

        if (curr->session != session) {
                detach_pid(curr, PIDTYPE_SID);
                curr->session = session;
                attach_pid(curr, PIDTYPE_SID, session);
        }
        if (curr->pgrp != pgrp) {
                detach_pid(curr, PIDTYPE_PGID);
                curr->pgrp = pgrp;
                attach_pid(curr, PIDTYPE_PGID, pgrp);
        }
}

void set_special_pids(pid_t session, pid_t pgrp)

{
        write_lock_irq(&tasklist_lock);
        __set_special_pids(session, pgrp);
        write_unlock_irq(&tasklist_lock);
}

and then update all places that do:

- current->session = 1;
- current->pgrp = 1;

to:

+ set_special_pids(1, 1);

otherwise we change the PIDs without properly rehashing them.

Ingo

#2 lore raw
Quoted 2 lines

- a read_lock(&tasklist_lock) is missing around the group_send_sig_info() in send_sig_info().

Indeed. I still intend to clean up those entry points and haven't gotten to it, so I hadn't bothered with this yet either (though I think I sent it to you for the backport). It certainly does bite in practice, e.g. SIGPIPE.

There is a similar failure to take the lock before using zap_other_threads. I thought I sent this patch before, but it's not in 2.5 yet.

home/roland/redhat/linux-2.5.59-1.1007/fs/exec.c renamed from home/roland/redhat/linux-2.5.59-1.1007/fs/exec.c.~1~ +8 -1
@@ -601,9 +601,12 @@ static inline int de_thread(struct task_
 
 	if (thread_group_empty(current))
 		goto no_thread_group;
+
 	/*
-	 * Kill all other threads in the thread group:
+	 * Kill all other threads in the thread group.
+	 * We must hold tasklist_lock to call zap_other_threads.
 	 */
+	read_lock(&tasklist_lock);
 	spin_lock_irq(lock);
 	if (oldsig->group_exit) {
 		/*
@@ -611,6 +614,7 @@ static inline int de_thread(struct task_
 		 * return so that the signal is processed.
 		 */
 		spin_unlock_irq(lock);
+		read_unlock(&tasklist_lock);
 		kmem_cache_free(sighand_cachep, newsighand);
 		if (newsig)
 			kmem_cache_free(signal_cachep, newsig);
@@ -629,12 +633,15 @@ static inline int de_thread(struct task_
 		oldsig->group_exit_task = current;
 		current->state = TASK_UNINTERRUPTIBLE;
 		spin_unlock_irq(lock);
+		read_unlock(&tasklist_lock);
 		schedule();
+		read_lock(&tasklist_lock);
 		spin_lock_irq(lock);
 		if (oldsig->group_exit_task)
 			BUG();
 	}
 	spin_unlock_irq(lock);
+	read_unlock(&tasklist_lock);
 
 	/*
 	 * At this point all other threads have exited, all we have to
Quoted 2 lines

- session-IDs and group-IDs are set outside the tasklist lock. This causes breakage in the USB code. The correct fix is to do this:

This is outside the part of the code that I've touched lately.

Thanks,
Roland

#3 lore raw
Quoted 7 lines · Roland McGrath
Quoted 2 lines

- a read_lock(&tasklist_lock) is missing around the group_send_sig_info() in send_sig_info().

Indeed. I still intend to clean up those entry points and haven't gotten to it, so I hadn't bothered with this yet either (though I think I sent it to you for the backport). It certainly does bite in practice, e.g. SIGPIPE.

it does bite - here's the correctness fix meanwhile, until the interface is cleaned up.

Ingo

kernel/signal.c renamed from kernel/signal.c.orig +9 -7
@@ -1083,17 +1083,19 @@
 int
 send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
 {
+	int ret;
+
 	/* XXX should nix these interfaces and update the kernel */
-	if (T(sig, SIG_KERNEL_BROADCAST_MASK))
-		/* XXX do callers really always hold the tasklist_lock?? */
-		return group_send_sig_info(sig, info, p);
-	else {
-		int error;
+	if (T(sig, SIG_KERNEL_BROADCAST_MASK)) {
+		read_lock(&tasklist_lock);
+		ret = group_send_sig_info(sig, info, p);
+		read_unlock(&tasklist_lock);
+	} else {
 		spin_lock_irq(&p->sighand->siglock);
-		error = specific_send_sig_info(sig, info, p);
+		ret = specific_send_sig_info(sig, info, p);
 		spin_unlock_irq(&p->sighand->siglock);
-		return error;
 	}
+	return ret;
 }
 
 int
#4 lore raw
In reply to Roland McGrath
Quoted 4 lines · Roland McGrath
Quoted 2 lines

- session-IDs and group-IDs are set outside the tasklist lock. This causes breakage in the USB code. The correct fix is to do this:

This is outside the part of the code that I've touched lately.

yes, i introduced the bug with the new pidhash. Here's the fix, against BK-curr.

Ingo

include/linux/sched.h renamed from include/linux/sched.h.orig +2 -0
@@ -503,6 +503,8 @@
 extern struct   mm_struct init_mm;
 
 extern struct task_struct *find_task_by_pid(int pid);
+extern void set_special_pids(pid_t session, pid_t pgrp);
+extern void __set_special_pids(pid_t session, pid_t pgrp);
 
 /* per-UID process charging. */
 extern struct user_struct * alloc_uid(uid_t);
fs/jffs/intrep.c renamed from fs/jffs/intrep.c.orig +1 -2
@@ -3344,8 +3344,7 @@
 	lock_kernel();
 	exit_mm(c->gc_task);
 
-	current->session = 1;
-	current->pgrp = 1;
+	set_special_pids(1, 1);
 	init_completion(&c->gc_thread_comp); /* barrier */ 
 	spin_lock_irq(&current->sighand->siglock);
 	siginitsetinv (&current->blocked, sigmask(SIGHUP) | sigmask(SIGKILL) | sigmask(SIGSTOP) | sigmask(SIGCONT));
kernel/exit.c renamed from kernel/exit.c.orig +24 -2
@@ -254,6 +254,29 @@
 	write_unlock_irq(&tasklist_lock);
 }
 
+void __set_special_pids(pid_t session, pid_t pgrp)
+{
+	struct task_struct *curr = current;
+
+	if (curr->session != session) {
+		detach_pid(curr, PIDTYPE_SID);
+		curr->session = session;
+		attach_pid(curr, PIDTYPE_SID, session);
+	}
+	if (curr->pgrp != pgrp) {
+		detach_pid(curr, PIDTYPE_PGID);
+		curr->pgrp = pgrp;
+		attach_pid(curr, PIDTYPE_PGID, pgrp);
+	}
+}
+
+void set_special_pids(pid_t session, pid_t pgrp)
+{
+	write_lock_irq(&tasklist_lock);
+	__set_special_pids(session, pgrp);
+	write_unlock_irq(&tasklist_lock);
+}
+
 /*
  *	Put all the gunge required to become a kernel thread without
  *	attached user resources in one place where it belongs.
@@ -271,8 +294,7 @@
 	 */
 	exit_mm(current);
 
-	current->session = 1;
-	current->pgrp = 1;
+	set_special_pids(1, 1);
 	current->tty = NULL;
 
 	/* Become as one with the init task */
kernel/kmod.c renamed from kernel/kmod.c.orig +1 -2
@@ -100,8 +100,7 @@
 	int i;
 	struct task_struct *curtask = current;
 
-	curtask->session = 1;
-	curtask->pgrp = 1;
+	set_special_pids(1, 1);
 
 	use_init_fs_context();
 
kernel/sys.c renamed from kernel/sys.c.orig +1 -10
@@ -1021,16 +1021,7 @@
 		goto out;
 
 	current->leader = 1;
-	if (current->session != current->pid) {
-		detach_pid(current, PIDTYPE_SID);
-		current->session = current->pid;
-		attach_pid(current, PIDTYPE_SID, current->pid);
-	}
-	if (current->pgrp != current->pid) {
-		detach_pid(current, PIDTYPE_PGID);
-		current->pgrp = current->pid;
-		attach_pid(current, PIDTYPE_PGID, current->pid);
-	}
+	__set_special_pids(current->pid, current->pid);
 	current->tty = NULL;
 	current->tty_old_pgrp = 0;
 	err = current->pgrp;
#5 lore raw

Arjan pointed out that this one is needed as well:

kernel/ksyms.c renamed from kernel/ksyms.c.orig +1 -0
@@ -604,6 +604,7 @@
 EXPORT_SYMBOL(tasklist_lock);
 EXPORT_SYMBOL(find_task_by_pid);
 EXPORT_SYMBOL(next_thread);
+EXPORT_SYMBOL_GPL(set_special_pids);
 #if defined(CONFIG_SMP) && defined(__GENERIC_PER_CPU)
 EXPORT_SYMBOL(__per_cpu_offset);
 #endif
#6 lore raw
Quoted 2 lines · Ingo Molnar

Arjan pointed out that this one is needed as well:

but in second thought... modules don't really need to set the pids at all, since reparent_to_init() is designed for doing so...

#7 lore raw
In reply to Roland McGrath
Quoted 3 lines · Roland McGrath

There is a similar failure to take the lock before using zap_other_threads. I thought I sent this patch before, but it's not in 2.5 yet.

Hmm.. From looking at this patch, it seems as if you believe that spinlocks must next correctly. They don't have to.

The ABBA kind of deadlock only means that you have to _take_ the spinlocks in the right order, you can release them in any order you like (as long as you release all of them).

So if the only requirement is that zap_other_threads() is called with the tasklist lock held, I would suggest just dropping the tasklist lock after the zap_other threads thing, despite the fact that you still want to hold on to the siglock for a while longer. That simplifies the patch, and means that you don't have to worry about dropping and re-taking the tasklist lock in the loop that follows.

Linus

#8 lore raw
home/roland/redhat/linux-2.5.59-1.1007/fs/exec.c renamed from home/roland/redhat/linux-2.5.59-1.1007/fs/exec.c.~1~ +6 -1
@@ -601,9 +601,12 @@ static inline int de_thread(struct task_
 
 	if (thread_group_empty(current))
 		goto no_thread_group;
+
 	/*
-	 * Kill all other threads in the thread group:
+	 * Kill all other threads in the thread group.
+	 * We must hold tasklist_lock to call zap_other_threads.
 	 */
+	read_lock(&tasklist_lock);
 	spin_lock_irq(lock);
 	if (oldsig->group_exit) {
 		/*
@@ -611,6 +614,7 @@ static inline int de_thread(struct task_
 		 * return so that the signal is processed.
 		 */
 		spin_unlock_irq(lock);
+		read_unlock(&tasklist_lock);
 		kmem_cache_free(sighand_cachep, newsighand);
 		if (newsig)
 			kmem_cache_free(signal_cachep, newsig);
@@ -618,6 +622,7 @@ static inline int de_thread(struct task_
 	}
 	oldsig->group_exit = 1;
 	zap_other_threads(current);
+	read_unlock(&tasklist_lock);
 
 	/*
 	 * Account for the thread group leader hanging around: