arm64: fixmap: fix missing break-before-make in __set_fixmap()
Subsystems (2)
PATCH v2 1/1 arm64: fixmap: fix missing break-before-make in __set_fixmap()
__set_fixmap() installs a new PTE with __set_pte() but never invalidates the TLB; only the clear path calls flush_tlb_kernel_range(). The dsb(ishst)/isb that __set_pte() emits makes the new PTE visible to the table walker but does not invalidate stale TLB entries. Overwriting a still-valid slot therefore leaves the old translation cached and violates break-before-make: accesses may keep hitting the old output address, and changing the attributes of a cached entry can trigger TLB conflict aborts or CONSTRAINED UNPREDICTABLE behaviour.
Two in-tree callers overwrite a valid FIX_EARLYCON_MEM_BASE slot:
- earlycon: earlycon_map() maps the UART before match->setup() runs. If setup fails, con->write is unset, or the DT reg-io-width is unsupported, the slot stays valid but no console is registered. The console_is_registered() guard only blocks successful registrations, so a later attempt (e.g. a failed DT earlycon followed by earlycon= on the command line) remaps the slot to a different UART.
- samsung_tty: apple_s5l_early_console_setup() overrides the just-installed entry from PROT_DEVICE_nGnRE (FIXMAP_PAGE_IO) to PROT_DEVICE_nGnRnE on the normal successful boot path.
Both are valid-to-valid changes (different output address or memory type), which arm64 requires to be break-before-make.
Fix this in __set_fixmap() by invalidating any existing valid mapping up front: clear the old PTE and flush the TLB, then install the new PTE only when setting. The old translation is gone before the new one is created, so the two can never coexist in the TLB. A first-time mapping (old entry invalid) skips the invalidation and pays no extra cost, and clearing a slot is handled by the same step.
A clear (!flags) always clears and flushes, even when the PTE is already invalid. An invalid PTE does not imply an invalid TLB: another CPU may have cleared the PTE but not yet completed its TLB flush, leaving a stale entry cached locally. The unconditional flush preserves clear_fixmap()'s TLB shootdown guarantee, which callers such as the KVM VNCR unmap path rely on when racing with a remote invalidation.
This belongs in the callee, as on x86: __set_fixmap() there calls set_pte_vaddr(), which flushes the TLB unconditionally after writing the PTE. x86 can flush after the write because it has no break-before-make requirement; arm64 must invalidate and flush before installing the new valid entry to avoid TLB conflict aborts.
---
v2:
- Rebase to arm64 for-next/fixes
- Fix comments from sashiko: always clear and flush on the clear path (!flags), even when the PTE is already invalid.
--- arch/arm64/mm/fixmap.c | 42 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-)
arch/arm64/mm/fixmap.c +39 -3
@@ -126,13 +126,49 @@ void __set_fixmap(enum fixed_addresses idx, ptep =fixmap_pte (addr); +/* +* Break-before-make: when the slot still holds a valid mapping +* (e.g. earlycon re-mapping FIX_EARLYCON_MEM_BASE), invalidate the +* old entry and flush the TLB entries caching it before a new +* mapping is installed, so old and new translations never coexist. +* +* A clear (!flags) always flushes even if the PTE is already invalid: +* a remote CPU may have cleared the PTE but not yet completed its TLB +* flush, leaving a stale entry cached locally. Take KVM VNCR unmap as +* example: +* When CPU1 wins atomic_xchg_relaxed(&vt->cpu, -1) in unmap_l1_vncr(), +* CPU0 calls clear_fixmap() on its own slot so it cannot finish +* vcpu_put() before the local TLB entry is invalidated: +* CPU0 CPU1 +* invalidate_vncr() +* unmap_l1_vncr() +* atomic_xchg_relaxed(&vt->cpu, -1) +* clear_fixmap(vncr_fixmap(0)) +* __pte_clear(..., ptep) +* <delayed before TLB flush> +* kvm_vcpu_put_hw_mmu() +* this_cpu_reset_vncr_fixmap() +* unmap_l1_vncr() == -1 +* clear_fixmap(vncr_fixmap(0)) +* !pte_valid(__ptep_get(ptep)) +* <skips flush_tlb_kernel_range()> +* <stale TLB entry still live on CPU0> +* If another nested vCPU then schedules on CPU0 and calls +* kvm_map_l1_vncr() before CPU1 finishes flush_tlb_kernel_range(), +* __set_fixmap() also sees !pte_valid(__ptep_get(ptep)) and installs +* the new PTE while the old translation is still cached in CPU0's TLB. +* Unconditionally clearing and flushing when !pgprot_val(flags) +* preserves clear_fixmap()'s TLB shootdown guarantee. +*/ +if (pte_valid (__ptep_get (ptep)) || !pgprot_val (flags)) { +__pte_clear (&init_mm, addr, ptep); +flush_tlb_kernel_range (addr, addr +PAGE_SIZE ); + } +if (pgprot_val (flags)) {/* The slot must be usable on return, even in lazy MMU mode. */ __set_pte_nosync (ptep,pfn_pte (phys >>PAGE_SHIFT , flags));emit_pte_barriers (); - }else { -__pte_clear (&init_mm, addr, ptep); -flush_tlb_kernel_range (addr, addr+PAGE_SIZE ); } }
Signature · 1 line
2.43.0
Quoted 45 lines · Yadong Qi
__set_fixmap()installs a new PTE with__set_pte()but never invalidates the TLB; only the clear path callsflush_tlb_kernel_range(). The dsb(ishst)/isb that__set_pte()emits makes the new PTE visible to the table walker but does not invalidate stale TLB entries. Overwriting a still-valid slot therefore leaves the old translation cached and violates break-before-make: accesses may keep hitting the old output address, and changing the attributes of a cached entry can trigger TLB conflict aborts or CONSTRAINED UNPREDICTABLE behaviour.Two in-tree callers overwrite a valid
FIX_EARLYCON_MEM_BASEslot:- earlycon:
earlycon_map()maps the UART beforematch->setup()runs. If setup fails,con->writeis unset, or the DT reg-io-width is unsupported, the slot stays valid but no console is registered. Theconsole_is_registered()guard only blocks successful registrations, so a later attempt (e.g. a failed DT earlycon followed by earlycon= on the command line) remaps the slot to a different UART.-
samsung_tty:apple_s5l_early_console_setup()overrides the just-installed entry fromPROT_DEVICE_nGnRE(FIXMAP_PAGE_IO) toPROT_DEVICE_nGnRnEon the normal successful boot path.Both are valid-to-valid changes (different output address or memory type), which arm64 requires to be break-before-make.
Fix this in
__set_fixmap()by invalidating any existing valid mapping up front: clear the old PTE and flush the TLB, then install the new PTE only when setting. The old translation is gone before the new one is created, so the two can never coexist in the TLB. A first-time mapping (old entry invalid) skips the invalidation and pays no extra cost, and clearing a slot is handled by the same step.A clear (!flags) always clears and flushes, even when the PTE is already invalid. An invalid PTE does not imply an invalid TLB: another CPU may have cleared the PTE but not yet completed its TLB flush, leaving a stale entry cached locally. The unconditional flush preserves clear_fixmap()'s TLB shootdown guarantee, which callers such as the KVM VNCR unmap path rely on when racing with a remote invalidation.
This belongs in the callee, as on x86:
__set_fixmap()there callsset_pte_vaddr(), which flushes the TLB unconditionally after writing the PTE. x86 can flush after the write because it has no break-before-make requirement; arm64 must invalidate and flush before installing the new valid entry to avoid TLB conflict aborts.
Quoted 4 lines
Fixes 9aac5887595b ("tty/serial: add generic serial earlycon")Reported-by Huang Ying <[email protected]>Signed-off-by Yadong Qi <[email protected]>
Nit: commit 9aac5 added earlycon_map() -> set_fixmap_io() but I think a a few other commits later caused overriding this fixmap entry. I think set_fixmap_io() was underspecified, not requiring a clear_fixmap() after use but they inherited the x86 behaviour already which did the flushing. Anyway, I think a better tag would be (to make it arm64-specific issue):
Quoted 32 lines
arch/arm64/mm/fixmap.c +24
-0@@ -126,13 +126,49 @@ void __set_fixmap(enum fixed_addresses idx, ptep =fixmap_pte (addr); +/* +* Break-before-make: when the slot still holds a valid mapping +* (e.g. earlycon re-mapping FIX_EARLYCON_MEM_BASE), invalidate the +* old entry and flush the TLB entries caching it before a new +* mapping is installed, so old and new translations never coexist. +* +* A clear (!flags) always flushes even if the PTE is already invalid: +* a remote CPU may have cleared the PTE but not yet completed its TLB +* flush, leaving a stale entry cached locally. Take KVM VNCR unmap as +* example: +* When CPU1 wins atomic_xchg_relaxed(&vt->cpu, -1) in unmap_l1_vncr(), +* CPU0 calls clear_fixmap() on its own slot so it cannot finish +* vcpu_put() before the local TLB entry is invalidated: +* CPU0 CPU1 +* invalidate_vncr() +* unmap_l1_vncr() +* atomic_xchg_relaxed(&vt->cpu, -1) +* clear_fixmap(vncr_fixmap(0)) +* __pte_clear(..., ptep) +* <delayed before TLB flush> +* kvm_vcpu_put_hw_mmu() +* this_cpu_reset_vncr_fixmap() +* unmap_l1_vncr() == -1 +* clear_fixmap(vncr_fixmap(0))
arch/arm64/mm/fixmap.c void __set_fixmap(enum fixed_addresses idx, · line 162
Quoted 10 lines
arch/arm64/mm/fixmap.c +10
-0+* !pte_valid(__ptep_get(ptep)) +* <skips flush_tlb_kernel_range()> +* <stale TLB entry still live on CPU0> +* If another nested vCPU then schedules on CPU0 and calls +* kvm_map_l1_vncr() before CPU1 finishes flush_tlb_kernel_range(), +* __set_fixmap() also sees !pte_valid(__ptep_get(ptep)) and installs +* the new PTE while the old translation is still cached in CPU0's TLB. +* Unconditionally clearing and flushing when !pgprot_val(flags) +* preserves clear_fixmap()'s TLB shootdown guarantee. +*/
There's too much text here. Move it in the commit log if you want and just write a few lines in the code, something like:
/*
* Break-before-make if the slot holds a valid mapping. Always
* flush on clear since another CPU may have cleared the pte
* without completing its TLBI yet.
*/
arch/arm64/mm/fixmap.c void __set_fixmap(enum fixed_addresses idx, · line 167
Quoted 5 lines
arch/arm64/mm/fixmap.c +5
-0+if (pte_valid (__ptep_get (ptep)) || !pgprot_val (flags)) { +__pte_clear (&init_mm, addr, ptep); +flush_tlb_kernel_range (addr, addr +PAGE_SIZE ); + } +
With these changes,