gup.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. /* Similar to the PMD case, NUMA hinting must take slow path */
  79. if (pte_numa(pte)) {
  80. pte_unmap(ptep);
  81. return 0;
  82. }
  83. if ((pte_flags(pte) & (mask | _PAGE_SPECIAL)) != mask) {
  84. pte_unmap(ptep);
  85. return 0;
  86. }
  87. VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
  88. page = pte_page(pte);
  89. get_page(page);
  90. SetPageReferenced(page);
  91. pages[*nr] = page;
  92. (*nr)++;
  93. } while (ptep++, addr += PAGE_SIZE, addr != end);
  94. pte_unmap(ptep - 1);
  95. return 1;
  96. }
  97. static inline void get_head_page_multiple(struct page *page, int nr)
  98. {
  99. VM_BUG_ON_PAGE(page != compound_head(page), page);
  100. VM_BUG_ON_PAGE(page_count(page) == 0, page);
  101. atomic_add(nr, &page->_count);
  102. SetPageReferenced(page);
  103. }
  104. static noinline int gup_huge_pmd(pmd_t pmd, unsigned long addr,
  105. unsigned long end, int write, struct page **pages, int *nr)
  106. {
  107. unsigned long mask;
  108. pte_t pte = *(pte_t *)&pmd;
  109. struct page *head, *page;
  110. int refs;
  111. mask = _PAGE_PRESENT|_PAGE_USER;
  112. if (write)
  113. mask |= _PAGE_RW;
  114. if ((pte_flags(pte) & mask) != mask)
  115. return 0;
  116. /* hugepages are never "special" */
  117. VM_BUG_ON(pte_flags(pte) & _PAGE_SPECIAL);
  118. VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
  119. refs = 0;
  120. head = pte_page(pte);
  121. page = head + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
  122. do {
  123. VM_BUG_ON_PAGE(compound_head(page) != head, page);
  124. pages[*nr] = page;
  125. if (PageTail(page))
  126. get_huge_page_tail(page);
  127. (*nr)++;
  128. page++;
  129. refs++;
  130. } while (addr += PAGE_SIZE, addr != end);
  131. get_head_page_multiple(head, refs);
  132. return 1;
  133. }
  134. static int gup_pmd_range(pud_t pud, unsigned long addr, unsigned long end,
  135. int write, struct page **pages, int *nr)
  136. {
  137. unsigned long next;
  138. pmd_t *pmdp;
  139. pmdp = pmd_offset(&pud, addr);
  140. do {
  141. pmd_t pmd = *pmdp;
  142. next = pmd_addr_end(addr, end);
  143. /*
  144. * The pmd_trans_splitting() check below explains why
  145. * pmdp_splitting_flush has to flush the tlb, to stop
  146. * this gup-fast code from running while we set the
  147. * splitting bit in the pmd. Returning zero will take
  148. * the slow path that will call wait_split_huge_page()
  149. * if the pmd is still in splitting state. gup-fast
  150. * can't because it has irq disabled and
  151. * wait_split_huge_page() would never return as the
  152. * tlb flush IPI wouldn't run.
  153. */
  154. if (pmd_none(pmd) || pmd_trans_splitting(pmd))
  155. return 0;
  156. if (unlikely(pmd_large(pmd))) {
  157. /*
  158. * NUMA hinting faults need to be handled in the GUP
  159. * slowpath for accounting purposes and so that they
  160. * can be serialised against THP migration.
  161. */
  162. if (pmd_numa(pmd))
  163. return 0;
  164. if (!gup_huge_pmd(pmd, addr, next, write, pages, nr))
  165. return 0;
  166. } else {
  167. if (!gup_pte_range(pmd, addr, next, write, pages, nr))
  168. return 0;
  169. }
  170. } while (pmdp++, addr = next, addr != end);
  171. return 1;
  172. }
  173. static noinline int gup_huge_pud(pud_t pud, unsigned long addr,
  174. unsigned long end, int write, struct page **pages, int *nr)
  175. {
  176. unsigned long mask;
  177. pte_t pte = *(pte_t *)&pud;
  178. struct page *head, *page;
  179. int refs;
  180. mask = _PAGE_PRESENT|_PAGE_USER;
  181. if (write)
  182. mask |= _PAGE_RW;
  183. if ((pte_flags(pte) & mask) != mask)
  184. return 0;
  185. /* hugepages are never "special" */
  186. VM_BUG_ON(pte_flags(pte) & _PAGE_SPECIAL);
  187. VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
  188. refs = 0;
  189. head = pte_page(pte);
  190. page = head + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
  191. do {
  192. VM_BUG_ON_PAGE(compound_head(page) != head, page);
  193. pages[*nr] = page;
  194. if (PageTail(page))
  195. get_huge_page_tail(page);
  196. (*nr)++;
  197. page++;
  198. refs++;
  199. } while (addr += PAGE_SIZE, addr != end);
  200. get_head_page_multiple(head, refs);
  201. return 1;
  202. }
  203. static int gup_pud_range(pgd_t pgd, unsigned long addr, unsigned long end,
  204. int write, struct page **pages, int *nr)
  205. {
  206. unsigned long next;
  207. pud_t *pudp;
  208. pudp = pud_offset(&pgd, addr);
  209. do {
  210. pud_t pud = *pudp;
  211. next = pud_addr_end(addr, end);
  212. if (pud_none(pud))
  213. return 0;
  214. if (unlikely(pud_large(pud))) {
  215. if (!gup_huge_pud(pud, addr, next, write, pages, nr))
  216. return 0;
  217. } else {
  218. if (!gup_pmd_range(pud, addr, next, write, pages, nr))
  219. return 0;
  220. }
  221. } while (pudp++, addr = next, addr != end);
  222. return 1;
  223. }
  224. /*
  225. * Like get_user_pages_fast() except its IRQ-safe in that it won't fall
  226. * back to the regular GUP.
  227. */
  228. int __get_user_pages_fast(unsigned long start, int nr_pages, int write,
  229. struct page **pages)
  230. {
  231. struct mm_struct *mm = current->mm;
  232. unsigned long addr, len, end;
  233. unsigned long next;
  234. unsigned long flags;
  235. pgd_t *pgdp;
  236. int nr = 0;
  237. start &= PAGE_MASK;
  238. addr = start;
  239. len = (unsigned long) nr_pages << PAGE_SHIFT;
  240. end = start + len;
  241. if (unlikely(!access_ok(write ? VERIFY_WRITE : VERIFY_READ,
  242. (void __user *)start, len)))
  243. return 0;
  244. /*
  245. * XXX: batch / limit 'nr', to avoid large irq off latency
  246. * needs some instrumenting to determine the common sizes used by
  247. * important workloads (eg. DB2), and whether limiting the batch size
  248. * will decrease performance.
  249. *
  250. * It seems like we're in the clear for the moment. Direct-IO is
  251. * the main guy that batches up lots of get_user_pages, and even
  252. * they are limited to 64-at-a-time which is not so many.
  253. */
  254. /*
  255. * This doesn't prevent pagetable teardown, but does prevent
  256. * the pagetables and pages from being freed on x86.
  257. *
  258. * So long as we atomically load page table pointers versus teardown
  259. * (which we do on x86, with the above PAE exception), we can follow the
  260. * address down to the the page and take a ref on it.
  261. */
  262. local_irq_save(flags);
  263. pgdp = pgd_offset(mm, addr);
  264. do {
  265. pgd_t pgd = *pgdp;
  266. next = pgd_addr_end(addr, end);
  267. if (pgd_none(pgd))
  268. break;
  269. if (!gup_pud_range(pgd, addr, next, write, pages, &nr))
  270. break;
  271. } while (pgdp++, addr = next, addr != end);
  272. local_irq_restore(flags);
  273. return nr;
  274. }
  275. /**
  276. * get_user_pages_fast() - pin user pages in memory
  277. * @start: starting user address
  278. * @nr_pages: number of pages from start to pin
  279. * @write: whether pages will be written to
  280. * @pages: array that receives pointers to the pages pinned.
  281. * Should be at least nr_pages long.
  282. *
  283. * Attempt to pin user pages in memory without taking mm->mmap_sem.
  284. * If not successful, it will fall back to taking the lock and
  285. * calling get_user_pages().
  286. *
  287. * Returns number of pages pinned. This may be fewer than the number
  288. * requested. If nr_pages is 0 or negative, returns 0. If no pages
  289. * were pinned, returns -errno.
  290. */
  291. int get_user_pages_fast(unsigned long start, int nr_pages, int write,
  292. struct page **pages)
  293. {
  294. struct mm_struct *mm = current->mm;
  295. unsigned long addr, len, end;
  296. unsigned long next;
  297. pgd_t *pgdp;
  298. int nr = 0;
  299. start &= PAGE_MASK;
  300. addr = start;
  301. len = (unsigned long) nr_pages << PAGE_SHIFT;
  302. end = start + len;
  303. if (end < start)
  304. goto slow_irqon;
  305. #ifdef CONFIG_X86_64
  306. if (end >> __VIRTUAL_MASK_SHIFT)
  307. goto slow_irqon;
  308. #endif
  309. /*
  310. * XXX: batch / limit 'nr', to avoid large irq off latency
  311. * needs some instrumenting to determine the common sizes used by
  312. * important workloads (eg. DB2), and whether limiting the batch size
  313. * will decrease performance.
  314. *
  315. * It seems like we're in the clear for the moment. Direct-IO is
  316. * the main guy that batches up lots of get_user_pages, and even
  317. * they are limited to 64-at-a-time which is not so many.
  318. */
  319. /*
  320. * This doesn't prevent pagetable teardown, but does prevent
  321. * the pagetables and pages from being freed on x86.
  322. *
  323. * So long as we atomically load page table pointers versus teardown
  324. * (which we do on x86, with the above PAE exception), we can follow the
  325. * address down to the the page and take a ref on it.
  326. */
  327. local_irq_disable();
  328. pgdp = pgd_offset(mm, addr);
  329. do {
  330. pgd_t pgd = *pgdp;
  331. next = pgd_addr_end(addr, end);
  332. if (pgd_none(pgd))
  333. goto slow;
  334. if (!gup_pud_range(pgd, addr, next, write, pages, &nr))
  335. goto slow;
  336. } while (pgdp++, addr = next, addr != end);
  337. local_irq_enable();
  338. VM_BUG_ON(nr != (end - start) >> PAGE_SHIFT);
  339. return nr;
  340. {
  341. int ret;
  342. slow:
  343. local_irq_enable();
  344. slow_irqon:
  345. /* Try to get the remaining pages with get_user_pages */
  346. start += nr << PAGE_SHIFT;
  347. pages += nr;
  348. down_read(&mm->mmap_sem);
  349. ret = get_user_pages(current, mm, start,
  350. (end - start) >> PAGE_SHIFT, write, 0, pages, NULL);
  351. up_read(&mm->mmap_sem);
  352. /* Have to be a bit careful with return values */
  353. if (nr > 0) {
  354. if (ret < 0)
  355. ret = nr;
  356. else
  357. ret += nr;
  358. }
  359. return ret;
  360. }
  361. }