Index: sys/kern/kern_exec.c
===================================================================
--- sys/kern/kern_exec.c	(revision 266582)
+++ sys/kern/kern_exec.c	(revision 266583)
@@ -278,6 +278,7 @@ kern_execve(td, args, mac_p)
 	struct mac *mac_p;
 {
 	struct proc *p = td->td_proc;
+	struct vmspace *oldvmspace;
 	int error;
 
 	AUDIT_ARG_ARGV(args->begin_argv, args->argc,
@@ -294,6 +295,8 @@ kern_execve(td, args, mac_p)
 		PROC_UNLOCK(p);
 	}
 
+	KASSERT((td->td_pflags & TDP_EXECVMSPC) == 0, ("nested execve"));
+	oldvmspace = td->td_proc->p_vmspace;
 	error = do_execve(td, args, mac_p);
 
 	if (p->p_flag & P_HADTHREADS) {
@@ -308,6 +311,12 @@ kern_execve(td, args, mac_p)
 			thread_single_end();
 		PROC_UNLOCK(p);
 	}
+	if ((td->td_pflags & TDP_EXECVMSPC) != 0) {
+		KASSERT(td->td_proc->p_vmspace != oldvmspace,
+		    ("oldvmspace still used"));
+		vmspace_free(oldvmspace);
+		td->td_pflags &= ~TDP_EXECVMSPC;
+	}
 
 	return (error);
 }
Index: sys/kern
===================================================================
--- sys/kern	(revision 266582)
+++ sys/kern	(revision 266583)

Property changes on: sys/kern
___________________________________________________________________
Modified: svn:mergeinfo
   Merged /head/sys/kern:r266464
Index: sys/vm/vm_map.c
===================================================================
--- sys/vm/vm_map.c	(revision 266582)
+++ sys/vm/vm_map.c	(revision 266583)
@@ -3521,6 +3521,8 @@ vmspace_exec(struct proc *p, vm_offset_t minuser,
 	struct vmspace *oldvmspace = p->p_vmspace;
 	struct vmspace *newvmspace;
 
+	KASSERT((curthread->td_pflags & TDP_EXECVMSPC) == 0,
+	    ("vmspace_exec recursed"));
 	newvmspace = vmspace_alloc(minuser, maxuser);
 	if (newvmspace == NULL)
 		return (ENOMEM);
@@ -3537,7 +3539,7 @@ vmspace_exec(struct proc *p, vm_offset_t minuser,
 	PROC_VMSPACE_UNLOCK(p);
 	if (p == curthread->td_proc)
 		pmap_activate(curthread);
-	vmspace_free(oldvmspace);
+	curthread->td_pflags |= TDP_EXECVMSPC;
 	return (0);
 }
 
Index: sys/vm
===================================================================
--- sys/vm	(revision 266582)
+++ sys/vm	(revision 266583)

Property changes on: sys/vm
___________________________________________________________________
Modified: svn:mergeinfo
   Merged /head/sys/vm:r266464
Index: sys/sys/proc.h
===================================================================
--- sys/sys/proc.h	(revision 266582)
+++ sys/sys/proc.h	(revision 266583)
@@ -414,6 +414,7 @@ do {									\
 #define	TDP_AUDITREC	0x01000000 /* Audit record pending on thread */
 #define	TDP_RESETSPUR	0x04000000 /* Reset spurious page fault history. */
 #define	TDP_NERRNO	0x08000000 /* Last errno is already in td_errno */
+#define	TDP_EXECVMSPC	0x40000000 /* Execve destroyed old vmspace */
 
 /*
  * Reasons that the current thread can not be run yet.
Index: sys/sys
===================================================================
--- sys/sys	(revision 266582)
+++ sys/sys	(revision 266583)

Property changes on: sys/sys
___________________________________________________________________
Modified: svn:mergeinfo
   Merged /head/sys/sys:r266464
Index: sys
===================================================================
--- sys	(revision 266582)
+++ sys	(revision 266583)

Property changes on: sys
___________________________________________________________________
Modified: svn:mergeinfo
   Merged /head/sys:r266464
