1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/arch/arm/lib/uaccess_with_memcpy.c
5 * Written by: Lennert Buytenhek and Nicolas Pitre
6 * Copyright (C) 2009 Marvell Semiconductor
9 #include <linux/kernel.h>
10 #include <linux/ctype.h>
11 #include <linux/uaccess.h>
12 #include <linux/rwsem.h>
14 #include <linux/sched.h>
15 #include <linux/hardirq.h> /* for in_atomic() */
16 #include <linux/gfp.h>
17 #include <linux/highmem.h>
18 #include <linux/hugetlb.h>
19 #include <asm/current.h>
23 pin_page_for_write(const void __user *_addr, pte_t **ptep, spinlock_t **ptlp)
25 unsigned long addr = (unsigned long)_addr;
33 pgd = pgd_offset(current->mm, addr);
34 if (unlikely(pgd_none(*pgd) || pgd_bad(*pgd)))
37 p4d = p4d_offset(pgd, addr);
38 if (unlikely(p4d_none(*p4d) || p4d_bad(*p4d)))
41 pud = pud_offset(p4d, addr);
42 if (unlikely(pud_none(*pud) || pud_bad(*pud)))
45 pmd = pmd_offset(pud, addr);
46 if (unlikely(pmd_none(*pmd)))
50 * A pmd can be bad if it refers to a HugeTLB or THP page.
52 * Both THP and HugeTLB pages have the same pmd layout
53 * and should not be manipulated by the pte functions.
55 * Lock the page table for the destination and check
56 * to see that it's still huge and whether or not we will
57 * need to fault on write.
59 if (unlikely(pmd_leaf(*pmd))) {
60 ptl = ¤t->mm->page_table_lock;
62 if (unlikely(!pmd_leaf(*pmd)
63 || pmd_hugewillfault(*pmd))) {
73 if (unlikely(pmd_bad(*pmd)))
76 pte = pte_offset_map_lock(current->mm, pmd, addr, &ptl);
80 if (unlikely(!pte_present(*pte) || !pte_young(*pte) ||
81 !pte_write(*pte) || !pte_dirty(*pte))) {
82 pte_unmap_unlock(pte, ptl);
92 static unsigned long noinline
93 __copy_to_user_memcpy(void __user *to, const void *from, unsigned long n)
95 unsigned long ua_flags;
98 /* the mmap semaphore is taken only if not in an atomic context */
99 atomic = faulthandler_disabled();
102 mmap_read_lock(current->mm);
108 while (!pin_page_for_write(to, &pte, &ptl)) {
110 mmap_read_unlock(current->mm);
111 if (__put_user(0, (char __user *)to))
114 mmap_read_lock(current->mm);
117 tocopy = (~(unsigned long)to & ~PAGE_MASK) + 1;
121 ua_flags = uaccess_save_and_enable();
122 __memcpy((void *)to, from, tocopy);
123 uaccess_restore(ua_flags);
129 pte_unmap_unlock(pte, ptl);
134 mmap_read_unlock(current->mm);
141 arm_copy_to_user(void __user *to, const void *from, unsigned long n)
144 * This test is stubbed out of the main function above to keep
145 * the overhead for small copies low by avoiding a large
146 * register dump on the stack just to reload them right away.
147 * With frame pointer disabled, tail call optimization kicks in
148 * as well making this test almost invisible.
151 unsigned long ua_flags = uaccess_save_and_enable();
152 n = __copy_to_user_std(to, from, n);
153 uaccess_restore(ua_flags);
155 n = __copy_to_user_memcpy(uaccess_mask_range_ptr(to, n),
161 static unsigned long noinline
162 __clear_user_memset(void __user *addr, unsigned long n)
164 unsigned long ua_flags;
166 mmap_read_lock(current->mm);
172 while (!pin_page_for_write(addr, &pte, &ptl)) {
173 mmap_read_unlock(current->mm);
174 if (__put_user(0, (char __user *)addr))
176 mmap_read_lock(current->mm);
179 tocopy = (~(unsigned long)addr & ~PAGE_MASK) + 1;
183 ua_flags = uaccess_save_and_enable();
184 __memset((void *)addr, 0, tocopy);
185 uaccess_restore(ua_flags);
190 pte_unmap_unlock(pte, ptl);
194 mmap_read_unlock(current->mm);
200 unsigned long arm_clear_user(void __user *addr, unsigned long n)
202 /* See rational for this in __copy_to_user() above. */
204 unsigned long ua_flags = uaccess_save_and_enable();
205 n = __clear_user_std(addr, n);
206 uaccess_restore(ua_flags);
208 n = __clear_user_memset(addr, n);
216 * This code is disabled by default, but kept around in case the chosen
217 * thresholds need to be revalidated. Some overhead (small but still)
218 * would be implied by a runtime determined variable threshold, and
219 * so far the measurement on concerned targets didn't show a worthwhile
222 * Note that a fairly precise sched_clock() implementation is needed
223 * for results to make some sense.
226 #include <linux/vmalloc.h>
228 static int __init test_size_treshold(void)
230 struct page *src_page, *dst_page;
231 void *user_ptr, *kernel_ptr;
232 unsigned long long t0, t1, t2;
236 src_page = alloc_page(GFP_KERNEL);
239 dst_page = alloc_page(GFP_KERNEL);
242 kernel_ptr = page_address(src_page);
243 user_ptr = vmap(&dst_page, 1, VM_IOREMAP, __pgprot(__PAGE_COPY));
247 /* warm up the src page dcache */
248 ret = __copy_to_user_memcpy(user_ptr, kernel_ptr, PAGE_SIZE);
250 for (size = PAGE_SIZE; size >= 4; size /= 2) {
252 ret |= __copy_to_user_memcpy(user_ptr, kernel_ptr, size);
254 ret |= __copy_to_user_std(user_ptr, kernel_ptr, size);
256 printk("copy_to_user: %d %llu %llu\n", size, t1 - t0, t2 - t1);
259 for (size = PAGE_SIZE; size >= 4; size /= 2) {
261 ret |= __clear_user_memset(user_ptr, size);
263 ret |= __clear_user_std(user_ptr, size);
265 printk("clear_user: %d %llu %llu\n", size, t1 - t0, t2 - t1);
280 subsys_initcall(test_size_treshold);