perf_counter: fixes and an optimization
cover perf_counter: fixes and an optimization
- fixes an off-by-one in string handling
- fixes the mmap tracing to provide the full path
- allows unpriviledged use of cpu counters
- optimize mmap/comm tracing
PATCH 1/4 perf_counter: fix off-by-one on the string lengths
strlen() does _not_ include the terminating \0.
--- kernel/perf_counter.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/perf_counter.c +2 -2
@@ -1989,7 +1989,7 @@ static void perf_counter_comm_event(struunsigned int size;char *comm = comm_event->task->comm; - size =ALIGN (strlen (comm),sizeof (u64)); + size =ALIGN (strlen (comm)+1 ,sizeof (u64)); comm_event->comm = comm; comm_event->comm_size = size;
@@ -2109,7 +2109,7 @@ static void perf_counter_mmap_event(stru } got_name: - size =ALIGN (strlen (name),sizeof (u64)); + size =ALIGN (strlen (name)+1 ,sizeof (u64)); mmap_event->file_name = name; mmap_event->file_size = size;
PATCH 2/4 perf_counter: optimize mmap/comm tracking
In reply to Peter ZijlstraThe mmap/comm tracking code does quite a lot of work before it discovers there's no interest in it, avoid that by keeping a counter.
--- kernel/perf_counter.c | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-)
kernel/perf_counter.c +30 -2
@@ -38,6 +38,10 @@ int perf_max_counters __read_mostly = 1;static int perf_reserved_percpu__read_mostly ;static int perf_overcommit__read_mostly =1 ; +static atomic_t nr_mmap_tracking__read_mostly ; +static atomic_t nr_munmap_tracking__read_mostly ; +static atomic_t nr_comm_tracking__read_mostly ; +/* * Mutex for (sysadmin-configurable) counter reservations: */
@@ -1186,6 +1190,13 @@ static void free_counter(struct perf_cou {perf_pending_sync (counter); +if (counter->hw_event.mmap) +atomic_dec (&nr_mmap_tracking); +if (counter->hw_event.munmap) +atomic_dec (&nr_munmap_tracking); +if (counter->hw_event.comm) +atomic_dec (&nr_comm_tracking); +if (counter->destroy) counter->destroy (counter);
@@ -2128,7 +2139,12 @@ got_name:void perf_counter_mmap (unsigned long addr,unsigned long len,unsigned long pgoff,struct file *file) { -struct perf_mmap_event mmap_event = { +struct perf_mmap_event mmap_event; + +if (!atomic_read (&nr_mmap_tracking)) +return ; + + mmap_event = (struct perf_mmap_event ){ .file = file, .event = { .header = { .type =PERF_EVENT_MMAP , },
@@ -2146,7 +2162,12 @@ void perf_counter_mmap(unsigned long addvoid perf_counter_munmap (unsigned long addr,unsigned long len,unsigned long pgoff,struct file *file) { -struct perf_mmap_event mmap_event = { +struct perf_mmap_event mmap_event; + +if (!atomic_read (&nr_munmap_tracking)) +return ; + + mmap_event = (struct perf_mmap_event ){ .file = file, .event = { .header = { .type =PERF_EVENT_MUNMAP , },
@@ -2725,6 +2746,13 @@ done: counter->hw_ops = hw_ops; +if (counter->hw_event.mmap) +atomic_inc (&nr_mmap_tracking); +if (counter->hw_event.munmap) +atomic_inc (&nr_munmap_tracking); +if (counter->hw_event.comm) +atomic_inc (&nr_comm_tracking); +return counter; }
[tip:perfcounters/core] perf_counter: optimize mmap/comm tracking
Commit-ID: e37f8d14c1f56e274b3e827912f23f0b2fb7a7bb Gitweb: http://git.kernel.org/tip/e37f8d14c1f56e274b3e827912f23f0b2fb7a7bb Author: Peter Zijlstra <[email protected]> AuthorDate: Thu, 9 Apr 2009 10:53:44 +0200 Committer: Ingo Molnar <[email protected]> CommitDate: Thu, 9 Apr 2009 11:00:53 +0200
perf_counter: optimize mmap/comm tracking
Impact: performance optimization
The mmap/comm tracking code does quite a lot of work before it discovers there's no interest in it, avoid that by keeping a counter.
LKML-Reference: <[email protected]>
--- kernel/perf_counter.c | 32 ++++++++++++++++++++++++++++++-- 1 files changed, 30 insertions(+), 2 deletions(-)
kernel/perf_counter.c +30 -2
@@ -38,6 +38,10 @@ int perf_max_counters __read_mostly = 1;static int perf_reserved_percpu__read_mostly ;static int perf_overcommit__read_mostly =1 ; +static atomic_t nr_mmap_tracking__read_mostly ; +static atomic_t nr_munmap_tracking__read_mostly ; +static atomic_t nr_comm_tracking__read_mostly ; +/* * Mutex for (sysadmin-configurable) counter reservations: */
@@ -1186,6 +1190,13 @@ static void free_counter(struct perf_counter *counter) {perf_pending_sync (counter); +if (counter->hw_event.mmap) +atomic_dec (&nr_mmap_tracking); +if (counter->hw_event.munmap) +atomic_dec (&nr_munmap_tracking); +if (counter->hw_event.comm) +atomic_dec (&nr_comm_tracking); +if (counter->destroy) counter->destroy (counter);
@@ -2128,7 +2139,12 @@ got_name:void perf_counter_mmap (unsigned long addr,unsigned long len,unsigned long pgoff,struct file *file) { -struct perf_mmap_event mmap_event = { +struct perf_mmap_event mmap_event; + +if (!atomic_read (&nr_mmap_tracking)) +return ; + + mmap_event = (struct perf_mmap_event ){ .file = file, .event = { .header = { .type =PERF_EVENT_MMAP , },
@@ -2146,7 +2162,12 @@ void perf_counter_mmap(unsigned long addr, unsigned long len,void perf_counter_munmap (unsigned long addr,unsigned long len,unsigned long pgoff,struct file *file) { -struct perf_mmap_event mmap_event = { +struct perf_mmap_event mmap_event; + +if (!atomic_read (&nr_munmap_tracking)) +return ; + + mmap_event = (struct perf_mmap_event ){ .file = file, .event = { .header = { .type =PERF_EVENT_MUNMAP , },
@@ -2725,6 +2746,13 @@ done: counter->hw_ops = hw_ops; +if (counter->hw_event.mmap) +atomic_inc (&nr_mmap_tracking); +if (counter->hw_event.munmap) +atomic_inc (&nr_munmap_tracking); +if (counter->hw_event.comm) +atomic_inc (&nr_comm_tracking); +return counter; }
Re: [PATCH 2/4] perf_counter: optimize mmap/comm tracking
In reply to Peter ZijlstraQuoted 1 line · Peter Zijlstra
+static
atomic_tnr_comm_tracking__read_mostly;
hm, nr_comm_tracking is not actually used by any fastpath - just increased and then decreased.
Ingo
Re: [PATCH 2/4] perf_counter: optimize mmap/comm tracking
Quoted 6 lines · Ingo Molnar
Quoted 1 line · Peter Zijlstra
+static
atomic_tnr_comm_tracking__read_mostly;hm,
nr_comm_trackingis not actually used by any fastpath - just increased and then decreased.
kernel/perf_counter.c +6 -1
@@ -2018,7 +2018,12 @@ static void perf_counter_comm_event(struvoid perf_counter_comm (struct task_struct *task) { -struct perf_comm_event comm_event = { +struct perf_comm_event comm_event; + +if (!atomic_read (&nr_comm_tracking)) +return ; + + comm_event = (struct perf_comm_event ){ .task = task, .event = { .header = { .type =PERF_EVENT_COMM , },
[tip:perfcounters/core] perf_counter: optimize mmap/comm tracking
In reply to Peter ZijlstraCommit-ID: 9ee318a7825929bc3734110b83ae8e20e53d9de3 Gitweb: http://git.kernel.org/tip/9ee318a7825929bc3734110b83ae8e20e53d9de3 Author: Peter Zijlstra <[email protected]> AuthorDate: Thu, 9 Apr 2009 10:53:44 +0200 Committer: Ingo Molnar <[email protected]> CommitDate: Thu, 9 Apr 2009 11:50:43 +0200
perf_counter: optimize mmap/comm tracking
Impact: performance optimization
The mmap/comm tracking code does quite a lot of work before it discovers there's no interest in it, avoid that by keeping a counter.
LKML-Reference: <[email protected]>
--- kernel/perf_counter.c | 39 ++++++++++++++++++++++++++++++++++++--- 1 files changed, 36 insertions(+), 3 deletions(-)
kernel/perf_counter.c +36 -3
@@ -38,6 +38,10 @@ int perf_max_counters __read_mostly = 1;static int perf_reserved_percpu__read_mostly ;static int perf_overcommit__read_mostly =1 ; +static atomic_t nr_mmap_tracking__read_mostly ; +static atomic_t nr_munmap_tracking__read_mostly ; +static atomic_t nr_comm_tracking__read_mostly ; +/* * Mutex for (sysadmin-configurable) counter reservations: */
@@ -1186,6 +1190,13 @@ static void free_counter(struct perf_counter *counter) {perf_pending_sync (counter); +if (counter->hw_event.mmap) +atomic_dec (&nr_mmap_tracking); +if (counter->hw_event.munmap) +atomic_dec (&nr_munmap_tracking); +if (counter->hw_event.comm) +atomic_dec (&nr_comm_tracking); +if (counter->destroy) counter->destroy (counter);
@@ -2005,7 +2016,12 @@ static void perf_counter_comm_event(struct perf_comm_event *comm_event)void perf_counter_comm (struct task_struct *task) { -struct perf_comm_event comm_event = { +struct perf_comm_event comm_event; + +if (!atomic_read (&nr_comm_tracking)) +return ; + + comm_event = (struct perf_comm_event ){ .task = task, .event = { .header = { .type =PERF_EVENT_COMM , },
@@ -2128,7 +2144,12 @@ got_name:void perf_counter_mmap (unsigned long addr,unsigned long len,unsigned long pgoff,struct file *file) { -struct perf_mmap_event mmap_event = { +struct perf_mmap_event mmap_event; + +if (!atomic_read (&nr_mmap_tracking)) +return ; + + mmap_event = (struct perf_mmap_event ){ .file = file, .event = { .header = { .type =PERF_EVENT_MMAP , },
@@ -2146,7 +2167,12 @@ void perf_counter_mmap(unsigned long addr, unsigned long len,void perf_counter_munmap (unsigned long addr,unsigned long len,unsigned long pgoff,struct file *file) { -struct perf_mmap_event mmap_event = { +struct perf_mmap_event mmap_event; + +if (!atomic_read (&nr_munmap_tracking)) +return ; + + mmap_event = (struct perf_mmap_event ){ .file = file, .event = { .header = { .type =PERF_EVENT_MUNMAP , },
@@ -2725,6 +2751,13 @@ done: counter->hw_ops = hw_ops; +if (counter->hw_event.mmap) +atomic_inc (&nr_mmap_tracking); +if (counter->hw_event.munmap) +atomic_inc (&nr_munmap_tracking); +if (counter->hw_event.comm) +atomic_inc (&nr_comm_tracking); +return counter; }
PATCH 3/4 perf_counter: sysctl for system wide perf counters
In reply to Peter ZijlstraAllow the use of system wide perf counters to everybody, but provide a sysctl to disable it for the paranoid security minded.
--- include/linux/perf_counter.h | 2 ++ kernel/perf_counter.c | 4 +++- kernel/sysctl.c | 11 +++++++++++ 3 files changed, 16 insertions(+), 1 deletion(-)
include/linux/perf_counter.h +2 -0
@@ -568,6 +568,8 @@ struct perf_callchain_entry {extern struct perf_callchain_entry *perf_callchain (struct pt_regs *regs); +extern int sysctl_perf_counter_priv; +#else static inline void perf_counter_task_sched_in (struct task_struct *task,int cpu) { }
kernel/perf_counter.c +3 -1
@@ -42,6 +42,8 @@ static atomic_t nr_mmap_tracking __read_static atomic_t nr_munmap_tracking__read_mostly ;static atomic_t nr_comm_tracking__read_mostly ; +int sysctl_perf_counter_priv;/* do we need to be privileged */ +/* * Mutex for (sysadmin-configurable) counter reservations: */
@@ -1132,7 +1134,7 @@ static struct perf_counter_context *find*/ if (cpu !=-1 ) {/* Must be root to operate on a CPU counter: */ -if (!capable (CAP_SYS_ADMIN )) +if (sysctl_perf_counter_priv && !capable (CAP_SYS_ADMIN ))return ERR_PTR (-EACCES );if (cpu <0 || cpu >num_possible_cpus ())
kernel/sysctl.c +11 -0
@@ -50,6 +50,7 @@#include <linux/reboot.h> #include <linux/ftrace.h> #include <linux/slow-work.h> +#include <linux/perf_counter.h> #include <asm/uaccess.h> #include <asm/processor.h>
@@ -931,6 +932,16 @@ static struct ctl_table kern_table[] = { .child = slow_work_sysctls, },#endif +#ifdef CONFIG_PERF_COUNTERS + { + .ctl_name = CTL_UNNUMBERED, + .procname = "perf_counter_privileged", + .data = &sysctl_perf_counter_priv, + .maxlen = sizeof(sysctl_perf_counter_priv), + .mode = 0644, + .proc_handler = &proc_dointvec, + }, +#endif /* * NOTE: do not add new entries to this table unless you have read * Documentation/sysctl/ctl_unnumbered.txt
[tip:perfcounters/core] perf_counter: sysctl for system wide perf counters
Commit-ID: 79bbb0bfff331422caf8a353be68c4764bc1e47f Gitweb: http://git.kernel.org/tip/79bbb0bfff331422caf8a353be68c4764bc1e47f Author: Peter Zijlstra <[email protected]> AuthorDate: Thu, 9 Apr 2009 10:53:45 +0200 Committer: Ingo Molnar <[email protected]> CommitDate: Thu, 9 Apr 2009 11:00:54 +0200
perf_counter: sysctl for system wide perf counters
Impact: add sysctl for paranoid/relaxed perfcounters policy
Allow the use of system wide perf counters to everybody, but provide a sysctl to disable it for the paranoid security minded.
LKML-Reference: <[email protected]>
--- include/linux/perf_counter.h | 2 ++ kernel/perf_counter.c | 4 +++- kernel/sysctl.c | 11 +++++++++++ 3 files changed, 16 insertions(+), 1 deletions(-)
include/linux/perf_counter.h +2 -0
@@ -568,6 +568,8 @@ struct perf_callchain_entry {extern struct perf_callchain_entry *perf_callchain (struct pt_regs *regs); +extern int sysctl_perf_counter_priv; +#else static inline void perf_counter_task_sched_in (struct task_struct *task,int cpu) { }
kernel/perf_counter.c +3 -1
@@ -42,6 +42,8 @@ static atomic_t nr_mmap_tracking __read_mostly;static atomic_t nr_munmap_tracking__read_mostly ;static atomic_t nr_comm_tracking__read_mostly ; +int sysctl_perf_counter_priv__read_mostly ;/* do we need to be privileged */ +/* * Mutex for (sysadmin-configurable) counter reservations: */
@@ -1132,7 +1134,7 @@ static struct perf_counter_context *find_get_context(pid_t pid, int cpu)*/ if (cpu !=-1 ) {/* Must be root to operate on a CPU counter: */ -if (!capable (CAP_SYS_ADMIN )) +if (sysctl_perf_counter_priv && !capable (CAP_SYS_ADMIN ))return ERR_PTR (-EACCES );if (cpu <0 || cpu >num_possible_cpus ())
kernel/sysctl.c +11 -0
@@ -49,6 +49,7 @@#include <linux/reboot.h> #include <linux/ftrace.h> #include <linux/slow-work.h> +#include <linux/perf_counter.h> #include <asm/uaccess.h> #include <asm/processor.h>
@@ -920,6 +921,16 @@ static struct ctl_table kern_table[] = { .child = slow_work_sysctls, },#endif +#ifdef CONFIG_PERF_COUNTERS + { + .ctl_name = CTL_UNNUMBERED, + .procname = "perf_counter_privileged", + .data = &sysctl_perf_counter_priv, + .maxlen = sizeof(sysctl_perf_counter_priv), + .mode = 0644, + .proc_handler = &proc_dointvec, + }, +#endif /* * NOTE: do not add new entries to this table unless you have read * Documentation/sysctl/ctl_unnumbered.txt
[tip:perfcounters/core] perf_counter: sysctl for system wide perf counters
In reply to Peter ZijlstraCommit-ID: 1ccd15497869f3ed83b5225d410df53a96e52757 Gitweb: http://git.kernel.org/tip/1ccd15497869f3ed83b5225d410df53a96e52757 Author: Peter Zijlstra <[email protected]> AuthorDate: Thu, 9 Apr 2009 10:53:45 +0200 Committer: Ingo Molnar <[email protected]> CommitDate: Thu, 9 Apr 2009 11:50:52 +0200
perf_counter: sysctl for system wide perf counters
Impact: add sysctl for paranoid/relaxed perfcounters policy
Allow the use of system wide perf counters to everybody, but provide a sysctl to disable it for the paranoid security minded.
LKML-Reference: <[email protected]>
--- include/linux/perf_counter.h | 2 ++ kernel/perf_counter.c | 4 +++- kernel/sysctl.c | 11 +++++++++++ 3 files changed, 16 insertions(+), 1 deletions(-)
include/linux/perf_counter.h +2 -0
@@ -568,6 +568,8 @@ struct perf_callchain_entry {extern struct perf_callchain_entry *perf_callchain (struct pt_regs *regs); +extern int sysctl_perf_counter_priv; +#else static inline void perf_counter_task_sched_in (struct task_struct *task,int cpu) { }
kernel/perf_counter.c +3 -1
@@ -42,6 +42,8 @@ static atomic_t nr_mmap_tracking __read_mostly;static atomic_t nr_munmap_tracking__read_mostly ;static atomic_t nr_comm_tracking__read_mostly ; +int sysctl_perf_counter_priv__read_mostly ;/* do we need to be privileged */ +/* * Mutex for (sysadmin-configurable) counter reservations: */
@@ -1132,7 +1134,7 @@ static struct perf_counter_context *find_get_context(pid_t pid, int cpu)*/ if (cpu !=-1 ) {/* Must be root to operate on a CPU counter: */ -if (!capable (CAP_SYS_ADMIN )) +if (sysctl_perf_counter_priv && !capable (CAP_SYS_ADMIN ))return ERR_PTR (-EACCES );if (cpu <0 || cpu >num_possible_cpus ())
kernel/sysctl.c +11 -0
@@ -49,6 +49,7 @@#include <linux/reboot.h> #include <linux/ftrace.h> #include <linux/slow-work.h> +#include <linux/perf_counter.h> #include <asm/uaccess.h> #include <asm/processor.h>
@@ -920,6 +921,16 @@ static struct ctl_table kern_table[] = { .child = slow_work_sysctls, },#endif +#ifdef CONFIG_PERF_COUNTERS + { + .ctl_name = CTL_UNNUMBERED, + .procname = "perf_counter_privileged", + .data = &sysctl_perf_counter_priv, + .maxlen = sizeof(sysctl_perf_counter_priv), + .mode = 0644, + .proc_handler = &proc_dointvec, + }, +#endif /* * NOTE: do not add new entries to this table unless you have read * Documentation/sysctl/ctl_unnumbered.txt
PATCH 4/4 perf_counter: log full path names
In reply to Peter Zijlstradentry_path() only provide path-names up to the mount root, which is unsuited for out purpose, use d_path() instead.
--- kernel/perf_counter.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/perf_counter.c +1 -1
@@ -2111,7 +2111,7 @@ static void perf_counter_mmap_event(stru name =strncpy (tmp,"//enomem" ,sizeof (tmp)); goto got_name; } - name =dentry_path (file->f_dentry, buf,PATH_MAX ); + name =d_path (&file->f_path, buf,PATH_MAX );if (IS_ERR (name)) { name =strncpy (tmp,"//toolong" ,sizeof (tmp)); goto got_name;
[tip:perfcounters/core] perf_counter: log full path names
Commit-ID: 0a24487bebbee88609004bc5a5ae1b375b292897 Gitweb: http://git.kernel.org/tip/0a24487bebbee88609004bc5a5ae1b375b292897 Author: Peter Zijlstra <[email protected]> AuthorDate: Thu, 9 Apr 2009 10:53:46 +0200 Committer: Ingo Molnar <[email protected]> CommitDate: Thu, 9 Apr 2009 11:00:54 +0200
perf_counter: log full path names
Impact: fix perf-report output for /home mounted binaries, etc.
dentry_path() only provide path-names up to the mount root, which is unsuited for out purpose, use d_path() instead.
LKML-Reference: <[email protected]>
--- kernel/perf_counter.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
kernel/perf_counter.c +1 -1
@@ -2111,7 +2111,7 @@ static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event) name =strncpy (tmp,"//enomem" ,sizeof (tmp)); goto got_name; } - name =dentry_path (file->f_dentry, buf,PATH_MAX ); + name =d_path (&file->f_path, buf,PATH_MAX );if (IS_ERR (name)) { name =strncpy (tmp,"//toolong" ,sizeof (tmp)); goto got_name;
[tip:perfcounters/core] perf_counter: log full path names
In reply to Peter ZijlstraCommit-ID: d3d21c412d8525eb2e208d990ab5eee5fb0fe03d Gitweb: http://git.kernel.org/tip/d3d21c412d8525eb2e208d990ab5eee5fb0fe03d Author: Peter Zijlstra <[email protected]> AuthorDate: Thu, 9 Apr 2009 10:53:46 +0200 Committer: Ingo Molnar <[email protected]> CommitDate: Thu, 9 Apr 2009 11:50:54 +0200
perf_counter: log full path names
Impact: fix perf-report output for /home mounted binaries, etc.
dentry_path() only provide path-names up to the mount root, which is unsuited for out purpose, use d_path() instead.
LKML-Reference: <[email protected]>
--- kernel/perf_counter.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
kernel/perf_counter.c +1 -1
@@ -2116,7 +2116,7 @@ static void perf_counter_mmap_event(struct perf_mmap_event *mmap_event) name =strncpy (tmp,"//enomem" ,sizeof (tmp)); goto got_name; } - name =dentry_path (file->f_dentry, buf,PATH_MAX ); + name =d_path (&file->f_path, buf,PATH_MAX );if (IS_ERR (name)) { name =strncpy (tmp,"//toolong" ,sizeof (tmp)); goto got_name;
Ingo Molnar
AppliedQuoted 3 lines · Peter Zijlstra
- fixes an off-by-one in string handling
- fixes the mmap tracing to provide the full path
- allows unpriviledged use of cpu counters
i made the variable here __read_mostly.
Quoted 1 line
- optimize mmap/comm tracing
Applied, thanks!
Ingo