gup.c 40 KB

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