uaccess_with_memcpy.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/arch/arm/lib/uaccess_with_memcpy.c
  4. *
  5. * Written by: Lennert Buytenhek and Nicolas Pitre
  6. * Copyright (C) 2009 Marvell Semiconductor
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/ctype.h>
  10. #include <linux/uaccess.h>
  11. #include <linux/rwsem.h>
  12. #include <linux/mm.h>
  13. #include <linux/sched.h>
  14. #include <linux/hardirq.h> /* for in_atomic() */
  15. #include <linux/gfp.h>
  16. #include <linux/highmem.h>
  17. #include <linux/hugetlb.h>
  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. p4d_t *p4d;
  26. pmd_t *pmd;
  27. pte_t *pte;
  28. pud_t *pud;
  29. spinlock_t *ptl;
  30. pgd = pgd_offset(current->mm, addr);
  31. if (unlikely(pgd_none(*pgd) || pgd_bad(*pgd)))
  32. return 0;
  33. p4d = p4d_offset(pgd, addr);
  34. if (unlikely(p4d_none(*p4d) || p4d_bad(*p4d)))
  35. return 0;
  36. pud = pud_offset(p4d, 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. /* the mmap semaphore is taken only if not in an atomic context */
  82. atomic = faulthandler_disabled();
  83. if (!atomic)
  84. mmap_read_lock(current->mm);
  85. while (n) {
  86. pte_t *pte;
  87. spinlock_t *ptl;
  88. int tocopy;
  89. while (!pin_page_for_write(to, &pte, &ptl)) {
  90. if (!atomic)
  91. mmap_read_unlock(current->mm);
  92. if (__put_user(0, (char __user *)to))
  93. goto out;
  94. if (!atomic)
  95. mmap_read_lock(current->mm);
  96. }
  97. tocopy = (~(unsigned long)to & ~PAGE_MASK) + 1;
  98. if (tocopy > n)
  99. tocopy = n;
  100. ua_flags = uaccess_save_and_enable();
  101. __memcpy((void *)to, from, tocopy);
  102. uaccess_restore(ua_flags);
  103. to += tocopy;
  104. from += tocopy;
  105. n -= tocopy;
  106. if (pte)
  107. pte_unmap_unlock(pte, ptl);
  108. else
  109. spin_unlock(ptl);
  110. }
  111. if (!atomic)
  112. mmap_read_unlock(current->mm);
  113. out:
  114. return n;
  115. }
  116. unsigned long
  117. arm_copy_to_user(void __user *to, const void *from, unsigned long n)
  118. {
  119. /*
  120. * This test is stubbed out of the main function above to keep
  121. * the overhead for small copies low by avoiding a large
  122. * register dump on the stack just to reload them right away.
  123. * With frame pointer disabled, tail call optimization kicks in
  124. * as well making this test almost invisible.
  125. */
  126. if (n < 64) {
  127. unsigned long ua_flags = uaccess_save_and_enable();
  128. n = __copy_to_user_std(to, from, n);
  129. uaccess_restore(ua_flags);
  130. } else {
  131. n = __copy_to_user_memcpy(uaccess_mask_range_ptr(to, n),
  132. from, n);
  133. }
  134. return n;
  135. }
  136. static unsigned long noinline
  137. __clear_user_memset(void __user *addr, unsigned long n)
  138. {
  139. unsigned long ua_flags;
  140. mmap_read_lock(current->mm);
  141. while (n) {
  142. pte_t *pte;
  143. spinlock_t *ptl;
  144. int tocopy;
  145. while (!pin_page_for_write(addr, &pte, &ptl)) {
  146. mmap_read_unlock(current->mm);
  147. if (__put_user(0, (char __user *)addr))
  148. goto out;
  149. mmap_read_lock(current->mm);
  150. }
  151. tocopy = (~(unsigned long)addr & ~PAGE_MASK) + 1;
  152. if (tocopy > n)
  153. tocopy = n;
  154. ua_flags = uaccess_save_and_enable();
  155. __memset((void *)addr, 0, tocopy);
  156. uaccess_restore(ua_flags);
  157. addr += tocopy;
  158. n -= tocopy;
  159. if (pte)
  160. pte_unmap_unlock(pte, ptl);
  161. else
  162. spin_unlock(ptl);
  163. }
  164. mmap_read_unlock(current->mm);
  165. out:
  166. return n;
  167. }
  168. unsigned long arm_clear_user(void __user *addr, unsigned long n)
  169. {
  170. /* See rational for this in __copy_to_user() above. */
  171. if (n < 64) {
  172. unsigned long ua_flags = uaccess_save_and_enable();
  173. n = __clear_user_std(addr, n);
  174. uaccess_restore(ua_flags);
  175. } else {
  176. n = __clear_user_memset(addr, n);
  177. }
  178. return n;
  179. }
  180. #if 0
  181. /*
  182. * This code is disabled by default, but kept around in case the chosen
  183. * thresholds need to be revalidated. Some overhead (small but still)
  184. * would be implied by a runtime determined variable threshold, and
  185. * so far the measurement on concerned targets didn't show a worthwhile
  186. * variation.
  187. *
  188. * Note that a fairly precise sched_clock() implementation is needed
  189. * for results to make some sense.
  190. */
  191. #include <linux/vmalloc.h>
  192. static int __init test_size_treshold(void)
  193. {
  194. struct page *src_page, *dst_page;
  195. void *user_ptr, *kernel_ptr;
  196. unsigned long long t0, t1, t2;
  197. int size, ret;
  198. ret = -ENOMEM;
  199. src_page = alloc_page(GFP_KERNEL);
  200. if (!src_page)
  201. goto no_src;
  202. dst_page = alloc_page(GFP_KERNEL);
  203. if (!dst_page)
  204. goto no_dst;
  205. kernel_ptr = page_address(src_page);
  206. user_ptr = vmap(&dst_page, 1, VM_IOREMAP, __pgprot(__PAGE_COPY));
  207. if (!user_ptr)
  208. goto no_vmap;
  209. /* warm up the src page dcache */
  210. ret = __copy_to_user_memcpy(user_ptr, kernel_ptr, PAGE_SIZE);
  211. for (size = PAGE_SIZE; size >= 4; size /= 2) {
  212. t0 = sched_clock();
  213. ret |= __copy_to_user_memcpy(user_ptr, kernel_ptr, size);
  214. t1 = sched_clock();
  215. ret |= __copy_to_user_std(user_ptr, kernel_ptr, size);
  216. t2 = sched_clock();
  217. printk("copy_to_user: %d %llu %llu\n", size, t1 - t0, t2 - t1);
  218. }
  219. for (size = PAGE_SIZE; size >= 4; size /= 2) {
  220. t0 = sched_clock();
  221. ret |= __clear_user_memset(user_ptr, size);
  222. t1 = sched_clock();
  223. ret |= __clear_user_std(user_ptr, size);
  224. t2 = sched_clock();
  225. printk("clear_user: %d %llu %llu\n", size, t1 - t0, t2 - t1);
  226. }
  227. if (ret)
  228. ret = -EFAULT;
  229. vunmap(user_ptr);
  230. no_vmap:
  231. put_page(dst_page);
  232. no_dst:
  233. put_page(src_page);
  234. no_src:
  235. return ret;
  236. }
  237. subsys_initcall(test_size_treshold);
  238. #endif