Skip to contentSkip to conversation

perf pmu: Make parser reentrant

PATCH In review Ian Rogers 4 participants 7 messages Apr 6, 2023 perf events

lore
Share & tools
lore

Ian Rogers

author reviewer
#0 lore raw

PATCH v3 1/2 perf pmu: Make parser reentrant

By default bison uses global state for compatibility with yacc. Make the parser reentrant so that it may be used in asynchronous and multithreaded situations.

---
 tools/perf/util/pmu.c | 26 ++++++++++++++++++++------
 tools/perf/util/pmu.h |  2 +-
 tools/perf/util/pmu.l | 17 ++++++++++++-----
 tools/perf/util/pmu.y |  5 ++++-
 4 files changed, 37 insertions(+), 13 deletions(-)
tools/perf/util/pmu.c +20 -6
index 78a407b42ad1..96ef317bac41 100644
@@ -24,6 +24,8 @@
 #include "evsel.h"
 #include "pmu.h"
 #include "pmus.h"
+#include "pmu-bison.h"
+#include "pmu-flex.h"
 #include "parse-events.h"
 #include "print-events.h"
 #include "header.h"
@@ -57,9 +59,6 @@ struct perf_pmu_format {
 	struct list_head list;
 };
 
-int perf_pmu_parse(struct list_head *list, char *name);
-extern FILE *perf_pmu_in;
-
 static bool hybrid_scanned;
 
 static struct perf_pmu *perf_pmu__find2(int dirfd, const char *name);
@@ -81,6 +80,8 @@ int perf_pmu__format_parse(int dirfd, struct list_head *head)
 	while (!ret && (evt_ent = readdir(format_dir))) {
 		char *name = evt_ent->d_name;
 		int fd;
+		void *scanner;
+		FILE *file;
 
 		if (!strcmp(name, ".") || !strcmp(name, ".."))
 			continue;
@@ -91,9 +92,22 @@ int perf_pmu__format_parse(int dirfd, struct list_head *head)
 		if (fd < 0)
 			break;
 
-		perf_pmu_in = fdopen(fd, "r");
-		ret = perf_pmu_parse(head, name);
-		fclose(perf_pmu_in);
+		file = fdopen(fd, "r");
+		if (!file) {
+			close(fd);
+			break;
+		}
+
+		ret = perf_pmu_lex_init(&scanner);
+		if (ret) {
+			fclose(file);
+			break;
+		}
+
+		perf_pmu_set_in(file, scanner);
+		ret = perf_pmu_parse(head, name, scanner);
+		perf_pmu_lex_destroy(scanner);
+		fclose(file);
 	}
 
 	closedir(format_dir);
tools/perf/util/pmu.h +1 -1
index 32c3a75bca0e..d53618c65c92 100644
@@ -206,7 +206,7 @@ int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
 			  struct perf_pmu_info *info);
 struct list_head *perf_pmu__alias(struct perf_pmu *pmu,
 				  struct list_head *head_terms);
-void perf_pmu_error(struct list_head *list, char *name, char const *msg);
+void perf_pmu_error(struct list_head *list, char *name, void *scanner, char const *msg);
 
 int perf_pmu__new_format(struct list_head *list, char *name,
 			 int config, unsigned long *bits);
tools/perf/util/pmu.l +12 -5
index 58b4926cfaca..67b247be693b 100644
@@ -1,4 +1,6 @@
 %option prefix="perf_pmu_"
+%option reentrant
+%option bison-bridge
 
 %{
 #include <stdlib.h>
@@ -6,16 +8,21 @@
 #include "pmu.h"
 #include "pmu-bison.h"
 
-static int value(int base)
+char *perf_pmu_get_text(yyscan_t yyscanner);
+YYSTYPE *perf_pmu_get_lval(yyscan_t yyscanner);
+
+static int value(yyscan_t scanner, int base)
 {
+	YYSTYPE *yylval = perf_pmu_get_lval(scanner);
+	char *text = perf_pmu_get_text(scanner);
 	long num;
 
 	errno = 0;
-	num = strtoul(perf_pmu_text, NULL, base);
+	num = strtoul(text, NULL, base);
 	if (errno)
 		return PP_ERROR;
 
-	perf_pmu_lval.num = num;
+	yylval->num = num;
 	return PP_VALUE;
 }
 
@@ -25,7 +32,7 @@ num_dec         [0-9]+
 
 %%
 
-{num_dec}	{ return value(10); }
+{num_dec}	{ return value(yyscanner, 10); }
 config		{ return PP_CONFIG; }
 -		{ return '-'; }
 :		{ return ':'; }
@@ -35,7 +42,7 @@ config		{ return PP_CONFIG; }
 
 %%
 
-int perf_pmu_wrap(void)
+int perf_pmu_wrap(void *scanner __maybe_unused)
 {
 	return 1;
 }
tools/perf/util/pmu.y +4 -1
index e675d79a0274..dff4e892ac4d 100644
@@ -1,6 +1,8 @@
-
+%define api.pure full
 %parse-param {struct list_head *format}
 %parse-param {char *name}
+%parse-param {void *scanner}
+%lex-param {void* scanner}
 
 %{
 
@@ -78,6 +80,7 @@ PP_VALUE
 
 void perf_pmu_error(struct list_head *list __maybe_unused,
 		    char *name __maybe_unused,
+		    void *scanner __maybe_unused,
 		    char const *msg __maybe_unused)
 {
 }
Signature · 1 line
2.40.0.348.gf938b09366-goog
author reviewer
#1 lore raw

PATCH v3 2/2 perf pmu: Fix a few potential fd leaks

Ensure fd is closed on error paths.

---
 tools/perf/util/pmu.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
tools/perf/util/pmu.c +10 -2
index 96ef317bac41..9eedbfc9e863 100644
@@ -494,9 +494,13 @@ static int pmu_aliases_parse(int dirfd, struct list_head *head)
 			continue;
 
 		fd = openat(dirfd, name, O_RDONLY);
+		if (fd == -1) {
+			pr_debug("Cannot open %s\n", name);
+			continue;
+		}
 		file = fdopen(fd, "r");
 		if (!file) {
-			pr_debug("Cannot open %s\n", name);
+			close(fd);
 			continue;
 		}
 
@@ -1882,9 +1886,13 @@ int perf_pmu__caps_parse(struct perf_pmu *pmu)
 			continue;
 
 		fd = openat(caps_fd, name, O_RDONLY);
+		if (fd == -1)
+			continue;
 		file = fdopen(fd, "r");
-		if (!file)
+		if (!file) {
+			close(fd);
 			continue;
+		}
 
 		if (!fgets(value, sizeof(value), file) ||
 		    (perf_pmu__new_caps(&pmu->caps, name, value) < 0)) {
Signature · 1 line
2.40.0.348.gf938b09366-goog
maintainer Acked-by on v3 2/2
#2 lore raw

Re: [PATCH v3 2/2] perf pmu: Fix a few potential fd leaks

Quoted 4 lines · Ian Rogers

Ensure fd is closed on error paths.

Thanks,
Namhyung

Quoted 41 lines
---
 tools/perf/util/pmu.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
tools/perf/util/pmu.c +10 -3
index 96ef317bac41..9eedbfc9e863 100644
@@ -494,9 +494,13 @@ static int pmu_aliases_parse(int dirfd, struct list_head *head)
                        continue;
 
                fd = openat(dirfd, name, O_RDONLY);
+               if (fd == -1) {
+                       pr_debug("Cannot open %s\n", name);
+                       continue;
+               }
                file = fdopen(fd, "r");
                if (!file) {
-                       pr_debug("Cannot open %s\n", name);
+                       close(fd);
                        continue;
                }
 
@@ -1882,9 +1886,13 @@ int perf_pmu__caps_parse(struct perf_pmu *pmu)
                        continue;
 
                fd = openat(caps_fd, name, O_RDONLY);
+               if (fd == -1)
+                       continue;
                file = fdopen(fd, "r");
-               if (!file)
+               if (!file) {
+                       close(fd);
                        continue;
+               }
 
                if (!fgets(value, sizeof(value), file) ||
                    (perf_pmu__new_caps(&pmu->caps, name, value) < 0)) {
--

2.40.0.348.gf938b09366-goog

#3 lore raw

Re: [PATCH v3 2/2] perf pmu: Fix a few potential fd leaks

In reply to Ian Rogers

Em Wed, Apr 05, 2023 at 11:52:24PM -0700, Ian Rogers escreveu:

Quoted 1 line

Ensure fd is closed on error paths.

Was this reported by Jiri?

- Arnaldo

Quoted 42 lines
---
 tools/perf/util/pmu.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
tools/perf/util/pmu.c +10 -2
index 96ef317bac41..9eedbfc9e863 100644
@@ -494,9 +494,13 @@ static int pmu_aliases_parse(int dirfd, struct list_head *head)
 			continue;
 
 		fd = openat(dirfd, name, O_RDONLY);
+		if (fd == -1) {
+			pr_debug("Cannot open %s\n", name);
+			continue;
+		}
 		file = fdopen(fd, "r");
 		if (!file) {
-			pr_debug("Cannot open %s\n", name);
+			close(fd);
 			continue;
 		}
 
@@ -1882,9 +1886,13 @@ int perf_pmu__caps_parse(struct perf_pmu *pmu)
 			continue;
 
 		fd = openat(caps_fd, name, O_RDONLY);
+		if (fd == -1)
+			continue;
 		file = fdopen(fd, "r");
-		if (!file)
+		if (!file) {
+			close(fd);
 			continue;
+		}
 
 		if (!fgets(value, sizeof(value), file) ||
 		    (perf_pmu__new_caps(&pmu->caps, name, value) < 0)) {
Signature · 1 line
2.40.0.348.gf938b09366-goog
Signature · 1 line
- Arnaldo
author reviewer
#4 lore raw

Re: [PATCH v3 2/2] perf pmu: Fix a few potential fd leaks

Quoted 5 lines · Arnaldo Carvalho de Melo

Em Wed, Apr 05, 2023 at 11:52:24PM -0700, Ian Rogers escreveu:

Quoted 1 line

Ensure fd is closed on error paths.

Was this reported by Jiri?

Jiri's one was in the code I modified, whereas these were from Namhyung's previous openat change. It is the same issue, I'm agnostic on whether it needs a tag or not.

Thanks,
Ian

Quoted 48 lines

- Arnaldo

Quoted 40 lines
---
 tools/perf/util/pmu.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
tools/perf/util/pmu.c +10 -3
index 96ef317bac41..9eedbfc9e863 100644
@@ -494,9 +494,13 @@ static int pmu_aliases_parse(int dirfd, struct list_head *head)
                      continue;
 
              fd = openat(dirfd, name, O_RDONLY);
+             if (fd == -1) {
+                     pr_debug("Cannot open %s\n", name);
+                     continue;
+             }
              file = fdopen(fd, "r");
              if (!file) {
-                     pr_debug("Cannot open %s\n", name);
+                     close(fd);
                      continue;
              }
 
@@ -1882,9 +1886,13 @@ int perf_pmu__caps_parse(struct perf_pmu *pmu)
                      continue;
 
              fd = openat(caps_fd, name, O_RDONLY);
+             if (fd == -1)
+                     continue;
              file = fdopen(fd, "r");
-             if (!file)
+             if (!file) {
+                     close(fd);
                      continue;
+             }
 
              if (!fgets(value, sizeof(value), file) ||
                  (perf_pmu__new_caps(&pmu->caps, name, value) < 0)) {
--
Quoted 2 lines

2.40.0.348.gf938b09366-goog

--

- Arnaldo

Acked-by on v3 2/2
#5 lore raw

Re: [PATCH v3 2/2] perf pmu: Fix a few potential fd leaks

In reply to Ian Rogers
Quoted 3 lines · Ian Rogers

Ensure fd is closed on error paths.

nice catch

thanks,
jirka

Quoted 41 lines
---
 tools/perf/util/pmu.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
tools/perf/util/pmu.c +10 -2
index 96ef317bac41..9eedbfc9e863 100644
@@ -494,9 +494,13 @@ static int pmu_aliases_parse(int dirfd, struct list_head *head)
 			continue;
 
 		fd = openat(dirfd, name, O_RDONLY);
+		if (fd == -1) {
+			pr_debug("Cannot open %s\n", name);
+			continue;
+		}
 		file = fdopen(fd, "r");
 		if (!file) {
-			pr_debug("Cannot open %s\n", name);
+			close(fd);
 			continue;
 		}
 
@@ -1882,9 +1886,13 @@ int perf_pmu__caps_parse(struct perf_pmu *pmu)
 			continue;
 
 		fd = openat(caps_fd, name, O_RDONLY);
+		if (fd == -1)
+			continue;
 		file = fdopen(fd, "r");
-		if (!file)
+		if (!file) {
+			close(fd);
 			continue;
+		}
 
 		if (!fgets(value, sizeof(value), file) ||
 		    (perf_pmu__new_caps(&pmu->caps, name, value) < 0)) {
Signature · 1 line
2.40.0.348.gf938b09366-goog
Acked-by on v3 1/2
#6 lore raw
In reply to Ian Rogers
Quoted 5 lines · Ian Rogers

By default bison uses global state for compatibility with yacc. Make the parser reentrant so that it may be used in asynchronous and multithreaded situations.

jirka

Quoted 157 lines
---
 tools/perf/util/pmu.c | 26 ++++++++++++++++++++------
 tools/perf/util/pmu.h |  2 +-
 tools/perf/util/pmu.l | 17 ++++++++++++-----
 tools/perf/util/pmu.y |  5 ++++-
 4 files changed, 37 insertions(+), 13 deletions(-)
tools/perf/util/pmu.c +20 -6
index 78a407b42ad1..96ef317bac41 100644
@@ -24,6 +24,8 @@
 #include "evsel.h"
 #include "pmu.h"
 #include "pmus.h"
+#include "pmu-bison.h"
+#include "pmu-flex.h"
 #include "parse-events.h"
 #include "print-events.h"
 #include "header.h"
@@ -57,9 +59,6 @@ struct perf_pmu_format {
 	struct list_head list;
 };
 
-int perf_pmu_parse(struct list_head *list, char *name);
-extern FILE *perf_pmu_in;
-
 static bool hybrid_scanned;
 
 static struct perf_pmu *perf_pmu__find2(int dirfd, const char *name);
@@ -81,6 +80,8 @@ int perf_pmu__format_parse(int dirfd, struct list_head *head)
 	while (!ret && (evt_ent = readdir(format_dir))) {
 		char *name = evt_ent->d_name;
 		int fd;
+		void *scanner;
+		FILE *file;
 
 		if (!strcmp(name, ".") || !strcmp(name, ".."))
 			continue;
@@ -91,9 +92,22 @@ int perf_pmu__format_parse(int dirfd, struct list_head *head)
 		if (fd < 0)
 			break;
 
-		perf_pmu_in = fdopen(fd, "r");
-		ret = perf_pmu_parse(head, name);
-		fclose(perf_pmu_in);
+		file = fdopen(fd, "r");
+		if (!file) {
+			close(fd);
+			break;
+		}
+
+		ret = perf_pmu_lex_init(&scanner);
+		if (ret) {
+			fclose(file);
+			break;
+		}
+
+		perf_pmu_set_in(file, scanner);
+		ret = perf_pmu_parse(head, name, scanner);
+		perf_pmu_lex_destroy(scanner);
+		fclose(file);
 	}
 
 	closedir(format_dir);
tools/perf/util/pmu.h +1 -1
index 32c3a75bca0e..d53618c65c92 100644
@@ -206,7 +206,7 @@ int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
 			  struct perf_pmu_info *info);
 struct list_head *perf_pmu__alias(struct perf_pmu *pmu,
 				  struct list_head *head_terms);
-void perf_pmu_error(struct list_head *list, char *name, char const *msg);
+void perf_pmu_error(struct list_head *list, char *name, void *scanner, char const *msg);
 
 int perf_pmu__new_format(struct list_head *list, char *name,
 			 int config, unsigned long *bits);
tools/perf/util/pmu.l +12 -5
index 58b4926cfaca..67b247be693b 100644
@@ -1,4 +1,6 @@
 %option prefix="perf_pmu_"
+%option reentrant
+%option bison-bridge
 
 %{
 #include <stdlib.h>
@@ -6,16 +8,21 @@
 #include "pmu.h"
 #include "pmu-bison.h"
 
-static int value(int base)
+char *perf_pmu_get_text(yyscan_t yyscanner);
+YYSTYPE *perf_pmu_get_lval(yyscan_t yyscanner);
+
+static int value(yyscan_t scanner, int base)
 {
+	YYSTYPE *yylval = perf_pmu_get_lval(scanner);
+	char *text = perf_pmu_get_text(scanner);
 	long num;
 
 	errno = 0;
-	num = strtoul(perf_pmu_text, NULL, base);
+	num = strtoul(text, NULL, base);
 	if (errno)
 		return PP_ERROR;
 
-	perf_pmu_lval.num = num;
+	yylval->num = num;
 	return PP_VALUE;
 }
 
@@ -25,7 +32,7 @@ num_dec         [0-9]+
 
 %%
 
-{num_dec}	{ return value(10); }
+{num_dec}	{ return value(yyscanner, 10); }
 config		{ return PP_CONFIG; }
 -		{ return '-'; }
 :		{ return ':'; }
@@ -35,7 +42,7 @@ config		{ return PP_CONFIG; }
 
 %%
 
-int perf_pmu_wrap(void)
+int perf_pmu_wrap(void *scanner __maybe_unused)
 {
 	return 1;
 }
tools/perf/util/pmu.y +4 -1
index e675d79a0274..dff4e892ac4d 100644
@@ -1,6 +1,8 @@
-
+%define api.pure full
 %parse-param {struct list_head *format}
 %parse-param {char *name}
+%parse-param {void *scanner}
+%lex-param {void* scanner}
 
 %{
 
@@ -78,6 +80,7 @@ PP_VALUE
 
 void perf_pmu_error(struct list_head *list __maybe_unused,
 		    char *name __maybe_unused,
+		    void *scanner __maybe_unused,
 		    char const *msg __maybe_unused)
 {
 }
Signature · 1 line
2.40.0.348.gf938b09366-goog