arm64: Missing barriers in kernel page-table updates
Subsystems (2)
cover arm64: Missing barriers in kernel page-table updates
Hi folks,
Three fixes for kernel mappings that are made valid and then used on the same CPU before an ISB, so the access can take a translation fault.
Tested on QEMU. The runtime changes to the linear map that patch 3 covers were not exercised, since they require a CPU with BBML3.
Based on Linux 7.4-rc1 (6628e7d4c6b25).
Cheers,
/fuad
Fuad Tabba (3):
arm64: pi: Add an ISB after mapping the FDT in the initial ID map
arm64: mm: Add an ISB after remapping the KPTI page-table slot
arm64: mm: Don't defer the barriers when installing a fixmap entry
arch/arm64/kernel/pi/map_kernel.c | 1 + arch/arm64/mm/fixmap.c | 4 +++- arch/arm64/mm/proc.S | 1 + 3 files changed, 5 insertions(+), 1 deletion(-)
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
Signature · 1 line
2.47.3
PATCH 1/3 arm64: pi: Add an ISB after mapping the FDT in the initial ID map
map_fdt() maps the FDT into the live initial ID map with only a DSB before the caller reads it, so the read can fault on a translation made speculatively before the mapping was written. This early in boot, VBAR_EL1 doesn't yet point at the kernel's vectors, and the kernel fails to boot.
Add an ISB after the DSB.
--- arch/arm64/kernel/pi/map_kernel.c | 1 + 1 file changed, 1 insertion(+)
arch/arm64/kernel/pi/map_kernel.c +1 -0
@@ -211,6 +211,7 @@ static void *__init map_fdt(phys_addr_t fdt) fdt,PAGE_KERNEL ,IDMAP_ROOT_LEVEL , (pte_t *)init_idmap_pg_dir, false, 0);dsb (ishst); +isb ();return (void *)fdt; }
Signature · 1 line
2.47.3
Re: [PATCH 1/3] arm64: pi: Add an ISB after mapping the FDT in the initial ID map
Quoted 11 lines · Fuad Tabba
map_fdt()maps the FDT into the live initial ID map with only a DSB before the caller reads it, so the read can fault on a translation made speculatively before the mapping was written. This early in boot,VBAR_EL1doesn't yet point at the kernel's vectors, and the kernel fails to boot.Add an ISB after the DSB.
Fixes 84b04d3e6bdbc ("arm64: kernel: Create initial ID map from C code")Signed-off-by Fuad Tabba <[email protected]>
I did a quick audit of all the other DSBs without ISBs in the pi code and I think the rest are all correct because they are not synchronising stores to _live_ tables so there can't have been any speculation with the old invalid value.
Quoted 16 lines
--- arch/arm64/kernel/pi/map_kernel.c | 1 + 1 file changed, 1 insertion(+)arch/arm64/kernel/pi/map_kernel.c +1
-0@@ -211,6 +211,7 @@ static void *__init map_fdt(phys_addr_t fdt) fdt,PAGE_KERNEL ,IDMAP_ROOT_LEVEL , (pte_t *)init_idmap_pg_dir, false, 0);dsb (ishst); +isb ();return (void *)fdt; }
PATCH 2/3 arm64: mm: Add an ISB after remapping the KPTI page-table slot
In reply to Fuad Tabbakpti_map_pgtbl remaps a temporary slot with break-before-make, and kpti_mk_tbl_ng then reads the page tables through it. The make ends in a DSB alone, so the reads can fault on a translation of the slot made speculatively before the new entry was written. With the temporary page tables in TTBR1 the kernel's vectors are unmapped, and the boot hangs.
Add an ISB after the DSB.
--- arch/arm64/mm/proc.S | 1 + 1 file changed, 1 insertion(+)
arch/arm64/mm/proc.S +1 -0
@@ -262,6 +262,7 @@ SYM_FUNC_ALIAS(__pi_idmap_cpu_replace_ttbr1, idmap_cpu_replace_ttbr1) orr pte, pte, pte_flags str pte, [temp_pte, #8 * (\level + 2)] dsb nshst + isb .endm /*
Signature · 1 line
2.47.3
Re: [PATCH 2/3] arm64: mm: Add an ISB after remapping the KPTI page-table slot
Quoted 7 lines · Fuad Tabba
kpti_map_pgtblremaps a temporary slot with break-before-make, andkpti_mk_tbl_ngthen reads the page tables through it. The make ends in a DSB alone, so the reads can fault on a translation of the slot made speculatively before the new entry was written. With the temporary page tables in TTBR1 the kernel's vectors are unmapped, and the boot hangs.Add an ISB after the DSB.
Quoted 20 lines
Fixes 47546a1912fc4 ("arm64: mm: install KPTI nG mappings with MMU enabled")Signed-off-by Fuad Tabba <[email protected]>--- arch/arm64/mm/proc.S | 1 + 1 file changed, 1 insertion(+)arch/arm64/mm/proc.S +1
-0@@ -262,6 +262,7 @@ SYM_FUNC_ALIAS(__pi_idmap_cpu_replace_ttbr1, idmap_cpu_replace_ttbr1) orr pte, pte, pte_flags str pte, [temp_pte, #8 * (\level + 2)] dsb nshst + isb .endm /*
PATCH 3/3 arm64: mm: Don't defer the barriers when installing a fixmap entry
In reply to Fuad TabbaIn lazy MMU mode, __set_pte(), which __set_fixmap() uses, only queues its DSB and ISB, but a caller can dereference the slot as soon as __set_fixmap() returns. On a CPU with BBML3, runtime changes to the linear map are made in lazy MMU mode. With 4K pages and 39-bit VAs, for example, such a change can update a top-level entry, which set_swapper_pgd() writes through FIX_PGD. That store can fault, and the kernel then warns about a spurious translation fault. When kgdb stops a CPU in lazy MMU mode, its breakpoint writes through FIX_TEXT_POKE0 can fault the same way, and fail.
Emit the barriers directly in __set_fixmap().
--- arch/arm64/mm/fixmap.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
arch/arm64/mm/fixmap.c +3 -1
@@ -127,7 +127,9 @@ void __set_fixmap(enum fixed_addresses idx, ptep =fixmap_pte (addr);if (pgprot_val (flags)) { -__set_pte (ptep,pfn_pte (phys >>PAGE_SHIFT , 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.47.3
Re: [PATCH 3/3] arm64: mm: Don't defer the barriers when installing a fixmap entry
Quoted 10 lines · Fuad Tabba
In lazy MMU mode,
__set_pte(), which__set_fixmap()uses, only queues its DSB and ISB, but a caller can dereference the slot as soon as__set_fixmap()returns. On a CPU with BBML3, runtime changes to the linear map are made in lazy MMU mode. With 4K pages and 39-bit VAs, for example, such a change can update a top-level entry, whichset_swapper_pgd()writes throughFIX_PGD. That store can fault, and the kernel then warns about a spurious translation fault. When kgdb stops a CPU in lazy MMU mode, its breakpoint writes throughFIX_TEXT_POKE0can fault the same way, and fail.
Quoted 3 lines
Emit the barriers directly in
__set_fixmap().Fixes 5fdd05efa1cd3 ("arm64/mm: Batch barriers when updating kernel mappings")
My memory might be betraying me, but I think this change went in before BBML3 (BBML2_NOABORT)? In which case, the bug is only exposed by the latter series?
Quoted 10 lines
Signed-off-by Fuad Tabba <[email protected]>--->
arch/arm64/mm/fixmap.c| 4 +++-1 file changed, 3 insertions(+), 1 deletion(-)arch/arm64/mm/fixmap.c +0
-0
arch/arm64/mm/fixmap.c void __set_fixmap(enum fixed_addresses idx, · line 132
Quoted 7 lines
arch/arm64/mm/fixmap.c +3
-1@@ -127,7 +127,9 @@ void __set_fixmap(enum fixed_addresses idx, ptep =fixmap_pte (addr);if (pgprot_val (flags)) { -__set_pte (ptep,pfn_pte (phys >>PAGE_SHIFT , 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 ();
I wonder if it would be clearer to wrap __set_ptes():
lazy_mmu_mode_pause()__set_ptes()lazy_mmu_mode_resume()
I notice that ptep_try_set() is also using emit_pte_barriers() directly, but that feels like a layering violation to me.
Thanks,
Ryan
Quoted 3 lines
arch/arm64/mm/fixmap.c +0
-0}else {__pte_clear (&init_mm, addr, ptep);flush_tlb_kernel_range (addr, addr+PAGE_SIZE );
Re: [PATCH 3/3] arm64: mm: Don't defer the barriers when installing a fixmap entry
Hi Ryan,
On Sat, 21 Nov 2026 11:01:49 +0000, Ryan Roberts <[email protected]> wrote:
[...]
Quoted 10 lines
Quoted 7 lines
kernel then warns about a spurious translation fault. When kgdb stops a CPU in lazy MMU mode, its breakpoint writes through
FIX_TEXT_POKE0can fault the same way, and fail.Emit the barriers directly in
__set_fixmap().Fixes 5fdd05efa1cd3 ("arm64/mm: Batch barriers when updating kernel mappings")My memory might be betraying me, but I think this change went in before BBML3 (
BBML2_NOABORT)? In which case, the bug is only exposed by the latter series?
It did, and the set_swapper_pgd() path is only exposed by the later series. The tag is for the kgdb path, which doesn't depend on BBML3. When kgdb is entered from a breakpoint in process context, in_interrupt() is false, so in lazy MMU mode the barriers for its FIX_TEXT_POKE0 mapping are still deferred.
[...]
Quoted 1 line
Quoted 1 line
arch/arm64/mm/fixmap.c +0
-0
[...]
Quoted 11 lines
arch/arm64/mm/fixmap.c void __set_fixmap(enum fixed_addresses idx, · line 132
Quoted 8 lines
arch/arm64/mm/fixmap.c +3
-1@@ -127,7 +127,9 @@ void __set_fixmap(enum fixed_addresses idx, ptep =fixmap_pte (addr);if (pgprot_val (flags)) { -__set_pte (ptep,pfn_pte (phys >>PAGE_SHIFT , 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 ();I wonder if it would be clearer to wrap
__set_ptes():
Quoted 3 lines
lazy_mmu_mode_pause()__set_ptes()lazy_mmu_mode_resume()
That would work. However, there's the stable backport. 6.18 has the bug, but lazy_mmu_mode_pause() was added in 7.0, so 6.18.y would need its own patch. This patch applies to 6.18 unchanged.
I'm happy with either, or with a followup patch in the same series.
Cheers,
/fuad
Ryan Roberts
Reviewed-byRe: [PATCH 3/3] arm64: mm: Don't defer the barriers when installing a fixmap entry
Quoted 12 lines · Fuad Tabba
Hi Ryan,
On Sat, 21 Nov 2026 11:01:49 +0000, Ryan Roberts <[email protected]> wrote:
[...]Quoted 8 lines
Quoted 7 lines
kernel then warns about a spurious translation fault. When kgdb stops a CPU in lazy MMU mode, its breakpoint writes through
FIX_TEXT_POKE0can fault the same way, and fail.Emit the barriers directly in
__set_fixmap().Fixes 5fdd05efa1cd3 ("arm64/mm: Batch barriers when updating kernel mappings")
Quoted 8 lines
Quoted 2 lines
My memory might be betraying me, but I think this change went in before BBML3 (
BBML2_NOABORT)? In which case, the bug is only exposed by the latter series?It did, and the
set_swapper_pgd()path is only exposed by the later series. The tag is for the kgdb path, which doesn't depend on BBML3. When kgdb is entered from a breakpoint in process context,in_interrupt()is false, so in lazy MMU mode the barriers for itsFIX_TEXT_POKE0mapping are still deferred.
ACK
Quoted 19 lines
[...]
Quoted 1 line
Quoted 1 line
arch/arm64/mm/fixmap.c +0
-0[...]
Quoted 11 lines
arch/arm64/mm/fixmap.c void __set_fixmap(enum fixed_addresses idx, · line 132
Quoted 8 lines
arch/arm64/mm/fixmap.c +3
-1@@ -127,7 +127,9 @@ void __set_fixmap(enum fixed_addresses idx, ptep =fixmap_pte (addr);if (pgprot_val (flags)) { -__set_pte (ptep,pfn_pte (phys >>PAGE_SHIFT , 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 ();I wonder if it would be clearer to wrap
__set_ptes():Quoted 3 lines
lazy_mmu_mode_pause()__set_ptes()lazy_mmu_mode_resume()
Quoted 5 lines
That would work. However, there's the stable backport. 6.18 has the bug, but
lazy_mmu_mode_pause()was added in 7.0, so 6.18.y would need its own patch. This patch applies to 6.18 unchanged.I'm happy with either, or with a followup patch in the same series.
I'd personally prefer to use pause/resume since it's makes it very clear what's going on and avoids the layer violation. Perhaps your approach as the back portable one, then pause/resume as follow up (along with ptep_try_set() tidy up)? Others might think differently though...
Regardless:
Quoted 3 lines
Cheers,
/fuad
Kevin Brodsky
Reviewed-byRe: [PATCH 3/3] arm64: mm: Don't defer the barriers when installing a fixmap entry
Quoted 18 lines · Ryan Roberts
Quoted 18 lines
Quoted 11 lines
arch/arm64/mm/fixmap.c void __set_fixmap(enum fixed_addresses idx, · line 132
Quoted 9 lines
arch/arm64/mm/fixmap.c +3
-1@@ -127,7 +127,9 @@ void __set_fixmap(enum fixed_addresses idx, ptep =fixmap_pte (addr);if (pgprot_val (flags)) { -__set_pte (ptep,pfn_pte (phys >>PAGE_SHIFT , 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 ();I wonder if it would be clearer to wrap
__set_ptes():Quoted 3 lines
lazy_mmu_mode_pause()__set_ptes()lazy_mmu_mode_resume()That would work. However, there's the stable backport. 6.18 has the bug, but
lazy_mmu_mode_pause()was added in 7.0, so 6.18.y would need its own patch. This patch applies to 6.18 unchanged.
Quoted 5 lines
Quoted 1 line
I'm happy with either, or with a followup patch in the same series.
I'd personally prefer to use pause/resume since it's makes it very clear what's going on and avoids the layer violation. Perhaps your approach as the back portable one, then pause/resume as follow up (along with
ptep_try_set()tidy up)? Others might think differently though...
Using pause/resume does work, but it is a bigger hammer than necessary. If other features make use of lazy MMU mode, then we may end up paying extra cost by leaving/re-entering the mode.
For instance, with page table hardening [1], using the pause/resume approach means 4 extra ISBs:
- 1 when pause() leaves lazy MMU mode: restoring default kpkeys context
- 2 in __set_pte(): switching to page table update kpkeys context, and then back to default
- 1 when resume() enters lazy MMU mode: entering page table update kpkeys context
An explicit __set_pte_nosync() + emit_pte_barriers() achieves exactly what we're after (immediately visible PTE update) and doesn't disrupt lazy MMU mode - we don't unnecessarily set TIF_LAZY_MMU_PENDING.
An alternative approach that addresses your point about layer violation is to call arch_flush_lazy_mmu_mode() and leave the __set_pte() call unchanged. This is slightly slower as we still end up setting/clearing the TIF flag in that case. Not sure this is worth it - ultimately we have to be mindful of what lazy MMU mode is doing to ensure this code is correct. I'm happy with the current approach.
- Kevin
Re: [PATCH 3/3] arm64: mm: Don't defer the barriers when installing a fixmap entry
Quoted 37 lines · Kevin Brodsky
Quoted 18 lines · Ryan Roberts
Quoted 18 lines
Quoted 11 lines
arch/arm64/mm/fixmap.c void __set_fixmap(enum fixed_addresses idx, · line 132
Quoted 9 lines
arch/arm64/mm/fixmap.c +3
-1@@ -127,7 +127,9 @@ void __set_fixmap(enum fixed_addresses idx, ptep =fixmap_pte (addr);if (pgprot_val (flags)) { -__set_pte (ptep,pfn_pte (phys >>PAGE_SHIFT , 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 ();I wonder if it would be clearer to wrap
__set_ptes():Quoted 3 lines
lazy_mmu_mode_pause()__set_ptes()lazy_mmu_mode_resume()That would work. However, there's the stable backport. 6.18 has the bug, but
lazy_mmu_mode_pause()was added in 7.0, so 6.18.y would need its own patch. This patch applies to 6.18 unchanged.Quoted 5 lines
Quoted 1 line
I'm happy with either, or with a followup patch in the same series.
I'd personally prefer to use pause/resume since it's makes it very clear what's going on and avoids the layer violation. Perhaps your approach as the back portable one, then pause/resume as follow up (along with
ptep_try_set()tidy up)? Others might think differently though...Using pause/resume does work, but it is a bigger hammer than necessary. If other features make use of lazy MMU mode, then we may end up paying extra cost by leaving/re-entering the mode.
For instance, with page table hardening [1], using the pause/resume approach means 4 extra ISBs:
- 1 whenpause()leaves lazy MMU mode: restoring default kpkeys context
- 2 in__set_pte(): switching to page table update kpkeys context, and then back to default
- 1 whenresume()enters lazy MMU mode: entering page table update kpkeys context
Quoted 3 lines
An explicit
__set_pte_nosync()+emit_pte_barriers()achieves exactly what we're after (immediately visible PTE update) and doesn't disrupt lazy MMU mode - we don't unnecessarily setTIF_LAZY_MMU_PENDING.
It's so ugly to spill this stuff everywhere though.
How about a nested lazy mmu section?
lazy_mmu_mode_enable()__set_ptes()lazy_mmu_mode_disable()
If it's nested the enable() is essentially nop, and the disable is a flush, which emits the barriers. If it's the first enable, we still set TIF_LAZY_MMU_PENDING though...
Quoted 13 lines
An alternative approach that addresses your point about layer violation is to call
arch_flush_lazy_mmu_mode()and leave the__set_pte()call unchanged. This is slightly slower as we still end up setting/clearing the TIF flag in that case. Not sure this is worth it - ultimately we have to be mindful of what lazy MMU mode is doing to ensure this code is correct. I'm happy with the current approach.Reviewed-by Kevin Brodsky <[email protected]>- Kevin
Re: [PATCH 3/3] arm64: mm: Don't defer the barriers when installing a fixmap entry
Quoted 43 lines · Ryan Roberts
Quoted 35 lines · Kevin Brodsky
Quoted 17 lines · Ryan Roberts
Quoted 17 lines
Quoted 10 lines
arch/arm64/mm/fixmap.c void __set_fixmap(enum fixed_addresses idx, · line 132
Quoted 8 lines
arch/arm64/mm/fixmap.c +3
-1@@ -127,7 +127,9 @@ void __set_fixmap(enum fixed_addresses idx, ptep =fixmap_pte (addr);if (pgprot_val (flags)) { -__set_pte (ptep,pfn_pte (phys >>PAGE_SHIFT , 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 ();I wonder if it would be clearer to wrap
__set_ptes():Quoted 3 lines
lazy_mmu_mode_pause()__set_ptes()lazy_mmu_mode_resume()That would work. However, there's the stable backport. 6.18 has the bug, but
lazy_mmu_mode_pause()was added in 7.0, so 6.18.y would need its own patch. This patch applies to 6.18 unchanged.Quoted 5 lines
Quoted 1 line
I'm happy with either, or with a followup patch in the same series.
I'd personally prefer to use pause/resume since it's makes it very clear what's going on and avoids the layer violation. Perhaps your approach as the back portable one, then pause/resume as follow up (along with
ptep_try_set()tidy up)? Others might think differently though...Using pause/resume does work, but it is a bigger hammer than necessary. If other features make use of lazy MMU mode, then we may end up paying extra cost by leaving/re-entering the mode.
For instance, with page table hardening [1], using the pause/resume approach means 4 extra ISBs:
- 1 whenpause()leaves lazy MMU mode: restoring default kpkeys context
- 2 in__set_pte(): switching to page table update kpkeys context, and then back to default
- 1 whenresume()enters lazy MMU mode: entering page table update kpkeys contextQuoted 3 lines
An explicit
__set_pte_nosync()+emit_pte_barriers()achieves exactly what we're after (immediately visible PTE update) and doesn't disrupt lazy MMU mode - we don't unnecessarily setTIF_LAZY_MMU_PENDING.It's so ugly to spill this stuff everywhere though.
How about a nested lazy mmu section?
Quoted 7 lines
lazy_mmu_mode_enable()__set_ptes()lazy_mmu_mode_disable()If it's nested the
enable()is essentially nop, and the disable is a flush, which emits the barriers. If it's the first enable, we still setTIF_LAZY_MMU_PENDINGthough...
Yes that's essentially the same as the explicit call to arch_flush_lazy_mmu_mode() I mentioned below. IMHO that looks pretty obfuscated though - the casual reader will wonder why we're creating a lazy MMU section for setting just one PTE. If we go down this route, I'd argue the explicit call to arch_flush_lazy_mmu_mode() is clearer (and our implementation is safe to call even outside of a lazy MMU section).
- Kevin
Quoted 12 lines
Quoted 12 lines
An alternative approach that addresses your point about layer violation is to call
arch_flush_lazy_mmu_mode()and leave the__set_pte()call unchanged. This is slightly slower as we still end up setting/clearing the TIF flag in that case. Not sure this is worth it - ultimately we have to be mindful of what lazy MMU mode is doing to ensure this code is correct. I'm happy with the current approach.Reviewed-by Kevin Brodsky <[email protected]>- Kevin