gup.c 9.5 KB

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