Skip to contentSkip to conversation

More cleanups in do_wait() pathes

PATCH In review Vitaly Mayatskikh 3 participants 8 messages May 13, 2009 – May 16, 2009

lore
Share & tools
lore
#0 lore raw

cover More cleanups in do_wait() pathes

Further cleanups of do_wait() and friends. Thanks for all your reviews of previous patches and ideas how to make it better. I'm currently running kernel with these patches on my laptop for 1+ day, it is still able to boot and live 5-10 hours w/o visible issues.

Next task is to optimize a bunch of put_user() in copy_wait_opts_to_user(), but I'd like to use Ingo's idea with put_user_try/catch. Therefore, need to implement generic stub for architectures which does not support try/catch yet.

Vitaly Mayatskikh (3):
Introduce "struct wait_info" to simplify wait_task*() pathes Replace wait_noreap_copyout() by copy_wait_opts_to_user() Move more common code from wait_task_*() to copy_wait_opts_to_user()

 kernel/exit.c |  127 +++++++++++++++++++++++++++-----------------------------
 1 files changed, 61 insertions(+), 66 deletions(-)
#1 lore raw

PATCH 1/3 Introduce "struct wait_info" to simplify wait_task*() pathes

There's too much parameters passing to wait_noreap_copyout() and copy_wait_opts_to_user().

Introduce "struct wait_info" which holds data needed for user's siginfo structure. This will save registers and some stack on copy_wait_opts_to_user() invoke.

---
 kernel/exit.c |  116 +++++++++++++++++++++++++++++++--------------------------
 1 files changed, 63 insertions(+), 53 deletions(-)
kernel/exit.c +63 -53
index f22e82c..f069bc1 100644
@@ -1088,6 +1088,15 @@ struct wait_opts {
 	int			notask_error;
 };
 
+struct wait_info {
+	struct task_struct*	p;
+	pid_t			pid;
+	uid_t			uid;
+	int			why;
+	int			status;
+	int			signal;
+};
+
 static struct pid *task_pid_type(struct task_struct *task, enum pid_type type)
 {
 	struct pid *pid = NULL;
@@ -1123,36 +1132,36 @@ static int eligible_child(struct wait_opts *wo, struct task_struct *p)
 	return 1;
 }
 
-static int copy_wait_opts_to_user(struct wait_opts *wo, struct task_struct *p,
-				  pid_t pid, uid_t uid, int why, int status, int signal)
+static int copy_wait_opts_to_user(struct wait_opts *wo, struct wait_info *wi)
 {
 	struct siginfo __user *infop = wo->wo_info;
 	int retval = wo->wo_rusage
-		? getrusage(p, RUSAGE_BOTH, wo->wo_rusage) : 0;
+		? getrusage(wi->p, RUSAGE_BOTH, wo->wo_rusage) : 0;
 
 	if (!retval && infop) {
-		retval = put_user(signal, &infop->si_signo);
+		retval = put_user(wi->signal, &infop->si_signo);
 		if (!retval)
 			retval = put_user(0, &infop->si_errno);
 		if (!retval)
-			retval = put_user((short)why, &infop->si_code);
+			retval = put_user((short)wi->why, &infop->si_code);
 		if (!retval)
-			retval = put_user(pid, &infop->si_pid);
+			retval = put_user(wi->pid, &infop->si_pid);
 		if (!retval)
-			retval = put_user(uid, &infop->si_uid);
+			retval = put_user(wi->uid, &infop->si_uid);
 		if (!retval)
-			retval = put_user(status, &infop->si_status);
+			retval = put_user(wi->status, &infop->si_status);
 	}
 	return retval;
 }
 
-static int wait_noreap_copyout(struct wait_opts *wo, struct task_struct *p,
-				pid_t pid, uid_t uid, int why, int status)
+static int wait_noreap_copyout(struct wait_opts *wo, struct wait_info *wi)
 {
-	int retval = copy_wait_opts_to_user(wo, p, pid, uid, why, status, SIGCHLD);
-	put_task_struct(p);
+	int retval;
+	wi->signal = SIGCHLD;
+	retval = copy_wait_opts_to_user(wo, wi);
+	put_task_struct(wi->p);
 	if (!retval)
-		retval = pid;
+		retval = wi->pid;
 	return retval;
 }
 
@@ -1165,26 +1174,27 @@ static int wait_noreap_copyout(struct wait_opts *wo, struct task_struct *p,
 static int wait_task_zombie(struct wait_opts *wo, struct task_struct *p)
 {
 	unsigned long state;
-	int retval, why, status, traced;
-	pid_t pid = task_pid_vnr(p);
-	uid_t uid = __task_cred(p)->uid;
+	int retval, traced;
+	struct wait_info wi = { .p = p, .pid = task_pid_vnr(p),
+				.uid = __task_cred(p)->uid,
+				.signal = SIGCHLD };
 
 	if (!likely(wo->wo_flags & WEXITED))
 		return 0;
 
 	if (unlikely(wo->wo_flags & WNOWAIT)) {
-		int exit_code = p->exit_code;
+		wi.status = p->exit_code;
 
 		get_task_struct(p);
 		read_unlock(&tasklist_lock);
-		if ((exit_code & 0x7f) == 0) {
-			why = CLD_EXITED;
-			status = exit_code >> 8;
+		if ((wi.status & 0x7f) == 0) {
+			wi.why = CLD_EXITED;
+			wi.status >>= 8;
 		} else {
-			why = (exit_code & 0x80) ? CLD_DUMPED : CLD_KILLED;
-			status = exit_code & 0x7f;
+			wi.why = (wi.status & 0x80) ? CLD_DUMPED : CLD_KILLED;
+			wi.status &= 0x7f;
 		}
-		return wait_noreap_copyout(wo, p, pid, uid, why, status);
+		return wait_noreap_copyout(wo, &wi);
 	}
 
 	/*
@@ -1265,23 +1275,23 @@ static int wait_task_zombie(struct wait_opts *wo, struct task_struct *p)
 	 */
 	read_unlock(&tasklist_lock);
 
-	status = (p->signal->flags & SIGNAL_GROUP_EXIT)
+	wi.status = (p->signal->flags & SIGNAL_GROUP_EXIT)
 		? p->signal->group_exit_code : p->exit_code;
 	if (wo->wo_stat)
-		retval = put_user(status, wo->wo_stat);
+		retval = put_user(wi.status, wo->wo_stat);
 
-	if ((status & 0x7f) == 0) {
-		why = CLD_EXITED;
-		status >>= 8;
+	if ((wi.status & 0x7f) == 0) {
+		wi.why = CLD_EXITED;
+		wi.status >>= 8;
 	} else {
-		why = (status & 0x80) ? CLD_DUMPED : CLD_KILLED;
-		status &= 0x7f;
+		wi.why = (wi.status & 0x80) ? CLD_DUMPED : CLD_KILLED;
+		wi.status &= 0x7f;
 	}
 
-	retval = copy_wait_opts_to_user(wo, p, pid, uid, why, status, SIGCHLD);
+	retval = copy_wait_opts_to_user(wo, &wi);
 
 	if (!retval)
-		retval = pid;
+		retval = wi.pid;
 
 	if (traced) {
 		write_lock_irq(&tasklist_lock);
@@ -1328,9 +1338,8 @@ static int *task_stopped_code(struct task_struct *p, bool ptrace)
 static int wait_task_stopped(struct wait_opts *wo,
 				int ptrace, struct task_struct *p)
 {
-	int retval, exit_code, *p_code, why;
-	uid_t uid = 0; /* unneeded, required by compiler */
-	pid_t pid;
+	int retval, *p_code;
+	struct wait_info wi = { .p = p, .signal = SIGCHLD };
 
 	/*
 	 * Traditionally we see ptrace'd stopped tasks regardless of options.
@@ -1338,25 +1347,25 @@ static int wait_task_stopped(struct wait_opts *wo,
 	if (!ptrace && !(wo->wo_flags & WUNTRACED))
 		return 0;
 
-	exit_code = 0;
+	wi.status = 0;
 	spin_lock_irq(&p->sighand->siglock);
 
 	p_code = task_stopped_code(p, ptrace);
 	if (unlikely(!p_code))
 		goto unlock_sig;
 
-	exit_code = *p_code;
-	if (!exit_code)
+	wi.status = *p_code;
+	if (!wi.status)
 		goto unlock_sig;
 
 	if (!unlikely(wo->wo_flags & WNOWAIT))
 		*p_code = 0;
 
 	/* don't need the RCU readlock here as we're holding a spinlock */
-	uid = __task_cred(p)->uid;
+	wi.uid = __task_cred(p)->uid;
 unlock_sig:
 	spin_unlock_irq(&p->sighand->siglock);
-	if (!exit_code)
+	if (!wi.status)
 		return 0;
 
 	/*
@@ -1367,16 +1376,16 @@ unlock_sig:
 	 * possibly take page faults for user memory.
 	 */
 	get_task_struct(p);
-	pid = task_pid_vnr(p);
-	why = ptrace ? CLD_TRAPPED : CLD_STOPPED;
+	wi.pid = task_pid_vnr(p);
+	wi.why = ptrace ? CLD_TRAPPED : CLD_STOPPED;
 	read_unlock(&tasklist_lock);
 
-	retval = copy_wait_opts_to_user(wo, p, pid, uid, why, exit_code, SIGCHLD);
+	retval = copy_wait_opts_to_user(wo, &wi);
 
 	if (!retval && wo->wo_stat)
-		retval = put_user((exit_code << 8) | 0x7f, wo->wo_stat);
+		retval = put_user((wi.status << 8) | 0x7f, wo->wo_stat);
 	if (!retval)
-		retval = pid;
+		retval = wi.pid;
 	put_task_struct(p);
 
 	BUG_ON(!retval);
@@ -1392,8 +1401,8 @@ unlock_sig:
 static int wait_task_continued(struct wait_opts *wo, struct task_struct *p)
 {
 	int retval;
-	pid_t pid;
-	uid_t uid;
+	struct wait_info wi = { .p = p, .why = CLD_CONTINUED,
+				.status = SIGCONT, .signal = SIGCHLD };
 
 	if (!unlikely(wo->wo_flags & WCONTINUED))
 		return 0;
@@ -1409,21 +1418,21 @@ static int wait_task_continued(struct wait_opts *wo, struct task_struct *p)
 	}
 	if (!unlikely(wo->wo_flags & WNOWAIT))
 		p->signal->flags &= ~SIGNAL_STOP_CONTINUED;
-	uid = __task_cred(p)->uid;
+	wi.uid = __task_cred(p)->uid;
 	spin_unlock_irq(&p->sighand->siglock);
 
-	pid = task_pid_vnr(p);
+	wi.pid = task_pid_vnr(p);
 	get_task_struct(p);
 	read_unlock(&tasklist_lock);
 
-	retval = copy_wait_opts_to_user(wo, p, pid, uid,
-					CLD_CONTINUED, SIGCONT, SIGCHLD);
+	retval = copy_wait_opts_to_user(wo, &wi);
+
 	put_task_struct(p);
 
 	if (!retval && wo->wo_stat)
 		retval = put_user(0xffff, wo->wo_stat);
 	if (!retval)
-		retval = pid;
+		retval = wi.pid;
 
 	BUG_ON(retval == 0);
 
@@ -1588,7 +1597,8 @@ end:
 			 * we would set so the user can easily tell the
 			 * difference.
 			 */
-			retval = copy_wait_opts_to_user(wo, 0, 0, 0, 0, 0, 0);
+			struct wait_info wi = { 0 };
+			retval = copy_wait_opts_to_user(wo, &wi);
 		}
 	}
 	return retval;
Signature · 1 line
1.6.2.2
#2 lore raw

Re: [PATCH 1/3] Introduce "struct wait_info" to simplify wait_task*() pathes

What's the benefit of this? Those parameters are probably happily in registers and compiled well, I would guess. Adding more memory copying does not seem like a benefit. Have you verified that your change reduces code size or something like that?

Thanks,
Roland

#3 lore raw

Re: [PATCH 1/3] Introduce "struct wait_info" to simplify wait_task*() pathes

Quoted 4 lines · Roland McGrath

What's the benefit of this? Those parameters are probably happily in registers and compiled well, I would guess. Adding more memory copying does not seem like a benefit. Have you verified that your change reduces code size or something like that?

Yeah, I was wrong. There's no benefit in size of .text

Signature · 1 line
wbr, Vitaly
#4 lore raw

Re: [PATCH 1/3] Introduce "struct wait_info" to simplify wait_task*() pathes

Quoted 9 lines · Vitaly Mayatskikh
Quoted 5 lines · Roland McGrath

What's the benefit of this? Those parameters are probably happily in registers and compiled well, I would guess. Adding more memory copying does not seem like a benefit. Have you verified that your change reduces code size or something like that?

Yeah, I was wrong. There's no benefit in size of .text

but there's a lot of benefit in cleanliness - just look at the simplification of the prototypes.

Ingo

#5 lore raw

Re: [PATCH 1/3] Introduce "struct wait_info" to simplify wait_task*() pathes

Quoted 4 lines · Ingo Molnar
Quoted 1 line

Yeah, I was wrong. There's no benefit in size of .text

but there's a lot of benefit in cleanliness - just look at the simplification of the prototypes.

Simplification was my other intention. Ok, I'll wait for Oleg's review :)

Signature · 1 line
wbr, Vitaly
#6 lore raw

PATCH 2/3 Replace wait_noreap_copyout() by copy_wait_opts_to_user()

In reply to Vitaly Mayatskikh

wait_noreap_copyout() currently is a short wrapper for copy_wait_opts_to_user(), and wait_task_zombie() is the only one caller of it.

---
 kernel/exit.c |   17 +++++------------
 1 files changed, 5 insertions(+), 12 deletions(-)
kernel/exit.c +5 -12
index f069bc1..cea85c9 100644
@@ -1154,17 +1154,6 @@ static int copy_wait_opts_to_user(struct wait_opts *wo, struct wait_info *wi)
 	return retval;
 }
 
-static int wait_noreap_copyout(struct wait_opts *wo, struct wait_info *wi)
-{
-	int retval;
-	wi->signal = SIGCHLD;
-	retval = copy_wait_opts_to_user(wo, wi);
-	put_task_struct(wi->p);
-	if (!retval)
-		retval = wi->pid;
-	return retval;
-}
-
 /*
  * Handle sys_wait4 work for one task in state EXIT_ZOMBIE.  We hold
  * read_lock(&tasklist_lock) on entry.  If we return zero, we still hold
@@ -1194,7 +1183,11 @@ static int wait_task_zombie(struct wait_opts *wo, struct task_struct *p)
 			wi.why = (wi.status & 0x80) ? CLD_DUMPED : CLD_KILLED;
 			wi.status &= 0x7f;
 		}
-		return wait_noreap_copyout(wo, &wi);
+		retval = copy_wait_opts_to_user(wo, &wi);
+		put_task_struct(p);
+		if (!retval)
+			retval = wi.pid;
+		return retval;
 	}
 
 	/*
Signature · 1 line
1.6.2.2
#7 lore raw

PATCH 3/3 Move more common code from wait_task_*() to copy_wait_opts_to_user()

In reply to Vitaly Mayatskikh

Roland McGrath pointed to more common checks and put_user() around invoke of copy_wait_opts_to_user() in wait_task_*() functions. This common code is moved to copy_wait_opts_to_user().

---
 kernel/exit.c |   40 ++++++++++++++++------------------------
 1 files changed, 16 insertions(+), 24 deletions(-)
kernel/exit.c +16 -24
index cea85c9..cf41c2b 100644
@@ -1095,6 +1095,7 @@ struct wait_info {
 	int			why;
 	int			status;
 	int			signal;
+	int			stat;
 };
 
 static struct pid *task_pid_type(struct task_struct *task, enum pid_type type)
@@ -1151,6 +1152,10 @@ static int copy_wait_opts_to_user(struct wait_opts *wo, struct wait_info *wi)
 		if (!retval)
 			retval = put_user(wi->status, &infop->si_status);
 	}
+	if (!retval && wo->wo_stat)
+		retval = put_user(wi->stat, wo->wo_stat);
+	if (!retval)
+		retval = wi->pid;
 	return retval;
 }
 
@@ -1166,7 +1171,7 @@ static int wait_task_zombie(struct wait_opts *wo, struct task_struct *p)
 	int retval, traced;
 	struct wait_info wi = { .p = p, .pid = task_pid_vnr(p),
 				.uid = __task_cred(p)->uid,
-				.signal = SIGCHLD };
+				.signal = SIGCHLD, .stat = 0 };
 
 	if (!likely(wo->wo_flags & WEXITED))
 		return 0;
@@ -1185,8 +1190,6 @@ static int wait_task_zombie(struct wait_opts *wo, struct task_struct *p)
 		}
 		retval = copy_wait_opts_to_user(wo, &wi);
 		put_task_struct(p);
-		if (!retval)
-			retval = wi.pid;
 		return retval;
 	}
 
@@ -1268,24 +1271,19 @@ static int wait_task_zombie(struct wait_opts *wo, struct task_struct *p)
 	 */
 	read_unlock(&tasklist_lock);
 
-	wi.status = (p->signal->flags & SIGNAL_GROUP_EXIT)
+	wi.stat = (p->signal->flags & SIGNAL_GROUP_EXIT)
 		? p->signal->group_exit_code : p->exit_code;
-	if (wo->wo_stat)
-		retval = put_user(wi.status, wo->wo_stat);
 
-	if ((wi.status & 0x7f) == 0) {
+	if ((wi.stat & 0x7f) == 0) {
 		wi.why = CLD_EXITED;
-		wi.status >>= 8;
+		wi.status = wi.stat >> 8;
 	} else {
-		wi.why = (wi.status & 0x80) ? CLD_DUMPED : CLD_KILLED;
-		wi.status &= 0x7f;
+		wi.why = (wi.stat & 0x80) ? CLD_DUMPED : CLD_KILLED;
+		wi.status = wi.stat & 0x7f;
 	}
 
 	retval = copy_wait_opts_to_user(wo, &wi);
 
-	if (!retval)
-		retval = wi.pid;
-
 	if (traced) {
 		write_lock_irq(&tasklist_lock);
 		/* We dropped tasklist, ptracer could die and untrace */
@@ -1369,16 +1367,14 @@ unlock_sig:
 	 * possibly take page faults for user memory.
 	 */
 	get_task_struct(p);
+	read_unlock(&tasklist_lock);
+
 	wi.pid = task_pid_vnr(p);
 	wi.why = ptrace ? CLD_TRAPPED : CLD_STOPPED;
-	read_unlock(&tasklist_lock);
+	wi.stat = (wi.status << 8) | 0x7f;
 
 	retval = copy_wait_opts_to_user(wo, &wi);
 
-	if (!retval && wo->wo_stat)
-		retval = put_user((wi.status << 8) | 0x7f, wo->wo_stat);
-	if (!retval)
-		retval = wi.pid;
 	put_task_struct(p);
 
 	BUG_ON(!retval);
@@ -1395,7 +1391,8 @@ static int wait_task_continued(struct wait_opts *wo, struct task_struct *p)
 {
 	int retval;
 	struct wait_info wi = { .p = p, .why = CLD_CONTINUED,
-				.status = SIGCONT, .signal = SIGCHLD };
+				.status = SIGCONT, .signal = SIGCHLD,
+				.stat = 0xffff };
 
 	if (!unlikely(wo->wo_flags & WCONTINUED))
 		return 0;
@@ -1422,11 +1419,6 @@ static int wait_task_continued(struct wait_opts *wo, struct task_struct *p)
 
 	put_task_struct(p);
 
-	if (!retval && wo->wo_stat)
-		retval = put_user(0xffff, wo->wo_stat);
-	if (!retval)
-		retval = wi.pid;
-
 	BUG_ON(retval == 0);
 
 	return retval;
Signature · 1 line
1.6.2.2