gup.c 40 KB

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