public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/2] perf callchain: Make callchain cursors TLS
@ 2012-05-31  5:43 Namhyung Kim
  2012-05-31  5:43 ` [PATCH/RFC 2/2] perf tools: Check callchain is corrupted Namhyung Kim
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Namhyung Kim @ 2012-05-31  5:43 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: Peter Zijlstra, Paul Mackerras, Ingo Molnar, Namhyung Kim, LKML,
	Sunjin Yang

perf top -G has a race on callchain cursor between main
thread and display thread. Since the callchain cursors
are used locally make them thread-local data would solve
the problem.

Reported-by: Sunjin Yang <fan4326@gmail.com>
Suggested-by: Arnaldo Carvalho de Melo <acme@ghostprotocols.net>
Signed-off-by: Namhyung Kim <namhyung.kim@lge.com>
---
 tools/perf/builtin-report.c |    2 +-
 tools/perf/builtin-top.c    |    2 +-
 tools/perf/util/callchain.c |    2 ++
 tools/perf/util/callchain.h |    2 ++
 tools/perf/util/hist.c      |    7 ++++---
 tools/perf/util/hist.h      |    2 --
 tools/perf/util/session.c   |   14 +++++++-------
 7 files changed, 17 insertions(+), 14 deletions(-)

diff --git a/tools/perf/builtin-report.c b/tools/perf/builtin-report.c
index d58e41445d0d..159ca6435788 100644
--- a/tools/perf/builtin-report.c
+++ b/tools/perf/builtin-report.c
@@ -152,7 +152,7 @@ static int perf_evsel__add_hist_entry(struct perf_evsel *evsel,
 
 	if (symbol_conf.use_callchain) {
 		err = callchain_append(he->callchain,
-				       &evsel->hists.callchain_cursor,
+				       &callchain_cursor,
 				       sample->period);
 		if (err)
 			return err;
diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c
index 6031dce0429f..ea7b600dc652 100644
--- a/tools/perf/builtin-top.c
+++ b/tools/perf/builtin-top.c
@@ -787,7 +787,7 @@ static void perf_event__process_sample(struct perf_tool *tool,
 		}
 
 		if (symbol_conf.use_callchain) {
-			err = callchain_append(he->callchain, &evsel->hists.callchain_cursor,
+			err = callchain_append(he->callchain, &callchain_cursor,
 					       sample->period);
 			if (err)
 				return;
diff --git a/tools/perf/util/callchain.c b/tools/perf/util/callchain.c
index 9f7106a8d9a4..3a6bff47614f 100644
--- a/tools/perf/util/callchain.c
+++ b/tools/perf/util/callchain.c
@@ -18,6 +18,8 @@
 #include "util.h"
 #include "callchain.h"
 
+__thread struct callchain_cursor callchain_cursor;
+
 bool ip_callchain__valid(struct ip_callchain *chain,
 			 const union perf_event *event)
 {
diff --git a/tools/perf/util/callchain.h b/tools/perf/util/callchain.h
index 7f9c0f1ae3a9..3bdb407f9cd9 100644
--- a/tools/perf/util/callchain.h
+++ b/tools/perf/util/callchain.h
@@ -76,6 +76,8 @@ struct callchain_cursor {
 	struct callchain_cursor_node	*curr;
 };
 
+extern __thread struct callchain_cursor callchain_cursor;
+
 static inline void callchain_init(struct callchain_root *root)
 {
 	INIT_LIST_HEAD(&root->node.siblings);
diff --git a/tools/perf/util/hist.c b/tools/perf/util/hist.c
index 1293b5ebea4d..514e2a4b367d 100644
--- a/tools/perf/util/hist.c
+++ b/tools/perf/util/hist.c
@@ -378,7 +378,7 @@ void hist_entry__free(struct hist_entry *he)
  * collapse the histogram
  */
 
-static bool hists__collapse_insert_entry(struct hists *hists,
+static bool hists__collapse_insert_entry(struct hists *hists __used,
 					 struct rb_root *root,
 					 struct hist_entry *he)
 {
@@ -397,8 +397,9 @@ static bool hists__collapse_insert_entry(struct hists *hists,
 			iter->period += he->period;
 			iter->nr_events += he->nr_events;
 			if (symbol_conf.use_callchain) {
-				callchain_cursor_reset(&hists->callchain_cursor);
-				callchain_merge(&hists->callchain_cursor, iter->callchain,
+				callchain_cursor_reset(&callchain_cursor);
+				callchain_merge(&callchain_cursor,
+						iter->callchain,
 						he->callchain);
 			}
 			hist_entry__free(he);
diff --git a/tools/perf/util/hist.h b/tools/perf/util/hist.h
index cfc64e293f90..34bb556d6219 100644
--- a/tools/perf/util/hist.h
+++ b/tools/perf/util/hist.h
@@ -67,8 +67,6 @@ struct hists {
 	struct events_stats	stats;
 	u64			event_stream;
 	u16			col_len[HISTC_NR_COLS];
-	/* Best would be to reuse the session callchain cursor */
-	struct callchain_cursor	callchain_cursor;
 };
 
 struct hist_entry *__hists__add_entry(struct hists *self,
diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c
index 93d355d27109..4eed171d275d 100644
--- a/tools/perf/util/session.c
+++ b/tools/perf/util/session.c
@@ -288,7 +288,8 @@ struct branch_info *machine__resolve_bstack(struct machine *self,
 	return bi;
 }
 
-int machine__resolve_callchain(struct machine *self, struct perf_evsel *evsel,
+int machine__resolve_callchain(struct machine *self,
+			       struct perf_evsel *evsel __used,
 			       struct thread *thread,
 			       struct ip_callchain *chain,
 			       struct symbol **parent)
@@ -297,7 +298,7 @@ int machine__resolve_callchain(struct machine *self, struct perf_evsel *evsel,
 	unsigned int i;
 	int err;
 
-	callchain_cursor_reset(&evsel->hists.callchain_cursor);
+	callchain_cursor_reset(&callchain_cursor);
 
 	for (i = 0; i < chain->nr; i++) {
 		u64 ip;
@@ -333,7 +334,7 @@ int machine__resolve_callchain(struct machine *self, struct perf_evsel *evsel,
 				break;
 		}
 
-		err = callchain_cursor_append(&evsel->hists.callchain_cursor,
+		err = callchain_cursor_append(&callchain_cursor,
 					      ip, al.map, al.sym);
 		if (err)
 			return err;
@@ -1428,7 +1429,6 @@ void perf_event__print_ip(union perf_event *event, struct perf_sample *sample,
 			  int print_sym, int print_dso, int print_symoffset)
 {
 	struct addr_location al;
-	struct callchain_cursor *cursor = &evsel->hists.callchain_cursor;
 	struct callchain_cursor_node *node;
 
 	if (perf_event__preprocess_sample(event, machine, &al, sample,
@@ -1446,10 +1446,10 @@ void perf_event__print_ip(union perf_event *event, struct perf_sample *sample,
 				error("Failed to resolve callchain. Skipping\n");
 			return;
 		}
-		callchain_cursor_commit(cursor);
+		callchain_cursor_commit(&callchain_cursor);
 
 		while (1) {
-			node = callchain_cursor_current(cursor);
+			node = callchain_cursor_current(&callchain_cursor);
 			if (!node)
 				break;
 
@@ -1465,7 +1465,7 @@ void perf_event__print_ip(union perf_event *event, struct perf_sample *sample,
 			}
 			printf("\n");
 
-			callchain_cursor_advance(cursor);
+			callchain_cursor_advance(&callchain_cursor);
 		}
 
 	} else {
-- 
1.7.10.2


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH/RFC 2/2] perf tools: Check callchain is corrupted
  2012-05-31  5:43 [PATCH 1/2] perf callchain: Make callchain cursors TLS Namhyung Kim
@ 2012-05-31  5:43 ` Namhyung Kim
  2012-05-31 13:45   ` Arnaldo Carvalho de Melo
                     ` (2 more replies)
  2012-06-01 17:50 ` [PATCH 1/2] perf callchain: Make callchain cursors TLS Zachary Landau
  2012-06-06  7:00 ` [tip:perf/urgent] " tip-bot for Namhyung Kim
  2 siblings, 3 replies; 9+ messages in thread
From: Namhyung Kim @ 2012-05-31  5:43 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: Peter Zijlstra, Paul Mackerras, Ingo Molnar, Namhyung Kim, LKML,
	Sunjin Yang

We faced segmentation fault on perf top -G at very high sampling
rate due to a corrupted callchain. While the root cause was not
revealed (I failed to figure it out), this patch tries to protect
us from the segfault on such cases.

Reported-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Signed-off-by: Namhyung Kim <namhyung.kim@lge.com>
---
 include/linux/perf_event.h |    4 ++--
 tools/perf/util/session.c  |   14 +++++++++++++-
 2 files changed, 15 insertions(+), 3 deletions(-)

diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h
index 8adf70e9e3cc..0430ea6054ef 100644
--- a/include/linux/perf_event.h
+++ b/include/linux/perf_event.h
@@ -555,6 +555,8 @@ enum perf_event_type {
 	PERF_RECORD_MAX,			/* non-ABI */
 };
 
+#define PERF_MAX_STACK_DEPTH		255
+
 enum perf_callchain_context {
 	PERF_CONTEXT_HV			= (__u64)-32,
 	PERF_CONTEXT_KERNEL		= (__u64)-128,
@@ -609,8 +611,6 @@ struct perf_guest_info_callbacks {
 #include <linux/sysfs.h>
 #include <asm/local.h>
 
-#define PERF_MAX_STACK_DEPTH		255
-
 struct perf_callchain_entry {
 	__u64				nr;
 	__u64				ip[PERF_MAX_STACK_DEPTH];
diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c
index a7de2acac460..fa14e60ed459 100644
--- a/tools/perf/util/session.c
+++ b/tools/perf/util/session.c
@@ -300,6 +300,11 @@ int machine__resolve_callchain(struct machine *self,
 
 	callchain_cursor_reset(&callchain_cursor);
 
+	if (chain->nr > PERF_MAX_STACK_DEPTH) {
+		pr_warning("corrupted callchain. skipping...\n");
+		return 0;
+	}
+
 	for (i = 0; i < chain->nr; i++) {
 		u64 ip;
 		struct addr_location al;
@@ -318,7 +323,14 @@ int machine__resolve_callchain(struct machine *self,
 			case PERF_CONTEXT_USER:
 				cpumode = PERF_RECORD_MISC_USER;	break;
 			default:
-				break;
+				pr_debug("invalid callchain context: "
+					 "%"PRId64"\n", (s64) ip);
+				/*
+				 * It seems the callchain is corrupted.
+				 * Discard all.
+				 */
+				callchain_cursor_reset(&callchain_cursor);
+				return 0;
 			}
 			continue;
 		}
-- 
1.7.10.1


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH/RFC 2/2] perf tools: Check callchain is corrupted
  2012-05-31  5:43 ` [PATCH/RFC 2/2] perf tools: Check callchain is corrupted Namhyung Kim
@ 2012-05-31 13:45   ` Arnaldo Carvalho de Melo
  2012-05-31 14:04     ` Namhyung Kim
  2012-06-01 17:52   ` Zachary Landau
  2012-06-06  7:01   ` [tip:perf/urgent] perf tools: Check if " tip-bot for Namhyung Kim
  2 siblings, 1 reply; 9+ messages in thread
From: Arnaldo Carvalho de Melo @ 2012-05-31 13:45 UTC (permalink / raw)
  To: Namhyung Kim
  Cc: Peter Zijlstra, Paul Mackerras, Ingo Molnar, Namhyung Kim, LKML,
	Sunjin Yang

Em Thu, May 31, 2012 at 02:43:27PM +0900, Namhyung Kim escreveu:
> We faced segmentation fault on perf top -G at very high sampling
> rate due to a corrupted callchain. While the root cause was not
> revealed (I failed to figure it out), this patch tries to protect
> us from the segfault on such cases.

Capping the callchain lenght may even be a feature, not a consistency
check, but I don't know how long, for consistency checking, we should
consider too much, as there are cases of loooong callchains.

Ingo? Peter?

- Arnaldo
 
> Reported-by: Arnaldo Carvalho de Melo <acme@redhat.com>
> Signed-off-by: Namhyung Kim <namhyung.kim@lge.com>
> ---
>  include/linux/perf_event.h |    4 ++--
>  tools/perf/util/session.c  |   14 +++++++++++++-
>  2 files changed, 15 insertions(+), 3 deletions(-)
> 
> diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h
> index 8adf70e9e3cc..0430ea6054ef 100644
> --- a/include/linux/perf_event.h
> +++ b/include/linux/perf_event.h
> @@ -555,6 +555,8 @@ enum perf_event_type {
>  	PERF_RECORD_MAX,			/* non-ABI */
>  };
>  
> +#define PERF_MAX_STACK_DEPTH		255
> +
>  enum perf_callchain_context {
>  	PERF_CONTEXT_HV			= (__u64)-32,
>  	PERF_CONTEXT_KERNEL		= (__u64)-128,
> @@ -609,8 +611,6 @@ struct perf_guest_info_callbacks {
>  #include <linux/sysfs.h>
>  #include <asm/local.h>
>  
> -#define PERF_MAX_STACK_DEPTH		255
> -
>  struct perf_callchain_entry {
>  	__u64				nr;
>  	__u64				ip[PERF_MAX_STACK_DEPTH];
> diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c
> index a7de2acac460..fa14e60ed459 100644
> --- a/tools/perf/util/session.c
> +++ b/tools/perf/util/session.c
> @@ -300,6 +300,11 @@ int machine__resolve_callchain(struct machine *self,
>  
>  	callchain_cursor_reset(&callchain_cursor);
>  
> +	if (chain->nr > PERF_MAX_STACK_DEPTH) {
> +		pr_warning("corrupted callchain. skipping...\n");
> +		return 0;
> +	}
> +
>  	for (i = 0; i < chain->nr; i++) {
>  		u64 ip;
>  		struct addr_location al;
> @@ -318,7 +323,14 @@ int machine__resolve_callchain(struct machine *self,
>  			case PERF_CONTEXT_USER:
>  				cpumode = PERF_RECORD_MISC_USER;	break;
>  			default:
> -				break;
> +				pr_debug("invalid callchain context: "
> +					 "%"PRId64"\n", (s64) ip);
> +				/*
> +				 * It seems the callchain is corrupted.
> +				 * Discard all.
> +				 */
> +				callchain_cursor_reset(&callchain_cursor);
> +				return 0;
>  			}
>  			continue;
>  		}
> -- 
> 1.7.10.1

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH/RFC 2/2] perf tools: Check callchain is corrupted
  2012-05-31 13:45   ` Arnaldo Carvalho de Melo
@ 2012-05-31 14:04     ` Namhyung Kim
  2012-05-31 14:31       ` Arnaldo Carvalho de Melo
  0 siblings, 1 reply; 9+ messages in thread
From: Namhyung Kim @ 2012-05-31 14:04 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: Namhyung Kim, Peter Zijlstra, Paul Mackerras, Ingo Molnar, LKML,
	Sunjin Yang, Arun Sharma

Hi,

2012-05-31 (목), 10:45 -0300, Arnaldo Carvalho de Melo:
> Em Thu, May 31, 2012 at 02:43:27PM +0900, Namhyung Kim escreveu:
> > We faced segmentation fault on perf top -G at very high sampling
> > rate due to a corrupted callchain. While the root cause was not
> > revealed (I failed to figure it out), this patch tries to protect
> > us from the segfault on such cases.
> 
> Capping the callchain lenght may even be a feature, not a consistency
> check, but I don't know how long, for consistency checking, we should
> consider too much, as there are cases of loooong callchains.
> 

Maybe we should consider applying (a part of, at least) Arun's multiple
stack patchset. Also note that Linus complained about the current stack
depth (255) is too long.

  https://lkml.org/lkml/2012/4/20/497


-- 
Regards,
Namhyung Kim



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH/RFC 2/2] perf tools: Check callchain is corrupted
  2012-05-31 14:04     ` Namhyung Kim
@ 2012-05-31 14:31       ` Arnaldo Carvalho de Melo
  0 siblings, 0 replies; 9+ messages in thread
From: Arnaldo Carvalho de Melo @ 2012-05-31 14:31 UTC (permalink / raw)
  To: Namhyung Kim
  Cc: Namhyung Kim, Peter Zijlstra, Paul Mackerras, Ingo Molnar, LKML,
	Sunjin Yang, Arun Sharma

Em Thu, May 31, 2012 at 11:04:37PM +0900, Namhyung Kim escreveu:
> 2012-05-31 (목), 10:45 -0300, Arnaldo Carvalho de Melo:
> > Em Thu, May 31, 2012 at 02:43:27PM +0900, Namhyung Kim escreveu:
> > > We faced segmentation fault on perf top -G at very high sampling
> > > rate due to a corrupted callchain. While the root cause was not
> > > revealed (I failed to figure it out), this patch tries to protect
> > > us from the segfault on such cases.

> > Capping the callchain lenght may even be a feature, not a consistency
> > check, but I don't know how long, for consistency checking, we should
> > consider too much, as there are cases of loooong callchains.

> Maybe we should consider applying (a part of, at least) Arun's multiple
> stack patchset. Also note that Linus complained about the current stack
> depth (255) is too long.

>   https://lkml.org/lkml/2012/4/20/497

Ok, I applied it, 'perf top -G -F 100000' now is holding up, no
segfaults, yay!

Thanks,

- Arnaldo

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 1/2] perf callchain: Make callchain cursors TLS
  2012-05-31  5:43 [PATCH 1/2] perf callchain: Make callchain cursors TLS Namhyung Kim
  2012-05-31  5:43 ` [PATCH/RFC 2/2] perf tools: Check callchain is corrupted Namhyung Kim
@ 2012-06-01 17:50 ` Zachary Landau
  2012-06-06  7:00 ` [tip:perf/urgent] " tip-bot for Namhyung Kim
  2 siblings, 0 replies; 9+ messages in thread
From: Zachary Landau @ 2012-06-01 17:50 UTC (permalink / raw)
  To: Namhyung Kim
  Cc: Arnaldo Carvalho de Melo, Peter Zijlstra, Paul Mackerras,
	Ingo Molnar, Namhyung Kim, LKML, Sunjin Yang

On Thu, May 31, 2012 at 02:43:26PM +0900, Namhyung Kim wrote:
> perf top -G has a race on callchain cursor between main
> thread and display thread. Since the callchain cursors
> are used locally make them thread-local data would solve
> the problem.
> 
> Reported-by: Sunjin Yang <fan4326@gmail.com>
> Suggested-by: Arnaldo Carvalho de Melo <acme@ghostprotocols.net>
> Signed-off-by: Namhyung Kim <namhyung.kim@lge.com>

Acked-by: Zachary Landau <zacharyl@fb.com>

- Zack

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH/RFC 2/2] perf tools: Check callchain is corrupted
  2012-05-31  5:43 ` [PATCH/RFC 2/2] perf tools: Check callchain is corrupted Namhyung Kim
  2012-05-31 13:45   ` Arnaldo Carvalho de Melo
@ 2012-06-01 17:52   ` Zachary Landau
  2012-06-06  7:01   ` [tip:perf/urgent] perf tools: Check if " tip-bot for Namhyung Kim
  2 siblings, 0 replies; 9+ messages in thread
From: Zachary Landau @ 2012-06-01 17:52 UTC (permalink / raw)
  To: Namhyung Kim
  Cc: Arnaldo Carvalho de Melo, Peter Zijlstra, Paul Mackerras,
	Ingo Molnar, Namhyung Kim, LKML, Sunjin Yang

On Thu, May 31, 2012 at 02:43:27PM +0900, Namhyung Kim wrote:
> We faced segmentation fault on perf top -G at very high sampling
> rate due to a corrupted callchain. While the root cause was not
> revealed (I failed to figure it out), this patch tries to protect
> us from the segfault on such cases.
> 
> Reported-by: Arnaldo Carvalho de Melo <acme@redhat.com>
> Signed-off-by: Namhyung Kim <namhyung.kim@lge.com>

Acked-by: Zachary Landau <zacharyl@fb.com>

- Zack

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [tip:perf/urgent] perf callchain: Make callchain cursors TLS
  2012-05-31  5:43 [PATCH 1/2] perf callchain: Make callchain cursors TLS Namhyung Kim
  2012-05-31  5:43 ` [PATCH/RFC 2/2] perf tools: Check callchain is corrupted Namhyung Kim
  2012-06-01 17:50 ` [PATCH 1/2] perf callchain: Make callchain cursors TLS Zachary Landau
@ 2012-06-06  7:00 ` tip-bot for Namhyung Kim
  2 siblings, 0 replies; 9+ messages in thread
From: tip-bot for Namhyung Kim @ 2012-06-06  7:00 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: acme, linux-kernel, paulus, mingo, hpa, mingo, a.p.zijlstra, acme,
	namhyung.kim, namhyung, fan4326, tglx

Commit-ID:  472606458f3e1ced5fe3cc5f04e90a6b5a4732cf
Gitweb:     http://git.kernel.org/tip/472606458f3e1ced5fe3cc5f04e90a6b5a4732cf
Author:     Namhyung Kim <namhyung.kim@lge.com>
AuthorDate: Thu, 31 May 2012 14:43:26 +0900
Committer:  Arnaldo Carvalho de Melo <acme@redhat.com>
CommitDate: Thu, 31 May 2012 10:47:12 -0300

perf callchain: Make callchain cursors TLS

perf top -G has a race on callchain cursor between main thread and
display thread. Since the callchain cursors are used locally make them
thread-local data would solve the problem.

Signed-off-by: Namhyung Kim <namhyung.kim@lge.com>
Reported-by: Sunjin Yang <fan4326@gmail.com>
Suggested-by: Arnaldo Carvalho de Melo <acme@ghostprotocols.net>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Namhyung Kim <namhyung@gmail.com>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Sunjin Yang <fan4326@gmail.com>
Link: http://lkml.kernel.org/r/1338443007-24857-1-git-send-email-namhyung.kim@lge.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/perf/builtin-report.c |    2 +-
 tools/perf/builtin-top.c    |    2 +-
 tools/perf/util/callchain.c |    2 ++
 tools/perf/util/callchain.h |    2 ++
 tools/perf/util/hist.c      |    7 ++++---
 tools/perf/util/hist.h      |    2 --
 tools/perf/util/session.c   |   14 +++++++-------
 7 files changed, 17 insertions(+), 14 deletions(-)

diff --git a/tools/perf/builtin-report.c b/tools/perf/builtin-report.c
index 2400e00..25249f7 100644
--- a/tools/perf/builtin-report.c
+++ b/tools/perf/builtin-report.c
@@ -152,7 +152,7 @@ static int perf_evsel__add_hist_entry(struct perf_evsel *evsel,
 
 	if (symbol_conf.use_callchain) {
 		err = callchain_append(he->callchain,
-				       &evsel->hists.callchain_cursor,
+				       &callchain_cursor,
 				       sample->period);
 		if (err)
 			return err;
diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c
index 871b540..6bb0277 100644
--- a/tools/perf/builtin-top.c
+++ b/tools/perf/builtin-top.c
@@ -787,7 +787,7 @@ static void perf_event__process_sample(struct perf_tool *tool,
 		}
 
 		if (symbol_conf.use_callchain) {
-			err = callchain_append(he->callchain, &evsel->hists.callchain_cursor,
+			err = callchain_append(he->callchain, &callchain_cursor,
 					       sample->period);
 			if (err)
 				return;
diff --git a/tools/perf/util/callchain.c b/tools/perf/util/callchain.c
index 9f7106a..3a6bff4 100644
--- a/tools/perf/util/callchain.c
+++ b/tools/perf/util/callchain.c
@@ -18,6 +18,8 @@
 #include "util.h"
 #include "callchain.h"
 
+__thread struct callchain_cursor callchain_cursor;
+
 bool ip_callchain__valid(struct ip_callchain *chain,
 			 const union perf_event *event)
 {
diff --git a/tools/perf/util/callchain.h b/tools/perf/util/callchain.h
index 7f9c0f1..3bdb407 100644
--- a/tools/perf/util/callchain.h
+++ b/tools/perf/util/callchain.h
@@ -76,6 +76,8 @@ struct callchain_cursor {
 	struct callchain_cursor_node	*curr;
 };
 
+extern __thread struct callchain_cursor callchain_cursor;
+
 static inline void callchain_init(struct callchain_root *root)
 {
 	INIT_LIST_HEAD(&root->node.siblings);
diff --git a/tools/perf/util/hist.c b/tools/perf/util/hist.c
index 1293b5e..514e2a4 100644
--- a/tools/perf/util/hist.c
+++ b/tools/perf/util/hist.c
@@ -378,7 +378,7 @@ void hist_entry__free(struct hist_entry *he)
  * collapse the histogram
  */
 
-static bool hists__collapse_insert_entry(struct hists *hists,
+static bool hists__collapse_insert_entry(struct hists *hists __used,
 					 struct rb_root *root,
 					 struct hist_entry *he)
 {
@@ -397,8 +397,9 @@ static bool hists__collapse_insert_entry(struct hists *hists,
 			iter->period += he->period;
 			iter->nr_events += he->nr_events;
 			if (symbol_conf.use_callchain) {
-				callchain_cursor_reset(&hists->callchain_cursor);
-				callchain_merge(&hists->callchain_cursor, iter->callchain,
+				callchain_cursor_reset(&callchain_cursor);
+				callchain_merge(&callchain_cursor,
+						iter->callchain,
 						he->callchain);
 			}
 			hist_entry__free(he);
diff --git a/tools/perf/util/hist.h b/tools/perf/util/hist.h
index cfc64e2..34bb556 100644
--- a/tools/perf/util/hist.h
+++ b/tools/perf/util/hist.h
@@ -67,8 +67,6 @@ struct hists {
 	struct events_stats	stats;
 	u64			event_stream;
 	u16			col_len[HISTC_NR_COLS];
-	/* Best would be to reuse the session callchain cursor */
-	struct callchain_cursor	callchain_cursor;
 };
 
 struct hist_entry *__hists__add_entry(struct hists *self,
diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c
index 4820614..3b6f8e4 100644
--- a/tools/perf/util/session.c
+++ b/tools/perf/util/session.c
@@ -288,7 +288,8 @@ struct branch_info *machine__resolve_bstack(struct machine *self,
 	return bi;
 }
 
-int machine__resolve_callchain(struct machine *self, struct perf_evsel *evsel,
+int machine__resolve_callchain(struct machine *self,
+			       struct perf_evsel *evsel __used,
 			       struct thread *thread,
 			       struct ip_callchain *chain,
 			       struct symbol **parent)
@@ -297,7 +298,7 @@ int machine__resolve_callchain(struct machine *self, struct perf_evsel *evsel,
 	unsigned int i;
 	int err;
 
-	callchain_cursor_reset(&evsel->hists.callchain_cursor);
+	callchain_cursor_reset(&callchain_cursor);
 
 	for (i = 0; i < chain->nr; i++) {
 		u64 ip;
@@ -333,7 +334,7 @@ int machine__resolve_callchain(struct machine *self, struct perf_evsel *evsel,
 				break;
 		}
 
-		err = callchain_cursor_append(&evsel->hists.callchain_cursor,
+		err = callchain_cursor_append(&callchain_cursor,
 					      ip, al.map, al.sym);
 		if (err)
 			return err;
@@ -1428,7 +1429,6 @@ void perf_event__print_ip(union perf_event *event, struct perf_sample *sample,
 			  int print_sym, int print_dso, int print_symoffset)
 {
 	struct addr_location al;
-	struct callchain_cursor *cursor = &evsel->hists.callchain_cursor;
 	struct callchain_cursor_node *node;
 
 	if (perf_event__preprocess_sample(event, machine, &al, sample,
@@ -1446,10 +1446,10 @@ void perf_event__print_ip(union perf_event *event, struct perf_sample *sample,
 				error("Failed to resolve callchain. Skipping\n");
 			return;
 		}
-		callchain_cursor_commit(cursor);
+		callchain_cursor_commit(&callchain_cursor);
 
 		while (1) {
-			node = callchain_cursor_current(cursor);
+			node = callchain_cursor_current(&callchain_cursor);
 			if (!node)
 				break;
 
@@ -1465,7 +1465,7 @@ void perf_event__print_ip(union perf_event *event, struct perf_sample *sample,
 			}
 			printf("\n");
 
-			callchain_cursor_advance(cursor);
+			callchain_cursor_advance(&callchain_cursor);
 		}
 
 	} else {

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [tip:perf/urgent] perf tools: Check if callchain is corrupted
  2012-05-31  5:43 ` [PATCH/RFC 2/2] perf tools: Check callchain is corrupted Namhyung Kim
  2012-05-31 13:45   ` Arnaldo Carvalho de Melo
  2012-06-01 17:52   ` Zachary Landau
@ 2012-06-06  7:01   ` tip-bot for Namhyung Kim
  2 siblings, 0 replies; 9+ messages in thread
From: tip-bot for Namhyung Kim @ 2012-06-06  7:01 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: linux-kernel, paulus, acme, hpa, mingo, a.p.zijlstra,
	namhyung.kim, namhyung, fan4326, tglx

Commit-ID:  114067b69e7b2c691faace0e33db2f04096f668d
Gitweb:     http://git.kernel.org/tip/114067b69e7b2c691faace0e33db2f04096f668d
Author:     Namhyung Kim <namhyung.kim@lge.com>
AuthorDate: Thu, 31 May 2012 14:43:27 +0900
Committer:  Arnaldo Carvalho de Melo <acme@redhat.com>
CommitDate: Thu, 31 May 2012 11:20:34 -0300

perf tools: Check if callchain is corrupted

We faced segmentation fault on perf top -G at very high sampling rate
due to a corrupted callchain. While the root cause was not revealed (I
failed to figure it out), this patch tries to protect us from the
segfault on such cases.

Reported-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Signed-off-by: Namhyung Kim <namhyung.kim@lge.com>
Cc: Namhyung Kim <namhyung@gmail.com>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Sunjin Yang <fan4326@gmail.com>
Link: http://lkml.kernel.org/r/1338443007-24857-2-git-send-email-namhyung.kim@lge.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 include/linux/perf_event.h |    4 ++--
 tools/perf/util/session.c  |   14 +++++++++++++-
 2 files changed, 15 insertions(+), 3 deletions(-)

diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h
index f325786..1817d40 100644
--- a/include/linux/perf_event.h
+++ b/include/linux/perf_event.h
@@ -555,6 +555,8 @@ enum perf_event_type {
 	PERF_RECORD_MAX,			/* non-ABI */
 };
 
+#define PERF_MAX_STACK_DEPTH		255
+
 enum perf_callchain_context {
 	PERF_CONTEXT_HV			= (__u64)-32,
 	PERF_CONTEXT_KERNEL		= (__u64)-128,
@@ -609,8 +611,6 @@ struct perf_guest_info_callbacks {
 #include <linux/sysfs.h>
 #include <asm/local.h>
 
-#define PERF_MAX_STACK_DEPTH		255
-
 struct perf_callchain_entry {
 	__u64				nr;
 	__u64				ip[PERF_MAX_STACK_DEPTH];
diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c
index 3b6f8e4..04d1e33 100644
--- a/tools/perf/util/session.c
+++ b/tools/perf/util/session.c
@@ -300,6 +300,11 @@ int machine__resolve_callchain(struct machine *self,
 
 	callchain_cursor_reset(&callchain_cursor);
 
+	if (chain->nr > PERF_MAX_STACK_DEPTH) {
+		pr_warning("corrupted callchain. skipping...\n");
+		return 0;
+	}
+
 	for (i = 0; i < chain->nr; i++) {
 		u64 ip;
 		struct addr_location al;
@@ -318,7 +323,14 @@ int machine__resolve_callchain(struct machine *self,
 			case PERF_CONTEXT_USER:
 				cpumode = PERF_RECORD_MISC_USER;	break;
 			default:
-				break;
+				pr_debug("invalid callchain context: "
+					 "%"PRId64"\n", (s64) ip);
+				/*
+				 * It seems the callchain is corrupted.
+				 * Discard all.
+				 */
+				callchain_cursor_reset(&callchain_cursor);
+				return 0;
 			}
 			continue;
 		}

^ permalink raw reply related	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2012-06-06  7:02 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-05-31  5:43 [PATCH 1/2] perf callchain: Make callchain cursors TLS Namhyung Kim
2012-05-31  5:43 ` [PATCH/RFC 2/2] perf tools: Check callchain is corrupted Namhyung Kim
2012-05-31 13:45   ` Arnaldo Carvalho de Melo
2012-05-31 14:04     ` Namhyung Kim
2012-05-31 14:31       ` Arnaldo Carvalho de Melo
2012-06-01 17:52   ` Zachary Landau
2012-06-06  7:01   ` [tip:perf/urgent] perf tools: Check if " tip-bot for Namhyung Kim
2012-06-01 17:50 ` [PATCH 1/2] perf callchain: Make callchain cursors TLS Zachary Landau
2012-06-06  7:00 ` [tip:perf/urgent] " tip-bot for Namhyung Kim

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox