Skip to contentSkip to conversation

ptrace: introduce ptrace_tracer() helper

RFC PATCH In review Oleg Nesterov 1 participant 1 message May 30, 2009 ptrace support

lore
Share & tools
lore

Oleg Nesterov

author maintainer
#0 lore raw

RFC PATCH v3 9/12 ptrace: introduce ptrace_tracer() helper

Introduce ptrace_tracer() to simplify/cleanup the code which needs the tracer and checks task_ptrace(). From now nobody else uses ->tracer except ptrace_link/ptrace_unlink.

---

 include/linux/ptrace.h |   17 ++++++++++++-----
 kernel/ptrace.c        |    2 +-
 kernel/signal.c        |   20 +++++++++-----------
 3 files changed, 22 insertions(+), 17 deletions(-)
include/linux/ptrace.h renamed from include/linux/ptrace.h~09_PT_TRACER +12 -5
@@ -119,10 +119,17 @@ static inline int task_ptrace(struct tas
 		task->ptrace_ctx->flags : 0;
 }
 
-static inline int ptrace_reparented(struct task_struct *child)
+static inline struct task_struct *ptrace_tracer(struct task_struct *task)
 {
-	return unlikely(task_ptrace(child)) &&
-		child->ptrace_ctx->tracer != child->real_parent;
+	if (task->ptrace_ctx)
+		return task->ptrace_ctx->tracer;
+	return NULL;
+}
+
+static inline bool ptrace_reparented(struct task_struct *child)
+{
+	struct task_struct *tracer = ptrace_tracer(child);
+	return unlikely(tracer) && tracer != child->real_parent;
 }
 
 static inline void ptrace_unlink(struct task_struct *child)
@@ -170,9 +177,9 @@ static inline void ptrace_init_task(stru
 	INIT_LIST_HEAD(&child->ptrace_entry);
 	INIT_LIST_HEAD(&child->ptraced);
 
-	if (unlikely(child->ptrace_ctx) && task_ptrace(current))
+	if (unlikely(child->ptrace_ctx) && ptrace_tracer(current))
 		ptrace_link(child, task_ptrace(current),
-				current->ptrace_ctx->tracer);
+				ptrace_tracer(current));
 }
 
 /**
kernel/ptrace.c renamed from kernel/ptrace.c~09_PT_TRACER +1 -1
@@ -108,7 +108,7 @@ int ptrace_check_attach(struct task_stru
 	 * be changed by us so it's not changing right after this.
 	 */
 	read_lock(&tasklist_lock);
-	if (task_ptrace(child) && child->ptrace_ctx->tracer == current) {
+	if (ptrace_tracer(child) == current) {
 		ret = 0;
 		/*
 		 * child->sighand can't be NULL, release_task()
kernel/signal.c renamed from kernel/signal.c~09_PT_TRACER +9 -11
@@ -1411,10 +1411,7 @@ int do_notify_parent(struct task_struct 
 	BUG_ON(!task_ptrace(tsk) &&
 	       (tsk->group_leader != tsk || !thread_group_empty(tsk)));
 
-	if (task_ptrace(tsk))
-		parent = tsk->ptrace_ctx->tracer;
-	else
-		parent = tsk->real_parent;
+	parent = ptrace_tracer(tsk) ?: tsk->real_parent;
 
 	info.si_signo = sig;
 	info.si_errno = 0;
@@ -1489,9 +1486,8 @@ static void do_notify_parent_cldstop(str
 	struct task_struct *parent;
 	struct sighand_struct *sighand;
 
-	if (task_ptrace(tsk))
-		parent = tsk->ptrace_ctx->tracer;
-	else {
+	parent = ptrace_tracer(tsk);
+	if (likely(!parent)) {
 		tsk = tsk->group_leader;
 		parent = tsk->real_parent;
 	}
@@ -1538,7 +1534,9 @@ static void do_notify_parent_cldstop(str
 
 static inline int may_ptrace_stop(void)
 {
-	if (!likely(task_ptrace(current)))
+	struct task_struct *tracer = ptrace_tracer(current);
+
+	if (!likely(tracer))
 		return 0;
 	/*
 	 * Are we in the middle of do_coredump?
@@ -1550,7 +1548,7 @@ static inline int may_ptrace_stop(void)
 	 * is safe to enter schedule().
 	 */
 	if (unlikely(current->mm->core_state) &&
-	    unlikely(current->mm == current->ptrace_ctx->tracer->mm))
+	    unlikely(current->mm == tracer->mm))
 		return 0;
 
 	return 1;
@@ -1783,8 +1781,8 @@ static int ptrace_signal(int signr, sigi
 		info->si_code = SI_USER;
 
 		rcu_read_lock();
-		tracer = current->ptrace_ctx->tracer;
-		if (task_ptrace(current)) {
+		tracer = ptrace_tracer(current);
+		if (likely(tracer)) {
 			info->si_pid = task_pid_vnr(tracer);
 			info->si_uid = task_uid(tracer);
 		} else {