gup.c 44 KB

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