linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] perf expr: Force encapsulation on expr_id_data
@ 2020-08-26 15:30 Ian Rogers
  2020-08-26 15:57 ` Jiri Olsa
  0 siblings, 1 reply; 6+ messages in thread
From: Ian Rogers @ 2020-08-26 15:30 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Mark Rutland, Alexander Shishkin, Jiri Olsa, Namhyung Kim,
	Kajol Jain, Andi Kleen, Jin Yao, Kan Liang, Adrian Hunter,
	Leo Yan, linux-kernel
  Cc: Stephane Eranian, Ian Rogers

This patch resolves some undefined behavior where variables in
expr_id_data were accessed (for debugging) without being defined. To
better enforce the tagged union behavior, the struct is moved into
expr.c and accessors provided. Tag values (kinds) are explicitly
identified.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/util/expr.c        | 68 ++++++++++++++++++++++++++++++-----
 tools/perf/util/expr.h        | 17 +++------
 tools/perf/util/expr.y        |  2 +-
 tools/perf/util/metricgroup.c |  4 +--
 4 files changed, 66 insertions(+), 25 deletions(-)

diff --git a/tools/perf/util/expr.c b/tools/perf/util/expr.c
index 53482ef53c41..a850fd0be3ee 100644
--- a/tools/perf/util/expr.c
+++ b/tools/perf/util/expr.c
@@ -17,6 +17,29 @@
 extern int expr_debug;
 #endif
 
+struct expr_id_data {
+	union {
+		double val;
+		struct {
+			double val;
+			const char *metric_name;
+			const char *metric_expr;
+		} ref;
+		struct expr_id	*parent;
+	};
+
+	enum {
+		/* Holding a double value. */
+		EXPR_ID_DATA__VALUE,
+		/* Reference to another metric. */
+		EXPR_ID_DATA__REF,
+		/* A reference but the value has been computed. */
+		EXPR_ID_DATA__REF_VALUE,
+		/* A parent is remembered for the recursion check. */
+		EXPR_ID_DATA__PARENT,
+	} kind;
+};
+
 static size_t key_hash(const void *key, void *ctx __maybe_unused)
 {
 	const char *str = (const char *)key;
@@ -48,6 +71,7 @@ int expr__add_id(struct expr_parse_ctx *ctx, const char *id)
 		return -ENOMEM;
 
 	data_ptr->parent = ctx->parent;
+	data_ptr->kind = EXPR_ID_DATA__PARENT;
 
 	ret = hashmap__set(&ctx->ids, id, data_ptr,
 			   (const void **)&old_key, (void **)&old_data);
@@ -69,7 +93,7 @@ int expr__add_id_val(struct expr_parse_ctx *ctx, const char *id, double val)
 	if (!data_ptr)
 		return -ENOMEM;
 	data_ptr->val = val;
-	data_ptr->is_ref = false;
+	data_ptr->kind = EXPR_ID_DATA__VALUE;
 
 	ret = hashmap__set(&ctx->ids, id, data_ptr,
 			   (const void **)&old_key, (void **)&old_data);
@@ -114,8 +138,7 @@ int expr__add_ref(struct expr_parse_ctx *ctx, struct metric_ref *ref)
 	 */
 	data_ptr->ref.metric_name = ref->metric_name;
 	data_ptr->ref.metric_expr = ref->metric_expr;
-	data_ptr->ref.counted = false;
-	data_ptr->is_ref = true;
+	data_ptr->kind = EXPR_ID_DATA__REF;
 
 	ret = hashmap__set(&ctx->ids, name, data_ptr,
 			   (const void **)&old_key, (void **)&old_data);
@@ -148,17 +171,30 @@ int expr__resolve_id(struct expr_parse_ctx *ctx, const char *id,
 
 	data = *datap;
 
-	pr_debug2("lookup: is_ref %d, counted %d, val %f: %s\n",
-		  data->is_ref, data->ref.counted, data->val, id);
-
-	if (data->is_ref && !data->ref.counted) {
-		data->ref.counted = true;
+	switch (data->kind) {
+	case EXPR_ID_DATA__VALUE:
+		pr_debug2("lookup(%s): val %f\n", id, data->val);
+		break;
+	case EXPR_ID_DATA__PARENT:
+		pr_debug2("lookup(%s): parent %s\n", id, data->parent->id);
+		break;
+	case EXPR_ID_DATA__REF:
+		pr_debug2("lookup(%s): ref metric name %s\n", id,
+			data->ref.metric_name);
 		pr_debug("processing metric: %s ENTRY\n", id);
-		if (expr__parse(&data->val, ctx, data->ref.metric_expr, 1)) {
+		data->kind = EXPR_ID_DATA__REF_VALUE;
+		if (expr__parse(&data->ref.val, ctx, data->ref.metric_expr, 1)) {
 			pr_debug("%s failed to count\n", id);
 			return -1;
 		}
 		pr_debug("processing metric: %s EXIT: %f\n", id, data->val);
+		break;
+	case EXPR_ID_DATA__REF_VALUE:
+		pr_debug2("lookup(%s): ref val %f metric name %s\n", id,
+			data->ref.val, data->ref.metric_name);
+		break;
+	default:
+		assert(0);  /* Unreachable. */
 	}
 
 	return 0;
@@ -241,3 +277,17 @@ int expr__find_other(const char *expr, const char *one,
 
 	return ret;
 }
+
+double expr_id_data__value(const struct expr_id_data *data)
+{
+	if (data->kind == EXPR_ID_DATA__VALUE)
+		return data->val;
+	assert(data->kind == EXPR_ID_DATA__REF_VALUE);
+	return data->ref.val;
+}
+
+struct expr_id *expr_id_data__parent(struct expr_id_data *data)
+{
+	assert(data->kind == EXPR_ID_DATA__PARENT);
+	return data->parent;
+}
diff --git a/tools/perf/util/expr.h b/tools/perf/util/expr.h
index fc2b5e824a66..dcf8d19b83c8 100644
--- a/tools/perf/util/expr.h
+++ b/tools/perf/util/expr.h
@@ -23,19 +23,7 @@ struct expr_parse_ctx {
 	struct expr_id	*parent;
 };
 
-struct expr_id_data {
-	union {
-		double val;
-		struct {
-			const char *metric_name;
-			const char *metric_expr;
-			bool counted;
-		} ref;
-		struct expr_id	*parent;
-	};
-
-	bool is_ref;
-};
+struct expr_id_data;
 
 struct expr_scanner_ctx {
 	int start_token;
@@ -57,4 +45,7 @@ int expr__parse(double *final_val, struct expr_parse_ctx *ctx,
 int expr__find_other(const char *expr, const char *one,
 		struct expr_parse_ctx *ids, int runtime);
 
+double expr_id_data__value(const struct expr_id_data *data);
+struct expr_id *expr_id_data__parent(struct expr_id_data *data);
+
 #endif
diff --git a/tools/perf/util/expr.y b/tools/perf/util/expr.y
index d34b370391c6..b2ada8f8309a 100644
--- a/tools/perf/util/expr.y
+++ b/tools/perf/util/expr.y
@@ -93,7 +93,7 @@ expr:	  NUMBER
 						YYABORT;
 					}
 
-					$$ = data->val;
+					$$ = expr_id_data__value(data);
 					free($1);
 				}
 	| expr '|' expr		{ $$ = (long)$1 | (long)$3; }
diff --git a/tools/perf/util/metricgroup.c b/tools/perf/util/metricgroup.c
index 8831b964288f..339bfb19a10b 100644
--- a/tools/perf/util/metricgroup.c
+++ b/tools/perf/util/metricgroup.c
@@ -786,7 +786,7 @@ static int recursion_check(struct metric *m, const char *id, struct expr_id **pa
 	if (ret)
 		return ret;
 
-	p = data->parent;
+	p = expr_id_data__parent(data);
 
 	while (p->parent) {
 		if (!strcmp(p->id, id)) {
@@ -807,7 +807,7 @@ static int recursion_check(struct metric *m, const char *id, struct expr_id **pa
 	}
 
 	p->id     = strdup(id);
-	p->parent = data->parent;
+	p->parent = expr_id_data__parent(data);
 	*parent   = p;
 
 	return p->id ? 0 : -ENOMEM;
-- 
2.28.0.297.g1956fa8f8d-goog


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

* Re: [PATCH v2] perf expr: Force encapsulation on expr_id_data
  2020-08-26 15:30 [PATCH v2] perf expr: Force encapsulation on expr_id_data Ian Rogers
@ 2020-08-26 15:57 ` Jiri Olsa
  2020-08-27  7:00   ` kajoljain
  0 siblings, 1 reply; 6+ messages in thread
From: Jiri Olsa @ 2020-08-26 15:57 UTC (permalink / raw)
  To: Ian Rogers
  Cc: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Mark Rutland, Alexander Shishkin, Namhyung Kim, Kajol Jain,
	Andi Kleen, Jin Yao, Kan Liang, Adrian Hunter, Leo Yan,
	linux-kernel, Stephane Eranian

On Wed, Aug 26, 2020 at 08:30:55AM -0700, Ian Rogers wrote:
> This patch resolves some undefined behavior where variables in
> expr_id_data were accessed (for debugging) without being defined. To
> better enforce the tagged union behavior, the struct is moved into
> expr.c and accessors provided. Tag values (kinds) are explicitly
> identified.
> 
> Signed-off-by: Ian Rogers <irogers@google.com>

great, thanks for doing this

Acked-by: Jiri Olsa <jolsa@redhat.com>

jirka


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

* Re: [PATCH v2] perf expr: Force encapsulation on expr_id_data
  2020-08-26 15:57 ` Jiri Olsa
@ 2020-08-27  7:00   ` kajoljain
  2020-09-04  5:53     ` Ian Rogers
  0 siblings, 1 reply; 6+ messages in thread
From: kajoljain @ 2020-08-27  7:00 UTC (permalink / raw)
  To: Jiri Olsa, Ian Rogers
  Cc: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Mark Rutland, Alexander Shishkin, Namhyung Kim, Andi Kleen,
	Jin Yao, Kan Liang, Adrian Hunter, Leo Yan, linux-kernel,
	Stephane Eranian



On 8/26/20 9:27 PM, Jiri Olsa wrote:
> On Wed, Aug 26, 2020 at 08:30:55AM -0700, Ian Rogers wrote:
>> This patch resolves some undefined behavior where variables in
>> expr_id_data were accessed (for debugging) without being defined. To
>> better enforce the tagged union behavior, the struct is moved into
>> expr.c and accessors provided. Tag values (kinds) are explicitly
>> identified.

Reviewed-By: Kajol Jain<kjain@linux.ibm.com>

Thanks,
Kajol Jain
>>
>> Signed-off-by: Ian Rogers <irogers@google.com>
> 
> great, thanks for doing this
> 
> Acked-by: Jiri Olsa <jolsa@redhat.com>
> 
> jirka
> 

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

* Re: [PATCH v2] perf expr: Force encapsulation on expr_id_data
  2020-08-27  7:00   ` kajoljain
@ 2020-09-04  5:53     ` Ian Rogers
  2020-09-04 16:29       ` Arnaldo Carvalho de Melo
  0 siblings, 1 reply; 6+ messages in thread
From: Ian Rogers @ 2020-09-04  5:53 UTC (permalink / raw)
  To: kajoljain
  Cc: Jiri Olsa, Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Mark Rutland, Alexander Shishkin, Namhyung Kim, Andi Kleen,
	Jin Yao, Kan Liang, Adrian Hunter, Leo Yan, LKML,
	Stephane Eranian

On Thu, Aug 27, 2020 at 12:00 AM kajoljain <kjain@linux.ibm.com> wrote:
>
>
>
> On 8/26/20 9:27 PM, Jiri Olsa wrote:
> > On Wed, Aug 26, 2020 at 08:30:55AM -0700, Ian Rogers wrote:
> >> This patch resolves some undefined behavior where variables in
> >> expr_id_data were accessed (for debugging) without being defined. To
> >> better enforce the tagged union behavior, the struct is moved into
> >> expr.c and accessors provided. Tag values (kinds) are explicitly
> >> identified.
>
> Reviewed-By: Kajol Jain<kjain@linux.ibm.com>
>
> Thanks,
> Kajol Jain
> >>
> >> Signed-off-by: Ian Rogers <irogers@google.com>
> >
> > great, thanks for doing this
> >
> > Acked-by: Jiri Olsa <jolsa@redhat.com>
> >
> > jirka
> >

Thanks for the reviews! Arnaldo could this get merged? Thanks!
Ian

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

* Re: [PATCH v2] perf expr: Force encapsulation on expr_id_data
  2020-09-04  5:53     ` Ian Rogers
@ 2020-09-04 16:29       ` Arnaldo Carvalho de Melo
       [not found]         ` <CAP-5=fW7bF4PJpNQnDnf--RVtmYQ+nKC-OoU_wYjaR4cuAXZfg@mail.gmail.com>
  0 siblings, 1 reply; 6+ messages in thread
From: Arnaldo Carvalho de Melo @ 2020-09-04 16:29 UTC (permalink / raw)
  To: Ian Rogers
  Cc: kajoljain, Jiri Olsa, Peter Zijlstra, Ingo Molnar, Mark Rutland,
	Alexander Shishkin, Namhyung Kim, Andi Kleen, Jin Yao, Kan Liang,
	Adrian Hunter, Leo Yan, LKML, Stephane Eranian

Em Thu, Sep 03, 2020 at 10:53:16PM -0700, Ian Rogers escreveu:
> On Thu, Aug 27, 2020 at 12:00 AM kajoljain <kjain@linux.ibm.com> wrote:
> >
> >
> >
> > On 8/26/20 9:27 PM, Jiri Olsa wrote:
> > > On Wed, Aug 26, 2020 at 08:30:55AM -0700, Ian Rogers wrote:
> > >> This patch resolves some undefined behavior where variables in
> > >> expr_id_data were accessed (for debugging) without being defined. To
> > >> better enforce the tagged union behavior, the struct is moved into
> > >> expr.c and accessors provided. Tag values (kinds) are explicitly
> > >> identified.
> >
> > Reviewed-By: Kajol Jain<kjain@linux.ibm.com>
> >
> > Thanks,
> > Kajol Jain
> > >>
> > >> Signed-off-by: Ian Rogers <irogers@google.com>
> > >
> > > great, thanks for doing this
> > >
> > > Acked-by: Jiri Olsa <jolsa@redhat.com>
> > >
> > > jirka
> > >
> 
> Thanks for the reviews! Arnaldo could this get merged? Thanks!

I'll get this and the other outstanding patches into perf/core soon as I
got urgent stuff already merged by Linus,

Thanks!

- Arnaldo

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

* Re: [PATCH v2] perf expr: Force encapsulation on expr_id_data
       [not found]         ` <CAP-5=fW7bF4PJpNQnDnf--RVtmYQ+nKC-OoU_wYjaR4cuAXZfg@mail.gmail.com>
@ 2020-11-16 17:10           ` Arnaldo Carvalho de Melo
  0 siblings, 0 replies; 6+ messages in thread
From: Arnaldo Carvalho de Melo @ 2020-11-16 17:10 UTC (permalink / raw)
  To: Ian Rogers
  Cc: kajoljain, Jiri Olsa, Peter Zijlstra, Ingo Molnar, Mark Rutland,
	Alexander Shishkin, Namhyung Kim, Andi Kleen, Jin Yao, Kan Liang,
	Adrian Hunter, Leo Yan, LKML, Stephane Eranian

Em Fri, Nov 13, 2020 at 10:38:28AM -0800, Ian Rogers escreveu:
> On Fri, Sep 4, 2020 at 9:29 AM Arnaldo Carvalho de Melo <acme@kernel.org>
> wrote:
> 
> > Em Thu, Sep 03, 2020 at 10:53:16PM -0700, Ian Rogers escreveu:
> > > On Thu, Aug 27, 2020 at 12:00 AM kajoljain <kjain@linux.ibm.com> wrote:
> > > >
> > > >
> > > >
> > > > On 8/26/20 9:27 PM, Jiri Olsa wrote:
> > > > > On Wed, Aug 26, 2020 at 08:30:55AM -0700, Ian Rogers wrote:
> > > > >> This patch resolves some undefined behavior where variables in
> > > > >> expr_id_data were accessed (for debugging) without being defined. To
> > > > >> better enforce the tagged union behavior, the struct is moved into
> > > > >> expr.c and accessors provided. Tag values (kinds) are explicitly
> > > > >> identified.
> > > >
> > > > Reviewed-By: Kajol Jain<kjain@linux.ibm.com>
> > > >
> > > > Thanks,
> > > > Kajol Jain
> > > > >>
> > > > >> Signed-off-by: Ian Rogers <irogers@google.com>
> > > > >
> > > > > great, thanks for doing this
> > > > >
> > > > > Acked-by: Jiri Olsa <jolsa@redhat.com>
> > > > >
> > > > > jirka
> > > > >
> > >
> > > Thanks for the reviews! Arnaldo could this get merged? Thanks!
> >
> > I'll get this and the other outstanding patches into perf/core soon as I
> > got urgent stuff already merged by Linus,
> >
> > Thanks!
> >
> > - Arnaldo
> >
> 
> I just spotted this wasn't merged and will conflict with:
> https://lore.kernel.org/lkml/20201113001651.544348-1-irogers@google.com/
> I can fix that patch when this lands.

Thanks, applied, will test build and push publicly today.

- Arnaldo

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

end of thread, other threads:[~2020-11-16 17:10 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-26 15:30 [PATCH v2] perf expr: Force encapsulation on expr_id_data Ian Rogers
2020-08-26 15:57 ` Jiri Olsa
2020-08-27  7:00   ` kajoljain
2020-09-04  5:53     ` Ian Rogers
2020-09-04 16:29       ` Arnaldo Carvalho de Melo
     [not found]         ` <CAP-5=fW7bF4PJpNQnDnf--RVtmYQ+nKC-OoU_wYjaR4cuAXZfg@mail.gmail.com>
2020-11-16 17:10           ` Arnaldo Carvalho de Melo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).