Repository navigation
ramips: work around EPC erratum in MIPS 24K cores - #26650
Conversation
adb79cb to
9fd35b7
Compare
|
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. |
|
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:
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 |
|
Ping @dormancygrace |
DragonBluep
left a comment
There was a problem hiding this comment.
TP-Link's GPL code shows why: MediaTek's SDK adds . = . + 0x8000; at the start of .text in arch/mips/kernel/vmlinux.lds.S.
Not tested: MT7628 hardware,
MT7628 is working well as before with this patch. This PR LGTM.
|
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 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. |
|
Have you tested https://github.com/user-attachments/files/34721109/draft.txt ? Patch: 335-mt7620-free-the-padding-in-front-of-the-kernel-text.patch
|
9fd35b7 to
bee1a9c
Compare
|
Thanks @dormancygrace for testing it on the WE826-T2, and @DragonBluep for checking MT7628.
I also took the bot's suggestion: the comment and the patch description now say J or JAL. |
bee1a9c to
a0a50d5
Compare
|
@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:
I made two changes to your patch, noted in the commit: I dropped the second message, since The comment next to the |
|
Should this also be sent upstream somewhere? |
|
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]; |
There was a problem hiding this comment.
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.
| + 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) |
There was a problem hiding this comment.
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
|
I was working on the same thing in parallel.
@MarcoMartins86 Please take a look at this. |
| * Reserved space for exception handlers. | ||
| * Necessary for machines which link their kernels at KSEG0. | ||
| */ | ||
| +#ifdef CONFIG_SOC_MT7620 |
There was a problem hiding this comment.
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?
a0a50d5 to
219644c
Compare
|
Thanks for the reviews. @DragonBluep, @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 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. @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. |
But no one has confirmed that the error occurs on this chipset. |
|
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.
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 |
|
@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:
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
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): With ujal.c (build:
|
|
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. |
|
Edit: auto_awesome |
|
My complete patch, ready for testing: namiltd#147 |
|
@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 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":
The vmalloc fallback: first segment only. The range ends at 0xd0000000, so no later segment start can be reached and the 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 |
|
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:
Two smaller points:
|
|
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:
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 If it holds up, it could go in as a separate commit that reverts the MT7620 exception and refers to #16396. Edit: Just as a side note, I also tested |
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]>
467e0c1 to
6df6804
Compare
|
Now, quickly following up, create a PR for the openwrt-25.12 branch (cherry picked from). |
|
Thanks! Rebased on top of main and merged! |
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.
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.
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.
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.
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.
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.
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]>
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]>
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]>
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]>
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]>
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]>
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]>
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]>
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]>
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]>
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]>
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]>
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]>
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:
ramips: mt7620: keep the kernel code out of the first 32 KiB:RALINK_PAD_FIRST_32Kmakes head.S use.fill 0x8000instead 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).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_MT7620is off on mt76x8). 332, which skipsarch_align_stack()on exec, stays: it is an optimization of its own.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, andmodule_finalize()warns if module code starts there all the same. Based on @namiltd's changes to 301-02.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 withRALINK_PAD_FIRST_32K.ramips: rt305x: keep the kernel code out of the first 32 KiB: turnsRALINK_PAD_FIRST_32Kon for SOC_RT305X, as @DragonBluep measured the fault on an RT5350. Built, not booted yet.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, withrajust 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
goinstalls 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):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):
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:
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.textinarch/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:
iw <ap> station dumpon both access points (2200 rounds), together with exec-heavy load (24.5k rounds), without an oops or a warningRALINK_PAD_FIRST_32Kcomes out on for rt305x, with the kernel code at 0x80008000 and the free, and off for mt76x8, at 0x80000400 as before, without the freeThe 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/refreshleaves 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