memory.c 122 KB

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