[REGRESSION] perf build failed after 5cf6e76e4f4f ("libperf: Don't remove -g when EXTRA_CFLAGS are used") on riscv64 with gcc 13

From: Haixiao Yan

Date: Wed Mar 18 2026 - 22:03:43 EST


Hi,

Commit[5cf6e76e4f4f](https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/tools/lib/perf/Makefile?h=linux-6.6.y&id=5cf6e76e4f4fee54c0056758b639cf4919cffba9)
changed the libperf Makefile to preserve external CFLAGS instead of overriding them. As a result, the -O6 optimization flags from perf's
build system are now inherited by libperf during compilation. This triggers a false positive -Walloc-size-larger-than= warning in GCC 13 on
riscv64, causing the build to fail with -Werror.

| cpumap.c: In function 'perf_cpu_map__merge':
| cpumap.c:422:20: error: argument 1 range [18446744065119617024, 18446744073709551612] exceeds maximum objec
t size 9223372036854775807 [-Werror=alloc-size-larger-than=]
| 422 | tmp_cpus = malloc(tmp_len * sizeof(struct perf_cpu));
| | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| In file included from cpumap.c:3:
| /buildarea5/hyan-cn/project_yocto/poky/build-riscv64/tmp/work/qemuriscv64-poky-linux/perf/1.0/recipe-sysroo
t/usr/include/stdlib.h:672:14: note: in a call to allocation function 'malloc' declared here
| 672 | extern void *malloc (size_t __size) __THROW __attribute_malloc__
| | ^~~~~~
| rm -f /buildarea5/hyan-cn/project_yocto/poky/build-riscv64/tmp/work/qemuriscv64-poky-linux/perf/1.0/perf-1.
0/libapi/libapi.a && riscv64-poky-linux-gcc-ar rcs /buildarea5/hyan-cn/project_yocto/poky/build-riscv64/tmp/w
ork/qemuriscv64-poky-linux/perf/1.0/perf-1.0/libapi/libapi.a /buildarea5/hyan-cn/project_yocto/poky/build-ris
cv64/tmp/work/qemuriscv64-poky-linux/perf/1.0/perf-1.0/libapi/libapi-in.o
| cc1: all warnings being treated as errors


Steps to reproduce:

git clone -b scarthgap https://git.yoctoproject.org/poky
cd poky
sed -i 's/af240d7d57ebf66e87bc2dff34855e630a97ead1/5cf6e76e4f4fee54c0056758b639cf4919cffba9/' meta/recipes-kernel/linux/linux-yocto_6.6.bb

source oe-init-build-env build-riscv64

cat >> conf/local.conf << 'EOF'
MACHINE = "qemuriscv64"
'KERNEL_VERSION_SANITY_SKIP = "1"'
EOF

bitbake perf

I have confirmed that:
Known to fail: gcc 13.3.0, 13.4.0
Known to work: gcc 11.5.0, 12.5.0, 14.3.0, 15.2.0

Not sure whether this is a gcc bug.
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=124549 filed to gcc.

Thanks,
Haixiao