gup.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Lockless get_user_pages_fast for sparc, cribbed from powerpc
  4. *
  5. * Copyright (C) 2008 Nick Piggin
  6. * Copyright (C) 2008 Novell Inc.
  7. */
  8. #include <linux/sched.h>
  9. #include <linux/mm.h>
  10. #include <linux/vmstat.h>
  11. #include <linux/pagemap.h>
  12. #include <linux/rwsem.h>
  13. #include <asm/pgtable.h>
  14. #include <asm/adi.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 noinline int gup_pte_range(pmd_t pmd, unsigned long addr,
  21. unsigned long end, int write, struct page **pages, int *nr)
  22. {
  23. unsigned long mask, result;
  24. pte_t *ptep;
  25. if (tlb_type == hypervisor) {
  26. result = _PAGE_PRESENT_4V|_PAGE_P_4V;
  27. if (write)
  28. result |= _PAGE_WRITE_4V;
  29. } else {
  30. result = _PAGE_PRESENT_4U|_PAGE_P_4U;
  31. if (write)
  32. result |= _PAGE_WRITE_4U;
  33. }
  34. mask = result | _PAGE_SPECIAL;
  35. ptep = pte_offset_kernel(&pmd, addr);
  36. do {
  37. struct page *page, *head;
  38. pte_t pte = *ptep;
  39. if ((pte_val(pte) & mask) != result)
  40. return 0;
  41. VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
  42. /* The hugepage case is simplified on sparc64 because
  43. * we encode the sub-page pfn offsets into the
  44. * hugepage PTEs. We could optimize this in the future
  45. * use page_cache_add_speculative() for the hugepage case.
  46. */
  47. page = pte_page(pte);
  48. head = compound_head(page);
  49. if (!page_cache_get_speculative(head))
  50. return 0;
  51. if (unlikely(pte_val(pte) != pte_val(*ptep))) {
  52. put_page(head);
  53. return 0;
  54. }
  55. pages[*nr] = page;
  56. (*nr)++;
  57. } while (ptep++, addr += PAGE_SIZE, addr != end);
  58. return 1;
  59. }
  60. static int gup_huge_pmd(pmd_t *pmdp, pmd_t pmd, unsigned long addr,
  61. unsigned long end, int write, struct page **pages,
  62. int *nr)
  63. {
  64. struct page *head, *page;
  65. int refs;
  66. if (!(pmd_val(pmd) & _PAGE_VALID))
  67. return 0;
  68. if (write && !pmd_write(pmd))
  69. return 0;
  70. refs = 0;
  71. page = pmd_page(pmd) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
  72. head = compound_head(page);
  73. do {
  74. VM_BUG_ON(compound_head(page) != head);
  75. pages[*nr] = page;
  76. (*nr)++;
  77. page++;
  78. refs++;
  79. } while (addr += PAGE_SIZE, addr != end);
  80. if (!page_cache_add_speculative(head, refs)) {
  81. *nr -= refs;
  82. return 0;
  83. }
  84. if (unlikely(pmd_val(pmd) != pmd_val(*pmdp))) {
  85. *nr -= refs;
  86. while (refs--)
  87. put_page(head);
  88. return 0;
  89. }
  90. return 1;
  91. }
  92. static int gup_huge_pud(pud_t *pudp, pud_t pud, unsigned long addr,
  93. unsigned long end, int write, struct page **pages,
  94. int *nr)
  95. {
  96. struct page *head, *page;
  97. int refs;
  98. if (!(pud_val(pud) & _PAGE_VALID))
  99. return 0;
  100. if (write && !pud_write(pud))
  101. return 0;
  102. refs = 0;
  103. page = pud_page(pud) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
  104. head = compound_head(page);
  105. do {
  106. VM_BUG_ON(compound_head(page) != head);
  107. pages[*nr] = page;
  108. (*nr)++;
  109. page++;
  110. refs++;
  111. } while (addr += PAGE_SIZE, addr != end);
  112. if (!page_cache_add_speculative(head, refs)) {
  113. *nr -= refs;
  114. return 0;
  115. }
  116. if (unlikely(pud_val(pud) != pud_val(*pudp))) {
  117. *nr -= refs;
  118. while (refs--)
  119. put_page(head);
  120. return 0;
  121. }
  122. return 1;
  123. }
  124. static int gup_pmd_range(pud_t pud, unsigned long addr, unsigned long end,
  125. int write, struct page **pages, int *nr)
  126. {
  127. unsigned long next;
  128. pmd_t *pmdp;
  129. pmdp = pmd_offset(&pud, addr);
  130. do {
  131. pmd_t pmd = *pmdp;
  132. next = pmd_addr_end(addr, end);
  133. if (pmd_none(pmd))
  134. return 0;
  135. if (unlikely(pmd_large(pmd))) {
  136. if (!gup_huge_pmd(pmdp, pmd, addr, next,
  137. write, pages, nr))
  138. return 0;
  139. } else if (!gup_pte_range(pmd, addr, next, write,
  140. pages, nr))
  141. return 0;
  142. } while (pmdp++, addr = next, addr != end);
  143. return 1;
  144. }
  145. static int gup_pud_range(pgd_t pgd, unsigned long addr, unsigned long end,
  146. int write, struct page **pages, int *nr)
  147. {
  148. unsigned long next;
  149. pud_t *pudp;
  150. pudp = pud_offset(&pgd, addr);
  151. do {
  152. pud_t pud = *pudp;
  153. next = pud_addr_end(addr, end);
  154. if (pud_none(pud))
  155. return 0;
  156. if (unlikely(pud_large(pud))) {
  157. if (!gup_huge_pud(pudp, pud, addr, next,
  158. write, pages, nr))
  159. return 0;
  160. } else if (!gup_pmd_range(pud, addr, next, write, pages, nr))
  161. return 0;
  162. } while (pudp++, addr = next, addr != end);
  163. return 1;
  164. }
  165. /*
  166. * Note a difference with get_user_pages_fast: this always returns the
  167. * number of pages pinned, 0 if no pages were pinned.
  168. */
  169. int __get_user_pages_fast(unsigned long start, int nr_pages, int write,
  170. struct page **pages)
  171. {
  172. struct mm_struct *mm = current->mm;
  173. unsigned long addr, len, end;
  174. unsigned long next, flags;
  175. pgd_t *pgdp;
  176. int nr = 0;
  177. #ifdef CONFIG_SPARC64
  178. if (adi_capable()) {
  179. long addr = start;
  180. /* If userspace has passed a versioned address, kernel
  181. * will not find it in the VMAs since it does not store
  182. * the version tags in the list of VMAs. Storing version
  183. * tags in list of VMAs is impractical since they can be
  184. * changed any time from userspace without dropping into
  185. * kernel. Any address search in VMAs will be done with
  186. * non-versioned addresses. Ensure the ADI version bits
  187. * are dropped here by sign extending the last bit before
  188. * ADI bits. IOMMU does not implement version tags.
  189. */
  190. addr = (addr << (long)adi_nbits()) >> (long)adi_nbits();
  191. start = addr;
  192. }
  193. #endif
  194. start &= PAGE_MASK;
  195. addr = start;
  196. len = (unsigned long) nr_pages << PAGE_SHIFT;
  197. end = start + len;
  198. local_irq_save(flags);
  199. pgdp = pgd_offset(mm, addr);
  200. do {
  201. pgd_t pgd = *pgdp;
  202. next = pgd_addr_end(addr, end);
  203. if (pgd_none(pgd))
  204. break;
  205. if (!gup_pud_range(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. int get_user_pages_fast(unsigned long start, int nr_pages, int write,
  212. struct page **pages)
  213. {
  214. struct mm_struct *mm = current->mm;
  215. unsigned long addr, len, end;
  216. unsigned long next;
  217. pgd_t *pgdp;
  218. int nr = 0;
  219. #ifdef CONFIG_SPARC64
  220. if (adi_capable()) {
  221. long addr = start;
  222. /* If userspace has passed a versioned address, kernel
  223. * will not find it in the VMAs since it does not store
  224. * the version tags in the list of VMAs. Storing version
  225. * tags in list of VMAs is impractical since they can be
  226. * changed any time from userspace without dropping into
  227. * kernel. Any address search in VMAs will be done with
  228. * non-versioned addresses. Ensure the ADI version bits
  229. * are dropped here by sign extending the last bit before
  230. * ADI bits. IOMMU does not implements version tags,
  231. */
  232. addr = (addr << (long)adi_nbits()) >> (long)adi_nbits();
  233. start = addr;
  234. }
  235. #endif
  236. start &= PAGE_MASK;
  237. addr = start;
  238. len = (unsigned long) nr_pages << PAGE_SHIFT;
  239. end = start + len;
  240. /*
  241. * XXX: batch / limit 'nr', to avoid large irq off latency
  242. * needs some instrumenting to determine the common sizes used by
  243. * important workloads (eg. DB2), and whether limiting the batch size
  244. * will decrease performance.
  245. *
  246. * It seems like we're in the clear for the moment. Direct-IO is
  247. * the main guy that batches up lots of get_user_pages, and even
  248. * they are limited to 64-at-a-time which is not so many.
  249. */
  250. /*
  251. * This doesn't prevent pagetable teardown, but does prevent
  252. * the pagetables from being freed on sparc.
  253. *
  254. * So long as we atomically load page table pointers versus teardown,
  255. * we can follow the address down to the the page and take a ref on it.
  256. */
  257. local_irq_disable();
  258. pgdp = pgd_offset(mm, addr);
  259. do {
  260. pgd_t pgd = *pgdp;
  261. next = pgd_addr_end(addr, end);
  262. if (pgd_none(pgd))
  263. goto slow;
  264. if (!gup_pud_range(pgd, addr, next, write, pages, &nr))
  265. goto slow;
  266. } while (pgdp++, addr = next, addr != end);
  267. local_irq_enable();
  268. VM_BUG_ON(nr != (end - start) >> PAGE_SHIFT);
  269. return nr;
  270. {
  271. int ret;
  272. slow:
  273. local_irq_enable();
  274. /* Try to get the remaining pages with get_user_pages */
  275. start += nr << PAGE_SHIFT;
  276. pages += nr;
  277. ret = get_user_pages_unlocked(start,
  278. (end - start) >> PAGE_SHIFT, pages,
  279. write ? FOLL_WRITE : 0);
  280. /* Have to be a bit careful with return values */
  281. if (nr > 0) {
  282. if (ret < 0)
  283. ret = nr;
  284. else
  285. ret += nr;
  286. }
  287. return ret;
  288. }
  289. }