gup.c 5.5 KB

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