gup.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * Lockless get_user_pages_fast for s390
  3. *
  4. * Copyright IBM Corp. 2010
  5. * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
  6. */
  7. #include <linux/sched.h>
  8. #include <linux/mm.h>
  9. #include <linux/hugetlb.h>
  10. #include <linux/vmstat.h>
  11. #include <linux/pagemap.h>
  12. #include <linux/rwsem.h>
  13. #include <asm/pgtable.h>
  14. /*
  15. * The performance critical leaf functions are made noinline otherwise gcc
  16. * inlines everything into a single function which results in too much
  17. * register pressure.
  18. */
  19. static inline int gup_pte_range(pmd_t *pmdp, pmd_t pmd, unsigned long addr,
  20. unsigned long end, int write, struct page **pages, int *nr)
  21. {
  22. unsigned long mask;
  23. pte_t *ptep, pte;
  24. struct page *page;
  25. mask = (write ? _PAGE_RO : 0) | _PAGE_INVALID | _PAGE_SPECIAL;
  26. ptep = ((pte_t *) pmd_deref(pmd)) + pte_index(addr);
  27. do {
  28. pte = *ptep;
  29. barrier();
  30. if ((pte_val(pte) & mask) != 0)
  31. return 0;
  32. VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
  33. page = pte_page(pte);
  34. if (!page_cache_get_speculative(page))
  35. return 0;
  36. if (unlikely(pte_val(pte) != pte_val(*ptep))) {
  37. put_page(page);
  38. return 0;
  39. }
  40. pages[*nr] = page;
  41. (*nr)++;
  42. } while (ptep++, addr += PAGE_SIZE, addr != end);
  43. return 1;
  44. }
  45. static inline int gup_huge_pmd(pmd_t *pmdp, pmd_t pmd, unsigned long addr,
  46. unsigned long end, int write, struct page **pages, int *nr)
  47. {
  48. unsigned long mask, result;
  49. struct page *head, *page, *tail;
  50. int refs;
  51. result = write ? 0 : _SEGMENT_ENTRY_RO;
  52. mask = result | _SEGMENT_ENTRY_INV;
  53. if ((pmd_val(pmd) & mask) != result)
  54. return 0;
  55. VM_BUG_ON(!pfn_valid(pmd_val(pmd) >> PAGE_SHIFT));
  56. refs = 0;
  57. head = pmd_page(pmd);
  58. page = head + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
  59. tail = page;
  60. do {
  61. VM_BUG_ON(compound_head(page) != head);
  62. pages[*nr] = page;
  63. (*nr)++;
  64. page++;
  65. refs++;
  66. } while (addr += PAGE_SIZE, addr != end);
  67. if (!page_cache_add_speculative(head, refs)) {
  68. *nr -= refs;
  69. return 0;
  70. }
  71. if (unlikely(pmd_val(pmd) != pmd_val(*pmdp))) {
  72. *nr -= refs;
  73. while (refs--)
  74. put_page(head);
  75. return 0;
  76. }
  77. /*
  78. * Any tail page need their mapcount reference taken before we
  79. * return.
  80. */
  81. while (refs--) {
  82. if (PageTail(tail))
  83. get_huge_page_tail(tail);
  84. tail++;
  85. }
  86. return 1;
  87. }
  88. static inline int gup_pmd_range(pud_t *pudp, pud_t pud, unsigned long addr,
  89. unsigned long end, int write, struct page **pages, int *nr)
  90. {
  91. unsigned long next;
  92. pmd_t *pmdp, pmd;
  93. pmdp = (pmd_t *) pudp;
  94. #ifdef CONFIG_64BIT
  95. if ((pud_val(pud) & _REGION_ENTRY_TYPE_MASK) == _REGION_ENTRY_TYPE_R3)
  96. pmdp = (pmd_t *) pud_deref(pud);
  97. pmdp += pmd_index(addr);
  98. #endif
  99. do {
  100. pmd = *pmdp;
  101. barrier();
  102. next = pmd_addr_end(addr, end);
  103. /*
  104. * The pmd_trans_splitting() check below explains why
  105. * pmdp_splitting_flush() has to serialize with
  106. * smp_call_function() against our disabled IRQs, to stop
  107. * this gup-fast code from running while we set the
  108. * splitting bit in the pmd. Returning zero will take
  109. * the slow path that will call wait_split_huge_page()
  110. * if the pmd is still in splitting state.
  111. */
  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(pmdp, pmd, addr, next,
  119. write, pages, nr))
  120. return 0;
  121. } while (pmdp++, addr = next, addr != end);
  122. return 1;
  123. }
  124. static inline int gup_pud_range(pgd_t *pgdp, pgd_t pgd, unsigned long addr,
  125. unsigned long end, int write, struct page **pages, int *nr)
  126. {
  127. unsigned long next;
  128. pud_t *pudp, pud;
  129. pudp = (pud_t *) pgdp;
  130. #ifdef CONFIG_64BIT
  131. if ((pgd_val(pgd) & _REGION_ENTRY_TYPE_MASK) == _REGION_ENTRY_TYPE_R2)
  132. pudp = (pud_t *) pgd_deref(pgd);
  133. pudp += pud_index(addr);
  134. #endif
  135. do {
  136. pud = *pudp;
  137. barrier();
  138. next = pud_addr_end(addr, end);
  139. if (pud_none(pud))
  140. return 0;
  141. if (!gup_pmd_range(pudp, pud, addr, next, write, pages, nr))
  142. return 0;
  143. } while (pudp++, addr = next, addr != end);
  144. return 1;
  145. }
  146. /*
  147. * Like get_user_pages_fast() except its IRQ-safe in that it won't fall
  148. * back to the regular GUP.
  149. */
  150. int __get_user_pages_fast(unsigned long start, int nr_pages, int write,
  151. struct page **pages)
  152. {
  153. struct mm_struct *mm = current->mm;
  154. unsigned long addr, len, end;
  155. unsigned long next, flags;
  156. pgd_t *pgdp, pgd;
  157. int nr = 0;
  158. start &= PAGE_MASK;
  159. addr = start;
  160. len = (unsigned long) nr_pages << PAGE_SHIFT;
  161. end = start + len;
  162. if ((end < start) || (end > TASK_SIZE))
  163. return 0;
  164. local_irq_save(flags);
  165. pgdp = pgd_offset(mm, addr);
  166. do {
  167. pgd = *pgdp;
  168. barrier();
  169. next = pgd_addr_end(addr, end);
  170. if (pgd_none(pgd))
  171. break;
  172. if (!gup_pud_range(pgdp, pgd, addr, next, write, pages, &nr))
  173. break;
  174. } while (pgdp++, addr = next, addr != end);
  175. local_irq_restore(flags);
  176. return nr;
  177. }
  178. /**
  179. * get_user_pages_fast() - pin user pages in memory
  180. * @start: starting user address
  181. * @nr_pages: number of pages from start to pin
  182. * @write: whether pages will be written to
  183. * @pages: array that receives pointers to the pages pinned.
  184. * Should be at least nr_pages long.
  185. *
  186. * Attempt to pin user pages in memory without taking mm->mmap_sem.
  187. * If not successful, it will fall back to taking the lock and
  188. * calling get_user_pages().
  189. *
  190. * Returns number of pages pinned. This may be fewer than the number
  191. * requested. If nr_pages is 0 or negative, returns 0. If no pages
  192. * were pinned, returns -errno.
  193. */
  194. int get_user_pages_fast(unsigned long start, int nr_pages, int write,
  195. struct page **pages)
  196. {
  197. struct mm_struct *mm = current->mm;
  198. unsigned long addr, len, end;
  199. unsigned long next;
  200. pgd_t *pgdp, pgd;
  201. int nr = 0;
  202. start &= PAGE_MASK;
  203. addr = start;
  204. len = (unsigned long) nr_pages << PAGE_SHIFT;
  205. end = start + len;
  206. if ((end < start) || (end > TASK_SIZE))
  207. goto slow_irqon;
  208. /*
  209. * local_irq_disable() doesn't prevent pagetable teardown, but does
  210. * prevent the pagetables from being freed on s390.
  211. *
  212. * So long as we atomically load page table pointers versus teardown,
  213. * we can follow the address down to the the page and take a ref on it.
  214. */
  215. local_irq_disable();
  216. pgdp = pgd_offset(mm, addr);
  217. do {
  218. pgd = *pgdp;
  219. barrier();
  220. next = pgd_addr_end(addr, end);
  221. if (pgd_none(pgd))
  222. goto slow;
  223. if (!gup_pud_range(pgdp, pgd, addr, next, write, pages, &nr))
  224. goto slow;
  225. } while (pgdp++, addr = next, addr != end);
  226. local_irq_enable();
  227. VM_BUG_ON(nr != (end - start) >> PAGE_SHIFT);
  228. return nr;
  229. {
  230. int ret;
  231. slow:
  232. local_irq_enable();
  233. slow_irqon:
  234. /* Try to get the remaining pages with get_user_pages */
  235. start += nr << PAGE_SHIFT;
  236. pages += nr;
  237. down_read(&mm->mmap_sem);
  238. ret = get_user_pages(current, mm, start,
  239. (end - start) >> PAGE_SHIFT, write, 0, pages, NULL);
  240. up_read(&mm->mmap_sem);
  241. /* Have to be a bit careful with return values */
  242. if (nr > 0) {
  243. if (ret < 0)
  244. ret = nr;
  245. else
  246. ret += nr;
  247. }
  248. return ret;
  249. }
  250. }