memory.c 111 KB

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