Skip to contentSkip to conversation

tracing: Fixes to syscall tracing

PATCH In review Steven Rostedt 3 participants 8 messages Jun 20, 2014 – Jun 26, 2014

Subsystems (4)
lore
Share & tools
lore

Steven Rostedt

author maintainer
#0 lore raw

v2 cover tracing: Fixes to syscall tracing

I don't like to do this but I rebased my for-next branch to have the first (for stable) patch not break other archs. This set passed my cross compile test as well as my normal tests.

git://git.kernel.org/pub/scm/linux/kernel/git/rostedt/linux-trace.git
for-next

Head SHA1: 0f6af1fe54b371ab6dfc05d963c0356a43c83226

Oleg Nesterov (3):
tracing: Fix syscall_*regfunc() vs copy_process() race
tracing: Change syscall_*regfunc() to check PF_KTHREAD and use for_each_process_thread()
tracing: syscall_regfunc() should not skip kernel threads

----

 include/trace/syscall.h | 15 +++++++++++++++
 kernel/fork.c           |  2 ++
 kernel/tracepoint.c     | 26 +++++++++++---------------
 3 files changed, 28 insertions(+), 15 deletions(-)
author maintainer
#1 lore raw

PATCH v2 1/3 tracing: Fix syscall_*regfunc() vs copy_process() race

From: Oleg Nesterov <[email protected]>

syscall_regfunc() and syscall_unregfunc() should set/clear TIF_SYSCALL_TRACEPOINT system-wide, but do_each_thread() can race with copy_process() and miss the new child which was not added to the process/thread lists yet.

Change copy_process() to update the child's TIF_SYSCALL_TRACEPOINT under tasklist.

Cc [email protected] # 2.6.33
Fixes a871bd33a6c0 "tracing: Add syscall tracepoints"
---
 include/trace/syscall.h | 15 +++++++++++++++
 kernel/fork.c           |  2 ++
 2 files changed, 17 insertions(+)
include/trace/syscall.h +15 -0
index fed853f3d7aa..9674145e2f6a 100644
@@ -4,6 +4,7 @@
 #include <linux/tracepoint.h>
 #include <linux/unistd.h>
 #include <linux/ftrace_event.h>
+#include <linux/thread_info.h>
 
 #include <asm/ptrace.h>
 
@@ -32,4 +33,18 @@ struct syscall_metadata {
 	struct ftrace_event_call *exit_event;
 };
 
+#if defined(CONFIG_TRACEPOINTS) && defined(CONFIG_HAVE_SYSCALL_TRACEPOINTS)
+static inline void syscall_tracepoint_update(struct task_struct *p)
+{
+	if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
+		set_tsk_thread_flag(p, TIF_SYSCALL_TRACEPOINT);
+	else
+		clear_tsk_thread_flag(p, TIF_SYSCALL_TRACEPOINT);
+}
+#else
+static inline void syscall_tracepoint_update(struct task_struct *p)
+{
+}
+#endif
+
 #endif /* _TRACE_SYSCALL_H */
kernel/fork.c +2 -0
index d2799d1fc952..6a13c46cd87d 100644
@@ -1487,7 +1487,9 @@ static struct task_struct *copy_process(unsigned long clone_flags,
 
 	total_forks++;
 	spin_unlock(&current->sighand->siglock);
+	syscall_tracepoint_update(p);
 	write_unlock_irq(&tasklist_lock);
+
 	proc_fork_connector(p);
 	cgroup_post_fork(p);
 	if (clone_flags & CLONE_THREAD)
Signature · 1 line
2.0.0
Acked-by on v2 1/3
#2 lore raw

Re: [for-next][PATCH v2 1/3] tracing: Fix syscall_*regfunc() vs copy_process() race

Quoted 10 lines · Steven Rostedt

From: Oleg Nesterov <[email protected]>

syscall_regfunc() and syscall_unregfunc() should set/clear TIF_SYSCALL_TRACEPOINT system-wide, but do_each_thread() can race with copy_process() and miss the new child which was not added to the process/thread lists yet.

Change copy_process() to update the child's TIF_SYSCALL_TRACEPOINT under tasklist.

Quoted 7 lines
Cc [email protected] # 2.6.33
Fixes a871bd33a6c0 "tracing: Add syscall tracepoints"
Quoted 59 lines
---
 include/trace/syscall.h | 15 +++++++++++++++
 kernel/fork.c           |  2 ++
 2 files changed, 17 insertions(+)
include/trace/syscall.h +15 -0
index fed853f3d7aa..9674145e2f6a 100644
@@ -4,6 +4,7 @@
 #include <linux/tracepoint.h>
 #include <linux/unistd.h>
 #include <linux/ftrace_event.h>
+#include <linux/thread_info.h>
 
 #include <asm/ptrace.h>
 
@@ -32,4 +33,18 @@ struct syscall_metadata {
 	struct ftrace_event_call *exit_event;
 };
 
+#if defined(CONFIG_TRACEPOINTS) && defined(CONFIG_HAVE_SYSCALL_TRACEPOINTS)
+static inline void syscall_tracepoint_update(struct task_struct *p)
+{
+	if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
+		set_tsk_thread_flag(p, TIF_SYSCALL_TRACEPOINT);
+	else
+		clear_tsk_thread_flag(p, TIF_SYSCALL_TRACEPOINT);
+}
+#else
+static inline void syscall_tracepoint_update(struct task_struct *p)
+{
+}
+#endif
+
 #endif /* _TRACE_SYSCALL_H */
kernel/fork.c +2 -0
index d2799d1fc952..6a13c46cd87d 100644
@@ -1487,7 +1487,9 @@ static struct task_struct *copy_process(unsigned long clone_flags,
 
 	total_forks++;
 	spin_unlock(&current->sighand->siglock);
+	syscall_tracepoint_update(p);
 	write_unlock_irq(&tasklist_lock);
+
 	proc_fork_connector(p);
 	cgroup_post_fork(p);
 	if (clone_flags & CLONE_THREAD)
Signature · 8 lines
2.0.0


--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/
author maintainer
#3 lore raw

Re: [for-next][PATCH v2 1/3] tracing: Fix syscall_*regfunc() vs copy_process() race

Quoted 13 lines · On Fri, 20 Jun 2014 18:11:25 -0700 "Paul E. McKenney"…
Quoted 12 lines · Steven Rostedt

From: Oleg Nesterov <[email protected]>

syscall_regfunc() and syscall_unregfunc() should set/clear TIF_SYSCALL_TRACEPOINT system-wide, but do_each_thread() can race with copy_process() and miss the new child which was not added to the process/thread lists yet.

Change copy_process() to update the child's TIF_SYSCALL_TRACEPOINT under tasklist.

Quoted 8 lines
Quoted 5 lines
Cc [email protected] # 2.6.33
Fixes a871bd33a6c0 "tracing: Add syscall tracepoints"

I don't usually rebase my for-next branch for acks, but I already rebased once for fixing an issue, and it's early in the rc cycle, and this is the first patch on the branch, so I think I will do it.

Thanks!

-- Steve

#4 lore raw

Re: [for-next][PATCH v2 1/3] tracing: Fix syscall_*regfunc() vs copy_process() race

In reply to Steven Rostedt

Hi Steve,

Quoted 9 lines · Steven Rostedt

From: Oleg Nesterov <[email protected]>

syscall_regfunc() and syscall_unregfunc() should set/clear TIF_SYSCALL_TRACEPOINT system-wide, but do_each_thread() can race with copy_process() and miss the new child which was not added to the process/thread lists yet.

Change copy_process() to update the child's TIF_SYSCALL_TRACEPOINT under tasklist.

s/tasklist/tasklist_lock/ ?

Probably I'm too late..

Thanks,
Namhyung

author maintainer
#5 lore raw

Re: [for-next][PATCH v2 1/3] tracing: Fix syscall_*regfunc() vs copy_process() race

Quoted 10 lines · On Tue, 24 Jun 2014 15:55:41 +0900 Namhyung Kim <[email protected]> wrote:

Hi Steve,

Quoted 7 lines · Steven Rostedt

From: Oleg Nesterov <[email protected]>

syscall_regfunc() and syscall_unregfunc() should set/clear TIF_SYSCALL_TRACEPOINT system-wide, but do_each_thread() can race with copy_process() and miss the new child which was not added to the process/thread lists yet.

Quoted 6 lines
Quoted 2 lines

Change copy_process() to update the child's TIF_SYSCALL_TRACEPOINT under tasklist.

s/tasklist/tasklist_lock/ ?

Probably I'm too late..

Yeah, but it's just a change log, and I think people can figure it out (I hope).

-- Steve

author maintainer
#6 lore raw

PATCH v2 2/3 tracing: Change syscall_*regfunc() to check PF_KTHREAD and use for_each_process_thread()

In reply to Steven Rostedt

From: Oleg Nesterov <[email protected]>

1. Remove _irqsafe from syscall_regfunc/syscall_unregfunc, read_lock(tasklist) doesn't need to disable irqs.

2. Change this code to avoid the deprecated do_each_thread() and use for_each_process_thread() (stolen from the patch from Frederic).

3. Change syscall_regfunc() to check PF_KTHREAD to skip the kernel threads, ->mm != NULL is the common mistake.

Note: probably this check should be simply removed, needs another patch.

[[email protected]: s/do_each_thread/for_each_process_thread/]
---
 kernel/tracepoint.c | 24 +++++++++++-------------
 1 file changed, 11 insertions(+), 13 deletions(-)
kernel/tracepoint.c +11 -13
index 33cbd8c203f8..9cf12640de5a 100644
@@ -492,33 +492,31 @@ static int sys_tracepoint_refcount;
 
 void syscall_regfunc(void)
 {
-	unsigned long flags;
-	struct task_struct *g, *t;
+	struct task_struct *p, *t;
 
 	if (!sys_tracepoint_refcount) {
-		read_lock_irqsave(&tasklist_lock, flags);
-		do_each_thread(g, t) {
+		read_lock(&tasklist_lock);
+		for_each_process_thread(p, t) {
 			/* Skip kernel threads. */
-			if (t->mm)
+			if (!(t->flags & PF_KTHREAD))
 				set_tsk_thread_flag(t, TIF_SYSCALL_TRACEPOINT);
-		} while_each_thread(g, t);
-		read_unlock_irqrestore(&tasklist_lock, flags);
+		}
+		read_unlock(&tasklist_lock);
 	}
 	sys_tracepoint_refcount++;
 }
 
 void syscall_unregfunc(void)
 {
-	unsigned long flags;
-	struct task_struct *g, *t;
+	struct task_struct *p, *t;
 
 	sys_tracepoint_refcount--;
 	if (!sys_tracepoint_refcount) {
-		read_lock_irqsave(&tasklist_lock, flags);
-		do_each_thread(g, t) {
+		read_lock(&tasklist_lock);
+		for_each_process_thread(p, t) {
 			clear_tsk_thread_flag(t, TIF_SYSCALL_TRACEPOINT);
-		} while_each_thread(g, t);
-		read_unlock_irqrestore(&tasklist_lock, flags);
+		}
+		read_unlock(&tasklist_lock);
 	}
 }
 #endif
Signature · 1 line
2.0.0
author maintainer
#7 lore raw

PATCH v2 3/3 tracing: syscall_regfunc() should not skip kernel threads

In reply to Steven Rostedt

From: Oleg Nesterov <[email protected]>

syscall_regfunc() ignores the kernel threads because "it has no effect", see cc3b13c1 "Don't trace kernel thread syscalls" which added this check.

However, this means that a user-space task spawned by call_usermodehelper() will run without TIF_SYSCALL_TRACEPOINT if sys_tracepoint_refcount != 0.

Remove this check. The unnecessary report from ret_from_fork path mentioned by cc3b13c1 is no longer possible, see See commit fb45550d76bb5 "make sure that kernel_thread() callbacks call do_exit() themselves".

A kernel_thread() callback can only return and take the int_ret_from_sys_call path after do_execve() succeeds, otherwise the kernel will crash. But in this case it is no longer a kernel thread and thus is needs TIF_SYSCALL_TRACEPOINT.

---
 kernel/tracepoint.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
kernel/tracepoint.c +1 -3
index 9cf12640de5a..3490407dc7b7 100644
@@ -497,9 +497,7 @@ void syscall_regfunc(void)
 	if (!sys_tracepoint_refcount) {
 		read_lock(&tasklist_lock);
 		for_each_process_thread(p, t) {
-			/* Skip kernel threads. */
-			if (!(t->flags & PF_KTHREAD))
-				set_tsk_thread_flag(t, TIF_SYSCALL_TRACEPOINT);
+			set_tsk_thread_flag(t, TIF_SYSCALL_TRACEPOINT);
 		}
 		read_unlock(&tasklist_lock);
 	}
Signature · 1 line
2.0.0