Skip to content

ramips: work around EPC erratum in MIPS 24K cores - #26650

Merged
openwrt-bot merged 6 commits into
openwrt:mainfrom
MarcoMartins86:ramips-mt7620-first-32k
Nov 17, 2026
Merged

openwrt-bot merged 6 commits into
openwrt:mainfrom
MarcoMartins86:ramips-mt7620-first-32k

Conversation

@MarcoMartins86

@MarcoMartins86 MarcoMartins86 commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

The MT7620 crashes of #16396 (worked around at single call sites in #20553) come from what looks like an erratum of the MIPS 24K core. When an interrupt is taken at the first instruction of a J or JAL target, the CPU can save an EPC with wrong bits 31:28. That only gives a wrong address for a jump in the first 32 KiB of a 256 MiB segment. ramips links the kernel at 0x80000000, so its first 32 KiB hold such jumps.

Current main still crashes with those workarounds in place: on a TL-WPA7510 v2 (MT7620A), 6 of 17 boots crashed in try_to_generate_entropy(), which #20553 doesn't cover.

This PR keeps the kernel code out of there and drops the call-site workarounds that this makes unnecessary:

  1. ramips: mt7620: keep the kernel code out of the first 32 KiB: RALINK_PAD_FIRST_32K makes head.S use .fill 0x8000 instead of .fill 0x400. It has no prompt, so it can't be switched off where it's needed, and is on for SOC_MT7620 kernels without the MT76x8 pin controller, i.e. the mt7620 subtarget: SOC_MT7620 also covers the MT7628, which doesn't show the fault (see below). Suggested by @namiltd. It costs 31 KiB of RAM, of which commit 4 gives 28 KiB back. MediaTek's SDK kernels do the same (see below).
  2. ramips: mt7620: drop the workarounds for the EPC erratum: removes 330 and 331, the jump workarounds. They only covered the call sites where crashes had been reported, and only applied to the mt7620 subtarget (NET_RALINK_MT7620 is off on mt76x8). 332, which skips arch_align_stack() on exec, stays: it is an optimization of its own.
  3. generic: mips: keep module code out of the first 32 KiB: hack 301-03. On the 24K cores known to have the fault, those with a PRId below 0x00019655 (every 24Kc and the 24KEc before V5.5, so not the MT7628), OpenWrt's module allocator keeps module and BPF JIT code out of the first 32 KiB of a segment, in KSEG0 and in the vmalloc fallback, and module_finalize() warns if module code starts there all the same. Based on @namiltd's changes to 301-02.
  4. ramips: free the padding in front of the kernel text: by @namiltd, gives 28 KiB of the padding back to the page allocator; the first page stays reserved, and nothing is freed if the exception vectors are in the padding. It is in ralink's of.c, for every SoC with RALINK_PAD_FIRST_32K.
  5. ramips: rt305x: keep the kernel code out of the first 32 KiB: turns RALINK_PAD_FIRST_32K on for SOC_RT305X, as @DragonBluep measured the fault on an RT5350. Built, not booted yet.
  6. ramips: mt7620: enable LIST_HARDENED again: reverts the MT7620 exception from 7423841, which the crashes of mt7620 crash since kernel > 6.6.45 #16396, this erratum, had led to. Suggested and tested by @namiltd.

What happens

When it goes wrong, the return from the interrupt lands at the right offset in the segment below (0x7... instead of 0x8...). The kernel then dies with Unable to handle kernel paging request at virtual address 7xxxxxxx, epc == 7xxxxxxx, with ra just after the jump. That is the signature in #16396 (arch_align_stack, get_random_u16/u32, try_to_generate_entropy, resume_user_mode_work).

Measured rule: EPC[31:28] comes out as bits 31:28 of (delay-slot address + the sign-extended low 16 bits of the instruction), as if it were a branch with an unshifted offset. It happens when bits 19 and 16 of the instruction are set; bits 27:0 are right. Within a segment, the result only differs from the correct address when (delay slot & 0x0fffffff) + low16 < 0x10000 with bit 15 of low16 set, i.e. for a jump in the first 32 KiB.

Measured without Linux, MT7620A (24KEc, PRId 0x00019650)

A small program started from U-Boot with go installs its own exception vector (EBase moved) and runs the CP0 timer at a random phase (~100k interrupts/s). Its handler reads EPC directly. Per run of 50M calls (~31k interrupts on the target each):

call at to wrong EPC / interrupts at target
0x80003f90 jal 0x82e70040 31093 / 31093 (EPC 0x72e70040)
0x80003f90 jalr 0x82e70040 0 / 62262
0x80003f90 j 0x82e70040 31232 / 31233
0x80008f90 jal 0x82e70040 0 / 31464
0x80003f90 jal 0x82c70040 (bit 19 of the jal clear) 0 / 31110
0x80003f90 jal 0x82e30040 (bit 16 clear) 0 / 30823
0x80003f90 jal 0x82e50040 (bit 15 clear) 0 / 31249
0x80003f90 jal 0x82e71040 (sum 0x103a4) 0 / 31286
0x80003fe8 jal 0x82e70040 (sum 0xfffc) 31132 / 31132
0x80003fec jal 0x82e70040 (sum 0x10000) 0 / 31556
0xa0003f90 jal (uncached) 0xa2e70040 12850 / 25711 (EPC 0x92e70040)

From cached code it happens on every interrupt that lands on such a target. From uncached code it happens on half of them.

Not MediaTek-specific

I ran the same test from user space on an Atheros AR9132 (24Kc V7.4, ath79): code mapped at 0x20003f90, target 0x22e70040, the wrong EPC 0x12e70040 caught with a SIGSEGV handler. It gave 52 wrong EPCs/s with jal and 48/s with j. It gave none with jalr, none from 0x20008f90 and none for the targets with a cleared bit, and the 0xfffc/0x10000 edge was the same. ath79 doesn't crash because its kernel is loaded at 0x80060000.

@DragonBluep ran the same user-space test on more SoCs (20 s per run, wrong EPCs per second):

SoC core j jal jalr
MT7620 24KEc 50.4 51.2 0
RT5350 24KEc 38.2 37.4 0
QCA9531 24Kc 73.4 104.0 0
MT7628 24KEc 0 0 0
MT7621 1004Kc 0 0 0

So not every 24K core has it: the MT7628 doesn't.

@namiltd's table of the 24K SoCs by PRId (the rows without "tested" are expected from the PRId, not measured). The PRId check of commit 3 takes every row with the erratum and leaves out the MT7628 and MT7688:

SoC type PRId revision EPC erratum
MT7620A 24KEc 00019650 v5.0 yes - tested
MT7628, MT7688 24KEc 00019655 v5.5 no - tested
RT5350 24KEc 0001964c v4.12 yes - tested
RT3050, RT3052, RT3350, RT3352 24KEc 0001964c v4.12 yes
AR9132 24Kc 00019374 v7.4 yes - tested
QCA9531 24Kc 00019374 v7.4 yes - tested
QCA9533, AR9331, AR7240/7241, AR9341/9342 24Kc 00019374 v7.4 yes
AR7161 24Kc 00019374 v7.4 yes
Lantiq Danube (PSB 50702/50712) 24KEc 00019641 v4.1 yes
Ikanos Vx180 24KEc 0001964c v4.12 yes

MediaTek's SDK does the same

TP-Link's stock kernel for the TL-WPA7510 v2 (MediaTek SDK, Linux 2.6.36) is also loaded at 0x80000000, but its first 32 KiB are empty and its code starts at 0x80008400. TP-Link's GPL code shows why: MediaTek's SDK adds . = . + 0x8000; at the start of .text in arch/mips/kernel/vmlinux.lds.S. Upstream 2.6.36 doesn't have that line, and the SDK gives no reason for it. In MediaTek's later 3.10.14 SDK (e.g. in D-Link's DIR-882 GPL code) the same line is under #if defined(CONFIG_RALINK_MT7628) || defined(CONFIG_RALINK_MT7620). As the MT7628 doesn't show the fault, this PR pads the MT7620 and, by the measurement above, the RT305x.

In current OpenWrt the first 32 KiB of the mt7620 kernel hold 510 jumps. 7 of them meet the conditions, all in random.c (try_to_generate_entropy, _credit_init_bits, entropy_timer, execute_with_initialized_rng), and #20553 doesn't touch any of them. With this PR there are no jumps there at all.

Tested

TL-WPA7510 v2 (MT7620A, port in preparation), kernel 6.18.60:

  • with 330-332, without the fix: 6 of 17 boots crashed in try_to_generate_entropy() (initramfs image booted from U-Boot)
  • with all six commits, initramfs image booted from RAM with kexec: no J or JAL in the first 32 KiB of the kernel; 15 boots, each with "Freeing head padding memory: 28K", 24 modules, none in a first 32 KiB, and no oops or warning; 50 rounds of reloading five modules, none in a first 32 KiB; 10 minutes of the reproducer from mt7620 crash since kernel > 6.6.45 #16396, a loop of iw <ap> station dump on both access points (2200 rounds), together with exec-heavy load (24.5k rounds), without an oops or a warning
  • the guard, in a test build of all six commits that took code blocks from the freed pages: 0 of 9 blocks in the first 32 KiB; with the guard off, 4 of 9 at 0x80004000-0x80007000
  • LIST_HARDENED (commit 6) adds 51,212 bytes of kernel text, 13,409 to the compressed kernel and 1,920 to the root file system
  • rt305x and mt76x8 kernels built with all six commits: RALINK_PAD_FIRST_32K comes out on for rt305x, with the kernel code at 0x80008000 and the free, and off for mt76x8, at 0x80000400 as before, without the free

The guard (commit 3) also on a TL-WR941ND v3 (AR9132, PRId 0x00019374), flashed, with 25.12's kernel 6.12, which takes the same patch file: with a test patch that keeps pages 1 to 7 free during boot, 0 of 9 blocks in the first 32 KiB with the guard and 4 of 9 without, and the vmalloc fallback started at 0xc0008000; without the test patch, 18 modules, none in a first 32 KiB, and no oops or warning.

Also tested by reviewers: a Zbtlink WE826-T2 (MT7620A) without 330-332 and its own local workarounds, with GCC 14.4 and 16.2 (40,000 execs, SPI reads, IPv6 UDP load, no oops), and an earlier version on MT7628.

make target/linux/refresh leaves the patches unchanged.

Not tested: booting rt305x (I have no such device), other exceptions at the jump target (TLB refill, address error), 34K/74K/1004K cores.

🤖 Generated with Claude Code

@openwrt openwrt Bot added the target/ramips pull request/issue for ramips target label Nov 11, 2026
@MarcoMartins86
MarcoMartins86 force-pushed the ramips-mt7620-first-32k branch from adb79cb to 9fd35b7 Compare November 11, 2026 23:56
@namiltd

namiltd commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

The patch 332-mt7620-conditional-stack-align.patch is unrelated to this bug; it is simply an optimization. Therefore, there is no need for you to remove it. However, the benefit it provides is minimal, so it can be removed; this should be noted in the description, and it should not be conflated with jump-related errors.
And perhaps those 32KB could be put to some use, so they don't go to waste?

@namiltd

namiltd commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Following up on my question about putting the 32 KiB to use: here is a concrete proposal.

Only the first 0x400 is needed for the exception vectors (that is what the .fill in head.S is for on kernels linked in KSEG0). The rest up to 0x8000 is pure padding, but it sits inside the kernel image (_text.._end), so it stays reserved and counts against free RAM. That matters on the 32 MB boards.

The erratum only concerns code executed through J/JAL in that range, so the pages can still be used as data. The simplest option would be to hand them back to the page allocator after mem_init, from the ralink platform code, e.g. free_reserved_area() on 0x80001000-0x80008000 (7 pages, 28 KiB), keeping the first page for the vectors.

Things I would check before choosing the range:

  • how much space the vectors really use on the 24KEc (ebase, vectored interrupts);
  • that nothing else lives in that region (e.g. via platform code);
  • early-boot reservation changes have broken boards before on MIPS, so it deserves a test on real hardware.

Alternatively the kernel could be linked at 0x80008000 instead of padding, since MIPS only reserves _text.._end these days. But that changes the load address and entry point in the uImage header, and many vendor bootloaders expect 0x80000000, so I would not go that way.

Either as a third commit here or as a follow-up, plus a short comment next to .fill 0x8000 saying why it is there and that no code may be placed in that region.

I have not tested any of this, so the free_reserved_area() part is a suggestion to verify, not a tested patch.

Draft of code: draft.txt

@DragonBluep

Copy link
Copy Markdown
Contributor

Ping @dormancygrace

@DragonBluep DragonBluep left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TP-Link's GPL code shows why: MediaTek's SDK adds . = . + 0x8000; at the start of .text in arch/mips/kernel/vmlinux.lds.S.

Ref:
https://github.com/hanwckf/rt-n56u/blob/23387b278a7cf728748af606760758f5d59d1451/trunk/linux-3.4.x/arch/mips/kernel/vmlinux.lds.S#L53

Not tested: MT7628 hardware,

MT7628 is working well as before with this patch. This PR LGTM.

@dormancygrace

Copy link
Copy Markdown
Contributor

Thanks for investigating the underlying erratum. I tested the padding change on a physical Zbtlink WE826-T2, MT7620A rev. 2 eco 6, backported to Linux 6.18.44.

With the existing 330–332 patches and my local entropy/SPI/IPv6 UDP workarounds removed, the GCC 14.4 and GCC 16.2 builds each completed three RAM boots. Together they passed 40,000 execs, repeated hash-verified SPI reads, and bidirectional IPv6 UDP at 20 Mbit/s without an Oops or panic. Quiet UDP runs reported zero loss; one run under concurrent exec load reported 7/20,836 lost packets (0.034%).

ELF inspection and live symbols confirmed _stext=0x80008000, with no J/JAL instructions remaining in the first 32 KiB.

These results support replacing the targeted workarounds with this structural fix.

I also agree that removing patch 332 should be described separately: it is an exec-path optimization, not another JAL workaround.

@namiltd

namiltd commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

@MarcoMartins86
MarcoMartins86 force-pushed the ramips-mt7620-first-32k branch from 9fd35b7 to bee1a9c Compare November 12, 2026 21:04
@MarcoMartins86

Copy link
Copy Markdown
Contributor Author

Thanks @dormancygrace for testing it on the WE826-T2, and @DragonBluep for checking MT7628.

@namiltd

  • 332: agreed, it is not one of the jump workarounds. It stays: the PR now drops only 330 and 331, and the second commit says why.

  • Freeing the padding: I don't think that is safe in OpenWrt. hack-6.18/301-02-mips-replace-mlong-calls-with-mno-long-calls-if-poss.patch takes the memory for module code from alloc_pages() and runs the code at the pages' KSEG0 address; on my TL-WPA7510 v2, cfg80211 sits at 0x82a00000 and compat at 0x81cc0000. Once 0x80001000-0x80007fff go back to the page allocator, module text can land there and run in exactly the range the erratum hits. Keeping the 28 KiB reserved is what keeps all code out of it, so I added that to the comment next to the .fill and to the commit message.

    I'll look further into what can be done to reuse that space without that risk, for example by keeping the module allocator away from it, and follow up separately.

I also took the bot's suggestion: the comment and the patch description now say J or JAL.

@MarcoMartins86
MarcoMartins86 force-pushed the ramips-mt7620-first-32k branch from bee1a9c to a0a50d5 Compare November 13, 2026 00:14
@openwrt openwrt Bot added the kernel pull request/issue with Linux kernel related changes label Nov 13, 2026
@MarcoMartins86

Copy link
Copy Markdown
Contributor Author

@namiltd, I looked further into reusing the padding, as I said I would, and it works with a guard in the module allocator, so I picked your patch up as commit 4 of this PR.

What I measured on the TL-WPA7510 v2, booted from RAM:

  • The freed pages lie in a movable pageblock. Module allocations are unmovable, so they don't reach those pages while unmovable memory lasts, and in normal boots no module landed there. That only holds while memory is plentiful, though: a test build that took code blocks from movable pages right after the padding was freed put 4 of 9 blocks at 0x80004000-0x80007000.
  • So commit 3 adds a hack patch on top of 301-02: alloc_phys() sets aside the blocks the page allocator offers in the first 32 KiB and asks again (at most eight times), and the vmalloc fallback starts 32 KiB in. With it, the same test build got all seven freed pages offered first, set them aside, and none of the 9 blocks landed there. This also covers ath79, where the RAM below the kernel at 0x80060000 is free for module code today.
  • The final set: "Freeing mt7620 head padding memory: 28K", 24 modules, none below 0x80008000, also after 50 rounds of reloading five modules, and 5 minutes of exec-heavy load (~49k execs) without an oops.

I made two changes to your patch, noted in the commit: I dropped the second message, since free_reserved_area() already logs one, and the comment now says that the module allocator keeps code out of those pages, instead of that they are never executed through their KSEG0 address. I kept you as the author, with your Signed-off-by. Tell me if you'd rather have it differently.

The comment next to the .fill and the description of 334 no longer say that the space has to stay reserved.

@willmo

willmo commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

Should this also be sent upstream somewhere?

@DragonBluep

Copy link
Copy Markdown
Contributor

Unless they can be merged upstream, I don’t think it’s worth keeping two patches just to save 20+ KB of RAM. And we don't even know if there are any other potential risks.

static void *alloc_phys(unsigned long size)
{
+ /* at most this many blocks start in the first 32 KiB */
+ struct page *held[MODULE_CODE_SKIP >> PAGE_SHIFT];

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With CONFIG_PAGE_SIZE_64KB this array has zero elements, yet a page at phys 0 still passes the < MODULE_CODE_SKIP test and is written to held[0]. Round up so there is always room for the one page that starts in the range.

Suggested change
+ struct page *held[MODULE_CODE_SKIP >> PAGE_SHIFT];
+ struct page *held[DIV_ROUND_UP(MODULE_CODE_SKIP, PAGE_SIZE)];

Generated by Claude Code

+ while (1) {
+ page = alloc_pages(GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN |
+ __GFP_THISNODE, order);
+ if (!page || page_to_phys(page) >= MODULE_CODE_SKIP)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only rejects the first 32 KiB of physical memory, but KSEG0 spans two 256 MiB segments, so phys 0x10000000 (0x90000000) is also a segment start on boards with more than 256 MiB of lowmem; the vmalloc fallback has the same gap at 0xd0000000 and up (VMALLOC_START/VMALLOC_END in pgtable-32.h). Is limiting it to the first segment intentional, given the comment says "a 256 MiB segment"? If not, test page_to_phys(page) & (SZ_256M - 1) here and double the held[] bound to match.


Generated by Claude Code

@namiltd

namiltd commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

I was working on the same thing in parallel.
In addition to your patch 334-MIPS-ralink-mt7620-keep-the-kernel-code-out-of-the-first-32K.patch, there is also:

@MarcoMartins86 Please take a look at this.
It is permissible to free the entire 32 KiB; this confirms that my code works: [ 2.692134] MT7620: ebase=80b58000, freed 32 KiB of head padding (80000000-80008000)
Could you compile my code and run your stability tests?

* Reserved space for exception handlers.
* Necessary for machines which link their kernels at KSEG0.
*/
+#ifdef CONFIG_SOC_MT7620

@namiltd namiltd Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The CONFIG_SOC_MT7620 parameter is also set for the MT7628 chip; it is worth conducting tests to see whether the issue affects this chipset as well. Since the SDK modifies the address for it too, this does not necessarily mean that the problem occurs in this case as well. Perhaps it would be better to use CONFIG_NET_RALINK_MT7620 as a switch, so that it applies only to the verified MT7620?

@MarcoMartins86
MarcoMartins86 force-pushed the ramips-mt7620-first-32k branch from a0a50d5 to 219644c Compare November 13, 2026 18:02
@MarcoMartins86

MarcoMartins86 commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for the reviews.

@DragonBluep, given your point, let's keep this PR simple: it only pads the kernel. I've dropped the two commits that reused the padding (the module allocator guard and freeing the padding), so it's back to the fix and the removal of 330 and 331. Changed my mind again due to latest discussions.

@namiltd, let's move reusing that space to a PR of its own. You've been driving it, so could you open it? I'll keep helping, and I can run the same tests with your patches on the TL-WPA7510 v2. Two things I noticed in your 301-02:

  • The vmalloc fallback is still a plain vmalloc(). On MIPS32 the vmalloc area starts at 0xc0000000, the start of a segment, so module code can still land in its first 32 KiB; the 301-03 I had here started the fallback at VMALLOC_START + SZ_32K.
  • The PLT entries only jump with jr, which the erratum doesn't affect (it takes a J or JAL in the first 32 KiB), so keeping them out of there isn't needed, though it does no harm.

The bot's two points on 301-03 (64 KiB pages, the second KSEG0 segment) apply there as well; your version already covers the second segment.

About MT7628 in 334: I'd keep it. The erratum is in the 24K core rather than in the MT7620: I measured it on the MT7620A's 24KEc and on an AR9132's 24Kc, and the MT7628 is a 24KEc too. MediaTek's own SDK pads both, and @DragonBluep confirmed MT7628 works with it. NET_RALINK_MT7620 is the Ethernet driver's option, and it's off on mt76x8, which is why 330 and 331 never covered MT7628.

@willmo, @DragonBluep, on upstream: I'd rather get this into OpenWrt first. Getting patches into the kernel may not go smoothly right now (see this comment), and here it reaches the affected devices sooner. Then we can see about sending the head.S change to linux-mips.

@namiltd

namiltd commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

@DragonBluep confirmed MT7628 works with it.

But no one has confirmed that the error occurs on this chipset.

@namiltd

namiltd commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

The head.S patch only pads the static kernel at 0x80000000. However, the 24KEc EPC erratum affects the first 32 KiB of every 256 MiB segment, which head.S cannot protect.
We still need the additional MIPS_KSEG0_LOW_NOEXEC changes in 301-02-mips-replace-mlong-calls-with-mno-long-calls-if-poss.patch because -mno-long-calls causes modules to emit JAL instructions, and dynamically allocated module code can still land in other vulnerable 32 KiB windows:

  • vmalloc (0xc0000000): VMALLOC_START begins on a 256 MiB boundary, so alloc_virt() must offset the start by 32 KiB.
  • Upper KSEG0 (0x90000000): Boards with >256 MiB RAM cross into the next 256 MiB segment, which alloc_phys() must filter out via in_noexec_window().
  • Reclaimed low memory (0x80000000): Any pages returned to the page allocator at the bottom of KSEG0 must also be skipped by alloc_phys().

In short: head.S protects the core kernel, while those extra checks in the module patch protect loadable modules across all other 256 MiB boundaries.

Version updated with your feedback: 301-02-mips-replace-mlong-calls-with-mno-long-calls-if-poss.patch

Tested on mt7620/TP-Link Archer C5 v4

@MarcoMartins86

Copy link
Copy Markdown
Contributor Author

@namiltd, right, nobody has measured it on an MT7628 yet. Going by tests only, not by the core's name, this is where the OpenWrt targets with a 24K core stand.

The erratum takes a J or JAL in the first 32 KiB of any 256 MiB segment. In the kernel, code can sit in three kinds of such windows:

  • 0x80000000: the kernel image, and module and BPF JIT code that alloc_phys() takes from the page allocator
  • 0x90000000: the same, but only with more than 256 MiB of lowmem
  • 0xc0000000, 0xd0000000, 0xe0000000 and 0xf0000000, the segment starts in the vmalloc area: module and BPF JIT code when alloc_phys() fails

Only the first differs between targets, so that's what the table shows; the vmalloc ones are the same on every 32-bit target (see the notes on modules below). User space has its own windows, from 0x00000000 to 0x70000000 in every process, whatever the RAM size. None of the guards discussed here cover them, because there mmap() picks the address, not the RAM page behind it. But a hit only affects that program, and programs and libraries don't normally load there: none of the 45 processes on my TL-WPA7510 v2 has code in one.

SoCs (core) Target Kernel code in 0x80000000-0x80007fff Needs, if affected Erratum
MT7620A (24KEc V5.0) ramips/mt7620 yes; none with this PR padding (this PR); module guard if the padding is freed confirmed: by me on a TL-WPA7510 v2 (bare metal, in the kernel and from user space), and the crash reports linked in the description
MT7620N (24KEc) ramips/mt7620 yes; none with this PR padding (this PR); module guard if the padding is freed not tested
MT7628, MT7688 (24KEc V5.0, V5.5) ramips/mt76x8 yes; none with this PR padding (this PR); module guard if the padding is freed not tested yet: @DragonBluep, could you run the test below on yours? If it doesn't show the erratum, I'll take the MT7628 out of 334
RT3050, RT3052, RT3350, RT3352, RT5350 (24KEc) ramips/rt305x yes padding not tested
Danube (24KEc) lantiq/xway, xway_legacy yes, from 0x80002000 padding and module guard not tested
AR9130, AR9132 (24Kc V7.4) ath79 no, starts at 0x80060000 module guard confirmed: by me on a TL-WR941ND v3, from user space
AR71xx, AR724x, AR933x, QCA953x (24Kc) ath79 no, starts at 0x80060000 module guard not tested

Padding is this PR's head.S change, which moves the kernel code to 0x80008000 (on lantiq, whose kernel starts at 0x80002000, that takes 24 KiB); module guard is your 301-02 changes. The column only counts 0x80000000-0x80007fff; every affected target also needs the guard for the vmalloc segment starts. The first 4 KiB is reserved for the exception vectors on all of them. I've left out targets with other cores (4KEc, 34Kc, 74Kc, 1004Kc). No crashes have been reported on the MT7628, but that doesn't tell either way: a kernel only crashes when a jump that meets the erratum's conditions ends up in the first 32 KiB, and that depends on each kernel's layout.

The program below tests a chip in 20 seconds from user space. It loops on a call from the first 32 KiB of a 256 MiB segment and counts the interrupts that return to a wrong EPC (one segment lower, where nothing is mapped, so they fault):

ujal jal  20003f90 22e70040 20    # affected: wrong EPCs every second
ujal jalr 20003f90 22e70040 20    # control: none

With jal, the MT7620A gives 29 per second and the AR9132 52; both give 0 with jalr. Anyone with one of the other untested chips is welcome to run it too, and I'll add the results to the table.

ujal.c (build: mipsel-openwrt-linux-musl-gcc -O2 -static -no-pie -o ujal ujal.c)
/*
 * User-space test of the MIPS EPC erratum seen on the MT7620: a loop of
 * calls from the first 32 KiB of a 256 MiB segment. A wrong EPC on an
 * interrupt at the call target makes the return from the interrupt land
 * in the segment below, which is unmapped: the SIGSEGV handler counts it
 * and resumes at the real target.
 *
 *   ujal <jal|jalr|j> <call address> <target> <seconds>
 */
#define _GNU_SOURCE
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/syscall.h>
#include <time.h>
#include <ucontext.h>
#include <unistd.h>

typedef unsigned int u32;

static volatile u32 bad, last_bad;
static u32 target;

static void on_segv(int sig, siginfo_t *si, void *ctx)
{
	ucontext_t *uc = ctx;
	u32 pc = (u32)uc->uc_mcontext.pc;
	char msg[96];
	int n;

	(void)sig;
	if (pc != target && ((pc ^ target) & 0x0fffffffu) == 0) {
		bad++;
		last_bad = pc;
		uc->uc_mcontext.pc = target;
		return;
	}
	n = snprintf(msg, sizeof(msg), "unexpected SIGSEGV pc %08x addr %08lx\n",
		     pc, (unsigned long)si->si_addr);
	write(2, msg, n);
	_exit(3);
}

static u32 jtype(u32 op, u32 t)
{
	return (op << 26) | ((t & 0x0fffffffu) >> 2);
}

static void map_page(u32 addr)
{
	void *want = (void *)(unsigned long)(addr & ~0xfffu);
	void *p = mmap(want, 4096, PROT_READ | PROT_WRITE | PROT_EXEC,
		       MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED_NOREPLACE, -1, 0);

	if (p != want) {
		fprintf(stderr, "mmap %08x failed\n", addr);
		exit(2);
	}
}

static unsigned long intr_total(void)
{
	char line[256];
	unsigned long v = 0;
	FILE *f = fopen("/proc/stat", "r");

	if (!f)
		return 0;
	while (fgets(line, sizeof(line), f))
		if (!strncmp(line, "intr ", 5)) {
			v = strtoul(line + 5, NULL, 10);
			break;
		}
	fclose(f);
	return v;
}

static double now(void)
{
	struct timespec ts;

	clock_gettime(CLOCK_MONOTONIC, &ts);
	return ts.tv_sec + ts.tv_nsec / 1e9;
}

int main(int argc, char **argv)
{
	u32 src, *s, *d, i;
	int mode, secs;
	struct sigaction sa;
	void (*loop)(u32 n, u32 t);
	unsigned long calls = 0, i0, i1;
	double t0, t1;

	if (argc != 5) {
		fprintf(stderr, "usage: %s <jal|jalr|j> <call address> <target> <seconds>\n", argv[0]);
		return 1;
	}
	mode = !strcmp(argv[1], "jalr") ? 1 : !strcmp(argv[1], "j") ? 2 : 0;
	src = strtoul(argv[2], NULL, 16);
	target = strtoul(argv[3], NULL, 16);
	secs = atoi(argv[4]);
	if ((src ^ target) >> 28 || ((src - 8) ^ target) >> 12 == 0 || ((src + 28) ^ target) >> 12 == 0) {
		fprintf(stderr, "call address and target must be in the same segment, on other pages\n");
		return 1;
	}

	map_page(src - 8);
	if ((src + 28) >> 12 != (src - 8) >> 12)
		map_page(src + 28);
	map_page(target);
	s = (u32 *)(unsigned long)(src - 8);
	d = (u32 *)(unsigned long)target;
	s[0] = 0x03e01825;			/* or v1, ra, zero */
	s[1] = 0;
	s[2] = mode == 0 ? jtype(3, target) :	/* jal target */
	       mode == 1 ? 0x00a0f809 :		/* jalr a1 */
	       jtype(2, target);		/* j target */
	s[3] = 0;
	s[4] = 0x2484ffff;			/* addiu a0, a0, -1 */
	s[5] = 0x1480fffc;			/* bnez a0, src */
	s[6] = 0;
	s[7] = 0x00600008;			/* jr v1 */
	s[8] = 0;
	d[0] = mode == 2 ? jtype(2, src + 8) : 0x03e00008;	/* j src+8 / jr ra */
	d[1] = 0;
	syscall(__NR_cacheflush, s, 36, 3);
	syscall(__NR_cacheflush, d, 8, 3);

	memset(&sa, 0, sizeof(sa));
	sa.sa_sigaction = on_segv;
	sa.sa_flags = SA_SIGINFO;
	sigaction(SIGSEGV, &sa, NULL);

	loop = (void (*)(u32, u32))(unsigned long)(src - 8);
	i0 = intr_total();
	t0 = now();
	do {
		for (i = 0; i < 10; i++)
			loop(1000000, target);
		calls += 10000000;
		t1 = now();
	} while (t1 - t0 < secs);
	i1 = intr_total();

	printf("%-4s %08x -> %08x: %lu calls in %.1f s, %lu interrupts, wrong EPC %u (%.1f/s), last %08x\n",
	       argv[1], src, target, calls, t1 - t0, i1 - i0, bad, bad / (t1 - t0), last_bad);
	return 0;
}

On modules: agreed, the padding only covers the kernel image, and module code needs the same care. That's what your 301-02 changes do, and they fit best in their own PR: they change the generic hack every MIPS target uses, so they deserve their own review. With only this PR, nothing hands the padding back, and the MT7620 and MT7628 have at most 256 MiB of RAM, so the only places module code can still hit the erratum on them are the segment starts in the vmalloc area, which a module only reaches when its KSEG0 allocation fails (all 24 modules on my TL-WPA7510 v2 are in KSEG0).

Three notes for that PR:

  • The vmalloc area runs up to the fixmap, just below 0xff000000, so 0xd0000000, 0xe0000000 and 0xf0000000 are segment starts in it too. alloc_virt() could try one segment at a time, each from 32 KiB past its start to its end (or VMALLOC_END). No range then reaches into the next segment's first 32 KiB, so checking only the base address in module_finalize() stays enough; blocks from alloc_phys() are naturally aligned and can't cross a boundary either. Careful with the last segment: 0xf0000000 + SZ_256M wraps to 0.
  • ath79 and lantiq have the module exposure today, with no padding involved: nothing reserves the RAM between the first page and their kernels (on my TL-WR941ND v3 the boot log reports 5308K reserved, less than the 4942K kernel image plus the 380 KiB below it, so they're in use as ordinary RAM), so alloc_phys() can hand out pages at 0x80001000-0x80007fff on ath79 and 0x80001000-0x80001fff on lantiq. On the padded targets it's 0x80000000-0x80007fff, once the padding is freed. It's rare, since modules load at boot when those pages are usually taken, but possible. ath79 builds one kernel for several SoCs, so a SoC symbol can't select it; a runtime check such as current_cpu_type() == CPU_24K (the 24Kc and the 24KEc) would let the symbol stay on for every 32-bit target and act only on 24K cores.
  • Once the guard is in, loading the kernel at 0x80008000 is an alternative to freeing the padding: the RAM below it becomes ordinary memory, as on ath79, without any code to free it. The new load address would need testing with the boot loaders of the mt7620 and mt76x8 devices, though.

@namiltd

namiltd commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

So, do you agree with me that a complete solution to this problem involves using padding and a module guard mechanism? Thanks to this safeguard, freeing this memory is safe. Unlike you, I’m in favor of resolving all of this in a single PR and putting the issue behind us. Even if the issue is sporadic, it’s worth fixing while you’re at it. If you create a separate PR, no one will be keen to test a fix for a problem they’ve never actually encountered.

@namiltd

namiltd commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Since we have a corrected 301-02, we could try setting loadaddr-$(CONFIG_TARGET_ramips_mt7620) := 0x80008000 directly in the Makefile (+changes in arch/mips/ralink/Platform) , which would eliminate the need for any memory freeing. Also, the .fill directive in head.S should be removed entirely, as the TLB is located elsewhere.

Edit: auto_awesome
It won't work because the bootloader loads from a fixed address, 0x80000000

@namiltd

namiltd commented Nov 14, 2026

Copy link
Copy Markdown
Contributor

My complete patch, ready for testing: namiltd#147

@MarcoMartins86

Copy link
Copy Markdown
Contributor Author

@namiltd, I see your point. If this PR only pads, the problem looks solved, and nobody will feel the need to free the padding or to add the guard later. And the guard can help the chips that need no padding. So both are in this PR now, as two commits on top, based on your patch.

What differs from your version, and why:

Module guard: every 24K core. It's a patch of its own (301-03) instead of edits inside 301-02, and it checks boot_cpu_type() == CPU_24K at run time instead of a Kconfig symbol, so it also acts on ath79, where the AR9132 shows the erratum. On a chip without the erratum it costs nothing: those pages still hold data, only no module code. Besides the MT7620A, I tested it on the AR9132 of a TL-WR941ND v3, with 25.12's kernel 6.12, which takes the same patch file unchanged.

Padding and freeing: MT7620, MT7628 and MT7688 for now. The RT305x and the Lantiq Danube also have a 24K core and kernel code in that range, but a new boot layout needs a boot test, and I have neither.

28 KiB are freed, not 32: the first page stays reserved. It is physical address 0, and too much code reads 0 as "nothing":

  • cpu_probe() reserves that page on every MIPS CPU, and memblock, where 0 means failure, never hands it out.
  • In OHCI a buffer pointer of 0 means a zero-length packet, and td_fill() writes the buffer's bus address into hwCBP as it is. A transfer buffer at address 0 would go out as an empty packet. More than 120 of the 279 mt7620 and mt76x8 devices ship kmod-usb-ohci.
  • The freed pages are movable, so the page cache and user memory get them first, and from there the address can reach any driver.

The ebase check doesn't cover this: on MIPS32r2 trap_init() always takes ebase from memblock (0x80a20000 on my device), so it is never in the padding and all 32 KiB get freed. This is from reading the code, my device has no USB port. But I can't check every driver, and the gain would be 4 KiB.

vmalloc fallback: first segment only. The range ends at 0xd0000000, so no later segment start can be reached and the WARN_ON() in module_finalize() isn't needed. It's simpler than going through the segments one at a time, as I had suggested: vmalloc hands out the lowest free address, and my device uses 1.6 MiB of that area.

The PLT table change is not included. It isn't about the erratum, and it changes module loading on every 32-bit MIPS target. The 24K SoCs in ramips, ath79 and lantiq (Danube) have at most 256 MiB of RAM, so the kernel and all RAM are in one segment and the KSEG0 table is hardly ever used: for none of the 24 modules on my device. It can help SoCs with more RAM, like the MT7621, but those aren't 24K, so it fits better in a PR of its own.

Tested from RAM on a TL-WPA7510 v2: 28K freed, and none of the 24 modules is in a first 32 KiB. In a forced test the allocator offered the freed pages 28 times and the guard set every one aside; with the guard off, 4 of 9 code blocks landed at 0x80004000-0x80007000. On the TL-WR941ND v3, flashed, with a test patch that keeps pages 1 to 7 free during boot and frees them late: the same, 0 of 9 with the guard and 4 of 9 without, and the vmalloc fallback started at 0xc0008000.

The guard commit carries Co-developed-by and your Signed-off-by, taken from your commit, and the freeing commit keeps you as its author, with a note on what I changed. Please tell me if you're fine with both.

@namiltd

namiltd commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

Thanks, this looks good, and keeping the first page is a clear improvement over my version. I'm fine with the Co-developed-by and my Signed-off-by on the guard commit, and with my authorship plus your note on the freeing commit.

Two safeguards were already contained in my version (namiltd#147) and are missing now:

  1. 335: skip freeing if ebase lies inside the padding, i.e. in [_text, _text + 0x8000). You are probably right that trap_init() always takes ebase from memblock on MT7620, but if some configuration ever leaves it at CAC_BASE, the vectors would be freed silently.

  2. 301-03: a WARN_ON() in module_finalize() when the module text or init text base is in the first 32 KiB of a segment. It costs nothing, and it is the only place that would notice a bug in the allocator guard. in_first_32k() would have to move out of the #ifndef MODULES_VADDR block for that.

Two smaller points:

  1. 334: the text inside the patch still says "This costs 31 KiB of RAM". With 335 the net cost is about 4 KiB, so please reword it.

  2. alloc_virt(): a short comment that module loading fails once the first segment of the vmalloc area is used up, instead of moving on to a later segment. It cannot happen on these SoCs, but it should be written down.

@namiltd

namiltd commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

One more thing, if you have the time: could you test whether CONFIG_LIST_HARDENED can be enabled again on MT7620 with this series, like on the other ramips SoCs?

It was disabled for MT7620 only, because of the crashes in #16396, with the cause unknown. The traces there look like this erratum:

  • epc == 7036837c, ra == 80004574 (arch_align_stack+0x50)
  • epc == 70360604, ra == 80004294 (arch_align_stack+0x50)
  • epc == 7036c2d8, ra == 800042f4

In all of them ra is in the first 32 KiB of KSEG0 and the EPC has 0x7... instead of 0x8.... LIST_HARDENED adds checks with calls at many more places, so it would only have made the crash more likely. Those traces are from 6.6, so I do not know if it is the same on 6.18.

The reproducer from the issue is a loop of iw phy0-ap0 station dump. A test could be: LIST_HARDENED=y on MT7620 without the series (should crash) and with it (should not), plus the size of vmlinux and of the kernel image for both, as it costs some code size.

If it holds up, it could go in as a separate commit that reverts the MT7620 exception and refers to #16396.

Edit:
On my TP-Link Archer C5 v4 (MT7620A), enabling CONFIG_LIST_HARDENED did no harm. No crashes occur.
As a test I ran this loop (based on the reproducer from #16396):
while true ; do var=$(iw phy1-ap0 station dump | grep "Station") ; done

Just as a side note, I also tested ujal on a TP-Link TL-WR1043ND v4 (Qualcomm Atheros QCA956X, MIPS 74Kc V5.0) confirmed unaffected (0 wrong EPCs on both jal and jalr).

MarcoMartins86 and others added 5 commits November 17, 2026 21:10
Patches 330 and 331 avoided direct JALs at the call sites that had been
seen to crash: the random helpers of get_random_u32_below() and
blkcg_maybe_throttle_current() in resume_user_mode_work(). They only
applied to the mt7620 subtarget, and they did not cover the jumps in
random.c that crash the boot in try_to_generate_entropy().

With the kernel code out of the first 32 KiB, no jump is left where the
erratum gives a wrong EPC, so drop them. 332, which skips
arch_align_stack() on exec, stays: it is an optimization of its own,
not one of the jump workarounds.

Tested on a TL-WPA7510 v2 with the whole series: no oops in 15 boots,
nor in 10 minutes of exec-heavy load (24.5k rounds) together with the
reproducer from openwrt#16396.

Signed-off-by: Marco Martins <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Link: openwrt#26650
Signed-off-by: Robert Marko <[email protected]>
MIPS 24K cores can save a wrong EPC for an interrupt taken at the
target of a J or JAL in the first 32 KiB of a 256 MiB segment, and the
return from the interrupt then goes to the segment below. This was
measured on the 24KEc of an MT7620A and an RT5350 and on the 24Kc of an
AR9132 and a QCA9531, though not on the 24KEc of an MT7628.

OpenWrt's module allocator (301-02) runs module and BPF JIT code from
the KSEG0 address of the pages it gets, and falls back to vmalloc,
whose area starts at 0xc0000000. On ath79 the kernel starts at
0x80060000, and what the early boot allocations leave free of the RAM
below it goes to the page allocator; on mt7620 the next commit frees
most of the padding that head.S puts there.

Add 301-03: on the 24K cores known to have the fault, those with a PRId
below 0x00019655 (every 24Kc, and the 24KEc before V5.5, which leaves
out the MT7628), hold the blocks the page allocator offers in the first
32 KiB of a segment and ask again, and take the vmalloc fallback from
the rest of the first segment of its area. module_finalize() warns if
module code starts there all the same. Other cores keep the allocator
as it is.

Tested on a TL-WPA7510 v2 with the whole series, in a test build that
took code blocks from the freed movable pages: the seven freed pages
were offered to each 4 KiB block and held, 28 times in all, and none of
9 blocks landed in the first 32 KiB. With the test of the address
switched off, 4 of 9 blocks landed at 0x80004000-0x80007000.

Without the test code: 24 modules, none in a first 32 KiB, in 15 boots
and after 50 rounds of reloading five modules, and 10 minutes of
exec-heavy load without an oops or a warning.

Co-developed-by: Mieczyslaw Nalewaj <[email protected]>
Signed-off-by: Mieczyslaw Nalewaj <[email protected]>
Signed-off-by: Marco Martins <[email protected]>
Co-Authored-By: Claude Fable 5.2 <[email protected]>
Link: openwrt#26650
Signed-off-by: Robert Marko <[email protected]>
The first 32 KiB of the kernel image are kept free of code because of
the MIPS 24K EPC erratum, and the module allocator keeps module code
out of them, so hand them back to the page allocator as ordinary data
pages. The first page stays reserved, as the kernel reserves it on
every CPU, which leaves 28 KiB. Nothing is freed if the exception
vectors are in the padding.

Signed-off-by: Mieczyslaw Nalewaj <[email protected]>
[Marco: drop the second message, free_reserved_area() logs one; say
 what keeps code out of the pages; keep the first page; move it from
 mt7620.c to of.c, for every SoC with RALINK_PAD_FIRST_32K; add it as a
 patch file]
Signed-off-by: Marco Martins <[email protected]>
Co-Authored-By: Claude Fable 5.2 <[email protected]>
Link: openwrt#26650
Signed-off-by: Robert Marko <[email protected]>
The 24KEc of the RT5350 shows the same EPC fault as the MT7620's,
measured from user space: about 38 wrong EPCs a second with J and with
JAL from 0x20003f90 to 0x22e70040, none with JALR. rt305x also links
the kernel at 0x80000000, so turn RALINK_PAD_FIRST_32K on for
SOC_RT305X too. The
padding is freed again as on mt7620, and the module allocator already
keeps module code out of it on every 24K core.

Signed-off-by: Marco Martins <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Link: openwrt#26650
Signed-off-by: Robert Marko <[email protected]>
Commit 7423841 ("ramips: disable CONFIG_LIST_HARDENED on MT7620")
disabled it after the crashes in openwrt#16396, whose cause was unknown. Their
traces show the MIPS 24K EPC erratum: ra in the first 32 KiB of KSEG0
and the EPC in the segment below (0x7...). The checks only made the
crashes more likely, with more cold code and calls in those 32 KiB.
The mt76x8 subtarget kept them, and the MT7628 does not show the fault.

With kernel and module code kept out of the first 32 KiB of a segment,
it can be enabled again, as on the other ramips subtargets. On a
TL-WPA7510 v2 it adds 50 KiB of kernel text, 13 KiB to the compressed
kernel and 2 KiB to the root file system, the modules' checks.

Tested on a TL-WPA7510 v2 booted from RAM: 10 minutes of the reproducer
from openwrt#16396, a loop of `iw <ap> station dump` on both access points
(2200 rounds), together with exec-heavy load (24.5k rounds), without an
oops or a warning. Tested on a TP-Link Archer C5 v4 by Mieczyslaw
Nalewaj.

Suggested-by: Mieczyslaw Nalewaj <[email protected]>
Tested-by: Mieczyslaw Nalewaj <[email protected]>
Signed-off-by: Marco Martins <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Link: openwrt#26650
Signed-off-by: Robert Marko <[email protected]>
@robimarko
robimarko force-pushed the ramips-mt7620-first-32k branch from 467e0c1 to 6df6804 Compare November 17, 2026 20:11
@openwrt-bot
openwrt-bot merged commit 6df6804 into openwrt:main Nov 17, 2026
3 checks passed
@namiltd

namiltd commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Now, quickly following up, create a PR for the openwrt-25.12 branch (cherry picked from).

@robimarko

Copy link
Copy Markdown
Contributor

Thanks! Rebased on top of main and merged!

hnt510-great pushed a commit to hnt510-great/openwrt-systemd-host that referenced this pull request Nov 18, 2026
The MIPS 24K core of the MT7620 can save a wrong EPC for an interrupt
taken at the first instruction of a J or JAL target, when the jump is
in the first 32 KiB of a 256 MiB segment. The return from the
interrupt then goes to 0x7... instead of 0x8..., and the kernel dies
with "Unable to handle kernel paging request". ramips links the kernel
at 0x80000000, so its first 32 KiB hold such jumps.

The fault was measured without Linux, from a program started by the
boot loader, and the 24Kc of an AR9132 shows it too. MediaTek's SDK
kernels leave the same 32 KiB empty for the MT7620 and the MT7628, but
an MT7628 doesn't show the fault, so the padding comes with
RALINK_PAD_FIRST_32K. It has no prompt, so it cannot be switched off,
and is on for SOC_MT7620 kernels without the MT76x8 pin controller: the
mt7620 subtarget, not mt76x8.

On the TL-WPA7510 v2, 6 of 17 boots crashed in
try_to_generate_entropy(). With the kernel code moved behind
0x80008000, no J or JAL is left in the first 32 KiB; with the whole
series, 15 boots and 10 minutes of load gave no oops.

Suggested-by: Mieczyslaw Nalewaj <[email protected]>
Signed-off-by: Marco Martins <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Link: openwrt/openwrt#26650
Signed-off-by: Robert Marko <[email protected]>

GitLab-source: 3c2a81d482d845cb01190c7311dd8f4b5629ff5b
Source delta sanitized for publication; author attribution preserved.
hnt510-great pushed a commit to hnt510-great/openwrt-systemd-host that referenced this pull request Nov 18, 2026
Patches 330 and 331 avoided direct JALs at the call sites that had been
seen to crash: the random helpers of get_random_u32_below() and
blkcg_maybe_throttle_current() in resume_user_mode_work(). They only
applied to the mt7620 subtarget, and they did not cover the jumps in
random.c that crash the boot in try_to_generate_entropy().

With the kernel code out of the first 32 KiB, no jump is left where the
erratum gives a wrong EPC, so drop them. 332, which skips
arch_align_stack() on exec, stays: it is an optimization of its own,
not one of the jump workarounds.

Tested on a TL-WPA7510 v2 with the whole series: no oops in 15 boots,
nor in 10 minutes of exec-heavy load (24.5k rounds) together with the
reproducer from #16396.

Signed-off-by: Marco Martins <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Link: openwrt/openwrt#26650
Signed-off-by: Robert Marko <[email protected]>

GitLab-source: 959a7f9ea96cff203b274c151561efa086c31213
Source delta sanitized for publication; author attribution preserved.
hnt510-great pushed a commit to hnt510-great/openwrt-systemd-host that referenced this pull request Nov 18, 2026
MIPS 24K cores can save a wrong EPC for an interrupt taken at the
target of a J or JAL in the first 32 KiB of a 256 MiB segment, and the
return from the interrupt then goes to the segment below. This was
measured on the 24KEc of an MT7620A and an RT5350 and on the 24Kc of an
AR9132 and a QCA9531, though not on the 24KEc of an MT7628.

OpenWrt's module allocator (301-02) runs module and BPF JIT code from
the KSEG0 address of the pages it gets, and falls back to vmalloc,
whose area starts at 0xc0000000. On ath79 the kernel starts at
0x80060000, and what the early boot allocations leave free of the RAM
below it goes to the page allocator; on mt7620 the next commit frees
most of the padding that head.S puts there.

Add 301-03: on the 24K cores known to have the fault, those with a PRId
below 0x00019655 (every 24Kc, and the 24KEc before V5.5, which leaves
out the MT7628), hold the blocks the page allocator offers in the first
32 KiB of a segment and ask again, and take the vmalloc fallback from
the rest of the first segment of its area. module_finalize() warns if
module code starts there all the same. Other cores keep the allocator
as it is.

Tested on a TL-WPA7510 v2 with the whole series, in a test build that
took code blocks from the freed movable pages: the seven freed pages
were offered to each 4 KiB block and held, 28 times in all, and none of
9 blocks landed in the first 32 KiB. With the test of the address
switched off, 4 of 9 blocks landed at 0x80004000-0x80007000.

Without the test code: 24 modules, none in a first 32 KiB, in 15 boots
and after 50 rounds of reloading five modules, and 10 minutes of
exec-heavy load without an oops or a warning.

Co-developed-by: Mieczyslaw Nalewaj <[email protected]>
Signed-off-by: Mieczyslaw Nalewaj <[email protected]>
Signed-off-by: Marco Martins <[email protected]>
Co-Authored-By: Claude Fable 5.2 <[email protected]>
Link: openwrt/openwrt#26650
Signed-off-by: Robert Marko <[email protected]>

GitLab-source: c06ccca080872d054e564a79b652d4343ea3a1fc
Source delta sanitized for publication; author attribution preserved.
hnt510-great pushed a commit to hnt510-great/openwrt-systemd-host that referenced this pull request Nov 18, 2026
The first 32 KiB of the kernel image are kept free of code because of
the MIPS 24K EPC erratum, and the module allocator keeps module code
out of them, so hand them back to the page allocator as ordinary data
pages. The first page stays reserved, as the kernel reserves it on
every CPU, which leaves 28 KiB. Nothing is freed if the exception
vectors are in the padding.

Signed-off-by: Mieczyslaw Nalewaj <[email protected]>
[Marco: drop the second message, free_reserved_area() logs one; say
 what keeps code out of the pages; keep the first page; move it from
 mt7620.c to of.c, for every SoC with RALINK_PAD_FIRST_32K; add it as a
 patch file]
Signed-off-by: Marco Martins <[email protected]>
Co-Authored-By: Claude Fable 5.2 <[email protected]>
Link: openwrt/openwrt#26650
Signed-off-by: Robert Marko <[email protected]>

GitLab-source: d1f05be244cfe173407bc592595b5ce3707d9aed
Source delta sanitized for publication; author attribution preserved.
hnt510-great pushed a commit to hnt510-great/openwrt-systemd-host that referenced this pull request Nov 18, 2026
The 24KEc of the RT5350 shows the same EPC fault as the MT7620's,
measured from user space: about 38 wrong EPCs a second with J and with
JAL from 0x20003f90 to 0x22e70040, none with JALR. rt305x also links
the kernel at 0x80000000, so turn RALINK_PAD_FIRST_32K on for
SOC_RT305X too. The
padding is freed again as on mt7620, and the module allocator already
keeps module code out of it on every 24K core.

Signed-off-by: Marco Martins <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Link: openwrt/openwrt#26650
Signed-off-by: Robert Marko <[email protected]>

GitLab-source: 656925183532053e8c1757b382bddc8ef3779494
Source delta sanitized for publication; author attribution preserved.
hnt510-great pushed a commit to hnt510-great/openwrt-systemd-host that referenced this pull request Nov 18, 2026
Commit 7423841 ("ramips: disable CONFIG_LIST_HARDENED on MT7620")
disabled it after the crashes in #16396, whose cause was unknown. Their
traces show the MIPS 24K EPC erratum: ra in the first 32 KiB of KSEG0
and the EPC in the segment below (0x7...). The checks only made the
crashes more likely, with more cold code and calls in those 32 KiB.
The mt76x8 subtarget kept them, and the MT7628 does not show the fault.

With kernel and module code kept out of the first 32 KiB of a segment,
it can be enabled again, as on the other ramips subtargets. On a
TL-WPA7510 v2 it adds 50 KiB of kernel text, 13 KiB to the compressed
kernel and 2 KiB to the root file system, the modules' checks.

Tested on a TL-WPA7510 v2 booted from RAM: 10 minutes of the reproducer
from #16396, a loop of `iw <ap> station dump` on both access points
(2200 rounds), together with exec-heavy load (24.5k rounds), without an
oops or a warning. Tested on a TP-Link Archer C5 v4 by Mieczyslaw
Nalewaj.

Suggested-by: Mieczyslaw Nalewaj <[email protected]>
Tested-by: Mieczyslaw Nalewaj <[email protected]>
Signed-off-by: Marco Martins <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Link: openwrt/openwrt#26650
Signed-off-by: Robert Marko <[email protected]>

GitLab-source: 6df68042d1db716eabca0754880876cc8a408c85
Source delta sanitized for publication; author attribution preserved.
Vladdrako pushed a commit to Vladdrako/openwrt that referenced this pull request Nov 19, 2026
The 24KEc of the RT5350 shows the same EPC fault as the MT7620's,
measured from user space: about 38 wrong EPCs a second with J and with
JAL from 0x20003f90 to 0x22e70040, none with JALR. rt305x also links
the kernel at 0x80000000, so turn RALINK_PAD_FIRST_32K on for
SOC_RT305X too. The
padding is freed again as on mt7620, and the module allocator already
keeps module code out of it on every 24K core.

Signed-off-by: Marco Martins <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Link: openwrt#26650
Signed-off-by: Robert Marko <[email protected]>
robimarko pushed a commit to MarcoMartins86/openwrt that referenced this pull request Nov 19, 2026
The MIPS 24K core of the MT7620 can save a wrong EPC for an interrupt
taken at the first instruction of a J or JAL target, when the jump is
in the first 32 KiB of a 256 MiB segment. The return from the
interrupt then goes to 0x7... instead of 0x8..., and the kernel dies
with "Unable to handle kernel paging request". ramips links the kernel
at 0x80000000, so its first 32 KiB hold such jumps.

The fault was measured without Linux, from a program started by the
boot loader, and the 24Kc of an AR9132 shows it too. MediaTek's SDK
kernels leave the same 32 KiB empty for the MT7620 and the MT7628, but
an MT7628 doesn't show the fault, so the padding comes with
RALINK_PAD_FIRST_32K. It has no prompt, so it cannot be switched off,
and is on for SOC_MT7620 kernels without the MT76x8 pin controller: the
mt7620 subtarget, not mt76x8.

On the TL-WPA7510 v2, 6 of 17 boots crashed in
try_to_generate_entropy(). With the kernel code moved behind
0x80008000, no J or JAL is left in the first 32 KiB; with the whole
series, 15 boots and 10 minutes of load gave no oops.

Suggested-by: Mieczyslaw Nalewaj <[email protected]>
Signed-off-by: Marco Martins <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Link: openwrt#26650
(cherry picked from commit 3c2a81d)
Link: openwrt#26784
Signed-off-by: Robert Marko <[email protected]>
robimarko pushed a commit to MarcoMartins86/openwrt that referenced this pull request Nov 19, 2026
Patches 330 and 331 avoided direct JALs at the call sites that had been
seen to crash: the random helpers of get_random_u32_below() and
blkcg_maybe_throttle_current() in resume_user_mode_work(). They only
applied to the mt7620 subtarget, and they did not cover the jumps in
random.c that crash the boot in try_to_generate_entropy().

With the kernel code out of the first 32 KiB, no jump is left where the
erratum gives a wrong EPC, so drop them. 332, which skips
arch_align_stack() on exec, stays: it is an optimization of its own,
not one of the jump workarounds.

Tested on a TL-WPA7510 v2 with the whole series: no oops in 15 boots,
nor in 10 minutes of exec-heavy load (24.5k rounds) together with the
reproducer from openwrt#16396.

Signed-off-by: Marco Martins <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Link: openwrt#26650
(cherry picked from commit 959a7f9)
Link: openwrt#26784
Signed-off-by: Robert Marko <[email protected]>
robimarko pushed a commit to MarcoMartins86/openwrt that referenced this pull request Nov 19, 2026
MIPS 24K cores can save a wrong EPC for an interrupt taken at the
target of a J or JAL in the first 32 KiB of a 256 MiB segment, and the
return from the interrupt then goes to the segment below. This was
measured on the 24KEc of an MT7620A and an RT5350 and on the 24Kc of an
AR9132 and a QCA9531, though not on the 24KEc of an MT7628.

OpenWrt's module allocator (301-02) runs module and BPF JIT code from
the KSEG0 address of the pages it gets, and falls back to vmalloc,
whose area starts at 0xc0000000. On ath79 the kernel starts at
0x80060000, and what the early boot allocations leave free of the RAM
below it goes to the page allocator; on mt7620 the next commit frees
most of the padding that head.S puts there.

Add 301-03: on the 24K cores known to have the fault, those with a PRId
below 0x00019655 (every 24Kc, and the 24KEc before V5.5, which leaves
out the MT7628), hold the blocks the page allocator offers in the first
32 KiB of a segment and ask again, and take the vmalloc fallback from
the rest of the first segment of its area. module_finalize() warns if
module code starts there all the same. Other cores keep the allocator
as it is.

Tested on a TL-WPA7510 v2 with the whole series, in a test build that
took code blocks from the freed movable pages: the seven freed pages
were offered to each 4 KiB block and held, 28 times in all, and none of
9 blocks landed in the first 32 KiB. With the test of the address
switched off, 4 of 9 blocks landed at 0x80004000-0x80007000.

Without the test code: 24 modules, none in a first 32 KiB, in 15 boots
and after 50 rounds of reloading five modules, and 10 minutes of
exec-heavy load without an oops or a warning.

Co-developed-by: Mieczyslaw Nalewaj <[email protected]>
Signed-off-by: Mieczyslaw Nalewaj <[email protected]>
Signed-off-by: Marco Martins <[email protected]>
Co-Authored-By: Claude Fable 5.2 <[email protected]>
Link: openwrt#26650
(cherry picked from commit c06ccca)
Link: openwrt#26784
Signed-off-by: Robert Marko <[email protected]>
robimarko pushed a commit to MarcoMartins86/openwrt that referenced this pull request Nov 19, 2026
The first 32 KiB of the kernel image are kept free of code because of
the MIPS 24K EPC erratum, and the module allocator keeps module code
out of them, so hand them back to the page allocator as ordinary data
pages. The first page stays reserved, as the kernel reserves it on
every CPU, which leaves 28 KiB. Nothing is freed if the exception
vectors are in the padding.

Signed-off-by: Mieczyslaw Nalewaj <[email protected]>
[Marco: drop the second message, free_reserved_area() logs one; say
 what keeps code out of the pages; keep the first page; move it from
 mt7620.c to of.c, for every SoC with RALINK_PAD_FIRST_32K; add it as a
 patch file]
Signed-off-by: Marco Martins <[email protected]>
Co-Authored-By: Claude Fable 5.2 <[email protected]>
Link: openwrt#26650
(cherry picked from commit d1f05be)
Link: openwrt#26784
Signed-off-by: Robert Marko <[email protected]>
robimarko pushed a commit to MarcoMartins86/openwrt that referenced this pull request Nov 19, 2026
The 24KEc of the RT5350 shows the same EPC fault as the MT7620's,
measured from user space: about 38 wrong EPCs a second with J and with
JAL from 0x20003f90 to 0x22e70040, none with JALR. rt305x also links
the kernel at 0x80000000, so turn RALINK_PAD_FIRST_32K on for
SOC_RT305X too. The
padding is freed again as on mt7620, and the module allocator already
keeps module code out of it on every 24K core.

Signed-off-by: Marco Martins <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Link: openwrt#26650
(cherry picked from commit 6569251)
Link: openwrt#26784
Signed-off-by: Robert Marko <[email protected]>
robimarko pushed a commit to MarcoMartins86/openwrt that referenced this pull request Nov 19, 2026
Commit 7423841 ("ramips: disable CONFIG_LIST_HARDENED on MT7620")
disabled it after the crashes in openwrt#16396, whose cause was unknown. Their
traces show the MIPS 24K EPC erratum: ra in the first 32 KiB of KSEG0
and the EPC in the segment below (0x7...). The checks only made the
crashes more likely, with more cold code and calls in those 32 KiB.
The mt76x8 subtarget kept them, and the MT7628 does not show the fault.

With kernel and module code kept out of the first 32 KiB of a segment,
it can be enabled again, as on the other ramips subtargets. On a
TL-WPA7510 v2 it adds 50 KiB of kernel text, 13 KiB to the compressed
kernel and 2 KiB to the root file system, the modules' checks.

Tested on a TL-WPA7510 v2 booted from RAM: 10 minutes of the reproducer
from openwrt#16396, a loop of `iw <ap> station dump` on both access points
(2200 rounds), together with exec-heavy load (24.5k rounds), without an
oops or a warning. Tested on a TP-Link Archer C5 v4 by Mieczyslaw
Nalewaj.

Suggested-by: Mieczyslaw Nalewaj <[email protected]>
Tested-by: Mieczyslaw Nalewaj <[email protected]>
Signed-off-by: Marco Martins <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Link: openwrt#26650
(cherry picked from commit 6df6804)
Link: openwrt#26784
Signed-off-by: Robert Marko <[email protected]>
@MarcoMartins86
MarcoMartins86 deleted the ramips-mt7620-first-32k branch November 20, 2026 01:01
liudf0716 pushed a commit to liudf0716/chawrt that referenced this pull request Nov 23, 2026
The MIPS 24K core of the MT7620 can save a wrong EPC for an interrupt
taken at the first instruction of a J or JAL target, when the jump is
in the first 32 KiB of a 256 MiB segment. The return from the
interrupt then goes to 0x7... instead of 0x8..., and the kernel dies
with "Unable to handle kernel paging request". ramips links the kernel
at 0x80000000, so its first 32 KiB hold such jumps.

The fault was measured without Linux, from a program started by the
boot loader, and the 24Kc of an AR9132 shows it too. MediaTek's SDK
kernels leave the same 32 KiB empty for the MT7620 and the MT7628, but
an MT7628 doesn't show the fault, so the padding comes with
RALINK_PAD_FIRST_32K. It has no prompt, so it cannot be switched off,
and is on for SOC_MT7620 kernels without the MT76x8 pin controller: the
mt7620 subtarget, not mt76x8.

On the TL-WPA7510 v2, 6 of 17 boots crashed in
try_to_generate_entropy(). With the kernel code moved behind
0x80008000, no J or JAL is left in the first 32 KiB; with the whole
series, 15 boots and 10 minutes of load gave no oops.

Suggested-by: Mieczyslaw Nalewaj <[email protected]>
Signed-off-by: Marco Martins <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Link: openwrt/openwrt#26650
(cherry picked from commit 3c2a81d482d845cb01190c7311dd8f4b5629ff5b)
Link: openwrt/openwrt#26784
Signed-off-by: Robert Marko <[email protected]>
liudf0716 pushed a commit to liudf0716/chawrt that referenced this pull request Nov 23, 2026
Patches 330 and 331 avoided direct JALs at the call sites that had been
seen to crash: the random helpers of get_random_u32_below() and
blkcg_maybe_throttle_current() in resume_user_mode_work(). They only
applied to the mt7620 subtarget, and they did not cover the jumps in
random.c that crash the boot in try_to_generate_entropy().

With the kernel code out of the first 32 KiB, no jump is left where the
erratum gives a wrong EPC, so drop them. 332, which skips
arch_align_stack() on exec, stays: it is an optimization of its own,
not one of the jump workarounds.

Tested on a TL-WPA7510 v2 with the whole series: no oops in 15 boots,
nor in 10 minutes of exec-heavy load (24.5k rounds) together with the
reproducer from #16396.

Signed-off-by: Marco Martins <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Link: openwrt/openwrt#26650
(cherry picked from commit 959a7f9ea96cff203b274c151561efa086c31213)
Link: openwrt/openwrt#26784
Signed-off-by: Robert Marko <[email protected]>
liudf0716 pushed a commit to liudf0716/chawrt that referenced this pull request Nov 23, 2026
MIPS 24K cores can save a wrong EPC for an interrupt taken at the
target of a J or JAL in the first 32 KiB of a 256 MiB segment, and the
return from the interrupt then goes to the segment below. This was
measured on the 24KEc of an MT7620A and an RT5350 and on the 24Kc of an
AR9132 and a QCA9531, though not on the 24KEc of an MT7628.

OpenWrt's module allocator (301-02) runs module and BPF JIT code from
the KSEG0 address of the pages it gets, and falls back to vmalloc,
whose area starts at 0xc0000000. On ath79 the kernel starts at
0x80060000, and what the early boot allocations leave free of the RAM
below it goes to the page allocator; on mt7620 the next commit frees
most of the padding that head.S puts there.

Add 301-03: on the 24K cores known to have the fault, those with a PRId
below 0x00019655 (every 24Kc, and the 24KEc before V5.5, which leaves
out the MT7628), hold the blocks the page allocator offers in the first
32 KiB of a segment and ask again, and take the vmalloc fallback from
the rest of the first segment of its area. module_finalize() warns if
module code starts there all the same. Other cores keep the allocator
as it is.

Tested on a TL-WPA7510 v2 with the whole series, in a test build that
took code blocks from the freed movable pages: the seven freed pages
were offered to each 4 KiB block and held, 28 times in all, and none of
9 blocks landed in the first 32 KiB. With the test of the address
switched off, 4 of 9 blocks landed at 0x80004000-0x80007000.

Without the test code: 24 modules, none in a first 32 KiB, in 15 boots
and after 50 rounds of reloading five modules, and 10 minutes of
exec-heavy load without an oops or a warning.

Co-developed-by: Mieczyslaw Nalewaj <[email protected]>
Signed-off-by: Mieczyslaw Nalewaj <[email protected]>
Signed-off-by: Marco Martins <[email protected]>
Co-Authored-By: Claude Fable 5.2 <[email protected]>
Link: openwrt/openwrt#26650
(cherry picked from commit c06ccca080872d054e564a79b652d4343ea3a1fc)
Link: openwrt/openwrt#26784
Signed-off-by: Robert Marko <[email protected]>
liudf0716 pushed a commit to liudf0716/chawrt that referenced this pull request Nov 23, 2026
The first 32 KiB of the kernel image are kept free of code because of
the MIPS 24K EPC erratum, and the module allocator keeps module code
out of them, so hand them back to the page allocator as ordinary data
pages. The first page stays reserved, as the kernel reserves it on
every CPU, which leaves 28 KiB. Nothing is freed if the exception
vectors are in the padding.

Signed-off-by: Mieczyslaw Nalewaj <[email protected]>
[Marco: drop the second message, free_reserved_area() logs one; say
 what keeps code out of the pages; keep the first page; move it from
 mt7620.c to of.c, for every SoC with RALINK_PAD_FIRST_32K; add it as a
 patch file]
Signed-off-by: Marco Martins <[email protected]>
Co-Authored-By: Claude Fable 5.2 <[email protected]>
Link: openwrt/openwrt#26650
(cherry picked from commit d1f05be244cfe173407bc592595b5ce3707d9aed)
Link: openwrt/openwrt#26784
Signed-off-by: Robert Marko <[email protected]>
liudf0716 pushed a commit to liudf0716/chawrt that referenced this pull request Nov 23, 2026
The 24KEc of the RT5350 shows the same EPC fault as the MT7620's,
measured from user space: about 38 wrong EPCs a second with J and with
JAL from 0x20003f90 to 0x22e70040, none with JALR. rt305x also links
the kernel at 0x80000000, so turn RALINK_PAD_FIRST_32K on for
SOC_RT305X too. The
padding is freed again as on mt7620, and the module allocator already
keeps module code out of it on every 24K core.

Signed-off-by: Marco Martins <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Link: openwrt/openwrt#26650
(cherry picked from commit 656925183532053e8c1757b382bddc8ef3779494)
Link: openwrt/openwrt#26784
Signed-off-by: Robert Marko <[email protected]>
liudf0716 pushed a commit to liudf0716/chawrt that referenced this pull request Nov 23, 2026
Commit 7423841 ("ramips: disable CONFIG_LIST_HARDENED on MT7620")
disabled it after the crashes in #16396, whose cause was unknown. Their
traces show the MIPS 24K EPC erratum: ra in the first 32 KiB of KSEG0
and the EPC in the segment below (0x7...). The checks only made the
crashes more likely, with more cold code and calls in those 32 KiB.
The mt76x8 subtarget kept them, and the MT7628 does not show the fault.

With kernel and module code kept out of the first 32 KiB of a segment,
it can be enabled again, as on the other ramips subtargets. On a
TL-WPA7510 v2 it adds 50 KiB of kernel text, 13 KiB to the compressed
kernel and 2 KiB to the root file system, the modules' checks.

Tested on a TL-WPA7510 v2 booted from RAM: 10 minutes of the reproducer
from #16396, a loop of `iw <ap> station dump` on both access points
(2200 rounds), together with exec-heavy load (24.5k rounds), without an
oops or a warning. Tested on a TP-Link Archer C5 v4 by Mieczyslaw
Nalewaj.

Suggested-by: Mieczyslaw Nalewaj <[email protected]>
Tested-by: Mieczyslaw Nalewaj <[email protected]>
Signed-off-by: Marco Martins <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Link: openwrt/openwrt#26650
(cherry picked from commit 6df68042d1db716eabca0754880876cc8a408c85)
Link: openwrt/openwrt#26784
Signed-off-by: Robert Marko <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kernel pull request/issue with Linux kernel related changes target/ramips pull request/issue for ramips target

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants