uaccess_with_memcpy.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. unsigned long ua_flags;
  80. int atomic;
  81. if (unlikely(segment_eq(get_fs(), KERNEL_DS))) {
  82. memcpy((void *)to, from, n);
  83. return 0;
  84. }
  85. /* the mmap semaphore is taken only if not in an atomic context */
  86. atomic = faulthandler_disabled();
  87. if (!atomic)
  88. down_read(&current->mm->mmap_sem);
  89. while (n) {
  90. pte_t *pte;
  91. spinlock_t *ptl;
  92. int tocopy;
  93. while (!pin_page_for_write(to, &pte, &ptl)) {
  94. if (!atomic)
  95. up_read(&current->mm->mmap_sem);
  96. if (__put_user(0, (char __user *)to))
  97. goto out;
  98. if (!atomic)
  99. down_read(&current->mm->mmap_sem);
  100. }
  101. tocopy = (~(unsigned long)to & ~PAGE_MASK) + 1;
  102. if (tocopy > n)
  103. tocopy = n;
  104. ua_flags = uaccess_save_and_enable();
  105. memcpy((void *)to, from, tocopy);
  106. uaccess_restore(ua_flags);
  107. to += tocopy;
  108. from += tocopy;
  109. n -= tocopy;
  110. if (pte)
  111. pte_unmap_unlock(pte, ptl);
  112. else
  113. spin_unlock(ptl);
  114. }
  115. if (!atomic)
  116. up_read(&current->mm->mmap_sem);
  117. out:
  118. return n;
  119. }
  120. unsigned long
  121. arm_copy_to_user(void __user *to, const void *from, unsigned long n)
  122. {
  123. /*
  124. * This test is stubbed out of the main function above to keep
  125. * the overhead for small copies low by avoiding a large
  126. * register dump on the stack just to reload them right away.
  127. * With frame pointer disabled, tail call optimization kicks in
  128. * as well making this test almost invisible.
  129. */
  130. if (n < 64) {
  131. unsigned long ua_flags = uaccess_save_and_enable();
  132. n = __copy_to_user_std(to, from, n);
  133. uaccess_restore(ua_flags);
  134. } else {
  135. n = __copy_to_user_memcpy(to, from, n);
  136. }
  137. return n;
  138. }
  139. static unsigned long noinline
  140. __clear_user_memset(void __user *addr, unsigned long n)
  141. {
  142. unsigned long ua_flags;
  143. if (unlikely(segment_eq(get_fs(), KERNEL_DS))) {
  144. memset((void *)addr, 0, n);
  145. return 0;
  146. }
  147. down_read(&current->mm->mmap_sem);
  148. while (n) {
  149. pte_t *pte;
  150. spinlock_t *ptl;
  151. int tocopy;
  152. while (!pin_page_for_write(addr, &pte, &ptl)) {
  153. up_read(&current->mm->mmap_sem);
  154. if (__put_user(0, (char __user *)addr))
  155. goto out;
  156. down_read(&current->mm->mmap_sem);
  157. }
  158. tocopy = (~(unsigned long)addr & ~PAGE_MASK) + 1;
  159. if (tocopy > n)
  160. tocopy = n;
  161. ua_flags = uaccess_save_and_enable();
  162. memset((void *)addr, 0, tocopy);
  163. uaccess_restore(ua_flags);
  164. addr += tocopy;
  165. n -= tocopy;
  166. if (pte)
  167. pte_unmap_unlock(pte, ptl);
  168. else
  169. spin_unlock(ptl);
  170. }
  171. up_read(&current->mm->mmap_sem);
  172. out:
  173. return n;
  174. }
  175. unsigned long arm_clear_user(void __user *addr, unsigned long n)
  176. {
  177. /* See rational for this in __copy_to_user() above. */
  178. if (n < 64) {
  179. unsigned long ua_flags = uaccess_save_and_enable();
  180. n = __clear_user_std(addr, n);
  181. uaccess_restore(ua_flags);
  182. } else {
  183. n = __clear_user_memset(addr, n);
  184. }
  185. return n;
  186. }
  187. #if 0
  188. /*
  189. * This code is disabled by default, but kept around in case the chosen
  190. * thresholds need to be revalidated. Some overhead (small but still)
  191. * would be implied by a runtime determined variable threshold, and
  192. * so far the measurement on concerned targets didn't show a worthwhile
  193. * variation.
  194. *
  195. * Note that a fairly precise sched_clock() implementation is needed
  196. * for results to make some sense.
  197. */
  198. #include <linux/vmalloc.h>
  199. static int __init test_size_treshold(void)
  200. {
  201. struct page *src_page, *dst_page;
  202. void *user_ptr, *kernel_ptr;
  203. unsigned long long t0, t1, t2;
  204. int size, ret;
  205. ret = -ENOMEM;
  206. src_page = alloc_page(GFP_KERNEL);
  207. if (!src_page)
  208. goto no_src;
  209. dst_page = alloc_page(GFP_KERNEL);
  210. if (!dst_page)
  211. goto no_dst;
  212. kernel_ptr = page_address(src_page);
  213. user_ptr = vmap(&dst_page, 1, VM_IOREMAP, __pgprot(__P010));
  214. if (!user_ptr)
  215. goto no_vmap;
  216. /* warm up the src page dcache */
  217. ret = __copy_to_user_memcpy(user_ptr, kernel_ptr, PAGE_SIZE);
  218. for (size = PAGE_SIZE; size >= 4; size /= 2) {
  219. t0 = sched_clock();
  220. ret |= __copy_to_user_memcpy(user_ptr, kernel_ptr, size);
  221. t1 = sched_clock();
  222. ret |= __copy_to_user_std(user_ptr, kernel_ptr, size);
  223. t2 = sched_clock();
  224. printk("copy_to_user: %d %llu %llu\n", size, t1 - t0, t2 - t1);
  225. }
  226. for (size = PAGE_SIZE; size >= 4; size /= 2) {
  227. t0 = sched_clock();
  228. ret |= __clear_user_memset(user_ptr, size);
  229. t1 = sched_clock();
  230. ret |= __clear_user_std(user_ptr, size);
  231. t2 = sched_clock();
  232. printk("clear_user: %d %llu %llu\n", size, t1 - t0, t2 - t1);
  233. }
  234. if (ret)
  235. ret = -EFAULT;
  236. vunmap(user_ptr);
  237. no_vmap:
  238. put_page(dst_page);
  239. no_dst:
  240. put_page(src_page);
  241. no_src:
  242. return ret;
  243. }
  244. subsys_initcall(test_size_treshold);
  245. #endif