gup.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Lockless get_user_pages_fast for SuperH
  4. *
  5. * Copyright (C) 2009 - 2010 Paul Mundt
  6. *
  7. * Cloned from the x86 and PowerPC versions, by:
  8. *
  9. * Copyright (C) 2008 Nick Piggin
  10. * Copyright (C) 2008 Novell Inc.
  11. */
  12. #include <linux/sched.h>
  13. #include <linux/mm.h>
  14. #include <linux/vmstat.h>
  15. #include <linux/highmem.h>
  16. #include <asm/pgtable.h>
  17. static inline pte_t gup_get_pte(pte_t *ptep)
  18. {
  19. #ifndef CONFIG_X2TLB
  20. return READ_ONCE(*ptep);
  21. #else
  22. /*
  23. * With get_user_pages_fast, we walk down the pagetables without
  24. * taking any locks. For this we would like to load the pointers
  25. * atomically, but that is not possible with 64-bit PTEs. What
  26. * we do have is the guarantee that a pte will only either go
  27. * from not present to present, or present to not present or both
  28. * -- it will not switch to a completely different present page
  29. * without a TLB flush in between; something that we are blocking
  30. * by holding interrupts off.
  31. *
  32. * Setting ptes from not present to present goes:
  33. * ptep->pte_high = h;
  34. * smp_wmb();
  35. * ptep->pte_low = l;
  36. *
  37. * And present to not present goes:
  38. * ptep->pte_low = 0;
  39. * smp_wmb();
  40. * ptep->pte_high = 0;
  41. *
  42. * We must ensure here that the load of pte_low sees l iff pte_high
  43. * sees h. We load pte_high *after* loading pte_low, which ensures we
  44. * don't see an older value of pte_high. *Then* we recheck pte_low,
  45. * which ensures that we haven't picked up a changed pte high. We might
  46. * have got rubbish values from pte_low and pte_high, but we are
  47. * guaranteed that pte_low will not have the present bit set *unless*
  48. * it is 'l'. And get_user_pages_fast only operates on present ptes, so
  49. * we're safe.
  50. *
  51. * gup_get_pte should not be used or copied outside gup.c without being
  52. * very careful -- it does not atomically load the pte or anything that
  53. * is likely to be useful for you.
  54. */
  55. pte_t pte;
  56. retry:
  57. pte.pte_low = ptep->pte_low;
  58. smp_rmb();
  59. pte.pte_high = ptep->pte_high;
  60. smp_rmb();
  61. if (unlikely(pte.pte_low != ptep->pte_low))
  62. goto retry;
  63. return pte;
  64. #endif
  65. }
  66. /*
  67. * The performance critical leaf functions are made noinline otherwise gcc
  68. * inlines everything into a single function which results in too much
  69. * register pressure.
  70. */
  71. static noinline int gup_pte_range(pmd_t pmd, unsigned long addr,
  72. unsigned long end, int write, struct page **pages, int *nr)
  73. {
  74. u64 mask, result;
  75. pte_t *ptep;
  76. #ifdef CONFIG_X2TLB
  77. result = _PAGE_PRESENT | _PAGE_EXT(_PAGE_EXT_KERN_READ | _PAGE_EXT_USER_READ);
  78. if (write)
  79. result |= _PAGE_EXT(_PAGE_EXT_KERN_WRITE | _PAGE_EXT_USER_WRITE);
  80. #elif defined(CONFIG_SUPERH64)
  81. result = _PAGE_PRESENT | _PAGE_USER | _PAGE_READ;
  82. if (write)
  83. result |= _PAGE_WRITE;
  84. #else
  85. result = _PAGE_PRESENT | _PAGE_USER;
  86. if (write)
  87. result |= _PAGE_RW;
  88. #endif
  89. mask = result | _PAGE_SPECIAL;
  90. ptep = pte_offset_map(&pmd, addr);
  91. do {
  92. pte_t pte = gup_get_pte(ptep);
  93. struct page *page;
  94. if ((pte_val(pte) & mask) != result) {
  95. pte_unmap(ptep);
  96. return 0;
  97. }
  98. VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
  99. page = pte_page(pte);
  100. get_page(page);
  101. __flush_anon_page(page, addr);
  102. flush_dcache_page(page);
  103. pages[*nr] = page;
  104. (*nr)++;
  105. } while (ptep++, addr += PAGE_SIZE, addr != end);
  106. pte_unmap(ptep - 1);
  107. return 1;
  108. }
  109. static int gup_pmd_range(pud_t pud, unsigned long addr, unsigned long end,
  110. int write, struct page **pages, int *nr)
  111. {
  112. unsigned long next;
  113. pmd_t *pmdp;
  114. pmdp = pmd_offset(&pud, addr);
  115. do {
  116. pmd_t pmd = *pmdp;
  117. next = pmd_addr_end(addr, end);
  118. if (pmd_none(pmd))
  119. return 0;
  120. if (!gup_pte_range(pmd, addr, next, write, pages, nr))
  121. return 0;
  122. } while (pmdp++, addr = next, addr != end);
  123. return 1;
  124. }
  125. static int gup_pud_range(pgd_t pgd, unsigned long addr, unsigned long end,
  126. int write, struct page **pages, int *nr)
  127. {
  128. unsigned long next;
  129. pud_t *pudp;
  130. pudp = pud_offset(&pgd, addr);
  131. do {
  132. pud_t pud = *pudp;
  133. next = pud_addr_end(addr, end);
  134. if (pud_none(pud))
  135. return 0;
  136. if (!gup_pmd_range(pud, addr, next, write, pages, nr))
  137. return 0;
  138. } while (pudp++, addr = next, addr != end);
  139. return 1;
  140. }
  141. /*
  142. * Like get_user_pages_fast() except its IRQ-safe in that it won't fall
  143. * back to the regular GUP.
  144. * Note a difference with get_user_pages_fast: this always returns the
  145. * number of pages pinned, 0 if no pages were pinned.
  146. */
  147. int __get_user_pages_fast(unsigned long start, int nr_pages, int write,
  148. struct page **pages)
  149. {
  150. struct mm_struct *mm = current->mm;
  151. unsigned long addr, len, end;
  152. unsigned long next;
  153. unsigned long flags;
  154. pgd_t *pgdp;
  155. int nr = 0;
  156. start &= PAGE_MASK;
  157. addr = start;
  158. len = (unsigned long) nr_pages << PAGE_SHIFT;
  159. end = start + len;
  160. if (unlikely(!access_ok(write ? VERIFY_WRITE : VERIFY_READ,
  161. (void __user *)start, len)))
  162. return 0;
  163. /*
  164. * This doesn't prevent pagetable teardown, but does prevent
  165. * the pagetables and pages from being freed.
  166. */
  167. local_irq_save(flags);
  168. pgdp = pgd_offset(mm, addr);
  169. do {
  170. pgd_t pgd = *pgdp;
  171. next = pgd_addr_end(addr, end);
  172. if (pgd_none(pgd))
  173. break;
  174. if (!gup_pud_range(pgd, addr, next, write, pages, &nr))
  175. break;
  176. } while (pgdp++, addr = next, addr != end);
  177. local_irq_restore(flags);
  178. return nr;
  179. }
  180. /**
  181. * get_user_pages_fast() - pin user pages in memory
  182. * @start: starting user address
  183. * @nr_pages: number of pages from start to pin
  184. * @write: whether pages will be written to
  185. * @pages: array that receives pointers to the pages pinned.
  186. * Should be at least nr_pages long.
  187. *
  188. * Attempt to pin user pages in memory without taking mm->mmap_sem.
  189. * If not successful, it will fall back to taking the lock and
  190. * calling get_user_pages().
  191. *
  192. * Returns number of pages pinned. This may be fewer than the number
  193. * requested. If nr_pages is 0 or negative, returns 0. If no pages
  194. * were pinned, returns -errno.
  195. */
  196. int get_user_pages_fast(unsigned long start, int nr_pages, int write,
  197. struct page **pages)
  198. {
  199. struct mm_struct *mm = current->mm;
  200. unsigned long addr, len, end;
  201. unsigned long next;
  202. pgd_t *pgdp;
  203. int nr = 0;
  204. start &= PAGE_MASK;
  205. addr = start;
  206. len = (unsigned long) nr_pages << PAGE_SHIFT;
  207. end = start + len;
  208. if (end < start)
  209. goto slow_irqon;
  210. local_irq_disable();
  211. pgdp = pgd_offset(mm, addr);
  212. do {
  213. pgd_t pgd = *pgdp;
  214. next = pgd_addr_end(addr, end);
  215. if (pgd_none(pgd))
  216. goto slow;
  217. if (!gup_pud_range(pgd, addr, next, write, pages, &nr))
  218. goto slow;
  219. } while (pgdp++, addr = next, addr != end);
  220. local_irq_enable();
  221. VM_BUG_ON(nr != (end - start) >> PAGE_SHIFT);
  222. return nr;
  223. {
  224. int ret;
  225. slow:
  226. local_irq_enable();
  227. slow_irqon:
  228. /* Try to get the remaining pages with get_user_pages */
  229. start += nr << PAGE_SHIFT;
  230. pages += nr;
  231. ret = get_user_pages_unlocked(start,
  232. (end - start) >> PAGE_SHIFT, pages,
  233. write ? FOLL_WRITE : 0);
  234. /* Have to be a bit careful with return values */
  235. if (nr > 0) {
  236. if (ret < 0)
  237. ret = nr;
  238. else
  239. ret += nr;
  240. }
  241. return ret;
  242. }
  243. }