Skip to contentSkip to conversation

perf: Fix vmalloc ring buffer free function

PATCH Superseded Jiri Olsa 2 participants 3 messages Feb 26, 2013 – Feb 27, 2013 perf events

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Jiri Olsa

author
#0 lore raw

PATCH 1/1 perf: Fix vmalloc ring buffer free function

If we allocate perf ring buffer with the size of single page, we will get memory corruption when releasing it. It's caused by rb_free_work function (the CONFIG_PERF_USE_VMALLOC option variant).

For single page sized ring buffer the page_order is -1 (because nr_pages is 0). This needs to be recognized in the rb_free_work function and set 'nr' to 0 in this case, so only the user page gets freed.

Cc Corey Ashford <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
---
 kernel/events/ring_buffer.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/events/ring_buffer.c +2 -1
index 23cb34f..21159fb 100644
@@ -336,7 +336,8 @@ static void rb_free_work(struct work_struct *work)
 	int i, nr;
 
 	rb = container_of(work, struct ring_buffer, work);
-	nr = 1 << page_order(rb);
+	/* -1 if there's only user page */
+	nr = page_order(rb) == -1 ? 0 : 1 << page_order(rb);
 
 	base = rb->user_page;
 	for (i = 0; i < nr + 1; i++)
Signature · 1 line
1.7.11.7
#1 lore raw

2013/2/26 Jiri Olsa <[email protected]>:

Quoted 28 lines

If we allocate perf ring buffer with the size of single page, we will get memory corruption when releasing it. It's caused by rb_free_work function (the CONFIG_PERF_USE_VMALLOC option variant).

For single page sized ring buffer the page_order is -1 (because nr_pages is 0). This needs to be recognized in the rb_free_work function and set 'nr' to 0 in this case, so only the user page gets freed.

Cc Corey Ashford <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
---
 kernel/events/ring_buffer.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/events/ring_buffer.c +0 -0
index 23cb34f..21159fb 100644

kernel/events/ring_buffer.c static void rb_free_work(struct work_struct *work) · line 340

Quoted 6 lines
kernel/events/ring_buffer.c +2 -1
@@ -336,7 +336,8 @@ static void rb_free_work(struct work_struct *work)
        int i, nr;
 
        rb = container_of(work, struct ring_buffer, work);
-       nr = 1 << page_order(rb);
+       /* -1 if there's only user page */
+       nr = page_order(rb) == -1 ? 0 : 1 << page_order(rb);

So we allow for 0 sized ring buffer? I'm not entirely convinced this is a good idea. Besides this above case, perf_output_begin() looks dangerous in the case of CONFIG_PERF_USE_VMALLOC due to page_order() being -1. At least rb->nr_pages sould be zero in that case.

Quoted 5 lines
kernel/events/ring_buffer.c +0 -1
        base = rb->user_page;
        for (i = 0; i < nr + 1; i++)
--

1.7.11.7

author
#2 lore raw
Quoted 31 lines · Frederic Weisbecker

2013/2/26 Jiri Olsa <[email protected]>:

Quoted 30 lines

If we allocate perf ring buffer with the size of single page, we will get memory corruption when releasing it. It's caused by rb_free_work function (the CONFIG_PERF_USE_VMALLOC option variant).

For single page sized ring buffer the page_order is -1 (because nr_pages is 0). This needs to be recognized in the rb_free_work function and set 'nr' to 0 in this case, so only the user page gets freed.

Cc Corey Ashford <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
---
 kernel/events/ring_buffer.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/events/ring_buffer.c +0 -0
index 23cb34f..21159fb 100644
@@ -336,7 +336,8 @@ static void rb_free_work(struct work_struct *work)
        int i, nr;
 
Quoted 7 lines

kernel/events/ring_buffer.c static void rb_free_work(struct work_struct *work) · line 340

Quoted 4 lines
kernel/events/ring_buffer.c +2 -1
        rb = container_of(work, struct ring_buffer, work);
-       nr = 1 << page_order(rb);
+       /* -1 if there's only user page */
+       nr = page_order(rb) == -1 ? 0 : 1 << page_order(rb);

So we allow for 0 sized ring buffer? I'm not entirely convinced this is a good idea. Besides this above case, perf_output_begin() looks

We actually have test perf case 'tools/perf/tests/rdpmc.c' using just the user page for counter id and other stuff..

Quoted 2 lines

dangerous in the case of CONFIG_PERF_USE_VMALLOC due to page_order() being -1. At least rb->nr_pages sould be zero in that case.

right, seems to suffer the same way as rb_free_work.. will check

thanks,
jirka