memory.c 109 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053
  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 bool 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. unsigned long mmun_start; /* For mmu_notifiers */
  915. unsigned long mmun_end; /* For mmu_notifiers */
  916. bool is_cow;
  917. int ret;
  918. /*
  919. * Don't copy ptes where a page fault will fill them correctly.
  920. * Fork becomes much lighter when there are big shared or private
  921. * readonly mappings. The tradeoff is that copy_page_range is more
  922. * efficient than faulting.
  923. */
  924. if (!(vma->vm_flags & (VM_HUGETLB | VM_NONLINEAR |
  925. VM_PFNMAP | VM_MIXEDMAP))) {
  926. if (!vma->anon_vma)
  927. return 0;
  928. }
  929. if (is_vm_hugetlb_page(vma))
  930. return copy_hugetlb_page_range(dst_mm, src_mm, vma);
  931. if (unlikely(vma->vm_flags & VM_PFNMAP)) {
  932. /*
  933. * We do not free on error cases below as remove_vma
  934. * gets called on error from higher level routine
  935. */
  936. ret = track_pfn_copy(vma);
  937. if (ret)
  938. return ret;
  939. }
  940. /*
  941. * We need to invalidate the secondary MMU mappings only when
  942. * there could be a permission downgrade on the ptes of the
  943. * parent mm. And a permission downgrade will only happen if
  944. * is_cow_mapping() returns true.
  945. */
  946. is_cow = is_cow_mapping(vma->vm_flags);
  947. mmun_start = addr;
  948. mmun_end = end;
  949. if (is_cow)
  950. mmu_notifier_invalidate_range_start(src_mm, mmun_start,
  951. mmun_end);
  952. ret = 0;
  953. dst_pgd = pgd_offset(dst_mm, addr);
  954. src_pgd = pgd_offset(src_mm, addr);
  955. do {
  956. next = pgd_addr_end(addr, end);
  957. if (pgd_none_or_clear_bad(src_pgd))
  958. continue;
  959. if (unlikely(copy_pud_range(dst_mm, src_mm, dst_pgd, src_pgd,
  960. vma, addr, next))) {
  961. ret = -ENOMEM;
  962. break;
  963. }
  964. } while (dst_pgd++, src_pgd++, addr = next, addr != end);
  965. if (is_cow)
  966. mmu_notifier_invalidate_range_end(src_mm, mmun_start, mmun_end);
  967. return ret;
  968. }
  969. static unsigned long zap_pte_range(struct mmu_gather *tlb,
  970. struct vm_area_struct *vma, pmd_t *pmd,
  971. unsigned long addr, unsigned long end,
  972. struct zap_details *details)
  973. {
  974. struct mm_struct *mm = tlb->mm;
  975. int force_flush = 0;
  976. int rss[NR_MM_COUNTERS];
  977. spinlock_t *ptl;
  978. pte_t *start_pte;
  979. pte_t *pte;
  980. again:
  981. init_rss_vec(rss);
  982. start_pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
  983. pte = start_pte;
  984. arch_enter_lazy_mmu_mode();
  985. do {
  986. pte_t ptent = *pte;
  987. if (pte_none(ptent)) {
  988. continue;
  989. }
  990. if (pte_present(ptent)) {
  991. struct page *page;
  992. page = vm_normal_page(vma, addr, ptent);
  993. if (unlikely(details) && page) {
  994. /*
  995. * unmap_shared_mapping_pages() wants to
  996. * invalidate cache without truncating:
  997. * unmap shared but keep private pages.
  998. */
  999. if (details->check_mapping &&
  1000. details->check_mapping != page->mapping)
  1001. continue;
  1002. /*
  1003. * Each page->index must be checked when
  1004. * invalidating or truncating nonlinear.
  1005. */
  1006. if (details->nonlinear_vma &&
  1007. (page->index < details->first_index ||
  1008. page->index > details->last_index))
  1009. continue;
  1010. }
  1011. ptent = ptep_get_and_clear_full(mm, addr, pte,
  1012. tlb->fullmm);
  1013. tlb_remove_tlb_entry(tlb, pte, addr);
  1014. if (unlikely(!page))
  1015. continue;
  1016. if (unlikely(details) && details->nonlinear_vma
  1017. && linear_page_index(details->nonlinear_vma,
  1018. addr) != page->index)
  1019. set_pte_at(mm, addr, pte,
  1020. pgoff_to_pte(page->index));
  1021. if (PageAnon(page))
  1022. rss[MM_ANONPAGES]--;
  1023. else {
  1024. if (pte_dirty(ptent))
  1025. set_page_dirty(page);
  1026. if (pte_young(ptent) &&
  1027. likely(!VM_SequentialReadHint(vma)))
  1028. mark_page_accessed(page);
  1029. rss[MM_FILEPAGES]--;
  1030. }
  1031. page_remove_rmap(page);
  1032. if (unlikely(page_mapcount(page) < 0))
  1033. print_bad_pte(vma, addr, ptent, page);
  1034. force_flush = !__tlb_remove_page(tlb, page);
  1035. if (force_flush)
  1036. break;
  1037. continue;
  1038. }
  1039. /*
  1040. * If details->check_mapping, we leave swap entries;
  1041. * if details->nonlinear_vma, we leave file entries.
  1042. */
  1043. if (unlikely(details))
  1044. continue;
  1045. if (pte_file(ptent)) {
  1046. if (unlikely(!(vma->vm_flags & VM_NONLINEAR)))
  1047. print_bad_pte(vma, addr, ptent, NULL);
  1048. } else {
  1049. swp_entry_t entry = pte_to_swp_entry(ptent);
  1050. if (!non_swap_entry(entry))
  1051. rss[MM_SWAPENTS]--;
  1052. else if (is_migration_entry(entry)) {
  1053. struct page *page;
  1054. page = migration_entry_to_page(entry);
  1055. if (PageAnon(page))
  1056. rss[MM_ANONPAGES]--;
  1057. else
  1058. rss[MM_FILEPAGES]--;
  1059. }
  1060. if (unlikely(!free_swap_and_cache(entry)))
  1061. print_bad_pte(vma, addr, ptent, NULL);
  1062. }
  1063. pte_clear_not_present_full(mm, addr, pte, tlb->fullmm);
  1064. } while (pte++, addr += PAGE_SIZE, addr != end);
  1065. add_mm_rss_vec(mm, rss);
  1066. arch_leave_lazy_mmu_mode();
  1067. pte_unmap_unlock(start_pte, ptl);
  1068. /*
  1069. * mmu_gather ran out of room to batch pages, we break out of
  1070. * the PTE lock to avoid doing the potential expensive TLB invalidate
  1071. * and page-free while holding it.
  1072. */
  1073. if (force_flush) {
  1074. force_flush = 0;
  1075. #ifdef HAVE_GENERIC_MMU_GATHER
  1076. tlb->start = addr;
  1077. tlb->end = end;
  1078. #endif
  1079. tlb_flush_mmu(tlb);
  1080. if (addr != end)
  1081. goto again;
  1082. }
  1083. return addr;
  1084. }
  1085. static inline unsigned long zap_pmd_range(struct mmu_gather *tlb,
  1086. struct vm_area_struct *vma, pud_t *pud,
  1087. unsigned long addr, unsigned long end,
  1088. struct zap_details *details)
  1089. {
  1090. pmd_t *pmd;
  1091. unsigned long next;
  1092. pmd = pmd_offset(pud, addr);
  1093. do {
  1094. next = pmd_addr_end(addr, end);
  1095. if (pmd_trans_huge(*pmd)) {
  1096. if (next - addr != HPAGE_PMD_SIZE) {
  1097. #ifdef CONFIG_DEBUG_VM
  1098. if (!rwsem_is_locked(&tlb->mm->mmap_sem)) {
  1099. pr_err("%s: mmap_sem is unlocked! addr=0x%lx end=0x%lx vma->vm_start=0x%lx vma->vm_end=0x%lx\n",
  1100. __func__, addr, end,
  1101. vma->vm_start,
  1102. vma->vm_end);
  1103. BUG();
  1104. }
  1105. #endif
  1106. split_huge_page_pmd(vma->vm_mm, pmd);
  1107. } else if (zap_huge_pmd(tlb, vma, pmd, addr))
  1108. goto next;
  1109. /* fall through */
  1110. }
  1111. /*
  1112. * Here there can be other concurrent MADV_DONTNEED or
  1113. * trans huge page faults running, and if the pmd is
  1114. * none or trans huge it can change under us. This is
  1115. * because MADV_DONTNEED holds the mmap_sem in read
  1116. * mode.
  1117. */
  1118. if (pmd_none_or_trans_huge_or_clear_bad(pmd))
  1119. goto next;
  1120. next = zap_pte_range(tlb, vma, pmd, addr, next, details);
  1121. next:
  1122. cond_resched();
  1123. } while (pmd++, addr = next, addr != end);
  1124. return addr;
  1125. }
  1126. static inline unsigned long zap_pud_range(struct mmu_gather *tlb,
  1127. struct vm_area_struct *vma, pgd_t *pgd,
  1128. unsigned long addr, unsigned long end,
  1129. struct zap_details *details)
  1130. {
  1131. pud_t *pud;
  1132. unsigned long next;
  1133. pud = pud_offset(pgd, addr);
  1134. do {
  1135. next = pud_addr_end(addr, end);
  1136. if (pud_none_or_clear_bad(pud))
  1137. continue;
  1138. next = zap_pmd_range(tlb, vma, pud, addr, next, details);
  1139. } while (pud++, addr = next, addr != end);
  1140. return addr;
  1141. }
  1142. static void unmap_page_range(struct mmu_gather *tlb,
  1143. struct vm_area_struct *vma,
  1144. unsigned long addr, unsigned long end,
  1145. struct zap_details *details)
  1146. {
  1147. pgd_t *pgd;
  1148. unsigned long next;
  1149. if (details && !details->check_mapping && !details->nonlinear_vma)
  1150. details = NULL;
  1151. BUG_ON(addr >= end);
  1152. mem_cgroup_uncharge_start();
  1153. tlb_start_vma(tlb, vma);
  1154. pgd = pgd_offset(vma->vm_mm, addr);
  1155. do {
  1156. next = pgd_addr_end(addr, end);
  1157. if (pgd_none_or_clear_bad(pgd))
  1158. continue;
  1159. next = zap_pud_range(tlb, vma, pgd, addr, next, details);
  1160. } while (pgd++, addr = next, addr != end);
  1161. tlb_end_vma(tlb, vma);
  1162. mem_cgroup_uncharge_end();
  1163. }
  1164. static void unmap_single_vma(struct mmu_gather *tlb,
  1165. struct vm_area_struct *vma, unsigned long start_addr,
  1166. unsigned long end_addr,
  1167. struct zap_details *details)
  1168. {
  1169. unsigned long start = max(vma->vm_start, start_addr);
  1170. unsigned long end;
  1171. if (start >= vma->vm_end)
  1172. return;
  1173. end = min(vma->vm_end, end_addr);
  1174. if (end <= vma->vm_start)
  1175. return;
  1176. if (vma->vm_file)
  1177. uprobe_munmap(vma, start, end);
  1178. if (unlikely(vma->vm_flags & VM_PFNMAP))
  1179. untrack_pfn(vma, 0, 0);
  1180. if (start != end) {
  1181. if (unlikely(is_vm_hugetlb_page(vma))) {
  1182. /*
  1183. * It is undesirable to test vma->vm_file as it
  1184. * should be non-null for valid hugetlb area.
  1185. * However, vm_file will be NULL in the error
  1186. * cleanup path of do_mmap_pgoff. When
  1187. * hugetlbfs ->mmap method fails,
  1188. * do_mmap_pgoff() nullifies vma->vm_file
  1189. * before calling this function to clean up.
  1190. * Since no pte has actually been setup, it is
  1191. * safe to do nothing in this case.
  1192. */
  1193. if (vma->vm_file) {
  1194. mutex_lock(&vma->vm_file->f_mapping->i_mmap_mutex);
  1195. __unmap_hugepage_range_final(tlb, vma, start, end, NULL);
  1196. mutex_unlock(&vma->vm_file->f_mapping->i_mmap_mutex);
  1197. }
  1198. } else
  1199. unmap_page_range(tlb, vma, start, end, details);
  1200. }
  1201. }
  1202. /**
  1203. * unmap_vmas - unmap a range of memory covered by a list of vma's
  1204. * @tlb: address of the caller's struct mmu_gather
  1205. * @vma: the starting vma
  1206. * @start_addr: virtual address at which to start unmapping
  1207. * @end_addr: virtual address at which to end unmapping
  1208. *
  1209. * Unmap all pages in the vma list.
  1210. *
  1211. * Only addresses between `start' and `end' will be unmapped.
  1212. *
  1213. * The VMA list must be sorted in ascending virtual address order.
  1214. *
  1215. * unmap_vmas() assumes that the caller will flush the whole unmapped address
  1216. * range after unmap_vmas() returns. So the only responsibility here is to
  1217. * ensure that any thus-far unmapped pages are flushed before unmap_vmas()
  1218. * drops the lock and schedules.
  1219. */
  1220. void unmap_vmas(struct mmu_gather *tlb,
  1221. struct vm_area_struct *vma, unsigned long start_addr,
  1222. unsigned long end_addr)
  1223. {
  1224. struct mm_struct *mm = vma->vm_mm;
  1225. mmu_notifier_invalidate_range_start(mm, start_addr, end_addr);
  1226. for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next)
  1227. unmap_single_vma(tlb, vma, start_addr, end_addr, NULL);
  1228. mmu_notifier_invalidate_range_end(mm, start_addr, end_addr);
  1229. }
  1230. /**
  1231. * zap_page_range - remove user pages in a given range
  1232. * @vma: vm_area_struct holding the applicable pages
  1233. * @start: starting address of pages to zap
  1234. * @size: number of bytes to zap
  1235. * @details: details of nonlinear truncation or shared cache invalidation
  1236. *
  1237. * Caller must protect the VMA list
  1238. */
  1239. void zap_page_range(struct vm_area_struct *vma, unsigned long start,
  1240. unsigned long size, struct zap_details *details)
  1241. {
  1242. struct mm_struct *mm = vma->vm_mm;
  1243. struct mmu_gather tlb;
  1244. unsigned long end = start + size;
  1245. lru_add_drain();
  1246. tlb_gather_mmu(&tlb, mm, 0);
  1247. update_hiwater_rss(mm);
  1248. mmu_notifier_invalidate_range_start(mm, start, end);
  1249. for ( ; vma && vma->vm_start < end; vma = vma->vm_next)
  1250. unmap_single_vma(&tlb, vma, start, end, details);
  1251. mmu_notifier_invalidate_range_end(mm, start, end);
  1252. tlb_finish_mmu(&tlb, start, end);
  1253. }
  1254. /**
  1255. * zap_page_range_single - remove user pages in a given range
  1256. * @vma: vm_area_struct holding the applicable pages
  1257. * @address: starting address of pages to zap
  1258. * @size: number of bytes to zap
  1259. * @details: details of nonlinear truncation or shared cache invalidation
  1260. *
  1261. * The range must fit into one VMA.
  1262. */
  1263. static void zap_page_range_single(struct vm_area_struct *vma, unsigned long address,
  1264. unsigned long size, struct zap_details *details)
  1265. {
  1266. struct mm_struct *mm = vma->vm_mm;
  1267. struct mmu_gather tlb;
  1268. unsigned long end = address + size;
  1269. lru_add_drain();
  1270. tlb_gather_mmu(&tlb, mm, 0);
  1271. update_hiwater_rss(mm);
  1272. mmu_notifier_invalidate_range_start(mm, address, end);
  1273. unmap_single_vma(&tlb, vma, address, end, details);
  1274. mmu_notifier_invalidate_range_end(mm, address, end);
  1275. tlb_finish_mmu(&tlb, address, end);
  1276. }
  1277. /**
  1278. * zap_vma_ptes - remove ptes mapping the vma
  1279. * @vma: vm_area_struct holding ptes to be zapped
  1280. * @address: starting address of pages to zap
  1281. * @size: number of bytes to zap
  1282. *
  1283. * This function only unmaps ptes assigned to VM_PFNMAP vmas.
  1284. *
  1285. * The entire address range must be fully contained within the vma.
  1286. *
  1287. * Returns 0 if successful.
  1288. */
  1289. int zap_vma_ptes(struct vm_area_struct *vma, unsigned long address,
  1290. unsigned long size)
  1291. {
  1292. if (address < vma->vm_start || address + size > vma->vm_end ||
  1293. !(vma->vm_flags & VM_PFNMAP))
  1294. return -1;
  1295. zap_page_range_single(vma, address, size, NULL);
  1296. return 0;
  1297. }
  1298. EXPORT_SYMBOL_GPL(zap_vma_ptes);
  1299. /**
  1300. * follow_page - look up a page descriptor from a user-virtual address
  1301. * @vma: vm_area_struct mapping @address
  1302. * @address: virtual address to look up
  1303. * @flags: flags modifying lookup behaviour
  1304. *
  1305. * @flags can have FOLL_ flags set, defined in <linux/mm.h>
  1306. *
  1307. * Returns the mapped (struct page *), %NULL if no mapping exists, or
  1308. * an error pointer if there is a mapping to something not represented
  1309. * by a page descriptor (see also vm_normal_page()).
  1310. */
  1311. struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
  1312. unsigned int flags)
  1313. {
  1314. pgd_t *pgd;
  1315. pud_t *pud;
  1316. pmd_t *pmd;
  1317. pte_t *ptep, pte;
  1318. spinlock_t *ptl;
  1319. struct page *page;
  1320. struct mm_struct *mm = vma->vm_mm;
  1321. page = follow_huge_addr(mm, address, flags & FOLL_WRITE);
  1322. if (!IS_ERR(page)) {
  1323. BUG_ON(flags & FOLL_GET);
  1324. goto out;
  1325. }
  1326. page = NULL;
  1327. pgd = pgd_offset(mm, address);
  1328. if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
  1329. goto no_page_table;
  1330. pud = pud_offset(pgd, address);
  1331. if (pud_none(*pud))
  1332. goto no_page_table;
  1333. if (pud_huge(*pud) && vma->vm_flags & VM_HUGETLB) {
  1334. BUG_ON(flags & FOLL_GET);
  1335. page = follow_huge_pud(mm, address, pud, flags & FOLL_WRITE);
  1336. goto out;
  1337. }
  1338. if (unlikely(pud_bad(*pud)))
  1339. goto no_page_table;
  1340. pmd = pmd_offset(pud, address);
  1341. if (pmd_none(*pmd))
  1342. goto no_page_table;
  1343. if (pmd_huge(*pmd) && vma->vm_flags & VM_HUGETLB) {
  1344. BUG_ON(flags & FOLL_GET);
  1345. page = follow_huge_pmd(mm, address, pmd, flags & FOLL_WRITE);
  1346. goto out;
  1347. }
  1348. if (pmd_trans_huge(*pmd)) {
  1349. if (flags & FOLL_SPLIT) {
  1350. split_huge_page_pmd(mm, pmd);
  1351. goto split_fallthrough;
  1352. }
  1353. spin_lock(&mm->page_table_lock);
  1354. if (likely(pmd_trans_huge(*pmd))) {
  1355. if (unlikely(pmd_trans_splitting(*pmd))) {
  1356. spin_unlock(&mm->page_table_lock);
  1357. wait_split_huge_page(vma->anon_vma, pmd);
  1358. } else {
  1359. page = follow_trans_huge_pmd(vma, address,
  1360. pmd, flags);
  1361. spin_unlock(&mm->page_table_lock);
  1362. goto out;
  1363. }
  1364. } else
  1365. spin_unlock(&mm->page_table_lock);
  1366. /* fall through */
  1367. }
  1368. split_fallthrough:
  1369. if (unlikely(pmd_bad(*pmd)))
  1370. goto no_page_table;
  1371. ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
  1372. pte = *ptep;
  1373. if (!pte_present(pte))
  1374. goto no_page;
  1375. if ((flags & FOLL_WRITE) && !pte_write(pte))
  1376. goto unlock;
  1377. page = vm_normal_page(vma, address, pte);
  1378. if (unlikely(!page)) {
  1379. if ((flags & FOLL_DUMP) ||
  1380. !is_zero_pfn(pte_pfn(pte)))
  1381. goto bad_page;
  1382. page = pte_page(pte);
  1383. }
  1384. if (flags & FOLL_GET)
  1385. get_page_foll(page);
  1386. if (flags & FOLL_TOUCH) {
  1387. if ((flags & FOLL_WRITE) &&
  1388. !pte_dirty(pte) && !PageDirty(page))
  1389. set_page_dirty(page);
  1390. /*
  1391. * pte_mkyoung() would be more correct here, but atomic care
  1392. * is needed to avoid losing the dirty bit: it is easier to use
  1393. * mark_page_accessed().
  1394. */
  1395. mark_page_accessed(page);
  1396. }
  1397. if ((flags & FOLL_MLOCK) && (vma->vm_flags & VM_LOCKED)) {
  1398. /*
  1399. * The preliminary mapping check is mainly to avoid the
  1400. * pointless overhead of lock_page on the ZERO_PAGE
  1401. * which might bounce very badly if there is contention.
  1402. *
  1403. * If the page is already locked, we don't need to
  1404. * handle it now - vmscan will handle it later if and
  1405. * when it attempts to reclaim the page.
  1406. */
  1407. if (page->mapping && trylock_page(page)) {
  1408. lru_add_drain(); /* push cached pages to LRU */
  1409. /*
  1410. * Because we lock page here, and migration is
  1411. * blocked by the pte's page reference, and we
  1412. * know the page is still mapped, we don't even
  1413. * need to check for file-cache page truncation.
  1414. */
  1415. mlock_vma_page(page);
  1416. unlock_page(page);
  1417. }
  1418. }
  1419. unlock:
  1420. pte_unmap_unlock(ptep, ptl);
  1421. out:
  1422. return page;
  1423. bad_page:
  1424. pte_unmap_unlock(ptep, ptl);
  1425. return ERR_PTR(-EFAULT);
  1426. no_page:
  1427. pte_unmap_unlock(ptep, ptl);
  1428. if (!pte_none(pte))
  1429. return page;
  1430. no_page_table:
  1431. /*
  1432. * When core dumping an enormous anonymous area that nobody
  1433. * has touched so far, we don't want to allocate unnecessary pages or
  1434. * page tables. Return error instead of NULL to skip handle_mm_fault,
  1435. * then get_dump_page() will return NULL to leave a hole in the dump.
  1436. * But we can only make this optimization where a hole would surely
  1437. * be zero-filled if handle_mm_fault() actually did handle it.
  1438. */
  1439. if ((flags & FOLL_DUMP) &&
  1440. (!vma->vm_ops || !vma->vm_ops->fault))
  1441. return ERR_PTR(-EFAULT);
  1442. return page;
  1443. }
  1444. static inline int stack_guard_page(struct vm_area_struct *vma, unsigned long addr)
  1445. {
  1446. return stack_guard_page_start(vma, addr) ||
  1447. stack_guard_page_end(vma, addr+PAGE_SIZE);
  1448. }
  1449. /**
  1450. * __get_user_pages() - pin user pages in memory
  1451. * @tsk: task_struct of target task
  1452. * @mm: mm_struct of target mm
  1453. * @start: starting user address
  1454. * @nr_pages: number of pages from start to pin
  1455. * @gup_flags: flags modifying pin behaviour
  1456. * @pages: array that receives pointers to the pages pinned.
  1457. * Should be at least nr_pages long. Or NULL, if caller
  1458. * only intends to ensure the pages are faulted in.
  1459. * @vmas: array of pointers to vmas corresponding to each page.
  1460. * Or NULL if the caller does not require them.
  1461. * @nonblocking: whether waiting for disk IO or mmap_sem contention
  1462. *
  1463. * Returns number of pages pinned. This may be fewer than the number
  1464. * requested. If nr_pages is 0 or negative, returns 0. If no pages
  1465. * were pinned, returns -errno. Each page returned must be released
  1466. * with a put_page() call when it is finished with. vmas will only
  1467. * remain valid while mmap_sem is held.
  1468. *
  1469. * Must be called with mmap_sem held for read or write.
  1470. *
  1471. * __get_user_pages walks a process's page tables and takes a reference to
  1472. * each struct page that each user address corresponds to at a given
  1473. * instant. That is, it takes the page that would be accessed if a user
  1474. * thread accesses the given user virtual address at that instant.
  1475. *
  1476. * This does not guarantee that the page exists in the user mappings when
  1477. * __get_user_pages returns, and there may even be a completely different
  1478. * page there in some cases (eg. if mmapped pagecache has been invalidated
  1479. * and subsequently re faulted). However it does guarantee that the page
  1480. * won't be freed completely. And mostly callers simply care that the page
  1481. * contains data that was valid *at some point in time*. Typically, an IO
  1482. * or similar operation cannot guarantee anything stronger anyway because
  1483. * locks can't be held over the syscall boundary.
  1484. *
  1485. * If @gup_flags & FOLL_WRITE == 0, the page must not be written to. If
  1486. * the page is written to, set_page_dirty (or set_page_dirty_lock, as
  1487. * appropriate) must be called after the page is finished with, and
  1488. * before put_page is called.
  1489. *
  1490. * If @nonblocking != NULL, __get_user_pages will not wait for disk IO
  1491. * or mmap_sem contention, and if waiting is needed to pin all pages,
  1492. * *@nonblocking will be set to 0.
  1493. *
  1494. * In most cases, get_user_pages or get_user_pages_fast should be used
  1495. * instead of __get_user_pages. __get_user_pages should be used only if
  1496. * you need some special @gup_flags.
  1497. */
  1498. int __get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
  1499. unsigned long start, int nr_pages, unsigned int gup_flags,
  1500. struct page **pages, struct vm_area_struct **vmas,
  1501. int *nonblocking)
  1502. {
  1503. int i;
  1504. unsigned long vm_flags;
  1505. if (nr_pages <= 0)
  1506. return 0;
  1507. VM_BUG_ON(!!pages != !!(gup_flags & FOLL_GET));
  1508. /*
  1509. * Require read or write permissions.
  1510. * If FOLL_FORCE is set, we only require the "MAY" flags.
  1511. */
  1512. vm_flags = (gup_flags & FOLL_WRITE) ?
  1513. (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
  1514. vm_flags &= (gup_flags & FOLL_FORCE) ?
  1515. (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
  1516. i = 0;
  1517. do {
  1518. struct vm_area_struct *vma;
  1519. vma = find_extend_vma(mm, start);
  1520. if (!vma && in_gate_area(mm, start)) {
  1521. unsigned long pg = start & PAGE_MASK;
  1522. pgd_t *pgd;
  1523. pud_t *pud;
  1524. pmd_t *pmd;
  1525. pte_t *pte;
  1526. /* user gate pages are read-only */
  1527. if (gup_flags & FOLL_WRITE)
  1528. return i ? : -EFAULT;
  1529. if (pg > TASK_SIZE)
  1530. pgd = pgd_offset_k(pg);
  1531. else
  1532. pgd = pgd_offset_gate(mm, pg);
  1533. BUG_ON(pgd_none(*pgd));
  1534. pud = pud_offset(pgd, pg);
  1535. BUG_ON(pud_none(*pud));
  1536. pmd = pmd_offset(pud, pg);
  1537. if (pmd_none(*pmd))
  1538. return i ? : -EFAULT;
  1539. VM_BUG_ON(pmd_trans_huge(*pmd));
  1540. pte = pte_offset_map(pmd, pg);
  1541. if (pte_none(*pte)) {
  1542. pte_unmap(pte);
  1543. return i ? : -EFAULT;
  1544. }
  1545. vma = get_gate_vma(mm);
  1546. if (pages) {
  1547. struct page *page;
  1548. page = vm_normal_page(vma, start, *pte);
  1549. if (!page) {
  1550. if (!(gup_flags & FOLL_DUMP) &&
  1551. is_zero_pfn(pte_pfn(*pte)))
  1552. page = pte_page(*pte);
  1553. else {
  1554. pte_unmap(pte);
  1555. return i ? : -EFAULT;
  1556. }
  1557. }
  1558. pages[i] = page;
  1559. get_page(page);
  1560. }
  1561. pte_unmap(pte);
  1562. goto next_page;
  1563. }
  1564. if (!vma ||
  1565. (vma->vm_flags & (VM_IO | VM_PFNMAP)) ||
  1566. !(vm_flags & vma->vm_flags))
  1567. return i ? : -EFAULT;
  1568. if (is_vm_hugetlb_page(vma)) {
  1569. i = follow_hugetlb_page(mm, vma, pages, vmas,
  1570. &start, &nr_pages, i, gup_flags);
  1571. continue;
  1572. }
  1573. do {
  1574. struct page *page;
  1575. unsigned int foll_flags = gup_flags;
  1576. /*
  1577. * If we have a pending SIGKILL, don't keep faulting
  1578. * pages and potentially allocating memory.
  1579. */
  1580. if (unlikely(fatal_signal_pending(current)))
  1581. return i ? i : -ERESTARTSYS;
  1582. cond_resched();
  1583. while (!(page = follow_page(vma, start, foll_flags))) {
  1584. int ret;
  1585. unsigned int fault_flags = 0;
  1586. /* For mlock, just skip the stack guard page. */
  1587. if (foll_flags & FOLL_MLOCK) {
  1588. if (stack_guard_page(vma, start))
  1589. goto next_page;
  1590. }
  1591. if (foll_flags & FOLL_WRITE)
  1592. fault_flags |= FAULT_FLAG_WRITE;
  1593. if (nonblocking)
  1594. fault_flags |= FAULT_FLAG_ALLOW_RETRY;
  1595. if (foll_flags & FOLL_NOWAIT)
  1596. fault_flags |= (FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT);
  1597. ret = handle_mm_fault(mm, vma, start,
  1598. fault_flags);
  1599. if (ret & VM_FAULT_ERROR) {
  1600. if (ret & VM_FAULT_OOM)
  1601. return i ? i : -ENOMEM;
  1602. if (ret & (VM_FAULT_HWPOISON |
  1603. VM_FAULT_HWPOISON_LARGE)) {
  1604. if (i)
  1605. return i;
  1606. else if (gup_flags & FOLL_HWPOISON)
  1607. return -EHWPOISON;
  1608. else
  1609. return -EFAULT;
  1610. }
  1611. if (ret & VM_FAULT_SIGBUS)
  1612. return i ? i : -EFAULT;
  1613. BUG();
  1614. }
  1615. if (tsk) {
  1616. if (ret & VM_FAULT_MAJOR)
  1617. tsk->maj_flt++;
  1618. else
  1619. tsk->min_flt++;
  1620. }
  1621. if (ret & VM_FAULT_RETRY) {
  1622. if (nonblocking)
  1623. *nonblocking = 0;
  1624. return i;
  1625. }
  1626. /*
  1627. * The VM_FAULT_WRITE bit tells us that
  1628. * do_wp_page has broken COW when necessary,
  1629. * even if maybe_mkwrite decided not to set
  1630. * pte_write. We can thus safely do subsequent
  1631. * page lookups as if they were reads. But only
  1632. * do so when looping for pte_write is futile:
  1633. * in some cases userspace may also be wanting
  1634. * to write to the gotten user page, which a
  1635. * read fault here might prevent (a readonly
  1636. * page might get reCOWed by userspace write).
  1637. */
  1638. if ((ret & VM_FAULT_WRITE) &&
  1639. !(vma->vm_flags & VM_WRITE))
  1640. foll_flags &= ~FOLL_WRITE;
  1641. cond_resched();
  1642. }
  1643. if (IS_ERR(page))
  1644. return i ? i : PTR_ERR(page);
  1645. if (pages) {
  1646. pages[i] = page;
  1647. flush_anon_page(vma, page, start);
  1648. flush_dcache_page(page);
  1649. }
  1650. next_page:
  1651. if (vmas)
  1652. vmas[i] = vma;
  1653. i++;
  1654. start += PAGE_SIZE;
  1655. nr_pages--;
  1656. } while (nr_pages && start < vma->vm_end);
  1657. } while (nr_pages);
  1658. return i;
  1659. }
  1660. EXPORT_SYMBOL(__get_user_pages);
  1661. /*
  1662. * fixup_user_fault() - manually resolve a user page fault
  1663. * @tsk: the task_struct to use for page fault accounting, or
  1664. * NULL if faults are not to be recorded.
  1665. * @mm: mm_struct of target mm
  1666. * @address: user address
  1667. * @fault_flags:flags to pass down to handle_mm_fault()
  1668. *
  1669. * This is meant to be called in the specific scenario where for locking reasons
  1670. * we try to access user memory in atomic context (within a pagefault_disable()
  1671. * section), this returns -EFAULT, and we want to resolve the user fault before
  1672. * trying again.
  1673. *
  1674. * Typically this is meant to be used by the futex code.
  1675. *
  1676. * The main difference with get_user_pages() is that this function will
  1677. * unconditionally call handle_mm_fault() which will in turn perform all the
  1678. * necessary SW fixup of the dirty and young bits in the PTE, while
  1679. * handle_mm_fault() only guarantees to update these in the struct page.
  1680. *
  1681. * This is important for some architectures where those bits also gate the
  1682. * access permission to the page because they are maintained in software. On
  1683. * such architectures, gup() will not be enough to make a subsequent access
  1684. * succeed.
  1685. *
  1686. * This should be called with the mm_sem held for read.
  1687. */
  1688. int fixup_user_fault(struct task_struct *tsk, struct mm_struct *mm,
  1689. unsigned long address, unsigned int fault_flags)
  1690. {
  1691. struct vm_area_struct *vma;
  1692. int ret;
  1693. vma = find_extend_vma(mm, address);
  1694. if (!vma || address < vma->vm_start)
  1695. return -EFAULT;
  1696. ret = handle_mm_fault(mm, vma, address, fault_flags);
  1697. if (ret & VM_FAULT_ERROR) {
  1698. if (ret & VM_FAULT_OOM)
  1699. return -ENOMEM;
  1700. if (ret & (VM_FAULT_HWPOISON | VM_FAULT_HWPOISON_LARGE))
  1701. return -EHWPOISON;
  1702. if (ret & VM_FAULT_SIGBUS)
  1703. return -EFAULT;
  1704. BUG();
  1705. }
  1706. if (tsk) {
  1707. if (ret & VM_FAULT_MAJOR)
  1708. tsk->maj_flt++;
  1709. else
  1710. tsk->min_flt++;
  1711. }
  1712. return 0;
  1713. }
  1714. /*
  1715. * get_user_pages() - pin user pages in memory
  1716. * @tsk: the task_struct to use for page fault accounting, or
  1717. * NULL if faults are not to be recorded.
  1718. * @mm: mm_struct of target mm
  1719. * @start: starting user address
  1720. * @nr_pages: number of pages from start to pin
  1721. * @write: whether pages will be written to by the caller
  1722. * @force: whether to force write access even if user mapping is
  1723. * readonly. This will result in the page being COWed even
  1724. * in MAP_SHARED mappings. You do not want this.
  1725. * @pages: array that receives pointers to the pages pinned.
  1726. * Should be at least nr_pages long. Or NULL, if caller
  1727. * only intends to ensure the pages are faulted in.
  1728. * @vmas: array of pointers to vmas corresponding to each page.
  1729. * Or NULL if the caller does not require them.
  1730. *
  1731. * Returns number of pages pinned. This may be fewer than the number
  1732. * requested. If nr_pages is 0 or negative, returns 0. If no pages
  1733. * were pinned, returns -errno. Each page returned must be released
  1734. * with a put_page() call when it is finished with. vmas will only
  1735. * remain valid while mmap_sem is held.
  1736. *
  1737. * Must be called with mmap_sem held for read or write.
  1738. *
  1739. * get_user_pages walks a process's page tables and takes a reference to
  1740. * each struct page that each user address corresponds to at a given
  1741. * instant. That is, it takes the page that would be accessed if a user
  1742. * thread accesses the given user virtual address at that instant.
  1743. *
  1744. * This does not guarantee that the page exists in the user mappings when
  1745. * get_user_pages returns, and there may even be a completely different
  1746. * page there in some cases (eg. if mmapped pagecache has been invalidated
  1747. * and subsequently re faulted). However it does guarantee that the page
  1748. * won't be freed completely. And mostly callers simply care that the page
  1749. * contains data that was valid *at some point in time*. Typically, an IO
  1750. * or similar operation cannot guarantee anything stronger anyway because
  1751. * locks can't be held over the syscall boundary.
  1752. *
  1753. * If write=0, the page must not be written to. If the page is written to,
  1754. * set_page_dirty (or set_page_dirty_lock, as appropriate) must be called
  1755. * after the page is finished with, and before put_page is called.
  1756. *
  1757. * get_user_pages is typically used for fewer-copy IO operations, to get a
  1758. * handle on the memory by some means other than accesses via the user virtual
  1759. * addresses. The pages may be submitted for DMA to devices or accessed via
  1760. * their kernel linear mapping (via the kmap APIs). Care should be taken to
  1761. * use the correct cache flushing APIs.
  1762. *
  1763. * See also get_user_pages_fast, for performance critical applications.
  1764. */
  1765. int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
  1766. unsigned long start, int nr_pages, int write, int force,
  1767. struct page **pages, struct vm_area_struct **vmas)
  1768. {
  1769. int flags = FOLL_TOUCH;
  1770. if (pages)
  1771. flags |= FOLL_GET;
  1772. if (write)
  1773. flags |= FOLL_WRITE;
  1774. if (force)
  1775. flags |= FOLL_FORCE;
  1776. return __get_user_pages(tsk, mm, start, nr_pages, flags, pages, vmas,
  1777. NULL);
  1778. }
  1779. EXPORT_SYMBOL(get_user_pages);
  1780. /**
  1781. * get_dump_page() - pin user page in memory while writing it to core dump
  1782. * @addr: user address
  1783. *
  1784. * Returns struct page pointer of user page pinned for dump,
  1785. * to be freed afterwards by page_cache_release() or put_page().
  1786. *
  1787. * Returns NULL on any kind of failure - a hole must then be inserted into
  1788. * the corefile, to preserve alignment with its headers; and also returns
  1789. * NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found -
  1790. * allowing a hole to be left in the corefile to save diskspace.
  1791. *
  1792. * Called without mmap_sem, but after all other threads have been killed.
  1793. */
  1794. #ifdef CONFIG_ELF_CORE
  1795. struct page *get_dump_page(unsigned long addr)
  1796. {
  1797. struct vm_area_struct *vma;
  1798. struct page *page;
  1799. if (__get_user_pages(current, current->mm, addr, 1,
  1800. FOLL_FORCE | FOLL_DUMP | FOLL_GET, &page, &vma,
  1801. NULL) < 1)
  1802. return NULL;
  1803. flush_cache_page(vma, addr, page_to_pfn(page));
  1804. return page;
  1805. }
  1806. #endif /* CONFIG_ELF_CORE */
  1807. pte_t *__get_locked_pte(struct mm_struct *mm, unsigned long addr,
  1808. spinlock_t **ptl)
  1809. {
  1810. pgd_t * pgd = pgd_offset(mm, addr);
  1811. pud_t * pud = pud_alloc(mm, pgd, addr);
  1812. if (pud) {
  1813. pmd_t * pmd = pmd_alloc(mm, pud, addr);
  1814. if (pmd) {
  1815. VM_BUG_ON(pmd_trans_huge(*pmd));
  1816. return pte_alloc_map_lock(mm, pmd, addr, ptl);
  1817. }
  1818. }
  1819. return NULL;
  1820. }
  1821. /*
  1822. * This is the old fallback for page remapping.
  1823. *
  1824. * For historical reasons, it only allows reserved pages. Only
  1825. * old drivers should use this, and they needed to mark their
  1826. * pages reserved for the old functions anyway.
  1827. */
  1828. static int insert_page(struct vm_area_struct *vma, unsigned long addr,
  1829. struct page *page, pgprot_t prot)
  1830. {
  1831. struct mm_struct *mm = vma->vm_mm;
  1832. int retval;
  1833. pte_t *pte;
  1834. spinlock_t *ptl;
  1835. retval = -EINVAL;
  1836. if (PageAnon(page))
  1837. goto out;
  1838. retval = -ENOMEM;
  1839. flush_dcache_page(page);
  1840. pte = get_locked_pte(mm, addr, &ptl);
  1841. if (!pte)
  1842. goto out;
  1843. retval = -EBUSY;
  1844. if (!pte_none(*pte))
  1845. goto out_unlock;
  1846. /* Ok, finally just insert the thing.. */
  1847. get_page(page);
  1848. inc_mm_counter_fast(mm, MM_FILEPAGES);
  1849. page_add_file_rmap(page);
  1850. set_pte_at(mm, addr, pte, mk_pte(page, prot));
  1851. retval = 0;
  1852. pte_unmap_unlock(pte, ptl);
  1853. return retval;
  1854. out_unlock:
  1855. pte_unmap_unlock(pte, ptl);
  1856. out:
  1857. return retval;
  1858. }
  1859. /**
  1860. * vm_insert_page - insert single page into user vma
  1861. * @vma: user vma to map to
  1862. * @addr: target user address of this page
  1863. * @page: source kernel page
  1864. *
  1865. * This allows drivers to insert individual pages they've allocated
  1866. * into a user vma.
  1867. *
  1868. * The page has to be a nice clean _individual_ kernel allocation.
  1869. * If you allocate a compound page, you need to have marked it as
  1870. * such (__GFP_COMP), or manually just split the page up yourself
  1871. * (see split_page()).
  1872. *
  1873. * NOTE! Traditionally this was done with "remap_pfn_range()" which
  1874. * took an arbitrary page protection parameter. This doesn't allow
  1875. * that. Your vma protection will have to be set up correctly, which
  1876. * means that if you want a shared writable mapping, you'd better
  1877. * ask for a shared writable mapping!
  1878. *
  1879. * The page does not need to be reserved.
  1880. *
  1881. * Usually this function is called from f_op->mmap() handler
  1882. * under mm->mmap_sem write-lock, so it can change vma->vm_flags.
  1883. * Caller must set VM_MIXEDMAP on vma if it wants to call this
  1884. * function from other places, for example from page-fault handler.
  1885. */
  1886. int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
  1887. struct page *page)
  1888. {
  1889. if (addr < vma->vm_start || addr >= vma->vm_end)
  1890. return -EFAULT;
  1891. if (!page_count(page))
  1892. return -EINVAL;
  1893. if (!(vma->vm_flags & VM_MIXEDMAP)) {
  1894. BUG_ON(down_read_trylock(&vma->vm_mm->mmap_sem));
  1895. BUG_ON(vma->vm_flags & VM_PFNMAP);
  1896. vma->vm_flags |= VM_MIXEDMAP;
  1897. }
  1898. return insert_page(vma, addr, page, vma->vm_page_prot);
  1899. }
  1900. EXPORT_SYMBOL(vm_insert_page);
  1901. static int insert_pfn(struct vm_area_struct *vma, unsigned long addr,
  1902. unsigned long pfn, pgprot_t prot)
  1903. {
  1904. struct mm_struct *mm = vma->vm_mm;
  1905. int retval;
  1906. pte_t *pte, entry;
  1907. spinlock_t *ptl;
  1908. retval = -ENOMEM;
  1909. pte = get_locked_pte(mm, addr, &ptl);
  1910. if (!pte)
  1911. goto out;
  1912. retval = -EBUSY;
  1913. if (!pte_none(*pte))
  1914. goto out_unlock;
  1915. /* Ok, finally just insert the thing.. */
  1916. entry = pte_mkspecial(pfn_pte(pfn, prot));
  1917. set_pte_at(mm, addr, pte, entry);
  1918. update_mmu_cache(vma, addr, pte); /* XXX: why not for insert_page? */
  1919. retval = 0;
  1920. out_unlock:
  1921. pte_unmap_unlock(pte, ptl);
  1922. out:
  1923. return retval;
  1924. }
  1925. /**
  1926. * vm_insert_pfn - insert single pfn into user vma
  1927. * @vma: user vma to map to
  1928. * @addr: target user address of this page
  1929. * @pfn: source kernel pfn
  1930. *
  1931. * Similar to vm_insert_page, this allows drivers to insert individual pages
  1932. * they've allocated into a user vma. Same comments apply.
  1933. *
  1934. * This function should only be called from a vm_ops->fault handler, and
  1935. * in that case the handler should return NULL.
  1936. *
  1937. * vma cannot be a COW mapping.
  1938. *
  1939. * As this is called only for pages that do not currently exist, we
  1940. * do not need to flush old virtual caches or the TLB.
  1941. */
  1942. int vm_insert_pfn(struct vm_area_struct *vma, unsigned long addr,
  1943. unsigned long pfn)
  1944. {
  1945. int ret;
  1946. pgprot_t pgprot = vma->vm_page_prot;
  1947. /*
  1948. * Technically, architectures with pte_special can avoid all these
  1949. * restrictions (same for remap_pfn_range). However we would like
  1950. * consistency in testing and feature parity among all, so we should
  1951. * try to keep these invariants in place for everybody.
  1952. */
  1953. BUG_ON(!(vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)));
  1954. BUG_ON((vma->vm_flags & (VM_PFNMAP|VM_MIXEDMAP)) ==
  1955. (VM_PFNMAP|VM_MIXEDMAP));
  1956. BUG_ON((vma->vm_flags & VM_PFNMAP) && is_cow_mapping(vma->vm_flags));
  1957. BUG_ON((vma->vm_flags & VM_MIXEDMAP) && pfn_valid(pfn));
  1958. if (addr < vma->vm_start || addr >= vma->vm_end)
  1959. return -EFAULT;
  1960. if (track_pfn_insert(vma, &pgprot, pfn))
  1961. return -EINVAL;
  1962. ret = insert_pfn(vma, addr, pfn, pgprot);
  1963. return ret;
  1964. }
  1965. EXPORT_SYMBOL(vm_insert_pfn);
  1966. int vm_insert_mixed(struct vm_area_struct *vma, unsigned long addr,
  1967. unsigned long pfn)
  1968. {
  1969. BUG_ON(!(vma->vm_flags & VM_MIXEDMAP));
  1970. if (addr < vma->vm_start || addr >= vma->vm_end)
  1971. return -EFAULT;
  1972. /*
  1973. * If we don't have pte special, then we have to use the pfn_valid()
  1974. * based VM_MIXEDMAP scheme (see vm_normal_page), and thus we *must*
  1975. * refcount the page if pfn_valid is true (hence insert_page rather
  1976. * than insert_pfn). If a zero_pfn were inserted into a VM_MIXEDMAP
  1977. * without pte special, it would there be refcounted as a normal page.
  1978. */
  1979. if (!HAVE_PTE_SPECIAL && pfn_valid(pfn)) {
  1980. struct page *page;
  1981. page = pfn_to_page(pfn);
  1982. return insert_page(vma, addr, page, vma->vm_page_prot);
  1983. }
  1984. return insert_pfn(vma, addr, pfn, vma->vm_page_prot);
  1985. }
  1986. EXPORT_SYMBOL(vm_insert_mixed);
  1987. /*
  1988. * maps a range of physical memory into the requested pages. the old
  1989. * mappings are removed. any references to nonexistent pages results
  1990. * in null mappings (currently treated as "copy-on-access")
  1991. */
  1992. static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd,
  1993. unsigned long addr, unsigned long end,
  1994. unsigned long pfn, pgprot_t prot)
  1995. {
  1996. pte_t *pte;
  1997. spinlock_t *ptl;
  1998. pte = pte_alloc_map_lock(mm, pmd, addr, &ptl);
  1999. if (!pte)
  2000. return -ENOMEM;
  2001. arch_enter_lazy_mmu_mode();
  2002. do {
  2003. BUG_ON(!pte_none(*pte));
  2004. set_pte_at(mm, addr, pte, pte_mkspecial(pfn_pte(pfn, prot)));
  2005. pfn++;
  2006. } while (pte++, addr += PAGE_SIZE, addr != end);
  2007. arch_leave_lazy_mmu_mode();
  2008. pte_unmap_unlock(pte - 1, ptl);
  2009. return 0;
  2010. }
  2011. static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud,
  2012. unsigned long addr, unsigned long end,
  2013. unsigned long pfn, pgprot_t prot)
  2014. {
  2015. pmd_t *pmd;
  2016. unsigned long next;
  2017. pfn -= addr >> PAGE_SHIFT;
  2018. pmd = pmd_alloc(mm, pud, addr);
  2019. if (!pmd)
  2020. return -ENOMEM;
  2021. VM_BUG_ON(pmd_trans_huge(*pmd));
  2022. do {
  2023. next = pmd_addr_end(addr, end);
  2024. if (remap_pte_range(mm, pmd, addr, next,
  2025. pfn + (addr >> PAGE_SHIFT), prot))
  2026. return -ENOMEM;
  2027. } while (pmd++, addr = next, addr != end);
  2028. return 0;
  2029. }
  2030. static inline int remap_pud_range(struct mm_struct *mm, pgd_t *pgd,
  2031. unsigned long addr, unsigned long end,
  2032. unsigned long pfn, pgprot_t prot)
  2033. {
  2034. pud_t *pud;
  2035. unsigned long next;
  2036. pfn -= addr >> PAGE_SHIFT;
  2037. pud = pud_alloc(mm, pgd, addr);
  2038. if (!pud)
  2039. return -ENOMEM;
  2040. do {
  2041. next = pud_addr_end(addr, end);
  2042. if (remap_pmd_range(mm, pud, addr, next,
  2043. pfn + (addr >> PAGE_SHIFT), prot))
  2044. return -ENOMEM;
  2045. } while (pud++, addr = next, addr != end);
  2046. return 0;
  2047. }
  2048. /**
  2049. * remap_pfn_range - remap kernel memory to userspace
  2050. * @vma: user vma to map to
  2051. * @addr: target user address to start at
  2052. * @pfn: physical address of kernel memory
  2053. * @size: size of map area
  2054. * @prot: page protection flags for this mapping
  2055. *
  2056. * Note: this is only safe if the mm semaphore is held when called.
  2057. */
  2058. int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
  2059. unsigned long pfn, unsigned long size, pgprot_t prot)
  2060. {
  2061. pgd_t *pgd;
  2062. unsigned long next;
  2063. unsigned long end = addr + PAGE_ALIGN(size);
  2064. struct mm_struct *mm = vma->vm_mm;
  2065. int err;
  2066. /*
  2067. * Physically remapped pages are special. Tell the
  2068. * rest of the world about it:
  2069. * VM_IO tells people not to look at these pages
  2070. * (accesses can have side effects).
  2071. * VM_PFNMAP tells the core MM that the base pages are just
  2072. * raw PFN mappings, and do not have a "struct page" associated
  2073. * with them.
  2074. * VM_DONTEXPAND
  2075. * Disable vma merging and expanding with mremap().
  2076. * VM_DONTDUMP
  2077. * Omit vma from core dump, even when VM_IO turned off.
  2078. *
  2079. * There's a horrible special case to handle copy-on-write
  2080. * behaviour that some programs depend on. We mark the "original"
  2081. * un-COW'ed pages by matching them up with "vma->vm_pgoff".
  2082. * See vm_normal_page() for details.
  2083. */
  2084. if (is_cow_mapping(vma->vm_flags)) {
  2085. if (addr != vma->vm_start || end != vma->vm_end)
  2086. return -EINVAL;
  2087. vma->vm_pgoff = pfn;
  2088. }
  2089. err = track_pfn_remap(vma, &prot, pfn, addr, PAGE_ALIGN(size));
  2090. if (err)
  2091. return -EINVAL;
  2092. vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
  2093. BUG_ON(addr >= end);
  2094. pfn -= addr >> PAGE_SHIFT;
  2095. pgd = pgd_offset(mm, addr);
  2096. flush_cache_range(vma, addr, end);
  2097. do {
  2098. next = pgd_addr_end(addr, end);
  2099. err = remap_pud_range(mm, pgd, addr, next,
  2100. pfn + (addr >> PAGE_SHIFT), prot);
  2101. if (err)
  2102. break;
  2103. } while (pgd++, addr = next, addr != end);
  2104. if (err)
  2105. untrack_pfn(vma, pfn, PAGE_ALIGN(size));
  2106. return err;
  2107. }
  2108. EXPORT_SYMBOL(remap_pfn_range);
  2109. static int apply_to_pte_range(struct mm_struct *mm, pmd_t *pmd,
  2110. unsigned long addr, unsigned long end,
  2111. pte_fn_t fn, void *data)
  2112. {
  2113. pte_t *pte;
  2114. int err;
  2115. pgtable_t token;
  2116. spinlock_t *uninitialized_var(ptl);
  2117. pte = (mm == &init_mm) ?
  2118. pte_alloc_kernel(pmd, addr) :
  2119. pte_alloc_map_lock(mm, pmd, addr, &ptl);
  2120. if (!pte)
  2121. return -ENOMEM;
  2122. BUG_ON(pmd_huge(*pmd));
  2123. arch_enter_lazy_mmu_mode();
  2124. token = pmd_pgtable(*pmd);
  2125. do {
  2126. err = fn(pte++, token, addr, data);
  2127. if (err)
  2128. break;
  2129. } while (addr += PAGE_SIZE, addr != end);
  2130. arch_leave_lazy_mmu_mode();
  2131. if (mm != &init_mm)
  2132. pte_unmap_unlock(pte-1, ptl);
  2133. return err;
  2134. }
  2135. static int apply_to_pmd_range(struct mm_struct *mm, pud_t *pud,
  2136. unsigned long addr, unsigned long end,
  2137. pte_fn_t fn, void *data)
  2138. {
  2139. pmd_t *pmd;
  2140. unsigned long next;
  2141. int err;
  2142. BUG_ON(pud_huge(*pud));
  2143. pmd = pmd_alloc(mm, pud, addr);
  2144. if (!pmd)
  2145. return -ENOMEM;
  2146. do {
  2147. next = pmd_addr_end(addr, end);
  2148. err = apply_to_pte_range(mm, pmd, addr, next, fn, data);
  2149. if (err)
  2150. break;
  2151. } while (pmd++, addr = next, addr != end);
  2152. return err;
  2153. }
  2154. static int apply_to_pud_range(struct mm_struct *mm, pgd_t *pgd,
  2155. unsigned long addr, unsigned long end,
  2156. pte_fn_t fn, void *data)
  2157. {
  2158. pud_t *pud;
  2159. unsigned long next;
  2160. int err;
  2161. pud = pud_alloc(mm, pgd, addr);
  2162. if (!pud)
  2163. return -ENOMEM;
  2164. do {
  2165. next = pud_addr_end(addr, end);
  2166. err = apply_to_pmd_range(mm, pud, addr, next, fn, data);
  2167. if (err)
  2168. break;
  2169. } while (pud++, addr = next, addr != end);
  2170. return err;
  2171. }
  2172. /*
  2173. * Scan a region of virtual memory, filling in page tables as necessary
  2174. * and calling a provided function on each leaf page table.
  2175. */
  2176. int apply_to_page_range(struct mm_struct *mm, unsigned long addr,
  2177. unsigned long size, pte_fn_t fn, void *data)
  2178. {
  2179. pgd_t *pgd;
  2180. unsigned long next;
  2181. unsigned long end = addr + size;
  2182. int err;
  2183. BUG_ON(addr >= end);
  2184. pgd = pgd_offset(mm, addr);
  2185. do {
  2186. next = pgd_addr_end(addr, end);
  2187. err = apply_to_pud_range(mm, pgd, addr, next, fn, data);
  2188. if (err)
  2189. break;
  2190. } while (pgd++, addr = next, addr != end);
  2191. return err;
  2192. }
  2193. EXPORT_SYMBOL_GPL(apply_to_page_range);
  2194. /*
  2195. * handle_pte_fault chooses page fault handler according to an entry
  2196. * which was read non-atomically. Before making any commitment, on
  2197. * those architectures or configurations (e.g. i386 with PAE) which
  2198. * might give a mix of unmatched parts, do_swap_page and do_nonlinear_fault
  2199. * must check under lock before unmapping the pte and proceeding
  2200. * (but do_wp_page is only called after already making such a check;
  2201. * and do_anonymous_page can safely check later on).
  2202. */
  2203. static inline int pte_unmap_same(struct mm_struct *mm, pmd_t *pmd,
  2204. pte_t *page_table, pte_t orig_pte)
  2205. {
  2206. int same = 1;
  2207. #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
  2208. if (sizeof(pte_t) > sizeof(unsigned long)) {
  2209. spinlock_t *ptl = pte_lockptr(mm, pmd);
  2210. spin_lock(ptl);
  2211. same = pte_same(*page_table, orig_pte);
  2212. spin_unlock(ptl);
  2213. }
  2214. #endif
  2215. pte_unmap(page_table);
  2216. return same;
  2217. }
  2218. static inline void cow_user_page(struct page *dst, struct page *src, unsigned long va, struct vm_area_struct *vma)
  2219. {
  2220. /*
  2221. * If the source page was a PFN mapping, we don't have
  2222. * a "struct page" for it. We do a best-effort copy by
  2223. * just copying from the original user address. If that
  2224. * fails, we just zero-fill it. Live with it.
  2225. */
  2226. if (unlikely(!src)) {
  2227. void *kaddr = kmap_atomic(dst);
  2228. void __user *uaddr = (void __user *)(va & PAGE_MASK);
  2229. /*
  2230. * This really shouldn't fail, because the page is there
  2231. * in the page tables. But it might just be unreadable,
  2232. * in which case we just give up and fill the result with
  2233. * zeroes.
  2234. */
  2235. if (__copy_from_user_inatomic(kaddr, uaddr, PAGE_SIZE))
  2236. clear_page(kaddr);
  2237. kunmap_atomic(kaddr);
  2238. flush_dcache_page(dst);
  2239. } else
  2240. copy_user_highpage(dst, src, va, vma);
  2241. }
  2242. /*
  2243. * This routine handles present pages, when users try to write
  2244. * to a shared page. It is done by copying the page to a new address
  2245. * and decrementing the shared-page counter for the old page.
  2246. *
  2247. * Note that this routine assumes that the protection checks have been
  2248. * done by the caller (the low-level page fault routine in most cases).
  2249. * Thus we can safely just mark it writable once we've done any necessary
  2250. * COW.
  2251. *
  2252. * We also mark the page dirty at this point even though the page will
  2253. * change only once the write actually happens. This avoids a few races,
  2254. * and potentially makes it more efficient.
  2255. *
  2256. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  2257. * but allow concurrent faults), with pte both mapped and locked.
  2258. * We return with mmap_sem still held, but pte unmapped and unlocked.
  2259. */
  2260. static int do_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
  2261. unsigned long address, pte_t *page_table, pmd_t *pmd,
  2262. spinlock_t *ptl, pte_t orig_pte)
  2263. __releases(ptl)
  2264. {
  2265. struct page *old_page, *new_page = NULL;
  2266. pte_t entry;
  2267. int ret = 0;
  2268. int page_mkwrite = 0;
  2269. struct page *dirty_page = NULL;
  2270. unsigned long mmun_start = 0; /* For mmu_notifiers */
  2271. unsigned long mmun_end = 0; /* For mmu_notifiers */
  2272. old_page = vm_normal_page(vma, address, orig_pte);
  2273. if (!old_page) {
  2274. /*
  2275. * VM_MIXEDMAP !pfn_valid() case
  2276. *
  2277. * We should not cow pages in a shared writeable mapping.
  2278. * Just mark the pages writable as we can't do any dirty
  2279. * accounting on raw pfn maps.
  2280. */
  2281. if ((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
  2282. (VM_WRITE|VM_SHARED))
  2283. goto reuse;
  2284. goto gotten;
  2285. }
  2286. /*
  2287. * Take out anonymous pages first, anonymous shared vmas are
  2288. * not dirty accountable.
  2289. */
  2290. if (PageAnon(old_page) && !PageKsm(old_page)) {
  2291. if (!trylock_page(old_page)) {
  2292. page_cache_get(old_page);
  2293. pte_unmap_unlock(page_table, ptl);
  2294. lock_page(old_page);
  2295. page_table = pte_offset_map_lock(mm, pmd, address,
  2296. &ptl);
  2297. if (!pte_same(*page_table, orig_pte)) {
  2298. unlock_page(old_page);
  2299. goto unlock;
  2300. }
  2301. page_cache_release(old_page);
  2302. }
  2303. if (reuse_swap_page(old_page)) {
  2304. /*
  2305. * The page is all ours. Move it to our anon_vma so
  2306. * the rmap code will not search our parent or siblings.
  2307. * Protected against the rmap code by the page lock.
  2308. */
  2309. page_move_anon_rmap(old_page, vma, address);
  2310. unlock_page(old_page);
  2311. goto reuse;
  2312. }
  2313. unlock_page(old_page);
  2314. } else if (unlikely((vma->vm_flags & (VM_WRITE|VM_SHARED)) ==
  2315. (VM_WRITE|VM_SHARED))) {
  2316. /*
  2317. * Only catch write-faults on shared writable pages,
  2318. * read-only shared pages can get COWed by
  2319. * get_user_pages(.write=1, .force=1).
  2320. */
  2321. if (vma->vm_ops && vma->vm_ops->page_mkwrite) {
  2322. struct vm_fault vmf;
  2323. int tmp;
  2324. vmf.virtual_address = (void __user *)(address &
  2325. PAGE_MASK);
  2326. vmf.pgoff = old_page->index;
  2327. vmf.flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE;
  2328. vmf.page = old_page;
  2329. /*
  2330. * Notify the address space that the page is about to
  2331. * become writable so that it can prohibit this or wait
  2332. * for the page to get into an appropriate state.
  2333. *
  2334. * We do this without the lock held, so that it can
  2335. * sleep if it needs to.
  2336. */
  2337. page_cache_get(old_page);
  2338. pte_unmap_unlock(page_table, ptl);
  2339. tmp = vma->vm_ops->page_mkwrite(vma, &vmf);
  2340. if (unlikely(tmp &
  2341. (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) {
  2342. ret = tmp;
  2343. goto unwritable_page;
  2344. }
  2345. if (unlikely(!(tmp & VM_FAULT_LOCKED))) {
  2346. lock_page(old_page);
  2347. if (!old_page->mapping) {
  2348. ret = 0; /* retry the fault */
  2349. unlock_page(old_page);
  2350. goto unwritable_page;
  2351. }
  2352. } else
  2353. VM_BUG_ON(!PageLocked(old_page));
  2354. /*
  2355. * Since we dropped the lock we need to revalidate
  2356. * the PTE as someone else may have changed it. If
  2357. * they did, we just return, as we can count on the
  2358. * MMU to tell us if they didn't also make it writable.
  2359. */
  2360. page_table = pte_offset_map_lock(mm, pmd, address,
  2361. &ptl);
  2362. if (!pte_same(*page_table, orig_pte)) {
  2363. unlock_page(old_page);
  2364. goto unlock;
  2365. }
  2366. page_mkwrite = 1;
  2367. }
  2368. dirty_page = old_page;
  2369. get_page(dirty_page);
  2370. reuse:
  2371. flush_cache_page(vma, address, pte_pfn(orig_pte));
  2372. entry = pte_mkyoung(orig_pte);
  2373. entry = maybe_mkwrite(pte_mkdirty(entry), vma);
  2374. if (ptep_set_access_flags(vma, address, page_table, entry,1))
  2375. update_mmu_cache(vma, address, page_table);
  2376. pte_unmap_unlock(page_table, ptl);
  2377. ret |= VM_FAULT_WRITE;
  2378. if (!dirty_page)
  2379. return ret;
  2380. /*
  2381. * Yes, Virginia, this is actually required to prevent a race
  2382. * with clear_page_dirty_for_io() from clearing the page dirty
  2383. * bit after it clear all dirty ptes, but before a racing
  2384. * do_wp_page installs a dirty pte.
  2385. *
  2386. * __do_fault is protected similarly.
  2387. */
  2388. if (!page_mkwrite) {
  2389. wait_on_page_locked(dirty_page);
  2390. set_page_dirty_balance(dirty_page, page_mkwrite);
  2391. /* file_update_time outside page_lock */
  2392. if (vma->vm_file)
  2393. file_update_time(vma->vm_file);
  2394. }
  2395. put_page(dirty_page);
  2396. if (page_mkwrite) {
  2397. struct address_space *mapping = dirty_page->mapping;
  2398. set_page_dirty(dirty_page);
  2399. unlock_page(dirty_page);
  2400. page_cache_release(dirty_page);
  2401. if (mapping) {
  2402. /*
  2403. * Some device drivers do not set page.mapping
  2404. * but still dirty their pages
  2405. */
  2406. balance_dirty_pages_ratelimited(mapping);
  2407. }
  2408. }
  2409. return ret;
  2410. }
  2411. /*
  2412. * Ok, we need to copy. Oh, well..
  2413. */
  2414. page_cache_get(old_page);
  2415. gotten:
  2416. pte_unmap_unlock(page_table, ptl);
  2417. if (unlikely(anon_vma_prepare(vma)))
  2418. goto oom;
  2419. if (is_zero_pfn(pte_pfn(orig_pte))) {
  2420. new_page = alloc_zeroed_user_highpage_movable(vma, address);
  2421. if (!new_page)
  2422. goto oom;
  2423. } else {
  2424. new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address);
  2425. if (!new_page)
  2426. goto oom;
  2427. cow_user_page(new_page, old_page, address, vma);
  2428. }
  2429. __SetPageUptodate(new_page);
  2430. if (mem_cgroup_newpage_charge(new_page, mm, GFP_KERNEL))
  2431. goto oom_free_new;
  2432. mmun_start = address & PAGE_MASK;
  2433. mmun_end = mmun_start + PAGE_SIZE;
  2434. mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
  2435. /*
  2436. * Re-check the pte - we dropped the lock
  2437. */
  2438. page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
  2439. if (likely(pte_same(*page_table, orig_pte))) {
  2440. if (old_page) {
  2441. if (!PageAnon(old_page)) {
  2442. dec_mm_counter_fast(mm, MM_FILEPAGES);
  2443. inc_mm_counter_fast(mm, MM_ANONPAGES);
  2444. }
  2445. } else
  2446. inc_mm_counter_fast(mm, MM_ANONPAGES);
  2447. flush_cache_page(vma, address, pte_pfn(orig_pte));
  2448. entry = mk_pte(new_page, vma->vm_page_prot);
  2449. entry = maybe_mkwrite(pte_mkdirty(entry), vma);
  2450. /*
  2451. * Clear the pte entry and flush it first, before updating the
  2452. * pte with the new entry. This will avoid a race condition
  2453. * seen in the presence of one thread doing SMC and another
  2454. * thread doing COW.
  2455. */
  2456. ptep_clear_flush(vma, address, page_table);
  2457. page_add_new_anon_rmap(new_page, vma, address);
  2458. /*
  2459. * We call the notify macro here because, when using secondary
  2460. * mmu page tables (such as kvm shadow page tables), we want the
  2461. * new page to be mapped directly into the secondary page table.
  2462. */
  2463. set_pte_at_notify(mm, address, page_table, entry);
  2464. update_mmu_cache(vma, address, page_table);
  2465. if (old_page) {
  2466. /*
  2467. * Only after switching the pte to the new page may
  2468. * we remove the mapcount here. Otherwise another
  2469. * process may come and find the rmap count decremented
  2470. * before the pte is switched to the new page, and
  2471. * "reuse" the old page writing into it while our pte
  2472. * here still points into it and can be read by other
  2473. * threads.
  2474. *
  2475. * The critical issue is to order this
  2476. * page_remove_rmap with the ptp_clear_flush above.
  2477. * Those stores are ordered by (if nothing else,)
  2478. * the barrier present in the atomic_add_negative
  2479. * in page_remove_rmap.
  2480. *
  2481. * Then the TLB flush in ptep_clear_flush ensures that
  2482. * no process can access the old page before the
  2483. * decremented mapcount is visible. And the old page
  2484. * cannot be reused until after the decremented
  2485. * mapcount is visible. So transitively, TLBs to
  2486. * old page will be flushed before it can be reused.
  2487. */
  2488. page_remove_rmap(old_page);
  2489. }
  2490. /* Free the old page.. */
  2491. new_page = old_page;
  2492. ret |= VM_FAULT_WRITE;
  2493. } else
  2494. mem_cgroup_uncharge_page(new_page);
  2495. if (new_page)
  2496. page_cache_release(new_page);
  2497. unlock:
  2498. pte_unmap_unlock(page_table, ptl);
  2499. if (mmun_end > mmun_start)
  2500. mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
  2501. if (old_page) {
  2502. /*
  2503. * Don't let another task, with possibly unlocked vma,
  2504. * keep the mlocked page.
  2505. */
  2506. if ((ret & VM_FAULT_WRITE) && (vma->vm_flags & VM_LOCKED)) {
  2507. lock_page(old_page); /* LRU manipulation */
  2508. munlock_vma_page(old_page);
  2509. unlock_page(old_page);
  2510. }
  2511. page_cache_release(old_page);
  2512. }
  2513. return ret;
  2514. oom_free_new:
  2515. page_cache_release(new_page);
  2516. oom:
  2517. if (old_page) {
  2518. if (page_mkwrite) {
  2519. unlock_page(old_page);
  2520. page_cache_release(old_page);
  2521. }
  2522. page_cache_release(old_page);
  2523. }
  2524. return VM_FAULT_OOM;
  2525. unwritable_page:
  2526. page_cache_release(old_page);
  2527. return ret;
  2528. }
  2529. static void unmap_mapping_range_vma(struct vm_area_struct *vma,
  2530. unsigned long start_addr, unsigned long end_addr,
  2531. struct zap_details *details)
  2532. {
  2533. zap_page_range_single(vma, start_addr, end_addr - start_addr, details);
  2534. }
  2535. static inline void unmap_mapping_range_tree(struct rb_root *root,
  2536. struct zap_details *details)
  2537. {
  2538. struct vm_area_struct *vma;
  2539. pgoff_t vba, vea, zba, zea;
  2540. vma_interval_tree_foreach(vma, root,
  2541. details->first_index, details->last_index) {
  2542. vba = vma->vm_pgoff;
  2543. vea = vba + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) - 1;
  2544. /* Assume for now that PAGE_CACHE_SHIFT == PAGE_SHIFT */
  2545. zba = details->first_index;
  2546. if (zba < vba)
  2547. zba = vba;
  2548. zea = details->last_index;
  2549. if (zea > vea)
  2550. zea = vea;
  2551. unmap_mapping_range_vma(vma,
  2552. ((zba - vba) << PAGE_SHIFT) + vma->vm_start,
  2553. ((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start,
  2554. details);
  2555. }
  2556. }
  2557. static inline void unmap_mapping_range_list(struct list_head *head,
  2558. struct zap_details *details)
  2559. {
  2560. struct vm_area_struct *vma;
  2561. /*
  2562. * In nonlinear VMAs there is no correspondence between virtual address
  2563. * offset and file offset. So we must perform an exhaustive search
  2564. * across *all* the pages in each nonlinear VMA, not just the pages
  2565. * whose virtual address lies outside the file truncation point.
  2566. */
  2567. list_for_each_entry(vma, head, shared.nonlinear) {
  2568. details->nonlinear_vma = vma;
  2569. unmap_mapping_range_vma(vma, vma->vm_start, vma->vm_end, details);
  2570. }
  2571. }
  2572. /**
  2573. * unmap_mapping_range - unmap the portion of all mmaps in the specified address_space corresponding to the specified page range in the underlying file.
  2574. * @mapping: the address space containing mmaps to be unmapped.
  2575. * @holebegin: byte in first page to unmap, relative to the start of
  2576. * the underlying file. This will be rounded down to a PAGE_SIZE
  2577. * boundary. Note that this is different from truncate_pagecache(), which
  2578. * must keep the partial page. In contrast, we must get rid of
  2579. * partial pages.
  2580. * @holelen: size of prospective hole in bytes. This will be rounded
  2581. * up to a PAGE_SIZE boundary. A holelen of zero truncates to the
  2582. * end of the file.
  2583. * @even_cows: 1 when truncating a file, unmap even private COWed pages;
  2584. * but 0 when invalidating pagecache, don't throw away private data.
  2585. */
  2586. void unmap_mapping_range(struct address_space *mapping,
  2587. loff_t const holebegin, loff_t const holelen, int even_cows)
  2588. {
  2589. struct zap_details details;
  2590. pgoff_t hba = holebegin >> PAGE_SHIFT;
  2591. pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
  2592. /* Check for overflow. */
  2593. if (sizeof(holelen) > sizeof(hlen)) {
  2594. long long holeend =
  2595. (holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
  2596. if (holeend & ~(long long)ULONG_MAX)
  2597. hlen = ULONG_MAX - hba + 1;
  2598. }
  2599. details.check_mapping = even_cows? NULL: mapping;
  2600. details.nonlinear_vma = NULL;
  2601. details.first_index = hba;
  2602. details.last_index = hba + hlen - 1;
  2603. if (details.last_index < details.first_index)
  2604. details.last_index = ULONG_MAX;
  2605. mutex_lock(&mapping->i_mmap_mutex);
  2606. if (unlikely(!RB_EMPTY_ROOT(&mapping->i_mmap)))
  2607. unmap_mapping_range_tree(&mapping->i_mmap, &details);
  2608. if (unlikely(!list_empty(&mapping->i_mmap_nonlinear)))
  2609. unmap_mapping_range_list(&mapping->i_mmap_nonlinear, &details);
  2610. mutex_unlock(&mapping->i_mmap_mutex);
  2611. }
  2612. EXPORT_SYMBOL(unmap_mapping_range);
  2613. /*
  2614. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  2615. * but allow concurrent faults), and pte mapped but not yet locked.
  2616. * We return with mmap_sem still held, but pte unmapped and unlocked.
  2617. */
  2618. static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma,
  2619. unsigned long address, pte_t *page_table, pmd_t *pmd,
  2620. unsigned int flags, pte_t orig_pte)
  2621. {
  2622. spinlock_t *ptl;
  2623. struct page *page, *swapcache = NULL;
  2624. swp_entry_t entry;
  2625. pte_t pte;
  2626. int locked;
  2627. struct mem_cgroup *ptr;
  2628. int exclusive = 0;
  2629. int ret = 0;
  2630. if (!pte_unmap_same(mm, pmd, page_table, orig_pte))
  2631. goto out;
  2632. entry = pte_to_swp_entry(orig_pte);
  2633. if (unlikely(non_swap_entry(entry))) {
  2634. if (is_migration_entry(entry)) {
  2635. migration_entry_wait(mm, pmd, address);
  2636. } else if (is_hwpoison_entry(entry)) {
  2637. ret = VM_FAULT_HWPOISON;
  2638. } else {
  2639. print_bad_pte(vma, address, orig_pte, NULL);
  2640. ret = VM_FAULT_SIGBUS;
  2641. }
  2642. goto out;
  2643. }
  2644. delayacct_set_flag(DELAYACCT_PF_SWAPIN);
  2645. page = lookup_swap_cache(entry);
  2646. if (!page) {
  2647. page = swapin_readahead(entry,
  2648. GFP_HIGHUSER_MOVABLE, vma, address);
  2649. if (!page) {
  2650. /*
  2651. * Back out if somebody else faulted in this pte
  2652. * while we released the pte lock.
  2653. */
  2654. page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
  2655. if (likely(pte_same(*page_table, orig_pte)))
  2656. ret = VM_FAULT_OOM;
  2657. delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
  2658. goto unlock;
  2659. }
  2660. /* Had to read the page from swap area: Major fault */
  2661. ret = VM_FAULT_MAJOR;
  2662. count_vm_event(PGMAJFAULT);
  2663. mem_cgroup_count_vm_event(mm, PGMAJFAULT);
  2664. } else if (PageHWPoison(page)) {
  2665. /*
  2666. * hwpoisoned dirty swapcache pages are kept for killing
  2667. * owner processes (which may be unknown at hwpoison time)
  2668. */
  2669. ret = VM_FAULT_HWPOISON;
  2670. delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
  2671. goto out_release;
  2672. }
  2673. locked = lock_page_or_retry(page, mm, flags);
  2674. delayacct_clear_flag(DELAYACCT_PF_SWAPIN);
  2675. if (!locked) {
  2676. ret |= VM_FAULT_RETRY;
  2677. goto out_release;
  2678. }
  2679. /*
  2680. * Make sure try_to_free_swap or reuse_swap_page or swapoff did not
  2681. * release the swapcache from under us. The page pin, and pte_same
  2682. * test below, are not enough to exclude that. Even if it is still
  2683. * swapcache, we need to check that the page's swap has not changed.
  2684. */
  2685. if (unlikely(!PageSwapCache(page) || page_private(page) != entry.val))
  2686. goto out_page;
  2687. if (ksm_might_need_to_copy(page, vma, address)) {
  2688. swapcache = page;
  2689. page = ksm_does_need_to_copy(page, vma, address);
  2690. if (unlikely(!page)) {
  2691. ret = VM_FAULT_OOM;
  2692. page = swapcache;
  2693. swapcache = NULL;
  2694. goto out_page;
  2695. }
  2696. }
  2697. if (mem_cgroup_try_charge_swapin(mm, page, GFP_KERNEL, &ptr)) {
  2698. ret = VM_FAULT_OOM;
  2699. goto out_page;
  2700. }
  2701. /*
  2702. * Back out if somebody else already faulted in this pte.
  2703. */
  2704. page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
  2705. if (unlikely(!pte_same(*page_table, orig_pte)))
  2706. goto out_nomap;
  2707. if (unlikely(!PageUptodate(page))) {
  2708. ret = VM_FAULT_SIGBUS;
  2709. goto out_nomap;
  2710. }
  2711. /*
  2712. * The page isn't present yet, go ahead with the fault.
  2713. *
  2714. * Be careful about the sequence of operations here.
  2715. * To get its accounting right, reuse_swap_page() must be called
  2716. * while the page is counted on swap but not yet in mapcount i.e.
  2717. * before page_add_anon_rmap() and swap_free(); try_to_free_swap()
  2718. * must be called after the swap_free(), or it will never succeed.
  2719. * Because delete_from_swap_page() may be called by reuse_swap_page(),
  2720. * mem_cgroup_commit_charge_swapin() may not be able to find swp_entry
  2721. * in page->private. In this case, a record in swap_cgroup is silently
  2722. * discarded at swap_free().
  2723. */
  2724. inc_mm_counter_fast(mm, MM_ANONPAGES);
  2725. dec_mm_counter_fast(mm, MM_SWAPENTS);
  2726. pte = mk_pte(page, vma->vm_page_prot);
  2727. if ((flags & FAULT_FLAG_WRITE) && reuse_swap_page(page)) {
  2728. pte = maybe_mkwrite(pte_mkdirty(pte), vma);
  2729. flags &= ~FAULT_FLAG_WRITE;
  2730. ret |= VM_FAULT_WRITE;
  2731. exclusive = 1;
  2732. }
  2733. flush_icache_page(vma, page);
  2734. set_pte_at(mm, address, page_table, pte);
  2735. do_page_add_anon_rmap(page, vma, address, exclusive);
  2736. /* It's better to call commit-charge after rmap is established */
  2737. mem_cgroup_commit_charge_swapin(page, ptr);
  2738. swap_free(entry);
  2739. if (vm_swap_full() || (vma->vm_flags & VM_LOCKED) || PageMlocked(page))
  2740. try_to_free_swap(page);
  2741. unlock_page(page);
  2742. if (swapcache) {
  2743. /*
  2744. * Hold the lock to avoid the swap entry to be reused
  2745. * until we take the PT lock for the pte_same() check
  2746. * (to avoid false positives from pte_same). For
  2747. * further safety release the lock after the swap_free
  2748. * so that the swap count won't change under a
  2749. * parallel locked swapcache.
  2750. */
  2751. unlock_page(swapcache);
  2752. page_cache_release(swapcache);
  2753. }
  2754. if (flags & FAULT_FLAG_WRITE) {
  2755. ret |= do_wp_page(mm, vma, address, page_table, pmd, ptl, pte);
  2756. if (ret & VM_FAULT_ERROR)
  2757. ret &= VM_FAULT_ERROR;
  2758. goto out;
  2759. }
  2760. /* No need to invalidate - it was non-present before */
  2761. update_mmu_cache(vma, address, page_table);
  2762. unlock:
  2763. pte_unmap_unlock(page_table, ptl);
  2764. out:
  2765. return ret;
  2766. out_nomap:
  2767. mem_cgroup_cancel_charge_swapin(ptr);
  2768. pte_unmap_unlock(page_table, ptl);
  2769. out_page:
  2770. unlock_page(page);
  2771. out_release:
  2772. page_cache_release(page);
  2773. if (swapcache) {
  2774. unlock_page(swapcache);
  2775. page_cache_release(swapcache);
  2776. }
  2777. return ret;
  2778. }
  2779. /*
  2780. * This is like a special single-page "expand_{down|up}wards()",
  2781. * except we must first make sure that 'address{-|+}PAGE_SIZE'
  2782. * doesn't hit another vma.
  2783. */
  2784. static inline int check_stack_guard_page(struct vm_area_struct *vma, unsigned long address)
  2785. {
  2786. address &= PAGE_MASK;
  2787. if ((vma->vm_flags & VM_GROWSDOWN) && address == vma->vm_start) {
  2788. struct vm_area_struct *prev = vma->vm_prev;
  2789. /*
  2790. * Is there a mapping abutting this one below?
  2791. *
  2792. * That's only ok if it's the same stack mapping
  2793. * that has gotten split..
  2794. */
  2795. if (prev && prev->vm_end == address)
  2796. return prev->vm_flags & VM_GROWSDOWN ? 0 : -ENOMEM;
  2797. expand_downwards(vma, address - PAGE_SIZE);
  2798. }
  2799. if ((vma->vm_flags & VM_GROWSUP) && address + PAGE_SIZE == vma->vm_end) {
  2800. struct vm_area_struct *next = vma->vm_next;
  2801. /* As VM_GROWSDOWN but s/below/above/ */
  2802. if (next && next->vm_start == address + PAGE_SIZE)
  2803. return next->vm_flags & VM_GROWSUP ? 0 : -ENOMEM;
  2804. expand_upwards(vma, address + PAGE_SIZE);
  2805. }
  2806. return 0;
  2807. }
  2808. /*
  2809. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  2810. * but allow concurrent faults), and pte mapped but not yet locked.
  2811. * We return with mmap_sem still held, but pte unmapped and unlocked.
  2812. */
  2813. static int do_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
  2814. unsigned long address, pte_t *page_table, pmd_t *pmd,
  2815. unsigned int flags)
  2816. {
  2817. struct page *page;
  2818. spinlock_t *ptl;
  2819. pte_t entry;
  2820. pte_unmap(page_table);
  2821. /* Check if we need to add a guard page to the stack */
  2822. if (check_stack_guard_page(vma, address) < 0)
  2823. return VM_FAULT_SIGBUS;
  2824. /* Use the zero-page for reads */
  2825. if (!(flags & FAULT_FLAG_WRITE)) {
  2826. entry = pte_mkspecial(pfn_pte(my_zero_pfn(address),
  2827. vma->vm_page_prot));
  2828. page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
  2829. if (!pte_none(*page_table))
  2830. goto unlock;
  2831. goto setpte;
  2832. }
  2833. /* Allocate our own private page. */
  2834. if (unlikely(anon_vma_prepare(vma)))
  2835. goto oom;
  2836. page = alloc_zeroed_user_highpage_movable(vma, address);
  2837. if (!page)
  2838. goto oom;
  2839. __SetPageUptodate(page);
  2840. if (mem_cgroup_newpage_charge(page, mm, GFP_KERNEL))
  2841. goto oom_free_page;
  2842. entry = mk_pte(page, vma->vm_page_prot);
  2843. if (vma->vm_flags & VM_WRITE)
  2844. entry = pte_mkwrite(pte_mkdirty(entry));
  2845. page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
  2846. if (!pte_none(*page_table))
  2847. goto release;
  2848. inc_mm_counter_fast(mm, MM_ANONPAGES);
  2849. page_add_new_anon_rmap(page, vma, address);
  2850. setpte:
  2851. set_pte_at(mm, address, page_table, entry);
  2852. /* No need to invalidate - it was non-present before */
  2853. update_mmu_cache(vma, address, page_table);
  2854. unlock:
  2855. pte_unmap_unlock(page_table, ptl);
  2856. return 0;
  2857. release:
  2858. mem_cgroup_uncharge_page(page);
  2859. page_cache_release(page);
  2860. goto unlock;
  2861. oom_free_page:
  2862. page_cache_release(page);
  2863. oom:
  2864. return VM_FAULT_OOM;
  2865. }
  2866. /*
  2867. * __do_fault() tries to create a new page mapping. It aggressively
  2868. * tries to share with existing pages, but makes a separate copy if
  2869. * the FAULT_FLAG_WRITE is set in the flags parameter in order to avoid
  2870. * the next page fault.
  2871. *
  2872. * As this is called only for pages that do not currently exist, we
  2873. * do not need to flush old virtual caches or the TLB.
  2874. *
  2875. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  2876. * but allow concurrent faults), and pte neither mapped nor locked.
  2877. * We return with mmap_sem still held, but pte unmapped and unlocked.
  2878. */
  2879. static int __do_fault(struct mm_struct *mm, struct vm_area_struct *vma,
  2880. unsigned long address, pmd_t *pmd,
  2881. pgoff_t pgoff, unsigned int flags, pte_t orig_pte)
  2882. {
  2883. pte_t *page_table;
  2884. spinlock_t *ptl;
  2885. struct page *page;
  2886. struct page *cow_page;
  2887. pte_t entry;
  2888. int anon = 0;
  2889. struct page *dirty_page = NULL;
  2890. struct vm_fault vmf;
  2891. int ret;
  2892. int page_mkwrite = 0;
  2893. /*
  2894. * If we do COW later, allocate page befor taking lock_page()
  2895. * on the file cache page. This will reduce lock holding time.
  2896. */
  2897. if ((flags & FAULT_FLAG_WRITE) && !(vma->vm_flags & VM_SHARED)) {
  2898. if (unlikely(anon_vma_prepare(vma)))
  2899. return VM_FAULT_OOM;
  2900. cow_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, address);
  2901. if (!cow_page)
  2902. return VM_FAULT_OOM;
  2903. if (mem_cgroup_newpage_charge(cow_page, mm, GFP_KERNEL)) {
  2904. page_cache_release(cow_page);
  2905. return VM_FAULT_OOM;
  2906. }
  2907. } else
  2908. cow_page = NULL;
  2909. vmf.virtual_address = (void __user *)(address & PAGE_MASK);
  2910. vmf.pgoff = pgoff;
  2911. vmf.flags = flags;
  2912. vmf.page = NULL;
  2913. ret = vma->vm_ops->fault(vma, &vmf);
  2914. if (unlikely(ret & (VM_FAULT_ERROR | VM_FAULT_NOPAGE |
  2915. VM_FAULT_RETRY)))
  2916. goto uncharge_out;
  2917. if (unlikely(PageHWPoison(vmf.page))) {
  2918. if (ret & VM_FAULT_LOCKED)
  2919. unlock_page(vmf.page);
  2920. ret = VM_FAULT_HWPOISON;
  2921. goto uncharge_out;
  2922. }
  2923. /*
  2924. * For consistency in subsequent calls, make the faulted page always
  2925. * locked.
  2926. */
  2927. if (unlikely(!(ret & VM_FAULT_LOCKED)))
  2928. lock_page(vmf.page);
  2929. else
  2930. VM_BUG_ON(!PageLocked(vmf.page));
  2931. /*
  2932. * Should we do an early C-O-W break?
  2933. */
  2934. page = vmf.page;
  2935. if (flags & FAULT_FLAG_WRITE) {
  2936. if (!(vma->vm_flags & VM_SHARED)) {
  2937. page = cow_page;
  2938. anon = 1;
  2939. copy_user_highpage(page, vmf.page, address, vma);
  2940. __SetPageUptodate(page);
  2941. } else {
  2942. /*
  2943. * If the page will be shareable, see if the backing
  2944. * address space wants to know that the page is about
  2945. * to become writable
  2946. */
  2947. if (vma->vm_ops->page_mkwrite) {
  2948. int tmp;
  2949. unlock_page(page);
  2950. vmf.flags = FAULT_FLAG_WRITE|FAULT_FLAG_MKWRITE;
  2951. tmp = vma->vm_ops->page_mkwrite(vma, &vmf);
  2952. if (unlikely(tmp &
  2953. (VM_FAULT_ERROR | VM_FAULT_NOPAGE))) {
  2954. ret = tmp;
  2955. goto unwritable_page;
  2956. }
  2957. if (unlikely(!(tmp & VM_FAULT_LOCKED))) {
  2958. lock_page(page);
  2959. if (!page->mapping) {
  2960. ret = 0; /* retry the fault */
  2961. unlock_page(page);
  2962. goto unwritable_page;
  2963. }
  2964. } else
  2965. VM_BUG_ON(!PageLocked(page));
  2966. page_mkwrite = 1;
  2967. }
  2968. }
  2969. }
  2970. page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
  2971. /*
  2972. * This silly early PAGE_DIRTY setting removes a race
  2973. * due to the bad i386 page protection. But it's valid
  2974. * for other architectures too.
  2975. *
  2976. * Note that if FAULT_FLAG_WRITE is set, we either now have
  2977. * an exclusive copy of the page, or this is a shared mapping,
  2978. * so we can make it writable and dirty to avoid having to
  2979. * handle that later.
  2980. */
  2981. /* Only go through if we didn't race with anybody else... */
  2982. if (likely(pte_same(*page_table, orig_pte))) {
  2983. flush_icache_page(vma, page);
  2984. entry = mk_pte(page, vma->vm_page_prot);
  2985. if (flags & FAULT_FLAG_WRITE)
  2986. entry = maybe_mkwrite(pte_mkdirty(entry), vma);
  2987. if (anon) {
  2988. inc_mm_counter_fast(mm, MM_ANONPAGES);
  2989. page_add_new_anon_rmap(page, vma, address);
  2990. } else {
  2991. inc_mm_counter_fast(mm, MM_FILEPAGES);
  2992. page_add_file_rmap(page);
  2993. if (flags & FAULT_FLAG_WRITE) {
  2994. dirty_page = page;
  2995. get_page(dirty_page);
  2996. }
  2997. }
  2998. set_pte_at(mm, address, page_table, entry);
  2999. /* no need to invalidate: a not-present page won't be cached */
  3000. update_mmu_cache(vma, address, page_table);
  3001. } else {
  3002. if (cow_page)
  3003. mem_cgroup_uncharge_page(cow_page);
  3004. if (anon)
  3005. page_cache_release(page);
  3006. else
  3007. anon = 1; /* no anon but release faulted_page */
  3008. }
  3009. pte_unmap_unlock(page_table, ptl);
  3010. if (dirty_page) {
  3011. struct address_space *mapping = page->mapping;
  3012. int dirtied = 0;
  3013. if (set_page_dirty(dirty_page))
  3014. dirtied = 1;
  3015. unlock_page(dirty_page);
  3016. put_page(dirty_page);
  3017. if ((dirtied || page_mkwrite) && mapping) {
  3018. /*
  3019. * Some device drivers do not set page.mapping but still
  3020. * dirty their pages
  3021. */
  3022. balance_dirty_pages_ratelimited(mapping);
  3023. }
  3024. /* file_update_time outside page_lock */
  3025. if (vma->vm_file && !page_mkwrite)
  3026. file_update_time(vma->vm_file);
  3027. } else {
  3028. unlock_page(vmf.page);
  3029. if (anon)
  3030. page_cache_release(vmf.page);
  3031. }
  3032. return ret;
  3033. unwritable_page:
  3034. page_cache_release(page);
  3035. return ret;
  3036. uncharge_out:
  3037. /* fs's fault handler get error */
  3038. if (cow_page) {
  3039. mem_cgroup_uncharge_page(cow_page);
  3040. page_cache_release(cow_page);
  3041. }
  3042. return ret;
  3043. }
  3044. static int do_linear_fault(struct mm_struct *mm, struct vm_area_struct *vma,
  3045. unsigned long address, pte_t *page_table, pmd_t *pmd,
  3046. unsigned int flags, pte_t orig_pte)
  3047. {
  3048. pgoff_t pgoff = (((address & PAGE_MASK)
  3049. - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
  3050. pte_unmap(page_table);
  3051. return __do_fault(mm, vma, address, pmd, pgoff, flags, orig_pte);
  3052. }
  3053. /*
  3054. * Fault of a previously existing named mapping. Repopulate the pte
  3055. * from the encoded file_pte if possible. This enables swappable
  3056. * nonlinear vmas.
  3057. *
  3058. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  3059. * but allow concurrent faults), and pte mapped but not yet locked.
  3060. * We return with mmap_sem still held, but pte unmapped and unlocked.
  3061. */
  3062. static int do_nonlinear_fault(struct mm_struct *mm, struct vm_area_struct *vma,
  3063. unsigned long address, pte_t *page_table, pmd_t *pmd,
  3064. unsigned int flags, pte_t orig_pte)
  3065. {
  3066. pgoff_t pgoff;
  3067. flags |= FAULT_FLAG_NONLINEAR;
  3068. if (!pte_unmap_same(mm, pmd, page_table, orig_pte))
  3069. return 0;
  3070. if (unlikely(!(vma->vm_flags & VM_NONLINEAR))) {
  3071. /*
  3072. * Page table corrupted: show pte and kill process.
  3073. */
  3074. print_bad_pte(vma, address, orig_pte, NULL);
  3075. return VM_FAULT_SIGBUS;
  3076. }
  3077. pgoff = pte_to_pgoff(orig_pte);
  3078. return __do_fault(mm, vma, address, pmd, pgoff, flags, orig_pte);
  3079. }
  3080. /*
  3081. * These routines also need to handle stuff like marking pages dirty
  3082. * and/or accessed for architectures that don't do it in hardware (most
  3083. * RISC architectures). The early dirtying is also good on the i386.
  3084. *
  3085. * There is also a hook called "update_mmu_cache()" that architectures
  3086. * with external mmu caches can use to update those (ie the Sparc or
  3087. * PowerPC hashed page tables that act as extended TLBs).
  3088. *
  3089. * We enter with non-exclusive mmap_sem (to exclude vma changes,
  3090. * but allow concurrent faults), and pte mapped but not yet locked.
  3091. * We return with mmap_sem still held, but pte unmapped and unlocked.
  3092. */
  3093. int handle_pte_fault(struct mm_struct *mm,
  3094. struct vm_area_struct *vma, unsigned long address,
  3095. pte_t *pte, pmd_t *pmd, unsigned int flags)
  3096. {
  3097. pte_t entry;
  3098. spinlock_t *ptl;
  3099. entry = *pte;
  3100. if (!pte_present(entry)) {
  3101. if (pte_none(entry)) {
  3102. if (vma->vm_ops) {
  3103. if (likely(vma->vm_ops->fault))
  3104. return do_linear_fault(mm, vma, address,
  3105. pte, pmd, flags, entry);
  3106. }
  3107. return do_anonymous_page(mm, vma, address,
  3108. pte, pmd, flags);
  3109. }
  3110. if (pte_file(entry))
  3111. return do_nonlinear_fault(mm, vma, address,
  3112. pte, pmd, flags, entry);
  3113. return do_swap_page(mm, vma, address,
  3114. pte, pmd, flags, entry);
  3115. }
  3116. ptl = pte_lockptr(mm, pmd);
  3117. spin_lock(ptl);
  3118. if (unlikely(!pte_same(*pte, entry)))
  3119. goto unlock;
  3120. if (flags & FAULT_FLAG_WRITE) {
  3121. if (!pte_write(entry))
  3122. return do_wp_page(mm, vma, address,
  3123. pte, pmd, ptl, entry);
  3124. entry = pte_mkdirty(entry);
  3125. }
  3126. entry = pte_mkyoung(entry);
  3127. if (ptep_set_access_flags(vma, address, pte, entry, flags & FAULT_FLAG_WRITE)) {
  3128. update_mmu_cache(vma, address, pte);
  3129. } else {
  3130. /*
  3131. * This is needed only for protection faults but the arch code
  3132. * is not yet telling us if this is a protection fault or not.
  3133. * This still avoids useless tlb flushes for .text page faults
  3134. * with threads.
  3135. */
  3136. if (flags & FAULT_FLAG_WRITE)
  3137. flush_tlb_fix_spurious_fault(vma, address);
  3138. }
  3139. unlock:
  3140. pte_unmap_unlock(pte, ptl);
  3141. return 0;
  3142. }
  3143. /*
  3144. * By the time we get here, we already hold the mm semaphore
  3145. */
  3146. int handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma,
  3147. unsigned long address, unsigned int flags)
  3148. {
  3149. pgd_t *pgd;
  3150. pud_t *pud;
  3151. pmd_t *pmd;
  3152. pte_t *pte;
  3153. __set_current_state(TASK_RUNNING);
  3154. count_vm_event(PGFAULT);
  3155. mem_cgroup_count_vm_event(mm, PGFAULT);
  3156. /* do counter updates before entering really critical section. */
  3157. check_sync_rss_stat(current);
  3158. if (unlikely(is_vm_hugetlb_page(vma)))
  3159. return hugetlb_fault(mm, vma, address, flags);
  3160. retry:
  3161. pgd = pgd_offset(mm, address);
  3162. pud = pud_alloc(mm, pgd, address);
  3163. if (!pud)
  3164. return VM_FAULT_OOM;
  3165. pmd = pmd_alloc(mm, pud, address);
  3166. if (!pmd)
  3167. return VM_FAULT_OOM;
  3168. if (pmd_none(*pmd) && transparent_hugepage_enabled(vma)) {
  3169. if (!vma->vm_ops)
  3170. return do_huge_pmd_anonymous_page(mm, vma, address,
  3171. pmd, flags);
  3172. } else {
  3173. pmd_t orig_pmd = *pmd;
  3174. int ret;
  3175. barrier();
  3176. if (pmd_trans_huge(orig_pmd)) {
  3177. if (flags & FAULT_FLAG_WRITE &&
  3178. !pmd_write(orig_pmd) &&
  3179. !pmd_trans_splitting(orig_pmd)) {
  3180. ret = do_huge_pmd_wp_page(mm, vma, address, pmd,
  3181. orig_pmd);
  3182. /*
  3183. * If COW results in an oom, the huge pmd will
  3184. * have been split, so retry the fault on the
  3185. * pte for a smaller charge.
  3186. */
  3187. if (unlikely(ret & VM_FAULT_OOM))
  3188. goto retry;
  3189. return ret;
  3190. }
  3191. return 0;
  3192. }
  3193. }
  3194. /*
  3195. * Use __pte_alloc instead of pte_alloc_map, because we can't
  3196. * run pte_offset_map on the pmd, if an huge pmd could
  3197. * materialize from under us from a different thread.
  3198. */
  3199. if (unlikely(pmd_none(*pmd)) && __pte_alloc(mm, vma, pmd, address))
  3200. return VM_FAULT_OOM;
  3201. /* if an huge pmd materialized from under us just retry later */
  3202. if (unlikely(pmd_trans_huge(*pmd)))
  3203. return 0;
  3204. /*
  3205. * A regular pmd is established and it can't morph into a huge pmd
  3206. * from under us anymore at this point because we hold the mmap_sem
  3207. * read mode and khugepaged takes it in write mode. So now it's
  3208. * safe to run pte_offset_map().
  3209. */
  3210. pte = pte_offset_map(pmd, address);
  3211. return handle_pte_fault(mm, vma, address, pte, pmd, flags);
  3212. }
  3213. #ifndef __PAGETABLE_PUD_FOLDED
  3214. /*
  3215. * Allocate page upper directory.
  3216. * We've already handled the fast-path in-line.
  3217. */
  3218. int __pud_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
  3219. {
  3220. pud_t *new = pud_alloc_one(mm, address);
  3221. if (!new)
  3222. return -ENOMEM;
  3223. smp_wmb(); /* See comment in __pte_alloc */
  3224. spin_lock(&mm->page_table_lock);
  3225. if (pgd_present(*pgd)) /* Another has populated it */
  3226. pud_free(mm, new);
  3227. else
  3228. pgd_populate(mm, pgd, new);
  3229. spin_unlock(&mm->page_table_lock);
  3230. return 0;
  3231. }
  3232. #endif /* __PAGETABLE_PUD_FOLDED */
  3233. #ifndef __PAGETABLE_PMD_FOLDED
  3234. /*
  3235. * Allocate page middle directory.
  3236. * We've already handled the fast-path in-line.
  3237. */
  3238. int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address)
  3239. {
  3240. pmd_t *new = pmd_alloc_one(mm, address);
  3241. if (!new)
  3242. return -ENOMEM;
  3243. smp_wmb(); /* See comment in __pte_alloc */
  3244. spin_lock(&mm->page_table_lock);
  3245. #ifndef __ARCH_HAS_4LEVEL_HACK
  3246. if (pud_present(*pud)) /* Another has populated it */
  3247. pmd_free(mm, new);
  3248. else
  3249. pud_populate(mm, pud, new);
  3250. #else
  3251. if (pgd_present(*pud)) /* Another has populated it */
  3252. pmd_free(mm, new);
  3253. else
  3254. pgd_populate(mm, pud, new);
  3255. #endif /* __ARCH_HAS_4LEVEL_HACK */
  3256. spin_unlock(&mm->page_table_lock);
  3257. return 0;
  3258. }
  3259. #endif /* __PAGETABLE_PMD_FOLDED */
  3260. int make_pages_present(unsigned long addr, unsigned long end)
  3261. {
  3262. int ret, len, write;
  3263. struct vm_area_struct * vma;
  3264. vma = find_vma(current->mm, addr);
  3265. if (!vma)
  3266. return -ENOMEM;
  3267. /*
  3268. * We want to touch writable mappings with a write fault in order
  3269. * to break COW, except for shared mappings because these don't COW
  3270. * and we would not want to dirty them for nothing.
  3271. */
  3272. write = (vma->vm_flags & (VM_WRITE | VM_SHARED)) == VM_WRITE;
  3273. BUG_ON(addr >= end);
  3274. BUG_ON(end > vma->vm_end);
  3275. len = DIV_ROUND_UP(end, PAGE_SIZE) - addr/PAGE_SIZE;
  3276. ret = get_user_pages(current, current->mm, addr,
  3277. len, write, 0, NULL, NULL);
  3278. if (ret < 0)
  3279. return ret;
  3280. return ret == len ? 0 : -EFAULT;
  3281. }
  3282. #if !defined(__HAVE_ARCH_GATE_AREA)
  3283. #if defined(AT_SYSINFO_EHDR)
  3284. static struct vm_area_struct gate_vma;
  3285. static int __init gate_vma_init(void)
  3286. {
  3287. gate_vma.vm_mm = NULL;
  3288. gate_vma.vm_start = FIXADDR_USER_START;
  3289. gate_vma.vm_end = FIXADDR_USER_END;
  3290. gate_vma.vm_flags = VM_READ | VM_MAYREAD | VM_EXEC | VM_MAYEXEC;
  3291. gate_vma.vm_page_prot = __P101;
  3292. return 0;
  3293. }
  3294. __initcall(gate_vma_init);
  3295. #endif
  3296. struct vm_area_struct *get_gate_vma(struct mm_struct *mm)
  3297. {
  3298. #ifdef AT_SYSINFO_EHDR
  3299. return &gate_vma;
  3300. #else
  3301. return NULL;
  3302. #endif
  3303. }
  3304. int in_gate_area_no_mm(unsigned long addr)
  3305. {
  3306. #ifdef AT_SYSINFO_EHDR
  3307. if ((addr >= FIXADDR_USER_START) && (addr < FIXADDR_USER_END))
  3308. return 1;
  3309. #endif
  3310. return 0;
  3311. }
  3312. #endif /* __HAVE_ARCH_GATE_AREA */
  3313. static int __follow_pte(struct mm_struct *mm, unsigned long address,
  3314. pte_t **ptepp, spinlock_t **ptlp)
  3315. {
  3316. pgd_t *pgd;
  3317. pud_t *pud;
  3318. pmd_t *pmd;
  3319. pte_t *ptep;
  3320. pgd = pgd_offset(mm, address);
  3321. if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
  3322. goto out;
  3323. pud = pud_offset(pgd, address);
  3324. if (pud_none(*pud) || unlikely(pud_bad(*pud)))
  3325. goto out;
  3326. pmd = pmd_offset(pud, address);
  3327. VM_BUG_ON(pmd_trans_huge(*pmd));
  3328. if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
  3329. goto out;
  3330. /* We cannot handle huge page PFN maps. Luckily they don't exist. */
  3331. if (pmd_huge(*pmd))
  3332. goto out;
  3333. ptep = pte_offset_map_lock(mm, pmd, address, ptlp);
  3334. if (!ptep)
  3335. goto out;
  3336. if (!pte_present(*ptep))
  3337. goto unlock;
  3338. *ptepp = ptep;
  3339. return 0;
  3340. unlock:
  3341. pte_unmap_unlock(ptep, *ptlp);
  3342. out:
  3343. return -EINVAL;
  3344. }
  3345. static inline int follow_pte(struct mm_struct *mm, unsigned long address,
  3346. pte_t **ptepp, spinlock_t **ptlp)
  3347. {
  3348. int res;
  3349. /* (void) is needed to make gcc happy */
  3350. (void) __cond_lock(*ptlp,
  3351. !(res = __follow_pte(mm, address, ptepp, ptlp)));
  3352. return res;
  3353. }
  3354. /**
  3355. * follow_pfn - look up PFN at a user virtual address
  3356. * @vma: memory mapping
  3357. * @address: user virtual address
  3358. * @pfn: location to store found PFN
  3359. *
  3360. * Only IO mappings and raw PFN mappings are allowed.
  3361. *
  3362. * Returns zero and the pfn at @pfn on success, -ve otherwise.
  3363. */
  3364. int follow_pfn(struct vm_area_struct *vma, unsigned long address,
  3365. unsigned long *pfn)
  3366. {
  3367. int ret = -EINVAL;
  3368. spinlock_t *ptl;
  3369. pte_t *ptep;
  3370. if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
  3371. return ret;
  3372. ret = follow_pte(vma->vm_mm, address, &ptep, &ptl);
  3373. if (ret)
  3374. return ret;
  3375. *pfn = pte_pfn(*ptep);
  3376. pte_unmap_unlock(ptep, ptl);
  3377. return 0;
  3378. }
  3379. EXPORT_SYMBOL(follow_pfn);
  3380. #ifdef CONFIG_HAVE_IOREMAP_PROT
  3381. int follow_phys(struct vm_area_struct *vma,
  3382. unsigned long address, unsigned int flags,
  3383. unsigned long *prot, resource_size_t *phys)
  3384. {
  3385. int ret = -EINVAL;
  3386. pte_t *ptep, pte;
  3387. spinlock_t *ptl;
  3388. if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
  3389. goto out;
  3390. if (follow_pte(vma->vm_mm, address, &ptep, &ptl))
  3391. goto out;
  3392. pte = *ptep;
  3393. if ((flags & FOLL_WRITE) && !pte_write(pte))
  3394. goto unlock;
  3395. *prot = pgprot_val(pte_pgprot(pte));
  3396. *phys = (resource_size_t)pte_pfn(pte) << PAGE_SHIFT;
  3397. ret = 0;
  3398. unlock:
  3399. pte_unmap_unlock(ptep, ptl);
  3400. out:
  3401. return ret;
  3402. }
  3403. int generic_access_phys(struct vm_area_struct *vma, unsigned long addr,
  3404. void *buf, int len, int write)
  3405. {
  3406. resource_size_t phys_addr;
  3407. unsigned long prot = 0;
  3408. void __iomem *maddr;
  3409. int offset = addr & (PAGE_SIZE-1);
  3410. if (follow_phys(vma, addr, write, &prot, &phys_addr))
  3411. return -EINVAL;
  3412. maddr = ioremap_prot(phys_addr, PAGE_SIZE, prot);
  3413. if (write)
  3414. memcpy_toio(maddr + offset, buf, len);
  3415. else
  3416. memcpy_fromio(buf, maddr + offset, len);
  3417. iounmap(maddr);
  3418. return len;
  3419. }
  3420. #endif
  3421. /*
  3422. * Access another process' address space as given in mm. If non-NULL, use the
  3423. * given task for page fault accounting.
  3424. */
  3425. static int __access_remote_vm(struct task_struct *tsk, struct mm_struct *mm,
  3426. unsigned long addr, void *buf, int len, int write)
  3427. {
  3428. struct vm_area_struct *vma;
  3429. void *old_buf = buf;
  3430. down_read(&mm->mmap_sem);
  3431. /* ignore errors, just check how much was successfully transferred */
  3432. while (len) {
  3433. int bytes, ret, offset;
  3434. void *maddr;
  3435. struct page *page = NULL;
  3436. ret = get_user_pages(tsk, mm, addr, 1,
  3437. write, 1, &page, &vma);
  3438. if (ret <= 0) {
  3439. /*
  3440. * Check if this is a VM_IO | VM_PFNMAP VMA, which
  3441. * we can access using slightly different code.
  3442. */
  3443. #ifdef CONFIG_HAVE_IOREMAP_PROT
  3444. vma = find_vma(mm, addr);
  3445. if (!vma || vma->vm_start > addr)
  3446. break;
  3447. if (vma->vm_ops && vma->vm_ops->access)
  3448. ret = vma->vm_ops->access(vma, addr, buf,
  3449. len, write);
  3450. if (ret <= 0)
  3451. #endif
  3452. break;
  3453. bytes = ret;
  3454. } else {
  3455. bytes = len;
  3456. offset = addr & (PAGE_SIZE-1);
  3457. if (bytes > PAGE_SIZE-offset)
  3458. bytes = PAGE_SIZE-offset;
  3459. maddr = kmap(page);
  3460. if (write) {
  3461. copy_to_user_page(vma, page, addr,
  3462. maddr + offset, buf, bytes);
  3463. set_page_dirty_lock(page);
  3464. } else {
  3465. copy_from_user_page(vma, page, addr,
  3466. buf, maddr + offset, bytes);
  3467. }
  3468. kunmap(page);
  3469. page_cache_release(page);
  3470. }
  3471. len -= bytes;
  3472. buf += bytes;
  3473. addr += bytes;
  3474. }
  3475. up_read(&mm->mmap_sem);
  3476. return buf - old_buf;
  3477. }
  3478. /**
  3479. * access_remote_vm - access another process' address space
  3480. * @mm: the mm_struct of the target address space
  3481. * @addr: start address to access
  3482. * @buf: source or destination buffer
  3483. * @len: number of bytes to transfer
  3484. * @write: whether the access is a write
  3485. *
  3486. * The caller must hold a reference on @mm.
  3487. */
  3488. int access_remote_vm(struct mm_struct *mm, unsigned long addr,
  3489. void *buf, int len, int write)
  3490. {
  3491. return __access_remote_vm(NULL, mm, addr, buf, len, write);
  3492. }
  3493. /*
  3494. * Access another process' address space.
  3495. * Source/target buffer must be kernel space,
  3496. * Do not walk the page table directly, use get_user_pages
  3497. */
  3498. int access_process_vm(struct task_struct *tsk, unsigned long addr,
  3499. void *buf, int len, int write)
  3500. {
  3501. struct mm_struct *mm;
  3502. int ret;
  3503. mm = get_task_mm(tsk);
  3504. if (!mm)
  3505. return 0;
  3506. ret = __access_remote_vm(tsk, mm, addr, buf, len, write);
  3507. mmput(mm);
  3508. return ret;
  3509. }
  3510. /*
  3511. * Print the name of a VMA.
  3512. */
  3513. void print_vma_addr(char *prefix, unsigned long ip)
  3514. {
  3515. struct mm_struct *mm = current->mm;
  3516. struct vm_area_struct *vma;
  3517. /*
  3518. * Do not print if we are in atomic
  3519. * contexts (in exception stacks, etc.):
  3520. */
  3521. if (preempt_count())
  3522. return;
  3523. down_read(&mm->mmap_sem);
  3524. vma = find_vma(mm, ip);
  3525. if (vma && vma->vm_file) {
  3526. struct file *f = vma->vm_file;
  3527. char *buf = (char *)__get_free_page(GFP_KERNEL);
  3528. if (buf) {
  3529. char *p, *s;
  3530. p = d_path(&f->f_path, buf, PAGE_SIZE);
  3531. if (IS_ERR(p))
  3532. p = "?";
  3533. s = strrchr(p, '/');
  3534. if (s)
  3535. p = s+1;
  3536. printk("%s%s[%lx+%lx]", prefix, p,
  3537. vma->vm_start,
  3538. vma->vm_end - vma->vm_start);
  3539. free_page((unsigned long)buf);
  3540. }
  3541. }
  3542. up_read(&mm->mmap_sem);
  3543. }
  3544. #ifdef CONFIG_PROVE_LOCKING
  3545. void might_fault(void)
  3546. {
  3547. /*
  3548. * Some code (nfs/sunrpc) uses socket ops on kernel memory while
  3549. * holding the mmap_sem, this is safe because kernel memory doesn't
  3550. * get paged out, therefore we'll never actually fault, and the
  3551. * below annotations will generate false positives.
  3552. */
  3553. if (segment_eq(get_fs(), KERNEL_DS))
  3554. return;
  3555. might_sleep();
  3556. /*
  3557. * it would be nicer only to annotate paths which are not under
  3558. * pagefault_disable, however that requires a larger audit and
  3559. * providing helpers like get_user_atomic.
  3560. */
  3561. if (!in_atomic() && current->mm)
  3562. might_lock_read(&current->mm->mmap_sem);
  3563. }
  3564. EXPORT_SYMBOL(might_fault);
  3565. #endif
  3566. #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLBFS)
  3567. static void clear_gigantic_page(struct page *page,
  3568. unsigned long addr,
  3569. unsigned int pages_per_huge_page)
  3570. {
  3571. int i;
  3572. struct page *p = page;
  3573. might_sleep();
  3574. for (i = 0; i < pages_per_huge_page;
  3575. i++, p = mem_map_next(p, page, i)) {
  3576. cond_resched();
  3577. clear_user_highpage(p, addr + i * PAGE_SIZE);
  3578. }
  3579. }
  3580. void clear_huge_page(struct page *page,
  3581. unsigned long addr, unsigned int pages_per_huge_page)
  3582. {
  3583. int i;
  3584. if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) {
  3585. clear_gigantic_page(page, addr, pages_per_huge_page);
  3586. return;
  3587. }
  3588. might_sleep();
  3589. for (i = 0; i < pages_per_huge_page; i++) {
  3590. cond_resched();
  3591. clear_user_highpage(page + i, addr + i * PAGE_SIZE);
  3592. }
  3593. }
  3594. static void copy_user_gigantic_page(struct page *dst, struct page *src,
  3595. unsigned long addr,
  3596. struct vm_area_struct *vma,
  3597. unsigned int pages_per_huge_page)
  3598. {
  3599. int i;
  3600. struct page *dst_base = dst;
  3601. struct page *src_base = src;
  3602. for (i = 0; i < pages_per_huge_page; ) {
  3603. cond_resched();
  3604. copy_user_highpage(dst, src, addr + i*PAGE_SIZE, vma);
  3605. i++;
  3606. dst = mem_map_next(dst, dst_base, i);
  3607. src = mem_map_next(src, src_base, i);
  3608. }
  3609. }
  3610. void copy_user_huge_page(struct page *dst, struct page *src,
  3611. unsigned long addr, struct vm_area_struct *vma,
  3612. unsigned int pages_per_huge_page)
  3613. {
  3614. int i;
  3615. if (unlikely(pages_per_huge_page > MAX_ORDER_NR_PAGES)) {
  3616. copy_user_gigantic_page(dst, src, addr, vma,
  3617. pages_per_huge_page);
  3618. return;
  3619. }
  3620. might_sleep();
  3621. for (i = 0; i < pages_per_huge_page; i++) {
  3622. cond_resched();
  3623. copy_user_highpage(dst + i, src + i, addr + i*PAGE_SIZE, vma);
  3624. }
  3625. }
  3626. #endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLBFS */