memory.c 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859
  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. /*
  957. * The VM_FAULT_WRITE bit tells us that do_wp_page has
  958. * broken COW when necessary, even if maybe_mkwrite
  959. * decided not to set pte_write. We can thus safely do
  960. * subsequent page lookups as if they were reads.
  961. */
  962. if (ret & VM_FAULT_WRITE)
  963. foll_flags &= ~FOLL_WRITE;
  964. switch (ret & ~VM_FAULT_WRITE) {
  965. case VM_FAULT_MINOR:
  966. tsk->min_flt++;
  967. break;
  968. case VM_FAULT_MAJOR:
  969. tsk->maj_flt++;
  970. break;
  971. case VM_FAULT_SIGBUS:
  972. return i ? i : -EFAULT;
  973. case VM_FAULT_OOM:
  974. return i ? i : -ENOMEM;
  975. default:
  976. BUG();
  977. }
  978. cond_resched();
  979. }
  980. if (pages) {
  981. pages[i] = page;
  982. flush_anon_page(vma, page, start);
  983. flush_dcache_page(page);
  984. }
  985. if (vmas)
  986. vmas[i] = vma;
  987. i++;
  988. start += PAGE_SIZE;
  989. len--;
  990. } while (len && start < vma->vm_end);
  991. } while (len);
  992. return i;
  993. }
  994. EXPORT_SYMBOL(get_user_pages);
  995. static int zeromap_pte_range(struct mm_struct *mm, pmd_t *pmd,
  996. unsigned long addr, unsigned long end, pgprot_t prot)
  997. {
  998. pte_t *pte;
  999. spinlock_t *ptl;
  1000. int err = 0;
  1001. pte = pte_alloc_map_lock(mm, pmd, addr, &ptl);
  1002. if (!pte)
  1003. return -EAGAIN;
  1004. arch_enter_lazy_mmu_mode();
  1005. do {
  1006. struct page *page = ZERO_PAGE(addr);
  1007. pte_t zero_pte = pte_wrprotect(mk_pte(page, prot));
  1008. if (unlikely(!pte_none(*pte))) {
  1009. err = -EEXIST;
  1010. pte++;
  1011. break;
  1012. }
  1013. page_cache_get(page);
  1014. page_add_file_rmap(page);
  1015. inc_mm_counter(mm, file_rss);
  1016. set_pte_at(mm, addr, pte, zero_pte);
  1017. } while (pte++, addr += PAGE_SIZE, addr != end);
  1018. arch_leave_lazy_mmu_mode();
  1019. pte_unmap_unlock(pte - 1, ptl);
  1020. return err;
  1021. }
  1022. static inline int zeromap_pmd_range(struct mm_struct *mm, pud_t *pud,
  1023. unsigned long addr, unsigned long end, pgprot_t prot)
  1024. {
  1025. pmd_t *pmd;
  1026. unsigned long next;
  1027. int err;
  1028. pmd = pmd_alloc(mm, pud, addr);
  1029. if (!pmd)
  1030. return -EAGAIN;
  1031. do {
  1032. next = pmd_addr_end(addr, end);
  1033. err = zeromap_pte_range(mm, pmd, addr, next, prot);
  1034. if (err)
  1035. break;
  1036. } while (pmd++, addr = next, addr != end);
  1037. return err;
  1038. }
  1039. static inline int zeromap_pud_range(struct mm_struct *mm, pgd_t *pgd,
  1040. unsigned long addr, unsigned long end, pgprot_t prot)
  1041. {
  1042. pud_t *pud;
  1043. unsigned long next;
  1044. int err;
  1045. pud = pud_alloc(mm, pgd, addr);
  1046. if (!pud)
  1047. return -EAGAIN;
  1048. do {
  1049. next = pud_addr_end(addr, end);
  1050. err = zeromap_pmd_range(mm, pud, addr, next, prot);
  1051. if (err)
  1052. break;
  1053. } while (pud++, addr = next, addr != end);
  1054. return err;
  1055. }
  1056. int zeromap_page_range(struct vm_area_struct *vma,
  1057. unsigned long addr, unsigned long size, pgprot_t prot)
  1058. {
  1059. pgd_t *pgd;
  1060. unsigned long next;
  1061. unsigned long end = addr + size;
  1062. struct mm_struct *mm = vma->vm_mm;
  1063. int err;
  1064. BUG_ON(addr >= end);
  1065. pgd = pgd_offset(mm, addr);
  1066. flush_cache_range(vma, addr, end);
  1067. do {
  1068. next = pgd_addr_end(addr, end);
  1069. err = zeromap_pud_range(mm, pgd, addr, next, prot);
  1070. if (err)
  1071. break;
  1072. } while (pgd++, addr = next, addr != end);
  1073. return err;
  1074. }
  1075. pte_t * fastcall get_locked_pte(struct mm_struct *mm, unsigned long addr, spinlock_t **ptl)
  1076. {
  1077. pgd_t * pgd = pgd_offset(mm, addr);
  1078. pud_t * pud = pud_alloc(mm, pgd, addr);
  1079. if (pud) {
  1080. pmd_t * pmd = pmd_alloc(mm, pud, addr);
  1081. if (pmd)
  1082. return pte_alloc_map_lock(mm, pmd, addr, ptl);
  1083. }
  1084. return NULL;
  1085. }
  1086. /*
  1087. * This is the old fallback for page remapping.
  1088. *
  1089. * For historical reasons, it only allows reserved pages. Only
  1090. * old drivers should use this, and they needed to mark their
  1091. * pages reserved for the old functions anyway.
  1092. */
  1093. static int insert_page(struct mm_struct *mm, unsigned long addr, struct page *page, pgprot_t prot)
  1094. {
  1095. int retval;
  1096. pte_t *pte;
  1097. spinlock_t *ptl;
  1098. retval = -EINVAL;
  1099. if (PageAnon(page))
  1100. goto out;
  1101. retval = -ENOMEM;
  1102. flush_dcache_page(page);
  1103. pte = get_locked_pte(mm, addr, &ptl);
  1104. if (!pte)
  1105. goto out;
  1106. retval = -EBUSY;
  1107. if (!pte_none(*pte))
  1108. goto out_unlock;
  1109. /* Ok, finally just insert the thing.. */
  1110. get_page(page);
  1111. inc_mm_counter(mm, file_rss);
  1112. page_add_file_rmap(page);
  1113. set_pte_at(mm, addr, pte, mk_pte(page, prot));
  1114. retval = 0;
  1115. out_unlock:
  1116. pte_unmap_unlock(pte, ptl);
  1117. out:
  1118. return retval;
  1119. }
  1120. /**
  1121. * vm_insert_page - insert single page into user vma
  1122. * @vma: user vma to map to
  1123. * @addr: target user address of this page
  1124. * @page: source kernel page
  1125. *
  1126. * This allows drivers to insert individual pages they've allocated
  1127. * into a user vma.
  1128. *
  1129. * The page has to be a nice clean _individual_ kernel allocation.
  1130. * If you allocate a compound page, you need to have marked it as
  1131. * such (__GFP_COMP), or manually just split the page up yourself
  1132. * (see split_page()).
  1133. *
  1134. * NOTE! Traditionally this was done with "remap_pfn_range()" which
  1135. * took an arbitrary page protection parameter. This doesn't allow
  1136. * that. Your vma protection will have to be set up correctly, which
  1137. * means that if you want a shared writable mapping, you'd better
  1138. * ask for a shared writable mapping!
  1139. *
  1140. * The page does not need to be reserved.
  1141. */
  1142. int vm_insert_page(struct vm_area_struct *vma, unsigned long addr, struct page *page)
  1143. {
  1144. if (addr < vma->vm_start || addr >= vma->vm_end)
  1145. return -EFAULT;
  1146. if (!page_count(page))
  1147. return -EINVAL;
  1148. vma->vm_flags |= VM_INSERTPAGE;
  1149. return insert_page(vma->vm_mm, addr, page, vma->vm_page_prot);
  1150. }
  1151. EXPORT_SYMBOL(vm_insert_page);
  1152. /**
  1153. * vm_insert_pfn - insert single pfn into user vma
  1154. * @vma: user vma to map to
  1155. * @addr: target user address of this page
  1156. * @pfn: source kernel pfn
  1157. *
  1158. * Similar to vm_inert_page, this allows drivers to insert individual pages
  1159. * they've allocated into a user vma. Same comments apply.
  1160. *
  1161. * This function should only be called from a vm_ops->fault handler, and
  1162. * in that case the handler should return NULL.
  1163. */
  1164. int vm_insert_pfn(struct vm_area_struct *vma, unsigned long addr,
  1165. unsigned long pfn)
  1166. {
  1167. struct mm_struct *mm = vma->vm_mm;
  1168. int retval;
  1169. pte_t *pte, entry;
  1170. spinlock_t *ptl;
  1171. BUG_ON(!(vma->vm_flags & VM_PFNMAP));
  1172. BUG_ON(is_cow_mapping(vma->vm_flags));
  1173. retval = -ENOMEM;
  1174. pte = get_locked_pte(mm, addr, &ptl);
  1175. if (!pte)
  1176. goto out;
  1177. retval = -EBUSY;
  1178. if (!pte_none(*pte))
  1179. goto out_unlock;
  1180. /* Ok, finally just insert the thing.. */
  1181. entry = pfn_pte(pfn, vma->vm_page_prot);
  1182. set_pte_at(mm, addr, pte, entry);
  1183. update_mmu_cache(vma, addr, entry);
  1184. retval = 0;
  1185. out_unlock:
  1186. pte_unmap_unlock(pte, ptl);
  1187. out:
  1188. return retval;
  1189. }
  1190. EXPORT_SYMBOL(vm_insert_pfn);
  1191. /*
  1192. * maps a range of physical memory into the requested pages. the old
  1193. * mappings are removed. any references to nonexistent pages results
  1194. * in null mappings (currently treated as "copy-on-access")
  1195. */
  1196. static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd,
  1197. unsigned long addr, unsigned long end,
  1198. unsigned long pfn, pgprot_t prot)
  1199. {
  1200. pte_t *pte;
  1201. spinlock_t *ptl;
  1202. pte = pte_alloc_map_lock(mm, pmd, addr, &ptl);
  1203. if (!pte)
  1204. return -ENOMEM;
  1205. arch_enter_lazy_mmu_mode();
  1206. do {
  1207. BUG_ON(!pte_none(*pte));
  1208. set_pte_at(mm, addr, pte, pfn_pte(pfn, prot));
  1209. pfn++;
  1210. } while (pte++, addr += PAGE_SIZE, addr != end);
  1211. arch_leave_lazy_mmu_mode();
  1212. pte_unmap_unlock(pte - 1, ptl);
  1213. return 0;
  1214. }
  1215. static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud,
  1216. unsigned long addr, unsigned long end,
  1217. unsigned long pfn, pgprot_t prot)
  1218. {
  1219. pmd_t *pmd;
  1220. unsigned long next;
  1221. pfn -= addr >> PAGE_SHIFT;
  1222. pmd = pmd_alloc(mm, pud, addr);
  1223. if (!pmd)
  1224. return -ENOMEM;
  1225. do {
  1226. next = pmd_addr_end(addr, end);
  1227. if (remap_pte_range(mm, pmd, addr, next,
  1228. pfn + (addr >> PAGE_SHIFT), prot))
  1229. return -ENOMEM;
  1230. } while (pmd++, addr = next, addr != end);
  1231. return 0;
  1232. }
  1233. static inline int remap_pud_range(struct mm_struct *mm, pgd_t *pgd,
  1234. unsigned long addr, unsigned long end,
  1235. unsigned long pfn, pgprot_t prot)
  1236. {
  1237. pud_t *pud;
  1238. unsigned long next;
  1239. pfn -= addr >> PAGE_SHIFT;
  1240. pud = pud_alloc(mm, pgd, addr);
  1241. if (!pud)
  1242. return -ENOMEM;
  1243. do {
  1244. next = pud_addr_end(addr, end);
  1245. if (remap_pmd_range(mm, pud, addr, next,
  1246. pfn + (addr >> PAGE_SHIFT), prot))
  1247. return -ENOMEM;
  1248. } while (pud++, addr = next, addr != end);
  1249. return 0;
  1250. }
  1251. /**
  1252. * remap_pfn_range - remap kernel memory to userspace
  1253. * @vma: user vma to map to
  1254. * @addr: target user address to start at
  1255. * @pfn: physical address of kernel memory
  1256. * @size: size of map area
  1257. * @prot: page protection flags for this mapping
  1258. *
  1259. * Note: this is only safe if the mm semaphore is held when called.
  1260. */
  1261. int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
  1262. unsigned long pfn, unsigned long size, pgprot_t prot)
  1263. {
  1264. pgd_t *pgd;
  1265. unsigned long next;
  1266. unsigned long end = addr + PAGE_ALIGN(size);
  1267. struct mm_struct *mm = vma->vm_mm;
  1268. int err;
  1269. /*
  1270. * Physically remapped pages are special. Tell the
  1271. * rest of the world about it:
  1272. * VM_IO tells people not to look at these pages
  1273. * (accesses can have side effects).
  1274. * VM_RESERVED is specified all over the place, because
  1275. * in 2.4 it kept swapout's vma scan off this vma; but
  1276. * in 2.6 the LRU scan won't even find its pages, so this
  1277. * flag means no more than count its pages in reserved_vm,
  1278. * and omit it from core dump, even when VM_IO turned off.
  1279. * VM_PFNMAP tells the core MM that the base pages are just
  1280. * raw PFN mappings, and do not have a "struct page" associated
  1281. * with them.
  1282. *
  1283. * There's a horrible special case to handle copy-on-write
  1284. * behaviour that some programs depend on. We mark the "original"
  1285. * un-COW'ed pages by matching them up with "vma->vm_pgoff".
  1286. */
  1287. if (is_cow_mapping(vma->vm_flags)) {
  1288. if (addr != vma->vm_start || end != vma->vm_end)
  1289. return -EINVAL;
  1290. vma->vm_pgoff = pfn;
  1291. }
  1292. vma->vm_flags |= VM_IO | VM_RESERVED | VM_PFNMAP;
  1293. BUG_ON(addr >= end);
  1294. pfn -= addr >> PAGE_SHIFT;
  1295. pgd = pgd_offset(mm, addr);
  1296. flush_cache_range(vma, addr, end);
  1297. do {
  1298. next = pgd_addr_end(addr, end);
  1299. err = remap_pud_range(mm, pgd, addr, next,
  1300. pfn + (addr >> PAGE_SHIFT), prot);
  1301. if (err)
  1302. break;
  1303. } while (pgd++, addr = next, addr != end);
  1304. return err;
  1305. }
  1306. EXPORT_SYMBOL(remap_pfn_range);
  1307. static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd,
  1308. unsigned long addr, unsigned long end,
  1309. pte_fn_t fn, void *data)
  1310. {
  1311. pte_t *pte;
  1312. int err;
  1313. struct page *pmd_page;
  1314. spinlock_t *uninitialized_var(ptl);
  1315. pte = (mm == &init_mm) ?
  1316. pte_alloc_kernel(pmd, addr) :
  1317. pte_alloc_map_lock(mm, pmd, addr, &ptl);
  1318. if (!pte)
  1319. return -ENOMEM;
  1320. BUG_ON(pmd_huge(*pmd));
  1321. pmd_page = pmd_page(*pmd);
  1322. do {
  1323. err = fn(pte, pmd_page, addr, data);
  1324. if (err)
  1325. break;
  1326. } while (pte++, addr += PAGE_SIZE, addr != end);
  1327. if (mm != &init_mm)
  1328. pte_unmap_unlock(pte-1, ptl);
  1329. return err;
  1330. }
  1331. static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud,
  1332. unsigned long addr, unsigned long end,
  1333. pte_fn_t fn, void *data)
  1334. {
  1335. pmd_t *pmd;
  1336. unsigned long next;
  1337. int err;
  1338. pmd = pmd_alloc(mm, pud, addr);
  1339. if (!pmd)
  1340. return -ENOMEM;
  1341. do {
  1342. next = pmd_addr_end(addr, end);
  1343. err = apply_to_pte_range(mm, pmd, addr, next, fn, data);
  1344. if (err)
  1345. break;
  1346. } while (pmd++, addr = next, addr != end);
  1347. return err;
  1348. }
  1349. static int apply_to_pud_range(struct mm_struct *mm, pgd_t *pgd,
  1350. unsigned long addr, unsigned long end,
  1351. pte_fn_t fn, void *data)
  1352. {
  1353. pud_t *pud;
  1354. unsigned long next;
  1355. int err;
  1356. pud = pud_alloc(mm, pgd, addr);
  1357. if (!pud)
  1358. return -ENOMEM;
  1359. do {
  1360. next = pud_addr_end(addr, end);
  1361. err = apply_to_pmd_range(mm, pud, addr, next, fn, data);
  1362. if (err)
  1363. break;
  1364. } while (pud++, addr = next, addr != end);
  1365. return err;
  1366. }
  1367. /*
  1368. * Scan a region of virtual memory, filling in page tables as necessary
  1369. * and calling a provided function on each leaf page table.
  1370. */
  1371. int apply_to_page_range(struct mm_struct *mm, unsigned long addr,
  1372. unsigned long size, pte_fn_t fn, void *data)
  1373. {
  1374. pgd_t *pgd;
  1375. unsigned long next;
  1376. unsigned long end = addr + size;
  1377. int err;
  1378. BUG_ON(addr >= end);
  1379. pgd = pgd_offset(mm, addr);
  1380. do {
  1381. next = pgd_addr_end(addr, end);
  1382. err = apply_to_pud_range(mm, pgd, addr, next, fn, data);
  1383. if (err)
  1384. break;
  1385. } while (pgd++, addr = next, addr != end);
  1386. return err;
  1387. }
  1388. EXPORT_SYMBOL_GPL(apply_to_page_range);
  1389. /*
  1390. * handle_pte_fault chooses page fault handler according to an entry
  1391. * which was read non-atomically. Before making any commitment, on
  1392. * those architectures or configurations (e.g. i386 with PAE) which
  1393. * might give a mix of unmatched parts, do_swap_page and do_file_page
  1394. * must check under lock before unmapping the pte and proceeding
  1395. * (but do_wp_page is only called after already making such a check;
  1396. * and do_anonymous_page and do_no_page can safely check later on).
  1397. */
  1398. static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd,
  1399. pte_t *page_table, pte_t orig_pte)
  1400. {
  1401. int same = 1;
  1402. #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
  1403. if (sizeof(pte_t) > sizeof(unsigned long)) {
  1404. spinlock_t *ptl = pte_lockptr(mm, pmd);
  1405. spin_lock(ptl);
  1406. same = pte_same(*page_table, orig_pte);
  1407. spin_unlock(ptl);
  1408. }
  1409. #endif
  1410. pte_unmap(page_table);
  1411. return same;
  1412. }
  1413. /*
  1414. * Do pte_mkwrite, but only if the vma says VM_WRITE. We do this when
  1415. * servicing faults for write access. In the normal case, do always want
  1416. * pte_mkwrite. But get_user_pages can cause write faults for mappings
  1417. * that do not have writing enabled, when used by access_process_vm.
  1418. */
  1419. static inline pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma)
  1420. {
  1421. if (likely(vma->vm_flags & VM_WRITE))
  1422. pte = pte_mkwrite(pte);
  1423. return pte;
  1424. }
  1425. static inline void cow_user_page(struct page *dst, struct page *src, unsigned long va, struct vm_area_struct *vma)
  1426. {
  1427. /*
  1428. * If the source page was a PFN mapping, we don't have
  1429. * a "struct page" for it. We do a best-effort copy by
  1430. * just copying from the original user address. If that
  1431. * fails, we just zero-fill it. Live with it.
  1432. */
  1433. if (unlikely(!src)) {
  1434. void *kaddr = kmap_atomic(dst, KM_USER0);
  1435. void __user *uaddr = (void __user *)(va & PAGE_MASK);
  1436. /*
  1437. * This really shouldn't fail, because the page is there
  1438. * in the page tables. But it might just be unreadable,
  1439. * in which case we just give up and fill the result with
  1440. * zeroes.
  1441. */
  1442. if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE))
  1443. memset(kaddr, 0, PAGE_SIZE);
  1444. kunmap_atomic(kaddr, KM_USER0);
  1445. flush_dcache_page(dst);
  1446. return;
  1447. }
  1448. copy_user_highpage(dst, src, va, vma);
  1449. }
  1450. /*
  1451. * This routine handles present pages, when users try to write
  1452. * to a shared page. It is done by copying the page to a new address
  1453. * and decrementing the shared-page counter for the old page.
  1454. *
  1455. * Note that this routine assumes that the protection checks have been
  1456. * done by the caller (the low-level page fault routine in most cases).
  1457. * Thus we can safely just mark it writable once we've done any necessary
  1458. * COW.
  1459. *
  1460. * We also mark the page dirty at this point even though the page will
  1461. * change only once the write actually happens. This avoids a few races,
  1462. * and potentially makes it more efficient.
  1463. *
  1464. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  1465. * but allow concurrent faults), with pte both mapped and locked.
  1466. * We return with mmap_sem still held, but pte unmapped and unlocked.
  1467. */
  1468. static int do_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
  1469. unsigned long address, pte_t *page_table, pmd_t *pmd,
  1470. spinlock_t *ptl, pte_t orig_pte)
  1471. {
  1472. struct page *old_page, *new_page;
  1473. pte_t entry;
  1474. int reuse = 0, ret = VM_FAULT_MINOR;
  1475. struct page *dirty_page = NULL;
  1476. old_page = vm_normal_page(vma, address, orig_pte);
  1477. if (!old_page)
  1478. goto gotten;
  1479. /*
  1480. * Take out anonymous pages first, anonymous shared vmas are
  1481. * not dirty accountable.
  1482. */
  1483. if (PageAnon(old_page)) {
  1484. if (!TestSetPageLocked(old_page)) {
  1485. reuse = can_share_swap_page(old_page);
  1486. unlock_page(old_page);
  1487. }
  1488. } else if (unlikely((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
  1489. (VM_WRITE|VM_SHARED))) {
  1490. /*
  1491. * Only catch write-faults on shared writable pages,
  1492. * read-only shared pages can get COWed by
  1493. * get_user_pages(.write=1, .force=1).
  1494. */
  1495. if (vma->vm_ops && vma->vm_ops->page_mkwrite) {
  1496. /*
  1497. * Notify the address space that the page is about to
  1498. * become writable so that it can prohibit this or wait
  1499. * for the page to get into an appropriate state.
  1500. *
  1501. * We do this without the lock held, so that it can
  1502. * sleep if it needs to.
  1503. */
  1504. page_cache_get(old_page);
  1505. pte_unmap_unlock(page_table, ptl);
  1506. if (vma->vm_ops->page_mkwrite(vma, old_page) < 0)
  1507. goto unwritable_page;
  1508. /*
  1509. * Since we dropped the lock we need to revalidate
  1510. * the PTE as someone else may have changed it. If
  1511. * they did, we just return, as we can count on the
  1512. * MMU to tell us if they didn't also make it writable.
  1513. */
  1514. page_table = pte_offset_map_lock(mm, pmd, address,
  1515. &ptl);
  1516. page_cache_release(old_page);
  1517. if (!pte_same(*page_table, orig_pte))
  1518. goto unlock;
  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. set_page_dirty_balance(dirty_page);
  1593. put_page(dirty_page);
  1594. }
  1595. return ret;
  1596. oom:
  1597. if (old_page)
  1598. page_cache_release(old_page);
  1599. return VM_FAULT_OOM;
  1600. unwritable_page:
  1601. page_cache_release(old_page);
  1602. return VM_FAULT_SIGBUS;
  1603. }
  1604. /*
  1605. * Helper functions for unmap_mapping_range().
  1606. *
  1607. * __ Notes on dropping i_mmap_lock to reduce latency while unmapping __
  1608. *
  1609. * We have to restart searching the prio_tree whenever we drop the lock,
  1610. * since the iterator is only valid while the lock is held, and anyway
  1611. * a later vma might be split and reinserted earlier while lock dropped.
  1612. *
  1613. * The list of nonlinear vmas could be handled more efficiently, using
  1614. * a placeholder, but handle it in the same way until a need is shown.
  1615. * It is important to search the prio_tree before nonlinear list: a vma
  1616. * may become nonlinear and be shifted from prio_tree to nonlinear list
  1617. * while the lock is dropped; but never shifted from list to prio_tree.
  1618. *
  1619. * In order to make forward progress despite restarting the search,
  1620. * vm_truncate_count is used to mark a vma as now dealt with, so we can
  1621. * quickly skip it next time around. Since the prio_tree search only
  1622. * shows us those vmas affected by unmapping the range in question, we
  1623. * can't efficiently keep all vmas in step with mapping->truncate_count:
  1624. * so instead reset them all whenever it wraps back to 0 (then go to 1).
  1625. * mapping->truncate_count and vma->vm_truncate_count are protected by
  1626. * i_mmap_lock.
  1627. *
  1628. * In order to make forward progress despite repeatedly restarting some
  1629. * large vma, note the restart_addr from unmap_vmas when it breaks out:
  1630. * and restart from that address when we reach that vma again. It might
  1631. * have been split or merged, shrunk or extended, but never shifted: so
  1632. * restart_addr remains valid so long as it remains in the vma's range.
  1633. * unmap_mapping_range forces truncate_count to leap over page-aligned
  1634. * values so we can save vma's restart_addr in its truncate_count field.
  1635. */
  1636. #define is_restart_addr(truncate_count) (!((truncate_count) & ~PAGE_MASK))
  1637. static void reset_vma_truncate_counts(struct address_space *mapping)
  1638. {
  1639. struct vm_area_struct *vma;
  1640. struct prio_tree_iter iter;
  1641. vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, 0, ULONG_MAX)
  1642. vma->vm_truncate_count = 0;
  1643. list_for_each_entry(vma, &mapping->i_mmap_nonlinear, shared.vm_set.list)
  1644. vma->vm_truncate_count = 0;
  1645. }
  1646. static int unmap_mapping_range_vma(struct vm_area_struct *vma,
  1647. unsigned long start_addr, unsigned long end_addr,
  1648. struct zap_details *details)
  1649. {
  1650. unsigned long restart_addr;
  1651. int need_break;
  1652. /*
  1653. * files that support invalidating or truncating portions of the
  1654. * file from under mmaped areas must set the VM_CAN_INVALIDATE flag, and
  1655. * have their .nopage function return the page locked.
  1656. */
  1657. BUG_ON(!(vma->vm_flags & VM_CAN_INVALIDATE));
  1658. again:
  1659. restart_addr = vma->vm_truncate_count;
  1660. if (is_restart_addr(restart_addr) && start_addr < restart_addr) {
  1661. start_addr = restart_addr;
  1662. if (start_addr >= end_addr) {
  1663. /* Top of vma has been split off since last time */
  1664. vma->vm_truncate_count = details->truncate_count;
  1665. return 0;
  1666. }
  1667. }
  1668. restart_addr = zap_page_range(vma, start_addr,
  1669. end_addr - start_addr, details);
  1670. need_break = need_resched() ||
  1671. need_lockbreak(details->i_mmap_lock);
  1672. if (restart_addr >= end_addr) {
  1673. /* We have now completed this vma: mark it so */
  1674. vma->vm_truncate_count = details->truncate_count;
  1675. if (!need_break)
  1676. return 0;
  1677. } else {
  1678. /* Note restart_addr in vma's truncate_count field */
  1679. vma->vm_truncate_count = restart_addr;
  1680. if (!need_break)
  1681. goto again;
  1682. }
  1683. spin_unlock(details->i_mmap_lock);
  1684. cond_resched();
  1685. spin_lock(details->i_mmap_lock);
  1686. return -EINTR;
  1687. }
  1688. static inline void unmap_mapping_range_tree(struct prio_tree_root *root,
  1689. struct zap_details *details)
  1690. {
  1691. struct vm_area_struct *vma;
  1692. struct prio_tree_iter iter;
  1693. pgoff_t vba, vea, zba, zea;
  1694. restart:
  1695. vma_prio_tree_foreach(vma, &iter, root,
  1696. details->first_index, details->last_index) {
  1697. /* Skip quickly over those we have already dealt with */
  1698. if (vma->vm_truncate_count == details->truncate_count)
  1699. continue;
  1700. vba = vma->vm_pgoff;
  1701. vea = vba + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) - 1;
  1702. /* Assume for now that PAGE_CACHE_SHIFT == PAGE_SHIFT */
  1703. zba = details->first_index;
  1704. if (zba < vba)
  1705. zba = vba;
  1706. zea = details->last_index;
  1707. if (zea > vea)
  1708. zea = vea;
  1709. if (unmap_mapping_range_vma(vma,
  1710. ((zba - vba) << PAGE_SHIFT) + vma->vm_start,
  1711. ((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start,
  1712. details) < 0)
  1713. goto restart;
  1714. }
  1715. }
  1716. static inline void unmap_mapping_range_list(struct list_head *head,
  1717. struct zap_details *details)
  1718. {
  1719. struct vm_area_struct *vma;
  1720. /*
  1721. * In nonlinear VMAs there is no correspondence between virtual address
  1722. * offset and file offset. So we must perform an exhaustive search
  1723. * across *all* the pages in each nonlinear VMA, not just the pages
  1724. * whose virtual address lies outside the file truncation point.
  1725. */
  1726. restart:
  1727. list_for_each_entry(vma, head, shared.vm_set.list) {
  1728. /* Skip quickly over those we have already dealt with */
  1729. if (vma->vm_truncate_count == details->truncate_count)
  1730. continue;
  1731. details->nonlinear_vma = vma;
  1732. if (unmap_mapping_range_vma(vma, vma->vm_start,
  1733. vma->vm_end, details) < 0)
  1734. goto restart;
  1735. }
  1736. }
  1737. /**
  1738. * unmap_mapping_range - unmap the portion of all mmaps in the specified address_space corresponding to the specified page range in the underlying file.
  1739. * @mapping: the address space containing mmaps to be unmapped.
  1740. * @holebegin: byte in first page to unmap, relative to the start of
  1741. * the underlying file. This will be rounded down to a PAGE_SIZE
  1742. * boundary. Note that this is different from vmtruncate(), which
  1743. * must keep the partial page. In contrast, we must get rid of
  1744. * partial pages.
  1745. * @holelen: size of prospective hole in bytes. This will be rounded
  1746. * up to a PAGE_SIZE boundary. A holelen of zero truncates to the
  1747. * end of the file.
  1748. * @even_cows: 1 when truncating a file, unmap even private COWed pages;
  1749. * but 0 when invalidating pagecache, don't throw away private data.
  1750. */
  1751. void unmap_mapping_range(struct address_space *mapping,
  1752. loff_t const holebegin, loff_t const holelen, int even_cows)
  1753. {
  1754. struct zap_details details;
  1755. pgoff_t hba = holebegin >> PAGE_SHIFT;
  1756. pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1757. /* Check for overflow. */
  1758. if (sizeof(holelen) > sizeof(hlen)) {
  1759. long long holeend =
  1760. (holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1761. if (holeend & ~(long long)ULONG_MAX)
  1762. hlen = ULONG_MAX - hba + 1;
  1763. }
  1764. details.check_mapping = even_cows? NULL: mapping;
  1765. details.nonlinear_vma = NULL;
  1766. details.first_index = hba;
  1767. details.last_index = hba + hlen - 1;
  1768. if (details.last_index < details.first_index)
  1769. details.last_index = ULONG_MAX;
  1770. details.i_mmap_lock = &mapping->i_mmap_lock;
  1771. spin_lock(&mapping->i_mmap_lock);
  1772. /* Protect against endless unmapping loops */
  1773. mapping->truncate_count++;
  1774. if (unlikely(is_restart_addr(mapping->truncate_count))) {
  1775. if (mapping->truncate_count == 0)
  1776. reset_vma_truncate_counts(mapping);
  1777. mapping->truncate_count++;
  1778. }
  1779. details.truncate_count = mapping->truncate_count;
  1780. if (unlikely(!prio_tree_empty(&mapping->i_mmap)))
  1781. unmap_mapping_range_tree(&mapping->i_mmap, &details);
  1782. if (unlikely(!list_empty(&mapping->i_mmap_nonlinear)))
  1783. unmap_mapping_range_list(&mapping->i_mmap_nonlinear, &details);
  1784. spin_unlock(&mapping->i_mmap_lock);
  1785. }
  1786. EXPORT_SYMBOL(unmap_mapping_range);
  1787. /**
  1788. * vmtruncate - unmap mappings "freed" by truncate() syscall
  1789. * @inode: inode of the file used
  1790. * @offset: file offset to start truncating
  1791. *
  1792. * NOTE! We have to be ready to update the memory sharing
  1793. * between the file and the memory map for a potential last
  1794. * incomplete page. Ugly, but necessary.
  1795. */
  1796. int vmtruncate(struct inode * inode, loff_t offset)
  1797. {
  1798. struct address_space *mapping = inode->i_mapping;
  1799. unsigned long limit;
  1800. if (inode->i_size < offset)
  1801. goto do_expand;
  1802. /*
  1803. * truncation of in-use swapfiles is disallowed - it would cause
  1804. * subsequent swapout to scribble on the now-freed blocks.
  1805. */
  1806. if (IS_SWAPFILE(inode))
  1807. goto out_busy;
  1808. i_size_write(inode, offset);
  1809. /*
  1810. * unmap_mapping_range is called twice, first simply for efficiency
  1811. * so that truncate_inode_pages does fewer single-page unmaps. However
  1812. * after this first call, and before truncate_inode_pages finishes,
  1813. * it is possible for private pages to be COWed, which remain after
  1814. * truncate_inode_pages finishes, hence the second unmap_mapping_range
  1815. * call must be made for correctness.
  1816. */
  1817. unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
  1818. truncate_inode_pages(mapping, offset);
  1819. unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
  1820. goto out_truncate;
  1821. do_expand:
  1822. limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
  1823. if (limit != RLIM_INFINITY && offset > limit)
  1824. goto out_sig;
  1825. if (offset > inode->i_sb->s_maxbytes)
  1826. goto out_big;
  1827. i_size_write(inode, offset);
  1828. out_truncate:
  1829. if (inode->i_op && inode->i_op->truncate)
  1830. inode->i_op->truncate(inode);
  1831. return 0;
  1832. out_sig:
  1833. send_sig(SIGXFSZ, current, 0);
  1834. out_big:
  1835. return -EFBIG;
  1836. out_busy:
  1837. return -ETXTBSY;
  1838. }
  1839. EXPORT_SYMBOL(vmtruncate);
  1840. int vmtruncate_range(struct inode *inode, loff_t offset, loff_t end)
  1841. {
  1842. struct address_space *mapping = inode->i_mapping;
  1843. /*
  1844. * If the underlying filesystem is not going to provide
  1845. * a way to truncate a range of blocks (punch a hole) -
  1846. * we should return failure right now.
  1847. */
  1848. if (!inode->i_op || !inode->i_op->truncate_range)
  1849. return -ENOSYS;
  1850. mutex_lock(&inode->i_mutex);
  1851. down_write(&inode->i_alloc_sem);
  1852. unmap_mapping_range(mapping, offset, (end - offset), 1);
  1853. truncate_inode_pages_range(mapping, offset, end);
  1854. unmap_mapping_range(mapping, offset, (end - offset), 1);
  1855. inode->i_op->truncate_range(inode, offset, end);
  1856. up_write(&inode->i_alloc_sem);
  1857. mutex_unlock(&inode->i_mutex);
  1858. return 0;
  1859. }
  1860. /**
  1861. * swapin_readahead - swap in pages in hope we need them soon
  1862. * @entry: swap entry of this memory
  1863. * @addr: address to start
  1864. * @vma: user vma this addresses belong to
  1865. *
  1866. * Primitive swap readahead code. We simply read an aligned block of
  1867. * (1 << page_cluster) entries in the swap area. This method is chosen
  1868. * because it doesn't cost us any seek time. We also make sure to queue
  1869. * the 'original' request together with the readahead ones...
  1870. *
  1871. * This has been extended to use the NUMA policies from the mm triggering
  1872. * the readahead.
  1873. *
  1874. * Caller must hold down_read on the vma->vm_mm if vma is not NULL.
  1875. */
  1876. void swapin_readahead(swp_entry_t entry, unsigned long addr,struct vm_area_struct *vma)
  1877. {
  1878. #ifdef CONFIG_NUMA
  1879. struct vm_area_struct *next_vma = vma ? vma->vm_next : NULL;
  1880. #endif
  1881. int i, num;
  1882. struct page *new_page;
  1883. unsigned long offset;
  1884. /*
  1885. * Get the number of handles we should do readahead io to.
  1886. */
  1887. num = valid_swaphandles(entry, &offset);
  1888. for (i = 0; i < num; offset++, i++) {
  1889. /* Ok, do the async read-ahead now */
  1890. new_page = read_swap_cache_async(swp_entry(swp_type(entry),
  1891. offset), vma, addr);
  1892. if (!new_page)
  1893. break;
  1894. page_cache_release(new_page);
  1895. #ifdef CONFIG_NUMA
  1896. /*
  1897. * Find the next applicable VMA for the NUMA policy.
  1898. */
  1899. addr += PAGE_SIZE;
  1900. if (addr == 0)
  1901. vma = NULL;
  1902. if (vma) {
  1903. if (addr >= vma->vm_end) {
  1904. vma = next_vma;
  1905. next_vma = vma ? vma->vm_next : NULL;
  1906. }
  1907. if (vma && addr < vma->vm_start)
  1908. vma = NULL;
  1909. } else {
  1910. if (next_vma && addr >= next_vma->vm_start) {
  1911. vma = next_vma;
  1912. next_vma = vma->vm_next;
  1913. }
  1914. }
  1915. #endif
  1916. }
  1917. lru_add_drain(); /* Push any new pages onto the LRU now */
  1918. }
  1919. /*
  1920. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  1921. * but allow concurrent faults), and pte mapped but not yet locked.
  1922. * We return with mmap_sem still held, but pte unmapped and unlocked.
  1923. */
  1924. static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma,
  1925. unsigned long address, pte_t *page_table, pmd_t *pmd,
  1926. int write_access, pte_t orig_pte)
  1927. {
  1928. spinlock_t *ptl;
  1929. struct page *page;
  1930. swp_entry_t entry;
  1931. pte_t pte;
  1932. int ret = VM_FAULT_MINOR;
  1933. if (!pte_unmap_same(mm, pmd, page_table, orig_pte))
  1934. goto out;
  1935. entry = pte_to_swp_entry(orig_pte);
  1936. if (is_migration_entry(entry)) {
  1937. migration_entry_wait(mm, pmd, address);
  1938. goto out;
  1939. }
  1940. delayacct_set_flag(DELAYACCT_PF_SWAPIN);
  1941. page = lookup_swap_cache(entry);
  1942. if (!page) {
  1943. grab_swap_token(); /* Contend for token _before_ read-in */
  1944. swapin_readahead(entry, address, vma);
  1945. page = read_swap_cache_async(entry, vma, address);
  1946. if (!page) {
  1947. /*
  1948. * Back out if somebody else faulted in this pte
  1949. * while we released the pte lock.
  1950. */
  1951. page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
  1952. if (likely(pte_same(*page_table, orig_pte)))
  1953. ret = VM_FAULT_OOM;
  1954. delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
  1955. goto unlock;
  1956. }
  1957. /* Had to read the page from swap area: Major fault */
  1958. ret = VM_FAULT_MAJOR;
  1959. count_vm_event(PGMAJFAULT);
  1960. }
  1961. delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
  1962. mark_page_accessed(page);
  1963. lock_page(page);
  1964. /*
  1965. * Back out if somebody else already faulted in this pte.
  1966. */
  1967. page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
  1968. if (unlikely(!pte_same(*page_table, orig_pte)))
  1969. goto out_nomap;
  1970. if (unlikely(!PageUptodate(page))) {
  1971. ret = VM_FAULT_SIGBUS;
  1972. goto out_nomap;
  1973. }
  1974. /* The page isn't present yet, go ahead with the fault. */
  1975. inc_mm_counter(mm, anon_rss);
  1976. pte = mk_pte(page, vma->vm_page_prot);
  1977. if (write_access && can_share_swap_page(page)) {
  1978. pte = maybe_mkwrite(pte_mkdirty(pte), vma);
  1979. write_access = 0;
  1980. }
  1981. flush_icache_page(vma, page);
  1982. set_pte_at(mm, address, page_table, pte);
  1983. page_add_anon_rmap(page, vma, address);
  1984. swap_free(entry);
  1985. if (vm_swap_full())
  1986. remove_exclusive_swap_page(page);
  1987. unlock_page(page);
  1988. if (write_access) {
  1989. if (do_wp_page(mm, vma, address,
  1990. page_table, pmd, ptl, pte) == VM_FAULT_OOM)
  1991. ret = VM_FAULT_OOM;
  1992. goto out;
  1993. }
  1994. /* No need to invalidate - it was non-present before */
  1995. update_mmu_cache(vma, address, pte);
  1996. unlock:
  1997. pte_unmap_unlock(page_table, ptl);
  1998. out:
  1999. return ret;
  2000. out_nomap:
  2001. pte_unmap_unlock(page_table, ptl);
  2002. unlock_page(page);
  2003. page_cache_release(page);
  2004. return ret;
  2005. }
  2006. /*
  2007. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  2008. * but allow concurrent faults), and pte mapped but not yet locked.
  2009. * We return with mmap_sem still held, but pte unmapped and unlocked.
  2010. */
  2011. static int do_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
  2012. unsigned long address, pte_t *page_table, pmd_t *pmd,
  2013. int write_access)
  2014. {
  2015. struct page *page;
  2016. spinlock_t *ptl;
  2017. pte_t entry;
  2018. if (write_access) {
  2019. /* Allocate our own private page. */
  2020. pte_unmap(page_table);
  2021. if (unlikely(anon_vma_prepare(vma)))
  2022. goto oom;
  2023. page = alloc_zeroed_user_highpage_movable(vma, address);
  2024. if (!page)
  2025. goto oom;
  2026. entry = mk_pte(page, vma->vm_page_prot);
  2027. entry = maybe_mkwrite(pte_mkdirty(entry), vma);
  2028. page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
  2029. if (!pte_none(*page_table))
  2030. goto release;
  2031. inc_mm_counter(mm, anon_rss);
  2032. lru_cache_add_active(page);
  2033. page_add_new_anon_rmap(page, vma, address);
  2034. } else {
  2035. /* Map the ZERO_PAGE - vm_page_prot is readonly */
  2036. page = ZERO_PAGE(address);
  2037. page_cache_get(page);
  2038. entry = mk_pte(page, vma->vm_page_prot);
  2039. ptl = pte_lockptr(mm, pmd);
  2040. spin_lock(ptl);
  2041. if (!pte_none(*page_table))
  2042. goto release;
  2043. inc_mm_counter(mm, file_rss);
  2044. page_add_file_rmap(page);
  2045. }
  2046. set_pte_at(mm, address, page_table, entry);
  2047. /* No need to invalidate - it was non-present before */
  2048. update_mmu_cache(vma, address, entry);
  2049. lazy_mmu_prot_update(entry);
  2050. unlock:
  2051. pte_unmap_unlock(page_table, ptl);
  2052. return VM_FAULT_MINOR;
  2053. release:
  2054. page_cache_release(page);
  2055. goto unlock;
  2056. oom:
  2057. return VM_FAULT_OOM;
  2058. }
  2059. /*
  2060. * __do_fault() tries to create a new page mapping. It aggressively
  2061. * tries to share with existing pages, but makes a separate copy if
  2062. * the FAULT_FLAG_WRITE is set in the flags parameter in order to avoid
  2063. * the next page fault.
  2064. *
  2065. * As this is called only for pages that do not currently exist, we
  2066. * do not need to flush old virtual caches or the TLB.
  2067. *
  2068. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  2069. * but allow concurrent faults), and pte mapped but not yet locked.
  2070. * We return with mmap_sem still held, but pte unmapped and unlocked.
  2071. */
  2072. static int __do_fault(struct mm_struct *mm, struct vm_area_struct *vma,
  2073. unsigned long address, pte_t *page_table, pmd_t *pmd,
  2074. pgoff_t pgoff, unsigned int flags, pte_t orig_pte)
  2075. {
  2076. spinlock_t *ptl;
  2077. struct page *page, *faulted_page;
  2078. pte_t entry;
  2079. int anon = 0;
  2080. struct page *dirty_page = NULL;
  2081. struct fault_data fdata;
  2082. fdata.address = address & PAGE_MASK;
  2083. fdata.pgoff = pgoff;
  2084. fdata.flags = flags;
  2085. pte_unmap(page_table);
  2086. BUG_ON(vma->vm_flags & VM_PFNMAP);
  2087. if (likely(vma->vm_ops->fault)) {
  2088. fdata.type = -1;
  2089. faulted_page = vma->vm_ops->fault(vma, &fdata);
  2090. WARN_ON(fdata.type == -1);
  2091. if (unlikely(!faulted_page))
  2092. return fdata.type;
  2093. } else {
  2094. /* Legacy ->nopage path */
  2095. fdata.type = VM_FAULT_MINOR;
  2096. faulted_page = vma->vm_ops->nopage(vma, address & PAGE_MASK,
  2097. &fdata.type);
  2098. /* no page was available -- either SIGBUS or OOM */
  2099. if (unlikely(faulted_page == NOPAGE_SIGBUS))
  2100. return VM_FAULT_SIGBUS;
  2101. else if (unlikely(faulted_page == NOPAGE_OOM))
  2102. return VM_FAULT_OOM;
  2103. }
  2104. /*
  2105. * For consistency in subsequent calls, make the faulted_page always
  2106. * locked.
  2107. */
  2108. if (unlikely(!(vma->vm_flags & VM_CAN_INVALIDATE)))
  2109. lock_page(faulted_page);
  2110. else
  2111. BUG_ON(!PageLocked(faulted_page));
  2112. /*
  2113. * Should we do an early C-O-W break?
  2114. */
  2115. page = faulted_page;
  2116. if (flags & FAULT_FLAG_WRITE) {
  2117. if (!(vma->vm_flags & VM_SHARED)) {
  2118. anon = 1;
  2119. if (unlikely(anon_vma_prepare(vma))) {
  2120. fdata.type = VM_FAULT_OOM;
  2121. goto out;
  2122. }
  2123. page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address);
  2124. if (!page) {
  2125. fdata.type = VM_FAULT_OOM;
  2126. goto out;
  2127. }
  2128. copy_user_highpage(page, faulted_page, address, vma);
  2129. } else {
  2130. /*
  2131. * If the page will be shareable, see if the backing
  2132. * address space wants to know that the page is about
  2133. * to become writable
  2134. */
  2135. if (vma->vm_ops->page_mkwrite &&
  2136. vma->vm_ops->page_mkwrite(vma, page) < 0) {
  2137. fdata.type = VM_FAULT_SIGBUS;
  2138. anon = 1; /* no anon but release faulted_page */
  2139. goto out;
  2140. }
  2141. }
  2142. }
  2143. page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
  2144. /*
  2145. * This silly early PAGE_DIRTY setting removes a race
  2146. * due to the bad i386 page protection. But it's valid
  2147. * for other architectures too.
  2148. *
  2149. * Note that if write_access is true, we either now have
  2150. * an exclusive copy of the page, or this is a shared mapping,
  2151. * so we can make it writable and dirty to avoid having to
  2152. * handle that later.
  2153. */
  2154. /* Only go through if we didn't race with anybody else... */
  2155. if (likely(pte_same(*page_table, orig_pte))) {
  2156. flush_icache_page(vma, page);
  2157. entry = mk_pte(page, vma->vm_page_prot);
  2158. if (flags & FAULT_FLAG_WRITE)
  2159. entry = maybe_mkwrite(pte_mkdirty(entry), vma);
  2160. set_pte_at(mm, address, page_table, entry);
  2161. if (anon) {
  2162. inc_mm_counter(mm, anon_rss);
  2163. lru_cache_add_active(page);
  2164. page_add_new_anon_rmap(page, vma, address);
  2165. } else {
  2166. inc_mm_counter(mm, file_rss);
  2167. page_add_file_rmap(page);
  2168. if (flags & FAULT_FLAG_WRITE) {
  2169. dirty_page = page;
  2170. get_page(dirty_page);
  2171. }
  2172. }
  2173. /* no need to invalidate: a not-present page won't be cached */
  2174. update_mmu_cache(vma, address, entry);
  2175. lazy_mmu_prot_update(entry);
  2176. } else {
  2177. if (anon)
  2178. page_cache_release(page);
  2179. else
  2180. anon = 1; /* no anon but release faulted_page */
  2181. }
  2182. pte_unmap_unlock(page_table, ptl);
  2183. out:
  2184. unlock_page(faulted_page);
  2185. if (anon)
  2186. page_cache_release(faulted_page);
  2187. else if (dirty_page) {
  2188. set_page_dirty_balance(dirty_page);
  2189. put_page(dirty_page);
  2190. }
  2191. return fdata.type;
  2192. }
  2193. static int do_linear_fault(struct mm_struct *mm, struct vm_area_struct *vma,
  2194. unsigned long address, pte_t *page_table, pmd_t *pmd,
  2195. int write_access, pte_t orig_pte)
  2196. {
  2197. pgoff_t pgoff = (((address & PAGE_MASK)
  2198. - vma->vm_start) >> PAGE_CACHE_SHIFT) + vma->vm_pgoff;
  2199. unsigned int flags = (write_access ? FAULT_FLAG_WRITE : 0);
  2200. return __do_fault(mm, vma, address, page_table, pmd, pgoff, flags, orig_pte);
  2201. }
  2202. static int do_nonlinear_fault(struct mm_struct *mm, struct vm_area_struct *vma,
  2203. unsigned long address, pte_t *page_table, pmd_t *pmd,
  2204. int write_access, pgoff_t pgoff, pte_t orig_pte)
  2205. {
  2206. unsigned int flags = FAULT_FLAG_NONLINEAR |
  2207. (write_access ? FAULT_FLAG_WRITE : 0);
  2208. return __do_fault(mm, vma, address, page_table, pmd, pgoff, flags, orig_pte);
  2209. }
  2210. /*
  2211. * do_no_pfn() tries to create a new page mapping for a page without
  2212. * a struct_page backing it
  2213. *
  2214. * As this is called only for pages that do not currently exist, we
  2215. * do not need to flush old virtual caches or the TLB.
  2216. *
  2217. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  2218. * but allow concurrent faults), and pte mapped but not yet locked.
  2219. * We return with mmap_sem still held, but pte unmapped and unlocked.
  2220. *
  2221. * It is expected that the ->nopfn handler always returns the same pfn
  2222. * for a given virtual mapping.
  2223. *
  2224. * Mark this `noinline' to prevent it from bloating the main pagefault code.
  2225. */
  2226. static noinline int do_no_pfn(struct mm_struct *mm, struct vm_area_struct *vma,
  2227. unsigned long address, pte_t *page_table, pmd_t *pmd,
  2228. int write_access)
  2229. {
  2230. spinlock_t *ptl;
  2231. pte_t entry;
  2232. unsigned long pfn;
  2233. int ret = VM_FAULT_MINOR;
  2234. pte_unmap(page_table);
  2235. BUG_ON(!(vma->vm_flags & VM_PFNMAP));
  2236. BUG_ON(is_cow_mapping(vma->vm_flags));
  2237. pfn = vma->vm_ops->nopfn(vma, address & PAGE_MASK);
  2238. if (unlikely(pfn == NOPFN_OOM))
  2239. return VM_FAULT_OOM;
  2240. else if (unlikely(pfn == NOPFN_SIGBUS))
  2241. return VM_FAULT_SIGBUS;
  2242. else if (unlikely(pfn == NOPFN_REFAULT))
  2243. return VM_FAULT_MINOR;
  2244. page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
  2245. /* Only go through if we didn't race with anybody else... */
  2246. if (pte_none(*page_table)) {
  2247. entry = pfn_pte(pfn, vma->vm_page_prot);
  2248. if (write_access)
  2249. entry = maybe_mkwrite(pte_mkdirty(entry), vma);
  2250. set_pte_at(mm, address, page_table, entry);
  2251. }
  2252. pte_unmap_unlock(page_table, ptl);
  2253. return ret;
  2254. }
  2255. /*
  2256. * Fault of a previously existing named mapping. Repopulate the pte
  2257. * from the encoded file_pte if possible. This enables swappable
  2258. * nonlinear vmas.
  2259. *
  2260. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  2261. * but allow concurrent faults), and pte mapped but not yet locked.
  2262. * We return with mmap_sem still held, but pte unmapped and unlocked.
  2263. */
  2264. static int do_file_page(struct mm_struct *mm, struct vm_area_struct *vma,
  2265. unsigned long address, pte_t *page_table, pmd_t *pmd,
  2266. int write_access, pte_t orig_pte)
  2267. {
  2268. pgoff_t pgoff;
  2269. int err;
  2270. if (!pte_unmap_same(mm, pmd, page_table, orig_pte))
  2271. return VM_FAULT_MINOR;
  2272. if (unlikely(!(vma->vm_flags & VM_NONLINEAR))) {
  2273. /*
  2274. * Page table corrupted: show pte and kill process.
  2275. */
  2276. print_bad_pte(vma, orig_pte, address);
  2277. return VM_FAULT_OOM;
  2278. }
  2279. pgoff = pte_to_pgoff(orig_pte);
  2280. if (vma->vm_ops && vma->vm_ops->fault)
  2281. return do_nonlinear_fault(mm, vma, address, page_table, pmd,
  2282. write_access, pgoff, orig_pte);
  2283. /* We can then assume vm->vm_ops && vma->vm_ops->populate */
  2284. err = vma->vm_ops->populate(vma, address & PAGE_MASK, PAGE_SIZE,
  2285. vma->vm_page_prot, pgoff, 0);
  2286. if (err == -ENOMEM)
  2287. return VM_FAULT_OOM;
  2288. if (err)
  2289. return VM_FAULT_SIGBUS;
  2290. return VM_FAULT_MAJOR;
  2291. }
  2292. /*
  2293. * These routines also need to handle stuff like marking pages dirty
  2294. * and/or accessed for architectures that don't do it in hardware (most
  2295. * RISC architectures). The early dirtying is also good on the i386.
  2296. *
  2297. * There is also a hook called "update_mmu_cache()" that architectures
  2298. * with external mmu caches can use to update those (ie the Sparc or
  2299. * PowerPC hashed page tables that act as extended TLBs).
  2300. *
  2301. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  2302. * but allow concurrent faults), and pte mapped but not yet locked.
  2303. * We return with mmap_sem still held, but pte unmapped and unlocked.
  2304. */
  2305. static inline int handle_pte_fault(struct mm_struct *mm,
  2306. struct vm_area_struct *vma, unsigned long address,
  2307. pte_t *pte, pmd_t *pmd, int write_access)
  2308. {
  2309. pte_t entry;
  2310. spinlock_t *ptl;
  2311. entry = *pte;
  2312. if (!pte_present(entry)) {
  2313. if (pte_none(entry)) {
  2314. if (vma->vm_ops) {
  2315. if (vma->vm_ops->fault || vma->vm_ops->nopage)
  2316. return do_linear_fault(mm, vma, address,
  2317. pte, pmd, write_access, entry);
  2318. if (unlikely(vma->vm_ops->nopfn))
  2319. return do_no_pfn(mm, vma, address, pte,
  2320. pmd, write_access);
  2321. }
  2322. return do_anonymous_page(mm, vma, address,
  2323. pte, pmd, write_access);
  2324. }
  2325. if (pte_file(entry))
  2326. return do_file_page(mm, vma, address,
  2327. pte, pmd, write_access, entry);
  2328. return do_swap_page(mm, vma, address,
  2329. pte, pmd, write_access, entry);
  2330. }
  2331. ptl = pte_lockptr(mm, pmd);
  2332. spin_lock(ptl);
  2333. if (unlikely(!pte_same(*pte, entry)))
  2334. goto unlock;
  2335. if (write_access) {
  2336. if (!pte_write(entry))
  2337. return do_wp_page(mm, vma, address,
  2338. pte, pmd, ptl, entry);
  2339. entry = pte_mkdirty(entry);
  2340. }
  2341. entry = pte_mkyoung(entry);
  2342. if (ptep_set_access_flags(vma, address, pte, entry, write_access)) {
  2343. update_mmu_cache(vma, address, entry);
  2344. lazy_mmu_prot_update(entry);
  2345. } else {
  2346. /*
  2347. * This is needed only for protection faults but the arch code
  2348. * is not yet telling us if this is a protection fault or not.
  2349. * This still avoids useless tlb flushes for .text page faults
  2350. * with threads.
  2351. */
  2352. if (write_access)
  2353. flush_tlb_page(vma, address);
  2354. }
  2355. unlock:
  2356. pte_unmap_unlock(pte, ptl);
  2357. return VM_FAULT_MINOR;
  2358. }
  2359. /*
  2360. * By the time we get here, we already hold the mm semaphore
  2361. */
  2362. int __handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma,
  2363. unsigned long address, int write_access)
  2364. {
  2365. pgd_t *pgd;
  2366. pud_t *pud;
  2367. pmd_t *pmd;
  2368. pte_t *pte;
  2369. __set_current_state(TASK_RUNNING);
  2370. count_vm_event(PGFAULT);
  2371. if (unlikely(is_vm_hugetlb_page(vma)))
  2372. return hugetlb_fault(mm, vma, address, write_access);
  2373. pgd = pgd_offset(mm, address);
  2374. pud = pud_alloc(mm, pgd, address);
  2375. if (!pud)
  2376. return VM_FAULT_OOM;
  2377. pmd = pmd_alloc(mm, pud, address);
  2378. if (!pmd)
  2379. return VM_FAULT_OOM;
  2380. pte = pte_alloc_map(mm, pmd, address);
  2381. if (!pte)
  2382. return VM_FAULT_OOM;
  2383. return handle_pte_fault(mm, vma, address, pte, pmd, write_access);
  2384. }
  2385. EXPORT_SYMBOL_GPL(__handle_mm_fault);
  2386. #ifndef __PAGETABLE_PUD_FOLDED
  2387. /*
  2388. * Allocate page upper directory.
  2389. * We've already handled the fast-path in-line.
  2390. */
  2391. int __pud_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
  2392. {
  2393. pud_t *new = pud_alloc_one(mm, address);
  2394. if (!new)
  2395. return -ENOMEM;
  2396. spin_lock(&mm->page_table_lock);
  2397. if (pgd_present(*pgd)) /* Another has populated it */
  2398. pud_free(new);
  2399. else
  2400. pgd_populate(mm, pgd, new);
  2401. spin_unlock(&mm->page_table_lock);
  2402. return 0;
  2403. }
  2404. #endif /* __PAGETABLE_PUD_FOLDED */
  2405. #ifndef __PAGETABLE_PMD_FOLDED
  2406. /*
  2407. * Allocate page middle directory.
  2408. * We've already handled the fast-path in-line.
  2409. */
  2410. int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address)
  2411. {
  2412. pmd_t *new = pmd_alloc_one(mm, address);
  2413. if (!new)
  2414. return -ENOMEM;
  2415. spin_lock(&mm->page_table_lock);
  2416. #ifndef __ARCH_HAS_4LEVEL_HACK
  2417. if (pud_present(*pud)) /* Another has populated it */
  2418. pmd_free(new);
  2419. else
  2420. pud_populate(mm, pud, new);
  2421. #else
  2422. if (pgd_present(*pud)) /* Another has populated it */
  2423. pmd_free(new);
  2424. else
  2425. pgd_populate(mm, pud, new);
  2426. #endif /* __ARCH_HAS_4LEVEL_HACK */
  2427. spin_unlock(&mm->page_table_lock);
  2428. return 0;
  2429. }
  2430. #endif /* __PAGETABLE_PMD_FOLDED */
  2431. int make_pages_present(unsigned long addr, unsigned long end)
  2432. {
  2433. int ret, len, write;
  2434. struct vm_area_struct * vma;
  2435. vma = find_vma(current->mm, addr);
  2436. if (!vma)
  2437. return -1;
  2438. write = (vma->vm_flags & VM_WRITE) != 0;
  2439. BUG_ON(addr >= end);
  2440. BUG_ON(end > vma->vm_end);
  2441. len = DIV_ROUND_UP(end, PAGE_SIZE) - addr/PAGE_SIZE;
  2442. ret = get_user_pages(current, current->mm, addr,
  2443. len, write, 0, NULL, NULL);
  2444. if (ret < 0)
  2445. return ret;
  2446. return ret == len ? 0 : -1;
  2447. }
  2448. /*
  2449. * Map a vmalloc()-space virtual address to the physical page.
  2450. */
  2451. struct page * vmalloc_to_page(void * vmalloc_addr)
  2452. {
  2453. unsigned long addr = (unsigned long) vmalloc_addr;
  2454. struct page *page = NULL;
  2455. pgd_t *pgd = pgd_offset_k(addr);
  2456. pud_t *pud;
  2457. pmd_t *pmd;
  2458. pte_t *ptep, pte;
  2459. if (!pgd_none(*pgd)) {
  2460. pud = pud_offset(pgd, addr);
  2461. if (!pud_none(*pud)) {
  2462. pmd = pmd_offset(pud, addr);
  2463. if (!pmd_none(*pmd)) {
  2464. ptep = pte_offset_map(pmd, addr);
  2465. pte = *ptep;
  2466. if (pte_present(pte))
  2467. page = pte_page(pte);
  2468. pte_unmap(ptep);
  2469. }
  2470. }
  2471. }
  2472. return page;
  2473. }
  2474. EXPORT_SYMBOL(vmalloc_to_page);
  2475. /*
  2476. * Map a vmalloc()-space virtual address to the physical page frame number.
  2477. */
  2478. unsigned long vmalloc_to_pfn(void * vmalloc_addr)
  2479. {
  2480. return page_to_pfn(vmalloc_to_page(vmalloc_addr));
  2481. }
  2482. EXPORT_SYMBOL(vmalloc_to_pfn);
  2483. #if !defined(__HAVE_ARCH_GATE_AREA)
  2484. #if defined(AT_SYSINFO_EHDR)
  2485. static struct vm_area_struct gate_vma;
  2486. static int __init gate_vma_init(void)
  2487. {
  2488. gate_vma.vm_mm = NULL;
  2489. gate_vma.vm_start = FIXADDR_USER_START;
  2490. gate_vma.vm_end = FIXADDR_USER_END;
  2491. gate_vma.vm_flags = VM_READ | VM_MAYREAD | VM_EXEC | VM_MAYEXEC;
  2492. gate_vma.vm_page_prot = __P101;
  2493. /*
  2494. * Make sure the vDSO gets into every core dump.
  2495. * Dumping its contents makes post-mortem fully interpretable later
  2496. * without matching up the same kernel and hardware config to see
  2497. * what PC values meant.
  2498. */
  2499. gate_vma.vm_flags |= VM_ALWAYSDUMP;
  2500. return 0;
  2501. }
  2502. __initcall(gate_vma_init);
  2503. #endif
  2504. struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
  2505. {
  2506. #ifdef AT_SYSINFO_EHDR
  2507. return &gate_vma;
  2508. #else
  2509. return NULL;
  2510. #endif
  2511. }
  2512. int in_gate_area_no_task(unsigned long addr)
  2513. {
  2514. #ifdef AT_SYSINFO_EHDR
  2515. if ((addr >= FIXADDR_USER_START) && (addr < FIXADDR_USER_END))
  2516. return 1;
  2517. #endif
  2518. return 0;
  2519. }
  2520. #endif /* __HAVE_ARCH_GATE_AREA */
  2521. /*
  2522. * Access another process' address space.
  2523. * Source/target buffer must be kernel space,
  2524. * Do not walk the page table directly, use get_user_pages
  2525. */
  2526. int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
  2527. {
  2528. struct mm_struct *mm;
  2529. struct vm_area_struct *vma;
  2530. struct page *page;
  2531. void *old_buf = buf;
  2532. mm = get_task_mm(tsk);
  2533. if (!mm)
  2534. return 0;
  2535. down_read(&mm->mmap_sem);
  2536. /* ignore errors, just check how much was sucessfully transfered */
  2537. while (len) {
  2538. int bytes, ret, offset;
  2539. void *maddr;
  2540. ret = get_user_pages(tsk, mm, addr, 1,
  2541. write, 1, &page, &vma);
  2542. if (ret <= 0)
  2543. break;
  2544. bytes = len;
  2545. offset = addr & (PAGE_SIZE-1);
  2546. if (bytes > PAGE_SIZE-offset)
  2547. bytes = PAGE_SIZE-offset;
  2548. maddr = kmap(page);
  2549. if (write) {
  2550. copy_to_user_page(vma, page, addr,
  2551. maddr + offset, buf, bytes);
  2552. set_page_dirty_lock(page);
  2553. } else {
  2554. copy_from_user_page(vma, page, addr,
  2555. buf, maddr + offset, bytes);
  2556. }
  2557. kunmap(page);
  2558. page_cache_release(page);
  2559. len -= bytes;
  2560. buf += bytes;
  2561. addr += bytes;
  2562. }
  2563. up_read(&mm->mmap_sem);
  2564. mmput(mm);
  2565. return buf - old_buf;
  2566. }