memory.c 114 KB

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