linux-perf-users.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/3] perf disasm: Use disasm_line__free() to properly free disasm_line
@ 2024-10-14 11:42 Li Huafei
  2024-10-14 11:42 ` [PATCH 2/3] " Li Huafei
  2024-10-14 11:42 ` [PATCH 3/3] perf disasm: Fix not cleaning up disasm_line in symbol__disassemble_raw() Li Huafei
  0 siblings, 2 replies; 7+ messages in thread
From: Li Huafei @ 2024-10-14 11:42 UTC (permalink / raw)
  To: atrajeev, namhyung
  Cc: peterz, mingo, acme, mark.rutland, alexander.shishkin, jolsa,
	irogers, adrian.hunter, kan.liang, kjain, sesse, linux-perf-users,
	linux-kernel, lihuafei1

The structure disasm_line contains members that require dynamically
allocated memory and need to be freed correctly using
disasm_line__free().

This patch fixes the incorrect release in
symbol__disassemble_capstone().

Fixes: 6d17edc113de ("perf annotate: Use libcapstone to disassemble")
Signed-off-by: Li Huafei <lihuafei1@huawei.com>
---
 tools/perf/util/disasm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/perf/util/disasm.c b/tools/perf/util/disasm.c
index f05ba7739c1e..42222d61ceb5 100644
--- a/tools/perf/util/disasm.c
+++ b/tools/perf/util/disasm.c
@@ -1717,7 +1717,7 @@ static int symbol__disassemble_capstone(char *filename, struct symbol *sym,
 		 */
 		list_for_each_entry_safe(dl, tmp, &notes->src->source, al.node) {
 			list_del(&dl->al.node);
-			free(dl);
+			disasm_line__free(dl);
 		}
 	}
 	count = -1;
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 2/3] perf disasm: Use disasm_line__free() to properly free disasm_line
  2024-10-14 11:42 [PATCH 1/3] perf disasm: Use disasm_line__free() to properly free disasm_line Li Huafei
@ 2024-10-14 11:42 ` Li Huafei
  2024-10-15 13:55   ` Athira Rajeev
  2024-10-14 11:42 ` [PATCH 3/3] perf disasm: Fix not cleaning up disasm_line in symbol__disassemble_raw() Li Huafei
  1 sibling, 1 reply; 7+ messages in thread
From: Li Huafei @ 2024-10-14 11:42 UTC (permalink / raw)
  To: atrajeev, namhyung
  Cc: peterz, mingo, acme, mark.rutland, alexander.shishkin, jolsa,
	irogers, adrian.hunter, kan.liang, kjain, sesse, linux-perf-users,
	linux-kernel, lihuafei1

The structure disasm_line contains members that require dynamically
allocated memory and need to be freed correctly using
disasm_line__free().

This patch fixes the incorrect freeing in
symbol__disassemble_capstone_powerpc().  Also, since cs_disasm is not
enabled, it does not need to handle the situation described in
symbol__disassemble_capstone() where "offset != len" may occur due to
unknown instructions.

Fixes: c5d60de1813a ("perf annotate: Add support to use libcapstone in powerpc")
Signed-off-by: Li Huafei <lihuafei1@huawei.com>
---
 tools/perf/util/disasm.c | 16 +---------------
 1 file changed, 1 insertion(+), 15 deletions(-)

diff --git a/tools/perf/util/disasm.c b/tools/perf/util/disasm.c
index 42222d61ceb5..40d99f4d5cc7 100644
--- a/tools/perf/util/disasm.c
+++ b/tools/perf/util/disasm.c
@@ -1580,20 +1580,6 @@ static int symbol__disassemble_capstone_powerpc(char *filename, struct symbol *s
 		offset += 4;
 	}
 
-	/* It failed in the middle */
-	if (offset != len) {
-		struct list_head *list = &notes->src->source;
-
-		/* Discard all lines and fallback to objdump */
-		while (!list_empty(list)) {
-			dl = list_first_entry(list, struct disasm_line, al.node);
-
-			list_del_init(&dl->al.node);
-			disasm_line__free(dl);
-		}
-		count = -1;
-	}
-
 out:
 	if (needs_cs_close)
 		cs_close(&handle);
@@ -1612,7 +1598,7 @@ static int symbol__disassemble_capstone_powerpc(char *filename, struct symbol *s
 		 */
 		list_for_each_entry_safe(dl, tmp, &notes->src->source, al.node) {
 			list_del(&dl->al.node);
-			free(dl);
+			disasm_line__free(dl);
 		}
 	}
 	count = -1;
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 3/3] perf disasm: Fix not cleaning up disasm_line in symbol__disassemble_raw()
  2024-10-14 11:42 [PATCH 1/3] perf disasm: Use disasm_line__free() to properly free disasm_line Li Huafei
  2024-10-14 11:42 ` [PATCH 2/3] " Li Huafei
@ 2024-10-14 11:42 ` Li Huafei
  2024-10-17 22:20   ` Namhyung Kim
  1 sibling, 1 reply; 7+ messages in thread
From: Li Huafei @ 2024-10-14 11:42 UTC (permalink / raw)
  To: atrajeev, namhyung
  Cc: peterz, mingo, acme, mark.rutland, alexander.shishkin, jolsa,
	irogers, adrian.hunter, kan.liang, kjain, sesse, linux-perf-users,
	linux-kernel, lihuafei1

In symbol__disassemble_raw(), the created disasm_line should be
discarded before returning an error.

Fixes: 0b971e6bf1c3 ("perf annotate: Add support to capture and parse raw instruction in powerpc using dso__data_read_offset utility")
Signed-off-by: Li Huafei <lihuafei1@huawei.com>
---
 tools/perf/util/disasm.c | 26 ++++++++++++--------------
 1 file changed, 12 insertions(+), 14 deletions(-)

diff --git a/tools/perf/util/disasm.c b/tools/perf/util/disasm.c
index 40d99f4d5cc7..4e5becd5eca6 100644
--- a/tools/perf/util/disasm.c
+++ b/tools/perf/util/disasm.c
@@ -1774,25 +1774,23 @@ static int symbol__disassemble_raw(char *filename, struct symbol *sym,
 		offset += 4;
 	}
 
-	/* It failed in the middle */
-	if (offset != len) {
-		struct list_head *list = &notes->src->source;
-
-		/* Discard all lines and fallback to objdump */
-		while (!list_empty(list)) {
-			dl = list_first_entry(list, struct disasm_line, al.node);
-
-			list_del_init(&dl->al.node);
-			disasm_line__free(dl);
-		}
-		count = -1;
-	}
-
 out:
 	free(buf);
 	return count < 0 ? count : 0;
 
 err:
+	if (!list_empty(&notes->src->source)) {
+		struct disasm_line *tmp;
+
+		/*
+		 * It probably failed in the middle of the above loop.
+		 * Release any resources it might add.
+		 */
+		list_for_each_entry_safe(dl, tmp, &notes->src->source, al.node) {
+			list_del(&dl->al.node);
+			disasm_line__free(dl);
+		}
+	}
 	count = -1;
 	goto out;
 }
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH 2/3] perf disasm: Use disasm_line__free() to properly free disasm_line
  2024-10-14 11:42 ` [PATCH 2/3] " Li Huafei
@ 2024-10-15 13:55   ` Athira Rajeev
  2024-10-19  7:47     ` Li Huafei
  0 siblings, 1 reply; 7+ messages in thread
From: Athira Rajeev @ 2024-10-15 13:55 UTC (permalink / raw)
  To: Li Huafei
  Cc: namhyung, peterz, mingo, acme, mark.rutland, alexander.shishkin,
	jolsa, irogers, adrian.hunter, kan.liang, kjain, sesse,
	linux-perf-users, linux-kernel



> On 14 Oct 2024, at 5:12 PM, Li Huafei <lihuafei1@huawei.com> wrote:
> 
> The structure disasm_line contains members that require dynamically
> allocated memory and need to be freed correctly using
> disasm_line__free().
> 
> This patch fixes the incorrect freeing in
> symbol__disassemble_capstone_powerpc().  Also, since cs_disasm is not
> enabled, it does not need to handle the situation described in
> symbol__disassemble_capstone() where "offset != len" may occur due to
> unknown instructions.
> 
> Fixes: c5d60de1813a ("perf annotate: Add support to use libcapstone in powerpc")
> Signed-off-by: Li Huafei <lihuafei1@huawei.com>

For the patches

Tested-by: Athira Rajeev <atrajeev@linux.vnet.ibm.com <mailto:atrajeev@linux.vnet.ibm.com>>

Thanks
Athira
> ---
> tools/perf/util/disasm.c | 16 +---------------
> 1 file changed, 1 insertion(+), 15 deletions(-)
> 
> diff --git a/tools/perf/util/disasm.c b/tools/perf/util/disasm.c
> index 42222d61ceb5..40d99f4d5cc7 100644
> --- a/tools/perf/util/disasm.c
> +++ b/tools/perf/util/disasm.c
> @@ -1580,20 +1580,6 @@ static int symbol__disassemble_capstone_powerpc(char *filename, struct symbol *s
> offset += 4;
> }
> 
> - /* It failed in the middle */
> - if (offset != len) {
> - struct list_head *list = &notes->src->source;
> -
> - /* Discard all lines and fallback to objdump */
> - while (!list_empty(list)) {
> - dl = list_first_entry(list, struct disasm_line, al.node);
> -
> - list_del_init(&dl->al.node);
> - disasm_line__free(dl);
> - }
> - count = -1;
> - }
> -
> out:
> if (needs_cs_close)
> cs_close(&handle);
> @@ -1612,7 +1598,7 @@ static int symbol__disassemble_capstone_powerpc(char *filename, struct symbol *s
> */
> list_for_each_entry_safe(dl, tmp, &notes->src->source, al.node) {
> list_del(&dl->al.node);
> - free(dl);
> + disasm_line__free(dl);
> }
> }
> count = -1;
> -- 
> 2.25.1
> 


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 3/3] perf disasm: Fix not cleaning up disasm_line in symbol__disassemble_raw()
  2024-10-14 11:42 ` [PATCH 3/3] perf disasm: Fix not cleaning up disasm_line in symbol__disassemble_raw() Li Huafei
@ 2024-10-17 22:20   ` Namhyung Kim
  2024-10-19  2:44     ` Li Huafei
  0 siblings, 1 reply; 7+ messages in thread
From: Namhyung Kim @ 2024-10-17 22:20 UTC (permalink / raw)
  To: Li Huafei
  Cc: atrajeev, peterz, mingo, acme, mark.rutland, alexander.shishkin,
	jolsa, irogers, adrian.hunter, kan.liang, kjain, sesse,
	linux-perf-users, linux-kernel

On Mon, Oct 14, 2024 at 07:42:48PM +0800, Li Huafei wrote:
> In symbol__disassemble_raw(), the created disasm_line should be
> discarded before returning an error.

But other error paths don't need to free the disasm_line because they
failed before creating one.  I think the simpler fix is to break the
loop when it fails on disasm_line__new().

Thanks,
Namhyung

> 
> Fixes: 0b971e6bf1c3 ("perf annotate: Add support to capture and parse raw instruction in powerpc using dso__data_read_offset utility")
> Signed-off-by: Li Huafei <lihuafei1@huawei.com>
> ---
>  tools/perf/util/disasm.c | 26 ++++++++++++--------------
>  1 file changed, 12 insertions(+), 14 deletions(-)
> 
> diff --git a/tools/perf/util/disasm.c b/tools/perf/util/disasm.c
> index 40d99f4d5cc7..4e5becd5eca6 100644
> --- a/tools/perf/util/disasm.c
> +++ b/tools/perf/util/disasm.c
> @@ -1774,25 +1774,23 @@ static int symbol__disassemble_raw(char *filename, struct symbol *sym,
>  		offset += 4;
>  	}
>  
> -	/* It failed in the middle */
> -	if (offset != len) {
> -		struct list_head *list = &notes->src->source;
> -
> -		/* Discard all lines and fallback to objdump */
> -		while (!list_empty(list)) {
> -			dl = list_first_entry(list, struct disasm_line, al.node);
> -
> -			list_del_init(&dl->al.node);
> -			disasm_line__free(dl);
> -		}
> -		count = -1;
> -	}
> -
>  out:
>  	free(buf);
>  	return count < 0 ? count : 0;
>  
>  err:
> +	if (!list_empty(&notes->src->source)) {
> +		struct disasm_line *tmp;
> +
> +		/*
> +		 * It probably failed in the middle of the above loop.
> +		 * Release any resources it might add.
> +		 */
> +		list_for_each_entry_safe(dl, tmp, &notes->src->source, al.node) {
> +			list_del(&dl->al.node);
> +			disasm_line__free(dl);
> +		}
> +	}
>  	count = -1;
>  	goto out;
>  }
> -- 
> 2.25.1
> 

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 3/3] perf disasm: Fix not cleaning up disasm_line in symbol__disassemble_raw()
  2024-10-17 22:20   ` Namhyung Kim
@ 2024-10-19  2:44     ` Li Huafei
  0 siblings, 0 replies; 7+ messages in thread
From: Li Huafei @ 2024-10-19  2:44 UTC (permalink / raw)
  To: Namhyung Kim
  Cc: atrajeev, peterz, mingo, acme, mark.rutland, alexander.shishkin,
	jolsa, irogers, adrian.hunter, kan.liang, kjain, sesse,
	linux-perf-users, linux-kernel



On 2024/10/18 6:20, Namhyung Kim wrote:
> On Mon, Oct 14, 2024 at 07:42:48PM +0800, Li Huafei wrote:
>> In symbol__disassemble_raw(), the created disasm_line should be
>> discarded before returning an error.
> 
> But other error paths don't need to free the disasm_line because they
> failed before creating one.  I think the simpler fix is to break the
> loop when it fails on disasm_line__new().
> 

Yes, we only need to consider whether to release the created lines when
the loop fails in the middle. I will send a v2 version later.

Thanks,
Huafei

> Thanks,
> Namhyung
> 
>>
>> Fixes: 0b971e6bf1c3 ("perf annotate: Add support to capture and parse raw instruction in powerpc using dso__data_read_offset utility")
>> Signed-off-by: Li Huafei <lihuafei1@huawei.com>
>> ---
>>  tools/perf/util/disasm.c | 26 ++++++++++++--------------
>>  1 file changed, 12 insertions(+), 14 deletions(-)
>>
>> diff --git a/tools/perf/util/disasm.c b/tools/perf/util/disasm.c
>> index 40d99f4d5cc7..4e5becd5eca6 100644
>> --- a/tools/perf/util/disasm.c
>> +++ b/tools/perf/util/disasm.c
>> @@ -1774,25 +1774,23 @@ static int symbol__disassemble_raw(char *filename, struct symbol *sym,
>>  		offset += 4;
>>  	}
>>  
>> -	/* It failed in the middle */
>> -	if (offset != len) {
>> -		struct list_head *list = &notes->src->source;
>> -
>> -		/* Discard all lines and fallback to objdump */
>> -		while (!list_empty(list)) {
>> -			dl = list_first_entry(list, struct disasm_line, al.node);
>> -
>> -			list_del_init(&dl->al.node);
>> -			disasm_line__free(dl);
>> -		}
>> -		count = -1;
>> -	}
>> -
>>  out:
>>  	free(buf);
>>  	return count < 0 ? count : 0;
>>  
>>  err:
>> +	if (!list_empty(&notes->src->source)) {
>> +		struct disasm_line *tmp;
>> +
>> +		/*
>> +		 * It probably failed in the middle of the above loop.
>> +		 * Release any resources it might add.
>> +		 */
>> +		list_for_each_entry_safe(dl, tmp, &notes->src->source, al.node) {
>> +			list_del(&dl->al.node);
>> +			disasm_line__free(dl);
>> +		}
>> +	}
>>  	count = -1;
>>  	goto out;
>>  }
>> -- 
>> 2.25.1
>>
> .
> 

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 2/3] perf disasm: Use disasm_line__free() to properly free disasm_line
  2024-10-15 13:55   ` Athira Rajeev
@ 2024-10-19  7:47     ` Li Huafei
  0 siblings, 0 replies; 7+ messages in thread
From: Li Huafei @ 2024-10-19  7:47 UTC (permalink / raw)
  To: Athira Rajeev
  Cc: namhyung, peterz, mingo, acme, mark.rutland, alexander.shishkin,
	jolsa, irogers, adrian.hunter, kan.liang, kjain, sesse,
	linux-perf-users, linux-kernel



On 2024/10/15 21:55, Athira Rajeev wrote:
> 
> 
>> On 14 Oct 2024, at 5:12 PM, Li Huafei <lihuafei1@huawei.com> wrote:
>>
>> The structure disasm_line contains members that require dynamically
>> allocated memory and need to be freed correctly using
>> disasm_line__free().
>>
>> This patch fixes the incorrect freeing in
>> symbol__disassemble_capstone_powerpc().  Also, since cs_disasm is not
>> enabled, it does not need to handle the situation described in
>> symbol__disassemble_capstone() where "offset != len" may occur due to
>> unknown instructions.
>>
>> Fixes: c5d60de1813a ("perf annotate: Add support to use libcapstone in powerpc")
>> Signed-off-by: Li Huafei <lihuafei1@huawei.com>
> 
> For the patches
> 
> Tested-by: Athira Rajeev <atrajeev@linux.vnet.ibm.com <mailto:atrajeev@linux.vnet.ibm.com>>
> 

Thank you for the testing. As suggested by Namhyung in patch 3, the
patches need some modifications. I have sent v2, and if there are any
issues with v2, please let me know.

Thanks,
Huafei

> Thanks
> Athira
>> ---
>> tools/perf/util/disasm.c | 16 +---------------
>> 1 file changed, 1 insertion(+), 15 deletions(-)
>>
>> diff --git a/tools/perf/util/disasm.c b/tools/perf/util/disasm.c
>> index 42222d61ceb5..40d99f4d5cc7 100644
>> --- a/tools/perf/util/disasm.c
>> +++ b/tools/perf/util/disasm.c
>> @@ -1580,20 +1580,6 @@ static int symbol__disassemble_capstone_powerpc(char *filename, struct symbol *s
>> offset += 4;
>> }
>>
>> - /* It failed in the middle */
>> - if (offset != len) {
>> - struct list_head *list = &notes->src->source;
>> -
>> - /* Discard all lines and fallback to objdump */
>> - while (!list_empty(list)) {
>> - dl = list_first_entry(list, struct disasm_line, al.node);
>> -
>> - list_del_init(&dl->al.node);
>> - disasm_line__free(dl);
>> - }
>> - count = -1;
>> - }
>> -
>> out:
>> if (needs_cs_close)
>> cs_close(&handle);
>> @@ -1612,7 +1598,7 @@ static int symbol__disassemble_capstone_powerpc(char *filename, struct symbol *s
>> */
>> list_for_each_entry_safe(dl, tmp, &notes->src->source, al.node) {
>> list_del(&dl->al.node);
>> - free(dl);
>> + disasm_line__free(dl);
>> }
>> }
>> count = -1;
>> -- 
>> 2.25.1
>>
> 
> 
> .
> 

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2024-10-19  7:47 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-14 11:42 [PATCH 1/3] perf disasm: Use disasm_line__free() to properly free disasm_line Li Huafei
2024-10-14 11:42 ` [PATCH 2/3] " Li Huafei
2024-10-15 13:55   ` Athira Rajeev
2024-10-19  7:47     ` Li Huafei
2024-10-14 11:42 ` [PATCH 3/3] perf disasm: Fix not cleaning up disasm_line in symbol__disassemble_raw() Li Huafei
2024-10-17 22:20   ` Namhyung Kim
2024-10-19  2:44     ` Li Huafei

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).