From owner-svn-src-user@FreeBSD.ORG  Thu May  1 13:42:21 2014
Return-Path: <owner-svn-src-user@FreeBSD.ORG>
Delivered-To: svn-src-user@freebsd.org
Received: from mx1.freebsd.org (mx1.freebsd.org
 [IPv6:2001:1900:2254:206a::19:1])
 (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits))
 (No client certificate requested)
 by hub.freebsd.org (Postfix) with ESMTPS id 0AFF17C5;
 Thu,  1 May 2014 13:42:21 +0000 (UTC)
Received: from svn.freebsd.org (svn.freebsd.org
 [IPv6:2001:1900:2254:2068::e6a:0])
 (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))
 (Client did not present a certificate)
 by mx1.freebsd.org (Postfix) with ESMTPS id DFEA91CCB;
 Thu,  1 May 2014 13:42:20 +0000 (UTC)
Received: from svn.freebsd.org ([127.0.1.70])
 by svn.freebsd.org (8.14.8/8.14.8) with ESMTP id s41DgKZv065274;
 Thu, 1 May 2014 13:42:20 GMT (envelope-from dchagin@svn.freebsd.org)
Received: (from dchagin@localhost)
 by svn.freebsd.org (8.14.8/8.14.8/Submit) id s41DgKKZ065266;
 Thu, 1 May 2014 13:42:20 GMT (envelope-from dchagin@svn.freebsd.org)
Message-Id: <201405011342.s41DgKKZ065266@svn.freebsd.org>
From: Dmitry Chagin <dchagin@FreeBSD.org>
Date: Thu, 1 May 2014 13:42:20 +0000 (UTC)
To: src-committers@freebsd.org, svn-src-user@freebsd.org
Subject: svn commit: r265179 - in user/dchagin/lemul/sys: amd64/linux
 amd64/linux32 compat/linux
X-SVN-Group: user
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-BeenThere: svn-src-user@freebsd.org
X-Mailman-Version: 2.1.17
Precedence: list
List-Id: "SVN commit messages for the experimental &quot; user&quot;
 src tree" <svn-src-user.freebsd.org>
List-Unsubscribe: <http://lists.freebsd.org/mailman/options/svn-src-user>,
 <mailto:svn-src-user-request@freebsd.org?subject=unsubscribe>
List-Archive: <http://lists.freebsd.org/pipermail/svn-src-user/>
List-Post: <mailto:svn-src-user@freebsd.org>
List-Help: <mailto:svn-src-user-request@freebsd.org?subject=help>
List-Subscribe: <http://lists.freebsd.org/mailman/listinfo/svn-src-user>,
 <mailto:svn-src-user-request@freebsd.org?subject=subscribe>
X-List-Received-Date: Thu, 01 May 2014 13:42:21 -0000

Author: dchagin
Date: Thu May  1 13:42:19 2014
New Revision: 265179
URL: http://svnweb.freebsd.org/changeset/base/265179

Log:
  To fix a panic when in case of transition from linux binary execing
  to native binary we destroy emuldata thread entry and kern_execve()
  returns error a bit rewrite linux_execve system call. For now we destroy
  emuldata thread entry after kern_execve finished, not in event handler.

Modified:
  user/dchagin/lemul/sys/amd64/linux/linux_machdep.c
  user/dchagin/lemul/sys/amd64/linux32/linux32_machdep.c
  user/dchagin/lemul/sys/compat/linux/linux_emul.c
  user/dchagin/lemul/sys/compat/linux/linux_emul.h

Modified: user/dchagin/lemul/sys/amd64/linux/linux_machdep.c
==============================================================================
--- user/dchagin/lemul/sys/amd64/linux/linux_machdep.c	Thu May  1 13:30:14 2014	(r265178)
+++ user/dchagin/lemul/sys/amd64/linux/linux_machdep.c	Thu May  1 13:42:19 2014	(r265179)
@@ -126,7 +126,7 @@ linux_execve(struct thread *td, struct l
 	    args->envp);
 	free(path, M_TEMP);
 	if (error == 0)
-		error = kern_execve(td, &eargs, NULL);
+		error = linux_common_execve(td, &eargs);
 	return (error);
 }
 

Modified: user/dchagin/lemul/sys/amd64/linux32/linux32_machdep.c
==============================================================================
--- user/dchagin/lemul/sys/amd64/linux32/linux32_machdep.c	Thu May  1 13:30:14 2014	(r265178)
+++ user/dchagin/lemul/sys/amd64/linux32/linux32_machdep.c	Thu May  1 13:42:19 2014	(r265179)
@@ -150,7 +150,7 @@ linux_execve(struct thread *td, struct l
 	    args->argp, args->envp);
 	free(path, M_TEMP);
 	if (error == 0)
-		error = kern_execve(td, &eargs, NULL);
+		error = linux_common_execve(td, &eargs);
 	return (error);
 }
 

Modified: user/dchagin/lemul/sys/compat/linux/linux_emul.c
==============================================================================
--- user/dchagin/lemul/sys/compat/linux/linux_emul.c	Thu May  1 13:30:14 2014	(r265178)
+++ user/dchagin/lemul/sys/compat/linux/linux_emul.c	Thu May  1 13:42:19 2014	(r265179)
@@ -158,39 +158,23 @@ linux_proc_init(struct thread *td, struc
 	LIN_SDT_PROBE0(emul, proc_init, return);
 }
 
-void 
-linux_proc_exec(void *arg __unused, struct proc *p, struct image_params *imgp)
+int 
+linux_common_execve(struct thread *td, struct image_args *eargs)
 {
-	struct thread *td = curthread;
 	struct linux_emuldata *em;
+	struct proc *p;
+	int error;
 
-	/*
-	 * In a case of execing to linux binary we create linux
-	 * emuldata thread entry.
-	 */
-	if (__predict_false((imgp->sysent->sv_flags & SV_ABI_MASK) ==
-	    SV_ABI_LINUX)) {
-		LIN_SDT_PROBE2(emul, proc_exec, entry, p, imgp);
-		if (SV_PROC_ABI(p) == SV_ABI_LINUX)
-			linux_proc_init(td, NULL, 0);
-		else
-			linux_proc_init(td, td, 0);
-	}
+	error = kern_execve(td, eargs, NULL);
+	if (error)
+		return (error);
 
+	p = td->td_proc;
 	/*
 	 * In a case of transition from Linux binary execing to
 	 * FreeBSD binary we destroy linux emuldata thread entry.
 	 */
-	if (__predict_false((imgp->sysent->sv_flags & SV_ABI_MASK) != SV_ABI_LINUX &&
-	    SV_PROC_ABI(p) == SV_ABI_LINUX)) {
-
-		/* 
-		 * XXX:There's a race because here we assign p->p_emuldata NULL
-		 * but the process is still counted as linux one for a short
-		 * time so some other process might reference it and try to
-		 * access its p->p_emuldata and panicing on a NULL reference.
-		 */
-
+	if (SV_CURPROC_ABI() != SV_ABI_LINUX) {
 		PROC_LOCK(p);
 		em = em_find(td);
 		KASSERT(em != NULL, ("proc_exec: emuldata not found.\n"));
@@ -199,10 +183,30 @@ linux_proc_exec(void *arg __unused, stru
 
 		free(em, M_TEMP);
 	}
+	return (0);
+}
+
 
+
+void 
+linux_proc_exec(void *arg __unused, struct proc *p, struct image_params *imgp)
+{
+	struct thread *td = curthread;
+
+	/*
+	 * In a case of execing to linux binary we create linux
+	 * emuldata thread entry.
+	 */
 	if (__predict_false((imgp->sysent->sv_flags & SV_ABI_MASK) ==
-	    SV_ABI_LINUX))
+	    SV_ABI_LINUX)) {
+		LIN_SDT_PROBE2(emul, proc_exec, entry, p, imgp);
+		if (SV_PROC_ABI(p) == SV_ABI_LINUX)
+			linux_proc_init(td, NULL, 0);
+		else
+			linux_proc_init(td, td, 0);
+
 		LIN_SDT_PROBE0(emul, proc_exec, return);
+	}
 }
 
 void

Modified: user/dchagin/lemul/sys/compat/linux/linux_emul.h
==============================================================================
--- user/dchagin/lemul/sys/compat/linux/linux_emul.h	Thu May  1 13:30:14 2014	(r265178)
+++ user/dchagin/lemul/sys/compat/linux/linux_emul.h	Thu May  1 13:42:19 2014	(r265179)
@@ -54,6 +54,7 @@ struct linux_emuldata	*em_find(struct th
 #define	LINUX_XDEPR_REQUEUEOP	0x00000001	/* uses deprecated
 						   futex REQUEUE op*/
 
+int	linux_common_execve(struct thread *, struct image_args *);
 void	linux_proc_init(struct thread *, struct thread *, int);
 void	linux_schedtail(struct thread *);
 void	linux_proc_exec(void *, struct proc *, struct image_params *);