memory.c 109 KB

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