gup.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Lockless get_user_pages_fast for s390
  4. *
  5. * Copyright IBM Corp. 2010
  6. * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
  7. */
  8. #include <linux/sched.h>
  9. #include <linux/mm.h>
  10. #include <linux/hugetlb.h>
  11. #include <linux/vmstat.h>
  12. #include <linux/pagemap.h>
  13. #include <linux/rwsem.h>
  14. #include <asm/pgtable.h>
  15. /*
  16. * The performance critical leaf functions are made noinline otherwise gcc
  17. * inlines everything into a single function which results in too much
  18. * register pressure.
  19. */
  20. static inline int gup_pte_range(pmd_t *pmdp, pmd_t pmd, unsigned long addr,
  21. unsigned long end, int write, struct page **pages, int *nr)
  22. {
  23. struct page *head, *page;
  24. unsigned long mask;
  25. pte_t *ptep, pte;
  26. mask = (write ? _PAGE_PROTECT : 0) | _PAGE_INVALID | _PAGE_SPECIAL;
  27. ptep = ((pte_t *) pmd_deref(pmd)) + pte_index(addr);
  28. do {
  29. pte = *ptep;
  30. barrier();
  31. /* Similar to the PMD case, NUMA hinting must take slow path */
  32. if (pte_protnone(pte))
  33. return 0;
  34. if ((pte_val(pte) & mask) != 0)
  35. return 0;
  36. VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
  37. page = pte_page(pte);
  38. head = compound_head(page);
  39. if (unlikely(WARN_ON_ONCE(page_ref_count(head) < 0)
  40. || !page_cache_get_speculative(head)))
  41. return 0;
  42. if (unlikely(pte_val(pte) != pte_val(*ptep))) {
  43. put_page(head);
  44. return 0;
  45. }
  46. VM_BUG_ON_PAGE(compound_head(page) != head, page);
  47. pages[*nr] = page;
  48. (*nr)++;
  49. } while (ptep++, addr += PAGE_SIZE, addr != end);
  50. return 1;
  51. }
  52. static inline int gup_huge_pmd(pmd_t *pmdp, pmd_t pmd, unsigned long addr,
  53. unsigned long end, int write, struct page **pages, int *nr)
  54. {
  55. struct page *head, *page;
  56. unsigned long mask;
  57. int refs;
  58. mask = (write ? _SEGMENT_ENTRY_PROTECT : 0) | _SEGMENT_ENTRY_INVALID;
  59. if ((pmd_val(pmd) & mask) != 0)
  60. return 0;
  61. VM_BUG_ON(!pfn_valid(pmd_val(pmd) >> PAGE_SHIFT));
  62. refs = 0;
  63. head = pmd_page(pmd);
  64. page = head + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
  65. do {
  66. VM_BUG_ON(compound_head(page) != head);
  67. pages[*nr] = page;
  68. (*nr)++;
  69. page++;
  70. refs++;
  71. } while (addr += PAGE_SIZE, addr != end);
  72. if (unlikely(WARN_ON_ONCE(page_ref_count(head) < 0)
  73. || !page_cache_add_speculative(head, refs))) {
  74. *nr -= refs;
  75. return 0;
  76. }
  77. if (unlikely(pmd_val(pmd) != pmd_val(*pmdp))) {
  78. *nr -= refs;
  79. while (refs--)
  80. put_page(head);
  81. return 0;
  82. }
  83. return 1;
  84. }
  85. static inline int gup_pmd_range(pud_t *pudp, pud_t pud, unsigned long addr,
  86. unsigned long end, int write, struct page **pages, int *nr)
  87. {
  88. unsigned long next;
  89. pmd_t *pmdp, pmd;
  90. pmdp = (pmd_t *) pudp;
  91. if ((pud_val(pud) & _REGION_ENTRY_TYPE_MASK) == _REGION_ENTRY_TYPE_R3)
  92. pmdp = (pmd_t *) pud_deref(pud);
  93. pmdp += pmd_index(addr);
  94. do {
  95. pmd = *pmdp;
  96. barrier();
  97. next = pmd_addr_end(addr, end);
  98. if (pmd_none(pmd))
  99. return 0;
  100. if (unlikely(pmd_large(pmd))) {
  101. /*
  102. * NUMA hinting faults need to be handled in the GUP
  103. * slowpath for accounting purposes and so that they
  104. * can be serialised against THP migration.
  105. */
  106. if (pmd_protnone(pmd))
  107. return 0;
  108. if (!gup_huge_pmd(pmdp, pmd, addr, next,
  109. write, pages, nr))
  110. return 0;
  111. } else if (!gup_pte_range(pmdp, pmd, addr, next,
  112. write, pages, nr))
  113. return 0;
  114. } while (pmdp++, addr = next, addr != end);
  115. return 1;
  116. }
  117. static int gup_huge_pud(pud_t *pudp, pud_t pud, unsigned long addr,
  118. unsigned long end, int write, struct page **pages, int *nr)
  119. {
  120. struct page *head, *page;
  121. unsigned long mask;
  122. int refs;
  123. mask = (write ? _REGION_ENTRY_PROTECT : 0) | _REGION_ENTRY_INVALID;
  124. if ((pud_val(pud) & mask) != 0)
  125. return 0;
  126. VM_BUG_ON(!pfn_valid(pud_pfn(pud)));
  127. refs = 0;
  128. head = pud_page(pud);
  129. page = head + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
  130. do {
  131. VM_BUG_ON_PAGE(compound_head(page) != head, page);
  132. pages[*nr] = page;
  133. (*nr)++;
  134. page++;
  135. refs++;
  136. } while (addr += PAGE_SIZE, addr != end);
  137. if (unlikely(WARN_ON_ONCE(page_ref_count(head) < 0)
  138. || !page_cache_add_speculative(head, refs))) {
  139. *nr -= refs;
  140. return 0;
  141. }
  142. if (unlikely(pud_val(pud) != pud_val(*pudp))) {
  143. *nr -= refs;
  144. while (refs--)
  145. put_page(head);
  146. return 0;
  147. }
  148. return 1;
  149. }
  150. static inline int gup_pud_range(p4d_t *p4dp, p4d_t p4d, unsigned long addr,
  151. unsigned long end, int write, struct page **pages, int *nr)
  152. {
  153. unsigned long next;
  154. pud_t *pudp, pud;
  155. pudp = (pud_t *) p4dp;
  156. if ((p4d_val(p4d) & _REGION_ENTRY_TYPE_MASK) == _REGION_ENTRY_TYPE_R2)
  157. pudp = (pud_t *) p4d_deref(p4d);
  158. pudp += pud_index(addr);
  159. do {
  160. pud = *pudp;
  161. barrier();
  162. next = pud_addr_end(addr, end);
  163. if (pud_none(pud))
  164. return 0;
  165. if (unlikely(pud_large(pud))) {
  166. if (!gup_huge_pud(pudp, pud, addr, next, write, pages,
  167. nr))
  168. return 0;
  169. } else if (!gup_pmd_range(pudp, pud, addr, next, write, pages,
  170. nr))
  171. return 0;
  172. } while (pudp++, addr = next, addr != end);
  173. return 1;
  174. }
  175. static inline int gup_p4d_range(pgd_t *pgdp, pgd_t pgd, unsigned long addr,
  176. unsigned long end, int write, struct page **pages, int *nr)
  177. {
  178. unsigned long next;
  179. p4d_t *p4dp, p4d;
  180. p4dp = (p4d_t *) pgdp;
  181. if ((pgd_val(pgd) & _REGION_ENTRY_TYPE_MASK) == _REGION_ENTRY_TYPE_R1)
  182. p4dp = (p4d_t *) pgd_deref(pgd);
  183. p4dp += p4d_index(addr);
  184. do {
  185. p4d = *p4dp;
  186. barrier();
  187. next = p4d_addr_end(addr, end);
  188. if (p4d_none(p4d))
  189. return 0;
  190. if (!gup_pud_range(p4dp, p4d, addr, next, write, pages, nr))
  191. return 0;
  192. } while (p4dp++, addr = next, addr != end);
  193. return 1;
  194. }
  195. /*
  196. * Like get_user_pages_fast() except its IRQ-safe in that it won't fall
  197. * back to the regular GUP.
  198. * Note a difference with get_user_pages_fast: this always returns the
  199. * number of pages pinned, 0 if no pages were pinned.
  200. */
  201. int __get_user_pages_fast(unsigned long start, int nr_pages, int write,
  202. struct page **pages)
  203. {
  204. struct mm_struct *mm = current->mm;
  205. unsigned long addr, len, end;
  206. unsigned long next, flags;
  207. pgd_t *pgdp, pgd;
  208. int nr = 0;
  209. start &= PAGE_MASK;
  210. addr = start;
  211. len = (unsigned long) nr_pages << PAGE_SHIFT;
  212. end = start + len;
  213. if ((end <= start) || (end > mm->context.asce_limit))
  214. return 0;
  215. /*
  216. * local_irq_save() doesn't prevent pagetable teardown, but does
  217. * prevent the pagetables from being freed on s390.
  218. *
  219. * So long as we atomically load page table pointers versus teardown,
  220. * we can follow the address down to the the page and take a ref on it.
  221. */
  222. local_irq_save(flags);
  223. pgdp = pgd_offset(mm, addr);
  224. do {
  225. pgd = *pgdp;
  226. barrier();
  227. next = pgd_addr_end(addr, end);
  228. if (pgd_none(pgd))
  229. break;
  230. if (!gup_p4d_range(pgdp, pgd, addr, next, write, pages, &nr))
  231. break;
  232. } while (pgdp++, addr = next, addr != end);
  233. local_irq_restore(flags);
  234. return nr;
  235. }
  236. /**
  237. * get_user_pages_fast() - pin user pages in memory
  238. * @start: starting user address
  239. * @nr_pages: number of pages from start to pin
  240. * @write: whether pages will be written to
  241. * @pages: array that receives pointers to the pages pinned.
  242. * Should be at least nr_pages long.
  243. *
  244. * Attempt to pin user pages in memory without taking mm->mmap_sem.
  245. * If not successful, it will fall back to taking the lock and
  246. * calling get_user_pages().
  247. *
  248. * Returns number of pages pinned. This may be fewer than the number
  249. * requested. If nr_pages is 0 or negative, returns 0. If no pages
  250. * were pinned, returns -errno.
  251. */
  252. int get_user_pages_fast(unsigned long start, int nr_pages, int write,
  253. struct page **pages)
  254. {
  255. int nr, ret;
  256. might_sleep();
  257. start &= PAGE_MASK;
  258. nr = __get_user_pages_fast(start, nr_pages, write, pages);
  259. if (nr == nr_pages)
  260. return nr;
  261. /* Try to get the remaining pages with get_user_pages */
  262. start += nr << PAGE_SHIFT;
  263. pages += nr;
  264. ret = get_user_pages_unlocked(start, nr_pages - nr, pages,
  265. write ? FOLL_WRITE : 0);
  266. /* Have to be a bit careful with return values */
  267. if (nr > 0)
  268. ret = (ret < 0) ? nr : ret + nr;
  269. return ret;
  270. }