memory.c 116 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302
  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/export.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 <linux/migrate.h>
  56. #include <linux/string.h>
  57. #include <asm/io.h>
  58. #include <asm/pgalloc.h>
  59. #include <asm/uaccess.h>
  60. #include <asm/tlb.h>
  61. #include <asm/tlbflush.h>
  62. #include <asm/pgtable.h>
  63. #include "internal.h"
  64. #ifdef LAST_NID_NOT_IN_PAGE_FLAGS
  65. #warning Unfortunate NUMA and NUMA Balancing config, growing page-frame for last_nid.
  66. #endif
  67. #ifndef CONFIG_NEED_MULTIPLE_NODES
  68. /* use the per-pgdat data instead for discontigmem - mbligh */
  69. unsigned long max_mapnr;
  70. struct page *mem_map;
  71. EXPORT_SYMBOL(max_mapnr);
  72. EXPORT_SYMBOL(mem_map);
  73. #endif
  74. /*
  75. * A number of key systems in x86 including ioremap() rely on the assumption
  76. * that high_memory defines the upper bound on direct map memory, then end
  77. * of ZONE_NORMAL. Under CONFIG_DISCONTIG this means that max_low_pfn and
  78. * highstart_pfn must be the same; there must be no gap between ZONE_NORMAL
  79. * and ZONE_HIGHMEM.
  80. */
  81. void * high_memory;
  82. EXPORT_SYMBOL(high_memory);
  83. /*
  84. * Randomize the address space (stacks, mmaps, brk, etc.).
  85. *
  86. * ( When CONFIG_COMPAT_BRK=y we exclude brk from randomization,
  87. * as ancient (libc5 based) binaries can segfault. )
  88. */
  89. int randomize_va_space __read_mostly =
  90. #ifdef CONFIG_COMPAT_BRK
  91. 1;
  92. #else
  93. 2;
  94. #endif
  95. static int __init disable_randmaps(char *s)
  96. {
  97. randomize_va_space = 0;
  98. return 1;
  99. }
  100. __setup("norandmaps", disable_randmaps);
  101. unsigned long zero_pfn __read_mostly;
  102. unsigned long highest_memmap_pfn __read_mostly;
  103. /*
  104. * CONFIG_MMU architectures set up ZERO_PAGE in their paging_init()
  105. */
  106. static int __init init_zero_pfn(void)
  107. {
  108. zero_pfn = page_to_pfn(ZERO_PAGE(0));
  109. return 0;
  110. }
  111. core_initcall(init_zero_pfn);
  112. #if defined(SPLIT_RSS_COUNTING)
  113. void sync_mm_rss(struct mm_struct *mm)
  114. {
  115. int i;
  116. for (i = 0; i < NR_MM_COUNTERS; i++) {
  117. if (current->rss_stat.count[i]) {
  118. add_mm_counter(mm, i, current->rss_stat.count[i]);
  119. current->rss_stat.count[i] = 0;
  120. }
  121. }
  122. current->rss_stat.events = 0;
  123. }
  124. static void add_mm_counter_fast(struct mm_struct *mm, int member, int val)
  125. {
  126. struct task_struct *task = current;
  127. if (likely(task->mm == mm))
  128. task->rss_stat.count[member] += val;
  129. else
  130. add_mm_counter(mm, member, val);
  131. }
  132. #define inc_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, 1)
  133. #define dec_mm_counter_fast(mm, member) add_mm_counter_fast(mm, member, -1)
  134. /* sync counter once per 64 page faults */
  135. #define TASK_RSS_EVENTS_THRESH (64)
  136. static void check_sync_rss_stat(struct task_struct *task)
  137. {
  138. if (unlikely(task != current))
  139. return;
  140. if (unlikely(task->rss_stat.events++ > TASK_RSS_EVENTS_THRESH))
  141. sync_mm_rss(task->mm);
  142. }
  143. #else /* SPLIT_RSS_COUNTING */
  144. #define inc_mm_counter_fast(mm, member) inc_mm_counter(mm, member)
  145. #define dec_mm_counter_fast(mm, member) dec_mm_counter(mm, member)
  146. static void check_sync_rss_stat(struct task_struct *task)
  147. {
  148. }
  149. #endif /* SPLIT_RSS_COUNTING */
  150. #ifdef HAVE_GENERIC_MMU_GATHER
  151. static int tlb_next_batch(struct mmu_gather *tlb)
  152. {
  153. struct mmu_gather_batch *batch;
  154. batch = tlb->active;
  155. if (batch->next) {
  156. tlb->active = batch->next;
  157. return 1;
  158. }
  159. if (tlb->batch_count == MAX_GATHER_BATCH_COUNT)
  160. return 0;
  161. batch = (void *)__get_free_pages(GFP_NOWAIT | __GFP_NOWARN, 0);
  162. if (!batch)
  163. return 0;
  164. tlb->batch_count++;
  165. batch->next = NULL;
  166. batch->nr = 0;
  167. batch->max = MAX_GATHER_BATCH;
  168. tlb->active->next = batch;
  169. tlb->active = batch;
  170. return 1;
  171. }
  172. /* tlb_gather_mmu
  173. * Called to initialize an (on-stack) mmu_gather structure for page-table
  174. * tear-down from @mm. The @fullmm argument is used when @mm is without
  175. * users and we're going to destroy the full address space (exit/execve).
  176. */
  177. void tlb_gather_mmu(struct mmu_gather *tlb, struct mm_struct *mm, bool fullmm)
  178. {
  179. tlb->mm = mm;
  180. tlb->fullmm = fullmm;
  181. tlb->need_flush_all = 0;
  182. tlb->start = -1UL;
  183. tlb->end = 0;
  184. tlb->need_flush = 0;
  185. tlb->local.next = NULL;
  186. tlb->local.nr = 0;
  187. tlb->local.max = ARRAY_SIZE(tlb->__pages);
  188. tlb->active = &tlb->local;
  189. tlb->batch_count = 0;
  190. #ifdef CONFIG_HAVE_RCU_TABLE_FREE
  191. tlb->batch = NULL;
  192. #endif
  193. }
  194. void tlb_flush_mmu(struct mmu_gather *tlb)
  195. {
  196. struct mmu_gather_batch *batch;
  197. if (!tlb->need_flush)
  198. return;
  199. tlb->need_flush = 0;
  200. tlb_flush(tlb);
  201. #ifdef CONFIG_HAVE_RCU_TABLE_FREE
  202. tlb_table_flush(tlb);
  203. #endif
  204. for (batch = &tlb->local; batch; batch = batch->next) {
  205. free_pages_and_swap_cache(batch->pages, batch->nr);
  206. batch->nr = 0;
  207. }
  208. tlb->active = &tlb->local;
  209. }
  210. /* tlb_finish_mmu
  211. * Called at the end of the shootdown operation to free up any resources
  212. * that were required.
  213. */
  214. void tlb_finish_mmu(struct mmu_gather *tlb, unsigned long start, unsigned long end)
  215. {
  216. struct mmu_gather_batch *batch, *next;
  217. tlb->start = start;
  218. tlb->end = end;
  219. tlb_flush_mmu(tlb);
  220. /* keep the page table cache within bounds */
  221. check_pgt_cache();
  222. for (batch = tlb->local.next; batch; batch = next) {
  223. next = batch->next;
  224. free_pages((unsigned long)batch, 0);
  225. }
  226. tlb->local.next = NULL;
  227. }
  228. /* __tlb_remove_page
  229. * Must perform the equivalent to __free_pte(pte_get_and_clear(ptep)), while
  230. * handling the additional races in SMP caused by other CPUs caching valid
  231. * mappings in their TLBs. Returns the number of free page slots left.
  232. * When out of page slots we must call tlb_flush_mmu().
  233. */
  234. int __tlb_remove_page(struct mmu_gather *tlb, struct page *page)
  235. {
  236. struct mmu_gather_batch *batch;
  237. VM_BUG_ON(!tlb->need_flush);
  238. batch = tlb->active;
  239. batch->pages[batch->nr++] = page;
  240. if (batch->nr == batch->max) {
  241. if (!tlb_next_batch(tlb))
  242. return 0;
  243. batch = tlb->active;
  244. }
  245. VM_BUG_ON(batch->nr > batch->max);
  246. return batch->max - batch->nr;
  247. }
  248. #endif /* HAVE_GENERIC_MMU_GATHER */
  249. #ifdef CONFIG_HAVE_RCU_TABLE_FREE
  250. /*
  251. * See the comment near struct mmu_table_batch.
  252. */
  253. static void tlb_remove_table_smp_sync(void *arg)
  254. {
  255. /* Simply deliver the interrupt */
  256. }
  257. static void tlb_remove_table_one(void *table)
  258. {
  259. /*
  260. * This isn't an RCU grace period and hence the page-tables cannot be
  261. * assumed to be actually RCU-freed.
  262. *
  263. * It is however sufficient for software page-table walkers that rely on
  264. * IRQ disabling. See the comment near struct mmu_table_batch.
  265. */
  266. smp_call_function(tlb_remove_table_smp_sync, NULL, 1);
  267. __tlb_remove_table(table);
  268. }
  269. static void tlb_remove_table_rcu(struct rcu_head *head)
  270. {
  271. struct mmu_table_batch *batch;
  272. int i;
  273. batch = container_of(head, struct mmu_table_batch, rcu);
  274. for (i = 0; i < batch->nr; i++)
  275. __tlb_remove_table(batch->tables[i]);
  276. free_page((unsigned long)batch);
  277. }
  278. void tlb_table_flush(struct mmu_gather *tlb)
  279. {
  280. struct mmu_table_batch **batch = &tlb->batch;
  281. if (*batch) {
  282. call_rcu_sched(&(*batch)->rcu, tlb_remove_table_rcu);
  283. *batch = NULL;
  284. }
  285. }
  286. void tlb_remove_table(struct mmu_gather *tlb, void *table)
  287. {
  288. struct mmu_table_batch **batch = &tlb->batch;
  289. tlb->need_flush = 1;
  290. /*
  291. * When there's less then two users of this mm there cannot be a
  292. * concurrent page-table walk.
  293. */
  294. if (atomic_read(&tlb->mm->mm_users) < 2) {
  295. __tlb_remove_table(table);
  296. return;
  297. }
  298. if (*batch == NULL) {
  299. *batch = (struct mmu_table_batch *)__get_free_page(GFP_NOWAIT | __GFP_NOWARN);
  300. if (*batch == NULL) {
  301. tlb_remove_table_one(table);
  302. return;
  303. }
  304. (*batch)->nr = 0;
  305. }
  306. (*batch)->tables[(*batch)->nr++] = table;
  307. if ((*batch)->nr == MAX_TABLE_BATCH)
  308. tlb_table_flush(tlb);
  309. }
  310. #endif /* CONFIG_HAVE_RCU_TABLE_FREE */
  311. /*
  312. * If a p?d_bad entry is found while walking page tables, report
  313. * the error, before resetting entry to p?d_none. Usually (but
  314. * very seldom) called out from the p?d_none_or_clear_bad macros.
  315. */
  316. void pgd_clear_bad(pgd_t *pgd)
  317. {
  318. pgd_ERROR(*pgd);
  319. pgd_clear(pgd);
  320. }
  321. void pud_clear_bad(pud_t *pud)
  322. {
  323. pud_ERROR(*pud);
  324. pud_clear(pud);
  325. }
  326. void pmd_clear_bad(pmd_t *pmd)
  327. {
  328. pmd_ERROR(*pmd);
  329. pmd_clear(pmd);
  330. }
  331. /*
  332. * Note: this doesn't free the actual pages themselves. That
  333. * has been handled earlier when unmapping all the memory regions.
  334. */
  335. static void free_pte_range(struct mmu_gather *tlb, pmd_t *pmd,
  336. unsigned long addr)
  337. {
  338. pgtable_t token = pmd_pgtable(*pmd);
  339. pmd_clear(pmd);
  340. pte_free_tlb(tlb, token, addr);
  341. tlb->mm->nr_ptes--;
  342. }
  343. static inline void free_pmd_range(struct mmu_gather *tlb, pud_t *pud,
  344. unsigned long addr, unsigned long end,
  345. unsigned long floor, unsigned long ceiling)
  346. {
  347. pmd_t *pmd;
  348. unsigned long next;
  349. unsigned long start;
  350. start = addr;
  351. pmd = pmd_offset(pud, addr);
  352. do {
  353. next = pmd_addr_end(addr, end);
  354. if (pmd_none_or_clear_bad(pmd))
  355. continue;
  356. free_pte_range(tlb, pmd, addr);
  357. } while (pmd++, addr = next, addr != end);
  358. start &= PUD_MASK;
  359. if (start < floor)
  360. return;
  361. if (ceiling) {
  362. ceiling &= PUD_MASK;
  363. if (!ceiling)
  364. return;
  365. }
  366. if (end - 1 > ceiling - 1)
  367. return;
  368. pmd = pmd_offset(pud, start);
  369. pud_clear(pud);
  370. pmd_free_tlb(tlb, pmd, start);
  371. }
  372. static inline void free_pud_range(struct mmu_gather *tlb, pgd_t *pgd,
  373. unsigned long addr, unsigned long end,
  374. unsigned long floor, unsigned long ceiling)
  375. {
  376. pud_t *pud;
  377. unsigned long next;
  378. unsigned long start;
  379. start = addr;
  380. pud = pud_offset(pgd, addr);
  381. do {
  382. next = pud_addr_end(addr, end);
  383. if (pud_none_or_clear_bad(pud))
  384. continue;
  385. free_pmd_range(tlb, pud, addr, next, floor, ceiling);
  386. } while (pud++, addr = next, addr != end);
  387. start &= PGDIR_MASK;
  388. if (start < floor)
  389. return;
  390. if (ceiling) {
  391. ceiling &= PGDIR_MASK;
  392. if (!ceiling)
  393. return;
  394. }
  395. if (end - 1 > ceiling - 1)
  396. return;
  397. pud = pud_offset(pgd, start);
  398. pgd_clear(pgd);
  399. pud_free_tlb(tlb, pud, start);
  400. }
  401. /*
  402. * This function frees user-level page tables of a process.
  403. *
  404. * Must be called with pagetable lock held.
  405. */
  406. void free_pgd_range(struct mmu_gather *tlb,
  407. unsigned long addr, unsigned long end,
  408. unsigned long floor, unsigned long ceiling)
  409. {
  410. pgd_t *pgd;
  411. unsigned long next;
  412. /*
  413. * The next few lines have given us lots of grief...
  414. *
  415. * Why are we testing PMD* at this top level? Because often
  416. * there will be no work to do at all, and we'd prefer not to
  417. * go all the way down to the bottom just to discover that.
  418. *
  419. * Why all these "- 1"s? Because 0 represents both the bottom
  420. * of the address space and the top of it (using -1 for the
  421. * top wouldn't help much: the masks would do the wrong thing).
  422. * The rule is that addr 0 and floor 0 refer to the bottom of
  423. * the address space, but end 0 and ceiling 0 refer to the top
  424. * Comparisons need to use "end - 1" and "ceiling - 1" (though
  425. * that end 0 case should be mythical).
  426. *
  427. * Wherever addr is brought up or ceiling brought down, we must
  428. * be careful to reject "the opposite 0" before it confuses the
  429. * subsequent tests. But what about where end is brought down
  430. * by PMD_SIZE below? no, end can't go down to 0 there.
  431. *
  432. * Whereas we round start (addr) and ceiling down, by different
  433. * masks at different levels, in order to test whether a table
  434. * now has no other vmas using it, so can be freed, we don't
  435. * bother to round floor or end up - the tests don't need that.
  436. */
  437. addr &= PMD_MASK;
  438. if (addr < floor) {
  439. addr += PMD_SIZE;
  440. if (!addr)
  441. return;
  442. }
  443. if (ceiling) {
  444. ceiling &= PMD_MASK;
  445. if (!ceiling)
  446. return;
  447. }
  448. if (end - 1 > ceiling - 1)
  449. end -= PMD_SIZE;
  450. if (addr > end - 1)
  451. return;
  452. pgd = pgd_offset(tlb->mm, addr);
  453. do {
  454. next = pgd_addr_end(addr, end);
  455. if (pgd_none_or_clear_bad(pgd))
  456. continue;
  457. free_pud_range(tlb, pgd, addr, next, floor, ceiling);
  458. } while (pgd++, addr = next, addr != end);
  459. }
  460. void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *vma,
  461. unsigned long floor, unsigned long ceiling)
  462. {
  463. while (vma) {
  464. struct vm_area_struct *next = vma->vm_next;
  465. unsigned long addr = vma->vm_start;
  466. /*
  467. * Hide vma from rmap and truncate_pagecache before freeing
  468. * pgtables
  469. */
  470. unlink_anon_vmas(vma);
  471. unlink_file_vma(vma);
  472. if (is_vm_hugetlb_page(vma)) {
  473. hugetlb_free_pgd_range(tlb, addr, vma->vm_end,
  474. floor, next? next->vm_start: ceiling);
  475. } else {
  476. /*
  477. * Optimization: gather nearby vmas into one call down
  478. */
  479. while (next && next->vm_start <= vma->vm_end + PMD_SIZE
  480. && !is_vm_hugetlb_page(next)) {
  481. vma = next;
  482. next = vma->vm_next;
  483. unlink_anon_vmas(vma);
  484. unlink_file_vma(vma);
  485. }
  486. free_pgd_range(tlb, addr, vma->vm_end,
  487. floor, next? next->vm_start: ceiling);
  488. }
  489. vma = next;
  490. }
  491. }
  492. int __pte_alloc(struct mm_struct *mm, struct vm_area_struct *vma,
  493. pmd_t *pmd, unsigned long address)
  494. {
  495. pgtable_t new = pte_alloc_one(mm, address);
  496. int wait_split_huge_page;
  497. if (!new)
  498. return -ENOMEM;
  499. /*
  500. * Ensure all pte setup (eg. pte page lock and page clearing) are
  501. * visible before the pte is made visible to other CPUs by being
  502. * put into page tables.
  503. *
  504. * The other side of the story is the pointer chasing in the page
  505. * table walking code (when walking the page table without locking;
  506. * ie. most of the time). Fortunately, these data accesses consist
  507. * of a chain of data-dependent loads, meaning most CPUs (alpha
  508. * being the notable exception) will already guarantee loads are
  509. * seen in-order. See the alpha page table accessors for the
  510. * smp_read_barrier_depends() barriers in page table walking code.
  511. */
  512. smp_wmb(); /* Could be smp_wmb__xxx(before|after)_spin_lock */
  513. spin_lock(&mm->page_table_lock);
  514. wait_split_huge_page = 0;
  515. if (likely(pmd_none(*pmd))) { /* Has another populated it ? */
  516. mm->nr_ptes++;
  517. pmd_populate(mm, pmd, new);
  518. new = NULL;
  519. } else if (unlikely(pmd_trans_splitting(*pmd)))
  520. wait_split_huge_page = 1;
  521. spin_unlock(&mm->page_table_lock);
  522. if (new)
  523. pte_free(mm, new);
  524. if (wait_split_huge_page)
  525. wait_split_huge_page(vma->anon_vma, pmd);
  526. return 0;
  527. }
  528. int __pte_alloc_kernel(pmd_t *pmd, unsigned long address)
  529. {
  530. pte_t *new = pte_alloc_one_kernel(&init_mm, address);
  531. if (!new)
  532. return -ENOMEM;
  533. smp_wmb(); /* See comment in __pte_alloc */
  534. spin_lock(&init_mm.page_table_lock);
  535. if (likely(pmd_none(*pmd))) { /* Has another populated it ? */
  536. pmd_populate_kernel(&init_mm, pmd, new);
  537. new = NULL;
  538. } else
  539. VM_BUG_ON(pmd_trans_splitting(*pmd));
  540. spin_unlock(&init_mm.page_table_lock);
  541. if (new)
  542. pte_free_kernel(&init_mm, new);
  543. return 0;
  544. }
  545. static inline void init_rss_vec(int *rss)
  546. {
  547. memset(rss, 0, sizeof(int) * NR_MM_COUNTERS);
  548. }
  549. static inline void add_mm_rss_vec(struct mm_struct *mm, int *rss)
  550. {
  551. int i;
  552. if (current->mm == mm)
  553. sync_mm_rss(mm);
  554. for (i = 0; i < NR_MM_COUNTERS; i++)
  555. if (rss[i])
  556. add_mm_counter(mm, i, rss[i]);
  557. }
  558. /*
  559. * This function is called to print an error when a bad pte
  560. * is found. For example, we might have a PFN-mapped pte in
  561. * a region that doesn't allow it.
  562. *
  563. * The calling function must still handle the error.
  564. */
  565. static void print_bad_pte(struct vm_area_struct *vma, unsigned long addr,
  566. pte_t pte, struct page *page)
  567. {
  568. pgd_t *pgd = pgd_offset(vma->vm_mm, addr);
  569. pud_t *pud = pud_offset(pgd, addr);
  570. pmd_t *pmd = pmd_offset(pud, addr);
  571. struct address_space *mapping;
  572. pgoff_t index;
  573. static unsigned long resume;
  574. static unsigned long nr_shown;
  575. static unsigned long nr_unshown;
  576. /*
  577. * Allow a burst of 60 reports, then keep quiet for that minute;
  578. * or allow a steady drip of one report per second.
  579. */
  580. if (nr_shown == 60) {
  581. if (time_before(jiffies, resume)) {
  582. nr_unshown++;
  583. return;
  584. }
  585. if (nr_unshown) {
  586. printk(KERN_ALERT
  587. "BUG: Bad page map: %lu messages suppressed\n",
  588. nr_unshown);
  589. nr_unshown = 0;
  590. }
  591. nr_shown = 0;
  592. }
  593. if (nr_shown++ == 0)
  594. resume = jiffies + 60 * HZ;
  595. mapping = vma->vm_file ? vma->vm_file->f_mapping : NULL;
  596. index = linear_page_index(vma, addr);
  597. printk(KERN_ALERT
  598. "BUG: Bad page map in process %s pte:%08llx pmd:%08llx\n",
  599. current->comm,
  600. (long long)pte_val(pte), (long long)pmd_val(*pmd));
  601. if (page)
  602. dump_page(page);
  603. printk(KERN_ALERT
  604. "addr:%p vm_flags:%08lx anon_vma:%p mapping:%p index:%lx\n",
  605. (void *)addr, vma->vm_flags, vma->anon_vma, mapping, index);
  606. /*
  607. * Choose text because data symbols depend on CONFIG_KALLSYMS_ALL=y
  608. */
  609. if (vma->vm_ops)
  610. printk(KERN_ALERT "vma->vm_ops->fault: %pSR\n",
  611. vma->vm_ops->fault);
  612. if (vma->vm_file && vma->vm_file->f_op)
  613. printk(KERN_ALERT "vma->vm_file->f_op->mmap: %pSR\n",
  614. vma->vm_file->f_op->mmap);
  615. dump_stack();
  616. add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
  617. }
  618. static inline bool is_cow_mapping(vm_flags_t flags)
  619. {
  620. return (flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
  621. }
  622. /*
  623. * vm_normal_page -- This function gets the "struct page" associated with a pte.
  624. *
  625. * "Special" mappings do not wish to be associated with a "struct page" (either
  626. * it doesn't exist, or it exists but they don't want to touch it). In this
  627. * case, NULL is returned here. "Normal" mappings do have a struct page.
  628. *
  629. * There are 2 broad cases. Firstly, an architecture may define a pte_special()
  630. * pte bit, in which case this function is trivial. Secondly, an architecture
  631. * may not have a spare pte bit, which requires a more complicated scheme,
  632. * described below.
  633. *
  634. * A raw VM_PFNMAP mapping (ie. one that is not COWed) is always considered a
  635. * special mapping (even if there are underlying and valid "struct pages").
  636. * COWed pages of a VM_PFNMAP are always normal.
  637. *
  638. * The way we recognize COWed pages within VM_PFNMAP mappings is through the
  639. * rules set up by "remap_pfn_range()": the vma will have the VM_PFNMAP bit
  640. * set, and the vm_pgoff will point to the first PFN mapped: thus every special
  641. * mapping will always honor the rule
  642. *
  643. * pfn_of_page == vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT)
  644. *
  645. * And for normal mappings this is false.
  646. *
  647. * This restricts such mappings to be a linear translation from virtual address
  648. * to pfn. To get around this restriction, we allow arbitrary mappings so long
  649. * as the vma is not a COW mapping; in that case, we know that all ptes are
  650. * special (because none can have been COWed).
  651. *
  652. *
  653. * In order to support COW of arbitrary special mappings, we have VM_MIXEDMAP.
  654. *
  655. * VM_MIXEDMAP mappings can likewise contain memory with or without "struct
  656. * page" backing, however the difference is that _all_ pages with a struct
  657. * page (that is, those where pfn_valid is true) are refcounted and considered
  658. * normal pages by the VM. The disadvantage is that pages are refcounted
  659. * (which can be slower and simply not an option for some PFNMAP users). The
  660. * advantage is that we don't have to follow the strict linearity rule of
  661. * PFNMAP mappings in order to support COWable mappings.
  662. *
  663. */
  664. #ifdef __HAVE_ARCH_PTE_SPECIAL
  665. # define HAVE_PTE_SPECIAL 1
  666. #else
  667. # define HAVE_PTE_SPECIAL 0
  668. #endif
  669. struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr,
  670. pte_t pte)
  671. {
  672. unsigned long pfn = pte_pfn(pte);
  673. if (HAVE_PTE_SPECIAL) {
  674. if (likely(!pte_special(pte)))
  675. goto check_pfn;
  676. if (vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP))
  677. return NULL;
  678. if (!is_zero_pfn(pfn))
  679. print_bad_pte(vma, addr, pte, NULL);
  680. return NULL;
  681. }
  682. /* !HAVE_PTE_SPECIAL case follows: */
  683. if (unlikely(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP))) {
  684. if (vma->vm_flags & VM_MIXEDMAP) {
  685. if (!pfn_valid(pfn))
  686. return NULL;
  687. goto out;
  688. } else {
  689. unsigned long off;
  690. off = (addr - vma->vm_start) >> PAGE_SHIFT;
  691. if (pfn == vma->vm_pgoff + off)
  692. return NULL;
  693. if (!is_cow_mapping(vma->vm_flags))
  694. return NULL;
  695. }
  696. }
  697. if (is_zero_pfn(pfn))
  698. return NULL;
  699. check_pfn:
  700. if (unlikely(pfn > highest_memmap_pfn)) {
  701. print_bad_pte(vma, addr, pte, NULL);
  702. return NULL;
  703. }
  704. /*
  705. * NOTE! We still have PageReserved() pages in the page tables.
  706. * eg. VDSO mappings can cause them to exist.
  707. */
  708. out:
  709. return pfn_to_page(pfn);
  710. }
  711. /*
  712. * copy one vm_area from one task to the other. Assumes the page tables
  713. * already present in the new task to be cleared in the whole range
  714. * covered by this vma.
  715. */
  716. static inline unsigned long
  717. copy_one_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm,
  718. pte_t *dst_pte, pte_t *src_pte, struct vm_area_struct *vma,
  719. unsigned long addr, int *rss)
  720. {
  721. unsigned long vm_flags = vma->vm_flags;
  722. pte_t pte = *src_pte;
  723. struct page *page;
  724. /* pte contains position in swap or file, so copy. */
  725. if (unlikely(!pte_present(pte))) {
  726. if (!pte_file(pte)) {
  727. swp_entry_t entry = pte_to_swp_entry(pte);
  728. if (swap_duplicate(entry) < 0)
  729. return entry.val;
  730. /* make sure dst_mm is on swapoff's mmlist. */
  731. if (unlikely(list_empty(&dst_mm->mmlist))) {
  732. spin_lock(&mmlist_lock);
  733. if (list_empty(&dst_mm->mmlist))
  734. list_add(&dst_mm->mmlist,
  735. &src_mm->mmlist);
  736. spin_unlock(&mmlist_lock);
  737. }
  738. if (likely(!non_swap_entry(entry)))
  739. rss[MM_SWAPENTS]++;
  740. else if (is_migration_entry(entry)) {
  741. page = migration_entry_to_page(entry);
  742. if (PageAnon(page))
  743. rss[MM_ANONPAGES]++;
  744. else
  745. rss[MM_FILEPAGES]++;
  746. if (is_write_migration_entry(entry) &&
  747. is_cow_mapping(vm_flags)) {
  748. /*
  749. * COW mappings require pages in both
  750. * parent and child to be set to read.
  751. */
  752. make_migration_entry_read(&entry);
  753. pte = swp_entry_to_pte(entry);
  754. set_pte_at(src_mm, addr, src_pte, pte);
  755. }
  756. }
  757. }
  758. goto out_set_pte;
  759. }
  760. /*
  761. * If it's a COW mapping, write protect it both
  762. * in the parent and the child
  763. */
  764. if (is_cow_mapping(vm_flags)) {
  765. ptep_set_wrprotect(src_mm, addr, src_pte);
  766. pte = pte_wrprotect(pte);
  767. }
  768. /*
  769. * If it's a shared mapping, mark it clean in
  770. * the child
  771. */
  772. if (vm_flags & VM_SHARED)
  773. pte = pte_mkclean(pte);
  774. pte = pte_mkold(pte);
  775. page = vm_normal_page(vma, addr, pte);
  776. if (page) {
  777. get_page(page);
  778. page_dup_rmap(page);
  779. if (PageAnon(page))
  780. rss[MM_ANONPAGES]++;
  781. else
  782. rss[MM_FILEPAGES]++;
  783. }
  784. out_set_pte:
  785. set_pte_at(dst_mm, addr, dst_pte, pte);
  786. return 0;
  787. }
  788. int copy_pte_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
  789. pmd_t *dst_pmd, pmd_t *src_pmd, struct vm_area_struct *vma,
  790. unsigned long addr, unsigned long end)
  791. {
  792. pte_t *orig_src_pte, *orig_dst_pte;
  793. pte_t *src_pte, *dst_pte;
  794. spinlock_t *src_ptl, *dst_ptl;
  795. int progress = 0;
  796. int rss[NR_MM_COUNTERS];
  797. swp_entry_t entry = (swp_entry_t){0};
  798. again:
  799. init_rss_vec(rss);
  800. dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl);
  801. if (!dst_pte)
  802. return -ENOMEM;
  803. src_pte = pte_offset_map(src_pmd, addr);
  804. src_ptl = pte_lockptr(src_mm, src_pmd);
  805. spin_lock_nested(src_ptl, SINGLE_DEPTH_NESTING);
  806. orig_src_pte = src_pte;
  807. orig_dst_pte = dst_pte;
  808. arch_enter_lazy_mmu_mode();
  809. do {
  810. /*
  811. * We are holding two locks at this point - either of them
  812. * could generate latencies in another task on another CPU.
  813. */
  814. if (progress >= 32) {
  815. progress = 0;
  816. if (need_resched() ||
  817. spin_needbreak(src_ptl) || spin_needbreak(dst_ptl))
  818. break;
  819. }
  820. if (pte_none(*src_pte)) {
  821. progress++;
  822. continue;
  823. }
  824. entry.val = copy_one_pte(dst_mm, src_mm, dst_pte, src_pte,
  825. vma, addr, rss);
  826. if (entry.val)
  827. break;
  828. progress += 8;
  829. } while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end);
  830. arch_leave_lazy_mmu_mode();
  831. spin_unlock(src_ptl);
  832. pte_unmap(orig_src_pte);
  833. add_mm_rss_vec(dst_mm, rss);
  834. pte_unmap_unlock(orig_dst_pte, dst_ptl);
  835. cond_resched();
  836. if (entry.val) {
  837. if (add_swap_count_continuation(entry, GFP_KERNEL) < 0)
  838. return -ENOMEM;
  839. progress = 0;
  840. }
  841. if (addr != end)
  842. goto again;
  843. return 0;
  844. }
  845. static inline int copy_pmd_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
  846. pud_t *dst_pud, pud_t *src_pud, struct vm_area_struct *vma,
  847. unsigned long addr, unsigned long end)
  848. {
  849. pmd_t *src_pmd, *dst_pmd;
  850. unsigned long next;
  851. dst_pmd = pmd_alloc(dst_mm, dst_pud, addr);
  852. if (!dst_pmd)
  853. return -ENOMEM;
  854. src_pmd = pmd_offset(src_pud, addr);
  855. do {
  856. next = pmd_addr_end(addr, end);
  857. if (pmd_trans_huge(*src_pmd)) {
  858. int err;
  859. VM_BUG_ON(next-addr != HPAGE_PMD_SIZE);
  860. err = copy_huge_pmd(dst_mm, src_mm,
  861. dst_pmd, src_pmd, addr, vma);
  862. if (err == -ENOMEM)
  863. return -ENOMEM;
  864. if (!err)
  865. continue;
  866. /* fall through */
  867. }
  868. if (pmd_none_or_clear_bad(src_pmd))
  869. continue;
  870. if (copy_pte_range(dst_mm, src_mm, dst_pmd, src_pmd,
  871. vma, addr, next))
  872. return -ENOMEM;
  873. } while (dst_pmd++, src_pmd++, addr = next, addr != end);
  874. return 0;
  875. }
  876. static inline int copy_pud_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
  877. pgd_t *dst_pgd, pgd_t *src_pgd, struct vm_area_struct *vma,
  878. unsigned long addr, unsigned long end)
  879. {
  880. pud_t *src_pud, *dst_pud;
  881. unsigned long next;
  882. dst_pud = pud_alloc(dst_mm, dst_pgd, addr);
  883. if (!dst_pud)
  884. return -ENOMEM;
  885. src_pud = pud_offset(src_pgd, addr);
  886. do {
  887. next = pud_addr_end(addr, end);
  888. if (pud_none_or_clear_bad(src_pud))
  889. continue;
  890. if (copy_pmd_range(dst_mm, src_mm, dst_pud, src_pud,
  891. vma, addr, next))
  892. return -ENOMEM;
  893. } while (dst_pud++, src_pud++, addr = next, addr != end);
  894. return 0;
  895. }
  896. int copy_page_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
  897. struct vm_area_struct *vma)
  898. {
  899. pgd_t *src_pgd, *dst_pgd;
  900. unsigned long next;
  901. unsigned long addr = vma->vm_start;
  902. unsigned long end = vma->vm_end;
  903. unsigned long mmun_start; /* For mmu_notifiers */
  904. unsigned long mmun_end; /* For mmu_notifiers */
  905. bool is_cow;
  906. int ret;
  907. /*
  908. * Don't copy ptes where a page fault will fill them correctly.
  909. * Fork becomes much lighter when there are big shared or private
  910. * readonly mappings. The tradeoff is that copy_page_range is more
  911. * efficient than faulting.
  912. */
  913. if (!(vma->vm_flags & (VM_HUGETLB | VM_NONLINEAR |
  914. VM_PFNMAP | VM_MIXEDMAP))) {
  915. if (!vma->anon_vma)
  916. return 0;
  917. }
  918. if (is_vm_hugetlb_page(vma))
  919. return copy_hugetlb_page_range(dst_mm, src_mm, vma);
  920. if (unlikely(vma->vm_flags & VM_PFNMAP)) {
  921. /*
  922. * We do not free on error cases below as remove_vma
  923. * gets called on error from higher level routine
  924. */
  925. ret = track_pfn_copy(vma);
  926. if (ret)
  927. return ret;
  928. }
  929. /*
  930. * We need to invalidate the secondary MMU mappings only when
  931. * there could be a permission downgrade on the ptes of the
  932. * parent mm. And a permission downgrade will only happen if
  933. * is_cow_mapping() returns true.
  934. */
  935. is_cow = is_cow_mapping(vma->vm_flags);
  936. mmun_start = addr;
  937. mmun_end = end;
  938. if (is_cow)
  939. mmu_notifier_invalidate_range_start(src_mm, mmun_start,
  940. mmun_end);
  941. ret = 0;
  942. dst_pgd = pgd_offset(dst_mm, addr);
  943. src_pgd = pgd_offset(src_mm, addr);
  944. do {
  945. next = pgd_addr_end(addr, end);
  946. if (pgd_none_or_clear_bad(src_pgd))
  947. continue;
  948. if (unlikely(copy_pud_range(dst_mm, src_mm, dst_pgd, src_pgd,
  949. vma, addr, next))) {
  950. ret = -ENOMEM;
  951. break;
  952. }
  953. } while (dst_pgd++, src_pgd++, addr = next, addr != end);
  954. if (is_cow)
  955. mmu_notifier_invalidate_range_end(src_mm, mmun_start, mmun_end);
  956. return ret;
  957. }
  958. static unsigned long zap_pte_range(struct mmu_gather *tlb,
  959. struct vm_area_struct *vma, pmd_t *pmd,
  960. unsigned long addr, unsigned long end,
  961. struct zap_details *details)
  962. {
  963. struct mm_struct *mm = tlb->mm;
  964. int force_flush = 0;
  965. int rss[NR_MM_COUNTERS];
  966. spinlock_t *ptl;
  967. pte_t *start_pte;
  968. pte_t *pte;
  969. unsigned long range_start = addr;
  970. again:
  971. init_rss_vec(rss);
  972. start_pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
  973. pte = start_pte;
  974. arch_enter_lazy_mmu_mode();
  975. do {
  976. pte_t ptent = *pte;
  977. if (pte_none(ptent)) {
  978. continue;
  979. }
  980. if (pte_present(ptent)) {
  981. struct page *page;
  982. page = vm_normal_page(vma, addr, ptent);
  983. if (unlikely(details) && page) {
  984. /*
  985. * unmap_shared_mapping_pages() wants to
  986. * invalidate cache without truncating:
  987. * unmap shared but keep private pages.
  988. */
  989. if (details->check_mapping &&
  990. details->check_mapping != page->mapping)
  991. continue;
  992. /*
  993. * Each page->index must be checked when
  994. * invalidating or truncating nonlinear.
  995. */
  996. if (details->nonlinear_vma &&
  997. (page->index < details->first_index ||
  998. page->index > details->last_index))
  999. continue;
  1000. }
  1001. ptent = ptep_get_and_clear_full(mm, addr, pte,
  1002. tlb->fullmm);
  1003. tlb_remove_tlb_entry(tlb, pte, addr);
  1004. if (unlikely(!page))
  1005. continue;
  1006. if (unlikely(details) && details->nonlinear_vma
  1007. && linear_page_index(details->nonlinear_vma,
  1008. addr) != page->index)
  1009. set_pte_at(mm, addr, pte,
  1010. pgoff_to_pte(page->index));
  1011. if (PageAnon(page))
  1012. rss[MM_ANONPAGES]--;
  1013. else {
  1014. if (pte_dirty(ptent))
  1015. set_page_dirty(page);
  1016. if (pte_young(ptent) &&
  1017. likely(!(vma->vm_flags & VM_SEQ_READ)))
  1018. mark_page_accessed(page);
  1019. rss[MM_FILEPAGES]--;
  1020. }
  1021. page_remove_rmap(page);
  1022. if (unlikely(page_mapcount(page) < 0))
  1023. print_bad_pte(vma, addr, ptent, page);
  1024. force_flush = !__tlb_remove_page(tlb, page);
  1025. if (force_flush)
  1026. break;
  1027. continue;
  1028. }
  1029. /*
  1030. * If details->check_mapping, we leave swap entries;
  1031. * if details->nonlinear_vma, we leave file entries.
  1032. */
  1033. if (unlikely(details))
  1034. continue;
  1035. if (pte_file(ptent)) {
  1036. if (unlikely(!(vma->vm_flags & VM_NONLINEAR)))
  1037. print_bad_pte(vma, addr, ptent, NULL);
  1038. } else {
  1039. swp_entry_t entry = pte_to_swp_entry(ptent);
  1040. if (!non_swap_entry(entry))
  1041. rss[MM_SWAPENTS]--;
  1042. else if (is_migration_entry(entry)) {
  1043. struct page *page;
  1044. page = migration_entry_to_page(entry);
  1045. if (PageAnon(page))
  1046. rss[MM_ANONPAGES]--;
  1047. else
  1048. rss[MM_FILEPAGES]--;
  1049. }
  1050. if (unlikely(!free_swap_and_cache(entry)))
  1051. print_bad_pte(vma, addr, ptent, NULL);
  1052. }
  1053. pte_clear_not_present_full(mm, addr, pte, tlb->fullmm);
  1054. } while (pte++, addr += PAGE_SIZE, addr != end);
  1055. add_mm_rss_vec(mm, rss);
  1056. arch_leave_lazy_mmu_mode();
  1057. pte_unmap_unlock(start_pte, ptl);
  1058. /*
  1059. * mmu_gather ran out of room to batch pages, we break out of
  1060. * the PTE lock to avoid doing the potential expensive TLB invalidate
  1061. * and page-free while holding it.
  1062. */
  1063. if (force_flush) {
  1064. force_flush = 0;
  1065. #ifdef HAVE_GENERIC_MMU_GATHER
  1066. tlb->start = range_start;
  1067. tlb->end = addr;
  1068. #endif
  1069. tlb_flush_mmu(tlb);
  1070. if (addr != end) {
  1071. range_start = addr;
  1072. goto again;
  1073. }
  1074. }
  1075. return addr;
  1076. }
  1077. static inline unsigned long zap_pmd_range(struct mmu_gather *tlb,
  1078. struct vm_area_struct *vma, pud_t *pud,
  1079. unsigned long addr, unsigned long end,
  1080. struct zap_details *details)
  1081. {
  1082. pmd_t *pmd;
  1083. unsigned long next;
  1084. pmd = pmd_offset(pud, addr);
  1085. do {
  1086. next = pmd_addr_end(addr, end);
  1087. if (pmd_trans_huge(*pmd)) {
  1088. if (next - addr != HPAGE_PMD_SIZE) {
  1089. #ifdef CONFIG_DEBUG_VM
  1090. if (!rwsem_is_locked(&tlb->mm->mmap_sem)) {
  1091. pr_err("%s: mmap_sem is unlocked! addr=0x%lx end=0x%lx vma->vm_start=0x%lx vma->vm_end=0x%lx\n",
  1092. __func__, addr, end,
  1093. vma->vm_start,
  1094. vma->vm_end);
  1095. BUG();
  1096. }
  1097. #endif
  1098. split_huge_page_pmd(vma, addr, pmd);
  1099. } else if (zap_huge_pmd(tlb, vma, pmd, addr))
  1100. goto next;
  1101. /* fall through */
  1102. }
  1103. /*
  1104. * Here there can be other concurrent MADV_DONTNEED or
  1105. * trans huge page faults running, and if the pmd is
  1106. * none or trans huge it can change under us. This is
  1107. * because MADV_DONTNEED holds the mmap_sem in read
  1108. * mode.
  1109. */
  1110. if (pmd_none_or_trans_huge_or_clear_bad(pmd))
  1111. goto next;
  1112. next = zap_pte_range(tlb, vma, pmd, addr, next, details);
  1113. next:
  1114. cond_resched();
  1115. } while (pmd++, addr = next, addr != end);
  1116. return addr;
  1117. }
  1118. static inline unsigned long zap_pud_range(struct mmu_gather *tlb,
  1119. struct vm_area_struct *vma, pgd_t *pgd,
  1120. unsigned long addr, unsigned long end,
  1121. struct zap_details *details)
  1122. {
  1123. pud_t *pud;
  1124. unsigned long next;
  1125. pud = pud_offset(pgd, addr);
  1126. do {
  1127. next = pud_addr_end(addr, end);
  1128. if (pud_none_or_clear_bad(pud))
  1129. continue;
  1130. next = zap_pmd_range(tlb, vma, pud, addr, next, details);
  1131. } while (pud++, addr = next, addr != end);
  1132. return addr;
  1133. }
  1134. static void unmap_page_range(struct mmu_gather *tlb,
  1135. struct vm_area_struct *vma,
  1136. unsigned long addr, unsigned long end,
  1137. struct zap_details *details)
  1138. {
  1139. pgd_t *pgd;
  1140. unsigned long next;
  1141. if (details && !details->check_mapping && !details->nonlinear_vma)
  1142. details = NULL;
  1143. BUG_ON(addr >= end);
  1144. mem_cgroup_uncharge_start();
  1145. tlb_start_vma(tlb, vma);
  1146. pgd = pgd_offset(vma->vm_mm, addr);
  1147. do {
  1148. next = pgd_addr_end(addr, end);
  1149. if (pgd_none_or_clear_bad(pgd))
  1150. continue;
  1151. next = zap_pud_range(tlb, vma, pgd, addr, next, details);
  1152. } while (pgd++, addr = next, addr != end);
  1153. tlb_end_vma(tlb, vma);
  1154. mem_cgroup_uncharge_end();
  1155. }
  1156. static void unmap_single_vma(struct mmu_gather *tlb,
  1157. struct vm_area_struct *vma, unsigned long start_addr,
  1158. unsigned long end_addr,
  1159. struct zap_details *details)
  1160. {
  1161. unsigned long start = max(vma->vm_start, start_addr);
  1162. unsigned long end;
  1163. if (start >= vma->vm_end)
  1164. return;
  1165. end = min(vma->vm_end, end_addr);
  1166. if (end <= vma->vm_start)
  1167. return;
  1168. if (vma->vm_file)
  1169. uprobe_munmap(vma, start, end);
  1170. if (unlikely(vma->vm_flags & VM_PFNMAP))
  1171. untrack_pfn(vma, 0, 0);
  1172. if (start != end) {
  1173. if (unlikely(is_vm_hugetlb_page(vma))) {
  1174. /*
  1175. * It is undesirable to test vma->vm_file as it
  1176. * should be non-null for valid hugetlb area.
  1177. * However, vm_file will be NULL in the error
  1178. * cleanup path of do_mmap_pgoff. When
  1179. * hugetlbfs ->mmap method fails,
  1180. * do_mmap_pgoff() nullifies vma->vm_file
  1181. * before calling this function to clean up.
  1182. * Since no pte has actually been setup, it is
  1183. * safe to do nothing in this case.
  1184. */
  1185. if (vma->vm_file) {
  1186. mutex_lock(&vma->vm_file->f_mapping->i_mmap_mutex);
  1187. __unmap_hugepage_range_final(tlb, vma, start, end, NULL);
  1188. mutex_unlock(&vma->vm_file->f_mapping->i_mmap_mutex);
  1189. }
  1190. } else
  1191. unmap_page_range(tlb, vma, start, end, details);
  1192. }
  1193. }
  1194. /**
  1195. * unmap_vmas - unmap a range of memory covered by a list of vma's
  1196. * @tlb: address of the caller's struct mmu_gather
  1197. * @vma: the starting vma
  1198. * @start_addr: virtual address at which to start unmapping
  1199. * @end_addr: virtual address at which to end unmapping
  1200. *
  1201. * Unmap all pages in the vma list.
  1202. *
  1203. * Only addresses between `start' and `end' will be unmapped.
  1204. *
  1205. * The VMA list must be sorted in ascending virtual address order.
  1206. *
  1207. * unmap_vmas() assumes that the caller will flush the whole unmapped address
  1208. * range after unmap_vmas() returns. So the only responsibility here is to
  1209. * ensure that any thus-far unmapped pages are flushed before unmap_vmas()
  1210. * drops the lock and schedules.
  1211. */
  1212. void unmap_vmas(struct mmu_gather *tlb,
  1213. struct vm_area_struct *vma, unsigned long start_addr,
  1214. unsigned long end_addr)
  1215. {
  1216. struct mm_struct *mm = vma->vm_mm;
  1217. mmu_notifier_invalidate_range_start(mm, start_addr, end_addr);
  1218. for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next)
  1219. unmap_single_vma(tlb, vma, start_addr, end_addr, NULL);
  1220. mmu_notifier_invalidate_range_end(mm, start_addr, end_addr);
  1221. }
  1222. /**
  1223. * zap_page_range - remove user pages in a given range
  1224. * @vma: vm_area_struct holding the applicable pages
  1225. * @start: starting address of pages to zap
  1226. * @size: number of bytes to zap
  1227. * @details: details of nonlinear truncation or shared cache invalidation
  1228. *
  1229. * Caller must protect the VMA list
  1230. */
  1231. void zap_page_range(struct vm_area_struct *vma, unsigned long start,
  1232. unsigned long size, struct zap_details *details)
  1233. {
  1234. struct mm_struct *mm = vma->vm_mm;
  1235. struct mmu_gather tlb;
  1236. unsigned long end = start + size;
  1237. lru_add_drain();
  1238. tlb_gather_mmu(&tlb, mm, 0);
  1239. update_hiwater_rss(mm);
  1240. mmu_notifier_invalidate_range_start(mm, start, end);
  1241. for ( ; vma && vma->vm_start < end; vma = vma->vm_next)
  1242. unmap_single_vma(&tlb, vma, start, end, details);
  1243. mmu_notifier_invalidate_range_end(mm, start, end);
  1244. tlb_finish_mmu(&tlb, start, end);
  1245. }
  1246. /**
  1247. * zap_page_range_single - remove user pages in a given range
  1248. * @vma: vm_area_struct holding the applicable pages
  1249. * @address: starting address of pages to zap
  1250. * @size: number of bytes to zap
  1251. * @details: details of nonlinear truncation or shared cache invalidation
  1252. *
  1253. * The range must fit into one VMA.
  1254. */
  1255. static void zap_page_range_single(struct vm_area_struct *vma, unsigned long address,
  1256. unsigned long size, struct zap_details *details)
  1257. {
  1258. struct mm_struct *mm = vma->vm_mm;
  1259. struct mmu_gather tlb;
  1260. unsigned long end = address + size;
  1261. lru_add_drain();
  1262. tlb_gather_mmu(&tlb, mm, 0);
  1263. update_hiwater_rss(mm);
  1264. mmu_notifier_invalidate_range_start(mm, address, end);
  1265. unmap_single_vma(&tlb, vma, address, end, details);
  1266. mmu_notifier_invalidate_range_end(mm, address, end);
  1267. tlb_finish_mmu(&tlb, address, end);
  1268. }
  1269. /**
  1270. * zap_vma_ptes - remove ptes mapping the vma
  1271. * @vma: vm_area_struct holding ptes to be zapped
  1272. * @address: starting address of pages to zap
  1273. * @size: number of bytes to zap
  1274. *
  1275. * This function only unmaps ptes assigned to VM_PFNMAP vmas.
  1276. *
  1277. * The entire address range must be fully contained within the vma.
  1278. *
  1279. * Returns 0 if successful.
  1280. */
  1281. int zap_vma_ptes(struct vm_area_struct *vma, unsigned long address,
  1282. unsigned long size)
  1283. {
  1284. if (address < vma->vm_start || address + size > vma->vm_end ||
  1285. !(vma->vm_flags & VM_PFNMAP))
  1286. return -1;
  1287. zap_page_range_single(vma, address, size, NULL);
  1288. return 0;
  1289. }
  1290. EXPORT_SYMBOL_GPL(zap_vma_ptes);
  1291. /**
  1292. * follow_page_mask - look up a page descriptor from a user-virtual address
  1293. * @vma: vm_area_struct mapping @address
  1294. * @address: virtual address to look up
  1295. * @flags: flags modifying lookup behaviour
  1296. * @page_mask: on output, *page_mask is set according to the size of the page
  1297. *
  1298. * @flags can have FOLL_ flags set, defined in <linux/mm.h>
  1299. *
  1300. * Returns the mapped (struct page *), %NULL if no mapping exists, or
  1301. * an error pointer if there is a mapping to something not represented
  1302. * by a page descriptor (see also vm_normal_page()).
  1303. */
  1304. struct page *follow_page_mask(struct vm_area_struct *vma,
  1305. unsigned long address, unsigned int flags,
  1306. unsigned int *page_mask)
  1307. {
  1308. pgd_t *pgd;
  1309. pud_t *pud;
  1310. pmd_t *pmd;
  1311. pte_t *ptep, pte;
  1312. spinlock_t *ptl;
  1313. struct page *page;
  1314. struct mm_struct *mm = vma->vm_mm;
  1315. *page_mask = 0;
  1316. page = follow_huge_addr(mm, address, flags & FOLL_WRITE);
  1317. if (!IS_ERR(page)) {
  1318. BUG_ON(flags & FOLL_GET);
  1319. goto out;
  1320. }
  1321. page = NULL;
  1322. pgd = pgd_offset(mm, address);
  1323. if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
  1324. goto no_page_table;
  1325. pud = pud_offset(pgd, address);
  1326. if (pud_none(*pud))
  1327. goto no_page_table;
  1328. if (pud_huge(*pud) && vma->vm_flags & VM_HUGETLB) {
  1329. BUG_ON(flags & FOLL_GET);
  1330. page = follow_huge_pud(mm, address, pud, flags & FOLL_WRITE);
  1331. goto out;
  1332. }
  1333. if (unlikely(pud_bad(*pud)))
  1334. goto no_page_table;
  1335. pmd = pmd_offset(pud, address);
  1336. if (pmd_none(*pmd))
  1337. goto no_page_table;
  1338. if (pmd_huge(*pmd) && vma->vm_flags & VM_HUGETLB) {
  1339. BUG_ON(flags & FOLL_GET);
  1340. page = follow_huge_pmd(mm, address, pmd, flags & FOLL_WRITE);
  1341. goto out;
  1342. }
  1343. if ((flags & FOLL_NUMA) && pmd_numa(*pmd))
  1344. goto no_page_table;
  1345. if (pmd_trans_huge(*pmd)) {
  1346. if (flags & FOLL_SPLIT) {
  1347. split_huge_page_pmd(vma, address, pmd);
  1348. goto split_fallthrough;
  1349. }
  1350. spin_lock(&mm->page_table_lock);
  1351. if (likely(pmd_trans_huge(*pmd))) {
  1352. if (unlikely(pmd_trans_splitting(*pmd))) {
  1353. spin_unlock(&mm->page_table_lock);
  1354. wait_split_huge_page(vma->anon_vma, pmd);
  1355. } else {
  1356. page = follow_trans_huge_pmd(vma, address,
  1357. pmd, flags);
  1358. spin_unlock(&mm->page_table_lock);
  1359. *page_mask = HPAGE_PMD_NR - 1;
  1360. goto out;
  1361. }
  1362. } else
  1363. spin_unlock(&mm->page_table_lock);
  1364. /* fall through */
  1365. }
  1366. split_fallthrough:
  1367. if (unlikely(pmd_bad(*pmd)))
  1368. goto no_page_table;
  1369. ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
  1370. pte = *ptep;
  1371. if (!pte_present(pte)) {
  1372. swp_entry_t entry;
  1373. /*
  1374. * KSM's break_ksm() relies upon recognizing a ksm page
  1375. * even while it is being migrated, so for that case we
  1376. * need migration_entry_wait().
  1377. */
  1378. if (likely(!(flags & FOLL_MIGRATION)))
  1379. goto no_page;
  1380. if (pte_none(pte) || pte_file(pte))
  1381. goto no_page;
  1382. entry = pte_to_swp_entry(pte);
  1383. if (!is_migration_entry(entry))
  1384. goto no_page;
  1385. pte_unmap_unlock(ptep, ptl);
  1386. migration_entry_wait(mm, pmd, address);
  1387. goto split_fallthrough;
  1388. }
  1389. if ((flags & FOLL_NUMA) && pte_numa(pte))
  1390. goto no_page;
  1391. if ((flags & FOLL_WRITE) && !pte_write(pte))
  1392. goto unlock;
  1393. page = vm_normal_page(vma, address, pte);
  1394. if (unlikely(!page)) {
  1395. if ((flags & FOLL_DUMP) ||
  1396. !is_zero_pfn(pte_pfn(pte)))
  1397. goto bad_page;
  1398. page = pte_page(pte);
  1399. }
  1400. if (flags & FOLL_GET)
  1401. get_page_foll(page);
  1402. if (flags & FOLL_TOUCH) {
  1403. if ((flags & FOLL_WRITE) &&
  1404. !pte_dirty(pte) && !PageDirty(page))
  1405. set_page_dirty(page);
  1406. /*
  1407. * pte_mkyoung() would be more correct here, but atomic care
  1408. * is needed to avoid losing the dirty bit: it is easier to use
  1409. * mark_page_accessed().
  1410. */
  1411. mark_page_accessed(page);
  1412. }
  1413. if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) {
  1414. /*
  1415. * The preliminary mapping check is mainly to avoid the
  1416. * pointless overhead of lock_page on the ZERO_PAGE
  1417. * which might bounce very badly if there is contention.
  1418. *
  1419. * If the page is already locked, we don't need to
  1420. * handle it now - vmscan will handle it later if and
  1421. * when it attempts to reclaim the page.
  1422. */
  1423. if (page->mapping && trylock_page(page)) {
  1424. lru_add_drain(); /* push cached pages to LRU */
  1425. /*
  1426. * Because we lock page here, and migration is
  1427. * blocked by the pte's page reference, and we
  1428. * know the page is still mapped, we don't even
  1429. * need to check for file-cache page truncation.
  1430. */
  1431. mlock_vma_page(page);
  1432. unlock_page(page);
  1433. }
  1434. }
  1435. unlock:
  1436. pte_unmap_unlock(ptep, ptl);
  1437. out:
  1438. return page;
  1439. bad_page:
  1440. pte_unmap_unlock(ptep, ptl);
  1441. return ERR_PTR(-EFAULT);
  1442. no_page:
  1443. pte_unmap_unlock(ptep, ptl);
  1444. if (!pte_none(pte))
  1445. return page;
  1446. no_page_table:
  1447. /*
  1448. * When core dumping an enormous anonymous area that nobody
  1449. * has touched so far, we don't want to allocate unnecessary pages or
  1450. * page tables. Return error instead of NULL to skip handle_mm_fault,
  1451. * then get_dump_page() will return NULL to leave a hole in the dump.
  1452. * But we can only make this optimization where a hole would surely
  1453. * be zero-filled if handle_mm_fault() actually did handle it.
  1454. */
  1455. if ((flags & FOLL_DUMP) &&
  1456. (!vma->vm_ops || !vma->vm_ops->fault))
  1457. return ERR_PTR(-EFAULT);
  1458. return page;
  1459. }
  1460. static inline int stack_guard_page(struct vm_area_struct *vma, unsigned long addr)
  1461. {
  1462. return stack_guard_page_start(vma, addr) ||
  1463. stack_guard_page_end(vma, addr+PAGE_SIZE);
  1464. }
  1465. /**
  1466. * __get_user_pages() - pin user pages in memory
  1467. * @tsk: task_struct of target task
  1468. * @mm: mm_struct of target mm
  1469. * @start: starting user address
  1470. * @nr_pages: number of pages from start to pin
  1471. * @gup_flags: flags modifying pin behaviour
  1472. * @pages: array that receives pointers to the pages pinned.
  1473. * Should be at least nr_pages long. Or NULL, if caller
  1474. * only intends to ensure the pages are faulted in.
  1475. * @vmas: array of pointers to vmas corresponding to each page.
  1476. * Or NULL if the caller does not require them.
  1477. * @nonblocking: whether waiting for disk IO or mmap_sem contention
  1478. *
  1479. * Returns number of pages pinned. This may be fewer than the number
  1480. * requested. If nr_pages is 0 or negative, returns 0. If no pages
  1481. * were pinned, returns -errno. Each page returned must be released
  1482. * with a put_page() call when it is finished with. vmas will only
  1483. * remain valid while mmap_sem is held.
  1484. *
  1485. * Must be called with mmap_sem held for read or write.
  1486. *
  1487. * __get_user_pages walks a process's page tables and takes a reference to
  1488. * each struct page that each user address corresponds to at a given
  1489. * instant. That is, it takes the page that would be accessed if a user
  1490. * thread accesses the given user virtual address at that instant.
  1491. *
  1492. * This does not guarantee that the page exists in the user mappings when
  1493. * __get_user_pages returns, and there may even be a completely different
  1494. * page there in some cases (eg. if mmapped pagecache has been invalidated
  1495. * and subsequently re faulted). However it does guarantee that the page
  1496. * won't be freed completely. And mostly callers simply care that the page
  1497. * contains data that was valid *at some point in time*. Typically, an IO
  1498. * or similar operation cannot guarantee anything stronger anyway because
  1499. * locks can't be held over the syscall boundary.
  1500. *
  1501. * If @gup_flags & FOLL_WRITE == 0, the page must not be written to. If
  1502. * the page is written to, set_page_dirty (or set_page_dirty_lock, as
  1503. * appropriate) must be called after the page is finished with, and
  1504. * before put_page is called.
  1505. *
  1506. * If @nonblocking != NULL, __get_user_pages will not wait for disk IO
  1507. * or mmap_sem contention, and if waiting is needed to pin all pages,
  1508. * *@nonblocking will be set to 0.
  1509. *
  1510. * In most cases, get_user_pages or get_user_pages_fast should be used
  1511. * instead of __get_user_pages. __get_user_pages should be used only if
  1512. * you need some special @gup_flags.
  1513. */
  1514. long __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
  1515. unsigned long start, unsigned long nr_pages,
  1516. unsigned int gup_flags, struct page **pages,
  1517. struct vm_area_struct **vmas, int *nonblocking)
  1518. {
  1519. long i;
  1520. unsigned long vm_flags;
  1521. unsigned int page_mask;
  1522. if (!nr_pages)
  1523. return 0;
  1524. VM_BUG_ON(!!pages != !!(gup_flags & FOLL_GET));
  1525. /*
  1526. * Require read or write permissions.
  1527. * If FOLL_FORCE is set, we only require the "MAY" flags.
  1528. */
  1529. vm_flags = (gup_flags & FOLL_WRITE) ?
  1530. (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
  1531. vm_flags &= (gup_flags & FOLL_FORCE) ?
  1532. (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
  1533. /*
  1534. * If FOLL_FORCE and FOLL_NUMA are both set, handle_mm_fault
  1535. * would be called on PROT_NONE ranges. We must never invoke
  1536. * handle_mm_fault on PROT_NONE ranges or the NUMA hinting
  1537. * page faults would unprotect the PROT_NONE ranges if
  1538. * _PAGE_NUMA and _PAGE_PROTNONE are sharing the same pte/pmd
  1539. * bitflag. So to avoid that, don't set FOLL_NUMA if
  1540. * FOLL_FORCE is set.
  1541. */
  1542. if (!(gup_flags & FOLL_FORCE))
  1543. gup_flags |= FOLL_NUMA;
  1544. i = 0;
  1545. do {
  1546. struct vm_area_struct *vma;
  1547. vma = find_extend_vma(mm, start);
  1548. if (!vma && in_gate_area(mm, start)) {
  1549. unsigned long pg = start & PAGE_MASK;
  1550. pgd_t *pgd;
  1551. pud_t *pud;
  1552. pmd_t *pmd;
  1553. pte_t *pte;
  1554. /* user gate pages are read-only */
  1555. if (gup_flags & FOLL_WRITE)
  1556. return i ? : -EFAULT;
  1557. if (pg > TASK_SIZE)
  1558. pgd = pgd_offset_k(pg);
  1559. else
  1560. pgd = pgd_offset_gate(mm, pg);
  1561. BUG_ON(pgd_none(*pgd));
  1562. pud = pud_offset(pgd, pg);
  1563. BUG_ON(pud_none(*pud));
  1564. pmd = pmd_offset(pud, pg);
  1565. if (pmd_none(*pmd))
  1566. return i ? : -EFAULT;
  1567. VM_BUG_ON(pmd_trans_huge(*pmd));
  1568. pte = pte_offset_map(pmd, pg);
  1569. if (pte_none(*pte)) {
  1570. pte_unmap(pte);
  1571. return i ? : -EFAULT;
  1572. }
  1573. vma = get_gate_vma(mm);
  1574. if (pages) {
  1575. struct page *page;
  1576. page = vm_normal_page(vma, start, *pte);
  1577. if (!page) {
  1578. if (!(gup_flags & FOLL_DUMP) &&
  1579. is_zero_pfn(pte_pfn(*pte)))
  1580. page = pte_page(*pte);
  1581. else {
  1582. pte_unmap(pte);
  1583. return i ? : -EFAULT;
  1584. }
  1585. }
  1586. pages[i] = page;
  1587. get_page(page);
  1588. }
  1589. pte_unmap(pte);
  1590. page_mask = 0;
  1591. goto next_page;
  1592. }
  1593. if (!vma ||
  1594. (vma->vm_flags & (VM_IO | VM_PFNMAP)) ||
  1595. !(vm_flags & vma->vm_flags))
  1596. return i ? : -EFAULT;
  1597. if (is_vm_hugetlb_page(vma)) {
  1598. i = follow_hugetlb_page(mm, vma, pages, vmas,
  1599. &start, &nr_pages, i, gup_flags);
  1600. continue;
  1601. }
  1602. do {
  1603. struct page *page;
  1604. unsigned int foll_flags = gup_flags;
  1605. unsigned int page_increm;
  1606. /*
  1607. * If we have a pending SIGKILL, don't keep faulting
  1608. * pages and potentially allocating memory.
  1609. */
  1610. if (unlikely(fatal_signal_pending(current)))
  1611. return i ? i : -ERESTARTSYS;
  1612. cond_resched();
  1613. while (!(page = follow_page_mask(vma, start,
  1614. foll_flags, &page_mask))) {
  1615. int ret;
  1616. unsigned int fault_flags = 0;
  1617. /* For mlock, just skip the stack guard page. */
  1618. if (foll_flags & FOLL_MLOCK) {
  1619. if (stack_guard_page(vma, start))
  1620. goto next_page;
  1621. }
  1622. if (foll_flags & FOLL_WRITE)
  1623. fault_flags |= FAULT_FLAG_WRITE;
  1624. if (nonblocking)
  1625. fault_flags |= FAULT_FLAG_ALLOW_RETRY;
  1626. if (foll_flags & FOLL_NOWAIT)
  1627. fault_flags |= (FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT);
  1628. ret = handle_mm_fault(mm, vma, start,
  1629. fault_flags);
  1630. if (ret & VM_FAULT_ERROR) {
  1631. if (ret & VM_FAULT_OOM)
  1632. return i ? i : -ENOMEM;
  1633. if (ret & (VM_FAULT_HWPOISON |
  1634. VM_FAULT_HWPOISON_LARGE)) {
  1635. if (i)
  1636. return i;
  1637. else if (gup_flags & FOLL_HWPOISON)
  1638. return -EHWPOISON;
  1639. else
  1640. return -EFAULT;
  1641. }
  1642. if (ret & VM_FAULT_SIGBUS)
  1643. return i ? i : -EFAULT;
  1644. BUG();
  1645. }
  1646. if (tsk) {
  1647. if (ret & VM_FAULT_MAJOR)
  1648. tsk->maj_flt++;
  1649. else
  1650. tsk->min_flt++;
  1651. }
  1652. if (ret & VM_FAULT_RETRY) {
  1653. if (nonblocking)
  1654. *nonblocking = 0;
  1655. return i;
  1656. }
  1657. /*
  1658. * The VM_FAULT_WRITE bit tells us that
  1659. * do_wp_page has broken COW when necessary,
  1660. * even if maybe_mkwrite decided not to set
  1661. * pte_write. We can thus safely do subsequent
  1662. * page lookups as if they were reads. But only
  1663. * do so when looping for pte_write is futile:
  1664. * in some cases userspace may also be wanting
  1665. * to write to the gotten user page, which a
  1666. * read fault here might prevent (a readonly
  1667. * page might get reCOWed by userspace write).
  1668. */
  1669. if ((ret & VM_FAULT_WRITE) &&
  1670. !(vma->vm_flags & VM_WRITE))
  1671. foll_flags &= ~FOLL_WRITE;
  1672. cond_resched();
  1673. }
  1674. if (IS_ERR(page))
  1675. return i ? i : PTR_ERR(page);
  1676. if (pages) {
  1677. pages[i] = page;
  1678. flush_anon_page(vma, page, start);
  1679. flush_dcache_page(page);
  1680. page_mask = 0;
  1681. }
  1682. next_page:
  1683. if (vmas) {
  1684. vmas[i] = vma;
  1685. page_mask = 0;
  1686. }
  1687. page_increm = 1 + (~(start >> PAGE_SHIFT) & page_mask);
  1688. if (page_increm > nr_pages)
  1689. page_increm = nr_pages;
  1690. i += page_increm;
  1691. start += page_increm * PAGE_SIZE;
  1692. nr_pages -= page_increm;
  1693. } while (nr_pages && start < vma->vm_end);
  1694. } while (nr_pages);
  1695. return i;
  1696. }
  1697. EXPORT_SYMBOL(__get_user_pages);
  1698. /*
  1699. * fixup_user_fault() - manually resolve a user page fault
  1700. * @tsk: the task_struct to use for page fault accounting, or
  1701. * NULL if faults are not to be recorded.
  1702. * @mm: mm_struct of target mm
  1703. * @address: user address
  1704. * @fault_flags:flags to pass down to handle_mm_fault()
  1705. *
  1706. * This is meant to be called in the specific scenario where for locking reasons
  1707. * we try to access user memory in atomic context (within a pagefault_disable()
  1708. * section), this returns -EFAULT, and we want to resolve the user fault before
  1709. * trying again.
  1710. *
  1711. * Typically this is meant to be used by the futex code.
  1712. *
  1713. * The main difference with get_user_pages() is that this function will
  1714. * unconditionally call handle_mm_fault() which will in turn perform all the
  1715. * necessary SW fixup of the dirty and young bits in the PTE, while
  1716. * handle_mm_fault() only guarantees to update these in the struct page.
  1717. *
  1718. * This is important for some architectures where those bits also gate the
  1719. * access permission to the page because they are maintained in software. On
  1720. * such architectures, gup() will not be enough to make a subsequent access
  1721. * succeed.
  1722. *
  1723. * This should be called with the mm_sem held for read.
  1724. */
  1725. int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm,
  1726. unsigned long address, unsigned int fault_flags)
  1727. {
  1728. struct vm_area_struct *vma;
  1729. int ret;
  1730. vma = find_extend_vma(mm, address);
  1731. if (!vma || address < vma->vm_start)
  1732. return -EFAULT;
  1733. ret = handle_mm_fault(mm, vma, address, fault_flags);
  1734. if (ret & VM_FAULT_ERROR) {
  1735. if (ret & VM_FAULT_OOM)
  1736. return -ENOMEM;
  1737. if (ret & (VM_FAULT_HWPOISON | VM_FAULT_HWPOISON_LARGE))
  1738. return -EHWPOISON;
  1739. if (ret & VM_FAULT_SIGBUS)
  1740. return -EFAULT;
  1741. BUG();
  1742. }
  1743. if (tsk) {
  1744. if (ret & VM_FAULT_MAJOR)
  1745. tsk->maj_flt++;
  1746. else
  1747. tsk->min_flt++;
  1748. }
  1749. return 0;
  1750. }
  1751. /*
  1752. * get_user_pages() - pin user pages in memory
  1753. * @tsk: the task_struct to use for page fault accounting, or
  1754. * NULL if faults are not to be recorded.
  1755. * @mm: mm_struct of target mm
  1756. * @start: starting user address
  1757. * @nr_pages: number of pages from start to pin
  1758. * @write: whether pages will be written to by the caller
  1759. * @force: whether to force write access even if user mapping is
  1760. * readonly. This will result in the page being COWed even
  1761. * in MAP_SHARED mappings. You do not want this.
  1762. * @pages: array that receives pointers to the pages pinned.
  1763. * Should be at least nr_pages long. Or NULL, if caller
  1764. * only intends to ensure the pages are faulted in.
  1765. * @vmas: array of pointers to vmas corresponding to each page.
  1766. * Or NULL if the caller does not require them.
  1767. *
  1768. * Returns number of pages pinned. This may be fewer than the number
  1769. * requested. If nr_pages is 0 or negative, returns 0. If no pages
  1770. * were pinned, returns -errno. Each page returned must be released
  1771. * with a put_page() call when it is finished with. vmas will only
  1772. * remain valid while mmap_sem is held.
  1773. *
  1774. * Must be called with mmap_sem held for read or write.
  1775. *
  1776. * get_user_pages walks a process's page tables and takes a reference to
  1777. * each struct page that each user address corresponds to at a given
  1778. * instant. That is, it takes the page that would be accessed if a user
  1779. * thread accesses the given user virtual address at that instant.
  1780. *
  1781. * This does not guarantee that the page exists in the user mappings when
  1782. * get_user_pages returns, and there may even be a completely different
  1783. * page there in some cases (eg. if mmapped pagecache has been invalidated
  1784. * and subsequently re faulted). However it does guarantee that the page
  1785. * won't be freed completely. And mostly callers simply care that the page
  1786. * contains data that was valid *at some point in time*. Typically, an IO
  1787. * or similar operation cannot guarantee anything stronger anyway because
  1788. * locks can't be held over the syscall boundary.
  1789. *
  1790. * If write=0, the page must not be written to. If the page is written to,
  1791. * set_page_dirty (or set_page_dirty_lock, as appropriate) must be called
  1792. * after the page is finished with, and before put_page is called.
  1793. *
  1794. * get_user_pages is typically used for fewer-copy IO operations, to get a
  1795. * handle on the memory by some means other than accesses via the user virtual
  1796. * addresses. The pages may be submitted for DMA to devices or accessed via
  1797. * their kernel linear mapping (via the kmap APIs). Care should be taken to
  1798. * use the correct cache flushing APIs.
  1799. *
  1800. * See also get_user_pages_fast, for performance critical applications.
  1801. */
  1802. long get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
  1803. unsigned long start, unsigned long nr_pages, int write,
  1804. int force, struct page **pages, struct vm_area_struct **vmas)
  1805. {
  1806. int flags = FOLL_TOUCH;
  1807. if (pages)
  1808. flags |= FOLL_GET;
  1809. if (write)
  1810. flags |= FOLL_WRITE;
  1811. if (force)
  1812. flags |= FOLL_FORCE;
  1813. return __get_user_pages(tsk, mm, start, nr_pages, flags, pages, vmas,
  1814. NULL);
  1815. }
  1816. EXPORT_SYMBOL(get_user_pages);
  1817. /**
  1818. * get_dump_page() - pin user page in memory while writing it to core dump
  1819. * @addr: user address
  1820. *
  1821. * Returns struct page pointer of user page pinned for dump,
  1822. * to be freed afterwards by page_cache_release() or put_page().
  1823. *
  1824. * Returns NULL on any kind of failure - a hole must then be inserted into
  1825. * the corefile, to preserve alignment with its headers; and also returns
  1826. * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found -
  1827. * allowing a hole to be left in the corefile to save diskspace.
  1828. *
  1829. * Called without mmap_sem, but after all other threads have been killed.
  1830. */
  1831. #ifdef CONFIG_ELF_CORE
  1832. struct page *get_dump_page(unsigned long addr)
  1833. {
  1834. struct vm_area_struct *vma;
  1835. struct page *page;
  1836. if (__get_user_pages(current, current->mm, addr, 1,
  1837. FOLL_FORCE | FOLL_DUMP | FOLL_GET, &page, &vma,
  1838. NULL) < 1)
  1839. return NULL;
  1840. flush_cache_page(vma, addr, page_to_pfn(page));
  1841. return page;
  1842. }
  1843. #endif /* CONFIG_ELF_CORE */
  1844. pte_t *__get_locked_pte(struct mm_struct *mm, unsigned long addr,
  1845. spinlock_t **ptl)
  1846. {
  1847. pgd_t * pgd = pgd_offset(mm, addr);
  1848. pud_t * pud = pud_alloc(mm, pgd, addr);
  1849. if (pud) {
  1850. pmd_t * pmd = pmd_alloc(mm, pud, addr);
  1851. if (pmd) {
  1852. VM_BUG_ON(pmd_trans_huge(*pmd));
  1853. return pte_alloc_map_lock(mm, pmd, addr, ptl);
  1854. }
  1855. }
  1856. return NULL;
  1857. }
  1858. /*
  1859. * This is the old fallback for page remapping.
  1860. *
  1861. * For historical reasons, it only allows reserved pages. Only
  1862. * old drivers should use this, and they needed to mark their
  1863. * pages reserved for the old functions anyway.
  1864. */
  1865. static int insert_page(struct vm_area_struct *vma, unsigned long addr,
  1866. struct page *page, pgprot_t prot)
  1867. {
  1868. struct mm_struct *mm = vma->vm_mm;
  1869. int retval;
  1870. pte_t *pte;
  1871. spinlock_t *ptl;
  1872. retval = -EINVAL;
  1873. if (PageAnon(page))
  1874. goto out;
  1875. retval = -ENOMEM;
  1876. flush_dcache_page(page);
  1877. pte = get_locked_pte(mm, addr, &ptl);
  1878. if (!pte)
  1879. goto out;
  1880. retval = -EBUSY;
  1881. if (!pte_none(*pte))
  1882. goto out_unlock;
  1883. /* Ok, finally just insert the thing.. */
  1884. get_page(page);
  1885. inc_mm_counter_fast(mm, MM_FILEPAGES);
  1886. page_add_file_rmap(page);
  1887. set_pte_at(mm, addr, pte, mk_pte(page, prot));
  1888. retval = 0;
  1889. pte_unmap_unlock(pte, ptl);
  1890. return retval;
  1891. out_unlock:
  1892. pte_unmap_unlock(pte, ptl);
  1893. out:
  1894. return retval;
  1895. }
  1896. /**
  1897. * vm_insert_page - insert single page into user vma
  1898. * @vma: user vma to map to
  1899. * @addr: target user address of this page
  1900. * @page: source kernel page
  1901. *
  1902. * This allows drivers to insert individual pages they've allocated
  1903. * into a user vma.
  1904. *
  1905. * The page has to be a nice clean _individual_ kernel allocation.
  1906. * If you allocate a compound page, you need to have marked it as
  1907. * such (__GFP_COMP), or manually just split the page up yourself
  1908. * (see split_page()).
  1909. *
  1910. * NOTE! Traditionally this was done with "remap_pfn_range()" which
  1911. * took an arbitrary page protection parameter. This doesn't allow
  1912. * that. Your vma protection will have to be set up correctly, which
  1913. * means that if you want a shared writable mapping, you'd better
  1914. * ask for a shared writable mapping!
  1915. *
  1916. * The page does not need to be reserved.
  1917. *
  1918. * Usually this function is called from f_op->mmap() handler
  1919. * under mm->mmap_sem write-lock, so it can change vma->vm_flags.
  1920. * Caller must set VM_MIXEDMAP on vma if it wants to call this
  1921. * function from other places, for example from page-fault handler.
  1922. */
  1923. int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
  1924. struct page *page)
  1925. {
  1926. if (addr < vma->vm_start || addr >= vma->vm_end)
  1927. return -EFAULT;
  1928. if (!page_count(page))
  1929. return -EINVAL;
  1930. if (!(vma->vm_flags & VM_MIXEDMAP)) {
  1931. BUG_ON(down_read_trylock(&vma->vm_mm->mmap_sem));
  1932. BUG_ON(vma->vm_flags & VM_PFNMAP);
  1933. vma->vm_flags |= VM_MIXEDMAP;
  1934. }
  1935. return insert_page(vma, addr, page, vma->vm_page_prot);
  1936. }
  1937. EXPORT_SYMBOL(vm_insert_page);
  1938. static int insert_pfn(struct vm_area_struct *vma, unsigned long addr,
  1939. unsigned long pfn, pgprot_t prot)
  1940. {
  1941. struct mm_struct *mm = vma->vm_mm;
  1942. int retval;
  1943. pte_t *pte, entry;
  1944. spinlock_t *ptl;
  1945. retval = -ENOMEM;
  1946. pte = get_locked_pte(mm, addr, &ptl);
  1947. if (!pte)
  1948. goto out;
  1949. retval = -EBUSY;
  1950. if (!pte_none(*pte))
  1951. goto out_unlock;
  1952. /* Ok, finally just insert the thing.. */
  1953. entry = pte_mkspecial(pfn_pte(pfn, prot));
  1954. set_pte_at(mm, addr, pte, entry);
  1955. update_mmu_cache(vma, addr, pte); /* XXX: why not for insert_page? */
  1956. retval = 0;
  1957. out_unlock:
  1958. pte_unmap_unlock(pte, ptl);
  1959. out:
  1960. return retval;
  1961. }
  1962. /**
  1963. * vm_insert_pfn - insert single pfn into user vma
  1964. * @vma: user vma to map to
  1965. * @addr: target user address of this page
  1966. * @pfn: source kernel pfn
  1967. *
  1968. * Similar to vm_insert_page, this allows drivers to insert individual pages
  1969. * they've allocated into a user vma. Same comments apply.
  1970. *
  1971. * This function should only be called from a vm_ops->fault handler, and
  1972. * in that case the handler should return NULL.
  1973. *
  1974. * vma cannot be a COW mapping.
  1975. *
  1976. * As this is called only for pages that do not currently exist, we
  1977. * do not need to flush old virtual caches or the TLB.
  1978. */
  1979. int vm_insert_pfn(struct vm_area_struct *vma, unsigned long addr,
  1980. unsigned long pfn)
  1981. {
  1982. int ret;
  1983. pgprot_t pgprot = vma->vm_page_prot;
  1984. /*
  1985. * Technically, architectures with pte_special can avoid all these
  1986. * restrictions (same for remap_pfn_range). However we would like
  1987. * consistency in testing and feature parity among all, so we should
  1988. * try to keep these invariants in place for everybody.
  1989. */
  1990. BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)));
  1991. BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) ==
  1992. (VM_PFNMAP|VM_MIXEDMAP));
  1993. BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags));
  1994. BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn));
  1995. if (addr < vma->vm_start || addr >= vma->vm_end)
  1996. return -EFAULT;
  1997. if (track_pfn_insert(vma, &pgprot, pfn))
  1998. return -EINVAL;
  1999. ret = insert_pfn(vma, addr, pfn, pgprot);
  2000. return ret;
  2001. }
  2002. EXPORT_SYMBOL(vm_insert_pfn);
  2003. int vm_insert_mixed(struct vm_area_struct *vma, unsigned long addr,
  2004. unsigned long pfn)
  2005. {
  2006. BUG_ON(!(vma->vm_flags & VM_MIXEDMAP));
  2007. if (addr < vma->vm_start || addr >= vma->vm_end)
  2008. return -EFAULT;
  2009. /*
  2010. * If we don't have pte special, then we have to use the pfn_valid()
  2011. * based VM_MIXEDMAP scheme (see vm_normal_page), and thus we *must*
  2012. * refcount the page if pfn_valid is true (hence insert_page rather
  2013. * than insert_pfn). If a zero_pfn were inserted into a VM_MIXEDMAP
  2014. * without pte special, it would there be refcounted as a normal page.
  2015. */
  2016. if (!HAVE_PTE_SPECIAL && pfn_valid(pfn)) {
  2017. struct page *page;
  2018. page = pfn_to_page(pfn);
  2019. return insert_page(vma, addr, page, vma->vm_page_prot);
  2020. }
  2021. return insert_pfn(vma, addr, pfn, vma->vm_page_prot);
  2022. }
  2023. EXPORT_SYMBOL(vm_insert_mixed);
  2024. /*
  2025. * maps a range of physical memory into the requested pages. the old
  2026. * mappings are removed. any references to nonexistent pages results
  2027. * in null mappings (currently treated as "copy-on-access")
  2028. */
  2029. static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd,
  2030. unsigned long addr, unsigned long end,
  2031. unsigned long pfn, pgprot_t prot)
  2032. {
  2033. pte_t *pte;
  2034. spinlock_t *ptl;
  2035. pte = pte_alloc_map_lock(mm, pmd, addr, &ptl);
  2036. if (!pte)
  2037. return -ENOMEM;
  2038. arch_enter_lazy_mmu_mode();
  2039. do {
  2040. BUG_ON(!pte_none(*pte));
  2041. set_pte_at(mm, addr, pte, pte_mkspecial(pfn_pte(pfn, prot)));
  2042. pfn++;
  2043. } while (pte++, addr += PAGE_SIZE, addr != end);
  2044. arch_leave_lazy_mmu_mode();
  2045. pte_unmap_unlock(pte - 1, ptl);
  2046. return 0;
  2047. }
  2048. static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud,
  2049. unsigned long addr, unsigned long end,
  2050. unsigned long pfn, pgprot_t prot)
  2051. {
  2052. pmd_t *pmd;
  2053. unsigned long next;
  2054. pfn -= addr >> PAGE_SHIFT;
  2055. pmd = pmd_alloc(mm, pud, addr);
  2056. if (!pmd)
  2057. return -ENOMEM;
  2058. VM_BUG_ON(pmd_trans_huge(*pmd));
  2059. do {
  2060. next = pmd_addr_end(addr, end);
  2061. if (remap_pte_range(mm, pmd, addr, next,
  2062. pfn + (addr >> PAGE_SHIFT), prot))
  2063. return -ENOMEM;
  2064. } while (pmd++, addr = next, addr != end);
  2065. return 0;
  2066. }
  2067. static inline int remap_pud_range(struct mm_struct *mm, pgd_t *pgd,
  2068. unsigned long addr, unsigned long end,
  2069. unsigned long pfn, pgprot_t prot)
  2070. {
  2071. pud_t *pud;
  2072. unsigned long next;
  2073. pfn -= addr >> PAGE_SHIFT;
  2074. pud = pud_alloc(mm, pgd, addr);
  2075. if (!pud)
  2076. return -ENOMEM;
  2077. do {
  2078. next = pud_addr_end(addr, end);
  2079. if (remap_pmd_range(mm, pud, addr, next,
  2080. pfn + (addr >> PAGE_SHIFT), prot))
  2081. return -ENOMEM;
  2082. } while (pud++, addr = next, addr != end);
  2083. return 0;
  2084. }
  2085. /**
  2086. * remap_pfn_range - remap kernel memory to userspace
  2087. * @vma: user vma to map to
  2088. * @addr: target user address to start at
  2089. * @pfn: physical address of kernel memory
  2090. * @size: size of map area
  2091. * @prot: page protection flags for this mapping
  2092. *
  2093. * Note: this is only safe if the mm semaphore is held when called.
  2094. */
  2095. int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
  2096. unsigned long pfn, unsigned long size, pgprot_t prot)
  2097. {
  2098. pgd_t *pgd;
  2099. unsigned long next;
  2100. unsigned long end = addr + PAGE_ALIGN(size);
  2101. struct mm_struct *mm = vma->vm_mm;
  2102. int err;
  2103. /*
  2104. * Physically remapped pages are special. Tell the
  2105. * rest of the world about it:
  2106. * VM_IO tells people not to look at these pages
  2107. * (accesses can have side effects).
  2108. * VM_PFNMAP tells the core MM that the base pages are just
  2109. * raw PFN mappings, and do not have a "struct page" associated
  2110. * with them.
  2111. * VM_DONTEXPAND
  2112. * Disable vma merging and expanding with mremap().
  2113. * VM_DONTDUMP
  2114. * Omit vma from core dump, even when VM_IO turned off.
  2115. *
  2116. * There's a horrible special case to handle copy-on-write
  2117. * behaviour that some programs depend on. We mark the "original"
  2118. * un-COW'ed pages by matching them up with "vma->vm_pgoff".
  2119. * See vm_normal_page() for details.
  2120. */
  2121. if (is_cow_mapping(vma->vm_flags)) {
  2122. if (addr != vma->vm_start || end != vma->vm_end)
  2123. return -EINVAL;
  2124. vma->vm_pgoff = pfn;
  2125. }
  2126. err = track_pfn_remap(vma, &prot, pfn, addr, PAGE_ALIGN(size));
  2127. if (err)
  2128. return -EINVAL;
  2129. vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
  2130. BUG_ON(addr >= end);
  2131. pfn -= addr >> PAGE_SHIFT;
  2132. pgd = pgd_offset(mm, addr);
  2133. flush_cache_range(vma, addr, end);
  2134. do {
  2135. next = pgd_addr_end(addr, end);
  2136. err = remap_pud_range(mm, pgd, addr, next,
  2137. pfn + (addr >> PAGE_SHIFT), prot);
  2138. if (err)
  2139. break;
  2140. } while (pgd++, addr = next, addr != end);
  2141. if (err)
  2142. untrack_pfn(vma, pfn, PAGE_ALIGN(size));
  2143. return err;
  2144. }
  2145. EXPORT_SYMBOL(remap_pfn_range);
  2146. /**
  2147. * vm_iomap_memory - remap memory to userspace
  2148. * @vma: user vma to map to
  2149. * @start: start of area
  2150. * @len: size of area
  2151. *
  2152. * This is a simplified io_remap_pfn_range() for common driver use. The
  2153. * driver just needs to give us the physical memory range to be mapped,
  2154. * we'll figure out the rest from the vma information.
  2155. *
  2156. * NOTE! Some drivers might want to tweak vma->vm_page_prot first to get
  2157. * whatever write-combining details or similar.
  2158. */
  2159. int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len)
  2160. {
  2161. unsigned long vm_len, pfn, pages;
  2162. /* Check that the physical memory area passed in looks valid */
  2163. if (start + len < start)
  2164. return -EINVAL;
  2165. /*
  2166. * You *really* shouldn't map things that aren't page-aligned,
  2167. * but we've historically allowed it because IO memory might
  2168. * just have smaller alignment.
  2169. */
  2170. len += start & ~PAGE_MASK;
  2171. pfn = start >> PAGE_SHIFT;
  2172. pages = (len + ~PAGE_MASK) >> PAGE_SHIFT;
  2173. if (pfn + pages < pfn)
  2174. return -EINVAL;
  2175. /* We start the mapping 'vm_pgoff' pages into the area */
  2176. if (vma->vm_pgoff > pages)
  2177. return -EINVAL;
  2178. pfn += vma->vm_pgoff;
  2179. pages -= vma->vm_pgoff;
  2180. /* Can we fit all of the mapping? */
  2181. vm_len = vma->vm_end - vma->vm_start;
  2182. if (vm_len >> PAGE_SHIFT > pages)
  2183. return -EINVAL;
  2184. /* Ok, let it rip */
  2185. return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot);
  2186. }
  2187. EXPORT_SYMBOL(vm_iomap_memory);
  2188. static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd,
  2189. unsigned long addr, unsigned long end,
  2190. pte_fn_t fn, void *data)
  2191. {
  2192. pte_t *pte;
  2193. int err;
  2194. pgtable_t token;
  2195. spinlock_t *uninitialized_var(ptl);
  2196. pte = (mm == &init_mm) ?
  2197. pte_alloc_kernel(pmd, addr) :
  2198. pte_alloc_map_lock(mm, pmd, addr, &ptl);
  2199. if (!pte)
  2200. return -ENOMEM;
  2201. BUG_ON(pmd_huge(*pmd));
  2202. arch_enter_lazy_mmu_mode();
  2203. token = pmd_pgtable(*pmd);
  2204. do {
  2205. err = fn(pte++, token, addr, data);
  2206. if (err)
  2207. break;
  2208. } while (addr += PAGE_SIZE, addr != end);
  2209. arch_leave_lazy_mmu_mode();
  2210. if (mm != &init_mm)
  2211. pte_unmap_unlock(pte-1, ptl);
  2212. return err;
  2213. }
  2214. static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud,
  2215. unsigned long addr, unsigned long end,
  2216. pte_fn_t fn, void *data)
  2217. {
  2218. pmd_t *pmd;
  2219. unsigned long next;
  2220. int err;
  2221. BUG_ON(pud_huge(*pud));
  2222. pmd = pmd_alloc(mm, pud, addr);
  2223. if (!pmd)
  2224. return -ENOMEM;
  2225. do {
  2226. next = pmd_addr_end(addr, end);
  2227. err = apply_to_pte_range(mm, pmd, addr, next, fn, data);
  2228. if (err)
  2229. break;
  2230. } while (pmd++, addr = next, addr != end);
  2231. return err;
  2232. }
  2233. static int apply_to_pud_range(struct mm_struct *mm, pgd_t *pgd,
  2234. unsigned long addr, unsigned long end,
  2235. pte_fn_t fn, void *data)
  2236. {
  2237. pud_t *pud;
  2238. unsigned long next;
  2239. int err;
  2240. pud = pud_alloc(mm, pgd, addr);
  2241. if (!pud)
  2242. return -ENOMEM;
  2243. do {
  2244. next = pud_addr_end(addr, end);
  2245. err = apply_to_pmd_range(mm, pud, addr, next, fn, data);
  2246. if (err)
  2247. break;
  2248. } while (pud++, addr = next, addr != end);
  2249. return err;
  2250. }
  2251. /*
  2252. * Scan a region of virtual memory, filling in page tables as necessary
  2253. * and calling a provided function on each leaf page table.
  2254. */
  2255. int apply_to_page_range(struct mm_struct *mm, unsigned long addr,
  2256. unsigned long size, pte_fn_t fn, void *data)
  2257. {
  2258. pgd_t *pgd;
  2259. unsigned long next;
  2260. unsigned long end = addr + size;
  2261. int err;
  2262. BUG_ON(addr >= end);
  2263. pgd = pgd_offset(mm, addr);
  2264. do {
  2265. next = pgd_addr_end(addr, end);
  2266. err = apply_to_pud_range(mm, pgd, addr, next, fn, data);
  2267. if (err)
  2268. break;
  2269. } while (pgd++, addr = next, addr != end);
  2270. return err;
  2271. }
  2272. EXPORT_SYMBOL_GPL(apply_to_page_range);
  2273. /*
  2274. * handle_pte_fault chooses page fault handler according to an entry
  2275. * which was read non-atomically. Before making any commitment, on
  2276. * those architectures or configurations (e.g. i386 with PAE) which
  2277. * might give a mix of unmatched parts, do_swap_page and do_nonlinear_fault
  2278. * must check under lock before unmapping the pte and proceeding
  2279. * (but do_wp_page is only called after already making such a check;
  2280. * and do_anonymous_page can safely check later on).
  2281. */
  2282. static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd,
  2283. pte_t *page_table, pte_t orig_pte)
  2284. {
  2285. int same = 1;
  2286. #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
  2287. if (sizeof(pte_t) > sizeof(unsigned long)) {
  2288. spinlock_t *ptl = pte_lockptr(mm, pmd);
  2289. spin_lock(ptl);
  2290. same = pte_same(*page_table, orig_pte);
  2291. spin_unlock(ptl);
  2292. }
  2293. #endif
  2294. pte_unmap(page_table);
  2295. return same;
  2296. }
  2297. static inline void cow_user_page(struct page *dst, struct page *src, unsigned long va, struct vm_area_struct *vma)
  2298. {
  2299. /*
  2300. * If the source page was a PFN mapping, we don't have
  2301. * a "struct page" for it. We do a best-effort copy by
  2302. * just copying from the original user address. If that
  2303. * fails, we just zero-fill it. Live with it.
  2304. */
  2305. if (unlikely(!src)) {
  2306. void *kaddr = kmap_atomic(dst);
  2307. void __user *uaddr = (void __user *)(va & PAGE_MASK);
  2308. /*
  2309. * This really shouldn't fail, because the page is there
  2310. * in the page tables. But it might just be unreadable,
  2311. * in which case we just give up and fill the result with
  2312. * zeroes.
  2313. */
  2314. if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE))
  2315. clear_page(kaddr);
  2316. kunmap_atomic(kaddr);
  2317. flush_dcache_page(dst);
  2318. } else
  2319. copy_user_highpage(dst, src, va, vma);
  2320. }
  2321. /*
  2322. * This routine handles present pages, when users try to write
  2323. * to a shared page. It is done by copying the page to a new address
  2324. * and decrementing the shared-page counter for the old page.
  2325. *
  2326. * Note that this routine assumes that the protection checks have been
  2327. * done by the caller (the low-level page fault routine in most cases).
  2328. * Thus we can safely just mark it writable once we've done any necessary
  2329. * COW.
  2330. *
  2331. * We also mark the page dirty at this point even though the page will
  2332. * change only once the write actually happens. This avoids a few races,
  2333. * and potentially makes it more efficient.
  2334. *
  2335. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  2336. * but allow concurrent faults), with pte both mapped and locked.
  2337. * We return with mmap_sem still held, but pte unmapped and unlocked.
  2338. */
  2339. static int do_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
  2340. unsigned long address, pte_t *page_table, pmd_t *pmd,
  2341. spinlock_t *ptl, pte_t orig_pte)
  2342. __releases(ptl)
  2343. {
  2344. struct page *old_page, *new_page = NULL;
  2345. pte_t entry;
  2346. int ret = 0;
  2347. int page_mkwrite = 0;
  2348. struct page *dirty_page = NULL;
  2349. unsigned long mmun_start = 0; /* For mmu_notifiers */
  2350. unsigned long mmun_end = 0; /* For mmu_notifiers */
  2351. old_page = vm_normal_page(vma, address, orig_pte);
  2352. if (!old_page) {
  2353. /*
  2354. * VM_MIXEDMAP !pfn_valid() case
  2355. *
  2356. * We should not cow pages in a shared writeable mapping.
  2357. * Just mark the pages writable as we can't do any dirty
  2358. * accounting on raw pfn maps.
  2359. */
  2360. if ((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
  2361. (VM_WRITE|VM_SHARED))
  2362. goto reuse;
  2363. goto gotten;
  2364. }
  2365. /*
  2366. * Take out anonymous pages first, anonymous shared vmas are
  2367. * not dirty accountable.
  2368. */
  2369. if (PageAnon(old_page) && !PageKsm(old_page)) {
  2370. if (!trylock_page(old_page)) {
  2371. page_cache_get(old_page);
  2372. pte_unmap_unlock(page_table, ptl);
  2373. lock_page(old_page);
  2374. page_table = pte_offset_map_lock(mm, pmd, address,
  2375. &ptl);
  2376. if (!pte_same(*page_table, orig_pte)) {
  2377. unlock_page(old_page);
  2378. goto unlock;
  2379. }
  2380. page_cache_release(old_page);
  2381. }
  2382. if (reuse_swap_page(old_page)) {
  2383. /*
  2384. * The page is all ours. Move it to our anon_vma so
  2385. * the rmap code will not search our parent or siblings.
  2386. * Protected against the rmap code by the page lock.
  2387. */
  2388. page_move_anon_rmap(old_page, vma, address);
  2389. unlock_page(old_page);
  2390. goto reuse;
  2391. }
  2392. unlock_page(old_page);
  2393. } else if (unlikely((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
  2394. (VM_WRITE|VM_SHARED))) {
  2395. /*
  2396. * Only catch write-faults on shared writable pages,
  2397. * read-only shared pages can get COWed by
  2398. * get_user_pages(.write=1, .force=1).
  2399. */
  2400. if (vma->vm_ops && vma->vm_ops->page_mkwrite) {
  2401. struct vm_fault vmf;
  2402. int tmp;
  2403. vmf.virtual_address = (void __user *)(address &
  2404. PAGE_MASK);
  2405. vmf.pgoff = old_page->index;
  2406. vmf.flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE;
  2407. vmf.page = old_page;
  2408. /*
  2409. * Notify the address space that the page is about to
  2410. * become writable so that it can prohibit this or wait
  2411. * for the page to get into an appropriate state.
  2412. *
  2413. * We do this without the lock held, so that it can
  2414. * sleep if it needs to.
  2415. */
  2416. page_cache_get(old_page);
  2417. pte_unmap_unlock(page_table, ptl);
  2418. tmp = vma->vm_ops->page_mkwrite(vma, &vmf);
  2419. if (unlikely(tmp &
  2420. (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) {
  2421. ret = tmp;
  2422. goto unwritable_page;
  2423. }
  2424. if (unlikely(!(tmp & VM_FAULT_LOCKED))) {
  2425. lock_page(old_page);
  2426. if (!old_page->mapping) {
  2427. ret = 0; /* retry the fault */
  2428. unlock_page(old_page);
  2429. goto unwritable_page;
  2430. }
  2431. } else
  2432. VM_BUG_ON(!PageLocked(old_page));
  2433. /*
  2434. * Since we dropped the lock we need to revalidate
  2435. * the PTE as someone else may have changed it. If
  2436. * they did, we just return, as we can count on the
  2437. * MMU to tell us if they didn't also make it writable.
  2438. */
  2439. page_table = pte_offset_map_lock(mm, pmd, address,
  2440. &ptl);
  2441. if (!pte_same(*page_table, orig_pte)) {
  2442. unlock_page(old_page);
  2443. goto unlock;
  2444. }
  2445. page_mkwrite = 1;
  2446. }
  2447. dirty_page = old_page;
  2448. get_page(dirty_page);
  2449. reuse:
  2450. flush_cache_page(vma, address, pte_pfn(orig_pte));
  2451. entry = pte_mkyoung(orig_pte);
  2452. entry = maybe_mkwrite(pte_mkdirty(entry), vma);
  2453. if (ptep_set_access_flags(vma, address, page_table, entry,1))
  2454. update_mmu_cache(vma, address, page_table);
  2455. pte_unmap_unlock(page_table, ptl);
  2456. ret |= VM_FAULT_WRITE;
  2457. if (!dirty_page)
  2458. return ret;
  2459. /*
  2460. * Yes, Virginia, this is actually required to prevent a race
  2461. * with clear_page_dirty_for_io() from clearing the page dirty
  2462. * bit after it clear all dirty ptes, but before a racing
  2463. * do_wp_page installs a dirty pte.
  2464. *
  2465. * __do_fault is protected similarly.
  2466. */
  2467. if (!page_mkwrite) {
  2468. wait_on_page_locked(dirty_page);
  2469. set_page_dirty_balance(dirty_page, page_mkwrite);
  2470. /* file_update_time outside page_lock */
  2471. if (vma->vm_file)
  2472. file_update_time(vma->vm_file);
  2473. }
  2474. put_page(dirty_page);
  2475. if (page_mkwrite) {
  2476. struct address_space *mapping = dirty_page->mapping;
  2477. set_page_dirty(dirty_page);
  2478. unlock_page(dirty_page);
  2479. page_cache_release(dirty_page);
  2480. if (mapping) {
  2481. /*
  2482. * Some device drivers do not set page.mapping
  2483. * but still dirty their pages
  2484. */
  2485. balance_dirty_pages_ratelimited(mapping);
  2486. }
  2487. }
  2488. return ret;
  2489. }
  2490. /*
  2491. * Ok, we need to copy. Oh, well..
  2492. */
  2493. page_cache_get(old_page);
  2494. gotten:
  2495. pte_unmap_unlock(page_table, ptl);
  2496. if (unlikely(anon_vma_prepare(vma)))
  2497. goto oom;
  2498. if (is_zero_pfn(pte_pfn(orig_pte))) {
  2499. new_page = alloc_zeroed_user_highpage_movable(vma, address);
  2500. if (!new_page)
  2501. goto oom;
  2502. } else {
  2503. new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address);
  2504. if (!new_page)
  2505. goto oom;
  2506. cow_user_page(new_page, old_page, address, vma);
  2507. }
  2508. __SetPageUptodate(new_page);
  2509. if (mem_cgroup_newpage_charge(new_page, mm, GFP_KERNEL))
  2510. goto oom_free_new;
  2511. mmun_start = address & PAGE_MASK;
  2512. mmun_end = mmun_start + PAGE_SIZE;
  2513. mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
  2514. /*
  2515. * Re-check the pte - we dropped the lock
  2516. */
  2517. page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
  2518. if (likely(pte_same(*page_table, orig_pte))) {
  2519. if (old_page) {
  2520. if (!PageAnon(old_page)) {
  2521. dec_mm_counter_fast(mm, MM_FILEPAGES);
  2522. inc_mm_counter_fast(mm, MM_ANONPAGES);
  2523. }
  2524. } else
  2525. inc_mm_counter_fast(mm, MM_ANONPAGES);
  2526. flush_cache_page(vma, address, pte_pfn(orig_pte));
  2527. entry = mk_pte(new_page, vma->vm_page_prot);
  2528. entry = maybe_mkwrite(pte_mkdirty(entry), vma);
  2529. /*
  2530. * Clear the pte entry and flush it first, before updating the
  2531. * pte with the new entry. This will avoid a race condition
  2532. * seen in the presence of one thread doing SMC and another
  2533. * thread doing COW.
  2534. */
  2535. ptep_clear_flush(vma, address, page_table);
  2536. page_add_new_anon_rmap(new_page, vma, address);
  2537. /*
  2538. * We call the notify macro here because, when using secondary
  2539. * mmu page tables (such as kvm shadow page tables), we want the
  2540. * new page to be mapped directly into the secondary page table.
  2541. */
  2542. set_pte_at_notify(mm, address, page_table, entry);
  2543. update_mmu_cache(vma, address, page_table);
  2544. if (old_page) {
  2545. /*
  2546. * Only after switching the pte to the new page may
  2547. * we remove the mapcount here. Otherwise another
  2548. * process may come and find the rmap count decremented
  2549. * before the pte is switched to the new page, and
  2550. * "reuse" the old page writing into it while our pte
  2551. * here still points into it and can be read by other
  2552. * threads.
  2553. *
  2554. * The critical issue is to order this
  2555. * page_remove_rmap with the ptp_clear_flush above.
  2556. * Those stores are ordered by (if nothing else,)
  2557. * the barrier present in the atomic_add_negative
  2558. * in page_remove_rmap.
  2559. *
  2560. * Then the TLB flush in ptep_clear_flush ensures that
  2561. * no process can access the old page before the
  2562. * decremented mapcount is visible. And the old page
  2563. * cannot be reused until after the decremented
  2564. * mapcount is visible. So transitively, TLBs to
  2565. * old page will be flushed before it can be reused.
  2566. */
  2567. page_remove_rmap(old_page);
  2568. }
  2569. /* Free the old page.. */
  2570. new_page = old_page;
  2571. ret |= VM_FAULT_WRITE;
  2572. } else
  2573. mem_cgroup_uncharge_page(new_page);
  2574. if (new_page)
  2575. page_cache_release(new_page);
  2576. unlock:
  2577. pte_unmap_unlock(page_table, ptl);
  2578. if (mmun_end > mmun_start)
  2579. mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
  2580. if (old_page) {
  2581. /*
  2582. * Don't let another task, with possibly unlocked vma,
  2583. * keep the mlocked page.
  2584. */
  2585. if ((ret & VM_FAULT_WRITE) && (vma->vm_flags & VM_LOCKED)) {
  2586. lock_page(old_page); /* LRU manipulation */
  2587. munlock_vma_page(old_page);
  2588. unlock_page(old_page);
  2589. }
  2590. page_cache_release(old_page);
  2591. }
  2592. return ret;
  2593. oom_free_new:
  2594. page_cache_release(new_page);
  2595. oom:
  2596. if (old_page)
  2597. page_cache_release(old_page);
  2598. return VM_FAULT_OOM;
  2599. unwritable_page:
  2600. page_cache_release(old_page);
  2601. return ret;
  2602. }
  2603. static void unmap_mapping_range_vma(struct vm_area_struct *vma,
  2604. unsigned long start_addr, unsigned long end_addr,
  2605. struct zap_details *details)
  2606. {
  2607. zap_page_range_single(vma, start_addr, end_addr - start_addr, details);
  2608. }
  2609. static inline void unmap_mapping_range_tree(struct rb_root *root,
  2610. struct zap_details *details)
  2611. {
  2612. struct vm_area_struct *vma;
  2613. pgoff_t vba, vea, zba, zea;
  2614. vma_interval_tree_foreach(vma, root,
  2615. details->first_index, details->last_index) {
  2616. vba = vma->vm_pgoff;
  2617. vea = vba + vma_pages(vma) - 1;
  2618. /* Assume for now that PAGE_CACHE_SHIFT == PAGE_SHIFT */
  2619. zba = details->first_index;
  2620. if (zba < vba)
  2621. zba = vba;
  2622. zea = details->last_index;
  2623. if (zea > vea)
  2624. zea = vea;
  2625. unmap_mapping_range_vma(vma,
  2626. ((zba - vba) << PAGE_SHIFT) + vma->vm_start,
  2627. ((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start,
  2628. details);
  2629. }
  2630. }
  2631. static inline void unmap_mapping_range_list(struct list_head *head,
  2632. struct zap_details *details)
  2633. {
  2634. struct vm_area_struct *vma;
  2635. /*
  2636. * In nonlinear VMAs there is no correspondence between virtual address
  2637. * offset and file offset. So we must perform an exhaustive search
  2638. * across *all* the pages in each nonlinear VMA, not just the pages
  2639. * whose virtual address lies outside the file truncation point.
  2640. */
  2641. list_for_each_entry(vma, head, shared.nonlinear) {
  2642. details->nonlinear_vma = vma;
  2643. unmap_mapping_range_vma(vma, vma->vm_start, vma->vm_end, details);
  2644. }
  2645. }
  2646. /**
  2647. * unmap_mapping_range - unmap the portion of all mmaps in the specified address_space corresponding to the specified page range in the underlying file.
  2648. * @mapping: the address space containing mmaps to be unmapped.
  2649. * @holebegin: byte in first page to unmap, relative to the start of
  2650. * the underlying file. This will be rounded down to a PAGE_SIZE
  2651. * boundary. Note that this is different from truncate_pagecache(), which
  2652. * must keep the partial page. In contrast, we must get rid of
  2653. * partial pages.
  2654. * @holelen: size of prospective hole in bytes. This will be rounded
  2655. * up to a PAGE_SIZE boundary. A holelen of zero truncates to the
  2656. * end of the file.
  2657. * @even_cows: 1 when truncating a file, unmap even private COWed pages;
  2658. * but 0 when invalidating pagecache, don't throw away private data.
  2659. */
  2660. void unmap_mapping_range(struct address_space *mapping,
  2661. loff_t const holebegin, loff_t const holelen, int even_cows)
  2662. {
  2663. struct zap_details details;
  2664. pgoff_t hba = holebegin >> PAGE_SHIFT;
  2665. pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
  2666. /* Check for overflow. */
  2667. if (sizeof(holelen) > sizeof(hlen)) {
  2668. long long holeend =
  2669. (holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
  2670. if (holeend & ~(long long)ULONG_MAX)
  2671. hlen = ULONG_MAX - hba + 1;
  2672. }
  2673. details.check_mapping = even_cows? NULL: mapping;
  2674. details.nonlinear_vma = NULL;
  2675. details.first_index = hba;
  2676. details.last_index = hba + hlen - 1;
  2677. if (details.last_index < details.first_index)
  2678. details.last_index = ULONG_MAX;
  2679. mutex_lock(&mapping->i_mmap_mutex);
  2680. if (unlikely(!RB_EMPTY_ROOT(&mapping->i_mmap)))
  2681. unmap_mapping_range_tree(&mapping->i_mmap, &details);
  2682. if (unlikely(!list_empty(&mapping->i_mmap_nonlinear)))
  2683. unmap_mapping_range_list(&mapping->i_mmap_nonlinear, &details);
  2684. mutex_unlock(&mapping->i_mmap_mutex);
  2685. }
  2686. EXPORT_SYMBOL(unmap_mapping_range);
  2687. /*
  2688. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  2689. * but allow concurrent faults), and pte mapped but not yet locked.
  2690. * We return with mmap_sem still held, but pte unmapped and unlocked.
  2691. */
  2692. static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma,
  2693. unsigned long address, pte_t *page_table, pmd_t *pmd,
  2694. unsigned int flags, pte_t orig_pte)
  2695. {
  2696. spinlock_t *ptl;
  2697. struct page *page, *swapcache;
  2698. swp_entry_t entry;
  2699. pte_t pte;
  2700. int locked;
  2701. struct mem_cgroup *ptr;
  2702. int exclusive = 0;
  2703. int ret = 0;
  2704. if (!pte_unmap_same(mm, pmd, page_table, orig_pte))
  2705. goto out;
  2706. entry = pte_to_swp_entry(orig_pte);
  2707. if (unlikely(non_swap_entry(entry))) {
  2708. if (is_migration_entry(entry)) {
  2709. migration_entry_wait(mm, pmd, address);
  2710. } else if (is_hwpoison_entry(entry)) {
  2711. ret = VM_FAULT_HWPOISON;
  2712. } else {
  2713. print_bad_pte(vma, address, orig_pte, NULL);
  2714. ret = VM_FAULT_SIGBUS;
  2715. }
  2716. goto out;
  2717. }
  2718. delayacct_set_flag(DELAYACCT_PF_SWAPIN);
  2719. page = lookup_swap_cache(entry);
  2720. if (!page) {
  2721. page = swapin_readahead(entry,
  2722. GFP_HIGHUSER_MOVABLE, vma, address);
  2723. if (!page) {
  2724. /*
  2725. * Back out if somebody else faulted in this pte
  2726. * while we released the pte lock.
  2727. */
  2728. page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
  2729. if (likely(pte_same(*page_table, orig_pte)))
  2730. ret = VM_FAULT_OOM;
  2731. delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
  2732. goto unlock;
  2733. }
  2734. /* Had to read the page from swap area: Major fault */
  2735. ret = VM_FAULT_MAJOR;
  2736. count_vm_event(PGMAJFAULT);
  2737. mem_cgroup_count_vm_event(mm, PGMAJFAULT);
  2738. } else if (PageHWPoison(page)) {
  2739. /*
  2740. * hwpoisoned dirty swapcache pages are kept for killing
  2741. * owner processes (which may be unknown at hwpoison time)
  2742. */
  2743. ret = VM_FAULT_HWPOISON;
  2744. delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
  2745. swapcache = page;
  2746. goto out_release;
  2747. }
  2748. swapcache = page;
  2749. locked = lock_page_or_retry(page, mm, flags);
  2750. delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
  2751. if (!locked) {
  2752. ret |= VM_FAULT_RETRY;
  2753. goto out_release;
  2754. }
  2755. /*
  2756. * Make sure try_to_free_swap or reuse_swap_page or swapoff did not
  2757. * release the swapcache from under us. The page pin, and pte_same
  2758. * test below, are not enough to exclude that. Even if it is still
  2759. * swapcache, we need to check that the page's swap has not changed.
  2760. */
  2761. if (unlikely(!PageSwapCache(page) || page_private(page) != entry.val))
  2762. goto out_page;
  2763. page = ksm_might_need_to_copy(page, vma, address);
  2764. if (unlikely(!page)) {
  2765. ret = VM_FAULT_OOM;
  2766. page = swapcache;
  2767. goto out_page;
  2768. }
  2769. if (mem_cgroup_try_charge_swapin(mm, page, GFP_KERNEL, &ptr)) {
  2770. ret = VM_FAULT_OOM;
  2771. goto out_page;
  2772. }
  2773. /*
  2774. * Back out if somebody else already faulted in this pte.
  2775. */
  2776. page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
  2777. if (unlikely(!pte_same(*page_table, orig_pte)))
  2778. goto out_nomap;
  2779. if (unlikely(!PageUptodate(page))) {
  2780. ret = VM_FAULT_SIGBUS;
  2781. goto out_nomap;
  2782. }
  2783. /*
  2784. * The page isn't present yet, go ahead with the fault.
  2785. *
  2786. * Be careful about the sequence of operations here.
  2787. * To get its accounting right, reuse_swap_page() must be called
  2788. * while the page is counted on swap but not yet in mapcount i.e.
  2789. * before page_add_anon_rmap() and swap_free(); try_to_free_swap()
  2790. * must be called after the swap_free(), or it will never succeed.
  2791. * Because delete_from_swap_page() may be called by reuse_swap_page(),
  2792. * mem_cgroup_commit_charge_swapin() may not be able to find swp_entry
  2793. * in page->private. In this case, a record in swap_cgroup is silently
  2794. * discarded at swap_free().
  2795. */
  2796. inc_mm_counter_fast(mm, MM_ANONPAGES);
  2797. dec_mm_counter_fast(mm, MM_SWAPENTS);
  2798. pte = mk_pte(page, vma->vm_page_prot);
  2799. if ((flags & FAULT_FLAG_WRITE) && reuse_swap_page(page)) {
  2800. pte = maybe_mkwrite(pte_mkdirty(pte), vma);
  2801. flags &= ~FAULT_FLAG_WRITE;
  2802. ret |= VM_FAULT_WRITE;
  2803. exclusive = 1;
  2804. }
  2805. flush_icache_page(vma, page);
  2806. set_pte_at(mm, address, page_table, pte);
  2807. if (page == swapcache)
  2808. do_page_add_anon_rmap(page, vma, address, exclusive);
  2809. else /* ksm created a completely new copy */
  2810. page_add_new_anon_rmap(page, vma, address);
  2811. /* It's better to call commit-charge after rmap is established */
  2812. mem_cgroup_commit_charge_swapin(page, ptr);
  2813. swap_free(entry);
  2814. if (vm_swap_full() || (vma->vm_flags & VM_LOCKED) || PageMlocked(page))
  2815. try_to_free_swap(page);
  2816. unlock_page(page);
  2817. if (page != swapcache) {
  2818. /*
  2819. * Hold the lock to avoid the swap entry to be reused
  2820. * until we take the PT lock for the pte_same() check
  2821. * (to avoid false positives from pte_same). For
  2822. * further safety release the lock after the swap_free
  2823. * so that the swap count won't change under a
  2824. * parallel locked swapcache.
  2825. */
  2826. unlock_page(swapcache);
  2827. page_cache_release(swapcache);
  2828. }
  2829. if (flags & FAULT_FLAG_WRITE) {
  2830. ret |= do_wp_page(mm, vma, address, page_table, pmd, ptl, pte);
  2831. if (ret & VM_FAULT_ERROR)
  2832. ret &= VM_FAULT_ERROR;
  2833. goto out;
  2834. }
  2835. /* No need to invalidate - it was non-present before */
  2836. update_mmu_cache(vma, address, page_table);
  2837. unlock:
  2838. pte_unmap_unlock(page_table, ptl);
  2839. out:
  2840. return ret;
  2841. out_nomap:
  2842. mem_cgroup_cancel_charge_swapin(ptr);
  2843. pte_unmap_unlock(page_table, ptl);
  2844. out_page:
  2845. unlock_page(page);
  2846. out_release:
  2847. page_cache_release(page);
  2848. if (page != swapcache) {
  2849. unlock_page(swapcache);
  2850. page_cache_release(swapcache);
  2851. }
  2852. return ret;
  2853. }
  2854. /*
  2855. * This is like a special single-page "expand_{down|up}wards()",
  2856. * except we must first make sure that 'address{-|+}PAGE_SIZE'
  2857. * doesn't hit another vma.
  2858. */
  2859. static inline int check_stack_guard_page(struct vm_area_struct *vma, unsigned long address)
  2860. {
  2861. address &= PAGE_MASK;
  2862. if ((vma->vm_flags & VM_GROWSDOWN) && address == vma->vm_start) {
  2863. struct vm_area_struct *prev = vma->vm_prev;
  2864. /*
  2865. * Is there a mapping abutting this one below?
  2866. *
  2867. * That's only ok if it's the same stack mapping
  2868. * that has gotten split..
  2869. */
  2870. if (prev && prev->vm_end == address)
  2871. return prev->vm_flags & VM_GROWSDOWN ? 0 : -ENOMEM;
  2872. expand_downwards(vma, address - PAGE_SIZE);
  2873. }
  2874. if ((vma->vm_flags & VM_GROWSUP) && address + PAGE_SIZE == vma->vm_end) {
  2875. struct vm_area_struct *next = vma->vm_next;
  2876. /* As VM_GROWSDOWN but s/below/above/ */
  2877. if (next && next->vm_start == address + PAGE_SIZE)
  2878. return next->vm_flags & VM_GROWSUP ? 0 : -ENOMEM;
  2879. expand_upwards(vma, address + PAGE_SIZE);
  2880. }
  2881. return 0;
  2882. }
  2883. /*
  2884. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  2885. * but allow concurrent faults), and pte mapped but not yet locked.
  2886. * We return with mmap_sem still held, but pte unmapped and unlocked.
  2887. */
  2888. static int do_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
  2889. unsigned long address, pte_t *page_table, pmd_t *pmd,
  2890. unsigned int flags)
  2891. {
  2892. struct page *page;
  2893. spinlock_t *ptl;
  2894. pte_t entry;
  2895. pte_unmap(page_table);
  2896. /* Check if we need to add a guard page to the stack */
  2897. if (check_stack_guard_page(vma, address) < 0)
  2898. return VM_FAULT_SIGBUS;
  2899. /* Use the zero-page for reads */
  2900. if (!(flags & FAULT_FLAG_WRITE)) {
  2901. entry = pte_mkspecial(pfn_pte(my_zero_pfn(address),
  2902. vma->vm_page_prot));
  2903. page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
  2904. if (!pte_none(*page_table))
  2905. goto unlock;
  2906. goto setpte;
  2907. }
  2908. /* Allocate our own private page. */
  2909. if (unlikely(anon_vma_prepare(vma)))
  2910. goto oom;
  2911. page = alloc_zeroed_user_highpage_movable(vma, address);
  2912. if (!page)
  2913. goto oom;
  2914. /*
  2915. * The memory barrier inside __SetPageUptodate makes sure that
  2916. * preceeding stores to the page contents become visible before
  2917. * the set_pte_at() write.
  2918. */
  2919. __SetPageUptodate(page);
  2920. if (mem_cgroup_newpage_charge(page, mm, GFP_KERNEL))
  2921. goto oom_free_page;
  2922. entry = mk_pte(page, vma->vm_page_prot);
  2923. if (vma->vm_flags & VM_WRITE)
  2924. entry = pte_mkwrite(pte_mkdirty(entry));
  2925. page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
  2926. if (!pte_none(*page_table))
  2927. goto release;
  2928. inc_mm_counter_fast(mm, MM_ANONPAGES);
  2929. page_add_new_anon_rmap(page, vma, address);
  2930. setpte:
  2931. set_pte_at(mm, address, page_table, entry);
  2932. /* No need to invalidate - it was non-present before */
  2933. update_mmu_cache(vma, address, page_table);
  2934. unlock:
  2935. pte_unmap_unlock(page_table, ptl);
  2936. return 0;
  2937. release:
  2938. mem_cgroup_uncharge_page(page);
  2939. page_cache_release(page);
  2940. goto unlock;
  2941. oom_free_page:
  2942. page_cache_release(page);
  2943. oom:
  2944. return VM_FAULT_OOM;
  2945. }
  2946. /*
  2947. * __do_fault() tries to create a new page mapping. It aggressively
  2948. * tries to share with existing pages, but makes a separate copy if
  2949. * the FAULT_FLAG_WRITE is set in the flags parameter in order to avoid
  2950. * the next page fault.
  2951. *
  2952. * As this is called only for pages that do not currently exist, we
  2953. * do not need to flush old virtual caches or the TLB.
  2954. *
  2955. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  2956. * but allow concurrent faults), and pte neither mapped nor locked.
  2957. * We return with mmap_sem still held, but pte unmapped and unlocked.
  2958. */
  2959. static int __do_fault(struct mm_struct *mm, struct vm_area_struct *vma,
  2960. unsigned long address, pmd_t *pmd,
  2961. pgoff_t pgoff, unsigned int flags, pte_t orig_pte)
  2962. {
  2963. pte_t *page_table;
  2964. spinlock_t *ptl;
  2965. struct page *page;
  2966. struct page *cow_page;
  2967. pte_t entry;
  2968. int anon = 0;
  2969. struct page *dirty_page = NULL;
  2970. struct vm_fault vmf;
  2971. int ret;
  2972. int page_mkwrite = 0;
  2973. /*
  2974. * If we do COW later, allocate page befor taking lock_page()
  2975. * on the file cache page. This will reduce lock holding time.
  2976. */
  2977. if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) {
  2978. if (unlikely(anon_vma_prepare(vma)))
  2979. return VM_FAULT_OOM;
  2980. cow_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address);
  2981. if (!cow_page)
  2982. return VM_FAULT_OOM;
  2983. if (mem_cgroup_newpage_charge(cow_page, mm, GFP_KERNEL)) {
  2984. page_cache_release(cow_page);
  2985. return VM_FAULT_OOM;
  2986. }
  2987. } else
  2988. cow_page = NULL;
  2989. vmf.virtual_address = (void __user *)(address & PAGE_MASK);
  2990. vmf.pgoff = pgoff;
  2991. vmf.flags = flags;
  2992. vmf.page = NULL;
  2993. ret = vma->vm_ops->fault(vma, &vmf);
  2994. if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE |
  2995. VM_FAULT_RETRY)))
  2996. goto uncharge_out;
  2997. if (unlikely(PageHWPoison(vmf.page))) {
  2998. if (ret & VM_FAULT_LOCKED)
  2999. unlock_page(vmf.page);
  3000. ret = VM_FAULT_HWPOISON;
  3001. goto uncharge_out;
  3002. }
  3003. /*
  3004. * For consistency in subsequent calls, make the faulted page always
  3005. * locked.
  3006. */
  3007. if (unlikely(!(ret & VM_FAULT_LOCKED)))
  3008. lock_page(vmf.page);
  3009. else
  3010. VM_BUG_ON(!PageLocked(vmf.page));
  3011. /*
  3012. * Should we do an early C-O-W break?
  3013. */
  3014. page = vmf.page;
  3015. if (flags & FAULT_FLAG_WRITE) {
  3016. if (!(vma->vm_flags & VM_SHARED)) {
  3017. page = cow_page;
  3018. anon = 1;
  3019. copy_user_highpage(page, vmf.page, address, vma);
  3020. __SetPageUptodate(page);
  3021. } else {
  3022. /*
  3023. * If the page will be shareable, see if the backing
  3024. * address space wants to know that the page is about
  3025. * to become writable
  3026. */
  3027. if (vma->vm_ops->page_mkwrite) {
  3028. int tmp;
  3029. unlock_page(page);
  3030. vmf.flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE;
  3031. tmp = vma->vm_ops->page_mkwrite(vma, &vmf);
  3032. if (unlikely(tmp &
  3033. (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) {
  3034. ret = tmp;
  3035. goto unwritable_page;
  3036. }
  3037. if (unlikely(!(tmp & VM_FAULT_LOCKED))) {
  3038. lock_page(page);
  3039. if (!page->mapping) {
  3040. ret = 0; /* retry the fault */
  3041. unlock_page(page);
  3042. goto unwritable_page;
  3043. }
  3044. } else
  3045. VM_BUG_ON(!PageLocked(page));
  3046. page_mkwrite = 1;
  3047. }
  3048. }
  3049. }
  3050. page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
  3051. /*
  3052. * This silly early PAGE_DIRTY setting removes a race
  3053. * due to the bad i386 page protection. But it's valid
  3054. * for other architectures too.
  3055. *
  3056. * Note that if FAULT_FLAG_WRITE is set, we either now have
  3057. * an exclusive copy of the page, or this is a shared mapping,
  3058. * so we can make it writable and dirty to avoid having to
  3059. * handle that later.
  3060. */
  3061. /* Only go through if we didn't race with anybody else... */
  3062. if (likely(pte_same(*page_table, orig_pte))) {
  3063. flush_icache_page(vma, page);
  3064. entry = mk_pte(page, vma->vm_page_prot);
  3065. if (flags & FAULT_FLAG_WRITE)
  3066. entry = maybe_mkwrite(pte_mkdirty(entry), vma);
  3067. if (anon) {
  3068. inc_mm_counter_fast(mm, MM_ANONPAGES);
  3069. page_add_new_anon_rmap(page, vma, address);
  3070. } else {
  3071. inc_mm_counter_fast(mm, MM_FILEPAGES);
  3072. page_add_file_rmap(page);
  3073. if (flags & FAULT_FLAG_WRITE) {
  3074. dirty_page = page;
  3075. get_page(dirty_page);
  3076. }
  3077. }
  3078. set_pte_at(mm, address, page_table, entry);
  3079. /* no need to invalidate: a not-present page won't be cached */
  3080. update_mmu_cache(vma, address, page_table);
  3081. } else {
  3082. if (cow_page)
  3083. mem_cgroup_uncharge_page(cow_page);
  3084. if (anon)
  3085. page_cache_release(page);
  3086. else
  3087. anon = 1; /* no anon but release faulted_page */
  3088. }
  3089. pte_unmap_unlock(page_table, ptl);
  3090. if (dirty_page) {
  3091. struct address_space *mapping = page->mapping;
  3092. int dirtied = 0;
  3093. if (set_page_dirty(dirty_page))
  3094. dirtied = 1;
  3095. unlock_page(dirty_page);
  3096. put_page(dirty_page);
  3097. if ((dirtied || page_mkwrite) && mapping) {
  3098. /*
  3099. * Some device drivers do not set page.mapping but still
  3100. * dirty their pages
  3101. */
  3102. balance_dirty_pages_ratelimited(mapping);
  3103. }
  3104. /* file_update_time outside page_lock */
  3105. if (vma->vm_file && !page_mkwrite)
  3106. file_update_time(vma->vm_file);
  3107. } else {
  3108. unlock_page(vmf.page);
  3109. if (anon)
  3110. page_cache_release(vmf.page);
  3111. }
  3112. return ret;
  3113. unwritable_page:
  3114. page_cache_release(page);
  3115. return ret;
  3116. uncharge_out:
  3117. /* fs's fault handler get error */
  3118. if (cow_page) {
  3119. mem_cgroup_uncharge_page(cow_page);
  3120. page_cache_release(cow_page);
  3121. }
  3122. return ret;
  3123. }
  3124. static int do_linear_fault(struct mm_struct *mm, struct vm_area_struct *vma,
  3125. unsigned long address, pte_t *page_table, pmd_t *pmd,
  3126. unsigned int flags, pte_t orig_pte)
  3127. {
  3128. pgoff_t pgoff = (((address & PAGE_MASK)
  3129. - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
  3130. pte_unmap(page_table);
  3131. return __do_fault(mm, vma, address, pmd, pgoff, flags, orig_pte);
  3132. }
  3133. /*
  3134. * Fault of a previously existing named mapping. Repopulate the pte
  3135. * from the encoded file_pte if possible. This enables swappable
  3136. * nonlinear vmas.
  3137. *
  3138. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  3139. * but allow concurrent faults), and pte mapped but not yet locked.
  3140. * We return with mmap_sem still held, but pte unmapped and unlocked.
  3141. */
  3142. static int do_nonlinear_fault(struct mm_struct *mm, struct vm_area_struct *vma,
  3143. unsigned long address, pte_t *page_table, pmd_t *pmd,
  3144. unsigned int flags, pte_t orig_pte)
  3145. {
  3146. pgoff_t pgoff;
  3147. flags |= FAULT_FLAG_NONLINEAR;
  3148. if (!pte_unmap_same(mm, pmd, page_table, orig_pte))
  3149. return 0;
  3150. if (unlikely(!(vma->vm_flags & VM_NONLINEAR))) {
  3151. /*
  3152. * Page table corrupted: show pte and kill process.
  3153. */
  3154. print_bad_pte(vma, address, orig_pte, NULL);
  3155. return VM_FAULT_SIGBUS;
  3156. }
  3157. pgoff = pte_to_pgoff(orig_pte);
  3158. return __do_fault(mm, vma, address, pmd, pgoff, flags, orig_pte);
  3159. }
  3160. int numa_migrate_prep(struct page *page, struct vm_area_struct *vma,
  3161. unsigned long addr, int current_nid)
  3162. {
  3163. get_page(page);
  3164. count_vm_numa_event(NUMA_HINT_FAULTS);
  3165. if (current_nid == numa_node_id())
  3166. count_vm_numa_event(NUMA_HINT_FAULTS_LOCAL);
  3167. return mpol_misplaced(page, vma, addr);
  3168. }
  3169. int do_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
  3170. unsigned long addr, pte_t pte, pte_t *ptep, pmd_t *pmd)
  3171. {
  3172. struct page *page = NULL;
  3173. spinlock_t *ptl;
  3174. int current_nid = -1;
  3175. int target_nid;
  3176. bool migrated = false;
  3177. /*
  3178. * The "pte" at this point cannot be used safely without
  3179. * validation through pte_unmap_same(). It's of NUMA type but
  3180. * the pfn may be screwed if the read is non atomic.
  3181. *
  3182. * ptep_modify_prot_start is not called as this is clearing
  3183. * the _PAGE_NUMA bit and it is not really expected that there
  3184. * would be concurrent hardware modifications to the PTE.
  3185. */
  3186. ptl = pte_lockptr(mm, pmd);
  3187. spin_lock(ptl);
  3188. if (unlikely(!pte_same(*ptep, pte))) {
  3189. pte_unmap_unlock(ptep, ptl);
  3190. goto out;
  3191. }
  3192. pte = pte_mknonnuma(pte);
  3193. set_pte_at(mm, addr, ptep, pte);
  3194. update_mmu_cache(vma, addr, ptep);
  3195. page = vm_normal_page(vma, addr, pte);
  3196. if (!page) {
  3197. pte_unmap_unlock(ptep, ptl);
  3198. return 0;
  3199. }
  3200. current_nid = page_to_nid(page);
  3201. target_nid = numa_migrate_prep(page, vma, addr, current_nid);
  3202. pte_unmap_unlock(ptep, ptl);
  3203. if (target_nid == -1) {
  3204. /*
  3205. * Account for the fault against the current node if it not
  3206. * being replaced regardless of where the page is located.
  3207. */
  3208. current_nid = numa_node_id();
  3209. put_page(page);
  3210. goto out;
  3211. }
  3212. /* Migrate to the requested node */
  3213. migrated = migrate_misplaced_page(page, target_nid);
  3214. if (migrated)
  3215. current_nid = target_nid;
  3216. out:
  3217. if (current_nid != -1)
  3218. task_numa_fault(current_nid, 1, migrated);
  3219. return 0;
  3220. }
  3221. /* NUMA hinting page fault entry point for regular pmds */
  3222. #ifdef CONFIG_NUMA_BALANCING
  3223. static int do_pmd_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
  3224. unsigned long addr, pmd_t *pmdp)
  3225. {
  3226. pmd_t pmd;
  3227. pte_t *pte, *orig_pte;
  3228. unsigned long _addr = addr & PMD_MASK;
  3229. unsigned long offset;
  3230. spinlock_t *ptl;
  3231. bool numa = false;
  3232. int local_nid = numa_node_id();
  3233. spin_lock(&mm->page_table_lock);
  3234. pmd = *pmdp;
  3235. if (pmd_numa(pmd)) {
  3236. set_pmd_at(mm, _addr, pmdp, pmd_mknonnuma(pmd));
  3237. numa = true;
  3238. }
  3239. spin_unlock(&mm->page_table_lock);
  3240. if (!numa)
  3241. return 0;
  3242. /* we're in a page fault so some vma must be in the range */
  3243. BUG_ON(!vma);
  3244. BUG_ON(vma->vm_start >= _addr + PMD_SIZE);
  3245. offset = max(_addr, vma->vm_start) & ~PMD_MASK;
  3246. VM_BUG_ON(offset >= PMD_SIZE);
  3247. orig_pte = pte = pte_offset_map_lock(mm, pmdp, _addr, &ptl);
  3248. pte += offset >> PAGE_SHIFT;
  3249. for (addr = _addr + offset; addr < _addr + PMD_SIZE; pte++, addr += PAGE_SIZE) {
  3250. pte_t pteval = *pte;
  3251. struct page *page;
  3252. int curr_nid = local_nid;
  3253. int target_nid;
  3254. bool migrated;
  3255. if (!pte_present(pteval))
  3256. continue;
  3257. if (!pte_numa(pteval))
  3258. continue;
  3259. if (addr >= vma->vm_end) {
  3260. vma = find_vma(mm, addr);
  3261. /* there's a pte present so there must be a vma */
  3262. BUG_ON(!vma);
  3263. BUG_ON(addr < vma->vm_start);
  3264. }
  3265. if (pte_numa(pteval)) {
  3266. pteval = pte_mknonnuma(pteval);
  3267. set_pte_at(mm, addr, pte, pteval);
  3268. }
  3269. page = vm_normal_page(vma, addr, pteval);
  3270. if (unlikely(!page))
  3271. continue;
  3272. /* only check non-shared pages */
  3273. if (unlikely(page_mapcount(page) != 1))
  3274. continue;
  3275. /*
  3276. * Note that the NUMA fault is later accounted to either
  3277. * the node that is currently running or where the page is
  3278. * migrated to.
  3279. */
  3280. curr_nid = local_nid;
  3281. target_nid = numa_migrate_prep(page, vma, addr,
  3282. page_to_nid(page));
  3283. if (target_nid == -1) {
  3284. put_page(page);
  3285. continue;
  3286. }
  3287. /* Migrate to the requested node */
  3288. pte_unmap_unlock(pte, ptl);
  3289. migrated = migrate_misplaced_page(page, target_nid);
  3290. if (migrated)
  3291. curr_nid = target_nid;
  3292. task_numa_fault(curr_nid, 1, migrated);
  3293. pte = pte_offset_map_lock(mm, pmdp, addr, &ptl);
  3294. }
  3295. pte_unmap_unlock(orig_pte, ptl);
  3296. return 0;
  3297. }
  3298. #else
  3299. static int do_pmd_numa_page(struct mm_struct *mm, struct vm_area_struct *vma,
  3300. unsigned long addr, pmd_t *pmdp)
  3301. {
  3302. BUG();
  3303. return 0;
  3304. }
  3305. #endif /* CONFIG_NUMA_BALANCING */
  3306. /*
  3307. * These routines also need to handle stuff like marking pages dirty
  3308. * and/or accessed for architectures that don't do it in hardware (most
  3309. * RISC architectures). The early dirtying is also good on the i386.
  3310. *
  3311. * There is also a hook called "update_mmu_cache()" that architectures
  3312. * with external mmu caches can use to update those (ie the Sparc or
  3313. * PowerPC hashed page tables that act as extended TLBs).
  3314. *
  3315. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  3316. * but allow concurrent faults), and pte mapped but not yet locked.
  3317. * We return with mmap_sem still held, but pte unmapped and unlocked.
  3318. */
  3319. int handle_pte_fault(struct mm_struct *mm,
  3320. struct vm_area_struct *vma, unsigned long address,
  3321. pte_t *pte, pmd_t *pmd, unsigned int flags)
  3322. {
  3323. pte_t entry;
  3324. spinlock_t *ptl;
  3325. entry = *pte;
  3326. if (!pte_present(entry)) {
  3327. if (pte_none(entry)) {
  3328. if (vma->vm_ops) {
  3329. if (likely(vma->vm_ops->fault))
  3330. return do_linear_fault(mm, vma, address,
  3331. pte, pmd, flags, entry);
  3332. }
  3333. return do_anonymous_page(mm, vma, address,
  3334. pte, pmd, flags);
  3335. }
  3336. if (pte_file(entry))
  3337. return do_nonlinear_fault(mm, vma, address,
  3338. pte, pmd, flags, entry);
  3339. return do_swap_page(mm, vma, address,
  3340. pte, pmd, flags, entry);
  3341. }
  3342. if (pte_numa(entry))
  3343. return do_numa_page(mm, vma, address, entry, pte, pmd);
  3344. ptl = pte_lockptr(mm, pmd);
  3345. spin_lock(ptl);
  3346. if (unlikely(!pte_same(*pte, entry)))
  3347. goto unlock;
  3348. if (flags & FAULT_FLAG_WRITE) {
  3349. if (!pte_write(entry))
  3350. return do_wp_page(mm, vma, address,
  3351. pte, pmd, ptl, entry);
  3352. entry = pte_mkdirty(entry);
  3353. }
  3354. entry = pte_mkyoung(entry);
  3355. if (ptep_set_access_flags(vma, address, pte, entry, flags & FAULT_FLAG_WRITE)) {
  3356. update_mmu_cache(vma, address, pte);
  3357. } else {
  3358. /*
  3359. * This is needed only for protection faults but the arch code
  3360. * is not yet telling us if this is a protection fault or not.
  3361. * This still avoids useless tlb flushes for .text page faults
  3362. * with threads.
  3363. */
  3364. if (flags & FAULT_FLAG_WRITE)
  3365. flush_tlb_fix_spurious_fault(vma, address);
  3366. }
  3367. unlock:
  3368. pte_unmap_unlock(pte, ptl);
  3369. return 0;
  3370. }
  3371. /*
  3372. * By the time we get here, we already hold the mm semaphore
  3373. */
  3374. int handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma,
  3375. unsigned long address, unsigned int flags)
  3376. {
  3377. pgd_t *pgd;
  3378. pud_t *pud;
  3379. pmd_t *pmd;
  3380. pte_t *pte;
  3381. __set_current_state(TASK_RUNNING);
  3382. count_vm_event(PGFAULT);
  3383. mem_cgroup_count_vm_event(mm, PGFAULT);
  3384. /* do counter updates before entering really critical section. */
  3385. check_sync_rss_stat(current);
  3386. if (unlikely(is_vm_hugetlb_page(vma)))
  3387. return hugetlb_fault(mm, vma, address, flags);
  3388. retry:
  3389. pgd = pgd_offset(mm, address);
  3390. pud = pud_alloc(mm, pgd, address);
  3391. if (!pud)
  3392. return VM_FAULT_OOM;
  3393. pmd = pmd_alloc(mm, pud, address);
  3394. if (!pmd)
  3395. return VM_FAULT_OOM;
  3396. if (pmd_none(*pmd) && transparent_hugepage_enabled(vma)) {
  3397. if (!vma->vm_ops)
  3398. return do_huge_pmd_anonymous_page(mm, vma, address,
  3399. pmd, flags);
  3400. } else {
  3401. pmd_t orig_pmd = *pmd;
  3402. int ret;
  3403. barrier();
  3404. if (pmd_trans_huge(orig_pmd)) {
  3405. unsigned int dirty = flags & FAULT_FLAG_WRITE;
  3406. /*
  3407. * If the pmd is splitting, return and retry the
  3408. * the fault. Alternative: wait until the split
  3409. * is done, and goto retry.
  3410. */
  3411. if (pmd_trans_splitting(orig_pmd))
  3412. return 0;
  3413. if (pmd_numa(orig_pmd))
  3414. return do_huge_pmd_numa_page(mm, vma, address,
  3415. orig_pmd, pmd);
  3416. if (dirty && !pmd_write(orig_pmd)) {
  3417. ret = do_huge_pmd_wp_page(mm, vma, address, pmd,
  3418. orig_pmd);
  3419. /*
  3420. * If COW results in an oom, the huge pmd will
  3421. * have been split, so retry the fault on the
  3422. * pte for a smaller charge.
  3423. */
  3424. if (unlikely(ret & VM_FAULT_OOM))
  3425. goto retry;
  3426. return ret;
  3427. } else {
  3428. huge_pmd_set_accessed(mm, vma, address, pmd,
  3429. orig_pmd, dirty);
  3430. }
  3431. return 0;
  3432. }
  3433. }
  3434. if (pmd_numa(*pmd))
  3435. return do_pmd_numa_page(mm, vma, address, pmd);
  3436. /*
  3437. * Use __pte_alloc instead of pte_alloc_map, because we can't
  3438. * run pte_offset_map on the pmd, if an huge pmd could
  3439. * materialize from under us from a different thread.
  3440. */
  3441. if (unlikely(pmd_none(*pmd)) &&
  3442. unlikely(__pte_alloc(mm, vma, pmd, address)))
  3443. return VM_FAULT_OOM;
  3444. /* if an huge pmd materialized from under us just retry later */
  3445. if (unlikely(pmd_trans_huge(*pmd)))
  3446. return 0;
  3447. /*
  3448. * A regular pmd is established and it can't morph into a huge pmd
  3449. * from under us anymore at this point because we hold the mmap_sem
  3450. * read mode and khugepaged takes it in write mode. So now it's
  3451. * safe to run pte_offset_map().
  3452. */
  3453. pte = pte_offset_map(pmd, address);
  3454. return handle_pte_fault(mm, vma, address, pte, pmd, flags);
  3455. }
  3456. #ifndef __PAGETABLE_PUD_FOLDED
  3457. /*
  3458. * Allocate page upper directory.
  3459. * We've already handled the fast-path in-line.
  3460. */
  3461. int __pud_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
  3462. {
  3463. pud_t *new = pud_alloc_one(mm, address);
  3464. if (!new)
  3465. return -ENOMEM;
  3466. smp_wmb(); /* See comment in __pte_alloc */
  3467. spin_lock(&mm->page_table_lock);
  3468. if (pgd_present(*pgd)) /* Another has populated it */
  3469. pud_free(mm, new);
  3470. else
  3471. pgd_populate(mm, pgd, new);
  3472. spin_unlock(&mm->page_table_lock);
  3473. return 0;
  3474. }
  3475. #endif /* __PAGETABLE_PUD_FOLDED */
  3476. #ifndef __PAGETABLE_PMD_FOLDED
  3477. /*
  3478. * Allocate page middle directory.
  3479. * We've already handled the fast-path in-line.
  3480. */
  3481. int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address)
  3482. {
  3483. pmd_t *new = pmd_alloc_one(mm, address);
  3484. if (!new)
  3485. return -ENOMEM;
  3486. smp_wmb(); /* See comment in __pte_alloc */
  3487. spin_lock(&mm->page_table_lock);
  3488. #ifndef __ARCH_HAS_4LEVEL_HACK
  3489. if (pud_present(*pud)) /* Another has populated it */
  3490. pmd_free(mm, new);
  3491. else
  3492. pud_populate(mm, pud, new);
  3493. #else
  3494. if (pgd_present(*pud)) /* Another has populated it */
  3495. pmd_free(mm, new);
  3496. else
  3497. pgd_populate(mm, pud, new);
  3498. #endif /* __ARCH_HAS_4LEVEL_HACK */
  3499. spin_unlock(&mm->page_table_lock);
  3500. return 0;
  3501. }
  3502. #endif /* __PAGETABLE_PMD_FOLDED */
  3503. #if !defined(__HAVE_ARCH_GATE_AREA)
  3504. #if defined(AT_SYSINFO_EHDR)
  3505. static struct vm_area_struct gate_vma;
  3506. static int __init gate_vma_init(void)
  3507. {
  3508. gate_vma.vm_mm = NULL;
  3509. gate_vma.vm_start = FIXADDR_USER_START;
  3510. gate_vma.vm_end = FIXADDR_USER_END;
  3511. gate_vma.vm_flags = VM_READ | VM_MAYREAD | VM_EXEC | VM_MAYEXEC;
  3512. gate_vma.vm_page_prot = __P101;
  3513. return 0;
  3514. }
  3515. __initcall(gate_vma_init);
  3516. #endif
  3517. struct vm_area_struct *get_gate_vma(struct mm_struct *mm)
  3518. {
  3519. #ifdef AT_SYSINFO_EHDR
  3520. return &gate_vma;
  3521. #else
  3522. return NULL;
  3523. #endif
  3524. }
  3525. int in_gate_area_no_mm(unsigned long addr)
  3526. {
  3527. #ifdef AT_SYSINFO_EHDR
  3528. if ((addr >= FIXADDR_USER_START) && (addr < FIXADDR_USER_END))
  3529. return 1;
  3530. #endif
  3531. return 0;
  3532. }
  3533. #endif /* __HAVE_ARCH_GATE_AREA */
  3534. static int __follow_pte(struct mm_struct *mm, unsigned long address,
  3535. pte_t **ptepp, spinlock_t **ptlp)
  3536. {
  3537. pgd_t *pgd;
  3538. pud_t *pud;
  3539. pmd_t *pmd;
  3540. pte_t *ptep;
  3541. pgd = pgd_offset(mm, address);
  3542. if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
  3543. goto out;
  3544. pud = pud_offset(pgd, address);
  3545. if (pud_none(*pud) || unlikely(pud_bad(*pud)))
  3546. goto out;
  3547. pmd = pmd_offset(pud, address);
  3548. VM_BUG_ON(pmd_trans_huge(*pmd));
  3549. if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
  3550. goto out;
  3551. /* We cannot handle huge page PFN maps. Luckily they don't exist. */
  3552. if (pmd_huge(*pmd))
  3553. goto out;
  3554. ptep = pte_offset_map_lock(mm, pmd, address, ptlp);
  3555. if (!ptep)
  3556. goto out;
  3557. if (!pte_present(*ptep))
  3558. goto unlock;
  3559. *ptepp = ptep;
  3560. return 0;
  3561. unlock:
  3562. pte_unmap_unlock(ptep, *ptlp);
  3563. out:
  3564. return -EINVAL;
  3565. }
  3566. static inline int follow_pte(struct mm_struct *mm, unsigned long address,
  3567. pte_t **ptepp, spinlock_t **ptlp)
  3568. {
  3569. int res;
  3570. /* (void) is needed to make gcc happy */
  3571. (void) __cond_lock(*ptlp,
  3572. !(res = __follow_pte(mm, address, ptepp, ptlp)));
  3573. return res;
  3574. }
  3575. /**
  3576. * follow_pfn - look up PFN at a user virtual address
  3577. * @vma: memory mapping
  3578. * @address: user virtual address
  3579. * @pfn: location to store found PFN
  3580. *
  3581. * Only IO mappings and raw PFN mappings are allowed.
  3582. *
  3583. * Returns zero and the pfn at @pfn on success, -ve otherwise.
  3584. */
  3585. int follow_pfn(struct vm_area_struct *vma, unsigned long address,
  3586. unsigned long *pfn)
  3587. {
  3588. int ret = -EINVAL;
  3589. spinlock_t *ptl;
  3590. pte_t *ptep;
  3591. if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
  3592. return ret;
  3593. ret = follow_pte(vma->vm_mm, address, &ptep, &ptl);
  3594. if (ret)
  3595. return ret;
  3596. *pfn = pte_pfn(*ptep);
  3597. pte_unmap_unlock(ptep, ptl);
  3598. return 0;
  3599. }
  3600. EXPORT_SYMBOL(follow_pfn);
  3601. #ifdef CONFIG_HAVE_IOREMAP_PROT
  3602. int follow_phys(struct vm_area_struct *vma,
  3603. unsigned long address, unsigned int flags,
  3604. unsigned long *prot, resource_size_t *phys)
  3605. {
  3606. int ret = -EINVAL;
  3607. pte_t *ptep, pte;
  3608. spinlock_t *ptl;
  3609. if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
  3610. goto out;
  3611. if (follow_pte(vma->vm_mm, address, &ptep, &ptl))
  3612. goto out;
  3613. pte = *ptep;
  3614. if ((flags & FOLL_WRITE) && !pte_write(pte))
  3615. goto unlock;
  3616. *prot = pgprot_val(pte_pgprot(pte));
  3617. *phys = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT;
  3618. ret = 0;
  3619. unlock:
  3620. pte_unmap_unlock(ptep, ptl);
  3621. out:
  3622. return ret;
  3623. }
  3624. int generic_access_phys(struct vm_area_struct *vma, unsigned long addr,
  3625. void *buf, int len, int write)
  3626. {
  3627. resource_size_t phys_addr;
  3628. unsigned long prot = 0;
  3629. void __iomem *maddr;
  3630. int offset = addr & (PAGE_SIZE-1);
  3631. if (follow_phys(vma, addr, write, &prot, &phys_addr))
  3632. return -EINVAL;
  3633. maddr = ioremap_prot(phys_addr, PAGE_SIZE, prot);
  3634. if (write)
  3635. memcpy_toio(maddr + offset, buf, len);
  3636. else
  3637. memcpy_fromio(buf, maddr + offset, len);
  3638. iounmap(maddr);
  3639. return len;
  3640. }
  3641. #endif
  3642. /*
  3643. * Access another process' address space as given in mm. If non-NULL, use the
  3644. * given task for page fault accounting.
  3645. */
  3646. static int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm,
  3647. unsigned long addr, void *buf, int len, int write)
  3648. {
  3649. struct vm_area_struct *vma;
  3650. void *old_buf = buf;
  3651. down_read(&mm->mmap_sem);
  3652. /* ignore errors, just check how much was successfully transferred */
  3653. while (len) {
  3654. int bytes, ret, offset;
  3655. void *maddr;
  3656. struct page *page = NULL;
  3657. ret = get_user_pages(tsk, mm, addr, 1,
  3658. write, 1, &page, &vma);
  3659. if (ret <= 0) {
  3660. /*
  3661. * Check if this is a VM_IO | VM_PFNMAP VMA, which
  3662. * we can access using slightly different code.
  3663. */
  3664. #ifdef CONFIG_HAVE_IOREMAP_PROT
  3665. vma = find_vma(mm, addr);
  3666. if (!vma || vma->vm_start > addr)
  3667. break;
  3668. if (vma->vm_ops && vma->vm_ops->access)
  3669. ret = vma->vm_ops->access(vma, addr, buf,
  3670. len, write);
  3671. if (ret <= 0)
  3672. #endif
  3673. break;
  3674. bytes = ret;
  3675. } else {
  3676. bytes = len;
  3677. offset = addr & (PAGE_SIZE-1);
  3678. if (bytes > PAGE_SIZE-offset)
  3679. bytes = PAGE_SIZE-offset;
  3680. maddr = kmap(page);
  3681. if (write) {
  3682. copy_to_user_page(vma, page, addr,
  3683. maddr + offset, buf, bytes);
  3684. set_page_dirty_lock(page);
  3685. } else {
  3686. copy_from_user_page(vma, page, addr,
  3687. buf, maddr + offset, bytes);
  3688. }
  3689. kunmap(page);
  3690. page_cache_release(page);
  3691. }
  3692. len -= bytes;
  3693. buf += bytes;
  3694. addr += bytes;
  3695. }
  3696. up_read(&mm->mmap_sem);
  3697. return buf - old_buf;
  3698. }
  3699. /**
  3700. * access_remote_vm - access another process' address space
  3701. * @mm: the mm_struct of the target address space
  3702. * @addr: start address to access
  3703. * @buf: source or destination buffer
  3704. * @len: number of bytes to transfer
  3705. * @write: whether the access is a write
  3706. *
  3707. * The caller must hold a reference on @mm.
  3708. */
  3709. int access_remote_vm(struct mm_struct *mm, unsigned long addr,
  3710. void *buf, int len, int write)
  3711. {
  3712. return __access_remote_vm(NULL, mm, addr, buf, len, write);
  3713. }
  3714. /*
  3715. * Access another process' address space.
  3716. * Source/target buffer must be kernel space,
  3717. * Do not walk the page table directly, use get_user_pages
  3718. */
  3719. int access_process_vm(struct task_struct *tsk, unsigned long addr,
  3720. void *buf, int len, int write)
  3721. {
  3722. struct mm_struct *mm;
  3723. int ret;
  3724. mm = get_task_mm(tsk);
  3725. if (!mm)
  3726. return 0;
  3727. ret = __access_remote_vm(tsk, mm, addr, buf, len, write);
  3728. mmput(mm);
  3729. return ret;
  3730. }
  3731. /*
  3732. * Print the name of a VMA.
  3733. */
  3734. void print_vma_addr(char *prefix, unsigned long ip)
  3735. {
  3736. struct mm_struct *mm = current->mm;
  3737. struct vm_area_struct *vma;
  3738. /*
  3739. * Do not print if we are in atomic
  3740. * contexts (in exception stacks, etc.):
  3741. */
  3742. if (preempt_count())
  3743. return;
  3744. down_read(&mm->mmap_sem);
  3745. vma = find_vma(mm, ip);
  3746. if (vma && vma->vm_file) {
  3747. struct file *f = vma->vm_file;
  3748. char *buf = (char *)__get_free_page(GFP_KERNEL);
  3749. if (buf) {
  3750. char *p;
  3751. p = d_path(&f->f_path, buf, PAGE_SIZE);
  3752. if (IS_ERR(p))
  3753. p = "?";
  3754. printk("%s%s[%lx+%lx]", prefix, kbasename(p),
  3755. vma->vm_start,
  3756. vma->vm_end - vma->vm_start);
  3757. free_page((unsigned long)buf);
  3758. }
  3759. }
  3760. up_read(&mm->mmap_sem);
  3761. }
  3762. #if defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP)
  3763. void might_fault(void)
  3764. {
  3765. /*
  3766. * Some code (nfs/sunrpc) uses socket ops on kernel memory while
  3767. * holding the mmap_sem, this is safe because kernel memory doesn't
  3768. * get paged out, therefore we'll never actually fault, and the
  3769. * below annotations will generate false positives.
  3770. */
  3771. if (segment_eq(get_fs(), KERNEL_DS))
  3772. return;
  3773. /*
  3774. * it would be nicer only to annotate paths which are not under
  3775. * pagefault_disable, however that requires a larger audit and
  3776. * providing helpers like get_user_atomic.
  3777. */
  3778. if (in_atomic())
  3779. return;
  3780. __might_sleep(__FILE__, __LINE__, 0);
  3781. if (current->mm)
  3782. might_lock_read(&current->mm->mmap_sem);
  3783. }
  3784. EXPORT_SYMBOL(might_fault);
  3785. #endif
  3786. #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS)
  3787. static void clear_gigantic_page(struct page *page,
  3788. unsigned long addr,
  3789. unsigned int pages_per_huge_page)
  3790. {
  3791. int i;
  3792. struct page *p = page;
  3793. might_sleep();
  3794. for (i = 0; i < pages_per_huge_page;
  3795. i++, p = mem_map_next(p, page, i)) {
  3796. cond_resched();
  3797. clear_user_highpage(p, addr + i * PAGE_SIZE);
  3798. }
  3799. }
  3800. void clear_huge_page(struct page *page,
  3801. unsigned long addr, unsigned int pages_per_huge_page)
  3802. {
  3803. int i;
  3804. if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) {
  3805. clear_gigantic_page(page, addr, pages_per_huge_page);
  3806. return;
  3807. }
  3808. might_sleep();
  3809. for (i = 0; i < pages_per_huge_page; i++) {
  3810. cond_resched();
  3811. clear_user_highpage(page + i, addr + i * PAGE_SIZE);
  3812. }
  3813. }
  3814. static void copy_user_gigantic_page(struct page *dst, struct page *src,
  3815. unsigned long addr,
  3816. struct vm_area_struct *vma,
  3817. unsigned int pages_per_huge_page)
  3818. {
  3819. int i;
  3820. struct page *dst_base = dst;
  3821. struct page *src_base = src;
  3822. for (i = 0; i < pages_per_huge_page; ) {
  3823. cond_resched();
  3824. copy_user_highpage(dst, src, addr + i*PAGE_SIZE, vma);
  3825. i++;
  3826. dst = mem_map_next(dst, dst_base, i);
  3827. src = mem_map_next(src, src_base, i);
  3828. }
  3829. }
  3830. void copy_user_huge_page(struct page *dst, struct page *src,
  3831. unsigned long addr, struct vm_area_struct *vma,
  3832. unsigned int pages_per_huge_page)
  3833. {
  3834. int i;
  3835. if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) {
  3836. copy_user_gigantic_page(dst, src, addr, vma,
  3837. pages_per_huge_page);
  3838. return;
  3839. }
  3840. might_sleep();
  3841. for (i = 0; i < pages_per_huge_page; i++) {
  3842. cond_resched();
  3843. copy_user_highpage(dst + i, src + i, addr + i*PAGE_SIZE, vma);
  3844. }
  3845. }
  3846. #endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLBFS */