memory.c 116 KB

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