gup.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*
  2. * Lockless get_user_pages_fast for x86
  3. *
  4. * Copyright (C) 2008 Nick Piggin
  5. * Copyright (C) 2008 Novell Inc.
  6. */
  7. #include <linux/sched.h>
  8. #include <linux/mm.h>
  9. #include <linux/vmstat.h>
  10. #include <linux/highmem.h>
  11. #include <linux/swap.h>
  12. #include <asm/pgtable.h>
  13. static inline pte_t gup_get_pte(pte_t *ptep)
  14. {
  15. #ifndef CONFIG_X86_PAE
  16. return ACCESS_ONCE(*ptep);
  17. #else
  18. /*
  19. * With get_user_pages_fast, we walk down the pagetables without taking
  20. * any locks. For this we would like to load the pointers atomically,
  21. * but that is not possible (without expensive cmpxchg8b) on PAE. What
  22. * we do have is the guarantee that a pte will only either go from not
  23. * present to present, or present to not present or both -- it will not
  24. * switch to a completely different present page without a TLB flush in
  25. * between; something that we are blocking by holding interrupts off.
  26. *
  27. * Setting ptes from not present to present goes:
  28. * ptep->pte_high = h;
  29. * smp_wmb();
  30. * ptep->pte_low = l;
  31. *
  32. * And present to not present goes:
  33. * ptep->pte_low = 0;
  34. * smp_wmb();
  35. * ptep->pte_high = 0;
  36. *
  37. * We must ensure here that the load of pte_low sees l iff pte_high
  38. * sees h. We load pte_high *after* loading pte_low, which ensures we
  39. * don't see an older value of pte_high. *Then* we recheck pte_low,
  40. * which ensures that we haven't picked up a changed pte high. We might
  41. * have got rubbish values from pte_low and pte_high, but we are
  42. * guaranteed that pte_low will not have the present bit set *unless*
  43. * it is 'l'. And get_user_pages_fast only operates on present ptes, so
  44. * we're safe.
  45. *
  46. * gup_get_pte should not be used or copied outside gup.c without being
  47. * very careful -- it does not atomically load the pte or anything that
  48. * is likely to be useful for you.
  49. */
  50. pte_t pte;
  51. retry:
  52. pte.pte_low = ptep->pte_low;
  53. smp_rmb();
  54. pte.pte_high = ptep->pte_high;
  55. smp_rmb();
  56. if (unlikely(pte.pte_low != ptep->pte_low))
  57. goto retry;
  58. return pte;
  59. #endif
  60. }
  61. /*
  62. * The performance critical leaf functions are made noinline otherwise gcc
  63. * inlines everything into a single function which results in too much
  64. * register pressure.
  65. */
  66. static noinline int gup_pte_range(pmd_t pmd, unsigned long addr,
  67. unsigned long end, int write, struct page **pages, int *nr)
  68. {
  69. unsigned long mask;
  70. pte_t *ptep;
  71. mask = _PAGE_PRESENT|_PAGE_USER;
  72. if (write)
  73. mask |= _PAGE_RW;
  74. ptep = pte_offset_map(&pmd, addr);
  75. do {
  76. pte_t pte = gup_get_pte(ptep);
  77. struct page *page;
  78. if ((pte_flags(pte) & (mask | _PAGE_SPECIAL)) != mask) {
  79. pte_unmap(ptep);
  80. return 0;
  81. }
  82. VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
  83. page = pte_page(pte);
  84. get_page(page);
  85. SetPageReferenced(page);
  86. pages[*nr] = page;
  87. (*nr)++;
  88. } while (ptep++, addr += PAGE_SIZE, addr != end);
  89. pte_unmap(ptep - 1);
  90. return 1;
  91. }
  92. static inline void get_head_page_multiple(struct page *page, int nr)
  93. {
  94. VM_BUG_ON(page != compound_head(page));
  95. VM_BUG_ON(page_count(page) == 0);
  96. atomic_add(nr, &page->_count);
  97. SetPageReferenced(page);
  98. }
  99. static inline void get_huge_page_tail(struct page *page)
  100. {
  101. /*
  102. * __split_huge_page_refcount() cannot run
  103. * from under us.
  104. */
  105. VM_BUG_ON(atomic_read(&page->_count) < 0);
  106. atomic_inc(&page->_count);
  107. }
  108. static noinline int gup_huge_pmd(pmd_t pmd, unsigned long addr,
  109. unsigned long end, int write, struct page **pages, int *nr)
  110. {
  111. unsigned long mask;
  112. pte_t pte = *(pte_t *)&pmd;
  113. struct page *head, *page;
  114. int refs;
  115. mask = _PAGE_PRESENT|_PAGE_USER;
  116. if (write)
  117. mask |= _PAGE_RW;
  118. if ((pte_flags(pte) & mask) != mask)
  119. return 0;
  120. /* hugepages are never "special" */
  121. VM_BUG_ON(pte_flags(pte) & _PAGE_SPECIAL);
  122. VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
  123. refs = 0;
  124. head = pte_page(pte);
  125. page = head + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
  126. do {
  127. VM_BUG_ON(compound_head(page) != head);
  128. pages[*nr] = page;
  129. if (PageTail(page))
  130. get_huge_page_tail(page);
  131. (*nr)++;
  132. page++;
  133. refs++;
  134. } while (addr += PAGE_SIZE, addr != end);
  135. get_head_page_multiple(head, refs);
  136. return 1;
  137. }
  138. static int gup_pmd_range(pud_t pud, unsigned long addr, unsigned long end,
  139. int write, struct page **pages, int *nr)
  140. {
  141. unsigned long next;
  142. pmd_t *pmdp;
  143. pmdp = pmd_offset(&pud, addr);
  144. do {
  145. pmd_t pmd = *pmdp;
  146. next = pmd_addr_end(addr, end);
  147. /*
  148. * The pmd_trans_splitting() check below explains why
  149. * pmdp_splitting_flush has to flush the tlb, to stop
  150. * this gup-fast code from running while we set the
  151. * splitting bit in the pmd. Returning zero will take
  152. * the slow path that will call wait_split_huge_page()
  153. * if the pmd is still in splitting state. gup-fast
  154. * can't because it has irq disabled and
  155. * wait_split_huge_page() would never return as the
  156. * tlb flush IPI wouldn't run.
  157. */
  158. if (pmd_none(pmd) || pmd_trans_splitting(pmd))
  159. return 0;
  160. if (unlikely(pmd_large(pmd))) {
  161. if (!gup_huge_pmd(pmd, addr, next, write, pages, nr))
  162. return 0;
  163. } else {
  164. if (!gup_pte_range(pmd, addr, next, write, pages, nr))
  165. return 0;
  166. }
  167. } while (pmdp++, addr = next, addr != end);
  168. return 1;
  169. }
  170. static noinline int gup_huge_pud(pud_t pud, unsigned long addr,
  171. unsigned long end, int write, struct page **pages, int *nr)
  172. {
  173. unsigned long mask;
  174. pte_t pte = *(pte_t *)&pud;
  175. struct page *head, *page;
  176. int refs;
  177. mask = _PAGE_PRESENT|_PAGE_USER;
  178. if (write)
  179. mask |= _PAGE_RW;
  180. if ((pte_flags(pte) & mask) != mask)
  181. return 0;
  182. /* hugepages are never "special" */
  183. VM_BUG_ON(pte_flags(pte) & _PAGE_SPECIAL);
  184. VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
  185. refs = 0;
  186. head = pte_page(pte);
  187. page = head + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
  188. do {
  189. VM_BUG_ON(compound_head(page) != head);
  190. pages[*nr] = page;
  191. (*nr)++;
  192. page++;
  193. refs++;
  194. } while (addr += PAGE_SIZE, addr != end);
  195. get_head_page_multiple(head, refs);
  196. return 1;
  197. }
  198. static int gup_pud_range(pgd_t pgd, unsigned long addr, unsigned long end,
  199. int write, struct page **pages, int *nr)
  200. {
  201. unsigned long next;
  202. pud_t *pudp;
  203. pudp = pud_offset(&pgd, addr);
  204. do {
  205. pud_t pud = *pudp;
  206. next = pud_addr_end(addr, end);
  207. if (pud_none(pud))
  208. return 0;
  209. if (unlikely(pud_large(pud))) {
  210. if (!gup_huge_pud(pud, addr, next, write, pages, nr))
  211. return 0;
  212. } else {
  213. if (!gup_pmd_range(pud, addr, next, write, pages, nr))
  214. return 0;
  215. }
  216. } while (pudp++, addr = next, addr != end);
  217. return 1;
  218. }
  219. /*
  220. * Like get_user_pages_fast() except its IRQ-safe in that it won't fall
  221. * back to the regular GUP.
  222. */
  223. int __get_user_pages_fast(unsigned long start, int nr_pages, int write,
  224. struct page **pages)
  225. {
  226. struct mm_struct *mm = current->mm;
  227. unsigned long addr, len, end;
  228. unsigned long next;
  229. unsigned long flags;
  230. pgd_t *pgdp;
  231. int nr = 0;
  232. start &= PAGE_MASK;
  233. addr = start;
  234. len = (unsigned long) nr_pages << PAGE_SHIFT;
  235. end = start + len;
  236. if (unlikely(!access_ok(write ? VERIFY_WRITE : VERIFY_READ,
  237. (void __user *)start, len)))
  238. return 0;
  239. /*
  240. * XXX: batch / limit 'nr', to avoid large irq off latency
  241. * needs some instrumenting to determine the common sizes used by
  242. * important workloads (eg. DB2), and whether limiting the batch size
  243. * will decrease performance.
  244. *
  245. * It seems like we're in the clear for the moment. Direct-IO is
  246. * the main guy that batches up lots of get_user_pages, and even
  247. * they are limited to 64-at-a-time which is not so many.
  248. */
  249. /*
  250. * This doesn't prevent pagetable teardown, but does prevent
  251. * the pagetables and pages from being freed on x86.
  252. *
  253. * So long as we atomically load page table pointers versus teardown
  254. * (which we do on x86, with the above PAE exception), we can follow the
  255. * address down to the the page and take a ref on it.
  256. */
  257. local_irq_save(flags);
  258. pgdp = pgd_offset(mm, addr);
  259. do {
  260. pgd_t pgd = *pgdp;
  261. next = pgd_addr_end(addr, end);
  262. if (pgd_none(pgd))
  263. break;
  264. if (!gup_pud_range(pgd, addr, next, write, pages, &nr))
  265. break;
  266. } while (pgdp++, addr = next, addr != end);
  267. local_irq_restore(flags);
  268. return nr;
  269. }
  270. /**
  271. * get_user_pages_fast() - pin user pages in memory
  272. * @start: starting user address
  273. * @nr_pages: number of pages from start to pin
  274. * @write: whether pages will be written to
  275. * @pages: array that receives pointers to the pages pinned.
  276. * Should be at least nr_pages long.
  277. *
  278. * Attempt to pin user pages in memory without taking mm->mmap_sem.
  279. * If not successful, it will fall back to taking the lock and
  280. * calling get_user_pages().
  281. *
  282. * Returns number of pages pinned. This may be fewer than the number
  283. * requested. If nr_pages is 0 or negative, returns 0. If no pages
  284. * were pinned, returns -errno.
  285. */
  286. int get_user_pages_fast(unsigned long start, int nr_pages, int write,
  287. struct page **pages)
  288. {
  289. struct mm_struct *mm = current->mm;
  290. unsigned long addr, len, end;
  291. unsigned long next;
  292. pgd_t *pgdp;
  293. int nr = 0;
  294. start &= PAGE_MASK;
  295. addr = start;
  296. len = (unsigned long) nr_pages << PAGE_SHIFT;
  297. end = start + len;
  298. if (end < start)
  299. goto slow_irqon;
  300. #ifdef CONFIG_X86_64
  301. if (end >> __VIRTUAL_MASK_SHIFT)
  302. goto slow_irqon;
  303. #endif
  304. /*
  305. * XXX: batch / limit 'nr', to avoid large irq off latency
  306. * needs some instrumenting to determine the common sizes used by
  307. * important workloads (eg. DB2), and whether limiting the batch size
  308. * will decrease performance.
  309. *
  310. * It seems like we're in the clear for the moment. Direct-IO is
  311. * the main guy that batches up lots of get_user_pages, and even
  312. * they are limited to 64-at-a-time which is not so many.
  313. */
  314. /*
  315. * This doesn't prevent pagetable teardown, but does prevent
  316. * the pagetables and pages from being freed on x86.
  317. *
  318. * So long as we atomically load page table pointers versus teardown
  319. * (which we do on x86, with the above PAE exception), we can follow the
  320. * address down to the the page and take a ref on it.
  321. */
  322. local_irq_disable();
  323. pgdp = pgd_offset(mm, addr);
  324. do {
  325. pgd_t pgd = *pgdp;
  326. next = pgd_addr_end(addr, end);
  327. if (pgd_none(pgd))
  328. goto slow;
  329. if (!gup_pud_range(pgd, addr, next, write, pages, &nr))
  330. goto slow;
  331. } while (pgdp++, addr = next, addr != end);
  332. local_irq_enable();
  333. VM_BUG_ON(nr != (end - start) >> PAGE_SHIFT);
  334. return nr;
  335. {
  336. int ret;
  337. slow:
  338. local_irq_enable();
  339. slow_irqon:
  340. /* Try to get the remaining pages with get_user_pages */
  341. start += nr << PAGE_SHIFT;
  342. pages += nr;
  343. down_read(&mm->mmap_sem);
  344. ret = get_user_pages(current, mm, start,
  345. (end - start) >> PAGE_SHIFT, write, 0, pages, NULL);
  346. up_read(&mm->mmap_sem);
  347. /* Have to be a bit careful with return values */
  348. if (nr > 0) {
  349. if (ret < 0)
  350. ret = nr;
  351. else
  352. ret += nr;
  353. }
  354. return ret;
  355. }
  356. }