memory.c 120 KB

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