Skip to contentSkip to conversation

tracing: Fix memory leak on failure path in ftrace_allocate_pages()

PATCH In review Namhyung Kim 2 participants 5 messages Jun 11, 2014 – Jun 12, 2014

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 tracing: Fix memory leak on failure path in ftrace_allocate_pages()

As struct ftrace_page is managed in a single linked list, it should free from the start page.

---
 kernel/trace/ftrace.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/trace/ftrace.c +2 -1
index 5b372e3ed675..ddfda763ded7 100644
@@ -2398,7 +2398,8 @@ ftrace_allocate_pages(unsigned long num_to_init)
 	return start_pg;
 
  free_pages:
-	while (start_pg) {
+	pg = start_pg;
+	while (pg) {
 		order = get_count_order(pg->size / ENTRIES_PER_PAGE);
 		free_pages((unsigned long)pg->records, order);
 		start_pg = pg->next;
Signature · 1 line
2.0.0
#1 lore raw

PATCH 2/2 tracing: Do not copy hash if O_TRUNC is set

When a filter file is open for writing and O_TRUNC is set, there's no need to copy and free the filter entries.

---
 kernel/trace/ftrace.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
kernel/trace/ftrace.c +7 -5
index ddfda763ded7..13885590a184 100644
@@ -2759,7 +2759,13 @@ ftrace_regex_open(struct ftrace_ops *ops, int flag,
 		hash = ops->filter_hash;
 
 	if (file->f_mode & FMODE_WRITE) {
-		iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
+		const int size_bits = FTRACE_HASH_DEFAULT_BITS;
+
+		if (file->f_flags & O_TRUNC)
+			iter->hash = alloc_ftrace_hash(size_bits);
+		else
+			iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
+
 		if (!iter->hash) {
 			trace_parser_put(&iter->parser);
 			kfree(iter);
@@ -2768,10 +2774,6 @@ ftrace_regex_open(struct ftrace_ops *ops, int flag,
 		}
 	}
 
-	if ((file->f_mode & FMODE_WRITE) &&
-	    (file->f_flags & O_TRUNC))
-		ftrace_filter_reset(iter->hash);
-
 	if (file->f_mode & FMODE_READ) {
 		iter->pg = ftrace_pages_start;
 
Signature · 1 line
2.0.0
maintainer
#2 lore raw

Re: [PATCH 2/2] tracing: Do not copy hash if O_TRUNC is set

Quoted 3 lines · On Wed, 11 Jun 2014 17:06:54 +0900 Namhyung Kim <[email protected]> wrote:

When a filter file is open for writing and O_TRUNC is set, there's no need to copy and free the filter entries.

Nice cleanup. I'll add it for 3.17.

Thanks,

-- Steve

Quoted 35 lines
---
 kernel/trace/ftrace.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
kernel/trace/ftrace.c +7 -5
index ddfda763ded7..13885590a184 100644
@@ -2759,7 +2759,13 @@ ftrace_regex_open(struct ftrace_ops *ops, int flag,
 		hash = ops->filter_hash;
 
 	if (file->f_mode & FMODE_WRITE) {
-		iter->hash = alloc_and_copy_ftrace_hash(FTRACE_HASH_DEFAULT_BITS, hash);
+		const int size_bits = FTRACE_HASH_DEFAULT_BITS;
+
+		if (file->f_flags & O_TRUNC)
+			iter->hash = alloc_ftrace_hash(size_bits);
+		else
+			iter->hash = alloc_and_copy_ftrace_hash(size_bits, hash);
+
 		if (!iter->hash) {
 			trace_parser_put(&iter->parser);
 			kfree(iter);
@@ -2768,10 +2774,6 @@ ftrace_regex_open(struct ftrace_ops *ops, int flag,
 		}
 	}
 
-	if ((file->f_mode & FMODE_WRITE) &&
-	    (file->f_flags & O_TRUNC))
-		ftrace_filter_reset(iter->hash);
-
 	if (file->f_mode & FMODE_READ) {
 		iter->pg = ftrace_pages_start;
 
maintainer
#3 lore raw
In reply to Namhyung Kim
Quoted 13 lines · On Wed, 11 Jun 2014 17:06:53 +0900 Namhyung Kim <[email protected]> wrote:

As struct ftrace_page is managed in a single linked list, it should free from the start page.

---
 kernel/trace/ftrace.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/trace/ftrace.c +0 -0
index 5b372e3ed675..ddfda763ded7 100644

kernel/trace/ftrace.c ftrace_allocate_pages(unsigned long num_to_init) · line 2402

Quoted 6 lines
kernel/trace/ftrace.c +2 -1
@@ -2398,7 +2398,8 @@ ftrace_allocate_pages(unsigned long num_to_init)
 	return start_pg;
 
  free_pages:
-	while (start_pg) {
+	pg = start_pg;
+	while (pg) {

It works with just the added "pg = start_page", I would keep the while (start_pg) still.

-- Steve

Quoted 3 lines
kernel/trace/ftrace.c +0 -0
 		order = get_count_order(pg->size / ENTRIES_PER_PAGE);
 		free_pages((unsigned long)pg->records, order);
 		start_pg = pg->next;
#4 lore raw

Hi Steve,

Quoted 18 lines · Steven Rostedt
Quoted 15 lines · On Wed, 11 Jun 2014 17:06:53 +0900 Namhyung Kim <[email protected]> wrote:

As struct ftrace_page is managed in a single linked list, it should free from the start page.

---
 kernel/trace/ftrace.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/trace/ftrace.c +0 -0
index 5b372e3ed675..ddfda763ded7 100644
@@ -2398,7 +2398,8 @@ ftrace_allocate_pages(unsigned long num_to_init)
 	return start_pg;
 
Quoted 7 lines

kernel/trace/ftrace.c ftrace_allocate_pages(unsigned long num_to_init) · line 2402

Quoted 4 lines
kernel/trace/ftrace.c +2 -1
  free_pages:
-	while (start_pg) {
+	pg = start_pg;
+	while (pg) {

It works with just the added "pg = start_page", I would keep the while (start_pg) still.

The reason why I changed it is the code actually uses pg rather than start_pg in the loop. So it's more comfortable for me to check the pg in the condition. But it's minor, I won't insist it strongly.. :)

Thanks,
Namhyung

Quoted 3 lines
Quoted 3 lines
kernel/trace/ftrace.c +0 -0
 		order = get_count_order(pg->size / ENTRIES_PER_PAGE);
 		free_pages((unsigned long)pg->records, order);
 		start_pg = pg->next;