memory.c 102 KB

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