gup.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 = ACCESS_ONCE(*ptep);
  33. struct page *page;
  34. /*
  35. * Similar to the PMD case, NUMA hinting must take slow path
  36. */
  37. if (pte_numa(pte))
  38. return 0;
  39. if ((pte_val(pte) & mask) != result)
  40. return 0;
  41. VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
  42. page = pte_page(pte);
  43. if (!page_cache_get_speculative(page))
  44. return 0;
  45. if (unlikely(pte_val(pte) != pte_val(*ptep))) {
  46. put_page(page);
  47. return 0;
  48. }
  49. pages[*nr] = page;
  50. (*nr)++;
  51. } while (ptep++, addr += PAGE_SIZE, addr != end);
  52. return 1;
  53. }
  54. static int gup_pmd_range(pud_t pud, unsigned long addr, unsigned long end,
  55. int write, struct page **pages, int *nr)
  56. {
  57. unsigned long next;
  58. pmd_t *pmdp;
  59. pmdp = pmd_offset(&pud, addr);
  60. do {
  61. pmd_t pmd = ACCESS_ONCE(*pmdp);
  62. next = pmd_addr_end(addr, end);
  63. /*
  64. * If we find a splitting transparent hugepage we
  65. * return zero. That will result in taking the slow
  66. * path which will call wait_split_huge_page()
  67. * if the pmd is still in splitting state
  68. */
  69. if (pmd_none(pmd) || pmd_trans_splitting(pmd))
  70. return 0;
  71. if (pmd_huge(pmd) || pmd_large(pmd)) {
  72. /*
  73. * NUMA hinting faults need to be handled in the GUP
  74. * slowpath for accounting purposes and so that they
  75. * can be serialised against THP migration.
  76. */
  77. if (pmd_numa(pmd))
  78. return 0;
  79. if (!gup_hugepte((pte_t *)pmdp, PMD_SIZE, addr, next,
  80. write, pages, nr))
  81. return 0;
  82. } else if (is_hugepd(pmdp)) {
  83. if (!gup_hugepd((hugepd_t *)pmdp, PMD_SHIFT,
  84. addr, next, write, pages, nr))
  85. return 0;
  86. } else if (!gup_pte_range(pmd, addr, next, write, pages, nr))
  87. return 0;
  88. } while (pmdp++, addr = next, addr != end);
  89. return 1;
  90. }
  91. static int gup_pud_range(pgd_t pgd, unsigned long addr, unsigned long end,
  92. int write, struct page **pages, int *nr)
  93. {
  94. unsigned long next;
  95. pud_t *pudp;
  96. pudp = pud_offset(&pgd, addr);
  97. do {
  98. pud_t pud = ACCESS_ONCE(*pudp);
  99. next = pud_addr_end(addr, end);
  100. if (pud_none(pud))
  101. return 0;
  102. if (pud_huge(pud)) {
  103. if (!gup_hugepte((pte_t *)pudp, PUD_SIZE, addr, next,
  104. write, pages, nr))
  105. return 0;
  106. } else if (is_hugepd(pudp)) {
  107. if (!gup_hugepd((hugepd_t *)pudp, PUD_SHIFT,
  108. addr, next, write, pages, nr))
  109. return 0;
  110. } else if (!gup_pmd_range(pud, addr, next, write, pages, nr))
  111. return 0;
  112. } while (pudp++, addr = next, addr != end);
  113. return 1;
  114. }
  115. int __get_user_pages_fast(unsigned long start, int nr_pages, int write,
  116. struct page **pages)
  117. {
  118. struct mm_struct *mm = current->mm;
  119. unsigned long addr, len, end;
  120. unsigned long next;
  121. unsigned long flags;
  122. pgd_t *pgdp;
  123. int nr = 0;
  124. pr_devel("%s(%lx,%x,%s)\n", __func__, start, nr_pages, write ? "write" : "read");
  125. start &= PAGE_MASK;
  126. addr = start;
  127. len = (unsigned long) nr_pages << PAGE_SHIFT;
  128. end = start + len;
  129. if (unlikely(!access_ok(write ? VERIFY_WRITE : VERIFY_READ,
  130. start, len)))
  131. return 0;
  132. pr_devel(" aligned: %lx .. %lx\n", start, end);
  133. /*
  134. * XXX: batch / limit 'nr', to avoid large irq off latency
  135. * needs some instrumenting to determine the common sizes used by
  136. * important workloads (eg. DB2), and whether limiting the batch size
  137. * will decrease performance.
  138. *
  139. * It seems like we're in the clear for the moment. Direct-IO is
  140. * the main guy that batches up lots of get_user_pages, and even
  141. * they are limited to 64-at-a-time which is not so many.
  142. */
  143. /*
  144. * This doesn't prevent pagetable teardown, but does prevent
  145. * the pagetables from being freed on powerpc.
  146. *
  147. * So long as we atomically load page table pointers versus teardown,
  148. * we can follow the address down to the the page and take a ref on it.
  149. */
  150. local_irq_save(flags);
  151. pgdp = pgd_offset(mm, addr);
  152. do {
  153. pgd_t pgd = ACCESS_ONCE(*pgdp);
  154. pr_devel(" %016lx: normal pgd %p\n", addr,
  155. (void *)pgd_val(pgd));
  156. next = pgd_addr_end(addr, end);
  157. if (pgd_none(pgd))
  158. break;
  159. if (pgd_huge(pgd)) {
  160. if (!gup_hugepte((pte_t *)pgdp, PGDIR_SIZE, addr, next,
  161. write, pages, &nr))
  162. break;
  163. } else if (is_hugepd(pgdp)) {
  164. if (!gup_hugepd((hugepd_t *)pgdp, PGDIR_SHIFT,
  165. addr, next, write, pages, &nr))
  166. break;
  167. } else if (!gup_pud_range(pgd, addr, next, write, pages, &nr))
  168. break;
  169. } while (pgdp++, addr = next, addr != end);
  170. local_irq_restore(flags);
  171. return nr;
  172. }
  173. int get_user_pages_fast(unsigned long start, int nr_pages, int write,
  174. struct page **pages)
  175. {
  176. struct mm_struct *mm = current->mm;
  177. int nr, ret;
  178. start &= PAGE_MASK;
  179. nr = __get_user_pages_fast(start, nr_pages, write, pages);
  180. ret = nr;
  181. if (nr < nr_pages) {
  182. pr_devel(" slow path ! nr = %d\n", nr);
  183. /* Try to get the remaining pages with get_user_pages */
  184. start += nr << PAGE_SHIFT;
  185. pages += nr;
  186. down_read(&mm->mmap_sem);
  187. ret = get_user_pages(current, mm, start,
  188. nr_pages - nr, write, 0, pages, NULL);
  189. up_read(&mm->mmap_sem);
  190. /* Have to be a bit careful with return values */
  191. if (nr > 0) {
  192. if (ret < 0)
  193. ret = nr;
  194. else
  195. ret += nr;
  196. }
  197. }
  198. return ret;
  199. }
  200. #endif /* __HAVE_ARCH_PTE_SPECIAL */