uaccess_with_memcpy.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 <linux/export.h>
  22. #include <asm/current.h>
  23. #include <asm/page.h>
  24. static int
  25. pin_page_for_write(const void __user *_addr, pte_t **ptep, spinlock_t **ptlp)
  26. {
  27. unsigned long addr = (unsigned long)_addr;
  28. pgd_t *pgd;
  29. pmd_t *pmd;
  30. pte_t *pte;
  31. pud_t *pud;
  32. spinlock_t *ptl;
  33. pgd = pgd_offset(current->mm, addr);
  34. if (unlikely(pgd_none(*pgd) || pgd_bad(*pgd)))
  35. return 0;
  36. pud = pud_offset(pgd, addr);
  37. if (unlikely(pud_none(*pud) || pud_bad(*pud)))
  38. return 0;
  39. pmd = pmd_offset(pud, addr);
  40. if (unlikely(pmd_none(*pmd)))
  41. return 0;
  42. /*
  43. * A pmd can be bad if it refers to a HugeTLB or THP page.
  44. *
  45. * Both THP and HugeTLB pages have the same pmd layout
  46. * and should not be manipulated by the pte functions.
  47. *
  48. * Lock the page table for the destination and check
  49. * to see that it's still huge and whether or not we will
  50. * need to fault on write.
  51. */
  52. if (unlikely(pmd_thp_or_huge(*pmd))) {
  53. ptl = &current->mm->page_table_lock;
  54. spin_lock(ptl);
  55. if (unlikely(!pmd_thp_or_huge(*pmd)
  56. || pmd_hugewillfault(*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. EXPORT_SYMBOL(arm_copy_to_user);
  140. static unsigned long noinline
  141. __clear_user_memset(void __user *addr, unsigned long n)
  142. {
  143. unsigned long ua_flags;
  144. if (unlikely(segment_eq(get_fs(), KERNEL_DS))) {
  145. memset((void *)addr, 0, n);
  146. return 0;
  147. }
  148. down_read(&current->mm->mmap_sem);
  149. while (n) {
  150. pte_t *pte;
  151. spinlock_t *ptl;
  152. int tocopy;
  153. while (!pin_page_for_write(addr, &pte, &ptl)) {
  154. up_read(&current->mm->mmap_sem);
  155. if (__put_user(0, (char __user *)addr))
  156. goto out;
  157. down_read(&current->mm->mmap_sem);
  158. }
  159. tocopy = (~(unsigned long)addr & ~PAGE_MASK) + 1;
  160. if (tocopy > n)
  161. tocopy = n;
  162. ua_flags = uaccess_save_and_enable();
  163. memset((void *)addr, 0, tocopy);
  164. uaccess_restore(ua_flags);
  165. addr += tocopy;
  166. n -= tocopy;
  167. if (pte)
  168. pte_unmap_unlock(pte, ptl);
  169. else
  170. spin_unlock(ptl);
  171. }
  172. up_read(&current->mm->mmap_sem);
  173. out:
  174. return n;
  175. }
  176. unsigned long arm_clear_user(void __user *addr, unsigned long n)
  177. {
  178. /* See rational for this in __copy_to_user() above. */
  179. if (n < 64) {
  180. unsigned long ua_flags = uaccess_save_and_enable();
  181. n = __clear_user_std(addr, n);
  182. uaccess_restore(ua_flags);
  183. } else {
  184. n = __clear_user_memset(addr, n);
  185. }
  186. return n;
  187. }
  188. EXPORT_SYMBOL(arm_clear_user);
  189. #if 0
  190. /*
  191. * This code is disabled by default, but kept around in case the chosen
  192. * thresholds need to be revalidated. Some overhead (small but still)
  193. * would be implied by a runtime determined variable threshold, and
  194. * so far the measurement on concerned targets didn't show a worthwhile
  195. * variation.
  196. *
  197. * Note that a fairly precise sched_clock() implementation is needed
  198. * for results to make some sense.
  199. */
  200. #include <linux/vmalloc.h>
  201. static int __init test_size_treshold(void)
  202. {
  203. struct page *src_page, *dst_page;
  204. void *user_ptr, *kernel_ptr;
  205. unsigned long long t0, t1, t2;
  206. int size, ret;
  207. ret = -ENOMEM;
  208. src_page = alloc_page(GFP_KERNEL);
  209. if (!src_page)
  210. goto no_src;
  211. dst_page = alloc_page(GFP_KERNEL);
  212. if (!dst_page)
  213. goto no_dst;
  214. kernel_ptr = page_address(src_page);
  215. user_ptr = vmap(&dst_page, 1, VM_IOREMAP, __pgprot(__P010));
  216. if (!user_ptr)
  217. goto no_vmap;
  218. /* warm up the src page dcache */
  219. ret = __copy_to_user_memcpy(user_ptr, kernel_ptr, PAGE_SIZE);
  220. for (size = PAGE_SIZE; size >= 4; size /= 2) {
  221. t0 = sched_clock();
  222. ret |= __copy_to_user_memcpy(user_ptr, kernel_ptr, size);
  223. t1 = sched_clock();
  224. ret |= __copy_to_user_std(user_ptr, kernel_ptr, size);
  225. t2 = sched_clock();
  226. printk("copy_to_user: %d %llu %llu\n", size, t1 - t0, t2 - t1);
  227. }
  228. for (size = PAGE_SIZE; size >= 4; size /= 2) {
  229. t0 = sched_clock();
  230. ret |= __clear_user_memset(user_ptr, size);
  231. t1 = sched_clock();
  232. ret |= __clear_user_std(user_ptr, size);
  233. t2 = sched_clock();
  234. printk("clear_user: %d %llu %llu\n", size, t1 - t0, t2 - t1);
  235. }
  236. if (ret)
  237. ret = -EFAULT;
  238. vunmap(user_ptr);
  239. no_vmap:
  240. put_page(dst_page);
  241. no_dst:
  242. put_page(src_page);
  243. no_src:
  244. return ret;
  245. }
  246. subsys_initcall(test_size_treshold);
  247. #endif