Skip to contentSkip to conversation

xtensa: align: validate user access in fast_load_store under MMU

PATCH Superseded Ricky Ringler 2 participants 8 messages Dec 13, 2025 – Dec 15, 2025 Tensilica Xtensa port (xtensa)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xtensa: align: validate user access in fast_load_store under MMU

fast_load_store aligns faulting address then reads two words with l32i. With CONFIG_MMU, bad user access can fault in this path.

Replace arbitrary jump to .Linvalid_instruction with access_ok() to validate aligned address before loads and branch to .Linvalid_instruction on failure.

a0: scratch. a2: sp. Matches existing usage in
Xtensa entry.S.

Testing:
- Built Xtensa with CONFIG_MMU enabled
- objdump before/after comparison and validated code path
- Ran emulated Xtensa device with QEMU and manually triggered unaligned and aligned loads

---
 arch/xtensa/include/asm/asm-uaccess.h | 2 +-
 arch/xtensa/kernel/align.S            | 9 ++++++---
 2 files changed, 7 insertions(+), 4 deletions(-)
arch/xtensa/include/asm/asm-uaccess.h +1 -1
index 7cec869136e3..c5baa134d3d8 100644
@@ -68,7 +68,7 @@
  * 	<aa>	register containing memory address
  * 	<as>	register containing memory size
  * 	<at>	temp register
- * 	<sp>
+ * 	<sp>	unused; ignored
  * 	<error>	label to branch to on error; implies fall-through
  * 		macro on success
  * On Exit:
arch/xtensa/kernel/align.S +6 -3
index ee97edce2300..808f9f843d33 100644
@@ -21,6 +21,9 @@
 #include <asm/asm-offsets.h>
 #include <asm/asmmacro.h>
 #include <asm/processor.h>
+#ifdef CONFIG_MMU
+#include <asm/asm-uaccess.h>
+#endif
 
 #if XCHAL_UNALIGNED_LOAD_EXCEPTION || defined CONFIG_XTENSA_LOAD_STORE
 #define LOAD_EXCEPTION_HANDLER
@@ -178,15 +181,15 @@ ENTRY(fast_load_store)
 	bbsi.l	a4, OP1_SI_BIT + INSN_OP1, .Linvalid_instruction
 
 1:
-	movi	a3, ~3
+	movi    a3, ~3
 	and	a3, a3, a8		# align memory address
 
 	__ssa8	a8
 
 #ifdef CONFIG_MMU
 	/* l32e can't be used here even when it's available. */
-	/* TODO access_ok(a3) could be used here */
-	j	.Linvalid_instruction
+    movi    a5, 8
+    access_ok a3, a5, a0, a2, .Linvalid_instruction
 #endif
 	l32i	a5, a3, 0
 	l32i	a6, a3, 4
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/1 xtensa: mmu: validate user access in fast_load_store with MMU

fast_load_store aligns faulting address then reads two words with l32i. With CONFIG_MMU, bad user access can fault in this path.

Replace arbitrary jump to .Linvalid_instruction with access_ok() to validate aligned address before loads and branch to .Linvalid_instruction on failure.

a0: scratch. a2: sp. Matches existing usage in
Xtensa entry.S.

Testing:
- Built Xtensa with CONFIG_MMU enabled
- objdump before/after comparison and validated code path
- Ran emulated Xtensa device with QEMU and manually triggered unaligned and aligned loads

---
 arch/xtensa/kernel/align.S | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
arch/xtensa/kernel/align.S +7 -3
index ee97edce2300..5205c6ebb586 100644
@@ -21,6 +21,9 @@
 #include <asm/asm-offsets.h>
 #include <asm/asmmacro.h>
 #include <asm/processor.h>
+#ifdef CONFIG_MMU
+#include <asm/asm-uaccess.h>
+#endif
 
 #if XCHAL_UNALIGNED_LOAD_EXCEPTION || defined CONFIG_XTENSA_LOAD_STORE
 #define LOAD_EXCEPTION_HANDLER
@@ -178,16 +181,17 @@ ENTRY(fast_load_store)
 	bbsi.l	a4, OP1_SI_BIT + INSN_OP1, .Linvalid_instruction
 
 1:
-	movi	a3, ~3
+	movi    a3, ~3
 	and	a3, a3, a8		# align memory address
 
 	__ssa8	a8
 
 #ifdef CONFIG_MMU
 	/* l32e can't be used here even when it's available. */
-	/* TODO access_ok(a3) could be used here */
-	j	.Linvalid_instruction
+	movi	a5, 8
+	access_ok a3, a5, a0, a2, .Linvalid_instruction
 #endif
+
 	l32i	a5, a3, 0
 	l32i	a6, a3, 4
 	__src_b	a3, a5, a6		# a3 has the data word
Signature · 1 line
2.43.0
maintainer
#2 lore raw

Re: [PATCH] xtensa: mmu: validate user access in fast_load_store with MMU

Hi Ricky,

Quoted 4 lines · Ricky Ringler

fast_load_store aligns faulting address then reads two words with l32i. With CONFIG_MMU, bad user access can fault in this path.

This fault can also happen when accessing memory from the kernel mode.

Quoted 7 lines

Replace arbitrary jump to .Linvalid_instruction with access_ok() to validate aligned address before loads and branch to .Linvalid_instruction on failure.

a0: scratch. a2: sp. Matches existing usage in
Xtensa entry.S.

Quoted 7 lines

Testing:
- Built Xtensa with CONFIG_MMU enabled
- objdump before/after comparison and validated code path
- Ran emulated Xtensa device with QEMU and manually triggered unaligned and aligned loads

fast_load_store is not called for the unaligned access, it's called for the load/store exception. On the real hardware it happens e.g. when l8ui is used to load from the address range mapped to the instruction fetch bus. This doesn't happen on QEMU.

Quoted 20 lines
---
 arch/xtensa/kernel/align.S | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
arch/xtensa/kernel/align.S +3 -0
index ee97edce2300..5205c6ebb586 100644
@@ -21,6 +21,9 @@
 #include <asm/asm-offsets.h>
 #include <asm/asmmacro.h>
 #include <asm/processor.h>
+#ifdef CONFIG_MMU
+#include <asm/asm-uaccess.h>
+#endif
 
 #if XCHAL_UNALIGNED_LOAD_EXCEPTION || defined CONFIG_XTENSA_LOAD_STORE
 #define LOAD_EXCEPTION_HANDLER

arch/xtensa/kernel/align.S ENTRY(fast_load_store) · line 184

Quoted 5 lines
arch/xtensa/kernel/align.S +1 -1
@@ -178,16 +181,17 @@ ENTRY(fast_load_store)
        bbsi.l  a4, OP1_SI_BIT + INSN_OP1, .Linvalid_instruction
 
 1:
-       movi    a3, ~3
+       movi    a3, ~3

There's a formatting change here, but the original formatting is fine.

arch/xtensa/kernel/align.S ENTRY(fast_load_store) · line 192

Quoted 10 lines
arch/xtensa/kernel/align.S +2 -2
        and     a3, a3, a8              # align memory address
 
        __ssa8  a8
 
 #ifdef CONFIG_MMU
        /* l32e can't be used here even when it's available. */
-       /* TODO access_ok(a3) could be used here */
-       j       .Linvalid_instruction
+       movi    a5, 8
+       access_ok a3, a5, a0, a2, .Linvalid_instruction

a0 cannot be used here, it is clobbered by the macro, but the value loaded into it by the code above is used in the code below. a6 would work though.

access_ok only checks address validity for the user mode, but it is also possible to get here from the kernel mode.

New code should use tabs, not spaces, just like the existing code.

Quoted 8 lines
arch/xtensa/kernel/align.S +1 -1
 #endif
+
        l32i    a5, a3, 0
        l32i    a6, a3, 4
        __src_b a3, a5, a6              # a3 has the data word
--

2.43.0

Signature · 2 lines
Thanks.
-- Max
maintainer
#3 lore raw

Re: [PATCH] xtensa: mmu: validate user access in fast_load_store with MMU

Quoted 8 lines · Max Filippov

arch/xtensa/kernel/align.S ENTRY(fast_load_store) · line 192

Quoted 6 lines
 #ifdef CONFIG_MMU
        /* l32e can't be used here even when it's available. */
arch/xtensa/kernel/align.S +2 -2
-       /* TODO access_ok(a3) could be used here */
-       j       .Linvalid_instruction
+       movi    a5, 8
+       access_ok a3, a5, a0, a2, .Linvalid_instruction

New code should use tabs, not spaces, just like the existing code.

Disregard that, the other version of the patch is formatted with spaces, this one is good.

Please add patch version if you send multiple versions of the patch.

Signature · 2 lines
Thanks.
-- Max
#4 lore raw

PATCH v2 1/1 xtensa: align: validate access in fast_load_store

In reply to Ricky Ringler

access_ok() is used only in user mode and
branches to .Linvalid_instruction on fault.
Kernel mode behavior unchanged.

v2:
- Address Max's feedback
- Fixed formatting
- Added user mode branch

Testing:
- Built with CONFIG_MMU enabled
- objdump before/after comparison and validated code path

---
 arch/xtensa/kernel/align.S | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
arch/xtensa/kernel/align.S +9 -2
index ee97edce2300..64e775933e9b 100644
@@ -21,6 +21,9 @@
 #include <asm/asm-offsets.h>
 #include <asm/asmmacro.h>
 #include <asm/processor.h>
+#ifdef CONFIG_MMU
+#include <asm/asm-uaccess.h>
+#endif
 
 #if XCHAL_UNALIGNED_LOAD_EXCEPTION || defined CONFIG_XTENSA_LOAD_STORE
 #define LOAD_EXCEPTION_HANDLER
@@ -185,8 +188,12 @@ ENTRY(fast_load_store)
 
 #ifdef CONFIG_MMU
 	/* l32e can't be used here even when it's available. */
-	/* TODO access_ok(a3) could be used here */
-	j	.Linvalid_instruction
+	rsr	a6, ps
+	bbsi.l	a6, PS_UM_BIT, 1f	# user mode
+	j	.Linvalid_instruction	# kernel mode
+1:
+	movi	a5, 8
+	access_ok a3, a5, a6, a2, .Linvalid_instruction
 #endif
 	l32i	a5, a3, 0
 	l32i	a6, a3, 4
Signature · 1 line
2.43.0
maintainer
#5 lore raw

Re: [PATCH v2] xtensa: align: validate access in fast_load_store

Quoted 9 lines · Ricky Ringler

access_ok() is used only in user mode and
branches to .Linvalid_instruction on fault.
Kernel mode behavior unchanged.

v2:
- Address Max's feedback
- Fixed formatting
- Added user mode branch

Please move the patch change log past the --- marker so that it is dropped when the patch is committed.

Quoted 26 lines

Testing:
- Built with CONFIG_MMU enabled
- objdump before/after comparison and validated code path

---
 arch/xtensa/kernel/align.S | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
arch/xtensa/kernel/align.S +3 -0
index ee97edce2300..64e775933e9b 100644
@@ -21,6 +21,9 @@
 #include <asm/asm-offsets.h>
 #include <asm/asmmacro.h>
 #include <asm/processor.h>
+#ifdef CONFIG_MMU
+#include <asm/asm-uaccess.h>
+#endif
 
 #if XCHAL_UNALIGNED_LOAD_EXCEPTION || defined CONFIG_XTENSA_LOAD_STORE
 #define LOAD_EXCEPTION_HANDLER

arch/xtensa/kernel/align.S ENTRY(fast_load_store) · line 193

Quoted 8 lines
arch/xtensa/kernel/align.S +3 -2
@@ -185,8 +188,12 @@ ENTRY(fast_load_store)
 
 #ifdef CONFIG_MMU
        /* l32e can't be used here even when it's available. */
-       /* TODO access_ok(a3) could be used here */
-       j       .Linvalid_instruction
+       rsr     a6, ps
+       bbsi.l  a6, PS_UM_BIT, 1f       # user mode
+       j       .Linvalid_instruction   # kernel mode

It should be the other way around: kernel mode is allowed to access anything, user mode needs address checking.

Quoted 9 lines
arch/xtensa/kernel/align.S +3 -1
+1:
+       movi    a5, 8
+       access_ok a3, a5, a6, a2, .Linvalid_instruction
 #endif
        l32i    a5, a3, 0
        l32i    a6, a3, 4
--

2.43.0

Signature · 2 lines
Thanks.
-- Max
#6 lore raw

PATCH v3 1/1 xtensa: align: validate access in fast_load_store

access_ok() is used only in user mode and
branches to .Linvalid_instruction on fault.
Kernel mode skips access_ok().

---
v3:
- Changed kernel mode behavior to skip access_ok()

v2:
- Address Max's feedback
- Fixed formatting
- Added user mode branch

Testing:
- Built with CONFIG_MMU enabled
- objdump before/after comparison and validated code path

 arch/xtensa/kernel/align.S | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
arch/xtensa/kernel/align.S +8 -2
index ee97edce2300..f2d0d831ce43 100644
@@ -21,6 +21,9 @@
 #include <asm/asm-offsets.h>
 #include <asm/asmmacro.h>
 #include <asm/processor.h>
+#ifdef CONFIG_MMU
+#include <asm/asm-uaccess.h>
+#endif
 
 #if XCHAL_UNALIGNED_LOAD_EXCEPTION || defined CONFIG_XTENSA_LOAD_STORE
 #define LOAD_EXCEPTION_HANDLER
@@ -185,8 +188,11 @@ ENTRY(fast_load_store)
 
 #ifdef CONFIG_MMU
 	/* l32e can't be used here even when it's available. */
-	/* TODO access_ok(a3) could be used here */
-	j	.Linvalid_instruction
+	rsr	a6, ps
+	bbci.l	a6, PS_UM_BIT, 1f # kernel mode
+	movi	a5, 8
+	access_ok a3, a5, a6, a2, .Linvalid_instruction
+1:
 #endif
 	l32i	a5, a3, 0
 	l32i	a6, a3, 4
Signature · 1 line
2.43.0
maintainer
#7 lore raw

Re: [PATCH v3] xtensa: align: validate access in fast_load_store

Quoted 17 lines · Ricky Ringler

access_ok() is used only in user mode and
branches to .Linvalid_instruction on fault.
Kernel mode skips access_ok().

---
v3:
- Changed kernel mode behavior to skip access_ok()

v2:
- Address Max's feedback
- Fixed formatting
- Added user mode branch

Quoted 6 lines

Testing:
- Built with CONFIG_MMU enabled
- objdump before/after comparison and validated code path

 arch/xtensa/kernel/align.S | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

Thanks. Applied to my xtensa tree.
-- Max