linux-perf-users.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/3] perf: Clean up and fix potential mem leak
@ 2022-09-03  7:25 Shang XiaoJing
  2022-09-03  7:25 ` [PATCH 1/3] perf clean: Add same_cmd_with_prefix helper Shang XiaoJing
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Shang XiaoJing @ 2022-09-03  7:25 UTC (permalink / raw)
  To: peterz, mingo, acme, mark.rutland, alexander.shishkin, jolsa,
	namhyung, linux-perf-users, linux-kernel
  Cc: shangxiaojing

Some clean up in perf.c and builtin-c2c.c.

Shang XiaoJing (3):
  perf clean: Add same_cmd_with_prefix helper
  perf c2c: Add helpers to get counts of loads or stores
  perf c2c: Prevent potential memory leak in c2c_he_zalloc

 tools/perf/builtin-c2c.c | 79 ++++++++++++++++++++--------------------
 tools/perf/perf.c        | 12 ++++--
 2 files changed, 48 insertions(+), 43 deletions(-)

-- 
2.17.1


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

* [PATCH 1/3] perf clean: Add same_cmd_with_prefix helper
  2022-09-03  7:25 [PATCH 0/3] perf: Clean up and fix potential mem leak Shang XiaoJing
@ 2022-09-03  7:25 ` Shang XiaoJing
  2022-09-03  7:25 ` [PATCH 2/3] perf c2c: Add helpers to get counts of loads or stores Shang XiaoJing
  2022-09-03  7:25 ` [PATCH 3/3] perf c2c: Prevent potential memory leak in c2c_he_zalloc Shang XiaoJing
  2 siblings, 0 replies; 6+ messages in thread
From: Shang XiaoJing @ 2022-09-03  7:25 UTC (permalink / raw)
  To: peterz, mingo, acme, mark.rutland, alexander.shishkin, jolsa,
	namhyung, linux-perf-users, linux-kernel
  Cc: shangxiaojing

Wrap repeated code in helper function same_cmd_with_prefix for more
clearly.

Signed-off-by: Shang XiaoJing <shangxiaojing@huawei.com>
---
 tools/perf/perf.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/tools/perf/perf.c b/tools/perf/perf.c
index c21b3973641a..7af135dea1cd 100644
--- a/tools/perf/perf.c
+++ b/tools/perf/perf.c
@@ -99,10 +99,16 @@ struct pager_config {
 	int val;
 };
 
+static bool same_cmd_with_prefix(const char *var, struct pager_config *c,
+				  const char *header)
+{
+	return (strstarts(var, header) && !strcmp(var + strlen(header), c->cmd));
+}
+
 static int pager_command_config(const char *var, const char *value, void *data)
 {
 	struct pager_config *c = data;
-	if (strstarts(var, "pager.") && !strcmp(var + 6, c->cmd))
+	if (same_cmd_with_prefix(var, c, "pager."))
 		c->val = perf_config_bool(var, value);
 	return 0;
 }
@@ -121,9 +127,9 @@ static int check_pager_config(const char *cmd)
 static int browser_command_config(const char *var, const char *value, void *data)
 {
 	struct pager_config *c = data;
-	if (strstarts(var, "tui.") && !strcmp(var + 4, c->cmd))
+	if (same_cmd_with_prefix(var, c, "tui."))
 		c->val = perf_config_bool(var, value);
-	if (strstarts(var, "gtk.") && !strcmp(var + 4, c->cmd))
+	if (same_cmd_with_prefix(var, c, "gtk."))
 		c->val = perf_config_bool(var, value) ? 2 : 0;
 	return 0;
 }
-- 
2.17.1


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

* [PATCH 2/3] perf c2c: Add helpers to get counts of loads or stores
  2022-09-03  7:25 [PATCH 0/3] perf: Clean up and fix potential mem leak Shang XiaoJing
  2022-09-03  7:25 ` [PATCH 1/3] perf clean: Add same_cmd_with_prefix helper Shang XiaoJing
@ 2022-09-03  7:25 ` Shang XiaoJing
  2022-09-03  7:25 ` [PATCH 3/3] perf c2c: Prevent potential memory leak in c2c_he_zalloc Shang XiaoJing
  2 siblings, 0 replies; 6+ messages in thread
From: Shang XiaoJing @ 2022-09-03  7:25 UTC (permalink / raw)
  To: peterz, mingo, acme, mark.rutland, alexander.shishkin, jolsa,
	namhyung, linux-perf-users, linux-kernel
  Cc: shangxiaojing

Wrap repeated code in helper functions get_load_llc_misses,
get_load_cache_hits. For consistence, helper function get_stores is
wraped as well.

Signed-off-by: Shang XiaoJing <shangxiaojing@huawei.com>
---
 tools/perf/builtin-c2c.c | 65 +++++++++++++++++-----------------------
 1 file changed, 28 insertions(+), 37 deletions(-)

diff --git a/tools/perf/builtin-c2c.c b/tools/perf/builtin-c2c.c
index 653e13b5037e..12f272811487 100644
--- a/tools/perf/builtin-c2c.c
+++ b/tools/perf/builtin-c2c.c
@@ -673,28 +673,35 @@ STAT_FN(ld_l2hit)
 STAT_FN(ld_llchit)
 STAT_FN(rmt_hit)
 
-static uint64_t total_records(struct c2c_stats *stats)
+static uint64_t get_load_llc_misses(struct c2c_stats *stats)
 {
-	uint64_t lclmiss, ldcnt, total;
-
-	lclmiss  = stats->lcl_dram +
-		   stats->rmt_dram +
-		   stats->rmt_hitm +
-		   stats->rmt_hit;
+	return stats->lcl_dram +
+	       stats->rmt_dram +
+	       stats->rmt_hitm +
+	       stats->rmt_hit;
+}
 
-	ldcnt    = lclmiss +
-		   stats->ld_fbhit +
-		   stats->ld_l1hit +
-		   stats->ld_l2hit +
-		   stats->ld_llchit +
-		   stats->lcl_hitm;
+static uint64_t get_load_cache_hits(struct c2c_stats *stats)
+{
+	return stats->ld_fbhit +
+	       stats->ld_l1hit +
+	       stats->ld_l2hit +
+	       stats->ld_llchit +
+	       stats->lcl_hitm;
+}
 
-	total    = ldcnt +
-		   stats->st_l1hit +
-		   stats->st_l1miss +
-		   stats->st_na;
+static uint64_t get_stores(struct c2c_stats *stats)
+{
+	return stats->st_l1hit +
+	       stats->st_l1miss +
+	       stats->st_na;
+}
 
-	return total;
+static uint64_t total_records(struct c2c_stats *stats)
+{
+	return get_load_llc_misses(stats) +
+	       get_load_cache_hits(stats) +
+	       get_stores(stats);
 }
 
 static int
@@ -731,21 +738,8 @@ tot_recs_cmp(struct perf_hpp_fmt *fmt __maybe_unused,
 
 static uint64_t total_loads(struct c2c_stats *stats)
 {
-	uint64_t lclmiss, ldcnt;
-
-	lclmiss  = stats->lcl_dram +
-		   stats->rmt_dram +
-		   stats->rmt_hitm +
-		   stats->rmt_hit;
-
-	ldcnt    = lclmiss +
-		   stats->ld_fbhit +
-		   stats->ld_l1hit +
-		   stats->ld_l2hit +
-		   stats->ld_llchit +
-		   stats->lcl_hitm;
-
-	return ldcnt;
+	return get_load_llc_misses(stats) +
+	       get_load_cache_hits(stats);
 }
 
 static int
@@ -2370,10 +2364,7 @@ static void print_c2c__display_stats(FILE *out)
 	int llc_misses;
 	struct c2c_stats *stats = &c2c.hists.stats;
 
-	llc_misses = stats->lcl_dram +
-		     stats->rmt_dram +
-		     stats->rmt_hit +
-		     stats->rmt_hitm;
+	llc_misses = get_load_llc_misses(stats);
 
 	fprintf(out, "=================================================\n");
 	fprintf(out, "            Trace Event Information              \n");
-- 
2.17.1


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

* [PATCH 3/3] perf c2c: Prevent potential memory leak in c2c_he_zalloc
  2022-09-03  7:25 [PATCH 0/3] perf: Clean up and fix potential mem leak Shang XiaoJing
  2022-09-03  7:25 ` [PATCH 1/3] perf clean: Add same_cmd_with_prefix helper Shang XiaoJing
  2022-09-03  7:25 ` [PATCH 2/3] perf c2c: Add helpers to get counts of loads or stores Shang XiaoJing
@ 2022-09-03  7:25 ` Shang XiaoJing
  2022-09-05 10:19   ` Jiri Olsa
  2 siblings, 1 reply; 6+ messages in thread
From: Shang XiaoJing @ 2022-09-03  7:25 UTC (permalink / raw)
  To: peterz, mingo, acme, mark.rutland, alexander.shishkin, jolsa,
	namhyung, linux-perf-users, linux-kernel
  Cc: shangxiaojing

Free allocated resources when zalloc is failed for members in c2c_he, to
prevent potential memory leak in c2c_he_zalloc.

Signed-off-by: Shang XiaoJing <shangxiaojing@huawei.com>
---
 tools/perf/builtin-c2c.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/tools/perf/builtin-c2c.c b/tools/perf/builtin-c2c.c
index 12f272811487..5530433eda80 100644
--- a/tools/perf/builtin-c2c.c
+++ b/tools/perf/builtin-c2c.c
@@ -146,15 +146,15 @@ static void *c2c_he_zalloc(size_t size)
 
 	c2c_he->cpuset = bitmap_zalloc(c2c.cpus_cnt);
 	if (!c2c_he->cpuset)
-		return NULL;
+		goto out_free_he;
 
 	c2c_he->nodeset = bitmap_zalloc(c2c.nodes_cnt);
 	if (!c2c_he->nodeset)
-		return NULL;
+		goto out_free_cpuset;
 
 	c2c_he->node_stats = zalloc(c2c.nodes_cnt * sizeof(*c2c_he->node_stats));
 	if (!c2c_he->node_stats)
-		return NULL;
+		goto out_free_nodeset;
 
 	init_stats(&c2c_he->cstats.lcl_hitm);
 	init_stats(&c2c_he->cstats.rmt_hitm);
@@ -163,6 +163,14 @@ static void *c2c_he_zalloc(size_t size)
 	init_stats(&c2c_he->cstats.load);
 
 	return &c2c_he->he;
+
+out_free_nodeset:
+	free(c2c_he->nodeset);
+out_free_cpuset:
+	free(c2c_he->cpuset);
+out_free_he:
+	free(c2c_he);
+	return NULL;
 }
 
 static void c2c_he_free(void *he)
-- 
2.17.1


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

* Re: [PATCH 3/3] perf c2c: Prevent potential memory leak in c2c_he_zalloc
  2022-09-03  7:25 ` [PATCH 3/3] perf c2c: Prevent potential memory leak in c2c_he_zalloc Shang XiaoJing
@ 2022-09-05 10:19   ` Jiri Olsa
  2022-09-05 11:22     ` shangxiaojing
  0 siblings, 1 reply; 6+ messages in thread
From: Jiri Olsa @ 2022-09-05 10:19 UTC (permalink / raw)
  To: Shang XiaoJing
  Cc: peterz, mingo, acme, mark.rutland, alexander.shishkin, namhyung,
	linux-perf-users, linux-kernel

On Sat, Sep 03, 2022 at 03:25:42PM +0800, Shang XiaoJing wrote:
> Free allocated resources when zalloc is failed for members in c2c_he, to
> prevent potential memory leak in c2c_he_zalloc.
> 
> Signed-off-by: Shang XiaoJing <shangxiaojing@huawei.com>
> ---
>  tools/perf/builtin-c2c.c | 14 +++++++++++---
>  1 file changed, 11 insertions(+), 3 deletions(-)
> 
> diff --git a/tools/perf/builtin-c2c.c b/tools/perf/builtin-c2c.c
> index 12f272811487..5530433eda80 100644
> --- a/tools/perf/builtin-c2c.c
> +++ b/tools/perf/builtin-c2c.c
> @@ -146,15 +146,15 @@ static void *c2c_he_zalloc(size_t size)
>  
>  	c2c_he->cpuset = bitmap_zalloc(c2c.cpus_cnt);
>  	if (!c2c_he->cpuset)
> -		return NULL;
> +		goto out_free_he;
>  
>  	c2c_he->nodeset = bitmap_zalloc(c2c.nodes_cnt);
>  	if (!c2c_he->nodeset)
> -		return NULL;
> +		goto out_free_cpuset;
>  
>  	c2c_he->node_stats = zalloc(c2c.nodes_cnt * sizeof(*c2c_he->node_stats));
>  	if (!c2c_he->node_stats)
> -		return NULL;
> +		goto out_free_nodeset;
>  
>  	init_stats(&c2c_he->cstats.lcl_hitm);
>  	init_stats(&c2c_he->cstats.rmt_hitm);
> @@ -163,6 +163,14 @@ static void *c2c_he_zalloc(size_t size)
>  	init_stats(&c2c_he->cstats.load);
>  
>  	return &c2c_he->he;

nit, given that c2c_he is zero allocated we could just have
single error label that would free everything

for the patchset:

Acked-by: Jiri Olsa <jolsa@kernel.org>

thanks,
jirka

> +
> +out_free_nodeset:
> +	free(c2c_he->nodeset);
> +out_free_cpuset:
> +	free(c2c_he->cpuset);
> +out_free_he:
> +	free(c2c_he);
> +	return NULL;
>  }
>  
>  static void c2c_he_free(void *he)
> -- 
> 2.17.1
> 

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

* Re: [PATCH 3/3] perf c2c: Prevent potential memory leak in c2c_he_zalloc
  2022-09-05 10:19   ` Jiri Olsa
@ 2022-09-05 11:22     ` shangxiaojing
  0 siblings, 0 replies; 6+ messages in thread
From: shangxiaojing @ 2022-09-05 11:22 UTC (permalink / raw)
  To: Jiri Olsa
  Cc: peterz, mingo, acme, mark.rutland, alexander.shishkin, namhyung,
	linux-perf-users, linux-kernel


On 2022/9/5 18:19, Jiri Olsa wrote:
> On Sat, Sep 03, 2022 at 03:25:42PM +0800, Shang XiaoJing wrote:
>> Free allocated resources when zalloc is failed for members in c2c_he, to
>> prevent potential memory leak in c2c_he_zalloc.
>>
>> Signed-off-by: Shang XiaoJing <shangxiaojing@huawei.com>
>> ---
>>   tools/perf/builtin-c2c.c | 14 +++++++++++---
>>   1 file changed, 11 insertions(+), 3 deletions(-)
>>
>> diff --git a/tools/perf/builtin-c2c.c b/tools/perf/builtin-c2c.c
>> index 12f272811487..5530433eda80 100644
>> --- a/tools/perf/builtin-c2c.c
>> +++ b/tools/perf/builtin-c2c.c
>> @@ -146,15 +146,15 @@ static void *c2c_he_zalloc(size_t size)
>>   
>>   	c2c_he->cpuset = bitmap_zalloc(c2c.cpus_cnt);
>>   	if (!c2c_he->cpuset)
>> -		return NULL;
>> +		goto out_free_he;
>>   
>>   	c2c_he->nodeset = bitmap_zalloc(c2c.nodes_cnt);
>>   	if (!c2c_he->nodeset)
>> -		return NULL;
>> +		goto out_free_cpuset;
>>   
>>   	c2c_he->node_stats = zalloc(c2c.nodes_cnt * sizeof(*c2c_he->node_stats));
>>   	if (!c2c_he->node_stats)
>> -		return NULL;
>> +		goto out_free_nodeset;
>>   
>>   	init_stats(&c2c_he->cstats.lcl_hitm);
>>   	init_stats(&c2c_he->cstats.rmt_hitm);
>> @@ -163,6 +163,14 @@ static void *c2c_he_zalloc(size_t size)
>>   	init_stats(&c2c_he->cstats.load);
>>   
>>   	return &c2c_he->he;
> nit, given that c2c_he is zero allocated we could just have
> single error label that would free everything
>
> for the patchset:
>
> Acked-by: Jiri Olsa <jolsa@kernel.org>
>
> thanks,
> jirka

right, i'll be mindful of that in future.

this will be done in v2.

>> +
>> +out_free_nodeset:
>> +	free(c2c_he->nodeset);
>> +out_free_cpuset:
>> +	free(c2c_he->cpuset);
>> +out_free_he:
>> +	free(c2c_he);
>> +	return NULL;
>>   }
>>   
>>   static void c2c_he_free(void *he)
>> -- 
>> 2.17.1

Thanks,

Shang XiaoJing


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

end of thread, other threads:[~2022-09-05 11:22 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-09-03  7:25 [PATCH 0/3] perf: Clean up and fix potential mem leak Shang XiaoJing
2022-09-03  7:25 ` [PATCH 1/3] perf clean: Add same_cmd_with_prefix helper Shang XiaoJing
2022-09-03  7:25 ` [PATCH 2/3] perf c2c: Add helpers to get counts of loads or stores Shang XiaoJing
2022-09-03  7:25 ` [PATCH 3/3] perf c2c: Prevent potential memory leak in c2c_he_zalloc Shang XiaoJing
2022-09-05 10:19   ` Jiri Olsa
2022-09-05 11:22     ` shangxiaojing

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).