uaccess_with_memcpy.c 6.1 KB

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