memory.c 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961
  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. /*
  810. * It is undesirable to test vma->vm_file as it
  811. * should be non-null for valid hugetlb area.
  812. * However, vm_file will be NULL in the error
  813. * cleanup path of do_mmap_pgoff. When
  814. * hugetlbfs ->mmap method fails,
  815. * do_mmap_pgoff() nullifies vma->vm_file
  816. * before calling this function to clean up.
  817. * Since no pte has actually been setup, it is
  818. * safe to do nothing in this case.
  819. */
  820. if (vma->vm_file) {
  821. unmap_hugepage_range(vma, start, end, NULL);
  822. zap_work -= (end - start) /
  823. pages_per_huge_page(hstate_vma(vma));
  824. }
  825. start = end;
  826. } else
  827. start = unmap_page_range(*tlbp, vma,
  828. start, end, &zap_work, details);
  829. if (zap_work > 0) {
  830. BUG_ON(start != end);
  831. break;
  832. }
  833. tlb_finish_mmu(*tlbp, tlb_start, start);
  834. if (need_resched() ||
  835. (i_mmap_lock && spin_needbreak(i_mmap_lock))) {
  836. if (i_mmap_lock) {
  837. *tlbp = NULL;
  838. goto out;
  839. }
  840. cond_resched();
  841. }
  842. *tlbp = tlb_gather_mmu(vma->vm_mm, fullmm);
  843. tlb_start_valid = 0;
  844. zap_work = ZAP_BLOCK_SIZE;
  845. }
  846. }
  847. out:
  848. return start; /* which is now the end (or restart) address */
  849. }
  850. /**
  851. * zap_page_range - remove user pages in a given range
  852. * @vma: vm_area_struct holding the applicable pages
  853. * @address: starting address of pages to zap
  854. * @size: number of bytes to zap
  855. * @details: details of nonlinear truncation or shared cache invalidation
  856. */
  857. unsigned long zap_page_range(struct vm_area_struct *vma, unsigned long address,
  858. unsigned long size, struct zap_details *details)
  859. {
  860. struct mm_struct *mm = vma->vm_mm;
  861. struct mmu_gather *tlb;
  862. unsigned long end = address + size;
  863. unsigned long nr_accounted = 0;
  864. lru_add_drain();
  865. tlb = tlb_gather_mmu(mm, 0);
  866. update_hiwater_rss(mm);
  867. end = unmap_vmas(&tlb, vma, address, end, &nr_accounted, details);
  868. if (tlb)
  869. tlb_finish_mmu(tlb, address, end);
  870. return end;
  871. }
  872. /*
  873. * Do a quick page-table lookup for a single page.
  874. */
  875. struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
  876. unsigned int flags)
  877. {
  878. pgd_t *pgd;
  879. pud_t *pud;
  880. pmd_t *pmd;
  881. pte_t *ptep, pte;
  882. spinlock_t *ptl;
  883. struct page *page;
  884. struct mm_struct *mm = vma->vm_mm;
  885. page = follow_huge_addr(mm, address, flags & FOLL_WRITE);
  886. if (!IS_ERR(page)) {
  887. BUG_ON(flags & FOLL_GET);
  888. goto out;
  889. }
  890. page = NULL;
  891. pgd = pgd_offset(mm, address);
  892. if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
  893. goto no_page_table;
  894. pud = pud_offset(pgd, address);
  895. if (pud_none(*pud))
  896. goto no_page_table;
  897. if (pud_huge(*pud)) {
  898. BUG_ON(flags & FOLL_GET);
  899. page = follow_huge_pud(mm, address, pud, flags & FOLL_WRITE);
  900. goto out;
  901. }
  902. if (unlikely(pud_bad(*pud)))
  903. goto no_page_table;
  904. pmd = pmd_offset(pud, address);
  905. if (pmd_none(*pmd))
  906. goto no_page_table;
  907. if (pmd_huge(*pmd)) {
  908. BUG_ON(flags & FOLL_GET);
  909. page = follow_huge_pmd(mm, address, pmd, flags & FOLL_WRITE);
  910. goto out;
  911. }
  912. if (unlikely(pmd_bad(*pmd)))
  913. goto no_page_table;
  914. ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
  915. pte = *ptep;
  916. if (!pte_present(pte))
  917. goto no_page;
  918. if ((flags & FOLL_WRITE) && !pte_write(pte))
  919. goto unlock;
  920. page = vm_normal_page(vma, address, pte);
  921. if (unlikely(!page))
  922. goto bad_page;
  923. if (flags & FOLL_GET)
  924. get_page(page);
  925. if (flags & FOLL_TOUCH) {
  926. if ((flags & FOLL_WRITE) &&
  927. !pte_dirty(pte) && !PageDirty(page))
  928. set_page_dirty(page);
  929. mark_page_accessed(page);
  930. }
  931. unlock:
  932. pte_unmap_unlock(ptep, ptl);
  933. out:
  934. return page;
  935. bad_page:
  936. pte_unmap_unlock(ptep, ptl);
  937. return ERR_PTR(-EFAULT);
  938. no_page:
  939. pte_unmap_unlock(ptep, ptl);
  940. if (!pte_none(pte))
  941. return page;
  942. /* Fall through to ZERO_PAGE handling */
  943. no_page_table:
  944. /*
  945. * When core dumping an enormous anonymous area that nobody
  946. * has touched so far, we don't want to allocate page tables.
  947. */
  948. if (flags & FOLL_ANON) {
  949. page = ZERO_PAGE(0);
  950. if (flags & FOLL_GET)
  951. get_page(page);
  952. BUG_ON(flags & FOLL_WRITE);
  953. }
  954. return page;
  955. }
  956. /* Can we do the FOLL_ANON optimization? */
  957. static inline int use_zero_page(struct vm_area_struct *vma)
  958. {
  959. /*
  960. * We don't want to optimize FOLL_ANON for make_pages_present()
  961. * when it tries to page in a VM_LOCKED region. As to VM_SHARED,
  962. * we want to get the page from the page tables to make sure
  963. * that we serialize and update with any other user of that
  964. * mapping.
  965. */
  966. if (vma->vm_flags & (VM_LOCKED | VM_SHARED))
  967. return 0;
  968. /*
  969. * And if we have a fault routine, it's not an anonymous region.
  970. */
  971. return !vma->vm_ops || !vma->vm_ops->fault;
  972. }
  973. int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
  974. unsigned long start, int len, int write, int force,
  975. struct page **pages, struct vm_area_struct **vmas)
  976. {
  977. int i;
  978. unsigned int vm_flags;
  979. if (len <= 0)
  980. return 0;
  981. /*
  982. * Require read or write permissions.
  983. * If 'force' is set, we only require the "MAY" flags.
  984. */
  985. vm_flags = write ? (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
  986. vm_flags &= force ? (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
  987. i = 0;
  988. do {
  989. struct vm_area_struct *vma;
  990. unsigned int foll_flags;
  991. vma = find_extend_vma(mm, start);
  992. if (!vma && in_gate_area(tsk, start)) {
  993. unsigned long pg = start & PAGE_MASK;
  994. struct vm_area_struct *gate_vma = get_gate_vma(tsk);
  995. pgd_t *pgd;
  996. pud_t *pud;
  997. pmd_t *pmd;
  998. pte_t *pte;
  999. if (write) /* user gate pages are read-only */
  1000. return i ? : -EFAULT;
  1001. if (pg > TASK_SIZE)
  1002. pgd = pgd_offset_k(pg);
  1003. else
  1004. pgd = pgd_offset_gate(mm, pg);
  1005. BUG_ON(pgd_none(*pgd));
  1006. pud = pud_offset(pgd, pg);
  1007. BUG_ON(pud_none(*pud));
  1008. pmd = pmd_offset(pud, pg);
  1009. if (pmd_none(*pmd))
  1010. return i ? : -EFAULT;
  1011. pte = pte_offset_map(pmd, pg);
  1012. if (pte_none(*pte)) {
  1013. pte_unmap(pte);
  1014. return i ? : -EFAULT;
  1015. }
  1016. if (pages) {
  1017. struct page *page = vm_normal_page(gate_vma, start, *pte);
  1018. pages[i] = page;
  1019. if (page)
  1020. get_page(page);
  1021. }
  1022. pte_unmap(pte);
  1023. if (vmas)
  1024. vmas[i] = gate_vma;
  1025. i++;
  1026. start += PAGE_SIZE;
  1027. len--;
  1028. continue;
  1029. }
  1030. if (!vma || (vma->vm_flags & (VM_IO | VM_PFNMAP))
  1031. || !(vm_flags & vma->vm_flags))
  1032. return i ? : -EFAULT;
  1033. if (is_vm_hugetlb_page(vma)) {
  1034. i = follow_hugetlb_page(mm, vma, pages, vmas,
  1035. &start, &len, i, write);
  1036. continue;
  1037. }
  1038. foll_flags = FOLL_TOUCH;
  1039. if (pages)
  1040. foll_flags |= FOLL_GET;
  1041. if (!write && use_zero_page(vma))
  1042. foll_flags |= FOLL_ANON;
  1043. do {
  1044. struct page *page;
  1045. /*
  1046. * If tsk is ooming, cut off its access to large memory
  1047. * allocations. It has a pending SIGKILL, but it can't
  1048. * be processed until returning to user space.
  1049. */
  1050. if (unlikely(test_tsk_thread_flag(tsk, TIF_MEMDIE)))
  1051. return i ? i : -ENOMEM;
  1052. if (write)
  1053. foll_flags |= FOLL_WRITE;
  1054. cond_resched();
  1055. while (!(page = follow_page(vma, start, foll_flags))) {
  1056. int ret;
  1057. ret = handle_mm_fault(mm, vma, start,
  1058. foll_flags & FOLL_WRITE);
  1059. if (ret & VM_FAULT_ERROR) {
  1060. if (ret & VM_FAULT_OOM)
  1061. return i ? i : -ENOMEM;
  1062. else if (ret & VM_FAULT_SIGBUS)
  1063. return i ? i : -EFAULT;
  1064. BUG();
  1065. }
  1066. if (ret & VM_FAULT_MAJOR)
  1067. tsk->maj_flt++;
  1068. else
  1069. tsk->min_flt++;
  1070. /*
  1071. * The VM_FAULT_WRITE bit tells us that
  1072. * do_wp_page has broken COW when necessary,
  1073. * even if maybe_mkwrite decided not to set
  1074. * pte_write. We can thus safely do subsequent
  1075. * page lookups as if they were reads.
  1076. */
  1077. if (ret & VM_FAULT_WRITE)
  1078. foll_flags &= ~FOLL_WRITE;
  1079. cond_resched();
  1080. }
  1081. if (IS_ERR(page))
  1082. return i ? i : PTR_ERR(page);
  1083. if (pages) {
  1084. pages[i] = page;
  1085. flush_anon_page(vma, page, start);
  1086. flush_dcache_page(page);
  1087. }
  1088. if (vmas)
  1089. vmas[i] = vma;
  1090. i++;
  1091. start += PAGE_SIZE;
  1092. len--;
  1093. } while (len && start < vma->vm_end);
  1094. } while (len);
  1095. return i;
  1096. }
  1097. EXPORT_SYMBOL(get_user_pages);
  1098. pte_t *get_locked_pte(struct mm_struct *mm, unsigned long addr,
  1099. spinlock_t **ptl)
  1100. {
  1101. pgd_t * pgd = pgd_offset(mm, addr);
  1102. pud_t * pud = pud_alloc(mm, pgd, addr);
  1103. if (pud) {
  1104. pmd_t * pmd = pmd_alloc(mm, pud, addr);
  1105. if (pmd)
  1106. return pte_alloc_map_lock(mm, pmd, addr, ptl);
  1107. }
  1108. return NULL;
  1109. }
  1110. /*
  1111. * This is the old fallback for page remapping.
  1112. *
  1113. * For historical reasons, it only allows reserved pages. Only
  1114. * old drivers should use this, and they needed to mark their
  1115. * pages reserved for the old functions anyway.
  1116. */
  1117. static int insert_page(struct vm_area_struct *vma, unsigned long addr,
  1118. struct page *page, pgprot_t prot)
  1119. {
  1120. struct mm_struct *mm = vma->vm_mm;
  1121. int retval;
  1122. pte_t *pte;
  1123. spinlock_t *ptl;
  1124. retval = mem_cgroup_charge(page, mm, GFP_KERNEL);
  1125. if (retval)
  1126. goto out;
  1127. retval = -EINVAL;
  1128. if (PageAnon(page))
  1129. goto out_uncharge;
  1130. retval = -ENOMEM;
  1131. flush_dcache_page(page);
  1132. pte = get_locked_pte(mm, addr, &ptl);
  1133. if (!pte)
  1134. goto out_uncharge;
  1135. retval = -EBUSY;
  1136. if (!pte_none(*pte))
  1137. goto out_unlock;
  1138. /* Ok, finally just insert the thing.. */
  1139. get_page(page);
  1140. inc_mm_counter(mm, file_rss);
  1141. page_add_file_rmap(page);
  1142. set_pte_at(mm, addr, pte, mk_pte(page, prot));
  1143. retval = 0;
  1144. pte_unmap_unlock(pte, ptl);
  1145. return retval;
  1146. out_unlock:
  1147. pte_unmap_unlock(pte, ptl);
  1148. out_uncharge:
  1149. mem_cgroup_uncharge_page(page);
  1150. out:
  1151. return retval;
  1152. }
  1153. /**
  1154. * vm_insert_page - insert single page into user vma
  1155. * @vma: user vma to map to
  1156. * @addr: target user address of this page
  1157. * @page: source kernel page
  1158. *
  1159. * This allows drivers to insert individual pages they've allocated
  1160. * into a user vma.
  1161. *
  1162. * The page has to be a nice clean _individual_ kernel allocation.
  1163. * If you allocate a compound page, you need to have marked it as
  1164. * such (__GFP_COMP), or manually just split the page up yourself
  1165. * (see split_page()).
  1166. *
  1167. * NOTE! Traditionally this was done with "remap_pfn_range()" which
  1168. * took an arbitrary page protection parameter. This doesn't allow
  1169. * that. Your vma protection will have to be set up correctly, which
  1170. * means that if you want a shared writable mapping, you'd better
  1171. * ask for a shared writable mapping!
  1172. *
  1173. * The page does not need to be reserved.
  1174. */
  1175. int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
  1176. struct page *page)
  1177. {
  1178. if (addr < vma->vm_start || addr >= vma->vm_end)
  1179. return -EFAULT;
  1180. if (!page_count(page))
  1181. return -EINVAL;
  1182. vma->vm_flags |= VM_INSERTPAGE;
  1183. return insert_page(vma, addr, page, vma->vm_page_prot);
  1184. }
  1185. EXPORT_SYMBOL(vm_insert_page);
  1186. static int insert_pfn(struct vm_area_struct *vma, unsigned long addr,
  1187. unsigned long pfn, pgprot_t prot)
  1188. {
  1189. struct mm_struct *mm = vma->vm_mm;
  1190. int retval;
  1191. pte_t *pte, entry;
  1192. spinlock_t *ptl;
  1193. retval = -ENOMEM;
  1194. pte = get_locked_pte(mm, addr, &ptl);
  1195. if (!pte)
  1196. goto out;
  1197. retval = -EBUSY;
  1198. if (!pte_none(*pte))
  1199. goto out_unlock;
  1200. /* Ok, finally just insert the thing.. */
  1201. entry = pte_mkspecial(pfn_pte(pfn, prot));
  1202. set_pte_at(mm, addr, pte, entry);
  1203. update_mmu_cache(vma, addr, entry); /* XXX: why not for insert_page? */
  1204. retval = 0;
  1205. out_unlock:
  1206. pte_unmap_unlock(pte, ptl);
  1207. out:
  1208. return retval;
  1209. }
  1210. /**
  1211. * vm_insert_pfn - insert single pfn into user vma
  1212. * @vma: user vma to map to
  1213. * @addr: target user address of this page
  1214. * @pfn: source kernel pfn
  1215. *
  1216. * Similar to vm_inert_page, this allows drivers to insert individual pages
  1217. * they've allocated into a user vma. Same comments apply.
  1218. *
  1219. * This function should only be called from a vm_ops->fault handler, and
  1220. * in that case the handler should return NULL.
  1221. *
  1222. * vma cannot be a COW mapping.
  1223. *
  1224. * As this is called only for pages that do not currently exist, we
  1225. * do not need to flush old virtual caches or the TLB.
  1226. */
  1227. int vm_insert_pfn(struct vm_area_struct *vma, unsigned long addr,
  1228. unsigned long pfn)
  1229. {
  1230. /*
  1231. * Technically, architectures with pte_special can avoid all these
  1232. * restrictions (same for remap_pfn_range). However we would like
  1233. * consistency in testing and feature parity among all, so we should
  1234. * try to keep these invariants in place for everybody.
  1235. */
  1236. BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)));
  1237. BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) ==
  1238. (VM_PFNMAP|VM_MIXEDMAP));
  1239. BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags));
  1240. BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn));
  1241. if (addr < vma->vm_start || addr >= vma->vm_end)
  1242. return -EFAULT;
  1243. return insert_pfn(vma, addr, pfn, vma->vm_page_prot);
  1244. }
  1245. EXPORT_SYMBOL(vm_insert_pfn);
  1246. int vm_insert_mixed(struct vm_area_struct *vma, unsigned long addr,
  1247. unsigned long pfn)
  1248. {
  1249. BUG_ON(!(vma->vm_flags & VM_MIXEDMAP));
  1250. if (addr < vma->vm_start || addr >= vma->vm_end)
  1251. return -EFAULT;
  1252. /*
  1253. * If we don't have pte special, then we have to use the pfn_valid()
  1254. * based VM_MIXEDMAP scheme (see vm_normal_page), and thus we *must*
  1255. * refcount the page if pfn_valid is true (hence insert_page rather
  1256. * than insert_pfn).
  1257. */
  1258. if (!HAVE_PTE_SPECIAL && pfn_valid(pfn)) {
  1259. struct page *page;
  1260. page = pfn_to_page(pfn);
  1261. return insert_page(vma, addr, page, vma->vm_page_prot);
  1262. }
  1263. return insert_pfn(vma, addr, pfn, vma->vm_page_prot);
  1264. }
  1265. EXPORT_SYMBOL(vm_insert_mixed);
  1266. /*
  1267. * maps a range of physical memory into the requested pages. the old
  1268. * mappings are removed. any references to nonexistent pages results
  1269. * in null mappings (currently treated as "copy-on-access")
  1270. */
  1271. static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd,
  1272. unsigned long addr, unsigned long end,
  1273. unsigned long pfn, pgprot_t prot)
  1274. {
  1275. pte_t *pte;
  1276. spinlock_t *ptl;
  1277. pte = pte_alloc_map_lock(mm, pmd, addr, &ptl);
  1278. if (!pte)
  1279. return -ENOMEM;
  1280. arch_enter_lazy_mmu_mode();
  1281. do {
  1282. BUG_ON(!pte_none(*pte));
  1283. set_pte_at(mm, addr, pte, pte_mkspecial(pfn_pte(pfn, prot)));
  1284. pfn++;
  1285. } while (pte++, addr += PAGE_SIZE, addr != end);
  1286. arch_leave_lazy_mmu_mode();
  1287. pte_unmap_unlock(pte - 1, ptl);
  1288. return 0;
  1289. }
  1290. static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud,
  1291. unsigned long addr, unsigned long end,
  1292. unsigned long pfn, pgprot_t prot)
  1293. {
  1294. pmd_t *pmd;
  1295. unsigned long next;
  1296. pfn -= addr >> PAGE_SHIFT;
  1297. pmd = pmd_alloc(mm, pud, addr);
  1298. if (!pmd)
  1299. return -ENOMEM;
  1300. do {
  1301. next = pmd_addr_end(addr, end);
  1302. if (remap_pte_range(mm, pmd, addr, next,
  1303. pfn + (addr >> PAGE_SHIFT), prot))
  1304. return -ENOMEM;
  1305. } while (pmd++, addr = next, addr != end);
  1306. return 0;
  1307. }
  1308. static inline int remap_pud_range(struct mm_struct *mm, pgd_t *pgd,
  1309. unsigned long addr, unsigned long end,
  1310. unsigned long pfn, pgprot_t prot)
  1311. {
  1312. pud_t *pud;
  1313. unsigned long next;
  1314. pfn -= addr >> PAGE_SHIFT;
  1315. pud = pud_alloc(mm, pgd, addr);
  1316. if (!pud)
  1317. return -ENOMEM;
  1318. do {
  1319. next = pud_addr_end(addr, end);
  1320. if (remap_pmd_range(mm, pud, addr, next,
  1321. pfn + (addr >> PAGE_SHIFT), prot))
  1322. return -ENOMEM;
  1323. } while (pud++, addr = next, addr != end);
  1324. return 0;
  1325. }
  1326. /**
  1327. * remap_pfn_range - remap kernel memory to userspace
  1328. * @vma: user vma to map to
  1329. * @addr: target user address to start at
  1330. * @pfn: physical address of kernel memory
  1331. * @size: size of map area
  1332. * @prot: page protection flags for this mapping
  1333. *
  1334. * Note: this is only safe if the mm semaphore is held when called.
  1335. */
  1336. int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
  1337. unsigned long pfn, unsigned long size, pgprot_t prot)
  1338. {
  1339. pgd_t *pgd;
  1340. unsigned long next;
  1341. unsigned long end = addr + PAGE_ALIGN(size);
  1342. struct mm_struct *mm = vma->vm_mm;
  1343. int err;
  1344. /*
  1345. * Physically remapped pages are special. Tell the
  1346. * rest of the world about it:
  1347. * VM_IO tells people not to look at these pages
  1348. * (accesses can have side effects).
  1349. * VM_RESERVED is specified all over the place, because
  1350. * in 2.4 it kept swapout's vma scan off this vma; but
  1351. * in 2.6 the LRU scan won't even find its pages, so this
  1352. * flag means no more than count its pages in reserved_vm,
  1353. * and omit it from core dump, even when VM_IO turned off.
  1354. * VM_PFNMAP tells the core MM that the base pages are just
  1355. * raw PFN mappings, and do not have a "struct page" associated
  1356. * with them.
  1357. *
  1358. * There's a horrible special case to handle copy-on-write
  1359. * behaviour that some programs depend on. We mark the "original"
  1360. * un-COW'ed pages by matching them up with "vma->vm_pgoff".
  1361. */
  1362. if (is_cow_mapping(vma->vm_flags)) {
  1363. if (addr != vma->vm_start || end != vma->vm_end)
  1364. return -EINVAL;
  1365. vma->vm_pgoff = pfn;
  1366. }
  1367. vma->vm_flags |= VM_IO | VM_RESERVED | VM_PFNMAP;
  1368. BUG_ON(addr >= end);
  1369. pfn -= addr >> PAGE_SHIFT;
  1370. pgd = pgd_offset(mm, addr);
  1371. flush_cache_range(vma, addr, end);
  1372. do {
  1373. next = pgd_addr_end(addr, end);
  1374. err = remap_pud_range(mm, pgd, addr, next,
  1375. pfn + (addr >> PAGE_SHIFT), prot);
  1376. if (err)
  1377. break;
  1378. } while (pgd++, addr = next, addr != end);
  1379. return err;
  1380. }
  1381. EXPORT_SYMBOL(remap_pfn_range);
  1382. static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd,
  1383. unsigned long addr, unsigned long end,
  1384. pte_fn_t fn, void *data)
  1385. {
  1386. pte_t *pte;
  1387. int err;
  1388. pgtable_t token;
  1389. spinlock_t *uninitialized_var(ptl);
  1390. pte = (mm == &init_mm) ?
  1391. pte_alloc_kernel(pmd, addr) :
  1392. pte_alloc_map_lock(mm, pmd, addr, &ptl);
  1393. if (!pte)
  1394. return -ENOMEM;
  1395. BUG_ON(pmd_huge(*pmd));
  1396. token = pmd_pgtable(*pmd);
  1397. do {
  1398. err = fn(pte, token, addr, data);
  1399. if (err)
  1400. break;
  1401. } while (pte++, addr += PAGE_SIZE, addr != end);
  1402. if (mm != &init_mm)
  1403. pte_unmap_unlock(pte-1, ptl);
  1404. return err;
  1405. }
  1406. static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud,
  1407. unsigned long addr, unsigned long end,
  1408. pte_fn_t fn, void *data)
  1409. {
  1410. pmd_t *pmd;
  1411. unsigned long next;
  1412. int err;
  1413. BUG_ON(pud_huge(*pud));
  1414. pmd = pmd_alloc(mm, pud, addr);
  1415. if (!pmd)
  1416. return -ENOMEM;
  1417. do {
  1418. next = pmd_addr_end(addr, end);
  1419. err = apply_to_pte_range(mm, pmd, addr, next, fn, data);
  1420. if (err)
  1421. break;
  1422. } while (pmd++, addr = next, addr != end);
  1423. return err;
  1424. }
  1425. static int apply_to_pud_range(struct mm_struct *mm, pgd_t *pgd,
  1426. unsigned long addr, unsigned long end,
  1427. pte_fn_t fn, void *data)
  1428. {
  1429. pud_t *pud;
  1430. unsigned long next;
  1431. int err;
  1432. pud = pud_alloc(mm, pgd, addr);
  1433. if (!pud)
  1434. return -ENOMEM;
  1435. do {
  1436. next = pud_addr_end(addr, end);
  1437. err = apply_to_pmd_range(mm, pud, addr, next, fn, data);
  1438. if (err)
  1439. break;
  1440. } while (pud++, addr = next, addr != end);
  1441. return err;
  1442. }
  1443. /*
  1444. * Scan a region of virtual memory, filling in page tables as necessary
  1445. * and calling a provided function on each leaf page table.
  1446. */
  1447. int apply_to_page_range(struct mm_struct *mm, unsigned long addr,
  1448. unsigned long size, pte_fn_t fn, void *data)
  1449. {
  1450. pgd_t *pgd;
  1451. unsigned long next;
  1452. unsigned long end = addr + size;
  1453. int err;
  1454. BUG_ON(addr >= end);
  1455. pgd = pgd_offset(mm, addr);
  1456. do {
  1457. next = pgd_addr_end(addr, end);
  1458. err = apply_to_pud_range(mm, pgd, addr, next, fn, data);
  1459. if (err)
  1460. break;
  1461. } while (pgd++, addr = next, addr != end);
  1462. return err;
  1463. }
  1464. EXPORT_SYMBOL_GPL(apply_to_page_range);
  1465. /*
  1466. * handle_pte_fault chooses page fault handler according to an entry
  1467. * which was read non-atomically. Before making any commitment, on
  1468. * those architectures or configurations (e.g. i386 with PAE) which
  1469. * might give a mix of unmatched parts, do_swap_page and do_file_page
  1470. * must check under lock before unmapping the pte and proceeding
  1471. * (but do_wp_page is only called after already making such a check;
  1472. * and do_anonymous_page and do_no_page can safely check later on).
  1473. */
  1474. static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd,
  1475. pte_t *page_table, pte_t orig_pte)
  1476. {
  1477. int same = 1;
  1478. #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
  1479. if (sizeof(pte_t) > sizeof(unsigned long)) {
  1480. spinlock_t *ptl = pte_lockptr(mm, pmd);
  1481. spin_lock(ptl);
  1482. same = pte_same(*page_table, orig_pte);
  1483. spin_unlock(ptl);
  1484. }
  1485. #endif
  1486. pte_unmap(page_table);
  1487. return same;
  1488. }
  1489. /*
  1490. * Do pte_mkwrite, but only if the vma says VM_WRITE. We do this when
  1491. * servicing faults for write access. In the normal case, do always want
  1492. * pte_mkwrite. But get_user_pages can cause write faults for mappings
  1493. * that do not have writing enabled, when used by access_process_vm.
  1494. */
  1495. static inline pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma)
  1496. {
  1497. if (likely(vma->vm_flags & VM_WRITE))
  1498. pte = pte_mkwrite(pte);
  1499. return pte;
  1500. }
  1501. static inline void cow_user_page(struct page *dst, struct page *src, unsigned long va, struct vm_area_struct *vma)
  1502. {
  1503. /*
  1504. * If the source page was a PFN mapping, we don't have
  1505. * a "struct page" for it. We do a best-effort copy by
  1506. * just copying from the original user address. If that
  1507. * fails, we just zero-fill it. Live with it.
  1508. */
  1509. if (unlikely(!src)) {
  1510. void *kaddr = kmap_atomic(dst, KM_USER0);
  1511. void __user *uaddr = (void __user *)(va & PAGE_MASK);
  1512. /*
  1513. * This really shouldn't fail, because the page is there
  1514. * in the page tables. But it might just be unreadable,
  1515. * in which case we just give up and fill the result with
  1516. * zeroes.
  1517. */
  1518. if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE))
  1519. memset(kaddr, 0, PAGE_SIZE);
  1520. kunmap_atomic(kaddr, KM_USER0);
  1521. flush_dcache_page(dst);
  1522. } else
  1523. copy_user_highpage(dst, src, va, vma);
  1524. }
  1525. /*
  1526. * This routine handles present pages, when users try to write
  1527. * to a shared page. It is done by copying the page to a new address
  1528. * and decrementing the shared-page counter for the old page.
  1529. *
  1530. * Note that this routine assumes that the protection checks have been
  1531. * done by the caller (the low-level page fault routine in most cases).
  1532. * Thus we can safely just mark it writable once we've done any necessary
  1533. * COW.
  1534. *
  1535. * We also mark the page dirty at this point even though the page will
  1536. * change only once the write actually happens. This avoids a few races,
  1537. * and potentially makes it more efficient.
  1538. *
  1539. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  1540. * but allow concurrent faults), with pte both mapped and locked.
  1541. * We return with mmap_sem still held, but pte unmapped and unlocked.
  1542. */
  1543. static int do_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
  1544. unsigned long address, pte_t *page_table, pmd_t *pmd,
  1545. spinlock_t *ptl, pte_t orig_pte)
  1546. {
  1547. struct page *old_page, *new_page;
  1548. pte_t entry;
  1549. int reuse = 0, ret = 0;
  1550. int page_mkwrite = 0;
  1551. struct page *dirty_page = NULL;
  1552. old_page = vm_normal_page(vma, address, orig_pte);
  1553. if (!old_page) {
  1554. /*
  1555. * VM_MIXEDMAP !pfn_valid() case
  1556. *
  1557. * We should not cow pages in a shared writeable mapping.
  1558. * Just mark the pages writable as we can't do any dirty
  1559. * accounting on raw pfn maps.
  1560. */
  1561. if ((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
  1562. (VM_WRITE|VM_SHARED))
  1563. goto reuse;
  1564. goto gotten;
  1565. }
  1566. /*
  1567. * Take out anonymous pages first, anonymous shared vmas are
  1568. * not dirty accountable.
  1569. */
  1570. if (PageAnon(old_page)) {
  1571. if (!TestSetPageLocked(old_page)) {
  1572. reuse = can_share_swap_page(old_page);
  1573. unlock_page(old_page);
  1574. }
  1575. } else if (unlikely((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
  1576. (VM_WRITE|VM_SHARED))) {
  1577. /*
  1578. * Only catch write-faults on shared writable pages,
  1579. * read-only shared pages can get COWed by
  1580. * get_user_pages(.write=1, .force=1).
  1581. */
  1582. if (vma->vm_ops && vma->vm_ops->page_mkwrite) {
  1583. /*
  1584. * Notify the address space that the page is about to
  1585. * become writable so that it can prohibit this or wait
  1586. * for the page to get into an appropriate state.
  1587. *
  1588. * We do this without the lock held, so that it can
  1589. * sleep if it needs to.
  1590. */
  1591. page_cache_get(old_page);
  1592. pte_unmap_unlock(page_table, ptl);
  1593. if (vma->vm_ops->page_mkwrite(vma, old_page) < 0)
  1594. goto unwritable_page;
  1595. /*
  1596. * Since we dropped the lock we need to revalidate
  1597. * the PTE as someone else may have changed it. If
  1598. * they did, we just return, as we can count on the
  1599. * MMU to tell us if they didn't also make it writable.
  1600. */
  1601. page_table = pte_offset_map_lock(mm, pmd, address,
  1602. &ptl);
  1603. page_cache_release(old_page);
  1604. if (!pte_same(*page_table, orig_pte))
  1605. goto unlock;
  1606. page_mkwrite = 1;
  1607. }
  1608. dirty_page = old_page;
  1609. get_page(dirty_page);
  1610. reuse = 1;
  1611. }
  1612. if (reuse) {
  1613. reuse:
  1614. flush_cache_page(vma, address, pte_pfn(orig_pte));
  1615. entry = pte_mkyoung(orig_pte);
  1616. entry = maybe_mkwrite(pte_mkdirty(entry), vma);
  1617. if (ptep_set_access_flags(vma, address, page_table, entry,1))
  1618. update_mmu_cache(vma, address, entry);
  1619. ret |= VM_FAULT_WRITE;
  1620. goto unlock;
  1621. }
  1622. /*
  1623. * Ok, we need to copy. Oh, well..
  1624. */
  1625. page_cache_get(old_page);
  1626. gotten:
  1627. pte_unmap_unlock(page_table, ptl);
  1628. if (unlikely(anon_vma_prepare(vma)))
  1629. goto oom;
  1630. VM_BUG_ON(old_page == ZERO_PAGE(0));
  1631. new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address);
  1632. if (!new_page)
  1633. goto oom;
  1634. cow_user_page(new_page, old_page, address, vma);
  1635. __SetPageUptodate(new_page);
  1636. if (mem_cgroup_charge(new_page, mm, GFP_KERNEL))
  1637. goto oom_free_new;
  1638. /*
  1639. * Re-check the pte - we dropped the lock
  1640. */
  1641. page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
  1642. if (likely(pte_same(*page_table, orig_pte))) {
  1643. if (old_page) {
  1644. if (!PageAnon(old_page)) {
  1645. dec_mm_counter(mm, file_rss);
  1646. inc_mm_counter(mm, anon_rss);
  1647. }
  1648. } else
  1649. inc_mm_counter(mm, anon_rss);
  1650. flush_cache_page(vma, address, pte_pfn(orig_pte));
  1651. entry = mk_pte(new_page, vma->vm_page_prot);
  1652. entry = maybe_mkwrite(pte_mkdirty(entry), vma);
  1653. /*
  1654. * Clear the pte entry and flush it first, before updating the
  1655. * pte with the new entry. This will avoid a race condition
  1656. * seen in the presence of one thread doing SMC and another
  1657. * thread doing COW.
  1658. */
  1659. ptep_clear_flush(vma, address, page_table);
  1660. set_pte_at(mm, address, page_table, entry);
  1661. update_mmu_cache(vma, address, entry);
  1662. lru_cache_add_active(new_page);
  1663. page_add_new_anon_rmap(new_page, vma, address);
  1664. if (old_page) {
  1665. /*
  1666. * Only after switching the pte to the new page may
  1667. * we remove the mapcount here. Otherwise another
  1668. * process may come and find the rmap count decremented
  1669. * before the pte is switched to the new page, and
  1670. * "reuse" the old page writing into it while our pte
  1671. * here still points into it and can be read by other
  1672. * threads.
  1673. *
  1674. * The critical issue is to order this
  1675. * page_remove_rmap with the ptp_clear_flush above.
  1676. * Those stores are ordered by (if nothing else,)
  1677. * the barrier present in the atomic_add_negative
  1678. * in page_remove_rmap.
  1679. *
  1680. * Then the TLB flush in ptep_clear_flush ensures that
  1681. * no process can access the old page before the
  1682. * decremented mapcount is visible. And the old page
  1683. * cannot be reused until after the decremented
  1684. * mapcount is visible. So transitively, TLBs to
  1685. * old page will be flushed before it can be reused.
  1686. */
  1687. page_remove_rmap(old_page, vma);
  1688. }
  1689. /* Free the old page.. */
  1690. new_page = old_page;
  1691. ret |= VM_FAULT_WRITE;
  1692. } else
  1693. mem_cgroup_uncharge_page(new_page);
  1694. if (new_page)
  1695. page_cache_release(new_page);
  1696. if (old_page)
  1697. page_cache_release(old_page);
  1698. unlock:
  1699. pte_unmap_unlock(page_table, ptl);
  1700. if (dirty_page) {
  1701. if (vma->vm_file)
  1702. file_update_time(vma->vm_file);
  1703. /*
  1704. * Yes, Virginia, this is actually required to prevent a race
  1705. * with clear_page_dirty_for_io() from clearing the page dirty
  1706. * bit after it clear all dirty ptes, but before a racing
  1707. * do_wp_page installs a dirty pte.
  1708. *
  1709. * do_no_page is protected similarly.
  1710. */
  1711. wait_on_page_locked(dirty_page);
  1712. set_page_dirty_balance(dirty_page, page_mkwrite);
  1713. put_page(dirty_page);
  1714. }
  1715. return ret;
  1716. oom_free_new:
  1717. page_cache_release(new_page);
  1718. oom:
  1719. if (old_page)
  1720. page_cache_release(old_page);
  1721. return VM_FAULT_OOM;
  1722. unwritable_page:
  1723. page_cache_release(old_page);
  1724. return VM_FAULT_SIGBUS;
  1725. }
  1726. /*
  1727. * Helper functions for unmap_mapping_range().
  1728. *
  1729. * __ Notes on dropping i_mmap_lock to reduce latency while unmapping __
  1730. *
  1731. * We have to restart searching the prio_tree whenever we drop the lock,
  1732. * since the iterator is only valid while the lock is held, and anyway
  1733. * a later vma might be split and reinserted earlier while lock dropped.
  1734. *
  1735. * The list of nonlinear vmas could be handled more efficiently, using
  1736. * a placeholder, but handle it in the same way until a need is shown.
  1737. * It is important to search the prio_tree before nonlinear list: a vma
  1738. * may become nonlinear and be shifted from prio_tree to nonlinear list
  1739. * while the lock is dropped; but never shifted from list to prio_tree.
  1740. *
  1741. * In order to make forward progress despite restarting the search,
  1742. * vm_truncate_count is used to mark a vma as now dealt with, so we can
  1743. * quickly skip it next time around. Since the prio_tree search only
  1744. * shows us those vmas affected by unmapping the range in question, we
  1745. * can't efficiently keep all vmas in step with mapping->truncate_count:
  1746. * so instead reset them all whenever it wraps back to 0 (then go to 1).
  1747. * mapping->truncate_count and vma->vm_truncate_count are protected by
  1748. * i_mmap_lock.
  1749. *
  1750. * In order to make forward progress despite repeatedly restarting some
  1751. * large vma, note the restart_addr from unmap_vmas when it breaks out:
  1752. * and restart from that address when we reach that vma again. It might
  1753. * have been split or merged, shrunk or extended, but never shifted: so
  1754. * restart_addr remains valid so long as it remains in the vma's range.
  1755. * unmap_mapping_range forces truncate_count to leap over page-aligned
  1756. * values so we can save vma's restart_addr in its truncate_count field.
  1757. */
  1758. #define is_restart_addr(truncate_count) (!((truncate_count) & ~PAGE_MASK))
  1759. static void reset_vma_truncate_counts(struct address_space *mapping)
  1760. {
  1761. struct vm_area_struct *vma;
  1762. struct prio_tree_iter iter;
  1763. vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, 0, ULONG_MAX)
  1764. vma->vm_truncate_count = 0;
  1765. list_for_each_entry(vma, &mapping->i_mmap_nonlinear, shared.vm_set.list)
  1766. vma->vm_truncate_count = 0;
  1767. }
  1768. static int unmap_mapping_range_vma(struct vm_area_struct *vma,
  1769. unsigned long start_addr, unsigned long end_addr,
  1770. struct zap_details *details)
  1771. {
  1772. unsigned long restart_addr;
  1773. int need_break;
  1774. /*
  1775. * files that support invalidating or truncating portions of the
  1776. * file from under mmaped areas must have their ->fault function
  1777. * return a locked page (and set VM_FAULT_LOCKED in the return).
  1778. * This provides synchronisation against concurrent unmapping here.
  1779. */
  1780. again:
  1781. restart_addr = vma->vm_truncate_count;
  1782. if (is_restart_addr(restart_addr) && start_addr < restart_addr) {
  1783. start_addr = restart_addr;
  1784. if (start_addr >= end_addr) {
  1785. /* Top of vma has been split off since last time */
  1786. vma->vm_truncate_count = details->truncate_count;
  1787. return 0;
  1788. }
  1789. }
  1790. restart_addr = zap_page_range(vma, start_addr,
  1791. end_addr - start_addr, details);
  1792. need_break = need_resched() || spin_needbreak(details->i_mmap_lock);
  1793. if (restart_addr >= end_addr) {
  1794. /* We have now completed this vma: mark it so */
  1795. vma->vm_truncate_count = details->truncate_count;
  1796. if (!need_break)
  1797. return 0;
  1798. } else {
  1799. /* Note restart_addr in vma's truncate_count field */
  1800. vma->vm_truncate_count = restart_addr;
  1801. if (!need_break)
  1802. goto again;
  1803. }
  1804. spin_unlock(details->i_mmap_lock);
  1805. cond_resched();
  1806. spin_lock(details->i_mmap_lock);
  1807. return -EINTR;
  1808. }
  1809. static inline void unmap_mapping_range_tree(struct prio_tree_root *root,
  1810. struct zap_details *details)
  1811. {
  1812. struct vm_area_struct *vma;
  1813. struct prio_tree_iter iter;
  1814. pgoff_t vba, vea, zba, zea;
  1815. restart:
  1816. vma_prio_tree_foreach(vma, &iter, root,
  1817. details->first_index, details->last_index) {
  1818. /* Skip quickly over those we have already dealt with */
  1819. if (vma->vm_truncate_count == details->truncate_count)
  1820. continue;
  1821. vba = vma->vm_pgoff;
  1822. vea = vba + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) - 1;
  1823. /* Assume for now that PAGE_CACHE_SHIFT == PAGE_SHIFT */
  1824. zba = details->first_index;
  1825. if (zba < vba)
  1826. zba = vba;
  1827. zea = details->last_index;
  1828. if (zea > vea)
  1829. zea = vea;
  1830. if (unmap_mapping_range_vma(vma,
  1831. ((zba - vba) << PAGE_SHIFT) + vma->vm_start,
  1832. ((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start,
  1833. details) < 0)
  1834. goto restart;
  1835. }
  1836. }
  1837. static inline void unmap_mapping_range_list(struct list_head *head,
  1838. struct zap_details *details)
  1839. {
  1840. struct vm_area_struct *vma;
  1841. /*
  1842. * In nonlinear VMAs there is no correspondence between virtual address
  1843. * offset and file offset. So we must perform an exhaustive search
  1844. * across *all* the pages in each nonlinear VMA, not just the pages
  1845. * whose virtual address lies outside the file truncation point.
  1846. */
  1847. restart:
  1848. list_for_each_entry(vma, head, shared.vm_set.list) {
  1849. /* Skip quickly over those we have already dealt with */
  1850. if (vma->vm_truncate_count == details->truncate_count)
  1851. continue;
  1852. details->nonlinear_vma = vma;
  1853. if (unmap_mapping_range_vma(vma, vma->vm_start,
  1854. vma->vm_end, details) < 0)
  1855. goto restart;
  1856. }
  1857. }
  1858. /**
  1859. * unmap_mapping_range - unmap the portion of all mmaps in the specified address_space corresponding to the specified page range in the underlying file.
  1860. * @mapping: the address space containing mmaps to be unmapped.
  1861. * @holebegin: byte in first page to unmap, relative to the start of
  1862. * the underlying file. This will be rounded down to a PAGE_SIZE
  1863. * boundary. Note that this is different from vmtruncate(), which
  1864. * must keep the partial page. In contrast, we must get rid of
  1865. * partial pages.
  1866. * @holelen: size of prospective hole in bytes. This will be rounded
  1867. * up to a PAGE_SIZE boundary. A holelen of zero truncates to the
  1868. * end of the file.
  1869. * @even_cows: 1 when truncating a file, unmap even private COWed pages;
  1870. * but 0 when invalidating pagecache, don't throw away private data.
  1871. */
  1872. void unmap_mapping_range(struct address_space *mapping,
  1873. loff_t const holebegin, loff_t const holelen, int even_cows)
  1874. {
  1875. struct zap_details details;
  1876. pgoff_t hba = holebegin >> PAGE_SHIFT;
  1877. pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1878. /* Check for overflow. */
  1879. if (sizeof(holelen) > sizeof(hlen)) {
  1880. long long holeend =
  1881. (holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
  1882. if (holeend & ~(long long)ULONG_MAX)
  1883. hlen = ULONG_MAX - hba + 1;
  1884. }
  1885. details.check_mapping = even_cows? NULL: mapping;
  1886. details.nonlinear_vma = NULL;
  1887. details.first_index = hba;
  1888. details.last_index = hba + hlen - 1;
  1889. if (details.last_index < details.first_index)
  1890. details.last_index = ULONG_MAX;
  1891. details.i_mmap_lock = &mapping->i_mmap_lock;
  1892. spin_lock(&mapping->i_mmap_lock);
  1893. /* Protect against endless unmapping loops */
  1894. mapping->truncate_count++;
  1895. if (unlikely(is_restart_addr(mapping->truncate_count))) {
  1896. if (mapping->truncate_count == 0)
  1897. reset_vma_truncate_counts(mapping);
  1898. mapping->truncate_count++;
  1899. }
  1900. details.truncate_count = mapping->truncate_count;
  1901. if (unlikely(!prio_tree_empty(&mapping->i_mmap)))
  1902. unmap_mapping_range_tree(&mapping->i_mmap, &details);
  1903. if (unlikely(!list_empty(&mapping->i_mmap_nonlinear)))
  1904. unmap_mapping_range_list(&mapping->i_mmap_nonlinear, &details);
  1905. spin_unlock(&mapping->i_mmap_lock);
  1906. }
  1907. EXPORT_SYMBOL(unmap_mapping_range);
  1908. /**
  1909. * vmtruncate - unmap mappings "freed" by truncate() syscall
  1910. * @inode: inode of the file used
  1911. * @offset: file offset to start truncating
  1912. *
  1913. * NOTE! We have to be ready to update the memory sharing
  1914. * between the file and the memory map for a potential last
  1915. * incomplete page. Ugly, but necessary.
  1916. */
  1917. int vmtruncate(struct inode * inode, loff_t offset)
  1918. {
  1919. if (inode->i_size < offset) {
  1920. unsigned long limit;
  1921. limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
  1922. if (limit != RLIM_INFINITY && offset > limit)
  1923. goto out_sig;
  1924. if (offset > inode->i_sb->s_maxbytes)
  1925. goto out_big;
  1926. i_size_write(inode, offset);
  1927. } else {
  1928. struct address_space *mapping = inode->i_mapping;
  1929. /*
  1930. * truncation of in-use swapfiles is disallowed - it would
  1931. * cause subsequent swapout to scribble on the now-freed
  1932. * blocks.
  1933. */
  1934. if (IS_SWAPFILE(inode))
  1935. return -ETXTBSY;
  1936. i_size_write(inode, offset);
  1937. /*
  1938. * unmap_mapping_range is called twice, first simply for
  1939. * efficiency so that truncate_inode_pages does fewer
  1940. * single-page unmaps. However after this first call, and
  1941. * before truncate_inode_pages finishes, it is possible for
  1942. * private pages to be COWed, which remain after
  1943. * truncate_inode_pages finishes, hence the second
  1944. * unmap_mapping_range call must be made for correctness.
  1945. */
  1946. unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
  1947. truncate_inode_pages(mapping, offset);
  1948. unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
  1949. }
  1950. if (inode->i_op && inode->i_op->truncate)
  1951. inode->i_op->truncate(inode);
  1952. return 0;
  1953. out_sig:
  1954. send_sig(SIGXFSZ, current, 0);
  1955. out_big:
  1956. return -EFBIG;
  1957. }
  1958. EXPORT_SYMBOL(vmtruncate);
  1959. int vmtruncate_range(struct inode *inode, loff_t offset, loff_t end)
  1960. {
  1961. struct address_space *mapping = inode->i_mapping;
  1962. /*
  1963. * If the underlying filesystem is not going to provide
  1964. * a way to truncate a range of blocks (punch a hole) -
  1965. * we should return failure right now.
  1966. */
  1967. if (!inode->i_op || !inode->i_op->truncate_range)
  1968. return -ENOSYS;
  1969. mutex_lock(&inode->i_mutex);
  1970. down_write(&inode->i_alloc_sem);
  1971. unmap_mapping_range(mapping, offset, (end - offset), 1);
  1972. truncate_inode_pages_range(mapping, offset, end);
  1973. unmap_mapping_range(mapping, offset, (end - offset), 1);
  1974. inode->i_op->truncate_range(inode, offset, end);
  1975. up_write(&inode->i_alloc_sem);
  1976. mutex_unlock(&inode->i_mutex);
  1977. return 0;
  1978. }
  1979. /*
  1980. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  1981. * but allow concurrent faults), and pte mapped but not yet locked.
  1982. * We return with mmap_sem still held, but pte unmapped and unlocked.
  1983. */
  1984. static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma,
  1985. unsigned long address, pte_t *page_table, pmd_t *pmd,
  1986. int write_access, pte_t orig_pte)
  1987. {
  1988. spinlock_t *ptl;
  1989. struct page *page;
  1990. swp_entry_t entry;
  1991. pte_t pte;
  1992. int ret = 0;
  1993. if (!pte_unmap_same(mm, pmd, page_table, orig_pte))
  1994. goto out;
  1995. entry = pte_to_swp_entry(orig_pte);
  1996. if (is_migration_entry(entry)) {
  1997. migration_entry_wait(mm, pmd, address);
  1998. goto out;
  1999. }
  2000. delayacct_set_flag(DELAYACCT_PF_SWAPIN);
  2001. page = lookup_swap_cache(entry);
  2002. if (!page) {
  2003. grab_swap_token(); /* Contend for token _before_ read-in */
  2004. page = swapin_readahead(entry,
  2005. GFP_HIGHUSER_MOVABLE, vma, address);
  2006. if (!page) {
  2007. /*
  2008. * Back out if somebody else faulted in this pte
  2009. * while we released the pte lock.
  2010. */
  2011. page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
  2012. if (likely(pte_same(*page_table, orig_pte)))
  2013. ret = VM_FAULT_OOM;
  2014. delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
  2015. goto unlock;
  2016. }
  2017. /* Had to read the page from swap area: Major fault */
  2018. ret = VM_FAULT_MAJOR;
  2019. count_vm_event(PGMAJFAULT);
  2020. }
  2021. if (mem_cgroup_charge(page, mm, GFP_KERNEL)) {
  2022. delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
  2023. ret = VM_FAULT_OOM;
  2024. goto out;
  2025. }
  2026. mark_page_accessed(page);
  2027. lock_page(page);
  2028. delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
  2029. /*
  2030. * Back out if somebody else already faulted in this pte.
  2031. */
  2032. page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
  2033. if (unlikely(!pte_same(*page_table, orig_pte)))
  2034. goto out_nomap;
  2035. if (unlikely(!PageUptodate(page))) {
  2036. ret = VM_FAULT_SIGBUS;
  2037. goto out_nomap;
  2038. }
  2039. /* The page isn't present yet, go ahead with the fault. */
  2040. inc_mm_counter(mm, anon_rss);
  2041. pte = mk_pte(page, vma->vm_page_prot);
  2042. if (write_access && can_share_swap_page(page)) {
  2043. pte = maybe_mkwrite(pte_mkdirty(pte), vma);
  2044. write_access = 0;
  2045. }
  2046. flush_icache_page(vma, page);
  2047. set_pte_at(mm, address, page_table, pte);
  2048. page_add_anon_rmap(page, vma, address);
  2049. swap_free(entry);
  2050. if (vm_swap_full())
  2051. remove_exclusive_swap_page(page);
  2052. unlock_page(page);
  2053. if (write_access) {
  2054. ret |= do_wp_page(mm, vma, address, page_table, pmd, ptl, pte);
  2055. if (ret & VM_FAULT_ERROR)
  2056. ret &= VM_FAULT_ERROR;
  2057. goto out;
  2058. }
  2059. /* No need to invalidate - it was non-present before */
  2060. update_mmu_cache(vma, address, pte);
  2061. unlock:
  2062. pte_unmap_unlock(page_table, ptl);
  2063. out:
  2064. return ret;
  2065. out_nomap:
  2066. mem_cgroup_uncharge_page(page);
  2067. pte_unmap_unlock(page_table, ptl);
  2068. unlock_page(page);
  2069. page_cache_release(page);
  2070. return ret;
  2071. }
  2072. /*
  2073. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  2074. * but allow concurrent faults), and pte mapped but not yet locked.
  2075. * We return with mmap_sem still held, but pte unmapped and unlocked.
  2076. */
  2077. static int do_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
  2078. unsigned long address, pte_t *page_table, pmd_t *pmd,
  2079. int write_access)
  2080. {
  2081. struct page *page;
  2082. spinlock_t *ptl;
  2083. pte_t entry;
  2084. /* Allocate our own private page. */
  2085. pte_unmap(page_table);
  2086. if (unlikely(anon_vma_prepare(vma)))
  2087. goto oom;
  2088. page = alloc_zeroed_user_highpage_movable(vma, address);
  2089. if (!page)
  2090. goto oom;
  2091. __SetPageUptodate(page);
  2092. if (mem_cgroup_charge(page, mm, GFP_KERNEL))
  2093. goto oom_free_page;
  2094. entry = mk_pte(page, vma->vm_page_prot);
  2095. entry = maybe_mkwrite(pte_mkdirty(entry), vma);
  2096. page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
  2097. if (!pte_none(*page_table))
  2098. goto release;
  2099. inc_mm_counter(mm, anon_rss);
  2100. lru_cache_add_active(page);
  2101. page_add_new_anon_rmap(page, vma, address);
  2102. set_pte_at(mm, address, page_table, entry);
  2103. /* No need to invalidate - it was non-present before */
  2104. update_mmu_cache(vma, address, entry);
  2105. unlock:
  2106. pte_unmap_unlock(page_table, ptl);
  2107. return 0;
  2108. release:
  2109. mem_cgroup_uncharge_page(page);
  2110. page_cache_release(page);
  2111. goto unlock;
  2112. oom_free_page:
  2113. page_cache_release(page);
  2114. oom:
  2115. return VM_FAULT_OOM;
  2116. }
  2117. /*
  2118. * __do_fault() tries to create a new page mapping. It aggressively
  2119. * tries to share with existing pages, but makes a separate copy if
  2120. * the FAULT_FLAG_WRITE is set in the flags parameter in order to avoid
  2121. * the next page fault.
  2122. *
  2123. * As this is called only for pages that do not currently exist, we
  2124. * do not need to flush old virtual caches or the TLB.
  2125. *
  2126. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  2127. * but allow concurrent faults), and pte neither mapped nor locked.
  2128. * We return with mmap_sem still held, but pte unmapped and unlocked.
  2129. */
  2130. static int __do_fault(struct mm_struct *mm, struct vm_area_struct *vma,
  2131. unsigned long address, pmd_t *pmd,
  2132. pgoff_t pgoff, unsigned int flags, pte_t orig_pte)
  2133. {
  2134. pte_t *page_table;
  2135. spinlock_t *ptl;
  2136. struct page *page;
  2137. pte_t entry;
  2138. int anon = 0;
  2139. struct page *dirty_page = NULL;
  2140. struct vm_fault vmf;
  2141. int ret;
  2142. int page_mkwrite = 0;
  2143. vmf.virtual_address = (void __user *)(address & PAGE_MASK);
  2144. vmf.pgoff = pgoff;
  2145. vmf.flags = flags;
  2146. vmf.page = NULL;
  2147. ret = vma->vm_ops->fault(vma, &vmf);
  2148. if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE)))
  2149. return ret;
  2150. /*
  2151. * For consistency in subsequent calls, make the faulted page always
  2152. * locked.
  2153. */
  2154. if (unlikely(!(ret & VM_FAULT_LOCKED)))
  2155. lock_page(vmf.page);
  2156. else
  2157. VM_BUG_ON(!PageLocked(vmf.page));
  2158. /*
  2159. * Should we do an early C-O-W break?
  2160. */
  2161. page = vmf.page;
  2162. if (flags & FAULT_FLAG_WRITE) {
  2163. if (!(vma->vm_flags & VM_SHARED)) {
  2164. anon = 1;
  2165. if (unlikely(anon_vma_prepare(vma))) {
  2166. ret = VM_FAULT_OOM;
  2167. goto out;
  2168. }
  2169. page = alloc_page_vma(GFP_HIGHUSER_MOVABLE,
  2170. vma, address);
  2171. if (!page) {
  2172. ret = VM_FAULT_OOM;
  2173. goto out;
  2174. }
  2175. copy_user_highpage(page, vmf.page, address, vma);
  2176. __SetPageUptodate(page);
  2177. } else {
  2178. /*
  2179. * If the page will be shareable, see if the backing
  2180. * address space wants to know that the page is about
  2181. * to become writable
  2182. */
  2183. if (vma->vm_ops->page_mkwrite) {
  2184. unlock_page(page);
  2185. if (vma->vm_ops->page_mkwrite(vma, page) < 0) {
  2186. ret = VM_FAULT_SIGBUS;
  2187. anon = 1; /* no anon but release vmf.page */
  2188. goto out_unlocked;
  2189. }
  2190. lock_page(page);
  2191. /*
  2192. * XXX: this is not quite right (racy vs
  2193. * invalidate) to unlock and relock the page
  2194. * like this, however a better fix requires
  2195. * reworking page_mkwrite locking API, which
  2196. * is better done later.
  2197. */
  2198. if (!page->mapping) {
  2199. ret = 0;
  2200. anon = 1; /* no anon but release vmf.page */
  2201. goto out;
  2202. }
  2203. page_mkwrite = 1;
  2204. }
  2205. }
  2206. }
  2207. if (mem_cgroup_charge(page, mm, GFP_KERNEL)) {
  2208. ret = VM_FAULT_OOM;
  2209. goto out;
  2210. }
  2211. page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
  2212. /*
  2213. * This silly early PAGE_DIRTY setting removes a race
  2214. * due to the bad i386 page protection. But it's valid
  2215. * for other architectures too.
  2216. *
  2217. * Note that if write_access is true, we either now have
  2218. * an exclusive copy of the page, or this is a shared mapping,
  2219. * so we can make it writable and dirty to avoid having to
  2220. * handle that later.
  2221. */
  2222. /* Only go through if we didn't race with anybody else... */
  2223. if (likely(pte_same(*page_table, orig_pte))) {
  2224. flush_icache_page(vma, page);
  2225. entry = mk_pte(page, vma->vm_page_prot);
  2226. if (flags & FAULT_FLAG_WRITE)
  2227. entry = maybe_mkwrite(pte_mkdirty(entry), vma);
  2228. set_pte_at(mm, address, page_table, entry);
  2229. if (anon) {
  2230. inc_mm_counter(mm, anon_rss);
  2231. lru_cache_add_active(page);
  2232. page_add_new_anon_rmap(page, vma, address);
  2233. } else {
  2234. inc_mm_counter(mm, file_rss);
  2235. page_add_file_rmap(page);
  2236. if (flags & FAULT_FLAG_WRITE) {
  2237. dirty_page = page;
  2238. get_page(dirty_page);
  2239. }
  2240. }
  2241. /* no need to invalidate: a not-present page won't be cached */
  2242. update_mmu_cache(vma, address, entry);
  2243. } else {
  2244. mem_cgroup_uncharge_page(page);
  2245. if (anon)
  2246. page_cache_release(page);
  2247. else
  2248. anon = 1; /* no anon but release faulted_page */
  2249. }
  2250. pte_unmap_unlock(page_table, ptl);
  2251. out:
  2252. unlock_page(vmf.page);
  2253. out_unlocked:
  2254. if (anon)
  2255. page_cache_release(vmf.page);
  2256. else if (dirty_page) {
  2257. if (vma->vm_file)
  2258. file_update_time(vma->vm_file);
  2259. set_page_dirty_balance(dirty_page, page_mkwrite);
  2260. put_page(dirty_page);
  2261. }
  2262. return ret;
  2263. }
  2264. static int do_linear_fault(struct mm_struct *mm, struct vm_area_struct *vma,
  2265. unsigned long address, pte_t *page_table, pmd_t *pmd,
  2266. int write_access, pte_t orig_pte)
  2267. {
  2268. pgoff_t pgoff = (((address & PAGE_MASK)
  2269. - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
  2270. unsigned int flags = (write_access ? FAULT_FLAG_WRITE : 0);
  2271. pte_unmap(page_table);
  2272. return __do_fault(mm, vma, address, pmd, pgoff, flags, orig_pte);
  2273. }
  2274. /*
  2275. * Fault of a previously existing named mapping. Repopulate the pte
  2276. * from the encoded file_pte if possible. This enables swappable
  2277. * nonlinear vmas.
  2278. *
  2279. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  2280. * but allow concurrent faults), and pte mapped but not yet locked.
  2281. * We return with mmap_sem still held, but pte unmapped and unlocked.
  2282. */
  2283. static int do_nonlinear_fault(struct mm_struct *mm, struct vm_area_struct *vma,
  2284. unsigned long address, pte_t *page_table, pmd_t *pmd,
  2285. int write_access, pte_t orig_pte)
  2286. {
  2287. unsigned int flags = FAULT_FLAG_NONLINEAR |
  2288. (write_access ? FAULT_FLAG_WRITE : 0);
  2289. pgoff_t pgoff;
  2290. if (!pte_unmap_same(mm, pmd, page_table, orig_pte))
  2291. return 0;
  2292. if (unlikely(!(vma->vm_flags & VM_NONLINEAR) ||
  2293. !(vma->vm_flags & VM_CAN_NONLINEAR))) {
  2294. /*
  2295. * Page table corrupted: show pte and kill process.
  2296. */
  2297. print_bad_pte(vma, orig_pte, address);
  2298. return VM_FAULT_OOM;
  2299. }
  2300. pgoff = pte_to_pgoff(orig_pte);
  2301. return __do_fault(mm, vma, address, pmd, pgoff, flags, orig_pte);
  2302. }
  2303. /*
  2304. * These routines also need to handle stuff like marking pages dirty
  2305. * and/or accessed for architectures that don't do it in hardware (most
  2306. * RISC architectures). The early dirtying is also good on the i386.
  2307. *
  2308. * There is also a hook called "update_mmu_cache()" that architectures
  2309. * with external mmu caches can use to update those (ie the Sparc or
  2310. * PowerPC hashed page tables that act as extended TLBs).
  2311. *
  2312. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  2313. * but allow concurrent faults), and pte mapped but not yet locked.
  2314. * We return with mmap_sem still held, but pte unmapped and unlocked.
  2315. */
  2316. static inline int handle_pte_fault(struct mm_struct *mm,
  2317. struct vm_area_struct *vma, unsigned long address,
  2318. pte_t *pte, pmd_t *pmd, int write_access)
  2319. {
  2320. pte_t entry;
  2321. spinlock_t *ptl;
  2322. entry = *pte;
  2323. if (!pte_present(entry)) {
  2324. if (pte_none(entry)) {
  2325. if (vma->vm_ops) {
  2326. if (likely(vma->vm_ops->fault))
  2327. return do_linear_fault(mm, vma, address,
  2328. pte, pmd, write_access, entry);
  2329. }
  2330. return do_anonymous_page(mm, vma, address,
  2331. pte, pmd, write_access);
  2332. }
  2333. if (pte_file(entry))
  2334. return do_nonlinear_fault(mm, vma, address,
  2335. pte, pmd, write_access, entry);
  2336. return do_swap_page(mm, vma, address,
  2337. pte, pmd, write_access, entry);
  2338. }
  2339. ptl = pte_lockptr(mm, pmd);
  2340. spin_lock(ptl);
  2341. if (unlikely(!pte_same(*pte, entry)))
  2342. goto unlock;
  2343. if (write_access) {
  2344. if (!pte_write(entry))
  2345. return do_wp_page(mm, vma, address,
  2346. pte, pmd, ptl, entry);
  2347. entry = pte_mkdirty(entry);
  2348. }
  2349. entry = pte_mkyoung(entry);
  2350. if (ptep_set_access_flags(vma, address, pte, entry, write_access)) {
  2351. update_mmu_cache(vma, address, entry);
  2352. } else {
  2353. /*
  2354. * This is needed only for protection faults but the arch code
  2355. * is not yet telling us if this is a protection fault or not.
  2356. * This still avoids useless tlb flushes for .text page faults
  2357. * with threads.
  2358. */
  2359. if (write_access)
  2360. flush_tlb_page(vma, address);
  2361. }
  2362. unlock:
  2363. pte_unmap_unlock(pte, ptl);
  2364. return 0;
  2365. }
  2366. /*
  2367. * By the time we get here, we already hold the mm semaphore
  2368. */
  2369. int handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma,
  2370. unsigned long address, int write_access)
  2371. {
  2372. pgd_t *pgd;
  2373. pud_t *pud;
  2374. pmd_t *pmd;
  2375. pte_t *pte;
  2376. __set_current_state(TASK_RUNNING);
  2377. count_vm_event(PGFAULT);
  2378. if (unlikely(is_vm_hugetlb_page(vma)))
  2379. return hugetlb_fault(mm, vma, address, write_access);
  2380. pgd = pgd_offset(mm, address);
  2381. pud = pud_alloc(mm, pgd, address);
  2382. if (!pud)
  2383. return VM_FAULT_OOM;
  2384. pmd = pmd_alloc(mm, pud, address);
  2385. if (!pmd)
  2386. return VM_FAULT_OOM;
  2387. pte = pte_alloc_map(mm, pmd, address);
  2388. if (!pte)
  2389. return VM_FAULT_OOM;
  2390. return handle_pte_fault(mm, vma, address, pte, pmd, write_access);
  2391. }
  2392. #ifndef __PAGETABLE_PUD_FOLDED
  2393. /*
  2394. * Allocate page upper directory.
  2395. * We've already handled the fast-path in-line.
  2396. */
  2397. int __pud_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
  2398. {
  2399. pud_t *new = pud_alloc_one(mm, address);
  2400. if (!new)
  2401. return -ENOMEM;
  2402. smp_wmb(); /* See comment in __pte_alloc */
  2403. spin_lock(&mm->page_table_lock);
  2404. if (pgd_present(*pgd)) /* Another has populated it */
  2405. pud_free(mm, new);
  2406. else
  2407. pgd_populate(mm, pgd, new);
  2408. spin_unlock(&mm->page_table_lock);
  2409. return 0;
  2410. }
  2411. #endif /* __PAGETABLE_PUD_FOLDED */
  2412. #ifndef __PAGETABLE_PMD_FOLDED
  2413. /*
  2414. * Allocate page middle directory.
  2415. * We've already handled the fast-path in-line.
  2416. */
  2417. int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address)
  2418. {
  2419. pmd_t *new = pmd_alloc_one(mm, address);
  2420. if (!new)
  2421. return -ENOMEM;
  2422. smp_wmb(); /* See comment in __pte_alloc */
  2423. spin_lock(&mm->page_table_lock);
  2424. #ifndef __ARCH_HAS_4LEVEL_HACK
  2425. if (pud_present(*pud)) /* Another has populated it */
  2426. pmd_free(mm, new);
  2427. else
  2428. pud_populate(mm, pud, new);
  2429. #else
  2430. if (pgd_present(*pud)) /* Another has populated it */
  2431. pmd_free(mm, new);
  2432. else
  2433. pgd_populate(mm, pud, new);
  2434. #endif /* __ARCH_HAS_4LEVEL_HACK */
  2435. spin_unlock(&mm->page_table_lock);
  2436. return 0;
  2437. }
  2438. #endif /* __PAGETABLE_PMD_FOLDED */
  2439. int make_pages_present(unsigned long addr, unsigned long end)
  2440. {
  2441. int ret, len, write;
  2442. struct vm_area_struct * vma;
  2443. vma = find_vma(current->mm, addr);
  2444. if (!vma)
  2445. return -1;
  2446. write = (vma->vm_flags & VM_WRITE) != 0;
  2447. BUG_ON(addr >= end);
  2448. BUG_ON(end > vma->vm_end);
  2449. len = DIV_ROUND_UP(end, PAGE_SIZE) - addr/PAGE_SIZE;
  2450. ret = get_user_pages(current, current->mm, addr,
  2451. len, write, 0, NULL, NULL);
  2452. if (ret < 0)
  2453. return ret;
  2454. return ret == len ? 0 : -1;
  2455. }
  2456. #if !defined(__HAVE_ARCH_GATE_AREA)
  2457. #if defined(AT_SYSINFO_EHDR)
  2458. static struct vm_area_struct gate_vma;
  2459. static int __init gate_vma_init(void)
  2460. {
  2461. gate_vma.vm_mm = NULL;
  2462. gate_vma.vm_start = FIXADDR_USER_START;
  2463. gate_vma.vm_end = FIXADDR_USER_END;
  2464. gate_vma.vm_flags = VM_READ | VM_MAYREAD | VM_EXEC | VM_MAYEXEC;
  2465. gate_vma.vm_page_prot = __P101;
  2466. /*
  2467. * Make sure the vDSO gets into every core dump.
  2468. * Dumping its contents makes post-mortem fully interpretable later
  2469. * without matching up the same kernel and hardware config to see
  2470. * what PC values meant.
  2471. */
  2472. gate_vma.vm_flags |= VM_ALWAYSDUMP;
  2473. return 0;
  2474. }
  2475. __initcall(gate_vma_init);
  2476. #endif
  2477. struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
  2478. {
  2479. #ifdef AT_SYSINFO_EHDR
  2480. return &gate_vma;
  2481. #else
  2482. return NULL;
  2483. #endif
  2484. }
  2485. int in_gate_area_no_task(unsigned long addr)
  2486. {
  2487. #ifdef AT_SYSINFO_EHDR
  2488. if ((addr >= FIXADDR_USER_START) && (addr < FIXADDR_USER_END))
  2489. return 1;
  2490. #endif
  2491. return 0;
  2492. }
  2493. #endif /* __HAVE_ARCH_GATE_AREA */
  2494. #ifdef CONFIG_HAVE_IOREMAP_PROT
  2495. static resource_size_t follow_phys(struct vm_area_struct *vma,
  2496. unsigned long address, unsigned int flags,
  2497. unsigned long *prot)
  2498. {
  2499. pgd_t *pgd;
  2500. pud_t *pud;
  2501. pmd_t *pmd;
  2502. pte_t *ptep, pte;
  2503. spinlock_t *ptl;
  2504. resource_size_t phys_addr = 0;
  2505. struct mm_struct *mm = vma->vm_mm;
  2506. VM_BUG_ON(!(vma->vm_flags & (VM_IO | VM_PFNMAP)));
  2507. pgd = pgd_offset(mm, address);
  2508. if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
  2509. goto no_page_table;
  2510. pud = pud_offset(pgd, address);
  2511. if (pud_none(*pud) || unlikely(pud_bad(*pud)))
  2512. goto no_page_table;
  2513. pmd = pmd_offset(pud, address);
  2514. if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
  2515. goto no_page_table;
  2516. /* We cannot handle huge page PFN maps. Luckily they don't exist. */
  2517. if (pmd_huge(*pmd))
  2518. goto no_page_table;
  2519. ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
  2520. if (!ptep)
  2521. goto out;
  2522. pte = *ptep;
  2523. if (!pte_present(pte))
  2524. goto unlock;
  2525. if ((flags & FOLL_WRITE) && !pte_write(pte))
  2526. goto unlock;
  2527. phys_addr = pte_pfn(pte);
  2528. phys_addr <<= PAGE_SHIFT; /* Shift here to avoid overflow on PAE */
  2529. *prot = pgprot_val(pte_pgprot(pte));
  2530. unlock:
  2531. pte_unmap_unlock(ptep, ptl);
  2532. out:
  2533. return phys_addr;
  2534. no_page_table:
  2535. return 0;
  2536. }
  2537. int generic_access_phys(struct vm_area_struct *vma, unsigned long addr,
  2538. void *buf, int len, int write)
  2539. {
  2540. resource_size_t phys_addr;
  2541. unsigned long prot = 0;
  2542. void *maddr;
  2543. int offset = addr & (PAGE_SIZE-1);
  2544. if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
  2545. return -EINVAL;
  2546. phys_addr = follow_phys(vma, addr, write, &prot);
  2547. if (!phys_addr)
  2548. return -EINVAL;
  2549. maddr = ioremap_prot(phys_addr, PAGE_SIZE, prot);
  2550. if (write)
  2551. memcpy_toio(maddr + offset, buf, len);
  2552. else
  2553. memcpy_fromio(buf, maddr + offset, len);
  2554. iounmap(maddr);
  2555. return len;
  2556. }
  2557. #endif
  2558. /*
  2559. * Access another process' address space.
  2560. * Source/target buffer must be kernel space,
  2561. * Do not walk the page table directly, use get_user_pages
  2562. */
  2563. int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
  2564. {
  2565. struct mm_struct *mm;
  2566. struct vm_area_struct *vma;
  2567. void *old_buf = buf;
  2568. mm = get_task_mm(tsk);
  2569. if (!mm)
  2570. return 0;
  2571. down_read(&mm->mmap_sem);
  2572. /* ignore errors, just check how much was successfully transferred */
  2573. while (len) {
  2574. int bytes, ret, offset;
  2575. void *maddr;
  2576. struct page *page = NULL;
  2577. ret = get_user_pages(tsk, mm, addr, 1,
  2578. write, 1, &page, &vma);
  2579. if (ret <= 0) {
  2580. /*
  2581. * Check if this is a VM_IO | VM_PFNMAP VMA, which
  2582. * we can access using slightly different code.
  2583. */
  2584. #ifdef CONFIG_HAVE_IOREMAP_PROT
  2585. vma = find_vma(mm, addr);
  2586. if (!vma)
  2587. break;
  2588. if (vma->vm_ops && vma->vm_ops->access)
  2589. ret = vma->vm_ops->access(vma, addr, buf,
  2590. len, write);
  2591. if (ret <= 0)
  2592. #endif
  2593. break;
  2594. bytes = ret;
  2595. } else {
  2596. bytes = len;
  2597. offset = addr & (PAGE_SIZE-1);
  2598. if (bytes > PAGE_SIZE-offset)
  2599. bytes = PAGE_SIZE-offset;
  2600. maddr = kmap(page);
  2601. if (write) {
  2602. copy_to_user_page(vma, page, addr,
  2603. maddr + offset, buf, bytes);
  2604. set_page_dirty_lock(page);
  2605. } else {
  2606. copy_from_user_page(vma, page, addr,
  2607. buf, maddr + offset, bytes);
  2608. }
  2609. kunmap(page);
  2610. page_cache_release(page);
  2611. }
  2612. len -= bytes;
  2613. buf += bytes;
  2614. addr += bytes;
  2615. }
  2616. up_read(&mm->mmap_sem);
  2617. mmput(mm);
  2618. return buf - old_buf;
  2619. }
  2620. /*
  2621. * Print the name of a VMA.
  2622. */
  2623. void print_vma_addr(char *prefix, unsigned long ip)
  2624. {
  2625. struct mm_struct *mm = current->mm;
  2626. struct vm_area_struct *vma;
  2627. /*
  2628. * Do not print if we are in atomic
  2629. * contexts (in exception stacks, etc.):
  2630. */
  2631. if (preempt_count())
  2632. return;
  2633. down_read(&mm->mmap_sem);
  2634. vma = find_vma(mm, ip);
  2635. if (vma && vma->vm_file) {
  2636. struct file *f = vma->vm_file;
  2637. char *buf = (char *)__get_free_page(GFP_KERNEL);
  2638. if (buf) {
  2639. char *p, *s;
  2640. p = d_path(&f->f_path, buf, PAGE_SIZE);
  2641. if (IS_ERR(p))
  2642. p = "?";
  2643. s = strrchr(p, '/');
  2644. if (s)
  2645. p = s+1;
  2646. printk("%s%s[%lx+%lx]", prefix, p,
  2647. vma->vm_start,
  2648. vma->vm_end - vma->vm_start);
  2649. free_page((unsigned long)buf);
  2650. }
  2651. }
  2652. up_read(&current->mm->mmap_sem);
  2653. }