gup.c 4.8 KB

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