memory.c 107 KB

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