memory.c 127 KB

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