memory.c 105 KB

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