gup.c 46 KB

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