gup.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 *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. * get_user_pages_fast() - pin user pages in memory
  196. * @start: starting user address
  197. * @nr_pages: number of pages from start to pin
  198. * @write: whether pages will be written to
  199. * @pages: array that receives pointers to the pages pinned.
  200. * Should be at least nr_pages long.
  201. *
  202. * Attempt to pin user pages in memory without taking mm->mmap_sem.
  203. * If not successful, it will fall back to taking the lock and
  204. * calling get_user_pages().
  205. *
  206. * Returns number of pages pinned. This may be fewer than the number
  207. * requested. If nr_pages is 0 or negative, returns 0. If no pages
  208. * were pinned, returns -errno.
  209. */
  210. int get_user_pages_fast(unsigned long start, int nr_pages, int write,
  211. struct page **pages)
  212. {
  213. struct mm_struct *mm = current->mm;
  214. unsigned long addr, len, end;
  215. unsigned long next;
  216. pgd_t *pgdp;
  217. int nr = 0;
  218. start &= PAGE_MASK;
  219. addr = start;
  220. len = (unsigned long) nr_pages << PAGE_SHIFT;
  221. end = start + len;
  222. if (unlikely(!access_ok(write ? VERIFY_WRITE : VERIFY_READ,
  223. (void __user *)start, len)))
  224. goto slow_irqon;
  225. /*
  226. * XXX: batch / limit 'nr', to avoid large irq off latency
  227. * needs some instrumenting to determine the common sizes used by
  228. * important workloads (eg. DB2), and whether limiting the batch size
  229. * will decrease performance.
  230. *
  231. * It seems like we're in the clear for the moment. Direct-IO is
  232. * the main guy that batches up lots of get_user_pages, and even
  233. * they are limited to 64-at-a-time which is not so many.
  234. */
  235. /*
  236. * This doesn't prevent pagetable teardown, but does prevent
  237. * the pagetables and pages from being freed on x86.
  238. *
  239. * So long as we atomically load page table pointers versus teardown
  240. * (which we do on x86, with the above PAE exception), we can follow the
  241. * address down to the the page and take a ref on it.
  242. */
  243. local_irq_disable();
  244. pgdp = pgd_offset(mm, addr);
  245. do {
  246. pgd_t pgd = *pgdp;
  247. next = pgd_addr_end(addr, end);
  248. if (pgd_none(pgd))
  249. goto slow;
  250. if (!gup_pud_range(pgd, addr, next, write, pages, &nr))
  251. goto slow;
  252. } while (pgdp++, addr = next, addr != end);
  253. local_irq_enable();
  254. VM_BUG_ON(nr != (end - start) >> PAGE_SHIFT);
  255. return nr;
  256. {
  257. int ret;
  258. slow:
  259. local_irq_enable();
  260. slow_irqon:
  261. /* Try to get the remaining pages with get_user_pages */
  262. start += nr << PAGE_SHIFT;
  263. pages += nr;
  264. down_read(&mm->mmap_sem);
  265. ret = get_user_pages(current, mm, start,
  266. (end - start) >> PAGE_SHIFT, write, 0, pages, NULL);
  267. up_read(&mm->mmap_sem);
  268. /* Have to be a bit careful with return values */
  269. if (nr > 0) {
  270. if (ret < 0)
  271. ret = nr;
  272. else
  273. ret += nr;
  274. }
  275. return ret;
  276. }
  277. }