Skip to content

ARM: uaccess_with_memcpy: don't retry forever when a page can't be pinned - #7811

Merged
pelwell merged 3 commits into
raspberrypi:rpi-6.18.yfrom
XECDesign:rpi-6.18.y-uaccess-livelock
Dec 1, 2026
Merged

pelwell merged 3 commits into
raspberrypi:rpi-6.18.yfrom
XECDesign:rpi-6.18.y-uaccess-livelock

Conversation

@XECDesign

Copy link
Copy Markdown
Contributor

Apologies for an AI-written PR, but it seems to have done a much better job than I would've.

Problem

On 32-bit kernels with CONFIG_UACCESS_WITH_MEMCPY (bcm2709_defconfig, bcmrpi_defconfig), a task can spin in the kernel at 100 % system time, take no page faults, and ignore kill -9. It shows up as apt/dpkg hanging on a Zero 2 W running trixie armhf (6.18.50 and 6.18.65 v7).

The spin is in the retry loop of __copy_to_user_memcpy() and __clear_user_memset() (and the downstream __copy_from_user_memcpy()):

while (!pin_page_for_write(to, &pte, &ptl)) {
	...
	if (__put_user(0, (char __user *)to))
		goto out;
	...
}

ARM32 has no hardware young bit. The generic ptep_test_and_clear_young() clears L_PTE_YOUNG, which zeroes the hardware PTE, but it doesn't flush the TLB. If the CPU still has a TLB entry for the page, __put_user() succeeds without faulting. The PTE therefore stays old, pin_page_for_write() keeps failing, and the loop never ends.

Evidence on the Zero 2 W:

  • The spinning task took no faults.
  • Moving it to another CPU, or unmapping the page, released it.
  • Flushing that TLB entry after lru_gen_look_around() (with a kretprobe) took the apt stalls from 3 of 4 rounds to 0 of 14.
  • Making ptep_test_and_clear_young() flush also stopped them.

Reproducer:

  1. Dirty an anonymous page.
  2. Write 1 to /proc/self/clear_refs.
  3. Without touching other memory, read() 64 bytes or more from a pipe or /dev/zero into that page, or write() 672 bytes or more from it on v6.

History

  • aaf6a06c3741 ("mm/vmscan: Maintain TLB coherency in LRU code", Sep 2024) worked around this on BCM2837 by making the neighbour loop in lru_gen_look_around() flush.
  • Upstream 1d4832becdc2 ("mm: multi-gen LRU: use {ptep,pmdp}_clear_young_notify()", Oct 2024, in 6.12) moved the clear of the target PTE to a non-flushing call at the top of lru_gen_look_around(). The neighbour loop now skips that PTE, so 6.12 and 6.18 have the hang again.
  • The retry loop itself dates from 39ec58f3fea4 (2009) and is unchanged in mainline, so this is an upstream bug as well.

This PR leaves aaf6a06c3741 in place.

Why fix the loop

Option Result
Fix the uaccess loop (this PR) Covers every non-flushing young clear: MGLRU, clear_refs, page_idle, DAMON.
ARM ptep_test_and_clear_young() flushes Fixes it, but the clear_refs write was about 1.9× slower (7.3 vs 3.9 ms for 64 MiB). Not measured on cores that need IPIs for TLB invalidation.
Flushing clear of the target PTE in lru_gen_look_around() MGLRU only; clear_refs still hangs.
Drop UACCESS_WITH_MEMCPY Fixes it, changes clear_user partial-copy return values, and the speed effect is mixed.

Change

Two commits:

  1. ARM: uaccess_with_memcpy: don't retry forever when a page can't be pinned: the copy_to_user and clear_user loops, which are upstream code (Fixes: 39ec58f3fea4).
  2. fixup! Improve __copy_to_user and __copy_from_user performance: the same change in the downstream __copy_from_user_memcpy(), to be squashed into f9680c2e9df8.

Each page gets one fault-in attempt. If the page still can't be pinned after the __put_user()/__get_user(), the rest of the copy goes to the standard fault-handling routine. A page that wasn't present still faults once and then takes the memcpy/memset path, as before. The clear_user fallback goes a page at a time, because __clear_user_std() returns the whole length on a fault (see below).

Testing

Kernels were built from rpi-6.18.y at 6.18.37 (7136f2b7245f), where arch/arm/lib/uaccess_with_memcpy.c is the same as in this branch's base. The tested code is identical to this branch with both commits applied. The split into two commits came afterwards: commit 1 on its own was compile-tested for v6 and v7 but not booted. Each kernel was tested on one boot, unless noted.

Zero 2 W, v7 (bcm2709_defconfig):

  • Reproducer: baseline hung in 7 and 6 of 9 cases (two boots); fixed kernel 0 of 9.
  • Return values, errno and data in 22 edge cases (page-crossing copies, copies into PROT_NONE or read-only memory, unmapped addresses): same as baseline, with and without aging by clear_refs.
  • Speed:
    • /dev/zero into fresh memory: 454 MiB/s (range 431–454) vs baseline 428.
    • Warm copy_to_user: at or above baseline.
    • Warm clear_user, 256 B / 4 KiB: 3.5 % / 2.8 % below baseline. The fast path's code differs by one register move per page, so this is unexplained; it may be code layout or boot-to-boot noise.

Zero W, v6 (bcmrpi_defconfig, single core):

  • On baseline the loop does spin, but it releases itself. In runs where the TLB entry survived the clear_refs write, the copy took 6–358 ms in 5 of 6 cases instead of the usual 12–31 µs, with extra kernel-mode faults. With the fix, all 10 such runs took 20–38 µs and had no extra kernel-mode faults in 9 of them.
  • Return values in 32 edge cases, including 10 that write() from the buffer and so go through __copy_from_user_memcpy(): same as baseline, with and without aging.
  • Speed: within 0.5–3 % of baseline, mostly slightly below; not separable from boot-to-boot noise.

Not in this PR

  • __clear_user_std() (arch/arm/lib/clear_user.S) returns the whole length it was given on a fault, not the bytes left, despite its header comment. That's why the clear_user fallback goes a page at a time. It also makes arm_clear_user() inexact for calls under 64 bytes today. Fixing it changes clear_user() on every ARM32 kernel, so it's an upstream patch.
  • Pre-existing issues in uaccess_with_memcpy.c:
    • arm_copy_from_user() compares against COPY_TO_USER_THRESHOLD (672 on v6) instead of COPY_FROM_USER_THRESHOLD (1600).
    • pin_page_for_read() has no NULL check after pte_offset_map_lock().
    • The copy-from path uses in_atomic(); copy-to uses faulthandler_disabled().
    • pin_page_for_write() tests pmd_bad() before the pmd_leaf() branch.

@popcornmix

Copy link
Copy Markdown
Collaborator

Review: #7811 ARM: uaccess_with_memcpy: don't retry forever when a page can't be pinned

The diagnosis is sound. On ARM32 the hardware PTE is only writable when L_PTE_YOUNG, L_PTE_DIRTY and write are all set, and ptep_test_and_clear_young() doesn't flush the TLB. So a __put_user()/__get_user() that succeeds without faulting is possible only through a stale TLB entry. Once that happens, nothing will ever make pin_page_for_*() succeed, and the old loop spins forever.

The fix looks correct:

  • retried is declared inside the outer loop, so each page gets its own fault-in attempt, as before.
  • The mmap lock is already dropped on the fallback path, and goto out skips the unlock, so the locking is balanced in both the atomic and non-atomic cases.
  • The __clear_user_std() claim is right: the fixup in clear_user.S (9001: ldmfd sp!, {r0, pc}) returns the original length, so going a page at a time is needed.
  • If the page is aged again between the fault and the next pin (rare), the code now takes the slow path instead of looping. It is still correct.
  • The diff applies cleanly to rpi-7.4.y too; the file there is the same as the PR's base.

Suggestions (none blocking)

  1. The fallback covers the whole rest of the copy, not just the stuck page. One stale page in a large read() sends the remaining bytes through the slow __copy_*_user_std(). It would be faster to copy just this page (tocopy) with the std routine, take the mmap lock again and carry on with the outer loop. clear_user already goes a page at a time, so the three paths could share that shape. This only matters for speed, and the stale-TLB case should be rare, so this can wait.

  2. Comment wording. "Faulting the page in succeeded but it still can't be pinned" doesn't describe the stale-TLB case, where nothing faulted: the write simply went through. Something like "The write succeeded but the page still can't be pinned (e.g. young bit cleared without a TLB flush)" is more accurate. The comment could also be shorter; the commit message already explains why.

  3. Commit 1 should go upstream (to rmk / linux-arm-kernel), since the loop is mainline code. It has Fixes:. Consider Cc: stable.

  4. Commit 1 on its own was only compile-tested. That's fine as long as the fixup is squashed before it lands, but worth stating when merging.

Verdict

Looks good to me. Point 1 is optional.

@pelwell

pelwell commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

I've been testing this patch on real hardware and it does what is expected. Once merged I will follow it up with a reversion of the earlier patch that no longer works (1d4832b) because it affects other architectures, is more fragile, and doesn't really improve performance.

XECDesign and others added 3 commits December 1, 2026 16:11
…nned

When pin_page_for_write() fails, __copy_to_user_memcpy() and
__clear_user_memset() write one byte with __put_user() and try again,
expecting the write to fault and update the PTE so that the next pin
succeeds. If the young bit was cleared without a TLB flush
(ptep_test_and_clear_young(), as used by MGLRU and
/proc/<pid>/clear_refs), the write can go through a stale TLB entry
without faulting. The PTE then never changes, and the loop spins with no
signal check, so the task can't be killed.

If the pin still fails after the write, do the rest with the standard
routine, which handles faults. A page that wasn't present still faults
once and then takes the memcpy/memset path, as before.

Fixes: 39ec58f ("[ARM] alternative copy_to_user/clear_user implementation")
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Serge Schneider <serge@raspberrypi.com>
__copy_from_user_memcpy() has the same retry loop as
__copy_to_user_memcpy(): if __get_user() goes through a stale TLB entry
without faulting, pin_page_for_read() keeps failing. Give it the same
single retry, then do the rest with __copy_from_user_std().

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Serge Schneider <serge@raspberrypi.com>
This reverts commit aaf6a06.

The reverted patch had become ineffective since [1] because the target
PTE is cleared without a flush. Superseded by the uaccess_with_memcpy fix.

[1] commit 1d4832b ("mm: multi-gen LRU: use
    {ptep,pmdp}_clear_young_notify()")

Signed-off-by: Phil Elwell <phil@raspberrypi.com>
@pelwell
pelwell force-pushed the rpi-6.18.y-uaccess-livelock branch from df7060b to fc14320 Compare December 1, 2026 16:14
@pelwell

pelwell commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

I've updated the comment and included the reversion, but left the 2nd commit separate as a pointer to the existing Improve ... performance commit that these are both altering.

@XECDesign

Copy link
Copy Markdown
Contributor Author

I did have a per-page version earlier. It copies just the stuck page with the standard routine and then carries on with the fast path for the rest. It passed the same reproducer and edge-case tests on the Zero 2 W. I went for the "rest of the copy" version because it's a smaller diff (46 lines against about 77), and the stale case should be rare. On the A53 I couldn't see a speed difference between the two. The test there ages every page, though, so it never exercises the case you mean: a long copy where only one page is stale. I haven't measured that on a Pi 1. If you'd like the per-page version, I'm happy to swap it in.

@pelwell

pelwell commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

I think this version has the advantage of being easy to verify by eye, and taking the slow path should be rare.

Although AI-generated reviews are useful, I would find it helpful if the submitter gave their own opinion about any suggestions that are included.

@pelwell
pelwell merged commit adf70b7 into raspberrypi:rpi-6.18.y Dec 1, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants