Skip to contentSkip to conversation

Possible bug in ftrace_profile_enable_event

PATCH In review Paul Mackerras 4 participants 12 messages Oct 1, 2009 – Oct 8, 2009 Tracing

lore
Share & tools
lore
#0 lore raw

I was looking through kernel/trace/trace_event_profile.c and I saw this code:

static int ftrace_profile_enable_event(struct ftrace_event_call *event)

{
	char *buf;
	int ret = -ENOMEM;

	if (atomic_inc_return(&event->profile_count))
		return 0;

	if (!total_profile_count++) {
		buf = (char *)alloc_percpu(profile_buf_t);
		if (!buf)
			goto fail_buf;

		rcu_assign_pointer(trace_profile_buf, buf);

		buf = (char *)alloc_percpu(profile_buf_t);
		if (!buf)
			goto fail_buf_nmi;

		rcu_assign_pointer(trace_profile_buf_nmi, buf);
	}

	ret = event->profile_enable();
	if (!ret)
		return 0;

	kfree(trace_profile_buf_nmi);
fail_buf_nmi:
	kfree(trace_profile_buf);
fail_buf:
	total_profile_count--;

...

So we only allocate trace_profile_buf and trace_profile_buf_nmi if total_profile_count was zero on entry, but if we get an error returned from event->profile_enable(), we free them both unconditionally, regardless of the value of total_profile_count. That seems wrong. Is there a subtle reason why that is the right thing to do?

(Also, is kfree the appropriate counterpart to alloc_percpu?)

Paul.

maintainer
#1 lore raw
Quoted 7 lines · Paul Mackerras

So we only allocate trace_profile_buf and trace_profile_buf_nmi if total_profile_count was zero on entry, but if we get an error returned from event->profile_enable(), we free them both unconditionally, regardless of the value of total_profile_count. That seems wrong. Is there a subtle reason why that is the right thing to do?

(Also, is kfree the appropriate counterpart to alloc_percpu?)

Hi Paul,

I think you have a valid point. Frederic and I are here in Dresden (last day). I'll make sure he sees this.

Thanks,

-- Steve

#2 lore raw
In reply to Paul Mackerras

2009/10/1 Paul Mackerras <[email protected]>:

Quoted 35 lines

I was looking through kernel/trace/trace_event_profile.c and I saw this code:

static int ftrace_profile_enable_event(struct ftrace_event_call *event) {
       char *buf;
       int ret = -ENOMEM;

       if (atomic_inc_return(&event->profile_count)) return 0;

       if (!total_profile_count++) {
               buf = (char *)alloc_percpu(profile_buf_t); if (!buf)
                       goto fail_buf;

               rcu_assign_pointer(trace_profile_buf, buf);

               buf = (char *)alloc_percpu(profile_buf_t); if (!buf)
                       goto fail_buf_nmi;

               rcu_assign_pointer(trace_profile_buf_nmi, buf); }

       ret = event->profile_enable();
       if (!ret)
               return 0;

       kfree(trace_profile_buf_nmi);
fail_buf_nmi:
       kfree(trace_profile_buf);
fail_buf:
       total_profile_count--;

Quoted 7 lines

...

So we only allocate trace_profile_buf and trace_profile_buf_nmi if total_profile_count was zero on entry, but if we get an error returned from event->profile_enable(), we free them both unconditionally, regardless of the value of total_profile_count.  That seems wrong.  Is there a subtle reason why that is the right thing to do?

Oh right. Good catch.
I'll fix that soon.

Quoted 2 lines

(Also, is kfree the appropriate counterpart to alloc_percpu?)

No indeed.
That said it looks harmless for now because percpu_free seem to just roughly wrap kfree. But
its implementation may change later, so I'll fix that too.

Thanks.

#3 lore raw

[GIT PULL] tracing: Perf tracing fixes

In reply to Paul Mackerras

Ingo,

Please pull these .33 tracing fixes from

git://git.kernel.org/pub/scm/linux/kernel/git/frederic/random-tracing.git
tracing/core

These address reviews from Paul mackerras.

I also plan to rename the whole "ftrace event profile" naming to "ftrace event perf" soon.

Thanks.

Frederic Weisbecker (2):
tracing: Check total refcount before releasing bufs in profile_enable failure
tracing: Use free_percpu instead of kfree

 kernel/trace/trace_event_profile.c |   15 ++++++++++-----
 1 files changed, 10 insertions(+), 5 deletions(-)
#4 lore raw

Re: [GIT PULL] tracing: Perf tracing fixes

Quoted 3 lines · Frederic Weisbecker

Ingo,

Please pull these .33 tracing fixes from

Hmm, now I realize this code is upstream already. I should better rebase it against latest Linus's tree.

Sorry.

Quoted 17 lines

git://git.kernel.org/pub/scm/linux/kernel/git/frederic/random-tracing.git
tracing/core

These address reviews from Paul mackerras.

I also plan to rename the whole "ftrace event profile" naming to "ftrace event perf" soon.

Thanks.

Frederic Weisbecker (2):
tracing: Check total refcount before releasing bufs in profile_enable failure
tracing: Use free_percpu instead of kfree

 kernel/trace/trace_event_profile.c |   15 ++++++++++-----
 1 files changed, 10 insertions(+), 5 deletions(-)
#5 lore raw

Re: [GIT PULL] tracing: Perf tracing fixes

In reply to Frederic Weisbecker
Quoted 7 lines · Frederic Weisbecker

Ingo,

Please pull these .33 tracing fixes from

git://git.kernel.org/pub/scm/linux/kernel/git/frederic/random-tracing.git
tracing/core

I've rebased it against latest Linus, you can pull it from

git://git.kernel.org/pub/scm/linux/kernel/git/frederic/random-tracing.git
tracing/urgent

Thanks!

Frederic.

#6 lore raw

Re: [GIT PULL] tracing: Perf tracing fixes

Quoted 10 lines · Frederic Weisbecker
Quoted 7 lines · Frederic Weisbecker

Ingo,

Please pull these .33 tracing fixes from

git://git.kernel.org/pub/scm/linux/kernel/git/frederic/random-tracing.git
tracing/core

Quoted 4 lines

I've rebased it against latest Linus, you can pull it from

git://git.kernel.org/pub/scm/linux/kernel/git/frederic/random-tracing.git
tracing/urgent

Pulled, thanks a lot Frederic!

Ingo

#7 lore raw

PATCH 1/2 tracing: Check total refcount before releasing bufs in profile_enable failure

In reply to Paul Mackerras

When we call the profile_enable() callback of an event, we release the shared perf event tracing buffers unconditionnaly in the failure path. This is wrong because there may be other users of these. Then check the total refcount before doing this.

---
 kernel/trace/trace_event_profile.c |   15 ++++++++++-----
 1 files changed, 10 insertions(+), 5 deletions(-)
kernel/trace/trace_event_profile.c +10 -5
index dd44b87..e52784b 100644
@@ -31,7 +31,7 @@ static int ftrace_profile_enable_event(struct ftrace_event_call *event)
 	if (atomic_inc_return(&event->profile_count))
 		return 0;
 
-	if (!total_profile_count++) {
+	if (!total_profile_count) {
 		buf = (char *)alloc_percpu(profile_buf_t);
 		if (!buf)
 			goto fail_buf;
@@ -46,14 +46,19 @@ static int ftrace_profile_enable_event(struct ftrace_event_call *event)
 	}
 
 	ret = event->profile_enable();
-	if (!ret)
+	if (!ret) {
+		total_profile_count++;
 		return 0;
+	}
 
-	kfree(trace_profile_buf_nmi);
 fail_buf_nmi:
-	kfree(trace_profile_buf);
+	if (!total_profile_count) {
+		kfree(trace_profile_buf_nmi);
+		kfree(trace_profile_buf);
+		trace_profile_buf_nmi = NULL;
+		trace_profile_buf = NULL;
+	}
 fail_buf:
-	total_profile_count--;
 	atomic_dec(&event->profile_count);
 
 	return ret;
Signature · 1 line
1.6.2.3
#8 lore raw

Re: [PATCH 1/2] tracing: Check total refcount before releasing bufs in profile_enable failure

Quoted 4 lines · Frederic Weisbecker

When we call the profile_enable() callback of an event, we release the shared perf event tracing buffers unconditionnaly in the failure path. This is wrong because there may be other users of these. Then check the total refcount before doing this.

[snip]

kernel/trace/trace_event_profile.c static int ftrace_profile_enable_event(struct ftrace_event_call *event) · line 55

Quoted 4 lines
kernel/trace/trace_event_profile.c +1 -2
-	kfree(trace_profile_buf_nmi);
 fail_buf_nmi:
-	kfree(trace_profile_buf);
+	if (!total_profile_count) {

A small problem here: total_profile_count will be 1, not 0, in the case where we need to free...

kernel/trace/trace_event_profile.c static int ftrace_profile_enable_event(struct ftrace_event_call *event) · line 56

Quoted 7 lines
kernel/trace/trace_event_profile.c +5 -1
+		kfree(trace_profile_buf_nmi);
+		kfree(trace_profile_buf);
+		trace_profile_buf_nmi = NULL;
+		trace_profile_buf = NULL;
+	}
 fail_buf:
-	total_profile_count--;

since we don't decrement total_profile_count until here.

Paul.

#9 lore raw

Re: [PATCH 1/2] tracing: Check total refcount before releasing bufs in profile_enable failure

Quoted 27 lines · Paul Mackerras
Quoted 4 lines · Frederic Weisbecker

When we call the profile_enable() callback of an event, we release the shared perf event tracing buffers unconditionnaly in the failure path. This is wrong because there may be other users of these. Then check the total refcount before doing this.

[snip]

kernel/trace/trace_event_profile.c static int ftrace_profile_enable_event(struct ftrace_event_call *event) · line 55

Quoted 4 lines
kernel/trace/trace_event_profile.c +1 -2
-	kfree(trace_profile_buf_nmi);
 fail_buf_nmi:
-	kfree(trace_profile_buf);
+	if (!total_profile_count) {

A small problem here: total_profile_count will be 1, not 0, in the case where we need to free...

kernel/trace/trace_event_profile.c static int ftrace_profile_enable_event(struct ftrace_event_call *event) · line 56

Quoted 7 lines
kernel/trace/trace_event_profile.c +5 -1
+		kfree(trace_profile_buf_nmi);
+		kfree(trace_profile_buf);
+		trace_profile_buf_nmi = NULL;
+		trace_profile_buf = NULL;
+	}
 fail_buf:
-	total_profile_count--;

since we don't decrement total_profile_count until here.

Quoted 1 line

Paul.

No, because now we only increment total_profile_count if nothing has failed. This is even the last thing done in the succeeded path. So if we fail and need to free, it means its values is still 0.

#10 lore raw

Re: [PATCH 1/2] tracing: Check total refcount before releasing bufs in profile_enable failure

Quoted 4 lines · Frederic Weisbecker

No, because now we only increment total_profile_count if nothing has failed. This is even the last thing done in the succeeded path. So if we fail and need to free, it means its values is still 0.

Ah yes, good point.

Paul.

#11 lore raw

PATCH 2/2 tracing: Use free_percpu instead of kfree

In reply to Paul Mackerras

In the event->profile_enable() failure path, we release the per cpu buffers using kfree which is wrong because they are per cpu pointers. Although free_percpu only wraps kfree for now, that may change in the future so lets use the correct way.

---
 kernel/trace/trace_event_profile.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
kernel/trace/trace_event_profile.c +2 -2
index e52784b..8d5c171 100644
@@ -53,8 +53,8 @@ static int ftrace_profile_enable_event(struct ftrace_event_call *event)
 
 fail_buf_nmi:
 	if (!total_profile_count) {
-		kfree(trace_profile_buf_nmi);
-		kfree(trace_profile_buf);
+		free_percpu(trace_profile_buf_nmi);
+		free_percpu(trace_profile_buf);
 		trace_profile_buf_nmi = NULL;
 		trace_profile_buf = NULL;
 	}
Signature · 1 line
1.6.2.3