gup.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. u32 mask;
  66. int refs;
  67. mask = PMD_HUGE_PRESENT;
  68. if (write)
  69. mask |= PMD_HUGE_WRITE;
  70. if ((pmd_val(pmd) & mask) != mask)
  71. return 0;
  72. refs = 0;
  73. head = pmd_page(pmd);
  74. page = head + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
  75. tail = page;
  76. do {
  77. VM_BUG_ON(compound_head(page) != head);
  78. pages[*nr] = page;
  79. (*nr)++;
  80. page++;
  81. refs++;
  82. } while (addr += PAGE_SIZE, addr != end);
  83. if (!page_cache_add_speculative(head, refs)) {
  84. *nr -= refs;
  85. return 0;
  86. }
  87. if (unlikely(pmd_val(pmd) != pmd_val(*pmdp))) {
  88. *nr -= refs;
  89. while (refs--)
  90. put_page(head);
  91. return 0;
  92. }
  93. /* Any tail page need their mapcount reference taken before we
  94. * return.
  95. */
  96. while (refs--) {
  97. if (PageTail(tail))
  98. get_huge_page_tail(tail);
  99. tail++;
  100. }
  101. return 1;
  102. }
  103. static int gup_pmd_range(pud_t pud, unsigned long addr, unsigned long end,
  104. int write, struct page **pages, int *nr)
  105. {
  106. unsigned long next;
  107. pmd_t *pmdp;
  108. pmdp = pmd_offset(&pud, addr);
  109. do {
  110. pmd_t pmd = *pmdp;
  111. next = pmd_addr_end(addr, end);
  112. if (pmd_none(pmd) || pmd_trans_splitting(pmd))
  113. return 0;
  114. if (unlikely(pmd_large(pmd))) {
  115. if (!gup_huge_pmd(pmdp, pmd, addr, next,
  116. write, pages, nr))
  117. return 0;
  118. } else if (!gup_pte_range(pmd, addr, next, write,
  119. pages, nr))
  120. return 0;
  121. } while (pmdp++, addr = next, addr != end);
  122. return 1;
  123. }
  124. static int gup_pud_range(pgd_t pgd, unsigned long addr, unsigned long end,
  125. int write, struct page **pages, int *nr)
  126. {
  127. unsigned long next;
  128. pud_t *pudp;
  129. pudp = pud_offset(&pgd, addr);
  130. do {
  131. pud_t pud = *pudp;
  132. next = pud_addr_end(addr, end);
  133. if (pud_none(pud))
  134. return 0;
  135. if (!gup_pmd_range(pud, addr, next, write, pages, nr))
  136. return 0;
  137. } while (pudp++, addr = next, addr != end);
  138. return 1;
  139. }
  140. int get_user_pages_fast(unsigned long start, int nr_pages, int write,
  141. struct page **pages)
  142. {
  143. struct mm_struct *mm = current->mm;
  144. unsigned long addr, len, end;
  145. unsigned long next;
  146. pgd_t *pgdp;
  147. int nr = 0;
  148. start &= PAGE_MASK;
  149. addr = start;
  150. len = (unsigned long) nr_pages << PAGE_SHIFT;
  151. end = start + len;
  152. /*
  153. * XXX: batch / limit 'nr', to avoid large irq off latency
  154. * needs some instrumenting to determine the common sizes used by
  155. * important workloads (eg. DB2), and whether limiting the batch size
  156. * will decrease performance.
  157. *
  158. * It seems like we're in the clear for the moment. Direct-IO is
  159. * the main guy that batches up lots of get_user_pages, and even
  160. * they are limited to 64-at-a-time which is not so many.
  161. */
  162. /*
  163. * This doesn't prevent pagetable teardown, but does prevent
  164. * the pagetables from being freed on sparc.
  165. *
  166. * So long as we atomically load page table pointers versus teardown,
  167. * we can follow the address down to the the page and take a ref on it.
  168. */
  169. local_irq_disable();
  170. pgdp = pgd_offset(mm, addr);
  171. do {
  172. pgd_t pgd = *pgdp;
  173. next = pgd_addr_end(addr, end);
  174. if (pgd_none(pgd))
  175. goto slow;
  176. if (!gup_pud_range(pgd, addr, next, write, pages, &nr))
  177. goto slow;
  178. } while (pgdp++, addr = next, addr != end);
  179. local_irq_enable();
  180. VM_BUG_ON(nr != (end - start) >> PAGE_SHIFT);
  181. return nr;
  182. {
  183. int ret;
  184. slow:
  185. local_irq_enable();
  186. /* Try to get the remaining pages with get_user_pages */
  187. start += nr << PAGE_SHIFT;
  188. pages += nr;
  189. down_read(&mm->mmap_sem);
  190. ret = get_user_pages(current, mm, start,
  191. (end - start) >> PAGE_SHIFT, write, 0, pages, NULL);
  192. up_read(&mm->mmap_sem);
  193. /* Have to be a bit careful with return values */
  194. if (nr > 0) {
  195. if (ret < 0)
  196. ret = nr;
  197. else
  198. ret += nr;
  199. }
  200. return ret;
  201. }
  202. }