gup.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * Lockless get_user_pages_fast for powerpc
  3. *
  4. * Copyright (C) 2008 Nick Piggin
  5. * Copyright (C) 2008 Novell Inc.
  6. */
  7. #undef DEBUG
  8. #include <linux/sched.h>
  9. #include <linux/mm.h>
  10. #include <linux/hugetlb.h>
  11. #include <linux/vmstat.h>
  12. #include <linux/pagemap.h>
  13. #include <linux/rwsem.h>
  14. #include <asm/pgtable.h>
  15. #ifdef __HAVE_ARCH_PTE_SPECIAL
  16. /*
  17. * The performance critical leaf functions are made noinline otherwise gcc
  18. * inlines everything into a single function which results in too much
  19. * register pressure.
  20. */
  21. static noinline int gup_pte_range(pmd_t pmd, unsigned long addr,
  22. unsigned long end, int write, struct page **pages, int *nr)
  23. {
  24. unsigned long mask, result;
  25. pte_t *ptep;
  26. result = _PAGE_PRESENT|_PAGE_USER;
  27. if (write)
  28. result |= _PAGE_RW;
  29. mask = result | _PAGE_SPECIAL;
  30. ptep = pte_offset_kernel(&pmd, addr);
  31. do {
  32. pte_t pte = *ptep;
  33. struct page *page;
  34. if ((pte_val(pte) & mask) != result)
  35. return 0;
  36. VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
  37. page = pte_page(pte);
  38. if (!page_cache_get_speculative(page))
  39. return 0;
  40. if (unlikely(pte_val(pte) != pte_val(*ptep))) {
  41. put_page(page);
  42. return 0;
  43. }
  44. pages[*nr] = page;
  45. (*nr)++;
  46. } while (ptep++, addr += PAGE_SIZE, addr != end);
  47. return 1;
  48. }
  49. #ifdef CONFIG_HUGETLB_PAGE
  50. static noinline int gup_huge_pte(pte_t *ptep, struct hstate *hstate,
  51. unsigned long *addr, unsigned long end,
  52. int write, struct page **pages, int *nr)
  53. {
  54. unsigned long mask;
  55. unsigned long pte_end;
  56. struct page *head, *page;
  57. pte_t pte;
  58. int refs;
  59. pte_end = (*addr + huge_page_size(hstate)) & huge_page_mask(hstate);
  60. if (pte_end < end)
  61. end = pte_end;
  62. pte = *ptep;
  63. mask = _PAGE_PRESENT|_PAGE_USER;
  64. if (write)
  65. mask |= _PAGE_RW;
  66. if ((pte_val(pte) & mask) != mask)
  67. return 0;
  68. /* hugepages are never "special" */
  69. VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
  70. refs = 0;
  71. head = pte_page(pte);
  72. page = head + ((*addr & ~huge_page_mask(hstate)) >> PAGE_SHIFT);
  73. do {
  74. VM_BUG_ON(compound_head(page) != head);
  75. pages[*nr] = page;
  76. (*nr)++;
  77. page++;
  78. refs++;
  79. } while (*addr += PAGE_SIZE, *addr != end);
  80. if (!page_cache_add_speculative(head, refs)) {
  81. *nr -= refs;
  82. return 0;
  83. }
  84. if (unlikely(pte_val(pte) != pte_val(*ptep))) {
  85. /* Could be optimized better */
  86. while (*nr) {
  87. put_page(page);
  88. (*nr)--;
  89. }
  90. }
  91. return 1;
  92. }
  93. #endif /* CONFIG_HUGETLB_PAGE */
  94. static int gup_pmd_range(pud_t pud, unsigned long addr, unsigned long end,
  95. int write, struct page **pages, int *nr)
  96. {
  97. unsigned long next;
  98. pmd_t *pmdp;
  99. pmdp = pmd_offset(&pud, addr);
  100. do {
  101. pmd_t pmd = *pmdp;
  102. next = pmd_addr_end(addr, end);
  103. if (pmd_none(pmd))
  104. return 0;
  105. if (!gup_pte_range(pmd, addr, next, write, pages, nr))
  106. return 0;
  107. } while (pmdp++, addr = next, addr != end);
  108. return 1;
  109. }
  110. static int gup_pud_range(pgd_t pgd, unsigned long addr, unsigned long end,
  111. int write, struct page **pages, int *nr)
  112. {
  113. unsigned long next;
  114. pud_t *pudp;
  115. pudp = pud_offset(&pgd, addr);
  116. do {
  117. pud_t pud = *pudp;
  118. next = pud_addr_end(addr, end);
  119. if (pud_none(pud))
  120. return 0;
  121. if (!gup_pmd_range(pud, addr, next, write, pages, nr))
  122. return 0;
  123. } while (pudp++, addr = next, addr != end);
  124. return 1;
  125. }
  126. int get_user_pages_fast(unsigned long start, int nr_pages, int write,
  127. struct page **pages)
  128. {
  129. struct mm_struct *mm = current->mm;
  130. unsigned long addr, len, end;
  131. unsigned long next;
  132. pgd_t *pgdp;
  133. int nr = 0;
  134. #ifdef CONFIG_PPC64
  135. unsigned int shift;
  136. int psize;
  137. #endif
  138. pr_devel("%s(%lx,%x,%s)\n", __func__, start, nr_pages, write ? "write" : "read");
  139. start &= PAGE_MASK;
  140. addr = start;
  141. len = (unsigned long) nr_pages << PAGE_SHIFT;
  142. end = start + len;
  143. if (unlikely(!access_ok(write ? VERIFY_WRITE : VERIFY_READ,
  144. start, len)))
  145. goto slow_irqon;
  146. pr_devel(" aligned: %lx .. %lx\n", start, end);
  147. #ifdef CONFIG_HUGETLB_PAGE
  148. /* We bail out on slice boundary crossing when hugetlb is
  149. * enabled in order to not have to deal with two different
  150. * page table formats
  151. */
  152. if (addr < SLICE_LOW_TOP) {
  153. if (end > SLICE_LOW_TOP)
  154. goto slow_irqon;
  155. if (unlikely(GET_LOW_SLICE_INDEX(addr) !=
  156. GET_LOW_SLICE_INDEX(end - 1)))
  157. goto slow_irqon;
  158. } else {
  159. if (unlikely(GET_HIGH_SLICE_INDEX(addr) !=
  160. GET_HIGH_SLICE_INDEX(end - 1)))
  161. goto slow_irqon;
  162. }
  163. #endif /* CONFIG_HUGETLB_PAGE */
  164. /*
  165. * XXX: batch / limit 'nr', to avoid large irq off latency
  166. * needs some instrumenting to determine the common sizes used by
  167. * important workloads (eg. DB2), and whether limiting the batch size
  168. * will decrease performance.
  169. *
  170. * It seems like we're in the clear for the moment. Direct-IO is
  171. * the main guy that batches up lots of get_user_pages, and even
  172. * they are limited to 64-at-a-time which is not so many.
  173. */
  174. /*
  175. * This doesn't prevent pagetable teardown, but does prevent
  176. * the pagetables from being freed on powerpc.
  177. *
  178. * So long as we atomically load page table pointers versus teardown,
  179. * we can follow the address down to the the page and take a ref on it.
  180. */
  181. local_irq_disable();
  182. #ifdef CONFIG_PPC64
  183. /* Those bits are related to hugetlbfs implementation and only exist
  184. * on 64-bit for now
  185. */
  186. psize = get_slice_psize(mm, addr);
  187. shift = mmu_psize_defs[psize].shift;
  188. #endif /* CONFIG_PPC64 */
  189. #ifdef CONFIG_HUGETLB_PAGE
  190. if (unlikely(mmu_huge_psizes[psize])) {
  191. pte_t *ptep;
  192. unsigned long a = addr;
  193. unsigned long sz = ((1UL) << shift);
  194. struct hstate *hstate = size_to_hstate(sz);
  195. BUG_ON(!hstate);
  196. /*
  197. * XXX: could be optimized to avoid hstate
  198. * lookup entirely (just use shift)
  199. */
  200. do {
  201. VM_BUG_ON(shift != mmu_psize_defs[get_slice_psize(mm, a)].shift);
  202. ptep = huge_pte_offset(mm, a);
  203. pr_devel(" %016lx: huge ptep %p\n", a, ptep);
  204. if (!ptep || !gup_huge_pte(ptep, hstate, &a, end, write, pages,
  205. &nr))
  206. goto slow;
  207. } while (a != end);
  208. } else
  209. #endif /* CONFIG_HUGETLB_PAGE */
  210. {
  211. pgdp = pgd_offset(mm, addr);
  212. do {
  213. pgd_t pgd = *pgdp;
  214. #ifdef CONFIG_PPC64
  215. VM_BUG_ON(shift != mmu_psize_defs[get_slice_psize(mm, addr)].shift);
  216. #endif
  217. pr_devel(" %016lx: normal pgd %p\n", addr,
  218. (void *)pgd_val(pgd));
  219. next = pgd_addr_end(addr, end);
  220. if (pgd_none(pgd))
  221. goto slow;
  222. if (!gup_pud_range(pgd, addr, next, write, pages, &nr))
  223. goto slow;
  224. } while (pgdp++, addr = next, addr != end);
  225. }
  226. local_irq_enable();
  227. VM_BUG_ON(nr != (end - start) >> PAGE_SHIFT);
  228. return nr;
  229. {
  230. int ret;
  231. slow:
  232. local_irq_enable();
  233. slow_irqon:
  234. pr_devel(" slow path ! nr = %d\n", nr);
  235. /* Try to get the remaining pages with get_user_pages */
  236. start += nr << PAGE_SHIFT;
  237. pages += nr;
  238. down_read(&mm->mmap_sem);
  239. ret = get_user_pages(current, mm, start,
  240. (end - start) >> PAGE_SHIFT, write, 0, pages, NULL);
  241. up_read(&mm->mmap_sem);
  242. /* Have to be a bit careful with return values */
  243. if (nr > 0) {
  244. if (ret < 0)
  245. ret = nr;
  246. else
  247. ret += nr;
  248. }
  249. return ret;
  250. }
  251. }
  252. #endif /* __HAVE_ARCH_PTE_SPECIAL */