memory.c 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278
  1. /*
  2. * linux/mm/memory.c
  3. *
  4. * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
  5. */
  6. /*
  7. * demand-loading started 01.12.91 - seems it is high on the list of
  8. * things wanted, and it should be easy to implement. - Linus
  9. */
  10. /*
  11. * Ok, demand-loading was easy, shared pages a little bit tricker. Shared
  12. * pages started 02.12.91, seems to work. - Linus.
  13. *
  14. * Tested sharing by executing about 30 /bin/sh: under the old kernel it
  15. * would have taken more than the 6M I have free, but it worked well as
  16. * far as I could see.
  17. *
  18. * Also corrected some "invalidate()"s - I wasn't doing enough of them.
  19. */
  20. /*
  21. * Real VM (paging to/from disk) started 18.12.91. Much more work and
  22. * thought has to go into this. Oh, well..
  23. * 19.12.91 - works, somewhat. Sometimes I get faults, don't know why.
  24. * Found it. Everything seems to work now.
  25. * 20.12.91 - Ok, making the swap-device changeable like the root.
  26. */
  27. /*
  28. * 05.04.94 - Multi-page memory management added for v1.1.
  29. * Idea by Alex Bligh (alex@cconcepts.co.uk)
  30. *
  31. * 16.07.99 - Support of BIGMEM added by Gerhard Wichert, Siemens AG
  32. * (Gerhard.Wichert@pdb.siemens.de)
  33. *
  34. * Aug/Sep 2004 Changed to four level page tables (Andi Kleen)
  35. */
  36. #include <linux/kernel_stat.h>
  37. #include <linux/mm.h>
  38. #include <linux/hugetlb.h>
  39. #include <linux/mman.h>
  40. #include <linux/swap.h>
  41. #include <linux/highmem.h>
  42. #include <linux/pagemap.h>
  43. #include <linux/rmap.h>
  44. #include <linux/module.h>
  45. #include <linux/init.h>
  46. #include <asm/pgalloc.h>
  47. #include <asm/uaccess.h>
  48. #include <asm/tlb.h>
  49. #include <asm/tlbflush.h>
  50. #include <asm/pgtable.h>
  51. #include <linux/swapops.h>
  52. #include <linux/elf.h>
  53. #ifndef CONFIG_NEED_MULTIPLE_NODES
  54. /* use the per-pgdat data instead for discontigmem - mbligh */
  55. unsigned long max_mapnr;
  56. struct page *mem_map;
  57. EXPORT_SYMBOL(max_mapnr);
  58. EXPORT_SYMBOL(mem_map);
  59. #endif
  60. unsigned long num_physpages;
  61. /*
  62. * A number of key systems in x86 including ioremap() rely on the assumption
  63. * that high_memory defines the upper bound on direct map memory, then end
  64. * of ZONE_NORMAL. Under CONFIG_DISCONTIG this means that max_low_pfn and
  65. * highstart_pfn must be the same; there must be no gap between ZONE_NORMAL
  66. * and ZONE_HIGHMEM.
  67. */
  68. void * high_memory;
  69. unsigned long vmalloc_earlyreserve;
  70. EXPORT_SYMBOL(num_physpages);
  71. EXPORT_SYMBOL(high_memory);
  72. EXPORT_SYMBOL(vmalloc_earlyreserve);
  73. /*
  74. * If a p?d_bad entry is found while walking page tables, report
  75. * the error, before resetting entry to p?d_none. Usually (but
  76. * very seldom) called out from the p?d_none_or_clear_bad macros.
  77. */
  78. void pgd_clear_bad(pgd_t *pgd)
  79. {
  80. pgd_ERROR(*pgd);
  81. pgd_clear(pgd);
  82. }
  83. void pud_clear_bad(pud_t *pud)
  84. {
  85. pud_ERROR(*pud);
  86. pud_clear(pud);
  87. }
  88. void pmd_clear_bad(pmd_t *pmd)
  89. {
  90. pmd_ERROR(*pmd);
  91. pmd_clear(pmd);
  92. }
  93. /*
  94. * Note: this doesn't free the actual pages themselves. That
  95. * has been handled earlier when unmapping all the memory regions.
  96. */
  97. static void free_pte_range(struct mmu_gather *tlb, pmd_t *pmd)
  98. {
  99. struct page *page = pmd_page(*pmd);
  100. pmd_clear(pmd);
  101. pte_lock_deinit(page);
  102. pte_free_tlb(tlb, page);
  103. dec_page_state(nr_page_table_pages);
  104. tlb->mm->nr_ptes--;
  105. }
  106. static inline void free_pmd_range(struct mmu_gather *tlb, pud_t *pud,
  107. unsigned long addr, unsigned long end,
  108. unsigned long floor, unsigned long ceiling)
  109. {
  110. pmd_t *pmd;
  111. unsigned long next;
  112. unsigned long start;
  113. start = addr;
  114. pmd = pmd_offset(pud, addr);
  115. do {
  116. next = pmd_addr_end(addr, end);
  117. if (pmd_none_or_clear_bad(pmd))
  118. continue;
  119. free_pte_range(tlb, pmd);
  120. } while (pmd++, addr = next, addr != end);
  121. start &= PUD_MASK;
  122. if (start < floor)
  123. return;
  124. if (ceiling) {
  125. ceiling &= PUD_MASK;
  126. if (!ceiling)
  127. return;
  128. }
  129. if (end - 1 > ceiling - 1)
  130. return;
  131. pmd = pmd_offset(pud, start);
  132. pud_clear(pud);
  133. pmd_free_tlb(tlb, pmd);
  134. }
  135. static inline void free_pud_range(struct mmu_gather *tlb, pgd_t *pgd,
  136. unsigned long addr, unsigned long end,
  137. unsigned long floor, unsigned long ceiling)
  138. {
  139. pud_t *pud;
  140. unsigned long next;
  141. unsigned long start;
  142. start = addr;
  143. pud = pud_offset(pgd, addr);
  144. do {
  145. next = pud_addr_end(addr, end);
  146. if (pud_none_or_clear_bad(pud))
  147. continue;
  148. free_pmd_range(tlb, pud, addr, next, floor, ceiling);
  149. } while (pud++, addr = next, addr != end);
  150. start &= PGDIR_MASK;
  151. if (start < floor)
  152. return;
  153. if (ceiling) {
  154. ceiling &= PGDIR_MASK;
  155. if (!ceiling)
  156. return;
  157. }
  158. if (end - 1 > ceiling - 1)
  159. return;
  160. pud = pud_offset(pgd, start);
  161. pgd_clear(pgd);
  162. pud_free_tlb(tlb, pud);
  163. }
  164. /*
  165. * This function frees user-level page tables of a process.
  166. *
  167. * Must be called with pagetable lock held.
  168. */
  169. void free_pgd_range(struct mmu_gather **tlb,
  170. unsigned long addr, unsigned long end,
  171. unsigned long floor, unsigned long ceiling)
  172. {
  173. pgd_t *pgd;
  174. unsigned long next;
  175. unsigned long start;
  176. /*
  177. * The next few lines have given us lots of grief...
  178. *
  179. * Why are we testing PMD* at this top level? Because often
  180. * there will be no work to do at all, and we'd prefer not to
  181. * go all the way down to the bottom just to discover that.
  182. *
  183. * Why all these "- 1"s? Because 0 represents both the bottom
  184. * of the address space and the top of it (using -1 for the
  185. * top wouldn't help much: the masks would do the wrong thing).
  186. * The rule is that addr 0 and floor 0 refer to the bottom of
  187. * the address space, but end 0 and ceiling 0 refer to the top
  188. * Comparisons need to use "end - 1" and "ceiling - 1" (though
  189. * that end 0 case should be mythical).
  190. *
  191. * Wherever addr is brought up or ceiling brought down, we must
  192. * be careful to reject "the opposite 0" before it confuses the
  193. * subsequent tests. But what about where end is brought down
  194. * by PMD_SIZE below? no, end can't go down to 0 there.
  195. *
  196. * Whereas we round start (addr) and ceiling down, by different
  197. * masks at different levels, in order to test whether a table
  198. * now has no other vmas using it, so can be freed, we don't
  199. * bother to round floor or end up - the tests don't need that.
  200. */
  201. addr &= PMD_MASK;
  202. if (addr < floor) {
  203. addr += PMD_SIZE;
  204. if (!addr)
  205. return;
  206. }
  207. if (ceiling) {
  208. ceiling &= PMD_MASK;
  209. if (!ceiling)
  210. return;
  211. }
  212. if (end - 1 > ceiling - 1)
  213. end -= PMD_SIZE;
  214. if (addr > end - 1)
  215. return;
  216. start = addr;
  217. pgd = pgd_offset((*tlb)->mm, addr);
  218. do {
  219. next = pgd_addr_end(addr, end);
  220. if (pgd_none_or_clear_bad(pgd))
  221. continue;
  222. free_pud_range(*tlb, pgd, addr, next, floor, ceiling);
  223. } while (pgd++, addr = next, addr != end);
  224. if (!(*tlb)->fullmm)
  225. flush_tlb_pgtables((*tlb)->mm, start, end);
  226. }
  227. void free_pgtables(struct mmu_gather **tlb, struct vm_area_struct *vma,
  228. unsigned long floor, unsigned long ceiling)
  229. {
  230. while (vma) {
  231. struct vm_area_struct *next = vma->vm_next;
  232. unsigned long addr = vma->vm_start;
  233. /*
  234. * Hide vma from rmap and vmtruncate before freeing pgtables
  235. */
  236. anon_vma_unlink(vma);
  237. unlink_file_vma(vma);
  238. if (is_hugepage_only_range(vma->vm_mm, addr, HPAGE_SIZE)) {
  239. hugetlb_free_pgd_range(tlb, addr, vma->vm_end,
  240. floor, next? next->vm_start: ceiling);
  241. } else {
  242. /*
  243. * Optimization: gather nearby vmas into one call down
  244. */
  245. while (next && next->vm_start <= vma->vm_end + PMD_SIZE
  246. && !is_hugepage_only_range(vma->vm_mm, next->vm_start,
  247. HPAGE_SIZE)) {
  248. vma = next;
  249. next = vma->vm_next;
  250. anon_vma_unlink(vma);
  251. unlink_file_vma(vma);
  252. }
  253. free_pgd_range(tlb, addr, vma->vm_end,
  254. floor, next? next->vm_start: ceiling);
  255. }
  256. vma = next;
  257. }
  258. }
  259. int __pte_alloc(struct mm_struct *mm, pmd_t *pmd, unsigned long address)
  260. {
  261. struct page *new = pte_alloc_one(mm, address);
  262. if (!new)
  263. return -ENOMEM;
  264. pte_lock_init(new);
  265. spin_lock(&mm->page_table_lock);
  266. if (pmd_present(*pmd)) { /* Another has populated it */
  267. pte_lock_deinit(new);
  268. pte_free(new);
  269. } else {
  270. mm->nr_ptes++;
  271. inc_page_state(nr_page_table_pages);
  272. pmd_populate(mm, pmd, new);
  273. }
  274. spin_unlock(&mm->page_table_lock);
  275. return 0;
  276. }
  277. int __pte_alloc_kernel(pmd_t *pmd, unsigned long address)
  278. {
  279. pte_t *new = pte_alloc_one_kernel(&init_mm, address);
  280. if (!new)
  281. return -ENOMEM;
  282. spin_lock(&init_mm.page_table_lock);
  283. if (pmd_present(*pmd)) /* Another has populated it */
  284. pte_free_kernel(new);
  285. else
  286. pmd_populate_kernel(&init_mm, pmd, new);
  287. spin_unlock(&init_mm.page_table_lock);
  288. return 0;
  289. }
  290. static inline void add_mm_rss(struct mm_struct *mm, int file_rss, int anon_rss)
  291. {
  292. if (file_rss)
  293. add_mm_counter(mm, file_rss, file_rss);
  294. if (anon_rss)
  295. add_mm_counter(mm, anon_rss, anon_rss);
  296. }
  297. /*
  298. * This function is called to print an error when a pte in a
  299. * !VM_UNPAGED region is found pointing to an invalid pfn (which
  300. * is an error.
  301. *
  302. * The calling function must still handle the error.
  303. */
  304. void print_bad_pte(struct vm_area_struct *vma, pte_t pte, unsigned long vaddr)
  305. {
  306. printk(KERN_ERR "Bad pte = %08llx, process = %s, "
  307. "vm_flags = %lx, vaddr = %lx\n",
  308. (long long)pte_val(pte),
  309. (vma->vm_mm == current->mm ? current->comm : "???"),
  310. vma->vm_flags, vaddr);
  311. dump_stack();
  312. }
  313. /*
  314. * page_is_anon applies strict checks for an anonymous page belonging to
  315. * this vma at this address. It is used on VM_UNPAGED vmas, which are
  316. * usually populated with shared originals (which must not be counted),
  317. * but occasionally contain private COWed copies (when !VM_SHARED, or
  318. * perhaps via ptrace when VM_SHARED). An mmap of /dev/mem might window
  319. * free pages, pages from other processes, or from other parts of this:
  320. * it's tricky, but try not to be deceived by foreign anonymous pages.
  321. */
  322. static inline int page_is_anon(struct page *page,
  323. struct vm_area_struct *vma, unsigned long addr)
  324. {
  325. return page && PageAnon(page) && page_mapped(page) &&
  326. page_address_in_vma(page, vma) == addr;
  327. }
  328. /*
  329. * copy one vm_area from one task to the other. Assumes the page tables
  330. * already present in the new task to be cleared in the whole range
  331. * covered by this vma.
  332. */
  333. static inline void
  334. copy_one_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm,
  335. pte_t *dst_pte, pte_t *src_pte, struct vm_area_struct *vma,
  336. unsigned long addr, int *rss)
  337. {
  338. unsigned long vm_flags = vma->vm_flags;
  339. pte_t pte = *src_pte;
  340. struct page *page;
  341. unsigned long pfn;
  342. /* pte contains position in swap or file, so copy. */
  343. if (unlikely(!pte_present(pte))) {
  344. if (!pte_file(pte)) {
  345. swap_duplicate(pte_to_swp_entry(pte));
  346. /* make sure dst_mm is on swapoff's mmlist. */
  347. if (unlikely(list_empty(&dst_mm->mmlist))) {
  348. spin_lock(&mmlist_lock);
  349. if (list_empty(&dst_mm->mmlist))
  350. list_add(&dst_mm->mmlist,
  351. &src_mm->mmlist);
  352. spin_unlock(&mmlist_lock);
  353. }
  354. }
  355. goto out_set_pte;
  356. }
  357. pfn = pte_pfn(pte);
  358. page = pfn_valid(pfn)? pfn_to_page(pfn): NULL;
  359. if (unlikely(vm_flags & VM_UNPAGED))
  360. if (!page_is_anon(page, vma, addr))
  361. goto out_set_pte;
  362. /*
  363. * If the pte points outside of valid memory but
  364. * the region is not VM_UNPAGED, we have a problem.
  365. */
  366. if (unlikely(!page)) {
  367. print_bad_pte(vma, pte, addr);
  368. goto out_set_pte; /* try to do something sane */
  369. }
  370. /*
  371. * If it's a COW mapping, write protect it both
  372. * in the parent and the child
  373. */
  374. if ((vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE) {
  375. ptep_set_wrprotect(src_mm, addr, src_pte);
  376. pte = *src_pte;
  377. }
  378. /*
  379. * If it's a shared mapping, mark it clean in
  380. * the child
  381. */
  382. if (vm_flags & VM_SHARED)
  383. pte = pte_mkclean(pte);
  384. pte = pte_mkold(pte);
  385. get_page(page);
  386. page_dup_rmap(page);
  387. rss[!!PageAnon(page)]++;
  388. out_set_pte:
  389. set_pte_at(dst_mm, addr, dst_pte, pte);
  390. }
  391. static int copy_pte_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
  392. pmd_t *dst_pmd, pmd_t *src_pmd, struct vm_area_struct *vma,
  393. unsigned long addr, unsigned long end)
  394. {
  395. pte_t *src_pte, *dst_pte;
  396. spinlock_t *src_ptl, *dst_ptl;
  397. int progress = 0;
  398. int rss[2];
  399. again:
  400. rss[1] = rss[0] = 0;
  401. dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl);
  402. if (!dst_pte)
  403. return -ENOMEM;
  404. src_pte = pte_offset_map_nested(src_pmd, addr);
  405. src_ptl = pte_lockptr(src_mm, src_pmd);
  406. spin_lock(src_ptl);
  407. do {
  408. /*
  409. * We are holding two locks at this point - either of them
  410. * could generate latencies in another task on another CPU.
  411. */
  412. if (progress >= 32) {
  413. progress = 0;
  414. if (need_resched() ||
  415. need_lockbreak(src_ptl) ||
  416. need_lockbreak(dst_ptl))
  417. break;
  418. }
  419. if (pte_none(*src_pte)) {
  420. progress++;
  421. continue;
  422. }
  423. copy_one_pte(dst_mm, src_mm, dst_pte, src_pte, vma, addr, rss);
  424. progress += 8;
  425. } while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end);
  426. spin_unlock(src_ptl);
  427. pte_unmap_nested(src_pte - 1);
  428. add_mm_rss(dst_mm, rss[0], rss[1]);
  429. pte_unmap_unlock(dst_pte - 1, dst_ptl);
  430. cond_resched();
  431. if (addr != end)
  432. goto again;
  433. return 0;
  434. }
  435. static inline int copy_pmd_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
  436. pud_t *dst_pud, pud_t *src_pud, struct vm_area_struct *vma,
  437. unsigned long addr, unsigned long end)
  438. {
  439. pmd_t *src_pmd, *dst_pmd;
  440. unsigned long next;
  441. dst_pmd = pmd_alloc(dst_mm, dst_pud, addr);
  442. if (!dst_pmd)
  443. return -ENOMEM;
  444. src_pmd = pmd_offset(src_pud, addr);
  445. do {
  446. next = pmd_addr_end(addr, end);
  447. if (pmd_none_or_clear_bad(src_pmd))
  448. continue;
  449. if (copy_pte_range(dst_mm, src_mm, dst_pmd, src_pmd,
  450. vma, addr, next))
  451. return -ENOMEM;
  452. } while (dst_pmd++, src_pmd++, addr = next, addr != end);
  453. return 0;
  454. }
  455. static inline int copy_pud_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
  456. pgd_t *dst_pgd, pgd_t *src_pgd, struct vm_area_struct *vma,
  457. unsigned long addr, unsigned long end)
  458. {
  459. pud_t *src_pud, *dst_pud;
  460. unsigned long next;
  461. dst_pud = pud_alloc(dst_mm, dst_pgd, addr);
  462. if (!dst_pud)
  463. return -ENOMEM;
  464. src_pud = pud_offset(src_pgd, addr);
  465. do {
  466. next = pud_addr_end(addr, end);
  467. if (pud_none_or_clear_bad(src_pud))
  468. continue;
  469. if (copy_pmd_range(dst_mm, src_mm, dst_pud, src_pud,
  470. vma, addr, next))
  471. return -ENOMEM;
  472. } while (dst_pud++, src_pud++, addr = next, addr != end);
  473. return 0;
  474. }
  475. int copy_page_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
  476. struct vm_area_struct *vma)
  477. {
  478. pgd_t *src_pgd, *dst_pgd;
  479. unsigned long next;
  480. unsigned long addr = vma->vm_start;
  481. unsigned long end = vma->vm_end;
  482. /*
  483. * Don't copy ptes where a page fault will fill them correctly.
  484. * Fork becomes much lighter when there are big shared or private
  485. * readonly mappings. The tradeoff is that copy_page_range is more
  486. * efficient than faulting.
  487. */
  488. if (!(vma->vm_flags & (VM_HUGETLB|VM_NONLINEAR|VM_UNPAGED))) {
  489. if (!vma->anon_vma)
  490. return 0;
  491. }
  492. if (is_vm_hugetlb_page(vma))
  493. return copy_hugetlb_page_range(dst_mm, src_mm, vma);
  494. dst_pgd = pgd_offset(dst_mm, addr);
  495. src_pgd = pgd_offset(src_mm, addr);
  496. do {
  497. next = pgd_addr_end(addr, end);
  498. if (pgd_none_or_clear_bad(src_pgd))
  499. continue;
  500. if (copy_pud_range(dst_mm, src_mm, dst_pgd, src_pgd,
  501. vma, addr, next))
  502. return -ENOMEM;
  503. } while (dst_pgd++, src_pgd++, addr = next, addr != end);
  504. return 0;
  505. }
  506. static unsigned long zap_pte_range(struct mmu_gather *tlb,
  507. struct vm_area_struct *vma, pmd_t *pmd,
  508. unsigned long addr, unsigned long end,
  509. long *zap_work, struct zap_details *details)
  510. {
  511. struct mm_struct *mm = tlb->mm;
  512. pte_t *pte;
  513. spinlock_t *ptl;
  514. int file_rss = 0;
  515. int anon_rss = 0;
  516. pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
  517. do {
  518. pte_t ptent = *pte;
  519. if (pte_none(ptent)) {
  520. (*zap_work)--;
  521. continue;
  522. }
  523. if (pte_present(ptent)) {
  524. struct page *page;
  525. unsigned long pfn;
  526. (*zap_work) -= PAGE_SIZE;
  527. pfn = pte_pfn(ptent);
  528. page = pfn_valid(pfn)? pfn_to_page(pfn): NULL;
  529. if (unlikely(vma->vm_flags & VM_UNPAGED)) {
  530. if (!page_is_anon(page, vma, addr))
  531. page = NULL;
  532. } else if (unlikely(!page))
  533. print_bad_pte(vma, ptent, addr);
  534. if (unlikely(details) && page) {
  535. /*
  536. * unmap_shared_mapping_pages() wants to
  537. * invalidate cache without truncating:
  538. * unmap shared but keep private pages.
  539. */
  540. if (details->check_mapping &&
  541. details->check_mapping != page->mapping)
  542. continue;
  543. /*
  544. * Each page->index must be checked when
  545. * invalidating or truncating nonlinear.
  546. */
  547. if (details->nonlinear_vma &&
  548. (page->index < details->first_index ||
  549. page->index > details->last_index))
  550. continue;
  551. }
  552. ptent = ptep_get_and_clear_full(mm, addr, pte,
  553. tlb->fullmm);
  554. tlb_remove_tlb_entry(tlb, pte, addr);
  555. if (unlikely(!page))
  556. continue;
  557. if (unlikely(details) && details->nonlinear_vma
  558. && linear_page_index(details->nonlinear_vma,
  559. addr) != page->index)
  560. set_pte_at(mm, addr, pte,
  561. pgoff_to_pte(page->index));
  562. if (PageAnon(page))
  563. anon_rss--;
  564. else {
  565. if (pte_dirty(ptent))
  566. set_page_dirty(page);
  567. if (pte_young(ptent))
  568. mark_page_accessed(page);
  569. file_rss--;
  570. }
  571. page_remove_rmap(page);
  572. tlb_remove_page(tlb, page);
  573. continue;
  574. }
  575. /*
  576. * If details->check_mapping, we leave swap entries;
  577. * if details->nonlinear_vma, we leave file entries.
  578. */
  579. if (unlikely(details))
  580. continue;
  581. if (!pte_file(ptent))
  582. free_swap_and_cache(pte_to_swp_entry(ptent));
  583. pte_clear_full(mm, addr, pte, tlb->fullmm);
  584. } while (pte++, addr += PAGE_SIZE, (addr != end && *zap_work > 0));
  585. add_mm_rss(mm, file_rss, anon_rss);
  586. pte_unmap_unlock(pte - 1, ptl);
  587. return addr;
  588. }
  589. static inline unsigned long zap_pmd_range(struct mmu_gather *tlb,
  590. struct vm_area_struct *vma, pud_t *pud,
  591. unsigned long addr, unsigned long end,
  592. long *zap_work, struct zap_details *details)
  593. {
  594. pmd_t *pmd;
  595. unsigned long next;
  596. pmd = pmd_offset(pud, addr);
  597. do {
  598. next = pmd_addr_end(addr, end);
  599. if (pmd_none_or_clear_bad(pmd)) {
  600. (*zap_work)--;
  601. continue;
  602. }
  603. next = zap_pte_range(tlb, vma, pmd, addr, next,
  604. zap_work, details);
  605. } while (pmd++, addr = next, (addr != end && *zap_work > 0));
  606. return addr;
  607. }
  608. static inline unsigned long zap_pud_range(struct mmu_gather *tlb,
  609. struct vm_area_struct *vma, pgd_t *pgd,
  610. unsigned long addr, unsigned long end,
  611. long *zap_work, struct zap_details *details)
  612. {
  613. pud_t *pud;
  614. unsigned long next;
  615. pud = pud_offset(pgd, addr);
  616. do {
  617. next = pud_addr_end(addr, end);
  618. if (pud_none_or_clear_bad(pud)) {
  619. (*zap_work)--;
  620. continue;
  621. }
  622. next = zap_pmd_range(tlb, vma, pud, addr, next,
  623. zap_work, details);
  624. } while (pud++, addr = next, (addr != end && *zap_work > 0));
  625. return addr;
  626. }
  627. static unsigned long unmap_page_range(struct mmu_gather *tlb,
  628. struct vm_area_struct *vma,
  629. unsigned long addr, unsigned long end,
  630. long *zap_work, struct zap_details *details)
  631. {
  632. pgd_t *pgd;
  633. unsigned long next;
  634. if (details && !details->check_mapping && !details->nonlinear_vma)
  635. details = NULL;
  636. BUG_ON(addr >= end);
  637. tlb_start_vma(tlb, vma);
  638. pgd = pgd_offset(vma->vm_mm, addr);
  639. do {
  640. next = pgd_addr_end(addr, end);
  641. if (pgd_none_or_clear_bad(pgd)) {
  642. (*zap_work)--;
  643. continue;
  644. }
  645. next = zap_pud_range(tlb, vma, pgd, addr, next,
  646. zap_work, details);
  647. } while (pgd++, addr = next, (addr != end && *zap_work > 0));
  648. tlb_end_vma(tlb, vma);
  649. return addr;
  650. }
  651. #ifdef CONFIG_PREEMPT
  652. # define ZAP_BLOCK_SIZE (8 * PAGE_SIZE)
  653. #else
  654. /* No preempt: go for improved straight-line efficiency */
  655. # define ZAP_BLOCK_SIZE (1024 * PAGE_SIZE)
  656. #endif
  657. /**
  658. * unmap_vmas - unmap a range of memory covered by a list of vma's
  659. * @tlbp: address of the caller's struct mmu_gather
  660. * @vma: the starting vma
  661. * @start_addr: virtual address at which to start unmapping
  662. * @end_addr: virtual address at which to end unmapping
  663. * @nr_accounted: Place number of unmapped pages in vm-accountable vma's here
  664. * @details: details of nonlinear truncation or shared cache invalidation
  665. *
  666. * Returns the end address of the unmapping (restart addr if interrupted).
  667. *
  668. * Unmap all pages in the vma list.
  669. *
  670. * We aim to not hold locks for too long (for scheduling latency reasons).
  671. * So zap pages in ZAP_BLOCK_SIZE bytecounts. This means we need to
  672. * return the ending mmu_gather to the caller.
  673. *
  674. * Only addresses between `start' and `end' will be unmapped.
  675. *
  676. * The VMA list must be sorted in ascending virtual address order.
  677. *
  678. * unmap_vmas() assumes that the caller will flush the whole unmapped address
  679. * range after unmap_vmas() returns. So the only responsibility here is to
  680. * ensure that any thus-far unmapped pages are flushed before unmap_vmas()
  681. * drops the lock and schedules.
  682. */
  683. unsigned long unmap_vmas(struct mmu_gather **tlbp,
  684. struct vm_area_struct *vma, unsigned long start_addr,
  685. unsigned long end_addr, unsigned long *nr_accounted,
  686. struct zap_details *details)
  687. {
  688. long zap_work = ZAP_BLOCK_SIZE;
  689. unsigned long tlb_start = 0; /* For tlb_finish_mmu */
  690. int tlb_start_valid = 0;
  691. unsigned long start = start_addr;
  692. spinlock_t *i_mmap_lock = details? details->i_mmap_lock: NULL;
  693. int fullmm = (*tlbp)->fullmm;
  694. for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next) {
  695. unsigned long end;
  696. start = max(vma->vm_start, start_addr);
  697. if (start >= vma->vm_end)
  698. continue;
  699. end = min(vma->vm_end, end_addr);
  700. if (end <= vma->vm_start)
  701. continue;
  702. if (vma->vm_flags & VM_ACCOUNT)
  703. *nr_accounted += (end - start) >> PAGE_SHIFT;
  704. while (start != end) {
  705. if (!tlb_start_valid) {
  706. tlb_start = start;
  707. tlb_start_valid = 1;
  708. }
  709. if (unlikely(is_vm_hugetlb_page(vma))) {
  710. unmap_hugepage_range(vma, start, end);
  711. zap_work -= (end - start) /
  712. (HPAGE_SIZE / PAGE_SIZE);
  713. start = end;
  714. } else
  715. start = unmap_page_range(*tlbp, vma,
  716. start, end, &zap_work, details);
  717. if (zap_work > 0) {
  718. BUG_ON(start != end);
  719. break;
  720. }
  721. tlb_finish_mmu(*tlbp, tlb_start, start);
  722. if (need_resched() ||
  723. (i_mmap_lock && need_lockbreak(i_mmap_lock))) {
  724. if (i_mmap_lock) {
  725. *tlbp = NULL;
  726. goto out;
  727. }
  728. cond_resched();
  729. }
  730. *tlbp = tlb_gather_mmu(vma->vm_mm, fullmm);
  731. tlb_start_valid = 0;
  732. zap_work = ZAP_BLOCK_SIZE;
  733. }
  734. }
  735. out:
  736. return start; /* which is now the end (or restart) address */
  737. }
  738. /**
  739. * zap_page_range - remove user pages in a given range
  740. * @vma: vm_area_struct holding the applicable pages
  741. * @address: starting address of pages to zap
  742. * @size: number of bytes to zap
  743. * @details: details of nonlinear truncation or shared cache invalidation
  744. */
  745. unsigned long zap_page_range(struct vm_area_struct *vma, unsigned long address,
  746. unsigned long size, struct zap_details *details)
  747. {
  748. struct mm_struct *mm = vma->vm_mm;
  749. struct mmu_gather *tlb;
  750. unsigned long end = address + size;
  751. unsigned long nr_accounted = 0;
  752. lru_add_drain();
  753. tlb = tlb_gather_mmu(mm, 0);
  754. update_hiwater_rss(mm);
  755. end = unmap_vmas(&tlb, vma, address, end, &nr_accounted, details);
  756. if (tlb)
  757. tlb_finish_mmu(tlb, address, end);
  758. return end;
  759. }
  760. /*
  761. * Do a quick page-table lookup for a single page.
  762. */
  763. struct page *follow_page(struct mm_struct *mm, unsigned long address,
  764. unsigned int flags)
  765. {
  766. pgd_t *pgd;
  767. pud_t *pud;
  768. pmd_t *pmd;
  769. pte_t *ptep, pte;
  770. spinlock_t *ptl;
  771. unsigned long pfn;
  772. struct page *page;
  773. page = follow_huge_addr(mm, address, flags & FOLL_WRITE);
  774. if (!IS_ERR(page)) {
  775. BUG_ON(flags & FOLL_GET);
  776. goto out;
  777. }
  778. page = NULL;
  779. pgd = pgd_offset(mm, address);
  780. if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
  781. goto no_page_table;
  782. pud = pud_offset(pgd, address);
  783. if (pud_none(*pud) || unlikely(pud_bad(*pud)))
  784. goto no_page_table;
  785. pmd = pmd_offset(pud, address);
  786. if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
  787. goto no_page_table;
  788. if (pmd_huge(*pmd)) {
  789. BUG_ON(flags & FOLL_GET);
  790. page = follow_huge_pmd(mm, address, pmd, flags & FOLL_WRITE);
  791. goto out;
  792. }
  793. ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
  794. if (!ptep)
  795. goto out;
  796. pte = *ptep;
  797. if (!pte_present(pte))
  798. goto unlock;
  799. if ((flags & FOLL_WRITE) && !pte_write(pte))
  800. goto unlock;
  801. pfn = pte_pfn(pte);
  802. if (!pfn_valid(pfn))
  803. goto unlock;
  804. page = pfn_to_page(pfn);
  805. if (flags & FOLL_GET)
  806. get_page(page);
  807. if (flags & FOLL_TOUCH) {
  808. if ((flags & FOLL_WRITE) &&
  809. !pte_dirty(pte) && !PageDirty(page))
  810. set_page_dirty(page);
  811. mark_page_accessed(page);
  812. }
  813. unlock:
  814. pte_unmap_unlock(ptep, ptl);
  815. out:
  816. return page;
  817. no_page_table:
  818. /*
  819. * When core dumping an enormous anonymous area that nobody
  820. * has touched so far, we don't want to allocate page tables.
  821. */
  822. if (flags & FOLL_ANON) {
  823. page = ZERO_PAGE(address);
  824. if (flags & FOLL_GET)
  825. get_page(page);
  826. BUG_ON(flags & FOLL_WRITE);
  827. }
  828. return page;
  829. }
  830. int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
  831. unsigned long start, int len, int write, int force,
  832. struct page **pages, struct vm_area_struct **vmas)
  833. {
  834. int i;
  835. unsigned int vm_flags;
  836. /*
  837. * Require read or write permissions.
  838. * If 'force' is set, we only require the "MAY" flags.
  839. */
  840. vm_flags = write ? (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
  841. vm_flags &= force ? (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
  842. i = 0;
  843. do {
  844. struct vm_area_struct *vma;
  845. unsigned int foll_flags;
  846. vma = find_extend_vma(mm, start);
  847. if (!vma && in_gate_area(tsk, start)) {
  848. unsigned long pg = start & PAGE_MASK;
  849. struct vm_area_struct *gate_vma = get_gate_vma(tsk);
  850. pgd_t *pgd;
  851. pud_t *pud;
  852. pmd_t *pmd;
  853. pte_t *pte;
  854. if (write) /* user gate pages are read-only */
  855. return i ? : -EFAULT;
  856. if (pg > TASK_SIZE)
  857. pgd = pgd_offset_k(pg);
  858. else
  859. pgd = pgd_offset_gate(mm, pg);
  860. BUG_ON(pgd_none(*pgd));
  861. pud = pud_offset(pgd, pg);
  862. BUG_ON(pud_none(*pud));
  863. pmd = pmd_offset(pud, pg);
  864. if (pmd_none(*pmd))
  865. return i ? : -EFAULT;
  866. pte = pte_offset_map(pmd, pg);
  867. if (pte_none(*pte)) {
  868. pte_unmap(pte);
  869. return i ? : -EFAULT;
  870. }
  871. if (pages) {
  872. pages[i] = pte_page(*pte);
  873. get_page(pages[i]);
  874. }
  875. pte_unmap(pte);
  876. if (vmas)
  877. vmas[i] = gate_vma;
  878. i++;
  879. start += PAGE_SIZE;
  880. len--;
  881. continue;
  882. }
  883. if (!vma || (vma->vm_flags & VM_IO)
  884. || !(vm_flags & vma->vm_flags))
  885. return i ? : -EFAULT;
  886. if (is_vm_hugetlb_page(vma)) {
  887. i = follow_hugetlb_page(mm, vma, pages, vmas,
  888. &start, &len, i);
  889. continue;
  890. }
  891. foll_flags = FOLL_TOUCH;
  892. if (pages)
  893. foll_flags |= FOLL_GET;
  894. if (!write && !(vma->vm_flags & VM_LOCKED) &&
  895. (!vma->vm_ops || !vma->vm_ops->nopage))
  896. foll_flags |= FOLL_ANON;
  897. do {
  898. struct page *page;
  899. if (write)
  900. foll_flags |= FOLL_WRITE;
  901. cond_resched();
  902. while (!(page = follow_page(mm, start, foll_flags))) {
  903. int ret;
  904. ret = __handle_mm_fault(mm, vma, start,
  905. foll_flags & FOLL_WRITE);
  906. /*
  907. * The VM_FAULT_WRITE bit tells us that do_wp_page has
  908. * broken COW when necessary, even if maybe_mkwrite
  909. * decided not to set pte_write. We can thus safely do
  910. * subsequent page lookups as if they were reads.
  911. */
  912. if (ret & VM_FAULT_WRITE)
  913. foll_flags &= ~FOLL_WRITE;
  914. switch (ret & ~VM_FAULT_WRITE) {
  915. case VM_FAULT_MINOR:
  916. tsk->min_flt++;
  917. break;
  918. case VM_FAULT_MAJOR:
  919. tsk->maj_flt++;
  920. break;
  921. case VM_FAULT_SIGBUS:
  922. return i ? i : -EFAULT;
  923. case VM_FAULT_OOM:
  924. return i ? i : -ENOMEM;
  925. default:
  926. BUG();
  927. }
  928. }
  929. if (pages) {
  930. pages[i] = page;
  931. flush_dcache_page(page);
  932. }
  933. if (vmas)
  934. vmas[i] = vma;
  935. i++;
  936. start += PAGE_SIZE;
  937. len--;
  938. } while (len && start < vma->vm_end);
  939. } while (len);
  940. return i;
  941. }
  942. EXPORT_SYMBOL(get_user_pages);
  943. static int zeromap_pte_range(struct mm_struct *mm, pmd_t *pmd,
  944. unsigned long addr, unsigned long end, pgprot_t prot)
  945. {
  946. pte_t *pte;
  947. spinlock_t *ptl;
  948. pte = pte_alloc_map_lock(mm, pmd, addr, &ptl);
  949. if (!pte)
  950. return -ENOMEM;
  951. do {
  952. struct page *page = ZERO_PAGE(addr);
  953. pte_t zero_pte = pte_wrprotect(mk_pte(page, prot));
  954. page_cache_get(page);
  955. page_add_file_rmap(page);
  956. inc_mm_counter(mm, file_rss);
  957. BUG_ON(!pte_none(*pte));
  958. set_pte_at(mm, addr, pte, zero_pte);
  959. } while (pte++, addr += PAGE_SIZE, addr != end);
  960. pte_unmap_unlock(pte - 1, ptl);
  961. return 0;
  962. }
  963. static inline int zeromap_pmd_range(struct mm_struct *mm, pud_t *pud,
  964. unsigned long addr, unsigned long end, pgprot_t prot)
  965. {
  966. pmd_t *pmd;
  967. unsigned long next;
  968. pmd = pmd_alloc(mm, pud, addr);
  969. if (!pmd)
  970. return -ENOMEM;
  971. do {
  972. next = pmd_addr_end(addr, end);
  973. if (zeromap_pte_range(mm, pmd, addr, next, prot))
  974. return -ENOMEM;
  975. } while (pmd++, addr = next, addr != end);
  976. return 0;
  977. }
  978. static inline int zeromap_pud_range(struct mm_struct *mm, pgd_t *pgd,
  979. unsigned long addr, unsigned long end, pgprot_t prot)
  980. {
  981. pud_t *pud;
  982. unsigned long next;
  983. pud = pud_alloc(mm, pgd, addr);
  984. if (!pud)
  985. return -ENOMEM;
  986. do {
  987. next = pud_addr_end(addr, end);
  988. if (zeromap_pmd_range(mm, pud, addr, next, prot))
  989. return -ENOMEM;
  990. } while (pud++, addr = next, addr != end);
  991. return 0;
  992. }
  993. int zeromap_page_range(struct vm_area_struct *vma,
  994. unsigned long addr, unsigned long size, pgprot_t prot)
  995. {
  996. pgd_t *pgd;
  997. unsigned long next;
  998. unsigned long end = addr + size;
  999. struct mm_struct *mm = vma->vm_mm;
  1000. int err;
  1001. BUG_ON(addr >= end);
  1002. pgd = pgd_offset(mm, addr);
  1003. flush_cache_range(vma, addr, end);
  1004. do {
  1005. next = pgd_addr_end(addr, end);
  1006. err = zeromap_pud_range(mm, pgd, addr, next, prot);
  1007. if (err)
  1008. break;
  1009. } while (pgd++, addr = next, addr != end);
  1010. return err;
  1011. }
  1012. /*
  1013. * maps a range of physical memory into the requested pages. the old
  1014. * mappings are removed. any references to nonexistent pages results
  1015. * in null mappings (currently treated as "copy-on-access")
  1016. */
  1017. static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd,
  1018. unsigned long addr, unsigned long end,
  1019. unsigned long pfn, pgprot_t prot)
  1020. {
  1021. pte_t *pte;
  1022. spinlock_t *ptl;
  1023. pte = pte_alloc_map_lock(mm, pmd, addr, &ptl);
  1024. if (!pte)
  1025. return -ENOMEM;
  1026. do {
  1027. BUG_ON(!pte_none(*pte));
  1028. set_pte_at(mm, addr, pte, pfn_pte(pfn, prot));
  1029. pfn++;
  1030. } while (pte++, addr += PAGE_SIZE, addr != end);
  1031. pte_unmap_unlock(pte - 1, ptl);
  1032. return 0;
  1033. }
  1034. static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud,
  1035. unsigned long addr, unsigned long end,
  1036. unsigned long pfn, pgprot_t prot)
  1037. {
  1038. pmd_t *pmd;
  1039. unsigned long next;
  1040. pfn -= addr >> PAGE_SHIFT;
  1041. pmd = pmd_alloc(mm, pud, addr);
  1042. if (!pmd)
  1043. return -ENOMEM;
  1044. do {
  1045. next = pmd_addr_end(addr, end);
  1046. if (remap_pte_range(mm, pmd, addr, next,
  1047. pfn + (addr >> PAGE_SHIFT), prot))
  1048. return -ENOMEM;
  1049. } while (pmd++, addr = next, addr != end);
  1050. return 0;
  1051. }
  1052. static inline int remap_pud_range(struct mm_struct *mm, pgd_t *pgd,
  1053. unsigned long addr, unsigned long end,
  1054. unsigned long pfn, pgprot_t prot)
  1055. {
  1056. pud_t *pud;
  1057. unsigned long next;
  1058. pfn -= addr >> PAGE_SHIFT;
  1059. pud = pud_alloc(mm, pgd, addr);
  1060. if (!pud)
  1061. return -ENOMEM;
  1062. do {
  1063. next = pud_addr_end(addr, end);
  1064. if (remap_pmd_range(mm, pud, addr, next,
  1065. pfn + (addr >> PAGE_SHIFT), prot))
  1066. return -ENOMEM;
  1067. } while (pud++, addr = next, addr != end);
  1068. return 0;
  1069. }
  1070. /* Note: this is only safe if the mm semaphore is held when called. */
  1071. int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
  1072. unsigned long pfn, unsigned long size, pgprot_t prot)
  1073. {
  1074. pgd_t *pgd;
  1075. unsigned long next;
  1076. unsigned long end = addr + PAGE_ALIGN(size);
  1077. struct mm_struct *mm = vma->vm_mm;
  1078. int err;
  1079. /*
  1080. * Physically remapped pages are special. Tell the
  1081. * rest of the world about it:
  1082. * VM_IO tells people not to look at these pages
  1083. * (accesses can have side effects).
  1084. * VM_RESERVED is specified all over the place, because
  1085. * in 2.4 it kept swapout's vma scan off this vma; but
  1086. * in 2.6 the LRU scan won't even find its pages, so this
  1087. * flag means no more than count its pages in reserved_vm,
  1088. * and omit it from core dump, even when VM_IO turned off.
  1089. * VM_UNPAGED tells the core MM not to "manage" these pages
  1090. * (e.g. refcount, mapcount, try to swap them out): in
  1091. * particular, zap_pte_range does not try to free them.
  1092. */
  1093. vma->vm_flags |= VM_IO | VM_RESERVED | VM_UNPAGED;
  1094. BUG_ON(addr >= end);
  1095. pfn -= addr >> PAGE_SHIFT;
  1096. pgd = pgd_offset(mm, addr);
  1097. flush_cache_range(vma, addr, end);
  1098. do {
  1099. next = pgd_addr_end(addr, end);
  1100. err = remap_pud_range(mm, pgd, addr, next,
  1101. pfn + (addr >> PAGE_SHIFT), prot);
  1102. if (err)
  1103. break;
  1104. } while (pgd++, addr = next, addr != end);
  1105. return err;
  1106. }
  1107. EXPORT_SYMBOL(remap_pfn_range);
  1108. /*
  1109. * handle_pte_fault chooses page fault handler according to an entry
  1110. * which was read non-atomically. Before making any commitment, on
  1111. * those architectures or configurations (e.g. i386 with PAE) which
  1112. * might give a mix of unmatched parts, do_swap_page and do_file_page
  1113. * must check under lock before unmapping the pte and proceeding
  1114. * (but do_wp_page is only called after already making such a check;
  1115. * and do_anonymous_page and do_no_page can safely check later on).
  1116. */
  1117. static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd,
  1118. pte_t *page_table, pte_t orig_pte)
  1119. {
  1120. int same = 1;
  1121. #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
  1122. if (sizeof(pte_t) > sizeof(unsigned long)) {
  1123. spinlock_t *ptl = pte_lockptr(mm, pmd);
  1124. spin_lock(ptl);
  1125. same = pte_same(*page_table, orig_pte);
  1126. spin_unlock(ptl);
  1127. }
  1128. #endif
  1129. pte_unmap(page_table);
  1130. return same;
  1131. }
  1132. /*
  1133. * Do pte_mkwrite, but only if the vma says VM_WRITE. We do this when
  1134. * servicing faults for write access. In the normal case, do always want
  1135. * pte_mkwrite. But get_user_pages can cause write faults for mappings
  1136. * that do not have writing enabled, when used by access_process_vm.
  1137. */
  1138. static inline pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma)
  1139. {
  1140. if (likely(vma->vm_flags & VM_WRITE))
  1141. pte = pte_mkwrite(pte);
  1142. return pte;
  1143. }
  1144. /*
  1145. * This routine handles present pages, when users try to write
  1146. * to a shared page. It is done by copying the page to a new address
  1147. * and decrementing the shared-page counter for the old page.
  1148. *
  1149. * Note that this routine assumes that the protection checks have been
  1150. * done by the caller (the low-level page fault routine in most cases).
  1151. * Thus we can safely just mark it writable once we've done any necessary
  1152. * COW.
  1153. *
  1154. * We also mark the page dirty at this point even though the page will
  1155. * change only once the write actually happens. This avoids a few races,
  1156. * and potentially makes it more efficient.
  1157. *
  1158. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  1159. * but allow concurrent faults), with pte both mapped and locked.
  1160. * We return with mmap_sem still held, but pte unmapped and unlocked.
  1161. */
  1162. static int do_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
  1163. unsigned long address, pte_t *page_table, pmd_t *pmd,
  1164. spinlock_t *ptl, pte_t orig_pte)
  1165. {
  1166. struct page *old_page, *src_page, *new_page;
  1167. unsigned long pfn = pte_pfn(orig_pte);
  1168. pte_t entry;
  1169. int ret = VM_FAULT_MINOR;
  1170. if (unlikely(!pfn_valid(pfn))) {
  1171. /*
  1172. * Page table corrupted: show pte and kill process.
  1173. * Or it's an attempt to COW an out-of-map VM_UNPAGED
  1174. * entry, which copy_user_highpage does not support.
  1175. */
  1176. print_bad_pte(vma, orig_pte, address);
  1177. ret = VM_FAULT_OOM;
  1178. goto unlock;
  1179. }
  1180. old_page = pfn_to_page(pfn);
  1181. src_page = old_page;
  1182. if (unlikely(vma->vm_flags & VM_UNPAGED))
  1183. if (!page_is_anon(old_page, vma, address)) {
  1184. old_page = NULL;
  1185. goto gotten;
  1186. }
  1187. if (PageAnon(old_page) && !TestSetPageLocked(old_page)) {
  1188. int reuse = can_share_swap_page(old_page);
  1189. unlock_page(old_page);
  1190. if (reuse) {
  1191. flush_cache_page(vma, address, pfn);
  1192. entry = pte_mkyoung(orig_pte);
  1193. entry = maybe_mkwrite(pte_mkdirty(entry), vma);
  1194. ptep_set_access_flags(vma, address, page_table, entry, 1);
  1195. update_mmu_cache(vma, address, entry);
  1196. lazy_mmu_prot_update(entry);
  1197. ret |= VM_FAULT_WRITE;
  1198. goto unlock;
  1199. }
  1200. }
  1201. /*
  1202. * Ok, we need to copy. Oh, well..
  1203. */
  1204. page_cache_get(old_page);
  1205. gotten:
  1206. pte_unmap_unlock(page_table, ptl);
  1207. if (unlikely(anon_vma_prepare(vma)))
  1208. goto oom;
  1209. if (src_page == ZERO_PAGE(address)) {
  1210. new_page = alloc_zeroed_user_highpage(vma, address);
  1211. if (!new_page)
  1212. goto oom;
  1213. } else {
  1214. new_page = alloc_page_vma(GFP_HIGHUSER, vma, address);
  1215. if (!new_page)
  1216. goto oom;
  1217. copy_user_highpage(new_page, src_page, address);
  1218. }
  1219. /*
  1220. * Re-check the pte - we dropped the lock
  1221. */
  1222. page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
  1223. if (likely(pte_same(*page_table, orig_pte))) {
  1224. if (old_page) {
  1225. page_remove_rmap(old_page);
  1226. if (!PageAnon(old_page)) {
  1227. dec_mm_counter(mm, file_rss);
  1228. inc_mm_counter(mm, anon_rss);
  1229. }
  1230. } else
  1231. inc_mm_counter(mm, anon_rss);
  1232. flush_cache_page(vma, address, pfn);
  1233. entry = mk_pte(new_page, vma->vm_page_prot);
  1234. entry = maybe_mkwrite(pte_mkdirty(entry), vma);
  1235. ptep_establish(vma, address, page_table, entry);
  1236. update_mmu_cache(vma, address, entry);
  1237. lazy_mmu_prot_update(entry);
  1238. lru_cache_add_active(new_page);
  1239. page_add_anon_rmap(new_page, vma, address);
  1240. /* Free the old page.. */
  1241. new_page = old_page;
  1242. ret |= VM_FAULT_WRITE;
  1243. }
  1244. if (new_page)
  1245. page_cache_release(new_page);
  1246. if (old_page)
  1247. page_cache_release(old_page);
  1248. unlock:
  1249. pte_unmap_unlock(page_table, ptl);
  1250. return ret;
  1251. oom:
  1252. if (old_page)
  1253. page_cache_release(old_page);
  1254. return VM_FAULT_OOM;
  1255. }
  1256. /*
  1257. * Helper functions for unmap_mapping_range().
  1258. *
  1259. * __ Notes on dropping i_mmap_lock to reduce latency while unmapping __
  1260. *
  1261. * We have to restart searching the prio_tree whenever we drop the lock,
  1262. * since the iterator is only valid while the lock is held, and anyway
  1263. * a later vma might be split and reinserted earlier while lock dropped.
  1264. *
  1265. * The list of nonlinear vmas could be handled more efficiently, using
  1266. * a placeholder, but handle it in the same way until a need is shown.
  1267. * It is important to search the prio_tree before nonlinear list: a vma
  1268. * may become nonlinear and be shifted from prio_tree to nonlinear list
  1269. * while the lock is dropped; but never shifted from list to prio_tree.
  1270. *
  1271. * In order to make forward progress despite restarting the search,
  1272. * vm_truncate_count is used to mark a vma as now dealt with, so we can
  1273. * quickly skip it next time around. Since the prio_tree search only
  1274. * shows us those vmas affected by unmapping the range in question, we
  1275. * can't efficiently keep all vmas in step with mapping->truncate_count:
  1276. * so instead reset them all whenever it wraps back to 0 (then go to 1).
  1277. * mapping->truncate_count and vma->vm_truncate_count are protected by
  1278. * i_mmap_lock.
  1279. *
  1280. * In order to make forward progress despite repeatedly restarting some
  1281. * large vma, note the restart_addr from unmap_vmas when it breaks out:
  1282. * and restart from that address when we reach that vma again. It might
  1283. * have been split or merged, shrunk or extended, but never shifted: so
  1284. * restart_addr remains valid so long as it remains in the vma's range.
  1285. * unmap_mapping_range forces truncate_count to leap over page-aligned
  1286. * values so we can save vma's restart_addr in its truncate_count field.
  1287. */
  1288. #define is_restart_addr(truncate_count) (!((truncate_count) & ~PAGE_MASK))
  1289. static void reset_vma_truncate_counts(struct address_space *mapping)
  1290. {
  1291. struct vm_area_struct *vma;
  1292. struct prio_tree_iter iter;
  1293. vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, 0, ULONG_MAX)
  1294. vma->vm_truncate_count = 0;
  1295. list_for_each_entry(vma, &mapping->i_mmap_nonlinear, shared.vm_set.list)
  1296. vma->vm_truncate_count = 0;
  1297. }
  1298. static int unmap_mapping_range_vma(struct vm_area_struct *vma,
  1299. unsigned long start_addr, unsigned long end_addr,
  1300. struct zap_details *details)
  1301. {
  1302. unsigned long restart_addr;
  1303. int need_break;
  1304. again:
  1305. restart_addr = vma->vm_truncate_count;
  1306. if (is_restart_addr(restart_addr) && start_addr < restart_addr) {
  1307. start_addr = restart_addr;
  1308. if (start_addr >= end_addr) {
  1309. /* Top of vma has been split off since last time */
  1310. vma->vm_truncate_count = details->truncate_count;
  1311. return 0;
  1312. }
  1313. }
  1314. restart_addr = zap_page_range(vma, start_addr,
  1315. end_addr - start_addr, details);
  1316. need_break = need_resched() ||
  1317. need_lockbreak(details->i_mmap_lock);
  1318. if (restart_addr >= end_addr) {
  1319. /* We have now completed this vma: mark it so */
  1320. vma->vm_truncate_count = details->truncate_count;
  1321. if (!need_break)
  1322. return 0;
  1323. } else {
  1324. /* Note restart_addr in vma's truncate_count field */
  1325. vma->vm_truncate_count = restart_addr;
  1326. if (!need_break)
  1327. goto again;
  1328. }
  1329. spin_unlock(details->i_mmap_lock);
  1330. cond_resched();
  1331. spin_lock(details->i_mmap_lock);
  1332. return -EINTR;
  1333. }
  1334. static inline void unmap_mapping_range_tree(struct prio_tree_root *root,
  1335. struct zap_details *details)
  1336. {
  1337. struct vm_area_struct *vma;
  1338. struct prio_tree_iter iter;
  1339. pgoff_t vba, vea, zba, zea;
  1340. restart:
  1341. vma_prio_tree_foreach(vma, &iter, root,
  1342. details->first_index, details->last_index) {
  1343. /* Skip quickly over those we have already dealt with */
  1344. if (vma->vm_truncate_count == details->truncate_count)
  1345. continue;
  1346. vba = vma->vm_pgoff;
  1347. vea = vba + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) - 1;
  1348. /* Assume for now that PAGE_CACHE_SHIFT == PAGE_SHIFT */
  1349. zba = details->first_index;
  1350. if (zba < vba)
  1351. zba = vba;
  1352. zea = details->last_index;
  1353. if (zea > vea)
  1354. zea = vea;
  1355. if (unmap_mapping_range_vma(vma,
  1356. ((zba - vba) << PAGE_SHIFT) + vma->vm_start,
  1357. ((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start,
  1358. details) < 0)
  1359. goto restart;
  1360. }
  1361. }
  1362. static inline void unmap_mapping_range_list(struct list_head *head,
  1363. struct zap_details *details)
  1364. {
  1365. struct vm_area_struct *vma;
  1366. /*
  1367. * In nonlinear VMAs there is no correspondence between virtual address
  1368. * offset and file offset. So we must perform an exhaustive search
  1369. * across *all* the pages in each nonlinear VMA, not just the pages
  1370. * whose virtual address lies outside the file truncation point.
  1371. */
  1372. restart:
  1373. list_for_each_entry(vma, head, shared.vm_set.list) {
  1374. /* Skip quickly over those we have already dealt with */
  1375. if (vma->vm_truncate_count == details->truncate_count)
  1376. continue;
  1377. details->nonlinear_vma = vma;
  1378. if (unmap_mapping_range_vma(vma, vma->vm_start,
  1379. vma->vm_end, details) < 0)
  1380. goto restart;
  1381. }
  1382. }
  1383. /**
  1384. * unmap_mapping_range - unmap the portion of all mmaps
  1385. * in the specified address_space corresponding to the specified
  1386. * page range in the underlying file.
  1387. * @mapping: the address space containing mmaps to be unmapped.
  1388. * @holebegin: byte in first page to unmap, relative to the start of
  1389. * the underlying file. This will be rounded down to a PAGE_SIZE
  1390. * boundary. Note that this is different from vmtruncate(), which
  1391. * must keep the partial page. In contrast, we must get rid of
  1392. * partial pages.
  1393. * @holelen: size of prospective hole in bytes. This will be rounded
  1394. * up to a PAGE_SIZE boundary. A holelen of zero truncates to the
  1395. * end of the file.
  1396. * @even_cows: 1 when truncating a file, unmap even private COWed pages;
  1397. * but 0 when invalidating pagecache, don't throw away private data.
  1398. */
  1399. void unmap_mapping_range(struct address_space *mapping,
  1400. loff_t const holebegin, loff_t const holelen, int even_cows)
  1401. {
  1402. struct zap_details details;
  1403. pgoff_t hba = holebegin >> PAGE_SHIFT;
  1404. pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1405. /* Check for overflow. */
  1406. if (sizeof(holelen) > sizeof(hlen)) {
  1407. long long holeend =
  1408. (holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1409. if (holeend & ~(long long)ULONG_MAX)
  1410. hlen = ULONG_MAX - hba + 1;
  1411. }
  1412. details.check_mapping = even_cows? NULL: mapping;
  1413. details.nonlinear_vma = NULL;
  1414. details.first_index = hba;
  1415. details.last_index = hba + hlen - 1;
  1416. if (details.last_index < details.first_index)
  1417. details.last_index = ULONG_MAX;
  1418. details.i_mmap_lock = &mapping->i_mmap_lock;
  1419. spin_lock(&mapping->i_mmap_lock);
  1420. /* serialize i_size write against truncate_count write */
  1421. smp_wmb();
  1422. /* Protect against page faults, and endless unmapping loops */
  1423. mapping->truncate_count++;
  1424. /*
  1425. * For archs where spin_lock has inclusive semantics like ia64
  1426. * this smp_mb() will prevent to read pagetable contents
  1427. * before the truncate_count increment is visible to
  1428. * other cpus.
  1429. */
  1430. smp_mb();
  1431. if (unlikely(is_restart_addr(mapping->truncate_count))) {
  1432. if (mapping->truncate_count == 0)
  1433. reset_vma_truncate_counts(mapping);
  1434. mapping->truncate_count++;
  1435. }
  1436. details.truncate_count = mapping->truncate_count;
  1437. if (unlikely(!prio_tree_empty(&mapping->i_mmap)))
  1438. unmap_mapping_range_tree(&mapping->i_mmap, &details);
  1439. if (unlikely(!list_empty(&mapping->i_mmap_nonlinear)))
  1440. unmap_mapping_range_list(&mapping->i_mmap_nonlinear, &details);
  1441. spin_unlock(&mapping->i_mmap_lock);
  1442. }
  1443. EXPORT_SYMBOL(unmap_mapping_range);
  1444. /*
  1445. * Handle all mappings that got truncated by a "truncate()"
  1446. * system call.
  1447. *
  1448. * NOTE! We have to be ready to update the memory sharing
  1449. * between the file and the memory map for a potential last
  1450. * incomplete page. Ugly, but necessary.
  1451. */
  1452. int vmtruncate(struct inode * inode, loff_t offset)
  1453. {
  1454. struct address_space *mapping = inode->i_mapping;
  1455. unsigned long limit;
  1456. if (inode->i_size < offset)
  1457. goto do_expand;
  1458. /*
  1459. * truncation of in-use swapfiles is disallowed - it would cause
  1460. * subsequent swapout to scribble on the now-freed blocks.
  1461. */
  1462. if (IS_SWAPFILE(inode))
  1463. goto out_busy;
  1464. i_size_write(inode, offset);
  1465. unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
  1466. truncate_inode_pages(mapping, offset);
  1467. goto out_truncate;
  1468. do_expand:
  1469. limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
  1470. if (limit != RLIM_INFINITY && offset > limit)
  1471. goto out_sig;
  1472. if (offset > inode->i_sb->s_maxbytes)
  1473. goto out_big;
  1474. i_size_write(inode, offset);
  1475. out_truncate:
  1476. if (inode->i_op && inode->i_op->truncate)
  1477. inode->i_op->truncate(inode);
  1478. return 0;
  1479. out_sig:
  1480. send_sig(SIGXFSZ, current, 0);
  1481. out_big:
  1482. return -EFBIG;
  1483. out_busy:
  1484. return -ETXTBSY;
  1485. }
  1486. EXPORT_SYMBOL(vmtruncate);
  1487. /*
  1488. * Primitive swap readahead code. We simply read an aligned block of
  1489. * (1 << page_cluster) entries in the swap area. This method is chosen
  1490. * because it doesn't cost us any seek time. We also make sure to queue
  1491. * the 'original' request together with the readahead ones...
  1492. *
  1493. * This has been extended to use the NUMA policies from the mm triggering
  1494. * the readahead.
  1495. *
  1496. * Caller must hold down_read on the vma->vm_mm if vma is not NULL.
  1497. */
  1498. void swapin_readahead(swp_entry_t entry, unsigned long addr,struct vm_area_struct *vma)
  1499. {
  1500. #ifdef CONFIG_NUMA
  1501. struct vm_area_struct *next_vma = vma ? vma->vm_next : NULL;
  1502. #endif
  1503. int i, num;
  1504. struct page *new_page;
  1505. unsigned long offset;
  1506. /*
  1507. * Get the number of handles we should do readahead io to.
  1508. */
  1509. num = valid_swaphandles(entry, &offset);
  1510. for (i = 0; i < num; offset++, i++) {
  1511. /* Ok, do the async read-ahead now */
  1512. new_page = read_swap_cache_async(swp_entry(swp_type(entry),
  1513. offset), vma, addr);
  1514. if (!new_page)
  1515. break;
  1516. page_cache_release(new_page);
  1517. #ifdef CONFIG_NUMA
  1518. /*
  1519. * Find the next applicable VMA for the NUMA policy.
  1520. */
  1521. addr += PAGE_SIZE;
  1522. if (addr == 0)
  1523. vma = NULL;
  1524. if (vma) {
  1525. if (addr >= vma->vm_end) {
  1526. vma = next_vma;
  1527. next_vma = vma ? vma->vm_next : NULL;
  1528. }
  1529. if (vma && addr < vma->vm_start)
  1530. vma = NULL;
  1531. } else {
  1532. if (next_vma && addr >= next_vma->vm_start) {
  1533. vma = next_vma;
  1534. next_vma = vma->vm_next;
  1535. }
  1536. }
  1537. #endif
  1538. }
  1539. lru_add_drain(); /* Push any new pages onto the LRU now */
  1540. }
  1541. /*
  1542. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  1543. * but allow concurrent faults), and pte mapped but not yet locked.
  1544. * We return with mmap_sem still held, but pte unmapped and unlocked.
  1545. */
  1546. static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma,
  1547. unsigned long address, pte_t *page_table, pmd_t *pmd,
  1548. int write_access, pte_t orig_pte)
  1549. {
  1550. spinlock_t *ptl;
  1551. struct page *page;
  1552. swp_entry_t entry;
  1553. pte_t pte;
  1554. int ret = VM_FAULT_MINOR;
  1555. if (!pte_unmap_same(mm, pmd, page_table, orig_pte))
  1556. goto out;
  1557. entry = pte_to_swp_entry(orig_pte);
  1558. page = lookup_swap_cache(entry);
  1559. if (!page) {
  1560. swapin_readahead(entry, address, vma);
  1561. page = read_swap_cache_async(entry, vma, address);
  1562. if (!page) {
  1563. /*
  1564. * Back out if somebody else faulted in this pte
  1565. * while we released the pte lock.
  1566. */
  1567. page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
  1568. if (likely(pte_same(*page_table, orig_pte)))
  1569. ret = VM_FAULT_OOM;
  1570. goto unlock;
  1571. }
  1572. /* Had to read the page from swap area: Major fault */
  1573. ret = VM_FAULT_MAJOR;
  1574. inc_page_state(pgmajfault);
  1575. grab_swap_token();
  1576. }
  1577. mark_page_accessed(page);
  1578. lock_page(page);
  1579. /*
  1580. * Back out if somebody else already faulted in this pte.
  1581. */
  1582. page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
  1583. if (unlikely(!pte_same(*page_table, orig_pte)))
  1584. goto out_nomap;
  1585. if (unlikely(!PageUptodate(page))) {
  1586. ret = VM_FAULT_SIGBUS;
  1587. goto out_nomap;
  1588. }
  1589. /* The page isn't present yet, go ahead with the fault. */
  1590. inc_mm_counter(mm, anon_rss);
  1591. pte = mk_pte(page, vma->vm_page_prot);
  1592. if (write_access && can_share_swap_page(page)) {
  1593. pte = maybe_mkwrite(pte_mkdirty(pte), vma);
  1594. write_access = 0;
  1595. }
  1596. flush_icache_page(vma, page);
  1597. set_pte_at(mm, address, page_table, pte);
  1598. page_add_anon_rmap(page, vma, address);
  1599. swap_free(entry);
  1600. if (vm_swap_full())
  1601. remove_exclusive_swap_page(page);
  1602. unlock_page(page);
  1603. if (write_access) {
  1604. if (do_wp_page(mm, vma, address,
  1605. page_table, pmd, ptl, pte) == VM_FAULT_OOM)
  1606. ret = VM_FAULT_OOM;
  1607. goto out;
  1608. }
  1609. /* No need to invalidate - it was non-present before */
  1610. update_mmu_cache(vma, address, pte);
  1611. lazy_mmu_prot_update(pte);
  1612. unlock:
  1613. pte_unmap_unlock(page_table, ptl);
  1614. out:
  1615. return ret;
  1616. out_nomap:
  1617. pte_unmap_unlock(page_table, ptl);
  1618. unlock_page(page);
  1619. page_cache_release(page);
  1620. return ret;
  1621. }
  1622. /*
  1623. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  1624. * but allow concurrent faults), and pte mapped but not yet locked.
  1625. * We return with mmap_sem still held, but pte unmapped and unlocked.
  1626. */
  1627. static int do_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
  1628. unsigned long address, pte_t *page_table, pmd_t *pmd,
  1629. int write_access)
  1630. {
  1631. struct page *page;
  1632. spinlock_t *ptl;
  1633. pte_t entry;
  1634. /*
  1635. * A VM_UNPAGED vma will normally be filled with present ptes
  1636. * by remap_pfn_range, and never arrive here; but it might have
  1637. * holes, or if !VM_DONTEXPAND, mremap might have expanded it.
  1638. * It's weird enough handling anon pages in unpaged vmas, we do
  1639. * not want to worry about ZERO_PAGEs too (it may or may not
  1640. * matter if their counts wrap): just give them anon pages.
  1641. */
  1642. if (write_access || (vma->vm_flags & VM_UNPAGED)) {
  1643. /* Allocate our own private page. */
  1644. pte_unmap(page_table);
  1645. if (unlikely(anon_vma_prepare(vma)))
  1646. goto oom;
  1647. page = alloc_zeroed_user_highpage(vma, address);
  1648. if (!page)
  1649. goto oom;
  1650. entry = mk_pte(page, vma->vm_page_prot);
  1651. entry = maybe_mkwrite(pte_mkdirty(entry), vma);
  1652. page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
  1653. if (!pte_none(*page_table))
  1654. goto release;
  1655. inc_mm_counter(mm, anon_rss);
  1656. lru_cache_add_active(page);
  1657. SetPageReferenced(page);
  1658. page_add_anon_rmap(page, vma, address);
  1659. } else {
  1660. /* Map the ZERO_PAGE - vm_page_prot is readonly */
  1661. page = ZERO_PAGE(address);
  1662. page_cache_get(page);
  1663. entry = mk_pte(page, vma->vm_page_prot);
  1664. ptl = pte_lockptr(mm, pmd);
  1665. spin_lock(ptl);
  1666. if (!pte_none(*page_table))
  1667. goto release;
  1668. inc_mm_counter(mm, file_rss);
  1669. page_add_file_rmap(page);
  1670. }
  1671. set_pte_at(mm, address, page_table, entry);
  1672. /* No need to invalidate - it was non-present before */
  1673. update_mmu_cache(vma, address, entry);
  1674. lazy_mmu_prot_update(entry);
  1675. unlock:
  1676. pte_unmap_unlock(page_table, ptl);
  1677. return VM_FAULT_MINOR;
  1678. release:
  1679. page_cache_release(page);
  1680. goto unlock;
  1681. oom:
  1682. return VM_FAULT_OOM;
  1683. }
  1684. /*
  1685. * do_no_page() tries to create a new page mapping. It aggressively
  1686. * tries to share with existing pages, but makes a separate copy if
  1687. * the "write_access" parameter is true in order to avoid the next
  1688. * page fault.
  1689. *
  1690. * As this is called only for pages that do not currently exist, we
  1691. * do not need to flush old virtual caches or the TLB.
  1692. *
  1693. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  1694. * but allow concurrent faults), and pte mapped but not yet locked.
  1695. * We return with mmap_sem still held, but pte unmapped and unlocked.
  1696. */
  1697. static int do_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
  1698. unsigned long address, pte_t *page_table, pmd_t *pmd,
  1699. int write_access)
  1700. {
  1701. spinlock_t *ptl;
  1702. struct page *new_page;
  1703. struct address_space *mapping = NULL;
  1704. pte_t entry;
  1705. unsigned int sequence = 0;
  1706. int ret = VM_FAULT_MINOR;
  1707. int anon = 0;
  1708. pte_unmap(page_table);
  1709. BUG_ON(vma->vm_flags & VM_UNPAGED);
  1710. if (vma->vm_file) {
  1711. mapping = vma->vm_file->f_mapping;
  1712. sequence = mapping->truncate_count;
  1713. smp_rmb(); /* serializes i_size against truncate_count */
  1714. }
  1715. retry:
  1716. new_page = vma->vm_ops->nopage(vma, address & PAGE_MASK, &ret);
  1717. /*
  1718. * No smp_rmb is needed here as long as there's a full
  1719. * spin_lock/unlock sequence inside the ->nopage callback
  1720. * (for the pagecache lookup) that acts as an implicit
  1721. * smp_mb() and prevents the i_size read to happen
  1722. * after the next truncate_count read.
  1723. */
  1724. /* no page was available -- either SIGBUS or OOM */
  1725. if (new_page == NOPAGE_SIGBUS)
  1726. return VM_FAULT_SIGBUS;
  1727. if (new_page == NOPAGE_OOM)
  1728. return VM_FAULT_OOM;
  1729. /*
  1730. * Should we do an early C-O-W break?
  1731. */
  1732. if (write_access && !(vma->vm_flags & VM_SHARED)) {
  1733. struct page *page;
  1734. if (unlikely(anon_vma_prepare(vma)))
  1735. goto oom;
  1736. page = alloc_page_vma(GFP_HIGHUSER, vma, address);
  1737. if (!page)
  1738. goto oom;
  1739. copy_user_highpage(page, new_page, address);
  1740. page_cache_release(new_page);
  1741. new_page = page;
  1742. anon = 1;
  1743. }
  1744. page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
  1745. /*
  1746. * For a file-backed vma, someone could have truncated or otherwise
  1747. * invalidated this page. If unmap_mapping_range got called,
  1748. * retry getting the page.
  1749. */
  1750. if (mapping && unlikely(sequence != mapping->truncate_count)) {
  1751. pte_unmap_unlock(page_table, ptl);
  1752. page_cache_release(new_page);
  1753. cond_resched();
  1754. sequence = mapping->truncate_count;
  1755. smp_rmb();
  1756. goto retry;
  1757. }
  1758. /*
  1759. * This silly early PAGE_DIRTY setting removes a race
  1760. * due to the bad i386 page protection. But it's valid
  1761. * for other architectures too.
  1762. *
  1763. * Note that if write_access is true, we either now have
  1764. * an exclusive copy of the page, or this is a shared mapping,
  1765. * so we can make it writable and dirty to avoid having to
  1766. * handle that later.
  1767. */
  1768. /* Only go through if we didn't race with anybody else... */
  1769. if (pte_none(*page_table)) {
  1770. flush_icache_page(vma, new_page);
  1771. entry = mk_pte(new_page, vma->vm_page_prot);
  1772. if (write_access)
  1773. entry = maybe_mkwrite(pte_mkdirty(entry), vma);
  1774. set_pte_at(mm, address, page_table, entry);
  1775. if (anon) {
  1776. inc_mm_counter(mm, anon_rss);
  1777. lru_cache_add_active(new_page);
  1778. page_add_anon_rmap(new_page, vma, address);
  1779. } else {
  1780. inc_mm_counter(mm, file_rss);
  1781. page_add_file_rmap(new_page);
  1782. }
  1783. } else {
  1784. /* One of our sibling threads was faster, back out. */
  1785. page_cache_release(new_page);
  1786. goto unlock;
  1787. }
  1788. /* no need to invalidate: a not-present page shouldn't be cached */
  1789. update_mmu_cache(vma, address, entry);
  1790. lazy_mmu_prot_update(entry);
  1791. unlock:
  1792. pte_unmap_unlock(page_table, ptl);
  1793. return ret;
  1794. oom:
  1795. page_cache_release(new_page);
  1796. return VM_FAULT_OOM;
  1797. }
  1798. /*
  1799. * Fault of a previously existing named mapping. Repopulate the pte
  1800. * from the encoded file_pte if possible. This enables swappable
  1801. * nonlinear vmas.
  1802. *
  1803. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  1804. * but allow concurrent faults), and pte mapped but not yet locked.
  1805. * We return with mmap_sem still held, but pte unmapped and unlocked.
  1806. */
  1807. static int do_file_page(struct mm_struct *mm, struct vm_area_struct *vma,
  1808. unsigned long address, pte_t *page_table, pmd_t *pmd,
  1809. int write_access, pte_t orig_pte)
  1810. {
  1811. pgoff_t pgoff;
  1812. int err;
  1813. if (!pte_unmap_same(mm, pmd, page_table, orig_pte))
  1814. return VM_FAULT_MINOR;
  1815. if (unlikely(!(vma->vm_flags & VM_NONLINEAR))) {
  1816. /*
  1817. * Page table corrupted: show pte and kill process.
  1818. */
  1819. print_bad_pte(vma, orig_pte, address);
  1820. return VM_FAULT_OOM;
  1821. }
  1822. /* We can then assume vm->vm_ops && vma->vm_ops->populate */
  1823. pgoff = pte_to_pgoff(orig_pte);
  1824. err = vma->vm_ops->populate(vma, address & PAGE_MASK, PAGE_SIZE,
  1825. vma->vm_page_prot, pgoff, 0);
  1826. if (err == -ENOMEM)
  1827. return VM_FAULT_OOM;
  1828. if (err)
  1829. return VM_FAULT_SIGBUS;
  1830. return VM_FAULT_MAJOR;
  1831. }
  1832. /*
  1833. * These routines also need to handle stuff like marking pages dirty
  1834. * and/or accessed for architectures that don't do it in hardware (most
  1835. * RISC architectures). The early dirtying is also good on the i386.
  1836. *
  1837. * There is also a hook called "update_mmu_cache()" that architectures
  1838. * with external mmu caches can use to update those (ie the Sparc or
  1839. * PowerPC hashed page tables that act as extended TLBs).
  1840. *
  1841. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  1842. * but allow concurrent faults), and pte mapped but not yet locked.
  1843. * We return with mmap_sem still held, but pte unmapped and unlocked.
  1844. */
  1845. static inline int handle_pte_fault(struct mm_struct *mm,
  1846. struct vm_area_struct *vma, unsigned long address,
  1847. pte_t *pte, pmd_t *pmd, int write_access)
  1848. {
  1849. pte_t entry;
  1850. pte_t old_entry;
  1851. spinlock_t *ptl;
  1852. old_entry = entry = *pte;
  1853. if (!pte_present(entry)) {
  1854. if (pte_none(entry)) {
  1855. if (!vma->vm_ops || !vma->vm_ops->nopage)
  1856. return do_anonymous_page(mm, vma, address,
  1857. pte, pmd, write_access);
  1858. return do_no_page(mm, vma, address,
  1859. pte, pmd, write_access);
  1860. }
  1861. if (pte_file(entry))
  1862. return do_file_page(mm, vma, address,
  1863. pte, pmd, write_access, entry);
  1864. return do_swap_page(mm, vma, address,
  1865. pte, pmd, write_access, entry);
  1866. }
  1867. ptl = pte_lockptr(mm, pmd);
  1868. spin_lock(ptl);
  1869. if (unlikely(!pte_same(*pte, entry)))
  1870. goto unlock;
  1871. if (write_access) {
  1872. if (!pte_write(entry))
  1873. return do_wp_page(mm, vma, address,
  1874. pte, pmd, ptl, entry);
  1875. entry = pte_mkdirty(entry);
  1876. }
  1877. entry = pte_mkyoung(entry);
  1878. if (!pte_same(old_entry, entry)) {
  1879. ptep_set_access_flags(vma, address, pte, entry, write_access);
  1880. update_mmu_cache(vma, address, entry);
  1881. lazy_mmu_prot_update(entry);
  1882. } else {
  1883. /*
  1884. * This is needed only for protection faults but the arch code
  1885. * is not yet telling us if this is a protection fault or not.
  1886. * This still avoids useless tlb flushes for .text page faults
  1887. * with threads.
  1888. */
  1889. if (write_access)
  1890. flush_tlb_page(vma, address);
  1891. }
  1892. unlock:
  1893. pte_unmap_unlock(pte, ptl);
  1894. return VM_FAULT_MINOR;
  1895. }
  1896. /*
  1897. * By the time we get here, we already hold the mm semaphore
  1898. */
  1899. int __handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma,
  1900. unsigned long address, int write_access)
  1901. {
  1902. pgd_t *pgd;
  1903. pud_t *pud;
  1904. pmd_t *pmd;
  1905. pte_t *pte;
  1906. __set_current_state(TASK_RUNNING);
  1907. inc_page_state(pgfault);
  1908. if (unlikely(is_vm_hugetlb_page(vma)))
  1909. return hugetlb_fault(mm, vma, address, write_access);
  1910. pgd = pgd_offset(mm, address);
  1911. pud = pud_alloc(mm, pgd, address);
  1912. if (!pud)
  1913. return VM_FAULT_OOM;
  1914. pmd = pmd_alloc(mm, pud, address);
  1915. if (!pmd)
  1916. return VM_FAULT_OOM;
  1917. pte = pte_alloc_map(mm, pmd, address);
  1918. if (!pte)
  1919. return VM_FAULT_OOM;
  1920. return handle_pte_fault(mm, vma, address, pte, pmd, write_access);
  1921. }
  1922. #ifndef __PAGETABLE_PUD_FOLDED
  1923. /*
  1924. * Allocate page upper directory.
  1925. * We've already handled the fast-path in-line.
  1926. */
  1927. int __pud_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
  1928. {
  1929. pud_t *new = pud_alloc_one(mm, address);
  1930. if (!new)
  1931. return -ENOMEM;
  1932. spin_lock(&mm->page_table_lock);
  1933. if (pgd_present(*pgd)) /* Another has populated it */
  1934. pud_free(new);
  1935. else
  1936. pgd_populate(mm, pgd, new);
  1937. spin_unlock(&mm->page_table_lock);
  1938. return 0;
  1939. }
  1940. #endif /* __PAGETABLE_PUD_FOLDED */
  1941. #ifndef __PAGETABLE_PMD_FOLDED
  1942. /*
  1943. * Allocate page middle directory.
  1944. * We've already handled the fast-path in-line.
  1945. */
  1946. int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address)
  1947. {
  1948. pmd_t *new = pmd_alloc_one(mm, address);
  1949. if (!new)
  1950. return -ENOMEM;
  1951. spin_lock(&mm->page_table_lock);
  1952. #ifndef __ARCH_HAS_4LEVEL_HACK
  1953. if (pud_present(*pud)) /* Another has populated it */
  1954. pmd_free(new);
  1955. else
  1956. pud_populate(mm, pud, new);
  1957. #else
  1958. if (pgd_present(*pud)) /* Another has populated it */
  1959. pmd_free(new);
  1960. else
  1961. pgd_populate(mm, pud, new);
  1962. #endif /* __ARCH_HAS_4LEVEL_HACK */
  1963. spin_unlock(&mm->page_table_lock);
  1964. return 0;
  1965. }
  1966. #endif /* __PAGETABLE_PMD_FOLDED */
  1967. int make_pages_present(unsigned long addr, unsigned long end)
  1968. {
  1969. int ret, len, write;
  1970. struct vm_area_struct * vma;
  1971. vma = find_vma(current->mm, addr);
  1972. if (!vma)
  1973. return -1;
  1974. write = (vma->vm_flags & VM_WRITE) != 0;
  1975. if (addr >= end)
  1976. BUG();
  1977. if (end > vma->vm_end)
  1978. BUG();
  1979. len = (end+PAGE_SIZE-1)/PAGE_SIZE-addr/PAGE_SIZE;
  1980. ret = get_user_pages(current, current->mm, addr,
  1981. len, write, 0, NULL, NULL);
  1982. if (ret < 0)
  1983. return ret;
  1984. return ret == len ? 0 : -1;
  1985. }
  1986. /*
  1987. * Map a vmalloc()-space virtual address to the physical page.
  1988. */
  1989. struct page * vmalloc_to_page(void * vmalloc_addr)
  1990. {
  1991. unsigned long addr = (unsigned long) vmalloc_addr;
  1992. struct page *page = NULL;
  1993. pgd_t *pgd = pgd_offset_k(addr);
  1994. pud_t *pud;
  1995. pmd_t *pmd;
  1996. pte_t *ptep, pte;
  1997. if (!pgd_none(*pgd)) {
  1998. pud = pud_offset(pgd, addr);
  1999. if (!pud_none(*pud)) {
  2000. pmd = pmd_offset(pud, addr);
  2001. if (!pmd_none(*pmd)) {
  2002. ptep = pte_offset_map(pmd, addr);
  2003. pte = *ptep;
  2004. if (pte_present(pte))
  2005. page = pte_page(pte);
  2006. pte_unmap(ptep);
  2007. }
  2008. }
  2009. }
  2010. return page;
  2011. }
  2012. EXPORT_SYMBOL(vmalloc_to_page);
  2013. /*
  2014. * Map a vmalloc()-space virtual address to the physical page frame number.
  2015. */
  2016. unsigned long vmalloc_to_pfn(void * vmalloc_addr)
  2017. {
  2018. return page_to_pfn(vmalloc_to_page(vmalloc_addr));
  2019. }
  2020. EXPORT_SYMBOL(vmalloc_to_pfn);
  2021. #if !defined(__HAVE_ARCH_GATE_AREA)
  2022. #if defined(AT_SYSINFO_EHDR)
  2023. static struct vm_area_struct gate_vma;
  2024. static int __init gate_vma_init(void)
  2025. {
  2026. gate_vma.vm_mm = NULL;
  2027. gate_vma.vm_start = FIXADDR_USER_START;
  2028. gate_vma.vm_end = FIXADDR_USER_END;
  2029. gate_vma.vm_page_prot = PAGE_READONLY;
  2030. gate_vma.vm_flags = 0;
  2031. return 0;
  2032. }
  2033. __initcall(gate_vma_init);
  2034. #endif
  2035. struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
  2036. {
  2037. #ifdef AT_SYSINFO_EHDR
  2038. return &gate_vma;
  2039. #else
  2040. return NULL;
  2041. #endif
  2042. }
  2043. int in_gate_area_no_task(unsigned long addr)
  2044. {
  2045. #ifdef AT_SYSINFO_EHDR
  2046. if ((addr >= FIXADDR_USER_START) && (addr < FIXADDR_USER_END))
  2047. return 1;
  2048. #endif
  2049. return 0;
  2050. }
  2051. #endif /* __HAVE_ARCH_GATE_AREA */