lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <1317052535-1765247-4-git-send-email-avagin@openvz.org>
Date:	Mon, 26 Sep 2011 19:55:34 +0400
From:	Andrew Vagin <avagin@...nvz.org>
To:	linux-kernel@...r.kernel.org
Cc:	Steven Rostedt <rostedt@...dmis.org>,
	Frederic Weisbecker <fweisbec@...il.com>,
	Ingo Molnar <mingo@...hat.com>,
	Peter Zijlstra <a.p.zijlstra@...llo.nl>,
	Paul Mackerras <paulus@...ba.org>,
	Arnaldo Carvalho de Melo <acme@...stprotocols.net>,
	avagin@...nvz.org
Subject: [PATCH 3/4] trace: add ability to collect call chain of non-current task.

An event may requires call chain of non-current task.  E.g. You may want
to know where and how long a task is sleeping. A time slice is known
when the task wakes up.  We can send event in this moment, but a target
task isn't "current".

The macro __perf_task set a task for which a call chain will be
collected.

Know issues:
* Now call chains for non-current tasks are collected on x86 only,
  but it may be done for other architectures simply.
* It collects only kernel call chains, because we can't get direct
  access to memory of other processes and this operation should be
  fast enough.

Signed-off-by: Andrew Vagin <avagin@...nvz.org>
---
 arch/x86/kernel/cpu/perf_event.c |    8 +++-----
 include/trace/ftrace.h           |   11 +++++++++--
 kernel/events/core.c             |   30 ++++++++++++++++++------------
 3 files changed, 30 insertions(+), 19 deletions(-)

diff --git a/arch/x86/kernel/cpu/perf_event.c b/arch/x86/kernel/cpu/perf_event.c
index c3d229d..18817a8 100644
--- a/arch/x86/kernel/cpu/perf_event.c
+++ b/arch/x86/kernel/cpu/perf_event.c
@@ -1839,17 +1839,15 @@ void
 perf_callchain_kernel(struct perf_callchain_entry *entry,
 			struct task_struct *tsk, struct pt_regs *regs)
 {
-	if (!regs)
-		return;
-
 	if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
 		/* TODO: We don't support guest os callchain now */
 		return;
 	}
 
-	perf_callchain_store(entry, regs->ip);
+	if (regs)
+		perf_callchain_store(entry, regs->ip);
 
-	dump_trace(NULL, regs, NULL, 0, &backtrace_ops, entry);
+	dump_trace(tsk, regs, NULL, 0, &backtrace_ops, entry);
 }
 
 #ifdef CONFIG_COMPAT
diff --git a/include/trace/ftrace.h b/include/trace/ftrace.h
index 5e4c72a..d03e926 100644
--- a/include/trace/ftrace.h
+++ b/include/trace/ftrace.h
@@ -711,6 +711,9 @@ __attribute__((section("_ftrace_events"))) *__event_##call = &event_##call
 #undef __perf_count
 #define __perf_count(c) __count = (c)
 
+#undef __perf_task
+#define __perf_task(t) __task = (t)
+
 #undef TP_perf_assign
 #define TP_perf_assign(args...) args
 
@@ -722,7 +725,8 @@ perf_trace_##call(void *__data, proto)					\
 	struct ftrace_event_call *event_call = __data;			\
 	struct ftrace_data_offsets_##call __maybe_unused __data_offsets;\
 	struct ftrace_raw_##call *entry;				\
-	struct pt_regs __regs;						\
+	struct pt_regs __regs, *__pregs = &__regs;			\
+	struct task_struct *__task = current;				\
 	u64 __addr = 0, __count = 1;					\
 	struct hlist_head *head;					\
 	int __entry_size;						\
@@ -749,9 +753,12 @@ perf_trace_##call(void *__data, proto)					\
 									\
 	{ assign; }							\
 									\
+	if (__task != current)						\
+		__pregs = NULL;						\
+									\
 	head = this_cpu_ptr(event_call->perf_events);			\
 	perf_tp_event(__addr, __count, entry, __entry_size,		\
-				current, &__regs, head, rctx);		\
+				__task, __pregs, head, rctx);		\
 }
 
 /*
diff --git a/kernel/events/core.c b/kernel/events/core.c
index 41ce4db..2dce0f9 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -2757,16 +2757,16 @@ static struct perf_callchain_entry
 
 	entry->nr = 0;
 
-	if (!user_mode(regs)) {
+	if (!regs || !user_mode(regs)) {
 		perf_callchain_store(entry, PERF_CONTEXT_KERNEL);
 		perf_callchain_kernel(entry, tsk, regs);
-		if (current->mm)
-			regs = task_pt_regs(current);
+		if (tsk->mm)
+			regs = task_pt_regs(tsk);
 		else
 			regs = NULL;
 	}
 
-	if (regs) {
+	if (tsk == current && regs) {
 		perf_callchain_store(entry, PERF_CONTEXT_USER);
 		perf_callchain_user(entry, regs);
 	}
@@ -3746,7 +3746,8 @@ EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
 
 static void __perf_event_header__init_id(struct perf_event_header *header,
 					 struct perf_sample_data *data,
-					 struct perf_event *event)
+					 struct perf_event *event,
+					 struct task_struct *tsk)
 {
 	u64 sample_type = event->attr.sample_type;
 
@@ -3755,8 +3756,8 @@ static void __perf_event_header__init_id(struct perf_event_header *header,
 
 	if (sample_type & PERF_SAMPLE_TID) {
 		/* namespace issues */
-		data->tid_entry.pid = perf_event_pid(event, current);
-		data->tid_entry.tid = perf_event_tid(event, current);
+		data->tid_entry.pid = perf_event_pid(event, tsk);
+		data->tid_entry.tid = perf_event_tid(event, tsk);
 	}
 
 	if (sample_type & PERF_SAMPLE_TIME)
@@ -3779,7 +3780,7 @@ void perf_event_header__init_id(struct perf_event_header *header,
 				struct perf_event *event)
 {
 	if (event->attr.sample_id_all)
-		__perf_event_header__init_id(header, data, event);
+		__perf_event_header__init_id(header, data, event, current);
 }
 
 static void __perf_event__output_id_sample(struct perf_output_handle *handle,
@@ -4000,12 +4001,17 @@ void perf_prepare_sample(struct perf_event_header *header,
 	header->size = sizeof(*header) + event->header_size;
 
 	header->misc = 0;
-	header->misc |= perf_misc_flags(regs);
 
-	__perf_event_header__init_id(header, data, event);
+	__perf_event_header__init_id(header, data, event, tsk);
 
-	if (sample_type & PERF_SAMPLE_IP)
-		data->ip = perf_instruction_pointer(regs);
+	if (regs) {
+		header->misc |= perf_misc_flags(regs);
+		if (sample_type & PERF_SAMPLE_IP)
+			data->ip = perf_instruction_pointer(regs);
+	} else {
+		header->misc |= PERF_RECORD_MISC_KERNEL;
+		data->ip = 0;
+	}
 
 	if (sample_type & PERF_SAMPLE_CALLCHAIN) {
 		int size = 1;
-- 
1.7.1

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@...r.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ