From 4c92045bb8547413867ce39f42f03f4a7e8cdb02 Mon Sep 17 00:00:00 2001 From: Serge Schneider Date: Mon, 30 Nov 2026 15:02:27 +0000 Subject: [PATCH 1/2] ARM: uaccess_with_memcpy: don't retry forever when a page can't be pinned 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//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: 39ec58f3fea4 ("[ARM] alternative copy_to_user/clear_user implementation") Assisted-by: Claude:claude-opus-5-5 Signed-off-by: Serge Schneider --- arch/arm/lib/uaccess_with_memcpy.c | 37 ++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/arch/arm/lib/uaccess_with_memcpy.c b/arch/arm/lib/uaccess_with_memcpy.c index c416c7d3242fe5..f3d7e7c1a123a6 100644 --- a/arch/arm/lib/uaccess_with_memcpy.c +++ b/arch/arm/lib/uaccess_with_memcpy.c @@ -153,12 +153,27 @@ __copy_to_user_memcpy(void __user *to, const void *from, unsigned long n) pte_t *pte; spinlock_t *ptl; int tocopy; + bool retried = false; while (!pin_page_for_write(to, &pte, &ptl)) { if (!atomic) mmap_read_unlock(current->mm); + if (retried) { + /* + * Faulting the page in succeeded but it still + * can't be pinned, as happens when the young bit + * was cleared without a TLB flush. Retrying would + * spin forever, so let the standard routine, which + * handles faults, do the rest. + */ + ua_flags = uaccess_save_and_enable(); + n = __copy_to_user_std(to, from, n); + uaccess_restore(ua_flags); + goto out; + } if (__put_user(0, (char __user *)to)) goto out; + retried = true; if (!atomic) mmap_read_lock(current->mm); } @@ -291,11 +306,33 @@ __clear_user_memset(void __user *addr, unsigned long n) pte_t *pte; spinlock_t *ptl; int tocopy; + bool retried = false; while (!pin_page_for_write(addr, &pte, &ptl)) { mmap_read_unlock(current->mm); + if (retried) { + /* + * See __copy_to_user_memcpy(). On a fault, + * __clear_user_std() returns the whole length + * it was given, so go a page at a time to keep + * the count of bytes not cleared accurate. + */ + ua_flags = uaccess_save_and_enable(); + while (n) { + tocopy = (~(unsigned long)addr & ~PAGE_MASK) + 1; + if (tocopy > n) + tocopy = n; + if (__clear_user_std(addr, tocopy)) + break; + addr += tocopy; + n -= tocopy; + } + uaccess_restore(ua_flags); + goto out; + } if (__put_user(0, (char __user *)addr)) goto out; + retried = true; mmap_read_lock(current->mm); } From df7060b864866c80efe9a8c460220ba90dca991f Mon Sep 17 00:00:00 2001 From: Serge Schneider Date: Mon, 30 Nov 2026 15:02:27 +0000 Subject: [PATCH 2/2] fixup! Improve __copy_to_user and __copy_from_user performance __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 --- arch/arm/lib/uaccess_with_memcpy.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/arch/arm/lib/uaccess_with_memcpy.c b/arch/arm/lib/uaccess_with_memcpy.c index f3d7e7c1a123a6..e28284c927614a 100644 --- a/arch/arm/lib/uaccess_with_memcpy.c +++ b/arch/arm/lib/uaccess_with_memcpy.c @@ -217,13 +217,22 @@ __copy_from_user_memcpy(void *to, const void __user *from, unsigned long n) pte_t *pte; spinlock_t *ptl; int tocopy; + bool retried = false; while (!pin_page_for_read(from, &pte, &ptl)) { char temp; if (!atomic) mmap_read_unlock(current->mm); + if (retried) { + /* See __copy_to_user_memcpy(). */ + ua_flags = uaccess_save_and_enable(); + n = __copy_from_user_std(to, from, n); + uaccess_restore(ua_flags); + goto out; + } if (__get_user(temp, (char __user *)from)) goto out; + retried = true; if (!atomic) mmap_read_lock(current->mm); }