Skip to content

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

Open
XECDesign wants to merge 2 commits into
raspberrypi:rpi-6.18.yfrom
XECDesign:rpi-6.18.y-uaccess-livelock
Open

XECDesign wants to merge 2 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.

…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>
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.

1 participant