public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Peter Zijlstra <peterz@infradead.org>
To: Avi Kivity <avi@redhat.com>
Cc: Ingo Molnar <mingo@elte.hu>,
	Frederic Weisbecker <fweisbec@gmail.com>,
	Steven Rostedt <rostedt@goodmis.org>,
	kvm-devel <kvm@vger.kernel.org>,
	Linux Kernel Mailing List <linux-kernel@vger.kernel.org>
Subject: Re: ftrace/perf_event leak
Date: Wed, 01 Sep 2010 11:04:28 +0200	[thread overview]
Message-ID: <1283331868.2059.808.camel@laptop> (raw)
In-Reply-To: <4C7E11E5.1040402@redhat.com>

On Wed, 2010-09-01 at 11:42 +0300, Avi Kivity wrote:
> I recently added perf_event support to kvm_stat, to display kvm 
> tracepoints as statistics (I'd like to fold this to tools/perf 
> eventually, but that's another story).  However I'm seeing a resource 
> leak - after I quit the tool, there are quite a few references into the 
> kvm module:
> 
>    kvm_intel              43655  0
>    kvm                   272984  269 kvm_intel
> 
> The tool is just a python script that reads 
> /sys/kernel/debug/tracing/events/kvm to find out which events are 
> available, uses perf_event_open() to create one group per cpu to which a 
> lot of events are attached.  The only special thing I can think of is 
> that we use an ioctl to attach a filter to many perf_event descriptors.
> 
> You can find the source at 
> http://git.kernel.org/?p=virt/kvm/qemu-kvm.git;a=blob_plain;f=kvm/kvm_stat;hb=5bd5f131b50cb373ff4e2a3632c6dad00a1f0b55.  
> All it needs are the kvm modules loaded; no need to actually run a 
> guest.  Run as root.
> 

Does something like the below cure that?

I seem to remember C doesn't make any promises about the order of logic
statements, hence we need to explicitly pull out that try_module_get()
so that it evaluates after the rest of the conditions.

---
 kernel/trace/trace_event_perf.c |   10 ++++++----
 1 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/kernel/trace/trace_event_perf.c b/kernel/trace/trace_event_perf.c
index 000e6e8..35051f2 100644
--- a/kernel/trace/trace_event_perf.c
+++ b/kernel/trace/trace_event_perf.c
@@ -88,10 +88,11 @@ int perf_trace_init(struct perf_event *p_event)
 	mutex_lock(&event_mutex);
 	list_for_each_entry(tp_event, &ftrace_events, list) {
 		if (tp_event->event.type == event_id &&
-		    tp_event->class && tp_event->class->reg &&
-		    try_module_get(tp_event->mod)) {
-			ret = perf_trace_event_init(tp_event, p_event);
-			break;
+		    tp_event->class && tp_event->class->reg) {
+		    	if (try_module_get(tp_event->mod)) {
+				ret = perf_trace_event_init(tp_event, p_event);
+				break;
+			}
 		}
 	}
 	mutex_unlock(&event_mutex);
@@ -138,6 +139,7 @@ void perf_trace_destroy(struct perf_event *p_event)
 
 	free_percpu(tp_event->perf_events);
 	tp_event->perf_events = NULL;
+	module_put(tp_event->mod);
 
 	if (!--total_ref_count) {
 		for (i = 0; i < 4; i++) {


  reply	other threads:[~2010-09-01  9:04 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-09-01  8:42 ftrace/perf_event leak Avi Kivity
2010-09-01  9:04 ` Peter Zijlstra [this message]
2010-09-01  9:26   ` Avi Kivity
2010-09-01  9:35     ` Peter Zijlstra
2010-09-01  9:38     ` Li Zefan
2010-09-01  9:41       ` Peter Zijlstra
2010-09-01 10:38       ` Avi Kivity
2010-09-01 11:02         ` Peter Zijlstra
2010-09-01 11:07           ` Ingo Molnar
2010-09-01 12:15           ` Frederic Weisbecker
2010-09-01 13:59             ` Steven Rostedt
2010-09-01 17:32               ` Ingo Molnar
2010-09-02  1:20             ` Li Zefan
2010-09-09 19:45       ` [tip:perf/core] perf, trace: Fix module leak tip-bot for Li Zefan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1283331868.2059.808.camel@laptop \
    --to=peterz@infradead.org \
    --cc=avi@redhat.com \
    --cc=fweisbec@gmail.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@elte.hu \
    --cc=rostedt@goodmis.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox