memory.c 107 KB

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