Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 17 Mar 2019 20:30:27 +0000 (UTC)
From:      Mark Johnston <markj@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-12@freebsd.org
Subject:   svn commit: r345247 - in stable/12/sys: cddl/contrib/opensolaris/uts/common/dtrace cddl/contrib/opensolaris/uts/intel/dtrace cddl/dev/dtrace modules/dtrace
Message-ID:  <201903172030.x2HKUREK097147@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: markj
Date: Sun Mar 17 20:30:27 2019
New Revision: 345247
URL: https://svnweb.freebsd.org/changeset/base/345247

Log:
  MFC r344450, r344452, r344453:
  Fix a tracepoint lookup race in fasttrap_pid_probe().

Modified:
  stable/12/sys/cddl/contrib/opensolaris/uts/common/dtrace/fasttrap.c
  stable/12/sys/cddl/contrib/opensolaris/uts/intel/dtrace/fasttrap_isa.c
  stable/12/sys/cddl/dev/dtrace/dtrace_cddl.h
  stable/12/sys/modules/dtrace/Makefile
Directory Properties:
  stable/12/   (props changed)

Modified: stable/12/sys/cddl/contrib/opensolaris/uts/common/dtrace/fasttrap.c
==============================================================================
--- stable/12/sys/cddl/contrib/opensolaris/uts/common/dtrace/fasttrap.c	Sun Mar 17 18:31:48 2019	(r345246)
+++ stable/12/sys/cddl/contrib/opensolaris/uts/common/dtrace/fasttrap.c	Sun Mar 17 20:30:27 2019	(r345247)
@@ -1089,6 +1089,8 @@ fasttrap_tracepoint_disable(proc_t *p, fasttrap_probe_
 		ASSERT(p->p_proc_flag & P_PR_LOCK);
 #endif
 		p->p_dtrace_count--;
+
+		atomic_add_rel_64(&p->p_fasttrap_tp_gen, 1);
 	}
 
 	/*

Modified: stable/12/sys/cddl/contrib/opensolaris/uts/intel/dtrace/fasttrap_isa.c
==============================================================================
--- stable/12/sys/cddl/contrib/opensolaris/uts/intel/dtrace/fasttrap_isa.c	Sun Mar 17 18:31:48 2019	(r345246)
+++ stable/12/sys/cddl/contrib/opensolaris/uts/intel/dtrace/fasttrap_isa.c	Sun Mar 17 20:30:27 2019	(r345247)
@@ -967,6 +967,7 @@ fasttrap_pid_probe(struct trapframe *tf)
 	struct reg reg, *rp;
 	proc_t *p = curproc, *pp;
 	struct rm_priotracker tracker;
+	uint64_t gen;
 	uintptr_t pc;
 	uintptr_t new_pc = 0;
 	fasttrap_bucket_t *bucket;
@@ -1026,8 +1027,22 @@ fasttrap_pid_probe(struct trapframe *tf)
 	while (pp->p_vmspace == pp->p_pptr->p_vmspace)
 		pp = pp->p_pptr;
 	pid = pp->p_pid;
+	if (pp != p) {
+		PROC_LOCK(pp);
+		if ((pp->p_flag & P_WEXIT) != 0) {
+			/*
+			 * This can happen if the child was created with
+			 * rfork(2).  Userspace tracing cannot work reliably in
+			 * such a scenario, but we can at least try.
+			 */
+			PROC_UNLOCK(pp);
+			sx_sunlock(&proctree_lock);
+			return (-1);
+		}
+		_PHOLD_LITE(pp);
+		PROC_UNLOCK(pp);
+	}
 	sx_sunlock(&proctree_lock);
-	pp = NULL;
 
 	rm_rlock(&fasttrap_tp_lock, &tracker);
 #endif
@@ -1051,11 +1066,32 @@ fasttrap_pid_probe(struct trapframe *tf)
 	if (tp == NULL) {
 #ifdef illumos
 		mutex_exit(pid_mtx);
+		return (-1);
 #else
 		rm_runlock(&fasttrap_tp_lock, &tracker);
+		gen = atomic_load_acq_64(&pp->p_fasttrap_tp_gen);
+		if (pp != p)
+			PRELE(pp);
+		if (curthread->t_fasttrap_tp_gen != gen) {
+			/*
+			 * At least one tracepoint associated with this PID has
+			 * been removed from the table since #BP was raised.
+			 * Speculate that we hit a tracepoint that has since
+			 * been removed, and retry the instruction.
+			 */
+			curthread->t_fasttrap_tp_gen = gen;
+#ifdef __amd64
+			tf->tf_rip = pc;
+#else
+			tf->tf_eip = pc;
 #endif
+			return (0);
+		}
 		return (-1);
+#endif
 	}
+	if (pp != p)
+		PRELE(pp);
 
 	/*
 	 * Set the program counter to the address of the traced instruction

Modified: stable/12/sys/cddl/dev/dtrace/dtrace_cddl.h
==============================================================================
--- stable/12/sys/cddl/dev/dtrace/dtrace_cddl.h	Sun Mar 17 18:31:48 2019	(r345246)
+++ stable/12/sys/cddl/dev/dtrace/dtrace_cddl.h	Sun Mar 17 20:30:27 2019	(r345247)
@@ -37,7 +37,7 @@ typedef struct kdtrace_proc {
 	u_int64_t	p_dtrace_count;		/* Number of DTrace tracepoints */
 	void		*p_dtrace_helpers;	/* DTrace helpers, if any */
 	int		p_dtrace_model;
-
+	uint64_t	p_fasttrap_tp_gen;	/* Tracepoint hash table gen */
 } kdtrace_proc_t;
 
 /*
@@ -86,6 +86,7 @@ typedef struct kdtrace_thread {
 	u_int64_t	td_hrtime;	/* Last time on cpu. */
 	void		*td_dtrace_sscr; /* Saved scratch space location. */
 	void		*td_systrace_args; /* syscall probe arguments. */
+	uint64_t	td_fasttrap_tp_gen; /* Tracepoint hash table gen. */
 } kdtrace_thread_t;
 
 /*
@@ -113,10 +114,12 @@ typedef struct kdtrace_thread {
 #define	t_dtrace_regv	td_dtrace->td_dtrace_regv
 #define	t_dtrace_sscr	td_dtrace->td_dtrace_sscr
 #define	t_dtrace_systrace_args	td_dtrace->td_systrace_args
+#define	t_fasttrap_tp_gen	td_dtrace->td_fasttrap_tp_gen
 #define	p_dtrace_helpers	p_dtrace->p_dtrace_helpers
 #define	p_dtrace_count	p_dtrace->p_dtrace_count
 #define	p_dtrace_probes	p_dtrace->p_dtrace_probes
 #define	p_model		p_dtrace->p_dtrace_model
+#define	p_fasttrap_tp_gen	p_dtrace->p_fasttrap_tp_gen
 
 #define	DATAMODEL_NATIVE	0
 #ifdef __amd64__

Modified: stable/12/sys/modules/dtrace/Makefile
==============================================================================
--- stable/12/sys/modules/dtrace/Makefile	Sun Mar 17 18:31:48 2019	(r345246)
+++ stable/12/sys/modules/dtrace/Makefile	Sun Mar 17 20:30:27 2019	(r345247)
@@ -19,9 +19,6 @@ SUBDIR+=	fasttrap fbt systrace_linux
 .if ${MACHINE_CPUARCH} == "amd64"
 SUBDIR+=	systrace_linux32
 .endif
-.if ${MACHINE_CPUARCH} == "powerpc"
-SUBDIR+=	fbt fasttrap
-.endif
 .if ${MACHINE_CPUARCH} == "amd64" || \
     ${MACHINE_CPUARCH} == "aarch64" || \
     ${MACHINE_ARCH} == "mips64" || \
@@ -31,7 +28,9 @@ SUBDIR+=	systrace_freebsd32
 .if ${MACHINE_CPUARCH} == "aarch64" || \
     ${MACHINE_CPUARCH} == "arm" || \
     ${MACHINE_CPUARCH} == "mips" || \
+    ${MACHINE_CPUARCH} == "powerpc" || \
     ${MACHINE_CPUARCH} == "riscv"
 SUBDIR+=	fbt
 .endif
+
 .include <bsd.subdir.mk>



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