memory.c 108 KB

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