Skip to contentSkip to conversation

perf parse-events: Avoid string for PE_BP_COLON, PE_BP_SLASH

PATCH In review Ian Rogers 2 participants 3 messages Jun 13, 2023 perf events

lore
Share & tools
lore

Ian Rogers

author reviewer
#0 lore raw

PATCH 1/1 perf parse-events: Avoid string for PE_BP_COLON, PE_BP_SLASH

There's no need to read the string ':' or '/' for PE_BP_COLON or PE_BP_SLASH and doing so causes parse-events.y to leak memory.

The original patch has a committer note about not using these tokens presumably as yacc spotted they were a memory leak because no %destructor could be run. Remove the unused token workaround as there is now no value associated with these tokens.

Fixes f0617f526cb0 ("perf parse: Allow config terms with breakpoints")
---
 tools/perf/util/parse-events.h | 4 ----
 tools/perf/util/parse-events.l | 4 ++--
 tools/perf/util/parse-events.y | 9 ---------
 3 files changed, 2 insertions(+), 15 deletions(-)
tools/perf/util/parse-events.h +0 -4
index 5fdc1f33f57e..b0eb95f93e9c 100644
@@ -228,10 +228,6 @@ void parse_events_error__handle(struct parse_events_error *err, int idx,
 void parse_events_error__print(struct parse_events_error *err,
 			       const char *event);
 
-static inline void parse_events_unused_value(const void *x __maybe_unused)
-{
-}
-
 #ifdef HAVE_LIBELF_SUPPORT
 /*
  * If the probe point starts with '%',
tools/perf/util/parse-events.l +2 -2
index 7629af3d5c7c..99335ec586ae 100644
@@ -315,13 +315,13 @@ r0x{num_raw_hex}	{ return str(yyscanner, PE_RAW); }
 	 * are the same, so trailing context can be used disambiguate the two
 	 * cases.
 	 */
-":"/{modifier_bp}	{ return str(yyscanner, PE_BP_COLON); }
+":"/{modifier_bp}	{ return PE_BP_COLON; }
 	/*
 	 * The slash before memory length can get mixed up with the slash before
 	 * config terms. Fortunately config terms do not start with a numeric
 	 * digit, so trailing context can be used disambiguate the two cases.
 	 */
-"/"/{digit}		{ return str(yyscanner, PE_BP_SLASH); }
+"/"/{digit}		{ return PE_BP_SLASH; }
 "/"/{non_digit}		{ BEGIN(config); return '/'; }
 {num_dec}		{ return value(yyscanner, 10); }
 {num_hex}		{ return value(yyscanner, 16); }
tools/perf/util/parse-events.y +0 -9
index 0c3d086cc22a..9f28d4b5502f 100644
@@ -80,8 +80,6 @@ static void free_list_evsel(struct list_head* list_evsel)
 %type <str> PE_LEGACY_CACHE
 %type <str> PE_MODIFIER_EVENT
 %type <str> PE_MODIFIER_BP
-%type <str> PE_BP_COLON
-%type <str> PE_BP_SLASH
 %type <str> PE_EVENT_NAME
 %type <str> PE_KERNEL_PMU_EVENT PE_PMU_EVENT_FAKE
 %type <str> PE_DRV_CFG_TERM
@@ -510,9 +508,6 @@ PE_PREFIX_MEM PE_VALUE PE_BP_SLASH PE_VALUE PE_BP_COLON PE_MODIFIER_BP opt_event
 	struct list_head *list;
 	int err;
 
-	parse_events_unused_value(&$3);
-	parse_events_unused_value(&$5);
-
 	list = alloc_list();
 	ABORT_ON(!list);
 	err = parse_events_add_breakpoint(_parse_state, list,
@@ -531,8 +526,6 @@ PE_PREFIX_MEM PE_VALUE PE_BP_SLASH PE_VALUE opt_event_config
 	struct list_head *list;
 	int err;
 
-	parse_events_unused_value(&$3);
-
 	list = alloc_list();
 	ABORT_ON(!list);
 	err = parse_events_add_breakpoint(_parse_state, list,
@@ -550,8 +543,6 @@ PE_PREFIX_MEM PE_VALUE PE_BP_COLON PE_MODIFIER_BP opt_event_config
 	struct list_head *list;
 	int err;
 
-	parse_events_unused_value(&$3);
-
 	list = alloc_list();
 	ABORT_ON(!list);
 	err = parse_events_add_breakpoint(_parse_state, list,
Signature · 1 line
2.41.0.162.gfafddb0af9-goog
#1 lore raw

Em Tue, Jun 13, 2023 at 11:26:29AM -0700, Ian Rogers escreveu:

Quoted 7 lines

There's no need to read the string ':' or '/' for PE_BP_COLON or PE_BP_SLASH and doing so causes parse-events.y to leak memory.

The original patch has a committer note about not using these tokens presumably as yacc spotted they were a memory leak because no %destructor could be run. Remove the unused token workaround as there is now no value associated with these tokens.

It looked like the compiler was the one warning (-Wother) about args not being used, didn't made it clear those were possible memory leaks :-\

util/parse-events.y:508.24-34: warning: unused value: $3 [-Wother]
508 | PE_PREFIX_MEM PE_VALUE PE_BP_SLASH PE_VALUE PE_BP_COLON PE_MODIFIER_BP opt_event_config

Anyway, I'll apply and test your patch.

- Arnaldo

Quoted 87 lines
Fixes f0617f526cb0 ("perf parse: Allow config terms with breakpoints")
---
 tools/perf/util/parse-events.h | 4 ----
 tools/perf/util/parse-events.l | 4 ++--
 tools/perf/util/parse-events.y | 9 ---------
 3 files changed, 2 insertions(+), 15 deletions(-)
tools/perf/util/parse-events.h +0 -4
index 5fdc1f33f57e..b0eb95f93e9c 100644
@@ -228,10 +228,6 @@ void parse_events_error__handle(struct parse_events_error *err, int idx,
 void parse_events_error__print(struct parse_events_error *err,
 			       const char *event);
 
-static inline void parse_events_unused_value(const void *x __maybe_unused)
-{
-}
-
 #ifdef HAVE_LIBELF_SUPPORT
 /*
  * If the probe point starts with '%',
tools/perf/util/parse-events.l +2 -2
index 7629af3d5c7c..99335ec586ae 100644
@@ -315,13 +315,13 @@ r0x{num_raw_hex}	{ return str(yyscanner, PE_RAW); }
 	 * are the same, so trailing context can be used disambiguate the two
 	 * cases.
 	 */
-":"/{modifier_bp}	{ return str(yyscanner, PE_BP_COLON); }
+":"/{modifier_bp}	{ return PE_BP_COLON; }
 	/*
 	 * The slash before memory length can get mixed up with the slash before
 	 * config terms. Fortunately config terms do not start with a numeric
 	 * digit, so trailing context can be used disambiguate the two cases.
 	 */
-"/"/{digit}		{ return str(yyscanner, PE_BP_SLASH); }
+"/"/{digit}		{ return PE_BP_SLASH; }
 "/"/{non_digit}		{ BEGIN(config); return '/'; }
 {num_dec}		{ return value(yyscanner, 10); }
 {num_hex}		{ return value(yyscanner, 16); }
tools/perf/util/parse-events.y +0 -9
index 0c3d086cc22a..9f28d4b5502f 100644
@@ -80,8 +80,6 @@ static void free_list_evsel(struct list_head* list_evsel)
 %type <str> PE_LEGACY_CACHE
 %type <str> PE_MODIFIER_EVENT
 %type <str> PE_MODIFIER_BP
-%type <str> PE_BP_COLON
-%type <str> PE_BP_SLASH
 %type <str> PE_EVENT_NAME
 %type <str> PE_KERNEL_PMU_EVENT PE_PMU_EVENT_FAKE
 %type <str> PE_DRV_CFG_TERM
@@ -510,9 +508,6 @@ PE_PREFIX_MEM PE_VALUE PE_BP_SLASH PE_VALUE PE_BP_COLON PE_MODIFIER_BP opt_event
 	struct list_head *list;
 	int err;
 
-	parse_events_unused_value(&$3);
-	parse_events_unused_value(&$5);
-
 	list = alloc_list();
 	ABORT_ON(!list);
 	err = parse_events_add_breakpoint(_parse_state, list,
@@ -531,8 +526,6 @@ PE_PREFIX_MEM PE_VALUE PE_BP_SLASH PE_VALUE opt_event_config
 	struct list_head *list;
 	int err;
 
-	parse_events_unused_value(&$3);
-
 	list = alloc_list();
 	ABORT_ON(!list);
 	err = parse_events_add_breakpoint(_parse_state, list,
@@ -550,8 +543,6 @@ PE_PREFIX_MEM PE_VALUE PE_BP_COLON PE_MODIFIER_BP opt_event_config
 	struct list_head *list;
 	int err;
 
-	parse_events_unused_value(&$3);
-
 	list = alloc_list();
 	ABORT_ON(!list);
 	err = parse_events_add_breakpoint(_parse_state, list,
Signature · 1 line
2.41.0.162.gfafddb0af9-goog
Signature · 1 line
- Arnaldo
author reviewer
#2 lore raw
Quoted 13 lines · Arnaldo Carvalho de Melo

Em Tue, Jun 13, 2023 at 11:26:29AM -0700, Ian Rogers escreveu:

Quoted 7 lines

There's no need to read the string ':' or '/' for PE_BP_COLON or PE_BP_SLASH and doing so causes parse-events.y to leak memory.

The original patch has a committer note about not using these tokens presumably as yacc spotted they were a memory leak because no %destructor could be run. Remove the unused token workaround as there is now no value associated with these tokens.

It looked like the compiler was the one warning (-Wother) about args not being used, didn't made it clear those were possible memory leaks :-\

Quoted 6 lines

util/parse-events.y:508.24-34: warning: unused value: $3 [-Wother]
508 | PE_PREFIX_MEM PE_VALUE PE_BP_SLASH PE_VALUE PE_BP_COLON PE_MODIFIER_BP opt_event_config

Anyway, I'll apply and test your patch.

- Arnaldo

Thanks, I couldn't repro the problem but I know that's because I don't have your full container set up. Given my ignorance please help me fix the commit message, or I can update it in a v2 set, or possibly we'll need to bring back the unused logic.

Ian

Quoted 92 lines
Quoted 86 lines
Fixes f0617f526cb0 ("perf parse: Allow config terms with breakpoints")
---
 tools/perf/util/parse-events.h | 4 ----
 tools/perf/util/parse-events.l | 4 ++--
 tools/perf/util/parse-events.y | 9 ---------
 3 files changed, 2 insertions(+), 15 deletions(-)
tools/perf/util/parse-events.h +0 -4
index 5fdc1f33f57e..b0eb95f93e9c 100644
@@ -228,10 +228,6 @@ void parse_events_error__handle(struct parse_events_error *err, int idx,
 void parse_events_error__print(struct parse_events_error *err,
                             const char *event);
 
-static inline void parse_events_unused_value(const void *x __maybe_unused)
-{
-}
-
 #ifdef HAVE_LIBELF_SUPPORT
 /*
  * If the probe point starts with '%',
tools/perf/util/parse-events.l +2 -2
index 7629af3d5c7c..99335ec586ae 100644
@@ -315,13 +315,13 @@ r0x{num_raw_hex}        { return str(yyscanner, PE_RAW); }
       * are the same, so trailing context can be used disambiguate the two
       * cases.
       */
-":"/{modifier_bp}    { return str(yyscanner, PE_BP_COLON); }
+":"/{modifier_bp}    { return PE_BP_COLON; }
      /*
       * The slash before memory length can get mixed up with the slash before
       * config terms. Fortunately config terms do not start with a numeric
       * digit, so trailing context can be used disambiguate the two cases.
       */
-"/"/{digit}          { return str(yyscanner, PE_BP_SLASH); }
+"/"/{digit}          { return PE_BP_SLASH; }
 "/"/{non_digit}              { BEGIN(config); return '/'; }
 {num_dec}            { return value(yyscanner, 10); }
 {num_hex}            { return value(yyscanner, 16); }
tools/perf/util/parse-events.y +0 -10
index 0c3d086cc22a..9f28d4b5502f 100644
@@ -80,8 +80,6 @@ static void free_list_evsel(struct list_head* list_evsel)
 %type <str> PE_LEGACY_CACHE
 %type <str> PE_MODIFIER_EVENT
 %type <str> PE_MODIFIER_BP
-%type <str> PE_BP_COLON
-%type <str> PE_BP_SLASH
 %type <str> PE_EVENT_NAME
 %type <str> PE_KERNEL_PMU_EVENT PE_PMU_EVENT_FAKE
 %type <str> PE_DRV_CFG_TERM
@@ -510,9 +508,6 @@ PE_PREFIX_MEM PE_VALUE PE_BP_SLASH PE_VALUE PE_BP_COLON PE_MODIFIER_BP opt_event
      struct list_head *list;
      int err;
 
-     parse_events_unused_value(&$3);
-     parse_events_unused_value(&$5);
-
      list = alloc_list();
      ABORT_ON(!list);
      err = parse_events_add_breakpoint(_parse_state, list,
@@ -531,8 +526,6 @@ PE_PREFIX_MEM PE_VALUE PE_BP_SLASH PE_VALUE opt_event_config
      struct list_head *list;
      int err;
 
-     parse_events_unused_value(&$3);
-
      list = alloc_list();
      ABORT_ON(!list);
      err = parse_events_add_breakpoint(_parse_state, list,
@@ -550,8 +543,6 @@ PE_PREFIX_MEM PE_VALUE PE_BP_COLON PE_MODIFIER_BP opt_event_config
      struct list_head *list;
      int err;
 
-     parse_events_unused_value(&$3);
-
      list = alloc_list();
      ABORT_ON(!list);
      err = parse_events_add_breakpoint(_parse_state, list,
--
Quoted 2 lines

2.41.0.162.gfafddb0af9-goog

--

- Arnaldo