memory.c 95 KB

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