memory.c 79 KB

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