gup.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /*
  2. * Lockless get_user_pages_fast for x86
  3. *
  4. * Copyright (C) 2008 Nick Piggin
  5. * Copyright (C) 2008 Novell Inc.
  6. */
  7. #include <linux/sched.h>
  8. #include <linux/mm.h>
  9. #include <linux/vmstat.h>
  10. #include <linux/highmem.h>
  11. #include <linux/swap.h>
  12. #include <linux/memremap.h>
  13. #include <asm/mmu_context.h>
  14. #include <asm/pgtable.h>
  15. static inline pte_t gup_get_pte(pte_t *ptep)
  16. {
  17. #ifndef CONFIG_X86_PAE
  18. return READ_ONCE(*ptep);
  19. #else
  20. /*
  21. * With get_user_pages_fast, we walk down the pagetables without taking
  22. * any locks. For this we would like to load the pointers atomically,
  23. * but that is not possible (without expensive cmpxchg8b) on PAE. What
  24. * we do have is the guarantee that a pte will only either go from not
  25. * present to present, or present to not present or both -- it will not
  26. * switch to a completely different present page without a TLB flush in
  27. * between; something that we are blocking by holding interrupts off.
  28. *
  29. * Setting ptes from not present to present goes:
  30. * ptep->pte_high = h;
  31. * smp_wmb();
  32. * ptep->pte_low = l;
  33. *
  34. * And present to not present goes:
  35. * ptep->pte_low = 0;
  36. * smp_wmb();
  37. * ptep->pte_high = 0;
  38. *
  39. * We must ensure here that the load of pte_low sees l iff pte_high
  40. * sees h. We load pte_high *after* loading pte_low, which ensures we
  41. * don't see an older value of pte_high. *Then* we recheck pte_low,
  42. * which ensures that we haven't picked up a changed pte high. We might
  43. * have got rubbish values from pte_low and pte_high, but we are
  44. * guaranteed that pte_low will not have the present bit set *unless*
  45. * it is 'l'. And get_user_pages_fast only operates on present ptes, so
  46. * we're safe.
  47. *
  48. * gup_get_pte should not be used or copied outside gup.c without being
  49. * very careful -- it does not atomically load the pte or anything that
  50. * is likely to be useful for you.
  51. */
  52. pte_t pte;
  53. retry:
  54. pte.pte_low = ptep->pte_low;
  55. smp_rmb();
  56. pte.pte_high = ptep->pte_high;
  57. smp_rmb();
  58. if (unlikely(pte.pte_low != ptep->pte_low))
  59. goto retry;
  60. return pte;
  61. #endif
  62. }
  63. static void undo_dev_pagemap(int *nr, int nr_start, struct page **pages)
  64. {
  65. while ((*nr) - nr_start) {
  66. struct page *page = pages[--(*nr)];
  67. ClearPageReferenced(page);
  68. put_page(page);
  69. }
  70. }
  71. /*
  72. * 'pteval' can come from a pte, pmd, pud or p4d. We only check
  73. * _PAGE_PRESENT, _PAGE_USER, and _PAGE_RW in here which are the
  74. * same value on all 4 types.
  75. */
  76. static inline int pte_allows_gup(unsigned long pteval, int write)
  77. {
  78. unsigned long need_pte_bits = _PAGE_PRESENT|_PAGE_USER;
  79. if (write)
  80. need_pte_bits |= _PAGE_RW;
  81. if ((pteval & need_pte_bits) != need_pte_bits)
  82. return 0;
  83. /* Check memory protection keys permissions. */
  84. if (!__pkru_allows_pkey(pte_flags_pkey(pteval), write))
  85. return 0;
  86. return 1;
  87. }
  88. /*
  89. * The performance critical leaf functions are made noinline otherwise gcc
  90. * inlines everything into a single function which results in too much
  91. * register pressure.
  92. */
  93. static noinline int gup_pte_range(pmd_t pmd, unsigned long addr,
  94. unsigned long end, int write, struct page **pages, int *nr)
  95. {
  96. struct dev_pagemap *pgmap = NULL;
  97. int nr_start = *nr, ret = 0;
  98. pte_t *ptep, *ptem;
  99. /*
  100. * Keep the original mapped PTE value (ptem) around since we
  101. * might increment ptep off the end of the page when finishing
  102. * our loop iteration.
  103. */
  104. ptem = ptep = pte_offset_map(&pmd, addr);
  105. do {
  106. pte_t pte = gup_get_pte(ptep);
  107. struct page *page;
  108. /* Similar to the PMD case, NUMA hinting must take slow path */
  109. if (pte_protnone(pte))
  110. break;
  111. if (!pte_allows_gup(pte_val(pte), write))
  112. break;
  113. if (pte_devmap(pte)) {
  114. pgmap = get_dev_pagemap(pte_pfn(pte), pgmap);
  115. if (unlikely(!pgmap)) {
  116. undo_dev_pagemap(nr, nr_start, pages);
  117. break;
  118. }
  119. } else if (pte_special(pte))
  120. break;
  121. VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
  122. page = pte_page(pte);
  123. get_page(page);
  124. put_dev_pagemap(pgmap);
  125. SetPageReferenced(page);
  126. pages[*nr] = page;
  127. (*nr)++;
  128. } while (ptep++, addr += PAGE_SIZE, addr != end);
  129. if (addr == end)
  130. ret = 1;
  131. pte_unmap(ptem);
  132. return ret;
  133. }
  134. static inline void get_head_page_multiple(struct page *page, int nr)
  135. {
  136. VM_BUG_ON_PAGE(page != compound_head(page), page);
  137. VM_BUG_ON_PAGE(page_count(page) == 0, page);
  138. page_ref_add(page, nr);
  139. SetPageReferenced(page);
  140. }
  141. static int __gup_device_huge(unsigned long pfn, unsigned long addr,
  142. unsigned long end, struct page **pages, int *nr)
  143. {
  144. int nr_start = *nr;
  145. struct dev_pagemap *pgmap = NULL;
  146. do {
  147. struct page *page = pfn_to_page(pfn);
  148. pgmap = get_dev_pagemap(pfn, pgmap);
  149. if (unlikely(!pgmap)) {
  150. undo_dev_pagemap(nr, nr_start, pages);
  151. return 0;
  152. }
  153. SetPageReferenced(page);
  154. pages[*nr] = page;
  155. get_page(page);
  156. put_dev_pagemap(pgmap);
  157. (*nr)++;
  158. pfn++;
  159. } while (addr += PAGE_SIZE, addr != end);
  160. return 1;
  161. }
  162. static int __gup_device_huge_pmd(pmd_t pmd, unsigned long addr,
  163. unsigned long end, struct page **pages, int *nr)
  164. {
  165. unsigned long fault_pfn;
  166. fault_pfn = pmd_pfn(pmd) + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
  167. return __gup_device_huge(fault_pfn, addr, end, pages, nr);
  168. }
  169. static int __gup_device_huge_pud(pud_t pud, unsigned long addr,
  170. unsigned long end, struct page **pages, int *nr)
  171. {
  172. unsigned long fault_pfn;
  173. fault_pfn = pud_pfn(pud) + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
  174. return __gup_device_huge(fault_pfn, addr, end, pages, nr);
  175. }
  176. static noinline int gup_huge_pmd(pmd_t pmd, unsigned long addr,
  177. unsigned long end, int write, struct page **pages, int *nr)
  178. {
  179. struct page *head, *page;
  180. int refs;
  181. if (!pte_allows_gup(pmd_val(pmd), write))
  182. return 0;
  183. VM_BUG_ON(!pfn_valid(pmd_pfn(pmd)));
  184. if (pmd_devmap(pmd))
  185. return __gup_device_huge_pmd(pmd, addr, end, pages, nr);
  186. /* hugepages are never "special" */
  187. VM_BUG_ON(pmd_flags(pmd) & _PAGE_SPECIAL);
  188. refs = 0;
  189. head = pmd_page(pmd);
  190. page = head + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
  191. do {
  192. VM_BUG_ON_PAGE(compound_head(page) != head, page);
  193. pages[*nr] = page;
  194. (*nr)++;
  195. page++;
  196. refs++;
  197. } while (addr += PAGE_SIZE, addr != end);
  198. get_head_page_multiple(head, refs);
  199. return 1;
  200. }
  201. static int gup_pmd_range(pud_t pud, unsigned long addr, unsigned long end,
  202. int write, struct page **pages, int *nr)
  203. {
  204. unsigned long next;
  205. pmd_t *pmdp;
  206. pmdp = pmd_offset(&pud, addr);
  207. do {
  208. pmd_t pmd = *pmdp;
  209. next = pmd_addr_end(addr, end);
  210. if (pmd_none(pmd))
  211. return 0;
  212. if (unlikely(pmd_large(pmd) || !pmd_present(pmd))) {
  213. /*
  214. * NUMA hinting faults need to be handled in the GUP
  215. * slowpath for accounting purposes and so that they
  216. * can be serialised against THP migration.
  217. */
  218. if (pmd_protnone(pmd))
  219. return 0;
  220. if (!gup_huge_pmd(pmd, addr, next, write, pages, nr))
  221. return 0;
  222. } else {
  223. if (!gup_pte_range(pmd, addr, next, write, pages, nr))
  224. return 0;
  225. }
  226. } while (pmdp++, addr = next, addr != end);
  227. return 1;
  228. }
  229. static noinline int gup_huge_pud(pud_t pud, unsigned long addr,
  230. unsigned long end, int write, struct page **pages, int *nr)
  231. {
  232. struct page *head, *page;
  233. int refs;
  234. if (!pte_allows_gup(pud_val(pud), write))
  235. return 0;
  236. VM_BUG_ON(!pfn_valid(pud_pfn(pud)));
  237. if (pud_devmap(pud))
  238. return __gup_device_huge_pud(pud, addr, end, pages, nr);
  239. /* hugepages are never "special" */
  240. VM_BUG_ON(pud_flags(pud) & _PAGE_SPECIAL);
  241. refs = 0;
  242. head = pud_page(pud);
  243. page = head + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
  244. do {
  245. VM_BUG_ON_PAGE(compound_head(page) != head, page);
  246. pages[*nr] = page;
  247. (*nr)++;
  248. page++;
  249. refs++;
  250. } while (addr += PAGE_SIZE, addr != end);
  251. get_head_page_multiple(head, refs);
  252. return 1;
  253. }
  254. static int gup_pud_range(p4d_t p4d, unsigned long addr, unsigned long end,
  255. int write, struct page **pages, int *nr)
  256. {
  257. unsigned long next;
  258. pud_t *pudp;
  259. pudp = pud_offset(&p4d, addr);
  260. do {
  261. pud_t pud = *pudp;
  262. next = pud_addr_end(addr, end);
  263. if (pud_none(pud))
  264. return 0;
  265. if (unlikely(pud_large(pud))) {
  266. if (!gup_huge_pud(pud, addr, next, write, pages, nr))
  267. return 0;
  268. } else {
  269. if (!gup_pmd_range(pud, addr, next, write, pages, nr))
  270. return 0;
  271. }
  272. } while (pudp++, addr = next, addr != end);
  273. return 1;
  274. }
  275. static int gup_p4d_range(pgd_t pgd, unsigned long addr, unsigned long end,
  276. int write, struct page **pages, int *nr)
  277. {
  278. unsigned long next;
  279. p4d_t *p4dp;
  280. p4dp = p4d_offset(&pgd, addr);
  281. do {
  282. p4d_t p4d = *p4dp;
  283. next = p4d_addr_end(addr, end);
  284. if (p4d_none(p4d))
  285. return 0;
  286. BUILD_BUG_ON(p4d_large(p4d));
  287. if (!gup_pud_range(p4d, addr, next, write, pages, nr))
  288. return 0;
  289. } while (p4dp++, addr = next, addr != end);
  290. return 1;
  291. }
  292. /*
  293. * Like get_user_pages_fast() except its IRQ-safe in that it won't fall
  294. * back to the regular GUP.
  295. */
  296. int __get_user_pages_fast(unsigned long start, int nr_pages, int write,
  297. struct page **pages)
  298. {
  299. struct mm_struct *mm = current->mm;
  300. unsigned long addr, len, end;
  301. unsigned long next;
  302. unsigned long flags;
  303. pgd_t *pgdp;
  304. int nr = 0;
  305. start &= PAGE_MASK;
  306. addr = start;
  307. len = (unsigned long) nr_pages << PAGE_SHIFT;
  308. end = start + len;
  309. if (unlikely(!access_ok(write ? VERIFY_WRITE : VERIFY_READ,
  310. (void __user *)start, len)))
  311. return 0;
  312. /*
  313. * XXX: batch / limit 'nr', to avoid large irq off latency
  314. * needs some instrumenting to determine the common sizes used by
  315. * important workloads (eg. DB2), and whether limiting the batch size
  316. * will decrease performance.
  317. *
  318. * It seems like we're in the clear for the moment. Direct-IO is
  319. * the main guy that batches up lots of get_user_pages, and even
  320. * they are limited to 64-at-a-time which is not so many.
  321. */
  322. /*
  323. * This doesn't prevent pagetable teardown, but does prevent
  324. * the pagetables and pages from being freed on x86.
  325. *
  326. * So long as we atomically load page table pointers versus teardown
  327. * (which we do on x86, with the above PAE exception), we can follow the
  328. * address down to the the page and take a ref on it.
  329. */
  330. local_irq_save(flags);
  331. pgdp = pgd_offset(mm, addr);
  332. do {
  333. pgd_t pgd = *pgdp;
  334. next = pgd_addr_end(addr, end);
  335. if (pgd_none(pgd))
  336. break;
  337. if (!gup_p4d_range(pgd, addr, next, write, pages, &nr))
  338. break;
  339. } while (pgdp++, addr = next, addr != end);
  340. local_irq_restore(flags);
  341. return nr;
  342. }
  343. /**
  344. * get_user_pages_fast() - pin user pages in memory
  345. * @start: starting user address
  346. * @nr_pages: number of pages from start to pin
  347. * @write: whether pages will be written to
  348. * @pages: array that receives pointers to the pages pinned.
  349. * Should be at least nr_pages long.
  350. *
  351. * Attempt to pin user pages in memory without taking mm->mmap_sem.
  352. * If not successful, it will fall back to taking the lock and
  353. * calling get_user_pages().
  354. *
  355. * Returns number of pages pinned. This may be fewer than the number
  356. * requested. If nr_pages is 0 or negative, returns 0. If no pages
  357. * were pinned, returns -errno.
  358. */
  359. int get_user_pages_fast(unsigned long start, int nr_pages, int write,
  360. struct page **pages)
  361. {
  362. struct mm_struct *mm = current->mm;
  363. unsigned long addr, len, end;
  364. unsigned long next;
  365. pgd_t *pgdp;
  366. int nr = 0;
  367. start &= PAGE_MASK;
  368. addr = start;
  369. len = (unsigned long) nr_pages << PAGE_SHIFT;
  370. end = start + len;
  371. if (end < start)
  372. goto slow_irqon;
  373. #ifdef CONFIG_X86_64
  374. if (end >> __VIRTUAL_MASK_SHIFT)
  375. goto slow_irqon;
  376. #endif
  377. /*
  378. * XXX: batch / limit 'nr', to avoid large irq off latency
  379. * needs some instrumenting to determine the common sizes used by
  380. * important workloads (eg. DB2), and whether limiting the batch size
  381. * will decrease performance.
  382. *
  383. * It seems like we're in the clear for the moment. Direct-IO is
  384. * the main guy that batches up lots of get_user_pages, and even
  385. * they are limited to 64-at-a-time which is not so many.
  386. */
  387. /*
  388. * This doesn't prevent pagetable teardown, but does prevent
  389. * the pagetables and pages from being freed on x86.
  390. *
  391. * So long as we atomically load page table pointers versus teardown
  392. * (which we do on x86, with the above PAE exception), we can follow the
  393. * address down to the the page and take a ref on it.
  394. */
  395. local_irq_disable();
  396. pgdp = pgd_offset(mm, addr);
  397. do {
  398. pgd_t pgd = *pgdp;
  399. next = pgd_addr_end(addr, end);
  400. if (pgd_none(pgd))
  401. goto slow;
  402. if (!gup_p4d_range(pgd, addr, next, write, pages, &nr))
  403. goto slow;
  404. } while (pgdp++, addr = next, addr != end);
  405. local_irq_enable();
  406. VM_BUG_ON(nr != (end - start) >> PAGE_SHIFT);
  407. return nr;
  408. {
  409. int ret;
  410. slow:
  411. local_irq_enable();
  412. slow_irqon:
  413. /* Try to get the remaining pages with get_user_pages */
  414. start += nr << PAGE_SHIFT;
  415. pages += nr;
  416. ret = get_user_pages_unlocked(start,
  417. (end - start) >> PAGE_SHIFT,
  418. pages, write ? FOLL_WRITE : 0);
  419. /* Have to be a bit careful with return values */
  420. if (nr > 0) {
  421. if (ret < 0)
  422. ret = nr;
  423. else
  424. ret += nr;
  425. }
  426. return ret;
  427. }
  428. }