memory.c 127 KB

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