Re: [PATCH] LoongArch: Fix off-by-one in bt_address() exception vector check
From: hanbings
Date: Sat Nov 21 2026 - 13:14:11 EST
Hi Tiezhu,
Thanks for looking at this. I can reproduce it deterministically on
QEMU with vanilla v7.2.9; the machine here runs 7.2.9, so I used the
same version for the test.
The important detail is that vector 78 is a real, installed vector:
trap_init() copies handle_vint into every slot from
EXCCODE_INT_START (64) through EXCCODE_INT_END (78). In the
handle_vint generated by this build (GCC 16.2.0), the call to do_vint
is at handle_vint + 0x154:
...
jirl $ra, $t0, 0 # handle_vint + 0x154
...
So when an interrupt enters vector 78 and do_vint is running, the
return address left in $ra is:
eentry + 78 * VECSIZE + 0x154 + LOONGARCH_INSN_SIZE
unwind_next_frame() subtracts LOONGARCH_INSN_SIZE and calls
bt_address() with eentry + 78 * VECSIZE + 0x154, which is exactly the
address the current range check rejects.
To isolate this, I used the small out-of-tree module below. It sets
state.pc = 0 (which selects the NULL-IP ORC entry, so the unwinder
consumes state.ra) and:
state.ra = eentry + vec * VECSIZE + 0x154 + LOONGARCH_INSN_SIZE
then calls unwind_next_frame(). It checks three vectors:
77 (last - 1): must succeed on both kernels
78 (last): must fail before the fix, succeed after
79 (last + 1): control, must fail on both
The module returns -EINVAL from init on purpose, so it is discarded
after printing; the results are in dmesg.
Both kernels were built from the same v7.2.9 source and base
configuration. The only source code difference was the one-line
patch. The module was built separately against each kernel.
Unpatched:
unwind: vec=77 ok=1 ...
cannot find unwind pc at ...
unwind: vec=78 ok=0 ...
cannot find unwind pc at ...
unwind: vec=79 ok=0 ...
Patched:
unwind: vec=77 ok=1 ...
unwind: vec=78 ok=1 ...
cannot find unwind pc at ...
unwind: vec=79 ok=0 ...
The patched vec=78 result maps to handle_vint + 0x154, i.e. the
do_vint call site.
To give some background: this issue shows up intermittently on a
3B6000 machine that I run as a NAS. In the end I only saw the kernel
log line, without a stack trace or any other context, for example:
[470049.376195] cannot find unwind pc at 9000000004e09d54
or, from journalctl -k:
Nov 14 12:19:30.400918 Lihuas kernel: cannot find unwind pc at 9000000004fc9d54
Nov 14 16:17:12.068835 Lihuas kernel: cannot find unwind pc at 9000000004fc9d54
Nov 15 03:53:19.899813 Lihuas kernel: cannot find unwind pc at 9000000004fc9d54
Nov 15 22:38:45.406173 Lihuas kernel: cannot find unwind pc at 9000000004fc9d54
All of these addresses end in ...9d54, which is the vector-78 slot
plus 0x154. But the log lines do not identify which software
triggered the interrupt, so the reproducer below simulates that
return address instead of actually triggering an AVEC interrupt.
On the real machine, which runs 7.2.9 with the fix installed since
2026-11-15, I have not seen the "cannot find unwind pc" message
again.
If you want to run the exact QEMU A/B, I can send the kernel build and
qemu-system-loongarch64 commands and the full logs.
The test module follows. It is test-only and not part of the patch.
// SPDX-License-Identifier: GPL-2.0
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <asm/unwind.h>
#include <asm/loongarch.h>
#include <asm/inst.h>
#include <asm/setup.h>
static int __init unwind_boundary_init(void)
{
unsigned long base;
int i;
asm volatile("csrrd %0, %1" : "=r"(base) : "i"(LOONGARCH_CSR_EENTRY));
for (i = -1; i <= 1; i++) {
struct unwind_state state = {
.task = current,
.sp = (unsigned long)__builtin_frame_address(0),
.pc = 0,
.stack_info.type = STACK_TYPE_TASK,
};
bool ok;
state.ra = base + (EXCCODE_INT_END + i) * VECSIZE +
0x154 + LOONGARCH_INSN_SIZE;
ok = unwind_next_frame(&state);
pr_info("unwind: vec=%d ok=%d pc=%px error=%d\n",
EXCCODE_INT_END + i, ok,
(void *)state.pc, state.error);
}
return -EINVAL; /* Fail init intentionally so the module is discarded. */
}
module_init(unwind_boundary_init);
MODULE_LICENSE("GPL");
Thanks,
hanbings