gup.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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_val(pmd) & _PAGE_VALID))
  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, flags;
  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. local_irq_save(flags);
  151. pgdp = pgd_offset(mm, addr);
  152. do {
  153. pgd_t pgd = *pgdp;
  154. next = pgd_addr_end(addr, end);
  155. if (pgd_none(pgd))
  156. break;
  157. if (!gup_pud_range(pgd, addr, next, write, pages, &nr))
  158. break;
  159. } while (pgdp++, addr = next, addr != end);
  160. local_irq_restore(flags);
  161. return nr;
  162. }
  163. int get_user_pages_fast(unsigned long start, int nr_pages, int write,
  164. struct page **pages)
  165. {
  166. struct mm_struct *mm = current->mm;
  167. unsigned long addr, len, end;
  168. unsigned long next;
  169. pgd_t *pgdp;
  170. int nr = 0;
  171. start &= PAGE_MASK;
  172. addr = start;
  173. len = (unsigned long) nr_pages << PAGE_SHIFT;
  174. end = start + len;
  175. /*
  176. * XXX: batch / limit 'nr', to avoid large irq off latency
  177. * needs some instrumenting to determine the common sizes used by
  178. * important workloads (eg. DB2), and whether limiting the batch size
  179. * will decrease performance.
  180. *
  181. * It seems like we're in the clear for the moment. Direct-IO is
  182. * the main guy that batches up lots of get_user_pages, and even
  183. * they are limited to 64-at-a-time which is not so many.
  184. */
  185. /*
  186. * This doesn't prevent pagetable teardown, but does prevent
  187. * the pagetables from being freed on sparc.
  188. *
  189. * So long as we atomically load page table pointers versus teardown,
  190. * we can follow the address down to the the page and take a ref on it.
  191. */
  192. local_irq_disable();
  193. pgdp = pgd_offset(mm, addr);
  194. do {
  195. pgd_t pgd = *pgdp;
  196. next = pgd_addr_end(addr, end);
  197. if (pgd_none(pgd))
  198. goto slow;
  199. if (!gup_pud_range(pgd, addr, next, write, pages, &nr))
  200. goto slow;
  201. } while (pgdp++, addr = next, addr != end);
  202. local_irq_enable();
  203. VM_BUG_ON(nr != (end - start) >> PAGE_SHIFT);
  204. return nr;
  205. {
  206. int ret;
  207. slow:
  208. local_irq_enable();
  209. /* Try to get the remaining pages with get_user_pages */
  210. start += nr << PAGE_SHIFT;
  211. pages += nr;
  212. ret = get_user_pages_unlocked(current, mm, start,
  213. (end - start) >> PAGE_SHIFT, pages,
  214. write ? FOLL_WRITE : 0);
  215. /* Have to be a bit careful with return values */
  216. if (nr > 0) {
  217. if (ret < 0)
  218. ret = nr;
  219. else
  220. ret += nr;
  221. }
  222. return ret;
  223. }
  224. }