[PATCH] riscv: fix KUnit test_kprobes crash when building with Clang

Jiakai Xu xujiakai2025 at iscas.ac.cn
Sun Nov 16 19:03:52 PST 2025


Hello Paul,

Thank you very much for reviewing my patch; your feedback was very helpful.


> Thanks for the patch.  Have you reported this difference in behavior to 
> the Clang/LLVM folks so they can render an opinion on it?  We can consider 
> this as a short term workaround if it's indeed a toolchain issue, but I'd 
> like to hear from the LLVM folks.

I have reported this issue to the LLVM community:
https://github.com/llvm/llvm-project/issues/168308
We now need to wait for feedback from the LLVM community.

> 
> Also: could you please include the narrative from your page 
> 
>     https://github.com/j1akai/temp/blob/main/20251113/readme.md 
> 
> in the patch description, just in case that URL eventually becomes 
> unavailable?

Below, I include the narrative from my readme for reference:

---
Kernel:
https://git.kernel.org/pub/scm/linux/kernel/git/snapshot/linux-riscv-for-linus-6.18-rc6.tar.gz

Configuration: 
https://github.com/j1akai/temp/blob/main/20251113/config.log

LLVM:
https://github.com/llvm/llvm-project/releases/download/llvmorg-21.1.5/LLVM-21.1.5-Linux-X64.tar.xz

When booting the kernel in QEMU, the error was traced to:
`arch/riscv/kernel/tests/kprobes`. Log:
https://github.com/j1akai/temp/blob/main/20251113/report0.log

Specifically, in the file `arch/riscv/kernel/tests/kprobes/test-kprobes.c`,
the following line fails:

```c
kp = kcalloc(num_kprobe, sizeof(*kp), GFP_KERNEL);
```

I added debugging output:

```c
while (test_kprobes_addresses[num_kprobe]) {
    pr_info("addr[%d] = %px\n", num_kprobe,
            test_kprobes_addresses[num_kprobe]);
    num_kprobe++;
}
pr_info("num_kprobe=%u\n", num_kprobe);
```

This revealed that the length of the `test_kprobes_addresses` array is
abnormally large. See:
https://github.com/j1akai/temp/blob/main/20251113/vm.log

According to the file `arch/riscv/kernel/tests/kprobes/test-kprobes-asm.S`,
only 25 entries should be inserted into `test_kprobes_addresses`. Therefore,
the array should not be this long.

When compiling the kernel with GCC, the kernel boots correctly. This indicates
that the issue is specific to Clang: Clang misaligns the arrays or generates
symbols incorrectly, causing `test_kprobes_addresses` to appear much larger than
it actually is, even containing invalid addresses. Consequently, the loop

```c
while(test_kprobes_addresses[num_kprobe])
```

overestimates `num_kprobe`, and `kcalloc` tries to allocate an excessively
large memory block.

The solution is to explicitly apply `.align 3` (8-byte alignment) to both
`test_kprobes_addresses` and `test_kprobes_functions`, place them in `.rodata`,
and declare the symbols as global using `.globl` to ensure consistent behavior
between Clang and GCC.

---

I would like to ask whether it is appropriate to 
include all this detail directly in the patch description and email, 
or if it would be better to submit it as a PATCH v1.

Thank you again for your guidance.

- Jiaki Xu


More information about the linux-riscv mailing list