uaccess_with_memcpy.c 6.2 KB

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