gup.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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_PROTECT : 0) | _PAGE_INVALID | _PAGE_SPECIAL;
  26. ptep = ((pte_t *) pmd_deref(pmd)) + pte_index(addr);
  27. do {
  28. pte = *ptep;
  29. barrier();
  30. /* Similar to the PMD case, NUMA hinting must take slow path */
  31. if (pte_protnone(pte))
  32. return 0;
  33. if ((pte_val(pte) & mask) != 0)
  34. return 0;
  35. VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
  36. page = pte_page(pte);
  37. if (!page_cache_get_speculative(page))
  38. return 0;
  39. if (unlikely(pte_val(pte) != pte_val(*ptep))) {
  40. put_page(page);
  41. return 0;
  42. }
  43. pages[*nr] = page;
  44. (*nr)++;
  45. } while (ptep++, addr += PAGE_SIZE, addr != end);
  46. return 1;
  47. }
  48. static inline int gup_huge_pmd(pmd_t *pmdp, pmd_t pmd, unsigned long addr,
  49. unsigned long end, int write, struct page **pages, int *nr)
  50. {
  51. unsigned long mask, result;
  52. struct page *head, *page, *tail;
  53. int refs;
  54. result = write ? 0 : _SEGMENT_ENTRY_PROTECT;
  55. mask = result | _SEGMENT_ENTRY_INVALID;
  56. if ((pmd_val(pmd) & mask) != result)
  57. return 0;
  58. VM_BUG_ON(!pfn_valid(pmd_val(pmd) >> PAGE_SHIFT));
  59. refs = 0;
  60. head = pmd_page(pmd);
  61. page = head + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
  62. tail = page;
  63. do {
  64. VM_BUG_ON(compound_head(page) != head);
  65. pages[*nr] = page;
  66. (*nr)++;
  67. page++;
  68. refs++;
  69. } while (addr += PAGE_SIZE, addr != end);
  70. if (!page_cache_add_speculative(head, refs)) {
  71. *nr -= refs;
  72. return 0;
  73. }
  74. if (unlikely(pmd_val(pmd) != pmd_val(*pmdp))) {
  75. *nr -= refs;
  76. while (refs--)
  77. put_page(head);
  78. return 0;
  79. }
  80. /*
  81. * Any tail page need their mapcount reference taken before we
  82. * return.
  83. */
  84. while (refs--) {
  85. if (PageTail(tail))
  86. get_huge_page_tail(tail);
  87. tail++;
  88. }
  89. return 1;
  90. }
  91. static inline int gup_pmd_range(pud_t *pudp, pud_t pud, unsigned long addr,
  92. unsigned long end, int write, struct page **pages, int *nr)
  93. {
  94. unsigned long next;
  95. pmd_t *pmdp, pmd;
  96. pmdp = (pmd_t *) pudp;
  97. if ((pud_val(pud) & _REGION_ENTRY_TYPE_MASK) == _REGION_ENTRY_TYPE_R3)
  98. pmdp = (pmd_t *) pud_deref(pud);
  99. pmdp += pmd_index(addr);
  100. do {
  101. pmd = *pmdp;
  102. barrier();
  103. next = pmd_addr_end(addr, end);
  104. /*
  105. * The pmd_trans_splitting() check below explains why
  106. * pmdp_splitting_flush() has to serialize with
  107. * smp_call_function() against our disabled IRQs, to stop
  108. * this gup-fast code from running while we set the
  109. * splitting bit in the pmd. Returning zero will take
  110. * the slow path that will call wait_split_huge_page()
  111. * if the pmd is still in splitting state.
  112. */
  113. if (pmd_none(pmd) || pmd_trans_splitting(pmd))
  114. return 0;
  115. if (unlikely(pmd_large(pmd))) {
  116. /*
  117. * NUMA hinting faults need to be handled in the GUP
  118. * slowpath for accounting purposes and so that they
  119. * can be serialised against THP migration.
  120. */
  121. if (pmd_protnone(pmd))
  122. return 0;
  123. if (!gup_huge_pmd(pmdp, pmd, addr, next,
  124. write, pages, nr))
  125. return 0;
  126. } else if (!gup_pte_range(pmdp, pmd, addr, next,
  127. write, pages, nr))
  128. return 0;
  129. } while (pmdp++, addr = next, addr != end);
  130. return 1;
  131. }
  132. static inline int gup_pud_range(pgd_t *pgdp, pgd_t pgd, unsigned long addr,
  133. unsigned long end, int write, struct page **pages, int *nr)
  134. {
  135. unsigned long next;
  136. pud_t *pudp, pud;
  137. pudp = (pud_t *) pgdp;
  138. if ((pgd_val(pgd) & _REGION_ENTRY_TYPE_MASK) == _REGION_ENTRY_TYPE_R2)
  139. pudp = (pud_t *) pgd_deref(pgd);
  140. pudp += pud_index(addr);
  141. do {
  142. pud = *pudp;
  143. barrier();
  144. next = pud_addr_end(addr, end);
  145. if (pud_none(pud))
  146. return 0;
  147. if (!gup_pmd_range(pudp, pud, addr, next, write, pages, nr))
  148. return 0;
  149. } while (pudp++, addr = next, addr != end);
  150. return 1;
  151. }
  152. /*
  153. * Like get_user_pages_fast() except its IRQ-safe in that it won't fall
  154. * back to the regular GUP.
  155. */
  156. int __get_user_pages_fast(unsigned long start, int nr_pages, int write,
  157. struct page **pages)
  158. {
  159. struct mm_struct *mm = current->mm;
  160. unsigned long addr, len, end;
  161. unsigned long next, flags;
  162. pgd_t *pgdp, pgd;
  163. int nr = 0;
  164. start &= PAGE_MASK;
  165. addr = start;
  166. len = (unsigned long) nr_pages << PAGE_SHIFT;
  167. end = start + len;
  168. if ((end <= start) || (end > TASK_SIZE))
  169. return 0;
  170. /*
  171. * local_irq_save() doesn't prevent pagetable teardown, but does
  172. * prevent the pagetables from being freed on s390.
  173. *
  174. * So long as we atomically load page table pointers versus teardown,
  175. * we can follow the address down to the the page and take a ref on it.
  176. */
  177. local_irq_save(flags);
  178. pgdp = pgd_offset(mm, addr);
  179. do {
  180. pgd = *pgdp;
  181. barrier();
  182. next = pgd_addr_end(addr, end);
  183. if (pgd_none(pgd))
  184. break;
  185. if (!gup_pud_range(pgdp, pgd, addr, next, write, pages, &nr))
  186. break;
  187. } while (pgdp++, addr = next, addr != end);
  188. local_irq_restore(flags);
  189. return nr;
  190. }
  191. /**
  192. * get_user_pages_fast() - pin user pages in memory
  193. * @start: starting user address
  194. * @nr_pages: number of pages from start to pin
  195. * @write: whether pages will be written to
  196. * @pages: array that receives pointers to the pages pinned.
  197. * Should be at least nr_pages long.
  198. *
  199. * Attempt to pin user pages in memory without taking mm->mmap_sem.
  200. * If not successful, it will fall back to taking the lock and
  201. * calling get_user_pages().
  202. *
  203. * Returns number of pages pinned. This may be fewer than the number
  204. * requested. If nr_pages is 0 or negative, returns 0. If no pages
  205. * were pinned, returns -errno.
  206. */
  207. int get_user_pages_fast(unsigned long start, int nr_pages, int write,
  208. struct page **pages)
  209. {
  210. struct mm_struct *mm = current->mm;
  211. int nr, ret;
  212. start &= PAGE_MASK;
  213. nr = __get_user_pages_fast(start, nr_pages, write, pages);
  214. if (nr == nr_pages)
  215. return nr;
  216. /* Try to get the remaining pages with get_user_pages */
  217. start += nr << PAGE_SHIFT;
  218. pages += nr;
  219. ret = get_user_pages_unlocked(current, mm, start,
  220. nr_pages - nr, pages, write ? FOLL_WRITE : 0);
  221. /* Have to be a bit careful with return values */
  222. if (nr > 0)
  223. ret = (ret < 0) ? nr : ret + nr;
  224. return ret;
  225. }