memory.c 108 KB

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