memory.c 108 KB

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