memory.c 108 KB

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