2 * linux/arch/arm/lib/uaccess_with_memcpy.c
4 * Written by: Lennert Buytenhek and Nicolas Pitre
5 * Copyright (C) 2009 Marvell Semiconductor
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/kernel.h>
13 #include <linux/ctype.h>
14 #include <linux/uaccess.h>
15 #include <linux/rwsem.h>
17 #include <linux/sched.h>
18 #include <linux/hardirq.h> /* for in_atomic() */
19 #include <linux/gfp.h>
20 #include <asm/current.h>
24 pin_page_for_write(const void __user *_addr, pte_t **ptep, spinlock_t **ptlp)
26 unsigned long addr = (unsigned long)_addr;
32 pgd = pgd_offset(current->mm, addr);
33 if (unlikely(pgd_none(*pgd) || pgd_bad(*pgd)))
36 pmd = pmd_offset(pgd, addr);
37 if (unlikely(pmd_none(*pmd) || pmd_bad(*pmd)))
40 pte = pte_offset_map_lock(current->mm, pmd, addr, &ptl);
41 if (unlikely(!pte_present(*pte) || !pte_young(*pte) ||
42 !pte_write(*pte) || !pte_dirty(*pte))) {
43 pte_unmap_unlock(pte, ptl);
53 static unsigned long noinline
54 __copy_to_user_memcpy(void __user *to, const void *from, unsigned long n)
58 if (unlikely(segment_eq(get_fs(), KERNEL_DS))) {
59 memcpy((void *)to, from, n);
63 /* the mmap semaphore is taken only if not in an atomic context */
67 down_read(¤t->mm->mmap_sem);
73 while (!pin_page_for_write(to, &pte, &ptl)) {
75 up_read(¤t->mm->mmap_sem);
76 if (__put_user(0, (char __user *)to))
79 down_read(¤t->mm->mmap_sem);
82 tocopy = (~(unsigned long)to & ~PAGE_MASK) + 1;
86 memcpy((void *)to, from, tocopy);
91 pte_unmap_unlock(pte, ptl);
94 up_read(¤t->mm->mmap_sem);
101 __copy_to_user(void __user *to, const void *from, unsigned long n)
104 * This test is stubbed out of the main function above to keep
105 * the overhead for small copies low by avoiding a large
106 * register dump on the stack just to reload them right away.
107 * With frame pointer disabled, tail call optimization kicks in
108 * as well making this test almost invisible.
111 return __copy_to_user_std(to, from, n);
112 return __copy_to_user_memcpy(to, from, n);
115 static unsigned long noinline
116 __clear_user_memset(void __user *addr, unsigned long n)
118 if (unlikely(segment_eq(get_fs(), KERNEL_DS))) {
119 memset((void *)addr, 0, n);
123 down_read(¤t->mm->mmap_sem);
129 while (!pin_page_for_write(addr, &pte, &ptl)) {
130 up_read(¤t->mm->mmap_sem);
131 if (__put_user(0, (char __user *)addr))
133 down_read(¤t->mm->mmap_sem);
136 tocopy = (~(unsigned long)addr & ~PAGE_MASK) + 1;
140 memset((void *)addr, 0, tocopy);
144 pte_unmap_unlock(pte, ptl);
146 up_read(¤t->mm->mmap_sem);
152 unsigned long __clear_user(void __user *addr, unsigned long n)
154 /* See rational for this in __copy_to_user() above. */
156 return __clear_user_std(addr, n);
157 return __clear_user_memset(addr, n);
163 * This code is disabled by default, but kept around in case the chosen
164 * thresholds need to be revalidated. Some overhead (small but still)
165 * would be implied by a runtime determined variable threshold, and
166 * so far the measurement on concerned targets didn't show a worthwhile
169 * Note that a fairly precise sched_clock() implementation is needed
170 * for results to make some sense.
173 #include <linux/vmalloc.h>
175 static int __init test_size_treshold(void)
177 struct page *src_page, *dst_page;
178 void *user_ptr, *kernel_ptr;
179 unsigned long long t0, t1, t2;
183 src_page = alloc_page(GFP_KERNEL);
186 dst_page = alloc_page(GFP_KERNEL);
189 kernel_ptr = page_address(src_page);
190 user_ptr = vmap(&dst_page, 1, VM_IOREMAP, __pgprot(__P010));
194 /* warm up the src page dcache */
195 ret = __copy_to_user_memcpy(user_ptr, kernel_ptr, PAGE_SIZE);
197 for (size = PAGE_SIZE; size >= 4; size /= 2) {
199 ret |= __copy_to_user_memcpy(user_ptr, kernel_ptr, size);
201 ret |= __copy_to_user_std(user_ptr, kernel_ptr, size);
203 printk("copy_to_user: %d %llu %llu\n", size, t1 - t0, t2 - t1);
206 for (size = PAGE_SIZE; size >= 4; size /= 2) {
208 ret |= __clear_user_memset(user_ptr, size);
210 ret |= __clear_user_std(user_ptr, size);
212 printk("clear_user: %d %llu %llu\n", size, t1 - t0, t2 - t1);
227 subsys_initcall(test_size_treshold);