gup.c 6.7 KB

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