perf intel-pt: Fix clang build failure in intel_pt_synth_pebs_sample
PATCH 1/1 perf intel-pt: Fix clang build failure in intel_pt_synth_pebs_sample
Fix the following build failure generated with command
$ make CC=clang HOSTCC=clang -C tools/ perf:
util/intel-pt.c:1802:24: error: field 'br_stack' with variable sized type 'struct branch_stack' not at the end of a struct or class is a GNU extension [-Werror,-Wgnu-variable-sized-type-not-at-end]
struct branch_stack br_stack;
^1 error generated.
Fix this by reordering the members of struct br.
Clang version 11.0.0 was used.
---
Here to fix what I break. :)
tools/perf/util/intel-pt.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/intel-pt.c +1 -1
@@ -1799,8 +1799,8 @@ static int intel_pt_synth_pebs_sample(struct intel_pt_queue *ptq)if (sample_type &PERF_SAMPLE_BRANCH_STACK ) {struct { -struct branch_stack br_stack;struct branch_entry entries[LBRS_MAX ]; +struct branch_stack br_stack; } br; if (items->mask[INTEL_PT_LBR_0_POS] ||
Signature · 1 line
2.26.2
Quoted 9 lines · Gustavo A. R. Silva
Fix the following build failure generated with command
$ make CC=clang HOSTCC=clang -C tools/ perf:
util/intel-pt.c:1802:24: error: field 'br_stack' with variable sized type 'structbranch_stack' not at the end of a struct or class is a GNU extension [-Werror,-Wgnu-variable-sized-type-not-at-end]struct branch_stack br_stack; ^1 error generated.
Quoted 7 lines
Fix this by reordering the members of struct br.
Clang version 11.0.0 was used.
Fixes f283f293a60d ("perf tools: Replace zero-length array with flexible-array")Reported-by Ian Rogers <[email protected]>Signed-off-by Gustavo A. R. Silva <[email protected]>
Thanks!
Ian
Quoted 23 lines
---
Here to fix what I break. :)tools/perf/util/intel-pt.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/perf/util/intel-pt.c +1
-2@@ -1799,8 +1799,8 @@ static int intel_pt_synth_pebs_sample(struct intel_pt_queue *ptq)if (sample_type &PERF_SAMPLE_BRANCH_STACK ) {struct { -struct branch_stack br_stack;struct branch_entry entries[LBRS_MAX ]; +struct branch_stack br_stack; } br; if (items->mask[INTEL_PT_LBR_0_POS] || --2.26.2
Em Wed, May 13, 2020 at 06:47:38PM -0500, Gustavo A. R. Silva escreveu:
Quoted 9 lines
Fix the following build failure generated with command
$ make CC=clang HOSTCC=clang -C tools/ perf:
util/intel-pt.c:1802:24: error: field 'br_stack' with variable sized type 'structbranch_stack' not at the end of a struct or class is a GNU extension [-Werror,-Wgnu-variable-sized-type-not-at-end]struct branch_stack br_stack; ^1 error generated.
Fix this by reordering the members of struct br.
Yeah, I noticed that as far back as with ubuntu 16.04's clang:
clang version 3.8.0-2ubuntu4 (tags/RELEASE_380/final)
util/intel-pt.c:1802:24: error: field 'br_stack' with variable sized type 'struct branch_stack' not at the end of a struct or class is a GNU
extension [-Werror,-Wgnu-variable-sized-type-not-at-end]
struct branch_stack br_stack;
^1 error generated.
Will fold this with the bug introducing the problem to avoid bisection problems.
Quoted 28 lines
Clang version 11.0.0 was used.
Fixes f283f293a60d ("perf tools: Replace zero-length array with flexible-array")Reported-by Ian Rogers <[email protected]>Signed-off-by Gustavo A. R. Silva <[email protected]>---
Here to fix what I break. :)tools/perf/util/intel-pt.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/perf/util/intel-pt.c +1
-1@@ -1799,8 +1799,8 @@ static int intel_pt_synth_pebs_sample(struct intel_pt_queue *ptq)if (sample_type &PERF_SAMPLE_BRANCH_STACK ) {struct { -struct branch_stack br_stack;struct branch_entry entries[LBRS_MAX ]; +struct branch_stack br_stack; } br; if (items->mask[INTEL_PT_LBR_0_POS] ||Signature · 1 line
2.26.2
Signature · 1 line
- Arnaldo
Quoted 16 lines · Arnaldo Carvalho de Melo
Em Wed, May 13, 2020 at 06:47:38PM -0500, Gustavo A. R. Silva escreveu:
Quoted 9 lines
Fix the following build failure generated with command
$ make CC=clang HOSTCC=clang -C tools/ perf:
util/intel-pt.c:1802:24: error: field 'br_stack' with variable sized type 'structbranch_stack' not at the end of a struct or class is a GNU extension [-Werror,-Wgnu-variable-sized-type-not-at-end]struct branch_stack br_stack; ^1 error generated.
Fix this by reordering the members of struct br.
Yeah, I noticed that as far back as with ubuntu 16.04's clang:
clang version 3.8.0-2ubuntu4 (tags/RELEASE_380/final)
util/intel-pt.c:1802:24: error: field 'br_stack' with variable sized type 'structbranch_stack' not at the end of a struct or class is a GNU
Quoted 9 lines
extension [-Werror,-Wgnu-variable-sized-type-not-at-end] struct branch_stack br_stack; ^1 error generated.
Will fold this with the bug introducing the problem to avoid bisection problems.
I agree. Also, the commit hash of the "Fixes" tag only applies to the perf/core branch and, I guess that might create confusion.
Thanks
--
Gustavo
Ian Rogers
reviewerQuoted 24 lines · Gustavo A. R. Silva
Quoted 22 lines · Arnaldo Carvalho de Melo
Em Wed, May 13, 2020 at 06:47:38PM -0500, Gustavo A. R. Silva escreveu:
Quoted 9 lines
Fix the following build failure generated with command
$ make CC=clang HOSTCC=clang -C tools/ perf:
util/intel-pt.c:1802:24: error: field 'br_stack' with variable sized type 'structbranch_stack' not at the end of a struct or class is a GNU extension [-Werror,-Wgnu-variable-sized-type-not-at-end]struct branch_stack br_stack; ^1 error generated.
Fix this by reordering the members of struct br.
Yeah, I noticed that as far back as with ubuntu 16.04's clang:
clang version 3.8.0-2ubuntu4 (tags/RELEASE_380/final)
util/intel-pt.c:1802:24: error: field 'br_stack' with variable sized type 'structbranch_stack' not at the end of a struct or class is a GNUextension [-Werror,-Wgnu-variable-sized-type-not-at-end] struct branch_stack br_stack; ^1 error generated.
Quoted 6 lines
Quoted 3 lines
Will fold this with the bug introducing the problem to avoid bisection problems.
I agree. Also, the commit hash of the "Fixes" tag only applies to the perf/core branch and, I guess that might create confusion.
So while this fixes the warning I believe it breaks the intent of the code.
tools/perf/util/branch.h:
struct branch_stack {
u64 nr;
u64 hw_idx;
struct branch_entry entries[];
};tools/perf/util/intel-pt.c:
struct {
struct branch_stack br_stack;
struct branch_entry entries[LBRS_MAX];
} br;The array in br is trying to extend branch_stack's entries array. You might have to do something like:
alignas(alignof(branch_stack)) char storage[sizeof(branch_stack) + sizeof(branch_entry) * LBRS_MAX];
struct branch_stack *br = &storage;
malloc/free may be nicer on the eyeballs.
Thanks,
Ian
Quoted 3 lines
Thanks
--
Gustavo
Quoted 49 lines · Ian Rogers
Quoted 24 lines · Gustavo A. R. Silva
Quoted 22 lines · Arnaldo Carvalho de Melo
Em Wed, May 13, 2020 at 06:47:38PM -0500, Gustavo A. R. Silva escreveu:
Quoted 9 lines
Fix the following build failure generated with command
$ make CC=clang HOSTCC=clang -C tools/ perf:
util/intel-pt.c:1802:24: error: field 'br_stack' with variable sized type 'structbranch_stack' not at the end of a struct or class is a GNU extension [-Werror,-Wgnu-variable-sized-type-not-at-end]struct branch_stack br_stack; ^1 error generated.
Fix this by reordering the members of struct br.
Yeah, I noticed that as far back as with ubuntu 16.04's clang:
clang version 3.8.0-2ubuntu4 (tags/RELEASE_380/final)
util/intel-pt.c:1802:24: error: field 'br_stack' with variable sized type 'structbranch_stack' not at the end of a struct or class is a GNUextension [-Werror,-Wgnu-variable-sized-type-not-at-end] struct branch_stack br_stack; ^1 error generated.
Quoted 6 lines
Quoted 3 lines
Will fold this with the bug introducing the problem to avoid bisection problems.
I agree. Also, the commit hash of the "Fixes" tag only applies to the perf/core branch and, I guess that might create confusion.
So while this fixes the warning I believe it breaks the intent of the code.
tools/perf/util/branch.h:struct branch_stack { u64 nr; u64 hw_idx; struct branch_entry entries[]; };
tools/perf/util/intel-pt.c:struct { struct branch_stack br_stack; struct branch_entry entries[LBRS_MAX]; } br;
Quoted 9 lines
The array in br is trying to extend branch_stack's entries array. You might have to do something like:
alignas(alignof(branch_stack)) char storage[sizeof(branch_stack) +
sizeof(branch_entry)*LBRS_MAX];
structbranch_stack*br = &storage;malloc/free may be nicer on the eyeballs.
Yep, I'd go for zalloc/free. There are a couple of places where dynamic memory is being allocated for struct branch_stack:
tools/perf/util/cs-etm.c-256- if (etm->synth_opts.last_branch) {tools/perf/util/cs-etm.c:257: size_t sz = sizeof(struct branch_stack);tools/perf/util/cs-etm.c-258-tools/perf/util/cs-etm.c-259- sz += etm->synth_opts.last_branch_sz *tools/perf/util/cs-etm.c-260- sizeof(struct branch_entry);tools/perf/util/cs-etm.c-261- tidq->last_branch = zalloc(sz);
tools/perf/util/thread-stack.c-148- if (br_stack_sz) {tools/perf/util/thread-stack.c:149: size_t sz = sizeof(struct branch_stack);tools/perf/util/thread-stack.c-150-tools/perf/util/thread-stack.c-151- sz += br_stack_sz * sizeof(struct branch_entry);tools/perf/util/thread-stack.c-152- ts->br_stack_rb = zalloc(sz);
there is even function intel_pt_alloc_br_stack().
Just out of curiosity, why the need of such a hack in this case (the on-stack extension of branch_stack's entries array)?
Thanks
--
Gustavo
Ian Rogers
reviewerQuoted 77 lines · Gustavo A. R. Silva
Quoted 49 lines · Ian Rogers
Quoted 24 lines · Gustavo A. R. Silva
Quoted 22 lines · Arnaldo Carvalho de Melo
Em Wed, May 13, 2020 at 06:47:38PM -0500, Gustavo A. R. Silva escreveu:
Quoted 9 lines
Fix the following build failure generated with command
$ make CC=clang HOSTCC=clang -C tools/ perf:
util/intel-pt.c:1802:24: error: field 'br_stack' with variable sized type 'structbranch_stack' not at the end of a struct or class is a GNU extension [-Werror,-Wgnu-variable-sized-type-not-at-end]struct branch_stack br_stack; ^1 error generated.
Fix this by reordering the members of struct br.
Yeah, I noticed that as far back as with ubuntu 16.04's clang:
clang version 3.8.0-2ubuntu4 (tags/RELEASE_380/final)
util/intel-pt.c:1802:24: error: field 'br_stack' with variable sized type 'structbranch_stack' not at the end of a struct or class is a GNUextension [-Werror,-Wgnu-variable-sized-type-not-at-end] struct branch_stack br_stack; ^1 error generated.
Quoted 6 lines
Quoted 3 lines
Will fold this with the bug introducing the problem to avoid bisection problems.
I agree. Also, the commit hash of the "Fixes" tag only applies to the perf/core branch and, I guess that might create confusion.
So while this fixes the warning I believe it breaks the intent of the code.
tools/perf/util/branch.h:struct branch_stack { u64 nr; u64 hw_idx; struct branch_entry entries[]; };
tools/perf/util/intel-pt.c:struct { struct branch_stack br_stack; struct branch_entry entries[LBRS_MAX]; } br;Quoted 9 lines
The array in br is trying to extend branch_stack's entries array. You might have to do something like:
alignas(alignof(branch_stack)) char storage[sizeof(branch_stack) +
sizeof(branch_entry)*LBRS_MAX];
structbranch_stack*br = &storage;malloc/free may be nicer on the eyeballs.
Yep, I'd go for zalloc/free. There are a couple of places where dynamic memory is being allocated for struct
branch_stack:
tools/perf/util/cs-etm.c-256-if (etm->synth_opts.last_branch) {tools/perf/util/cs-etm.c:257:size_tsz =sizeof(struct branch_stack);tools/perf/util/cs-etm.c-258-tools/perf/util/cs-etm.c-259-sz +=etm->synth_opts.last_branch_sz*tools/perf/util/cs-etm.c-260-sizeof(struct branch_entry);tools/perf/util/cs-etm.c-261-tidq->last_branch= zalloc(sz);
tools/perf/util/thread-stack.c-148-if (br_stack_sz) {tools/perf/util/thread-stack.c:149:size_tsz =sizeof(struct branch_stack);tools/perf/util/thread-stack.c-150-tools/perf/util/thread-stack.c-151-sz +=br_stack_sz*sizeof(struct branch_entry);tools/perf/util/thread-stack.c-152-ts->br_stack_rb= zalloc(sz);
Quoted 4 lines
there is even function
intel_pt_alloc_br_stack().Just out of curiosity, why the need of such a hack in this case (the on-stack extension of branch_stack's entries array)?
My guess would be that the lbr size is an architectural constant and so avoiding malloc/free in what could be a hot loop was desirable. As this is part of a larger patch set, is this the only place this problem has been encountered? Perhaps a macro could perform the complicated stack allocation I suggested. It may be nice to save cycles if code this pattern is widespread and the code hot.
Thanks,
Ian
Quoted 4 lines
Thanks
--
Gustavo
Quoted 81 lines · Ian Rogers
Quoted 79 lines · Gustavo A. R. Silva
Quoted 49 lines · Ian Rogers
Quoted 24 lines · Gustavo A. R. Silva
Quoted 22 lines · Arnaldo Carvalho de Melo
Em Wed, May 13, 2020 at 06:47:38PM -0500, Gustavo A. R. Silva escreveu:
Quoted 9 lines
Fix the following build failure generated with command
$ make CC=clang HOSTCC=clang -C tools/ perf:
util/intel-pt.c:1802:24: error: field 'br_stack' with variable sized type 'structbranch_stack' not at the end of a struct or class is a GNU extension [-Werror,-Wgnu-variable-sized-type-not-at-end]struct branch_stack br_stack; ^1 error generated.
Fix this by reordering the members of struct br.
Yeah, I noticed that as far back as with ubuntu 16.04's clang:
clang version 3.8.0-2ubuntu4 (tags/RELEASE_380/final)
util/intel-pt.c:1802:24: error: field 'br_stack' with variable sized type 'structbranch_stack' not at the end of a struct or class is a GNUextension [-Werror,-Wgnu-variable-sized-type-not-at-end] struct branch_stack br_stack; ^1 error generated.
Quoted 6 lines
Quoted 3 lines
Will fold this with the bug introducing the problem to avoid bisection problems.
I agree. Also, the commit hash of the "Fixes" tag only applies to the perf/core branch and, I guess that might create confusion.
So while this fixes the warning I believe it breaks the intent of the code.
tools/perf/util/branch.h:struct branch_stack { u64 nr; u64 hw_idx; struct branch_entry entries[]; };
tools/perf/util/intel-pt.c:struct { struct branch_stack br_stack; struct branch_entry entries[LBRS_MAX]; } br;Quoted 9 lines
The array in br is trying to extend branch_stack's entries array. You might have to do something like:
alignas(alignof(branch_stack)) char storage[sizeof(branch_stack) +
sizeof(branch_entry)*LBRS_MAX];
structbranch_stack*br = &storage;malloc/free may be nicer on the eyeballs.
Yep, I'd go for zalloc/free. There are a couple of places where dynamic memory is being allocated for struct
branch_stack:
tools/perf/util/cs-etm.c-256-if (etm->synth_opts.last_branch) {tools/perf/util/cs-etm.c:257:size_tsz =sizeof(struct branch_stack);tools/perf/util/cs-etm.c-258-tools/perf/util/cs-etm.c-259-sz +=etm->synth_opts.last_branch_sz*tools/perf/util/cs-etm.c-260-sizeof(struct branch_entry);tools/perf/util/cs-etm.c-261-tidq->last_branch= zalloc(sz);
tools/perf/util/thread-stack.c-148-if (br_stack_sz) {tools/perf/util/thread-stack.c:149:size_tsz =sizeof(struct branch_stack);tools/perf/util/thread-stack.c-150-tools/perf/util/thread-stack.c-151-sz +=br_stack_sz*sizeof(struct branch_entry);tools/perf/util/thread-stack.c-152-ts->br_stack_rb= zalloc(sz);there is even function
intel_pt_alloc_br_stack().
Quoted 7 lines
Quoted 2 lines
Just out of curiosity, why the need of such a hack in this case (the on-stack extension of branch_stack's entries array)?
My guess would be that the lbr size is an architectural constant and so avoiding malloc/free in what could be a hot loop was desirable. As this is part of a larger patch set, is this the only place this problem has been encountered? Perhaps a macro could perform the
Yep. I just built linux-next --which contains all the flexible-array conversions-- with Clang --GCC doesn't catch this issue, not even GCC 10-- and I don't see any other issue like this.
I mean, I have run into these other two:
https://lore.kernel.org/lkml/20200505235205.GA18539@embeddedor/
https://lore.kernel.org/lkml/20200508163826.GA768@embeddedor/
but those are due to the erroneous application of the sizeof operator to zero-length arrays.
Quoted 3 lines
complicated stack allocation I suggested. It may be nice to save cycles if code this pattern is widespread and the code hot.
Apparently, this is the only instace of this sort of issue in the whole codebase.
Thanks
--
Gustavo
Ian Rogers
reviewerQuoted 103 lines · Gustavo A. R. Silva
Quoted 81 lines · Ian Rogers
Quoted 79 lines · Gustavo A. R. Silva
Quoted 49 lines · Ian Rogers
Quoted 24 lines · Gustavo A. R. Silva
Quoted 22 lines · Arnaldo Carvalho de Melo
Em Wed, May 13, 2020 at 06:47:38PM -0500, Gustavo A. R. Silva escreveu:
Quoted 9 lines
Fix the following build failure generated with command
$ make CC=clang HOSTCC=clang -C tools/ perf:
util/intel-pt.c:1802:24: error: field 'br_stack' with variable sized type 'structbranch_stack' not at the end of a struct or class is a GNU extension [-Werror,-Wgnu-variable-sized-type-not-at-end]struct branch_stack br_stack; ^1 error generated.
Fix this by reordering the members of struct br.
Yeah, I noticed that as far back as with ubuntu 16.04's clang:
clang version 3.8.0-2ubuntu4 (tags/RELEASE_380/final)
util/intel-pt.c:1802:24: error: field 'br_stack' with variable sized type 'structbranch_stack' not at the end of a struct or class is a GNUextension [-Werror,-Wgnu-variable-sized-type-not-at-end] struct branch_stack br_stack; ^1 error generated.
Quoted 6 lines
Quoted 3 lines
Will fold this with the bug introducing the problem to avoid bisection problems.
I agree. Also, the commit hash of the "Fixes" tag only applies to the perf/core branch and, I guess that might create confusion.
So while this fixes the warning I believe it breaks the intent of the code.
tools/perf/util/branch.h:struct branch_stack { u64 nr; u64 hw_idx; struct branch_entry entries[]; };
tools/perf/util/intel-pt.c:struct { struct branch_stack br_stack; struct branch_entry entries[LBRS_MAX]; } br;Quoted 9 lines
The array in br is trying to extend branch_stack's entries array. You might have to do something like:
alignas(alignof(branch_stack)) char storage[sizeof(branch_stack) +
sizeof(branch_entry)*LBRS_MAX];
structbranch_stack*br = &storage;malloc/free may be nicer on the eyeballs.
Yep, I'd go for zalloc/free. There are a couple of places where dynamic memory is being allocated for struct
branch_stack:
tools/perf/util/cs-etm.c-256-if (etm->synth_opts.last_branch) {tools/perf/util/cs-etm.c:257:size_tsz =sizeof(struct branch_stack);tools/perf/util/cs-etm.c-258-tools/perf/util/cs-etm.c-259-sz +=etm->synth_opts.last_branch_sz*tools/perf/util/cs-etm.c-260-sizeof(struct branch_entry);tools/perf/util/cs-etm.c-261-tidq->last_branch= zalloc(sz);
tools/perf/util/thread-stack.c-148-if (br_stack_sz) {tools/perf/util/thread-stack.c:149:size_tsz =sizeof(struct branch_stack);tools/perf/util/thread-stack.c-150-tools/perf/util/thread-stack.c-151-sz +=br_stack_sz*sizeof(struct branch_entry);tools/perf/util/thread-stack.c-152-ts->br_stack_rb= zalloc(sz);there is even function
intel_pt_alloc_br_stack().Quoted 7 lines
Quoted 2 lines
Just out of curiosity, why the need of such a hack in this case (the on-stack extension of branch_stack's entries array)?
My guess would be that the lbr size is an architectural constant and so avoiding malloc/free in what could be a hot loop was desirable. As this is part of a larger patch set, is this the only place this problem has been encountered? Perhaps a macro could perform the
Yep. I just built linux-next --which contains all the flexible-array conversions-- with Clang --GCC doesn't catch this issue, not even GCC 10-- and I don't see any other issue like this.
I mean, I have run into these other two:
https://lore.kernel.org/lkml/20200505235205.GA18539@embeddedor/
https://lore.kernel.org/lkml/20200508163826.GA768@embeddedor/but those are due to the erroneous application of the sizeof operator to zero-length arrays.
Quoted 6 lines
Quoted 3 lines
complicated stack allocation I suggested. It may be nice to save cycles if code this pattern is widespread and the code hot.
Apparently, this is the only instace of this sort of issue in the whole codebase.
Thanks for checking, I'd convert it to malloc/free but Intel really owns this code.
Ian
Quoted 3 lines
Thanks
--
Gustavo
Quoted 109 lines · Ian Rogers
Quoted 107 lines · Gustavo A. R. Silva
Quoted 81 lines · Ian Rogers
Quoted 79 lines · Gustavo A. R. Silva
Quoted 49 lines · Ian Rogers
Quoted 24 lines · Gustavo A. R. Silva
Quoted 22 lines · Arnaldo Carvalho de Melo
Em Wed, May 13, 2020 at 06:47:38PM -0500, Gustavo A. R. Silva escreveu:
Quoted 9 lines
Fix the following build failure generated with command
$ make CC=clang HOSTCC=clang -C tools/ perf:
util/intel-pt.c:1802:24: error: field 'br_stack' with variable sized type 'structbranch_stack' not at the end of a struct or class is a GNU extension [-Werror,-Wgnu-variable-sized-type-not-at-end]struct branch_stack br_stack; ^1 error generated.
Fix this by reordering the members of struct br.
Yeah, I noticed that as far back as with ubuntu 16.04's clang:
clang version 3.8.0-2ubuntu4 (tags/RELEASE_380/final)
util/intel-pt.c:1802:24: error: field 'br_stack' with variable sized type 'structbranch_stack' not at the end of a struct or class is a GNUextension [-Werror,-Wgnu-variable-sized-type-not-at-end] struct branch_stack br_stack; ^1 error generated.
Quoted 6 lines
Quoted 3 lines
Will fold this with the bug introducing the problem to avoid bisection problems.
I agree. Also, the commit hash of the "Fixes" tag only applies to the perf/core branch and, I guess that might create confusion.
So while this fixes the warning I believe it breaks the intent of the code.
tools/perf/util/branch.h:struct branch_stack { u64 nr; u64 hw_idx; struct branch_entry entries[]; };
tools/perf/util/intel-pt.c:struct { struct branch_stack br_stack; struct branch_entry entries[LBRS_MAX]; } br;Quoted 9 lines
The array in br is trying to extend branch_stack's entries array. You might have to do something like:
alignas(alignof(branch_stack)) char storage[sizeof(branch_stack) +
sizeof(branch_entry)*LBRS_MAX];
structbranch_stack*br = &storage;malloc/free may be nicer on the eyeballs.
Yep, I'd go for zalloc/free. There are a couple of places where dynamic memory is being allocated for struct
branch_stack:
tools/perf/util/cs-etm.c-256-if (etm->synth_opts.last_branch) {tools/perf/util/cs-etm.c:257:size_tsz =sizeof(struct branch_stack);tools/perf/util/cs-etm.c-258-tools/perf/util/cs-etm.c-259-sz +=etm->synth_opts.last_branch_sz*tools/perf/util/cs-etm.c-260-sizeof(struct branch_entry);tools/perf/util/cs-etm.c-261-tidq->last_branch= zalloc(sz);
tools/perf/util/thread-stack.c-148-if (br_stack_sz) {tools/perf/util/thread-stack.c:149:size_tsz =sizeof(struct branch_stack);tools/perf/util/thread-stack.c-150-tools/perf/util/thread-stack.c-151-sz +=br_stack_sz*sizeof(struct branch_entry);tools/perf/util/thread-stack.c-152-ts->br_stack_rb= zalloc(sz);there is even function
intel_pt_alloc_br_stack().Quoted 7 lines
Quoted 2 lines
Just out of curiosity, why the need of such a hack in this case (the on-stack extension of branch_stack's entries array)?
My guess would be that the lbr size is an architectural constant and so avoiding malloc/free in what could be a hot loop was desirable. As this is part of a larger patch set, is this the only place this problem has been encountered? Perhaps a macro could perform the
Yep. I just built linux-next --which contains all the flexible-array conversions-- with Clang --GCC doesn't catch this issue, not even GCC 10-- and I don't see any other issue like this.
I mean, I have run into these other two:
https://lore.kernel.org/lkml/20200505235205.GA18539@embeddedor/
https://lore.kernel.org/lkml/20200508163826.GA768@embeddedor/but those are due to the erroneous application of the sizeof operator to zero-length arrays.
Quoted 3 lines
complicated stack allocation I suggested. It may be nice to save cycles if code this pattern is widespread and the code hot.
Quoted 6 lines
Quoted 2 lines
Apparently, this is the only instace of this sort of issue in the whole codebase.
Thanks for checking, I'd convert it to malloc/free but Intel really owns this code.
Go ahead and I can add that part to my patch and include a Co-developed-by tag. :)
Thanks
--
Gustavo
Sorry for the top post: Adrian, can you take a look at this?
Em Thu, May 14, 2020 at 07:10:25PM -0500, Gustavo A. R. Silva escreveu:
Quoted 102 lines · Ian Rogers
Quoted 81 lines
Quoted 79 lines · Gustavo A. R. Silva
Quoted 49 lines · Ian Rogers
Quoted 24 lines · Gustavo A. R. Silva
Quoted 22 lines · Arnaldo Carvalho de Melo
Em Wed, May 13, 2020 at 06:47:38PM -0500, Gustavo A. R. Silva escreveu:
Quoted 9 lines
Fix the following build failure generated with command
$ make CC=clang HOSTCC=clang -C tools/ perf:
util/intel-pt.c:1802:24: error: field 'br_stack' with variable sized type 'structbranch_stack' not at the end of a struct or class is a GNU extension [-Werror,-Wgnu-variable-sized-type-not-at-end]struct branch_stack br_stack; ^1 error generated.
Fix this by reordering the members of struct br.
Yeah, I noticed that as far back as with ubuntu 16.04's clang:
clang version 3.8.0-2ubuntu4 (tags/RELEASE_380/final)
util/intel-pt.c:1802:24: error: field 'br_stack' with variable sized type 'structbranch_stack' not at the end of a struct or class is a GNUextension [-Werror,-Wgnu-variable-sized-type-not-at-end] struct branch_stack br_stack; ^1 error generated.
Quoted 6 lines
Quoted 3 lines
Will fold this with the bug introducing the problem to avoid bisection problems.
I agree. Also, the commit hash of the "Fixes" tag only applies to the perf/core branch and, I guess that might create confusion.
So while this fixes the warning I believe it breaks the intent of the code.
tools/perf/util/branch.h:struct branch_stack { u64 nr; u64 hw_idx; struct branch_entry entries[]; };
tools/perf/util/intel-pt.c:struct { struct branch_stack br_stack; struct branch_entry entries[LBRS_MAX]; } br;Quoted 9 lines
The array in br is trying to extend branch_stack's entries array. You might have to do something like:
alignas(alignof(branch_stack)) char storage[sizeof(branch_stack) +
sizeof(branch_entry)*LBRS_MAX];
structbranch_stack*br = &storage;malloc/free may be nicer on the eyeballs.
Yep, I'd go for zalloc/free. There are a couple of places where dynamic memory is being allocated for struct
branch_stack:
tools/perf/util/cs-etm.c-256-if (etm->synth_opts.last_branch) {tools/perf/util/cs-etm.c:257:size_tsz =sizeof(struct branch_stack);tools/perf/util/cs-etm.c-258-tools/perf/util/cs-etm.c-259-sz +=etm->synth_opts.last_branch_sz*tools/perf/util/cs-etm.c-260-sizeof(struct branch_entry);tools/perf/util/cs-etm.c-261-tidq->last_branch= zalloc(sz);
tools/perf/util/thread-stack.c-148-if (br_stack_sz) {tools/perf/util/thread-stack.c:149:size_tsz =sizeof(struct branch_stack);tools/perf/util/thread-stack.c-150-tools/perf/util/thread-stack.c-151-sz +=br_stack_sz*sizeof(struct branch_entry);tools/perf/util/thread-stack.c-152-ts->br_stack_rb= zalloc(sz);there is even function
intel_pt_alloc_br_stack().Quoted 7 lines
Quoted 2 lines
Just out of curiosity, why the need of such a hack in this case (the on-stack extension of branch_stack's entries array)?
My guess would be that the lbr size is an architectural constant and so avoiding malloc/free in what could be a hot loop was desirable. As this is part of a larger patch set, is this the only place this problem has been encountered? Perhaps a macro could perform the
Yep. I just built linux-next --which contains all the flexible-array conversions-- with Clang --GCC doesn't catch this issue, not even GCC 10-- and I don't see any other issue like this.
I mean, I have run into these other two:
https://lore.kernel.org/lkml/20200505235205.GA18539@embeddedor/
https://lore.kernel.org/lkml/20200508163826.GA768@embeddedor/but those are due to the erroneous application of the sizeof operator to zero-length arrays.
Quoted 6 lines
Quoted 3 lines
complicated stack allocation I suggested. It may be nice to save cycles if code this pattern is widespread and the code hot.
Apparently, this is the only instace of this sort of issue in the whole codebase.
Adrian Hunter was not CCed, Adrian?
- Arnaldo
Em Fri, May 15, 2020 at 01:41:53PM -0300, Arnaldo Carvalho de Melo escreveu:
Quoted 17 lines
Em Thu, May 14, 2020 at 07:10:25PM -0500, Gustavo A. R. Silva escreveu:
Quoted 16 lines · Ian Rogers
Quoted 2 lines
On Thu, May 14, 2020 at 3:04 PM Gustavo A. R. Silva <[email protected]> wrote:
Yep. I just built linux-next --which contains all the flexible-array conversions-- with Clang --GCC doesn't catch this issue, not even GCC 10-- and I don't see any other issue like this.
I mean, I have run into these other two:
https://lore.kernel.org/lkml/20200505235205.GA18539@embeddedor/
https://lore.kernel.org/lkml/20200508163826.GA768@embeddedor/but those are due to the erroneous application of the sizeof operator to zero-length arrays.
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
complicated stack allocation I suggested. It may be nice to save cycles if code this pattern is widespread and the code hot.
Apparently, this is the only instace of this sort of issue in the whole codebase.
Adrian Hunter was not CCed, Adrian?
Gustavo, I've removed this from my tree from now till this gets resolved.
- Arnaldo
Quoted 22 lines · Arnaldo Carvalho de Melo
Em Fri, May 15, 2020 at 01:41:53PM -0300, Arnaldo Carvalho de Melo escreveu:
Quoted 21 lines
Em Thu, May 14, 2020 at 07:10:25PM -0500, Gustavo A. R. Silva escreveu:
Quoted 20 lines · Ian Rogers
Quoted 2 lines
On Thu, May 14, 2020 at 3:04 PM Gustavo A. R. Silva <[email protected]> wrote:
Yep. I just built linux-next --which contains all the flexible-array conversions-- with Clang --GCC doesn't catch this issue, not even GCC 10-- and I don't see any other issue like this.
I mean, I have run into these other two:
https://lore.kernel.org/lkml/20200505235205.GA18539@embeddedor/
https://lore.kernel.org/lkml/20200508163826.GA768@embeddedor/but those are due to the erroneous application of the sizeof operator to zero-length arrays.
Quoted 3 lines
complicated stack allocation I suggested. It may be nice to save cycles if code this pattern is widespread and the code hot.
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
Apparently, this is the only instace of this sort of issue in the whole codebase.
Adrian Hunter was not CCed, Adrian?
Gustavo, I've removed this from my tree from now till this gets resolved.
Yep, sure. In the meantime, I'll send a patch without the changes to struct branch_stack.
Thanks
--
Gustavo
Adrian Hunter
reviewerQuoted 3 lines · Arnaldo Carvalho de Melo
Sorry for the top post: Adrian, can you take a look at this?
Adrian Hunter was not CCed, Adrian?
From: Adrian Hunter <[email protected]>
Date: Sat, 16 May 2020 15:12:28 +0300
Subject: [PATCH] perf intel-pt: Use allocated branch stack for PEBS sample
To avoid having struct branch_stack as a non-last structure member, use allocated branch stack for PEBS sample.
--- tools/perf/util/intel-pt.c | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-)
tools/perf/util/intel-pt.c +13 -18
@@ -913,11 +913,11 @@ static void intel_pt_add_callchain(struct intel_pt *pt, sample->callchain = pt->chain; } -static struct branch_stack *intel_pt_alloc_br_stack (struct intel_pt *pt) +static struct branch_stack *intel_pt_alloc_br_stack (unsigned int entry_cnt) {size_t sz =sizeof (struct branch_stack ); - sz += pt->br_stack_sz *sizeof (struct branch_entry ); + sz += entry_cnt *sizeof (struct branch_entry );return zalloc (sz); }
@@ -930,7 +930,7 @@ static int intel_pt_br_stack_init(struct intel_pt *pt) evsel->synth_sample_type |=PERF_SAMPLE_BRANCH_STACK ; } - pt->br_stack =intel_pt_alloc_br_stack (pt); + pt->br_stack =intel_pt_alloc_br_stack (pt->br_stack_sz);if (!pt->br_stack)return -ENOMEM ;
@@ -951,6 +951,9 @@ static void intel_pt_add_br_stack(struct intel_pt *pt, sample->branch_stack = pt->br_stack; } +/* INTEL_PT_LBR_0, INTEL_PT_LBR_1 and INTEL_PT_LBR_2 */ +#define LBRS_MAX (INTEL_PT_BLK_ITEM_ID_CNT * 3U) +static struct intel_pt_queue *intel_pt_alloc_queue (struct intel_pt *pt,unsigned int queue_nr) {
@@ -968,8 +971,10 @@ static struct intel_pt_queue *intel_pt_alloc_queue(struct intel_pt *pt, goto out_free; } -if (pt->synth_opts.last_branch) { - ptq->last_branch =intel_pt_alloc_br_stack (pt); +if (pt->synth_opts.last_branch || pt->synth_opts.other_events) { +unsigned int entry_cnt =max (LBRS_MAX , pt->br_stack_sz); + + ptq->last_branch =intel_pt_alloc_br_stack (entry_cnt);if (!ptq->last_branch) goto out_free; }
@@ -1720,9 +1725,6 @@ static void intel_pt_add_lbrs(struct branch_stack *br_stack, } } -/* INTEL_PT_LBR_0, INTEL_PT_LBR_1 and INTEL_PT_LBR_2 */ -#define LBRS_MAX (INTEL_PT_BLK_ITEM_ID_CNT * 3) -static int intel_pt_synth_pebs_sample (struct intel_pt_queue *ptq) {const struct intel_pt_blk_items *items = &ptq->state->items;
@@ -1798,25 +1800,18 @@ static int intel_pt_synth_pebs_sample(struct intel_pt_queue *ptq) }if (sample_type &PERF_SAMPLE_BRANCH_STACK ) { -struct { -struct branch_stack br_stack; -struct branch_entry entries[LBRS_MAX ]; - } br; -if (items->mask[INTEL_PT_LBR_0_POS ] || items->mask[INTEL_PT_LBR_1_POS ] || items->mask[INTEL_PT_LBR_2_POS ]) { -intel_pt_add_lbrs (&br.br_stack, items); - sample.branch_stack = &br.br_stack; +intel_pt_add_lbrs (ptq->last_branch, items); }else if (pt->synth_opts.last_branch) {thread_stack__br_sample (ptq->thread, ptq->cpu, ptq->last_branch, pt->br_stack_sz); - sample.branch_stack = ptq->last_branch; }else { - br.br_stack.nr =0 ; - sample.branch_stack = &br.br_stack; + ptq->last_branch->nr =0 ; } + sample.branch_stack = ptq->last_branch; }if (sample_type &PERF_SAMPLE_ADDR && items->has_mem_access_address)
Signature · 1 line
2.17.1
Gustavo A. R. Silva
Acked-byQuoted 5 lines · Adrian Hunter
Quoted 3 lines · Arnaldo Carvalho de Melo
Sorry for the top post: Adrian, can you take a look at this?
Adrian Hunter was not CCed, Adrian?
Quoted 8 lines
From: Adrian Hunter <[email protected]>
Date: Sat, 16 May 2020 15:12:28 +0300
Subject: [PATCH] perf intel-pt: Use allocated branch stack for PEBS sampleTo avoid having struct
branch_stackas a non-last structure member, use allocated branch stack for PEBS sample.Signed-off-by Adrian Hunter <[email protected]>
Thanks, Adrian.
--
Gustavo
Quoted 98 lines
--- tools/perf/util/intel-pt.c | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-)tools/perf/util/intel-pt.c +13
-18@@ -913,11 +913,11 @@ static void intel_pt_add_callchain(struct intel_pt *pt, sample->callchain = pt->chain; } -static struct branch_stack *intel_pt_alloc_br_stack (struct intel_pt *pt) +static struct branch_stack *intel_pt_alloc_br_stack (unsigned int entry_cnt) {size_t sz =sizeof (struct branch_stack ); - sz += pt->br_stack_sz *sizeof (struct branch_entry ); + sz += entry_cnt *sizeof (struct branch_entry );return zalloc (sz); }@@ -930,7 +930,7 @@ static int intel_pt_br_stack_init(struct intel_pt *pt) evsel->synth_sample_type |=PERF_SAMPLE_BRANCH_STACK ; } - pt->br_stack =intel_pt_alloc_br_stack (pt); + pt->br_stack =intel_pt_alloc_br_stack (pt->br_stack_sz);if (!pt->br_stack)return -ENOMEM ;@@ -951,6 +951,9 @@ static void intel_pt_add_br_stack(struct intel_pt *pt, sample->branch_stack = pt->br_stack; } +/* INTEL_PT_LBR_0, INTEL_PT_LBR_1 and INTEL_PT_LBR_2 */ +#define LBRS_MAX (INTEL_PT_BLK_ITEM_ID_CNT * 3U) +static struct intel_pt_queue *intel_pt_alloc_queue (struct intel_pt *pt,unsigned int queue_nr) {@@ -968,8 +971,10 @@ static struct intel_pt_queue *intel_pt_alloc_queue(struct intel_pt *pt, goto out_free; } -if (pt->synth_opts.last_branch) { - ptq->last_branch =intel_pt_alloc_br_stack (pt); +if (pt->synth_opts.last_branch || pt->synth_opts.other_events) { +unsigned int entry_cnt =max (LBRS_MAX , pt->br_stack_sz); + + ptq->last_branch =intel_pt_alloc_br_stack (entry_cnt);if (!ptq->last_branch) goto out_free; }@@ -1720,9 +1725,6 @@ static void intel_pt_add_lbrs(struct branch_stack *br_stack, } } -/* INTEL_PT_LBR_0, INTEL_PT_LBR_1 and INTEL_PT_LBR_2 */ -#define LBRS_MAX (INTEL_PT_BLK_ITEM_ID_CNT * 3) -static int intel_pt_synth_pebs_sample (struct intel_pt_queue *ptq) {const struct intel_pt_blk_items *items = &ptq->state->items;@@ -1798,25 +1800,18 @@ static int intel_pt_synth_pebs_sample(struct intel_pt_queue *ptq) }if (sample_type &PERF_SAMPLE_BRANCH_STACK ) { -struct { -struct branch_stack br_stack; -struct branch_entry entries[LBRS_MAX ]; - } br; -if (items->mask[INTEL_PT_LBR_0_POS ] || items->mask[INTEL_PT_LBR_1_POS ] || items->mask[INTEL_PT_LBR_2_POS ]) { -intel_pt_add_lbrs (&br.br_stack, items); - sample.branch_stack = &br.br_stack; +intel_pt_add_lbrs (ptq->last_branch, items); }else if (pt->synth_opts.last_branch) {thread_stack__br_sample (ptq->thread, ptq->cpu, ptq->last_branch, pt->br_stack_sz); - sample.branch_stack = ptq->last_branch; }else { - br.br_stack.nr =0 ; - sample.branch_stack = &br.br_stack; + ptq->last_branch->nr =0 ; } + sample.branch_stack = ptq->last_branch; }if (sample_type &PERF_SAMPLE_ADDR && items->has_mem_access_address)Signature · 1 line
2.17.1
Arnaldo Carvalho de Melo
AppliedEm Sat, May 16, 2020 at 03:35:48PM +0300, Adrian Hunter escreveu:
Quoted 11 lines · Arnaldo Carvalho de Melo
Quoted 3 lines
Sorry for the top post: Adrian, can you take a look at this?
Adrian Hunter was not CCed, Adrian?
From: Adrian Hunter <[email protected]>
Date: Sat, 16 May 2020 15:12:28 +0300
Subject: [PATCH] perf intel-pt: Use allocated branch stack for PEBS sampleTo avoid having struct
branch_stackas a non-last structure member, use allocated branch stack for PEBS sample.
Thanks, applied.
- Arnaldo
Quoted 99 lines
Signed-off-by Adrian Hunter <[email protected]>--- tools/perf/util/intel-pt.c | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-)tools/perf/util/intel-pt.c +13
-18@@ -913,11 +913,11 @@ static void intel_pt_add_callchain(struct intel_pt *pt, sample->callchain = pt->chain; } -static struct branch_stack *intel_pt_alloc_br_stack (struct intel_pt *pt) +static struct branch_stack *intel_pt_alloc_br_stack (unsigned int entry_cnt) {size_t sz =sizeof (struct branch_stack ); - sz += pt->br_stack_sz *sizeof (struct branch_entry ); + sz += entry_cnt *sizeof (struct branch_entry );return zalloc (sz); }@@ -930,7 +930,7 @@ static int intel_pt_br_stack_init(struct intel_pt *pt) evsel->synth_sample_type |=PERF_SAMPLE_BRANCH_STACK ; } - pt->br_stack =intel_pt_alloc_br_stack (pt); + pt->br_stack =intel_pt_alloc_br_stack (pt->br_stack_sz);if (!pt->br_stack)return -ENOMEM ;@@ -951,6 +951,9 @@ static void intel_pt_add_br_stack(struct intel_pt *pt, sample->branch_stack = pt->br_stack; } +/* INTEL_PT_LBR_0, INTEL_PT_LBR_1 and INTEL_PT_LBR_2 */ +#define LBRS_MAX (INTEL_PT_BLK_ITEM_ID_CNT * 3U) +static struct intel_pt_queue *intel_pt_alloc_queue (struct intel_pt *pt,unsigned int queue_nr) {@@ -968,8 +971,10 @@ static struct intel_pt_queue *intel_pt_alloc_queue(struct intel_pt *pt, goto out_free; } -if (pt->synth_opts.last_branch) { - ptq->last_branch =intel_pt_alloc_br_stack (pt); +if (pt->synth_opts.last_branch || pt->synth_opts.other_events) { +unsigned int entry_cnt =max (LBRS_MAX , pt->br_stack_sz); + + ptq->last_branch =intel_pt_alloc_br_stack (entry_cnt);if (!ptq->last_branch) goto out_free; }@@ -1720,9 +1725,6 @@ static void intel_pt_add_lbrs(struct branch_stack *br_stack, } } -/* INTEL_PT_LBR_0, INTEL_PT_LBR_1 and INTEL_PT_LBR_2 */ -#define LBRS_MAX (INTEL_PT_BLK_ITEM_ID_CNT * 3) -static int intel_pt_synth_pebs_sample (struct intel_pt_queue *ptq) {const struct intel_pt_blk_items *items = &ptq->state->items;@@ -1798,25 +1800,18 @@ static int intel_pt_synth_pebs_sample(struct intel_pt_queue *ptq) }if (sample_type &PERF_SAMPLE_BRANCH_STACK ) { -struct { -struct branch_stack br_stack; -struct branch_entry entries[LBRS_MAX ]; - } br; -if (items->mask[INTEL_PT_LBR_0_POS ] || items->mask[INTEL_PT_LBR_1_POS ] || items->mask[INTEL_PT_LBR_2_POS ]) { -intel_pt_add_lbrs (&br.br_stack, items); - sample.branch_stack = &br.br_stack; +intel_pt_add_lbrs (ptq->last_branch, items); }else if (pt->synth_opts.last_branch) {thread_stack__br_sample (ptq->thread, ptq->cpu, ptq->last_branch, pt->br_stack_sz); - sample.branch_stack = ptq->last_branch; }else { - br.br_stack.nr =0 ; - sample.branch_stack = &br.br_stack; + ptq->last_branch->nr =0 ; } + sample.branch_stack = ptq->last_branch; }if (sample_type &PERF_SAMPLE_ADDR && items->has_mem_access_address)Signature · 1 line
2.17.1
Signature · 1 line
- Arnaldo