Repository navigation
arm64: invalidate the local TLB when TG0 changes - #1509
DjDeveloperr merged 1 commit into
Conversation
cpu_set_mm_tg0() switches TCR_EL1.TG0 between the granules of the outgoing and incoming address spaces and relies on ASIDs to keep their TLB entries apart. The architecture does not allow that: every TCR_EL1 field other than A1 and a set EPDx is permitted to be cached in a TLB, so entries walked under the old granule can still be used, or match alongside new ones, after the change. On an M4 Pro (T6040) booted with user4k=on this shows up as TLB conflict aborts (FSC 0x30) in native 16K tasks whenever 4K tasks (Wine, FEX) run on the same CPUs. EL0 aborts are silent SIGKILLs; one uaccess in kernel_wait4() oopsed. With debug.exception-trace=1, a 120 s run of one 4K task per CPU sleeping 20 us in a loop beside a gcc loop killed 3 of 920 compilers and two unrelated processes, all with "TLB conflict abort". Builds without any 4K task never saw one. With the fix, a kernel build on the same machine with 40 4K tasks running lost no compiler and logged no TLB conflict abort; the same build on the unfixed kernel with one 4K Wine tree running lost 8 compilers, all to TLB conflict aborts. The 120 s stress above was not rerun on the fixed kernel. Invalidate this CPU's TLB after changing TG0. Every caller has already installed reserved_pg_dir in TTBR0, so nothing is walked between the TCR write and the TLBI. The cost is paid only on a granule transition. Fixes: e46dbf8 ("arm64: support smaller userspace translation granules per mm") Signed-off-by: Jonathan Goyvaerts <[email protected]> Assisted-by: LLM
RyanTheTide
left a comment
There was a problem hiding this comment.
The ordering is right: TCR write, ISB, then the local TLBI with reserved_pg_dir still in TTBR0. We will run a mixed 4K/16K build on our M4 Pro and A18 Pro with exception tracing on, plus a sched pipe benchmark for the switch cost, and report back.
|
Interim results. Kernel: integration e661192 plus this PR and the others in this round, booted with
This load didn't trigger the abort on the production kernel either, so these runs show no regression but don't reproduce the bug. 3 s2idle cycles were clean on both Macs. The M2 and A18 Pro runs are still queued. |
|
M2 results, same setup as the table above (one 4K task per CPU, AT_PAGESZ 4096, beside a 90 s gcc loop):
3 s2idle cycles with the PR were clean too. That leaves the A18 Pro run. |
DjDeveloperr
left a comment
There was a problem hiding this comment.
I reviewed this against every per-mm TG0 caller: cpu_do_switch_mm(), the SW-PAN path in context.c, __switch_mm(&init_mm) and cpu_uninstall_idmap(). The fix is correct. TG0 is one of the TCR fields a TLB may cache, so maintenance is needed after it changes. Every caller has a synchronised reserved_pg_dir in TTBR0, so nothing new is allocated before the TLBI. Local VMALLE1 is the right scope: TG0 is per-CPU, and CnP sharing implies the same mm and therefore the same granule. The flush runs only on a real granule change, under CONFIG_ARM64_USER4K_EXPERIMENTAL. Approving; thanks also to @RyanTheTide for the M4 Pro, M1 Pro and M2 runs.
Optional follow-ups, none blocking:
- With TTBR0 already reserved, the TLBI could also go before the TCR write, as
cpu_install_idmap()does. That closes a few-instruction window: exec'sactivate_mm()runs with IRQs on, so an IRQ-time uaccess (e.g. a perf user callchain) could hit an old-granule entry under the new TG0 before the flush. - Skipping the TG0 switch for
init_mm, whose TTBR0 stays reserved, would save two flushes on a 4K → kthread → 4K sequence. - The comment could say EL0/uaccess "can take" conflict aborts and describe what was observed, instead of stating that ASIDs never separate them.
- Pre-existing and separate:
cpu_do_resumemasks T0SZ but restores the saved TG0 while the idmap is live. Masking TG0 the same way would be worth a follow-up.
A sched-pipe number for a 4K/16K task pair would still be welcome. The A18 Pro run continues after merge.
b08fca1
into
aurora-silicon:integration/aurora
cpu_set_mm_tg0()switches TCR_EL1.TG0 between the outgoing and incoming user granules and relies on ASIDs to keep their TLB entries apart. TG0 is one of the TCR_EL1 fields the architecture lets a TLB cache, so entries walked under the old granule can still be used after the switch. Withuser4k=onon an M4 Pro (J616s) this shows up as TLB conflict aborts (FSC 0x30) in native 16K tasks whenever 4K tasks (Wine, FEX) run on the same CPUs: silent SIGKILLs at EL0, and one oops in a uaccess.This invalidates the local TLB after the TG0 write. Every caller has already installed
reserved_pg_dirin TTBR0 with preemption off, so nothing is walked between the TCR write and the TLBI, and the cost is paid only on a granule transition.Testing (J616s, M4 Pro):
debug.exception-trace=1, a kernel build beside a 4K Wine tree lost 8 compilers, all to "TLB conflict abort"; a 120 s run of one 4K task per CPU beside a gcc loop killed 3 of 920 compilers and two unrelated processes.arch/arm64/mm/,kernel/sched/core.o,kernel/fork.o, W=1, no new warnings); checkpatch clean.Fixes: e46dbf8 ("arm64: support smaller userspace translation granules per mm")
🤖 Generated with Claude Code