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