uaccess_with_memcpy.c 5.2 KB

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