memory.c 114 KB

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