gup.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. static inline void get_huge_page_tail(struct page *page)
  17. {
  18. /*
  19. * __split_huge_page_refcount() cannot run
  20. * from under us.
  21. */
  22. VM_BUG_ON(page_mapcount(page) < 0);
  23. VM_BUG_ON(atomic_read(&page->_count) != 0);
  24. atomic_inc(&page->_mapcount);
  25. }
  26. /*
  27. * The performance critical leaf functions are made noinline otherwise gcc
  28. * inlines everything into a single function which results in too much
  29. * register pressure.
  30. */
  31. static noinline int gup_pte_range(pmd_t pmd, unsigned long addr,
  32. unsigned long end, int write, struct page **pages, int *nr)
  33. {
  34. unsigned long mask, result;
  35. pte_t *ptep;
  36. result = _PAGE_PRESENT|_PAGE_USER;
  37. if (write)
  38. result |= _PAGE_RW;
  39. mask = result | _PAGE_SPECIAL;
  40. ptep = pte_offset_kernel(&pmd, addr);
  41. do {
  42. pte_t pte = *ptep;
  43. struct page *page;
  44. if ((pte_val(pte) & mask) != result)
  45. return 0;
  46. VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
  47. page = pte_page(pte);
  48. if (!page_cache_get_speculative(page))
  49. return 0;
  50. if (unlikely(pte_val(pte) != pte_val(*ptep))) {
  51. put_page(page);
  52. return 0;
  53. }
  54. if (PageTail(page))
  55. get_huge_page_tail(page);
  56. pages[*nr] = page;
  57. (*nr)++;
  58. } while (ptep++, addr += PAGE_SIZE, addr != end);
  59. return 1;
  60. }
  61. static int gup_pmd_range(pud_t pud, unsigned long addr, unsigned long end,
  62. int write, struct page **pages, int *nr)
  63. {
  64. unsigned long next;
  65. pmd_t *pmdp;
  66. pmdp = pmd_offset(&pud, addr);
  67. do {
  68. pmd_t pmd = *pmdp;
  69. next = pmd_addr_end(addr, end);
  70. if (pmd_none(pmd))
  71. return 0;
  72. if (is_hugepd(pmdp)) {
  73. if (!gup_hugepd((hugepd_t *)pmdp, PMD_SHIFT,
  74. addr, next, write, pages, nr))
  75. return 0;
  76. } else if (!gup_pte_range(pmd, addr, next, write, pages, nr))
  77. return 0;
  78. } while (pmdp++, addr = next, addr != end);
  79. return 1;
  80. }
  81. static int gup_pud_range(pgd_t pgd, unsigned long addr, unsigned long end,
  82. int write, struct page **pages, int *nr)
  83. {
  84. unsigned long next;
  85. pud_t *pudp;
  86. pudp = pud_offset(&pgd, addr);
  87. do {
  88. pud_t pud = *pudp;
  89. next = pud_addr_end(addr, end);
  90. if (pud_none(pud))
  91. return 0;
  92. if (is_hugepd(pudp)) {
  93. if (!gup_hugepd((hugepd_t *)pudp, PUD_SHIFT,
  94. addr, next, write, pages, nr))
  95. return 0;
  96. } else if (!gup_pmd_range(pud, addr, next, write, pages, nr))
  97. return 0;
  98. } while (pudp++, addr = next, addr != end);
  99. return 1;
  100. }
  101. int get_user_pages_fast(unsigned long start, int nr_pages, int write,
  102. struct page **pages)
  103. {
  104. struct mm_struct *mm = current->mm;
  105. unsigned long addr, len, end;
  106. unsigned long next;
  107. pgd_t *pgdp;
  108. int nr = 0;
  109. pr_devel("%s(%lx,%x,%s)\n", __func__, start, nr_pages, write ? "write" : "read");
  110. start &= PAGE_MASK;
  111. addr = start;
  112. len = (unsigned long) nr_pages << PAGE_SHIFT;
  113. end = start + len;
  114. if (unlikely(!access_ok(write ? VERIFY_WRITE : VERIFY_READ,
  115. start, len)))
  116. goto slow_irqon;
  117. pr_devel(" aligned: %lx .. %lx\n", start, end);
  118. /*
  119. * XXX: batch / limit 'nr', to avoid large irq off latency
  120. * needs some instrumenting to determine the common sizes used by
  121. * important workloads (eg. DB2), and whether limiting the batch size
  122. * will decrease performance.
  123. *
  124. * It seems like we're in the clear for the moment. Direct-IO is
  125. * the main guy that batches up lots of get_user_pages, and even
  126. * they are limited to 64-at-a-time which is not so many.
  127. */
  128. /*
  129. * This doesn't prevent pagetable teardown, but does prevent
  130. * the pagetables from being freed on powerpc.
  131. *
  132. * So long as we atomically load page table pointers versus teardown,
  133. * we can follow the address down to the the page and take a ref on it.
  134. */
  135. local_irq_disable();
  136. pgdp = pgd_offset(mm, addr);
  137. do {
  138. pgd_t pgd = *pgdp;
  139. pr_devel(" %016lx: normal pgd %p\n", addr,
  140. (void *)pgd_val(pgd));
  141. next = pgd_addr_end(addr, end);
  142. if (pgd_none(pgd))
  143. goto slow;
  144. if (is_hugepd(pgdp)) {
  145. if (!gup_hugepd((hugepd_t *)pgdp, PGDIR_SHIFT,
  146. addr, next, write, pages, &nr))
  147. goto slow;
  148. } else if (!gup_pud_range(pgd, addr, next, write, pages, &nr))
  149. goto slow;
  150. } while (pgdp++, addr = next, addr != end);
  151. local_irq_enable();
  152. VM_BUG_ON(nr != (end - start) >> PAGE_SHIFT);
  153. return nr;
  154. {
  155. int ret;
  156. slow:
  157. local_irq_enable();
  158. slow_irqon:
  159. pr_devel(" slow path ! nr = %d\n", nr);
  160. /* Try to get the remaining pages with get_user_pages */
  161. start += nr << PAGE_SHIFT;
  162. pages += nr;
  163. down_read(&mm->mmap_sem);
  164. ret = get_user_pages(current, mm, start,
  165. (end - start) >> PAGE_SHIFT, write, 0, pages, NULL);
  166. up_read(&mm->mmap_sem);
  167. /* Have to be a bit careful with return values */
  168. if (nr > 0) {
  169. if (ret < 0)
  170. ret = nr;
  171. else
  172. ret += nr;
  173. }
  174. return ret;
  175. }
  176. }
  177. #endif /* __HAVE_ARCH_PTE_SPECIAL */