uaccess_with_memcpy.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * linux/arch/arm/lib/uaccess_with_memcpy.c
  3. *
  4. * Written by: Lennert Buytenhek and Nicolas Pitre
  5. * Copyright (C) 2009 Marvell Semiconductor
  6. *
  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.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/ctype.h>
  13. #include <linux/uaccess.h>
  14. #include <linux/rwsem.h>
  15. #include <linux/mm.h>
  16. #include <linux/sched.h>
  17. #include <linux/hardirq.h> /* for in_atomic() */
  18. #include <linux/gfp.h>
  19. #include <asm/current.h>
  20. #include <asm/page.h>
  21. static int
  22. pin_page_for_write(const void __user *_addr, pte_t **ptep, spinlock_t **ptlp)
  23. {
  24. unsigned long addr = (unsigned long)_addr;
  25. pgd_t *pgd;
  26. pmd_t *pmd;
  27. pte_t *pte;
  28. pud_t *pud;
  29. spinlock_t *ptl;
  30. pgd = pgd_offset(current->mm, addr);
  31. if (unlikely(pgd_none(*pgd) || pgd_bad(*pgd)))
  32. return 0;
  33. pud = pud_offset(pgd, addr);
  34. if (unlikely(pud_none(*pud) || pud_bad(*pud)))
  35. return 0;
  36. pmd = pmd_offset(pud, addr);
  37. if (unlikely(pmd_none(*pmd) || pmd_bad(*pmd)))
  38. return 0;
  39. pte = pte_offset_map_lock(current->mm, pmd, addr, &ptl);
  40. if (unlikely(!pte_present(*pte) || !pte_young(*pte) ||
  41. !pte_write(*pte) || !pte_dirty(*pte))) {
  42. pte_unmap_unlock(pte, ptl);
  43. return 0;
  44. }
  45. *ptep = pte;
  46. *ptlp = ptl;
  47. return 1;
  48. }
  49. static unsigned long noinline
  50. __copy_to_user_memcpy(void __user *to, const void *from, unsigned long n)
  51. {
  52. int atomic;
  53. if (unlikely(segment_eq(get_fs(), KERNEL_DS))) {
  54. memcpy((void *)to, from, n);
  55. return 0;
  56. }
  57. /* the mmap semaphore is taken only if not in an atomic context */
  58. atomic = in_atomic();
  59. if (!atomic)
  60. down_read(&current->mm->mmap_sem);
  61. while (n) {
  62. pte_t *pte;
  63. spinlock_t *ptl;
  64. int tocopy;
  65. while (!pin_page_for_write(to, &pte, &ptl)) {
  66. if (!atomic)
  67. up_read(&current->mm->mmap_sem);
  68. if (__put_user(0, (char __user *)to))
  69. goto out;
  70. if (!atomic)
  71. down_read(&current->mm->mmap_sem);
  72. }
  73. tocopy = (~(unsigned long)to & ~PAGE_MASK) + 1;
  74. if (tocopy > n)
  75. tocopy = n;
  76. memcpy((void *)to, from, tocopy);
  77. to += tocopy;
  78. from += tocopy;
  79. n -= tocopy;
  80. pte_unmap_unlock(pte, ptl);
  81. }
  82. if (!atomic)
  83. up_read(&current->mm->mmap_sem);
  84. out:
  85. return n;
  86. }
  87. unsigned long
  88. __copy_to_user(void __user *to, const void *from, unsigned long n)
  89. {
  90. /*
  91. * This test is stubbed out of the main function above to keep
  92. * the overhead for small copies low by avoiding a large
  93. * register dump on the stack just to reload them right away.
  94. * With frame pointer disabled, tail call optimization kicks in
  95. * as well making this test almost invisible.
  96. */
  97. if (n < 64)
  98. return __copy_to_user_std(to, from, n);
  99. return __copy_to_user_memcpy(to, from, n);
  100. }
  101. static unsigned long noinline
  102. __clear_user_memset(void __user *addr, unsigned long n)
  103. {
  104. if (unlikely(segment_eq(get_fs(), KERNEL_DS))) {
  105. memset((void *)addr, 0, n);
  106. return 0;
  107. }
  108. down_read(&current->mm->mmap_sem);
  109. while (n) {
  110. pte_t *pte;
  111. spinlock_t *ptl;
  112. int tocopy;
  113. while (!pin_page_for_write(addr, &pte, &ptl)) {
  114. up_read(&current->mm->mmap_sem);
  115. if (__put_user(0, (char __user *)addr))
  116. goto out;
  117. down_read(&current->mm->mmap_sem);
  118. }
  119. tocopy = (~(unsigned long)addr & ~PAGE_MASK) + 1;
  120. if (tocopy > n)
  121. tocopy = n;
  122. memset((void *)addr, 0, tocopy);
  123. addr += tocopy;
  124. n -= tocopy;
  125. pte_unmap_unlock(pte, ptl);
  126. }
  127. up_read(&current->mm->mmap_sem);
  128. out:
  129. return n;
  130. }
  131. unsigned long __clear_user(void __user *addr, unsigned long n)
  132. {
  133. /* See rational for this in __copy_to_user() above. */
  134. if (n < 64)
  135. return __clear_user_std(addr, n);
  136. return __clear_user_memset(addr, n);
  137. }
  138. #if 0
  139. /*
  140. * This code is disabled by default, but kept around in case the chosen
  141. * thresholds need to be revalidated. Some overhead (small but still)
  142. * would be implied by a runtime determined variable threshold, and
  143. * so far the measurement on concerned targets didn't show a worthwhile
  144. * variation.
  145. *
  146. * Note that a fairly precise sched_clock() implementation is needed
  147. * for results to make some sense.
  148. */
  149. #include <linux/vmalloc.h>
  150. static int __init test_size_treshold(void)
  151. {
  152. struct page *src_page, *dst_page;
  153. void *user_ptr, *kernel_ptr;
  154. unsigned long long t0, t1, t2;
  155. int size, ret;
  156. ret = -ENOMEM;
  157. src_page = alloc_page(GFP_KERNEL);
  158. if (!src_page)
  159. goto no_src;
  160. dst_page = alloc_page(GFP_KERNEL);
  161. if (!dst_page)
  162. goto no_dst;
  163. kernel_ptr = page_address(src_page);
  164. user_ptr = vmap(&dst_page, 1, VM_IOREMAP, __pgprot(__P010));
  165. if (!user_ptr)
  166. goto no_vmap;
  167. /* warm up the src page dcache */
  168. ret = __copy_to_user_memcpy(user_ptr, kernel_ptr, PAGE_SIZE);
  169. for (size = PAGE_SIZE; size >= 4; size /= 2) {
  170. t0 = sched_clock();
  171. ret |= __copy_to_user_memcpy(user_ptr, kernel_ptr, size);
  172. t1 = sched_clock();
  173. ret |= __copy_to_user_std(user_ptr, kernel_ptr, size);
  174. t2 = sched_clock();
  175. printk("copy_to_user: %d %llu %llu\n", size, t1 - t0, t2 - t1);
  176. }
  177. for (size = PAGE_SIZE; size >= 4; size /= 2) {
  178. t0 = sched_clock();
  179. ret |= __clear_user_memset(user_ptr, size);
  180. t1 = sched_clock();
  181. ret |= __clear_user_std(user_ptr, size);
  182. t2 = sched_clock();
  183. printk("clear_user: %d %llu %llu\n", size, t1 - t0, t2 - t1);
  184. }
  185. if (ret)
  186. ret = -EFAULT;
  187. vunmap(user_ptr);
  188. no_vmap:
  189. put_page(dst_page);
  190. no_dst:
  191. put_page(src_page);
  192. no_src:
  193. return ret;
  194. }
  195. subsys_initcall(test_size_treshold);
  196. #endif