Re: [PATCH v2] LoongArch: Fix preemptible warnings in cacheinfo and local TLB flush
From: Tiezhu Yang
Date: Thu Nov 19 2026 - 01:09:45 EST
On 11/19/26 2:05 PM, Tiezhu Yang wrote:
When CONFIG_DEBUG_PREEMPT is enabled on a Non-SMP kernel, calls to
smp_processor_id() (including via current_cpu_data) in preemptible
contexts trigger many "BUG: using smp_processor_id() in preemptible"
warnings during cacheinfo setup and local TLB flushes.
...
void local_flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
unsigned long end)
{
struct mm_struct *mm = vma->vm_mm;
- int cpu = smp_processor_id();
+ int cpu = raw_smp_processor_id();
if (asid_valid(mm, cpu)) {
unsigned long size, flags;
@@ -67,9 +61,9 @@ void local_flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
start = round_down(start, PAGE_SIZE << 1);
end = round_up(end, PAGE_SIZE << 1);
size = (end - start) >> (PAGE_SHIFT + 1);
- if (size <= (current_cpu_data.tlbsizestlbsets ?
- current_cpu_data.tlbsize / 8 :
- current_cpu_data.tlbsize / 2)) {
+ if (size <= (cpu_data[cpu].tlbsizestlbsets ?
+ cpu_data[cpu].tlbsize / 8 :
+ cpu_data[cpu].tlbsize / 2)) {
int asid = cpu_asid(cpu, mm);
while (start < end) {
I tested this v2 patch on a !CONFIG_SMP && CONFIG_PREEMPT
&& CONFIG_DEBUG_PREEMPT kernel. While it eliminates the
"BUG: using smp_processor_id() in preemptible" warnings,
removing preemption protection introduces a real ASID
race on UP preemptible kernels.
On !CONFIG_SMP, flush_tlb_{mm,range,page}() are directly
defined as local_flush_tlb_{mm,range,page}() in the arch
asm/tlbflush.h, bypassing the preempt_disable() protection
in arch/loongarch/kernel/smp.c.
Although task migration is impossible on UP, preemption
can still occur between asid_valid() and local_irq_save(),
allowing other tasks to trigger an ASID generation rollover
and modify both cpu_context(cpu, mm) and hardware CSR_ASID
mid-operation.
To verify this, I added a small debug check with mdelay(5)
on top of v2:
diff --git a/arch/loongarch/mm/tlb.c b/arch/loongarch/mm/tlb.c
index 07a6a5e94120..e2b48f08a28a 100644
--- a/arch/loongarch/mm/tlb.c
+++ b/arch/loongarch/mm/tlb.c
@@ -8,6 +8,7 @@
#include <linux/mm.h>
#include <linux/hugetlb.h>
#include <linux/export.h>
+#include <linux/delay.h>
#include <asm/bootinfo.h>
#include <asm/cpu.h>
@@ -56,8 +57,36 @@ void local_flush_tlb_range(struct vm_area_struct *vma, unsigned long start,
if (asid_valid(mm, cpu)) {
unsigned long size, flags;
+ unsigned long mask = cpu_asid_mask(&cpu_data[cpu]);
+ unsigned long old_ctx = cpu_context(cpu, mm);
+ unsigned long old_cache = asid_cache(cpu);
+ unsigned long old_hw_asid = csr_read64(LOONGARCH_CSR_ASID) & mask;
+ unsigned long new_ctx, new_cache, new_hw_asid;
+
+ mdelay(5);
local_irq_save(flags);
+
+ new_ctx = cpu_context(cpu, mm);
+ new_cache = asid_cache(cpu);
+ new_hw_asid = csr_read64(LOONGARCH_CSR_ASID) & mask;
+
+ if (unlikely(!asid_valid(mm, cpu) ||
+ old_ctx != new_ctx ||
+ old_hw_asid != new_hw_asid)) {
+ WARN_ONCE(1,
+ "ASID race!\n"
+ " valid=%d, is_cur_mm=%d\n"
+ " ctx: 0x%lx(asid=0x%lx) -> 0x%lx(asid=0x%lx)\n"
+ " cache: 0x%lx -> 0x%lx\n"
+ " hw_csr_asid: 0x%lx -> 0x%lx\n",
+ asid_valid(mm, cpu), (current->mm == mm),
+ old_ctx, old_ctx & mask,
+ new_ctx, new_ctx & mask,
+ old_cache, new_cache,
+ old_hw_asid, new_hw_asid);
+ }
+
start = round_down(start, PAGE_SIZE << 1);
end = round_up(end, PAGE_SIZE << 1);
size = (end - start) >> (PAGE_SHIFT + 1);
Here is the captured warning during boot:
------------[ cut here ]------------
ASID race!
valid=1, is_cur_mm=1
ctx: 0xbfb(asid=0x3fb) -> 0xc02(asid=0x2)
cache: 0xbff -> 0xc04
hw_csr_asid: 0x3fb -> 0x2
WARNING: arch/loongarch/mm/tlb.c:77 at local_flush_tlb_range+0x280/0x2f0, CPU#0: abrt-server/810
...
CPU: 0 UID: 0 PID: 810 Comm: abrt-server Not tainted 7.4.0-rc2+ #2 PREEMPT(full)
...
Call Trace:
[<9000000000d33de8>] show_stack+0xf8/0x1c0
[<9000000000d2fdac>] dump_stack_lvl+0x4c/0x78
[<9000000000d5aac4>] __warn+0x94/0x130
[<900000000241f888>] __report_bug+0x88/0x1b0
[<900000000241fa84>] report_bug+0x34/0xa0
[<90000000024568f8>] do_bp+0x2e8/0x370
[<9000000000d31f64>] handle_bp+0x124/0x1e0
[<9000000000d45610>] local_flush_tlb_range+0x280/0x2f0
[<9000000000febdc0>] tlb_flush+0x50/0xa0
[<9000000000fec838>] tlb_finish_mmu+0x58/0x160
[<9000000000fee584>] do_mprotect_pkey.constprop.0+0x3f4/0x450
[<9000000000fee5e8>] sys_mprotect+0x8/0x20
[<9000000002456eb8>] do_syscall+0x1b8/0x380
[<9000000000d314e4>] handle_syscall+0xc4/0x164
---[ end trace 0000000000000000 ]---
After switching back to get_cpu() / put_cpu() (as in v1), this ASID
race completely disappears under the exact same test.
So I think we should keep get_cpu() / put_cpu() in
local_flush_tlb_{mm,range,page}() (as in v1) to ensure preemption
safety, especially for UP preemptible kernels.
Thanks,
Tiezhu