memory.c 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869
  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/delayacct.h>
  46. #include <linux/init.h>
  47. #include <linux/writeback.h>
  48. #include <asm/pgalloc.h>
  49. #include <asm/uaccess.h>
  50. #include <asm/tlb.h>
  51. #include <asm/tlbflush.h>
  52. #include <asm/pgtable.h>
  53. #include <linux/swapops.h>
  54. #include <linux/elf.h>
  55. #ifndef CONFIG_NEED_MULTIPLE_NODES
  56. /* use the per-pgdat data instead for discontigmem - mbligh */
  57. unsigned long max_mapnr;
  58. struct page *mem_map;
  59. EXPORT_SYMBOL(max_mapnr);
  60. EXPORT_SYMBOL(mem_map);
  61. #endif
  62. unsigned long num_physpages;
  63. /*
  64. * A number of key systems in x86 including ioremap() rely on the assumption
  65. * that high_memory defines the upper bound on direct map memory, then end
  66. * of ZONE_NORMAL. Under CONFIG_DISCONTIG this means that max_low_pfn and
  67. * highstart_pfn must be the same; there must be no gap between ZONE_NORMAL
  68. * and ZONE_HIGHMEM.
  69. */
  70. void * high_memory;
  71. EXPORT_SYMBOL(num_physpages);
  72. EXPORT_SYMBOL(high_memory);
  73. int randomize_va_space __read_mostly = 1;
  74. static int __init disable_randmaps(char *s)
  75. {
  76. randomize_va_space = 0;
  77. return 1;
  78. }
  79. __setup("norandmaps", disable_randmaps);
  80. /*
  81. * If a p?d_bad entry is found while walking page tables, report
  82. * the error, before resetting entry to p?d_none. Usually (but
  83. * very seldom) called out from the p?d_none_or_clear_bad macros.
  84. */
  85. void pgd_clear_bad(pgd_t *pgd)
  86. {
  87. pgd_ERROR(*pgd);
  88. pgd_clear(pgd);
  89. }
  90. void pud_clear_bad(pud_t *pud)
  91. {
  92. pud_ERROR(*pud);
  93. pud_clear(pud);
  94. }
  95. void pmd_clear_bad(pmd_t *pmd)
  96. {
  97. pmd_ERROR(*pmd);
  98. pmd_clear(pmd);
  99. }
  100. /*
  101. * Note: this doesn't free the actual pages themselves. That
  102. * has been handled earlier when unmapping all the memory regions.
  103. */
  104. static void free_pte_range(struct mmu_gather *tlb, pmd_t *pmd)
  105. {
  106. struct page *page = pmd_page(*pmd);
  107. pmd_clear(pmd);
  108. pte_lock_deinit(page);
  109. pte_free_tlb(tlb, page);
  110. dec_zone_page_state(page, NR_PAGETABLE);
  111. tlb->mm->nr_ptes--;
  112. }
  113. static inline void free_pmd_range(struct mmu_gather *tlb, pud_t *pud,
  114. unsigned long addr, unsigned long end,
  115. unsigned long floor, unsigned long ceiling)
  116. {
  117. pmd_t *pmd;
  118. unsigned long next;
  119. unsigned long start;
  120. start = addr;
  121. pmd = pmd_offset(pud, addr);
  122. do {
  123. next = pmd_addr_end(addr, end);
  124. if (pmd_none_or_clear_bad(pmd))
  125. continue;
  126. free_pte_range(tlb, pmd);
  127. } while (pmd++, addr = next, addr != end);
  128. start &= PUD_MASK;
  129. if (start < floor)
  130. return;
  131. if (ceiling) {
  132. ceiling &= PUD_MASK;
  133. if (!ceiling)
  134. return;
  135. }
  136. if (end - 1 > ceiling - 1)
  137. return;
  138. pmd = pmd_offset(pud, start);
  139. pud_clear(pud);
  140. pmd_free_tlb(tlb, pmd);
  141. }
  142. static inline void free_pud_range(struct mmu_gather *tlb, pgd_t *pgd,
  143. unsigned long addr, unsigned long end,
  144. unsigned long floor, unsigned long ceiling)
  145. {
  146. pud_t *pud;
  147. unsigned long next;
  148. unsigned long start;
  149. start = addr;
  150. pud = pud_offset(pgd, addr);
  151. do {
  152. next = pud_addr_end(addr, end);
  153. if (pud_none_or_clear_bad(pud))
  154. continue;
  155. free_pmd_range(tlb, pud, addr, next, floor, ceiling);
  156. } while (pud++, addr = next, addr != end);
  157. start &= PGDIR_MASK;
  158. if (start < floor)
  159. return;
  160. if (ceiling) {
  161. ceiling &= PGDIR_MASK;
  162. if (!ceiling)
  163. return;
  164. }
  165. if (end - 1 > ceiling - 1)
  166. return;
  167. pud = pud_offset(pgd, start);
  168. pgd_clear(pgd);
  169. pud_free_tlb(tlb, pud);
  170. }
  171. /*
  172. * This function frees user-level page tables of a process.
  173. *
  174. * Must be called with pagetable lock held.
  175. */
  176. void free_pgd_range(struct mmu_gather **tlb,
  177. unsigned long addr, unsigned long end,
  178. unsigned long floor, unsigned long ceiling)
  179. {
  180. pgd_t *pgd;
  181. unsigned long next;
  182. unsigned long start;
  183. /*
  184. * The next few lines have given us lots of grief...
  185. *
  186. * Why are we testing PMD* at this top level? Because often
  187. * there will be no work to do at all, and we'd prefer not to
  188. * go all the way down to the bottom just to discover that.
  189. *
  190. * Why all these "- 1"s? Because 0 represents both the bottom
  191. * of the address space and the top of it (using -1 for the
  192. * top wouldn't help much: the masks would do the wrong thing).
  193. * The rule is that addr 0 and floor 0 refer to the bottom of
  194. * the address space, but end 0 and ceiling 0 refer to the top
  195. * Comparisons need to use "end - 1" and "ceiling - 1" (though
  196. * that end 0 case should be mythical).
  197. *
  198. * Wherever addr is brought up or ceiling brought down, we must
  199. * be careful to reject "the opposite 0" before it confuses the
  200. * subsequent tests. But what about where end is brought down
  201. * by PMD_SIZE below? no, end can't go down to 0 there.
  202. *
  203. * Whereas we round start (addr) and ceiling down, by different
  204. * masks at different levels, in order to test whether a table
  205. * now has no other vmas using it, so can be freed, we don't
  206. * bother to round floor or end up - the tests don't need that.
  207. */
  208. addr &= PMD_MASK;
  209. if (addr < floor) {
  210. addr += PMD_SIZE;
  211. if (!addr)
  212. return;
  213. }
  214. if (ceiling) {
  215. ceiling &= PMD_MASK;
  216. if (!ceiling)
  217. return;
  218. }
  219. if (end - 1 > ceiling - 1)
  220. end -= PMD_SIZE;
  221. if (addr > end - 1)
  222. return;
  223. start = addr;
  224. pgd = pgd_offset((*tlb)->mm, addr);
  225. do {
  226. next = pgd_addr_end(addr, end);
  227. if (pgd_none_or_clear_bad(pgd))
  228. continue;
  229. free_pud_range(*tlb, pgd, addr, next, floor, ceiling);
  230. } while (pgd++, addr = next, addr != end);
  231. if (!(*tlb)->fullmm)
  232. flush_tlb_pgtables((*tlb)->mm, start, end);
  233. }
  234. void free_pgtables(struct mmu_gather **tlb, struct vm_area_struct *vma,
  235. unsigned long floor, unsigned long ceiling)
  236. {
  237. while (vma) {
  238. struct vm_area_struct *next = vma->vm_next;
  239. unsigned long addr = vma->vm_start;
  240. /*
  241. * Hide vma from rmap and vmtruncate before freeing pgtables
  242. */
  243. anon_vma_unlink(vma);
  244. unlink_file_vma(vma);
  245. if (is_vm_hugetlb_page(vma)) {
  246. hugetlb_free_pgd_range(tlb, addr, vma->vm_end,
  247. floor, next? next->vm_start: ceiling);
  248. } else {
  249. /*
  250. * Optimization: gather nearby vmas into one call down
  251. */
  252. while (next && next->vm_start <= vma->vm_end + PMD_SIZE
  253. && !is_vm_hugetlb_page(next)) {
  254. vma = next;
  255. next = vma->vm_next;
  256. anon_vma_unlink(vma);
  257. unlink_file_vma(vma);
  258. }
  259. free_pgd_range(tlb, addr, vma->vm_end,
  260. floor, next? next->vm_start: ceiling);
  261. }
  262. vma = next;
  263. }
  264. }
  265. int __pte_alloc(struct mm_struct *mm, pmd_t *pmd, unsigned long address)
  266. {
  267. struct page *new = pte_alloc_one(mm, address);
  268. if (!new)
  269. return -ENOMEM;
  270. pte_lock_init(new);
  271. spin_lock(&mm->page_table_lock);
  272. if (pmd_present(*pmd)) { /* Another has populated it */
  273. pte_lock_deinit(new);
  274. pte_free(new);
  275. } else {
  276. mm->nr_ptes++;
  277. inc_zone_page_state(new, NR_PAGETABLE);
  278. pmd_populate(mm, pmd, new);
  279. }
  280. spin_unlock(&mm->page_table_lock);
  281. return 0;
  282. }
  283. int __pte_alloc_kernel(pmd_t *pmd, unsigned long address)
  284. {
  285. pte_t *new = pte_alloc_one_kernel(&init_mm, address);
  286. if (!new)
  287. return -ENOMEM;
  288. spin_lock(&init_mm.page_table_lock);
  289. if (pmd_present(*pmd)) /* Another has populated it */
  290. pte_free_kernel(new);
  291. else
  292. pmd_populate_kernel(&init_mm, pmd, new);
  293. spin_unlock(&init_mm.page_table_lock);
  294. return 0;
  295. }
  296. static inline void add_mm_rss(struct mm_struct *mm, int file_rss, int anon_rss)
  297. {
  298. if (file_rss)
  299. add_mm_counter(mm, file_rss, file_rss);
  300. if (anon_rss)
  301. add_mm_counter(mm, anon_rss, anon_rss);
  302. }
  303. /*
  304. * This function is called to print an error when a bad pte
  305. * is found. For example, we might have a PFN-mapped pte in
  306. * a region that doesn't allow it.
  307. *
  308. * The calling function must still handle the error.
  309. */
  310. void print_bad_pte(struct vm_area_struct *vma, pte_t pte, unsigned long vaddr)
  311. {
  312. printk(KERN_ERR "Bad pte = %08llx, process = %s, "
  313. "vm_flags = %lx, vaddr = %lx\n",
  314. (long long)pte_val(pte),
  315. (vma->vm_mm == current->mm ? current->comm : "???"),
  316. vma->vm_flags, vaddr);
  317. dump_stack();
  318. }
  319. static inline int is_cow_mapping(unsigned int flags)
  320. {
  321. return (flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
  322. }
  323. /*
  324. * This function gets the "struct page" associated with a pte.
  325. *
  326. * NOTE! Some mappings do not have "struct pages". A raw PFN mapping
  327. * will have each page table entry just pointing to a raw page frame
  328. * number, and as far as the VM layer is concerned, those do not have
  329. * pages associated with them - even if the PFN might point to memory
  330. * that otherwise is perfectly fine and has a "struct page".
  331. *
  332. * The way we recognize those mappings is through the rules set up
  333. * by "remap_pfn_range()": the vma will have the VM_PFNMAP bit set,
  334. * and the vm_pgoff will point to the first PFN mapped: thus every
  335. * page that is a raw mapping will always honor the rule
  336. *
  337. * pfn_of_page == vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT)
  338. *
  339. * and if that isn't true, the page has been COW'ed (in which case it
  340. * _does_ have a "struct page" associated with it even if it is in a
  341. * VM_PFNMAP range).
  342. */
  343. struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr, pte_t pte)
  344. {
  345. unsigned long pfn = pte_pfn(pte);
  346. if (unlikely(vma->vm_flags & VM_PFNMAP)) {
  347. unsigned long off = (addr - vma->vm_start) >> PAGE_SHIFT;
  348. if (pfn == vma->vm_pgoff + off)
  349. return NULL;
  350. if (!is_cow_mapping(vma->vm_flags))
  351. return NULL;
  352. }
  353. /*
  354. * Add some anal sanity checks for now. Eventually,
  355. * we should just do "return pfn_to_page(pfn)", but
  356. * in the meantime we check that we get a valid pfn,
  357. * and that the resulting page looks ok.
  358. */
  359. if (unlikely(!pfn_valid(pfn))) {
  360. print_bad_pte(vma, pte, addr);
  361. return NULL;
  362. }
  363. /*
  364. * NOTE! We still have PageReserved() pages in the page
  365. * tables.
  366. *
  367. * The PAGE_ZERO() pages and various VDSO mappings can
  368. * cause them to exist.
  369. */
  370. return pfn_to_page(pfn);
  371. }
  372. /*
  373. * copy one vm_area from one task to the other. Assumes the page tables
  374. * already present in the new task to be cleared in the whole range
  375. * covered by this vma.
  376. */
  377. static inline void
  378. copy_one_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm,
  379. pte_t *dst_pte, pte_t *src_pte, struct vm_area_struct *vma,
  380. unsigned long addr, int *rss)
  381. {
  382. unsigned long vm_flags = vma->vm_flags;
  383. pte_t pte = *src_pte;
  384. struct page *page;
  385. /* pte contains position in swap or file, so copy. */
  386. if (unlikely(!pte_present(pte))) {
  387. if (!pte_file(pte)) {
  388. swp_entry_t entry = pte_to_swp_entry(pte);
  389. swap_duplicate(entry);
  390. /* make sure dst_mm is on swapoff's mmlist. */
  391. if (unlikely(list_empty(&dst_mm->mmlist))) {
  392. spin_lock(&mmlist_lock);
  393. if (list_empty(&dst_mm->mmlist))
  394. list_add(&dst_mm->mmlist,
  395. &src_mm->mmlist);
  396. spin_unlock(&mmlist_lock);
  397. }
  398. if (is_write_migration_entry(entry) &&
  399. is_cow_mapping(vm_flags)) {
  400. /*
  401. * COW mappings require pages in both parent
  402. * and child to be set to read.
  403. */
  404. make_migration_entry_read(&entry);
  405. pte = swp_entry_to_pte(entry);
  406. set_pte_at(src_mm, addr, src_pte, pte);
  407. }
  408. }
  409. goto out_set_pte;
  410. }
  411. /*
  412. * If it's a COW mapping, write protect it both
  413. * in the parent and the child
  414. */
  415. if (is_cow_mapping(vm_flags)) {
  416. ptep_set_wrprotect(src_mm, addr, src_pte);
  417. pte = pte_wrprotect(pte);
  418. }
  419. /*
  420. * If it's a shared mapping, mark it clean in
  421. * the child
  422. */
  423. if (vm_flags & VM_SHARED)
  424. pte = pte_mkclean(pte);
  425. pte = pte_mkold(pte);
  426. page = vm_normal_page(vma, addr, pte);
  427. if (page) {
  428. get_page(page);
  429. page_dup_rmap(page, vma, addr);
  430. rss[!!PageAnon(page)]++;
  431. }
  432. out_set_pte:
  433. set_pte_at(dst_mm, addr, dst_pte, pte);
  434. }
  435. static int copy_pte_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
  436. pmd_t *dst_pmd, pmd_t *src_pmd, struct vm_area_struct *vma,
  437. unsigned long addr, unsigned long end)
  438. {
  439. pte_t *src_pte, *dst_pte;
  440. spinlock_t *src_ptl, *dst_ptl;
  441. int progress = 0;
  442. int rss[2];
  443. again:
  444. rss[1] = rss[0] = 0;
  445. dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl);
  446. if (!dst_pte)
  447. return -ENOMEM;
  448. src_pte = pte_offset_map_nested(src_pmd, addr);
  449. src_ptl = pte_lockptr(src_mm, src_pmd);
  450. spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
  451. arch_enter_lazy_mmu_mode();
  452. do {
  453. /*
  454. * We are holding two locks at this point - either of them
  455. * could generate latencies in another task on another CPU.
  456. */
  457. if (progress >= 32) {
  458. progress = 0;
  459. if (need_resched() ||
  460. need_lockbreak(src_ptl) ||
  461. need_lockbreak(dst_ptl))
  462. break;
  463. }
  464. if (pte_none(*src_pte)) {
  465. progress++;
  466. continue;
  467. }
  468. copy_one_pte(dst_mm, src_mm, dst_pte, src_pte, vma, addr, rss);
  469. progress += 8;
  470. } while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end);
  471. arch_leave_lazy_mmu_mode();
  472. spin_unlock(src_ptl);
  473. pte_unmap_nested(src_pte - 1);
  474. add_mm_rss(dst_mm, rss[0], rss[1]);
  475. pte_unmap_unlock(dst_pte - 1, dst_ptl);
  476. cond_resched();
  477. if (addr != end)
  478. goto again;
  479. return 0;
  480. }
  481. static inline int copy_pmd_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
  482. pud_t *dst_pud, pud_t *src_pud, struct vm_area_struct *vma,
  483. unsigned long addr, unsigned long end)
  484. {
  485. pmd_t *src_pmd, *dst_pmd;
  486. unsigned long next;
  487. dst_pmd = pmd_alloc(dst_mm, dst_pud, addr);
  488. if (!dst_pmd)
  489. return -ENOMEM;
  490. src_pmd = pmd_offset(src_pud, addr);
  491. do {
  492. next = pmd_addr_end(addr, end);
  493. if (pmd_none_or_clear_bad(src_pmd))
  494. continue;
  495. if (copy_pte_range(dst_mm, src_mm, dst_pmd, src_pmd,
  496. vma, addr, next))
  497. return -ENOMEM;
  498. } while (dst_pmd++, src_pmd++, addr = next, addr != end);
  499. return 0;
  500. }
  501. static inline int copy_pud_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
  502. pgd_t *dst_pgd, pgd_t *src_pgd, struct vm_area_struct *vma,
  503. unsigned long addr, unsigned long end)
  504. {
  505. pud_t *src_pud, *dst_pud;
  506. unsigned long next;
  507. dst_pud = pud_alloc(dst_mm, dst_pgd, addr);
  508. if (!dst_pud)
  509. return -ENOMEM;
  510. src_pud = pud_offset(src_pgd, addr);
  511. do {
  512. next = pud_addr_end(addr, end);
  513. if (pud_none_or_clear_bad(src_pud))
  514. continue;
  515. if (copy_pmd_range(dst_mm, src_mm, dst_pud, src_pud,
  516. vma, addr, next))
  517. return -ENOMEM;
  518. } while (dst_pud++, src_pud++, addr = next, addr != end);
  519. return 0;
  520. }
  521. int copy_page_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
  522. struct vm_area_struct *vma)
  523. {
  524. pgd_t *src_pgd, *dst_pgd;
  525. unsigned long next;
  526. unsigned long addr = vma->vm_start;
  527. unsigned long end = vma->vm_end;
  528. /*
  529. * Don't copy ptes where a page fault will fill them correctly.
  530. * Fork becomes much lighter when there are big shared or private
  531. * readonly mappings. The tradeoff is that copy_page_range is more
  532. * efficient than faulting.
  533. */
  534. if (!(vma->vm_flags & (VM_HUGETLB|VM_NONLINEAR|VM_PFNMAP|VM_INSERTPAGE))) {
  535. if (!vma->anon_vma)
  536. return 0;
  537. }
  538. if (is_vm_hugetlb_page(vma))
  539. return copy_hugetlb_page_range(dst_mm, src_mm, vma);
  540. dst_pgd = pgd_offset(dst_mm, addr);
  541. src_pgd = pgd_offset(src_mm, addr);
  542. do {
  543. next = pgd_addr_end(addr, end);
  544. if (pgd_none_or_clear_bad(src_pgd))
  545. continue;
  546. if (copy_pud_range(dst_mm, src_mm, dst_pgd, src_pgd,
  547. vma, addr, next))
  548. return -ENOMEM;
  549. } while (dst_pgd++, src_pgd++, addr = next, addr != end);
  550. return 0;
  551. }
  552. static unsigned long zap_pte_range(struct mmu_gather *tlb,
  553. struct vm_area_struct *vma, pmd_t *pmd,
  554. unsigned long addr, unsigned long end,
  555. long *zap_work, struct zap_details *details)
  556. {
  557. struct mm_struct *mm = tlb->mm;
  558. pte_t *pte;
  559. spinlock_t *ptl;
  560. int file_rss = 0;
  561. int anon_rss = 0;
  562. pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
  563. arch_enter_lazy_mmu_mode();
  564. do {
  565. pte_t ptent = *pte;
  566. if (pte_none(ptent)) {
  567. (*zap_work)--;
  568. continue;
  569. }
  570. (*zap_work) -= PAGE_SIZE;
  571. if (pte_present(ptent)) {
  572. struct page *page;
  573. page = vm_normal_page(vma, addr, ptent);
  574. if (unlikely(details) && page) {
  575. /*
  576. * unmap_shared_mapping_pages() wants to
  577. * invalidate cache without truncating:
  578. * unmap shared but keep private pages.
  579. */
  580. if (details->check_mapping &&
  581. details->check_mapping != page->mapping)
  582. continue;
  583. /*
  584. * Each page->index must be checked when
  585. * invalidating or truncating nonlinear.
  586. */
  587. if (details->nonlinear_vma &&
  588. (page->index < details->first_index ||
  589. page->index > details->last_index))
  590. continue;
  591. }
  592. ptent = ptep_get_and_clear_full(mm, addr, pte,
  593. tlb->fullmm);
  594. tlb_remove_tlb_entry(tlb, pte, addr);
  595. if (unlikely(!page))
  596. continue;
  597. if (unlikely(details) && details->nonlinear_vma
  598. && linear_page_index(details->nonlinear_vma,
  599. addr) != page->index)
  600. set_pte_at(mm, addr, pte,
  601. pgoff_to_pte(page->index));
  602. if (PageAnon(page))
  603. anon_rss--;
  604. else {
  605. if (pte_dirty(ptent))
  606. set_page_dirty(page);
  607. if (pte_young(ptent))
  608. SetPageReferenced(page);
  609. file_rss--;
  610. }
  611. page_remove_rmap(page, vma);
  612. tlb_remove_page(tlb, page);
  613. continue;
  614. }
  615. /*
  616. * If details->check_mapping, we leave swap entries;
  617. * if details->nonlinear_vma, we leave file entries.
  618. */
  619. if (unlikely(details))
  620. continue;
  621. if (!pte_file(ptent))
  622. free_swap_and_cache(pte_to_swp_entry(ptent));
  623. pte_clear_not_present_full(mm, addr, pte, tlb->fullmm);
  624. } while (pte++, addr += PAGE_SIZE, (addr != end && *zap_work > 0));
  625. add_mm_rss(mm, file_rss, anon_rss);
  626. arch_leave_lazy_mmu_mode();
  627. pte_unmap_unlock(pte - 1, ptl);
  628. return addr;
  629. }
  630. static inline unsigned long zap_pmd_range(struct mmu_gather *tlb,
  631. struct vm_area_struct *vma, pud_t *pud,
  632. unsigned long addr, unsigned long end,
  633. long *zap_work, struct zap_details *details)
  634. {
  635. pmd_t *pmd;
  636. unsigned long next;
  637. pmd = pmd_offset(pud, addr);
  638. do {
  639. next = pmd_addr_end(addr, end);
  640. if (pmd_none_or_clear_bad(pmd)) {
  641. (*zap_work)--;
  642. continue;
  643. }
  644. next = zap_pte_range(tlb, vma, pmd, addr, next,
  645. zap_work, details);
  646. } while (pmd++, addr = next, (addr != end && *zap_work > 0));
  647. return addr;
  648. }
  649. static inline unsigned long zap_pud_range(struct mmu_gather *tlb,
  650. struct vm_area_struct *vma, pgd_t *pgd,
  651. unsigned long addr, unsigned long end,
  652. long *zap_work, struct zap_details *details)
  653. {
  654. pud_t *pud;
  655. unsigned long next;
  656. pud = pud_offset(pgd, addr);
  657. do {
  658. next = pud_addr_end(addr, end);
  659. if (pud_none_or_clear_bad(pud)) {
  660. (*zap_work)--;
  661. continue;
  662. }
  663. next = zap_pmd_range(tlb, vma, pud, addr, next,
  664. zap_work, details);
  665. } while (pud++, addr = next, (addr != end && *zap_work > 0));
  666. return addr;
  667. }
  668. static unsigned long unmap_page_range(struct mmu_gather *tlb,
  669. struct vm_area_struct *vma,
  670. unsigned long addr, unsigned long end,
  671. long *zap_work, struct zap_details *details)
  672. {
  673. pgd_t *pgd;
  674. unsigned long next;
  675. if (details && !details->check_mapping && !details->nonlinear_vma)
  676. details = NULL;
  677. BUG_ON(addr >= end);
  678. tlb_start_vma(tlb, vma);
  679. pgd = pgd_offset(vma->vm_mm, addr);
  680. do {
  681. next = pgd_addr_end(addr, end);
  682. if (pgd_none_or_clear_bad(pgd)) {
  683. (*zap_work)--;
  684. continue;
  685. }
  686. next = zap_pud_range(tlb, vma, pgd, addr, next,
  687. zap_work, details);
  688. } while (pgd++, addr = next, (addr != end && *zap_work > 0));
  689. tlb_end_vma(tlb, vma);
  690. return addr;
  691. }
  692. #ifdef CONFIG_PREEMPT
  693. # define ZAP_BLOCK_SIZE (8 * PAGE_SIZE)
  694. #else
  695. /* No preempt: go for improved straight-line efficiency */
  696. # define ZAP_BLOCK_SIZE (1024 * PAGE_SIZE)
  697. #endif
  698. /**
  699. * unmap_vmas - unmap a range of memory covered by a list of vma's
  700. * @tlbp: address of the caller's struct mmu_gather
  701. * @vma: the starting vma
  702. * @start_addr: virtual address at which to start unmapping
  703. * @end_addr: virtual address at which to end unmapping
  704. * @nr_accounted: Place number of unmapped pages in vm-accountable vma's here
  705. * @details: details of nonlinear truncation or shared cache invalidation
  706. *
  707. * Returns the end address of the unmapping (restart addr if interrupted).
  708. *
  709. * Unmap all pages in the vma list.
  710. *
  711. * We aim to not hold locks for too long (for scheduling latency reasons).
  712. * So zap pages in ZAP_BLOCK_SIZE bytecounts. This means we need to
  713. * return the ending mmu_gather to the caller.
  714. *
  715. * Only addresses between `start' and `end' will be unmapped.
  716. *
  717. * The VMA list must be sorted in ascending virtual address order.
  718. *
  719. * unmap_vmas() assumes that the caller will flush the whole unmapped address
  720. * range after unmap_vmas() returns. So the only responsibility here is to
  721. * ensure that any thus-far unmapped pages are flushed before unmap_vmas()
  722. * drops the lock and schedules.
  723. */
  724. unsigned long unmap_vmas(struct mmu_gather **tlbp,
  725. struct vm_area_struct *vma, unsigned long start_addr,
  726. unsigned long end_addr, unsigned long *nr_accounted,
  727. struct zap_details *details)
  728. {
  729. long zap_work = ZAP_BLOCK_SIZE;
  730. unsigned long tlb_start = 0; /* For tlb_finish_mmu */
  731. int tlb_start_valid = 0;
  732. unsigned long start = start_addr;
  733. spinlock_t *i_mmap_lock = details? details->i_mmap_lock: NULL;
  734. int fullmm = (*tlbp)->fullmm;
  735. for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next) {
  736. unsigned long end;
  737. start = max(vma->vm_start, start_addr);
  738. if (start >= vma->vm_end)
  739. continue;
  740. end = min(vma->vm_end, end_addr);
  741. if (end <= vma->vm_start)
  742. continue;
  743. if (vma->vm_flags & VM_ACCOUNT)
  744. *nr_accounted += (end - start) >> PAGE_SHIFT;
  745. while (start != end) {
  746. if (!tlb_start_valid) {
  747. tlb_start = start;
  748. tlb_start_valid = 1;
  749. }
  750. if (unlikely(is_vm_hugetlb_page(vma))) {
  751. unmap_hugepage_range(vma, start, end);
  752. zap_work -= (end - start) /
  753. (HPAGE_SIZE / PAGE_SIZE);
  754. start = end;
  755. } else
  756. start = unmap_page_range(*tlbp, vma,
  757. start, end, &zap_work, details);
  758. if (zap_work > 0) {
  759. BUG_ON(start != end);
  760. break;
  761. }
  762. tlb_finish_mmu(*tlbp, tlb_start, start);
  763. if (need_resched() ||
  764. (i_mmap_lock && need_lockbreak(i_mmap_lock))) {
  765. if (i_mmap_lock) {
  766. *tlbp = NULL;
  767. goto out;
  768. }
  769. cond_resched();
  770. }
  771. *tlbp = tlb_gather_mmu(vma->vm_mm, fullmm);
  772. tlb_start_valid = 0;
  773. zap_work = ZAP_BLOCK_SIZE;
  774. }
  775. }
  776. out:
  777. return start; /* which is now the end (or restart) address */
  778. }
  779. /**
  780. * zap_page_range - remove user pages in a given range
  781. * @vma: vm_area_struct holding the applicable pages
  782. * @address: starting address of pages to zap
  783. * @size: number of bytes to zap
  784. * @details: details of nonlinear truncation or shared cache invalidation
  785. */
  786. unsigned long zap_page_range(struct vm_area_struct *vma, unsigned long address,
  787. unsigned long size, struct zap_details *details)
  788. {
  789. struct mm_struct *mm = vma->vm_mm;
  790. struct mmu_gather *tlb;
  791. unsigned long end = address + size;
  792. unsigned long nr_accounted = 0;
  793. lru_add_drain();
  794. tlb = tlb_gather_mmu(mm, 0);
  795. update_hiwater_rss(mm);
  796. end = unmap_vmas(&tlb, vma, address, end, &nr_accounted, details);
  797. if (tlb)
  798. tlb_finish_mmu(tlb, address, end);
  799. return end;
  800. }
  801. /*
  802. * Do a quick page-table lookup for a single page.
  803. */
  804. struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
  805. unsigned int flags)
  806. {
  807. pgd_t *pgd;
  808. pud_t *pud;
  809. pmd_t *pmd;
  810. pte_t *ptep, pte;
  811. spinlock_t *ptl;
  812. struct page *page;
  813. struct mm_struct *mm = vma->vm_mm;
  814. page = follow_huge_addr(mm, address, flags & FOLL_WRITE);
  815. if (!IS_ERR(page)) {
  816. BUG_ON(flags & FOLL_GET);
  817. goto out;
  818. }
  819. page = NULL;
  820. pgd = pgd_offset(mm, address);
  821. if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
  822. goto no_page_table;
  823. pud = pud_offset(pgd, address);
  824. if (pud_none(*pud) || unlikely(pud_bad(*pud)))
  825. goto no_page_table;
  826. pmd = pmd_offset(pud, address);
  827. if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
  828. goto no_page_table;
  829. if (pmd_huge(*pmd)) {
  830. BUG_ON(flags & FOLL_GET);
  831. page = follow_huge_pmd(mm, address, pmd, flags & FOLL_WRITE);
  832. goto out;
  833. }
  834. ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
  835. if (!ptep)
  836. goto out;
  837. pte = *ptep;
  838. if (!pte_present(pte))
  839. goto unlock;
  840. if ((flags & FOLL_WRITE) && !pte_write(pte))
  841. goto unlock;
  842. page = vm_normal_page(vma, address, pte);
  843. if (unlikely(!page))
  844. goto unlock;
  845. if (flags & FOLL_GET)
  846. get_page(page);
  847. if (flags & FOLL_TOUCH) {
  848. if ((flags & FOLL_WRITE) &&
  849. !pte_dirty(pte) && !PageDirty(page))
  850. set_page_dirty(page);
  851. mark_page_accessed(page);
  852. }
  853. unlock:
  854. pte_unmap_unlock(ptep, ptl);
  855. out:
  856. return page;
  857. no_page_table:
  858. /*
  859. * When core dumping an enormous anonymous area that nobody
  860. * has touched so far, we don't want to allocate page tables.
  861. */
  862. if (flags & FOLL_ANON) {
  863. page = ZERO_PAGE(address);
  864. if (flags & FOLL_GET)
  865. get_page(page);
  866. BUG_ON(flags & FOLL_WRITE);
  867. }
  868. return page;
  869. }
  870. int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
  871. unsigned long start, int len, int write, int force,
  872. struct page **pages, struct vm_area_struct **vmas)
  873. {
  874. int i;
  875. unsigned int vm_flags;
  876. /*
  877. * Require read or write permissions.
  878. * If 'force' is set, we only require the "MAY" flags.
  879. */
  880. vm_flags = write ? (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
  881. vm_flags &= force ? (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
  882. i = 0;
  883. do {
  884. struct vm_area_struct *vma;
  885. unsigned int foll_flags;
  886. vma = find_extend_vma(mm, start);
  887. if (!vma && in_gate_area(tsk, start)) {
  888. unsigned long pg = start & PAGE_MASK;
  889. struct vm_area_struct *gate_vma = get_gate_vma(tsk);
  890. pgd_t *pgd;
  891. pud_t *pud;
  892. pmd_t *pmd;
  893. pte_t *pte;
  894. if (write) /* user gate pages are read-only */
  895. return i ? : -EFAULT;
  896. if (pg > TASK_SIZE)
  897. pgd = pgd_offset_k(pg);
  898. else
  899. pgd = pgd_offset_gate(mm, pg);
  900. BUG_ON(pgd_none(*pgd));
  901. pud = pud_offset(pgd, pg);
  902. BUG_ON(pud_none(*pud));
  903. pmd = pmd_offset(pud, pg);
  904. if (pmd_none(*pmd))
  905. return i ? : -EFAULT;
  906. pte = pte_offset_map(pmd, pg);
  907. if (pte_none(*pte)) {
  908. pte_unmap(pte);
  909. return i ? : -EFAULT;
  910. }
  911. if (pages) {
  912. struct page *page = vm_normal_page(gate_vma, start, *pte);
  913. pages[i] = page;
  914. if (page)
  915. get_page(page);
  916. }
  917. pte_unmap(pte);
  918. if (vmas)
  919. vmas[i] = gate_vma;
  920. i++;
  921. start += PAGE_SIZE;
  922. len--;
  923. continue;
  924. }
  925. if (!vma || (vma->vm_flags & (VM_IO | VM_PFNMAP))
  926. || !(vm_flags & vma->vm_flags))
  927. return i ? : -EFAULT;
  928. if (is_vm_hugetlb_page(vma)) {
  929. i = follow_hugetlb_page(mm, vma, pages, vmas,
  930. &start, &len, i);
  931. continue;
  932. }
  933. foll_flags = FOLL_TOUCH;
  934. if (pages)
  935. foll_flags |= FOLL_GET;
  936. if (!write && !(vma->vm_flags & VM_LOCKED) &&
  937. (!vma->vm_ops || (!vma->vm_ops->nopage &&
  938. !vma->vm_ops->fault)))
  939. foll_flags |= FOLL_ANON;
  940. do {
  941. struct page *page;
  942. /*
  943. * If tsk is ooming, cut off its access to large memory
  944. * allocations. It has a pending SIGKILL, but it can't
  945. * be processed until returning to user space.
  946. */
  947. if (unlikely(test_tsk_thread_flag(tsk, TIF_MEMDIE)))
  948. return -ENOMEM;
  949. if (write)
  950. foll_flags |= FOLL_WRITE;
  951. cond_resched();
  952. while (!(page = follow_page(vma, start, foll_flags))) {
  953. int ret;
  954. ret = handle_mm_fault(mm, vma, start,
  955. foll_flags & FOLL_WRITE);
  956. if (ret & VM_FAULT_ERROR) {
  957. if (ret & VM_FAULT_OOM)
  958. return i ? i : -ENOMEM;
  959. else if (ret & VM_FAULT_SIGBUS)
  960. return i ? i : -EFAULT;
  961. BUG();
  962. }
  963. if (ret & VM_FAULT_MAJOR)
  964. tsk->maj_flt++;
  965. else
  966. tsk->min_flt++;
  967. /*
  968. * The VM_FAULT_WRITE bit tells us that
  969. * do_wp_page has broken COW when necessary,
  970. * even if maybe_mkwrite decided not to set
  971. * pte_write. We can thus safely do subsequent
  972. * page lookups as if they were reads.
  973. */
  974. if (ret & VM_FAULT_WRITE)
  975. foll_flags &= ~FOLL_WRITE;
  976. cond_resched();
  977. }
  978. if (pages) {
  979. pages[i] = page;
  980. flush_anon_page(vma, page, start);
  981. flush_dcache_page(page);
  982. }
  983. if (vmas)
  984. vmas[i] = vma;
  985. i++;
  986. start += PAGE_SIZE;
  987. len--;
  988. } while (len && start < vma->vm_end);
  989. } while (len);
  990. return i;
  991. }
  992. EXPORT_SYMBOL(get_user_pages);
  993. static int zeromap_pte_range(struct mm_struct *mm, pmd_t *pmd,
  994. unsigned long addr, unsigned long end, pgprot_t prot)
  995. {
  996. pte_t *pte;
  997. spinlock_t *ptl;
  998. int err = 0;
  999. pte = pte_alloc_map_lock(mm, pmd, addr, &ptl);
  1000. if (!pte)
  1001. return -EAGAIN;
  1002. arch_enter_lazy_mmu_mode();
  1003. do {
  1004. struct page *page = ZERO_PAGE(addr);
  1005. pte_t zero_pte = pte_wrprotect(mk_pte(page, prot));
  1006. if (unlikely(!pte_none(*pte))) {
  1007. err = -EEXIST;
  1008. pte++;
  1009. break;
  1010. }
  1011. page_cache_get(page);
  1012. page_add_file_rmap(page);
  1013. inc_mm_counter(mm, file_rss);
  1014. set_pte_at(mm, addr, pte, zero_pte);
  1015. } while (pte++, addr += PAGE_SIZE, addr != end);
  1016. arch_leave_lazy_mmu_mode();
  1017. pte_unmap_unlock(pte - 1, ptl);
  1018. return err;
  1019. }
  1020. static inline int zeromap_pmd_range(struct mm_struct *mm, pud_t *pud,
  1021. unsigned long addr, unsigned long end, pgprot_t prot)
  1022. {
  1023. pmd_t *pmd;
  1024. unsigned long next;
  1025. int err;
  1026. pmd = pmd_alloc(mm, pud, addr);
  1027. if (!pmd)
  1028. return -EAGAIN;
  1029. do {
  1030. next = pmd_addr_end(addr, end);
  1031. err = zeromap_pte_range(mm, pmd, addr, next, prot);
  1032. if (err)
  1033. break;
  1034. } while (pmd++, addr = next, addr != end);
  1035. return err;
  1036. }
  1037. static inline int zeromap_pud_range(struct mm_struct *mm, pgd_t *pgd,
  1038. unsigned long addr, unsigned long end, pgprot_t prot)
  1039. {
  1040. pud_t *pud;
  1041. unsigned long next;
  1042. int err;
  1043. pud = pud_alloc(mm, pgd, addr);
  1044. if (!pud)
  1045. return -EAGAIN;
  1046. do {
  1047. next = pud_addr_end(addr, end);
  1048. err = zeromap_pmd_range(mm, pud, addr, next, prot);
  1049. if (err)
  1050. break;
  1051. } while (pud++, addr = next, addr != end);
  1052. return err;
  1053. }
  1054. int zeromap_page_range(struct vm_area_struct *vma,
  1055. unsigned long addr, unsigned long size, pgprot_t prot)
  1056. {
  1057. pgd_t *pgd;
  1058. unsigned long next;
  1059. unsigned long end = addr + size;
  1060. struct mm_struct *mm = vma->vm_mm;
  1061. int err;
  1062. BUG_ON(addr >= end);
  1063. pgd = pgd_offset(mm, addr);
  1064. flush_cache_range(vma, addr, end);
  1065. do {
  1066. next = pgd_addr_end(addr, end);
  1067. err = zeromap_pud_range(mm, pgd, addr, next, prot);
  1068. if (err)
  1069. break;
  1070. } while (pgd++, addr = next, addr != end);
  1071. return err;
  1072. }
  1073. pte_t * fastcall get_locked_pte(struct mm_struct *mm, unsigned long addr, spinlock_t **ptl)
  1074. {
  1075. pgd_t * pgd = pgd_offset(mm, addr);
  1076. pud_t * pud = pud_alloc(mm, pgd, addr);
  1077. if (pud) {
  1078. pmd_t * pmd = pmd_alloc(mm, pud, addr);
  1079. if (pmd)
  1080. return pte_alloc_map_lock(mm, pmd, addr, ptl);
  1081. }
  1082. return NULL;
  1083. }
  1084. /*
  1085. * This is the old fallback for page remapping.
  1086. *
  1087. * For historical reasons, it only allows reserved pages. Only
  1088. * old drivers should use this, and they needed to mark their
  1089. * pages reserved for the old functions anyway.
  1090. */
  1091. static int insert_page(struct mm_struct *mm, unsigned long addr, struct page *page, pgprot_t prot)
  1092. {
  1093. int retval;
  1094. pte_t *pte;
  1095. spinlock_t *ptl;
  1096. retval = -EINVAL;
  1097. if (PageAnon(page))
  1098. goto out;
  1099. retval = -ENOMEM;
  1100. flush_dcache_page(page);
  1101. pte = get_locked_pte(mm, addr, &ptl);
  1102. if (!pte)
  1103. goto out;
  1104. retval = -EBUSY;
  1105. if (!pte_none(*pte))
  1106. goto out_unlock;
  1107. /* Ok, finally just insert the thing.. */
  1108. get_page(page);
  1109. inc_mm_counter(mm, file_rss);
  1110. page_add_file_rmap(page);
  1111. set_pte_at(mm, addr, pte, mk_pte(page, prot));
  1112. retval = 0;
  1113. out_unlock:
  1114. pte_unmap_unlock(pte, ptl);
  1115. out:
  1116. return retval;
  1117. }
  1118. /**
  1119. * vm_insert_page - insert single page into user vma
  1120. * @vma: user vma to map to
  1121. * @addr: target user address of this page
  1122. * @page: source kernel page
  1123. *
  1124. * This allows drivers to insert individual pages they've allocated
  1125. * into a user vma.
  1126. *
  1127. * The page has to be a nice clean _individual_ kernel allocation.
  1128. * If you allocate a compound page, you need to have marked it as
  1129. * such (__GFP_COMP), or manually just split the page up yourself
  1130. * (see split_page()).
  1131. *
  1132. * NOTE! Traditionally this was done with "remap_pfn_range()" which
  1133. * took an arbitrary page protection parameter. This doesn't allow
  1134. * that. Your vma protection will have to be set up correctly, which
  1135. * means that if you want a shared writable mapping, you'd better
  1136. * ask for a shared writable mapping!
  1137. *
  1138. * The page does not need to be reserved.
  1139. */
  1140. int vm_insert_page(struct vm_area_struct *vma, unsigned long addr, struct page *page)
  1141. {
  1142. if (addr < vma->vm_start || addr >= vma->vm_end)
  1143. return -EFAULT;
  1144. if (!page_count(page))
  1145. return -EINVAL;
  1146. vma->vm_flags |= VM_INSERTPAGE;
  1147. return insert_page(vma->vm_mm, addr, page, vma->vm_page_prot);
  1148. }
  1149. EXPORT_SYMBOL(vm_insert_page);
  1150. /**
  1151. * vm_insert_pfn - insert single pfn into user vma
  1152. * @vma: user vma to map to
  1153. * @addr: target user address of this page
  1154. * @pfn: source kernel pfn
  1155. *
  1156. * Similar to vm_inert_page, this allows drivers to insert individual pages
  1157. * they've allocated into a user vma. Same comments apply.
  1158. *
  1159. * This function should only be called from a vm_ops->fault handler, and
  1160. * in that case the handler should return NULL.
  1161. */
  1162. int vm_insert_pfn(struct vm_area_struct *vma, unsigned long addr,
  1163. unsigned long pfn)
  1164. {
  1165. struct mm_struct *mm = vma->vm_mm;
  1166. int retval;
  1167. pte_t *pte, entry;
  1168. spinlock_t *ptl;
  1169. BUG_ON(!(vma->vm_flags & VM_PFNMAP));
  1170. BUG_ON(is_cow_mapping(vma->vm_flags));
  1171. retval = -ENOMEM;
  1172. pte = get_locked_pte(mm, addr, &ptl);
  1173. if (!pte)
  1174. goto out;
  1175. retval = -EBUSY;
  1176. if (!pte_none(*pte))
  1177. goto out_unlock;
  1178. /* Ok, finally just insert the thing.. */
  1179. entry = pfn_pte(pfn, vma->vm_page_prot);
  1180. set_pte_at(mm, addr, pte, entry);
  1181. update_mmu_cache(vma, addr, entry);
  1182. retval = 0;
  1183. out_unlock:
  1184. pte_unmap_unlock(pte, ptl);
  1185. out:
  1186. return retval;
  1187. }
  1188. EXPORT_SYMBOL(vm_insert_pfn);
  1189. /*
  1190. * maps a range of physical memory into the requested pages. the old
  1191. * mappings are removed. any references to nonexistent pages results
  1192. * in null mappings (currently treated as "copy-on-access")
  1193. */
  1194. static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd,
  1195. unsigned long addr, unsigned long end,
  1196. unsigned long pfn, pgprot_t prot)
  1197. {
  1198. pte_t *pte;
  1199. spinlock_t *ptl;
  1200. pte = pte_alloc_map_lock(mm, pmd, addr, &ptl);
  1201. if (!pte)
  1202. return -ENOMEM;
  1203. arch_enter_lazy_mmu_mode();
  1204. do {
  1205. BUG_ON(!pte_none(*pte));
  1206. set_pte_at(mm, addr, pte, pfn_pte(pfn, prot));
  1207. pfn++;
  1208. } while (pte++, addr += PAGE_SIZE, addr != end);
  1209. arch_leave_lazy_mmu_mode();
  1210. pte_unmap_unlock(pte - 1, ptl);
  1211. return 0;
  1212. }
  1213. static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud,
  1214. unsigned long addr, unsigned long end,
  1215. unsigned long pfn, pgprot_t prot)
  1216. {
  1217. pmd_t *pmd;
  1218. unsigned long next;
  1219. pfn -= addr >> PAGE_SHIFT;
  1220. pmd = pmd_alloc(mm, pud, addr);
  1221. if (!pmd)
  1222. return -ENOMEM;
  1223. do {
  1224. next = pmd_addr_end(addr, end);
  1225. if (remap_pte_range(mm, pmd, addr, next,
  1226. pfn + (addr >> PAGE_SHIFT), prot))
  1227. return -ENOMEM;
  1228. } while (pmd++, addr = next, addr != end);
  1229. return 0;
  1230. }
  1231. static inline int remap_pud_range(struct mm_struct *mm, pgd_t *pgd,
  1232. unsigned long addr, unsigned long end,
  1233. unsigned long pfn, pgprot_t prot)
  1234. {
  1235. pud_t *pud;
  1236. unsigned long next;
  1237. pfn -= addr >> PAGE_SHIFT;
  1238. pud = pud_alloc(mm, pgd, addr);
  1239. if (!pud)
  1240. return -ENOMEM;
  1241. do {
  1242. next = pud_addr_end(addr, end);
  1243. if (remap_pmd_range(mm, pud, addr, next,
  1244. pfn + (addr >> PAGE_SHIFT), prot))
  1245. return -ENOMEM;
  1246. } while (pud++, addr = next, addr != end);
  1247. return 0;
  1248. }
  1249. /**
  1250. * remap_pfn_range - remap kernel memory to userspace
  1251. * @vma: user vma to map to
  1252. * @addr: target user address to start at
  1253. * @pfn: physical address of kernel memory
  1254. * @size: size of map area
  1255. * @prot: page protection flags for this mapping
  1256. *
  1257. * Note: this is only safe if the mm semaphore is held when called.
  1258. */
  1259. int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
  1260. unsigned long pfn, unsigned long size, pgprot_t prot)
  1261. {
  1262. pgd_t *pgd;
  1263. unsigned long next;
  1264. unsigned long end = addr + PAGE_ALIGN(size);
  1265. struct mm_struct *mm = vma->vm_mm;
  1266. int err;
  1267. /*
  1268. * Physically remapped pages are special. Tell the
  1269. * rest of the world about it:
  1270. * VM_IO tells people not to look at these pages
  1271. * (accesses can have side effects).
  1272. * VM_RESERVED is specified all over the place, because
  1273. * in 2.4 it kept swapout's vma scan off this vma; but
  1274. * in 2.6 the LRU scan won't even find its pages, so this
  1275. * flag means no more than count its pages in reserved_vm,
  1276. * and omit it from core dump, even when VM_IO turned off.
  1277. * VM_PFNMAP tells the core MM that the base pages are just
  1278. * raw PFN mappings, and do not have a "struct page" associated
  1279. * with them.
  1280. *
  1281. * There's a horrible special case to handle copy-on-write
  1282. * behaviour that some programs depend on. We mark the "original"
  1283. * un-COW'ed pages by matching them up with "vma->vm_pgoff".
  1284. */
  1285. if (is_cow_mapping(vma->vm_flags)) {
  1286. if (addr != vma->vm_start || end != vma->vm_end)
  1287. return -EINVAL;
  1288. vma->vm_pgoff = pfn;
  1289. }
  1290. vma->vm_flags |= VM_IO | VM_RESERVED | VM_PFNMAP;
  1291. BUG_ON(addr >= end);
  1292. pfn -= addr >> PAGE_SHIFT;
  1293. pgd = pgd_offset(mm, addr);
  1294. flush_cache_range(vma, addr, end);
  1295. do {
  1296. next = pgd_addr_end(addr, end);
  1297. err = remap_pud_range(mm, pgd, addr, next,
  1298. pfn + (addr >> PAGE_SHIFT), prot);
  1299. if (err)
  1300. break;
  1301. } while (pgd++, addr = next, addr != end);
  1302. return err;
  1303. }
  1304. EXPORT_SYMBOL(remap_pfn_range);
  1305. static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd,
  1306. unsigned long addr, unsigned long end,
  1307. pte_fn_t fn, void *data)
  1308. {
  1309. pte_t *pte;
  1310. int err;
  1311. struct page *pmd_page;
  1312. spinlock_t *uninitialized_var(ptl);
  1313. pte = (mm == &init_mm) ?
  1314. pte_alloc_kernel(pmd, addr) :
  1315. pte_alloc_map_lock(mm, pmd, addr, &ptl);
  1316. if (!pte)
  1317. return -ENOMEM;
  1318. BUG_ON(pmd_huge(*pmd));
  1319. pmd_page = pmd_page(*pmd);
  1320. do {
  1321. err = fn(pte, pmd_page, addr, data);
  1322. if (err)
  1323. break;
  1324. } while (pte++, addr += PAGE_SIZE, addr != end);
  1325. if (mm != &init_mm)
  1326. pte_unmap_unlock(pte-1, ptl);
  1327. return err;
  1328. }
  1329. static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud,
  1330. unsigned long addr, unsigned long end,
  1331. pte_fn_t fn, void *data)
  1332. {
  1333. pmd_t *pmd;
  1334. unsigned long next;
  1335. int err;
  1336. pmd = pmd_alloc(mm, pud, addr);
  1337. if (!pmd)
  1338. return -ENOMEM;
  1339. do {
  1340. next = pmd_addr_end(addr, end);
  1341. err = apply_to_pte_range(mm, pmd, addr, next, fn, data);
  1342. if (err)
  1343. break;
  1344. } while (pmd++, addr = next, addr != end);
  1345. return err;
  1346. }
  1347. static int apply_to_pud_range(struct mm_struct *mm, pgd_t *pgd,
  1348. unsigned long addr, unsigned long end,
  1349. pte_fn_t fn, void *data)
  1350. {
  1351. pud_t *pud;
  1352. unsigned long next;
  1353. int err;
  1354. pud = pud_alloc(mm, pgd, addr);
  1355. if (!pud)
  1356. return -ENOMEM;
  1357. do {
  1358. next = pud_addr_end(addr, end);
  1359. err = apply_to_pmd_range(mm, pud, addr, next, fn, data);
  1360. if (err)
  1361. break;
  1362. } while (pud++, addr = next, addr != end);
  1363. return err;
  1364. }
  1365. /*
  1366. * Scan a region of virtual memory, filling in page tables as necessary
  1367. * and calling a provided function on each leaf page table.
  1368. */
  1369. int apply_to_page_range(struct mm_struct *mm, unsigned long addr,
  1370. unsigned long size, pte_fn_t fn, void *data)
  1371. {
  1372. pgd_t *pgd;
  1373. unsigned long next;
  1374. unsigned long end = addr + size;
  1375. int err;
  1376. BUG_ON(addr >= end);
  1377. pgd = pgd_offset(mm, addr);
  1378. do {
  1379. next = pgd_addr_end(addr, end);
  1380. err = apply_to_pud_range(mm, pgd, addr, next, fn, data);
  1381. if (err)
  1382. break;
  1383. } while (pgd++, addr = next, addr != end);
  1384. return err;
  1385. }
  1386. EXPORT_SYMBOL_GPL(apply_to_page_range);
  1387. /*
  1388. * handle_pte_fault chooses page fault handler according to an entry
  1389. * which was read non-atomically. Before making any commitment, on
  1390. * those architectures or configurations (e.g. i386 with PAE) which
  1391. * might give a mix of unmatched parts, do_swap_page and do_file_page
  1392. * must check under lock before unmapping the pte and proceeding
  1393. * (but do_wp_page is only called after already making such a check;
  1394. * and do_anonymous_page and do_no_page can safely check later on).
  1395. */
  1396. static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd,
  1397. pte_t *page_table, pte_t orig_pte)
  1398. {
  1399. int same = 1;
  1400. #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
  1401. if (sizeof(pte_t) > sizeof(unsigned long)) {
  1402. spinlock_t *ptl = pte_lockptr(mm, pmd);
  1403. spin_lock(ptl);
  1404. same = pte_same(*page_table, orig_pte);
  1405. spin_unlock(ptl);
  1406. }
  1407. #endif
  1408. pte_unmap(page_table);
  1409. return same;
  1410. }
  1411. /*
  1412. * Do pte_mkwrite, but only if the vma says VM_WRITE. We do this when
  1413. * servicing faults for write access. In the normal case, do always want
  1414. * pte_mkwrite. But get_user_pages can cause write faults for mappings
  1415. * that do not have writing enabled, when used by access_process_vm.
  1416. */
  1417. static inline pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma)
  1418. {
  1419. if (likely(vma->vm_flags & VM_WRITE))
  1420. pte = pte_mkwrite(pte);
  1421. return pte;
  1422. }
  1423. static inline void cow_user_page(struct page *dst, struct page *src, unsigned long va, struct vm_area_struct *vma)
  1424. {
  1425. /*
  1426. * If the source page was a PFN mapping, we don't have
  1427. * a "struct page" for it. We do a best-effort copy by
  1428. * just copying from the original user address. If that
  1429. * fails, we just zero-fill it. Live with it.
  1430. */
  1431. if (unlikely(!src)) {
  1432. void *kaddr = kmap_atomic(dst, KM_USER0);
  1433. void __user *uaddr = (void __user *)(va & PAGE_MASK);
  1434. /*
  1435. * This really shouldn't fail, because the page is there
  1436. * in the page tables. But it might just be unreadable,
  1437. * in which case we just give up and fill the result with
  1438. * zeroes.
  1439. */
  1440. if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE))
  1441. memset(kaddr, 0, PAGE_SIZE);
  1442. kunmap_atomic(kaddr, KM_USER0);
  1443. flush_dcache_page(dst);
  1444. return;
  1445. }
  1446. copy_user_highpage(dst, src, va, vma);
  1447. }
  1448. /*
  1449. * This routine handles present pages, when users try to write
  1450. * to a shared page. It is done by copying the page to a new address
  1451. * and decrementing the shared-page counter for the old page.
  1452. *
  1453. * Note that this routine assumes that the protection checks have been
  1454. * done by the caller (the low-level page fault routine in most cases).
  1455. * Thus we can safely just mark it writable once we've done any necessary
  1456. * COW.
  1457. *
  1458. * We also mark the page dirty at this point even though the page will
  1459. * change only once the write actually happens. This avoids a few races,
  1460. * and potentially makes it more efficient.
  1461. *
  1462. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  1463. * but allow concurrent faults), with pte both mapped and locked.
  1464. * We return with mmap_sem still held, but pte unmapped and unlocked.
  1465. */
  1466. static int do_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
  1467. unsigned long address, pte_t *page_table, pmd_t *pmd,
  1468. spinlock_t *ptl, pte_t orig_pte)
  1469. {
  1470. struct page *old_page, *new_page;
  1471. pte_t entry;
  1472. int reuse = 0, ret = 0;
  1473. int page_mkwrite = 0;
  1474. struct page *dirty_page = NULL;
  1475. old_page = vm_normal_page(vma, address, orig_pte);
  1476. if (!old_page)
  1477. goto gotten;
  1478. /*
  1479. * Take out anonymous pages first, anonymous shared vmas are
  1480. * not dirty accountable.
  1481. */
  1482. if (PageAnon(old_page)) {
  1483. if (!TestSetPageLocked(old_page)) {
  1484. reuse = can_share_swap_page(old_page);
  1485. unlock_page(old_page);
  1486. }
  1487. } else if (unlikely((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
  1488. (VM_WRITE|VM_SHARED))) {
  1489. /*
  1490. * Only catch write-faults on shared writable pages,
  1491. * read-only shared pages can get COWed by
  1492. * get_user_pages(.write=1, .force=1).
  1493. */
  1494. if (vma->vm_ops && vma->vm_ops->page_mkwrite) {
  1495. /*
  1496. * Notify the address space that the page is about to
  1497. * become writable so that it can prohibit this or wait
  1498. * for the page to get into an appropriate state.
  1499. *
  1500. * We do this without the lock held, so that it can
  1501. * sleep if it needs to.
  1502. */
  1503. page_cache_get(old_page);
  1504. pte_unmap_unlock(page_table, ptl);
  1505. if (vma->vm_ops->page_mkwrite(vma, old_page) < 0)
  1506. goto unwritable_page;
  1507. /*
  1508. * Since we dropped the lock we need to revalidate
  1509. * the PTE as someone else may have changed it. If
  1510. * they did, we just return, as we can count on the
  1511. * MMU to tell us if they didn't also make it writable.
  1512. */
  1513. page_table = pte_offset_map_lock(mm, pmd, address,
  1514. &ptl);
  1515. page_cache_release(old_page);
  1516. if (!pte_same(*page_table, orig_pte))
  1517. goto unlock;
  1518. page_mkwrite = 1;
  1519. }
  1520. dirty_page = old_page;
  1521. get_page(dirty_page);
  1522. reuse = 1;
  1523. }
  1524. if (reuse) {
  1525. flush_cache_page(vma, address, pte_pfn(orig_pte));
  1526. entry = pte_mkyoung(orig_pte);
  1527. entry = maybe_mkwrite(pte_mkdirty(entry), vma);
  1528. if (ptep_set_access_flags(vma, address, page_table, entry,1)) {
  1529. update_mmu_cache(vma, address, entry);
  1530. lazy_mmu_prot_update(entry);
  1531. }
  1532. ret |= VM_FAULT_WRITE;
  1533. goto unlock;
  1534. }
  1535. /*
  1536. * Ok, we need to copy. Oh, well..
  1537. */
  1538. page_cache_get(old_page);
  1539. gotten:
  1540. pte_unmap_unlock(page_table, ptl);
  1541. if (unlikely(anon_vma_prepare(vma)))
  1542. goto oom;
  1543. if (old_page == ZERO_PAGE(address)) {
  1544. new_page = alloc_zeroed_user_highpage_movable(vma, address);
  1545. if (!new_page)
  1546. goto oom;
  1547. } else {
  1548. new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address);
  1549. if (!new_page)
  1550. goto oom;
  1551. cow_user_page(new_page, old_page, address, vma);
  1552. }
  1553. /*
  1554. * Re-check the pte - we dropped the lock
  1555. */
  1556. page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
  1557. if (likely(pte_same(*page_table, orig_pte))) {
  1558. if (old_page) {
  1559. page_remove_rmap(old_page, vma);
  1560. if (!PageAnon(old_page)) {
  1561. dec_mm_counter(mm, file_rss);
  1562. inc_mm_counter(mm, anon_rss);
  1563. }
  1564. } else
  1565. inc_mm_counter(mm, anon_rss);
  1566. flush_cache_page(vma, address, pte_pfn(orig_pte));
  1567. entry = mk_pte(new_page, vma->vm_page_prot);
  1568. entry = maybe_mkwrite(pte_mkdirty(entry), vma);
  1569. lazy_mmu_prot_update(entry);
  1570. /*
  1571. * Clear the pte entry and flush it first, before updating the
  1572. * pte with the new entry. This will avoid a race condition
  1573. * seen in the presence of one thread doing SMC and another
  1574. * thread doing COW.
  1575. */
  1576. ptep_clear_flush(vma, address, page_table);
  1577. set_pte_at(mm, address, page_table, entry);
  1578. update_mmu_cache(vma, address, entry);
  1579. lru_cache_add_active(new_page);
  1580. page_add_new_anon_rmap(new_page, vma, address);
  1581. /* Free the old page.. */
  1582. new_page = old_page;
  1583. ret |= VM_FAULT_WRITE;
  1584. }
  1585. if (new_page)
  1586. page_cache_release(new_page);
  1587. if (old_page)
  1588. page_cache_release(old_page);
  1589. unlock:
  1590. pte_unmap_unlock(page_table, ptl);
  1591. if (dirty_page) {
  1592. /*
  1593. * Yes, Virginia, this is actually required to prevent a race
  1594. * with clear_page_dirty_for_io() from clearing the page dirty
  1595. * bit after it clear all dirty ptes, but before a racing
  1596. * do_wp_page installs a dirty pte.
  1597. *
  1598. * do_no_page is protected similarly.
  1599. */
  1600. wait_on_page_locked(dirty_page);
  1601. set_page_dirty_balance(dirty_page, page_mkwrite);
  1602. put_page(dirty_page);
  1603. }
  1604. return ret;
  1605. oom:
  1606. if (old_page)
  1607. page_cache_release(old_page);
  1608. return VM_FAULT_OOM;
  1609. unwritable_page:
  1610. page_cache_release(old_page);
  1611. return VM_FAULT_SIGBUS;
  1612. }
  1613. /*
  1614. * Helper functions for unmap_mapping_range().
  1615. *
  1616. * __ Notes on dropping i_mmap_lock to reduce latency while unmapping __
  1617. *
  1618. * We have to restart searching the prio_tree whenever we drop the lock,
  1619. * since the iterator is only valid while the lock is held, and anyway
  1620. * a later vma might be split and reinserted earlier while lock dropped.
  1621. *
  1622. * The list of nonlinear vmas could be handled more efficiently, using
  1623. * a placeholder, but handle it in the same way until a need is shown.
  1624. * It is important to search the prio_tree before nonlinear list: a vma
  1625. * may become nonlinear and be shifted from prio_tree to nonlinear list
  1626. * while the lock is dropped; but never shifted from list to prio_tree.
  1627. *
  1628. * In order to make forward progress despite restarting the search,
  1629. * vm_truncate_count is used to mark a vma as now dealt with, so we can
  1630. * quickly skip it next time around. Since the prio_tree search only
  1631. * shows us those vmas affected by unmapping the range in question, we
  1632. * can't efficiently keep all vmas in step with mapping->truncate_count:
  1633. * so instead reset them all whenever it wraps back to 0 (then go to 1).
  1634. * mapping->truncate_count and vma->vm_truncate_count are protected by
  1635. * i_mmap_lock.
  1636. *
  1637. * In order to make forward progress despite repeatedly restarting some
  1638. * large vma, note the restart_addr from unmap_vmas when it breaks out:
  1639. * and restart from that address when we reach that vma again. It might
  1640. * have been split or merged, shrunk or extended, but never shifted: so
  1641. * restart_addr remains valid so long as it remains in the vma's range.
  1642. * unmap_mapping_range forces truncate_count to leap over page-aligned
  1643. * values so we can save vma's restart_addr in its truncate_count field.
  1644. */
  1645. #define is_restart_addr(truncate_count) (!((truncate_count) & ~PAGE_MASK))
  1646. static void reset_vma_truncate_counts(struct address_space *mapping)
  1647. {
  1648. struct vm_area_struct *vma;
  1649. struct prio_tree_iter iter;
  1650. vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, 0, ULONG_MAX)
  1651. vma->vm_truncate_count = 0;
  1652. list_for_each_entry(vma, &mapping->i_mmap_nonlinear, shared.vm_set.list)
  1653. vma->vm_truncate_count = 0;
  1654. }
  1655. static int unmap_mapping_range_vma(struct vm_area_struct *vma,
  1656. unsigned long start_addr, unsigned long end_addr,
  1657. struct zap_details *details)
  1658. {
  1659. unsigned long restart_addr;
  1660. int need_break;
  1661. /*
  1662. * files that support invalidating or truncating portions of the
  1663. * file from under mmaped areas must have their ->fault function
  1664. * return a locked page (and set VM_FAULT_LOCKED in the return).
  1665. * This provides synchronisation against concurrent unmapping here.
  1666. */
  1667. again:
  1668. restart_addr = vma->vm_truncate_count;
  1669. if (is_restart_addr(restart_addr) && start_addr < restart_addr) {
  1670. start_addr = restart_addr;
  1671. if (start_addr >= end_addr) {
  1672. /* Top of vma has been split off since last time */
  1673. vma->vm_truncate_count = details->truncate_count;
  1674. return 0;
  1675. }
  1676. }
  1677. restart_addr = zap_page_range(vma, start_addr,
  1678. end_addr - start_addr, details);
  1679. need_break = need_resched() ||
  1680. need_lockbreak(details->i_mmap_lock);
  1681. if (restart_addr >= end_addr) {
  1682. /* We have now completed this vma: mark it so */
  1683. vma->vm_truncate_count = details->truncate_count;
  1684. if (!need_break)
  1685. return 0;
  1686. } else {
  1687. /* Note restart_addr in vma's truncate_count field */
  1688. vma->vm_truncate_count = restart_addr;
  1689. if (!need_break)
  1690. goto again;
  1691. }
  1692. spin_unlock(details->i_mmap_lock);
  1693. cond_resched();
  1694. spin_lock(details->i_mmap_lock);
  1695. return -EINTR;
  1696. }
  1697. static inline void unmap_mapping_range_tree(struct prio_tree_root *root,
  1698. struct zap_details *details)
  1699. {
  1700. struct vm_area_struct *vma;
  1701. struct prio_tree_iter iter;
  1702. pgoff_t vba, vea, zba, zea;
  1703. restart:
  1704. vma_prio_tree_foreach(vma, &iter, root,
  1705. details->first_index, details->last_index) {
  1706. /* Skip quickly over those we have already dealt with */
  1707. if (vma->vm_truncate_count == details->truncate_count)
  1708. continue;
  1709. vba = vma->vm_pgoff;
  1710. vea = vba + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) - 1;
  1711. /* Assume for now that PAGE_CACHE_SHIFT == PAGE_SHIFT */
  1712. zba = details->first_index;
  1713. if (zba < vba)
  1714. zba = vba;
  1715. zea = details->last_index;
  1716. if (zea > vea)
  1717. zea = vea;
  1718. if (unmap_mapping_range_vma(vma,
  1719. ((zba - vba) << PAGE_SHIFT) + vma->vm_start,
  1720. ((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start,
  1721. details) < 0)
  1722. goto restart;
  1723. }
  1724. }
  1725. static inline void unmap_mapping_range_list(struct list_head *head,
  1726. struct zap_details *details)
  1727. {
  1728. struct vm_area_struct *vma;
  1729. /*
  1730. * In nonlinear VMAs there is no correspondence between virtual address
  1731. * offset and file offset. So we must perform an exhaustive search
  1732. * across *all* the pages in each nonlinear VMA, not just the pages
  1733. * whose virtual address lies outside the file truncation point.
  1734. */
  1735. restart:
  1736. list_for_each_entry(vma, head, shared.vm_set.list) {
  1737. /* Skip quickly over those we have already dealt with */
  1738. if (vma->vm_truncate_count == details->truncate_count)
  1739. continue;
  1740. details->nonlinear_vma = vma;
  1741. if (unmap_mapping_range_vma(vma, vma->vm_start,
  1742. vma->vm_end, details) < 0)
  1743. goto restart;
  1744. }
  1745. }
  1746. /**
  1747. * unmap_mapping_range - unmap the portion of all mmaps in the specified address_space corresponding to the specified page range in the underlying file.
  1748. * @mapping: the address space containing mmaps to be unmapped.
  1749. * @holebegin: byte in first page to unmap, relative to the start of
  1750. * the underlying file. This will be rounded down to a PAGE_SIZE
  1751. * boundary. Note that this is different from vmtruncate(), which
  1752. * must keep the partial page. In contrast, we must get rid of
  1753. * partial pages.
  1754. * @holelen: size of prospective hole in bytes. This will be rounded
  1755. * up to a PAGE_SIZE boundary. A holelen of zero truncates to the
  1756. * end of the file.
  1757. * @even_cows: 1 when truncating a file, unmap even private COWed pages;
  1758. * but 0 when invalidating pagecache, don't throw away private data.
  1759. */
  1760. void unmap_mapping_range(struct address_space *mapping,
  1761. loff_t const holebegin, loff_t const holelen, int even_cows)
  1762. {
  1763. struct zap_details details;
  1764. pgoff_t hba = holebegin >> PAGE_SHIFT;
  1765. pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1766. /* Check for overflow. */
  1767. if (sizeof(holelen) > sizeof(hlen)) {
  1768. long long holeend =
  1769. (holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1770. if (holeend & ~(long long)ULONG_MAX)
  1771. hlen = ULONG_MAX - hba + 1;
  1772. }
  1773. details.check_mapping = even_cows? NULL: mapping;
  1774. details.nonlinear_vma = NULL;
  1775. details.first_index = hba;
  1776. details.last_index = hba + hlen - 1;
  1777. if (details.last_index < details.first_index)
  1778. details.last_index = ULONG_MAX;
  1779. details.i_mmap_lock = &mapping->i_mmap_lock;
  1780. spin_lock(&mapping->i_mmap_lock);
  1781. /* Protect against endless unmapping loops */
  1782. mapping->truncate_count++;
  1783. if (unlikely(is_restart_addr(mapping->truncate_count))) {
  1784. if (mapping->truncate_count == 0)
  1785. reset_vma_truncate_counts(mapping);
  1786. mapping->truncate_count++;
  1787. }
  1788. details.truncate_count = mapping->truncate_count;
  1789. if (unlikely(!prio_tree_empty(&mapping->i_mmap)))
  1790. unmap_mapping_range_tree(&mapping->i_mmap, &details);
  1791. if (unlikely(!list_empty(&mapping->i_mmap_nonlinear)))
  1792. unmap_mapping_range_list(&mapping->i_mmap_nonlinear, &details);
  1793. spin_unlock(&mapping->i_mmap_lock);
  1794. }
  1795. EXPORT_SYMBOL(unmap_mapping_range);
  1796. /**
  1797. * vmtruncate - unmap mappings "freed" by truncate() syscall
  1798. * @inode: inode of the file used
  1799. * @offset: file offset to start truncating
  1800. *
  1801. * NOTE! We have to be ready to update the memory sharing
  1802. * between the file and the memory map for a potential last
  1803. * incomplete page. Ugly, but necessary.
  1804. */
  1805. int vmtruncate(struct inode * inode, loff_t offset)
  1806. {
  1807. struct address_space *mapping = inode->i_mapping;
  1808. unsigned long limit;
  1809. if (inode->i_size < offset)
  1810. goto do_expand;
  1811. /*
  1812. * truncation of in-use swapfiles is disallowed - it would cause
  1813. * subsequent swapout to scribble on the now-freed blocks.
  1814. */
  1815. if (IS_SWAPFILE(inode))
  1816. goto out_busy;
  1817. i_size_write(inode, offset);
  1818. /*
  1819. * unmap_mapping_range is called twice, first simply for efficiency
  1820. * so that truncate_inode_pages does fewer single-page unmaps. However
  1821. * after this first call, and before truncate_inode_pages finishes,
  1822. * it is possible for private pages to be COWed, which remain after
  1823. * truncate_inode_pages finishes, hence the second unmap_mapping_range
  1824. * call must be made for correctness.
  1825. */
  1826. unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
  1827. truncate_inode_pages(mapping, offset);
  1828. unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
  1829. goto out_truncate;
  1830. do_expand:
  1831. limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
  1832. if (limit != RLIM_INFINITY && offset > limit)
  1833. goto out_sig;
  1834. if (offset > inode->i_sb->s_maxbytes)
  1835. goto out_big;
  1836. i_size_write(inode, offset);
  1837. out_truncate:
  1838. if (inode->i_op && inode->i_op->truncate)
  1839. inode->i_op->truncate(inode);
  1840. return 0;
  1841. out_sig:
  1842. send_sig(SIGXFSZ, current, 0);
  1843. out_big:
  1844. return -EFBIG;
  1845. out_busy:
  1846. return -ETXTBSY;
  1847. }
  1848. EXPORT_SYMBOL(vmtruncate);
  1849. int vmtruncate_range(struct inode *inode, loff_t offset, loff_t end)
  1850. {
  1851. struct address_space *mapping = inode->i_mapping;
  1852. /*
  1853. * If the underlying filesystem is not going to provide
  1854. * a way to truncate a range of blocks (punch a hole) -
  1855. * we should return failure right now.
  1856. */
  1857. if (!inode->i_op || !inode->i_op->truncate_range)
  1858. return -ENOSYS;
  1859. mutex_lock(&inode->i_mutex);
  1860. down_write(&inode->i_alloc_sem);
  1861. unmap_mapping_range(mapping, offset, (end - offset), 1);
  1862. truncate_inode_pages_range(mapping, offset, end);
  1863. unmap_mapping_range(mapping, offset, (end - offset), 1);
  1864. inode->i_op->truncate_range(inode, offset, end);
  1865. up_write(&inode->i_alloc_sem);
  1866. mutex_unlock(&inode->i_mutex);
  1867. return 0;
  1868. }
  1869. /**
  1870. * swapin_readahead - swap in pages in hope we need them soon
  1871. * @entry: swap entry of this memory
  1872. * @addr: address to start
  1873. * @vma: user vma this addresses belong to
  1874. *
  1875. * Primitive swap readahead code. We simply read an aligned block of
  1876. * (1 << page_cluster) entries in the swap area. This method is chosen
  1877. * because it doesn't cost us any seek time. We also make sure to queue
  1878. * the 'original' request together with the readahead ones...
  1879. *
  1880. * This has been extended to use the NUMA policies from the mm triggering
  1881. * the readahead.
  1882. *
  1883. * Caller must hold down_read on the vma->vm_mm if vma is not NULL.
  1884. */
  1885. void swapin_readahead(swp_entry_t entry, unsigned long addr,struct vm_area_struct *vma)
  1886. {
  1887. #ifdef CONFIG_NUMA
  1888. struct vm_area_struct *next_vma = vma ? vma->vm_next : NULL;
  1889. #endif
  1890. int i, num;
  1891. struct page *new_page;
  1892. unsigned long offset;
  1893. /*
  1894. * Get the number of handles we should do readahead io to.
  1895. */
  1896. num = valid_swaphandles(entry, &offset);
  1897. for (i = 0; i < num; offset++, i++) {
  1898. /* Ok, do the async read-ahead now */
  1899. new_page = read_swap_cache_async(swp_entry(swp_type(entry),
  1900. offset), vma, addr);
  1901. if (!new_page)
  1902. break;
  1903. page_cache_release(new_page);
  1904. #ifdef CONFIG_NUMA
  1905. /*
  1906. * Find the next applicable VMA for the NUMA policy.
  1907. */
  1908. addr += PAGE_SIZE;
  1909. if (addr == 0)
  1910. vma = NULL;
  1911. if (vma) {
  1912. if (addr >= vma->vm_end) {
  1913. vma = next_vma;
  1914. next_vma = vma ? vma->vm_next : NULL;
  1915. }
  1916. if (vma && addr < vma->vm_start)
  1917. vma = NULL;
  1918. } else {
  1919. if (next_vma && addr >= next_vma->vm_start) {
  1920. vma = next_vma;
  1921. next_vma = vma->vm_next;
  1922. }
  1923. }
  1924. #endif
  1925. }
  1926. lru_add_drain(); /* Push any new pages onto the LRU now */
  1927. }
  1928. /*
  1929. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  1930. * but allow concurrent faults), and pte mapped but not yet locked.
  1931. * We return with mmap_sem still held, but pte unmapped and unlocked.
  1932. */
  1933. static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma,
  1934. unsigned long address, pte_t *page_table, pmd_t *pmd,
  1935. int write_access, pte_t orig_pte)
  1936. {
  1937. spinlock_t *ptl;
  1938. struct page *page;
  1939. swp_entry_t entry;
  1940. pte_t pte;
  1941. int ret = 0;
  1942. if (!pte_unmap_same(mm, pmd, page_table, orig_pte))
  1943. goto out;
  1944. entry = pte_to_swp_entry(orig_pte);
  1945. if (is_migration_entry(entry)) {
  1946. migration_entry_wait(mm, pmd, address);
  1947. goto out;
  1948. }
  1949. delayacct_set_flag(DELAYACCT_PF_SWAPIN);
  1950. page = lookup_swap_cache(entry);
  1951. if (!page) {
  1952. grab_swap_token(); /* Contend for token _before_ read-in */
  1953. swapin_readahead(entry, address, vma);
  1954. page = read_swap_cache_async(entry, vma, address);
  1955. if (!page) {
  1956. /*
  1957. * Back out if somebody else faulted in this pte
  1958. * while we released the pte lock.
  1959. */
  1960. page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
  1961. if (likely(pte_same(*page_table, orig_pte)))
  1962. ret = VM_FAULT_OOM;
  1963. delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
  1964. goto unlock;
  1965. }
  1966. /* Had to read the page from swap area: Major fault */
  1967. ret = VM_FAULT_MAJOR;
  1968. count_vm_event(PGMAJFAULT);
  1969. }
  1970. delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
  1971. mark_page_accessed(page);
  1972. lock_page(page);
  1973. /*
  1974. * Back out if somebody else already faulted in this pte.
  1975. */
  1976. page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
  1977. if (unlikely(!pte_same(*page_table, orig_pte)))
  1978. goto out_nomap;
  1979. if (unlikely(!PageUptodate(page))) {
  1980. ret = VM_FAULT_SIGBUS;
  1981. goto out_nomap;
  1982. }
  1983. /* The page isn't present yet, go ahead with the fault. */
  1984. inc_mm_counter(mm, anon_rss);
  1985. pte = mk_pte(page, vma->vm_page_prot);
  1986. if (write_access && can_share_swap_page(page)) {
  1987. pte = maybe_mkwrite(pte_mkdirty(pte), vma);
  1988. write_access = 0;
  1989. }
  1990. flush_icache_page(vma, page);
  1991. set_pte_at(mm, address, page_table, pte);
  1992. page_add_anon_rmap(page, vma, address);
  1993. swap_free(entry);
  1994. if (vm_swap_full())
  1995. remove_exclusive_swap_page(page);
  1996. unlock_page(page);
  1997. if (write_access) {
  1998. /* XXX: We could OR the do_wp_page code with this one? */
  1999. if (do_wp_page(mm, vma, address,
  2000. page_table, pmd, ptl, pte) & VM_FAULT_OOM)
  2001. ret = VM_FAULT_OOM;
  2002. goto out;
  2003. }
  2004. /* No need to invalidate - it was non-present before */
  2005. update_mmu_cache(vma, address, pte);
  2006. unlock:
  2007. pte_unmap_unlock(page_table, ptl);
  2008. out:
  2009. return ret;
  2010. out_nomap:
  2011. pte_unmap_unlock(page_table, ptl);
  2012. unlock_page(page);
  2013. page_cache_release(page);
  2014. return ret;
  2015. }
  2016. /*
  2017. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  2018. * but allow concurrent faults), and pte mapped but not yet locked.
  2019. * We return with mmap_sem still held, but pte unmapped and unlocked.
  2020. */
  2021. static int do_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
  2022. unsigned long address, pte_t *page_table, pmd_t *pmd,
  2023. int write_access)
  2024. {
  2025. struct page *page;
  2026. spinlock_t *ptl;
  2027. pte_t entry;
  2028. if (write_access) {
  2029. /* Allocate our own private page. */
  2030. pte_unmap(page_table);
  2031. if (unlikely(anon_vma_prepare(vma)))
  2032. goto oom;
  2033. page = alloc_zeroed_user_highpage_movable(vma, address);
  2034. if (!page)
  2035. goto oom;
  2036. entry = mk_pte(page, vma->vm_page_prot);
  2037. entry = maybe_mkwrite(pte_mkdirty(entry), vma);
  2038. page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
  2039. if (!pte_none(*page_table))
  2040. goto release;
  2041. inc_mm_counter(mm, anon_rss);
  2042. lru_cache_add_active(page);
  2043. page_add_new_anon_rmap(page, vma, address);
  2044. } else {
  2045. /* Map the ZERO_PAGE - vm_page_prot is readonly */
  2046. page = ZERO_PAGE(address);
  2047. page_cache_get(page);
  2048. entry = mk_pte(page, vma->vm_page_prot);
  2049. ptl = pte_lockptr(mm, pmd);
  2050. spin_lock(ptl);
  2051. if (!pte_none(*page_table))
  2052. goto release;
  2053. inc_mm_counter(mm, file_rss);
  2054. page_add_file_rmap(page);
  2055. }
  2056. set_pte_at(mm, address, page_table, entry);
  2057. /* No need to invalidate - it was non-present before */
  2058. update_mmu_cache(vma, address, entry);
  2059. lazy_mmu_prot_update(entry);
  2060. unlock:
  2061. pte_unmap_unlock(page_table, ptl);
  2062. return 0;
  2063. release:
  2064. page_cache_release(page);
  2065. goto unlock;
  2066. oom:
  2067. return VM_FAULT_OOM;
  2068. }
  2069. /*
  2070. * __do_fault() tries to create a new page mapping. It aggressively
  2071. * tries to share with existing pages, but makes a separate copy if
  2072. * the FAULT_FLAG_WRITE is set in the flags parameter in order to avoid
  2073. * the next page fault.
  2074. *
  2075. * As this is called only for pages that do not currently exist, we
  2076. * do not need to flush old virtual caches or the TLB.
  2077. *
  2078. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  2079. * but allow concurrent faults), and pte neither mapped nor locked.
  2080. * We return with mmap_sem still held, but pte unmapped and unlocked.
  2081. */
  2082. static int __do_fault(struct mm_struct *mm, struct vm_area_struct *vma,
  2083. unsigned long address, pmd_t *pmd,
  2084. pgoff_t pgoff, unsigned int flags, pte_t orig_pte)
  2085. {
  2086. pte_t *page_table;
  2087. spinlock_t *ptl;
  2088. struct page *page;
  2089. pte_t entry;
  2090. int anon = 0;
  2091. struct page *dirty_page = NULL;
  2092. struct vm_fault vmf;
  2093. int ret;
  2094. int page_mkwrite = 0;
  2095. vmf.virtual_address = (void __user *)(address & PAGE_MASK);
  2096. vmf.pgoff = pgoff;
  2097. vmf.flags = flags;
  2098. vmf.page = NULL;
  2099. BUG_ON(vma->vm_flags & VM_PFNMAP);
  2100. if (likely(vma->vm_ops->fault)) {
  2101. ret = vma->vm_ops->fault(vma, &vmf);
  2102. if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))
  2103. return ret;
  2104. } else {
  2105. /* Legacy ->nopage path */
  2106. ret = 0;
  2107. vmf.page = vma->vm_ops->nopage(vma, address & PAGE_MASK, &ret);
  2108. /* no page was available -- either SIGBUS or OOM */
  2109. if (unlikely(vmf.page == NOPAGE_SIGBUS))
  2110. return VM_FAULT_SIGBUS;
  2111. else if (unlikely(vmf.page == NOPAGE_OOM))
  2112. return VM_FAULT_OOM;
  2113. }
  2114. /*
  2115. * For consistency in subsequent calls, make the faulted page always
  2116. * locked.
  2117. */
  2118. if (unlikely(!(ret & VM_FAULT_LOCKED)))
  2119. lock_page(vmf.page);
  2120. else
  2121. VM_BUG_ON(!PageLocked(vmf.page));
  2122. /*
  2123. * Should we do an early C-O-W break?
  2124. */
  2125. page = vmf.page;
  2126. if (flags & FAULT_FLAG_WRITE) {
  2127. if (!(vma->vm_flags & VM_SHARED)) {
  2128. anon = 1;
  2129. if (unlikely(anon_vma_prepare(vma))) {
  2130. ret = VM_FAULT_OOM;
  2131. goto out;
  2132. }
  2133. page = alloc_page_vma(GFP_HIGHUSER_MOVABLE,
  2134. vma, address);
  2135. if (!page) {
  2136. ret = VM_FAULT_OOM;
  2137. goto out;
  2138. }
  2139. copy_user_highpage(page, vmf.page, address, vma);
  2140. } else {
  2141. /*
  2142. * If the page will be shareable, see if the backing
  2143. * address space wants to know that the page is about
  2144. * to become writable
  2145. */
  2146. if (vma->vm_ops->page_mkwrite) {
  2147. unlock_page(page);
  2148. if (vma->vm_ops->page_mkwrite(vma, page) < 0) {
  2149. ret = VM_FAULT_SIGBUS;
  2150. anon = 1; /* no anon but release vmf.page */
  2151. goto out_unlocked;
  2152. }
  2153. lock_page(page);
  2154. /*
  2155. * XXX: this is not quite right (racy vs
  2156. * invalidate) to unlock and relock the page
  2157. * like this, however a better fix requires
  2158. * reworking page_mkwrite locking API, which
  2159. * is better done later.
  2160. */
  2161. if (!page->mapping) {
  2162. ret = 0;
  2163. anon = 1; /* no anon but release vmf.page */
  2164. goto out;
  2165. }
  2166. page_mkwrite = 1;
  2167. }
  2168. }
  2169. }
  2170. page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
  2171. /*
  2172. * This silly early PAGE_DIRTY setting removes a race
  2173. * due to the bad i386 page protection. But it's valid
  2174. * for other architectures too.
  2175. *
  2176. * Note that if write_access is true, we either now have
  2177. * an exclusive copy of the page, or this is a shared mapping,
  2178. * so we can make it writable and dirty to avoid having to
  2179. * handle that later.
  2180. */
  2181. /* Only go through if we didn't race with anybody else... */
  2182. if (likely(pte_same(*page_table, orig_pte))) {
  2183. flush_icache_page(vma, page);
  2184. entry = mk_pte(page, vma->vm_page_prot);
  2185. if (flags & FAULT_FLAG_WRITE)
  2186. entry = maybe_mkwrite(pte_mkdirty(entry), vma);
  2187. set_pte_at(mm, address, page_table, entry);
  2188. if (anon) {
  2189. inc_mm_counter(mm, anon_rss);
  2190. lru_cache_add_active(page);
  2191. page_add_new_anon_rmap(page, vma, address);
  2192. } else {
  2193. inc_mm_counter(mm, file_rss);
  2194. page_add_file_rmap(page);
  2195. if (flags & FAULT_FLAG_WRITE) {
  2196. dirty_page = page;
  2197. get_page(dirty_page);
  2198. }
  2199. }
  2200. /* no need to invalidate: a not-present page won't be cached */
  2201. update_mmu_cache(vma, address, entry);
  2202. lazy_mmu_prot_update(entry);
  2203. } else {
  2204. if (anon)
  2205. page_cache_release(page);
  2206. else
  2207. anon = 1; /* no anon but release faulted_page */
  2208. }
  2209. pte_unmap_unlock(page_table, ptl);
  2210. out:
  2211. unlock_page(vmf.page);
  2212. out_unlocked:
  2213. if (anon)
  2214. page_cache_release(vmf.page);
  2215. else if (dirty_page) {
  2216. set_page_dirty_balance(dirty_page, page_mkwrite);
  2217. put_page(dirty_page);
  2218. }
  2219. return ret;
  2220. }
  2221. static int do_linear_fault(struct mm_struct *mm, struct vm_area_struct *vma,
  2222. unsigned long address, pte_t *page_table, pmd_t *pmd,
  2223. int write_access, pte_t orig_pte)
  2224. {
  2225. pgoff_t pgoff = (((address & PAGE_MASK)
  2226. - vma->vm_start) >> PAGE_CACHE_SHIFT) + vma->vm_pgoff;
  2227. unsigned int flags = (write_access ? FAULT_FLAG_WRITE : 0);
  2228. pte_unmap(page_table);
  2229. return __do_fault(mm, vma, address, pmd, pgoff, flags, orig_pte);
  2230. }
  2231. /*
  2232. * do_no_pfn() tries to create a new page mapping for a page without
  2233. * a struct_page backing it
  2234. *
  2235. * As this is called only for pages that do not currently exist, we
  2236. * do not need to flush old virtual caches or the TLB.
  2237. *
  2238. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  2239. * but allow concurrent faults), and pte mapped but not yet locked.
  2240. * We return with mmap_sem still held, but pte unmapped and unlocked.
  2241. *
  2242. * It is expected that the ->nopfn handler always returns the same pfn
  2243. * for a given virtual mapping.
  2244. *
  2245. * Mark this `noinline' to prevent it from bloating the main pagefault code.
  2246. */
  2247. static noinline int do_no_pfn(struct mm_struct *mm, struct vm_area_struct *vma,
  2248. unsigned long address, pte_t *page_table, pmd_t *pmd,
  2249. int write_access)
  2250. {
  2251. spinlock_t *ptl;
  2252. pte_t entry;
  2253. unsigned long pfn;
  2254. pte_unmap(page_table);
  2255. BUG_ON(!(vma->vm_flags & VM_PFNMAP));
  2256. BUG_ON(is_cow_mapping(vma->vm_flags));
  2257. pfn = vma->vm_ops->nopfn(vma, address & PAGE_MASK);
  2258. if (unlikely(pfn == NOPFN_OOM))
  2259. return VM_FAULT_OOM;
  2260. else if (unlikely(pfn == NOPFN_SIGBUS))
  2261. return VM_FAULT_SIGBUS;
  2262. else if (unlikely(pfn == NOPFN_REFAULT))
  2263. return 0;
  2264. page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
  2265. /* Only go through if we didn't race with anybody else... */
  2266. if (pte_none(*page_table)) {
  2267. entry = pfn_pte(pfn, vma->vm_page_prot);
  2268. if (write_access)
  2269. entry = maybe_mkwrite(pte_mkdirty(entry), vma);
  2270. set_pte_at(mm, address, page_table, entry);
  2271. }
  2272. pte_unmap_unlock(page_table, ptl);
  2273. return 0;
  2274. }
  2275. /*
  2276. * Fault of a previously existing named mapping. Repopulate the pte
  2277. * from the encoded file_pte if possible. This enables swappable
  2278. * nonlinear vmas.
  2279. *
  2280. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  2281. * but allow concurrent faults), and pte mapped but not yet locked.
  2282. * We return with mmap_sem still held, but pte unmapped and unlocked.
  2283. */
  2284. static int do_nonlinear_fault(struct mm_struct *mm, struct vm_area_struct *vma,
  2285. unsigned long address, pte_t *page_table, pmd_t *pmd,
  2286. int write_access, pte_t orig_pte)
  2287. {
  2288. unsigned int flags = FAULT_FLAG_NONLINEAR |
  2289. (write_access ? FAULT_FLAG_WRITE : 0);
  2290. pgoff_t pgoff;
  2291. if (!pte_unmap_same(mm, pmd, page_table, orig_pte))
  2292. return 0;
  2293. if (unlikely(!(vma->vm_flags & VM_NONLINEAR) ||
  2294. !(vma->vm_flags & VM_CAN_NONLINEAR))) {
  2295. /*
  2296. * Page table corrupted: show pte and kill process.
  2297. */
  2298. print_bad_pte(vma, orig_pte, address);
  2299. return VM_FAULT_OOM;
  2300. }
  2301. pgoff = pte_to_pgoff(orig_pte);
  2302. return __do_fault(mm, vma, address, pmd, pgoff, flags, orig_pte);
  2303. }
  2304. /*
  2305. * These routines also need to handle stuff like marking pages dirty
  2306. * and/or accessed for architectures that don't do it in hardware (most
  2307. * RISC architectures). The early dirtying is also good on the i386.
  2308. *
  2309. * There is also a hook called "update_mmu_cache()" that architectures
  2310. * with external mmu caches can use to update those (ie the Sparc or
  2311. * PowerPC hashed page tables that act as extended TLBs).
  2312. *
  2313. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  2314. * but allow concurrent faults), and pte mapped but not yet locked.
  2315. * We return with mmap_sem still held, but pte unmapped and unlocked.
  2316. */
  2317. static inline int handle_pte_fault(struct mm_struct *mm,
  2318. struct vm_area_struct *vma, unsigned long address,
  2319. pte_t *pte, pmd_t *pmd, int write_access)
  2320. {
  2321. pte_t entry;
  2322. spinlock_t *ptl;
  2323. entry = *pte;
  2324. if (!pte_present(entry)) {
  2325. if (pte_none(entry)) {
  2326. if (vma->vm_ops) {
  2327. if (vma->vm_ops->fault || vma->vm_ops->nopage)
  2328. return do_linear_fault(mm, vma, address,
  2329. pte, pmd, write_access, entry);
  2330. if (unlikely(vma->vm_ops->nopfn))
  2331. return do_no_pfn(mm, vma, address, pte,
  2332. pmd, write_access);
  2333. }
  2334. return do_anonymous_page(mm, vma, address,
  2335. pte, pmd, write_access);
  2336. }
  2337. if (pte_file(entry))
  2338. return do_nonlinear_fault(mm, vma, address,
  2339. pte, pmd, write_access, entry);
  2340. return do_swap_page(mm, vma, address,
  2341. pte, pmd, write_access, entry);
  2342. }
  2343. ptl = pte_lockptr(mm, pmd);
  2344. spin_lock(ptl);
  2345. if (unlikely(!pte_same(*pte, entry)))
  2346. goto unlock;
  2347. if (write_access) {
  2348. if (!pte_write(entry))
  2349. return do_wp_page(mm, vma, address,
  2350. pte, pmd, ptl, entry);
  2351. entry = pte_mkdirty(entry);
  2352. }
  2353. entry = pte_mkyoung(entry);
  2354. if (ptep_set_access_flags(vma, address, pte, entry, write_access)) {
  2355. update_mmu_cache(vma, address, entry);
  2356. lazy_mmu_prot_update(entry);
  2357. } else {
  2358. /*
  2359. * This is needed only for protection faults but the arch code
  2360. * is not yet telling us if this is a protection fault or not.
  2361. * This still avoids useless tlb flushes for .text page faults
  2362. * with threads.
  2363. */
  2364. if (write_access)
  2365. flush_tlb_page(vma, address);
  2366. }
  2367. unlock:
  2368. pte_unmap_unlock(pte, ptl);
  2369. return 0;
  2370. }
  2371. /*
  2372. * By the time we get here, we already hold the mm semaphore
  2373. */
  2374. int handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma,
  2375. unsigned long address, int write_access)
  2376. {
  2377. pgd_t *pgd;
  2378. pud_t *pud;
  2379. pmd_t *pmd;
  2380. pte_t *pte;
  2381. __set_current_state(TASK_RUNNING);
  2382. count_vm_event(PGFAULT);
  2383. if (unlikely(is_vm_hugetlb_page(vma)))
  2384. return hugetlb_fault(mm, vma, address, write_access);
  2385. pgd = pgd_offset(mm, address);
  2386. pud = pud_alloc(mm, pgd, address);
  2387. if (!pud)
  2388. return VM_FAULT_OOM;
  2389. pmd = pmd_alloc(mm, pud, address);
  2390. if (!pmd)
  2391. return VM_FAULT_OOM;
  2392. pte = pte_alloc_map(mm, pmd, address);
  2393. if (!pte)
  2394. return VM_FAULT_OOM;
  2395. return handle_pte_fault(mm, vma, address, pte, pmd, write_access);
  2396. }
  2397. #ifndef __PAGETABLE_PUD_FOLDED
  2398. /*
  2399. * Allocate page upper directory.
  2400. * We've already handled the fast-path in-line.
  2401. */
  2402. int __pud_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
  2403. {
  2404. pud_t *new = pud_alloc_one(mm, address);
  2405. if (!new)
  2406. return -ENOMEM;
  2407. spin_lock(&mm->page_table_lock);
  2408. if (pgd_present(*pgd)) /* Another has populated it */
  2409. pud_free(new);
  2410. else
  2411. pgd_populate(mm, pgd, new);
  2412. spin_unlock(&mm->page_table_lock);
  2413. return 0;
  2414. }
  2415. #endif /* __PAGETABLE_PUD_FOLDED */
  2416. #ifndef __PAGETABLE_PMD_FOLDED
  2417. /*
  2418. * Allocate page middle directory.
  2419. * We've already handled the fast-path in-line.
  2420. */
  2421. int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address)
  2422. {
  2423. pmd_t *new = pmd_alloc_one(mm, address);
  2424. if (!new)
  2425. return -ENOMEM;
  2426. spin_lock(&mm->page_table_lock);
  2427. #ifndef __ARCH_HAS_4LEVEL_HACK
  2428. if (pud_present(*pud)) /* Another has populated it */
  2429. pmd_free(new);
  2430. else
  2431. pud_populate(mm, pud, new);
  2432. #else
  2433. if (pgd_present(*pud)) /* Another has populated it */
  2434. pmd_free(new);
  2435. else
  2436. pgd_populate(mm, pud, new);
  2437. #endif /* __ARCH_HAS_4LEVEL_HACK */
  2438. spin_unlock(&mm->page_table_lock);
  2439. return 0;
  2440. }
  2441. #endif /* __PAGETABLE_PMD_FOLDED */
  2442. int make_pages_present(unsigned long addr, unsigned long end)
  2443. {
  2444. int ret, len, write;
  2445. struct vm_area_struct * vma;
  2446. vma = find_vma(current->mm, addr);
  2447. if (!vma)
  2448. return -1;
  2449. write = (vma->vm_flags & VM_WRITE) != 0;
  2450. BUG_ON(addr >= end);
  2451. BUG_ON(end > vma->vm_end);
  2452. len = DIV_ROUND_UP(end, PAGE_SIZE) - addr/PAGE_SIZE;
  2453. ret = get_user_pages(current, current->mm, addr,
  2454. len, write, 0, NULL, NULL);
  2455. if (ret < 0)
  2456. return ret;
  2457. return ret == len ? 0 : -1;
  2458. }
  2459. /*
  2460. * Map a vmalloc()-space virtual address to the physical page.
  2461. */
  2462. struct page * vmalloc_to_page(void * vmalloc_addr)
  2463. {
  2464. unsigned long addr = (unsigned long) vmalloc_addr;
  2465. struct page *page = NULL;
  2466. pgd_t *pgd = pgd_offset_k(addr);
  2467. pud_t *pud;
  2468. pmd_t *pmd;
  2469. pte_t *ptep, pte;
  2470. if (!pgd_none(*pgd)) {
  2471. pud = pud_offset(pgd, addr);
  2472. if (!pud_none(*pud)) {
  2473. pmd = pmd_offset(pud, addr);
  2474. if (!pmd_none(*pmd)) {
  2475. ptep = pte_offset_map(pmd, addr);
  2476. pte = *ptep;
  2477. if (pte_present(pte))
  2478. page = pte_page(pte);
  2479. pte_unmap(ptep);
  2480. }
  2481. }
  2482. }
  2483. return page;
  2484. }
  2485. EXPORT_SYMBOL(vmalloc_to_page);
  2486. /*
  2487. * Map a vmalloc()-space virtual address to the physical page frame number.
  2488. */
  2489. unsigned long vmalloc_to_pfn(void * vmalloc_addr)
  2490. {
  2491. return page_to_pfn(vmalloc_to_page(vmalloc_addr));
  2492. }
  2493. EXPORT_SYMBOL(vmalloc_to_pfn);
  2494. #if !defined(__HAVE_ARCH_GATE_AREA)
  2495. #if defined(AT_SYSINFO_EHDR)
  2496. static struct vm_area_struct gate_vma;
  2497. static int __init gate_vma_init(void)
  2498. {
  2499. gate_vma.vm_mm = NULL;
  2500. gate_vma.vm_start = FIXADDR_USER_START;
  2501. gate_vma.vm_end = FIXADDR_USER_END;
  2502. gate_vma.vm_flags = VM_READ | VM_MAYREAD | VM_EXEC | VM_MAYEXEC;
  2503. gate_vma.vm_page_prot = __P101;
  2504. /*
  2505. * Make sure the vDSO gets into every core dump.
  2506. * Dumping its contents makes post-mortem fully interpretable later
  2507. * without matching up the same kernel and hardware config to see
  2508. * what PC values meant.
  2509. */
  2510. gate_vma.vm_flags |= VM_ALWAYSDUMP;
  2511. return 0;
  2512. }
  2513. __initcall(gate_vma_init);
  2514. #endif
  2515. struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
  2516. {
  2517. #ifdef AT_SYSINFO_EHDR
  2518. return &gate_vma;
  2519. #else
  2520. return NULL;
  2521. #endif
  2522. }
  2523. int in_gate_area_no_task(unsigned long addr)
  2524. {
  2525. #ifdef AT_SYSINFO_EHDR
  2526. if ((addr >= FIXADDR_USER_START) && (addr < FIXADDR_USER_END))
  2527. return 1;
  2528. #endif
  2529. return 0;
  2530. }
  2531. #endif /* __HAVE_ARCH_GATE_AREA */
  2532. /*
  2533. * Access another process' address space.
  2534. * Source/target buffer must be kernel space,
  2535. * Do not walk the page table directly, use get_user_pages
  2536. */
  2537. int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
  2538. {
  2539. struct mm_struct *mm;
  2540. struct vm_area_struct *vma;
  2541. struct page *page;
  2542. void *old_buf = buf;
  2543. mm = get_task_mm(tsk);
  2544. if (!mm)
  2545. return 0;
  2546. down_read(&mm->mmap_sem);
  2547. /* ignore errors, just check how much was sucessfully transfered */
  2548. while (len) {
  2549. int bytes, ret, offset;
  2550. void *maddr;
  2551. ret = get_user_pages(tsk, mm, addr, 1,
  2552. write, 1, &page, &vma);
  2553. if (ret <= 0)
  2554. break;
  2555. bytes = len;
  2556. offset = addr & (PAGE_SIZE-1);
  2557. if (bytes > PAGE_SIZE-offset)
  2558. bytes = PAGE_SIZE-offset;
  2559. maddr = kmap(page);
  2560. if (write) {
  2561. copy_to_user_page(vma, page, addr,
  2562. maddr + offset, buf, bytes);
  2563. set_page_dirty_lock(page);
  2564. } else {
  2565. copy_from_user_page(vma, page, addr,
  2566. buf, maddr + offset, bytes);
  2567. }
  2568. kunmap(page);
  2569. page_cache_release(page);
  2570. len -= bytes;
  2571. buf += bytes;
  2572. addr += bytes;
  2573. }
  2574. up_read(&mm->mmap_sem);
  2575. mmput(mm);
  2576. return buf - old_buf;
  2577. }
  2578. EXPORT_SYMBOL_GPL(access_process_vm);