memory.c 113 KB

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