gup.c 42 KB

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