Repository navigation
ARM: uaccess_with_memcpy: don't retry forever when a page can't be pinned - #7811
Conversation
Review: #7811 ARM: uaccess_with_memcpy: don't retry forever when a page can't be pinnedThe diagnosis is sound. On ARM32 the hardware PTE is only writable when The fix looks correct:
Suggestions (none blocking)
VerdictLooks good to me. Point 1 is optional. |
|
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. |
…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>
df7060b to
fc14320
Compare
|
I've updated the comment and included the reversion, but left the 2nd commit separate as a pointer to the existing |
|
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. |
|
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. |
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.