Skip to contentSkip to conversation

perf: double free at function perf_hpp__reset_output_field

PATCH Superseded [email protected] 4 participants 20 messages Mar 15, 2017 – May 31, 2017 perf events

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf: double free at function perf_hpp__reset_output_field

From: Changbin Du <[email protected]>

Some perf_hpp_fmt both registered at field and sort list. For such instance, we only can free it when removed from the both lists.

---
 tools/perf/ui/hist.c | 25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)
tools/perf/ui/hist.c +15 -10
index 5d632dc..f94b301 100644
@@ -609,20 +609,25 @@ static void fmt_free(struct perf_hpp_fmt *fmt)
 
 void perf_hpp__reset_output_field(struct perf_hpp_list *list)
 {
-	struct perf_hpp_fmt *fmt, *tmp;
+	struct perf_hpp_fmt *field_fmt, *sort_fmt, *tmp1, *tmp2;
 
 	/* reset output fields */
-	perf_hpp_list__for_each_format_safe(list, fmt, tmp) {
-		list_del_init(&fmt->list);
-		list_del_init(&fmt->sort_list);
-		fmt_free(fmt);
+	perf_hpp_list__for_each_format_safe(list, field_fmt, tmp1) {
+		list_del_init(&field_fmt->list);
+		/* reset sort keys */
+		perf_hpp_list__for_each_sort_list_safe(list, sort_fmt, tmp2) {
+			if (field_fmt == sort_fmt) {
+				list_del_init(&field_fmt->sort_list);
+				break;
+			}
+		}
+		fmt_free(field_fmt);
 	}
 
-	/* reset sort keys */
-	perf_hpp_list__for_each_sort_list_safe(list, fmt, tmp) {
-		list_del_init(&fmt->list);
-		list_del_init(&fmt->sort_list);
-		fmt_free(fmt);
+	/* reset remaining sort keys */
+	perf_hpp_list__for_each_sort_list_safe(list, sort_fmt, tmp1) {
+		list_del_init(&sort_fmt->sort_list);
+		fmt_free(sort_fmt);
 	}
 }
 
Signature · 1 line
2.7.4
#1 lore raw

PATCH v2 1/1 perf: fix double free at function perf_hpp__reset_output_field

From: Changbin Du <[email protected]>

Some perf_hpp_fmt both registered at field and sort list. For such instance, we only can free it when removed from the both lists. This function currently only used by self-test code, but still should fix it.

---
v2: removed redundant Signed-off.

---
 tools/perf/ui/hist.c | 25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)
tools/perf/ui/hist.c +15 -10
index 5d632dc..f94b301 100644
@@ -609,20 +609,25 @@ static void fmt_free(struct perf_hpp_fmt *fmt)
 
 void perf_hpp__reset_output_field(struct perf_hpp_list *list)
 {
-	struct perf_hpp_fmt *fmt, *tmp;
+	struct perf_hpp_fmt *field_fmt, *sort_fmt, *tmp1, *tmp2;
 
 	/* reset output fields */
-	perf_hpp_list__for_each_format_safe(list, fmt, tmp) {
-		list_del_init(&fmt->list);
-		list_del_init(&fmt->sort_list);
-		fmt_free(fmt);
+	perf_hpp_list__for_each_format_safe(list, field_fmt, tmp1) {
+		list_del_init(&field_fmt->list);
+		/* reset sort keys */
+		perf_hpp_list__for_each_sort_list_safe(list, sort_fmt, tmp2) {
+			if (field_fmt == sort_fmt) {
+				list_del_init(&field_fmt->sort_list);
+				break;
+			}
+		}
+		fmt_free(field_fmt);
 	}
 
-	/* reset sort keys */
-	perf_hpp_list__for_each_sort_list_safe(list, fmt, tmp) {
-		list_del_init(&fmt->list);
-		list_del_init(&fmt->sort_list);
-		fmt_free(fmt);
+	/* reset remaining sort keys */
+	perf_hpp_list__for_each_sort_list_safe(list, sort_fmt, tmp1) {
+		list_del_init(&sort_fmt->sort_list);
+		fmt_free(sort_fmt);
 	}
 }
 
Signature · 1 line
2.7.4
#2 lore raw

Re: [PATCH v2] perf: fix double free at function perf_hpp__reset_output_field

Em Mon, Mar 27, 2017 at 02:22:55PM +0800, [email protected] escreveu:

Quoted 6 lines

From: Changbin Du <[email protected]>

Some perf_hpp_fmt both registered at field and sort list. For such instance, we only can free it when removed from the both lists. This function currently only used by self-test code, but still should fix it.

Looks sane, applying,

Jiri, Namhyung, please holler (or ack) if needed,

- Arnaldo

Quoted 50 lines

---
v2: removed redundant Signed-off.

---
 tools/perf/ui/hist.c | 25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)
tools/perf/ui/hist.c +15 -10
index 5d632dc..f94b301 100644
@@ -609,20 +609,25 @@ static void fmt_free(struct perf_hpp_fmt *fmt)
 
 void perf_hpp__reset_output_field(struct perf_hpp_list *list)
 {
-	struct perf_hpp_fmt *fmt, *tmp;
+	struct perf_hpp_fmt *field_fmt, *sort_fmt, *tmp1, *tmp2;
 
 	/* reset output fields */
-	perf_hpp_list__for_each_format_safe(list, fmt, tmp) {
-		list_del_init(&fmt->list);
-		list_del_init(&fmt->sort_list);
-		fmt_free(fmt);
+	perf_hpp_list__for_each_format_safe(list, field_fmt, tmp1) {
+		list_del_init(&field_fmt->list);
+		/* reset sort keys */
+		perf_hpp_list__for_each_sort_list_safe(list, sort_fmt, tmp2) {
+			if (field_fmt == sort_fmt) {
+				list_del_init(&field_fmt->sort_list);
+				break;
+			}
+		}
+		fmt_free(field_fmt);
 	}
 
-	/* reset sort keys */
-	perf_hpp_list__for_each_sort_list_safe(list, fmt, tmp) {
-		list_del_init(&fmt->list);
-		list_del_init(&fmt->sort_list);
-		fmt_free(fmt);
+	/* reset remaining sort keys */
+	perf_hpp_list__for_each_sort_list_safe(list, sort_fmt, tmp1) {
+		list_del_init(&sort_fmt->sort_list);
+		fmt_free(sort_fmt);
 	}
 }
 
Signature · 1 line
2.7.4
maintainer
#3 lore raw

Re: [PATCH v2] perf: fix double free at function perf_hpp__reset_output_field

Hi Arnaldo,

Quoted 11 lines · Arnaldo Carvalho de Melo

Em Mon, Mar 27, 2017 at 02:22:55PM +0800, [email protected] escreveu:

Quoted 6 lines

From: Changbin Du <[email protected]>

Some perf_hpp_fmt both registered at field and sort list. For such instance, we only can free it when removed from the both lists. This function currently only used by self-test code, but still should fix it.

Looks sane, applying,

Jiri, Namhyung, please holler (or ack) if needed,

Did you actually see the double free problem? AFAICS the old code removed a fmt from both list before free it. In the first loop, fmt that was linked to both output list and sort list will be remove. And the second loop frees fmt that was linked only to the sort list (IOW, it frees fmt that was not freed in the first loop).

Thanks,
Namhyung

Quoted 53 lines

- Arnaldo

Quoted 50 lines

---
v2: removed redundant Signed-off.

---
 tools/perf/ui/hist.c | 25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)
tools/perf/ui/hist.c +15 -11
index 5d632dc..f94b301 100644
@@ -609,20 +609,25 @@ static void fmt_free(struct perf_hpp_fmt *fmt)
 
 void perf_hpp__reset_output_field(struct perf_hpp_list *list)
 {
-     struct perf_hpp_fmt *fmt, *tmp;
+     struct perf_hpp_fmt *field_fmt, *sort_fmt, *tmp1, *tmp2;
 
      /* reset output fields */
-     perf_hpp_list__for_each_format_safe(list, fmt, tmp) {
-             list_del_init(&fmt->list);
-             list_del_init(&fmt->sort_list);
-             fmt_free(fmt);
+     perf_hpp_list__for_each_format_safe(list, field_fmt, tmp1) {
+             list_del_init(&field_fmt->list);
+             /* reset sort keys */
+             perf_hpp_list__for_each_sort_list_safe(list, sort_fmt, tmp2) {
+                     if (field_fmt == sort_fmt) {
+                             list_del_init(&field_fmt->sort_list);
+                             break;
+                     }
+             }
+             fmt_free(field_fmt);
      }
 
-     /* reset sort keys */
-     perf_hpp_list__for_each_sort_list_safe(list, fmt, tmp) {
-             list_del_init(&fmt->list);
-             list_del_init(&fmt->sort_list);
-             fmt_free(fmt);
+     /* reset remaining sort keys */
+     perf_hpp_list__for_each_sort_list_safe(list, sort_fmt, tmp1) {
+             list_del_init(&sort_fmt->sort_list);
+             fmt_free(sort_fmt);
      }
 }
 
--

2.7.4

#4 lore raw

Re: [PATCH v2] perf: fix double free at function perf_hpp__reset_output_field

Em Wed, Apr 05, 2017 at 12:34:59AM +0900, Namhyung Kim escreveu:

Quoted 12 lines

Hi Arnaldo,

Quoted 8 lines · Arnaldo Carvalho de Melo

Em Mon, Mar 27, 2017 at 02:22:55PM +0800, [email protected] escreveu:

Quoted 6 lines

From: Changbin Du <[email protected]>

Some perf_hpp_fmt both registered at field and sort list. For such instance, we only can free it when removed from the both lists. This function currently only used by self-test code, but still should fix it.

Quoted 5 lines
Quoted 3 lines

Looks sane, applying,

Jiri, Namhyung, please holler (or ack) if needed,

Did you actually see the double free problem? AFAICS the old code

I assumed that he had seen it, in some self-test code, Changbin, can you please show command output or further describe when this patch would be necessary?

- Arnaldo

Quoted 62 lines

removed a fmt from both list before free it. In the first loop, fmt that was linked to both output list and sort list will be remove. And the second loop frees fmt that was linked only to the sort list (IOW, it frees fmt that was not freed in the first loop).

Thanks,
Namhyung

Quoted 53 lines

- Arnaldo

Quoted 50 lines

---
v2: removed redundant Signed-off.

---
 tools/perf/ui/hist.c | 25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)
tools/perf/ui/hist.c +15 -11
index 5d632dc..f94b301 100644
@@ -609,20 +609,25 @@ static void fmt_free(struct perf_hpp_fmt *fmt)
 
 void perf_hpp__reset_output_field(struct perf_hpp_list *list)
 {
-     struct perf_hpp_fmt *fmt, *tmp;
+     struct perf_hpp_fmt *field_fmt, *sort_fmt, *tmp1, *tmp2;
 
      /* reset output fields */
-     perf_hpp_list__for_each_format_safe(list, fmt, tmp) {
-             list_del_init(&fmt->list);
-             list_del_init(&fmt->sort_list);
-             fmt_free(fmt);
+     perf_hpp_list__for_each_format_safe(list, field_fmt, tmp1) {
+             list_del_init(&field_fmt->list);
+             /* reset sort keys */
+             perf_hpp_list__for_each_sort_list_safe(list, sort_fmt, tmp2) {
+                     if (field_fmt == sort_fmt) {
+                             list_del_init(&field_fmt->sort_list);
+                             break;
+                     }
+             }
+             fmt_free(field_fmt);
      }
 
-     /* reset sort keys */
-     perf_hpp_list__for_each_sort_list_safe(list, fmt, tmp) {
-             list_del_init(&fmt->list);
-             list_del_init(&fmt->sort_list);
-             fmt_free(fmt);
+     /* reset remaining sort keys */
+     perf_hpp_list__for_each_sort_list_safe(list, sort_fmt, tmp1) {
+             list_del_init(&sort_fmt->sort_list);
+             fmt_free(sort_fmt);
      }
 }
 
--

2.7.4

#5 lore raw

Re: [PATCH v2] perf: fix double free at function perf_hpp__reset_output_field

Quoted 15 lines · Arnaldo Carvalho de Melo

Em Wed, Apr 05, 2017 at 12:34:59AM +0900, Namhyung Kim escreveu:

Quoted 14 lines

Hi Arnaldo,

Quoted 10 lines · Arnaldo Carvalho de Melo

Em Mon, Mar 27, 2017 at 02:22:55PM +0800, [email protected] escreveu:

Quoted 6 lines

From: Changbin Du <[email protected]>

Some perf_hpp_fmt both registered at field and sort list. For such instance, we only can free it when removed from the both lists. This function currently only used by self-test code, but still should fix it.

Looks sane, applying,

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

Jiri, Namhyung, please holler (or ack) if needed,

Did you actually see the double free problem? AFAICS the old code

I assumed that he had seen it, in some self-test code, Changbin, can you please show command output or further describe when this patch would be necessary?

Arnaldo, I did observe this issue but not in self-test code. The self-test code uses that function but does not have a case that a fmt linked to two both list. I found this issue when I try to add 'dynamic sort' feature to perf, which I use this function to reset out fields.

Anyway, it is clear that this is a real bug, a potential issue need to fix.

Quoted 7 lines

- Arnaldo

Quoted 5 lines

removed a fmt from both list before free it. In the first loop, fmt that was linked to both output list and sort list will be remove. And the second loop frees fmt that was linked only to the sort list (IOW, it frees fmt that was not freed in the first loop).

This is right. It is to handle the fmts that linked to both two lists.

Quoted 7 lines
Quoted 7 lines

Thanks,
Namhyung

Quoted 3 lines

- Arnaldo

Signature · 2 lines
Thanks,
Changbin Du
#6 lore raw

Re: [PATCH v2] perf: fix double free at function perf_hpp__reset_output_field

Quoted 20 lines · Changbin
Quoted 19 lines · Arnaldo Carvalho de Melo

Em Wed, Apr 05, 2017 at 12:34:59AM +0900, Namhyung Kim escreveu:

Quoted 17 lines

Hi Arnaldo,

Quoted 11 lines · Arnaldo Carvalho de Melo

Em Mon, Mar 27, 2017 at 02:22:55PM +0800, [email protected] escreveu:

Quoted 6 lines

From: Changbin Du <[email protected]>

Some perf_hpp_fmt both registered at field and sort list. For such instance, we only can free it when removed from the both lists. This function currently only used by self-test code, but still should fix it.

Looks sane, applying,

Jiri, Namhyung, please holler (or ack) if needed,

Did you actually see the double free problem? AFAICS the old code

Quoted 8 lines
Quoted 4 lines

I assumed that he had seen it, in some self-test code, Changbin, can you please show command output or further describe when this patch would be necessary?

Arnaldo, I did observe this issue but not in self-test code. The self-test code uses that function but does not have a case that a fmt linked to two both list. I found this issue when I try to add 'dynamic sort' feature to perf, which I use this function to reset out fields.

could you post it with the rest of your patches? might be easier to review

thanks,
jirka

#7 lore raw

Re: [PATCH v2] perf: fix double free at function perf_hpp__reset_output_field

Quoted 25 lines · Jiri Olsa
Quoted 24 lines · Changbin
Quoted 23 lines · Arnaldo Carvalho de Melo

Em Wed, Apr 05, 2017 at 12:34:59AM +0900, Namhyung Kim escreveu:

Quoted 12 lines

Hi Arnaldo,

Quoted 8 lines · Arnaldo Carvalho de Melo

Em Mon, Mar 27, 2017 at 02:22:55PM +0800, [email protected] escreveu:

Quoted 6 lines

From: Changbin Du <[email protected]>

Some perf_hpp_fmt both registered at field and sort list. For such instance, we only can free it when removed from the both lists. This function currently only used by self-test code, but still should fix it.

Quoted 5 lines
Quoted 3 lines

Looks sane, applying,

Jiri, Namhyung, please holler (or ack) if needed,

Did you actually see the double free problem? AFAICS the old code

I assumed that he had seen it, in some self-test code, Changbin, can you please show command output or further describe when this patch would be necessary?

Quoted 7 lines
Quoted 4 lines

Arnaldo, I did observe this issue but not in self-test code. The self-test code uses that function but does not have a case that a fmt linked to two both list. I found this issue when I try to add 'dynamic sort' feature to perf, which I use this function to reset out fields.

could you post it with the rest of your patches? might be easier to review

jirka, I am sorry that the 'dynamic sort' is only half done. Now I am very busy with work at hand. I will send the rest of patches when I finish them. Could we check out this fix first?

Quoted 2 lines

thanks,
jirka

Signature · 2 lines
Thanks,
Changbin Du
#8 lore raw

Re: [PATCH v2] perf: fix double free at function perf_hpp__reset_output_field

In reply to Arnaldo Carvalho de Melo

On Tue, Apr 04, 2017 at 12:19:40PM -0300, Arnaldo Carvalho de Melo wrote:

SNIP

Quoted 15 lines
Quoted 15 lines
---
 tools/perf/ui/hist.c | 25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)
tools/perf/ui/hist.c +1 -1
index 5d632dc..f94b301 100644
@@ -609,20 +609,25 @@ static void fmt_free(struct perf_hpp_fmt *fmt)
 
 void perf_hpp__reset_output_field(struct perf_hpp_list *list)
 {
-	struct perf_hpp_fmt *fmt, *tmp;
+	struct perf_hpp_fmt *field_fmt, *sort_fmt, *tmp1, *tmp2;
 
Quoted 14 lines
Quoted 4 lines
tools/perf/ui/hist.c +0 -3
 	/* reset output fields */
-	perf_hpp_list__for_each_format_safe(list, fmt, tmp) {
-		list_del_init(&fmt->list);
-		list_del_init(&fmt->sort_list);

tools/perf/ui/hist.c static void fmt_free(struct perf_hpp_fmt *fmt) · line 623

Quoted 10 lines
tools/perf/ui/hist.c +9 -1
-		fmt_free(fmt);
+	perf_hpp_list__for_each_format_safe(list, field_fmt, tmp1) {
+		list_del_init(&field_fmt->list);
+		/* reset sort keys */
+		perf_hpp_list__for_each_sort_list_safe(list, sort_fmt, tmp2) {
+			if (field_fmt == sort_fmt) {
+				list_del_init(&field_fmt->sort_list);
+				break;
+			}
+		}

I agree with Namhyung in here.. seems like the only thing you added is to check if the field_fmt was also linked in as a sort entry before you call list_del_init on it

which I think should be also done with list_empty function, but more importantly I dont see a reason for that.. list_del_init call should be fine on empty list

please describe the issue in more details, perhaps we'ew missing something

jirka

#9 lore raw

Re: [PATCH v2] perf: fix double free at function perf_hpp__reset_output_field

Quoted 29 lines · Jiri Olsa

On Tue, Apr 04, 2017 at 12:19:40PM -0300, Arnaldo Carvalho de Melo wrote:

SNIP

Quoted 25 lines
Quoted 25 lines
---
 tools/perf/ui/hist.c | 25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)
tools/perf/ui/hist.c +6 -5
index 5d632dc..f94b301 100644
@@ -609,20 +609,25 @@ static void fmt_free(struct perf_hpp_fmt *fmt)
 
 void perf_hpp__reset_output_field(struct perf_hpp_list *list)
 {
-	struct perf_hpp_fmt *fmt, *tmp;
+	struct perf_hpp_fmt *field_fmt, *sort_fmt, *tmp1, *tmp2;
 
 	/* reset output fields */
-	perf_hpp_list__for_each_format_safe(list, fmt, tmp) {
-		list_del_init(&fmt->list);
-		list_del_init(&fmt->sort_list);
-		fmt_free(fmt);
+	perf_hpp_list__for_each_format_safe(list, field_fmt, tmp1) {
+		list_del_init(&field_fmt->list);
+		/* reset sort keys */
+		perf_hpp_list__for_each_sort_list_safe(list, sort_fmt, tmp2) {
+			if (field_fmt == sort_fmt) {
Quoted 9 lines
Quoted 4 lines

tools/perf/ui/hist.c static void fmt_free(struct perf_hpp_fmt *fmt) · line 623

Quoted 4 lines
tools/perf/ui/hist.c +4 -0
+				list_del_init(&field_fmt->sort_list);
+				break;
+			}
+		}

I agree with Namhyung in here.. seems like the only thing you added is to check if the field_fmt was also linked in as a sort entry before you call list_del_init on it

This is correct.

Quoted 4 lines

which I think should be also done with list_empty function, but more importantly I dont see a reason for that.. list_del_init call should be fine on empty list

You didn't catch the problem here. The problem is double free a fmt. For exampe, fmt A is linked to both list. Then it will be first free by the first iteration over list, then it will be freed again at the second iteration over sort_list. This must cause application crash.

Quoted 4 lines

please describe the issue in more details, perhaps we'ew missing something

jirka

Signature · 2 lines
Thanks,
Changbin Du
#10 lore raw

Re: [PATCH v2] perf: fix double free at function perf_hpp__reset_output_field

Quoted 41 lines · Changbin
Quoted 29 lines · Jiri Olsa

On Tue, Apr 04, 2017 at 12:19:40PM -0300, Arnaldo Carvalho de Melo wrote:

SNIP

Quoted 25 lines
Quoted 25 lines
---
 tools/perf/ui/hist.c | 25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)
tools/perf/ui/hist.c +6 -5
index 5d632dc..f94b301 100644
@@ -609,20 +609,25 @@ static void fmt_free(struct perf_hpp_fmt *fmt)
 
 void perf_hpp__reset_output_field(struct perf_hpp_list *list)
 {
-	struct perf_hpp_fmt *fmt, *tmp;
+	struct perf_hpp_fmt *field_fmt, *sort_fmt, *tmp1, *tmp2;
 
 	/* reset output fields */
-	perf_hpp_list__for_each_format_safe(list, fmt, tmp) {
-		list_del_init(&fmt->list);
-		list_del_init(&fmt->sort_list);
-		fmt_free(fmt);
+	perf_hpp_list__for_each_format_safe(list, field_fmt, tmp1) {
+		list_del_init(&field_fmt->list);
+		/* reset sort keys */
+		perf_hpp_list__for_each_sort_list_safe(list, sort_fmt, tmp2) {
+			if (field_fmt == sort_fmt) {
Quoted 9 lines
Quoted 4 lines

tools/perf/ui/hist.c static void fmt_free(struct perf_hpp_fmt *fmt) · line 623

Quoted 4 lines
tools/perf/ui/hist.c +4 -0
+				list_del_init(&field_fmt->sort_list);
+				break;
+			}
+		}

I agree with Namhyung in here.. seems like the only thing you added is to check if the field_fmt was also linked in as a sort entry before you call list_del_init on it

This is correct.

Quoted 8 lines
Quoted 4 lines

which I think should be also done with list_empty function, but more importantly I dont see a reason for that.. list_del_init call should be fine on empty list

You didn't catch the problem here. The problem is double free a fmt. For exampe, fmt A is linked to both list. Then it will be first free by the first iteration over list, then it will be freed again at the second iteration over sort_list. This must cause application crash.

the original code takes it out of both lists,
so the next itaration won't go over that entry

jirka

#11 lore raw

Re: [PATCH v2] perf: fix double free at function perf_hpp__reset_output_field

Quoted 46 lines · Jiri Olsa
Quoted 45 lines · Changbin
Quoted 29 lines · Jiri Olsa

On Tue, Apr 04, 2017 at 12:19:40PM -0300, Arnaldo Carvalho de Melo wrote:

SNIP

Quoted 25 lines
Quoted 25 lines
---
 tools/perf/ui/hist.c | 25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)
tools/perf/ui/hist.c +6 -5
index 5d632dc..f94b301 100644
@@ -609,20 +609,25 @@ static void fmt_free(struct perf_hpp_fmt *fmt)
 
 void perf_hpp__reset_output_field(struct perf_hpp_list *list)
 {
-	struct perf_hpp_fmt *fmt, *tmp;
+	struct perf_hpp_fmt *field_fmt, *sort_fmt, *tmp1, *tmp2;
 
 	/* reset output fields */
-	perf_hpp_list__for_each_format_safe(list, fmt, tmp) {
-		list_del_init(&fmt->list);
-		list_del_init(&fmt->sort_list);
-		fmt_free(fmt);
+	perf_hpp_list__for_each_format_safe(list, field_fmt, tmp1) {
+		list_del_init(&field_fmt->list);
+		/* reset sort keys */
+		perf_hpp_list__for_each_sort_list_safe(list, sort_fmt, tmp2) {
+			if (field_fmt == sort_fmt) {
Quoted 9 lines
Quoted 4 lines

tools/perf/ui/hist.c static void fmt_free(struct perf_hpp_fmt *fmt) · line 623

Quoted 4 lines
tools/perf/ui/hist.c +4 -0
+				list_del_init(&field_fmt->sort_list);
+				break;
+			}
+		}

I agree with Namhyung in here.. seems like the only thing you added is to check if the field_fmt was also linked in as a sort entry before you call list_del_init on it

This is correct.

Quoted 4 lines

which I think should be also done with list_empty function, but more importantly I dont see a reason for that.. list_del_init call should be fine on empty list

Quoted 8 lines
Quoted 4 lines

You didn't catch the problem here. The problem is double free a fmt. For exampe, fmt A is linked to both list. Then it will be first free by the first iteration over list, then it will be freed again at the second iteration over sort_list. This must cause application crash.

the original code takes it out of both lists,
so the next itaration won't go over that entry

oh, my bad, my desc is wrong. I replayed the crash. The problem is list_del_init a unlinked entry.

perf: Segmentation fault
-------- backtrace --------
./perf[0x57394b] /lib/x86_64-linux-gnu/libc.so.6(+0x354b0)[0x7fb8da3034b0] ./perf(perf_hpp__reset_output_field+0xb7)[0x55dfe7] ./perf(hists__sort_by_fields+0x3d7)[0x509777] ./perf[0x5704c1] ./perf(perf_evlist__tui_browse_hists+0x2e5)[0x5723e5] ./perf(cmd_report+0x1a9b)[0x43b4fb]
./perf[0x494731]
./perf(main+0x704)[0x426304] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7fb8da2ee830] ./perf(_start+0x29)[0x4263f9]
[0x0]

(gdb) print fmt.list
$4 = {next = 0x100, prev = 0x200} // LIST_POISON
(gdb) print fmt.sort_list
$5 = {next = 0x9727d0 <perf_hpp_list+16>, prev = 0x9727d0 <perf_hpp_list+16>}

In this case, the fmt is linked in sort_list, but not in list. So crash at the list_del_init(&fmt->list) of second loop.

Another potential case is the fmt is linked in list, but not in sort_list.

Oh, my brain was broken. correct patch but wrong commit message. :( Will drop this one and submit a new one.

Quoted 1 line

jirka

Signature · 2 lines
Thanks,
Changbin Du
#12 lore raw

Re: [PATCH v2] perf: fix double free at function perf_hpp__reset_output_field

On Tue, Apr 11, 2017 at 11:06:14AM +0800, Du, Changbin wrote:

SNIP

Quoted 21 lines
Quoted 3 lines

the original code takes it out of both lists,
so the next itaration won't go over that entry

oh, my bad, my desc is wrong. I replayed the crash. The problem is list_del_init a unlinked entry.

perf: Segmentation fault
-------- backtrace --------
./perf[0x57394b] /lib/x86_64-linux-gnu/libc.so.6(+0x354b0)[0x7fb8da3034b0] ./perf(perf_hpp__reset_output_field+0xb7)[0x55dfe7] ./perf(hists__sort_by_fields+0x3d7)[0x509777] ./perf[0x5704c1] ./perf(perf_evlist__tui_browse_hists+0x2e5)[0x5723e5] ./perf(cmd_report+0x1a9b)[0x43b4fb]
./perf[0x494731]
./perf(main+0x704)[0x426304] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7fb8da2ee830] ./perf(_start+0x29)[0x4263f9]
[0x0]

Quoted 7 lines

(gdb) print fmt.list
$4 = {next = 0x100, prev = 0x200} // LIST_POISON
(gdb) print fmt.sort_list
$5 = {next = 0x9727d0 <perf_hpp_list+16>, prev = 0x9727d0 <perf_hpp_list+16>}

In this case, the fmt is linked in sort_list, but not in list. So crash at the list_del_init(&fmt->list) of second loop.

so the only place I can see the POISON could get there is in perf_hpp__column_unregister.. can't we just get rid of it like below

jirka

---

tools/perf/ui/hist.c +1 -1
index 5d632dca672a..7577effbf746 100644
@@ -529,7 +529,7 @@ void perf_hpp_list__prepend_sort_field(struct perf_hpp_list *list,
 
 void perf_hpp__column_unregister(struct perf_hpp_fmt *format)
 {
-	list_del(&format->list);
+	list_del_init(&format->list);
 }
 
 void perf_hpp__cancel_cumulate(void)
#13 lore raw

Re: [PATCH v2] perf: fix double free at function perf_hpp__reset_output_field

Quoted 19 lines
Quoted 5 lines

(gdb) print fmt.sort_list
$5 = {next = 0x9727d0 <perf_hpp_list+16>, prev = 0x9727d0 <perf_hpp_list+16>}

In this case, the fmt is linked in sort_list, but not in list. So crash at the list_del_init(&fmt->list) of second loop.

so the only place I can see the POISON could get there is in perf_hpp__column_unregister.. can't we just get rid of it like below

jirka

---

tools/perf/ui/hist.c +0 -0
index 5d632dca672a..7577effbf746 100644

tools/perf/ui/hist.c void perf_hpp_list__prepend_sort_field(struct perf_hpp_list *list, · line 534

Quoted 7 lines
tools/perf/ui/hist.c +1 -1
@@ -529,7 +529,7 @@ void perf_hpp_list__prepend_sort_field(struct perf_hpp_list *list,
 
 void perf_hpp__column_unregister(struct perf_hpp_fmt *format)
 {
-	list_del(&format->list);
+	list_del_init(&format->list);
 }
 

yes, this is an option. But for safety, I sugguest do not rely on list_del_init. No rule rather than create one.

But anyway, both are ok for me. What's your options?

Quoted 1 line
tools/perf/ui/hist.c +0 -0
 void perf_hpp__cancel_cumulate(void)
Signature · 2 lines
Thanks,
Changbin Du
#14 lore raw

Re: [PATCH v2] perf: fix double free at function perf_hpp__reset_output_field

Quoted 29 lines · Changbin
Quoted 19 lines
Quoted 5 lines

(gdb) print fmt.sort_list
$5 = {next = 0x9727d0 <perf_hpp_list+16>, prev = 0x9727d0 <perf_hpp_list+16>}

In this case, the fmt is linked in sort_list, but not in list. So crash at the list_del_init(&fmt->list) of second loop.

so the only place I can see the POISON could get there is in perf_hpp__column_unregister.. can't we just get rid of it like below

jirka

---

tools/perf/ui/hist.c +0 -0
index 5d632dca672a..7577effbf746 100644

tools/perf/ui/hist.c void perf_hpp_list__prepend_sort_field(struct perf_hpp_list *list, · line 534

Quoted 7 lines
tools/perf/ui/hist.c +1 -1
@@ -529,7 +529,7 @@ void perf_hpp_list__prepend_sort_field(struct perf_hpp_list *list,
 
 void perf_hpp__column_unregister(struct perf_hpp_fmt *format)
 {
-	list_del(&format->list);
+	list_del_init(&format->list);
 }
 

yes, this is an option. But for safety, I sugguest do not rely on list_del_init. No rule rather than create one.

Quoted 1 line

But anyway, both are ok for me. What's your options?

hum, also I dont think we need to touch that bit at all if we are going to remove it right away.. how about the change below?

jirka

---

tools/perf/ui/hist.c +4 -4
index 5d632dca672a..0ee7db43dd7d 100644
@@ -613,15 +613,15 @@ void perf_hpp__reset_output_field(struct perf_hpp_list *list)
 
 	/* reset output fields */
 	perf_hpp_list__for_each_format_safe(list, fmt, tmp) {
-		list_del_init(&fmt->list);
-		list_del_init(&fmt->sort_list);
+		list_del(&fmt->list);
+		/* Remove the fmt from next loop processing. */
+		list_del(&fmt->sort_list);
 		fmt_free(fmt);
 	}
 
 	/* reset sort keys */
 	perf_hpp_list__for_each_sort_list_safe(list, fmt, tmp) {
-		list_del_init(&fmt->list);
-		list_del_init(&fmt->sort_list);
+		list_del(&fmt->sort_list);
 		fmt_free(fmt);
 	}
 }
#15 lore raw

Re: [PATCH v2] perf: fix double free at function perf_hpp__reset_output_field

Quoted 19 lines
Quoted 5 lines
Quoted 1 line

yes, this is an option. But for safety, I sugguest do not rely on list_del_init. No rule rather than create one.

But anyway, both are ok for me. What's your options?

hum, also I dont think we need to touch that bit at all if we are going to remove it right away.. how about the change below?

jirka

---

tools/perf/ui/hist.c +0 -0
index 5d632dca672a..0ee7db43dd7d 100644

tools/perf/ui/hist.c void perf_hpp__reset_output_field(struct perf_hpp_list *list) · line 619

Quoted 9 lines
tools/perf/ui/hist.c +3 -2
@@ -613,15 +613,15 @@ void perf_hpp__reset_output_field(struct perf_hpp_list *list)
 
 	/* reset output fields */
 	perf_hpp_list__for_each_format_safe(list, fmt, tmp) {
-		list_del_init(&fmt->list);
-		list_del_init(&fmt->sort_list);
+		list_del(&fmt->list);
+		/* Remove the fmt from next loop processing. */
+		list_del(&fmt->sort_list);
 		fmt_free(fmt);

What if the fmt is not linked to sort_list? I see it is possible (please checking perf_hpp__setup_output_field()). I am not sure if we really has sunch case currently, just concern :)

Quoted 10 lines
tools/perf/ui/hist.c +1 -2
 	}
 
 	/* reset sort keys */
 	perf_hpp_list__for_each_sort_list_safe(list, fmt, tmp) {
-		list_del_init(&fmt->list);
-		list_del_init(&fmt->sort_list);
+		list_del(&fmt->sort_list);
 		fmt_free(fmt);
 	}
 }
Signature · 2 lines
Thanks,
Changbin Du
#16 lore raw

Re: [PATCH v2] perf: fix double free at function perf_hpp__reset_output_field

Quoted 20 lines · Changbin
Quoted 20 lines
Quoted 5 lines
Quoted 1 line

yes, this is an option. But for safety, I sugguest do not rely on list_del_init. No rule rather than create one.

But anyway, both are ok for me. What's your options?

hum, also I dont think we need to touch that bit at all if we are going to remove it right away.. how about the change below?

jirka

---

tools/perf/ui/hist.c +0 -0
index 5d632dca672a..0ee7db43dd7d 100644
@@ -613,15 +613,15 @@ void perf_hpp__reset_output_field(struct perf_hpp_list *list)
 
Quoted 11 lines

tools/perf/ui/hist.c void perf_hpp__reset_output_field(struct perf_hpp_list *list) · line 619

Quoted 8 lines
tools/perf/ui/hist.c +3 -2
 	/* reset output fields */
 	perf_hpp_list__for_each_format_safe(list, fmt, tmp) {
-		list_del_init(&fmt->list);
-		list_del_init(&fmt->sort_list);
+		list_del(&fmt->list);
+		/* Remove the fmt from next loop processing. */
+		list_del(&fmt->sort_list);
 		fmt_free(fmt);

What if the fmt is not linked to sort_list? I see it is possible (please checking perf_hpp__setup_output_field()). I am not sure if we really has sunch case currently, just concern :)

if it's not linked to sort_list, then sort_list is initialized and list_del should do no harm

jirka

#17 lore raw

Re: [PATCH v2] perf: fix double free at function perf_hpp__reset_output_field

Quoted 33 lines · Jiri Olsa
Quoted 20 lines · Changbin
Quoted 20 lines
Quoted 5 lines
Quoted 1 line

yes, this is an option. But for safety, I sugguest do not rely on list_del_init. No rule rather than create one.

But anyway, both are ok for me. What's your options?

hum, also I dont think we need to touch that bit at all if we are going to remove it right away.. how about the change below?

jirka

---

tools/perf/ui/hist.c +0 -0
index 5d632dca672a..0ee7db43dd7d 100644
@@ -613,15 +613,15 @@ void perf_hpp__reset_output_field(struct perf_hpp_list *list)
 
Quoted 11 lines

tools/perf/ui/hist.c void perf_hpp__reset_output_field(struct perf_hpp_list *list) · line 619

Quoted 8 lines
tools/perf/ui/hist.c +3 -2
 	/* reset output fields */
 	perf_hpp_list__for_each_format_safe(list, fmt, tmp) {
-		list_del_init(&fmt->list);
-		list_del_init(&fmt->sort_list);
+		list_del(&fmt->list);
+		/* Remove the fmt from next loop processing. */
+		list_del(&fmt->sort_list);
 		fmt_free(fmt);

What if the fmt is not linked to sort_list? I see it is possible (please checking perf_hpp__setup_output_field()). I am not sure if we really has sunch case currently, just concern :)

Quoted 3 lines

if it's not linked to sort_list, then sort_list is initialized and list_del should do no harm

ok, then it's fine if you insist.

Quoted 1 line

jirka

Signature · 2 lines
Thanks,
Changbin Du
#18 lore raw

Re: [PATCH v2] perf: fix double free at function perf_hpp__reset_output_field

Hi jirka, Will you send a patch to fix this issue? If not I will send my solution in a new thread.

I have given up to add 'dynamic sort' feature since my code is not working and I am engaged in other things. I still hope this fix can be picked up. Thanks!

Quoted 44 lines · Changbin
Quoted 33 lines · Jiri Olsa
Quoted 20 lines · Changbin
Quoted 20 lines
Quoted 5 lines
Quoted 1 line

yes, this is an option. But for safety, I sugguest do not rely on list_del_init. No rule rather than create one.

But anyway, both are ok for me. What's your options?

hum, also I dont think we need to touch that bit at all if we are going to remove it right away.. how about the change below?

jirka

---

tools/perf/ui/hist.c +0 -0
index 5d632dca672a..0ee7db43dd7d 100644
@@ -613,15 +613,15 @@ void perf_hpp__reset_output_field(struct perf_hpp_list *list)
 
Quoted 11 lines

tools/perf/ui/hist.c void perf_hpp__reset_output_field(struct perf_hpp_list *list) · line 619

Quoted 8 lines
tools/perf/ui/hist.c +3 -2
 	/* reset output fields */
 	perf_hpp_list__for_each_format_safe(list, fmt, tmp) {
-		list_del_init(&fmt->list);
-		list_del_init(&fmt->sort_list);
+		list_del(&fmt->list);
+		/* Remove the fmt from next loop processing. */
+		list_del(&fmt->sort_list);
 		fmt_free(fmt);

What if the fmt is not linked to sort_list? I see it is possible (please checking perf_hpp__setup_output_field()). I am not sure if we really has sunch case currently, just concern :)

Quoted 3 lines

if it's not linked to sort_list, then sort_list is initialized and list_del should do no harm

ok, then it's fine if you insist.

Quoted 1 line

jirka

Signature · 2 lines
Thanks,
Changbin Du
Signature · 2 lines
Thanks,
Changbin Du
#19 lore raw

Re: [PATCH v2] perf: fix double free at function perf_hpp__reset_output_field

Quoted 3 lines · Changbin

Hi jirka, Will you send a patch to fix this issue? If not I will send my solution in a new thread.

oops, forgot about this one.. I'll pick it up

thanks,
jirka