Skip to contentSkip to conversation

perf ftrace: Fix the buffer size in __write_tracing_file

PATCH In review [email protected] 2 participants 5 messages Feb 8, 2018 – Feb 13, 2018 perf events

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 perf ftrace: Fix the buffer size in __write_tracing_file

From: Changbin Du <[email protected]>

The terminal character '\0' should take into account into size of the string buffer. Without this fix, the '--graph-funcs', '--nograph-funcs' and '--trace-funcs' options didn't work as expected when the <func> doesn't exist. If usersapce writes a non-terminated string, the kernel side will always return success but actually no filter applied.

As discussed before, the kernel now support '\0' to mark the end of string:
https://lkml.org/lkml/2018/1/16/116

After this fix in userspace, the perf will report correct error state. Also let it print an error if reset_tracing_files() fails.

The problem:

$ sudo ./perf ftrace -a --graph-depth 1 --graph-funcs abcdefg
 0)   0.140 us    |  rcu_all_qs();
 3)   0.304 us    |  mutex_unlock();
 0)   0.153 us    |  find_vma();
 3)   0.088 us    |  __fsnotify_parent();
 0)   6.145 us    |  handle_mm_fault();
 3)   0.089 us    |  fsnotify();
 3)   0.161 us    |  __sb_end_write();
 3)   0.710 us    |  SyS_close();
 3)   7.848 us    |  exit_to_usermode_loop();

On above example, I specified function filter 'abcdefg' but all functions are enabled. The expected error is hidden.

---
 tools/perf/builtin-ftrace.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
tools/perf/builtin-ftrace.c +4 -2
index 25a42ac..a87e9b3 100644
@@ -69,7 +69,7 @@ static int __write_tracing_file(const char *name, const char *val, bool append)
 {
 	char *file;
 	int fd, ret = -1;
-	ssize_t size = strlen(val);
+	ssize_t size = strlen(val) + 1;
 	int flags = O_WRONLY;
 	char errbuf[512];
 
@@ -280,8 +280,10 @@ static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv)
 	signal(SIGCHLD, sig_handler);
 	signal(SIGPIPE, sig_handler);
 
-	if (reset_tracing_files(ftrace) < 0)
+	if (reset_tracing_files(ftrace) < 0) {
+		pr_err("failed to reset ftrace\n");
 		goto out;
+	}
 
 	/* reset ftrace buffer */
 	if (write_tracing_file("trace", "0") < 0)
Signature · 1 line
2.7.4
maintainer
#1 lore raw

Hello,

Quoted 8 lines · On Thu, Feb 08, 2018 at 04:13:20PM +0800, [email protected] wrote:

From: Changbin Du <[email protected]>

The terminal character '\0' should take into account into size of the string buffer. Without this fix, the '--graph-funcs', '--nograph-funcs' and '--trace-funcs' options didn't work as expected when the <func> doesn't exist. If usersapce writes a non-terminated string, the kernel side will always return success but actually no filter applied.

Quoted 5 lines

As discussed before, the kernel now support '\0' to mark the end of string:
https://lkml.org/lkml/2018/1/16/116

After this fix in userspace, the perf will report correct error state. Also let it print an error if reset_tracing_files() fails.

But what about old kernels? IIRC there was an error with this change.

Thanks,
Namhyung

Quoted 49 lines

The problem:

$ sudo ./perf ftrace -a --graph-depth 1 --graph-funcs abcdefg
 0)   0.140 us    |  rcu_all_qs();
 3)   0.304 us    |  mutex_unlock();
 0)   0.153 us    |  find_vma();
 3)   0.088 us    |  __fsnotify_parent();
 0)   6.145 us    |  handle_mm_fault();
 3)   0.089 us    |  fsnotify();
 3)   0.161 us    |  __sb_end_write();
 3)   0.710 us    |  SyS_close();
 3)   7.848 us    |  exit_to_usermode_loop();

On above example, I specified function filter 'abcdefg' but all functions are enabled. The expected error is hidden.

---
 tools/perf/builtin-ftrace.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
tools/perf/builtin-ftrace.c +4 -2
index 25a42ac..a87e9b3 100644
@@ -69,7 +69,7 @@ static int __write_tracing_file(const char *name, const char *val, bool append)
 {
 	char *file;
 	int fd, ret = -1;
-	ssize_t size = strlen(val);
+	ssize_t size = strlen(val) + 1;
 	int flags = O_WRONLY;
 	char errbuf[512];
 
@@ -280,8 +280,10 @@ static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv)
 	signal(SIGCHLD, sig_handler);
 	signal(SIGPIPE, sig_handler);
 
-	if (reset_tracing_files(ftrace) < 0)
+	if (reset_tracing_files(ftrace) < 0) {
+		pr_err("failed to reset ftrace\n");
 		goto out;
+	}
 
 	/* reset ftrace buffer */
 	if (write_tracing_file("trace", "0") < 0)
Signature · 1 line
2.7.4
#2 lore raw

Hi,

Quoted 11 lines · Namhyung Kim

Hello,

Quoted 8 lines · On Thu, Feb 08, 2018 at 04:13:20PM +0800, [email protected] wrote:

From: Changbin Du <[email protected]>

The terminal character '\0' should take into account into size of the string buffer. Without this fix, the '--graph-funcs', '--nograph-funcs' and '--trace-funcs' options didn't work as expected when the <func> doesn't exist. If usersapce writes a non-terminated string, the kernel side will always return success but actually no filter applied.

Quoted 8 lines
Quoted 5 lines

As discussed before, the kernel now support '\0' to mark the end of string:
https://lkml.org/lkml/2018/1/16/116

After this fix in userspace, the perf will report correct error state. Also let it print an error if reset_tracing_files() fails.

But what about old kernels? IIRC there was an error with this change.

Yes, you're right. I can't find a good compitable change. So what is the compatibilty policy for perf? If it must work with recent kernel, I think the only idea is leave as it was.

Quoted 53 lines

Thanks,
Namhyung

Quoted 49 lines

The problem:

$ sudo ./perf ftrace -a --graph-depth 1 --graph-funcs abcdefg
 0)   0.140 us    |  rcu_all_qs();
 3)   0.304 us    |  mutex_unlock();
 0)   0.153 us    |  find_vma();
 3)   0.088 us    |  __fsnotify_parent();
 0)   6.145 us    |  handle_mm_fault();
 3)   0.089 us    |  fsnotify();
 3)   0.161 us    |  __sb_end_write();
 3)   0.710 us    |  SyS_close();
 3)   7.848 us    |  exit_to_usermode_loop();

On above example, I specified function filter 'abcdefg' but all functions are enabled. The expected error is hidden.

---
 tools/perf/builtin-ftrace.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
tools/perf/builtin-ftrace.c +4 -2
index 25a42ac..a87e9b3 100644
@@ -69,7 +69,7 @@ static int __write_tracing_file(const char *name, const char *val, bool append)
 {
 	char *file;
 	int fd, ret = -1;
-	ssize_t size = strlen(val);
+	ssize_t size = strlen(val) + 1;
 	int flags = O_WRONLY;
 	char errbuf[512];
 
@@ -280,8 +280,10 @@ static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv)
 	signal(SIGCHLD, sig_handler);
 	signal(SIGPIPE, sig_handler);
 
-	if (reset_tracing_files(ftrace) < 0)
+	if (reset_tracing_files(ftrace) < 0) {
+		pr_err("failed to reset ftrace\n");
 		goto out;
+	}
 
 	/* reset ftrace buffer */
 	if (write_tracing_file("trace", "0") < 0)
Signature · 1 line
2.7.4
Signature · 2 lines
Thanks,
Changbin Du
maintainer
#3 lore raw
Quoted 17 lines · Changbin

Hi,

Quoted 14 lines · Namhyung Kim

Hello,

Quoted 11 lines · On Thu, Feb 08, 2018 at 04:13:20PM +0800, [email protected] wrote:

From: Changbin Du <[email protected]>

The terminal character '\0' should take into account into size of the string buffer. Without this fix, the '--graph-funcs', '--nograph-funcs' and '--trace-funcs' options didn't work as expected when the <func> doesn't exist. If usersapce writes a non-terminated string, the kernel side will always return success but actually no filter applied.

As discussed before, the kernel now support '\0' to mark the end of string:
https://lkml.org/lkml/2018/1/16/116

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

After this fix in userspace, the perf will report correct error state. Also let it print an error if reset_tracing_files() fails.

But what about old kernels? IIRC there was an error with this change.

Yes, you're right. I can't find a good compitable change. So what is the compatibilty policy for perf? If it must work with recent kernel, I think the only idea is leave as it was.

It should support *both* of kernels. I suggest adding a space after function name. You can simply call write(fd, " ", 1) IMHO.

Thanks,
Namhyung

Quoted 55 lines
Quoted 51 lines
Quoted 51 lines

The problem:

$ sudo ./perf ftrace -a --graph-depth 1 --graph-funcs abcdefg
 0)   0.140 us    |  rcu_all_qs();
 3)   0.304 us    |  mutex_unlock();
 0)   0.153 us    |  find_vma();
 3)   0.088 us    |  __fsnotify_parent();
 0)   6.145 us    |  handle_mm_fault();
 3)   0.089 us    |  fsnotify();
 3)   0.161 us    |  __sb_end_write();
 3)   0.710 us    |  SyS_close();
 3)   7.848 us    |  exit_to_usermode_loop();

On above example, I specified function filter 'abcdefg' but all functions are enabled. The expected error is hidden.

---
 tools/perf/builtin-ftrace.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
tools/perf/builtin-ftrace.c +4 -2
index 25a42ac..a87e9b3 100644
@@ -69,7 +69,7 @@ static int __write_tracing_file(const char *name, const char *val, bool append)
 {
 	char *file;
 	int fd, ret = -1;
-	ssize_t size = strlen(val);
+	ssize_t size = strlen(val) + 1;
 	int flags = O_WRONLY;
 	char errbuf[512];
 
@@ -280,8 +280,10 @@ static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv)
 	signal(SIGCHLD, sig_handler);
 	signal(SIGPIPE, sig_handler);
 
-	if (reset_tracing_files(ftrace) < 0)
+	if (reset_tracing_files(ftrace) < 0) {
+		pr_err("failed to reset ftrace\n");
 		goto out;
+	}
 
 	/* reset ftrace buffer */
 	if (write_tracing_file("trace", "0") < 0)
Signature · 1 line
2.7.4
Signature · 2 lines
Thanks,
Changbin Du
#4 lore raw
Quoted 21 lines · Namhyung Kim
Quoted 20 lines · Changbin

Hi,

Quoted 17 lines · Namhyung Kim

Hello,

Quoted 13 lines · On Thu, Feb 08, 2018 at 04:13:20PM +0800, [email protected] wrote:

From: Changbin Du <[email protected]>

The terminal character '\0' should take into account into size of the string buffer. Without this fix, the '--graph-funcs', '--nograph-funcs' and '--trace-funcs' options didn't work as expected when the <func> doesn't exist. If usersapce writes a non-terminated string, the kernel side will always return success but actually no filter applied.

As discussed before, the kernel now support '\0' to mark the end of string:
https://lkml.org/lkml/2018/1/16/116

After this fix in userspace, the perf will report correct error state. Also let it print an error if reset_tracing_files() fails.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

But what about old kernels? IIRC there was an error with this change.

Yes, you're right. I can't find a good compitable change. So what is the compatibilty policy for perf? If it must work with recent kernel, I think the only idea is leave as it was.

It should support *both* of kernels. I suggest adding a space after function name. You can simply call write(fd, " ", 1) IMHO.

hmm, I see. I will try it later mabye tommorow. Thanks!

Quoted 59 lines

Thanks,
Namhyung

Quoted 55 lines
Quoted 51 lines
Quoted 51 lines

The problem:

$ sudo ./perf ftrace -a --graph-depth 1 --graph-funcs abcdefg
 0)   0.140 us    |  rcu_all_qs();
 3)   0.304 us    |  mutex_unlock();
 0)   0.153 us    |  find_vma();
 3)   0.088 us    |  __fsnotify_parent();
 0)   6.145 us    |  handle_mm_fault();
 3)   0.089 us    |  fsnotify();
 3)   0.161 us    |  __sb_end_write();
 3)   0.710 us    |  SyS_close();
 3)   7.848 us    |  exit_to_usermode_loop();

On above example, I specified function filter 'abcdefg' but all functions are enabled. The expected error is hidden.

---
 tools/perf/builtin-ftrace.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
tools/perf/builtin-ftrace.c +4 -2
index 25a42ac..a87e9b3 100644
@@ -69,7 +69,7 @@ static int __write_tracing_file(const char *name, const char *val, bool append)
 {
 	char *file;
 	int fd, ret = -1;
-	ssize_t size = strlen(val);
+	ssize_t size = strlen(val) + 1;
 	int flags = O_WRONLY;
 	char errbuf[512];
 
@@ -280,8 +280,10 @@ static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv)
 	signal(SIGCHLD, sig_handler);
 	signal(SIGPIPE, sig_handler);
 
-	if (reset_tracing_files(ftrace) < 0)
+	if (reset_tracing_files(ftrace) < 0) {
+		pr_err("failed to reset ftrace\n");
 		goto out;
+	}
 
 	/* reset ftrace buffer */
 	if (write_tracing_file("trace", "0") < 0)
Signature · 1 line
2.7.4
Signature · 2 lines
Thanks,
Changbin Du
Signature · 2 lines
Thanks,
Changbin Du