gup.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*
  2. * Lockless get_user_pages_fast for MIPS
  3. *
  4. * Copyright (C) 2008 Nick Piggin
  5. * Copyright (C) 2008 Novell Inc.
  6. * Copyright (C) 2011 Ralf Baechle
  7. */
  8. #include <linux/sched.h>
  9. #include <linux/mm.h>
  10. #include <linux/vmstat.h>
  11. #include <linux/highmem.h>
  12. #include <linux/swap.h>
  13. #include <linux/hugetlb.h>
  14. #include <asm/cpu-features.h>
  15. #include <asm/pgtable.h>
  16. static inline pte_t gup_get_pte(pte_t *ptep)
  17. {
  18. #if defined(CONFIG_PHYS_ADDR_T_64BIT) && defined(CONFIG_CPU_MIPS32)
  19. pte_t pte;
  20. retry:
  21. pte.pte_low = ptep->pte_low;
  22. smp_rmb();
  23. pte.pte_high = ptep->pte_high;
  24. smp_rmb();
  25. if (unlikely(pte.pte_low != ptep->pte_low))
  26. goto retry;
  27. return pte;
  28. #else
  29. return READ_ONCE(*ptep);
  30. #endif
  31. }
  32. static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
  33. int write, struct page **pages, int *nr)
  34. {
  35. pte_t *ptep = pte_offset_map(&pmd, addr);
  36. do {
  37. pte_t pte = gup_get_pte(ptep);
  38. struct page *page;
  39. if (!pte_present(pte) ||
  40. pte_special(pte) || (write && !pte_write(pte))) {
  41. pte_unmap(ptep);
  42. return 0;
  43. }
  44. VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
  45. page = pte_page(pte);
  46. get_page(page);
  47. SetPageReferenced(page);
  48. pages[*nr] = page;
  49. (*nr)++;
  50. } while (ptep++, addr += PAGE_SIZE, addr != end);
  51. pte_unmap(ptep - 1);
  52. return 1;
  53. }
  54. static inline void get_head_page_multiple(struct page *page, int nr)
  55. {
  56. VM_BUG_ON(page != compound_head(page));
  57. VM_BUG_ON(page_count(page) == 0);
  58. atomic_add(nr, &page->_count);
  59. SetPageReferenced(page);
  60. }
  61. static int gup_huge_pmd(pmd_t pmd, unsigned long addr, unsigned long end,
  62. int write, struct page **pages, int *nr)
  63. {
  64. pte_t pte = *(pte_t *)&pmd;
  65. struct page *head, *page;
  66. int refs;
  67. if (write && !pte_write(pte))
  68. return 0;
  69. /* hugepages are never "special" */
  70. VM_BUG_ON(pte_special(pte));
  71. VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
  72. refs = 0;
  73. head = pte_page(pte);
  74. page = head + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
  75. do {
  76. VM_BUG_ON(compound_head(page) != head);
  77. pages[*nr] = page;
  78. if (PageTail(page))
  79. get_huge_page_tail(page);
  80. (*nr)++;
  81. page++;
  82. refs++;
  83. } while (addr += PAGE_SIZE, addr != end);
  84. get_head_page_multiple(head, refs);
  85. return 1;
  86. }
  87. static int gup_pmd_range(pud_t pud, unsigned long addr, unsigned long end,
  88. int write, struct page **pages, int *nr)
  89. {
  90. unsigned long next;
  91. pmd_t *pmdp;
  92. pmdp = pmd_offset(&pud, addr);
  93. do {
  94. pmd_t pmd = *pmdp;
  95. next = pmd_addr_end(addr, end);
  96. /*
  97. * The pmd_trans_splitting() check below explains why
  98. * pmdp_splitting_flush has to flush the tlb, to stop
  99. * this gup-fast code from running while we set the
  100. * splitting bit in the pmd. Returning zero will take
  101. * the slow path that will call wait_split_huge_page()
  102. * if the pmd is still in splitting state. gup-fast
  103. * can't because it has irq disabled and
  104. * wait_split_huge_page() would never return as the
  105. * tlb flush IPI wouldn't run.
  106. */
  107. if (pmd_none(pmd) || pmd_trans_splitting(pmd))
  108. return 0;
  109. if (unlikely(pmd_huge(pmd))) {
  110. if (!gup_huge_pmd(pmd, addr, next, write, pages,nr))
  111. return 0;
  112. } else {
  113. if (!gup_pte_range(pmd, addr, next, write, pages,nr))
  114. return 0;
  115. }
  116. } while (pmdp++, addr = next, addr != end);
  117. return 1;
  118. }
  119. static int gup_huge_pud(pud_t pud, unsigned long addr, unsigned long end,
  120. int write, struct page **pages, int *nr)
  121. {
  122. pte_t pte = *(pte_t *)&pud;
  123. struct page *head, *page;
  124. int refs;
  125. if (write && !pte_write(pte))
  126. return 0;
  127. /* hugepages are never "special" */
  128. VM_BUG_ON(pte_special(pte));
  129. VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
  130. refs = 0;
  131. head = pte_page(pte);
  132. page = head + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
  133. do {
  134. VM_BUG_ON(compound_head(page) != head);
  135. pages[*nr] = page;
  136. if (PageTail(page))
  137. get_huge_page_tail(page);
  138. (*nr)++;
  139. page++;
  140. refs++;
  141. } while (addr += PAGE_SIZE, addr != end);
  142. get_head_page_multiple(head, refs);
  143. return 1;
  144. }
  145. static int gup_pud_range(pgd_t pgd, unsigned long addr, unsigned long end,
  146. int write, struct page **pages, int *nr)
  147. {
  148. unsigned long next;
  149. pud_t *pudp;
  150. pudp = pud_offset(&pgd, addr);
  151. do {
  152. pud_t pud = *pudp;
  153. next = pud_addr_end(addr, end);
  154. if (pud_none(pud))
  155. return 0;
  156. if (unlikely(pud_huge(pud))) {
  157. if (!gup_huge_pud(pud, addr, next, write, pages,nr))
  158. return 0;
  159. } else {
  160. if (!gup_pmd_range(pud, addr, next, write, pages,nr))
  161. return 0;
  162. }
  163. } while (pudp++, addr = next, addr != end);
  164. return 1;
  165. }
  166. /*
  167. * Like get_user_pages_fast() except its IRQ-safe in that it won't fall
  168. * back to the regular GUP.
  169. */
  170. int __get_user_pages_fast(unsigned long start, int nr_pages, int write,
  171. struct page **pages)
  172. {
  173. struct mm_struct *mm = current->mm;
  174. unsigned long addr, len, end;
  175. unsigned long next;
  176. unsigned long flags;
  177. pgd_t *pgdp;
  178. int nr = 0;
  179. start &= PAGE_MASK;
  180. addr = start;
  181. len = (unsigned long) nr_pages << PAGE_SHIFT;
  182. end = start + len;
  183. if (unlikely(!access_ok(write ? VERIFY_WRITE : VERIFY_READ,
  184. (void __user *)start, len)))
  185. return 0;
  186. /*
  187. * XXX: batch / limit 'nr', to avoid large irq off latency
  188. * needs some instrumenting to determine the common sizes used by
  189. * important workloads (eg. DB2), and whether limiting the batch
  190. * size will decrease performance.
  191. *
  192. * It seems like we're in the clear for the moment. Direct-IO is
  193. * the main guy that batches up lots of get_user_pages, and even
  194. * they are limited to 64-at-a-time which is not so many.
  195. */
  196. /*
  197. * This doesn't prevent pagetable teardown, but does prevent
  198. * the pagetables and pages from being freed.
  199. *
  200. * So long as we atomically load page table pointers versus teardown,
  201. * we can follow the address down to the page and take a ref on it.
  202. */
  203. local_irq_save(flags);
  204. pgdp = pgd_offset(mm, addr);
  205. do {
  206. pgd_t pgd = *pgdp;
  207. next = pgd_addr_end(addr, end);
  208. if (pgd_none(pgd))
  209. break;
  210. if (!gup_pud_range(pgd, addr, next, write, pages, &nr))
  211. break;
  212. } while (pgdp++, addr = next, addr != end);
  213. local_irq_restore(flags);
  214. return nr;
  215. }
  216. /**
  217. * get_user_pages_fast() - pin user pages in memory
  218. * @start: starting user address
  219. * @nr_pages: number of pages from start to pin
  220. * @write: whether pages will be written to
  221. * @pages: array that receives pointers to the pages pinned.
  222. * Should be at least nr_pages long.
  223. *
  224. * Attempt to pin user pages in memory without taking mm->mmap_sem.
  225. * If not successful, it will fall back to taking the lock and
  226. * calling get_user_pages().
  227. *
  228. * Returns number of pages pinned. This may be fewer than the number
  229. * requested. If nr_pages is 0 or negative, returns 0. If no pages
  230. * were pinned, returns -errno.
  231. */
  232. int get_user_pages_fast(unsigned long start, int nr_pages, int write,
  233. struct page **pages)
  234. {
  235. struct mm_struct *mm = current->mm;
  236. unsigned long addr, len, end;
  237. unsigned long next;
  238. pgd_t *pgdp;
  239. int ret, nr = 0;
  240. start &= PAGE_MASK;
  241. addr = start;
  242. len = (unsigned long) nr_pages << PAGE_SHIFT;
  243. end = start + len;
  244. if (end < start || cpu_has_dc_aliases)
  245. goto slow_irqon;
  246. /* XXX: batch / limit 'nr' */
  247. local_irq_disable();
  248. pgdp = pgd_offset(mm, addr);
  249. do {
  250. pgd_t pgd = *pgdp;
  251. next = pgd_addr_end(addr, end);
  252. if (pgd_none(pgd))
  253. goto slow;
  254. if (!gup_pud_range(pgd, addr, next, write, pages, &nr))
  255. goto slow;
  256. } while (pgdp++, addr = next, addr != end);
  257. local_irq_enable();
  258. VM_BUG_ON(nr != (end - start) >> PAGE_SHIFT);
  259. return nr;
  260. slow:
  261. local_irq_enable();
  262. slow_irqon:
  263. /* Try to get the remaining pages with get_user_pages */
  264. start += nr << PAGE_SHIFT;
  265. pages += nr;
  266. ret = get_user_pages_unlocked(current, mm, start,
  267. (end - start) >> PAGE_SHIFT,
  268. write, 0, pages);
  269. /* Have to be a bit careful with return values */
  270. if (nr > 0) {
  271. if (ret < 0)
  272. ret = nr;
  273. else
  274. ret += nr;
  275. }
  276. return ret;
  277. }