Skip to contentSkip to conversation

perf_events: fix errors path in perf_output_begin()

PATCH In review Stephane Eranian 4 participants 9 messages May 17, 2010

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf_events: fix errors path in perf_output_begin()

In case the sampling buffer has no "payload" pages, nr_pages is 0. The problem is that the error path in perf_output_begin() skips to a label which assumes perf_output_lock() has been issued which is not the case. That triggers a WARN_ON() is perf_output_unlock().

This patch fixes the problem by adding a new label and skipping perf_task_unlock() in case data->nr_pages is 0.

--

kernel/perf_event.c +4 -2
index a4fa381..95137b6 100644
@@ -3035,8 +3035,10 @@ int perf_output_begin(struct perf_output_handle *handle,
 	handle->nmi	= nmi;
 	handle->sample	= sample;
 
-	if (!data->nr_pages)
-		goto fail;
+	if (!data->nr_pages) {
+		atomic_inc(&data->lost);
+		goto out;
+	}
 
 	have_lost = atomic_read(&data->lost);
 	if (have_lost)
#1 lore raw
Quoted 7 lines · Stephane Eranian

In case the sampling buffer has no "payload" pages, nr_pages is 0. The problem is that the error path in perf_output_begin() skips to a label which assumes perf_output_lock() has been issued which is not the case. That triggers a WARN_ON() is perf_output_unlock().

This patch fixes the problem by adding a new label and skipping perf_task_unlock() in case data->nr_pages is 0.

It does? Your code looks good to me, but the description above is a little flawed, since no new label is added.

Thanks

Quoted 22 lines

--

kernel/perf_event.c +4 -3
index a4fa381..95137b6 100644
@@ -3035,8 +3035,10 @@ int perf_output_begin(struct perf_output_handle *handle,
 	handle->nmi	= nmi;
 	handle->sample	= sample;
 
-	if (!data->nr_pages)
-		goto fail;
+	if (!data->nr_pages) {
+		atomic_inc(&data->lost);
+		goto out;
+	}
 
 	have_lost = atomic_read(&data->lost);
 	if (have_lost)
--
#2 lore raw
Quoted 9 lines · John Kacur
Quoted 5 lines · Stephane Eranian

  In case the sampling buffer has no "payload" pages, nr_pages is 0. The problem is that the error path in perf_output_begin() skips to a label which assumes perf_output_lock() has been issued which is not the case. That triggers a WARN_ON() is perf_output_unlock().

Quoted 6 lines
Quoted 2 lines

  This patch fixes the problem by adding a new label and skipping perf_task_unlock() in case data->nr_pages is 0.

It does? Your code looks good to me, but the description above is a little flawed, since no new label is added.

Sorry about that. I had written the text before the final version of the patch! I will resubmit.

Quoted 20 lines
Quoted 19 lines

--

kernel/perf_event.c +4 -3
index a4fa381..95137b6 100644
@@ -3035,8 +3035,10 @@ int perf_output_begin(struct perf_output_handle *handle,
       handle->nmi     = nmi;
       handle->sample  = sample;
 
-     if (!data->nr_pages)
-             goto fail;
+     if (!data->nr_pages) {
+             atomic_inc(&data->lost);
+             goto out;
+     }
 
       have_lost = atomic_read(&data->lost);
       if (have_lost)
--
#3 lore raw
In reply to Stephane Eranian
Quoted 16 lines · Stephane Eranian

In case the sampling buffer has no "payload" pages, nr_pages is 0. The problem is that the error path in perf_output_begin() skips to a label which assumes perf_output_lock() has been issued which is not the case. That triggers a WARN_ON() is perf_output_unlock().

This patch fixes the problem by adding a new label and skipping perf_task_unlock() in case data->nr_pages is 0.

--

kernel/perf_event.c +0 -0
index a4fa381..95137b6 100644

kernel/perf_event.c int perf_output_begin(struct perf_output_handle *handle, · line 3041

Quoted 9 lines
kernel/perf_event.c +4 -2
@@ -3035,8 +3035,10 @@ int perf_output_begin(struct perf_output_handle *handle,
 	handle->nmi	= nmi;
 	handle->sample	= sample;
 
-	if (!data->nr_pages)
-		goto fail;
+	if (!data->nr_pages) {
+		atomic_inc(&data->lost);
+		goto out;
+	}

Oh indeed, handle->lock is in a random state. Whatever its value we have an unbalanced put_cpu() anyway.

And we don't race with someone else, data->lock = -1 and will then warn.

I just have a tiny doubt: should we really count this path to the lost events? I'm not sure when we can have data->nr_pages == 0, does this happen if we mmap after enabling the event?

All I know is that I observed I already lost events in this path using perf lock.

#4 lore raw
Quoted 20 lines · Frederic Weisbecker
Quoted 19 lines · Stephane Eranian

  In case the sampling buffer has no "payload" pages, nr_pages is 0. The problem is that the error path in perf_output_begin() skips to a label which assumes perf_output_lock() has been issued which is not the case. That triggers a WARN_ON() is perf_output_unlock().

  This patch fixes the problem by adding a new label and skipping perf_task_unlock() in case data->nr_pages is 0.

--

kernel/perf_event.c +0 -0
index a4fa381..95137b6 100644
@@ -3035,8 +3035,10 @@ int perf_output_begin(struct perf_output_handle *handle,
       handle->nmi     = nmi;
       handle->sample  = sample;
 
Quoted 13 lines

kernel/perf_event.c int perf_output_begin(struct perf_output_handle *handle, · line 3041

Quoted 6 lines
kernel/perf_event.c +4 -2
-     if (!data->nr_pages)
-             goto fail;
+     if (!data->nr_pages) {
+             atomic_inc(&data->lost);
+             goto out;
+     }

Oh indeed, handle->lock is in a random state. Whatever its value we have an unbalanced put_cpu() anyway.

Yes, I caught this problem while debugging the perf record -c issue.

Quoted 8 lines

And we don't race with someone else, data->lock = -1 and will then warn.

I just have a tiny doubt: should we really count this path to the lost events? I'm not sure when we can have data->nr_pages == 0, does this happen if we mmap after enabling the event?

Well, nr_pages = 0 means all you have is the sampling buffer header page. You cannot save any sample, so you actually loose everything. Perf reports a dozen samples but those are synthetic. The kernel cannot reject an mmap() for just one page because of the remapped counters, thus it has to handle this gracefully and at least report the lost samples.

Quoted 3 lines

All I know is that I observed I already lost events in this path using perf lock.

#5 lore raw
Quoted 6 lines · Stephane Eranian
Quoted 4 lines

kernel/perf_event.c int perf_output_begin(struct perf_output_handle *handle, · line 3041

Quoted 4 lines
kernel/perf_event.c +4 -0
+     if (!data->nr_pages) {
+             atomic_inc(&data->lost);
+             goto out;
+     }
Quoted 7 lines

Well, nr_pages = 0 means all you have is the sampling buffer header page. You cannot save any sample, so you actually loose everything. Perf reports a dozen samples but those are synthetic. The kernel cannot reject an mmap() for just one page because of the remapped counters, thus it has to handle this gracefully and at least report the lost samples.

So you want to preserve this state for when you munmap() and mmap() again? The only user of data->lost is writing the PERF_RECORD_LOST event, which only ever happens when you have pages, so counting it when there's no pages seems futile.

#6 lore raw
Quoted 15 lines · Peter Zijlstra
Quoted 6 lines · Stephane Eranian
Quoted 4 lines
Quoted 4 lines

+     if (!data->nr_pages) {
+             atomic_inc(&data->lost);
+             goto out;
+     }

Quoted 7 lines

Well, nr_pages = 0 means all you have is the sampling buffer header page. You cannot save any sample, so you  actually loose everything. Perf reports a dozen samples but those are synthetic. The kernel cannot reject an mmap() for just one page because of the remapped counters, thus it has to handle this gracefully and at least report the lost samples.

Quoted 4 lines

So you want to preserve this state for when you munmap() and mmap() again? The only user of data->lost is writing the PERF_RECORD_LOST event, which only ever happens when you have pages, so counting it when there's no pages seems futile.

You're right. But couldn't you report lost samples in the buffer header as well?

#7 lore raw
Quoted 7 lines · Stephane Eranian
Quoted 5 lines

So you want to preserve this state for when you munmap() and mmap() again? The only user of data->lost is writing the PERF_RECORD_LOST event, which only ever happens when you have pages, so counting it when there's no pages seems futile.

You're right. But couldn't you report lost samples in the buffer header as well?

I guess you could, but what's the point? If you ask for sampling but don't provide a buffer you don't get anything -- how's that a surprise?

#8 lore raw
Quoted 7 lines · Peter Zijlstra
Quoted 6 lines · Stephane Eranian
Quoted 5 lines

So you want to preserve this state for when you munmap() and mmap() again? The only user of data->lost is writing the PERF_RECORD_LOST event, which only ever happens when you have pages, so counting it when there's no pages seems futile.

Quoted 5 lines
Quoted 1 line

You're right. But couldn't you report lost samples in the buffer header as well?

I guess you could, but what's the point? If you ask for sampling but don't provide a buffer you don't get anything -- how's that a surprise?

Fine. I'll update the patch to not account for data->lost.