memory.c 116 KB

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