Repository navigation
Conversation
…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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 ignorekill -9. It shows up asapt/dpkghanging on a Zero 2 W running trixie armhf (6.18.50 and 6.18.65v7).The spin is in the retry loop of
__copy_to_user_memcpy()and__clear_user_memset()(and the downstream__copy_from_user_memcpy()):ARM32 has no hardware young bit. The generic
ptep_test_and_clear_young()clearsL_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:
lru_gen_look_around()(with a kretprobe) took theaptstalls from 3 of 4 rounds to 0 of 14.ptep_test_and_clear_young()flush also stopped them.Reproducer:
1to/proc/self/clear_refs.read()64 bytes or more from a pipe or/dev/zerointo that page, orwrite()672 bytes or more from it onv6.History
aaf6a06c3741("mm/vmscan: Maintain TLB coherency in LRU code", Sep 2024) worked around this on BCM2837 by making the neighbour loop inlru_gen_look_around()flush.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 oflru_gen_look_around(). The neighbour loop now skips that PTE, so 6.12 and 6.18 have the hang again.39ec58f3fea4(2009) and is unchanged in mainline, so this is an upstream bug as well.This PR leaves
aaf6a06c3741in place.Why fix the loop
clear_refs, page_idle, DAMON.ptep_test_and_clear_young()flushesclear_refswrite was about 1.9× slower (7.3 vs 3.9 ms for 64 MiB). Not measured on cores that need IPIs for TLB invalidation.lru_gen_look_around()clear_refsstill hangs.UACCESS_WITH_MEMCPYclear_userpartial-copy return values, and the speed effect is mixed.Change
Two commits:
ARM: uaccess_with_memcpy: don't retry forever when a page can't be pinned: thecopy_to_userandclear_userloops, which are upstream code (Fixes: 39ec58f3fea4).fixup! Improve __copy_to_user and __copy_from_user performance: the same change in the downstream__copy_from_user_memcpy(), to be squashed intof9680c2e9df8.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. Theclear_userfallback 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.yat 6.18.37 (7136f2b7245f), wherearch/arm/lib/uaccess_with_memcpy.cis 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 forv6andv7but not booted. Each kernel was tested on one boot, unless noted.Zero 2 W,
v7(bcm2709_defconfig):PROT_NONEor read-only memory, unmapped addresses): same as baseline, with and without aging byclear_refs./dev/zerointo fresh memory: 454 MiB/s (range 431–454) vs baseline 428.copy_to_user: at or above baseline.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):clear_refswrite, 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.write()from the buffer and so go through__copy_from_user_memcpy(): same as baseline, with and without aging.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 theclear_userfallback goes a page at a time. It also makesarm_clear_user()inexact for calls under 64 bytes today. Fixing it changesclear_user()on every ARM32 kernel, so it's an upstream patch.uaccess_with_memcpy.c:arm_copy_from_user()compares againstCOPY_TO_USER_THRESHOLD(672 onv6) instead ofCOPY_FROM_USER_THRESHOLD(1600).pin_page_for_read()has no NULL check afterpte_offset_map_lock().in_atomic(); copy-to usesfaulthandler_disabled().pin_page_for_write()testspmd_bad()before thepmd_leaf()branch.