gup.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380
  1. #include <linux/kernel.h>
  2. #include <linux/errno.h>
  3. #include <linux/err.h>
  4. #include <linux/spinlock.h>
  5. #include <linux/mm.h>
  6. #include <linux/pagemap.h>
  7. #include <linux/rmap.h>
  8. #include <linux/swap.h>
  9. #include <linux/swapops.h>
  10. #include <linux/sched.h>
  11. #include <linux/rwsem.h>
  12. #include <linux/hugetlb.h>
  13. #include <asm/pgtable.h>
  14. #include "internal.h"
  15. static struct page *no_page_table(struct vm_area_struct *vma,
  16. unsigned int flags)
  17. {
  18. /*
  19. * When core dumping an enormous anonymous area that nobody
  20. * has touched so far, we don't want to allocate unnecessary pages or
  21. * page tables. Return error instead of NULL to skip handle_mm_fault,
  22. * then get_dump_page() will return NULL to leave a hole in the dump.
  23. * But we can only make this optimization where a hole would surely
  24. * be zero-filled if handle_mm_fault() actually did handle it.
  25. */
  26. if ((flags & FOLL_DUMP) && (!vma->vm_ops || !vma->vm_ops->fault))
  27. return ERR_PTR(-EFAULT);
  28. return NULL;
  29. }
  30. static struct page *follow_page_pte(struct vm_area_struct *vma,
  31. unsigned long address, pmd_t *pmd, unsigned int flags)
  32. {
  33. struct mm_struct *mm = vma->vm_mm;
  34. struct page *page;
  35. spinlock_t *ptl;
  36. pte_t *ptep, pte;
  37. retry:
  38. if (unlikely(pmd_bad(*pmd)))
  39. return no_page_table(vma, flags);
  40. ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
  41. pte = *ptep;
  42. if (!pte_present(pte)) {
  43. swp_entry_t entry;
  44. /*
  45. * KSM's break_ksm() relies upon recognizing a ksm page
  46. * even while it is being migrated, so for that case we
  47. * need migration_entry_wait().
  48. */
  49. if (likely(!(flags & FOLL_MIGRATION)))
  50. goto no_page;
  51. if (pte_none(pte))
  52. goto no_page;
  53. entry = pte_to_swp_entry(pte);
  54. if (!is_migration_entry(entry))
  55. goto no_page;
  56. pte_unmap_unlock(ptep, ptl);
  57. migration_entry_wait(mm, pmd, address);
  58. goto retry;
  59. }
  60. if ((flags & FOLL_NUMA) && pte_protnone(pte))
  61. goto no_page;
  62. if ((flags & FOLL_WRITE) && !pte_write(pte)) {
  63. pte_unmap_unlock(ptep, ptl);
  64. return NULL;
  65. }
  66. page = vm_normal_page(vma, address, pte);
  67. if (unlikely(!page)) {
  68. if ((flags & FOLL_DUMP) ||
  69. !is_zero_pfn(pte_pfn(pte)))
  70. goto bad_page;
  71. page = pte_page(pte);
  72. }
  73. if (flags & FOLL_GET)
  74. get_page_foll(page);
  75. if (flags & FOLL_TOUCH) {
  76. if ((flags & FOLL_WRITE) &&
  77. !pte_dirty(pte) && !PageDirty(page))
  78. set_page_dirty(page);
  79. /*
  80. * pte_mkyoung() would be more correct here, but atomic care
  81. * is needed to avoid losing the dirty bit: it is easier to use
  82. * mark_page_accessed().
  83. */
  84. mark_page_accessed(page);
  85. }
  86. if ((flags & FOLL_POPULATE) && (vma->vm_flags & VM_LOCKED)) {
  87. /*
  88. * The preliminary mapping check is mainly to avoid the
  89. * pointless overhead of lock_page on the ZERO_PAGE
  90. * which might bounce very badly if there is contention.
  91. *
  92. * If the page is already locked, we don't need to
  93. * handle it now - vmscan will handle it later if and
  94. * when it attempts to reclaim the page.
  95. */
  96. if (page->mapping && trylock_page(page)) {
  97. lru_add_drain(); /* push cached pages to LRU */
  98. /*
  99. * Because we lock page here, and migration is
  100. * blocked by the pte's page reference, and we
  101. * know the page is still mapped, we don't even
  102. * need to check for file-cache page truncation.
  103. */
  104. mlock_vma_page(page);
  105. unlock_page(page);
  106. }
  107. }
  108. pte_unmap_unlock(ptep, ptl);
  109. return page;
  110. bad_page:
  111. pte_unmap_unlock(ptep, ptl);
  112. return ERR_PTR(-EFAULT);
  113. no_page:
  114. pte_unmap_unlock(ptep, ptl);
  115. if (!pte_none(pte))
  116. return NULL;
  117. return no_page_table(vma, flags);
  118. }
  119. /**
  120. * follow_page_mask - look up a page descriptor from a user-virtual address
  121. * @vma: vm_area_struct mapping @address
  122. * @address: virtual address to look up
  123. * @flags: flags modifying lookup behaviour
  124. * @page_mask: on output, *page_mask is set according to the size of the page
  125. *
  126. * @flags can have FOLL_ flags set, defined in <linux/mm.h>
  127. *
  128. * Returns the mapped (struct page *), %NULL if no mapping exists, or
  129. * an error pointer if there is a mapping to something not represented
  130. * by a page descriptor (see also vm_normal_page()).
  131. */
  132. struct page *follow_page_mask(struct vm_area_struct *vma,
  133. unsigned long address, unsigned int flags,
  134. unsigned int *page_mask)
  135. {
  136. pgd_t *pgd;
  137. pud_t *pud;
  138. pmd_t *pmd;
  139. spinlock_t *ptl;
  140. struct page *page;
  141. struct mm_struct *mm = vma->vm_mm;
  142. *page_mask = 0;
  143. page = follow_huge_addr(mm, address, flags & FOLL_WRITE);
  144. if (!IS_ERR(page)) {
  145. BUG_ON(flags & FOLL_GET);
  146. return page;
  147. }
  148. pgd = pgd_offset(mm, address);
  149. if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
  150. return no_page_table(vma, flags);
  151. pud = pud_offset(pgd, address);
  152. if (pud_none(*pud))
  153. return no_page_table(vma, flags);
  154. if (pud_huge(*pud) && vma->vm_flags & VM_HUGETLB) {
  155. page = follow_huge_pud(mm, address, pud, flags);
  156. if (page)
  157. return page;
  158. return no_page_table(vma, flags);
  159. }
  160. if (unlikely(pud_bad(*pud)))
  161. return no_page_table(vma, flags);
  162. pmd = pmd_offset(pud, address);
  163. if (pmd_none(*pmd))
  164. return no_page_table(vma, flags);
  165. if (pmd_huge(*pmd) && vma->vm_flags & VM_HUGETLB) {
  166. page = follow_huge_pmd(mm, address, pmd, flags);
  167. if (page)
  168. return page;
  169. return no_page_table(vma, flags);
  170. }
  171. if ((flags & FOLL_NUMA) && pmd_protnone(*pmd))
  172. return no_page_table(vma, flags);
  173. if (pmd_trans_huge(*pmd)) {
  174. if (flags & FOLL_SPLIT) {
  175. split_huge_page_pmd(vma, address, pmd);
  176. return follow_page_pte(vma, address, pmd, flags);
  177. }
  178. ptl = pmd_lock(mm, pmd);
  179. if (likely(pmd_trans_huge(*pmd))) {
  180. if (unlikely(pmd_trans_splitting(*pmd))) {
  181. spin_unlock(ptl);
  182. wait_split_huge_page(vma->anon_vma, pmd);
  183. } else {
  184. page = follow_trans_huge_pmd(vma, address,
  185. pmd, flags);
  186. spin_unlock(ptl);
  187. *page_mask = HPAGE_PMD_NR - 1;
  188. return page;
  189. }
  190. } else
  191. spin_unlock(ptl);
  192. }
  193. return follow_page_pte(vma, address, pmd, flags);
  194. }
  195. static int get_gate_page(struct mm_struct *mm, unsigned long address,
  196. unsigned int gup_flags, struct vm_area_struct **vma,
  197. struct page **page)
  198. {
  199. pgd_t *pgd;
  200. pud_t *pud;
  201. pmd_t *pmd;
  202. pte_t *pte;
  203. int ret = -EFAULT;
  204. /* user gate pages are read-only */
  205. if (gup_flags & FOLL_WRITE)
  206. return -EFAULT;
  207. if (address > TASK_SIZE)
  208. pgd = pgd_offset_k(address);
  209. else
  210. pgd = pgd_offset_gate(mm, address);
  211. BUG_ON(pgd_none(*pgd));
  212. pud = pud_offset(pgd, address);
  213. BUG_ON(pud_none(*pud));
  214. pmd = pmd_offset(pud, address);
  215. if (pmd_none(*pmd))
  216. return -EFAULT;
  217. VM_BUG_ON(pmd_trans_huge(*pmd));
  218. pte = pte_offset_map(pmd, address);
  219. if (pte_none(*pte))
  220. goto unmap;
  221. *vma = get_gate_vma(mm);
  222. if (!page)
  223. goto out;
  224. *page = vm_normal_page(*vma, address, *pte);
  225. if (!*page) {
  226. if ((gup_flags & FOLL_DUMP) || !is_zero_pfn(pte_pfn(*pte)))
  227. goto unmap;
  228. *page = pte_page(*pte);
  229. }
  230. get_page(*page);
  231. out:
  232. ret = 0;
  233. unmap:
  234. pte_unmap(pte);
  235. return ret;
  236. }
  237. /*
  238. * mmap_sem must be held on entry. If @nonblocking != NULL and
  239. * *@flags does not include FOLL_NOWAIT, the mmap_sem may be released.
  240. * If it is, *@nonblocking will be set to 0 and -EBUSY returned.
  241. */
  242. static int faultin_page(struct task_struct *tsk, struct vm_area_struct *vma,
  243. unsigned long address, unsigned int *flags, int *nonblocking)
  244. {
  245. struct mm_struct *mm = vma->vm_mm;
  246. unsigned int fault_flags = 0;
  247. int ret;
  248. /* For mm_populate(), just skip the stack guard page. */
  249. if ((*flags & FOLL_POPULATE) &&
  250. (stack_guard_page_start(vma, address) ||
  251. stack_guard_page_end(vma, address + PAGE_SIZE)))
  252. return -ENOENT;
  253. if (*flags & FOLL_WRITE)
  254. fault_flags |= FAULT_FLAG_WRITE;
  255. if (nonblocking)
  256. fault_flags |= FAULT_FLAG_ALLOW_RETRY;
  257. if (*flags & FOLL_NOWAIT)
  258. fault_flags |= FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT;
  259. if (*flags & FOLL_TRIED) {
  260. VM_WARN_ON_ONCE(fault_flags & FAULT_FLAG_ALLOW_RETRY);
  261. fault_flags |= FAULT_FLAG_TRIED;
  262. }
  263. ret = handle_mm_fault(mm, vma, address, fault_flags);
  264. if (ret & VM_FAULT_ERROR) {
  265. if (ret & VM_FAULT_OOM)
  266. return -ENOMEM;
  267. if (ret & (VM_FAULT_HWPOISON | VM_FAULT_HWPOISON_LARGE))
  268. return *flags & FOLL_HWPOISON ? -EHWPOISON : -EFAULT;
  269. if (ret & (VM_FAULT_SIGBUS | VM_FAULT_SIGSEGV))
  270. return -EFAULT;
  271. BUG();
  272. }
  273. if (tsk) {
  274. if (ret & VM_FAULT_MAJOR)
  275. tsk->maj_flt++;
  276. else
  277. tsk->min_flt++;
  278. }
  279. if (ret & VM_FAULT_RETRY) {
  280. if (nonblocking)
  281. *nonblocking = 0;
  282. return -EBUSY;
  283. }
  284. /*
  285. * The VM_FAULT_WRITE bit tells us that do_wp_page has broken COW when
  286. * necessary, even if maybe_mkwrite decided not to set pte_write. We
  287. * can thus safely do subsequent page lookups as if they were reads.
  288. * But only do so when looping for pte_write is futile: in some cases
  289. * userspace may also be wanting to write to the gotten user page,
  290. * which a read fault here might prevent (a readonly page might get
  291. * reCOWed by userspace write).
  292. */
  293. if ((ret & VM_FAULT_WRITE) && !(vma->vm_flags & VM_WRITE))
  294. *flags &= ~FOLL_WRITE;
  295. return 0;
  296. }
  297. static int check_vma_flags(struct vm_area_struct *vma, unsigned long gup_flags)
  298. {
  299. vm_flags_t vm_flags = vma->vm_flags;
  300. if (vm_flags & (VM_IO | VM_PFNMAP))
  301. return -EFAULT;
  302. if (gup_flags & FOLL_WRITE) {
  303. if (!(vm_flags & VM_WRITE)) {
  304. if (!(gup_flags & FOLL_FORCE))
  305. return -EFAULT;
  306. /*
  307. * We used to let the write,force case do COW in a
  308. * VM_MAYWRITE VM_SHARED !VM_WRITE vma, so ptrace could
  309. * set a breakpoint in a read-only mapping of an
  310. * executable, without corrupting the file (yet only
  311. * when that file had been opened for writing!).
  312. * Anon pages in shared mappings are surprising: now
  313. * just reject it.
  314. */
  315. if (!is_cow_mapping(vm_flags)) {
  316. WARN_ON_ONCE(vm_flags & VM_MAYWRITE);
  317. return -EFAULT;
  318. }
  319. }
  320. } else if (!(vm_flags & VM_READ)) {
  321. if (!(gup_flags & FOLL_FORCE))
  322. return -EFAULT;
  323. /*
  324. * Is there actually any vma we can reach here which does not
  325. * have VM_MAYREAD set?
  326. */
  327. if (!(vm_flags & VM_MAYREAD))
  328. return -EFAULT;
  329. }
  330. return 0;
  331. }
  332. /**
  333. * __get_user_pages() - pin user pages in memory
  334. * @tsk: task_struct of target task
  335. * @mm: mm_struct of target mm
  336. * @start: starting user address
  337. * @nr_pages: number of pages from start to pin
  338. * @gup_flags: flags modifying pin behaviour
  339. * @pages: array that receives pointers to the pages pinned.
  340. * Should be at least nr_pages long. Or NULL, if caller
  341. * only intends to ensure the pages are faulted in.
  342. * @vmas: array of pointers to vmas corresponding to each page.
  343. * Or NULL if the caller does not require them.
  344. * @nonblocking: whether waiting for disk IO or mmap_sem contention
  345. *
  346. * Returns number of pages pinned. This may be fewer than the number
  347. * requested. If nr_pages is 0 or negative, returns 0. If no pages
  348. * were pinned, returns -errno. Each page returned must be released
  349. * with a put_page() call when it is finished with. vmas will only
  350. * remain valid while mmap_sem is held.
  351. *
  352. * Must be called with mmap_sem held. It may be released. See below.
  353. *
  354. * __get_user_pages walks a process's page tables and takes a reference to
  355. * each struct page that each user address corresponds to at a given
  356. * instant. That is, it takes the page that would be accessed if a user
  357. * thread accesses the given user virtual address at that instant.
  358. *
  359. * This does not guarantee that the page exists in the user mappings when
  360. * __get_user_pages returns, and there may even be a completely different
  361. * page there in some cases (eg. if mmapped pagecache has been invalidated
  362. * and subsequently re faulted). However it does guarantee that the page
  363. * won't be freed completely. And mostly callers simply care that the page
  364. * contains data that was valid *at some point in time*. Typically, an IO
  365. * or similar operation cannot guarantee anything stronger anyway because
  366. * locks can't be held over the syscall boundary.
  367. *
  368. * If @gup_flags & FOLL_WRITE == 0, the page must not be written to. If
  369. * the page is written to, set_page_dirty (or set_page_dirty_lock, as
  370. * appropriate) must be called after the page is finished with, and
  371. * before put_page is called.
  372. *
  373. * If @nonblocking != NULL, __get_user_pages will not wait for disk IO
  374. * or mmap_sem contention, and if waiting is needed to pin all pages,
  375. * *@nonblocking will be set to 0. Further, if @gup_flags does not
  376. * include FOLL_NOWAIT, the mmap_sem will be released via up_read() in
  377. * this case.
  378. *
  379. * A caller using such a combination of @nonblocking and @gup_flags
  380. * must therefore hold the mmap_sem for reading only, and recognize
  381. * when it's been released. Otherwise, it must be held for either
  382. * reading or writing and will not be released.
  383. *
  384. * In most cases, get_user_pages or get_user_pages_fast should be used
  385. * instead of __get_user_pages. __get_user_pages should be used only if
  386. * you need some special @gup_flags.
  387. */
  388. long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
  389. unsigned long start, unsigned long nr_pages,
  390. unsigned int gup_flags, struct page **pages,
  391. struct vm_area_struct **vmas, int *nonblocking)
  392. {
  393. long i = 0;
  394. unsigned int page_mask;
  395. struct vm_area_struct *vma = NULL;
  396. if (!nr_pages)
  397. return 0;
  398. VM_BUG_ON(!!pages != !!(gup_flags & FOLL_GET));
  399. /*
  400. * If FOLL_FORCE is set then do not force a full fault as the hinting
  401. * fault information is unrelated to the reference behaviour of a task
  402. * using the address space
  403. */
  404. if (!(gup_flags & FOLL_FORCE))
  405. gup_flags |= FOLL_NUMA;
  406. do {
  407. struct page *page;
  408. unsigned int foll_flags = gup_flags;
  409. unsigned int page_increm;
  410. /* first iteration or cross vma bound */
  411. if (!vma || start >= vma->vm_end) {
  412. vma = find_extend_vma(mm, start);
  413. if (!vma && in_gate_area(mm, start)) {
  414. int ret;
  415. ret = get_gate_page(mm, start & PAGE_MASK,
  416. gup_flags, &vma,
  417. pages ? &pages[i] : NULL);
  418. if (ret)
  419. return i ? : ret;
  420. page_mask = 0;
  421. goto next_page;
  422. }
  423. if (!vma || check_vma_flags(vma, gup_flags))
  424. return i ? : -EFAULT;
  425. if (is_vm_hugetlb_page(vma)) {
  426. i = follow_hugetlb_page(mm, vma, pages, vmas,
  427. &start, &nr_pages, i,
  428. gup_flags);
  429. continue;
  430. }
  431. }
  432. retry:
  433. /*
  434. * If we have a pending SIGKILL, don't keep faulting pages and
  435. * potentially allocating memory.
  436. */
  437. if (unlikely(fatal_signal_pending(current)))
  438. return i ? i : -ERESTARTSYS;
  439. cond_resched();
  440. page = follow_page_mask(vma, start, foll_flags, &page_mask);
  441. if (!page) {
  442. int ret;
  443. ret = faultin_page(tsk, vma, start, &foll_flags,
  444. nonblocking);
  445. switch (ret) {
  446. case 0:
  447. goto retry;
  448. case -EFAULT:
  449. case -ENOMEM:
  450. case -EHWPOISON:
  451. return i ? i : ret;
  452. case -EBUSY:
  453. return i;
  454. case -ENOENT:
  455. goto next_page;
  456. }
  457. BUG();
  458. }
  459. if (IS_ERR(page))
  460. return i ? i : PTR_ERR(page);
  461. if (pages) {
  462. pages[i] = page;
  463. flush_anon_page(vma, page, start);
  464. flush_dcache_page(page);
  465. page_mask = 0;
  466. }
  467. next_page:
  468. if (vmas) {
  469. vmas[i] = vma;
  470. page_mask = 0;
  471. }
  472. page_increm = 1 + (~(start >> PAGE_SHIFT) & page_mask);
  473. if (page_increm > nr_pages)
  474. page_increm = nr_pages;
  475. i += page_increm;
  476. start += page_increm * PAGE_SIZE;
  477. nr_pages -= page_increm;
  478. } while (nr_pages);
  479. return i;
  480. }
  481. EXPORT_SYMBOL(__get_user_pages);
  482. /*
  483. * fixup_user_fault() - manually resolve a user page fault
  484. * @tsk: the task_struct to use for page fault accounting, or
  485. * NULL if faults are not to be recorded.
  486. * @mm: mm_struct of target mm
  487. * @address: user address
  488. * @fault_flags:flags to pass down to handle_mm_fault()
  489. *
  490. * This is meant to be called in the specific scenario where for locking reasons
  491. * we try to access user memory in atomic context (within a pagefault_disable()
  492. * section), this returns -EFAULT, and we want to resolve the user fault before
  493. * trying again.
  494. *
  495. * Typically this is meant to be used by the futex code.
  496. *
  497. * The main difference with get_user_pages() is that this function will
  498. * unconditionally call handle_mm_fault() which will in turn perform all the
  499. * necessary SW fixup of the dirty and young bits in the PTE, while
  500. * handle_mm_fault() only guarantees to update these in the struct page.
  501. *
  502. * This is important for some architectures where those bits also gate the
  503. * access permission to the page because they are maintained in software. On
  504. * such architectures, gup() will not be enough to make a subsequent access
  505. * succeed.
  506. *
  507. * This has the same semantics wrt the @mm->mmap_sem as does filemap_fault().
  508. */
  509. int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm,
  510. unsigned long address, unsigned int fault_flags)
  511. {
  512. struct vm_area_struct *vma;
  513. vm_flags_t vm_flags;
  514. int ret;
  515. vma = find_extend_vma(mm, address);
  516. if (!vma || address < vma->vm_start)
  517. return -EFAULT;
  518. vm_flags = (fault_flags & FAULT_FLAG_WRITE) ? VM_WRITE : VM_READ;
  519. if (!(vm_flags & vma->vm_flags))
  520. return -EFAULT;
  521. ret = handle_mm_fault(mm, vma, address, fault_flags);
  522. if (ret & VM_FAULT_ERROR) {
  523. if (ret & VM_FAULT_OOM)
  524. return -ENOMEM;
  525. if (ret & (VM_FAULT_HWPOISON | VM_FAULT_HWPOISON_LARGE))
  526. return -EHWPOISON;
  527. if (ret & (VM_FAULT_SIGBUS | VM_FAULT_SIGSEGV))
  528. return -EFAULT;
  529. BUG();
  530. }
  531. if (tsk) {
  532. if (ret & VM_FAULT_MAJOR)
  533. tsk->maj_flt++;
  534. else
  535. tsk->min_flt++;
  536. }
  537. return 0;
  538. }
  539. static __always_inline long __get_user_pages_locked(struct task_struct *tsk,
  540. struct mm_struct *mm,
  541. unsigned long start,
  542. unsigned long nr_pages,
  543. int write, int force,
  544. struct page **pages,
  545. struct vm_area_struct **vmas,
  546. int *locked, bool notify_drop,
  547. unsigned int flags)
  548. {
  549. long ret, pages_done;
  550. bool lock_dropped;
  551. if (locked) {
  552. /* if VM_FAULT_RETRY can be returned, vmas become invalid */
  553. BUG_ON(vmas);
  554. /* check caller initialized locked */
  555. BUG_ON(*locked != 1);
  556. }
  557. if (pages)
  558. flags |= FOLL_GET;
  559. if (write)
  560. flags |= FOLL_WRITE;
  561. if (force)
  562. flags |= FOLL_FORCE;
  563. pages_done = 0;
  564. lock_dropped = false;
  565. for (;;) {
  566. ret = __get_user_pages(tsk, mm, start, nr_pages, flags, pages,
  567. vmas, locked);
  568. if (!locked)
  569. /* VM_FAULT_RETRY couldn't trigger, bypass */
  570. return ret;
  571. /* VM_FAULT_RETRY cannot return errors */
  572. if (!*locked) {
  573. BUG_ON(ret < 0);
  574. BUG_ON(ret >= nr_pages);
  575. }
  576. if (!pages)
  577. /* If it's a prefault don't insist harder */
  578. return ret;
  579. if (ret > 0) {
  580. nr_pages -= ret;
  581. pages_done += ret;
  582. if (!nr_pages)
  583. break;
  584. }
  585. if (*locked) {
  586. /* VM_FAULT_RETRY didn't trigger */
  587. if (!pages_done)
  588. pages_done = ret;
  589. break;
  590. }
  591. /* VM_FAULT_RETRY triggered, so seek to the faulting offset */
  592. pages += ret;
  593. start += ret << PAGE_SHIFT;
  594. /*
  595. * Repeat on the address that fired VM_FAULT_RETRY
  596. * without FAULT_FLAG_ALLOW_RETRY but with
  597. * FAULT_FLAG_TRIED.
  598. */
  599. *locked = 1;
  600. lock_dropped = true;
  601. down_read(&mm->mmap_sem);
  602. ret = __get_user_pages(tsk, mm, start, 1, flags | FOLL_TRIED,
  603. pages, NULL, NULL);
  604. if (ret != 1) {
  605. BUG_ON(ret > 1);
  606. if (!pages_done)
  607. pages_done = ret;
  608. break;
  609. }
  610. nr_pages--;
  611. pages_done++;
  612. if (!nr_pages)
  613. break;
  614. pages++;
  615. start += PAGE_SIZE;
  616. }
  617. if (notify_drop && lock_dropped && *locked) {
  618. /*
  619. * We must let the caller know we temporarily dropped the lock
  620. * and so the critical section protected by it was lost.
  621. */
  622. up_read(&mm->mmap_sem);
  623. *locked = 0;
  624. }
  625. return pages_done;
  626. }
  627. /*
  628. * We can leverage the VM_FAULT_RETRY functionality in the page fault
  629. * paths better by using either get_user_pages_locked() or
  630. * get_user_pages_unlocked().
  631. *
  632. * get_user_pages_locked() is suitable to replace the form:
  633. *
  634. * down_read(&mm->mmap_sem);
  635. * do_something()
  636. * get_user_pages(tsk, mm, ..., pages, NULL);
  637. * up_read(&mm->mmap_sem);
  638. *
  639. * to:
  640. *
  641. * int locked = 1;
  642. * down_read(&mm->mmap_sem);
  643. * do_something()
  644. * get_user_pages_locked(tsk, mm, ..., pages, &locked);
  645. * if (locked)
  646. * up_read(&mm->mmap_sem);
  647. */
  648. long get_user_pages_locked(struct task_struct *tsk, struct mm_struct *mm,
  649. unsigned long start, unsigned long nr_pages,
  650. int write, int force, struct page **pages,
  651. int *locked)
  652. {
  653. return __get_user_pages_locked(tsk, mm, start, nr_pages, write, force,
  654. pages, NULL, locked, true, FOLL_TOUCH);
  655. }
  656. EXPORT_SYMBOL(get_user_pages_locked);
  657. /*
  658. * Same as get_user_pages_unlocked(...., FOLL_TOUCH) but it allows to
  659. * pass additional gup_flags as last parameter (like FOLL_HWPOISON).
  660. *
  661. * NOTE: here FOLL_TOUCH is not set implicitly and must be set by the
  662. * caller if required (just like with __get_user_pages). "FOLL_GET",
  663. * "FOLL_WRITE" and "FOLL_FORCE" are set implicitly as needed
  664. * according to the parameters "pages", "write", "force"
  665. * respectively.
  666. */
  667. __always_inline long __get_user_pages_unlocked(struct task_struct *tsk, struct mm_struct *mm,
  668. unsigned long start, unsigned long nr_pages,
  669. int write, int force, struct page **pages,
  670. unsigned int gup_flags)
  671. {
  672. long ret;
  673. int locked = 1;
  674. down_read(&mm->mmap_sem);
  675. ret = __get_user_pages_locked(tsk, mm, start, nr_pages, write, force,
  676. pages, NULL, &locked, false, gup_flags);
  677. if (locked)
  678. up_read(&mm->mmap_sem);
  679. return ret;
  680. }
  681. EXPORT_SYMBOL(__get_user_pages_unlocked);
  682. /*
  683. * get_user_pages_unlocked() is suitable to replace the form:
  684. *
  685. * down_read(&mm->mmap_sem);
  686. * get_user_pages(tsk, mm, ..., pages, NULL);
  687. * up_read(&mm->mmap_sem);
  688. *
  689. * with:
  690. *
  691. * get_user_pages_unlocked(tsk, mm, ..., pages);
  692. *
  693. * It is functionally equivalent to get_user_pages_fast so
  694. * get_user_pages_fast should be used instead, if the two parameters
  695. * "tsk" and "mm" are respectively equal to current and current->mm,
  696. * or if "force" shall be set to 1 (get_user_pages_fast misses the
  697. * "force" parameter).
  698. */
  699. long get_user_pages_unlocked(struct task_struct *tsk, struct mm_struct *mm,
  700. unsigned long start, unsigned long nr_pages,
  701. int write, int force, struct page **pages)
  702. {
  703. return __get_user_pages_unlocked(tsk, mm, start, nr_pages, write,
  704. force, pages, FOLL_TOUCH);
  705. }
  706. EXPORT_SYMBOL(get_user_pages_unlocked);
  707. /*
  708. * get_user_pages() - pin user pages in memory
  709. * @tsk: the task_struct to use for page fault accounting, or
  710. * NULL if faults are not to be recorded.
  711. * @mm: mm_struct of target mm
  712. * @start: starting user address
  713. * @nr_pages: number of pages from start to pin
  714. * @write: whether pages will be written to by the caller
  715. * @force: whether to force access even when user mapping is currently
  716. * protected (but never forces write access to shared mapping).
  717. * @pages: array that receives pointers to the pages pinned.
  718. * Should be at least nr_pages long. Or NULL, if caller
  719. * only intends to ensure the pages are faulted in.
  720. * @vmas: array of pointers to vmas corresponding to each page.
  721. * Or NULL if the caller does not require them.
  722. *
  723. * Returns number of pages pinned. This may be fewer than the number
  724. * requested. If nr_pages is 0 or negative, returns 0. If no pages
  725. * were pinned, returns -errno. Each page returned must be released
  726. * with a put_page() call when it is finished with. vmas will only
  727. * remain valid while mmap_sem is held.
  728. *
  729. * Must be called with mmap_sem held for read or write.
  730. *
  731. * get_user_pages walks a process's page tables and takes a reference to
  732. * each struct page that each user address corresponds to at a given
  733. * instant. That is, it takes the page that would be accessed if a user
  734. * thread accesses the given user virtual address at that instant.
  735. *
  736. * This does not guarantee that the page exists in the user mappings when
  737. * get_user_pages returns, and there may even be a completely different
  738. * page there in some cases (eg. if mmapped pagecache has been invalidated
  739. * and subsequently re faulted). However it does guarantee that the page
  740. * won't be freed completely. And mostly callers simply care that the page
  741. * contains data that was valid *at some point in time*. Typically, an IO
  742. * or similar operation cannot guarantee anything stronger anyway because
  743. * locks can't be held over the syscall boundary.
  744. *
  745. * If write=0, the page must not be written to. If the page is written to,
  746. * set_page_dirty (or set_page_dirty_lock, as appropriate) must be called
  747. * after the page is finished with, and before put_page is called.
  748. *
  749. * get_user_pages is typically used for fewer-copy IO operations, to get a
  750. * handle on the memory by some means other than accesses via the user virtual
  751. * addresses. The pages may be submitted for DMA to devices or accessed via
  752. * their kernel linear mapping (via the kmap APIs). Care should be taken to
  753. * use the correct cache flushing APIs.
  754. *
  755. * See also get_user_pages_fast, for performance critical applications.
  756. *
  757. * get_user_pages should be phased out in favor of
  758. * get_user_pages_locked|unlocked or get_user_pages_fast. Nothing
  759. * should use get_user_pages because it cannot pass
  760. * FAULT_FLAG_ALLOW_RETRY to handle_mm_fault.
  761. */
  762. long get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
  763. unsigned long start, unsigned long nr_pages, int write,
  764. int force, struct page **pages, struct vm_area_struct **vmas)
  765. {
  766. return __get_user_pages_locked(tsk, mm, start, nr_pages, write, force,
  767. pages, vmas, NULL, false, FOLL_TOUCH);
  768. }
  769. EXPORT_SYMBOL(get_user_pages);
  770. /**
  771. * populate_vma_page_range() - populate a range of pages in the vma.
  772. * @vma: target vma
  773. * @start: start address
  774. * @end: end address
  775. * @nonblocking:
  776. *
  777. * This takes care of mlocking the pages too if VM_LOCKED is set.
  778. *
  779. * return 0 on success, negative error code on error.
  780. *
  781. * vma->vm_mm->mmap_sem must be held.
  782. *
  783. * If @nonblocking is NULL, it may be held for read or write and will
  784. * be unperturbed.
  785. *
  786. * If @nonblocking is non-NULL, it must held for read only and may be
  787. * released. If it's released, *@nonblocking will be set to 0.
  788. */
  789. long populate_vma_page_range(struct vm_area_struct *vma,
  790. unsigned long start, unsigned long end, int *nonblocking)
  791. {
  792. struct mm_struct *mm = vma->vm_mm;
  793. unsigned long nr_pages = (end - start) / PAGE_SIZE;
  794. int gup_flags;
  795. VM_BUG_ON(start & ~PAGE_MASK);
  796. VM_BUG_ON(end & ~PAGE_MASK);
  797. VM_BUG_ON_VMA(start < vma->vm_start, vma);
  798. VM_BUG_ON_VMA(end > vma->vm_end, vma);
  799. VM_BUG_ON_MM(!rwsem_is_locked(&mm->mmap_sem), mm);
  800. gup_flags = FOLL_TOUCH | FOLL_POPULATE;
  801. /*
  802. * We want to touch writable mappings with a write fault in order
  803. * to break COW, except for shared mappings because these don't COW
  804. * and we would not want to dirty them for nothing.
  805. */
  806. if ((vma->vm_flags & (VM_WRITE | VM_SHARED)) == VM_WRITE)
  807. gup_flags |= FOLL_WRITE;
  808. /*
  809. * We want mlock to succeed for regions that have any permissions
  810. * other than PROT_NONE.
  811. */
  812. if (vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC))
  813. gup_flags |= FOLL_FORCE;
  814. /*
  815. * We made sure addr is within a VMA, so the following will
  816. * not result in a stack expansion that recurses back here.
  817. */
  818. return __get_user_pages(current, mm, start, nr_pages, gup_flags,
  819. NULL, NULL, nonblocking);
  820. }
  821. /*
  822. * __mm_populate - populate and/or mlock pages within a range of address space.
  823. *
  824. * This is used to implement mlock() and the MAP_POPULATE / MAP_LOCKED mmap
  825. * flags. VMAs must be already marked with the desired vm_flags, and
  826. * mmap_sem must not be held.
  827. */
  828. int __mm_populate(unsigned long start, unsigned long len, int ignore_errors)
  829. {
  830. struct mm_struct *mm = current->mm;
  831. unsigned long end, nstart, nend;
  832. struct vm_area_struct *vma = NULL;
  833. int locked = 0;
  834. long ret = 0;
  835. VM_BUG_ON(start & ~PAGE_MASK);
  836. VM_BUG_ON(len != PAGE_ALIGN(len));
  837. end = start + len;
  838. for (nstart = start; nstart < end; nstart = nend) {
  839. /*
  840. * We want to fault in pages for [nstart; end) address range.
  841. * Find first corresponding VMA.
  842. */
  843. if (!locked) {
  844. locked = 1;
  845. down_read(&mm->mmap_sem);
  846. vma = find_vma(mm, nstart);
  847. } else if (nstart >= vma->vm_end)
  848. vma = vma->vm_next;
  849. if (!vma || vma->vm_start >= end)
  850. break;
  851. /*
  852. * Set [nstart; nend) to intersection of desired address
  853. * range with the first VMA. Also, skip undesirable VMA types.
  854. */
  855. nend = min(end, vma->vm_end);
  856. if (vma->vm_flags & (VM_IO | VM_PFNMAP))
  857. continue;
  858. if (nstart < vma->vm_start)
  859. nstart = vma->vm_start;
  860. /*
  861. * Now fault in a range of pages. populate_vma_page_range()
  862. * double checks the vma flags, so that it won't mlock pages
  863. * if the vma was already munlocked.
  864. */
  865. ret = populate_vma_page_range(vma, nstart, nend, &locked);
  866. if (ret < 0) {
  867. if (ignore_errors) {
  868. ret = 0;
  869. continue; /* continue at next VMA */
  870. }
  871. break;
  872. }
  873. nend = nstart + ret * PAGE_SIZE;
  874. ret = 0;
  875. }
  876. if (locked)
  877. up_read(&mm->mmap_sem);
  878. return ret; /* 0 or negative error code */
  879. }
  880. /**
  881. * get_dump_page() - pin user page in memory while writing it to core dump
  882. * @addr: user address
  883. *
  884. * Returns struct page pointer of user page pinned for dump,
  885. * to be freed afterwards by page_cache_release() or put_page().
  886. *
  887. * Returns NULL on any kind of failure - a hole must then be inserted into
  888. * the corefile, to preserve alignment with its headers; and also returns
  889. * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found -
  890. * allowing a hole to be left in the corefile to save diskspace.
  891. *
  892. * Called without mmap_sem, but after all other threads have been killed.
  893. */
  894. #ifdef CONFIG_ELF_CORE
  895. struct page *get_dump_page(unsigned long addr)
  896. {
  897. struct vm_area_struct *vma;
  898. struct page *page;
  899. if (__get_user_pages(current, current->mm, addr, 1,
  900. FOLL_FORCE | FOLL_DUMP | FOLL_GET, &page, &vma,
  901. NULL) < 1)
  902. return NULL;
  903. flush_cache_page(vma, addr, page_to_pfn(page));
  904. return page;
  905. }
  906. #endif /* CONFIG_ELF_CORE */
  907. /*
  908. * Generic RCU Fast GUP
  909. *
  910. * get_user_pages_fast attempts to pin user pages by walking the page
  911. * tables directly and avoids taking locks. Thus the walker needs to be
  912. * protected from page table pages being freed from under it, and should
  913. * block any THP splits.
  914. *
  915. * One way to achieve this is to have the walker disable interrupts, and
  916. * rely on IPIs from the TLB flushing code blocking before the page table
  917. * pages are freed. This is unsuitable for architectures that do not need
  918. * to broadcast an IPI when invalidating TLBs.
  919. *
  920. * Another way to achieve this is to batch up page table containing pages
  921. * belonging to more than one mm_user, then rcu_sched a callback to free those
  922. * pages. Disabling interrupts will allow the fast_gup walker to both block
  923. * the rcu_sched callback, and an IPI that we broadcast for splitting THPs
  924. * (which is a relatively rare event). The code below adopts this strategy.
  925. *
  926. * Before activating this code, please be aware that the following assumptions
  927. * are currently made:
  928. *
  929. * *) HAVE_RCU_TABLE_FREE is enabled, and tlb_remove_table is used to free
  930. * pages containing page tables.
  931. *
  932. * *) THP splits will broadcast an IPI, this can be achieved by overriding
  933. * pmdp_splitting_flush.
  934. *
  935. * *) ptes can be read atomically by the architecture.
  936. *
  937. * *) access_ok is sufficient to validate userspace address ranges.
  938. *
  939. * The last two assumptions can be relaxed by the addition of helper functions.
  940. *
  941. * This code is based heavily on the PowerPC implementation by Nick Piggin.
  942. */
  943. #ifdef CONFIG_HAVE_GENERIC_RCU_GUP
  944. #ifdef __HAVE_ARCH_PTE_SPECIAL
  945. static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
  946. int write, struct page **pages, int *nr)
  947. {
  948. pte_t *ptep, *ptem;
  949. int ret = 0;
  950. ptem = ptep = pte_offset_map(&pmd, addr);
  951. do {
  952. /*
  953. * In the line below we are assuming that the pte can be read
  954. * atomically. If this is not the case for your architecture,
  955. * please wrap this in a helper function!
  956. *
  957. * for an example see gup_get_pte in arch/x86/mm/gup.c
  958. */
  959. pte_t pte = READ_ONCE(*ptep);
  960. struct page *page;
  961. /*
  962. * Similar to the PMD case below, NUMA hinting must take slow
  963. * path using the pte_protnone check.
  964. */
  965. if (!pte_present(pte) || pte_special(pte) ||
  966. pte_protnone(pte) || (write && !pte_write(pte)))
  967. goto pte_unmap;
  968. VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
  969. page = pte_page(pte);
  970. if (!page_cache_get_speculative(page))
  971. goto pte_unmap;
  972. if (unlikely(pte_val(pte) != pte_val(*ptep))) {
  973. put_page(page);
  974. goto pte_unmap;
  975. }
  976. pages[*nr] = page;
  977. (*nr)++;
  978. } while (ptep++, addr += PAGE_SIZE, addr != end);
  979. ret = 1;
  980. pte_unmap:
  981. pte_unmap(ptem);
  982. return ret;
  983. }
  984. #else
  985. /*
  986. * If we can't determine whether or not a pte is special, then fail immediately
  987. * for ptes. Note, we can still pin HugeTLB and THP as these are guaranteed not
  988. * to be special.
  989. *
  990. * For a futex to be placed on a THP tail page, get_futex_key requires a
  991. * __get_user_pages_fast implementation that can pin pages. Thus it's still
  992. * useful to have gup_huge_pmd even if we can't operate on ptes.
  993. */
  994. static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
  995. int write, struct page **pages, int *nr)
  996. {
  997. return 0;
  998. }
  999. #endif /* __HAVE_ARCH_PTE_SPECIAL */
  1000. static int gup_huge_pmd(pmd_t orig, pmd_t *pmdp, unsigned long addr,
  1001. unsigned long end, int write, struct page **pages, int *nr)
  1002. {
  1003. struct page *head, *page, *tail;
  1004. int refs;
  1005. if (write && !pmd_write(orig))
  1006. return 0;
  1007. refs = 0;
  1008. head = pmd_page(orig);
  1009. page = head + ((addr & ~PMD_MASK) >> PAGE_SHIFT);
  1010. tail = page;
  1011. do {
  1012. VM_BUG_ON_PAGE(compound_head(page) != head, page);
  1013. pages[*nr] = page;
  1014. (*nr)++;
  1015. page++;
  1016. refs++;
  1017. } while (addr += PAGE_SIZE, addr != end);
  1018. if (!page_cache_add_speculative(head, refs)) {
  1019. *nr -= refs;
  1020. return 0;
  1021. }
  1022. if (unlikely(pmd_val(orig) != pmd_val(*pmdp))) {
  1023. *nr -= refs;
  1024. while (refs--)
  1025. put_page(head);
  1026. return 0;
  1027. }
  1028. /*
  1029. * Any tail pages need their mapcount reference taken before we
  1030. * return. (This allows the THP code to bump their ref count when
  1031. * they are split into base pages).
  1032. */
  1033. while (refs--) {
  1034. if (PageTail(tail))
  1035. get_huge_page_tail(tail);
  1036. tail++;
  1037. }
  1038. return 1;
  1039. }
  1040. static int gup_huge_pud(pud_t orig, pud_t *pudp, unsigned long addr,
  1041. unsigned long end, int write, struct page **pages, int *nr)
  1042. {
  1043. struct page *head, *page, *tail;
  1044. int refs;
  1045. if (write && !pud_write(orig))
  1046. return 0;
  1047. refs = 0;
  1048. head = pud_page(orig);
  1049. page = head + ((addr & ~PUD_MASK) >> PAGE_SHIFT);
  1050. tail = page;
  1051. do {
  1052. VM_BUG_ON_PAGE(compound_head(page) != head, page);
  1053. pages[*nr] = page;
  1054. (*nr)++;
  1055. page++;
  1056. refs++;
  1057. } while (addr += PAGE_SIZE, addr != end);
  1058. if (!page_cache_add_speculative(head, refs)) {
  1059. *nr -= refs;
  1060. return 0;
  1061. }
  1062. if (unlikely(pud_val(orig) != pud_val(*pudp))) {
  1063. *nr -= refs;
  1064. while (refs--)
  1065. put_page(head);
  1066. return 0;
  1067. }
  1068. while (refs--) {
  1069. if (PageTail(tail))
  1070. get_huge_page_tail(tail);
  1071. tail++;
  1072. }
  1073. return 1;
  1074. }
  1075. static int gup_huge_pgd(pgd_t orig, pgd_t *pgdp, unsigned long addr,
  1076. unsigned long end, int write,
  1077. struct page **pages, int *nr)
  1078. {
  1079. int refs;
  1080. struct page *head, *page, *tail;
  1081. if (write && !pgd_write(orig))
  1082. return 0;
  1083. refs = 0;
  1084. head = pgd_page(orig);
  1085. page = head + ((addr & ~PGDIR_MASK) >> PAGE_SHIFT);
  1086. tail = page;
  1087. do {
  1088. VM_BUG_ON_PAGE(compound_head(page) != head, page);
  1089. pages[*nr] = page;
  1090. (*nr)++;
  1091. page++;
  1092. refs++;
  1093. } while (addr += PAGE_SIZE, addr != end);
  1094. if (!page_cache_add_speculative(head, refs)) {
  1095. *nr -= refs;
  1096. return 0;
  1097. }
  1098. if (unlikely(pgd_val(orig) != pgd_val(*pgdp))) {
  1099. *nr -= refs;
  1100. while (refs--)
  1101. put_page(head);
  1102. return 0;
  1103. }
  1104. while (refs--) {
  1105. if (PageTail(tail))
  1106. get_huge_page_tail(tail);
  1107. tail++;
  1108. }
  1109. return 1;
  1110. }
  1111. static int gup_pmd_range(pud_t pud, unsigned long addr, unsigned long end,
  1112. int write, struct page **pages, int *nr)
  1113. {
  1114. unsigned long next;
  1115. pmd_t *pmdp;
  1116. pmdp = pmd_offset(&pud, addr);
  1117. do {
  1118. pmd_t pmd = READ_ONCE(*pmdp);
  1119. next = pmd_addr_end(addr, end);
  1120. if (pmd_none(pmd) || pmd_trans_splitting(pmd))
  1121. return 0;
  1122. if (unlikely(pmd_trans_huge(pmd) || pmd_huge(pmd))) {
  1123. /*
  1124. * NUMA hinting faults need to be handled in the GUP
  1125. * slowpath for accounting purposes and so that they
  1126. * can be serialised against THP migration.
  1127. */
  1128. if (pmd_protnone(pmd))
  1129. return 0;
  1130. if (!gup_huge_pmd(pmd, pmdp, addr, next, write,
  1131. pages, nr))
  1132. return 0;
  1133. } else if (unlikely(is_hugepd(__hugepd(pmd_val(pmd))))) {
  1134. /*
  1135. * architecture have different format for hugetlbfs
  1136. * pmd format and THP pmd format
  1137. */
  1138. if (!gup_huge_pd(__hugepd(pmd_val(pmd)), addr,
  1139. PMD_SHIFT, next, write, pages, nr))
  1140. return 0;
  1141. } else if (!gup_pte_range(pmd, addr, next, write, pages, nr))
  1142. return 0;
  1143. } while (pmdp++, addr = next, addr != end);
  1144. return 1;
  1145. }
  1146. static int gup_pud_range(pgd_t pgd, unsigned long addr, unsigned long end,
  1147. int write, struct page **pages, int *nr)
  1148. {
  1149. unsigned long next;
  1150. pud_t *pudp;
  1151. pudp = pud_offset(&pgd, addr);
  1152. do {
  1153. pud_t pud = READ_ONCE(*pudp);
  1154. next = pud_addr_end(addr, end);
  1155. if (pud_none(pud))
  1156. return 0;
  1157. if (unlikely(pud_huge(pud))) {
  1158. if (!gup_huge_pud(pud, pudp, addr, next, write,
  1159. pages, nr))
  1160. return 0;
  1161. } else if (unlikely(is_hugepd(__hugepd(pud_val(pud))))) {
  1162. if (!gup_huge_pd(__hugepd(pud_val(pud)), addr,
  1163. PUD_SHIFT, next, write, pages, nr))
  1164. return 0;
  1165. } else if (!gup_pmd_range(pud, addr, next, write, pages, nr))
  1166. return 0;
  1167. } while (pudp++, addr = next, addr != end);
  1168. return 1;
  1169. }
  1170. /*
  1171. * Like get_user_pages_fast() except it's IRQ-safe in that it won't fall back to
  1172. * the regular GUP. It will only return non-negative values.
  1173. */
  1174. int __get_user_pages_fast(unsigned long start, int nr_pages, int write,
  1175. struct page **pages)
  1176. {
  1177. struct mm_struct *mm = current->mm;
  1178. unsigned long addr, len, end;
  1179. unsigned long next, flags;
  1180. pgd_t *pgdp;
  1181. int nr = 0;
  1182. start &= PAGE_MASK;
  1183. addr = start;
  1184. len = (unsigned long) nr_pages << PAGE_SHIFT;
  1185. end = start + len;
  1186. if (unlikely(!access_ok(write ? VERIFY_WRITE : VERIFY_READ,
  1187. start, len)))
  1188. return 0;
  1189. /*
  1190. * Disable interrupts. We use the nested form as we can already have
  1191. * interrupts disabled by get_futex_key.
  1192. *
  1193. * With interrupts disabled, we block page table pages from being
  1194. * freed from under us. See mmu_gather_tlb in asm-generic/tlb.h
  1195. * for more details.
  1196. *
  1197. * We do not adopt an rcu_read_lock(.) here as we also want to
  1198. * block IPIs that come from THPs splitting.
  1199. */
  1200. local_irq_save(flags);
  1201. pgdp = pgd_offset(mm, addr);
  1202. do {
  1203. pgd_t pgd = READ_ONCE(*pgdp);
  1204. next = pgd_addr_end(addr, end);
  1205. if (pgd_none(pgd))
  1206. break;
  1207. if (unlikely(pgd_huge(pgd))) {
  1208. if (!gup_huge_pgd(pgd, pgdp, addr, next, write,
  1209. pages, &nr))
  1210. break;
  1211. } else if (unlikely(is_hugepd(__hugepd(pgd_val(pgd))))) {
  1212. if (!gup_huge_pd(__hugepd(pgd_val(pgd)), addr,
  1213. PGDIR_SHIFT, next, write, pages, &nr))
  1214. break;
  1215. } else if (!gup_pud_range(pgd, addr, next, write, pages, &nr))
  1216. break;
  1217. } while (pgdp++, addr = next, addr != end);
  1218. local_irq_restore(flags);
  1219. return nr;
  1220. }
  1221. /**
  1222. * get_user_pages_fast() - pin user pages in memory
  1223. * @start: starting user address
  1224. * @nr_pages: number of pages from start to pin
  1225. * @write: whether pages will be written to
  1226. * @pages: array that receives pointers to the pages pinned.
  1227. * Should be at least nr_pages long.
  1228. *
  1229. * Attempt to pin user pages in memory without taking mm->mmap_sem.
  1230. * If not successful, it will fall back to taking the lock and
  1231. * calling get_user_pages().
  1232. *
  1233. * Returns number of pages pinned. This may be fewer than the number
  1234. * requested. If nr_pages is 0 or negative, returns 0. If no pages
  1235. * were pinned, returns -errno.
  1236. */
  1237. int get_user_pages_fast(unsigned long start, int nr_pages, int write,
  1238. struct page **pages)
  1239. {
  1240. struct mm_struct *mm = current->mm;
  1241. int nr, ret;
  1242. start &= PAGE_MASK;
  1243. nr = __get_user_pages_fast(start, nr_pages, write, pages);
  1244. ret = nr;
  1245. if (nr < nr_pages) {
  1246. /* Try to get the remaining pages with get_user_pages */
  1247. start += nr << PAGE_SHIFT;
  1248. pages += nr;
  1249. ret = get_user_pages_unlocked(current, mm, start,
  1250. nr_pages - nr, write, 0, pages);
  1251. /* Have to be a bit careful with return values */
  1252. if (nr > 0) {
  1253. if (ret < 0)
  1254. ret = nr;
  1255. else
  1256. ret += nr;
  1257. }
  1258. }
  1259. return ret;
  1260. }
  1261. #endif /* CONFIG_HAVE_GENERIC_RCU_GUP */