memory.c 128 KB

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