Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 13 Jan 2005 21:22:23 GMT
From:      John Baldwin <jhb@FreeBSD.org>
To:        Perforce Change Reviews <perforce@freebsd.org>
Subject:   PERFORCE change 68940 for review
Message-ID:  <200501132122.j0DLMNc8011325@repoman.freebsd.org>

next in thread | raw e-mail | index | archive | help
http://perforce.freebsd.org/chv.cgi?CH=68940

Change 68940 by jhb@jhb_slimer on 2005/01/13 21:22:16

	Use a simple API to manage md_utrap structures and protect their
	reference count with the mutex pool.  Hopefull this will fix the
	modified after free panics kris is seeing on sparc64.

Affected files ...

.. //depot/projects/smpng/sys/sparc64/include/md_var.h#13 edit
.. //depot/projects/smpng/sys/sparc64/sparc64/machdep.c#70 edit
.. //depot/projects/smpng/sys/sparc64/sparc64/sys_machdep.c#11 edit
.. //depot/projects/smpng/sys/sparc64/sparc64/vm_machdep.c#45 edit

Differences ...

==== //depot/projects/smpng/sys/sparc64/include/md_var.h#13 (text+ko) ====

@@ -45,10 +45,14 @@
 extern	vm_paddr_t kstack0_phys;
 
 struct	pcpu;
+struct	md_utrap;
 
 void	cpu_identify(u_long vers, u_int clock, u_int id);
 void	cpu_setregs(struct pcpu *pc);
 int	is_physical_memory(vm_paddr_t addr);
+struct md_utrap *utrap_alloc(void);
+void	utrap_free(struct md_utrap *ut);
+struct md_utrap *utrap_hold(struct md_utrap *ut);
 
 cpu_block_copy_t spitfire_block_copy;
 cpu_block_zero_t spitfire_block_zero;

==== //depot/projects/smpng/sys/sparc64/sparc64/machdep.c#70 (text+ko) ====

@@ -744,7 +744,6 @@
 exec_setregs(struct thread *td, u_long entry, u_long stack, u_long ps_strings)
 {
 	struct trapframe *tf;
-	struct md_utrap *ut;
 	struct pcb *pcb;
 	struct proc *p;
 	u_long sp;
@@ -752,10 +751,8 @@
 	/* XXX no cpu_exec */
 	p = td->td_proc;
 	p->p_md.md_sigtramp = NULL;
-	if ((ut = p->p_md.md_utrap) != NULL) {
-		ut->ut_refcnt--;
-		if (ut->ut_refcnt == 0)
-			free(ut, M_SUBPROC);
+	if (p->p_md.md_utrap != NULL) {
+		utrap_free(p->p_md.md_utrap);
 		p->p_md.md_utrap = NULL;
 	}
 
@@ -838,3 +835,40 @@
 	tf->tf_gsr = fpregs->fr_gsr;
 	return (0);
 }
+
+struct md_utrap *
+utrap_alloc(void)
+{
+	struct md_utrap *ut;
+
+	ut = malloc(sizeof(struct md_utrap), M_SUBPROC, M_WAITOK | M_ZERO);
+	ut->ut_refcnt = 1;
+	return (ut);
+}
+
+void
+utrap_free(struct md_utrap *ut)
+{
+	int refcnt;
+
+	if (ut == NULL)
+		return;
+	mtx_pool_lock(mtxpool_sleep, ut);
+	ut->ut_refcnt--;
+	refcnt = ut->ut_refcnt;
+	mtx_pool_unlock(mtxpool_sleep, ut);
+	if (refcnt == 0)
+		free(ut, M_SUBPROC);
+}
+
+struct md_utrap *
+utrap_hold(struct md_utrap *ut)
+{
+
+	if (ut == NULL)
+		return (NULL);
+	mtx_pool_lock(mtxpool_sleep, ut);
+	ut->ut_refcnt++;
+	mtx_pool_unlock(mtxpool_sleep, ut);
+	return (ut);
+}

==== //depot/projects/smpng/sys/sparc64/sparc64/sys_machdep.c#11 (text+ko) ====

@@ -119,9 +119,7 @@
 		}
 		if (ua.type != UTH_NOCHANGE) {
 			if (ut == NULL) {
-				ut = malloc(sizeof *ut, M_SUBPROC,
-				    M_WAITOK | M_ZERO);
-				ut->ut_refcnt = 1;
+				ut = utrap_alloc();
 				td->td_proc->p_md.md_utrap = ut;
 			}
 			ut->ut_precise[ua.type] = ua.new_precise;

==== //depot/projects/smpng/sys/sparc64/sparc64/vm_machdep.c#45 (text+ko) ====

@@ -116,10 +116,8 @@
 
 	p = td->td_proc;
 	p->p_md.md_sigtramp = NULL;
-	if ((ut = p->p_md.md_utrap) != NULL) {
-		ut->ut_refcnt--;
-		if (ut->ut_refcnt == 0)
-			free(ut, M_SUBPROC);
+	if (p->p_md.md_utrap != NULL) {
+		utrap_free(p->p_md.md_utrap);
 		p->p_md.md_utrap = NULL;
 	}
 }
@@ -200,7 +198,6 @@
 void
 cpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags)
 {
-	struct md_utrap *ut;
 	struct trapframe *tf;
 	struct frame *fp;
 	struct pcb *pcb1;
@@ -216,9 +213,7 @@
 		return;
 
 	p2->p_md.md_sigtramp = td1->td_proc->p_md.md_sigtramp;
-	if ((ut = td1->td_proc->p_md.md_utrap) != NULL)
-		ut->ut_refcnt++;
-	p2->p_md.md_utrap = ut;
+	p2->p_md.md_utrap = utrap_hold(td1->td_proc->p_md.md_utrap);
 
 	/* The pcb must be aligned on a 64-byte boundary. */
 	pcb1 = td1->td_pcb;



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200501132122.j0DLMNc8011325>