memory.c 108 KB

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