memory.c 123 KB

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