uaccess_with_memcpy.c 5.2 KB

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