perf header: Fix lock/unlock imbalances
PATCH 1/1 perf header: Fix lock/unlock imbalances
Fix lock/unlock imbalances by refactoring the code a bit and adding calls to up_write() before return.
Addresses-Coverity-ID: 1444315 ("Missing unlock")
Addresses-Coverity-ID: 1444316 ("Missing unlock")
--- tools/perf/util/header.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-)
tools/perf/util/header.c +13 -8
@@ -2606,6 +2606,7 @@ static int process_bpf_prog_info(struct feat_fd *ff, void *data __maybe_unused)perf_env__insert_bpf_prog_info (env, info_node); } +up_write (&env->bpf_progs.lock);return 0 ; out:free (info_linear);
@@ -2623,7 +2624,9 @@ static int process_bpf_prog_info(struct feat_fd *ff __maybe_unused, void *data _static int process_bpf_btf (struct feat_fd *ff,void *data__maybe_unused ) {struct perf_env *env = &ff->ph->env; +struct btf_node *node;u32 count, i; +int err =-1 ;if (ff->ph->needs_swap) {pr_warning ("interpreting btf from systems with endianity is not yet supported\n" );
@@ -2636,31 +2639,33 @@ static int process_bpf_btf(struct feat_fd *ff, void *data __maybe_unused)down_write (&env->bpf_progs.lock);for (i =0 ; i < count; ++i) { -struct btf_node *node;u32 id, data_size; + node =NULL ;if (do_read_u32 (ff, &id)) -return -1 ; + goto out;if (do_read_u32 (ff, &data_size)) -return -1 ; + goto out; node =malloc (sizeof (struct btf_node ) + data_size);if (!node) -return -1 ; + goto out; node->id = id; node->data_size = data_size; -if (__do_read (ff, node->data, data_size)) { -free (node); -return -1 ; - } +if (__do_read (ff, node->data, data_size)) + goto out;perf_env__insert_btf (env, node); }up_write (&env->bpf_progs.lock);return 0 ; +out: +up_write (&env->bpf_progs.lock); +free (node); +return err; }struct feature_ops {
Signature · 1 line
2.21.0
Quoted 12 lines · Gustavo A. R. Silva
Fix lock/unlock imbalances by refactoring the code a bit and adding calls to
up_write()before return.Addresses-Coverity-ID: 1444315 ("Missing unlock")
Addresses-Coverity-ID: 1444316 ("Missing unlock")Fixes a70a1123174a ("perf bpf: Save BTF information as headers to perf.data")Fixes 606f972b1361 ("perf bpf: Save bpf_prog_info information as headers to perf.data")Signed-off-by Gustavo A. R. Silva <[email protected]>
Thanks for the fix!
Quoted 71 lines
---
tools/perf/util/header.c| 21 +++++++++++++--------1 file changed, 13 insertions(+), 8 deletions(-)tools/perf/util/header.c +1
-0@@ -2606,6 +2606,7 @@ static int process_bpf_prog_info(struct feat_fd *ff, void *data __maybe_unused)perf_env__insert_bpf_prog_info (env, info_node); } +up_write (&env->bpf_progs.lock);return 0 ;out:
free(info_linear);tools/perf/util/header.c +12
-8@@ -2623,7 +2624,9 @@ static int process_bpf_prog_info(struct feat_fd *ff __maybe_unused, void *data _static int process_bpf_btf (struct feat_fd *ff,void *data__maybe_unused ) {struct perf_env *env = &ff->ph->env; +struct btf_node *node;u32 count, i; +int err =-1 ;if (ff->ph->needs_swap) {pr_warning ("interpreting btf from systems with endianity is not yet supported\n" );@@ -2636,31 +2639,33 @@ static int process_bpf_btf(struct feat_fd *ff, void *data __maybe_unused)down_write (&env->bpf_progs.lock);for (i =0 ; i < count; ++i) { -struct btf_node *node;u32 id, data_size; + node =NULL ;if (do_read_u32 (ff, &id)) -return -1 ; + goto out;if (do_read_u32 (ff, &data_size)) -return -1 ; + goto out; node =malloc (sizeof (struct btf_node ) + data_size);if (!node) -return -1 ; + goto out; node->id = id; node->data_size = data_size; -if (__do_read (ff, node->data, data_size)) { -free (node); -return -1 ; - } +if (__do_read (ff, node->data, data_size)) + goto out;perf_env__insert_btf (env, node); }up_write (&env->bpf_progs.lock);return 0 ; +out: +up_write (&env->bpf_progs.lock); +free (node); +return err; }struct feature_ops {Signature · 1 line
2.21.0
Quoted 13 lines · Song Liu
Quoted 12 lines · Gustavo A. R. Silva
Fix lock/unlock imbalances by refactoring the code a bit and adding calls to
up_write()before return.Addresses-Coverity-ID: 1444315 ("Missing unlock")
Addresses-Coverity-ID: 1444316 ("Missing unlock")Fixes a70a1123174a ("perf bpf: Save BTF information as headers to perf.data")Fixes 606f972b1361 ("perf bpf: Save bpf_prog_info information as headers to perf.data")Signed-off-by Gustavo A. R. Silva <[email protected]>
Quoted 4 lines
Acked-by Song Liu <[email protected]>Thanks for the fix!
Glad to help. :)
Thanks
--
Gustavo
Quoted 72 lines
Quoted 71 lines
---
tools/perf/util/header.c| 21 +++++++++++++--------1 file changed, 13 insertions(+), 8 deletions(-)tools/perf/util/header.c +1
-0@@ -2606,6 +2606,7 @@ static int process_bpf_prog_info(struct feat_fd *ff, void *data __maybe_unused)perf_env__insert_bpf_prog_info (env, info_node); } +up_write (&env->bpf_progs.lock);return 0 ;out:
free(info_linear);tools/perf/util/header.c +12
-8@@ -2623,7 +2624,9 @@ static int process_bpf_prog_info(struct feat_fd *ff __maybe_unused, void *data _static int process_bpf_btf (struct feat_fd *ff,void *data__maybe_unused ) {struct perf_env *env = &ff->ph->env; +struct btf_node *node;u32 count, i; +int err =-1 ;if (ff->ph->needs_swap) {pr_warning ("interpreting btf from systems with endianity is not yet supported\n" );@@ -2636,31 +2639,33 @@ static int process_bpf_btf(struct feat_fd *ff, void *data __maybe_unused)down_write (&env->bpf_progs.lock);for (i =0 ; i < count; ++i) { -struct btf_node *node;u32 id, data_size; + node =NULL ;if (do_read_u32 (ff, &id)) -return -1 ; + goto out;if (do_read_u32 (ff, &data_size)) -return -1 ; + goto out; node =malloc (sizeof (struct btf_node ) + data_size);if (!node) -return -1 ; + goto out; node->id = id; node->data_size = data_size; -if (__do_read (ff, node->data, data_size)) { -free (node); -return -1 ; - } +if (__do_read (ff, node->data, data_size)) + goto out;perf_env__insert_btf (env, node); }up_write (&env->bpf_progs.lock);return 0 ; +out: +up_write (&env->bpf_progs.lock); +free (node); +return err; }struct feature_ops {Signature · 1 line
2.21.0
Em Mon, Apr 08, 2019 at 01:26:09PM -0500, Gustavo A. R. Silva escreveu:
Quoted 16 lines · Song Liu
Quoted 13 lines
Quoted 12 lines · Gustavo A. R. Silva
Fix lock/unlock imbalances by refactoring the code a bit and adding calls to
up_write()before return.Addresses-Coverity-ID: 1444315 ("Missing unlock")
Addresses-Coverity-ID: 1444316 ("Missing unlock")Fixes a70a1123174a ("perf bpf: Save BTF information as headers to perf.data")Fixes 606f972b1361 ("perf bpf: Save bpf_prog_info information as headers to perf.data")Signed-off-by Gustavo A. R. Silva <[email protected]>
Quoted 6 lines
Quoted 4 lines
Acked-by Song Liu <[email protected]>Thanks for the fix!
Glad to help. :)
Super cool, using the same idiom as the kernel and living in the kernel sources has its advantages 8-)
But see below,
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
+++
b/tools/perf/util/header.c(quoted diff) +0
-0@@ -2606,6 +2606,7 @@ static int process_bpf_prog_info(struct feat_fd *ff, void *data __maybe_unused) perf_env__insert_bpf_prog_info(env, info_node); }
Quoted 11 lines
Quoted 11 lines
Review static int process_bpf_prog_info(struct feat_fd *ff __maybe_unused, void *data _ · line 2629
Quoted 11 lines
(quoted diff) +1
-0+ up_write(&env->bpf_progs.lock); return 0;out:
free(info_linear);(quoted diff) +2
-0@@ -2623,7 +2624,9 @@ static int process_bpf_prog_info(struct feat_fd *ff __maybe_unused, void *data _ static int process_bpf_btf(struct feat_fd *ff, void *data __maybe_unused) { struct perf_env *env = &ff->ph->env; + struct btf_node *node; u32 count, i; + int err = -1;
Why are you using this 'err' variable? It is only set here and at the end, i.e. one write, one read. We could as well have that out: block return -1 straight away.
Else we could do, see below
Quoted 33 lines
Quoted 33 lines
Quoted 33 lines
(quoted diff) +6
-8if (ff->ph->needs_swap) { pr_warning("interpreting btf from systems with endianity is not yet supported\n");@@ -2636,31 +2639,33 @@ static int process_bpf_btf(struct feat_fd *ff, void *data __maybe_unused) down_write(&env->bpf_progs.lock); for (i = 0; i < count; ++i) { - struct btf_node *node; u32 id, data_size; + node = NULL; if (do_read_u32(ff, &id)) - return -1; + goto out; if (do_read_u32(ff, &data_size)) - return -1; + goto out; node = malloc(sizeof(struct btf_node) + data_size); if (!node) - return -1; + goto out; node->id = id; node->data_size = data_size; - if (__do_read(ff, node->data, data_size)) { - free(node); - return -1; - } + if (__do_read(ff, node->data, data_size)) + goto out;
Quoted 2 lines
Quoted 2 lines
Review static int process_bpf_btf(struct feat_fd *ff, void *data __maybe_unused) · line 2661
Quoted 2 lines
(quoted diff) +0
-0perf_env__insert_btf(env, node); }
err = 0;
Quoted 1 line
Quoted 1 line
Quoted 1 line
out:
Quoted 1 line
Quoted 1 line
Quoted 1 line
up_write(&env->bpf_progs.lock);
return err;
And delete the rest.
but I see, you used the same pattern in the first #ifdef HAVE_LIBBPF_SUPPORT block :-)
Anyway, since we're fixing up that other case, we might as well streamline this, please check the patch below.
Quoted 6 lines
Quoted 6 lines
Review
Quoted 6 lines
return 0;(quoted diff) +4
-0+out: + up_write(&env->bpf_progs.lock); + free(node); + return err;
So, that is what I'm applying, please holler if I introduced some problem:
tools/perf/util/header.c +13 -9
@@ -2606,6 +2606,7 @@ static int process_bpf_prog_info(struct feat_fd *ff, void *data __maybe_unused)perf_env__insert_bpf_prog_info (env, info_node); } +up_write (&env->bpf_progs.lock);return 0 ; out:free (info_linear);
@@ -2623,7 +2624,9 @@ static int process_bpf_prog_info(struct feat_fd *ff __maybe_unused, void *data _static int process_bpf_btf (struct feat_fd *ff,void *data__maybe_unused ) {struct perf_env *env = &ff->ph->env; +struct btf_node *node =NULL ;u32 count, i; +int err =-1 ;if (ff->ph->needs_swap) {pr_warning ("interpreting btf from systems with endianity is not yet supported\n" );
@@ -2636,31 +2639,32 @@ static int process_bpf_btf(struct feat_fd *ff, void *data __maybe_unused)down_write (&env->bpf_progs.lock);for (i =0 ; i < count; ++i) { -struct btf_node *node;u32 id, data_size;if (do_read_u32 (ff, &id)) -return -1 ; + goto out;if (do_read_u32 (ff, &data_size)) -return -1 ; + goto out; node =malloc (sizeof (struct btf_node ) + data_size);if (!node) -return -1 ; + goto out; node->id = id; node->data_size = data_size; -if (__do_read (ff, node->data, data_size)) { -free (node); -return -1 ; - } +if (__do_read (ff, node->data, data_size)) + goto out;perf_env__insert_btf (env, node); + node =NULL ; } + err =0 ; +out:up_write (&env->bpf_progs.lock); -return 0 ; +free (node); +return err; }struct feature_ops {
Quoted 19 lines · Arnaldo Carvalho de Melo
Em Mon, Apr 08, 2019 at 01:26:09PM -0500, Gustavo A. R. Silva escreveu:
Quoted 18 lines · Song Liu
Quoted 15 lines
Quoted 12 lines · Gustavo A. R. Silva
Fix lock/unlock imbalances by refactoring the code a bit and adding calls to
up_write()before return.Addresses-Coverity-ID: 1444315 ("Missing unlock")
Addresses-Coverity-ID: 1444316 ("Missing unlock")Fixes a70a1123174a ("perf bpf: Save BTF information as headers to perf.data")Fixes 606f972b1361 ("perf bpf: Save bpf_prog_info information as headers to perf.data")Signed-off-by Gustavo A. R. Silva <[email protected]>Acked-by Song Liu <[email protected]>
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
Thanks for the fix!
Glad to help. :)
Super cool, using the same idiom as the kernel and living in the kernel sources has its advantages 8-)
:P
Quoted 71 lines
But see below,
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
+++
b/tools/perf/util/header.ctools/perf/util/header.c +0
-0@@ -2606,6 +2606,7 @@ static int process_bpf_prog_info(struct feat_fd *ff, void *data __maybe_unused)perf_env__insert_bpf_prog_info (env, info_node); }Quoted 11 lines
Quoted 11 lines
tools/perf/util/header.c static int process_bpf_prog_info(struct feat_fd *ff __maybe_unused, void *data _ · line 2629
Quoted 11 lines
tools/perf/util/header.c +1
-0+up_write (&env->bpf_progs.lock);return 0 ;out:
free(info_linear);tools/perf/util/header.c +2
-0@@ -2623,7 +2624,9 @@ static int process_bpf_prog_info(struct feat_fd *ff __maybe_unused, void *data _static int process_bpf_btf (struct feat_fd *ff,void *data__maybe_unused ) {struct perf_env *env = &ff->ph->env; +struct btf_node *node;u32 count, i; +int err =-1 ;Why are you using this 'err' variable? It is only set here and at the end, i.e. one write, one read. We could as well have that out: block return -1 straight away.
Else we could do, see below
Quoted 33 lines
Quoted 33 lines
Quoted 33 lines
tools/perf/util/header.c +6
-8if (ff->ph->needs_swap) {pr_warning ("interpreting btf from systems with endianity is not yet supported\n" );@@ -2636,31 +2639,33 @@ static int process_bpf_btf(struct feat_fd *ff, void *data __maybe_unused)down_write (&env->bpf_progs.lock);for (i =0 ; i < count; ++i) { -struct btf_node *node;u32 id, data_size; + node =NULL ;if (do_read_u32 (ff, &id)) -return -1 ; + goto out;if (do_read_u32 (ff, &data_size)) -return -1 ; + goto out; node =malloc (sizeof (struct btf_node ) + data_size);if (!node) -return -1 ; + goto out; node->id = id; node->data_size = data_size; -if (__do_read (ff, node->data, data_size)) { -free (node); -return -1 ; - } +if (__do_read (ff, node->data, data_size)) + goto out;Quoted 2 lines
Quoted 2 lines
tools/perf/util/header.c static int process_bpf_btf(struct feat_fd *ff, void *data __maybe_unused) · line 2661
Quoted 2 lines
tools/perf/util/header.c +0
-0perf_env__insert_btf (env, node); }err = 0;Quoted 1 line
Quoted 1 line
Quoted 1 line
out:
Quoted 1 line
Quoted 1 line
Quoted 1 line
up_write(&env->bpf_progs.lock);return err;
Quoted 8 lines
And delete the rest.
but I see, you used the same pattern in the first #ifdef
HAVE_LIBBPF_SUPPORTblock :-)Anyway, since we're fixing up that other case, we might as well streamline this, please check the patch below.
Yeah. This is exactly how I would have coded this from the beginning. But, as you correctly pointed out, I'm using the same pattern as in HAVE_LIBBPF_SUPPORT. :)
Just a comment below...
Quoted 53 lines
Quoted 6 lines
Quoted 6 lines
tools/perf/util/header.c static int process_bpf_btf(struct feat_fd *ff, void *data __maybe_unused) · line 2667
Quoted 6 lines
return 0;tools/perf/util/header.c +4
-0+out: +up_write (&env->bpf_progs.lock); +free (node); +return err;So, that is what I'm applying, please holler if I introduced some problem:
tools/perf/util/header.c +6
-4@@ -2606,6 +2606,7 @@ static int process_bpf_prog_info(struct feat_fd *ff, void *data __maybe_unused)perf_env__insert_bpf_prog_info (env, info_node); } +up_write (&env->bpf_progs.lock);return 0 ; out:free (info_linear);@@ -2623,7 +2624,9 @@ static int process_bpf_prog_info(struct feat_fd *ff __maybe_unused, void *data _static int process_bpf_btf (struct feat_fd *ff,void *data__maybe_unused ) {struct perf_env *env = &ff->ph->env; +struct btf_node *node =NULL ;u32 count, i; +int err =-1 ;if (ff->ph->needs_swap) {pr_warning ("interpreting btf from systems with endianity is not yet supported\n" );@@ -2636,31 +2639,32 @@ static int process_bpf_btf(struct feat_fd *ff, void *data __maybe_unused)down_write (&env->bpf_progs.lock);for (i =0 ; i < count; ++i) { -struct btf_node *node;u32 id, data_size;if (do_read_u32 (ff, &id)) -return -1 ; + goto out;if (do_read_u32 (ff, &data_size)) -return -1 ; + goto out; node =malloc (sizeof (struct btf_node ) + data_size);if (!node) -return -1 ; + goto out; node->id = id; node->data_size = data_size;
tools/perf/util/header.c static int process_bpf_btf(struct feat_fd *ff, void *data __maybe_unused) · line 2660
Quoted 10 lines
tools/perf/util/header.c +3
-4-if (__do_read (ff, node->data, data_size)) { -free (node); -return -1 ; - } +if (__do_read (ff, node->data, data_size)) + goto out;perf_env__insert_btf (env, node); + node =NULL ;
If we move this assignment to the beginning of the for loop, as in the original patch, we avoid the same assignment while declaring node at the beginning of the function.
For the rest, the code looks good to me.
Thanks for your comments. :)
--
Gustavo
Quoted 12 lines
tools/perf/util/header.c +4
-1} + err =0 ; +out:up_write (&env->bpf_progs.lock); -return 0 ; +free (node); +return err; }struct feature_ops {
Em Mon, Apr 08, 2019 at 02:52:52PM -0500, Gustavo A. R. Silva escreveu:
Quoted 179 lines · Arnaldo Carvalho de Melo
Quoted 19 lines
Em Mon, Apr 08, 2019 at 01:26:09PM -0500, Gustavo A. R. Silva escreveu:
Quoted 18 lines · Song Liu
Quoted 15 lines
Quoted 12 lines · Gustavo A. R. Silva
Fix lock/unlock imbalances by refactoring the code a bit and adding calls to
up_write()before return.Addresses-Coverity-ID: 1444315 ("Missing unlock")
Addresses-Coverity-ID: 1444316 ("Missing unlock")Fixes a70a1123174a ("perf bpf: Save BTF information as headers to perf.data")Fixes 606f972b1361 ("perf bpf: Save bpf_prog_info information as headers to perf.data")Signed-off-by Gustavo A. R. Silva <[email protected]>Acked-by Song Liu <[email protected]>Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
Thanks for the fix!
Glad to help. :)
Super cool, using the same idiom as the kernel and living in the kernel sources has its advantages 8-)
:P
Quoted 71 lines
But see below,
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
+++
b/tools/perf/util/header.ctools/perf/util/header.c +0
-0@@ -2606,6 +2606,7 @@ static int process_bpf_prog_info(struct feat_fd *ff, void *data __maybe_unused)perf_env__insert_bpf_prog_info (env, info_node); }Quoted 11 lines
Quoted 11 lines
tools/perf/util/header.c static int process_bpf_prog_info(struct feat_fd *ff __maybe_unused, void *data _ · line 2629
Quoted 11 lines
tools/perf/util/header.c +1
-0+up_write (&env->bpf_progs.lock);return 0 ;out:
free(info_linear);tools/perf/util/header.c +2
-0@@ -2623,7 +2624,9 @@ static int process_bpf_prog_info(struct feat_fd *ff __maybe_unused, void *data _static int process_bpf_btf (struct feat_fd *ff,void *data__maybe_unused ) {struct perf_env *env = &ff->ph->env; +struct btf_node *node;u32 count, i; +int err =-1 ;Why are you using this 'err' variable? It is only set here and at the end, i.e. one write, one read. We could as well have that out: block return -1 straight away.
Else we could do, see below
Quoted 33 lines
Quoted 33 lines
Quoted 33 lines
tools/perf/util/header.c +6
-8if (ff->ph->needs_swap) {pr_warning ("interpreting btf from systems with endianity is not yet supported\n" );@@ -2636,31 +2639,33 @@ static int process_bpf_btf(struct feat_fd *ff, void *data __maybe_unused)down_write (&env->bpf_progs.lock);for (i =0 ; i < count; ++i) { -struct btf_node *node;u32 id, data_size; + node =NULL ;if (do_read_u32 (ff, &id)) -return -1 ; + goto out;if (do_read_u32 (ff, &data_size)) -return -1 ; + goto out; node =malloc (sizeof (struct btf_node ) + data_size);if (!node) -return -1 ; + goto out; node->id = id; node->data_size = data_size; -if (__do_read (ff, node->data, data_size)) { -free (node); -return -1 ; - } +if (__do_read (ff, node->data, data_size)) + goto out;Quoted 2 lines
Quoted 2 lines
tools/perf/util/header.c static int process_bpf_btf(struct feat_fd *ff, void *data __maybe_unused) · line 2661
Quoted 2 lines
tools/perf/util/header.c +0
-0perf_env__insert_btf (env, node); }err = 0;Quoted 1 line
Quoted 1 line
Quoted 1 line
out:
Quoted 1 line
Quoted 1 line
Quoted 1 line
up_write(&env->bpf_progs.lock);return err;Quoted 8 lines
And delete the rest.
but I see, you used the same pattern in the first #ifdef
HAVE_LIBBPF_SUPPORTblock :-)Anyway, since we're fixing up that other case, we might as well streamline this, please check the patch below.
Yeah. This is exactly how I would have coded this from the beginning. But, as you correctly pointed out, I'm using the same pattern as in
HAVE_LIBBPF_SUPPORT. :)Just a comment below...
Quoted 61 lines
Quoted 6 lines
Quoted 6 lines
tools/perf/util/header.c static int process_bpf_btf(struct feat_fd *ff, void *data __maybe_unused) · line 2667
Quoted 6 lines
return 0;tools/perf/util/header.c +4
-0+out: +up_write (&env->bpf_progs.lock); +free (node); +return err;So, that is what I'm applying, please holler if I introduced some problem:
tools/perf/util/header.c +8
-8@@ -2606,6 +2606,7 @@ static int process_bpf_prog_info(struct feat_fd *ff, void *data __maybe_unused)perf_env__insert_bpf_prog_info (env, info_node); } +up_write (&env->bpf_progs.lock);return 0 ; out:free (info_linear);@@ -2623,7 +2624,9 @@ static int process_bpf_prog_info(struct feat_fd *ff __maybe_unused, void *data _static int process_bpf_btf (struct feat_fd *ff,void *data__maybe_unused ) {struct perf_env *env = &ff->ph->env; +struct btf_node *node =NULL ;u32 count, i; +int err =-1 ;if (ff->ph->needs_swap) {pr_warning ("interpreting btf from systems with endianity is not yet supported\n" );@@ -2636,31 +2639,32 @@ static int process_bpf_btf(struct feat_fd *ff, void *data __maybe_unused)down_write (&env->bpf_progs.lock);for (i =0 ; i < count; ++i) { -struct btf_node *node;u32 id, data_size;if (do_read_u32 (ff, &id)) -return -1 ; + goto out;if (do_read_u32 (ff, &data_size)) -return -1 ; + goto out; node =malloc (sizeof (struct btf_node ) + data_size);if (!node) -return -1 ; + goto out; node->id = id; node->data_size = data_size; -if (__do_read (ff, node->data, data_size)) { -free (node); -return -1 ; - } +if (__do_read (ff, node->data, data_size)) + goto out;
Quoted 6 lines
tools/perf/util/header.c static int process_bpf_btf(struct feat_fd *ff, void *data __maybe_unused) · line 2660
Quoted 2 lines
tools/perf/util/header.c +1
-0perf_env__insert_btf (env, node); + node =NULL ;If we move this assignment to the beginning of the for loop, as in the original patch, we avoid the same assignment while declaring node at the beginning of the function.
No, we don't, since the common exit path frees node, we better not free the last node in the success case, that is why I moved it to the end, i.e. after we're done with it, nullify it, so that the last btf_node isn't freed in the now uncoditionall free(node); call :-)
Right?
- Arnaldo
Quoted 18 lines
For the rest, the code looks good to me.
Thanks for your comments. :)
--
GustavoQuoted 12 lines
tools/perf/util/header.c +4
-1} + err =0 ; +out:up_write (&env->bpf_progs.lock); -return 0 ; +free (node); +return err; }struct feature_ops {
Signature · 1 line
- Arnaldo
Quoted 187 lines · Arnaldo Carvalho de Melo
Em Mon, Apr 08, 2019 at 02:52:52PM -0500, Gustavo A. R. Silva escreveu:
Quoted 179 lines · Arnaldo Carvalho de Melo
Quoted 19 lines
Em Mon, Apr 08, 2019 at 01:26:09PM -0500, Gustavo A. R. Silva escreveu:
Quoted 18 lines · Song Liu
Quoted 15 lines
Quoted 12 lines · Gustavo A. R. Silva
Fix lock/unlock imbalances by refactoring the code a bit and adding calls to
up_write()before return.Addresses-Coverity-ID: 1444315 ("Missing unlock")
Addresses-Coverity-ID: 1444316 ("Missing unlock")Fixes a70a1123174a ("perf bpf: Save BTF information as headers to perf.data")Fixes 606f972b1361 ("perf bpf: Save bpf_prog_info information as headers to perf.data")Signed-off-by Gustavo A. R. Silva <[email protected]>Acked-by Song Liu <[email protected]>Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
Thanks for the fix!
Glad to help. :)
Super cool, using the same idiom as the kernel and living in the kernel sources has its advantages 8-)
:P
Quoted 71 lines
But see below,
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
+++
b/tools/perf/util/header.ctools/perf/util/header.c +0
-0@@ -2606,6 +2606,7 @@ static int process_bpf_prog_info(struct feat_fd *ff, void *data __maybe_unused)perf_env__insert_bpf_prog_info (env, info_node); }Quoted 11 lines
Quoted 11 lines
tools/perf/util/header.c static int process_bpf_prog_info(struct feat_fd *ff __maybe_unused, void *data _ · line 2629
Quoted 11 lines
tools/perf/util/header.c +1
-0+up_write (&env->bpf_progs.lock);return 0 ;out:
free(info_linear);tools/perf/util/header.c +2
-0@@ -2623,7 +2624,9 @@ static int process_bpf_prog_info(struct feat_fd *ff __maybe_unused, void *data _static int process_bpf_btf (struct feat_fd *ff,void *data__maybe_unused ) {struct perf_env *env = &ff->ph->env; +struct btf_node *node;u32 count, i; +int err =-1 ;Why are you using this 'err' variable? It is only set here and at the end, i.e. one write, one read. We could as well have that out: block return -1 straight away.
Else we could do, see below
Quoted 33 lines
Quoted 33 lines
Quoted 33 lines
tools/perf/util/header.c +6
-8if (ff->ph->needs_swap) {pr_warning ("interpreting btf from systems with endianity is not yet supported\n" );@@ -2636,31 +2639,33 @@ static int process_bpf_btf(struct feat_fd *ff, void *data __maybe_unused)down_write (&env->bpf_progs.lock);for (i =0 ; i < count; ++i) { -struct btf_node *node;u32 id, data_size; + node =NULL ;if (do_read_u32 (ff, &id)) -return -1 ; + goto out;if (do_read_u32 (ff, &data_size)) -return -1 ; + goto out; node =malloc (sizeof (struct btf_node ) + data_size);if (!node) -return -1 ; + goto out; node->id = id; node->data_size = data_size; -if (__do_read (ff, node->data, data_size)) { -free (node); -return -1 ; - } +if (__do_read (ff, node->data, data_size)) + goto out;Quoted 2 lines
Quoted 2 lines
tools/perf/util/header.c static int process_bpf_btf(struct feat_fd *ff, void *data __maybe_unused) · line 2661
Quoted 2 lines
tools/perf/util/header.c +0
-0perf_env__insert_btf (env, node); }err = 0;Quoted 1 line
Quoted 1 line
Quoted 1 line
out:
Quoted 1 line
Quoted 1 line
Quoted 1 line
up_write(&env->bpf_progs.lock);return err;Quoted 8 lines
And delete the rest.
but I see, you used the same pattern in the first #ifdef
HAVE_LIBBPF_SUPPORTblock :-)Anyway, since we're fixing up that other case, we might as well streamline this, please check the patch below.
Yeah. This is exactly how I would have coded this from the beginning. But, as you correctly pointed out, I'm using the same pattern as in
HAVE_LIBBPF_SUPPORT. :)Just a comment below...
Quoted 61 lines
Quoted 6 lines
Quoted 6 lines
tools/perf/util/header.c static int process_bpf_btf(struct feat_fd *ff, void *data __maybe_unused) · line 2667
Quoted 6 lines
return 0;tools/perf/util/header.c +4
-0+out: +up_write (&env->bpf_progs.lock); +free (node); +return err;So, that is what I'm applying, please holler if I introduced some problem:
tools/perf/util/header.c +8
-8@@ -2606,6 +2606,7 @@ static int process_bpf_prog_info(struct feat_fd *ff, void *data __maybe_unused)perf_env__insert_bpf_prog_info (env, info_node); } +up_write (&env->bpf_progs.lock);return 0 ; out:free (info_linear);@@ -2623,7 +2624,9 @@ static int process_bpf_prog_info(struct feat_fd *ff __maybe_unused, void *data _static int process_bpf_btf (struct feat_fd *ff,void *data__maybe_unused ) {struct perf_env *env = &ff->ph->env; +struct btf_node *node =NULL ;u32 count, i; +int err =-1 ;if (ff->ph->needs_swap) {pr_warning ("interpreting btf from systems with endianity is not yet supported\n" );@@ -2636,31 +2639,32 @@ static int process_bpf_btf(struct feat_fd *ff, void *data __maybe_unused)down_write (&env->bpf_progs.lock);for (i =0 ; i < count; ++i) { -struct btf_node *node;u32 id, data_size;if (do_read_u32 (ff, &id)) -return -1 ; + goto out;if (do_read_u32 (ff, &data_size)) -return -1 ; + goto out; node =malloc (sizeof (struct btf_node ) + data_size);if (!node) -return -1 ; + goto out; node->id = id; node->data_size = data_size; -if (__do_read (ff, node->data, data_size)) { -free (node); -return -1 ; - } +if (__do_read (ff, node->data, data_size)) + goto out;Quoted 6 lines
tools/perf/util/header.c static int process_bpf_btf(struct feat_fd *ff, void *data __maybe_unused) · line 2660
Quoted 2 lines
tools/perf/util/header.c +1
-0perf_env__insert_btf (env, node); + node =NULL ;If we move this assignment to the beginning of the for loop, as in the original patch, we avoid the same assignment while declaring node at the beginning of the function.
Quoted 5 lines
No, we don't, since the common exit path frees node, we better not free the last node in the success case, that is why I moved it to the end, i.e. after we're done with it, nullify it, so that the last
btf_nodeisn't freed in the now uncoditionall free(node); call :-)
Yep. You're right.
So, everything is fine now. :)
Thanks
--
Gustavo
tip-bot for Gustavo A. R. Silva
merge bot[tip:perf/urgent] perf header: Fix lock/unlock imbalances when processing BPF/BTF info
In reply to Gustavo A. R. Silvaperf header: Fix lock/unlock imbalances when processing BPF/BTF info
Commit-ID: f0f702655b09cfe07bcbc1027be67c3fd2a7387b Gitweb: https://git.kernel.org/tip/f0f702655b09cfe07bcbc1027be67c3fd2a7387b Author: Gustavo A. R. Silva <[email protected]> AuthorDate: Mon, 8 Apr 2019 12:33:55 -0500 Committer: Arnaldo Carvalho de Melo <[email protected]> CommitDate: Mon, 8 Apr 2019 16:58:07 -0300
perf header: Fix lock/unlock imbalances when processing BPF/BTF info
Fix lock/unlock imbalances by refactoring the code a bit and adding calls to up_write() before return.
Addresses-Coverity-ID: 1444315 ("Missing unlock")
Addresses-Coverity-ID: 1444316 ("Missing unlock")
--- tools/perf/util/header.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-)
tools/perf/util/header.c +13 -9
@@ -2606,6 +2606,7 @@ static int process_bpf_prog_info(struct feat_fd *ff, void *data __maybe_unused)perf_env__insert_bpf_prog_info (env, info_node); } +up_write (&env->bpf_progs.lock);return 0 ; out:free (info_linear);
@@ -2623,7 +2624,9 @@ static int process_bpf_prog_info(struct feat_fd *ff __maybe_unused, void *data _static int process_bpf_btf (struct feat_fd *ff,void *data__maybe_unused ) {struct perf_env *env = &ff->ph->env; +struct btf_node *node =NULL ;u32 count, i; +int err =-1 ;if (ff->ph->needs_swap) {pr_warning ("interpreting btf from systems with endianity is not yet supported\n" );
@@ -2636,31 +2639,32 @@ static int process_bpf_btf(struct feat_fd *ff, void *data __maybe_unused)down_write (&env->bpf_progs.lock);for (i =0 ; i < count; ++i) { -struct btf_node *node;u32 id, data_size;if (do_read_u32 (ff, &id)) -return -1 ; + goto out;if (do_read_u32 (ff, &data_size)) -return -1 ; + goto out; node =malloc (sizeof (struct btf_node ) + data_size);if (!node) -return -1 ; + goto out; node->id = id; node->data_size = data_size; -if (__do_read (ff, node->data, data_size)) { -free (node); -return -1 ; - } +if (__do_read (ff, node->data, data_size)) + goto out;perf_env__insert_btf (env, node); + node =NULL ; } + err =0 ; +out:up_write (&env->bpf_progs.lock); -return 0 ; +free (node); +return err; }struct feature_ops {
tip-bot for Gustavo A. R. Silva
merge bot[tip:perf/urgent] perf header: Fix lock/unlock imbalances when processing BPF/BTF info
In reply to Gustavo A. R. Silvaperf header: Fix lock/unlock imbalances when processing BPF/BTF info
Commit-ID: 14c9b31a925a9f5c647523a12e2b575b97c0aa47 Gitweb: https://git.kernel.org/tip/14c9b31a925a9f5c647523a12e2b575b97c0aa47 Author: Gustavo A. R. Silva <[email protected]> AuthorDate: Mon, 8 Apr 2019 12:33:55 -0500 Committer: Arnaldo Carvalho de Melo <[email protected]> CommitDate: Tue, 16 Apr 2019 11:26:43 -0300
perf header: Fix lock/unlock imbalances when processing BPF/BTF info
Fix lock/unlock imbalances by refactoring the code a bit and adding calls to up_write() before return.
Addresses-Coverity-ID: 1444315 ("Missing unlock")
Addresses-Coverity-ID: 1444316 ("Missing unlock")
--- tools/perf/util/header.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-)
tools/perf/util/header.c +13 -9
@@ -2606,6 +2606,7 @@ static int process_bpf_prog_info(struct feat_fd *ff, void *data __maybe_unused)perf_env__insert_bpf_prog_info (env, info_node); } +up_write (&env->bpf_progs.lock);return 0 ; out:free (info_linear);
@@ -2623,7 +2624,9 @@ static int process_bpf_prog_info(struct feat_fd *ff __maybe_unused, void *data _static int process_bpf_btf (struct feat_fd *ff,void *data__maybe_unused ) {struct perf_env *env = &ff->ph->env; +struct btf_node *node =NULL ;u32 count, i; +int err =-1 ;if (ff->ph->needs_swap) {pr_warning ("interpreting btf from systems with endianity is not yet supported\n" );
@@ -2636,31 +2639,32 @@ static int process_bpf_btf(struct feat_fd *ff, void *data __maybe_unused)down_write (&env->bpf_progs.lock);for (i =0 ; i < count; ++i) { -struct btf_node *node;u32 id, data_size;if (do_read_u32 (ff, &id)) -return -1 ; + goto out;if (do_read_u32 (ff, &data_size)) -return -1 ; + goto out; node =malloc (sizeof (struct btf_node ) + data_size);if (!node) -return -1 ; + goto out; node->id = id; node->data_size = data_size; -if (__do_read (ff, node->data, data_size)) { -free (node); -return -1 ; - } +if (__do_read (ff, node->data, data_size)) + goto out;perf_env__insert_btf (env, node); + node =NULL ; } + err =0 ; +out:up_write (&env->bpf_progs.lock); -return 0 ; +free (node); +return err; }struct feature_ops {