[v2] perf data convert: Fix segfault when converting to json on arm64

Message ID 20240117071331.59725-1-ilkka@os.amperecomputing.com
State New
Headers
Series [v2] perf data convert: Fix segfault when converting to json on arm64 |

Commit Message

Ilkka Koskinen Jan. 17, 2024, 7:13 a.m. UTC
  Arm64 doesn't have Model in /proc/cpuinfo and, thus, cpu_desc doesn't get
assigned.

Running
	$ perf data convert --to-json perf.data.json

ends up calling output_json_string() with NULL pointer, which causes a
segmentation fault.

Signed-off-by: Ilkka Koskinen <ilkka@os.amperecomputing.com>
---
v1:
	- https://lore.kernel.org/all/20240111232923.8138-1-ilkka@os.amperecomputing.com/
v2:
	- Changed the patch based on James's comments.
---
tools/perf/util/data-convert-json.c | 6 ++++++
 1 file changed, 6 insertions(+)
  

Comments

James Clark Jan. 17, 2024, 3:30 p.m. UTC | #1
On 17/01/2024 07:13, Ilkka Koskinen wrote:
> Arm64 doesn't have Model in /proc/cpuinfo and, thus, cpu_desc doesn't get
> assigned.
> 
> Running
> 	$ perf data convert --to-json perf.data.json
> 
> ends up calling output_json_string() with NULL pointer, which causes a
> segmentation fault.
> 
> Signed-off-by: Ilkka Koskinen <ilkka@os.amperecomputing.com>
> ---
> v1:
> 	- https://lore.kernel.org/all/20240111232923.8138-1-ilkka@os.amperecomputing.com/
> v2:
> 	- Changed the patch based on James's comments.
> ---
> tools/perf/util/data-convert-json.c | 6 ++++++
>  1 file changed, 6 insertions(+)
> 
> diff --git a/tools/perf/util/data-convert-json.c b/tools/perf/util/data-convert-json.c
> index 5bb3c2ba95ca..e8b006c3581f 100644
> --- a/tools/perf/util/data-convert-json.c
> +++ b/tools/perf/util/data-convert-json.c
> @@ -284,7 +284,13 @@ static void output_headers(struct perf_session *session, struct convert_json *c)
>  	output_json_key_string(out, true, 2, "os-release", header->env.os_release);
>  	output_json_key_string(out, true, 2, "arch", header->env.arch);
>  
> +#if !defined(__aarch64__)

I think this needs to read from header->env.arch, otherwise it won't
work if you run data convert on another machine.

I'm assuming that perf data convert is one of the commands that runs on
a pre-recorded file, rather than a command that creates the perf.data
file to begin with?

> +	/*
> +	 * arm64 doesn't have Model section in /proc/cpuinfo and, thus, cpu-desc
> +	 * is not set.
> +	 */
>  	output_json_key_string(out, true, 2, "cpu-desc", header->env.cpu_desc);
> +#endif
>  	output_json_key_string(out, true, 2, "cpuid", header->env.cpuid);
>  	output_json_key_format(out, true, 2, "nrcpus-online", "%u", header->env.nr_cpus_online);
>  	output_json_key_format(out, true, 2, "nrcpus-avail", "%u", header->env.nr_cpus_avail);
  
Ilkka Koskinen Jan. 17, 2024, 9:38 p.m. UTC | #2
On Wed, 17 Jan 2024, James Clark wrote:

>
>
> On 17/01/2024 07:13, Ilkka Koskinen wrote:
>> Arm64 doesn't have Model in /proc/cpuinfo and, thus, cpu_desc doesn't get
>> assigned.
>>
>> Running
>> 	$ perf data convert --to-json perf.data.json
>>
>> ends up calling output_json_string() with NULL pointer, which causes a
>> segmentation fault.
>>
>> Signed-off-by: Ilkka Koskinen <ilkka@os.amperecomputing.com>
>> ---
>> v1:
>> 	- https://lore.kernel.org/all/20240111232923.8138-1-ilkka@os.amperecomputing.com/
>> v2:
>> 	- Changed the patch based on James's comments.
>> ---
>> tools/perf/util/data-convert-json.c | 6 ++++++
>>  1 file changed, 6 insertions(+)
>>
>> diff --git a/tools/perf/util/data-convert-json.c b/tools/perf/util/data-convert-json.c
>> index 5bb3c2ba95ca..e8b006c3581f 100644
>> --- a/tools/perf/util/data-convert-json.c
>> +++ b/tools/perf/util/data-convert-json.c
>> @@ -284,7 +284,13 @@ static void output_headers(struct perf_session *session, struct convert_json *c)
>>  	output_json_key_string(out, true, 2, "os-release", header->env.os_release);
>>  	output_json_key_string(out, true, 2, "arch", header->env.arch);
>>
>> +#if !defined(__aarch64__)
>
> I think this needs to read from header->env.arch, otherwise it won't
> work if you run data convert on another machine.
>
> I'm assuming that perf data convert is one of the commands that runs on
> a pre-recorded file, rather than a command that creates the perf.data
> file to begin with?

Oh, right. I'll fix it right away.

Cheers, Ilkka

>
>> +	/*
>> +	 * arm64 doesn't have Model section in /proc/cpuinfo and, thus, cpu-desc
>> +	 * is not set.
>> +	 */
>>  	output_json_key_string(out, true, 2, "cpu-desc", header->env.cpu_desc);
>> +#endif
>>  	output_json_key_string(out, true, 2, "cpuid", header->env.cpuid);
>>  	output_json_key_format(out, true, 2, "nrcpus-online", "%u", header->env.nr_cpus_online);
>>  	output_json_key_format(out, true, 2, "nrcpus-avail", "%u", header->env.nr_cpus_avail);
>
  

Patch

diff --git a/tools/perf/util/data-convert-json.c b/tools/perf/util/data-convert-json.c
index 5bb3c2ba95ca..e8b006c3581f 100644
--- a/tools/perf/util/data-convert-json.c
+++ b/tools/perf/util/data-convert-json.c
@@ -284,7 +284,13 @@  static void output_headers(struct perf_session *session, struct convert_json *c)
 	output_json_key_string(out, true, 2, "os-release", header->env.os_release);
 	output_json_key_string(out, true, 2, "arch", header->env.arch);
 
+#if !defined(__aarch64__)
+	/*
+	 * arm64 doesn't have Model section in /proc/cpuinfo and, thus, cpu-desc
+	 * is not set.
+	 */
 	output_json_key_string(out, true, 2, "cpu-desc", header->env.cpu_desc);
+#endif
 	output_json_key_string(out, true, 2, "cpuid", header->env.cpuid);
 	output_json_key_format(out, true, 2, "nrcpus-online", "%u", header->env.nr_cpus_online);
 	output_json_key_format(out, true, 2, "nrcpus-avail", "%u", header->env.nr_cpus_avail);