gup.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Lockless get_user_pages_fast for sparc, cribbed from powerpc
  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/pagemap.h>
  11. #include <linux/rwsem.h>
  12. #include <asm/pgtable.h>
  13. /*
  14. * The performance critical leaf functions are made noinline otherwise gcc
  15. * inlines everything into a single function which results in too much
  16. * register pressure.
  17. */
  18. static noinline int gup_pte_range(pmd_t pmd, unsigned long addr,
  19. unsigned long end, int write, struct page **pages, int *nr)
  20. {
  21. unsigned long mask, result;
  22. pte_t *ptep;
  23. if (tlb_type == hypervisor) {
  24. result = _PAGE_PRESENT_4V|_PAGE_P_4V;
  25. if (write)
  26. result |= _PAGE_WRITE_4V;
  27. } else {
  28. result = _PAGE_PRESENT_4U|_PAGE_P_4U;
  29. if (write)
  30. result |= _PAGE_WRITE_4U;
  31. }
  32. mask = result | _PAGE_SPECIAL;
  33. ptep = pte_offset_kernel(&pmd, addr);
  34. do {
  35. struct page *page, *head;
  36. pte_t pte = *ptep;
  37. if ((pte_val(pte) & mask) != result)
  38. return 0;
  39. VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
  40. /* The hugepage case is simplified on sparc64 because
  41. * we encode the sub-page pfn offsets into the
  42. * hugepage PTEs. We could optimize this in the future
  43. * use page_cache_add_speculative() for the hugepage case.
  44. */
  45. page = pte_page(pte);
  46. head = compound_head(page);
  47. if (!page_cache_get_speculative(head))
  48. return 0;
  49. if (unlikely(pte_val(pte) != pte_val(*ptep))) {
  50. put_page(head);
  51. return 0;
  52. }
  53. if (head != 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_huge_pmd(pmd_t *pmdp, pmd_t pmd, unsigned long addr,
  61. unsigned long end, int write, struct page **pages,
  62. int *nr)
  63. {
  64. struct page *head, *page, *tail;
  65. int refs;
  66. if (!pmd_large(pmd))
  67. return 0;
  68. if (write && !pmd_write(pmd))
  69. return 0;
  70. refs = 0;
  71. head = pmd_page(pmd);
  72. page = head + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
  73. tail = page;
  74. do {
  75. VM_BUG_ON(compound_head(page) != head);
  76. pages[*nr] = page;
  77. (*nr)++;
  78. page++;
  79. refs++;
  80. } while (addr += PAGE_SIZE, addr != end);
  81. if (!page_cache_add_speculative(head, refs)) {
  82. *nr -= refs;
  83. return 0;
  84. }
  85. if (unlikely(pmd_val(pmd) != pmd_val(*pmdp))) {
  86. *nr -= refs;
  87. while (refs--)
  88. put_page(head);
  89. return 0;
  90. }
  91. /* Any tail page need their mapcount reference taken before we
  92. * return.
  93. */
  94. while (refs--) {
  95. if (PageTail(tail))
  96. get_huge_page_tail(tail);
  97. tail++;
  98. }
  99. return 1;
  100. }
  101. static int gup_pmd_range(pud_t pud, unsigned long addr, unsigned long end,
  102. int write, struct page **pages, int *nr)
  103. {
  104. unsigned long next;
  105. pmd_t *pmdp;
  106. pmdp = pmd_offset(&pud, addr);
  107. do {
  108. pmd_t pmd = *pmdp;
  109. next = pmd_addr_end(addr, end);
  110. if (pmd_none(pmd) || pmd_trans_splitting(pmd))
  111. return 0;
  112. if (unlikely(pmd_large(pmd))) {
  113. if (!gup_huge_pmd(pmdp, pmd, addr, next,
  114. write, pages, nr))
  115. return 0;
  116. } else if (!gup_pte_range(pmd, addr, next, write,
  117. pages, nr))
  118. return 0;
  119. } while (pmdp++, addr = next, addr != end);
  120. return 1;
  121. }
  122. static int gup_pud_range(pgd_t pgd, unsigned long addr, unsigned long end,
  123. int write, struct page **pages, int *nr)
  124. {
  125. unsigned long next;
  126. pud_t *pudp;
  127. pudp = pud_offset(&pgd, addr);
  128. do {
  129. pud_t pud = *pudp;
  130. next = pud_addr_end(addr, end);
  131. if (pud_none(pud))
  132. return 0;
  133. if (!gup_pmd_range(pud, addr, next, write, pages, nr))
  134. return 0;
  135. } while (pudp++, addr = next, addr != end);
  136. return 1;
  137. }
  138. int get_user_pages_fast(unsigned long start, int nr_pages, int write,
  139. struct page **pages)
  140. {
  141. struct mm_struct *mm = current->mm;
  142. unsigned long addr, len, end;
  143. unsigned long next;
  144. pgd_t *pgdp;
  145. int nr = 0;
  146. start &= PAGE_MASK;
  147. addr = start;
  148. len = (unsigned long) nr_pages << PAGE_SHIFT;
  149. end = start + len;
  150. /*
  151. * XXX: batch / limit 'nr', to avoid large irq off latency
  152. * needs some instrumenting to determine the common sizes used by
  153. * important workloads (eg. DB2), and whether limiting the batch size
  154. * will decrease performance.
  155. *
  156. * It seems like we're in the clear for the moment. Direct-IO is
  157. * the main guy that batches up lots of get_user_pages, and even
  158. * they are limited to 64-at-a-time which is not so many.
  159. */
  160. /*
  161. * This doesn't prevent pagetable teardown, but does prevent
  162. * the pagetables from being freed on sparc.
  163. *
  164. * So long as we atomically load page table pointers versus teardown,
  165. * we can follow the address down to the the page and take a ref on it.
  166. */
  167. local_irq_disable();
  168. pgdp = pgd_offset(mm, addr);
  169. do {
  170. pgd_t pgd = *pgdp;
  171. next = pgd_addr_end(addr, end);
  172. if (pgd_none(pgd))
  173. goto slow;
  174. if (!gup_pud_range(pgd, addr, next, write, pages, &nr))
  175. goto slow;
  176. } while (pgdp++, addr = next, addr != end);
  177. local_irq_enable();
  178. VM_BUG_ON(nr != (end - start) >> PAGE_SHIFT);
  179. return nr;
  180. {
  181. int ret;
  182. slow:
  183. local_irq_enable();
  184. /* Try to get the remaining pages with get_user_pages */
  185. start += nr << PAGE_SHIFT;
  186. pages += nr;
  187. down_read(&mm->mmap_sem);
  188. ret = get_user_pages(current, mm, start,
  189. (end - start) >> PAGE_SHIFT, write, 0, pages, NULL);
  190. up_read(&mm->mmap_sem);
  191. /* Have to be a bit careful with return values */
  192. if (nr > 0) {
  193. if (ret < 0)
  194. ret = nr;
  195. else
  196. ret += nr;
  197. }
  198. return ret;
  199. }
  200. }