uaccess_with_memcpy.c 5.3 KB

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