linux-perf-users.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] perf ftrace: Fix the buffer size in __write_tracing_file
@ 2018-02-08  8:13 changbin.du
  2018-02-12  1:55 ` Namhyung Kim
  0 siblings, 1 reply; 5+ messages in thread
From: changbin.du @ 2018-02-08  8:13 UTC (permalink / raw)
  To: jolsa
  Cc: peterz, mingo, acme, namhyung, linux-kernel, linux-perf-users,
	Changbin Du

From: Changbin Du <changbin.du@intel.com>

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.

Signed-off-by: Changbin Du <changbin.du@intel.com>
---
 tools/perf/builtin-ftrace.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/tools/perf/builtin-ftrace.c b/tools/perf/builtin-ftrace.c
index 25a42ac..a87e9b3 100644
--- a/tools/perf/builtin-ftrace.c
+++ b/tools/perf/builtin-ftrace.c
@@ -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)
-- 
2.7.4

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

* Re: [PATCH v2] perf ftrace: Fix the buffer size in __write_tracing_file
  2018-02-08  8:13 [PATCH v2] perf ftrace: Fix the buffer size in __write_tracing_file changbin.du
@ 2018-02-12  1:55 ` Namhyung Kim
  2018-02-12  4:48   ` Du, Changbin
  0 siblings, 1 reply; 5+ messages in thread
From: Namhyung Kim @ 2018-02-12  1:55 UTC (permalink / raw)
  To: changbin.du
  Cc: jolsa, peterz, mingo, acme, linux-kernel, linux-perf-users,
	kernel-team

Hello,

On Thu, Feb 08, 2018 at 04:13:20PM +0800, changbin.du@intel.com wrote:
> From: Changbin Du <changbin.du@intel.com>
> 
> 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.

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

Thanks,
Namhyung


> 
> 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.
> 
> Signed-off-by: Changbin Du <changbin.du@intel.com>
> ---
>  tools/perf/builtin-ftrace.c | 6 ++++--
>  1 file changed, 4 insertions(+), 2 deletions(-)
> 
> diff --git a/tools/perf/builtin-ftrace.c b/tools/perf/builtin-ftrace.c
> index 25a42ac..a87e9b3 100644
> --- a/tools/perf/builtin-ftrace.c
> +++ b/tools/perf/builtin-ftrace.c
> @@ -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)
> -- 
> 2.7.4
> 

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

* Re: [PATCH v2] perf ftrace: Fix the buffer size in __write_tracing_file
  2018-02-12  1:55 ` Namhyung Kim
@ 2018-02-12  4:48   ` Du, Changbin
  2018-02-12  6:15     ` Namhyung Kim
  0 siblings, 1 reply; 5+ messages in thread
From: Du, Changbin @ 2018-02-12  4:48 UTC (permalink / raw)
  To: Namhyung Kim
  Cc: changbin.du, jolsa, peterz, mingo, acme, linux-kernel,
	linux-perf-users, kernel-team

Hi,

On Mon, Feb 12, 2018 at 10:55:27AM +0900, Namhyung Kim wrote:
> Hello,
> 
> On Thu, Feb 08, 2018 at 04:13:20PM +0800, changbin.du@intel.com wrote:
> > From: Changbin Du <changbin.du@intel.com>
> > 
> > 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.
> 
> 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.
 
> Thanks,
> Namhyung
> 
> 
> > 
> > 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.
> > 
> > Signed-off-by: Changbin Du <changbin.du@intel.com>
> > ---
> >  tools/perf/builtin-ftrace.c | 6 ++++--
> >  1 file changed, 4 insertions(+), 2 deletions(-)
> > 
> > diff --git a/tools/perf/builtin-ftrace.c b/tools/perf/builtin-ftrace.c
> > index 25a42ac..a87e9b3 100644
> > --- a/tools/perf/builtin-ftrace.c
> > +++ b/tools/perf/builtin-ftrace.c
> > @@ -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)
> > -- 
> > 2.7.4
> > 

-- 
Thanks,
Changbin Du

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

* Re: [PATCH v2] perf ftrace: Fix the buffer size in __write_tracing_file
  2018-02-12  4:48   ` Du, Changbin
@ 2018-02-12  6:15     ` Namhyung Kim
  2018-02-13  0:18       ` Du, Changbin
  0 siblings, 1 reply; 5+ messages in thread
From: Namhyung Kim @ 2018-02-12  6:15 UTC (permalink / raw)
  To: Du, Changbin
  Cc: jolsa, peterz, mingo, acme, linux-kernel, linux-perf-users,
	kernel-team, rostedt

On Mon, Feb 12, 2018 at 12:48:15PM +0800, Du, Changbin wrote:
> Hi,
> 
> On Mon, Feb 12, 2018 at 10:55:27AM +0900, Namhyung Kim wrote:
> > Hello,
> > 
> > On Thu, Feb 08, 2018 at 04:13:20PM +0800, changbin.du@intel.com wrote:
> > > From: Changbin Du <changbin.du@intel.com>
> > > 
> > > 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.
> > 
> > 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


> > 
> > 
> > > 
> > > 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.
> > > 
> > > Signed-off-by: Changbin Du <changbin.du@intel.com>
> > > ---
> > >  tools/perf/builtin-ftrace.c | 6 ++++--
> > >  1 file changed, 4 insertions(+), 2 deletions(-)
> > > 
> > > diff --git a/tools/perf/builtin-ftrace.c b/tools/perf/builtin-ftrace.c
> > > index 25a42ac..a87e9b3 100644
> > > --- a/tools/perf/builtin-ftrace.c
> > > +++ b/tools/perf/builtin-ftrace.c
> > > @@ -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)
> > > -- 
> > > 2.7.4
> > > 
> 
> -- 
> Thanks,
> Changbin Du

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

* Re: [PATCH v2] perf ftrace: Fix the buffer size in __write_tracing_file
  2018-02-12  6:15     ` Namhyung Kim
@ 2018-02-13  0:18       ` Du, Changbin
  0 siblings, 0 replies; 5+ messages in thread
From: Du, Changbin @ 2018-02-13  0:18 UTC (permalink / raw)
  To: Namhyung Kim
  Cc: Du, Changbin, jolsa@redhat.com, peterz@infradead.org,
	mingo@redhat.com, acme@kernel.org, linux-kernel@vger.kernel.org,
	linux-perf-users@vger.kernel.org, kernel-team@lge.com,
	rostedt@goodmis.org

On Sun, Feb 11, 2018 at 10:15:10PM -0800, Namhyung Kim wrote:
> On Mon, Feb 12, 2018 at 12:48:15PM +0800, Du, Changbin wrote:
> > Hi,
> > 
> > On Mon, Feb 12, 2018 at 10:55:27AM +0900, Namhyung Kim wrote:
> > > Hello,
> > > 
> > > On Thu, Feb 08, 2018 at 04:13:20PM +0800, changbin.du@intel.com wrote:
> > > > From: Changbin Du <changbin.du@intel.com>
> > > > 
> > > > 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.
> > > 
> > > 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!

> Thanks,
> Namhyung
> 
> 
> > > 
> > > 
> > > > 
> > > > 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.
> > > > 
> > > > Signed-off-by: Changbin Du <changbin.du@intel.com>
> > > > ---
> > > >  tools/perf/builtin-ftrace.c | 6 ++++--
> > > >  1 file changed, 4 insertions(+), 2 deletions(-)
> > > > 
> > > > diff --git a/tools/perf/builtin-ftrace.c b/tools/perf/builtin-ftrace.c
> > > > index 25a42ac..a87e9b3 100644
> > > > --- a/tools/perf/builtin-ftrace.c
> > > > +++ b/tools/perf/builtin-ftrace.c
> > > > @@ -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)
> > > > -- 
> > > > 2.7.4
> > > > 
> > 
> > -- 
> > Thanks,
> > Changbin Du

-- 
Thanks,
Changbin Du

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

end of thread, other threads:[~2018-02-13  0:18 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-02-08  8:13 [PATCH v2] perf ftrace: Fix the buffer size in __write_tracing_file changbin.du
2018-02-12  1:55 ` Namhyung Kim
2018-02-12  4:48   ` Du, Changbin
2018-02-12  6:15     ` Namhyung Kim
2018-02-13  0:18       ` Du, Changbin

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).